X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=inline;f=test%2Fjalview%2Futil%2FMapListTest.java;h=a2f38e2269ebc0c239cb5ca89db0ea70165db1d2;hb=8c865b34fb48c55582c2c77609cd04f96819ceef;hp=6e7e19e7481274c3b26dacb2a29444154fbc2989;hpb=ab22918ab8fc67d30dad1fb1ae0f37e51f49df95;p=jalview.git
diff --git a/test/jalview/util/MapListTest.java b/test/jalview/util/MapListTest.java
index 6e7e19e..a2f38e2 100644
--- a/test/jalview/util/MapListTest.java
+++ b/test/jalview/util/MapListTest.java
@@ -1,40 +1,67 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ *
+ * This file is part of Jalview.
+ *
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * Jalview is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview. If not, see .
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
package jalview.util;
import static org.testng.AssertJUnit.assertEquals;
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.internal.junit.ArrayAsserts.assertArrayEquals;
+
+import jalview.gui.JvOptionPane;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
+import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class MapListTest
{
- @Test(groups ={ "Functional" })
+ @BeforeClass(alwaysRun = true)
+ public void setUpJvOptionPane()
+ {
+ JvOptionPane.setInteractiveMode(false);
+ JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
+ }
+
+ @Test(groups = { "Functional" })
public void testSomething()
{
- MapList ml = new MapList(new int[]
- { 1, 5, 10, 15, 25, 20 }, new int[]
- { 51, 1 }, 1, 3);
- MapList ml1 = new MapList(new int[]
- { 1, 3, 17, 4 }, new int[]
- { 51, 1 }, 1, 3);
- MapList ml2 = new MapList(new int[]
- { 1, 60 }, new int[]
- { 1, 20 }, 3, 1);
+ MapList ml = new MapList(new int[] { 1, 5, 10, 15, 25, 20 }, new int[] {
+ 51, 1 }, 1, 3);
+ MapList ml1 = new MapList(new int[] { 1, 3, 17, 4 },
+ new int[] { 51, 1 }, 1, 3);
+ MapList ml2 = new MapList(new int[] { 1, 60 }, new int[] { 1, 20 }, 3,
+ 1);
// test internal consistency
int to[] = new int[51];
testMap(ml, 1, 60);
- MapList mldna = new MapList(new int[]
- { 2, 2, 6, 8, 12, 16 }, new int[]
+ MapList mldna = new MapList(new int[] { 2, 2, 6, 8, 12, 16 }, new int[]
{ 1, 3 }, 3, 1);
int[] frm = mldna.locateInFrom(1, 1);
- testLocateFrom(mldna, 1, 1, new int[]
- { 2, 2, 6, 7 });
+ testLocateFrom(mldna, 1, 1, new int[] { 2, 2, 6, 7 });
testMap(mldna, 1, 3);
/*
* for (int from=1; from<=51; from++) { int[] too=ml.shiftTo(from); int[]
@@ -99,7 +126,7 @@ public class MapListTest
if (mmap[1][i - 1] == -1)
{
System.out.print(i + "=XXX");
-
+
}
else
{
@@ -160,7 +187,7 @@ public class MapListTest
if (mmap[1][i - 1] == -1)
{
System.out.print(i + "=XXX");
-
+
}
else
{
@@ -219,16 +246,14 @@ public class MapListTest
* Tests for method that locates ranges in the 'from' map for given range in
* the 'to' map.
*/
- @Test(groups ={ "Functional" })
+ @Test(groups = { "Functional" })
public void testLocateInFrom_noIntrons()
{
/*
* Simple mapping with no introns
*/
- int[] codons = new int[]
- { 1, 12 };
- int[] protein = new int[]
- { 1, 4 };
+ int[] codons = new int[] { 1, 12 };
+ int[] protein = new int[] { 1, 4 };
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)));
@@ -251,17 +276,15 @@ public class MapListTest
* Tests for method that locates ranges in the 'from' map for given range in
* the 'to' map.
*/
- @Test(groups ={ "Functional" })
+ @Test(groups = { "Functional" })
public void testLocateInFrom_withIntrons()
{
/*
* Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
* 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
*/
- int[] codons =
- { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
- int[] protein =
- { 1, 4 };
+ int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
+ int[] protein = { 1, 4 };
MapList ml = new MapList(codons, protein, 3, 1);
assertEquals("[2, 3, 5, 5]", Arrays.toString(ml.locateInFrom(1, 1)));
assertEquals("[6, 7, 9, 9]", Arrays.toString(ml.locateInFrom(2, 2)));
@@ -274,16 +297,14 @@ public class MapListTest
* Tests for method that locates ranges in the 'to' map for given range in the
* 'from' map.
*/
- @Test(groups ={ "Functional" })
+ @Test(groups = { "Functional" })
public void testLocateInTo_noIntrons()
{
/*
* Simple mapping with no introns
*/
- int[] codons = new int[]
- { 1, 12 };
- int[] protein = new int[]
- { 1, 4 };
+ int[] codons = new int[] { 1, 12 };
+ int[] protein = new int[] { 1, 4 };
MapList ml = new MapList(codons, protein, 3, 1);
assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 3)));
assertEquals("[2, 2]", Arrays.toString(ml.locateInTo(4, 6)));
@@ -314,20 +335,18 @@ public class MapListTest
* Tests for method that locates ranges in the 'to' map for given range in the
* 'from' map.
*/
- @Test(groups ={ "Functional" })
+ @Test(groups = { "Functional" })
public void testLocateInTo_withIntrons()
{
/*
* Exons at positions [2, 3, 5] [6, 7, 9] [10, 12, 14] [16, 17, 18] i.e.
* 2-3, 5-7, 9-10, 12-12, 14-14, 16-18
*/
- int[] codons =
- { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
+ int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
/*
* Mapped proteins at positions 1, 3, 4, 6 in the sequence
*/
- int[] protein =
- { 1, 1, 3, 4, 6, 6 };
+ int[] protein = { 1, 1, 3, 4, 6, 6 };
MapList ml = new MapList(codons, protein, 3, 1);
/*
@@ -357,13 +376,11 @@ public class MapListTest
/**
* Test equals method.
*/
- @Test(groups ={ "Functional" })
+ @Test(groups = { "Functional" })
public void testEquals()
{
- int[] codons = new int[]
- { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
- int[] protein = new int[]
- { 1, 4 };
+ int[] codons = new int[] { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
+ int[] protein = new int[] { 1, 4 };
MapList ml = new MapList(codons, protein, 3, 1);
MapList ml1 = new MapList(codons, protein, 3, 1); // same values
MapList ml2 = new MapList(codons, protein, 2, 1); // fromRatio differs
@@ -406,27 +423,23 @@ public class MapListTest
/**
* Test for the method that flattens a list of ranges into a single array.
*/
- @Test(groups ={ "Functional" })
+ @Test(groups = { "Functional" })
public void testGetRanges()
{
List ranges = new ArrayList();
- ranges.add(new int[]
- { 2, 3 });
- ranges.add(new int[]
- { 5, 6 });
+ ranges.add(new int[] { 2, 3 });
+ ranges.add(new int[] { 5, 6 });
assertEquals("[2, 3, 5, 6]", Arrays.toString(MapList.getRanges(ranges)));
}
/**
* Check state after construction
*/
- @Test(groups ={ "Functional" })
+ @Test(groups = { "Functional" })
public void testConstructor()
{
- int[] codons =
- { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
- int[] protein =
- { 1, 1, 3, 4, 6, 6 };
+ int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
+ int[] protein = { 1, 1, 3, 4, 6, 6 };
MapList ml = new MapList(codons, protein, 3, 1);
assertEquals(3, ml.getFromRatio());
assertEquals(2, ml.getFromLowest());
@@ -449,6 +462,36 @@ public class MapListTest
assertEquals("{[2, 3], [5, 7], [9, 10], [12, 12], [14, 14], [16, 18]}",
prettyPrint(ml2.getFromRanges()));
assertEquals("{[1, 1], [3, 4], [6, 6]}", prettyPrint(ml2.getToRanges()));
+
+ /*
+ * reverse direction
+ */
+ codons = new int[] { 9, 6 };
+ protein = new int[] { 100, 91, 80, 79 };
+ ml = new MapList(codons, protein, 3, 1);
+ assertEquals(6, ml.getFromLowest());
+ assertEquals(9, ml.getFromHighest());
+ assertEquals(79, ml.getToLowest());
+ assertEquals(100, ml.getToHighest());
+ }
+
+ /**
+ * Test constructor can merge consecutive ranges
+ */
+ @Test(groups = { "Functional" })
+ public void testConstructor_mergeRanges()
+ {
+ int[] codons = { 2, 3, 3, 7, 9, 10, 12, 12, 14, 14, 16, 17 };
+ int[] protein = { 1, 1, 1, 3, 6, 6 };
+ MapList ml = new MapList(codons, protein, 3, 1);
+ assertEquals(3, ml.getFromRatio());
+ assertEquals(2, ml.getFromLowest());
+ assertEquals(17, ml.getFromHighest());
+ assertEquals(1, ml.getToLowest());
+ assertEquals(6, ml.getToHighest());
+ assertEquals("{[2, 7], [9, 10], [12, 12], [14, 14], [16, 17]}",
+ prettyPrint(ml.getFromRanges()));
+ assertEquals("{[1, 3], [6, 6]}", prettyPrint(ml.getToRanges()));
}
/**
@@ -478,13 +521,11 @@ public class MapListTest
/**
* Test the method that creates an inverse mapping
*/
- @Test(groups ={ "Functional" })
+ @Test(groups = { "Functional" })
public void testGetInverse()
{
- int[] codons =
- { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
- int[] protein =
- { 1, 1, 3, 4, 6, 6 };
+ int[] codons = { 2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18 };
+ int[] protein = { 1, 1, 3, 4, 6, 6 };
MapList ml = new MapList(codons, protein, 3, 1);
MapList ml2 = ml.getInverse();
@@ -498,14 +539,279 @@ public class MapListTest
prettyPrint(ml2.getFromRanges()));
}
- @Test(groups ={ "Functional" })
+ @Test(groups = { "Functional" })
public void testToString()
{
- MapList ml = new MapList(new int[]
- { 1, 5, 10, 15, 25, 20 }, new int[]
- { 51, 1 }, 1, 3);
+ MapList ml = new MapList(new int[] { 1, 5, 10, 15, 25, 20 }, new int[] {
+ 51, 1 }, 1, 3);
String s = ml.toString();
- assertEquals("From (1:3) [ [1, 5] [10, 15] [25, 20] ] To [ [51, 1] ]",
+ assertEquals("[ [1, 5] [10, 15] [25, 20] ] 1:3 to [ [51, 1] ]", s);
+ }
+
+ @Test(groups = { "Functional" })
+ public void testAddMapList()
+ {
+ MapList ml = new MapList(new int[] { 11, 15, 20, 25, 35, 30 },
+ new int[] { 72, 22 }, 1, 3);
+ assertEquals(11, ml.getFromLowest());
+ assertEquals(35, ml.getFromHighest());
+ assertEquals(22, ml.getToLowest());
+ assertEquals(72, ml.getToHighest());
+
+ MapList ml2 = new MapList(new int[] { 2, 4, 37, 40 }, new int[] { 12,
+ 17, 78, 83, 88, 96 }, 1, 3);
+ ml.addMapList(ml2);
+ assertEquals(2, ml.getFromLowest());
+ assertEquals(40, ml.getFromHighest());
+ assertEquals(12, ml.getToLowest());
+ assertEquals(96, ml.getToHighest());
+
+ String s = ml.toString();
+ assertEquals(
+ "[ [11, 15] [20, 25] [35, 30] [2, 4] [37, 40] ] 1:3 to [ [72, 22] [12, 17] [78, 83] [88, 96] ]",
s);
}
+
+ /**
+ * Test that confirms adding a map twice does nothing
+ */
+ @Test(groups = { "Functional" })
+ public void testAddMapList_sameMap()
+ {
+ MapList ml = new MapList(new int[] { 11, 15, 20, 25, 35, 30 },
+ new int[] { 72, 22 }, 1, 3);
+ String before = ml.toString();
+ ml.addMapList(ml);
+ assertEquals(before, ml.toString());
+ ml.addMapList(new MapList(ml));
+ assertEquals(before, ml.toString());
+ }
+
+ @Test(groups = { "Functional" })
+ public void testAddMapList_contiguous()
+ {
+ MapList ml = new MapList(new int[] { 11, 15 }, new int[] { 72, 58 }, 1,
+ 3);
+
+ MapList ml2 = new MapList(new int[] { 15, 16 }, new int[] { 58, 53 },
+ 1, 3);
+ ml.addMapList(ml2);
+ assertEquals("[ [11, 16] ] 1:3 to [ [72, 53] ]", ml.toString());
+ }
+
+ @Test(groups = "Functional")
+ public void testAddRange()
+ {
+ int[] range = { 1, 5 };
+ List ranges = new ArrayList();
+
+ // add to empty list:
+ MapList.addRange(range, ranges);
+ assertEquals(1, ranges.size());
+ assertSame(range, ranges.get(0));
+
+ // extend contiguous (same position):
+ MapList.addRange(new int[] { 5, 10 }, ranges);
+ assertEquals(1, ranges.size());
+ assertEquals(1, ranges.get(0)[0]);
+ assertEquals(10, ranges.get(0)[1]);
+
+ // extend contiguous (next position):
+ MapList.addRange(new int[] { 11, 15 }, ranges);
+ assertEquals(1, ranges.size());
+ assertEquals(1, ranges.get(0)[0]);
+ assertEquals(15, ranges.get(0)[1]);
+
+ // change direction: range is not merged:
+ MapList.addRange(new int[] { 16, 10 }, ranges);
+ assertEquals(2, ranges.size());
+ assertEquals(16, ranges.get(1)[0]);
+ assertEquals(10, ranges.get(1)[1]);
+
+ // extend reverse contiguous (same position):
+ MapList.addRange(new int[] { 10, 8 }, ranges);
+ assertEquals(2, ranges.size());
+ assertEquals(16, ranges.get(1)[0]);
+ assertEquals(8, ranges.get(1)[1]);
+
+ // extend reverse contiguous (next position):
+ MapList.addRange(new int[] { 7, 6 }, ranges);
+ assertEquals(2, ranges.size());
+ assertEquals(16, ranges.get(1)[0]);
+ assertEquals(6, ranges.get(1)[1]);
+
+ // change direction: range is not merged:
+ MapList.addRange(new int[] { 6, 9 }, ranges);
+ assertEquals(3, ranges.size());
+ assertEquals(6, ranges.get(2)[0]);
+ assertEquals(9, ranges.get(2)[1]);
+
+ // not contiguous: not merged
+ MapList.addRange(new int[] { 11, 12 }, ranges);
+ assertEquals(4, ranges.size());
+ assertEquals(11, ranges.get(3)[0]);
+ assertEquals(12, ranges.get(3)[1]);
+ }
+
+ /**
+ * Check state after construction
+ */
+ @Test(groups = { "Functional" })
+ public void testConstructor_withLists()
+ {
+ /*
+ * reverse direction
+ */
+ int[][] codons = new int[][] { { 9, 6 } };
+ int[][] protein = new int[][] { { 100, 91 }, { 80, 79 } };
+ MapList ml = new MapList(Arrays.asList(codons), Arrays.asList(protein),
+ 3, 1);
+ assertEquals(6, ml.getFromLowest());
+ assertEquals(9, ml.getFromHighest());
+ assertEquals(79, ml.getToLowest());
+ assertEquals(100, ml.getToHighest());
+ }
+
+ /**
+ * Test that method that inspects for the (first) forward or reverse from
+ * range. Single position ranges are ignored.
+ */
+ @Test(groups = { "Functional" })
+ public void testIsFromForwardStrand()
+ {
+ // [3-9] declares forward strand
+ MapList ml = new MapList(new int[] { 2, 2, 3, 9, 12, 11 }, new int[] {
+ 20, 11 }, 1, 1);
+ assertTrue(ml.isFromForwardStrand());
+
+ // [11-5] declares reverse strand ([13-14] is ignored)
+ ml = new MapList(new int[] { 2, 2, 11, 5, 13, 14 },
+ new int[] { 20, 11 }, 1, 1);
+ assertFalse(ml.isFromForwardStrand());
+
+ // all single position ranges - defaults to forward strand
+ ml = new MapList(new int[] { 2, 2, 4, 4, 6, 6 }, new int[] { 3, 1 }, 1,
+ 1);
+ assertTrue(ml.isFromForwardStrand());
+ }
+
+ /**
+ * Test the method that merges a list of ranges where possible
+ */
+ @Test(groups = { "Functional" })
+ public void testCoalesceRanges()
+ {
+ assertNull(MapList.coalesceRanges(null));
+ List ranges = new ArrayList();
+ assertSame(ranges, MapList.coalesceRanges(ranges));
+ ranges.add(new int[] { 1, 3 });
+ assertSame(ranges, MapList.coalesceRanges(ranges));
+
+ // add non-contiguous range:
+ ranges.add(new int[] { 5, 6 });
+ assertSame(ranges, MapList.coalesceRanges(ranges));
+
+ // 'contiguous' range in opposite direction is not merged:
+ ranges.add(new int[] { 7, 6 });
+ assertSame(ranges, MapList.coalesceRanges(ranges));
+
+ // merging in forward direction:
+ ranges.clear();
+ ranges.add(new int[] { 1, 3 });
+ ranges.add(new int[] { 4, 5 });
+ ranges.add(new int[] { 5, 5 });
+ ranges.add(new int[] { 5, 7 });
+ List merged = MapList.coalesceRanges(ranges);
+ assertEquals(1, merged.size());
+ assertArrayEquals(new int[] { 1, 7 }, merged.get(0));
+ // verify input list is unchanged
+ assertEquals(4, ranges.size());
+ assertArrayEquals(new int[] { 1, 3 }, ranges.get(0));
+ assertArrayEquals(new int[] { 4, 5 }, ranges.get(1));
+ assertArrayEquals(new int[] { 5, 5 }, ranges.get(2));
+ assertArrayEquals(new int[] { 5, 7 }, ranges.get(3));
+
+ // merging in reverse direction:
+ ranges.clear();
+ ranges.add(new int[] { 7, 5 });
+ ranges.add(new int[] { 5, 4 });
+ ranges.add(new int[] { 4, 4 });
+ ranges.add(new int[] { 3, 1 });
+ merged = MapList.coalesceRanges(ranges);
+ assertEquals(1, merged.size());
+ assertArrayEquals(new int[] { 7, 1 }, merged.get(0));
+
+ // merging with switches of direction:
+ ranges.clear();
+ ranges.add(new int[] { 1, 3 });
+ ranges.add(new int[] { 4, 5 });
+ ranges.add(new int[] { 5, 5 });
+ ranges.add(new int[] { 6, 6 });
+ ranges.add(new int[] { 12, 10 });
+ ranges.add(new int[] { 9, 8 });
+ ranges.add(new int[] { 8, 8 });
+ ranges.add(new int[] { 7, 7 });
+ merged = MapList.coalesceRanges(ranges);
+ assertEquals(2, merged.size());
+ assertArrayEquals(new int[] { 1, 6 }, merged.get(0));
+ assertArrayEquals(new int[] { 12, 7 }, merged.get(1));
+ }
+
+ /**
+ * Test the method that merges a list of ranges where possible
+ */
+ @Test(groups = { "Functional" })
+ public void testCoalesceRanges_withOverlap()
+ {
+ List ranges = new ArrayList();
+ ranges.add(new int[] { 1, 3 });
+ ranges.add(new int[] { 2, 5 });
+
+ /*
+ * [2, 5] should extend [1, 3]
+ */
+ List merged = MapList.coalesceRanges(ranges);
+ assertEquals(1, merged.size());
+ assertArrayEquals(new int[] { 1, 5 }, merged.get(0));
+
+ /*
+ * a subsumed interval should be dropped
+ */
+ ranges.clear();
+ ranges.add(new int[] { 1, 6 });
+ ranges.add(new int[] { 2, 4 });
+ merged = MapList.coalesceRanges(ranges);
+ assertEquals(1, merged.size());
+ assertArrayEquals(new int[] { 1, 6 }, merged.get(0));
+
+ ranges.clear();
+ ranges.add(new int[] { 1, 5 });
+ ranges.add(new int[] { 1, 6 });
+ merged = MapList.coalesceRanges(ranges);
+ assertEquals(1, merged.size());
+ assertArrayEquals(new int[] { 1, 6 }, merged.get(0));
+
+ /*
+ * merge duplicate ranges
+ */
+ ranges.clear();
+ ranges.add(new int[] { 1, 3 });
+ ranges.add(new int[] { 1, 3 });
+ merged = MapList.coalesceRanges(ranges);
+ assertEquals(1, merged.size());
+ assertArrayEquals(new int[] { 1, 3 }, merged.get(0));
+
+ /*
+ * reverse direction
+ */
+ ranges.clear();
+ ranges.add(new int[] { 9, 5 });
+ ranges.add(new int[] { 9, 4 });
+ ranges.add(new int[] { 8, 3 });
+ ranges.add(new int[] { 3, 2 });
+ ranges.add(new int[] { 1, 0 });
+ merged = MapList.coalesceRanges(ranges);
+ assertEquals(1, merged.size());
+ assertArrayEquals(new int[] { 9, 0 }, merged.get(0));
+ }
}