abb06ad4e660a882ec1910d123ccedec339c867f
[jalview.git] / test / jalview / datamodel / AlignedCodonIteratorTest.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.datamodel;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25
26 import jalview.util.MapList;
27
28 import java.util.Iterator;
29
30 import org.testng.Assert;
31 import org.testng.annotations.Test;
32
33 /**
34  * Unit tests for Mapping$AlignedCodonIterator
35  * 
36  * @author gmcarstairs
37  *
38  */
39 public class AlignedCodonIteratorTest
40 {
41   /**
42    * Test normal case for iterating over aligned codons.
43    */
44   @Test(groups = { "Functional" })
45   public void testNext()
46   {
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);
55
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());
67   }
68
69   /**
70    * Test weird case where the mapping skips over a peptide.
71    */
72   @Test(groups = { "Functional" })
73   public void testNext_unmappedPeptide()
74   {
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,
81                 2, 4, 4 }, 3, 1);
82     Mapping m = new Mapping(to.getDatasetSequence(), map);
83
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());
95   }
96
97   /**
98    * Test for exception thrown for an incomplete codon.
99    */
100   @Test(groups = { "Functional" })
101   public void testNext_incompleteCodon()
102   {
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);
110
111     Iterator<AlignedCodon> codons = m.getCodonIterator(from, '-');
112     AlignedCodon codon = codons.next();
113     assertEquals("[1, 3, 5]", codon.toString());
114     assertEquals("P", codon.product);
115     try
116     {
117       codon = codons.next();
118       Assert.fail("expected exception");
119     } catch (IncompleteCodonException e)
120     {
121       // expected
122     }
123   }
124
125   /**
126    * Test normal case for iterating over aligned codons.
127    */
128   @Test(groups = { "Functional" })
129   public void testAnother()
130   {
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);
137
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());
152   }
153 }