JAL-2089 patch broken merge to master for Release 2.10.0b1
[jalview.git] / test / jalview / analysis / DnaTest.java
index 55908dd..1851517 100644 (file)
@@ -1,3 +1,23 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.analysis;
 
 import static org.testng.AssertJUnit.assertEquals;
@@ -9,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;
@@ -94,7 +115,7 @@ public class DnaTest
    * 
    * @throws IOException
    */
-  @Test(groups ={ "Functional" })
+  @Test(groups = { "Functional" })
   public void testTranslateCdna_withUntranslatableCodons()
           throws IOException
   {
@@ -103,8 +124,7 @@ public class DnaTest
             "FASTA");
     ColumnSelection cs = new ColumnSelection();
     AlignViewportI av = new AlignViewport(alf, cs);
-    Dna dna = new Dna(av, new int[]
-    { 0, alf.getWidth() - 1 });
+    Dna dna = new Dna(av, new int[] { 0, alf.getWidth() - 1 });
     AlignmentI translated = dna.translateCdna();
     assertNotNull("Couldn't do a full width translation of test data.",
             translated);
@@ -116,7 +136,7 @@ public class DnaTest
    * 
    * @throws IOException
    */
-  @Test(groups ={ "Functional" })
+  @Test(groups = { "Functional" })
   public void testTranslateCdna_withUntranslatableCodonsAndHiddenColumns()
           throws IOException
   {
@@ -148,23 +168,22 @@ public class DnaTest
   }
 
   /**
-   * Test simple translation to Amino Acids (with STOP codons translated to X).
+   * Test simple translation to Amino Acids (with STOP codons translated to *).
    * 
    * @throws IOException
    */
-  @Test(groups ={ "Functional" })
+  @Test(groups = { "Functional" })
   public void testTranslateCdna_simple() throws IOException
   {
     AlignmentI alf = new FormatAdapter().readFile(fasta,
             FormatAdapter.PASTE, "FASTA");
     ColumnSelection cs = new ColumnSelection();
     AlignViewportI av = new AlignViewport(alf, cs);
-    Dna dna = new Dna(av, new int[]
-    { 0, alf.getWidth() - 1 });
+    Dna dna = new Dna(av, new int[] { 0, alf.getWidth() - 1 });
     AlignmentI translated = dna.translateCdna();
     String aa = translated.getSequenceAt(0).getSequenceAsString();
     assertEquals(
-            "AAAACCDDEEFFGGGGHHIIIKKLLLLLLMNNPPPPQQRRRRRRSSSSSSTTTTVVVVWYYXXX",
+            "AAAACCDDEEFFGGGGHHIIIKKLLLLLLMNNPPPPQQRRRRRRSSSSSSTTTTVVVVWYY***",
             aa);
   }
 
@@ -173,7 +192,7 @@ public class DnaTest
    * 
    * @throws IOException
    */
-  @Test(groups ={ "Functional" })
+  @Test(groups = { "Functional" })
   public void testTranslateCdna_hiddenColumns() throws IOException
   {
     AlignmentI alf = new FormatAdapter().readFile(fasta,
@@ -183,8 +202,7 @@ public class DnaTest
     cs.hideColumns(24, 35); // hide codons 9-12
     cs.hideColumns(177, 191); // hide codons 60-64
     AlignViewportI av = new AlignViewport(alf, cs);
-    Dna dna = new Dna(av, new int[]
-    { 0, alf.getWidth() - 1 });
+    Dna dna = new Dna(av, new int[] { 0, alf.getWidth() - 1 });
     AlignmentI translated = dna.translateCdna();
     String aa = translated.getSequenceAt(0).getSequenceAsString();
     assertEquals("AACDDGGGGHHIIIKKLLLLLLMNNPPPPQQRRRRRRSSSSSSTTTTVVVVW", aa);
@@ -193,7 +211,7 @@ public class DnaTest
   /**
    * Use this test to help debug into any cases of interest.
    */
-  @Test(groups ={ "Functional" })
+  @Test(groups = { "Functional" })
   public void testCompareCodonPos_oneOnly()
   {
     assertFollows("-AA--A", "G--GG"); // 2 shifted seq2, 3 shifted seq1
@@ -202,7 +220,7 @@ public class DnaTest
   /**
    * Tests for method that compares 'alignment' of two codon position triplets.
    */
-  @Test(groups ={ "Functional" })
+  @Test(groups = { "Functional" })
   public void testCompareCodonPos()
   {
     /*
@@ -259,7 +277,7 @@ public class DnaTest
    * reorders the cDNA and retranslates, and verifies that the translations are
    * the same (apart from ordering).
    */
-  @Test(groups ={ "Functional" })
+  @Test(groups = { "Functional" })
   public void testTranslateCdna_sequenceOrderIndependent()
   {
     /*
@@ -268,16 +286,14 @@ public class DnaTest
     AlignmentI cdna = new DnaAlignmentGenerator().generate(12, 8, 97, 5, 5);
     ColumnSelection cs = new ColumnSelection();
     AlignViewportI av = new AlignViewport(cdna, cs);
-    Dna dna = new Dna(av, new int[]
-    { 0, cdna.getWidth() - 1 });
+    Dna dna = new Dna(av, new int[] { 0, cdna.getWidth() - 1 });
     AlignmentI translated = dna.translateCdna();
 
     /*
      * Jumble the cDNA sequences and translate.
      */
     SequenceI[] sorted = new SequenceI[cdna.getHeight()];
-    final int[] jumbler = new int[]
-    { 6, 7, 3, 4, 2, 0, 1, 5 };
+    final int[] jumbler = new int[] { 6, 7, 3, 4, 2, 0, 1, 5 };
     int seqNo = 0;
     for (int i : jumbler)
     {
@@ -285,8 +301,7 @@ public class DnaTest
     }
     AlignmentI cdnaReordered = new Alignment(sorted);
     av = new AlignViewport(cdnaReordered, cs);
-    dna = new Dna(av, new int[]
-    { 0, cdna.getWidth() - 1 });
+    dna = new Dna(av, new int[] { 0, cdna.getWidth() - 1 });
     AlignmentI translated2 = dna.translateCdna();
 
     /*
@@ -302,8 +317,8 @@ public class DnaTest
     {
       final String translation1 = translated.getSequenceAt(
               originalSequenceIndex).getSequenceAsString();
-      final String translation2 = translated2.getSequenceAt(sortedSequenceIndex)
-              .getSequenceAsString();
+      final String translation2 = translated2.getSequenceAt(
+              sortedSequenceIndex).getSequenceAsString();
       assertEquals(translation2, translation1);
       sortedSequenceIndex++;
     }
@@ -313,7 +328,7 @@ public class DnaTest
    * Test that all the cases in testCompareCodonPos have a 'symmetric'
    * comparison (without checking the actual comparison result).
    */
-  @Test(groups ={ "Functional" })
+  @Test(groups = { "Functional" })
   public void testCompareCodonPos_isSymmetric()
   {
     assertSymmetric("AAA", "GGG");
@@ -436,11 +451,92 @@ public class DnaTest
   /**
    * Weirdly, maybe worth a test to prove the helper method of this test class.
    */
-  @Test(groups ={ "Functional" })
+  @Test(groups = { "Functional" })
   public void testConvertCodon()
   {
     assertEquals("[0, 1, 2]", convertCodon("AAA").toString());
     assertEquals("[0, 2, 5]", convertCodon("A-A--A").toString());
     assertEquals("[1, 3, 4]", convertCodon("-A-AA-").toString());
   }
+
+  /**
+   * Test dna complementing
+   */
+  @Test(groups = "Functional")
+  public void testGetComplement()
+  {
+    assertEquals('t', Dna.getComplement('a'));
+    assertEquals('T', Dna.getComplement('A'));
+    assertEquals('a', Dna.getComplement('t'));
+    assertEquals('A', Dna.getComplement('T'));
+    assertEquals('c', Dna.getComplement('g'));
+    assertEquals('C', Dna.getComplement('G'));
+    assertEquals('g', Dna.getComplement('c'));
+    assertEquals('G', Dna.getComplement('C'));
+    // note uU --> aA but not vice versa
+    assertEquals('a', Dna.getComplement('u'));
+    assertEquals('A', Dna.getComplement('U'));
+    // ambiguity codes, see http://www.bioinformatics.org/sms/iupac.html
+    assertEquals('r', Dna.getComplement('y'));
+    assertEquals('R', Dna.getComplement('Y'));
+    assertEquals('y', Dna.getComplement('r'));
+    assertEquals('Y', Dna.getComplement('R'));
+    assertEquals('k', Dna.getComplement('m'));
+    assertEquals('K', Dna.getComplement('M'));
+    assertEquals('m', Dna.getComplement('k'));
+    assertEquals('M', Dna.getComplement('K'));
+    assertEquals('b', Dna.getComplement('v'));
+    assertEquals('B', Dna.getComplement('V'));
+    assertEquals('v', Dna.getComplement('b'));
+    assertEquals('V', Dna.getComplement('B'));
+    assertEquals('d', Dna.getComplement('h'));
+    assertEquals('D', Dna.getComplement('H'));
+    assertEquals('h', Dna.getComplement('d'));
+    assertEquals('H', Dna.getComplement('D'));
+    assertEquals('Q', Dna.getComplement('Q'));
+  }
+
+  @Test(groups = "Functional")
+  public void testReverseSequence()
+  {
+    String seq = "-Ac-GtU--rYkMbVdHNX-";
+    String seqRev = new StringBuilder(seq).reverse().toString();
+
+    // reverse:
+    SequenceI reversed = Dna.reverseSequence("Seq1", seq, false);
+    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("-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());
+  }
 }