JAL-1759 merge from develop
[jalview.git] / test / jalview / util / MappingUtilsTest.java
index 41efa73..2c0045b 100644 (file)
@@ -1,30 +1,33 @@
 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;
@@ -33,7 +36,7 @@ public class MappingUtilsTest
   /**
    * 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");
@@ -86,8 +89,8 @@ 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();
@@ -163,7 +166,7 @@ public class MappingUtilsTest
    * 
    * @throws IOException
    */
-  @Test
+  @Test(groups ={ "Functional" })
   public void testMapSequenceGroup_sequences() throws IOException
   {
     /*
@@ -245,7 +248,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,7 +259,7 @@ public class MappingUtilsTest
    * 
    * @throws IOException
    */
-  @Test
+  @Test(groups ={ "Functional" })
   public void testMapColumnSelection_proteinToDna() throws IOException
   {
     setupMappedAlignments();
@@ -354,7 +357,7 @@ public class MappingUtilsTest
    * 
    * @throws IOException
    */
-  @Test
+  @Test(groups ={ "Functional" })
   public void testMapColumnSelection_dnaToProtein() throws IOException
   {
     setupMappedAlignments();
@@ -381,11 +384,20 @@ 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]",
@@ -411,7 +423,7 @@ public class MappingUtilsTest
    * 
    * @throws IOException
    */
-  @Test
+  @Test(groups ={ "Functional" })
   public void testMapSequenceGroup_columns() throws IOException
   {
     /*
@@ -494,7 +506,7 @@ public class MappingUtilsTest
    * 
    * @throws IOException
    */
-  @Test
+  @Test(groups ={ "Functional" })
   public void testMapSequenceGroup_region() throws IOException
   {
     /*
@@ -587,4 +599,75 @@ 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());
+}
 }