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;
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;
30 import jalview.gui.JvOptionPane;
31 import jalview.util.MapList;
33 import java.util.Arrays;
34 import java.util.List;
36 import org.testng.annotations.BeforeClass;
37 import org.testng.annotations.Test;
39 public class AlignedCodonFrameTest
42 @BeforeClass(alwaysRun = true)
43 public void setUpJvOptionPane()
45 JvOptionPane.setInteractiveMode(false);
46 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
50 * Test the method that locates the first aligned sequence that has a mapping.
52 @Test(groups = { "Functional" })
53 public void testFindAlignedSequence()
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);
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);
72 * Mapping from first DNA sequence to second AA sequence.
74 AlignedCodonFrame acf = new AlignedCodonFrame();
76 assertNull(acf.findAlignedSequence(seq1, aa));
78 MapList map = new MapList(new int[] { 1, 6 }, new int[] { 1, 2 }, 3, 1);
79 acf.addMap(seq1.getDatasetSequence(), aseq2.getDatasetSequence(), map);
82 * DNA seq1 maps to AA seq2
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));
90 assertEquals(cdna.getSequenceAt(0), acf.findAlignedSequence(aa
91 .getSequenceAt(1).getDatasetSequence(), cdna));
95 * Test the method that locates the mapped codon for a protein position.
97 @Test(groups = { "Functional" })
98 public void testGetMappedRegion()
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();
106 final Sequence aseq1 = new Sequence("Seq1", "-P-R");
107 aseq1.createDatasetSequence();
108 final Sequence aseq2 = new Sequence("Seq2", "-LY-Q");
109 aseq2.createDatasetSequence();
112 * First with no mappings
114 AlignedCodonFrame acf = new AlignedCodonFrame();
116 assertNull(acf.getMappedRegion(seq1, aseq1, 1));
119 * Set up the mappings for the exons (upper-case bases)
120 * Note residue Q is unmapped
122 MapList map = new MapList(new int[] { 2, 4, 6, 6, 8, 9 }, new int[] {
124 acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
125 map = new MapList(new int[] { 1, 2, 4, 5, 7, 8 }, new int[] { 1, 2 },
127 acf.addMap(seq2.getDatasetSequence(), aseq2.getDatasetSequence(), map);
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));
139 * No mapping from seq2 to Q
141 assertNull(acf.getMappedRegion(seq2, aseq2, 3));
144 * No mapping from sequence 1 to sequence 2
146 assertNull(acf.getMappedRegion(seq1, aseq2, 1));
149 @Test(groups = { "Functional" })
150 public void testGetMappedCodons()
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();
158 * First with no mappings
160 AlignedCodonFrame acf = new AlignedCodonFrame();
162 assertNull(acf.getMappedCodons(seq1.getDatasetSequence(), 0));
165 * Set up the mappings for the exons (upper-case bases)
167 MapList map = new MapList(new int[] { 2, 4, 6, 6, 8, 9 }, new int[] {
169 acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
171 assertEquals(1, acf.getMappedCodons(aseq1.getDatasetSequence(), 1)
175 Arrays.toString(acf.getMappedCodons(aseq1.getDatasetSequence(),
179 Arrays.toString(acf.getMappedCodons(aseq1.getDatasetSequence(),
184 * Test for the case where there is more than one variant of the DNA mapping
185 * to a protein sequence
187 @Test(groups = { "Functional" })
188 public void testGetMappedCodons_dnaVariants()
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();
197 AlignedCodonFrame acf = new AlignedCodonFrame();
200 * Set up the mappings for the exons (upper-case bases)
202 MapList map = new MapList(new int[] { 2, 4, 6, 6, 8, 9 }, new int[] {
204 acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
205 acf.addMap(seq2.getDatasetSequence(), aseq1.getDatasetSequence(), map);
207 assertEquals(2, acf.getMappedCodons(aseq1.getDatasetSequence(), 1)
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)));
220 * Test for the case where sequences have start > 1
222 @Test(groups = { "Functional" })
223 public void testGetMappedCodons_forSubSequences()
225 final Sequence seq1 = new Sequence("Seq1", "c-G-TA-gC-gT-T", 27, 35);
226 seq1.createDatasetSequence();
228 final Sequence aseq1 = new Sequence("Seq1", "-V-L", 12, 13);
229 aseq1.createDatasetSequence();
232 * Set up the mappings for the exons (upper-case bases)
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);
241 Arrays.toString(acf.getMappedCodons(aseq1.getDatasetSequence(),
245 Arrays.toString(acf.getMappedCodons(aseq1.getDatasetSequence(),
249 @Test(groups = { "Functional" })
250 public void testCouldReplaceSequence()
252 SequenceI seq1 = new Sequence("Seq1/10-21", "aaacccgggttt");
253 SequenceI seq1proxy = new SequenceDummy("Seq1");
255 // map to region within sequence is ok
256 assertTrue(AlignedCodonFrame.couldRealiseSequence(seq1proxy, seq1, 12,
258 // map to region overlapping sequence is ok
259 assertTrue(AlignedCodonFrame.couldRealiseSequence(seq1proxy, seq1, 5,
261 assertTrue(AlignedCodonFrame.couldRealiseSequence(seq1proxy, seq1, 21,
263 // map to region before sequence is not ok
264 assertFalse(AlignedCodonFrame.couldRealiseSequence(seq1proxy, seq1, 4,
266 // map to region after sequence is not ok
267 assertFalse(AlignedCodonFrame.couldRealiseSequence(seq1proxy, seq1, 22,
271 * test should fail if name doesn't match
273 seq1proxy.setName("Seq1a");
274 assertFalse(AlignedCodonFrame.couldRealiseSequence(seq1proxy, seq1, 12,
276 seq1proxy.setName("Seq1");
277 seq1.setName("Seq1a");
278 assertFalse(AlignedCodonFrame.couldRealiseSequence(seq1proxy, seq1, 12,
282 * a dummy sequence can't replace a real one
284 assertFalse(AlignedCodonFrame.couldRealiseSequence(seq1, seq1proxy, 12,
288 * a dummy sequence can't replace a dummy sequence
290 SequenceI seq1proxy2 = new SequenceDummy("Seq1");
291 assertFalse(AlignedCodonFrame.couldRealiseSequence(seq1proxy,
292 seq1proxy2, 12, 17));
295 * a real sequence can't replace a real one
297 SequenceI seq1a = new Sequence("Seq1/10-21", "aaacccgggttt");
298 assertFalse(AlignedCodonFrame.couldRealiseSequence(seq1, seq1a, 12, 17));
302 * Tests for the method that tests whether any mapping to a dummy sequence can
303 * be 'realised' to a given real sequence
305 @Test(groups = { "Functional" })
306 public void testIsRealisableWith()
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 },
315 AlignedCodonFrame acf = new AlignedCodonFrame();
316 acf.addMap(seq1proxy, seq2, mapList);
319 * Seq2 is mapped to SequenceDummy seq1proxy bases 4-9
320 * This is 'realisable' from real sequence Seq1
322 assertTrue(acf.isRealisableWith(seq1));
325 * test should fail if name doesn't match
327 seq1proxy.setName("Seq1a");
328 assertFalse(acf.isRealisableWith(seq1));
329 seq1proxy.setName("Seq1");
331 SequenceI seq1ds = seq1.getDatasetSequence();
332 seq1ds.setName("Seq1a");
333 assertFalse(acf.isRealisableWith(seq1));
334 seq1ds.setName("Seq1");
337 * test should fail if no sequence overlap with mapping of bases 7-12
338 * use artificial start/end values to test this
342 // seq1 precedes mapped region:
343 assertFalse(acf.isRealisableWith(seq1));
345 // seq1 includes first mapped base:
346 assertTrue(acf.isRealisableWith(seq1));
349 // seq1 follows mapped region:
350 assertFalse(acf.isRealisableWith(seq1));
352 // seq1 includes last mapped base:
353 assertTrue(acf.isRealisableWith(seq1));
357 * Tests for the method that converts mappings to a dummy sequence to mappings
358 * to a compatible real sequence
360 @Test(groups = { "Functional" })
361 public void testRealiseWith()
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();
372 * Make mappings from Seq2 and Seq2a peptides to dummy sequence Seq1
374 AlignedCodonFrame acf = new AlignedCodonFrame();
376 // map PG to codons 7-12 (CCCGGG)
377 MapList mapping1 = new MapList(new int[] { 7, 12 }, new int[] { 2, 3 },
379 acf.addMap(seq1proxy, seq2, mapping1);
380 acf.addMap(seq1proxy, seq2a, mapping1);
382 // map QP to codons 4-9 (CAACCC)
383 MapList mapping2 = new MapList(new int[] { 4, 9 }, new int[] { 1, 2 },
385 acf.addMap(seq1proxy, seq2, mapping2);
386 acf.addMap(seq1proxy, seq2a, mapping2);
389 * acf now has two mappings one from Seq1 to Seq2, one from Seq1 to Seq2a
391 assertEquals(2, acf.getdnaSeqs().length);
392 assertSame(seq1proxy, acf.getdnaSeqs()[0]);
393 assertSame(seq1proxy, acf.getdnaSeqs()[1]);
394 assertEquals(2, acf.getProtMappings().length);
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]);
404 * Test the method that locates the mapped codon for a protein position.
406 @Test(groups = { "Functional" })
407 public void testGetMappedRegion_eitherWay()
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();
417 * map Seq1 to all of Seq2 and part of Seq3
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);
426 * map part of Seq3 to Seq2
428 map = new MapList(new int[] { 3, 6 }, new int[] { 1, 4 }, 1, 1);
429 acf.addMap(seq3.getDatasetSequence(), seq2.getDatasetSequence(), map);
432 * original case - locate mapped codon for protein position
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));
441 * locate mapped protein for protein position
443 assertArrayEquals(new int[] { 4, 4 },
444 acf.getMappedRegion(seq3, seq2, 2));
447 * reverse location protein-to-protein
449 assertArrayEquals(new int[] { 2, 2 },
450 acf.getMappedRegion(seq2, seq3, 4));
453 * reverse location protein-from-nucleotide
454 * any of codon [4, 5, 6] positions map to seq2/2
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));
465 * Tests for addMap. See also tests for MapList.addMapList
467 @Test(groups = { "Functional" })
468 public void testAddMap()
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();
475 AlignedCodonFrame acf = new AlignedCodonFrame();
476 MapList map = new MapList(new int[] { 2, 4, 6, 6, 8, 9 }, new int[] {
478 acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
479 assertEquals(1, acf.getMappingsFromSequence(seq1).size());
480 Mapping before = acf.getMappingsFromSequence(seq1).get(0);
483 * add the same map again, verify it doesn't get duplicated
485 acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
486 assertEquals(1, acf.getMappingsFromSequence(seq1).size());
487 assertSame(before, acf.getMappingsFromSequence(seq1).get(0));