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