JAL-2011 adjust method to fix failing unit test
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 22 Aug 2016 08:26:41 +0000 (09:26 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 22 Aug 2016 08:26:41 +0000 (09:26 +0100)
src/jalview/analysis/AlignmentUtils.java
test/jalview/analysis/AlignmentUtilsTests.java

index 9aaaed2..d93f42f 100644 (file)
@@ -2634,13 +2634,16 @@ public class AlignmentUtils
       return false; // should only pass alignments with datasets here
     }
 
-    // map from dataset sequence to alignment sequence
-    Map<SequenceI, SequenceI> alignedDatasets = new HashMap<SequenceI, SequenceI>();
+    // map from dataset sequence to alignment sequence(s)
+    Map<SequenceI, List<SequenceI>> alignedDatasets = new HashMap<SequenceI, List<SequenceI>>();
     for (SequenceI seq : aligned.getSequences())
     {
-      // JAL-2110: fail if two or more alignment sequences have a common dataset
-      // sequence.
-      alignedDatasets.put(seq.getDatasetSequence(), seq);
+      SequenceI ds = seq.getDatasetSequence();
+      if (alignedDatasets.get(ds) == null)
+      {
+        alignedDatasets.put(ds, new ArrayList<SequenceI>());
+      }
+      alignedDatasets.get(ds).add(seq);
     }
 
     /*
@@ -2656,17 +2659,22 @@ public class AlignmentUtils
     }
 
     /*
-     * second pass - copy aligned sequences
+     * second pass - copy aligned sequences;
+     * heuristic rule: pair off sequences in order for the case where 
+     * more than one shares the same dataset sequence 
      */
     for (SequenceI seq : unaligned.getSequences())
     {
-      SequenceI alignedSequence = alignedDatasets.get(seq
+      List<SequenceI> alignedSequences = alignedDatasets.get(seq
               .getDatasetSequence());
-      // JAL-2110: fail if two or more alignment sequences have common dataset
-      // sequence.
       // TODO: getSequenceAsString() will be deprecated in the future
       // TODO: need to leave to SequenceI implementor to update gaps
-      seq.setSequence(alignedSequence.getSequenceAsString());
+      seq.setSequence(alignedSequences.get(0).getSequenceAsString());
+      if (alignedSequences.size() > 0)
+      {
+        // pop off aligned sequences (except the last one)
+        alignedSequences.remove(0);
+      }
     }
 
     return true;
index 7661c5d..22bb680 100644 (file)
@@ -2449,8 +2449,9 @@ public class AlignmentUtilsTests
   {
     SequenceI dna1 = new Sequence("dna1", "cccGGGTTTaaa");
     SequenceI dna2 = new Sequence("dna2", "CCCgggtttAAA");
-    SequenceI as1 = dna1.deriveSequence(), as2 = dna1.deriveSequence()
-            .getSubSequence(3, 7), as3 = dna2.deriveSequence();
+    SequenceI as1 = dna1.deriveSequence();
+    SequenceI as2 = dna1.deriveSequence().getSubSequence(3, 7);
+    SequenceI as3 = dna2.deriveSequence();
     as1.insertCharAt(6, 5, '-');
     String s_as1 = as1.getSequenceAsString();
     as2.insertCharAt(6, 5, '-');
@@ -2461,8 +2462,9 @@ public class AlignmentUtilsTests
 
     // why do we need to cast this still ?
     ((Alignment) aligned).createDatasetAlignment();
-    SequenceI uas1 = dna1.deriveSequence(), uas2 = dna1.deriveSequence()
-            .getSubSequence(3, 7), uas3 = dna2.deriveSequence();
+    SequenceI uas1 = dna1.deriveSequence();
+    SequenceI uas2 = dna1.deriveSequence().getSubSequence(3, 7);
+    SequenceI uas3 = dna2.deriveSequence();
     AlignmentI tobealigned = new Alignment(new SequenceI[] { uas1, uas2,
         uas3 });
     ((Alignment) tobealigned).createDatasetAlignment();