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