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