fb4073a0713e52f6a21c73b13142f623b9a3ab15
[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.assertFalse;
25 import static org.testng.AssertJUnit.assertNull;
26 import static org.testng.AssertJUnit.assertSame;
27 import static org.testng.AssertJUnit.assertTrue;
28 import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;
29
30 import jalview.gui.JvOptionPane;
31 import jalview.util.MapList;
32
33 import java.util.Arrays;
34 import java.util.List;
35
36 import org.testng.annotations.BeforeClass;
37 import org.testng.annotations.Test;
38
39 public class AlignedCodonFrameTest
40 {
41
42   @BeforeClass(alwaysRun = true)
43   public void setUpJvOptionPane()
44   {
45     JvOptionPane.setInteractiveMode(false);
46     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
47   }
48
49   /**
50    * Test the method that locates the first aligned sequence that has a mapping.
51    */
52   @Test(groups = { "Functional" })
53   public void testFindAlignedSequence()
54   {
55     AlignmentI cdna = new Alignment(new SequenceI[] {});
56     final Sequence seq1 = new Sequence("Seq1", "C-G-TA-GC");
57     seq1.createDatasetSequence();
58     cdna.addSequence(seq1);
59     final Sequence seq2 = new Sequence("Seq2", "-TA-GG-GG");
60     seq2.createDatasetSequence();
61     cdna.addSequence(seq2);
62
63     AlignmentI aa = new Alignment(new SequenceI[] {});
64     final Sequence aseq1 = new Sequence("Seq1", "-P-R");
65     aseq1.createDatasetSequence();
66     aa.addSequence(aseq1);
67     final Sequence aseq2 = new Sequence("Seq2", "-LY-");
68     aseq2.createDatasetSequence();
69     aa.addSequence(aseq2);
70
71     /*
72      * Mapping from first DNA sequence to second AA sequence.
73      */
74     AlignedCodonFrame acf = new AlignedCodonFrame();
75
76     assertNull(acf.findAlignedSequence(seq1, aa));
77
78     MapList map = new MapList(new int[] { 1, 6 }, new int[] { 1, 2 }, 3, 1);
79     acf.addMap(seq1.getDatasetSequence(), aseq2.getDatasetSequence(), map);
80
81     /*
82      * DNA seq1 maps to AA seq2
83      */
84     assertEquals(aa.getSequenceAt(1), acf.findAlignedSequence(cdna
85             .getSequenceAt(0).getDatasetSequence(), aa));
86     // can also find this from the dna aligned sequence
87     assertEquals(aa.getSequenceAt(1),
88             acf.findAlignedSequence(cdna.getSequenceAt(0), aa));
89
90     assertEquals(cdna.getSequenceAt(0), acf.findAlignedSequence(aa
91             .getSequenceAt(1).getDatasetSequence(), cdna));
92   }
93
94   /**
95    * Test the method that locates the mapped codon for a protein position.
96    */
97   @Test(groups = { "Functional" })
98   public void testGetMappedRegion()
99   {
100     // introns lower case, exons upper case
101     final Sequence seq1 = new Sequence("Seq1", "c-G-TA-gC-gT-T");
102     seq1.createDatasetSequence();
103     final Sequence seq2 = new Sequence("Seq2", "-TA-gG-Gg-CG-a");
104     seq2.createDatasetSequence();
105
106     final Sequence aseq1 = new Sequence("Seq1", "-P-R");
107     aseq1.createDatasetSequence();
108     final Sequence aseq2 = new Sequence("Seq2", "-LY-Q");
109     aseq2.createDatasetSequence();
110
111     /*
112      * First with no mappings
113      */
114     AlignedCodonFrame acf = new AlignedCodonFrame();
115
116     assertNull(acf.getMappedRegion(seq1, aseq1, 1));
117
118     /*
119      * Set up the mappings for the exons (upper-case bases)
120      * Note residue Q is unmapped
121      */
122     MapList map = new MapList(new int[] { 2, 4, 6, 6, 8, 9 }, new int[] {
123         1, 2 }, 3, 1);
124     acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
125     map = new MapList(new int[] { 1, 2, 4, 5, 7, 8 }, new int[] { 1, 2 },
126             3, 1);
127     acf.addMap(seq2.getDatasetSequence(), aseq2.getDatasetSequence(), map);
128
129     assertArrayEquals(new int[] { 2, 4 },
130             acf.getMappedRegion(seq1, aseq1, 1));
131     assertArrayEquals(new int[] { 6, 6, 8, 9 },
132             acf.getMappedRegion(seq1, aseq1, 2));
133     assertArrayEquals(new int[] { 1, 2, 4, 4 },
134             acf.getMappedRegion(seq2, aseq2, 1));
135     assertArrayEquals(new int[] { 5, 5, 7, 8 },
136             acf.getMappedRegion(seq2, aseq2, 2));
137
138     /*
139      * No mapping from seq2 to Q
140      */
141     assertNull(acf.getMappedRegion(seq2, aseq2, 3));
142
143     /*
144      * No mapping from sequence 1 to sequence 2
145      */
146     assertNull(acf.getMappedRegion(seq1, aseq2, 1));
147   }
148
149   @Test(groups = { "Functional" })
150   public void testGetMappedCodons()
151   {
152     final Sequence seq1 = new Sequence("Seq1", "c-G-TA-gC-gT-T");
153     seq1.createDatasetSequence();
154     final Sequence aseq1 = new Sequence("Seq1", "-V-L");
155     aseq1.createDatasetSequence();
156
157     /*
158      * First with no mappings
159      */
160     AlignedCodonFrame acf = new AlignedCodonFrame();
161
162     assertNull(acf.getMappedCodons(seq1.getDatasetSequence(), 0));
163
164     /*
165      * Set up the mappings for the exons (upper-case bases)
166      */
167     MapList map = new MapList(new int[] { 2, 4, 6, 6, 8, 9 }, new int[] {
168         1, 2 }, 3, 1);
169     acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
170
171     assertEquals(1, acf.getMappedCodons(aseq1.getDatasetSequence(), 1)
172             .size());
173     assertEquals(
174             "[G, T, A]",
175             Arrays.toString(acf.getMappedCodons(aseq1.getDatasetSequence(),
176                     1).get(0)));
177     assertEquals(
178             "[C, T, T]",
179             Arrays.toString(acf.getMappedCodons(aseq1.getDatasetSequence(),
180                     2).get(0)));
181   }
182
183   /**
184    * Test for the case where there is more than one variant of the DNA mapping
185    * to a protein sequence
186    */
187   @Test(groups = { "Functional" })
188   public void testGetMappedCodons_dnaVariants()
189   {
190     final Sequence seq1 = new Sequence("Seq1", "c-G-TA-gC-gT-T");
191     seq1.createDatasetSequence();
192     final Sequence seq2 = new Sequence("Seq2", "c-G-TT-gT-gT-A");
193     seq2.createDatasetSequence();
194     final Sequence aseq1 = new Sequence("Seq1", "-V-L");
195     aseq1.createDatasetSequence();
196
197     AlignedCodonFrame acf = new AlignedCodonFrame();
198
199     /*
200      * Set up the mappings for the exons (upper-case bases)
201      */
202     MapList map = new MapList(new int[] { 2, 4, 6, 6, 8, 9 }, new int[] {
203         1, 2 }, 3, 1);
204     acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
205     acf.addMap(seq2.getDatasetSequence(), aseq1.getDatasetSequence(), map);
206
207     assertEquals(2, acf.getMappedCodons(aseq1.getDatasetSequence(), 1)
208             .size());
209     List<char[]> codonsForV = acf.getMappedCodons(
210             aseq1.getDatasetSequence(), 1);
211     assertEquals("[G, T, A]", Arrays.toString(codonsForV.get(0)));
212     assertEquals("[G, T, T]", Arrays.toString(codonsForV.get(1)));
213     List<char[]> codonsForL = acf.getMappedCodons(
214             aseq1.getDatasetSequence(), 2);
215     assertEquals("[C, T, T]", Arrays.toString(codonsForL.get(0)));
216     assertEquals("[T, T, A]", Arrays.toString(codonsForL.get(1)));
217   }
218
219   /**
220    * Test for the case where sequences have start > 1
221    */
222   @Test(groups = { "Functional" })
223   public void testGetMappedCodons_forSubSequences()
224   {
225     final Sequence seq1 = new Sequence("Seq1", "c-G-TA-gC-gT-T", 27, 35);
226     seq1.createDatasetSequence();
227
228     final Sequence aseq1 = new Sequence("Seq1", "-V-L", 12, 13);
229     aseq1.createDatasetSequence();
230
231     /*
232      * Set up the mappings for the exons (upper-case bases)
233      */
234     AlignedCodonFrame acf = new AlignedCodonFrame();
235     MapList map = new MapList(new int[] { 28, 30, 32, 32, 34, 35 },
236             new int[] { 12, 13 }, 3, 1);
237     acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
238
239     assertEquals(
240             "[G, T, A]",
241             Arrays.toString(acf.getMappedCodons(aseq1.getDatasetSequence(),
242                     12).get(0)));
243     assertEquals(
244             "[C, T, T]",
245             Arrays.toString(acf.getMappedCodons(aseq1.getDatasetSequence(),
246                     13).get(0)));
247   }
248
249   @Test(groups = { "Functional" })
250   public void testCouldReplaceSequence()
251   {
252     SequenceI seq1 = new Sequence("Seq1/10-21", "aaacccgggttt");
253     SequenceI seq1proxy = new SequenceDummy("Seq1");
254
255     // map to region within sequence is ok
256     assertTrue(AlignedCodonFrame.couldRealiseSequence(seq1proxy, seq1, 12,
257             17));
258     // map to region overlapping sequence is ok
259     assertTrue(AlignedCodonFrame.couldRealiseSequence(seq1proxy, seq1, 5,
260             10));
261     assertTrue(AlignedCodonFrame.couldRealiseSequence(seq1proxy, seq1, 21,
262             26));
263     // map to region before sequence is not ok
264     assertFalse(AlignedCodonFrame.couldRealiseSequence(seq1proxy, seq1, 4,
265             9));
266     // map to region after sequence is not ok
267     assertFalse(AlignedCodonFrame.couldRealiseSequence(seq1proxy, seq1, 22,
268             27));
269
270     /*
271      * test should fail if name doesn't match
272      */
273     seq1proxy.setName("Seq1a");
274     assertFalse(AlignedCodonFrame.couldRealiseSequence(seq1proxy, seq1, 12,
275             17));
276     seq1proxy.setName("Seq1");
277     seq1.setName("Seq1a");
278     assertFalse(AlignedCodonFrame.couldRealiseSequence(seq1proxy, seq1, 12,
279             17));
280
281     /*
282      * a dummy sequence can't replace a real one
283      */
284     assertFalse(AlignedCodonFrame.couldRealiseSequence(seq1, seq1proxy, 12,
285             17));
286
287     /*
288      * a dummy sequence can't replace a dummy sequence
289      */
290     SequenceI seq1proxy2 = new SequenceDummy("Seq1");
291     assertFalse(AlignedCodonFrame.couldRealiseSequence(seq1proxy,
292             seq1proxy2, 12, 17));
293
294     /*
295      * a real sequence can't replace a real one
296      */
297     SequenceI seq1a = new Sequence("Seq1/10-21", "aaacccgggttt");
298     assertFalse(AlignedCodonFrame.couldRealiseSequence(seq1, seq1a, 12, 17));
299   }
300
301   /**
302    * Tests for the method that tests whether any mapping to a dummy sequence can
303    * be 'realised' to a given real sequence
304    */
305   @Test(groups = { "Functional" })
306   public void testIsRealisableWith()
307   {
308     SequenceI seq1 = new Sequence("Seq1", "tttaaaCCCGGGtttaaa");
309     SequenceI seq2 = new Sequence("Seq2", "PG");
310     SequenceI seq1proxy = new SequenceDummy("Seq1");
311     seq1.createDatasetSequence();
312     seq2.createDatasetSequence();
313     MapList mapList = new MapList(new int[] { 7, 12 }, new int[] { 2, 3 },
314             3, 1);
315     AlignedCodonFrame acf = new AlignedCodonFrame();
316     acf.addMap(seq1proxy, seq2, mapList);
317
318     /*
319      * Seq2 is mapped to SequenceDummy seq1proxy bases 4-9
320      * This is 'realisable' from real sequence Seq1
321      */
322     assertTrue(acf.isRealisableWith(seq1));
323
324     /*
325      * test should fail if name doesn't match
326      */
327     seq1proxy.setName("Seq1a");
328     assertFalse(acf.isRealisableWith(seq1));
329     seq1proxy.setName("Seq1");
330
331     SequenceI seq1ds = seq1.getDatasetSequence();
332     seq1ds.setName("Seq1a");
333     assertFalse(acf.isRealisableWith(seq1));
334     seq1ds.setName("Seq1");
335
336     /*
337      * test should fail if no sequence overlap with mapping of bases 7-12
338      * use artificial start/end values to test this
339      */
340     seq1ds.setStart(1);
341     seq1ds.setEnd(6);
342     // seq1 precedes mapped region:
343     assertFalse(acf.isRealisableWith(seq1));
344     seq1ds.setEnd(7);
345     // seq1 includes first mapped base:
346     assertTrue(acf.isRealisableWith(seq1));
347     seq1ds.setStart(13);
348     seq1ds.setEnd(18);
349     // seq1 follows mapped region:
350     assertFalse(acf.isRealisableWith(seq1));
351     seq1ds.setStart(12);
352     // seq1 includes last mapped base:
353     assertTrue(acf.isRealisableWith(seq1));
354   }
355
356   /**
357    * Tests for the method that converts mappings to a dummy sequence to mappings
358    * to a compatible real sequence
359    */
360   @Test(groups = { "Functional" })
361   public void testRealiseWith()
362   {
363     SequenceI seq1 = new Sequence("Seq1", "tttCAACCCGGGtttaaa");
364     SequenceI seq2 = new Sequence("Seq2", "QPG");
365     SequenceI seq2a = new Sequence("Seq2a", "QPG");
366     SequenceI seq1proxy = new SequenceDummy("Seq1");
367     seq1.createDatasetSequence();
368     seq2.createDatasetSequence();
369     seq2a.createDatasetSequence();
370
371     /*
372      * Make mappings from Seq2 and Seq2a peptides to dummy sequence Seq1
373      */
374     AlignedCodonFrame acf = new AlignedCodonFrame();
375
376     // map PG to codons 7-12 (CCCGGG)
377     MapList mapping1 = new MapList(new int[] { 7, 12 }, new int[] { 2, 3 },
378             3, 1);
379     acf.addMap(seq1proxy, seq2, mapping1);
380     acf.addMap(seq1proxy, seq2a, mapping1);
381
382     // map QP to codons 4-9 (CAACCC)
383     MapList mapping2 = new MapList(new int[] { 4, 9 }, new int[] { 1, 2 },
384             3, 1);
385     acf.addMap(seq1proxy, seq2, mapping2);
386     acf.addMap(seq1proxy, seq2a, mapping2);
387
388     /*
389      * acf now has two mappings one from Seq1 to Seq2, one from Seq1 to Seq2a
390      */
391     assertEquals(2, acf.getdnaSeqs().length);
392     assertSame(seq1proxy, acf.getdnaSeqs()[0]);
393     assertSame(seq1proxy, acf.getdnaSeqs()[1]);
394     assertEquals(2, acf.getProtMappings().length);
395
396     // 'realise' these mappings with the compatible sequence seq1
397     // two mappings should be updated:
398     assertEquals(2, acf.realiseWith(seq1));
399     assertSame(seq1.getDatasetSequence(), acf.getdnaSeqs()[0]);
400     assertSame(seq1.getDatasetSequence(), acf.getdnaSeqs()[1]);
401   }
402
403   /**
404    * Test the method that locates the mapped codon for a protein position.
405    */
406   @Test(groups = { "Functional" })
407   public void testGetMappedRegion_eitherWay()
408   {
409     final Sequence seq1 = new Sequence("Seq1", "AAACCCGGGTTT");
410     seq1.createDatasetSequence();
411     final Sequence seq2 = new Sequence("Seq2", "KPGF");
412     seq2.createDatasetSequence();
413     final Sequence seq3 = new Sequence("Seq3", "QYKPGFSW");
414     seq3.createDatasetSequence();
415
416     /*
417      * map Seq1 to all of Seq2 and part of Seq3
418      */
419     AlignedCodonFrame acf = new AlignedCodonFrame();
420     MapList map = new MapList(new int[] { 1, 12 }, new int[] { 1, 4 }, 3, 1);
421     acf.addMap(seq1.getDatasetSequence(), seq2.getDatasetSequence(), map);
422     map = new MapList(new int[] { 1, 12 }, new int[] { 3, 6 }, 3, 1);
423     acf.addMap(seq1.getDatasetSequence(), seq3.getDatasetSequence(), map);
424
425     /*
426      * map part of Seq3 to Seq2
427      */
428     map = new MapList(new int[] { 3, 6 }, new int[] { 1, 4 }, 1, 1);
429     acf.addMap(seq3.getDatasetSequence(), seq2.getDatasetSequence(), map);
430
431     /*
432      * original case - locate mapped codon for protein position
433      */
434     assertArrayEquals(new int[] { 4, 6 },
435             acf.getMappedRegion(seq1, seq2, 2));
436     assertArrayEquals(new int[] { 7, 9 },
437             acf.getMappedRegion(seq1, seq3, 5));
438     assertNull(acf.getMappedRegion(seq1, seq3, 1));
439
440     /*
441      * locate mapped protein for protein position
442      */
443     assertArrayEquals(new int[] { 4, 4 },
444             acf.getMappedRegion(seq3, seq2, 2));
445
446     /*
447      * reverse location protein-to-protein
448      */
449     assertArrayEquals(new int[] { 2, 2 },
450             acf.getMappedRegion(seq2, seq3, 4));
451
452     /*
453      * reverse location protein-from-nucleotide
454      * any of codon [4, 5, 6] positions map to seq2/2
455      */
456     assertArrayEquals(new int[] { 2, 2 },
457             acf.getMappedRegion(seq2, seq1, 4));
458     assertArrayEquals(new int[] { 2, 2 },
459             acf.getMappedRegion(seq2, seq1, 5));
460     assertArrayEquals(new int[] { 2, 2 },
461             acf.getMappedRegion(seq2, seq1, 6));
462   }
463
464   /**
465    * Tests for addMap. See also tests for MapList.addMapList
466    */
467   @Test(groups = { "Functional" })
468   public void testAddMap()
469   {
470     final Sequence seq1 = new Sequence("Seq1", "c-G-TA-gC-gT-T");
471     seq1.createDatasetSequence();
472     final Sequence aseq1 = new Sequence("Seq1", "-V-L");
473     aseq1.createDatasetSequence();
474
475     AlignedCodonFrame acf = new AlignedCodonFrame();
476     MapList map = new MapList(new int[] { 2, 4, 6, 6, 8, 9 }, new int[] {
477         1, 2 }, 3, 1);
478     acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
479     assertEquals(1, acf.getMappingsFromSequence(seq1).size());
480     Mapping before = acf.getMappingsFromSequence(seq1).get(0);
481
482     /*
483      * add the same map again, verify it doesn't get duplicated
484      */
485     acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
486     assertEquals(1, acf.getMappingsFromSequence(seq1).size());
487     assertSame(before, acf.getMappingsFromSequence(seq1).get(0));
488   }
489 }