From 56d75bc01515a2709263a5b30de447f28c4fa5c1 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Tue, 19 Jul 2016 16:21:18 +0100 Subject: [PATCH] JAL-391 corrected handling of gapped sequences --- src/jalview/analysis/Dna.java | 15 ++++++++++++--- test/jalview/analysis/DnaTest.java | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/jalview/analysis/Dna.java b/src/jalview/analysis/Dna.java index d1901c3..800cef2 100644 --- a/src/jalview/analysis/Dna.java +++ b/src/jalview/analysis/Dna.java @@ -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; diff --git a/test/jalview/analysis/DnaTest.java b/test/jalview/analysis/DnaTest.java index 9a4c357..0142ab5 100644 --- a/test/jalview/analysis/DnaTest.java +++ b/test/jalview/analysis/DnaTest.java @@ -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()); + } } -- 1.7.10.2