JAL-2089 patch broken merge to master for Release 2.10.0b1
[jalview.git] / test / jalview / analysis / DnaTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.analysis;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertNotNull;
25 import static org.testng.AssertJUnit.assertTrue;
26
27 import jalview.api.AlignViewportI;
28 import jalview.datamodel.AlignedCodon;
29 import jalview.datamodel.Alignment;
30 import jalview.datamodel.AlignmentI;
31 import jalview.datamodel.ColumnSelection;
32 import jalview.datamodel.Sequence;
33 import jalview.datamodel.SequenceI;
34 import jalview.gui.AlignViewport;
35 import jalview.io.FormatAdapter;
36
37 import java.io.IOException;
38
39 import org.testng.annotations.Test;
40
41 public class DnaTest
42 {
43   // @formatter:off
44   // AA encoding codons as ordered on the Jalview help page Amino Acid Table
45   private static String fasta = ">B\n" + "GCT" + "GCC" + "GCA" + "GCG"
46           + "TGT" + "TGC" + "GAT" + "GAC" + "GAA" + "GAG" + "TTT" + "TTC"
47           + "GGT" + "GGC" + "GGA" + "GGG" + "CAT" + "CAC" + "ATT" + "ATC"
48           + "ATA" + "AAA" + "AAG" + "TTG" + "TTA" + "CTT" + "CTC" + "CTA"
49           + "CTG" + "ATG" + "AAT" + "AAC" + "CCT" + "CCC" + "CCA" + "CCG"
50           + "CAA" + "CAG" + "CGT" + "CGC" + "CGA" + "CGG" + "AGA" + "AGG"
51           + "TCT" + "TCC" + "TCA" + "TCG" + "AGT" + "AGC" + "ACT" + "ACC"
52           + "ACA" + "ACG" + "GTT" + "GTC" + "GTA" + "GTG" + "TGG" + "TAT"
53           + "TAC" + "TAA" + "TAG" + "TGA";
54
55   private static String JAL_1312_example_align_fasta = ">B.FR.83.HXB2_LAI_IIIB_BRU_K03455/45-306\n"
56           + "ATGGGAAAAAATTCGGTTAAGGCCAGGGGGAAAGAAAAAATATAAATTAAAACATATAGTATGGGCAAGCAG\n"
57           + "GGAGCTAGAACGATTCGCAGTTAATCCTGGCCTGTTAGAAACATCAGAAGGCTGTAGACAAATACTGGGACA\n"
58           + "GCTACAACCATCCCTTCAGACAGGATCAGAAGAACTTAGATCATTATATAATACAGTAGCAACCCTCTATTG\n"
59           + "TGTGCATCAAAGGATAGAGATAAAAGACACCAAGGAAGCTTTAGAC\n"
60           + ">gi|27804621|gb|AY178912.1|/1-259\n"
61           + "-TGGGAGAA-ATTCGGTT-CGGCCAGGGGGAAAGAAAAAATATCAGTTAAAACATATAGTATGGGCAAGCAG\n"
62           + "AGAGCTAGAACGATTCGCAGTTAACCCTGGCCTTTTAGAGACATCACAAGGCTGTAGACAAATACTGGGACA\n"
63           + "GCTACAACCATCCCTTCAGACAGGATCAGAAGAACTTAAATCATTATATAATACAGTAGCAACCCTCTATTG\n"
64           + "TGTTCATCAAAGGATAGATATAAAAGACACCAAGGAAGCTTTAGAT\n"
65           + ">gi|27804623|gb|AY178913.1|/1-259\n"
66           + "-TGGGAGAA-ATTCGGTT-CGGCCAGGGGGAAAGAAAAAATATCAGTTAAAACATATAGTATGGGCAAGCAG\n"
67           + "AGAGCTAGAACGATTCGCAGTTAACCCTGGCCTTTTAGAGACATCACAAGGCTGTAGACAAATACTGGAACA\n"
68           + "GCTACAACCATCCCTTCAGACAGGATCAGAAGAACTTAAATCATTATATAATACAGTAGCAACCCTCTATTG\n"
69           + "TGTTCATCAAAGGATAGATGTAAAAGACACCAAGGAAGCTTTAGAT\n"
70           + ">gi|27804627|gb|AY178915.1|/1-260\n"
71           + "-TGGGAAAA-ATTCGGTTAAGGCCAGGGGGAAAGAAAAAATATAAGTTAAAACATATAGTATGGGCAAGCAG\n"
72           + "GGAGCTAGAACGATTCGCAGTTAACCCTGGCCTGTTAGAAACATCAGAAGGTTGTAGACAAATATTGGGACA\n"
73           + "GCTACAACCATCCCTTGAGACAGGATCAGAAGAACTTAAATCATTATWTAATACCATAGCAGTCCTCTATTG\n"
74           + "TGTACATCAAAGGATAGATATAAAAGACACCAAGGAAGCTTTAGAG\n"
75           + ">gi|27804631|gb|AY178917.1|/1-261\n"
76           + "-TGGGAAAAAATTCGGTTGAGGCCAGGGGGAAAGAAAAAATATAAGTTAAAACATATAGTATGGGCAAGCAG\n"
77           + "GGAGCTAGAACGATTCGCAGTCAACCCTGGCCTGTTAGAAACACCAGAAGGCTGTAGACAAATACTGGGACA\n"
78           + "GCTACAACCGTCCCTTCAGACAGGATCGGAAGAACTTAAATCATTATATAATACAGTAGCAACCCTCTATTG\n"
79           + "TGTGCATCAAAGGATAGATGTAAAAGACACCAAGGAGGCTTTAGAC\n"
80           + ">gi|27804635|gb|AY178919.1|/1-261\n"
81           + "-TGGGAGAGAATTCGGTTACGGCCAGGAGGAAAGAAAAAATATAAATTGAAACATATAGTATGGGCAGGCAG\n"
82           + "AGAGCTAGATCGATTCGCAGTCAATCCTGGCCTGTTAGAAACATCAGAAGGCTGCAGACAGATATTGGGACA\n"
83           + "GCTACAACCGTCCCTTAAGACAGGATCAGAAGAACTTAAATCATTATATAATACAGTAGCAACCCTCTATTG\n"
84           + "TGTACATCAAAGGATAGATGTAAAAGACACCAAGGAAGCTTTAGAT\n"
85           + ">gi|27804641|gb|AY178922.1|/1-261\n"
86           + "-TGGGAGAAAATTCGGTTACGGCCAGGGGGAAAGAAAAGATATAAGTTAAAACATATAGTATGGGCAAGCAG\n"
87           + "GGAGCTAGAACGATTCGCAGTCAACCCTGGCCTGTTAGAAACATCAGAAGGCTGCAGACAAATACTGGGACA\n"
88           + "GTTACACCCATCCCTTCATACAGGATCAGAAGAACTTAAATCATTATATAATACAGTAGCAACCCTCTATTG\n"
89           + "TGTGCATCAAAGGATAGAAGTAAAAGACACCAAGGAAGCTTTAGAC\n"
90           + ">gi|27804647|gb|AY178925.1|/1-261\n"
91           + "-TGGGAAAAAATTCGGTTAAGGCCAGGGGGAAAGAAAAAATATCAATTAAAACATGTAGTATGGGCAAGCAG\n"
92           + "GGAACTAGAACGATTCGCAGTTAATCCTGGCCTGTTAGAAACATCAGAAGGCTGTAGACAAATATTGGGACA\n"
93           + "GCTACAACCATCCCTTCAGACAGGATCAGAGGAACTTAAATCATTATTTAATACAGTAGCAGTCCTCTATTG\n"
94           + "TGTACATCAAAGAATAGATGTAAAAGACACCAAGGAAGCTCTAGAA\n"
95           + ">gi|27804649|gb|AY178926.1|/1-261\n"
96           + "-TGGGAAAAAATTCGGTTAAGGCCAGGGGGAAAGAAAAAATATAAGTTAAAACATATAGTATGGGCAAGCAG\n"
97           + "GGAGCTAGAACGATTCGCGGTCAATCCTGGCCTGTTAGAAACATCAGAAGGCTGTAGACAACTACTGGGACA\n"
98           + "GTTACAACCATCCCTTCAGACAGGATCAGAAGAACTCAAATCATTATATAATACAATAGCAACCCTCTATTG\n"
99           + "TGTGCATCAAAGGATAGAGATAAAAGACACCAAGGAAGCCTTAGAT\n"
100           + ">gi|27804653|gb|AY178928.1|/1-261\n"
101           + "-TGGGAAAGAATTCGGTTAAGGCCAGGGGGAAAGAAACAATATAAATTAAAACATATAGTATGGGCAAGCAG\n"
102           + "GGAGCTAGACCGATTCGCACTTAACCCCGGCCTGTTAGAAACATCAGAAGGCTGTAGACAAATATTGGGACA\n"
103           + "GCTACAATCGTCCCTTCAGACAGGATCAGAAGAACTTAGATCACTATATAATACAGTAGCAGTCCTCTATTG\n"
104           + "TGTGCATCAAAAGATAGATGTAAAAGACACCAAGGAAGCCTTAGAC\n"
105           + ">gi|27804659|gb|AY178931.1|/1-261\n"
106           + "-TGGGAAAAAATTCGGTTACGGCCAGGAGGAAAGAAAAGATATAAATTAAAACATATAGTATGGGCAAGCAG\n"
107           + "GGAGCTAGAACGATTYGCAGTTAATCCTGGCCTTTTAGAAACAGCAGAAGGCTGTAGACAAATACTGGGACA\n"
108           + "GCTACAACCATCCCTTCAGACAGGATCAGAAGAACTTAAATCATTATATAATACAGTAGCAACCCTCTATTG\n"
109           + "TGTACATCAAAGGATAGAGATAAAAGACACCAAGGAAGCTTTAGAA\n";
110   // @formatter:on
111
112   /**
113    * Corner case for this test is the presence of codons after codons that were
114    * not translated.
115    * 
116    * @throws IOException
117    */
118   @Test(groups = { "Functional" })
119   public void testTranslateCdna_withUntranslatableCodons()
120           throws IOException
121   {
122     AlignmentI alf = new FormatAdapter().readFile(
123             JAL_1312_example_align_fasta, jalview.io.FormatAdapter.PASTE,
124             "FASTA");
125     ColumnSelection cs = new ColumnSelection();
126     AlignViewportI av = new AlignViewport(alf, cs);
127     Dna dna = new Dna(av, new int[] { 0, alf.getWidth() - 1 });
128     AlignmentI translated = dna.translateCdna();
129     assertNotNull("Couldn't do a full width translation of test data.",
130             translated);
131   }
132
133   /**
134    * Test variant in which 15 column blocks at a time are translated (the rest
135    * hidden).
136    * 
137    * @throws IOException
138    */
139   @Test(groups = { "Functional" })
140   public void testTranslateCdna_withUntranslatableCodonsAndHiddenColumns()
141           throws IOException
142   {
143     AlignmentI alf = new FormatAdapter().readFile(
144             JAL_1312_example_align_fasta, jalview.io.FormatAdapter.PASTE,
145             "FASTA");
146     int vwidth = 15;
147     for (int ipos = 0; ipos + vwidth < alf.getWidth(); ipos += vwidth)
148     {
149       ColumnSelection cs = new ColumnSelection();
150       if (ipos > 0)
151       {
152         cs.hideColumns(0, ipos - 1);
153       }
154       cs.hideColumns(ipos + vwidth, alf.getWidth());
155       int[] vcontigs = cs.getVisibleContigs(0, alf.getWidth());
156       AlignViewportI av = new AlignViewport(alf, cs);
157       Dna dna = new Dna(av, vcontigs);
158       AlignmentI transAlf = dna.translateCdna();
159
160       assertTrue("Translation failed (ipos=" + ipos
161               + ") No alignment data.", transAlf != null);
162       assertTrue("Translation failed (ipos=" + ipos + ") Empty alignment.",
163               transAlf.getHeight() > 0);
164       assertTrue("Translation failed (ipos=" + ipos + ") Translated "
165               + transAlf.getHeight() + " sequences from " + alf.getHeight()
166               + " sequences", alf.getHeight() == transAlf.getHeight());
167     }
168   }
169
170   /**
171    * Test simple translation to Amino Acids (with STOP codons translated to *).
172    * 
173    * @throws IOException
174    */
175   @Test(groups = { "Functional" })
176   public void testTranslateCdna_simple() throws IOException
177   {
178     AlignmentI alf = new FormatAdapter().readFile(fasta,
179             FormatAdapter.PASTE, "FASTA");
180     ColumnSelection cs = new ColumnSelection();
181     AlignViewportI av = new AlignViewport(alf, cs);
182     Dna dna = new Dna(av, new int[] { 0, alf.getWidth() - 1 });
183     AlignmentI translated = dna.translateCdna();
184     String aa = translated.getSequenceAt(0).getSequenceAsString();
185     assertEquals(
186             "AAAACCDDEEFFGGGGHHIIIKKLLLLLLMNNPPPPQQRRRRRRSSSSSSTTTTVVVVWYY***",
187             aa);
188   }
189
190   /**
191    * Test translation excluding hidden columns.
192    * 
193    * @throws IOException
194    */
195   @Test(groups = { "Functional" })
196   public void testTranslateCdna_hiddenColumns() throws IOException
197   {
198     AlignmentI alf = new FormatAdapter().readFile(fasta,
199             FormatAdapter.PASTE, "FASTA");
200     ColumnSelection cs = new jalview.datamodel.ColumnSelection();
201     cs.hideColumns(6, 14); // hide codons 3/4/5
202     cs.hideColumns(24, 35); // hide codons 9-12
203     cs.hideColumns(177, 191); // hide codons 60-64
204     AlignViewportI av = new AlignViewport(alf, cs);
205     Dna dna = new Dna(av, new int[] { 0, alf.getWidth() - 1 });
206     AlignmentI translated = dna.translateCdna();
207     String aa = translated.getSequenceAt(0).getSequenceAsString();
208     assertEquals("AACDDGGGGHHIIIKKLLLLLLMNNPPPPQQRRRRRRSSSSSSTTTTVVVVW", aa);
209   }
210
211   /**
212    * Use this test to help debug into any cases of interest.
213    */
214   @Test(groups = { "Functional" })
215   public void testCompareCodonPos_oneOnly()
216   {
217     assertFollows("-AA--A", "G--GG"); // 2 shifted seq2, 3 shifted seq1
218   }
219
220   /**
221    * Tests for method that compares 'alignment' of two codon position triplets.
222    */
223   @Test(groups = { "Functional" })
224   public void testCompareCodonPos()
225   {
226     /*
227      * Returns 0 for any null argument
228      */
229     assertEquals(0, Dna.compareCodonPos(new AlignedCodon(1, 2, 3), null));
230     assertEquals(0, Dna.compareCodonPos(null, new AlignedCodon(1, 2, 3)));
231
232     /*
233      * Work through 27 combinations. First 9 cases where first position matches.
234      */
235     assertMatches("AAA", "GGG"); // 2 and 3 match
236     assertFollows("AA-A", "GGG"); // 2 matches, 3 shifted seq1
237     assertPrecedes("AAA", "GG-G"); // 2 matches, 3 shifted seq2
238     assertFollows("A-AA", "GG-G"); // 2 shifted seq1, 3 matches
239     assertFollows("A-A-A", "GG-G"); // 2 shifted seq1, 3 shifted seq1
240     assertPrecedes("A-AA", "GG--G"); // 2 shifted seq1, 3 shifted seq2
241     assertPrecedes("AA-A", "G-GG"); // 2 shifted seq2, 3 matches
242     assertFollows("AA--A", "G-GG"); // 2 shifted seq2, 3 shifted seq1
243     assertPrecedes("AAA", "G-GG"); // 2 shifted seq2, 3 shifted seq2
244
245     /*
246      * 9 cases where first position is shifted in first sequence.
247      */
248     assertFollows("-AAA", "G-GG"); // 2 and 3 match
249     assertFollows("-AA-A", "G-GG"); // 2 matches, 3 shifted seq1
250     // 'enclosing' case: pick first to start precedes
251     assertFollows("-AAA", "G-G-G"); // 2 matches, 3 shifted seq2
252     assertFollows("-A-AA", "G-G-G"); // 2 shifted seq1, 3 matches
253     assertFollows("-A-A-A", "G-G-G"); // 2 shifted seq1, 3 shifted seq1
254     // 'enclosing' case: pick first to start precedes
255     assertFollows("-A-AA", "G-G--G"); // 2 shifted seq1, 3 shifted seq2
256     assertFollows("-AA-A", "G--GG"); // 2 shifted seq2, 3 matches
257     assertFollows("-AA--A", "G--GG"); // 2 shifted seq2, 3 shifted seq1
258     assertPrecedes("-AAA", "G--GG"); // 2 shifted seq2, 3 shifted seq2
259
260     /*
261      * 9 cases where first position is shifted in second sequence.
262      */
263     assertPrecedes("A-AA", "-GGG"); // 2 and 3 match
264     assertPrecedes("A-A-A", "-GGG"); // 2 matches, 3 shifted seq1
265     assertPrecedes("A-AA", "-GG-G"); // 2 matches, 3 shifted seq2
266     assertPrecedes("A--AA", "-GG-G"); // 2 shifted seq1, 3 matches
267     // 'enclosing' case with middle base deciding:
268     assertFollows("A--AA", "-GGG"); // 2 shifted seq1, 3 shifted seq1
269     assertPrecedes("A--AA", "-GG--G"); // 2 shifted seq1, 3 shifted seq2
270     assertPrecedes("AA-A", "-GGG"); // 2 shifted seq2, 3 matches
271     assertPrecedes("AA--A", "-GGG"); // 2 shifted seq2, 3 shifted seq1
272     assertPrecedes("AAA", "-GGG"); // 2 shifted seq2, 3 shifted seq2
273   }
274
275   /**
276    * This test generates a random cDNA alignment and its translation, then
277    * reorders the cDNA and retranslates, and verifies that the translations are
278    * the same (apart from ordering).
279    */
280   @Test(groups = { "Functional" })
281   public void testTranslateCdna_sequenceOrderIndependent()
282   {
283     /*
284      * Generate cDNA - 8 sequences of 12 bases each.
285      */
286     AlignmentI cdna = new DnaAlignmentGenerator().generate(12, 8, 97, 5, 5);
287     ColumnSelection cs = new ColumnSelection();
288     AlignViewportI av = new AlignViewport(cdna, cs);
289     Dna dna = new Dna(av, new int[] { 0, cdna.getWidth() - 1 });
290     AlignmentI translated = dna.translateCdna();
291
292     /*
293      * Jumble the cDNA sequences and translate.
294      */
295     SequenceI[] sorted = new SequenceI[cdna.getHeight()];
296     final int[] jumbler = new int[] { 6, 7, 3, 4, 2, 0, 1, 5 };
297     int seqNo = 0;
298     for (int i : jumbler)
299     {
300       sorted[seqNo++] = cdna.getSequenceAt(i);
301     }
302     AlignmentI cdnaReordered = new Alignment(sorted);
303     av = new AlignViewport(cdnaReordered, cs);
304     dna = new Dna(av, new int[] { 0, cdna.getWidth() - 1 });
305     AlignmentI translated2 = dna.translateCdna();
306
307     /*
308      * Check translated sequences are the same in both alignments.
309      */
310     System.out.println("Original");
311     System.out.println(translated.toString());
312     System.out.println("Sorted");
313     System.out.println(translated2.toString());
314
315     int sortedSequenceIndex = 0;
316     for (int originalSequenceIndex : jumbler)
317     {
318       final String translation1 = translated.getSequenceAt(
319               originalSequenceIndex).getSequenceAsString();
320       final String translation2 = translated2.getSequenceAt(
321               sortedSequenceIndex).getSequenceAsString();
322       assertEquals(translation2, translation1);
323       sortedSequenceIndex++;
324     }
325   }
326
327   /**
328    * Test that all the cases in testCompareCodonPos have a 'symmetric'
329    * comparison (without checking the actual comparison result).
330    */
331   @Test(groups = { "Functional" })
332   public void testCompareCodonPos_isSymmetric()
333   {
334     assertSymmetric("AAA", "GGG");
335     assertSymmetric("AA-A", "GGG");
336     assertSymmetric("AAA", "GG-G");
337     assertSymmetric("A-AA", "GG-G");
338     assertSymmetric("A-A-A", "GG-G");
339     assertSymmetric("A-AA", "GG--G");
340     assertSymmetric("AA-A", "G-GG");
341     assertSymmetric("AA--A", "G-GG");
342     assertSymmetric("AAA", "G-GG");
343     assertSymmetric("-AAA", "G-GG");
344     assertSymmetric("-AA-A", "G-GG");
345     assertSymmetric("-AAA", "G-G-G");
346     assertSymmetric("-A-AA", "G-G-G");
347     assertSymmetric("-A-A-A", "G-G-G");
348     assertSymmetric("-A-AA", "G-G--G");
349     assertSymmetric("-AA-A", "G--GG");
350     assertSymmetric("-AA--A", "G--GG");
351     assertSymmetric("-AAA", "G--GG");
352     assertSymmetric("A-AA", "-GGG");
353     assertSymmetric("A-A-A", "-GGG");
354     assertSymmetric("A-AA", "-GG-G");
355     assertSymmetric("A--AA", "-GG-G");
356     assertSymmetric("A--AA", "-GGG");
357     assertSymmetric("A--AA", "-GG--G");
358     assertSymmetric("AA-A", "-GGG");
359     assertSymmetric("AA--A", "-GGG");
360     assertSymmetric("AAA", "-GGG");
361   }
362
363   private void assertSymmetric(String codon1, String codon2)
364   {
365     assertEquals("Comparison of '" + codon1 + "' and '" + codon2
366             + " not symmetric", Integer.signum(compare(codon1, codon2)),
367             -Integer.signum(compare(codon2, codon1)));
368   }
369
370   /**
371    * Assert that the first sequence should map to the same position as the
372    * second in a translated alignment. Also checks that this is true if the
373    * order of the codons is reversed.
374    * 
375    * @param codon1
376    * @param codon2
377    */
378   private void assertMatches(String codon1, String codon2)
379   {
380     assertEquals("Expected '" + codon1 + "' matches '" + codon2 + "'", 0,
381             compare(codon1, codon2));
382     assertEquals("Expected '" + codon2 + "' matches '" + codon1 + "'", 0,
383             compare(codon2, codon1));
384   }
385
386   /**
387    * Assert that the first sequence should precede the second in a translated
388    * alignment
389    * 
390    * @param codon1
391    * @param codon2
392    */
393   private void assertPrecedes(String codon1, String codon2)
394   {
395     assertEquals("Expected '" + codon1 + "'  precedes '" + codon2 + "'",
396             -1, compare(codon1, codon2));
397   }
398
399   /**
400    * Assert that the first sequence should follow the second in a translated
401    * alignment
402    * 
403    * @param codon1
404    * @param codon2
405    */
406   private void assertFollows(String codon1, String codon2)
407   {
408     assertEquals("Expected '" + codon1 + "'  follows '" + codon2 + "'", 1,
409             compare(codon1, codon2));
410   }
411
412   /**
413    * Convert two nucleotide strings to base positions and pass to
414    * Dna.compareCodonPos, return the result.
415    * 
416    * @param s1
417    * @param s2
418    * @return
419    */
420   private int compare(String s1, String s2)
421   {
422     final AlignedCodon cd1 = convertCodon(s1);
423     final AlignedCodon cd2 = convertCodon(s2);
424     System.out.println("K: " + s1 + "  " + cd1.toString());
425     System.out.println("G: " + s2 + "  " + cd2.toString());
426     System.out.println();
427     return Dna.compareCodonPos(cd1, cd2);
428   }
429
430   /**
431    * Convert a string e.g. "-GC-T" to base positions e.g. [1, 2, 4]. The string
432    * should have exactly 3 non-gap characters, and use '-' for gaps.
433    * 
434    * @param s
435    * @return
436    */
437   private AlignedCodon convertCodon(String s)
438   {
439     int[] codon = new int[3];
440     int i = 0;
441     for (int j = 0; j < s.length(); j++)
442     {
443       if (s.charAt(j) != '-')
444       {
445         codon[i++] = j;
446       }
447     }
448     return new AlignedCodon(codon[0], codon[1], codon[2]);
449   }
450
451   /**
452    * Weirdly, maybe worth a test to prove the helper method of this test class.
453    */
454   @Test(groups = { "Functional" })
455   public void testConvertCodon()
456   {
457     assertEquals("[0, 1, 2]", convertCodon("AAA").toString());
458     assertEquals("[0, 2, 5]", convertCodon("A-A--A").toString());
459     assertEquals("[1, 3, 4]", convertCodon("-A-AA-").toString());
460   }
461
462   /**
463    * Test dna complementing
464    */
465   @Test(groups = "Functional")
466   public void testGetComplement()
467   {
468     assertEquals('t', Dna.getComplement('a'));
469     assertEquals('T', Dna.getComplement('A'));
470     assertEquals('a', Dna.getComplement('t'));
471     assertEquals('A', Dna.getComplement('T'));
472     assertEquals('c', Dna.getComplement('g'));
473     assertEquals('C', Dna.getComplement('G'));
474     assertEquals('g', Dna.getComplement('c'));
475     assertEquals('G', Dna.getComplement('C'));
476     // note uU --> aA but not vice versa
477     assertEquals('a', Dna.getComplement('u'));
478     assertEquals('A', Dna.getComplement('U'));
479     // ambiguity codes, see http://www.bioinformatics.org/sms/iupac.html
480     assertEquals('r', Dna.getComplement('y'));
481     assertEquals('R', Dna.getComplement('Y'));
482     assertEquals('y', Dna.getComplement('r'));
483     assertEquals('Y', Dna.getComplement('R'));
484     assertEquals('k', Dna.getComplement('m'));
485     assertEquals('K', Dna.getComplement('M'));
486     assertEquals('m', Dna.getComplement('k'));
487     assertEquals('M', Dna.getComplement('K'));
488     assertEquals('b', Dna.getComplement('v'));
489     assertEquals('B', Dna.getComplement('V'));
490     assertEquals('v', Dna.getComplement('b'));
491     assertEquals('V', Dna.getComplement('B'));
492     assertEquals('d', Dna.getComplement('h'));
493     assertEquals('D', Dna.getComplement('H'));
494     assertEquals('h', Dna.getComplement('d'));
495     assertEquals('H', Dna.getComplement('D'));
496     assertEquals('Q', Dna.getComplement('Q'));
497   }
498
499   @Test(groups = "Functional")
500   public void testReverseSequence()
501   {
502     String seq = "-Ac-GtU--rYkMbVdHNX-";
503     String seqRev = new StringBuilder(seq).reverse().toString();
504
505     // reverse:
506     SequenceI reversed = Dna.reverseSequence("Seq1", seq, false);
507     assertEquals(1, reversed.getStart());
508     assertEquals(15, reversed.getEnd());
509     assertEquals(20, reversed.getLength());
510     assertEquals(seqRev, reversed.getSequenceAsString());
511     assertEquals("Seq1|rev", reversed.getName());
512
513     // reverse complement:
514     SequenceI revcomp = Dna.reverseSequence("Seq1", seq, true);
515     assertEquals("-XNDhBvKmRy--AaC-gT-", revcomp.getSequenceAsString());
516     assertEquals("Seq1|revcomp", revcomp.getName());
517   }
518
519   @Test(groups = "Functional")
520   public void testReverseCdna()
521   {
522     String seq = "-Ac-GtU--rYkMbVdHNX-";
523     String seqRev = new StringBuilder(seq).reverse().toString();
524     String seqDs = seq.replaceAll("-", "");
525     String seqDsRev = new StringBuilder(seqDs).reverse().toString();
526
527     SequenceI dna = new Sequence("Seq1", seq);
528     Alignment al = new Alignment(new SequenceI[] { dna });
529     al.createDatasetAlignment();
530     assertEquals(seqDs, al.getSequenceAt(0).getDatasetSequence()
531             .getSequenceAsString());
532
533     ColumnSelection cs = new ColumnSelection();
534     AlignViewportI av = new AlignViewport(al, cs);
535     Dna testee = new Dna(av, new int[] { 0, al.getWidth() - 1 });
536     AlignmentI reversed = testee.reverseCdna(false);
537     assertEquals(1, reversed.getHeight());
538     assertEquals(seqRev, reversed.getSequenceAt(0).getSequenceAsString());
539     assertEquals(seqDsRev, reversed.getSequenceAt(0).getDatasetSequence()
540             .getSequenceAsString());
541   }
542 }