Merge branch 'develop' into JAL-1705_trialMerge
[jalview.git] / test / jalview / util / MappingUtilsTest.java
index 51c99af..7100381 100644 (file)
@@ -43,12 +43,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 +74,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 +127,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 +208,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 +343,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 +375,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);
@@ -442,7 +445,7 @@ public class MappingUtilsTest
             "[1, 2, 3, 4, 7, 8, 9, 12]",
             Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 4, 7,
                 9, 12, 12 })));
-    // unpaired start position is ignored:
+    // trailing unpaired start position is ignored:
     assertEquals(
             "[1, 2, 3, 4, 7, 8, 9, 12]",
             Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 4, 7,
@@ -474,7 +477,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 +560,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 +656,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);
@@ -707,7 +712,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 });
@@ -738,6 +743,45 @@ public class MappingUtilsTest
   }
 
   /**
+   * Tests for the method that converts a series of [start, end] ranges to
+   * single positions, where the mapping is to a reverse strand i.e. start is
+   * greater than end point mapped to
+   */
+  @Test(groups = { "Functional" })
+  public void testFlattenRanges_reverseStrand()
+  {
+    assertEquals("[4, 3, 2, 1]",
+            Arrays.toString(MappingUtils.flattenRanges(new int[] { 4, 1 })));
+    assertEquals(
+            "[4, 3, 2, 1]",
+            Arrays.toString(MappingUtils.flattenRanges(new int[] { 4, 3, 2,
+                1 })));
+    assertEquals(
+            "[4, 3, 2, 1]",
+            Arrays.toString(MappingUtils.flattenRanges(new int[] { 4, 4, 3,
+                3, 2, 2, 1, 1 })));
+    assertEquals(
+            "[12, 9, 8, 7, 4, 3, 2, 1]",
+            Arrays.toString(MappingUtils.flattenRanges(new int[] { 12, 12,
+                9, 7, 4, 1 })));
+    // forwards and backwards anyone?
+    assertEquals(
+            "[4, 5, 6, 3, 2, 1]",
+            Arrays.toString(MappingUtils.flattenRanges(new int[] { 4, 6, 3,
+                1 })));
+    // backwards and forwards
+    assertEquals(
+            "[3, 2, 1, 4, 5, 6]",
+            Arrays.toString(MappingUtils.flattenRanges(new int[] { 3, 1, 4,
+                6 })));
+    // trailing unpaired start position is ignored:
+    assertEquals(
+            "[12, 9, 8, 7, 4, 3, 2]",
+            Arrays.toString(MappingUtils.flattenRanges(new int[] { 12, 12,
+                9, 7, 4, 2, 1 })));
+  }
+
+  /**
    * Test mapping a column selection including hidden columns
    * 
    * @throws IOException
@@ -747,62 +791,66 @@ public class MappingUtilsTest
   {
     setupMappedAlignments();
   
-    ColumnSelection colsel = new ColumnSelection();
+    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
      */
-    colsel.hideColumns(0);
-    ColumnSelection cs = MappingUtils.mapColumnSelection(colsel,
+    proteinSelection.hideColumns(0);
+    ColumnSelection dnaSelection = MappingUtils.mapColumnSelection(proteinSelection,
             proteinView, dnaView);
-    assertEquals("[]", cs.getSelected().toString());
-    List<int[]> hidden = cs.getHiddenColumns();
+    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
      */
-    colsel.revealAllHiddenColumns();
-    colsel.hideColumns(1);
-    cs = MappingUtils.mapColumnSelection(colsel, proteinView, dnaView);
-    hidden = cs.getHiddenColumns();
+    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
      */
-    colsel.revealAllHiddenColumns();
-    colsel.clear();
-    colsel.hideColumns(2);
-    cs = MappingUtils.mapColumnSelection(colsel, proteinView, dnaView);
-    assertTrue(cs.getHiddenColumns().isEmpty());
+    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
      */
-    colsel.revealAllHiddenColumns();
-    colsel.clear();
-    colsel.hideColumns(3); // 5-10 hidden in dna
-    colsel.addElement(1); // 0-3 selected in dna
-    cs = MappingUtils.mapColumnSelection(colsel, proteinView, dnaView);
-    assertEquals("[0, 1, 2, 3]", cs.getSelected().toString());
-    hidden = cs.getHiddenColumns();
+    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
      */
-    colsel.revealAllHiddenColumns();
-    colsel.clear();
-    colsel.hideColumns(1);
-    colsel.hideColumns(3);
-    cs = MappingUtils.mapColumnSelection(colsel, proteinView, dnaView);
-    hidden = cs.getHiddenColumns();
+    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)));