900565616b88767d4161e5842468a11923e509c3
[jalview.git] / test / jalview / datamodel / AlignedCodonFrameTest.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.assertNull;
25
26 import jalview.util.MapList;
27
28 import java.util.Arrays;
29
30 import org.testng.annotations.Test;
31
32 public class AlignedCodonFrameTest
33 {
34
35   /**
36    * Test the method that locates the first aligned sequence that has a mapping.
37    */
38   @Test(groups = { "Functional" })
39   public void testFindAlignedSequence()
40   {
41     AlignmentI cdna = new Alignment(new SequenceI[] {});
42     final Sequence seq1 = new Sequence("Seq1", "C-G-TA-GC");
43     seq1.createDatasetSequence();
44     cdna.addSequence(seq1);
45     final Sequence seq2 = new Sequence("Seq2", "-TA-GG-GG");
46     seq2.createDatasetSequence();
47     cdna.addSequence(seq2);
48
49     AlignmentI aa = new Alignment(new SequenceI[] {});
50     final Sequence aseq1 = new Sequence("Seq1", "-P-R");
51     aseq1.createDatasetSequence();
52     aa.addSequence(aseq1);
53     final Sequence aseq2 = new Sequence("Seq2", "-LY-");
54     aseq2.createDatasetSequence();
55     aa.addSequence(aseq2);
56
57     /*
58      * Mapping from first DNA sequence to second AA sequence.
59      */
60     AlignedCodonFrame acf = new AlignedCodonFrame();
61
62     assertNull(acf.findAlignedSequence(seq1, aa));
63
64     MapList map = new MapList(new int[] { 1, 6 }, new int[] { 1, 2 }, 3, 1);
65     acf.addMap(seq1.getDatasetSequence(), aseq2.getDatasetSequence(), map);
66
67     /*
68      * DNA seq1 maps to AA seq2
69      */
70     assertEquals(aa.getSequenceAt(1), acf.findAlignedSequence(cdna
71             .getSequenceAt(0).getDatasetSequence(), aa));
72
73     assertEquals(cdna.getSequenceAt(0), acf.findAlignedSequence(aa
74             .getSequenceAt(1).getDatasetSequence(), cdna));
75   }
76
77   /**
78    * Test the method that locates the mapped codon for a protein position.
79    */
80   @Test(groups = { "Functional" })
81   public void testGetMappedRegion()
82   {
83     // introns lower case, exons upper case
84     final Sequence seq1 = new Sequence("Seq1", "c-G-TA-gC-gT-T");
85     seq1.createDatasetSequence();
86     final Sequence seq2 = new Sequence("Seq2", "-TA-gG-Gg-CG-a");
87     seq2.createDatasetSequence();
88
89     final Sequence aseq1 = new Sequence("Seq1", "-P-R");
90     aseq1.createDatasetSequence();
91     final Sequence aseq2 = new Sequence("Seq2", "-LY-");
92     aseq2.createDatasetSequence();
93
94     /*
95      * First with no mappings
96      */
97     AlignedCodonFrame acf = new AlignedCodonFrame();
98
99     assertNull(acf.getMappedRegion(seq1, aseq1, 1));
100
101     /*
102      * Set up the mappings for the exons (upper-case bases)
103      */
104     MapList map = new MapList(new int[] { 2, 4, 6, 6, 8, 9 }, new int[] {
105         1, 2 }, 3, 1);
106     acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
107     map = new MapList(new int[] { 1, 2, 4, 5, 7, 8 }, new int[] { 1, 2 },
108             3, 1);
109     acf.addMap(seq2.getDatasetSequence(), aseq2.getDatasetSequence(), map);
110
111     assertEquals("[2, 4]",
112             Arrays.toString(acf.getMappedRegion(seq1, aseq1, 1)));
113     assertEquals("[6, 6, 8, 9]",
114             Arrays.toString(acf.getMappedRegion(seq1, aseq1, 2)));
115     assertEquals("[1, 2, 4, 4]",
116             Arrays.toString(acf.getMappedRegion(seq2, aseq2, 1)));
117     assertEquals("[5, 5, 7, 8]",
118             Arrays.toString(acf.getMappedRegion(seq2, aseq2, 2)));
119
120     /*
121      * No mapping from sequence 1 to sequence 2
122      */
123     assertNull(acf.getMappedRegion(seq1, aseq2, 1));
124   }
125
126   @Test(groups = { "Functional" })
127   public void testGetMappedCodon()
128   {
129     final Sequence seq1 = new Sequence("Seq1", "c-G-TA-gC-gT-T");
130     seq1.createDatasetSequence();
131     final Sequence aseq1 = new Sequence("Seq1", "-P-R");
132     aseq1.createDatasetSequence();
133
134     /*
135      * First with no mappings
136      */
137     AlignedCodonFrame acf = new AlignedCodonFrame();
138
139     assertNull(acf.getMappedCodon(seq1.getDatasetSequence(), 0));
140
141     /*
142      * Set up the mappings for the exons (upper-case bases)
143      */
144     MapList map = new MapList(new int[] { 2, 4, 6, 6, 8, 9 }, new int[] {
145         1, 2 }, 3, 1);
146     acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
147
148     assertEquals("[G, T, A]", Arrays.toString(acf.getMappedCodon(
149             aseq1.getDatasetSequence(), 1)));
150     assertEquals("[C, T, T]", Arrays.toString(acf.getMappedCodon(
151             aseq1.getDatasetSequence(), 2)));
152   }
153 }