JAL-1152 check 'autocalculated' not 'null sequenceRef'; sort by sequence
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 7 Sep 2015 13:32:04 +0000 (14:32 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 7 Sep 2015 13:32:04 +0000 (14:32 +0100)
respects autocalc above/below

src/jalview/analysis/AnnotationSorter.java
test/jalview/analysis/AnnotationSorterTest.java

index 81398eb..0873f89 100644 (file)
@@ -109,17 +109,38 @@ public class AnnotationSorter
         return 1;
       }
 
+      // TODO how to treat sequence-related autocalculated annotation
+      boolean o1auto = o1.autoCalculated && o1.sequenceRef == null;
+      boolean o2auto = o2.autoCalculated && o2.sequenceRef == null;
       /*
        * Ignore label (keep existing ordering) for
        * Conservation/Quality/Consensus etc
        */
-      if (o1.sequenceRef == null && o2.sequenceRef == null)
+      if (o1auto && o2auto)
       {
         return 0;
       }
+
+      /*
+       * Sort autocalculated before or after sequence-related.
+       */
+      if (o1auto)
+      {
+        return showAutocalcAbove ? -1 : 1;
+      }
+      if (o2auto)
+      {
+        return showAutocalcAbove ? 1 : -1;
+      }
       int sequenceOrder = compareSequences(o1, o2);
       return sequenceOrder == 0 ? compareLabels(o1, o2) : sequenceOrder;
     }
+
+    @Override
+    public String toString()
+    {
+      return "Sort by sequence and label";
+    }
   };
 
   /**
@@ -149,55 +170,75 @@ public class AnnotationSorter
         return 1;
       }
 
+      // TODO how to treat sequence-related autocalculated annotation
+      boolean o1auto = o1.autoCalculated && o1.sequenceRef == null;
+      boolean o2auto = o2.autoCalculated && o2.sequenceRef == null;
       /*
        * Ignore label (keep existing ordering) for
        * Conservation/Quality/Consensus etc
        */
-      if (o1.sequenceRef == null && o2.sequenceRef == null)
+      if (o1auto && o2auto)
       {
         return 0;
       }
 
       /*
-       * Sort non-sequence-related before or after sequence-related.
+       * Sort autocalculated before or after sequence-related.
        */
-      if (o1.sequenceRef == null)
+      if (o1auto)
       {
         return showAutocalcAbove ? -1 : 1;
       }
-      if (o2.sequenceRef == null)
+      if (o2auto)
       {
         return showAutocalcAbove ? 1 : -1;
       }
       int labelOrder = compareLabels(o1, o2);
       return labelOrder == 0 ? compareSequences(o1, o2) : labelOrder;
     }
+
+    @Override
+    public String toString()
+    {
+      return "Sort by label and sequence";
+    }
   };
 
   /**
-   * noSort leaves sort order unchanged, within sequence- and
-   * non-sequence-related annotations, but may switch the ordering of these
-   * groups. Note this is guaranteed (at least in Java 7) as Arrays.sort() is
-   * guaranteed to be 'stable' (not change ordering of equal items).
+   * noSort leaves sort order unchanged, within sequence- and autocalculated
+   * annotations, but may switch the ordering of these groups. Note this is
+   * guaranteed (at least in Java 7) as Arrays.sort() is guaranteed to be
+   * 'stable' (not change ordering of equal items).
    */
   private Comparator<? super AlignmentAnnotation> noSort = new Comparator<AlignmentAnnotation>()
   {
     @Override
     public int compare(AlignmentAnnotation o1, AlignmentAnnotation o2)
     {
+      // TODO how to treat sequence-related autocalculated annotation
+      boolean o1auto = o1.autoCalculated && o1.sequenceRef == null;
+      boolean o2auto = o2.autoCalculated && o2.sequenceRef == null;
+      // TODO skip this test to allow customised ordering of all annotations
+      // - needs a third option: place autocalculated first / last / none
       if (o1 != null && o2 != null)
       {
-        if (o1.sequenceRef == null && o2.sequenceRef != null)
+        if (o1auto && !o2auto)
         {
           return showAutocalcAbove ? -1 : 1;
         }
-        if (o1.sequenceRef != null && o2.sequenceRef == null)
+        if (!o1auto && o2auto)
         {
           return showAutocalcAbove ? 1 : -1;
         }
       }
       return 0;
     }
+
+    @Override
+    public String toString()
+    {
+      return "No sort";
+    }
   };
 
   /**
index 1731302..47271e5 100644 (file)
@@ -91,8 +91,8 @@ public class AnnotationSorterTest
     anns[0].sequenceRef = al.getSequenceAt(1); anns[0].label = "label0";
     anns[1].sequenceRef = al.getSequenceAt(3); anns[1].label = "structure";
     anns[2].sequenceRef = al.getSequenceAt(3); anns[2].label = "iron";
-    anns[3].sequenceRef = null;                anns[3].label = "Quality";
-    anns[4].sequenceRef = null;                anns[4].label = "Consensus";
+    anns[3].autoCalculated = true;             anns[3].label = "Quality";
+    anns[4].autoCalculated = true;             anns[4].label = "Consensus";
     anns[5].sequenceRef = al.getSequenceAt(0); anns[5].label = "label5";
     anns[6].sequenceRef = al.getSequenceAt(3); anns[6].label = "IRP";
     // @formatter:on
@@ -104,7 +104,7 @@ public class AnnotationSorterTest
     assertEquals("iron", anns[2].label); // sequence 3 /iron
     assertEquals("IRP", anns[3].label); // sequence 3/IRP
     assertEquals("structure", anns[4].label); // sequence 3/structure
-    assertEquals("Quality", anns[5].label); // non-sequence annotations
+    assertEquals("Quality", anns[5].label); // autocalc annotations
     assertEquals("Consensus", anns[6].label); // retain ordering
   }
 
@@ -118,15 +118,15 @@ public class AnnotationSorterTest
     anns[0].sequenceRef = al.getSequenceAt(1); anns[0].label = "label0";
     anns[1].sequenceRef = al.getSequenceAt(3); anns[1].label = "structure";
     anns[2].sequenceRef = al.getSequenceAt(3); anns[2].label = "iron";
-    anns[3].sequenceRef = null;                anns[3].label = "Quality";
-    anns[4].sequenceRef = null;                anns[4].label = "Consensus";
+    anns[3].autoCalculated = true;             anns[3].label = "Quality";
+    anns[4].autoCalculated = true;             anns[4].label = "Consensus";
     anns[5].sequenceRef = al.getSequenceAt(0); anns[5].label = "label5";
     anns[6].sequenceRef = al.getSequenceAt(3); anns[6].label = "IRP";
     // @formatter:on
 
     AnnotationSorter testee = new AnnotationSorter(al, true);
     testee.sort(anns, SequenceAnnotationOrder.SEQUENCE_AND_LABEL);
-    assertEquals("Quality", anns[0].label); // non-sequence annotations
+    assertEquals("Quality", anns[0].label); // autocalc annotations
     assertEquals("Consensus", anns[1].label); // retain ordering
     assertEquals("label5", anns[2].label); // for sequence 0
     assertEquals("label0", anns[3].label); // for sequence 1
@@ -154,8 +154,8 @@ public class AnnotationSorterTest
     anns[0].sequenceRef = al.getSequenceAt(1); anns[0].label = "label0";
     anns[1].sequenceRef = al.getSequenceAt(3); anns[1].label = "structure";
     anns[2].sequenceRef = al.getSequenceAt(3); anns[2].label = "iron";
-    anns[3].sequenceRef = null;                anns[3].label = "Quality";
-    anns[4].sequenceRef = null;                anns[4].label = "Consensus";
+    anns[3].autoCalculated = true;             anns[3].label = "Quality";
+    anns[4].autoCalculated = true;             anns[4].label = "Consensus";
     anns[5].sequenceRef = al.getSequenceAt(0); anns[5].label = "IRON";
     anns[6].sequenceRef = al.getSequenceAt(2); anns[6].label = "Structure";
     // @formatter:on
@@ -167,7 +167,7 @@ public class AnnotationSorterTest
     assertEquals("label0", anns[2].label); // label0 / sequence 1
     assertEquals("Structure", anns[3].label); // Structure / sequence 2
     assertEquals("structure", anns[4].label); // structure / sequence 3
-    assertEquals("Quality", anns[5].label); // non-sequence annotations
+    assertEquals("Quality", anns[5].label); // autocalc annotations
     assertEquals("Consensus", anns[6].label); // retain ordering
   }
 
@@ -181,15 +181,15 @@ public class AnnotationSorterTest
     anns[0].sequenceRef = al.getSequenceAt(1); anns[0].label = "label0";
     anns[1].sequenceRef = al.getSequenceAt(3); anns[1].label = "structure";
     anns[2].sequenceRef = al.getSequenceAt(3); anns[2].label = "iron";
-    anns[3].sequenceRef = null;                anns[3].label = "Quality";
-    anns[4].sequenceRef = null;                anns[4].label = "Consensus";
+    anns[3].autoCalculated = true;             anns[3].label = "Quality";
+    anns[4].autoCalculated = true;             anns[4].label = "Consensus";
     anns[5].sequenceRef = al.getSequenceAt(0); anns[5].label = "IRON";
     anns[6].sequenceRef = al.getSequenceAt(2); anns[6].label = "Structure";
     // @formatter:on
 
     AnnotationSorter testee = new AnnotationSorter(al, true);
     testee.sort(anns, SequenceAnnotationOrder.LABEL_AND_SEQUENCE);
-    assertEquals("Quality", anns[0].label); // non-sequence annotations
+    assertEquals("Quality", anns[0].label); // autocalc annotations
     assertEquals("Consensus", anns[1].label); // retain ordering
     assertEquals("IRON", anns[2].label); // IRON / sequence 0
     assertEquals("iron", anns[3].label); // iron / sequence 3
@@ -209,15 +209,15 @@ public class AnnotationSorterTest
     anns[0].sequenceRef = al.getSequenceAt(1); anns[0].label = "label0";
     anns[1].sequenceRef = al.getSequenceAt(3); anns[1].label = "structure";
     anns[2].sequenceRef = al.getSequenceAt(3); anns[2].label = "iron";
-    anns[3].sequenceRef = null;                anns[3].label = "Quality";
-    anns[4].sequenceRef = null;                anns[4].label = "Consensus";
+    anns[3].autoCalculated = true;             anns[3].label = "Quality";
+    anns[4].autoCalculated = true;             anns[4].label = "Consensus";
     anns[5].sequenceRef = al.getSequenceAt(0); anns[5].label = "IRON";
     anns[6].sequenceRef = al.getSequenceAt(2); anns[6].label = "Structure";
     // @formatter:on
 
     AnnotationSorter testee = new AnnotationSorter(al, true);
     testee.sort(anns, SequenceAnnotationOrder.NONE);
-    assertEquals("Quality", anns[0].label); // non-sequence annotations
+    assertEquals("Quality", anns[0].label); // autocalc annotations
     assertEquals("Consensus", anns[1].label); // retain ordering
     assertEquals("label0", anns[2].label);
     assertEquals("structure", anns[3].label);