From 53e0350c1c4464442938e4bace4e85bdbca665be Mon Sep 17 00:00:00 2001 From: gmungoc Date: Thu, 14 Nov 2019 16:01:29 +0000 Subject: [PATCH] JAL-3081 autocalculated annotation first/last leaves others unchanged --- src/jalview/analysis/AnnotationSorter.java | 17 +++++++++++++-- src/jalview/api/AlignmentViewPanel.java | 5 ++++- src/jalview/appletgui/AlignmentPanel.java | 7 +----- src/jalview/gui/AlignFrame.java | 8 +++---- src/jalview/gui/AlignmentPanel.java | 4 ++-- src/jalview/jbgui/GAlignFrame.java | 18 +++++++--------- src/jalview/project/Jalview2XML.java | 3 ++- src/jalview/ws/jws2/AbstractJabaCalcWorker.java | 2 +- test/jalview/analysis/AnnotationSorterTest.java | 26 +++++++++++------------ 9 files changed, 50 insertions(+), 40 deletions(-) diff --git a/src/jalview/analysis/AnnotationSorter.java b/src/jalview/analysis/AnnotationSorter.java index e5e9de4..f16d9ea 100644 --- a/src/jalview/analysis/AnnotationSorter.java +++ b/src/jalview/analysis/AnnotationSorter.java @@ -38,7 +38,6 @@ import java.util.Map; */ public class AnnotationSorter { - /** * enum for annotation sort options. The text description is used in the * Preferences drop-down options. The enum name is saved in the preferences @@ -93,18 +92,24 @@ public class AnnotationSorter // working map of sequence index in alignment private final Map sequenceIndices = new HashMap<>(); + // if true, sort only repositions auto-calculated annotation (to top or + // bottom) + private final boolean autocalcOnly; + /** * Constructor given an alignment and the location (top or bottom) of * Consensus and similar. * * @param alignmentI * @param showAutocalculatedAbove + * @param autoCalcOnly */ public AnnotationSorter(AlignmentI alignmentI, - boolean showAutocalculatedAbove) + boolean showAutocalculatedAbove, boolean autoCalcOnly) { this.alignment = alignmentI; this.showAutocalcAbove = showAutocalculatedAbove; + this.autocalcOnly = autoCalcOnly; } /** @@ -159,6 +164,10 @@ public class AnnotationSorter { return showAutocalcAbove ? 1 : -1; } + if (autocalcOnly) + { + return 0; // don't reorder other annotations + } int sequenceOrder = compareSequences(o1, o2); return sequenceOrder == 0 ? compareLabels(o1, o2) : sequenceOrder; } @@ -221,6 +230,10 @@ public class AnnotationSorter { return showAutocalcAbove ? 1 : -1; } + if (autocalcOnly) + { + return 0; // don't reorder other annotations + } int labelOrder = compareLabels(o1, o2); return labelOrder == 0 ? compareSequences(o1, o2) : labelOrder; } diff --git a/src/jalview/api/AlignmentViewPanel.java b/src/jalview/api/AlignmentViewPanel.java index 8918944..dd4f730 100644 --- a/src/jalview/api/AlignmentViewPanel.java +++ b/src/jalview/api/AlignmentViewPanel.java @@ -68,6 +68,9 @@ public interface AlignmentViewPanel extends OOMHandlerI * Sorts annotations according to currently selected preferences. Does nothing * if currently set to 'Custom' (manual) ordering, so as not to trash user * customised annotation ordering. + * + * @param autoCalcOnly + * if true, only automatically calculated annotations are reordered */ - void sortAnnotations(); + void sortAnnotations(boolean autoCalcOnly); } diff --git a/src/jalview/appletgui/AlignmentPanel.java b/src/jalview/appletgui/AlignmentPanel.java index 25974d4..0eb5c88 100644 --- a/src/jalview/appletgui/AlignmentPanel.java +++ b/src/jalview/appletgui/AlignmentPanel.java @@ -20,7 +20,6 @@ */ package jalview.appletgui; -import jalview.analysis.AnnotationSorter; import jalview.api.AlignViewportI; import jalview.api.AlignmentViewPanel; import jalview.bin.JalviewLite; @@ -911,10 +910,6 @@ public class AlignmentPanel extends Panel public void paintAlignment(boolean updateOverview, boolean updateStructures) { - final AnnotationSorter sorter = new AnnotationSorter(getAlignment(), - av.isShowAutocalculatedAbove()); - sorter.sort(getAlignment().getAlignmentAnnotation(), - av.getSortAnnotationsBy()); repaint(); if (updateStructures) @@ -1158,7 +1153,7 @@ public class AlignmentPanel extends Panel } @Override - public void sortAnnotations() + public void sortAnnotations(boolean autocalcOnly) { } diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index 63c0890..8994743 100644 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -479,7 +479,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, } }); - alignPanel.sortAnnotations(); + alignPanel.sortAnnotations(false); } /** @@ -4691,7 +4691,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, } if (isAnnotation) { - alignPanel.sortAnnotations(); + alignPanel.sortAnnotations(false); alignPanel.adjustAnnotationHeight(); viewport.updateSequenceIdColours(); buildSortByAnnotationScoresMenu(); @@ -5412,9 +5412,9 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener, * Sorts annotations and repaints the alignment */ @Override - public void sortAnnotations() + public void sortAnnotations(boolean autoCalcOnly) { - alignPanel.sortAnnotations(); + alignPanel.sortAnnotations(autoCalcOnly); alignPanel.paintAlignment(false, false); } diff --git a/src/jalview/gui/AlignmentPanel.java b/src/jalview/gui/AlignmentPanel.java index aba1e3d..b27d66d 100644 --- a/src/jalview/gui/AlignmentPanel.java +++ b/src/jalview/gui/AlignmentPanel.java @@ -826,13 +826,13 @@ public class AlignmentPanel extends GAlignmentPanel implements * if currently set to 'Custom' (manual) ordering. */ @Override - public void sortAnnotations() + public void sortAnnotations(boolean autoCalcOnly) { SequenceAnnotationOrder sortBy = av.getSortAnnotationsBy(); if (sortBy != SequenceAnnotationOrder.CUSTOM) { final AnnotationSorter sorter = new AnnotationSorter(getAlignment(), - av.isShowAutocalculatedAbove()); + av.isShowAutocalculatedAbove(), autoCalcOnly); sorter.sort(getAlignment().getAlignmentAnnotation(), sortBy); } } diff --git a/src/jalview/jbgui/GAlignFrame.java b/src/jalview/jbgui/GAlignFrame.java index 3aebcda..7caf7ea 100755 --- a/src/jalview/jbgui/GAlignFrame.java +++ b/src/jalview/jbgui/GAlignFrame.java @@ -601,9 +601,6 @@ public class GAlignFrame extends JInternalFrame hideAllAnnotations_actionPerformed(true, false); } }); - SequenceAnnotationOrder sortAnnotationsBy = SequenceAnnotationOrder - .valueOf(Cache.getDefault(Preferences.SORT_ANNOTATIONS, - SequenceAnnotationOrder.NONE.name())); sortAnnBySequence = new JCheckBoxMenuItem( MessageManager.getString("label.sort_annotations_by_sequence")); sortAnnByLabel = new JCheckBoxMenuItem( @@ -615,7 +612,7 @@ public class GAlignFrame extends JInternalFrame public void actionPerformed(ActionEvent e) { setAnnotationSortOrder(SequenceAnnotationOrder.SEQUENCE_AND_LABEL); - sortAnnotations(); + sortAnnotations(false); } }); sortAnnByLabel.addActionListener(new ActionListener() @@ -624,7 +621,7 @@ public class GAlignFrame extends JInternalFrame public void actionPerformed(ActionEvent e) { setAnnotationSortOrder(SequenceAnnotationOrder.LABEL_AND_SEQUENCE); - sortAnnotations(); + sortAnnotations(false); } }); colourTextMenuItem = new JCheckBoxMenuItem( @@ -882,7 +879,7 @@ public class GAlignFrame extends JInternalFrame { if (setShowAutoCalculatedAbove(showAutoFirst.isSelected())) { - sortAnnotations(); + sortAnnotations(true); } } }); @@ -894,7 +891,7 @@ public class GAlignFrame extends JInternalFrame { if (setShowAutoCalculatedAbove(!showAutoLast.isSelected())) { - sortAnnotations(); + sortAnnotations(true); } } }); @@ -2012,11 +2009,12 @@ public class GAlignFrame extends JInternalFrame } /** - * Action on clicking sort annotations by type. + * Action on clicking sort annotations by type, or change position of + * autocalculated annotation * - * @param sortOrder + * @param autoCalcOnly */ - protected void sortAnnotations() + protected void sortAnnotations(boolean autoCalcOnly) { } diff --git a/src/jalview/project/Jalview2XML.java b/src/jalview/project/Jalview2XML.java index e15bb1a..c017d11 100644 --- a/src/jalview/project/Jalview2XML.java +++ b/src/jalview/project/Jalview2XML.java @@ -4029,6 +4029,7 @@ public class Jalview2XML { af = loadViewport(file, jseqs, hiddenSeqs, al, jalviewModel, view, uniqueSeqSetId, viewId, autoAlan); + // TODO resort annotations here to their order in the project? av = af.getViewport(); ap = af.alignPanel; } @@ -5196,7 +5197,7 @@ public class Jalview2XML // recompute any autoannotation af.alignPanel.updateAnnotation(false, true); reorderAutoannotation(af, al, autoAlan); - af.sortAnnotations(); + af.sortAnnotations(false); af.alignPanel.alignmentChanged(); } else diff --git a/src/jalview/ws/jws2/AbstractJabaCalcWorker.java b/src/jalview/ws/jws2/AbstractJabaCalcWorker.java index 64a2662..2d91377 100644 --- a/src/jalview/ws/jws2/AbstractJabaCalcWorker.java +++ b/src/jalview/ws/jws2/AbstractJabaCalcWorker.java @@ -347,7 +347,7 @@ public abstract class AbstractJabaCalcWorker extends AlignCalcWorker jalview.bin.Cache.log.debug("Updating result annotation from Job " + rslt + " at " + service.getUri()); updateResultAnnotation(true); - ap.sortAnnotations(); + ap.sortAnnotations(false); ap.adjustAnnotationHeight(); } } diff --git a/test/jalview/analysis/AnnotationSorterTest.java b/test/jalview/analysis/AnnotationSorterTest.java index 1c5a7f9..7153f67 100644 --- a/test/jalview/analysis/AnnotationSorterTest.java +++ b/test/jalview/analysis/AnnotationSorterTest.java @@ -76,7 +76,7 @@ public class AnnotationSorterTest */ protected AlignmentAnnotation[] buildAnnotations(int numAnns) { - List annlist = new ArrayList(); + List annlist = new ArrayList<>(); for (int i = 0; i < numAnns; i++) { AlignmentAnnotation ann = new AlignmentAnnotation(SS + i, "", 0); @@ -115,7 +115,7 @@ public class AnnotationSorterTest * */ @Test(groups = { "Functional" }) - public void testSortBySequenceAndType_autocalcLast() + public void testSortBySequenceAndLabel_autocalcLast() { // @formatter:off anns[0].sequenceRef = al.getSequenceAt(1); anns[0].label = "label0"; @@ -127,7 +127,7 @@ public class AnnotationSorterTest anns[6].sequenceRef = al.getSequenceAt(3); anns[6].label = "IRP"; // @formatter:on - AnnotationSorter testee = new AnnotationSorter(al, false); + AnnotationSorter testee = new AnnotationSorter(al, false, false); testee.sort(anns, SequenceAnnotationOrder.SEQUENCE_AND_LABEL); assertEquals("label5", anns[0].label); // for sequence 0 assertEquals("label0", anns[1].label); // for sequence 1 @@ -142,7 +142,7 @@ public class AnnotationSorterTest * Variant with autocalculated annotations sorting to front */ @Test(groups = { "Functional" }) - public void testSortBySequenceAndType_autocalcFirst() + public void testSortBySequenceAndLabel_autocalcFirst() { // @formatter:off anns[0].sequenceRef = al.getSequenceAt(1); anns[0].label = "label0"; @@ -154,7 +154,7 @@ public class AnnotationSorterTest anns[6].sequenceRef = al.getSequenceAt(3); anns[6].label = "IRP"; // @formatter:on - AnnotationSorter testee = new AnnotationSorter(al, true); + AnnotationSorter testee = new AnnotationSorter(al, true, false); testee.sort(anns, SequenceAnnotationOrder.SEQUENCE_AND_LABEL); assertEquals("Quality", anns[0].label); // autocalc annotations assertEquals("Consensus", anns[1].label); // retain ordering @@ -178,7 +178,7 @@ public class AnnotationSorterTest * */ @Test(groups = { "Functional" }) - public void testSortByTypeAndSequence_autocalcLast() + public void testSortByLabelAndSequence_autocalcLast() { // @formatter:off anns[0].sequenceRef = al.getSequenceAt(1); anns[0].label = "label0"; @@ -190,7 +190,7 @@ public class AnnotationSorterTest anns[6].sequenceRef = al.getSequenceAt(2); anns[6].label = "Structure"; // @formatter:on - AnnotationSorter testee = new AnnotationSorter(al, false); + AnnotationSorter testee = new AnnotationSorter(al, false, false); testee.sort(anns, SequenceAnnotationOrder.LABEL_AND_SEQUENCE); assertEquals("IRON", anns[0].label); // IRON / sequence 0 assertEquals("iron", anns[1].label); // iron / sequence 3 @@ -205,7 +205,7 @@ public class AnnotationSorterTest * Variant of test with autocalculated annotations sorted to front */ @Test(groups = { "Functional" }) - public void testSortByTypeAndSequence_autocalcFirst() + public void testSortByLabelAndSequence_autocalcFirst() { // @formatter:off anns[0].sequenceRef = al.getSequenceAt(1); anns[0].label = "label0"; @@ -217,7 +217,7 @@ public class AnnotationSorterTest anns[6].sequenceRef = al.getSequenceAt(2); anns[6].label = "Structure"; // @formatter:on - AnnotationSorter testee = new AnnotationSorter(al, true); + AnnotationSorter testee = new AnnotationSorter(al, true, false); testee.sort(anns, SequenceAnnotationOrder.LABEL_AND_SEQUENCE); assertEquals("Quality", anns[0].label); // autocalc annotations assertEquals("Consensus", anns[1].label); // retain ordering @@ -245,7 +245,7 @@ public class AnnotationSorterTest anns[6].sequenceRef = al.getSequenceAt(2); anns[6].label = "Structure"; // @formatter:on - AnnotationSorter testee = new AnnotationSorter(al, true); + AnnotationSorter testee = new AnnotationSorter(al, true, false); testee.sort(anns, SequenceAnnotationOrder.NONE); assertEquals("Quality", anns[0].label); // autocalc annotations assertEquals("Consensus", anns[1].label); // retain ordering @@ -287,7 +287,7 @@ public class AnnotationSorterTest annotations[i].label = "label" + i; } long startTime = System.currentTimeMillis(); - AnnotationSorter testee = new AnnotationSorter(alignment, false); + AnnotationSorter testee = new AnnotationSorter(alignment, false, false); testee.sort(annotations, SequenceAnnotationOrder.LABEL_AND_SEQUENCE); long endTime = System.currentTimeMillis(); final long elapsed = endTime - startTime; @@ -331,7 +331,7 @@ public class AnnotationSorterTest annotations[i].label = "label" + i; } long startTime = System.currentTimeMillis(); - AnnotationSorter testee = new AnnotationSorter(alignment, false); + AnnotationSorter testee = new AnnotationSorter(alignment, false, false); testee.sort(annotations, SequenceAnnotationOrder.SEQUENCE_AND_LABEL); long endTime = System.currentTimeMillis(); final long elapsed = endTime - startTime; @@ -384,7 +384,7 @@ public class AnnotationSorterTest annotations[i].label = labels[r.nextInt(labels.length)]; } long startTime = System.currentTimeMillis(); - AnnotationSorter testee = new AnnotationSorter(alignment, false); + AnnotationSorter testee = new AnnotationSorter(alignment, false, false); testee.sort(annotations, SequenceAnnotationOrder.LABEL_AND_SEQUENCE); long endTime = System.currentTimeMillis(); long elapsed = endTime - startTime; -- 1.7.10.2