Merge branch 'develop' into update_212_Dec_merge_with_21125_chamges
[jalview.git] / test / jalview / util / MappingUtilsTest.java
index 204d803..9239ba5 100644 (file)
@@ -25,7 +25,6 @@ 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.awt.Color;
@@ -39,7 +38,7 @@ import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
 import jalview.api.AlignViewportI;
-import jalview.bin.Cache;
+import jalview.bin.Console;
 import jalview.commands.EditCommand;
 import jalview.commands.EditCommand.Action;
 import jalview.commands.EditCommand.Edit;
@@ -60,12 +59,13 @@ import jalview.io.FileFormat;
 import jalview.io.FileFormatI;
 import jalview.io.FormatAdapter;
 
+
 public class MappingUtilsTest
 {
   @BeforeClass(alwaysRun = true)
   public void setUp()
   {
-    Cache.initLogger();
+    Console.initLogger();
   }
 
   @BeforeClass(alwaysRun = true)
@@ -240,8 +240,8 @@ public class MappingUtilsTest
             .asList(new AlignedCodonFrame[]
             { acf });
 
-    AlignViewportI theDnaView = new AlignViewport(cdna);
-    AlignViewportI theProteinView = new AlignViewport(protein);
+    AlignViewportI dnaView = new AlignViewport(cdna);
+    AlignViewportI proteinView = new AlignViewport(protein);
     protein.setCodonFrames(acfList);
 
     /*
@@ -259,7 +259,7 @@ public class MappingUtilsTest
      * Verify the mapped sequence group in dna
      */
     SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
-            theProteinView, theDnaView);
+            proteinView, dnaView);
     assertTrue(mappedGroup.getColourText());
     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
@@ -277,8 +277,7 @@ public class MappingUtilsTest
     sg.addSequence(cdna.getSequenceAt(0), false);
     sg.setStartRes(0);
     sg.setEndRes(2);
-    mappedGroup = MappingUtils.mapSequenceGroup(sg, theDnaView,
-            theProteinView);
+    mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
     assertTrue(mappedGroup.getColourText());
     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
@@ -532,8 +531,8 @@ public class MappingUtilsTest
             .asList(new AlignedCodonFrame[]
             { acf });
 
-    AlignViewportI theDnaView = new AlignViewport(cdna);
-    AlignViewportI theProteinView = new AlignViewport(protein);
+    AlignViewportI dnaView = new AlignViewport(cdna);
+    AlignViewportI proteinView = new AlignViewport(protein);
     protein.setCodonFrames(acfList);
 
     /*
@@ -553,7 +552,7 @@ public class MappingUtilsTest
      * Verify the mapped sequence group in dna
      */
     SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
-            theProteinView, theDnaView);
+            proteinView, dnaView);
     assertTrue(mappedGroup.getColourText());
     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
@@ -574,8 +573,7 @@ public class MappingUtilsTest
     // select columns 2 and 3 in DNA which span protein columns 0 and 1
     sg.setStartRes(2);
     sg.setEndRes(3);
-    mappedGroup = MappingUtils.mapSequenceGroup(sg, theDnaView,
-            theProteinView);
+    mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
     assertTrue(mappedGroup.getColourText());
     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
@@ -600,11 +598,11 @@ public class MappingUtilsTest
      * viewport).
      */
     AlignmentI cdna = loadAlignment(
-            ">Cds11\nA-CG-GC--AT-CA\n>Cds2\n-TG-AC-AG-T-AT\n>Cds3\n-T--ACG-TAAT-G\n",
+            ">Seq1\nA-CG-GC--AT-CA\n>Seq2\n-TG-AC-AG-T-AT\n>Seq3\n-T--ACG-TAAT-G\n",
             FileFormat.Fasta);
     cdna.setDataset(null);
     AlignmentI protein = loadAlignment(
-            ">Pep1\n-KA-S\n>Pep2\n--L-QY\n>Pep3\nQ-V-M\n",
+            ">Seq1\n-KA-S\n>Seq2\n--L-QY\n>Seq3\nQ-V-M\n",
             FileFormat.Fasta);
     protein.setDataset(null);
     AlignedCodonFrame acf = new AlignedCodonFrame();
@@ -618,14 +616,15 @@ public class MappingUtilsTest
             .asList(new AlignedCodonFrame[]
             { acf });
 
-    AlignViewportI theDnaView = new AlignViewport(cdna);
-    AlignViewportI theProteinView = new AlignViewport(protein);
+    AlignViewportI dnaView = new AlignViewport(cdna);
+    AlignViewportI proteinView = new AlignViewport(protein);
     protein.setCodonFrames(acfList);
 
     /*
-     * Select Pep1 and Pep2 in the protein, column 1 (K/-). Expect mapped
-     * sequence group to cover Cds1, columns 0-3 (ACG). Although the selection
-     * only includes a gap in Cds2, mapped Cds2 is included with 'no columns'
+     * Select Seq1 and Seq2 in the protein, column 1 (K/-). Expect mapped
+     * sequence group to cover Seq1, columns 0-3 (ACG). Because the selection
+     * only includes a gap in Seq2 there is no mappable selection region in the
+     * corresponding DNA.
      */
     SequenceGroup sg = new SequenceGroup();
     sg.setColourText(true);
@@ -640,15 +639,14 @@ public class MappingUtilsTest
      * Verify the mapped sequence group in dna
      */
     SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
-            theProteinView, theDnaView);
+            proteinView, dnaView);
     assertTrue(mappedGroup.getColourText());
     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
-    assertEquals(2, mappedGroup.getSequences().size());
+    assertEquals(1, mappedGroup.getSequences().size());
     assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
-    assertSame(cdna.getSequenceAt(1), mappedGroup.getSequences().get(1));
-    // Pep2 in protein has a gap in column 1 - doesn't map to any column
-    // Pep1 has K which should map to columns 0-3 in Cds1
+    // Seq2 in protein has a gap in column 1 - ignored
+    // Seq1 has K which should map to columns 0-3 in Seq1
     assertEquals(0, mappedGroup.getStartRes());
     assertEquals(3, mappedGroup.getEndRes());
 
@@ -658,8 +656,7 @@ public class MappingUtilsTest
      */
     sg.setStartRes(2);
     sg.setEndRes(4);
-    mappedGroup = MappingUtils.mapSequenceGroup(sg, theProteinView,
-            theDnaView);
+    mappedGroup = MappingUtils.mapSequenceGroup(sg, proteinView, dnaView);
     assertEquals(1, mappedGroup.getStartRes());
     assertEquals(13, mappedGroup.getEndRes());
 
@@ -672,22 +669,19 @@ public class MappingUtilsTest
     // select columns 4,5 - includes Seq1:codon2 (A) only
     sg.setStartRes(4);
     sg.setEndRes(5);
-    mappedGroup = MappingUtils.mapSequenceGroup(sg, theDnaView,
-            theProteinView);
+    mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
     assertEquals(2, mappedGroup.getStartRes());
     assertEquals(2, mappedGroup.getEndRes());
 
     // add Seq2 to dna selection cols 4-5 include codons 1 and 2 (LQ)
     sg.addSequence(cdna.getSequenceAt(1), false);
-    mappedGroup = MappingUtils.mapSequenceGroup(sg, theDnaView,
-            theProteinView);
+    mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
     assertEquals(2, mappedGroup.getStartRes());
     assertEquals(4, mappedGroup.getEndRes());
 
     // add Seq3 to dna selection cols 4-5 include codon 1 (Q)
     sg.addSequence(cdna.getSequenceAt(2), false);
-    mappedGroup = MappingUtils.mapSequenceGroup(sg, theDnaView,
-            theProteinView);
+    mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
     assertEquals(0, mappedGroup.getStartRes());
     assertEquals(4, mappedGroup.getEndRes());
   }
@@ -1338,32 +1332,21 @@ public class MappingUtilsTest
   }
 
   @Test(groups = "Functional")
-  public void testListToArray()
+  public void testFindOverlap()
   {
     List<int[]> ranges = new ArrayList<>();
+    ranges.add(new int[] { 4, 8 });
+    ranges.add(new int[] { 10, 12 });
+    ranges.add(new int[] { 16, 19 });
 
-    int[] result = MappingUtils.listToArray(ranges);
-    assertEquals(result.length, 0);
-    ranges.add(new int[] { 24, 12 });
-    result = MappingUtils.listToArray(ranges);
-    assertEquals(result.length, 2);
-    assertEquals(result[0], 24);
-    assertEquals(result[1], 12);
-    ranges.add(new int[] { -7, 30 });
-    result = MappingUtils.listToArray(ranges);
-    assertEquals(result.length, 4);
-    assertEquals(result[0], 24);
-    assertEquals(result[1], 12);
-    assertEquals(result[2], -7);
-    assertEquals(result[3], 30);
-    try
-    {
-      MappingUtils.listToArray(null);
-      fail("Expected exception");
-    } catch (NullPointerException e)
-    {
-      // expected
-    }
+    int[] overlap = MappingUtils.findOverlap(ranges, 5, 13);
+    assertArrayEquals(overlap, new int[] { 5, 12 });
+    overlap = MappingUtils.findOverlap(ranges, -100, 100);
+    assertArrayEquals(overlap, new int[] { 4, 19 });
+    overlap = MappingUtils.findOverlap(ranges, 7, 17);
+    assertArrayEquals(overlap, new int[] { 7, 17 });
+    overlap = MappingUtils.findOverlap(ranges, 13, 15);
+    assertNull(overlap);
   }
 
   /**
@@ -1469,104 +1452,58 @@ public class MappingUtilsTest
     assertEquals(1, mappedGroup.getEndRes()); // two columns
   }
 
+  // new for 2.12
   @Test(groups = "Functional")
-  public void testFindOverlap()
+  public void testAddRange()
   {
+    int[] range = { 1, 5 };
     List<int[]> ranges = new ArrayList<>();
-    ranges.add(new int[] { 4, 8 });
-    ranges.add(new int[] { 10, 12 });
-    ranges.add(new int[] { 16, 19 });
-
-    int[] overlap = MappingUtils.findOverlap(ranges, 5, 13);
-    assertArrayEquals(overlap, new int[] { 5, 12 });
-    overlap = MappingUtils.findOverlap(ranges, -100, 100);
-    assertArrayEquals(overlap, new int[] { 4, 19 });
-    overlap = MappingUtils.findOverlap(ranges, 7, 17);
-    assertArrayEquals(overlap, new int[] { 7, 17 });
-    overlap = MappingUtils.findOverlap(ranges, 13, 15);
-    assertNull(overlap);
-  }
-
-  /**
-   * Test mapping a sequence group including a sequence which maps to more than
-   * one other sequence
-   * 
-   * @throws IOException
-   */
-  @Test(groups = { "Functional" })
-  public void testMapSequenceGroup_oneToMany() throws IOException
-  {
-    /*
-     * Uniprot:FER2_ARATH has cross-refs to 10 EMBLCDS sequences;
-     * we'll just mimic 3 of them here (abbreviated)
-     * From EMBLCDS|BAE98526 [ [1, 444] ] 3:1 to [ [1, 148] ] FER2_ARATH
-     * From EMBLCDS|AAM91336 same
-     * From EMBLCDS|AAM13033 same
-     */
-    String coding = "atggcttccactgctctctca";
-    AlignmentI cds = loadAlignment(">BAE98526\n" + coding + "\n>AAM91336\n"
-            + coding + "\n>AAM13033\n" + coding + "\n",
-            FileFormat.Fasta);
-    cds.setDataset(null);
-    AlignmentI protein = loadAlignment(">FER2_ARATH\nMASTALS\n",
-            FileFormat.Fasta);
-    protein.setDataset(null);
-    AlignedCodonFrame acf = new AlignedCodonFrame();
-    MapList map = new MapList(new int[] { 1, 21 }, new int[] { 1, 7 }, 3, 1);
-    for (int seq = 0; seq < 3; seq++)
-    {
-      acf.addMap(cds.getSequenceAt(seq).getDatasetSequence(),
-              protein.getSequenceAt(0).getDatasetSequence(), map);
-    }
-    List<AlignedCodonFrame> acfList = Arrays
-            .asList(new AlignedCodonFrame[]
-            { acf });
-
-    AlignViewportI theDnaView = new AlignViewport(cds);
-    AlignViewportI theProteinView = new AlignViewport(protein);
-    protein.setCodonFrames(acfList);
-
-    /*
-     * Select FER2_ARATH in the protein
-     */
-    SequenceGroup sg = new SequenceGroup();
-    sg.setColourText(true);
-    sg.setIdColour(Color.GREEN);
-    sg.setOutlineColour(Color.LIGHT_GRAY);
-    sg.addSequence(protein.getSequenceAt(0), false);
-    sg.setEndRes(protein.getWidth() - 1);
-
-    /*
-     * Verify the mapped sequence group in dna
-     */
-    SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
-            theProteinView, theDnaView);
-    assertTrue(mappedGroup.getColourText());
-    assertSame(sg.getIdColour(), mappedGroup.getIdColour());
-    assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
-    assertEquals(3, mappedGroup.getSequences().size());
-    assertSame(cds.getSequenceAt(0), mappedGroup.getSequences().get(0));
-    assertSame(cds.getSequenceAt(1), mappedGroup.getSequences().get(1));
-    assertSame(cds.getSequenceAt(2), mappedGroup.getSequences().get(2));
-    assertEquals(0, mappedGroup.getStartRes());
-    assertEquals(20, mappedGroup.getEndRes()); // 21 columns (7 codons)
-
-    /*
-     * Select 2 CDS, verify peptide is mapped
-     */
-    sg.clear();
-    sg.addSequence(cds.getSequenceAt(1), false);
-    sg.addSequence(cds.getSequenceAt(0), false);
-    sg.setStartRes(0);
-    sg.setEndRes(20);
-    mappedGroup = MappingUtils.mapSequenceGroup(sg, theDnaView,
-            theProteinView);
-    assertTrue(mappedGroup.getColourText());
-    assertSame(sg.getIdColour(), mappedGroup.getIdColour());
-    assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
-    assertEquals(1, mappedGroup.getSequences().size());
-    assertSame(protein.getSequenceAt(0), mappedGroup.getSequences().get(0));
-    assertEquals(0, mappedGroup.getStartRes());
-    assertEquals(6, mappedGroup.getEndRes());
+  
+    // add to empty list:
+    MappingUtils.addRange(range, ranges);
+    assertEquals(1, ranges.size());
+    assertSame(range, ranges.get(0));
+  
+    // extend contiguous (same position):
+    MappingUtils.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):
+    MappingUtils.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:
+    MappingUtils.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):
+    MappingUtils.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):
+    MappingUtils.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:
+    MappingUtils.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
+    MappingUtils.addRange(new int[] { 11, 12 }, ranges);
+    assertEquals(4, ranges.size());
+    assertEquals(11, ranges.get(3)[0]);
+    assertEquals(12, ranges.get(3)[1]);
   }
 }