JAL-2147 additional test coverage for findMappingsForSequenceAndOthers
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Tue, 6 Sep 2016 13:46:45 +0000 (14:46 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Tue, 6 Sep 2016 13:46:45 +0000 (14:46 +0100)
src/jalview/util/MappingUtils.java
src/jalview/viewmodel/AlignmentViewport.java
test/jalview/util/MappingUtilsTest.java

index 515ff51..da83338 100644 (file)
@@ -757,9 +757,19 @@ public final class MappingUtils
     return findMappingsForSequenceAndOthers(sequence, mappings, null);
   }
 
+  /**
+   * Returns a list of any mappings that are from or to the given (aligned or
+   * dataset) sequence, optionally limited to mappings involving one of a given
+   * list of sequences.
+   * 
+   * @param sequence
+   * @param mappings
+   * @param filterList
+   * @return
+   */
   public static List<AlignedCodonFrame> findMappingsForSequenceAndOthers(
           SequenceI sequence, List<AlignedCodonFrame> mappings,
-          AlignmentI alignment)
+          List<SequenceI> filterList)
   {
     List<AlignedCodonFrame> result = new ArrayList<AlignedCodonFrame>();
     if (sequence == null || mappings == null)
@@ -770,14 +780,14 @@ public final class MappingUtils
     {
       if (mapping.involvesSequence(sequence))
       {
-        if (alignment != null)
+        if (filterList != null)
         {
-          for (SequenceI otherseq : alignment.getSequences())
+          for (SequenceI otherseq : filterList)
           {
+            SequenceI otherDataset = otherseq.getDatasetSequence();
             if (otherseq == sequence
-                    || (otherseq.getDatasetSequence() != null && (otherseq
-                            .getDatasetSequence() == sequence || otherseq
-                            .getDatasetSequence() == sequence
+                    || otherseq == sequence.getDatasetSequence()
+                    || (otherDataset != null && (otherDataset == sequence || otherDataset == sequence
                             .getDatasetSequence())))
             {
               // skip sequences in subset which directly relate to sequence
index 19ebf45..a954a83 100644 (file)
@@ -2730,7 +2730,7 @@ public abstract class AlignmentViewport implements AlignViewportI,
       }
       seqMappings = MappingUtils
               .findMappingsForSequenceAndOthers(sequence, mappings,
-                      getCodingComplement().getAlignment());
+                      getCodingComplement().getAlignment().getSequences());
       if (!seqMappings.isEmpty())
       {
         break;
index 7e28579..d131ed2 100644 (file)
@@ -708,7 +708,7 @@ public class MappingUtilsTest
    * subselect the mapping search
    */
   @Test(groups = { "Functional" })
-  public void testFindMappingsBetweenSequenceAndOthers()
+  public void testFindMappingsForSequenceAndOthers()
   {
     SequenceI seq1 = new Sequence("Seq1", "ABC");
     SequenceI seq2 = new Sequence("Seq2", "ABC");
@@ -720,7 +720,7 @@ public class MappingUtilsTest
     seq4.createDatasetSequence();
 
     /*
-     * Create mappings from seq1 to seq2, seq2 to seq1, seq3 to seq1
+     * 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);
@@ -729,23 +729,54 @@ public class MappingUtilsTest
     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);
 
     /*
-     * Seq1 has three mappings
+     * test for null args
      */
     List<AlignedCodonFrame> result = MappingUtils
-            .findMappingsForSequenceAndOthers(seq1, mappings,
-                    new Alignment(new SequenceI[] { seq1, seq2 }));
+            .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));
-    assertEquals(2, result.size());
+    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" })