From: gmungoc Date: Fri, 8 Dec 2017 15:20:23 +0000 (+0000) Subject: JAL-2843 refactor / tidy for feature filter input/output X-Git-Tag: Release_2_11_0~87 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=9907d58721e88d7546d5f9d441b575573ae00a9a;p=jalview.git JAL-2843 refactor / tidy for feature filter input/output --- diff --git a/src/jalview/appletgui/AlignFrame.java b/src/jalview/appletgui/AlignFrame.java index ef87671..fe6b8d9 100644 --- a/src/jalview/appletgui/AlignFrame.java +++ b/src/jalview/appletgui/AlignFrame.java @@ -1445,9 +1445,10 @@ public class AlignFrame extends EmbmenuFrame implements ActionListener, FeaturesFile formatter = new FeaturesFile(); if (format.equalsIgnoreCase("Jalview")) { - features = formatter.printJalviewFormat(viewport.getAlignment() - .getSequencesArray(), getDisplayedFeatureCols(), - getDisplayedFeatureGroups(), true); + features = formatter.printJalviewFormat( + viewport.getAlignment().getSequencesArray(), + getDisplayedFeatureCols(), null, getDisplayedFeatureGroups(), + true); } else { diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index 8a44f27..4c9360e 100644 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -1392,13 +1392,13 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, @Override public void exportFeatures_actionPerformed(ActionEvent e) { - new AnnotationExporter().exportFeatures(alignPanel); + new AnnotationExporter(alignPanel).exportFeatures(); } @Override public void exportAnnotations_actionPerformed(ActionEvent e) { - new AnnotationExporter().exportAnnotations(alignPanel); + new AnnotationExporter(alignPanel).exportAnnotations(); } @Override diff --git a/src/jalview/gui/AnnotationExporter.java b/src/jalview/gui/AnnotationExporter.java index a619997..6fefbd0 100644 --- a/src/jalview/gui/AnnotationExporter.java +++ b/src/jalview/gui/AnnotationExporter.java @@ -21,8 +21,10 @@ package jalview.gui; import jalview.api.FeatureColourI; +import jalview.bin.Cache; import jalview.datamodel.AlignmentAnnotation; import jalview.datamodel.SequenceI; +import jalview.datamodel.features.FeatureMatcherSetI; import jalview.io.AnnotationFile; import jalview.io.FeaturesFile; import jalview.io.JalviewFileChooser; @@ -34,6 +36,8 @@ import java.awt.Color; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.io.FileWriter; +import java.io.PrintWriter; import java.util.List; import java.util.Map; @@ -57,18 +61,22 @@ import javax.swing.SwingConstants; */ public class AnnotationExporter extends JPanel { - JInternalFrame frame; + private JInternalFrame frame; - AlignmentPanel ap; + private AlignmentPanel ap; - boolean features = true; + /* + * true if exporting features, false if exporting annotations + */ + private boolean exportFeatures = true; private AlignmentAnnotation[] annotations; private boolean wholeView; - public AnnotationExporter() + public AnnotationExporter(AlignmentPanel panel) { + this.ap = panel; try { jbInit(); @@ -84,47 +92,54 @@ public class AnnotationExporter extends JPanel frame.getPreferredSize().height); } - public void exportFeatures(AlignmentPanel ap) + /** + * Configures the diglog for options to export visible features + */ + public void exportFeatures() { - this.ap = ap; - features = true; + exportFeatures = true; CSVFormat.setVisible(false); frame.setTitle(MessageManager.getString("label.export_features")); } - public void exportAnnotations(AlignmentPanel ap) + /** + * Configures the dialog for options to export all visible annotations + */ + public void exportAnnotations() { - this.ap = ap; - annotations = ap.av.isShowAnnotation() ? null - : ap.av.getAlignment().getAlignmentAnnotation(); - wholeView = true; - startExportAnnotation(); + boolean showAnnotation = ap.av.isShowAnnotation(); + exportAnnotation(showAnnotation ? null + : ap.av.getAlignment().getAlignmentAnnotation(), true); } - public void exportAnnotations(AlignmentPanel alp, - AlignmentAnnotation[] toExport) + /** + * Configures the dialog for options to export the given annotation row + * + * @param toExport + */ + public void exportAnnotation(AlignmentAnnotation toExport) { - ap = alp; - annotations = toExport; - wholeView = false; - startExportAnnotation(); + exportAnnotation(new AlignmentAnnotation[] { toExport }, false); } - private void startExportAnnotation() + private void exportAnnotation(AlignmentAnnotation[] toExport, + boolean forWholeView) { - features = false; + wholeView = forWholeView; + annotations = toExport; + exportFeatures = false; GFFFormat.setVisible(false); CSVFormat.setVisible(true); frame.setTitle(MessageManager.getString("label.export_annotations")); } - public void toFile_actionPerformed(ActionEvent e) + private void toFile_actionPerformed() { JalviewFileChooser chooser = new JalviewFileChooser( - jalview.bin.Cache.getProperty("LAST_DIRECTORY")); + Cache.getProperty("LAST_DIRECTORY")); chooser.setFileView(new JalviewFileView()); - chooser.setDialogTitle(features + chooser.setDialogTitle(exportFeatures ? MessageManager.getString("label.save_features_to_file") : MessageManager.getString("label.save_annotation_to_file")); chooser.setToolTipText(MessageManager.getString("action.save")); @@ -133,13 +148,12 @@ public class AnnotationExporter extends JPanel if (value == JalviewFileChooser.APPROVE_OPTION) { - String text = getFileContents(); + String text = getText(); try { - java.io.PrintWriter out = new java.io.PrintWriter( - new java.io.FileWriter(chooser.getSelectedFile())); - + PrintWriter out = new PrintWriter( + new FileWriter(chooser.getSelectedFile())); out.print(text); out.close(); } catch (Exception ex) @@ -148,64 +162,89 @@ public class AnnotationExporter extends JPanel } } - close_actionPerformed(null); + close_actionPerformed(); + } + + /** + * Answers the text to output for either Features (in GFF or Jalview format) or + * Annotations (in CSV or Jalview format) + * + * @return + */ + private String getText() + { + return exportFeatures ? getFeaturesText() : getAnnotationsText(); } - private String getFileContents() + /** + * Returns the text contents for output of annotations in either CSV or Jalview + * format + * + * @return + */ + private String getAnnotationsText() { - String text = MessageManager - .getString("label.no_features_on_alignment"); - if (features) + String text; + if (CSVFormat.isSelected()) { - FeaturesFile formatter = new FeaturesFile(); - SequenceI[] sequences = ap.av.getAlignment().getSequencesArray(); - Map featureColours = ap.getFeatureRenderer() - .getDisplayedFeatureCols(); - List featureGroups = ap.getFeatureRenderer() - .getDisplayedFeatureGroups(); - boolean includeNonPositional = ap.av.isShowNPFeats(); - if (GFFFormat.isSelected()) - { - text = formatter.printGffFormat(sequences, featureColours, - featureGroups, includeNonPositional); - } - else - { - text = formatter.printJalviewFormat(sequences, featureColours, - featureGroups, includeNonPositional); - } + text = new AnnotationFile().printCSVAnnotations(annotations); } else { - if (CSVFormat.isSelected()) + if (wholeView) { - text = new AnnotationFile().printCSVAnnotations(annotations); + text = new AnnotationFile().printAnnotationsForView(ap.av); } else { - if (wholeView) - { - text = new AnnotationFile().printAnnotationsForView(ap.av); - } - else - { - text = new AnnotationFile().printAnnotations(annotations, null, - null); - } + text = new AnnotationFile().printAnnotations(annotations, null, + null); } } return text; } - public void toTextbox_actionPerformed(ActionEvent e) + /** + * Returns the text contents for output of features in either GFF or Jalview + * format + * + * @return + */ + private String getFeaturesText() + { + String text; + SequenceI[] sequences = ap.av.getAlignment().getSequencesArray(); + Map featureColours = ap.getFeatureRenderer() + .getDisplayedFeatureCols(); + Map featureFilters = ap.getFeatureRenderer() + .getFeatureFilters(); + List featureGroups = ap.getFeatureRenderer() + .getDisplayedFeatureGroups(); + boolean includeNonPositional = ap.av.isShowNPFeats(); + + FeaturesFile formatter = new FeaturesFile(); + if (GFFFormat.isSelected()) + { + text = formatter.printGffFormat(sequences, featureColours, + featureGroups, includeNonPositional); + } + else + { + text = formatter.printJalviewFormat(sequences, featureColours, + featureFilters, featureGroups, includeNonPositional); + } + return text; + } + + private void toTextbox_actionPerformed() { CutAndPasteTransfer cap = new CutAndPasteTransfer(); try { - String text = getFileContents(); + String text = getText(); cap.setText(text); - Desktop.addInternalFrame(cap, (features ? MessageManager + Desktop.addInternalFrame(cap, (exportFeatures ? MessageManager .formatMessage("label.features_for_params", new String[] { ap.alignFrame.getTitle() }) : MessageManager.formatMessage("label.annotations_for_params", @@ -214,7 +253,7 @@ public class AnnotationExporter extends JPanel 600, 500); } catch (OutOfMemoryError oom) { - new OOMWarning((features ? MessageManager.formatMessage( + new OOMWarning((exportFeatures ? MessageManager.formatMessage( "label.generating_features_for_params", new String[] { ap.alignFrame.getTitle() }) : MessageManager.formatMessage( @@ -225,10 +264,10 @@ public class AnnotationExporter extends JPanel cap.dispose(); } - close_actionPerformed(null); + close_actionPerformed(); } - public void close_actionPerformed(ActionEvent e) + private void close_actionPerformed() { try { @@ -248,7 +287,7 @@ public class AnnotationExporter extends JPanel @Override public void actionPerformed(ActionEvent e) { - toFile_actionPerformed(e); + toFile_actionPerformed(); } }); toTextbox.setText(MessageManager.getString("label.to_textbox")); @@ -257,7 +296,7 @@ public class AnnotationExporter extends JPanel @Override public void actionPerformed(ActionEvent e) { - toTextbox_actionPerformed(e); + toTextbox_actionPerformed(); } }); close.setText(MessageManager.getString("action.close")); @@ -266,7 +305,7 @@ public class AnnotationExporter extends JPanel @Override public void actionPerformed(ActionEvent e) { - close_actionPerformed(e); + close_actionPerformed(); } }); jalviewFormat.setOpaque(false); diff --git a/src/jalview/gui/AnnotationLabels.java b/src/jalview/gui/AnnotationLabels.java index b94a615..de69907 100755 --- a/src/jalview/gui/AnnotationLabels.java +++ b/src/jalview/gui/AnnotationLabels.java @@ -266,9 +266,7 @@ public class AnnotationLabels extends JPanel } else if (evt.getActionCommand().equals(OUTPUT_TEXT)) { - new AnnotationExporter().exportAnnotations(ap, - new AlignmentAnnotation[] - { aa[selectedRow] }); + new AnnotationExporter(ap).exportAnnotation(aa[selectedRow]); } else if (evt.getActionCommand().equals(COPYCONS_SEQ)) {