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;
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;
18 import org.testng.annotations.Test;
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";
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";
92 * Corner case for this test is the presence of codons after codons that were
98 public void testTranslateCdna_withUntranslatableCodons()
101 AlignmentI alf = new FormatAdapter().readFile(
102 JAL_1312_example_align_fasta, jalview.io.FormatAdapter.PASTE,
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.",
114 * Test variant in which 15 column blocks at a time are translated (the rest
117 * @throws IOException
120 public void testTranslateCdna_withUntranslatableCodonsAndHiddenColumns()
123 AlignmentI alf = new FormatAdapter().readFile(
124 JAL_1312_example_align_fasta, jalview.io.FormatAdapter.PASTE,
127 for (int ipos = 0; ipos + vwidth < alf.getWidth(); ipos += vwidth)
129 ColumnSelection cs = new ColumnSelection();
132 cs.hideColumns(0, ipos - 1);
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();
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());
151 * Test simple translation to Amino Acids (with STOP codons translated to X).
153 * @throws IOException
156 public void testTranslateCdna_simple() throws IOException
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();
167 "AAAACCDDEEFFGGGGHHIIIKKLLLLLLMNNPPPPQQRRRRRRSSSSSSTTTTVVVVWYYXXX",
172 * Test translation excluding hidden columns.
174 * @throws IOException
177 public void testTranslateCdna_hiddenColumns() throws IOException
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);
194 * Use this test to help debug into any cases of interest.
197 public void testCompareCodonPos_oneOnly()
199 assertFollows("-AA--A", "G--GG"); // 2 shifted seq2, 3 shifted seq1
203 * Tests for method that compares 'alignment' of two codon position triplets.
206 public void testCompareCodonPos()
209 * Returns 0 for any null argument
211 assertEquals(0, Dna.compareCodonPos(new AlignedCodon(1, 2, 3), null));
212 assertEquals(0, Dna.compareCodonPos(null, new AlignedCodon(1, 2, 3)));
215 * Work through 27 combinations. First 9 cases where first position matches.
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
228 * 9 cases where first position is shifted in first sequence.
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
243 * 9 cases where first position is shifted in second sequence.
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
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).
263 public void testTranslateCdna_sequenceOrderIndependent()
266 * Generate cDNA - 8 sequences of 12 bases each.
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();
276 * Jumble the cDNA sequences and translate.
278 SequenceI[] sorted = new SequenceI[cdna.getHeight()];
279 final int[] jumbler = new int[]
280 { 6, 7, 3, 4, 2, 0, 1, 5 };
282 for (int i : jumbler)
284 sorted[seqNo++] = cdna.getSequenceAt(i);
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();
293 * Check translated sequences are the same in both alignments.
295 System.out.println("Original");
296 System.out.println(translated.toString());
297 System.out.println("Sorted");
298 System.out.println(translated2.toString());
300 int sortedSequenceIndex = 0;
301 for (int originalSequenceIndex : jumbler)
303 final String translation1 = translated.getSequenceAt(
304 originalSequenceIndex).getSequenceAsString();
305 final String translation2 = translated2.getSequenceAt(sortedSequenceIndex)
306 .getSequenceAsString();
307 assertEquals(translation2, translation1);
308 sortedSequenceIndex++;
313 * Test that all the cases in testCompareCodonPos have a 'symmetric'
314 * comparison (without checking the actual comparison result).
317 public void testCompareCodonPos_isSymmetric()
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");
348 private void assertSymmetric(String codon1, String codon2)
350 assertEquals("Comparison of '" + codon1 + "' and '" + codon2
351 + " not symmetric", Integer.signum(compare(codon1, codon2)),
352 -Integer.signum(compare(codon2, codon1)));
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.
363 private void assertMatches(String codon1, String codon2)
365 assertEquals("Expected '" + codon1 + "' matches '" + codon2 + "'", 0,
366 compare(codon1, codon2));
367 assertEquals("Expected '" + codon2 + "' matches '" + codon1 + "'", 0,
368 compare(codon2, codon1));
372 * Assert that the first sequence should precede the second in a translated
378 private void assertPrecedes(String codon1, String codon2)
380 assertEquals("Expected '" + codon1 + "' precedes '" + codon2 + "'",
381 -1, compare(codon1, codon2));
385 * Assert that the first sequence should follow the second in a translated
391 private void assertFollows(String codon1, String codon2)
393 assertEquals("Expected '" + codon1 + "' follows '" + codon2 + "'", 1,
394 compare(codon1, codon2));
398 * Convert two nucleotide strings to base positions and pass to
399 * Dna.compareCodonPos, return the result.
405 private int compare(String s1, String s2)
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);
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.
422 private AlignedCodon convertCodon(String s)
424 int[] codon = new int[3];
426 for (int j = 0; j < s.length(); j++)
428 if (s.charAt(j) != '-')
433 return new AlignedCodon(codon[0], codon[1], codon[2]);
437 * Weirdly, maybe worth a test to prove the helper method of this test class.
440 public void testConvertCodon()
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());