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