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