JAL-2089 patch broken merge to master for Release 2.10.0b1
[jalview.git] / test / jalview / util / MappingUtilsTest.java
index 758694a..655aa2a 100644 (file)
@@ -21,6 +21,7 @@
 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;
 
@@ -43,12 +44,9 @@ import jalview.io.FormatAdapter;
 
 import java.awt.Color;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.LinkedHashSet;
 import java.util.List;
-import java.util.Set;
 
 import org.testng.annotations.Test;
 
@@ -77,7 +75,8 @@ public class MappingUtilsTest
     MapList map = new MapList(new int[] { 5, 10 }, new int[] { 12, 13 }, 3,
             1);
     acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
-    Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
+    List<AlignedCodonFrame> acfList = Arrays.asList(new AlignedCodonFrame[]
+    { acf });
 
     /*
      * Check protein residue 12 maps to codon 5-7, 13 to codon 8-10
@@ -129,7 +128,8 @@ public class MappingUtilsTest
     MapList map = new MapList(new int[] { 6, 6, 8, 9, 11, 11, 13, 13, 15,
         15 }, new int[] { 8, 9 }, 3, 1);
     acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
-    Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
+    List<AlignedCodonFrame> acfList = Arrays.asList(new AlignedCodonFrame[]
+    { acf });
 
     /*
      * Check protein residue 8 maps to [6, 8, 9]
@@ -209,7 +209,8 @@ public class MappingUtilsTest
       acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), protein
               .getSequenceAt(seq).getDatasetSequence(), map);
     }
-    Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
+    List<AlignedCodonFrame> acfList = Arrays.asList(new AlignedCodonFrame[]
+    { acf });
 
     AlignViewportI dnaView = new AlignViewport(cdna);
     AlignViewportI proteinView = new AlignViewport(protein);
@@ -343,8 +344,10 @@ public class MappingUtilsTest
   protected void setupMappedAlignments() throws IOException
   {
     /*
-     * Set up dna and protein Seq1/2/3 with mappings (held on the protein
-     * viewport). Lower case for introns.
+     * Map (upper-case = coding):
+     * Seq1/10-18 AC-GctGtC-T to Seq1/40 -K-P
+     * Seq2/20-27 Tc-GA-G-T-T to Seq2/20-27 L--Q
+     * Seq3/30-38 TtTT-AaCGg- to Seq3/60-61\nG--S
      */
     AlignmentI cdna = loadAlignment(">Seq1/10-18\nAC-GctGtC-T\n"
             + ">Seq2/20-27\nTc-GA-G-T-Tc\n" + ">Seq3/30-38\nTtTT-AaCGg-\n",
@@ -373,7 +376,8 @@ public class MappingUtilsTest
         61 }, 3, 1);
     acf.addMap(cdna.getSequenceAt(2).getDatasetSequence(), protein
             .getSequenceAt(2).getDatasetSequence(), map);
-    Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
+    List<AlignedCodonFrame> acfList = Arrays.asList(new AlignedCodonFrame[]
+    { acf });
 
     dnaView = new AlignViewport(cdna);
     proteinView = new AlignViewport(protein);
@@ -474,7 +478,8 @@ public class MappingUtilsTest
       acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), protein
               .getSequenceAt(seq).getDatasetSequence(), map);
     }
-    Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
+    List<AlignedCodonFrame> acfList = Arrays.asList(new AlignedCodonFrame[]
+    { acf });
 
     AlignViewportI dnaView = new AlignViewport(cdna);
     AlignViewportI proteinView = new AlignViewport(protein);
@@ -556,7 +561,8 @@ public class MappingUtilsTest
       acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), protein
               .getSequenceAt(seq).getDatasetSequence(), map);
     }
-    Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
+    List<AlignedCodonFrame> acfList = Arrays.asList(new AlignedCodonFrame[]
+    { acf });
 
     AlignViewportI dnaView = new AlignViewport(cdna);
     AlignViewportI proteinView = new AlignViewport(protein);
@@ -651,7 +657,7 @@ public class MappingUtilsTest
     AlignedCodonFrame acf3 = new AlignedCodonFrame();
     acf3.addMap(seq3.getDatasetSequence(), seq1.getDatasetSequence(), map);
 
-    Set<AlignedCodonFrame> mappings = new HashSet<AlignedCodonFrame>();
+    List<AlignedCodonFrame> mappings = new ArrayList<AlignedCodonFrame>();
     mappings.add(acf1);
     mappings.add(acf2);
     mappings.add(acf3);
@@ -697,6 +703,82 @@ public class MappingUtilsTest
     assertEquals(0, result.size());
   }
 
+  /**
+   * just like the one above, but this time, we provide a set of sequences to
+   * subselect the mapping search
+   */
+  @Test(groups = { "Functional" })
+  public void testFindMappingsForSequenceAndOthers()
+  {
+    SequenceI seq1 = new Sequence("Seq1", "ABC");
+    SequenceI seq2 = new Sequence("Seq2", "ABC");
+    SequenceI seq3 = new Sequence("Seq3", "ABC");
+    SequenceI seq4 = new Sequence("Seq4", "ABC");
+    seq1.createDatasetSequence();
+    seq2.createDatasetSequence();
+    seq3.createDatasetSequence();
+    seq4.createDatasetSequence();
+
+    /*
+     * Create mappings from seq1 to seq2, seq2 to seq1, seq3 to seq1, seq3 to seq4
+     */
+    AlignedCodonFrame acf1 = new AlignedCodonFrame();
+    MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 3 }, 1, 1);
+    acf1.addMap(seq1.getDatasetSequence(), seq2.getDatasetSequence(), map);
+    AlignedCodonFrame acf2 = new AlignedCodonFrame();
+    acf2.addMap(seq2.getDatasetSequence(), seq1.getDatasetSequence(), map);
+    AlignedCodonFrame acf3 = new AlignedCodonFrame();
+    acf3.addMap(seq3.getDatasetSequence(), seq1.getDatasetSequence(), map);
+    AlignedCodonFrame acf4 = new AlignedCodonFrame();
+    acf4.addMap(seq3.getDatasetSequence(), seq4.getDatasetSequence(), map);
+
+    List<AlignedCodonFrame> mappings = new ArrayList<AlignedCodonFrame>();
+    mappings.add(acf1);
+    mappings.add(acf2);
+    mappings.add(acf3);
+    mappings.add(acf4);
+
+    /*
+     * test for null args
+     */
+    List<AlignedCodonFrame> result = MappingUtils
+            .findMappingsForSequenceAndOthers(null, mappings,
+                    Arrays.asList(new SequenceI[] { seq1, seq2 }));
+    assertTrue(result.isEmpty());
+
+    result = MappingUtils.findMappingsForSequenceAndOthers(seq1, null,
+            Arrays.asList(new SequenceI[] { seq1, seq2 }));
+    assertTrue(result.isEmpty());
+
+    /*
+     * Seq1 has three mappings, but filter argument will only accept
+     * those to seq2
+     */
+    result = MappingUtils.findMappingsForSequenceAndOthers(
+            seq1,
+            mappings,
+            Arrays.asList(new SequenceI[] { seq1, seq2,
+                seq1.getDatasetSequence() }));
+    assertEquals(2, result.size());
+    assertTrue(result.contains(acf1));
+    assertTrue(result.contains(acf2));
+    assertFalse("Did not expect to find mapping acf3 - subselect failed",
+            result.contains(acf3));
+    assertFalse(
+            "Did not expect to find mapping acf4 - doesn't involve sequence",
+            result.contains(acf4));
+
+    /*
+     * and verify the no filter case
+     */
+    result = MappingUtils.findMappingsForSequenceAndOthers(seq1, mappings,
+            null);
+    assertEquals(3, result.size());
+    assertTrue(result.contains(acf1));
+    assertTrue(result.contains(acf2));
+    assertTrue(result.contains(acf3));
+  }
+
   @Test(groups = { "Functional" })
   public void testMapEditCommand()
   {
@@ -707,7 +789,7 @@ public class MappingUtilsTest
     AlignedCodonFrame acf = new AlignedCodonFrame();
     MapList map = new MapList(new int[] { 8, 16 }, new int[] { 5, 7 }, 3, 1);
     acf.addMap(dna.getDatasetSequence(), protein.getDatasetSequence(), map);
-    Set<AlignedCodonFrame> mappings = new LinkedHashSet<AlignedCodonFrame>();
+    List<AlignedCodonFrame> mappings = new ArrayList<AlignedCodonFrame>();
     mappings.add(acf);
 
     AlignmentI prot = new Alignment(new SequenceI[] { protein });
@@ -775,4 +857,253 @@ public class MappingUtilsTest
             Arrays.toString(MappingUtils.flattenRanges(new int[] { 12, 12,
                 9, 7, 4, 2, 1 })));
   }
+
+  /**
+   * Test mapping a column selection including hidden columns
+   * 
+   * @throws IOException
+   */
+  @Test(groups = { "Functional" })
+  public void testMapColumnSelection_hiddenColumns() throws IOException
+  {
+    setupMappedAlignments();
+
+    ColumnSelection proteinSelection = new ColumnSelection();
+
+    /*
+     * Column 0 in protein picks up Seq2/L, Seq3/G which map to cols 0-4 and 0-3
+     * in dna respectively, overall 0-4
+     */
+    proteinSelection.hideColumns(0);
+    ColumnSelection dnaSelection = MappingUtils.mapColumnSelection(
+            proteinSelection, proteinView, dnaView);
+    assertEquals("[]", dnaSelection.getSelected().toString());
+    List<int[]> hidden = dnaSelection.getHiddenColumns();
+    assertEquals(1, hidden.size());
+    assertEquals("[0, 4]", Arrays.toString(hidden.get(0)));
+
+    /*
+     * Column 1 in protein picks up Seq1/K which maps to cols 0-3 in dna
+     */
+    proteinSelection.revealAllHiddenColumns();
+    // the unhidden columns are now marked selected!
+    assertEquals("[0]", proteinSelection.getSelected().toString());
+    // deselect these or hideColumns will be expanded to include 0
+    proteinSelection.clear();
+    proteinSelection.hideColumns(1);
+    dnaSelection = MappingUtils.mapColumnSelection(proteinSelection,
+            proteinView, dnaView);
+    hidden = dnaSelection.getHiddenColumns();
+    assertEquals(1, hidden.size());
+    assertEquals("[0, 3]", Arrays.toString(hidden.get(0)));
+
+    /*
+     * Column 2 in protein picks up gaps only - no mapping
+     */
+    proteinSelection.revealAllHiddenColumns();
+    proteinSelection.clear();
+    proteinSelection.hideColumns(2);
+    dnaSelection = MappingUtils.mapColumnSelection(proteinSelection,
+            proteinView, dnaView);
+    assertTrue(dnaSelection.getHiddenColumns().isEmpty());
+
+    /*
+     * Column 3 in protein picks up Seq1/P, Seq2/Q, Seq3/S which map to columns
+     * 6-9, 6-10, 5-8 respectively, overall to 5-10
+     */
+    proteinSelection.revealAllHiddenColumns();
+    proteinSelection.clear();
+    proteinSelection.hideColumns(3); // 5-10 hidden in dna
+    proteinSelection.addElement(1); // 0-3 selected in dna
+    dnaSelection = MappingUtils.mapColumnSelection(proteinSelection,
+            proteinView, dnaView);
+    assertEquals("[0, 1, 2, 3]", dnaSelection.getSelected().toString());
+    hidden = dnaSelection.getHiddenColumns();
+    assertEquals(1, hidden.size());
+    assertEquals("[5, 10]", Arrays.toString(hidden.get(0)));
+
+    /*
+     * Combine hiding columns 1 and 3 to get discontiguous hidden columns
+     */
+    proteinSelection.revealAllHiddenColumns();
+    proteinSelection.clear();
+    proteinSelection.hideColumns(1);
+    proteinSelection.hideColumns(3);
+    dnaSelection = MappingUtils.mapColumnSelection(proteinSelection,
+            proteinView, dnaView);
+    hidden = dnaSelection.getHiddenColumns();
+    assertEquals(2, hidden.size());
+    assertEquals("[0, 3]", Arrays.toString(hidden.get(0)));
+    assertEquals("[5, 10]", Arrays.toString(hidden.get(1)));
+  }
+
+  @Test(groups = { "Functional" })
+  public void testGetLength()
+  {
+    assertEquals(0, MappingUtils.getLength(null));
+
+    /*
+     * [start, end] ranges
+     */
+    List<int[]> ranges = new ArrayList<int[]>();
+    assertEquals(0, MappingUtils.getLength(ranges));
+    ranges.add(new int[] { 1, 1 });
+    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));
+
+    /*
+     * [start, end, start, end...] ranges
+     */
+    ranges.clear();
+    ranges.add(new int[] { 1, 5, 8, 4 });
+    ranges.add(new int[] { 8, 2 });
+    ranges.add(new int[] { 12, 12 });
+    assertEquals(18, 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, 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));
+  }
+
 }