JAL-1619 refactoring in progress for Dna translation
[jalview.git] / test / jalview / analysis / DnaTest.java
index b1ce141..4a8d4d0 100644 (file)
@@ -6,6 +6,7 @@ import jalview.datamodel.ColumnSelection;
 import jalview.io.FormatAdapter;
 
 import java.io.IOException;
+import java.util.Arrays;
 
 import org.junit.Test;
 
@@ -34,7 +35,7 @@ public class DnaTest
             FormatAdapter.PASTE, "FASTA");
     final String sequenceAsString = alf
                 .getSequenceAt(0).getSequenceAsString();
-    AlignmentI translated = Dna.CdnaTranslate(alf.getSequencesArray(),
+    AlignmentI translated = Dna.cdnaTranslate(alf.getSequencesArray(),
             new String[]
             { sequenceAsString }, new int[]
             { 0, alf.getWidth() - 1 }, alf.getGapCharacter(), null,
@@ -59,7 +60,7 @@ public class DnaTest
     cs.hideColumns(6, 14); // hide codons 3/4/5
     cs.hideColumns(24, 35); // hide codons 9-12
     cs.hideColumns(177, 191); // hide codons 60-64
-    AlignmentI translated = Dna.CdnaTranslate(alf.getSequencesArray(),
+    AlignmentI translated = Dna.cdnaTranslate(alf.getSequencesArray(),
             cs.getVisibleSequenceStrings(0, alf.getWidth(),
                     alf.getSequencesArray()), new int[]
             { 0, alf.getWidth() - 1 }, alf.getGapCharacter(), null,
@@ -67,4 +68,154 @@ public class DnaTest
     String aa = translated.getSequenceAt(0).getSequenceAsString();
     assertEquals("AACDDGGGGHHIIIKKLLLLLLMNNPPPPQQRRRRRRSSSSSSTTTTVVVVW", aa);
   }
+
+  /**
+   * Tests for method that compares 'alignment' of two codon position triplets.
+   */
+  @Test
+  public void testCompareCodonPos()
+  {
+    /*
+     * Returns 0 for any null argument
+     */
+    assertEquals(0, Dna.compareCodonPos(new int[]
+      { 1, 2, 3 }, null));
+    assertEquals(0, Dna.compareCodonPos(null, new int[]
+      { 1, 2, 3 }));
+
+    /*
+     * Work through 27 combinations. First 9 cases where first position matches.
+     */
+    assertMatches("AAA", "GGG"); // 2 and 3 match
+    assertFollows("AA-A", "GGG"); // 2 matches, 3 shifted seq1
+    assertPrecedes("AAA", "GG-G"); // 2 matches, 3 shifted seq2
+    assertFollows("A-AA", "GG-G"); // 2 shifted seq1, 3 matches
+    assertFollows("A-A-A", "GG-G"); // 2 shifted seq1, 3 shifted seq1
+    // TODO is this right?
+    assertPrecedes("A-AA", "GG--G"); // 2 shifted seq1, 3 shifted seq2
+    assertPrecedes("AA-A", "G-GG"); // 2 shifted seq2, 3 matches
+    assertPrecedes("AA--A", "G-GG"); // 2 shifted seq2, 3 shifted seq1
+    assertPrecedes("AAA", "G-GG"); // 2 shifted seq2, 3 shifted seq2
+
+    /*
+     * 9 cases where first position is shifted in first sequence.
+     */
+    assertFollows("-AAA", "G-GG"); // 2 and 3 match
+    assertFollows("-AA-A", "G-GG"); // 2 matches, 3 shifted seq1
+    assertPrecedes("-AAA", "G-G-G"); // 2 matches, 3 shifted seq2
+    assertFollows("-A-AA", "G-G-G"); // 2 shifted seq1, 3 matches
+    assertFollows("-A-A-A", "G-G-G"); // 2 shifted seq1, 3 shifted seq1
+    // is this right? codon2 ends after codon1
+    assertPrecedes("-A-AA", "G-G--G"); // 2 shifted seq1, 3 shifted seq2
+    assertPrecedes("-AA-A", "G--GG"); // 2 shifted seq2, 3 matches
+    assertPrecedes("-AA--A", "G--GG"); // 2 shifted seq2, 3 shifted seq1
+    assertPrecedes("-AAA", "G--GG"); // 2 shifted seq2, 3 shifted seq2
+
+    /*
+     * 9 cases where first position is shifted in second sequence.
+     */
+    assertPrecedes("A-AA", "-GGG"); // 2 and 3 match
+    assertPrecedes("A-A-A", "-GGG"); // 2 matches, 3 shifted seq1
+    assertPrecedes("A-AA", "-GG-G"); // 2 matches, 3 shifted seq2
+    assertPrecedes("A--AA", "-GG-G"); // 2 shifted seq1, 3 matches
+    assertPrecedes("A--AA", "-GGG"); // 2 shifted seq1, 3 shifted seq1
+    assertPrecedes("A--AA", "-GG--G"); // 2 shifted seq1, 3 shifted seq2
+    assertPrecedes("AA-A", "-GGG"); // 2 shifted seq2, 3 matches
+    assertPrecedes("AA--A", "-GGG"); // 2 shifted seq2, 3 shifted seq1
+    assertPrecedes("AAA", "-GGG"); // 2 shifted seq2, 3 shifted seq2
+
+    /*
+     * two codon positions can each 'precede' the other! the comparison is
+     * biased to the first sequence
+     */
+    // TODO is this correct?
+    assertPrecedes("-A--AA", "--GGG");
+    assertPrecedes("--AAA", "-A--AA");
+  }
+
+  /**
+   * Assert that the first sequence should map to the same position as the
+   * second in a translated alignment
+   * 
+   * @param codon1
+   * @param codon2
+   */
+  private void assertMatches(String codon1, String codon2)
+  {
+    assertEquals("Expected match (0)", 0, compare(codon1, codon2));
+  }
+
+  /**
+   * Assert that the first sequence should precede the second in a translated
+   * alignment
+   * 
+   * @param codon1
+   * @param codon2
+   */
+  private void assertPrecedes(String codon1, String codon2)
+  {
+    assertEquals("Expected precedes (-1)", -1, compare(codon1, codon2));
+  }
+
+  /**
+   * Assert that the first sequence should follow the second in a translated
+   * alignment
+   * 
+   * @param codon1
+   * @param codon2
+   */
+  private void assertFollows(String codon1, String codon2)
+  {
+    assertEquals("Expected follows (1)", 1, compare(codon1, codon2));
+  }
+
+  /**
+   * Convert two nucleotide strings to base positions and pass to
+   * Dna.compareCodonPos, return the result.
+   * 
+   * @param s1
+   * @param s2
+   * @return
+   */
+  private int compare(String s1, String s2)
+  {
+    final int[] cd1 = convertCodon(s1);
+    final int[] cd2 = convertCodon(s2);
+    System.out.println("K: " + s1 + "  " + Arrays.toString(cd1));
+    System.out.println("G: " + s2 + "  " + Arrays.toString(cd2));
+    System.out.println();
+    return Dna.compareCodonPos(cd1, cd2);
+  }
+
+  /**
+   * Convert a string e.g. "-GC-T" to base positions e.g. [1, 2, 4]. The string
+   * should have exactly 3 non-gap characters, and use '-' for gaps.
+   * 
+   * @param s
+   * @return
+   */
+  private int[] convertCodon(String s)
+  {
+    int[] result = new int[3];
+    int i = 0;
+    for (int j = 0; j < s.length(); j++)
+    {
+      if (s.charAt(j) != '-')
+      {
+        result[i++] = j;
+      }
+    }
+    return result;
+  }
+
+  /**
+   * Weirdly, maybe worth a test to prove the helper method of this test class.
+   */
+  @Test
+  public void testConvertCodon()
+  {
+    assertEquals("[0, 1, 2]", Arrays.toString(convertCodon("AAA")));
+    assertEquals("[0, 2, 5]", Arrays.toString(convertCodon("A-A--A")));
+    assertEquals("[1, 3, 4]", Arrays.toString(convertCodon("-A-AA-")));
+  }
 }