JAL-1152 annotation sort algorithm optimisation
[jalview.git] / test / jalview / analysis / AnnotationSorterTest.java
index 55dcf6d..239eb4c 100644 (file)
@@ -1,7 +1,6 @@
 package jalview.analysis;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
 import jalview.analysis.AnnotationSorter.SequenceAnnotationOrder;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentAnnotation;
@@ -85,7 +84,7 @@ public class AnnotationSorterTest
    * </ul>
    */
   @Test
-  public void testSortBySequenceAndType()
+  public void testSortBySequenceAndType_autocalcLast()
   {
     // @formatter:off
     anns[0].sequenceRef = al.getSequenceAt(1); anns[0].label = "label0";
@@ -109,6 +108,33 @@ public class AnnotationSorterTest
   }
 
   /**
+   * Variant with autocalculated annotations sorting to front
+   */
+  @Test
+  public void testSortBySequenceAndType_autocalcFirst()
+  {
+    // @formatter:off
+    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[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("Consensus", anns[1].label); // retain ordering
+    assertEquals("label5", anns[2].label); // for sequence 0
+    assertEquals("label0", anns[3].label); // for sequence 1
+    assertEquals("iron", anns[4].label); // sequence 3 /iron
+    assertEquals("IRP", anns[5].label); // sequence 3/IRP
+    assertEquals("structure", anns[6].label); // sequence 3/structure
+  }
+
+  /**
    * Test sorting by annotation type (label) within sequence order, including
    * <ul>
    * <li>annotations with no sequence reference - sort to end keeping mutual
@@ -121,7 +147,7 @@ public class AnnotationSorterTest
    * </ul>
    */
   @Test
-  public void testSortByTypeAndSequence()
+  public void testSortByTypeAndSequence_autocalcLast()
   {
     // @formatter:off
     anns[0].sequenceRef = al.getSequenceAt(1); anns[0].label = "label0";
@@ -144,12 +170,77 @@ public class AnnotationSorterTest
     assertEquals("Consensus", anns[6].label); // retain ordering
   }
 
+  /**
+   * Variant of test with autocalculated annotations sorted to front
+   */
+  @Test
+  public void testSortByTypeAndSequence_autocalcFirst()
+  {
+    // @formatter:off
+    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[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("Consensus", anns[1].label); // retain ordering
+    assertEquals("IRON", anns[2].label); // IRON / sequence 0
+    assertEquals("iron", anns[3].label); // iron / sequence 3
+    assertEquals("label0", anns[4].label); // label0 / sequence 1
+    assertEquals("Structure", anns[5].label); // Structure / sequence 2
+    assertEquals("structure", anns[6].label); // structure / sequence 3
+  }
+
+  /**
+   * Variant of test with autocalculated annotations sorted to front but
+   * otherwise no change.
+   */
+  @Test
+  public void testNoSort_autocalcFirst()
+  {
+    // @formatter:off
+    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[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("Consensus", anns[1].label); // retain ordering
+    assertEquals("label0", anns[2].label);
+    assertEquals("structure", anns[3].label);
+    assertEquals("iron", anns[4].label);
+    assertEquals("IRON", anns[5].label);
+    assertEquals("Structure", anns[6].label);
+  }
+
   @Test
   public void testSort_timingPresorted()
   {
-    final long targetTime = 100; // ms
-    final int numSeqs = 10000;
-    final int numAnns = 20000;
+    testTiming_presorted(50, 100);
+    testTiming_presorted(500, 1000);
+    testTiming_presorted(5000, 10000);
+  }
+
+  /**
+   * Test timing to sort annotations already in the sort order.
+   * 
+   * @param numSeqs
+   * @param numAnns
+   */
+  private void testTiming_presorted(final int numSeqs, final int numAnns)
+  {
     al = buildAlignment(numSeqs);
     anns = buildAnnotations(numAnns);
 
@@ -172,18 +263,28 @@ public class AnnotationSorterTest
     System.out.println("Timing test for presorted " + numSeqs
             + " sequences and "
             + numAnns + " annotations took " + elapsed + "ms");
-    assertTrue("Sort took more than " + targetTime + "ms",
-            elapsed <= targetTime);
   }
 
   /**
-   * Timing test for sorting randomly sorted annotations
+   * Timing tests for sorting randomly sorted annotations for various sizes.
    */
   @Test
   public void testSort_timingUnsorted()
   {
-    final int numSeqs = 2000;
-    final int numAnns = 4000;
+    testTiming_unsorted(50, 100);
+    testTiming_unsorted(500, 1000);
+    testTiming_unsorted(5000, 10000);
+  }
+
+  /**
+   * Generate annotations randomly sorted with respect to sequences, and time
+   * sorting.
+   * 
+   * @param numSeqs
+   * @param numAnns
+   */
+  private void testTiming_unsorted(final int numSeqs, final int numAnns)
+  {
     al = buildAlignment(numSeqs);
     anns = buildAnnotations(numAnns);
 
@@ -214,8 +315,26 @@ public class AnnotationSorterTest
   @Test
   public void testSort_timingSemisorted()
   {
-    final int numSeqs = 2000;
-    final int numAnns = 4000;
+    testTiming_semiSorted(50, 100);
+    testTiming_semiSorted(500, 1000);
+    testTiming_semiSorted(5000, 10000);
+  }
+
+  /**
+   * Mimic 'semi-sorted' annotations:
+   * <ul>
+   * <li>set up in sequence order, with randomly assigned labels from a limited
+   * range</li>
+   * <li>sort by label and sequence order, report timing</li>
+   * <li>resort by sequence and label, report timing</li>
+   * <li>resort by label and sequence, report timing</li>
+   * </ul>
+   * 
+   * @param numSeqs
+   * @param numAnns
+   */
+  private void testTiming_semiSorted(final int numSeqs, final int numAnns)
+  {
     al = buildAlignment(numSeqs);
     anns = buildAnnotations(numAnns);
 
@@ -238,7 +357,7 @@ public class AnnotationSorterTest
     testee.sort(anns, SequenceAnnotationOrder.LABEL_AND_SEQUENCE);
     long endTime = System.currentTimeMillis();
     long elapsed = endTime - startTime;
-    System.out.println("Sort by type for semisorted " + numSeqs
+    System.out.println("Sort by label for semisorted " + numSeqs
             + " sequences and "
             + numAnns + " annotations took " + elapsed + "ms");
 
@@ -251,12 +370,12 @@ public class AnnotationSorterTest
             + " sequences and " + numAnns + " annotations took " + elapsed
             + "ms");
 
-    // now resort by type
+    // now resort by label
     startTime = System.currentTimeMillis();
     testee.sort(anns, SequenceAnnotationOrder.LABEL_AND_SEQUENCE);
     endTime = System.currentTimeMillis();
     elapsed = endTime - startTime;
-    System.out.println("Resort by type for semisorted " + numSeqs
+    System.out.println("Resort by label for semisorted " + numSeqs
             + " sequences and " + numAnns + " annotations took " + elapsed
             + "ms");
   }