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