From: amwaterhouse Date: Fri, 9 Jun 2006 16:14:17 +0000 (+0000) Subject: Annotation/feature exporter X-Git-Tag: Release_2_1~372 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=9922c9eec14599f6b141a8db8a3859cab7227ce0;p=jalview.git Annotation/feature exporter --- diff --git a/src/jalview/gui/AnnotationExporter.java b/src/jalview/gui/AnnotationExporter.java new file mode 100755 index 0000000..d4975a6 --- /dev/null +++ b/src/jalview/gui/AnnotationExporter.java @@ -0,0 +1,227 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer + * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +package jalview.gui; + +import javax.swing.*; +import java.awt.event.ActionListener; +import java.awt.event.ActionEvent; + +import jalview.io.*; +import jalview.datamodel.AlignmentAnnotation; + +import java.awt.Color; +import java.awt.FlowLayout; +import javax.swing.BorderFactory; + + +public class AnnotationExporter + extends JPanel +{ + JInternalFrame frame; + AlignmentPanel ap; + boolean features = true; + AlignmentAnnotation [] annotations; + + public AnnotationExporter() + { + try + { + jbInit(); + } + catch (Exception ex) + { + ex.printStackTrace(); + } + + frame = new JInternalFrame(); + frame.setContentPane(this); + frame.setLayer(JLayeredPane.PALETTE_LAYER); + Desktop.addInternalFrame(frame, + "", + 260, 125); + } + + public void exportFeatures(AlignmentPanel ap) + { + this.ap = ap; + features = true; + frame.setTitle("Export Features"); + } + + public void exportAnnotations(AlignmentPanel ap, + AlignmentAnnotation [] annotations) + { + this.ap = ap; + features = false; + GFFFormat.setVisible(false); + this.annotations = annotations; + frame.setTitle("Export Annotations"); + } + + public void toFile_actionPerformed(ActionEvent e) + { + JalviewFileChooser chooser = new JalviewFileChooser( + jalview.bin.Cache.getProperty( "LAST_DIRECTORY")); + + chooser.setFileView(new JalviewFileView()); + chooser.setDialogTitle( + features ? "Save Features to File" : "Save Annotation to File"); + chooser.setToolTipText("Save"); + + int value = chooser.showSaveDialog(this); + + if (value == JalviewFileChooser.APPROVE_OPTION) + { + String text = "No features found on alignment"; + if (features) + { + if (GFFFormat.isSelected()) + text = new FeaturesFile().printGFFFormat( + ap.av.alignment.getDataset().getSequencesArray(), + ap.av.featuresDisplayed); + else + text = new FeaturesFile().printJalviewFormat( + ap.av.alignment.getDataset().getSequencesArray(), + ap.av.featuresDisplayed); + } + else + { + text = new AnnotationFile().printAnnotations( annotations ); + } + + try + { + java.io.PrintWriter out = new java.io.PrintWriter( + new java.io.FileWriter(chooser.getSelectedFile())); + + out.print(text); + out.close(); + } + catch (Exception ex) + { + ex.printStackTrace(); + } + } + } + + public void toTextbox_actionPerformed(ActionEvent e) + { + String text = "No features found on alignment"; + if(features) + { + if (GFFFormat.isSelected()) + text = new FeaturesFile().printGFFFormat( + ap.av.alignment.getDataset().getSequencesArray(), + ap.av.featuresDisplayed); + else + text = new FeaturesFile().printJalviewFormat( + ap.av.alignment.getDataset().getSequencesArray(), + ap.av.featuresDisplayed); + } + else if(!features) + { + text = new AnnotationFile().printAnnotations( annotations ); + } + + + CutAndPasteTransfer cap = new CutAndPasteTransfer(); + cap.setText(text); + Desktop.addInternalFrame(cap, + (features ? "Features for - " : "Annotations for - ") + + ap.alignFrame.getTitle(), + 600, + 500); + + + } + + public void close_actionPerformed(ActionEvent e) + { + try{ + frame.setClosed(true); + }catch(java.beans.PropertyVetoException ex) + {} + } + + + + private void jbInit() + throws Exception + { + this.setLayout(flowLayout1); + toFile.setText("to File"); + toFile.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + toFile_actionPerformed(e); + } + }); + toTextbox.setText("to Textbox"); + toTextbox.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + toTextbox_actionPerformed(e); + } + }); + close.setText("Close"); + close.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + close_actionPerformed(e); + } + }); + jalviewFormat.setOpaque(false); + jalviewFormat.setSelected(true); + jalviewFormat.setText("Jalview"); + GFFFormat.setOpaque(false); + GFFFormat.setText("GFF"); + jLabel1.setHorizontalAlignment(SwingConstants.TRAILING); + jLabel1.setText("Format: "); + this.setBackground(Color.white); + jPanel3.setBorder(BorderFactory.createEtchedBorder()); + jPanel3.setOpaque(false); + jPanel1.setOpaque(false); + jPanel1.add(toFile); + jPanel1.add(toTextbox); + jPanel1.add(close); + jPanel3.add(jLabel1); + jPanel3.add(jalviewFormat); + jPanel3.add(GFFFormat); + buttonGroup.add(jalviewFormat); + buttonGroup.add(GFFFormat); + this.add(jPanel3, null); + this.add(jPanel1, null); + } + + JPanel jPanel1 = new JPanel(); + JButton toFile = new JButton(); + JButton toTextbox = new JButton(); + JButton close = new JButton(); + ButtonGroup buttonGroup = new ButtonGroup(); + JRadioButton jalviewFormat = new JRadioButton(); + JRadioButton GFFFormat = new JRadioButton(); + JLabel jLabel1 = new JLabel(); + JPanel jPanel3 = new JPanel(); + FlowLayout flowLayout1 = new FlowLayout(); + +} diff --git a/src/jalview/io/AnnotationFile.java b/src/jalview/io/AnnotationFile.java new file mode 100755 index 0000000..e427605 --- /dev/null +++ b/src/jalview/io/AnnotationFile.java @@ -0,0 +1,420 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer + * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +package jalview.io; + +import java.io.*; +import jalview.datamodel.*; +import java.util.*; +import jalview.schemes.UserColourScheme; +import java.net.URL; + + +public class AnnotationFile +{ + + public String printAnnotations(AlignmentAnnotation [] annotations) + { + StringBuffer text = new StringBuffer( + "JALVIEW_ANNOTATION\n" + +"# Created: " + +new java.util.Date()+"\n\n"); + + AlignmentAnnotation row; + String comma; + String seqref = null; + + StringBuffer colours = new StringBuffer(); + StringBuffer graphLine = new StringBuffer(); + + Hashtable graphGroup = new Hashtable(); + + java.awt.Color color; + + for(int i=0; i-1) + { + String key = String.valueOf(row.graphGroup); + if(graphGroup.containsKey(key)) + graphGroup.put(key, graphGroup.get(key) + +"\t"+row.label); + else + graphGroup.put(key, row.label); + } + } + + text.append(row.label+"\t"); + + for(int j=0; j 0 + && !row.annotations[j].displayCharacter.equals(" ")) + { + text.append(row.annotations[j].displayCharacter); + comma = ","; + } + if (row.annotations[j].secondaryStructure!=' ') + { + text.append(comma + row.annotations[j].secondaryStructure); + comma = ","; + } + if (row.annotations[j].value!=0f) + { + color = row.annotations[j].colour; + text.append(comma + row.annotations[j].value); + } + } + text.append("|"); + } + + text.append("\n"); + + if(color!=null && color!=java.awt.Color.black) + { + colours.append("COLOUR\t" + +row.label+"\t" + +jalview.util.Format.getHexString(color)+"\n"); + } + + } + + text.append("\n"); + + text.append(colours.toString()); + text.append(graphLine.toString()); + if(graphGroup.size()>0) + { + text.append("COMBINE\t"); + Enumeration en = graphGroup.elements(); + while(en.hasMoreElements()) + { + text.append(en.nextElement()+"\n"); + } + } + + return text.toString(); + } + + public boolean readAnnotationFile(AlignmentI al, String file) + { + try + { + BufferedReader in = null; + java.io.InputStream is = getClass().getResourceAsStream("/" + file); + if (is != null) + { + in = new BufferedReader(new java.io.InputStreamReader(is)); + } + else + { + try + { + URL url = new URL(file); + in = new BufferedReader(new InputStreamReader(url.openStream())); + } + catch (java.net.MalformedURLException ex) + { + in = new BufferedReader(new FileReader(file)); + } + } + + String line, label, description, token; + int graphStyle, index; + SequenceI refSeq = null; + int refSeqIndex = 1; + int existingAnnotations = 0; + if(al.getAlignmentAnnotation()!=null) + existingAnnotations = al.getAlignmentAnnotation().length; + + int alWidth = al.getWidth(); + + StringTokenizer st; + Annotation[] annotations; + AlignmentAnnotation annotation = null; + + // First confirm this is an Annotation file + boolean jvAnnotationFile = false; + while ( (line = in.readLine()) != null) + { + if (line.indexOf("#") == 0 ) + continue; + + if (line.indexOf("JALVIEW_ANNOTATION") > -1) + { + jvAnnotationFile = true; + break; + } + } + + if(!jvAnnotationFile) + { + in.close(); + return false; + } + + while ( (line = in.readLine()) != null) + { + if(line.indexOf("#")==0 + || line.indexOf("JALVIEW_ANNOTATION")>-1 + || line.length()==0) + continue; + + st = new StringTokenizer(line, "\t"); + token = st.nextToken(); + if(token.equalsIgnoreCase("COLOUR")) + { + colourAnnotations(al, st.nextToken(), st.nextToken()); + continue; + } + + if(token.equalsIgnoreCase("COMBINE") ) + { + combineAnnotations(al, st); + continue; + } + + if (token.equalsIgnoreCase("GRAPHLINE")) + { + addLine(al, st); + continue; + } + + + if(token.equalsIgnoreCase("SEQUENCE_REF") ) + { + refSeq = al.findName(st.nextToken()); + try{ + refSeqIndex = Integer.parseInt(st.nextToken()); + } + catch(Exception ex) + { + refSeqIndex = 1; + } + + continue; + } + + + graphStyle = AlignmentAnnotation.getGraphValueFromString(token); + label = description = st.nextToken(); + + line = st.nextToken(); + + st = new StringTokenizer(line, "|", true); + annotations = new Annotation[alWidth]; + + index = 0; + boolean emptyColumn = true; + + + while (st.hasMoreElements() && index1 && st.countTokens() < 4 ) + { + GFFFile = false; + type = st.nextToken(); + if (type.equalsIgnoreCase("startgroup")) + { + featureGroup = st.nextToken(); + } + else if (type.equalsIgnoreCase("endgroup")) + { + //We should check whether this is the current group, + //but at present theres no way of showing more than 1 group + st.nextToken(); + featureGroup = null; + } + else + { + UserColourScheme ucs = new UserColourScheme(st.nextToken()); + colours.put(type, ucs.findColour("A")); + } + continue; + } + + while (st.hasMoreElements()) + { + + if(GFFFile) + { + // Still possible this is an old Jalview file, + // which does not have type colours at the beginning + token = st.nextToken(); + seq = align.findName(token); + if(seq != null) + { + desc = st.nextToken(); + type = st.nextToken(); + start = Integer.parseInt(st.nextToken()); + end = Integer.parseInt(st.nextToken()); + try + { + score = Float.parseFloat(st.nextToken()); + } + catch (NumberFormatException ex) + { + score = 0; + } + + sf = new SequenceFeature(type, desc, start, end, score, null); + + try + { + sf.setValue("STRAND", st.nextToken()); + sf.setValue("FRAME", st.nextToken()); + } + catch (Exception ex) + {} + + seq.getDatasetSequence().addSequenceFeature(sf); + + break; + } + } + + if(GFFFile && seq==null) + { + desc = token; + } + else + desc = st.nextToken(); + + + token = st.nextToken(); + if (!token.equals("ID_NOT_SPECIFIED")) + { + seq = align.findName(token); + st.nextToken(); + } + else + { + try{ + index = Integer.parseInt(st.nextToken()); + seq = align.getSequenceAt(index); + } + catch(NumberFormatException ex) + { + seq = null; + } + } + + if(seq==null) + { + System.out.println("Sequence not found: "+line); + break; + } + + start = Integer.parseInt(st.nextToken()); + end = Integer.parseInt(st.nextToken()); + + type = st.nextToken(); + + if (!colours.containsKey(type)) + { + // Probably the old style groups file + UserColourScheme ucs = new UserColourScheme(type); + colours.put(type, ucs.findColour("A")); + } + + sf = new SequenceFeature(type, desc, "", start, end, featureGroup); + + seq.getDatasetSequence().addSequenceFeature(sf); + + //If we got here, its not a GFFFile + GFFFile = false; + } + } + } + catch (Exception ex) + { + System.out.println(line); + ex.printStackTrace(); + System.out.println("Error parsing groups file: " + ex +"\n"+line); + return false; + } + + return true; + + } + + + /** + * DOCUMENT ME! + * + * @param s DOCUMENT ME! + * @param len DOCUMENT ME! + * @param gaps DOCUMENT ME! + * @param displayId DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public String printJalviewFormat(SequenceI [] seqs, + Hashtable visible) + { + StringBuffer out = new StringBuffer(); + SequenceFeature [] next; + + if(visible==null || visible.size()<1) + return "No Features Visible"; + + Enumeration en = visible.keys(); + String type; + int color; + while( en.hasMoreElements() ) + { + type = en.nextElement().toString(); + color = Integer.parseInt( visible.get(type).toString() ); + out.append(type + "\t" + + jalview.util.Format.getHexString( + new java.awt.Color(color) ) + +"\n"); + } + + //Work out which groups are both present and visible + Vector groups = new Vector(); + int groupIndex = 0; + + for(int i=0; i 0) + { + group = groups.elementAt(groupIndex).toString(); + out.append("\nSTARTGROUP\t" + group + "\n"); + } + + + for (int i = 0; i < seqs.length; i++) + { + next = seqs[i].getSequenceFeatures(); + if (next != null) + { + for (int j = 0; j < next.length; j++) + { + if (!visible.containsKey(next[j].type)) + continue; + + if (group != null && !next[j].featureGroup.equals(group)) + continue; + + if(next[j].description==null || next[j].description.equals("")) + out.append(next[j].type+"\t"); + else + out.append(next[j].description + "\t"); + + out.append( seqs[i].getName() + "\t-1\t" + + next[j].begin + "\t" + + next[j].end + "\t" + + next[j].type + "\n" + ); + } + } + } + + if(groups.size()>0) + { + out.append("ENDGROUP\t"+group+"\n"); + } + + groupIndex++; + } + while(groupIndex < groups.size()); + + + return out.toString(); + } + + public String printGFFFormat(SequenceI [] seqs, Hashtable visible) + { + StringBuffer out = new StringBuffer(); + SequenceFeature [] next; + + for(int i=0; i