JAL-1452 nucleotide/protein default colour; JAL-845 SplitFrame refactor
[jalview.git] / test / jalview / util / MapListTest.java
index 2c8e207..1913a70 100644 (file)
@@ -1,6 +1,13 @@
 package jalview.util;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.List;
 
 import org.junit.Assert;
 import org.junit.Test;
@@ -207,7 +214,264 @@ public class MapListTest
         System.out.print("\n");
       }
     }
-  
   }
 
+  /**
+   * Tests for method that locates ranges in the 'from' map for given range in
+   * the 'to' map.
+   */
+  @Test
+  public void testLocateInFrom_noIntrons()
+  {
+    /*
+     * Simple mapping with no introns
+     */
+    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)));
+    assertEquals("[7, 9]", Arrays.toString(ml.locateInFrom(3, 3)));
+    assertEquals("[10, 12]", Arrays.toString(ml.locateInFrom(4, 4)));
+    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("[4, 9]", Arrays.toString(ml.locateInFrom(2, 3)));
+    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)));
+
+    assertNull(ml.locateInFrom(0, 0));
+    assertNull(ml.locateInFrom(1, 5));
+    assertNull(ml.locateInFrom(-1, 1));
+  }
+
+  /**
+   * Tests for method that locates ranges in the 'from' map for given range in
+   * the 'to' map.
+   */
+  @Test
+  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 };
+    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)));
+    assertEquals("[10, 10, 12, 12, 14, 14]",
+            Arrays.toString(ml.locateInFrom(3, 3)));
+    assertEquals("[16, 18]", Arrays.toString(ml.locateInFrom(4, 4)));
+  }
+
+  /**
+   * Tests for method that locates ranges in the 'to' map for given range in the
+   * 'from' map.
+   */
+  @Test
+  public void testLocateInTo_noIntrons()
+  {
+    /*
+     * Simple mapping with no introns
+     */
+    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)));
+    assertEquals("[3, 3]", Arrays.toString(ml.locateInTo(7, 9)));
+    assertEquals("[4, 4]", Arrays.toString(ml.locateInTo(10, 12)));
+    assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 6)));
+    assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(1, 9)));
+    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)));
+
+    /*
+     * A part codon is treated as if a whole one.
+     */
+    assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 1)));
+    assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(1, 2)));
+    assertEquals("[1, 2]", Arrays.toString(ml.locateInTo(1, 4)));
+    assertEquals("[1, 3]", Arrays.toString(ml.locateInTo(2, 8)));
+    assertEquals("[1, 4]", Arrays.toString(ml.locateInTo(3, 11)));
+    assertEquals("[2, 4]", Arrays.toString(ml.locateInTo(5, 11)));
+
+    assertNull(ml.locateInTo(0, 0));
+    assertNull(ml.locateInTo(1, 13));
+    assertNull(ml.locateInTo(-1, 1));
+  }
+
+  /**
+   * Tests for method that locates ranges in the 'to' map for given range in the
+   * 'from' map.
+   */
+  @Test
+  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 };
+    /*
+     * Mapped proteins at positions 1, 3, 4, 6 in the sequence
+     */
+    int[] protein =
+    { 1, 1, 3, 4, 6, 6 };
+    MapList ml = new MapList(codons, protein, 3, 1);
+
+    /*
+     * Can't map from an unmapped position
+     */
+    assertNull(ml.locateInTo(1, 2));
+    assertNull(ml.locateInTo(2, 4));
+    assertNull(ml.locateInTo(4, 4));
+
+    /*
+     * 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)));
+    assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(3, 5)));
+    assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 3)));
+    assertEquals("[1, 1]", Arrays.toString(ml.locateInTo(2, 5)));
+
+    // codon position 6 starts the next protein:
+    assertEquals("[1, 1, 3, 3]", Arrays.toString(ml.locateInTo(3, 6)));
+
+    // 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)));
+
+  }
+
+  /**
+   * Test equals method.
+   */
+  @Test
+  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 };
+    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
+    MapList ml3 = new MapList(codons, protein, 3, 2); // toRatio differs
+    codons[2] = 4;
+    MapList ml6 = new MapList(codons, protein, 3, 1); // fromShifts differ
+    protein[1] = 3;
+    MapList ml7 = new MapList(codons, protein, 3, 1); // toShifts differ
+
+    assertTrue(ml.equals(ml));
+    assertTrue(ml.equals(ml1));
+    assertTrue(ml1.equals(ml));
+
+    assertFalse(ml.equals(null));
+    assertFalse(ml.equals("hello"));
+    assertFalse(ml.equals(ml2));
+    assertFalse(ml.equals(ml3));
+    assertFalse(ml.equals(ml6));
+    assertFalse(ml.equals(ml7));
+    assertFalse(ml6.equals(ml7));
+
+    try
+    {
+      MapList ml4 = new MapList(codons, null, 3, 1); // toShifts null
+      assertFalse(ml.equals(ml4));
+    } catch (NullPointerException e)
+    {
+      // actually thrown by constructor before equals can be called
+    }
+    try
+    {
+      MapList ml5 = new MapList(null, protein, 3, 1); // fromShifts null
+      assertFalse(ml.equals(ml5));
+    } catch (NullPointerException e)
+    {
+      // actually thrown by constructor before equals can be called
+    }
+  }
+
+  /**
+   * Test for the method that flattens a list of ranges into a single array.
+   */
+  @Test
+  public void testGetRanges()
+  {
+    List<int[]> ranges = new ArrayList<int[]>();
+    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
+  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 };
+    MapList ml = new MapList(codons, protein, 3, 1);
+    assertEquals(3, ml.getFromRatio());
+    assertEquals(2, ml.getFromLowest());
+    assertEquals(18, ml.getFromHighest());
+    assertEquals(1, ml.getToLowest());
+    assertEquals(6, ml.getToHighest());
+    assertEquals("[2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18]",
+            Arrays.toString(ml.getFromRanges()));
+    assertEquals("[1, 1, 3, 4, 6, 6]", Arrays.toString(ml.getToRanges()));
+
+    /*
+     * Also copy constructor
+     */
+    MapList ml2 = new MapList(ml);
+    assertEquals(3, ml2.getFromRatio());
+    assertEquals(2, ml2.getFromLowest());
+    assertEquals(18, ml2.getFromHighest());
+    assertEquals(1, ml2.getToLowest());
+    assertEquals(6, ml2.getToHighest());
+    assertEquals("[2, 3, 5, 7, 9, 10, 12, 12, 14, 14, 16, 18]",
+            Arrays.toString(ml2.getFromRanges()));
+    assertEquals("[1, 1, 3, 4, 6, 6]", Arrays.toString(ml2.getToRanges()));
+  }
+
+  /**
+   * Test the method that creates an inverse mapping
+   */
+  @Test
+  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 };
+
+    MapList ml = new MapList(codons, protein, 3, 1);
+    MapList ml2 = ml.getInverse();
+    assertEquals(ml.getFromRatio(), ml2.getToRatio());
+    assertEquals(ml.getFromRatio(), ml2.getToRatio());
+    assertEquals(ml.getToHighest(), ml2.getFromHighest());
+    assertEquals(ml.getFromHighest(), ml2.getToHighest());
+    assertEquals(Arrays.toString(ml.getFromRanges()),
+            Arrays.toString(ml2.getToRanges()));
+    assertEquals(Arrays.toString(ml.getToRanges()),
+            Arrays.toString(ml2.getFromRanges()));
+  }
 }