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.util.MapList;
28 import java.util.Iterator;
30 import org.testng.Assert;
31 import org.testng.annotations.Test;
34 * Unit tests for Mapping$AlignedCodonIterator
39 public class AlignedCodonIteratorTest
42 * Test normal case for iterating over aligned codons.
44 @Test(groups = { "Functional" })
45 public void testNext()
47 SequenceI from = new Sequence("Seq1", "-CgC-C-cCtAG-AtG-Gc");
48 from.createDatasetSequence();
49 SequenceI to = new Sequence("Seq1", "-PQ-R-");
50 to.createDatasetSequence();
51 MapList map = new MapList(
52 new int[] { 1, 1, 3, 4, 6, 6, 8, 10, 12, 13 },
53 new int[] { 1, 3 }, 3, 1);
54 Mapping m = new Mapping(to.getDatasetSequence(), map);
56 Iterator<AlignedCodon> codons = m.getCodonIterator(from, '-');
57 AlignedCodon codon = codons.next();
58 assertEquals("[1, 3, 5]", codon.toString());
59 assertEquals("P", codon.product);
60 codon = codons.next();
61 assertEquals("[8, 10, 11]", codon.toString());
62 assertEquals("Q", codon.product);
63 codon = codons.next();
64 assertEquals("[13, 15, 17]", codon.toString());
65 assertEquals("R", codon.product);
66 assertFalse(codons.hasNext());
70 * Test weird case where the mapping skips over a peptide.
72 @Test(groups = { "Functional" })
73 public void testNext_unmappedPeptide()
75 SequenceI from = new Sequence("Seq1", "-CgC-C-cCtAG-AtG-Gc");
76 from.createDatasetSequence();
77 SequenceI to = new Sequence("Seq1", "-PQ-TR-");
78 to.createDatasetSequence();
79 MapList map = new MapList(
80 new int[] { 1, 1, 3, 4, 6, 6, 8, 10, 12, 13 }, new int[] { 1,
82 Mapping m = new Mapping(to.getDatasetSequence(), map);
84 Iterator<AlignedCodon> codons = m.getCodonIterator(from, '-');
85 AlignedCodon codon = codons.next();
86 assertEquals("[1, 3, 5]", codon.toString());
87 assertEquals("P", codon.product);
88 codon = codons.next();
89 assertEquals("[8, 10, 11]", codon.toString());
90 assertEquals("Q", codon.product);
91 codon = codons.next();
92 assertEquals("[13, 15, 17]", codon.toString());
93 assertEquals("R", codon.product);
94 assertFalse(codons.hasNext());
98 * Test for exception thrown for an incomplete codon.
100 @Test(groups = { "Functional" })
101 public void testNext_incompleteCodon()
103 SequenceI from = new Sequence("Seq1", "-CgC-C-cCgTt");
104 from.createDatasetSequence();
105 SequenceI to = new Sequence("Seq1", "-PQ-R-");
106 to.createDatasetSequence();
107 MapList map = new MapList(new int[] { 1, 1, 3, 4, 6, 6, 8, 8 },
108 new int[] { 1, 3 }, 3, 1);
109 Mapping m = new Mapping(to.getDatasetSequence(), map);
111 Iterator<AlignedCodon> codons = m.getCodonIterator(from, '-');
112 AlignedCodon codon = codons.next();
113 assertEquals("[1, 3, 5]", codon.toString());
114 assertEquals("P", codon.product);
117 codon = codons.next();
118 Assert.fail("expected exception");
119 } catch (IncompleteCodonException e)
126 * Test normal case for iterating over aligned codons.
128 @Test(groups = { "Functional" })
129 public void testAnother()
131 SequenceI from = new Sequence("Seq1", "TGCCATTACCAG-");
132 from.createDatasetSequence();
133 SequenceI to = new Sequence("Seq1", "CHYQ");
134 to.createDatasetSequence();
135 MapList map = new MapList(new int[] { 1, 12 }, new int[] { 1, 4 }, 3, 1);
136 Mapping m = new Mapping(to.getDatasetSequence(), map);
138 Iterator<AlignedCodon> codons = m.getCodonIterator(from, '-');
139 AlignedCodon codon = codons.next();
140 assertEquals("[0, 1, 2]", codon.toString());
141 assertEquals("C", codon.product);
142 codon = codons.next();
143 assertEquals("[3, 4, 5]", codon.toString());
144 assertEquals("H", codon.product);
145 codon = codons.next();
146 assertEquals("[6, 7, 8]", codon.toString());
147 assertEquals("Y", codon.product);
148 codon = codons.next();
149 assertEquals("[9, 10, 11]", codon.toString());
150 assertEquals("Q", codon.product);
151 assertFalse(codons.hasNext());
155 * Test for a case with sequence (and mappings) not starting at 1
157 @Test(groups = { "Functional" })
158 public void testNext_withOffset()
160 SequenceI from = new Sequence("Seq1", "-CgC-C-cCtAG-AtG-Gc", 7, 20);
161 from.createDatasetSequence();
162 SequenceI to = new Sequence("Seq1/10-12", "-PQ-R-");
163 to.createDatasetSequence();
164 MapList map = new MapList(new int[] { 7, 7, 9, 10, 12, 12, 14, 16, 18,
165 19 }, new int[] { 10, 12 }, 3, 1);
166 Mapping m = new Mapping(to.getDatasetSequence(), map);
168 Iterator<AlignedCodon> codons = m.getCodonIterator(from, '-');
169 AlignedCodon codon = codons.next();
170 assertEquals("[1, 3, 5]", codon.toString());
171 assertEquals("P", codon.product);
172 codon = codons.next();
173 assertEquals("[8, 10, 11]", codon.toString());
174 assertEquals("Q", codon.product);
175 codon = codons.next();
176 assertEquals("[13, 15, 17]", codon.toString());
177 assertEquals("R", codon.product);
178 assertFalse(codons.hasNext());