JAL-391 corrected handling of gapped sequences
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Tue, 19 Jul 2016 15:21:18 +0000 (16:21 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Tue, 19 Jul 2016 15:21:18 +0000 (16:21 +0100)
src/jalview/analysis/Dna.java
test/jalview/analysis/DnaTest.java

index d1901c3..800cef2 100644 (file)
@@ -852,13 +852,18 @@ public class Dna
     char[] originalSequence = sequence.toCharArray();
     int length = originalSequence.length;
     char[] reversedSequence = new char[length];
-
+    int bases = 0;
     for (int i = 0; i < length; i++)
     {
-      reversedSequence[length - i - 1] = complement ? getComplement(originalSequence[i])
+      char c = complement ? getComplement(originalSequence[i])
               : originalSequence[i];
+      reversedSequence[length - i - 1] = c;
+      if (!Comparison.isGap(c))
+      {
+        bases++;
+      }
     }
-    SequenceI reversed = new Sequence(newName, reversedSequence, 1, length);
+    SequenceI reversed = new Sequence(newName, reversedSequence, 1, bases);
     return reversed;
   }
 
@@ -874,6 +879,10 @@ public class Dna
   {
     char result = c;
     switch (c) {
+    case '-':
+    case '.':
+    case ' ':
+      break;
     case 'a':
       result = 't';
       break;
index 9a4c357..0142ab5 100644 (file)
@@ -29,6 +29,7 @@ import jalview.datamodel.AlignedCodon;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.ColumnSelection;
+import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceI;
 import jalview.gui.AlignViewport;
 import jalview.io.FormatAdapter;
@@ -498,17 +499,44 @@ public class DnaTest
   @Test(groups = "Functional")
   public void testReverseSequence()
   {
-    String seq = "AcGtUrYkMbVdHNX";
+    String seq = "-Ac-GtU--rYkMbVdHNX-";
+    String seqRev = new StringBuilder(seq).reverse().toString();
 
     // reverse:
     SequenceI reversed = Dna.reverseSequence("Seq1", seq, false);
-    assertEquals(new StringBuilder(seq).reverse()
-            .toString(), reversed.getSequenceAsString());
+    assertEquals(1, reversed.getStart());
+    assertEquals(15, reversed.getEnd());
+    assertEquals(20, reversed.getLength());
+    assertEquals(seqRev, reversed.getSequenceAsString());
     assertEquals("Seq1|rev", reversed.getName());
 
     // reverse complement:
     SequenceI revcomp = Dna.reverseSequence("Seq1", seq, true);
-    assertEquals("XNDhBvKmRyAaCgT", revcomp.getSequenceAsString());
+    assertEquals("-XNDhBvKmRy--AaC-gT-", revcomp.getSequenceAsString());
     assertEquals("Seq1|revcomp", revcomp.getName());
   }
+
+  @Test(groups = "Functional")
+  public void testReverseCdna()
+  {
+    String seq = "-Ac-GtU--rYkMbVdHNX-";
+    String seqRev = new StringBuilder(seq).reverse().toString();
+    String seqDs = seq.replaceAll("-", "");
+    String seqDsRev = new StringBuilder(seqDs).reverse().toString();
+
+    SequenceI dna = new Sequence("Seq1", seq);
+    Alignment al = new Alignment(new SequenceI[] {dna});
+    al.createDatasetAlignment();
+    assertEquals(seqDs, al.getSequenceAt(0).getDatasetSequence()
+            .getSequenceAsString());
+
+    ColumnSelection cs = new ColumnSelection();
+    AlignViewportI av = new AlignViewport(al, cs);
+    Dna testee = new Dna(av, new int[] { 0, al.getWidth() - 1 });
+    AlignmentI reversed = testee.reverseCdna(false);
+    assertEquals(1, reversed.getHeight());
+    assertEquals(seqRev, reversed.getSequenceAt(0).getSequenceAsString());
+    assertEquals(seqDsRev, reversed.getSequenceAt(0).getDatasetSequence()
+            .getSequenceAsString());
+  }
 }