JAL-2029 many-to-many EnsemblCDS-to-Uniprot mappings
[jalview.git] / test / jalview / util / MappingUtilsTest.java
index 095ab1b..b53d513 100644 (file)
@@ -21,9 +21,9 @@
 package jalview.util;
 
 import static org.testng.AssertJUnit.assertEquals;
+import static org.testng.AssertJUnit.assertFalse;
 import static org.testng.AssertJUnit.assertSame;
 import static org.testng.AssertJUnit.assertTrue;
-import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;
 
 import jalview.api.AlignViewportI;
 import jalview.commands.EditCommand;
@@ -857,58 +857,160 @@ public class MappingUtilsTest
     assertEquals("[5, 10]", Arrays.toString(hidden.get(1)));
   }
 
-  /**
-   * Tests for the method that removes the trailing stop codon from a mapping
-   * range i.e. the last 3 positions (whether split or not)
-   */
   @Test(groups = { "Functional" })
-  public void testUnmapStopCodon()
+  public void testGetLength()
   {
+    assertEquals(0, MappingUtils.getLength(null));
     List<int[]> ranges = new ArrayList<int[]>();
-
-    // simple case, forward strand:
-    ranges.add(new int[] { 1, 3 });
-    ranges.add(new int[] { 9, 14 });
-    MappingUtils.unmapStopCodon(ranges, 9);
-    assertEquals(2, ranges.size());
-    assertArrayEquals(new int[] { 1, 3 }, ranges.get(0));
-    assertArrayEquals(new int[] { 9, 11 }, ranges.get(1));
-
-    // split stop codon, forward strand:
-    ranges.clear();
-    ranges.add(new int[] { 1, 8 });
-    ranges.add(new int[] { 10, 10 });
-    MappingUtils.unmapStopCodon(ranges, 9);
-    assertEquals(1, ranges.size());
-    assertArrayEquals(new int[] { 1, 6 }, ranges.get(0));
-
-    // very split stop codon, forward strand:
-    ranges.clear();
+    assertEquals(0, MappingUtils.getLength(ranges));
     ranges.add(new int[] { 1, 1 });
-    ranges.add(new int[] { 3, 4 });
+    assertEquals(1, MappingUtils.getLength(ranges));
+    ranges.add(new int[] { 2, 10 });
+    assertEquals(10, MappingUtils.getLength(ranges));
+    ranges.add(new int[] { 20, 10 });
+    assertEquals(21, MappingUtils.getLength(ranges));
+  }
+
+  @Test(groups = { "Functional" })
+  public void testContains()
+  {
+    assertFalse(MappingUtils.contains(null, 1));
+    List<int[]> ranges = new ArrayList<int[]>();
+    assertFalse(MappingUtils.contains(ranges, 1));
+
+    ranges.add(new int[] { 1, 4 });
     ranges.add(new int[] { 6, 6 });
-    ranges.add(new int[] { 8, 8 });
-    ranges.add(new int[] { 10, 10 });
-    MappingUtils.unmapStopCodon(ranges, 6);
-    assertEquals(2, ranges.size());
-    assertArrayEquals(new int[] { 1, 1 }, ranges.get(0));
-    assertArrayEquals(new int[] { 3, 4 }, ranges.get(1));
-
-    // simple case, reverse strand:
-    ranges.clear();
-    ranges.add(new int[] { 12, 10 });
-    ranges.add(new int[] { 6, 1 });
-    MappingUtils.unmapStopCodon(ranges, 9);
-    assertEquals(2, ranges.size());
-    assertArrayEquals(new int[] { 12, 10 }, ranges.get(0));
-    assertArrayEquals(new int[] { 6, 4 }, ranges.get(1));
-
-    // split stop codon, reverse strand:
-    ranges.clear();
-    ranges.add(new int[] { 12, 6 });
-    ranges.add(new int[] { 4, 3 });
-    MappingUtils.unmapStopCodon(ranges, 9);
-    assertEquals(1, ranges.size());
-    assertArrayEquals(new int[] { 12, 7 }, ranges.get(0));
+    ranges.add(new int[] { 8, 10 });
+    ranges.add(new int[] { 30, 20 });
+    ranges.add(new int[] { -16, -44 });
+
+    assertFalse(MappingUtils.contains(ranges, 0));
+    assertTrue(MappingUtils.contains(ranges, 1));
+    assertTrue(MappingUtils.contains(ranges, 2));
+    assertTrue(MappingUtils.contains(ranges, 3));
+    assertTrue(MappingUtils.contains(ranges, 4));
+    assertFalse(MappingUtils.contains(ranges, 5));
+
+    assertTrue(MappingUtils.contains(ranges, 6));
+    assertFalse(MappingUtils.contains(ranges, 7));
+
+    assertTrue(MappingUtils.contains(ranges, 8));
+    assertTrue(MappingUtils.contains(ranges, 9));
+    assertTrue(MappingUtils.contains(ranges, 10));
+
+    assertFalse(MappingUtils.contains(ranges, 31));
+    assertTrue(MappingUtils.contains(ranges, 30));
+    assertTrue(MappingUtils.contains(ranges, 29));
+    assertTrue(MappingUtils.contains(ranges, 20));
+    assertFalse(MappingUtils.contains(ranges, 19));
+
+    assertFalse(MappingUtils.contains(ranges, -15));
+    assertTrue(MappingUtils.contains(ranges, -16));
+    assertTrue(MappingUtils.contains(ranges, -44));
+    assertFalse(MappingUtils.contains(ranges, -45));
+  }
+
+  /**
+   * Test the method that drops positions from the start of a mapped range
+   */
+  @Test(groups = "Functional")
+  public void testRemoveStartPositions()
+  {
+    int[] ranges = new int[] { 1, 10 };
+    int[] adjusted = MappingUtils.removeStartPositions(0, ranges);
+    assertEquals("[1, 10]", Arrays.toString(adjusted));
+
+    adjusted = MappingUtils.removeStartPositions(1, ranges);
+    assertEquals("[2, 10]", Arrays.toString(adjusted));
+    assertEquals("[1, 10]", Arrays.toString(ranges));
+
+    ranges = adjusted;
+    adjusted = MappingUtils.removeStartPositions(1, ranges);
+    assertEquals("[3, 10]", Arrays.toString(adjusted));
+    assertEquals("[2, 10]", Arrays.toString(ranges));
+
+    ranges = new int[] { 2, 3, 10, 12 };
+    adjusted = MappingUtils.removeStartPositions(1, ranges);
+    assertEquals("[3, 3, 10, 12]", Arrays.toString(adjusted));
+    assertEquals("[2, 3, 10, 12]", Arrays.toString(ranges));
+
+    ranges = new int[] { 2, 2, 8, 12 };
+    adjusted = MappingUtils.removeStartPositions(1, ranges);
+    assertEquals("[8, 12]", Arrays.toString(adjusted));
+    assertEquals("[2, 2, 8, 12]", Arrays.toString(ranges));
+
+    ranges = new int[] { 2, 2, 8, 12 };
+    adjusted = MappingUtils.removeStartPositions(2, ranges);
+    assertEquals("[9, 12]", Arrays.toString(adjusted));
+    assertEquals("[2, 2, 8, 12]", Arrays.toString(ranges));
+
+    ranges = new int[] { 2, 2, 4, 4, 9, 12 };
+    adjusted = MappingUtils.removeStartPositions(1, ranges);
+    assertEquals("[4, 4, 9, 12]", Arrays.toString(adjusted));
+    assertEquals("[2, 2, 4, 4, 9, 12]", Arrays.toString(ranges));
+
+    ranges = new int[] { 2, 2, 4, 4, 9, 12 };
+    adjusted = MappingUtils.removeStartPositions(2, ranges);
+    assertEquals("[9, 12]", Arrays.toString(adjusted));
+    assertEquals("[2, 2, 4, 4, 9, 12]", Arrays.toString(ranges));
+
+    ranges = new int[] { 2, 3, 9, 12 };
+    adjusted = MappingUtils.removeStartPositions(3, ranges);
+    assertEquals("[10, 12]", Arrays.toString(adjusted));
+    assertEquals("[2, 3, 9, 12]", Arrays.toString(ranges));
   }
+
+  /**
+   * Test the method that drops positions from the start of a mapped range, on
+   * the reverse strand
+   */
+  @Test(groups = "Functional")
+  public void testRemoveStartPositions_reverseStrand()
+  {
+    int[] ranges = new int[] { 10, 1 };
+    int[] adjusted = MappingUtils.removeStartPositions(0, ranges);
+    assertEquals("[10, 1]", Arrays.toString(adjusted));
+    assertEquals("[10, 1]", Arrays.toString(ranges));
+  
+    ranges = adjusted;
+    adjusted = MappingUtils.removeStartPositions(1, ranges);
+    assertEquals("[9, 1]", Arrays.toString(adjusted));
+    assertEquals("[10, 1]", Arrays.toString(ranges));
+  
+    ranges = adjusted;
+    adjusted = MappingUtils.removeStartPositions(1, ranges);
+    assertEquals("[8, 1]", Arrays.toString(adjusted));
+    assertEquals("[9, 1]", Arrays.toString(ranges));
+  
+    ranges = new int[] { 12, 11, 9, 6 };
+    adjusted = MappingUtils.removeStartPositions(1, ranges);
+    assertEquals("[11, 11, 9, 6]", Arrays.toString(adjusted));
+    assertEquals("[12, 11, 9, 6]", Arrays.toString(ranges));
+  
+    ranges = new int[] { 12, 12, 8, 4 };
+    adjusted = MappingUtils.removeStartPositions(1, ranges);
+    assertEquals("[8, 4]", Arrays.toString(adjusted));
+    assertEquals("[12, 12, 8, 4]", Arrays.toString(ranges));
+  
+    ranges = new int[] { 12, 12, 8, 4 };
+    adjusted = MappingUtils.removeStartPositions(2, ranges);
+    assertEquals("[7, 4]", Arrays.toString(adjusted));
+    assertEquals("[12, 12, 8, 4]", Arrays.toString(ranges));
+  
+    ranges = new int[] { 12, 12, 10, 10, 8, 4 };
+    adjusted = MappingUtils.removeStartPositions(1, ranges);
+    assertEquals("[10, 10, 8, 4]", Arrays.toString(adjusted));
+    assertEquals("[12, 12, 10, 10, 8, 4]", Arrays.toString(ranges));
+  
+    ranges = new int[] { 12, 12, 10, 10, 8, 4 };
+    adjusted = MappingUtils.removeStartPositions(2, ranges);
+    assertEquals("[8, 4]", Arrays.toString(adjusted));
+    assertEquals("[12, 12, 10, 10, 8, 4]", Arrays.toString(ranges));
+  
+    ranges = new int[] { 12, 11, 8, 4 };
+    adjusted = MappingUtils.removeStartPositions(3, ranges);
+    assertEquals("[7, 4]", Arrays.toString(adjusted));
+    assertEquals("[12, 11, 8, 4]", Arrays.toString(ranges));
+  }
+
 }