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