Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.gui

File AnnotationExporter.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
56% of files have more coverage

Code metrics

18
97
15
1
360
258
28
0.29
6.47
15
1.87

Classes

Class Line # Actions
AnnotationExporter 62 97 28 130
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
20    */
21    package jalview.gui;
22   
23    import jalview.api.FeatureColourI;
24    import jalview.bin.Cache;
25    import jalview.datamodel.AlignmentAnnotation;
26    import jalview.datamodel.SequenceI;
27    import jalview.datamodel.features.FeatureMatcherSetI;
28    import jalview.io.AnnotationFile;
29    import jalview.io.FeaturesFile;
30    import jalview.io.JalviewFileChooser;
31    import jalview.io.JalviewFileView;
32    import jalview.util.MessageManager;
33   
34    import java.awt.BorderLayout;
35    import java.awt.Color;
36    import java.awt.FlowLayout;
37    import java.awt.event.ActionEvent;
38    import java.awt.event.ActionListener;
39    import java.io.FileWriter;
40    import java.io.PrintWriter;
41    import java.util.List;
42    import java.util.Map;
43   
44    import javax.swing.BorderFactory;
45    import javax.swing.ButtonGroup;
46    import javax.swing.JButton;
47    import javax.swing.JInternalFrame;
48    import javax.swing.JLabel;
49    import javax.swing.JLayeredPane;
50    import javax.swing.JPanel;
51    import javax.swing.JRadioButton;
52    import javax.swing.SwingConstants;
53   
54    /**
55    *
56    * GUI dialog for exporting features or alignment annotations depending upon
57    * which method is called.
58    *
59    * @author AMW
60    *
61    */
 
62    public class AnnotationExporter extends JPanel
63    {
64    private JInternalFrame frame;
65   
66    private AlignmentPanel ap;
67   
68    /*
69    * true if exporting features, false if exporting annotations
70    */
71    private boolean exportFeatures = true;
72   
73    private AlignmentAnnotation[] annotations;
74   
75    private boolean wholeView;
76   
 
77  0 toggle public AnnotationExporter(AlignmentPanel panel)
78    {
79  0 this.ap = panel;
80  0 try
81    {
82  0 jbInit();
83    } catch (Exception ex)
84    {
85  0 ex.printStackTrace();
86    }
87   
88  0 frame = new JInternalFrame();
89  0 frame.setContentPane(this);
90  0 frame.setLayer(JLayeredPane.PALETTE_LAYER);
91  0 Desktop.addInternalFrame(frame, "", frame.getPreferredSize().width,
92    frame.getPreferredSize().height);
93    }
94   
95    /**
96    * Configures the diglog for options to export visible features
97    */
 
98  0 toggle public void exportFeatures()
99    {
100  0 exportFeatures = true;
101  0 CSVFormat.setVisible(false);
102  0 frame.setTitle(MessageManager.getString("label.export_features"));
103    }
104   
105    /**
106    * Configures the dialog for options to export all visible annotations
107    */
 
108  0 toggle public void exportAnnotations()
109    {
110  0 boolean showAnnotation = ap.av.isShowAnnotation();
111  0 exportAnnotation(showAnnotation ? null
112    : ap.av.getAlignment().getAlignmentAnnotation(), true);
113    }
114   
115    /**
116    * Configures the dialog for options to export the given annotation row
117    *
118    * @param toExport
119    */
 
120  0 toggle public void exportAnnotation(AlignmentAnnotation toExport)
121    {
122  0 exportAnnotation(new AlignmentAnnotation[] { toExport }, false);
123    }
124   
 
125  0 toggle private void exportAnnotation(AlignmentAnnotation[] toExport,
126    boolean forWholeView)
127    {
128  0 wholeView = forWholeView;
129  0 annotations = toExport;
130  0 exportFeatures = false;
131  0 GFFFormat.setVisible(false);
132  0 CSVFormat.setVisible(true);
133  0 frame.setTitle(MessageManager.getString("label.export_annotations"));
134    }
135   
 
136  0 toggle private void toFile_actionPerformed()
137    {
138    // TODO: JAL-3048 JalviewFileChooser - Save option
139  0 JalviewFileChooser chooser = new JalviewFileChooser(
140    Cache.getProperty("LAST_DIRECTORY"));
141   
142  0 chooser.setFileView(new JalviewFileView());
143  0 chooser.setDialogTitle(exportFeatures
144    ? MessageManager.getString("label.save_features_to_file")
145    : MessageManager.getString("label.save_annotation_to_file"));
146  0 chooser.setToolTipText(MessageManager.getString("action.save"));
147   
148  0 int value = chooser.showSaveDialog(this);
149   
150  0 if (value == JalviewFileChooser.APPROVE_OPTION)
151    {
152  0 String text = getText();
153   
154  0 try
155    {
156  0 PrintWriter out = new PrintWriter(
157    new FileWriter(chooser.getSelectedFile()));
158  0 out.print(text);
159  0 out.close();
160    } catch (Exception ex)
161    {
162  0 ex.printStackTrace();
163    }
164    }
165   
166  0 close_actionPerformed();
167    }
168   
169    /**
170    * Answers the text to output for either Features (in GFF or Jalview format) or
171    * Annotations (in CSV or Jalview format)
172    *
173    * @return
174    */
 
175  0 toggle private String getText()
176    {
177  0 return exportFeatures ? getFeaturesText() : getAnnotationsText();
178    }
179   
180    /**
181    * Returns the text contents for output of annotations in either CSV or Jalview
182    * format
183    *
184    * @return
185    */
 
186  0 toggle private String getAnnotationsText()
187    {
188  0 String text;
189  0 if (CSVFormat.isSelected())
190    {
191  0 text = new AnnotationFile().printCSVAnnotations(annotations);
192    }
193    else
194    {
195  0 if (wholeView)
196    {
197  0 text = new AnnotationFile().printAnnotationsForView(ap.av);
198    }
199    else
200    {
201  0 text = new AnnotationFile().printAnnotations(annotations, null,
202    null);
203    }
204    }
205  0 return text;
206    }
207   
208    /**
209    * Returns the text contents for output of features in either GFF or Jalview
210    * format
211    *
212    * @return
213    */
 
214  0 toggle private String getFeaturesText()
215    {
216  0 String text;
217  0 SequenceI[] sequences = ap.av.getAlignment().getSequencesArray();
218  0 Map<String, FeatureColourI> featureColours = ap.getFeatureRenderer()
219    .getDisplayedFeatureCols();
220  0 Map<String, FeatureMatcherSetI> featureFilters = ap.getFeatureRenderer()
221    .getFeatureFilters();
222  0 List<String> featureGroups = ap.getFeatureRenderer()
223    .getDisplayedFeatureGroups();
224  0 boolean includeNonPositional = ap.av.isShowNPFeats();
225   
226  0 FeaturesFile formatter = new FeaturesFile();
227  0 if (GFFFormat.isSelected())
228    {
229  0 text = formatter.printGffFormat(sequences, featureColours,
230    featureGroups, includeNonPositional);
231    }
232    else
233    {
234  0 text = formatter.printJalviewFormat(sequences, featureColours,
235    featureFilters, featureGroups, includeNonPositional);
236    }
237  0 return text;
238    }
239   
 
240  0 toggle private void toTextbox_actionPerformed()
241    {
242  0 CutAndPasteTransfer cap = new CutAndPasteTransfer();
243   
244  0 try
245    {
246  0 String text = getText();
247  0 cap.setText(text);
248  0 Desktop.addInternalFrame(cap, (exportFeatures ? MessageManager
249    .formatMessage("label.features_for_params", new String[]
250    { ap.alignFrame.getTitle() })
251    : MessageManager.formatMessage("label.annotations_for_params",
252    new String[]
253    { ap.alignFrame.getTitle() })),
254    600, 500);
255    } catch (OutOfMemoryError oom)
256    {
257  0 new OOMWarning((exportFeatures ? MessageManager.formatMessage(
258    "label.generating_features_for_params", new String[]
259    { ap.alignFrame.getTitle() })
260    : MessageManager.formatMessage(
261    "label.generating_annotations_for_params",
262    new String[]
263    { ap.alignFrame.getTitle() })),
264    oom);
265  0 cap.dispose();
266    }
267   
268  0 close_actionPerformed();
269    }
270   
 
271  0 toggle private void close_actionPerformed()
272    {
273  0 try
274    {
275  0 frame.setClosed(true);
276    } catch (java.beans.PropertyVetoException ex)
277    {
278    }
279    }
280   
 
281  0 toggle private void jbInit() throws Exception
282    {
283  0 this.setLayout(new BorderLayout());
284   
285  0 toFile.setText(MessageManager.getString("label.to_file"));
286  0 toFile.addActionListener(new ActionListener()
287    {
 
288  0 toggle @Override
289    public void actionPerformed(ActionEvent e)
290    {
291  0 toFile_actionPerformed();
292    }
293    });
294  0 toTextbox.setText(MessageManager.getString("label.to_textbox"));
295  0 toTextbox.addActionListener(new ActionListener()
296    {
 
297  0 toggle @Override
298    public void actionPerformed(ActionEvent e)
299    {
300  0 toTextbox_actionPerformed();
301    }
302    });
303  0 close.setText(MessageManager.getString("action.close"));
304  0 close.addActionListener(new ActionListener()
305    {
 
306  0 toggle @Override
307    public void actionPerformed(ActionEvent e)
308    {
309  0 close_actionPerformed();
310    }
311    });
312  0 jalviewFormat.setOpaque(false);
313  0 jalviewFormat.setSelected(true);
314  0 jalviewFormat.setText("Jalview");
315  0 GFFFormat.setOpaque(false);
316  0 GFFFormat.setText("GFF");
317  0 CSVFormat.setOpaque(false);
318  0 CSVFormat.setText(MessageManager.getString("label.csv_spreadsheet"));
319  0 jLabel1.setHorizontalAlignment(SwingConstants.TRAILING);
320  0 jLabel1.setText(MessageManager.getString("action.format") + " ");
321  0 this.setBackground(Color.white);
322  0 jPanel3.setBorder(BorderFactory.createEtchedBorder());
323  0 jPanel3.setOpaque(false);
324  0 jPanel1.setOpaque(false);
325  0 jPanel1.add(toFile);
326  0 jPanel1.add(toTextbox);
327  0 jPanel1.add(close);
328  0 jPanel3.add(jLabel1);
329  0 jPanel3.add(jalviewFormat);
330  0 jPanel3.add(GFFFormat);
331  0 jPanel3.add(CSVFormat);
332  0 buttonGroup.add(jalviewFormat);
333  0 buttonGroup.add(GFFFormat);
334  0 buttonGroup.add(CSVFormat);
335  0 this.add(jPanel3, BorderLayout.CENTER);
336  0 this.add(jPanel1, BorderLayout.SOUTH);
337    }
338   
339    JPanel jPanel1 = new JPanel();
340   
341    JButton toFile = new JButton();
342   
343    JButton toTextbox = new JButton();
344   
345    JButton close = new JButton();
346   
347    ButtonGroup buttonGroup = new ButtonGroup();
348   
349    JRadioButton jalviewFormat = new JRadioButton();
350   
351    JRadioButton GFFFormat = new JRadioButton();
352   
353    JRadioButton CSVFormat = new JRadioButton();
354   
355    JLabel jLabel1 = new JLabel();
356   
357    JPanel jPanel3 = new JPanel();
358   
359    FlowLayout flowLayout1 = new FlowLayout();
360    }