JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / test / jalview / util / MappingUtilsTest.java
index 41efa73..9483dc5 100644 (file)
@@ -1,39 +1,62 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
+ * Copyright (C) 2015 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 <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.util;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertTrue;
-
-import java.awt.Color;
-import java.io.IOException;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.Set;
-
-import org.junit.Test;
+import static org.testng.AssertJUnit.assertEquals;
+import static org.testng.AssertJUnit.assertSame;
+import static org.testng.AssertJUnit.assertTrue;
 
 import jalview.api.AlignViewportI;
 import jalview.datamodel.AlignedCodonFrame;
-import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.ColumnSelection;
 import jalview.datamodel.SearchResults;
 import jalview.datamodel.SearchResults.Match;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceGroup;
+import jalview.datamodel.SequenceI;
 import jalview.gui.AlignViewport;
 import jalview.io.AppletFormatAdapter;
 import jalview.io.FormatAdapter;
 
+import java.awt.Color;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.testng.annotations.Test;
+
 public class MappingUtilsTest
 {
   private AlignViewportI dnaView;
+
   private AlignViewportI proteinView;
 
   /**
    * Simple test of mapping with no intron involved.
    */
-  @Test
+  @Test(groups = { "Functional" })
   public void testBuildSearchResults()
   {
     final Sequence seq1 = new Sequence("Seq1", "C-G-TA-GC");
@@ -46,9 +69,7 @@ public class MappingUtilsTest
      * Map dna bases 1-6 to protein residues 1-2
      */
     AlignedCodonFrame acf = new AlignedCodonFrame();
-    MapList map = new MapList(new int[]
-    { 1, 6 }, new int[]
-    { 1, 2 }, 3, 1);
+    MapList map = new MapList(new int[] { 1, 6 }, new int[] { 1, 2 }, 3, 1);
     acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
     Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
 
@@ -86,25 +107,24 @@ public class MappingUtilsTest
   /**
    * Simple test of mapping with introns involved.
    */
-  @Test
-  public void testBuildSearchResults_withIntro()
+  @Test(groups = { "Functional" })
+  public void testBuildSearchResults_withIntron()
   {
     final Sequence seq1 = new Sequence("Seq1", "C-G-TAGA-GCAGCTT");
     seq1.createDatasetSequence();
-  
+
     final Sequence aseq1 = new Sequence("Seq1", "-P-R");
     aseq1.createDatasetSequence();
-  
+
     /*
      * Map dna bases [2, 4, 5], [7, 9, 11] to protein residues 1 and 2
      */
     AlignedCodonFrame acf = new AlignedCodonFrame();
-    MapList map = new MapList(new int[]
-    { 2, 2, 4, 5, 7, 7, 9, 9, 11, 11 }, new int[]
-    { 1, 2 }, 3, 1);
+    MapList map = new MapList(new int[] { 2, 2, 4, 5, 7, 7, 9, 9, 11, 11 },
+            new int[] { 1, 2 }, 3, 1);
     acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
     Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
-  
+
     /*
      * Check protein residue 1 maps to [2, 4, 5]
      */
@@ -136,7 +156,7 @@ public class MappingUtilsTest
     assertEquals(seq1.getDatasetSequence(), m.getSequence());
     assertEquals(11, m.getStart());
     assertEquals(11, m.getEnd());
-  
+
     /*
      * Check inverse mappings, from codons to protein
      */
@@ -163,7 +183,7 @@ public class MappingUtilsTest
    * 
    * @throws IOException
    */
-  @Test
+  @Test(groups = { "Functional" })
   public void testMapSequenceGroup_sequences() throws IOException
   {
     /*
@@ -177,9 +197,7 @@ public class MappingUtilsTest
             "FASTA");
     protein.setDataset(null);
     AlignedCodonFrame acf = new AlignedCodonFrame();
-    MapList map = new MapList(new int[]
-    { 1, 3 }, new int[]
-    { 1, 1 }, 3, 1);
+    MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 1 }, 3, 1);
     for (int seq = 0; seq < 3; seq++)
     {
       acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), protein
@@ -204,7 +222,8 @@ public class MappingUtilsTest
     /*
      * Verify the mapped sequence group in dna
      */
-    SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg, proteinView, dnaView);
+    SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
+            proteinView, dnaView);
     assertTrue(mappedGroup.getColourText());
     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
@@ -245,7 +264,7 @@ public class MappingUtilsTest
   protected AlignmentI loadAlignment(final String data, String format)
           throws IOException
   {
-    Alignment a = new FormatAdapter().readFile(data,
+    AlignmentI a = new FormatAdapter().readFile(data,
             AppletFormatAdapter.PASTE, format);
     a.setDataset(null);
     return a;
@@ -256,11 +275,11 @@ public class MappingUtilsTest
    * 
    * @throws IOException
    */
-  @Test
+  @Test(groups = { "Functional" })
   public void testMapColumnSelection_proteinToDna() throws IOException
   {
     setupMappedAlignments();
-  
+
     ColumnSelection colsel = new ColumnSelection();
 
     /*
@@ -319,31 +338,26 @@ public class MappingUtilsTest
      * viewport). Lower case for introns.
      */
     AlignmentI cdna = loadAlignment(">Seq1\nAC-GctGtC-T\n"
-            + ">Seq2\nTc-GA-G-T-Tc\n" + ">Seq3\nTtTT-AaCGg-\n",
-            "FASTA");
+            + ">Seq2\nTc-GA-G-T-Tc\n" + ">Seq3\nTtTT-AaCGg-\n", "FASTA");
     cdna.setDataset(null);
     AlignmentI protein = loadAlignment(
-            ">Seq1\n-K-P\n>Seq2\nL--Q\n>Seq3\nG--S\n",
-            "FASTA");
+            ">Seq1\n-K-P\n>Seq2\nL--Q\n>Seq3\nG--S\n", "FASTA");
     protein.setDataset(null);
     AlignedCodonFrame acf = new AlignedCodonFrame();
-    MapList map = new MapList(new int[]
-    { 1, 3, 6, 6, 8, 9 }, new int[]
-    { 1, 2 }, 3, 1);
+    MapList map = new MapList(new int[] { 1, 3, 6, 6, 8, 9 }, new int[] {
+        1, 2 }, 3, 1);
     acf.addMap(cdna.getSequenceAt(0).getDatasetSequence(), protein
             .getSequenceAt(0).getDatasetSequence(), map);
-    map = new MapList(new int[]
-    { 1, 1, 3, 4, 5, 7 }, new int[]
-    { 1, 2 }, 3, 1);
+    map = new MapList(new int[] { 1, 1, 3, 4, 5, 7 }, new int[] { 1, 2 },
+            3, 1);
     acf.addMap(cdna.getSequenceAt(1).getDatasetSequence(), protein
             .getSequenceAt(1).getDatasetSequence(), map);
-    map = new MapList(new int[]
-    { 1, 1, 3, 4, 5, 5, 7, 8 }, new int[]
-    { 1, 2 }, 3, 1);
+    map = new MapList(new int[] { 1, 1, 3, 4, 5, 5, 7, 8 }, new int[] { 1,
+        2 }, 3, 1);
     acf.addMap(cdna.getSequenceAt(2).getDatasetSequence(), protein
             .getSequenceAt(2).getDatasetSequence(), map);
     Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
-  
+
     dnaView = new AlignViewport(cdna);
     proteinView = new AlignViewport(protein);
     protein.setCodonFrames(acfList);
@@ -354,13 +368,13 @@ public class MappingUtilsTest
    * 
    * @throws IOException
    */
-  @Test
+  @Test(groups = { "Functional" })
   public void testMapColumnSelection_dnaToProtein() throws IOException
   {
     setupMappedAlignments();
-  
+
     ColumnSelection colsel = new ColumnSelection();
-  
+
     /*
      * Column 0 in dna picks up first bases which map to residue 1, columns 0-1
      * in protein.
@@ -381,29 +395,41 @@ public class MappingUtilsTest
     assertEquals("[0, 1, 3]", cs.getSelected().toString());
   }
 
+  @Test(groups = { "Functional" })
+  public void testMapColumnSelection_null() throws IOException
+  {
+    setupMappedAlignments();
+    ColumnSelection cs = MappingUtils.mapColumnSelection(null, dnaView,
+            proteinView);
+    assertTrue("mapped selection not empty", cs.getSelected().isEmpty());
+  }
+
   /**
    * Tests for the method that converts a series of [start, end] ranges to
    * single positions
    */
-  @Test
+  @Test(groups = { "Functional" })
   public void testFlattenRanges()
   {
     assertEquals("[1, 2, 3, 4]",
-            Arrays.toString(MappingUtils.flattenRanges(new int[]
-            { 1, 4 })));
-    assertEquals("[1, 2, 3, 4]",
-            Arrays.toString(MappingUtils.flattenRanges(new int[]
-            { 1, 2, 3, 4 })));
-    assertEquals("[1, 2, 3, 4]",
-            Arrays.toString(MappingUtils.flattenRanges(new int[]
-            { 1, 1, 2, 2, 3, 3, 4, 4 })));
-    assertEquals("[1, 2, 3, 4, 7, 8, 9, 12]",
-            Arrays.toString(MappingUtils.flattenRanges(new int[]
-            { 1, 4, 7, 9, 12, 12 })));
+            Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 4 })));
+    assertEquals(
+            "[1, 2, 3, 4]",
+            Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 2, 3,
+                4 })));
+    assertEquals(
+            "[1, 2, 3, 4]",
+            Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 1, 2,
+                2, 3, 3, 4, 4 })));
+    assertEquals(
+            "[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:
-    assertEquals("[1, 2, 3, 4, 7, 8, 9, 12]",
-            Arrays.toString(MappingUtils.flattenRanges(new int[]
-            { 1, 4, 7, 9, 12, 12, 15 })));
+    assertEquals(
+            "[1, 2, 3, 4, 7, 8, 9, 12]",
+            Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 4, 7,
+                9, 12, 12, 15 })));
   }
 
   /**
@@ -411,7 +437,7 @@ public class MappingUtilsTest
    * 
    * @throws IOException
    */
-  @Test
+  @Test(groups = { "Functional" })
   public void testMapSequenceGroup_columns() throws IOException
   {
     /*
@@ -419,27 +445,24 @@ public class MappingUtilsTest
      * viewport).
      */
     AlignmentI cdna = loadAlignment(
-            ">Seq1\nACGGCA\n>Seq2\nTGACAG\n>Seq3\nTACGTA\n",
-            "FASTA");
+            ">Seq1\nACGGCA\n>Seq2\nTGACAG\n>Seq3\nTACGTA\n", "FASTA");
     cdna.setDataset(null);
     AlignmentI protein = loadAlignment(">Seq1\nKA\n>Seq2\nLQ\n>Seq3\nQV\n",
             "FASTA");
     protein.setDataset(null);
     AlignedCodonFrame acf = new AlignedCodonFrame();
-    MapList map = new MapList(new int[]
-    { 1, 6 }, new int[]
-    { 1, 2 }, 3, 1);
+    MapList map = new MapList(new int[] { 1, 6 }, new int[] { 1, 2 }, 3, 1);
     for (int seq = 0; seq < 3; seq++)
     {
       acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), protein
               .getSequenceAt(seq).getDatasetSequence(), map);
     }
     Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
-  
+
     AlignViewportI dnaView = new AlignViewport(cdna);
     AlignViewportI proteinView = new AlignViewport(protein);
     protein.setCodonFrames(acfList);
-  
+
     /*
      * Select all sequences, column 2 in the protein
      */
@@ -452,11 +475,12 @@ public class MappingUtilsTest
     sg.addSequence(protein.getSequenceAt(2), false);
     sg.setStartRes(1);
     sg.setEndRes(1);
-  
+
     /*
      * Verify the mapped sequence group in dna
      */
-    SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg, proteinView, dnaView);
+    SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
+            proteinView, dnaView);
     assertTrue(mappedGroup.getColourText());
     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
@@ -466,7 +490,7 @@ public class MappingUtilsTest
     assertSame(cdna.getSequenceAt(2), mappedGroup.getSequences().get(2));
     assertEquals(3, mappedGroup.getStartRes());
     assertEquals(5, mappedGroup.getEndRes());
-  
+
     /*
      * Verify mapping sequence group from dna to protein
      */
@@ -494,7 +518,7 @@ public class MappingUtilsTest
    * 
    * @throws IOException
    */
-  @Test
+  @Test(groups = { "Functional" })
   public void testMapSequenceGroup_region() throws IOException
   {
     /*
@@ -509,20 +533,18 @@ public class MappingUtilsTest
             ">Seq1\n-KA-S\n>Seq2\n--L-QY\n>Seq3\nQ-V-M\n", "FASTA");
     protein.setDataset(null);
     AlignedCodonFrame acf = new AlignedCodonFrame();
-    MapList map = new MapList(new int[]
-    { 1, 9 }, new int[]
-    { 1, 3 }, 3, 1);
+    MapList map = new MapList(new int[] { 1, 9 }, new int[] { 1, 3 }, 3, 1);
     for (int seq = 0; seq < 3; seq++)
     {
       acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), protein
               .getSequenceAt(seq).getDatasetSequence(), map);
     }
     Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
-  
+
     AlignViewportI dnaView = new AlignViewport(cdna);
     AlignViewportI proteinView = new AlignViewport(protein);
     protein.setCodonFrames(acfList);
-  
+
     /*
      * Select Seq1 and Seq2 in the protein, column 1 (K/-). Expect mapped
      * sequence group to cover Seq1, columns 0-3 (ACG). Because the selection
@@ -537,11 +559,12 @@ public class MappingUtilsTest
     sg.addSequence(protein.getSequenceAt(1), false);
     sg.setStartRes(1);
     sg.setEndRes(1);
-  
+
     /*
      * Verify the mapped sequence group in dna
      */
-    SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg, proteinView, dnaView);
+    SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
+            proteinView, dnaView);
     assertTrue(mappedGroup.getColourText());
     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
@@ -551,7 +574,7 @@ public class MappingUtilsTest
     // Seq1 has K which should map to columns 0-3 in Seq1
     assertEquals(0, mappedGroup.getStartRes());
     assertEquals(3, mappedGroup.getEndRes());
-  
+
     /*
      * Now select cols 2-4 in protein. These cover Seq1:AS Seq2:LQ Seq3:VM which
      * extend over DNA columns 3-12, 1-7, 6-13 respectively, or 1-13 overall.
@@ -587,4 +610,73 @@ public class MappingUtilsTest
     assertEquals(0, mappedGroup.getStartRes());
     assertEquals(4, mappedGroup.getEndRes());
   }
+
+  @Test(groups = { "Functional" })
+  public void testFindMappingsForSequence()
+  {
+    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
+     */
+    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);
+
+    Set<AlignedCodonFrame> mappings = new HashSet<AlignedCodonFrame>();
+    mappings.add(acf1);
+    mappings.add(acf2);
+    mappings.add(acf3);
+
+    /*
+     * Seq1 has three mappings
+     */
+    List<AlignedCodonFrame> result = MappingUtils.findMappingsForSequence(
+            seq1, mappings);
+    assertEquals(3, result.size());
+    assertTrue(result.contains(acf1));
+    assertTrue(result.contains(acf2));
+    assertTrue(result.contains(acf3));
+
+    /*
+     * Seq2 has two mappings
+     */
+    result = MappingUtils.findMappingsForSequence(seq2, mappings);
+    assertEquals(2, result.size());
+    assertTrue(result.contains(acf1));
+    assertTrue(result.contains(acf2));
+
+    /*
+     * Seq3 has one mapping
+     */
+    result = MappingUtils.findMappingsForSequence(seq3, mappings);
+    assertEquals(1, result.size());
+    assertTrue(result.contains(acf3));
+
+    /*
+     * Seq4 has no mappings
+     */
+    result = MappingUtils.findMappingsForSequence(seq4, mappings);
+    assertEquals(0, result.size());
+
+    result = MappingUtils.findMappingsForSequence(null, mappings);
+    assertEquals(0, result.size());
+
+    result = MappingUtils.findMappingsForSequence(seq1, null);
+    assertEquals(0, result.size());
+
+    result = MappingUtils.findMappingsForSequence(null, null);
+    assertEquals(0, result.size());
+  }
 }