2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
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;
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;
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;
56 * GUI dialog for exporting features or alignment annotations depending upon
57 * which method is called.
62 public class AnnotationExporter extends JPanel
64 private JInternalFrame frame;
66 private AlignmentPanel ap;
69 * true if exporting features, false if exporting annotations
71 private boolean exportFeatures = true;
73 private AlignmentAnnotation[] annotations;
75 private boolean wholeView;
77 public AnnotationExporter(AlignmentPanel panel)
83 } catch (Exception ex)
88 frame = new JInternalFrame();
89 frame.setContentPane(this);
90 frame.setLayer(JLayeredPane.PALETTE_LAYER);
91 Desktop.addInternalFrame(frame, "", frame.getPreferredSize().width,
92 frame.getPreferredSize().height);
96 * Configures the diglog for options to export visible features
98 public void exportFeatures()
100 exportFeatures = true;
101 CSVFormat.setVisible(false);
102 frame.setTitle(MessageManager.getString("label.export_features"));
106 * Configures the dialog for options to export all visible annotations
108 public void exportAnnotations()
110 boolean showAnnotation = ap.av.isShowAnnotation();
111 exportAnnotation(showAnnotation ? null
112 : ap.av.getAlignment().getAlignmentAnnotation(), true);
116 * Configures the dialog for options to export the given annotation row
120 public void exportAnnotation(AlignmentAnnotation toExport)
122 exportAnnotation(new AlignmentAnnotation[] { toExport }, false);
125 private void exportAnnotation(AlignmentAnnotation[] toExport,
126 boolean forWholeView)
128 wholeView = forWholeView;
129 annotations = toExport;
130 exportFeatures = false;
131 GFFFormat.setVisible(false);
132 CSVFormat.setVisible(true);
133 frame.setTitle(MessageManager.getString("label.export_annotations"));
136 private void toFile_actionPerformed()
138 JalviewFileChooser chooser = new JalviewFileChooser(
139 Cache.getProperty("LAST_DIRECTORY"));
141 chooser.setFileView(new JalviewFileView());
142 chooser.setDialogTitle(exportFeatures
143 ? MessageManager.getString("label.save_features_to_file")
144 : MessageManager.getString("label.save_annotation_to_file"));
145 chooser.setToolTipText(MessageManager.getString("action.save"));
147 int value = chooser.showSaveDialog(this);
149 if (value == JalviewFileChooser.APPROVE_OPTION)
151 String text = getText();
155 PrintWriter out = new PrintWriter(
156 new FileWriter(chooser.getSelectedFile()));
159 } catch (Exception ex)
161 ex.printStackTrace();
165 close_actionPerformed();
169 * Answers the text to output for either Features (in GFF or Jalview format) or
170 * Annotations (in CSV or Jalview format)
174 private String getText()
176 return exportFeatures ? getFeaturesText() : getAnnotationsText();
180 * Returns the text contents for output of annotations in either CSV or Jalview
185 private String getAnnotationsText()
188 if (CSVFormat.isSelected())
190 text = new AnnotationFile().printCSVAnnotations(annotations);
196 text = new AnnotationFile().printAnnotationsForView(ap.av);
200 text = new AnnotationFile().printAnnotations(annotations, null,
208 * Returns the text contents for output of features in either GFF or Jalview
213 private String getFeaturesText()
216 SequenceI[] sequences = ap.av.getAlignment().getSequencesArray();
217 Map<String, FeatureColourI> featureColours = ap.getFeatureRenderer()
218 .getDisplayedFeatureCols();
219 Map<String, FeatureMatcherSetI> featureFilters = ap.getFeatureRenderer()
220 .getFeatureFilters();
221 List<String> featureGroups = ap.getFeatureRenderer()
222 .getDisplayedFeatureGroups();
223 boolean includeNonPositional = ap.av.isShowNPFeats();
225 FeaturesFile formatter = new FeaturesFile();
226 if (GFFFormat.isSelected())
228 text = formatter.printGffFormat(sequences, featureColours,
229 featureGroups, includeNonPositional);
233 text = formatter.printJalviewFormat(sequences, featureColours,
234 featureFilters, featureGroups, includeNonPositional);
239 private void toTextbox_actionPerformed()
241 CutAndPasteTransfer cap = new CutAndPasteTransfer();
245 String text = getText();
247 Desktop.addInternalFrame(cap, (exportFeatures ? MessageManager
248 .formatMessage("label.features_for_params", new String[]
249 { ap.alignFrame.getTitle() })
250 : MessageManager.formatMessage("label.annotations_for_params",
252 { ap.alignFrame.getTitle() })),
254 } catch (OutOfMemoryError oom)
256 new OOMWarning((exportFeatures ? MessageManager.formatMessage(
257 "label.generating_features_for_params", new String[]
258 { ap.alignFrame.getTitle() })
259 : MessageManager.formatMessage(
260 "label.generating_annotations_for_params",
262 { ap.alignFrame.getTitle() })),
267 close_actionPerformed();
270 private void close_actionPerformed()
274 frame.setClosed(true);
275 } catch (java.beans.PropertyVetoException ex)
280 private void jbInit() throws Exception
282 this.setLayout(new BorderLayout());
284 toFile.setText(MessageManager.getString("label.to_file"));
285 toFile.addActionListener(new ActionListener()
288 public void actionPerformed(ActionEvent e)
290 toFile_actionPerformed();
293 toTextbox.setText(MessageManager.getString("label.to_textbox"));
294 toTextbox.addActionListener(new ActionListener()
297 public void actionPerformed(ActionEvent e)
299 toTextbox_actionPerformed();
302 close.setText(MessageManager.getString("action.close"));
303 close.addActionListener(new ActionListener()
306 public void actionPerformed(ActionEvent e)
308 close_actionPerformed();
311 jalviewFormat.setOpaque(false);
312 jalviewFormat.setSelected(true);
313 jalviewFormat.setText("Jalview");
314 GFFFormat.setOpaque(false);
315 GFFFormat.setText("GFF");
316 CSVFormat.setOpaque(false);
317 CSVFormat.setText(MessageManager.getString("label.csv_spreadsheet"));
318 jLabel1.setHorizontalAlignment(SwingConstants.TRAILING);
319 jLabel1.setText(MessageManager.getString("action.format") + " ");
320 this.setBackground(Color.white);
321 jPanel3.setBorder(BorderFactory.createEtchedBorder());
322 jPanel3.setOpaque(false);
323 jPanel1.setOpaque(false);
325 jPanel1.add(toTextbox);
327 jPanel3.add(jLabel1);
328 jPanel3.add(jalviewFormat);
329 jPanel3.add(GFFFormat);
330 jPanel3.add(CSVFormat);
331 buttonGroup.add(jalviewFormat);
332 buttonGroup.add(GFFFormat);
333 buttonGroup.add(CSVFormat);
334 this.add(jPanel3, BorderLayout.CENTER);
335 this.add(jPanel1, BorderLayout.SOUTH);
338 JPanel jPanel1 = new JPanel();
340 JButton toFile = new JButton();
342 JButton toTextbox = new JButton();
344 JButton close = new JButton();
346 ButtonGroup buttonGroup = new ButtonGroup();
348 JRadioButton jalviewFormat = new JRadioButton();
350 JRadioButton GFFFormat = new JRadioButton();
352 JRadioButton CSVFormat = new JRadioButton();
354 JLabel jLabel1 = new JLabel();
356 JPanel jPanel3 = new JPanel();
358 FlowLayout flowLayout1 = new FlowLayout();