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.FeatureRenderer;
24 import jalview.bin.Cache;
25 import jalview.datamodel.AlignmentAnnotation;
26 import jalview.datamodel.SequenceI;
27 import jalview.io.AnnotationFile;
28 import jalview.io.FeaturesFile;
29 import jalview.io.JalviewFileChooser;
30 import jalview.io.JalviewFileView;
31 import jalview.util.MessageManager;
33 import java.awt.Color;
34 import java.awt.Dimension;
35 import java.awt.event.ActionEvent;
36 import java.awt.event.ActionListener;
37 import java.io.FileWriter;
38 import java.io.PrintWriter;
40 import javax.swing.BoxLayout;
41 import javax.swing.ButtonGroup;
42 import javax.swing.JButton;
43 import javax.swing.JCheckBox;
44 import javax.swing.JInternalFrame;
45 import javax.swing.JLabel;
46 import javax.swing.JLayeredPane;
47 import javax.swing.JPanel;
48 import javax.swing.JRadioButton;
49 import javax.swing.SwingConstants;
53 * GUI dialog for exporting features or alignment annotations depending upon
54 * which method is called.
59 public class AnnotationExporter extends JPanel
61 private JInternalFrame frame;
63 private AlignmentPanel ap;
66 * true if exporting features, false if exporting annotations
68 private boolean exportFeatures = true;
70 private AlignmentAnnotation[] annotations;
72 private boolean wholeView;
75 * option to export linked (CDS/peptide) features when shown
76 * on the alignment, converted to this alignment's coordinates
78 private JCheckBox includeLinkedFeatures;
81 * output format option shown for feature export
83 JRadioButton GFFFormat = new JRadioButton();
86 * output format option shown for annotation export
88 JRadioButton CSVFormat = new JRadioButton();
90 private JPanel linkedFeaturesPanel;
97 public AnnotationExporter(AlignmentPanel panel)
103 } catch (Exception ex)
105 ex.printStackTrace();
108 frame = new JInternalFrame();
109 frame.setContentPane(this);
110 frame.setLayer(JLayeredPane.PALETTE_LAYER);
111 Dimension preferredSize = frame.getPreferredSize();
112 Desktop.addInternalFrame(frame, "", true, preferredSize.width,
113 preferredSize.height, true, true);
117 * Configures the dialog for options to export visible features. If from a split
118 * frame panel showing linked features, make the option to include these in the
121 public void exportFeatures()
123 exportFeatures = true;
124 CSVFormat.setVisible(false);
125 if (ap.av.isShowComplementFeatures())
127 linkedFeaturesPanel.setVisible(true);
130 frame.setTitle(MessageManager.getString("label.export_features"));
134 * Configures the dialog for options to export all visible annotations
136 public void exportAnnotations()
138 boolean showAnnotation = ap.av.isShowAnnotation();
139 exportAnnotation(showAnnotation ? null
140 : ap.av.getAlignment().getAlignmentAnnotation(), true);
144 * Configures the dialog for options to export the given annotation row
148 public void exportAnnotation(AlignmentAnnotation toExport)
150 exportAnnotation(new AlignmentAnnotation[] { toExport }, false);
153 private void exportAnnotation(AlignmentAnnotation[] toExport,
154 boolean forWholeView)
156 wholeView = forWholeView;
157 annotations = toExport;
158 exportFeatures = false;
159 GFFFormat.setVisible(false);
160 CSVFormat.setVisible(true);
161 frame.setTitle(MessageManager.getString("label.export_annotations"));
164 private void toFile_actionPerformed()
166 // TODO: JAL-3048 JalviewFileChooser - Save option
167 JalviewFileChooser chooser = new JalviewFileChooser(
168 Cache.getProperty("LAST_DIRECTORY"));
170 chooser.setFileView(new JalviewFileView());
171 chooser.setDialogTitle(exportFeatures
172 ? MessageManager.getString("label.save_features_to_file")
173 : MessageManager.getString("label.save_annotation_to_file"));
174 chooser.setToolTipText(MessageManager.getString("action.save"));
176 int value = chooser.showSaveDialog(this);
178 if (value == JalviewFileChooser.APPROVE_OPTION)
180 String text = getText();
184 PrintWriter out = new PrintWriter(
185 new FileWriter(chooser.getSelectedFile()));
188 } catch (Exception ex)
190 ex.printStackTrace();
194 close_actionPerformed();
198 * Answers the text to output for either Features (in GFF or Jalview format) or
199 * Annotations (in CSV or Jalview format)
203 private String getText()
205 return exportFeatures ? getFeaturesText() : getAnnotationsText();
209 * Returns the text contents for output of annotations in either CSV or Jalview
214 private String getAnnotationsText()
217 if (CSVFormat.isSelected())
219 text = new AnnotationFile().printCSVAnnotations(annotations);
225 text = new AnnotationFile().printAnnotationsForView(ap.av);
229 text = new AnnotationFile().printAnnotations(annotations, null,
237 * Returns the text contents for output of features in either GFF or Jalview
242 private String getFeaturesText()
245 SequenceI[] sequences = ap.av.getAlignment().getSequencesArray();
246 boolean includeNonPositional = ap.av.isShowNPFeats();
248 FeaturesFile formatter = new FeaturesFile();
249 final FeatureRenderer fr = ap.getFeatureRenderer();
250 boolean includeComplement = includeLinkedFeatures.isSelected();
252 if (GFFFormat.isSelected())
254 text = formatter.printGffFormat(sequences, fr, includeNonPositional,
259 text = formatter.printJalviewFormat(sequences, fr,
260 includeNonPositional, includeComplement);
265 private void toTextbox_actionPerformed()
267 CutAndPasteTransfer cap = new CutAndPasteTransfer();
271 String text = getText();
273 Desktop.addInternalFrame(cap, (exportFeatures ? MessageManager
274 .formatMessage("label.features_for_params", new String[]
275 { ap.alignFrame.getTitle() })
276 : MessageManager.formatMessage("label.annotations_for_params",
278 { ap.alignFrame.getTitle() })),
280 } catch (OutOfMemoryError oom)
282 new OOMWarning((exportFeatures ? MessageManager.formatMessage(
283 "label.generating_features_for_params", new String[]
284 { ap.alignFrame.getTitle() })
285 : MessageManager.formatMessage(
286 "label.generating_annotations_for_params",
288 { ap.alignFrame.getTitle() })),
293 close_actionPerformed();
296 private void close_actionPerformed()
300 frame.setClosed(true);
301 } catch (java.beans.PropertyVetoException ex)
307 * Adds widgets to the panel
311 private void jbInit() throws Exception
313 this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
314 this.setBackground(Color.white);
316 JPanel formatPanel = buildFormatOptionsPanel();
317 JPanel linkedFeatures = buildLinkedFeaturesPanel();
318 JPanel actionsPanel = buildActionsPanel();
320 this.add(formatPanel);
321 this.add(linkedFeatures);
322 this.add(actionsPanel);
326 * Builds a panel with a checkbox for the option to export linked (CDS/peptide)
327 * features. This is hidden by default, and only made visible if exporting
328 * features from a split frame panel which is configured to show linked
333 private JPanel buildLinkedFeaturesPanel()
335 linkedFeaturesPanel = new JPanel();
336 linkedFeaturesPanel.setOpaque(false);
338 boolean nucleotide = ap.av.isNucleotide();
339 String complement = nucleotide
340 ? MessageManager.getString("label.protein").toLowerCase()
342 JLabel label = new JLabel(
343 MessageManager.formatMessage("label.include_linked_features",
345 label.setHorizontalAlignment(SwingConstants.TRAILING);
346 String tooltip = MessageManager
347 .formatMessage("label.include_linked_tooltip", complement);
348 label.setToolTipText(
349 JvSwingUtils.wrapTooltip(true, tooltip));
351 includeLinkedFeatures = new JCheckBox();
352 linkedFeaturesPanel.add(label);
353 linkedFeaturesPanel.add(includeLinkedFeatures);
354 linkedFeaturesPanel.setVisible(false);
356 return linkedFeaturesPanel;
360 * Builds the panel with to File or Textbox or Close actions
364 JPanel buildActionsPanel()
366 JPanel actionsPanel = new JPanel();
367 actionsPanel.setOpaque(false);
369 JButton toFile = new JButton(MessageManager.getString("label.to_file"));
370 toFile.addActionListener(new ActionListener()
373 public void actionPerformed(ActionEvent e)
375 toFile_actionPerformed();
378 JButton toTextbox = new JButton(
379 MessageManager.getString("label.to_textbox"));
380 toTextbox.addActionListener(new ActionListener()
383 public void actionPerformed(ActionEvent e)
385 toTextbox_actionPerformed();
388 JButton close = new JButton(MessageManager.getString("action.close"));
389 close.addActionListener(new ActionListener()
392 public void actionPerformed(ActionEvent e)
394 close_actionPerformed();
398 actionsPanel.add(toFile);
399 actionsPanel.add(toTextbox);
400 actionsPanel.add(close);
406 * Builds the panel with options to output in Jalview, GFF or CSV format. GFF is
407 * only made visible when exporting features, CSV only when exporting
412 JPanel buildFormatOptionsPanel()
414 JPanel formatPanel = new JPanel();
415 // formatPanel.setBorder(BorderFactory.createEtchedBorder());
416 formatPanel.setOpaque(false);
418 JRadioButton jalviewFormat = new JRadioButton("Jalview");
419 jalviewFormat.setOpaque(false);
420 jalviewFormat.setSelected(true);
421 GFFFormat.setOpaque(false);
422 GFFFormat.setText("GFF");
423 CSVFormat.setOpaque(false);
424 CSVFormat.setText(MessageManager.getString("label.csv_spreadsheet"));
426 ButtonGroup buttonGroup = new ButtonGroup();
427 buttonGroup.add(jalviewFormat);
428 buttonGroup.add(GFFFormat);
429 buttonGroup.add(CSVFormat);
431 JLabel format = new JLabel(
432 MessageManager.getString("action.format") + " ");
433 format.setHorizontalAlignment(SwingConstants.TRAILING);
435 formatPanel.add(format);
436 formatPanel.add(jalviewFormat);
437 formatPanel.add(GFFFormat);
438 formatPanel.add(CSVFormat);