Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.analysis

File DnaTest.java

 

Code metrics

8
199
19
1
567
400
23
0.12
10.47
19
1.21

Classes

Class Line # Actions
DnaTest 46 199 23 0
1.0100%
 

Contributing tests

This file is covered by 12 tests. .

Source view

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  1 toggle @BeforeClass(alwaysRun = true)
49    public void setUpJvOptionPane()
50    {
51  1 JvOptionPane.setInteractiveMode(false);
52  1 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  1 toggle @Test(groups = { "Functional" })
131    public void testTranslateCdna_withUntranslatableCodons()
132    throws IOException
133    {
134  1 AlignmentI alf = new FormatAdapter().readFile(
135    JAL_1312_example_align_fasta, DataSourceType.PASTE,
136    FileFormat.Fasta);
137  1 HiddenColumns cs = new HiddenColumns();
138  1 AlignViewportI av = new AlignViewport(alf, cs);
139  1 Iterator<int[]> contigs = cs.getVisContigsIterator(0, alf.getWidth(),
140    false);
141  1 Dna dna = new Dna(av, contigs);
142  1 AlignmentI translated = dna.translateCdna();
143  1 assertNotNull("Couldn't do a full width translation of test data.",
144    translated);
145    }
146   
147    /**
148    * Test variant in which 15 column blocks at a time are translated (the rest
149    * hidden).
150    *
151    * @throws IOException
152    */
 
153  1 toggle @Test(groups = { "Functional" })
154    public void testTranslateCdna_withUntranslatableCodonsAndHiddenColumns()
155    throws IOException
156    {
157  1 AlignmentI alf = new FormatAdapter().readFile(
158    JAL_1312_example_align_fasta, DataSourceType.PASTE,
159    FileFormat.Fasta);
160  1 int vwidth = 15;
161  18 for (int ipos = 0; ipos + vwidth < alf.getWidth(); ipos += vwidth)
162    {
163  17 HiddenColumns cs = new HiddenColumns();
164  17 if (ipos > 0)
165    {
166  16 cs.hideColumns(0, ipos - 1);
167    }
168  17 cs.hideColumns(ipos + vwidth, alf.getWidth());
169  17 Iterator<int[]> vcontigs = cs.getVisContigsIterator(0,
170    alf.getWidth(), false);
171  17 AlignViewportI av = new AlignViewport(alf, cs);
172  17 Dna dna = new Dna(av, vcontigs);
173  17 AlignmentI transAlf = dna.translateCdna();
174   
175  17 assertTrue("Translation failed (ipos=" + ipos
176    + ") No alignment data.", transAlf != null);
177  17 assertTrue("Translation failed (ipos=" + ipos + ") Empty alignment.",
178    transAlf.getHeight() > 0);
179  17 assertTrue("Translation failed (ipos=" + ipos + ") Translated "
180    + transAlf.getHeight() + " sequences from " + alf.getHeight()
181    + " sequences", alf.getHeight() == transAlf.getHeight());
182    }
183    }
184   
185    /**
186    * Test simple translation to Amino Acids (with STOP codons translated to *).
187    *
188    * @throws IOException
189    */
 
190  1 toggle @Test(groups = { "Functional" })
191    public void testTranslateCdna_simple() throws IOException
192    {
193  1 AlignmentI alf = new FormatAdapter().readFile(fasta,
194    DataSourceType.PASTE, FileFormat.Fasta);
195  1 HiddenColumns cs = new HiddenColumns();
196  1 AlignViewportI av = new AlignViewport(alf, cs);
197  1 Iterator<int[]> contigs = cs.getVisContigsIterator(0, alf.getWidth(),
198    false);
199  1 Dna dna = new Dna(av, contigs);
200  1 AlignmentI translated = dna.translateCdna();
201  1 String aa = translated.getSequenceAt(0).getSequenceAsString();
202  1 assertEquals(
203    "AAAACCDDEEFFGGGGHHIIIKKLLLLLLMNNPPPPQQRRRRRRSSSSSSTTTTVVVVWYY***",
204    aa);
205    }
206   
207    /**
208    * Test translation excluding hidden columns.
209    *
210    * @throws IOException
211    */
 
212  1 toggle @Test(groups = { "Functional" })
213    public void testTranslateCdna_hiddenColumns() throws IOException
214    {
215  1 AlignmentI alf = new FormatAdapter().readFile(fasta,
216    DataSourceType.PASTE, FileFormat.Fasta);
217  1 HiddenColumns cs = new HiddenColumns();
218  1 cs.hideColumns(6, 14); // hide codons 3/4/5
219  1 cs.hideColumns(24, 35); // hide codons 9-12
220  1 cs.hideColumns(177, 191); // hide codons 60-64
221  1 AlignViewportI av = new AlignViewport(alf, cs);
222  1 Iterator<int[]> contigs = cs.getVisContigsIterator(0, alf.getWidth(),
223    false);
224  1 Dna dna = new Dna(av, contigs);
225  1 AlignmentI translated = dna.translateCdna();
226  1 String aa = translated.getSequenceAt(0).getSequenceAsString();
227  1 assertEquals("AACDDGGGGHHIIIKKLLLLLLMNNPPPPQQRRRRRRSSSSSSTTTTVVVVW", aa);
228    }
229   
230    /**
231    * Use this test to help debug into any cases of interest.
232    */
 
233  1 toggle @Test(groups = { "Functional" })
234    public void testCompareCodonPos_oneOnly()
235    {
236  1 assertFollows("-AA--A", "G--GG"); // 2 shifted seq2, 3 shifted seq1
237    }
238   
239    /**
240    * Tests for method that compares 'alignment' of two codon position triplets.
241    */
 
242  1 toggle @Test(groups = { "Functional" })
243    public void testCompareCodonPos()
244    {
245    /*
246    * Returns 0 for any null argument
247    */
248  1 assertEquals(0, Dna.compareCodonPos(new AlignedCodon(1, 2, 3), null));
249  1 assertEquals(0, Dna.compareCodonPos(null, new AlignedCodon(1, 2, 3)));
250   
251    /*
252    * Work through 27 combinations. First 9 cases where first position matches.
253    */
254  1 assertMatches("AAA", "GGG"); // 2 and 3 match
255  1 assertFollows("AA-A", "GGG"); // 2 matches, 3 shifted seq1
256  1 assertPrecedes("AAA", "GG-G"); // 2 matches, 3 shifted seq2
257  1 assertFollows("A-AA", "GG-G"); // 2 shifted seq1, 3 matches
258  1 assertFollows("A-A-A", "GG-G"); // 2 shifted seq1, 3 shifted seq1
259  1 assertPrecedes("A-AA", "GG--G"); // 2 shifted seq1, 3 shifted seq2
260  1 assertPrecedes("AA-A", "G-GG"); // 2 shifted seq2, 3 matches
261  1 assertFollows("AA--A", "G-GG"); // 2 shifted seq2, 3 shifted seq1
262  1 assertPrecedes("AAA", "G-GG"); // 2 shifted seq2, 3 shifted seq2
263   
264    /*
265    * 9 cases where first position is shifted in first sequence.
266    */
267  1 assertFollows("-AAA", "G-GG"); // 2 and 3 match
268  1 assertFollows("-AA-A", "G-GG"); // 2 matches, 3 shifted seq1
269    // 'enclosing' case: pick first to start precedes
270  1 assertFollows("-AAA", "G-G-G"); // 2 matches, 3 shifted seq2
271  1 assertFollows("-A-AA", "G-G-G"); // 2 shifted seq1, 3 matches
272  1 assertFollows("-A-A-A", "G-G-G"); // 2 shifted seq1, 3 shifted seq1
273    // 'enclosing' case: pick first to start precedes
274  1 assertFollows("-A-AA", "G-G--G"); // 2 shifted seq1, 3 shifted seq2
275  1 assertFollows("-AA-A", "G--GG"); // 2 shifted seq2, 3 matches
276  1 assertFollows("-AA--A", "G--GG"); // 2 shifted seq2, 3 shifted seq1
277  1 assertPrecedes("-AAA", "G--GG"); // 2 shifted seq2, 3 shifted seq2
278   
279    /*
280    * 9 cases where first position is shifted in second sequence.
281    */
282  1 assertPrecedes("A-AA", "-GGG"); // 2 and 3 match
283  1 assertPrecedes("A-A-A", "-GGG"); // 2 matches, 3 shifted seq1
284  1 assertPrecedes("A-AA", "-GG-G"); // 2 matches, 3 shifted seq2
285  1 assertPrecedes("A--AA", "-GG-G"); // 2 shifted seq1, 3 matches
286    // 'enclosing' case with middle base deciding:
287  1 assertFollows("A--AA", "-GGG"); // 2 shifted seq1, 3 shifted seq1
288  1 assertPrecedes("A--AA", "-GG--G"); // 2 shifted seq1, 3 shifted seq2
289  1 assertPrecedes("AA-A", "-GGG"); // 2 shifted seq2, 3 matches
290  1 assertPrecedes("AA--A", "-GGG"); // 2 shifted seq2, 3 shifted seq1
291  1 assertPrecedes("AAA", "-GGG"); // 2 shifted seq2, 3 shifted seq2
292    }
293   
294    /**
295    * This test generates a random cDNA alignment and its translation, then
296    * reorders the cDNA and retranslates, and verifies that the translations are
297    * the same (apart from ordering).
298    */
 
299  1 toggle @Test(groups = { "Functional" })
300    public void testTranslateCdna_sequenceOrderIndependent()
301    {
302    /*
303    * Generate cDNA - 8 sequences of 12 bases each.
304    */
305  1 AlignmentI cdna = new AlignmentGenerator(true)
306    .generate(12, 8, 97, 5, 5);
307  1 HiddenColumns cs = new HiddenColumns();
308  1 AlignViewportI av = new AlignViewport(cdna, cs);
309  1 Iterator<int[]> contigs = cs.getVisContigsIterator(0, cdna.getWidth(),
310    false);
311  1 Dna dna = new Dna(av, contigs);
312  1 AlignmentI translated = dna.translateCdna();
313   
314    /*
315    * Jumble the cDNA sequences and translate.
316    */
317  1 SequenceI[] sorted = new SequenceI[cdna.getHeight()];
318  1 final int[] jumbler = new int[] { 6, 7, 3, 4, 2, 0, 1, 5 };
319  1 int seqNo = 0;
320  1 for (int i : jumbler)
321    {
322  8 sorted[seqNo++] = cdna.getSequenceAt(i);
323    }
324  1 AlignmentI cdnaReordered = new Alignment(sorted);
325  1 av = new AlignViewport(cdnaReordered, cs);
326  1 contigs = cs.getVisContigsIterator(0, cdna.getWidth(), false);
327  1 dna = new Dna(av, contigs);
328  1 AlignmentI translated2 = dna.translateCdna();
329   
330    /*
331    * Check translated sequences are the same in both alignments.
332    */
333  1 System.out.println("Original");
334  1 System.out.println(translated.toString());
335  1 System.out.println("Sorted");
336  1 System.out.println(translated2.toString());
337   
338  1 int sortedSequenceIndex = 0;
339  1 for (int originalSequenceIndex : jumbler)
340    {
341  8 final String translation1 = translated.getSequenceAt(
342    originalSequenceIndex).getSequenceAsString();
343  8 final String translation2 = translated2.getSequenceAt(
344    sortedSequenceIndex).getSequenceAsString();
345  8 assertEquals(translation2, translation1);
346  8 sortedSequenceIndex++;
347    }
348    }
349   
350    /**
351    * Test that all the cases in testCompareCodonPos have a 'symmetric'
352    * comparison (without checking the actual comparison result).
353    */
 
354  1 toggle @Test(groups = { "Functional" })
355    public void testCompareCodonPos_isSymmetric()
356    {
357  1 assertSymmetric("AAA", "GGG");
358  1 assertSymmetric("AA-A", "GGG");
359  1 assertSymmetric("AAA", "GG-G");
360  1 assertSymmetric("A-AA", "GG-G");
361  1 assertSymmetric("A-A-A", "GG-G");
362  1 assertSymmetric("A-AA", "GG--G");
363  1 assertSymmetric("AA-A", "G-GG");
364  1 assertSymmetric("AA--A", "G-GG");
365  1 assertSymmetric("AAA", "G-GG");
366  1 assertSymmetric("-AAA", "G-GG");
367  1 assertSymmetric("-AA-A", "G-GG");
368  1 assertSymmetric("-AAA", "G-G-G");
369  1 assertSymmetric("-A-AA", "G-G-G");
370  1 assertSymmetric("-A-A-A", "G-G-G");
371  1 assertSymmetric("-A-AA", "G-G--G");
372  1 assertSymmetric("-AA-A", "G--GG");
373  1 assertSymmetric("-AA--A", "G--GG");
374  1 assertSymmetric("-AAA", "G--GG");
375  1 assertSymmetric("A-AA", "-GGG");
376  1 assertSymmetric("A-A-A", "-GGG");
377  1 assertSymmetric("A-AA", "-GG-G");
378  1 assertSymmetric("A--AA", "-GG-G");
379  1 assertSymmetric("A--AA", "-GGG");
380  1 assertSymmetric("A--AA", "-GG--G");
381  1 assertSymmetric("AA-A", "-GGG");
382  1 assertSymmetric("AA--A", "-GGG");
383  1 assertSymmetric("AAA", "-GGG");
384    }
385   
 
386  27 toggle private void assertSymmetric(String codon1, String codon2)
387    {
388  27 assertEquals("Comparison of '" + codon1 + "' and '" + codon2
389    + " not symmetric", Integer.signum(compare(codon1, codon2)),
390    -Integer.signum(compare(codon2, codon1)));
391    }
392   
393    /**
394    * Assert that the first sequence should map to the same position as the
395    * second in a translated alignment. Also checks that this is true if the
396    * order of the codons is reversed.
397    *
398    * @param codon1
399    * @param codon2
400    */
 
401  1 toggle private void assertMatches(String codon1, String codon2)
402    {
403  1 assertEquals("Expected '" + codon1 + "' matches '" + codon2 + "'", 0,
404    compare(codon1, codon2));
405  1 assertEquals("Expected '" + codon2 + "' matches '" + codon1 + "'", 0,
406    compare(codon2, codon1));
407    }
408   
409    /**
410    * Assert that the first sequence should precede the second in a translated
411    * alignment
412    *
413    * @param codon1
414    * @param codon2
415    */
 
416  13 toggle private void assertPrecedes(String codon1, String codon2)
417    {
418  13 assertEquals("Expected '" + codon1 + "' precedes '" + codon2 + "'",
419    -1, compare(codon1, codon2));
420    }
421   
422    /**
423    * Assert that the first sequence should follow the second in a translated
424    * alignment
425    *
426    * @param codon1
427    * @param codon2
428    */
 
429  14 toggle private void assertFollows(String codon1, String codon2)
430    {
431  14 assertEquals("Expected '" + codon1 + "' follows '" + codon2 + "'", 1,
432    compare(codon1, codon2));
433    }
434   
435    /**
436    * Convert two nucleotide strings to base positions and pass to
437    * Dna.compareCodonPos, return the result.
438    *
439    * @param s1
440    * @param s2
441    * @return
442    */
 
443  83 toggle private int compare(String s1, String s2)
444    {
445  83 final AlignedCodon cd1 = convertCodon(s1);
446  83 final AlignedCodon cd2 = convertCodon(s2);
447  83 System.out.println("K: " + s1 + " " + cd1.toString());
448  83 System.out.println("G: " + s2 + " " + cd2.toString());
449  83 System.out.println();
450  83 return Dna.compareCodonPos(cd1, cd2);
451    }
452   
453    /**
454    * Convert a string e.g. "-GC-T" to base positions e.g. [1, 2, 4]. The string
455    * should have exactly 3 non-gap characters, and use '-' for gaps.
456    *
457    * @param s
458    * @return
459    */
 
460  169 toggle private AlignedCodon convertCodon(String s)
461    {
462  169 int[] codon = new int[3];
463  169 int i = 0;
464  915 for (int j = 0; j < s.length(); j++)
465    {
466  746 if (s.charAt(j) != '-')
467    {
468  507 codon[i++] = j;
469    }
470    }
471  169 return new AlignedCodon(codon[0], codon[1], codon[2]);
472    }
473   
474    /**
475    * Weirdly, maybe worth a test to prove the helper method of this test class.
476    */
 
477  1 toggle @Test(groups = { "Functional" })
478    public void testConvertCodon()
479    {
480  1 assertEquals("[0, 1, 2]", convertCodon("AAA").toString());
481  1 assertEquals("[0, 2, 5]", convertCodon("A-A--A").toString());
482  1 assertEquals("[1, 3, 4]", convertCodon("-A-AA-").toString());
483    }
484   
485    /**
486    * Test dna complementing
487    */
 
488  1 toggle @Test(groups = "Functional")
489    public void testGetComplement()
490    {
491  1 assertEquals('t', Dna.getComplement('a'));
492  1 assertEquals('T', Dna.getComplement('A'));
493  1 assertEquals('a', Dna.getComplement('t'));
494  1 assertEquals('A', Dna.getComplement('T'));
495  1 assertEquals('c', Dna.getComplement('g'));
496  1 assertEquals('C', Dna.getComplement('G'));
497  1 assertEquals('g', Dna.getComplement('c'));
498  1 assertEquals('G', Dna.getComplement('C'));
499    // note uU --> aA but not vice versa
500  1 assertEquals('a', Dna.getComplement('u'));
501  1 assertEquals('A', Dna.getComplement('U'));
502    // ambiguity codes, see http://www.bioinformatics.org/sms/iupac.html
503  1 assertEquals('r', Dna.getComplement('y'));
504  1 assertEquals('R', Dna.getComplement('Y'));
505  1 assertEquals('y', Dna.getComplement('r'));
506  1 assertEquals('Y', Dna.getComplement('R'));
507  1 assertEquals('k', Dna.getComplement('m'));
508  1 assertEquals('K', Dna.getComplement('M'));
509  1 assertEquals('m', Dna.getComplement('k'));
510  1 assertEquals('M', Dna.getComplement('K'));
511  1 assertEquals('b', Dna.getComplement('v'));
512  1 assertEquals('B', Dna.getComplement('V'));
513  1 assertEquals('v', Dna.getComplement('b'));
514  1 assertEquals('V', Dna.getComplement('B'));
515  1 assertEquals('d', Dna.getComplement('h'));
516  1 assertEquals('D', Dna.getComplement('H'));
517  1 assertEquals('h', Dna.getComplement('d'));
518  1 assertEquals('H', Dna.getComplement('D'));
519  1 assertEquals('Q', Dna.getComplement('Q'));
520    }
521   
 
522  1 toggle @Test(groups = "Functional")
523    public void testReverseSequence()
524    {
525  1 String seq = "-Ac-GtU--rYkMbVdHNX-";
526  1 String seqRev = new StringBuilder(seq).reverse().toString();
527   
528    // reverse:
529  1 SequenceI reversed = Dna.reverseSequence("Seq1", seq, false);
530  1 assertEquals(1, reversed.getStart());
531  1 assertEquals(15, reversed.getEnd());
532  1 assertEquals(20, reversed.getLength());
533  1 assertEquals(seqRev, reversed.getSequenceAsString());
534  1 assertEquals("Seq1|rev", reversed.getName());
535   
536    // reverse complement:
537  1 SequenceI revcomp = Dna.reverseSequence("Seq1", seq, true);
538  1 assertEquals("-XNDhBvKmRy--AaC-gT-", revcomp.getSequenceAsString());
539  1 assertEquals("Seq1|revcomp", revcomp.getName());
540    }
541   
 
542  1 toggle @Test(groups = "Functional")
543    public void testReverseCdna()
544    {
545  1 String seq = "-Ac-GtU--rYkMbVdHNX-";
546  1 String seqRev = new StringBuilder(seq).reverse().toString();
547  1 String seqDs = seq.replaceAll("-", "");
548  1 String seqDsRev = new StringBuilder(seqDs).reverse().toString();
549   
550  1 SequenceI dna = new Sequence("Seq1", seq);
551  1 Alignment al = new Alignment(new SequenceI[] { dna });
552  1 al.createDatasetAlignment();
553  1 assertEquals(seqDs, al.getSequenceAt(0).getDatasetSequence()
554    .getSequenceAsString());
555   
556  1 HiddenColumns cs = new HiddenColumns();
557  1 AlignViewportI av = new AlignViewport(al, cs);
558  1 Iterator<int[]> contigs = cs.getVisContigsIterator(0, al.getWidth(),
559    false);
560  1 Dna testee = new Dna(av, contigs);
561  1 AlignmentI reversed = testee.reverseCdna(false);
562  1 assertEquals(1, reversed.getHeight());
563  1 assertEquals(seqRev, reversed.getSequenceAt(0).getSequenceAsString());
564  1 assertEquals(seqDsRev, reversed.getSequenceAt(0).getDatasetSequence()
565    .getSequenceAsString());
566    }
567    }