JAL-1759 merge from develop
[jalview.git] / test / jalview / analysis / DnaTest.java
1 package jalview.analysis;
2
3 import static org.testng.AssertJUnit.assertEquals;
4 import static org.testng.AssertJUnit.assertNotNull;
5 import static org.testng.AssertJUnit.assertTrue;
6
7 import jalview.api.AlignViewportI;
8 import jalview.datamodel.AlignedCodon;
9 import jalview.datamodel.Alignment;
10 import jalview.datamodel.AlignmentI;
11 import jalview.datamodel.ColumnSelection;
12 import jalview.datamodel.SequenceI;
13 import jalview.gui.AlignViewport;
14 import jalview.io.FormatAdapter;
15
16 import java.io.IOException;
17
18 import org.testng.annotations.Test;
19
20 public class DnaTest
21 {
22   // @formatter:off
23   // AA encoding codons as ordered on the Jalview help page Amino Acid Table
24   private static String fasta = ">B\n" + "GCT" + "GCC" + "GCA" + "GCG"
25           + "TGT" + "TGC" + "GAT" + "GAC" + "GAA" + "GAG" + "TTT" + "TTC"
26           + "GGT" + "GGC" + "GGA" + "GGG" + "CAT" + "CAC" + "ATT" + "ATC"
27           + "ATA" + "AAA" + "AAG" + "TTG" + "TTA" + "CTT" + "CTC" + "CTA"
28           + "CTG" + "ATG" + "AAT" + "AAC" + "CCT" + "CCC" + "CCA" + "CCG"
29           + "CAA" + "CAG" + "CGT" + "CGC" + "CGA" + "CGG" + "AGA" + "AGG"
30           + "TCT" + "TCC" + "TCA" + "TCG" + "AGT" + "AGC" + "ACT" + "ACC"
31           + "ACA" + "ACG" + "GTT" + "GTC" + "GTA" + "GTG" + "TGG" + "TAT"
32           + "TAC" + "TAA" + "TAG" + "TGA";
33
34   private static String JAL_1312_example_align_fasta = ">B.FR.83.HXB2_LAI_IIIB_BRU_K03455/45-306\n"
35           + "ATGGGAAAAAATTCGGTTAAGGCCAGGGGGAAAGAAAAAATATAAATTAAAACATATAGTATGGGCAAGCAG\n"
36           + "GGAGCTAGAACGATTCGCAGTTAATCCTGGCCTGTTAGAAACATCAGAAGGCTGTAGACAAATACTGGGACA\n"
37           + "GCTACAACCATCCCTTCAGACAGGATCAGAAGAACTTAGATCATTATATAATACAGTAGCAACCCTCTATTG\n"
38           + "TGTGCATCAAAGGATAGAGATAAAAGACACCAAGGAAGCTTTAGAC\n"
39           + ">gi|27804621|gb|AY178912.1|/1-259\n"
40           + "-TGGGAGAA-ATTCGGTT-CGGCCAGGGGGAAAGAAAAAATATCAGTTAAAACATATAGTATGGGCAAGCAG\n"
41           + "AGAGCTAGAACGATTCGCAGTTAACCCTGGCCTTTTAGAGACATCACAAGGCTGTAGACAAATACTGGGACA\n"
42           + "GCTACAACCATCCCTTCAGACAGGATCAGAAGAACTTAAATCATTATATAATACAGTAGCAACCCTCTATTG\n"
43           + "TGTTCATCAAAGGATAGATATAAAAGACACCAAGGAAGCTTTAGAT\n"
44           + ">gi|27804623|gb|AY178913.1|/1-259\n"
45           + "-TGGGAGAA-ATTCGGTT-CGGCCAGGGGGAAAGAAAAAATATCAGTTAAAACATATAGTATGGGCAAGCAG\n"
46           + "AGAGCTAGAACGATTCGCAGTTAACCCTGGCCTTTTAGAGACATCACAAGGCTGTAGACAAATACTGGAACA\n"
47           + "GCTACAACCATCCCTTCAGACAGGATCAGAAGAACTTAAATCATTATATAATACAGTAGCAACCCTCTATTG\n"
48           + "TGTTCATCAAAGGATAGATGTAAAAGACACCAAGGAAGCTTTAGAT\n"
49           + ">gi|27804627|gb|AY178915.1|/1-260\n"
50           + "-TGGGAAAA-ATTCGGTTAAGGCCAGGGGGAAAGAAAAAATATAAGTTAAAACATATAGTATGGGCAAGCAG\n"
51           + "GGAGCTAGAACGATTCGCAGTTAACCCTGGCCTGTTAGAAACATCAGAAGGTTGTAGACAAATATTGGGACA\n"
52           + "GCTACAACCATCCCTTGAGACAGGATCAGAAGAACTTAAATCATTATWTAATACCATAGCAGTCCTCTATTG\n"
53           + "TGTACATCAAAGGATAGATATAAAAGACACCAAGGAAGCTTTAGAG\n"
54           + ">gi|27804631|gb|AY178917.1|/1-261\n"
55           + "-TGGGAAAAAATTCGGTTGAGGCCAGGGGGAAAGAAAAAATATAAGTTAAAACATATAGTATGGGCAAGCAG\n"
56           + "GGAGCTAGAACGATTCGCAGTCAACCCTGGCCTGTTAGAAACACCAGAAGGCTGTAGACAAATACTGGGACA\n"
57           + "GCTACAACCGTCCCTTCAGACAGGATCGGAAGAACTTAAATCATTATATAATACAGTAGCAACCCTCTATTG\n"
58           + "TGTGCATCAAAGGATAGATGTAAAAGACACCAAGGAGGCTTTAGAC\n"
59           + ">gi|27804635|gb|AY178919.1|/1-261\n"
60           + "-TGGGAGAGAATTCGGTTACGGCCAGGAGGAAAGAAAAAATATAAATTGAAACATATAGTATGGGCAGGCAG\n"
61           + "AGAGCTAGATCGATTCGCAGTCAATCCTGGCCTGTTAGAAACATCAGAAGGCTGCAGACAGATATTGGGACA\n"
62           + "GCTACAACCGTCCCTTAAGACAGGATCAGAAGAACTTAAATCATTATATAATACAGTAGCAACCCTCTATTG\n"
63           + "TGTACATCAAAGGATAGATGTAAAAGACACCAAGGAAGCTTTAGAT\n"
64           + ">gi|27804641|gb|AY178922.1|/1-261\n"
65           + "-TGGGAGAAAATTCGGTTACGGCCAGGGGGAAAGAAAAGATATAAGTTAAAACATATAGTATGGGCAAGCAG\n"
66           + "GGAGCTAGAACGATTCGCAGTCAACCCTGGCCTGTTAGAAACATCAGAAGGCTGCAGACAAATACTGGGACA\n"
67           + "GTTACACCCATCCCTTCATACAGGATCAGAAGAACTTAAATCATTATATAATACAGTAGCAACCCTCTATTG\n"
68           + "TGTGCATCAAAGGATAGAAGTAAAAGACACCAAGGAAGCTTTAGAC\n"
69           + ">gi|27804647|gb|AY178925.1|/1-261\n"
70           + "-TGGGAAAAAATTCGGTTAAGGCCAGGGGGAAAGAAAAAATATCAATTAAAACATGTAGTATGGGCAAGCAG\n"
71           + "GGAACTAGAACGATTCGCAGTTAATCCTGGCCTGTTAGAAACATCAGAAGGCTGTAGACAAATATTGGGACA\n"
72           + "GCTACAACCATCCCTTCAGACAGGATCAGAGGAACTTAAATCATTATTTAATACAGTAGCAGTCCTCTATTG\n"
73           + "TGTACATCAAAGAATAGATGTAAAAGACACCAAGGAAGCTCTAGAA\n"
74           + ">gi|27804649|gb|AY178926.1|/1-261\n"
75           + "-TGGGAAAAAATTCGGTTAAGGCCAGGGGGAAAGAAAAAATATAAGTTAAAACATATAGTATGGGCAAGCAG\n"
76           + "GGAGCTAGAACGATTCGCGGTCAATCCTGGCCTGTTAGAAACATCAGAAGGCTGTAGACAACTACTGGGACA\n"
77           + "GTTACAACCATCCCTTCAGACAGGATCAGAAGAACTCAAATCATTATATAATACAATAGCAACCCTCTATTG\n"
78           + "TGTGCATCAAAGGATAGAGATAAAAGACACCAAGGAAGCCTTAGAT\n"
79           + ">gi|27804653|gb|AY178928.1|/1-261\n"
80           + "-TGGGAAAGAATTCGGTTAAGGCCAGGGGGAAAGAAACAATATAAATTAAAACATATAGTATGGGCAAGCAG\n"
81           + "GGAGCTAGACCGATTCGCACTTAACCCCGGCCTGTTAGAAACATCAGAAGGCTGTAGACAAATATTGGGACA\n"
82           + "GCTACAATCGTCCCTTCAGACAGGATCAGAAGAACTTAGATCACTATATAATACAGTAGCAGTCCTCTATTG\n"
83           + "TGTGCATCAAAAGATAGATGTAAAAGACACCAAGGAAGCCTTAGAC\n"
84           + ">gi|27804659|gb|AY178931.1|/1-261\n"
85           + "-TGGGAAAAAATTCGGTTACGGCCAGGAGGAAAGAAAAGATATAAATTAAAACATATAGTATGGGCAAGCAG\n"
86           + "GGAGCTAGAACGATTYGCAGTTAATCCTGGCCTTTTAGAAACAGCAGAAGGCTGTAGACAAATACTGGGACA\n"
87           + "GCTACAACCATCCCTTCAGACAGGATCAGAAGAACTTAAATCATTATATAATACAGTAGCAACCCTCTATTG\n"
88           + "TGTACATCAAAGGATAGAGATAAAAGACACCAAGGAAGCTTTAGAA\n";
89   // @formatter:on
90
91   /**
92    * Corner case for this test is the presence of codons after codons that were
93    * not translated.
94    * 
95    * @throws IOException
96    */
97   @Test(groups ={ "Functional" })
98   public void testTranslateCdna_withUntranslatableCodons()
99           throws IOException
100   {
101     AlignmentI alf = new FormatAdapter().readFile(
102             JAL_1312_example_align_fasta, jalview.io.FormatAdapter.PASTE,
103             "FASTA");
104     ColumnSelection cs = new ColumnSelection();
105     AlignViewportI av = new AlignViewport(alf, cs);
106     Dna dna = new Dna(av, new int[]
107     { 0, alf.getWidth() - 1 });
108     AlignmentI translated = dna.translateCdna();
109     assertNotNull("Couldn't do a full width translation of test data.",
110             translated);
111   }
112
113   /**
114    * Test variant in which 15 column blocks at a time are translated (the rest
115    * hidden).
116    * 
117    * @throws IOException
118    */
119   @Test(groups ={ "Functional" })
120   public void testTranslateCdna_withUntranslatableCodonsAndHiddenColumns()
121           throws IOException
122   {
123     AlignmentI alf = new FormatAdapter().readFile(
124             JAL_1312_example_align_fasta, jalview.io.FormatAdapter.PASTE,
125             "FASTA");
126     int vwidth = 15;
127     for (int ipos = 0; ipos + vwidth < alf.getWidth(); ipos += vwidth)
128     {
129       ColumnSelection cs = new ColumnSelection();
130       if (ipos > 0)
131       {
132         cs.hideColumns(0, ipos - 1);
133       }
134       cs.hideColumns(ipos + vwidth, alf.getWidth());
135       int[] vcontigs = cs.getVisibleContigs(0, alf.getWidth());
136       AlignViewportI av = new AlignViewport(alf, cs);
137       Dna dna = new Dna(av, vcontigs);
138       AlignmentI transAlf = dna.translateCdna();
139
140       assertTrue("Translation failed (ipos=" + ipos
141               + ") No alignment data.", transAlf != null);
142       assertTrue("Translation failed (ipos=" + ipos + ") Empty alignment.",
143               transAlf.getHeight() > 0);
144       assertTrue("Translation failed (ipos=" + ipos + ") Translated "
145               + transAlf.getHeight() + " sequences from " + alf.getHeight()
146               + " sequences", alf.getHeight() == transAlf.getHeight());
147     }
148   }
149
150   /**
151    * Test simple translation to Amino Acids (with STOP codons translated to X).
152    * 
153    * @throws IOException
154    */
155   @Test(groups ={ "Functional" })
156   public void testTranslateCdna_simple() throws IOException
157   {
158     AlignmentI alf = new FormatAdapter().readFile(fasta,
159             FormatAdapter.PASTE, "FASTA");
160     ColumnSelection cs = new ColumnSelection();
161     AlignViewportI av = new AlignViewport(alf, cs);
162     Dna dna = new Dna(av, new int[]
163     { 0, alf.getWidth() - 1 });
164     AlignmentI translated = dna.translateCdna();
165     String aa = translated.getSequenceAt(0).getSequenceAsString();
166     assertEquals(
167             "AAAACCDDEEFFGGGGHHIIIKKLLLLLLMNNPPPPQQRRRRRRSSSSSSTTTTVVVVWYYXXX",
168             aa);
169   }
170
171   /**
172    * Test translation excluding hidden columns.
173    * 
174    * @throws IOException
175    */
176   @Test(groups ={ "Functional" })
177   public void testTranslateCdna_hiddenColumns() throws IOException
178   {
179     AlignmentI alf = new FormatAdapter().readFile(fasta,
180             FormatAdapter.PASTE, "FASTA");
181     ColumnSelection cs = new jalview.datamodel.ColumnSelection();
182     cs.hideColumns(6, 14); // hide codons 3/4/5
183     cs.hideColumns(24, 35); // hide codons 9-12
184     cs.hideColumns(177, 191); // hide codons 60-64
185     AlignViewportI av = new AlignViewport(alf, cs);
186     Dna dna = new Dna(av, new int[]
187     { 0, alf.getWidth() - 1 });
188     AlignmentI translated = dna.translateCdna();
189     String aa = translated.getSequenceAt(0).getSequenceAsString();
190     assertEquals("AACDDGGGGHHIIIKKLLLLLLMNNPPPPQQRRRRRRSSSSSSTTTTVVVVW", aa);
191   }
192
193   /**
194    * Use this test to help debug into any cases of interest.
195    */
196   @Test(groups ={ "Functional" })
197   public void testCompareCodonPos_oneOnly()
198   {
199     assertFollows("-AA--A", "G--GG"); // 2 shifted seq2, 3 shifted seq1
200   }
201
202   /**
203    * Tests for method that compares 'alignment' of two codon position triplets.
204    */
205   @Test(groups ={ "Functional" })
206   public void testCompareCodonPos()
207   {
208     /*
209      * Returns 0 for any null argument
210      */
211     assertEquals(0, Dna.compareCodonPos(new AlignedCodon(1, 2, 3), null));
212     assertEquals(0, Dna.compareCodonPos(null, new AlignedCodon(1, 2, 3)));
213
214     /*
215      * Work through 27 combinations. First 9 cases where first position matches.
216      */
217     assertMatches("AAA", "GGG"); // 2 and 3 match
218     assertFollows("AA-A", "GGG"); // 2 matches, 3 shifted seq1
219     assertPrecedes("AAA", "GG-G"); // 2 matches, 3 shifted seq2
220     assertFollows("A-AA", "GG-G"); // 2 shifted seq1, 3 matches
221     assertFollows("A-A-A", "GG-G"); // 2 shifted seq1, 3 shifted seq1
222     assertPrecedes("A-AA", "GG--G"); // 2 shifted seq1, 3 shifted seq2
223     assertPrecedes("AA-A", "G-GG"); // 2 shifted seq2, 3 matches
224     assertFollows("AA--A", "G-GG"); // 2 shifted seq2, 3 shifted seq1
225     assertPrecedes("AAA", "G-GG"); // 2 shifted seq2, 3 shifted seq2
226
227     /*
228      * 9 cases where first position is shifted in first sequence.
229      */
230     assertFollows("-AAA", "G-GG"); // 2 and 3 match
231     assertFollows("-AA-A", "G-GG"); // 2 matches, 3 shifted seq1
232     // 'enclosing' case: pick first to start precedes
233     assertFollows("-AAA", "G-G-G"); // 2 matches, 3 shifted seq2
234     assertFollows("-A-AA", "G-G-G"); // 2 shifted seq1, 3 matches
235     assertFollows("-A-A-A", "G-G-G"); // 2 shifted seq1, 3 shifted seq1
236     // 'enclosing' case: pick first to start precedes
237     assertFollows("-A-AA", "G-G--G"); // 2 shifted seq1, 3 shifted seq2
238     assertFollows("-AA-A", "G--GG"); // 2 shifted seq2, 3 matches
239     assertFollows("-AA--A", "G--GG"); // 2 shifted seq2, 3 shifted seq1
240     assertPrecedes("-AAA", "G--GG"); // 2 shifted seq2, 3 shifted seq2
241
242     /*
243      * 9 cases where first position is shifted in second sequence.
244      */
245     assertPrecedes("A-AA", "-GGG"); // 2 and 3 match
246     assertPrecedes("A-A-A", "-GGG"); // 2 matches, 3 shifted seq1
247     assertPrecedes("A-AA", "-GG-G"); // 2 matches, 3 shifted seq2
248     assertPrecedes("A--AA", "-GG-G"); // 2 shifted seq1, 3 matches
249     // 'enclosing' case with middle base deciding:
250     assertFollows("A--AA", "-GGG"); // 2 shifted seq1, 3 shifted seq1
251     assertPrecedes("A--AA", "-GG--G"); // 2 shifted seq1, 3 shifted seq2
252     assertPrecedes("AA-A", "-GGG"); // 2 shifted seq2, 3 matches
253     assertPrecedes("AA--A", "-GGG"); // 2 shifted seq2, 3 shifted seq1
254     assertPrecedes("AAA", "-GGG"); // 2 shifted seq2, 3 shifted seq2
255   }
256
257   /**
258    * This test generates a random cDNA alignment and its translation, then
259    * reorders the cDNA and retranslates, and verifies that the translations are
260    * the same (apart from ordering).
261    */
262   @Test(groups ={ "Functional" })
263   public void testTranslateCdna_sequenceOrderIndependent()
264   {
265     /*
266      * Generate cDNA - 8 sequences of 12 bases each.
267      */
268     AlignmentI cdna = new DnaAlignmentGenerator().generate(12, 8, 97, 5, 5);
269     ColumnSelection cs = new ColumnSelection();
270     AlignViewportI av = new AlignViewport(cdna, cs);
271     Dna dna = new Dna(av, new int[]
272     { 0, cdna.getWidth() - 1 });
273     AlignmentI translated = dna.translateCdna();
274
275     /*
276      * Jumble the cDNA sequences and translate.
277      */
278     SequenceI[] sorted = new SequenceI[cdna.getHeight()];
279     final int[] jumbler = new int[]
280     { 6, 7, 3, 4, 2, 0, 1, 5 };
281     int seqNo = 0;
282     for (int i : jumbler)
283     {
284       sorted[seqNo++] = cdna.getSequenceAt(i);
285     }
286     AlignmentI cdnaReordered = new Alignment(sorted);
287     av = new AlignViewport(cdnaReordered, cs);
288     dna = new Dna(av, new int[]
289     { 0, cdna.getWidth() - 1 });
290     AlignmentI translated2 = dna.translateCdna();
291
292     /*
293      * Check translated sequences are the same in both alignments.
294      */
295     System.out.println("Original");
296     System.out.println(translated.toString());
297     System.out.println("Sorted");
298     System.out.println(translated2.toString());
299
300     int sortedSequenceIndex = 0;
301     for (int originalSequenceIndex : jumbler)
302     {
303       final String translation1 = translated.getSequenceAt(
304               originalSequenceIndex).getSequenceAsString();
305       final String translation2 = translated2.getSequenceAt(sortedSequenceIndex)
306               .getSequenceAsString();
307       assertEquals(translation2, translation1);
308       sortedSequenceIndex++;
309     }
310   }
311
312   /**
313    * Test that all the cases in testCompareCodonPos have a 'symmetric'
314    * comparison (without checking the actual comparison result).
315    */
316   @Test(groups ={ "Functional" })
317   public void testCompareCodonPos_isSymmetric()
318   {
319     assertSymmetric("AAA", "GGG");
320     assertSymmetric("AA-A", "GGG");
321     assertSymmetric("AAA", "GG-G");
322     assertSymmetric("A-AA", "GG-G");
323     assertSymmetric("A-A-A", "GG-G");
324     assertSymmetric("A-AA", "GG--G");
325     assertSymmetric("AA-A", "G-GG");
326     assertSymmetric("AA--A", "G-GG");
327     assertSymmetric("AAA", "G-GG");
328     assertSymmetric("-AAA", "G-GG");
329     assertSymmetric("-AA-A", "G-GG");
330     assertSymmetric("-AAA", "G-G-G");
331     assertSymmetric("-A-AA", "G-G-G");
332     assertSymmetric("-A-A-A", "G-G-G");
333     assertSymmetric("-A-AA", "G-G--G");
334     assertSymmetric("-AA-A", "G--GG");
335     assertSymmetric("-AA--A", "G--GG");
336     assertSymmetric("-AAA", "G--GG");
337     assertSymmetric("A-AA", "-GGG");
338     assertSymmetric("A-A-A", "-GGG");
339     assertSymmetric("A-AA", "-GG-G");
340     assertSymmetric("A--AA", "-GG-G");
341     assertSymmetric("A--AA", "-GGG");
342     assertSymmetric("A--AA", "-GG--G");
343     assertSymmetric("AA-A", "-GGG");
344     assertSymmetric("AA--A", "-GGG");
345     assertSymmetric("AAA", "-GGG");
346   }
347
348   private void assertSymmetric(String codon1, String codon2)
349   {
350     assertEquals("Comparison of '" + codon1 + "' and '" + codon2
351             + " not symmetric", Integer.signum(compare(codon1, codon2)),
352             -Integer.signum(compare(codon2, codon1)));
353   }
354
355   /**
356    * Assert that the first sequence should map to the same position as the
357    * second in a translated alignment. Also checks that this is true if the
358    * order of the codons is reversed.
359    * 
360    * @param codon1
361    * @param codon2
362    */
363   private void assertMatches(String codon1, String codon2)
364   {
365     assertEquals("Expected '" + codon1 + "' matches '" + codon2 + "'", 0,
366             compare(codon1, codon2));
367     assertEquals("Expected '" + codon2 + "' matches '" + codon1 + "'", 0,
368             compare(codon2, codon1));
369   }
370
371   /**
372    * Assert that the first sequence should precede the second in a translated
373    * alignment
374    * 
375    * @param codon1
376    * @param codon2
377    */
378   private void assertPrecedes(String codon1, String codon2)
379   {
380     assertEquals("Expected '" + codon1 + "'  precedes '" + codon2 + "'",
381             -1, compare(codon1, codon2));
382   }
383
384   /**
385    * Assert that the first sequence should follow the second in a translated
386    * alignment
387    * 
388    * @param codon1
389    * @param codon2
390    */
391   private void assertFollows(String codon1, String codon2)
392   {
393     assertEquals("Expected '" + codon1 + "'  follows '" + codon2 + "'", 1,
394             compare(codon1, codon2));
395   }
396
397   /**
398    * Convert two nucleotide strings to base positions and pass to
399    * Dna.compareCodonPos, return the result.
400    * 
401    * @param s1
402    * @param s2
403    * @return
404    */
405   private int compare(String s1, String s2)
406   {
407     final AlignedCodon cd1 = convertCodon(s1);
408     final AlignedCodon cd2 = convertCodon(s2);
409     System.out.println("K: " + s1 + "  " + cd1.toString());
410     System.out.println("G: " + s2 + "  " + cd2.toString());
411     System.out.println();
412     return Dna.compareCodonPos(cd1, cd2);
413   }
414
415   /**
416    * Convert a string e.g. "-GC-T" to base positions e.g. [1, 2, 4]. The string
417    * should have exactly 3 non-gap characters, and use '-' for gaps.
418    * 
419    * @param s
420    * @return
421    */
422   private AlignedCodon convertCodon(String s)
423   {
424     int[] codon = new int[3];
425     int i = 0;
426     for (int j = 0; j < s.length(); j++)
427     {
428       if (s.charAt(j) != '-')
429       {
430         codon[i++] = j;
431       }
432     }
433     return new AlignedCodon(codon[0], codon[1], codon[2]);
434   }
435
436   /**
437    * Weirdly, maybe worth a test to prove the helper method of this test class.
438    */
439   @Test(groups ={ "Functional" })
440   public void testConvertCodon()
441   {
442     assertEquals("[0, 1, 2]", convertCodon("AAA").toString());
443     assertEquals("[0, 2, 5]", convertCodon("A-A--A").toString());
444     assertEquals("[1, 3, 4]", convertCodon("-A-AA-").toString());
445   }
446 }