JAL-3761 locateInFrom2 work in progress commit
[jalview.git] / test / jalview / util / MapListTest.java
index bca778d..07da78f 100644 (file)
@@ -276,6 +276,8 @@ public class MapListTest
     assertEquals("[4, 12]", Arrays.toString(ml.locateInFrom(2, 4)));
     assertEquals("[7, 12]", Arrays.toString(ml.locateInFrom(3, 4)));
     assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
+    // reversing the range reverses the result:
+    assertEquals("[12, 7]", Arrays.toString(ml.locateInFrom(4, 3))); // fails with [10, 9] !
 
     assertNull(ml.locateInFrom(0, 0));
     assertNull(ml.locateInFrom(1, 5));
@@ -303,7 +305,17 @@ public class MapListTest
     assertEquals("[16, 18]", Arrays.toString(ml.locateInFrom(4, 4)));
   }
 
-  /**
+  @Test(groups = { "Functional" })
+  public void testLocateInFrom_reverseStrand()
+  {
+    int[] codons = new int[] { 12, 1 };
+    int[] protein = new int[] { 1, 4 };
+    MapList ml = new MapList(codons, protein, 3, 1);
+    assertEquals("[12, 10]", Arrays.toString(ml.locateInFrom(1, 1)));
+    assertEquals("[9, 4]", Arrays.toString(ml.locateInFrom(2, 3)));
+  }
+  
+   /**
    * Tests for method that locates ranges in the 'to' map for given range in the
    * 'from' map.
    */
@@ -325,6 +337,8 @@ public class MapListTest
     assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(1, 12)));
     assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
     assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(4, 12)));
+    // reversing the 'from' range reverses the result
+    assertEquals("[4, 2]", Arrays.toString(ml.locateInTo(12, 4)));
 
     /*
      * A part codon is treated as if a whole one.
@@ -363,11 +377,12 @@ public class MapListTest
      * Can't map from an unmapped position
      */
     assertNull(ml.locateInTo(1, 2));
+    assertNull(ml.locateInTo(1, 4));
     assertNull(ml.locateInTo(2, 4));
     assertNull(ml.locateInTo(4, 4));
 
     /*
-     * Valid range or subrange of codon1 maps to protein1.
+     * Valid range or subrange of codon1 maps to protein1
      */
     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 2)));
     assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 3)));
@@ -380,7 +395,6 @@ public class MapListTest
 
     // codon positions 7 to 17 (part) cover proteins 2/3/4 at positions 3/4/6
     assertEquals("[3, 4, 6, 6]", Arrays.toString(ml.locateInTo(7, 17)));
-
   }
 
   /**
@@ -966,6 +980,9 @@ public class MapListTest
   @Test(groups = { "Functional" })
   public void testLocateInFrom_withOverlap()
   {
+    /*
+     * gene to protein...
+     */
     int[] codons = new int[] { 1, 12, 12, 17 };
     int[] protein = new int[] { 1, 6 };
     MapList ml = new MapList(codons, protein, 3, 1);
@@ -986,6 +1003,16 @@ public class MapListTest
     assertNull(ml.locateInFrom(0, 0));
     assertNull(ml.locateInFrom(1, 7));
     assertNull(ml.locateInFrom(-1, 1));
+
+    /*
+     * gene to CDS...from EMBL:MN908947
+     */
+    int [] gene = new int[] { 266, 13468, 13468, 21555 };
+    int [] cds = new int[] { 1, 21291 };
+    ml = new MapList(gene, cds, 1, 1);
+    assertEquals("[13468, 13468]", Arrays.toString(ml.locateInFrom(13203, 13203)));
+    assertEquals("[13468, 13468]", Arrays.toString(ml.locateInFrom(13204, 13204)));
+    assertEquals("[13468, 13468]", Arrays.toString(ml.locateInFrom(13203, 13204)));
   }
 
   /**
@@ -994,6 +1021,9 @@ public class MapListTest
   @Test(groups = { "Functional" })
   public void testLocateInTo_withOverlap()
   {
+    /*
+     * gene to protein...
+     */
     int[] codons = new int[] { 1, 12, 12, 17 };
     int[] protein = new int[] { 1, 6 };
     MapList ml = new MapList(codons, protein, 3, 1);
@@ -1011,6 +1041,14 @@ public class MapListTest
     assertNull(ml.locateInTo(0, 0));
     assertNull(ml.locateInTo(1, 18));
     assertNull(ml.locateInTo(-1, 1));
+
+    /*
+     * gene to CDS...from EMBL:MN908947
+     */
+    int [] gene = new int[] { 266, 13468, 13468, 21555 };
+    int [] cds = new int[] { 1, 21291 };
+    ml = new MapList(gene, cds, 1, 1);
+    assertEquals("[13203, 13204]", Arrays.toString(ml.locateInTo(13468, 13468)));
   }
 
   @Test(groups = { "Functional" })
@@ -1024,11 +1062,11 @@ public class MapListTest
   }
 
   @Test(groups = { "Functional" })
-  public void testCountPos()
+  public void testCountPositions()
   {
     try
     {
-      MapList.countPos(null, 1);
+      MapList.countPositions(null, 1);
       fail("expected exception");
     } catch (NullPointerException e)
     {
@@ -1036,53 +1074,121 @@ public class MapListTest
     }
 
     List<int[]> intervals = new ArrayList<>();
-    assertNull(MapList.countPos(intervals,  1));
+    assertNull(MapList.countPositions(intervals,  1));
     
     /*
      * forward strand
      */
     intervals.add(new int[] {10, 20});
-    assertNull(MapList.countPos(intervals,  9));
-    assertNull(MapList.countPos(intervals,  21));
-    assertArrayEquals(new int[] {1, 1}, MapList.countPos(intervals,  10));
-    assertArrayEquals(new int[] {6, 1}, MapList.countPos(intervals,  15));
-    assertArrayEquals(new int[] {11, 1}, MapList.countPos(intervals,  20));
+    assertNull(MapList.countPositions(intervals,  9));
+    assertNull(MapList.countPositions(intervals,  21));
+    assertArrayEquals(new int[] {1, 1}, MapList.countPositions(intervals,  10));
+    assertArrayEquals(new int[] {6, 1}, MapList.countPositions(intervals,  15));
+    assertArrayEquals(new int[] {11, 1}, MapList.countPositions(intervals,  20));
 
     intervals.add(new int[] {25, 25});
-    assertArrayEquals(new int[] {12, 1}, MapList.countPos(intervals,  25));
+    assertArrayEquals(new int[] {12, 1}, MapList.countPositions(intervals,  25));
 
     // next interval repeats position 25 - which should be counted twice if traversed
     intervals.add(new int[] {25, 26});
-    assertArrayEquals(new int[] {12, 1}, MapList.countPos(intervals,  25));
-    assertArrayEquals(new int[] {14, 1}, MapList.countPos(intervals,  26));
+    assertArrayEquals(new int[] {12, 1}, MapList.countPositions(intervals,  25));
+    assertArrayEquals(new int[] {14, 1}, MapList.countPositions(intervals,  26));
     
     /*
      * reverse strand
      */
     intervals.clear();
     intervals.add(new int[] {5, -5});
-    assertNull(MapList.countPos(intervals,  6));
-    assertNull(MapList.countPos(intervals,  -6));
-    assertArrayEquals(new int[] {1, -1}, MapList.countPos(intervals,  5));
-    assertArrayEquals(new int[] {7, -1}, MapList.countPos(intervals,  -1));
-    assertArrayEquals(new int[] {11, -1}, MapList.countPos(intervals,  -5));
+    assertNull(MapList.countPositions(intervals,  6));
+    assertNull(MapList.countPositions(intervals,  -6));
+    assertArrayEquals(new int[] {1, -1}, MapList.countPositions(intervals,  5));
+    assertArrayEquals(new int[] {7, -1}, MapList.countPositions(intervals,  -1));
+    assertArrayEquals(new int[] {11, -1}, MapList.countPositions(intervals,  -5));
     
     /*
      * reverse then forward
      */
     intervals.add(new int[] {5, 10});
-    assertArrayEquals(new int[] {13, 1}, MapList.countPos(intervals,  6));
+    assertArrayEquals(new int[] {13, 1}, MapList.countPositions(intervals,  6));
     
     /*
      * reverse then forward then reverse
      */
     intervals.add(new int[] {-10, -20});
-    assertArrayEquals(new int[] {20, -1}, MapList.countPos(intervals,  -12));
+    assertArrayEquals(new int[] {20, -1}, MapList.countPositions(intervals,  -12));
     
     /*
      * an interval [x, x] is treated as forward
      */
     intervals.add(new int[] {30, 30});
-    assertArrayEquals(new int[] {29, 1}, MapList.countPos(intervals,  30));
+    assertArrayEquals(new int[] {29, 1}, MapList.countPositions(intervals,  30));
+    
+    /*
+     * it is the first matched occurrence that is returned
+     */
+    intervals.clear();
+    intervals.add(new int[] {1, 2});
+    intervals.add(new int[] {2, 3});
+    assertArrayEquals(new int[] {2, 1}, MapList.countPositions(intervals,  2));
+    intervals.add(new int[] {-1, -2});
+    intervals.add(new int[] {-2, -3});
+    assertArrayEquals(new int[] {6, -1}, MapList.countPositions(intervals,  -2));
+  }
+
+  @Test(groups = { "Functional" })
+  public void testFindOverlapPositions()
+  {
+    List<int[]> ranges = new ArrayList<>();
+    List<int[]> overlaps = MapList.findOverlapPositions(ranges,  20,  30);
+    assertTrue(overlaps.isEmpty());
+    ranges.add(new int[] {15, 25});
+    overlaps = MapList.findOverlapPositions(ranges,  20,  30);
+    assertEquals(1, overlaps.size());
+    assertArrayEquals(new int[] {6, 11}, overlaps.get(0));
+  }
+
+  @Test(groups = { "Functional" })
+  public void testMapWords()
+  {
+    List<int[]> ranges = new ArrayList<>();
+    
+    /*
+     * 1:1 (trivial) case
+     */
+    ranges.add(new int[] {2, 4});
+    ranges.add(new int[] {6, 9});
+    MapList.mapWords(ranges,  1, 1);
+    assertEquals(ranges.size(), 2);
+    assertArrayEquals(new int[] {2, 4}, ranges.get(0));
+    assertArrayEquals(new int[] {6, 9}, ranges.get(1));
+    
+    /*
+     * 1:3 case (peptide to codon ranges)
+     */
+    MapList.mapWords(ranges,  1, 3);
+    assertEquals(ranges.size(), 2);
+    assertArrayEquals(new int[] {6, 14}, ranges.get(0));
+    assertArrayEquals(new int[] {18, 29}, ranges.get(1));
+    
+    /*
+     * 3:1 case (codon or part codon to peptide)
+     */
+    ranges.clear();
+    ranges.add(new int[] {0, 5}); // 2 whole codons
+    ranges.add(new int[] {7, 11}); // part + whole codon
+    ranges.add(new int[] {15, 19}); // whole + part codon
+    ranges.add(new int[] {23, 27}); // part + part codon
+    ranges.add(new int[] {30, 30}); // first base of codon
+    ranges.add(new int[] {31, 31}); // second base of codon
+    ranges.add(new int[] {32, 32}); // third base of codon
+    MapList.mapWords(ranges,  3, 1);
+    assertEquals(ranges.size(), 7);
+    assertArrayEquals(new int[] {0, 1}, ranges.get(0));
+    assertArrayEquals(new int[] {2, 3}, ranges.get(1));
+    assertArrayEquals(new int[] {5, 6}, ranges.get(2));
+    assertArrayEquals(new int[] {7, 9}, ranges.get(3));
+    assertArrayEquals(new int[] {10, 10}, ranges.get(4));
+    assertArrayEquals(new int[] {10, 10}, ranges.get(5));
+    assertArrayEquals(new int[] {10, 10}, ranges.get(6));
   }
 }