X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=test%2Fjalview%2Futil%2FMapListTest.java;h=bca778d6fb69c00287a74e00a4a2b0417ccbccbc;hb=04efe1c889de25d44f65ee405eff249e399fe29f;hp=71fbdfdbed6533bfbe72e2a244226c7b8a4c3af6;hpb=df140c55ea500ff056e0602de542c13515ed1b55;p=jalview.git diff --git a/test/jalview/util/MapListTest.java b/test/jalview/util/MapListTest.java index 71fbdfd..bca778d 100644 --- a/test/jalview/util/MapListTest.java +++ b/test/jalview/util/MapListTest.java @@ -25,6 +25,7 @@ import static org.testng.AssertJUnit.assertFalse; import static org.testng.AssertJUnit.assertNull; import static org.testng.AssertJUnit.assertSame; import static org.testng.AssertJUnit.assertTrue; +import static org.testng.AssertJUnit.fail; import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals; import java.util.ArrayList; @@ -44,7 +45,7 @@ public class MapListTest { Cache.initLogger(); } - + @BeforeClass(alwaysRun = true) public void setUpJvOptionPane() { @@ -596,7 +597,8 @@ public class MapListTest public void testAddMapList_sameMap() { MapList ml = new MapList(new int[] { 11, 15, 20, 25, 35, 30 }, - new int[] { 72, 22 }, 1, 3); + new int[] + { 72, 22 }, 1, 3); String before = ml.toString(); ml.addMapList(ml); assertEquals(before, ml.toString()); @@ -779,11 +781,11 @@ public class MapListTest assertArrayEquals(new int[] { 5, 6 }, merged.get(1)); assertArrayEquals(new int[] { 12, 8 }, merged.get(2)); assertArrayEquals(new int[] { 8, 7 }, merged.get(3)); - + // 'subsumed' ranges are preserved ranges.clear(); ranges.add(new int[] { 10, 30 }); - ranges.add(new int[] { 15, 25 }); + ranges.add(new int[] { 15, 25 }); merged = MapList.coalesceRanges(ranges); assertEquals(2, merged.size()); assertArrayEquals(new int[] { 10, 30 }, merged.get(0)); @@ -957,4 +959,130 @@ public class MapListTest 1); assertTrue(ml.isToForwardStrand()); } + + /** + * Test for mapping with overlapping ranges + */ + @Test(groups = { "Functional" }) + public void testLocateInFrom_withOverlap() + { + int[] codons = new int[] { 1, 12, 12, 17 }; + int[] protein = new int[] { 1, 6 }; + MapList ml = new MapList(codons, protein, 3, 1); + assertEquals("[1, 3]", Arrays.toString(ml.locateInFrom(1, 1))); + assertEquals("[4, 6]", Arrays.toString(ml.locateInFrom(2, 2))); + assertEquals("[7, 9]", Arrays.toString(ml.locateInFrom(3, 3))); + assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4))); + assertEquals("[12, 14]", Arrays.toString(ml.locateInFrom(5, 5))); + assertEquals("[15, 17]", Arrays.toString(ml.locateInFrom(6, 6))); + assertEquals("[1, 6]", Arrays.toString(ml.locateInFrom(1, 2))); + assertEquals("[1, 9]", Arrays.toString(ml.locateInFrom(1, 3))); + assertEquals("[1, 12]", Arrays.toString(ml.locateInFrom(1, 4))); + assertEquals("[1, 12, 12, 14]", Arrays.toString(ml.locateInFrom(1, 5))); + assertEquals("[1, 12, 12, 17]", Arrays.toString(ml.locateInFrom(1, 6))); + assertEquals("[4, 9]", Arrays.toString(ml.locateInFrom(2, 3))); + assertEquals("[7, 12, 12, 17]", Arrays.toString(ml.locateInFrom(3, 6))); + + assertNull(ml.locateInFrom(0, 0)); + assertNull(ml.locateInFrom(1, 7)); + assertNull(ml.locateInFrom(-1, 1)); + } + + /** + * Test for mapping with overlapping ranges + */ + @Test(groups = { "Functional" }) + public void testLocateInTo_withOverlap() + { + int[] codons = new int[] { 1, 12, 12, 17 }; + int[] protein = new int[] { 1, 6 }; + MapList ml = new MapList(codons, protein, 3, 1); + assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 1))); + assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(3, 8))); + assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(2, 11))); + assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(3, 11))); + + // we want base 12 to map to both of the amino acids it codes for + assertEquals("[4, 5]", Arrays.toString(ml.locateInTo(12, 12))); + assertEquals("[4, 5]", Arrays.toString(ml.locateInTo(11, 12))); + assertEquals("[4, 6]", Arrays.toString(ml.locateInTo(11, 15))); + assertEquals("[6, 6]", Arrays.toString(ml.locateInTo(15, 17))); + + assertNull(ml.locateInTo(0, 0)); + assertNull(ml.locateInTo(1, 18)); + assertNull(ml.locateInTo(-1, 1)); + } + + @Test(groups = { "Functional" }) + public void testTraverseToPosition() + { + List ranges = new ArrayList<>(); + assertNull(MapList.traverseToPosition(ranges, 0)); + + ranges.add(new int[] { 3, 6 }); + assertNull(MapList.traverseToPosition(ranges, 0)); + } + + @Test(groups = { "Functional" }) + public void testCountPos() + { + try + { + MapList.countPos(null, 1); + fail("expected exception"); + } catch (NullPointerException e) + { + // expected + } + + List intervals = new ArrayList<>(); + assertNull(MapList.countPos(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)); + + intervals.add(new int[] {25, 25}); + assertArrayEquals(new int[] {12, 1}, MapList.countPos(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)); + + /* + * 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)); + + /* + * reverse then forward + */ + intervals.add(new int[] {5, 10}); + assertArrayEquals(new int[] {13, 1}, MapList.countPos(intervals, 6)); + + /* + * reverse then forward then reverse + */ + intervals.add(new int[] {-10, -20}); + assertArrayEquals(new int[] {20, -1}, MapList.countPos(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)); + } }