JAL-2147 findMappingsBetweenSequenceAndOthers for scoping search to sequences in...
authorJim Procter <jprocter@issues.jalview.org>
Thu, 14 Jul 2016 13:49:35 +0000 (14:49 +0100)
committerJim Procter <jprocter@issues.jalview.org>
Thu, 14 Jul 2016 13:49:35 +0000 (14:49 +0100)
src/jalview/util/MappingUtils.java
test/jalview/util/MappingUtilsTest.java

index ae4e55d..515ff51 100644 (file)
@@ -754,6 +754,13 @@ public final class MappingUtils
   public static List<AlignedCodonFrame> findMappingsForSequence(
           SequenceI sequence, List<AlignedCodonFrame> mappings)
   {
+    return findMappingsForSequenceAndOthers(sequence, mappings, null);
+  }
+
+  public static List<AlignedCodonFrame> findMappingsForSequenceAndOthers(
+          SequenceI sequence, List<AlignedCodonFrame> mappings,
+          AlignmentI alignment)
+  {
     List<AlignedCodonFrame> result = new ArrayList<AlignedCodonFrame>();
     if (sequence == null || mappings == null)
     {
@@ -763,7 +770,31 @@ public final class MappingUtils
     {
       if (mapping.involvesSequence(sequence))
       {
-        result.add(mapping);
+        if (alignment != null)
+        {
+          for (SequenceI otherseq : alignment.getSequences())
+          {
+            if (otherseq == sequence
+                    || (otherseq.getDatasetSequence() != null && (otherseq
+                            .getDatasetSequence() == sequence || otherseq
+                            .getDatasetSequence() == sequence
+                            .getDatasetSequence())))
+            {
+              // skip sequences in subset which directly relate to sequence
+              continue;
+            }
+            if (mapping.involvesSequence(otherseq))
+            {
+              // selected a mapping contained in subselect alignment
+              result.add(mapping);
+              break;
+            }
+          }
+        }
+        else
+        {
+          result.add(mapping);
+        }
       }
     }
     return result;
index 3c417c3..7e28579 100644 (file)
@@ -703,6 +703,51 @@ public class MappingUtilsTest
     assertEquals(0, result.size());
   }
 
+  /**
+   * just like the one above, but this time, we provide a set of sequences to
+   * subselect the mapping search
+   */
+  @Test(groups = { "Functional" })
+  public void testFindMappingsBetweenSequenceAndOthers()
+  {
+    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);
+
+    List<AlignedCodonFrame> mappings = new ArrayList<AlignedCodonFrame>();
+    mappings.add(acf1);
+    mappings.add(acf2);
+    mappings.add(acf3);
+
+    /*
+     * Seq1 has three mappings
+     */
+    List<AlignedCodonFrame> result = MappingUtils
+            .findMappingsForSequenceAndOthers(seq1, mappings,
+                    new Alignment(new SequenceI[] { seq1, seq2 }));
+    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());
+  }
+
   @Test(groups = { "Functional" })
   public void testMapEditCommand()
   {