2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.datamodel;
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
26 import jalview.gui.JvOptionPane;
27 import jalview.util.MapList;
29 import java.util.Iterator;
31 import org.testng.Assert;
32 import org.testng.annotations.BeforeClass;
33 import org.testng.annotations.Test;
36 * Unit tests for Mapping$AlignedCodonIterator
41 public class AlignedCodonIteratorTest
44 @BeforeClass(alwaysRun = true)
45 public void setUpJvOptionPane()
47 JvOptionPane.setInteractiveMode(false);
48 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
52 * Test normal case for iterating over aligned codons.
54 @Test(groups = { "Functional" })
55 public void testNext()
57 SequenceI from = new Sequence("Seq1", "-CgC-C-cCtAG-AtG-Gc");
58 from.createDatasetSequence();
59 SequenceI to = new Sequence("Seq1", "-PQ-R-");
60 to.createDatasetSequence();
61 MapList map = new MapList(
62 new int[] { 1, 1, 3, 4, 6, 6, 8, 10, 12, 13 },
63 new int[] { 1, 3 }, 3, 1);
64 Mapping m = new Mapping(to.getDatasetSequence(), map);
66 Iterator<AlignedCodon> codons = m.getCodonIterator(from, '-');
67 AlignedCodon codon = codons.next();
68 assertEquals("[1, 3, 5]", codon.toString());
69 assertEquals("P", codon.product);
70 codon = codons.next();
71 assertEquals("[8, 10, 11]", codon.toString());
72 assertEquals("Q", codon.product);
73 codon = codons.next();
74 assertEquals("[13, 15, 17]", codon.toString());
75 assertEquals("R", codon.product);
76 assertFalse(codons.hasNext());
80 * Test weird case where the mapping skips over a peptide.
82 @Test(groups = { "Functional" })
83 public void testNext_unmappedPeptide()
85 SequenceI from = new Sequence("Seq1", "-CgC-C-cCtAG-AtG-Gc");
86 from.createDatasetSequence();
87 SequenceI to = new Sequence("Seq1", "-PQ-TR-");
88 to.createDatasetSequence();
89 MapList map = new MapList(
90 new int[] { 1, 1, 3, 4, 6, 6, 8, 10, 12, 13 }, new int[] { 1,
92 Mapping m = new Mapping(to.getDatasetSequence(), map);
94 Iterator<AlignedCodon> codons = m.getCodonIterator(from, '-');
95 AlignedCodon codon = codons.next();
96 assertEquals("[1, 3, 5]", codon.toString());
97 assertEquals("P", codon.product);
98 codon = codons.next();
99 assertEquals("[8, 10, 11]", codon.toString());
100 assertEquals("Q", codon.product);
101 codon = codons.next();
102 assertEquals("[13, 15, 17]", codon.toString());
103 assertEquals("R", codon.product);
104 assertFalse(codons.hasNext());
108 * Test for exception thrown for an incomplete codon.
110 @Test(groups = { "Functional" })
111 public void testNext_incompleteCodon()
113 SequenceI from = new Sequence("Seq1", "-CgC-C-cCgTt");
114 from.createDatasetSequence();
115 SequenceI to = new Sequence("Seq1", "-PQ-R-");
116 to.createDatasetSequence();
117 MapList map = new MapList(new int[] { 1, 1, 3, 4, 6, 6, 8, 8 },
118 new int[] { 1, 3 }, 3, 1);
119 Mapping m = new Mapping(to.getDatasetSequence(), map);
121 Iterator<AlignedCodon> codons = m.getCodonIterator(from, '-');
122 AlignedCodon codon = codons.next();
123 assertEquals("[1, 3, 5]", codon.toString());
124 assertEquals("P", codon.product);
127 codon = codons.next();
128 Assert.fail("expected exception");
129 } catch (IncompleteCodonException e)
136 * Test normal case for iterating over aligned codons.
138 @Test(groups = { "Functional" })
139 public void testAnother()
141 SequenceI from = new Sequence("Seq1", "TGCCATTACCAG-");
142 from.createDatasetSequence();
143 SequenceI to = new Sequence("Seq1", "CHYQ");
144 to.createDatasetSequence();
145 MapList map = new MapList(new int[] { 1, 12 }, new int[] { 1, 4 }, 3, 1);
146 Mapping m = new Mapping(to.getDatasetSequence(), map);
148 Iterator<AlignedCodon> codons = m.getCodonIterator(from, '-');
149 AlignedCodon codon = codons.next();
150 assertEquals("[0, 1, 2]", codon.toString());
151 assertEquals("C", codon.product);
152 codon = codons.next();
153 assertEquals("[3, 4, 5]", codon.toString());
154 assertEquals("H", codon.product);
155 codon = codons.next();
156 assertEquals("[6, 7, 8]", codon.toString());
157 assertEquals("Y", codon.product);
158 codon = codons.next();
159 assertEquals("[9, 10, 11]", codon.toString());
160 assertEquals("Q", codon.product);
161 assertFalse(codons.hasNext());
165 * Test for a case with sequence (and mappings) not starting at 1
167 @Test(groups = { "Functional" })
168 public void testNext_withOffset()
170 SequenceI from = new Sequence("Seq1", "-CgC-C-cCtAG-AtG-Gc", 7, 20);
171 from.createDatasetSequence();
172 SequenceI to = new Sequence("Seq1/10-12", "-PQ-R-");
173 to.createDatasetSequence();
174 MapList map = new MapList(new int[] { 7, 7, 9, 10, 12, 12, 14, 16, 18,
175 19 }, new int[] { 10, 12 }, 3, 1);
176 Mapping m = new Mapping(to.getDatasetSequence(), map);
178 Iterator<AlignedCodon> codons = m.getCodonIterator(from, '-');
179 AlignedCodon codon = codons.next();
180 assertEquals("[1, 3, 5]", codon.toString());
181 assertEquals("P", codon.product);
182 codon = codons.next();
183 assertEquals("[8, 10, 11]", codon.toString());
184 assertEquals("Q", codon.product);
185 codon = codons.next();
186 assertEquals("[13, 15, 17]", codon.toString());
187 assertEquals("R", codon.product);
188 assertFalse(codons.hasNext());