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.analysis;
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertNotNull;
26 import static org.testng.AssertJUnit.assertNull;
27 import static org.testng.AssertJUnit.assertSame;
28 import static org.testng.AssertJUnit.assertTrue;
30 import jalview.analysis.AlignmentUtils.DnaVariant;
31 import jalview.datamodel.AlignedCodonFrame;
32 import jalview.datamodel.Alignment;
33 import jalview.datamodel.AlignmentAnnotation;
34 import jalview.datamodel.AlignmentI;
35 import jalview.datamodel.Annotation;
36 import jalview.datamodel.DBRefEntry;
37 import jalview.datamodel.GeneLociI;
38 import jalview.datamodel.Mapping;
39 import jalview.datamodel.SearchResultMatchI;
40 import jalview.datamodel.SearchResultsI;
41 import jalview.datamodel.Sequence;
42 import jalview.datamodel.SequenceFeature;
43 import jalview.datamodel.SequenceI;
44 import jalview.datamodel.features.SequenceFeatures;
45 import jalview.gui.JvOptionPane;
46 import jalview.io.AppletFormatAdapter;
47 import jalview.io.DataSourceType;
48 import jalview.io.FileFormat;
49 import jalview.io.FileFormatI;
50 import jalview.io.FormatAdapter;
51 import jalview.io.gff.SequenceOntologyI;
52 import jalview.util.MapList;
53 import jalview.util.MappingUtils;
54 import jalview.ws.params.InvalidArgumentException;
56 import java.io.IOException;
57 import java.util.ArrayList;
58 import java.util.Arrays;
59 import java.util.LinkedHashMap;
60 import java.util.List;
62 import java.util.TreeMap;
64 import org.testng.annotations.BeforeClass;
65 import org.testng.annotations.Test;
67 public class AlignmentUtilsTests
69 private static Sequence ts = new Sequence("short",
70 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm");
72 @BeforeClass(alwaysRun = true)
73 public void setUpJvOptionPane()
75 JvOptionPane.setInteractiveMode(false);
76 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
79 @Test(groups = { "Functional" })
80 public void testExpandContext()
82 AlignmentI al = new Alignment(new Sequence[] {});
83 for (int i = 4; i < 14; i += 2)
85 SequenceI s1 = ts.deriveSequence().getSubSequence(i, i + 7);
88 System.out.println(new AppletFormatAdapter().formatSequences(
91 for (int flnk = -1; flnk < 25; flnk++)
93 AlignmentI exp = AlignmentUtils.expandContext(al, flnk);
94 System.out.println("\nFlank size: " + flnk);
95 System.out.println(new AppletFormatAdapter().formatSequences(
96 FileFormat.Clustal, exp, true));
100 * Full expansion to complete sequences
102 for (SequenceI sq : exp.getSequences())
104 String ung = sq.getSequenceAsString().replaceAll("-+", "");
105 final String errorMsg = "Flanking sequence not the same as original dataset sequence.\n"
108 + sq.getDatasetSequence().getSequenceAsString();
109 assertTrue(errorMsg, ung.equalsIgnoreCase(sq.getDatasetSequence()
110 .getSequenceAsString()));
116 * Last sequence is fully expanded, others have leading gaps to match
118 assertTrue(exp.getSequenceAt(4).getSequenceAsString()
120 assertTrue(exp.getSequenceAt(3).getSequenceAsString()
121 .startsWith("--abc"));
122 assertTrue(exp.getSequenceAt(2).getSequenceAsString()
123 .startsWith("----abc"));
124 assertTrue(exp.getSequenceAt(1).getSequenceAsString()
125 .startsWith("------abc"));
126 assertTrue(exp.getSequenceAt(0).getSequenceAsString()
127 .startsWith("--------abc"));
133 * Test that annotations are correctly adjusted by expandContext
135 @Test(groups = { "Functional" })
136 public void testExpandContext_annotation()
138 AlignmentI al = new Alignment(new Sequence[] {});
139 SequenceI ds = new Sequence("Seq1", "ABCDEFGHI");
141 SequenceI seq1 = ds.deriveSequence().getSubSequence(3, 6);
142 al.addSequence(seq1);
145 * Annotate DEF with 4/5/6 respectively
147 Annotation[] anns = new Annotation[] { new Annotation(4),
148 new Annotation(5), new Annotation(6) };
149 AlignmentAnnotation ann = new AlignmentAnnotation("SS",
150 "secondary structure", anns);
151 seq1.addAlignmentAnnotation(ann);
154 * The annotations array should match aligned positions
156 assertEquals(3, ann.annotations.length);
157 assertEquals(4, ann.annotations[0].value, 0.001);
158 assertEquals(5, ann.annotations[1].value, 0.001);
159 assertEquals(6, ann.annotations[2].value, 0.001);
162 * Check annotation to sequence position mappings before expanding the
163 * sequence; these are set up in Sequence.addAlignmentAnnotation ->
164 * Annotation.setSequenceRef -> createSequenceMappings
166 assertNull(ann.getAnnotationForPosition(1));
167 assertNull(ann.getAnnotationForPosition(2));
168 assertNull(ann.getAnnotationForPosition(3));
169 assertEquals(4, ann.getAnnotationForPosition(4).value, 0.001);
170 assertEquals(5, ann.getAnnotationForPosition(5).value, 0.001);
171 assertEquals(6, ann.getAnnotationForPosition(6).value, 0.001);
172 assertNull(ann.getAnnotationForPosition(7));
173 assertNull(ann.getAnnotationForPosition(8));
174 assertNull(ann.getAnnotationForPosition(9));
177 * Expand the subsequence to the full sequence abcDEFghi
179 AlignmentI expanded = AlignmentUtils.expandContext(al, -1);
180 assertEquals("abcDEFghi", expanded.getSequenceAt(0)
181 .getSequenceAsString());
184 * Confirm the alignment and sequence have the same SS annotation,
185 * referencing the expanded sequence
187 ann = expanded.getSequenceAt(0).getAnnotation()[0];
188 assertSame(ann, expanded.getAlignmentAnnotation()[0]);
189 assertSame(expanded.getSequenceAt(0), ann.sequenceRef);
192 * The annotations array should have null values except for annotated
195 assertNull(ann.annotations[0]);
196 assertNull(ann.annotations[1]);
197 assertNull(ann.annotations[2]);
198 assertEquals(4, ann.annotations[3].value, 0.001);
199 assertEquals(5, ann.annotations[4].value, 0.001);
200 assertEquals(6, ann.annotations[5].value, 0.001);
201 assertNull(ann.annotations[6]);
202 assertNull(ann.annotations[7]);
203 assertNull(ann.annotations[8]);
206 * sequence position mappings should be unchanged
208 assertNull(ann.getAnnotationForPosition(1));
209 assertNull(ann.getAnnotationForPosition(2));
210 assertNull(ann.getAnnotationForPosition(3));
211 assertEquals(4, ann.getAnnotationForPosition(4).value, 0.001);
212 assertEquals(5, ann.getAnnotationForPosition(5).value, 0.001);
213 assertEquals(6, ann.getAnnotationForPosition(6).value, 0.001);
214 assertNull(ann.getAnnotationForPosition(7));
215 assertNull(ann.getAnnotationForPosition(8));
216 assertNull(ann.getAnnotationForPosition(9));
220 * Test method that returns a map of lists of sequences by sequence name.
222 * @throws IOException
224 @Test(groups = { "Functional" })
225 public void testGetSequencesByName() throws IOException
227 final String data = ">Seq1Name\nKQYL\n" + ">Seq2Name\nRFPW\n"
228 + ">Seq1Name\nABCD\n";
229 AlignmentI al = loadAlignment(data, FileFormat.Fasta);
230 Map<String, List<SequenceI>> map = AlignmentUtils
231 .getSequencesByName(al);
232 assertEquals(2, map.keySet().size());
233 assertEquals(2, map.get("Seq1Name").size());
234 assertEquals("KQYL", map.get("Seq1Name").get(0).getSequenceAsString());
235 assertEquals("ABCD", map.get("Seq1Name").get(1).getSequenceAsString());
236 assertEquals(1, map.get("Seq2Name").size());
237 assertEquals("RFPW", map.get("Seq2Name").get(0).getSequenceAsString());
241 * Helper method to load an alignment and ensure dataset sequences are set up.
247 * @throws IOException
249 protected AlignmentI loadAlignment(final String data, FileFormatI format)
252 AlignmentI a = new FormatAdapter().readFile(data,
253 DataSourceType.PASTE, format);
259 * Test mapping of protein to cDNA, for the case where we have no sequence
260 * cross-references, so mappings are made first-served 1-1 where sequences
263 * @throws IOException
265 @Test(groups = { "Functional" })
266 public void testMapProteinAlignmentToCdna_noXrefs() throws IOException
268 List<SequenceI> protseqs = new ArrayList<>();
269 protseqs.add(new Sequence("UNIPROT|V12345", "EIQ"));
270 protseqs.add(new Sequence("UNIPROT|V12346", "EIQ"));
271 protseqs.add(new Sequence("UNIPROT|V12347", "SAR"));
272 AlignmentI protein = new Alignment(protseqs.toArray(new SequenceI[3]));
273 protein.setDataset(null);
275 List<SequenceI> dnaseqs = new ArrayList<>();
276 dnaseqs.add(new Sequence("EMBL|A11111", "TCAGCACGC")); // = SAR
277 dnaseqs.add(new Sequence("EMBL|A22222", "GAGATACAA")); // = EIQ
278 dnaseqs.add(new Sequence("EMBL|A33333", "GAAATCCAG")); // = EIQ
279 dnaseqs.add(new Sequence("EMBL|A44444", "GAAATTCAG")); // = EIQ
280 AlignmentI cdna = new Alignment(dnaseqs.toArray(new SequenceI[4]));
281 cdna.setDataset(null);
283 assertTrue(AlignmentUtils.mapProteinAlignmentToCdna(protein, cdna));
285 // 3 mappings made, each from 1 to 1 sequence
286 assertEquals(3, protein.getCodonFrames().size());
287 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).size());
288 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).size());
289 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(2)).size());
291 // V12345 mapped to A22222
292 AlignedCodonFrame acf = protein.getCodonFrame(protein.getSequenceAt(0))
294 assertEquals(1, acf.getdnaSeqs().length);
295 assertEquals(cdna.getSequenceAt(1).getDatasetSequence(),
296 acf.getdnaSeqs()[0]);
297 Mapping[] protMappings = acf.getProtMappings();
298 assertEquals(1, protMappings.length);
299 MapList mapList = protMappings[0].getMap();
300 assertEquals(3, mapList.getFromRatio());
301 assertEquals(1, mapList.getToRatio());
302 assertTrue(Arrays.equals(new int[] { 1, 9 }, mapList.getFromRanges()
304 assertEquals(1, mapList.getFromRanges().size());
305 assertTrue(Arrays.equals(new int[] { 1, 3 },
306 mapList.getToRanges().get(0)));
307 assertEquals(1, mapList.getToRanges().size());
309 // V12346 mapped to A33333
310 acf = protein.getCodonFrame(protein.getSequenceAt(1)).get(0);
311 assertEquals(1, acf.getdnaSeqs().length);
312 assertEquals(cdna.getSequenceAt(2).getDatasetSequence(),
313 acf.getdnaSeqs()[0]);
315 // V12347 mapped to A11111
316 acf = protein.getCodonFrame(protein.getSequenceAt(2)).get(0);
317 assertEquals(1, acf.getdnaSeqs().length);
318 assertEquals(cdna.getSequenceAt(0).getDatasetSequence(),
319 acf.getdnaSeqs()[0]);
321 // no mapping involving the 'extra' A44444
322 assertTrue(protein.getCodonFrame(cdna.getSequenceAt(3)).isEmpty());
326 * Test for the alignSequenceAs method that takes two sequences and a mapping.
328 @Test(groups = { "Functional" })
329 public void testAlignSequenceAs_withMapping_noIntrons()
331 MapList map = new MapList(new int[] { 1, 6 }, new int[] { 1, 2 }, 3, 1);
334 * No existing gaps in dna:
336 checkAlignSequenceAs("GGGAAA", "-A-L-", false, false, map,
340 * Now introduce gaps in dna but ignore them when realigning.
342 checkAlignSequenceAs("-G-G-G-A-A-A-", "-A-L-", false, false, map,
346 * Now include gaps in dna when realigning. First retaining 'mapped' gaps
347 * only, i.e. those within the exon region.
349 checkAlignSequenceAs("-G-G--G-A--A-A-", "-A-L-", true, false, map,
350 "---G-G--G---A--A-A");
353 * Include all gaps in dna when realigning (within and without the exon
354 * region). The leading gap, and the gaps between codons, are subsumed by
355 * the protein alignment gap.
357 checkAlignSequenceAs("-G-GG--AA-A---", "-A-L-", true, true, map,
358 "---G-GG---AA-A---");
361 * Include only unmapped gaps in dna when realigning (outside the exon
362 * region). The leading gap, and the gaps between codons, are subsumed by
363 * the protein alignment gap.
365 checkAlignSequenceAs("-G-GG--AA-A-", "-A-L-", false, true, map,
370 * Test for the alignSequenceAs method that takes two sequences and a mapping.
372 @Test(groups = { "Functional" })
373 public void testAlignSequenceAs_withMapping_withIntrons()
376 * Exons at codon 2 (AAA) and 4 (TTT)
378 MapList map = new MapList(new int[] { 4, 6, 10, 12 },
379 new int[] { 1, 2 }, 3, 1);
382 * Simple case: no gaps in dna
384 checkAlignSequenceAs("GGGAAACCCTTTGGG", "--A-L-", false, false, map,
385 "GGG---AAACCCTTTGGG");
388 * Add gaps to dna - but ignore when realigning.
390 checkAlignSequenceAs("-G-G-G--A--A---AC-CC-T-TT-GG-G-", "--A-L-",
391 false, false, map, "GGG---AAACCCTTTGGG");
394 * Add gaps to dna - include within exons only when realigning.
396 checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
397 true, false, map, "GGG---A--A---ACCCT-TTGGG");
400 * Include gaps outside exons only when realigning.
402 checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
403 false, true, map, "-G-G-GAAAC-CCTTT-GG-G-");
406 * Include gaps following first intron if we are 'preserving mapped gaps'
408 checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
409 true, true, map, "-G-G-G--A--A---A-C-CC-T-TT-GG-G-");
412 * Include all gaps in dna when realigning.
414 checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
415 true, true, map, "-G-G-G--A--A---A-C-CC-T-TT-GG-G-");
419 * Test for the case where not all of the protein sequence is mapped to cDNA.
421 @Test(groups = { "Functional" })
422 public void testAlignSequenceAs_withMapping_withUnmappedProtein()
425 * Exons at codon 2 (AAA) and 4 (TTT) mapped to A and P
427 final MapList map = new MapList(new int[] { 4, 6, 10, 12 }, new int[] {
431 * -L- 'aligns' ccc------
433 checkAlignSequenceAs("gggAAAcccTTTggg", "-A-L-P-", false, false, map,
434 "gggAAAccc------TTTggg");
438 * Helper method that performs and verifies the method under test.
441 * the sequence to be realigned
443 * the sequence whose alignment is to be copied
444 * @param preserveMappedGaps
445 * @param preserveUnmappedGaps
449 protected void checkAlignSequenceAs(final String alignee,
450 final String alignModel, final boolean preserveMappedGaps,
451 final boolean preserveUnmappedGaps, MapList map,
452 final String expected)
454 SequenceI alignMe = new Sequence("Seq1", alignee);
455 alignMe.createDatasetSequence();
456 SequenceI alignFrom = new Sequence("Seq2", alignModel);
457 alignFrom.createDatasetSequence();
458 AlignedCodonFrame acf = new AlignedCodonFrame();
459 acf.addMap(alignMe.getDatasetSequence(),
460 alignFrom.getDatasetSequence(), map);
462 AlignmentUtils.alignSequenceAs(alignMe, alignFrom, acf, "---", '-',
463 preserveMappedGaps, preserveUnmappedGaps);
464 assertEquals(expected, alignMe.getSequenceAsString());
468 * Test for the alignSequenceAs method where we preserve gaps in introns only.
470 @Test(groups = { "Functional" })
471 public void testAlignSequenceAs_keepIntronGapsOnly()
475 * Intron GGGAAA followed by exon CCCTTT
477 MapList map = new MapList(new int[] { 7, 12 }, new int[] { 1, 2 }, 3, 1);
479 checkAlignSequenceAs("GG-G-AA-A-C-CC-T-TT", "AL", false, true, map,
484 * Test the method that realigns protein to match mapped codon alignment.
486 @Test(groups = { "Functional" })
487 public void testAlignProteinAsDna()
489 // seq1 codons are [1,2,3] [4,5,6] [7,8,9] [10,11,12]
490 SequenceI dna1 = new Sequence("Seq1", "TGCCATTACCAG-");
491 // seq2 codons are [1,3,4] [5,6,7] [8,9,10] [11,12,13]
492 SequenceI dna2 = new Sequence("Seq2", "T-GCCATTACCAG");
493 // seq3 codons are [1,2,3] [4,5,7] [8,9,10] [11,12,13]
494 SequenceI dna3 = new Sequence("Seq3", "TGCCA-TTACCAG");
495 AlignmentI dna = new Alignment(new SequenceI[] { dna1, dna2, dna3 });
496 dna.setDataset(null);
498 // protein alignment will be realigned like dna
499 SequenceI prot1 = new Sequence("Seq1", "CHYQ");
500 SequenceI prot2 = new Sequence("Seq2", "CHYQ");
501 SequenceI prot3 = new Sequence("Seq3", "CHYQ");
502 SequenceI prot4 = new Sequence("Seq4", "R-QSV"); // unmapped, unchanged
503 AlignmentI protein = new Alignment(new SequenceI[] { prot1, prot2,
505 protein.setDataset(null);
507 MapList map = new MapList(new int[] { 1, 12 }, new int[] { 1, 4 }, 3, 1);
508 AlignedCodonFrame acf = new AlignedCodonFrame();
509 acf.addMap(dna1.getDatasetSequence(), prot1.getDatasetSequence(), map);
510 acf.addMap(dna2.getDatasetSequence(), prot2.getDatasetSequence(), map);
511 acf.addMap(dna3.getDatasetSequence(), prot3.getDatasetSequence(), map);
512 ArrayList<AlignedCodonFrame> acfs = new ArrayList<>();
514 protein.setCodonFrames(acfs);
517 * Translated codon order is [1,2,3] [1,3,4] [4,5,6] [4,5,7] [5,6,7] [7,8,9]
518 * [8,9,10] [10,11,12] [11,12,13]
520 AlignmentUtils.alignProteinAsDna(protein, dna);
521 assertEquals("C-H--Y-Q-", prot1.getSequenceAsString());
522 assertEquals("-C--H-Y-Q", prot2.getSequenceAsString());
523 assertEquals("C--H--Y-Q", prot3.getSequenceAsString());
524 assertEquals("R-QSV", prot4.getSequenceAsString());
528 * Test the method that tests whether a CDNA sequence translates to a protein
531 @Test(groups = { "Functional" })
532 public void testTranslatesAs()
534 // null arguments check
535 assertFalse(AlignmentUtils.translatesAs(null, 0, null));
536 assertFalse(AlignmentUtils.translatesAs(new char[] { 't' }, 0, null));
537 assertFalse(AlignmentUtils.translatesAs(null, 0, new char[] { 'a' }));
539 // straight translation
540 assertTrue(AlignmentUtils.translatesAs("tttcccaaaggg".toCharArray(), 0,
541 "FPKG".toCharArray()));
542 // with extra start codon (not in protein)
543 assertTrue(AlignmentUtils.translatesAs("atgtttcccaaaggg".toCharArray(),
544 3, "FPKG".toCharArray()));
545 // with stop codon1 (not in protein)
546 assertTrue(AlignmentUtils.translatesAs("tttcccaaagggtaa".toCharArray(),
547 0, "FPKG".toCharArray()));
548 // with stop codon1 (in protein as *)
549 assertTrue(AlignmentUtils.translatesAs("tttcccaaagggtaa".toCharArray(),
550 0, "FPKG*".toCharArray()));
551 // with stop codon2 (not in protein)
552 assertTrue(AlignmentUtils.translatesAs("tttcccaaagggtag".toCharArray(),
553 0, "FPKG".toCharArray()));
554 // with stop codon3 (not in protein)
555 assertTrue(AlignmentUtils.translatesAs("tttcccaaagggtga".toCharArray(),
556 0, "FPKG".toCharArray()));
557 // with start and stop codon1
558 assertTrue(AlignmentUtils.translatesAs(
559 "atgtttcccaaagggtaa".toCharArray(), 3, "FPKG".toCharArray()));
560 // with start and stop codon1 (in protein as *)
561 assertTrue(AlignmentUtils.translatesAs(
562 "atgtttcccaaagggtaa".toCharArray(), 3, "FPKG*".toCharArray()));
563 // with start and stop codon2
564 assertTrue(AlignmentUtils.translatesAs(
565 "atgtttcccaaagggtag".toCharArray(), 3, "FPKG".toCharArray()));
566 // with start and stop codon3
567 assertTrue(AlignmentUtils.translatesAs(
568 "atgtttcccaaagggtga".toCharArray(), 3, "FPKG".toCharArray()));
570 // with embedded stop codons
571 assertTrue(AlignmentUtils.translatesAs(
572 "atgtttTAGcccaaaTAAgggtga".toCharArray(), 3,
573 "F*PK*G".toCharArray()));
576 assertFalse(AlignmentUtils.translatesAs("tttcccaaaggg".toCharArray(),
577 0, "FPMG".toCharArray()));
580 assertFalse(AlignmentUtils.translatesAs("tttcccaaagg".toCharArray(), 0,
581 "FPKG".toCharArray()));
584 assertFalse(AlignmentUtils.translatesAs("tttcccaaaggg".toCharArray(),
585 0, "FPK".toCharArray()));
587 // overlong dna (doesn't end in stop codon)
588 assertFalse(AlignmentUtils.translatesAs(
589 "tttcccaaagggttt".toCharArray(), 0, "FPKG".toCharArray()));
591 // dna + stop codon + more
592 assertFalse(AlignmentUtils.translatesAs(
593 "tttcccaaagggttaga".toCharArray(), 0, "FPKG".toCharArray()));
596 assertFalse(AlignmentUtils.translatesAs("tttcccaaaggg".toCharArray(),
597 0, "FPKGQ".toCharArray()));
601 * Test mapping of protein to cDNA, for cases where the cDNA has start and/or
602 * stop codons in addition to the protein coding sequence.
604 * @throws IOException
606 @Test(groups = { "Functional" })
607 public void testMapProteinAlignmentToCdna_withStartAndStopCodons()
610 List<SequenceI> protseqs = new ArrayList<>();
611 protseqs.add(new Sequence("UNIPROT|V12345", "EIQ"));
612 protseqs.add(new Sequence("UNIPROT|V12346", "EIQ"));
613 protseqs.add(new Sequence("UNIPROT|V12347", "SAR"));
614 AlignmentI protein = new Alignment(protseqs.toArray(new SequenceI[3]));
615 protein.setDataset(null);
617 List<SequenceI> dnaseqs = new ArrayList<>();
619 dnaseqs.add(new Sequence("EMBL|A11111", "ATGTCAGCACGC"));
621 dnaseqs.add(new Sequence("EMBL|A22222", "GAGATACAATAA"));
622 // = start +EIQ + stop
623 dnaseqs.add(new Sequence("EMBL|A33333", "ATGGAAATCCAGTAG"));
624 dnaseqs.add(new Sequence("EMBL|A44444", "GAAATTCAG"));
625 AlignmentI cdna = new Alignment(dnaseqs.toArray(new SequenceI[4]));
626 cdna.setDataset(null);
628 assertTrue(AlignmentUtils.mapProteinAlignmentToCdna(protein, cdna));
630 // 3 mappings made, each from 1 to 1 sequence
631 assertEquals(3, protein.getCodonFrames().size());
632 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).size());
633 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).size());
634 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(2)).size());
636 // V12345 mapped from A22222
637 AlignedCodonFrame acf = protein.getCodonFrame(protein.getSequenceAt(0))
639 assertEquals(1, acf.getdnaSeqs().length);
640 assertEquals(cdna.getSequenceAt(1).getDatasetSequence(),
641 acf.getdnaSeqs()[0]);
642 Mapping[] protMappings = acf.getProtMappings();
643 assertEquals(1, protMappings.length);
644 MapList mapList = protMappings[0].getMap();
645 assertEquals(3, mapList.getFromRatio());
646 assertEquals(1, mapList.getToRatio());
647 assertTrue(Arrays.equals(new int[] { 1, 9 }, mapList.getFromRanges()
649 assertEquals(1, mapList.getFromRanges().size());
650 assertTrue(Arrays.equals(new int[] { 1, 3 },
651 mapList.getToRanges().get(0)));
652 assertEquals(1, mapList.getToRanges().size());
654 // V12346 mapped from A33333 starting position 4
655 acf = protein.getCodonFrame(protein.getSequenceAt(1)).get(0);
656 assertEquals(1, acf.getdnaSeqs().length);
657 assertEquals(cdna.getSequenceAt(2).getDatasetSequence(),
658 acf.getdnaSeqs()[0]);
659 protMappings = acf.getProtMappings();
660 assertEquals(1, protMappings.length);
661 mapList = protMappings[0].getMap();
662 assertEquals(3, mapList.getFromRatio());
663 assertEquals(1, mapList.getToRatio());
664 assertTrue(Arrays.equals(new int[] { 4, 12 }, mapList.getFromRanges()
666 assertEquals(1, mapList.getFromRanges().size());
667 assertTrue(Arrays.equals(new int[] { 1, 3 },
668 mapList.getToRanges().get(0)));
669 assertEquals(1, mapList.getToRanges().size());
671 // V12347 mapped to A11111 starting position 4
672 acf = protein.getCodonFrame(protein.getSequenceAt(2)).get(0);
673 assertEquals(1, acf.getdnaSeqs().length);
674 assertEquals(cdna.getSequenceAt(0).getDatasetSequence(),
675 acf.getdnaSeqs()[0]);
676 protMappings = acf.getProtMappings();
677 assertEquals(1, protMappings.length);
678 mapList = protMappings[0].getMap();
679 assertEquals(3, mapList.getFromRatio());
680 assertEquals(1, mapList.getToRatio());
681 assertTrue(Arrays.equals(new int[] { 4, 12 }, mapList.getFromRanges()
683 assertEquals(1, mapList.getFromRanges().size());
684 assertTrue(Arrays.equals(new int[] { 1, 3 },
685 mapList.getToRanges().get(0)));
686 assertEquals(1, mapList.getToRanges().size());
688 // no mapping involving the 'extra' A44444
689 assertTrue(protein.getCodonFrame(cdna.getSequenceAt(3)).isEmpty());
693 * Test mapping of protein to cDNA, for the case where we have some sequence
694 * cross-references. Verify that 1-to-many mappings are made where
695 * cross-references exist and sequences are mappable.
697 * @throws IOException
699 @Test(groups = { "Functional" })
700 public void testMapProteinAlignmentToCdna_withXrefs() throws IOException
702 List<SequenceI> protseqs = new ArrayList<>();
703 protseqs.add(new Sequence("UNIPROT|V12345", "EIQ"));
704 protseqs.add(new Sequence("UNIPROT|V12346", "EIQ"));
705 protseqs.add(new Sequence("UNIPROT|V12347", "SAR"));
706 AlignmentI protein = new Alignment(protseqs.toArray(new SequenceI[3]));
707 protein.setDataset(null);
709 List<SequenceI> dnaseqs = new ArrayList<>();
710 dnaseqs.add(new Sequence("EMBL|A11111", "TCAGCACGC")); // = SAR
711 dnaseqs.add(new Sequence("EMBL|A22222", "ATGGAGATACAA")); // = start + EIQ
712 dnaseqs.add(new Sequence("EMBL|A33333", "GAAATCCAG")); // = EIQ
713 dnaseqs.add(new Sequence("EMBL|A44444", "GAAATTCAG")); // = EIQ
714 dnaseqs.add(new Sequence("EMBL|A55555", "GAGATTCAG")); // = EIQ
715 AlignmentI cdna = new Alignment(dnaseqs.toArray(new SequenceI[5]));
716 cdna.setDataset(null);
718 // Xref A22222 to V12345 (should get mapped)
719 dnaseqs.get(1).addDBRef(new DBRefEntry("UNIPROT", "1", "V12345"));
720 // Xref V12345 to A44444 (should get mapped)
721 protseqs.get(0).addDBRef(new DBRefEntry("EMBL", "1", "A44444"));
722 // Xref A33333 to V12347 (sequence mismatch - should not get mapped)
723 dnaseqs.get(2).addDBRef(new DBRefEntry("UNIPROT", "1", "V12347"));
724 // as V12345 is mapped to A22222 and A44444, this leaves V12346 unmapped.
725 // it should get paired up with the unmapped A33333
726 // A11111 should be mapped to V12347
727 // A55555 is spare and has no xref so is not mapped
729 assertTrue(AlignmentUtils.mapProteinAlignmentToCdna(protein, cdna));
731 // 4 protein mappings made for 3 proteins, 2 to V12345, 1 each to V12346/7
732 assertEquals(3, protein.getCodonFrames().size());
733 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).size());
734 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).size());
735 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(2)).size());
737 // one mapping for each of the first 4 cDNA sequences
738 assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(0)).size());
739 assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(1)).size());
740 assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(2)).size());
741 assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(3)).size());
743 // V12345 mapped to A22222 and A44444
744 AlignedCodonFrame acf = protein.getCodonFrame(protein.getSequenceAt(0))
746 assertEquals(2, acf.getdnaSeqs().length);
747 assertEquals(cdna.getSequenceAt(1).getDatasetSequence(),
748 acf.getdnaSeqs()[0]);
749 assertEquals(cdna.getSequenceAt(3).getDatasetSequence(),
750 acf.getdnaSeqs()[1]);
752 // V12346 mapped to A33333
753 acf = protein.getCodonFrame(protein.getSequenceAt(1)).get(0);
754 assertEquals(1, acf.getdnaSeqs().length);
755 assertEquals(cdna.getSequenceAt(2).getDatasetSequence(),
756 acf.getdnaSeqs()[0]);
758 // V12347 mapped to A11111
759 acf = protein.getCodonFrame(protein.getSequenceAt(2)).get(0);
760 assertEquals(1, acf.getdnaSeqs().length);
761 assertEquals(cdna.getSequenceAt(0).getDatasetSequence(),
762 acf.getdnaSeqs()[0]);
764 // no mapping involving the 'extra' A55555
765 assertTrue(protein.getCodonFrame(cdna.getSequenceAt(4)).isEmpty());
769 * Test mapping of protein to cDNA, for the case where we have some sequence
770 * cross-references. Verify that once we have made an xref mapping we don't
771 * also map un-xrefd sequeces.
773 * @throws IOException
775 @Test(groups = { "Functional" })
776 public void testMapProteinAlignmentToCdna_prioritiseXrefs()
779 List<SequenceI> protseqs = new ArrayList<>();
780 protseqs.add(new Sequence("UNIPROT|V12345", "EIQ"));
781 protseqs.add(new Sequence("UNIPROT|V12346", "EIQ"));
782 AlignmentI protein = new Alignment(
783 protseqs.toArray(new SequenceI[protseqs.size()]));
784 protein.setDataset(null);
786 List<SequenceI> dnaseqs = new ArrayList<>();
787 dnaseqs.add(new Sequence("EMBL|A11111", "GAAATCCAG")); // = EIQ
788 dnaseqs.add(new Sequence("EMBL|A22222", "GAAATTCAG")); // = EIQ
789 AlignmentI cdna = new Alignment(dnaseqs.toArray(new SequenceI[dnaseqs
791 cdna.setDataset(null);
793 // Xref A22222 to V12345 (should get mapped)
794 // A11111 should then be mapped to the unmapped V12346
795 dnaseqs.get(1).addDBRef(new DBRefEntry("UNIPROT", "1", "V12345"));
797 assertTrue(AlignmentUtils.mapProteinAlignmentToCdna(protein, cdna));
799 // 2 protein mappings made
800 assertEquals(2, protein.getCodonFrames().size());
801 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).size());
802 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).size());
804 // one mapping for each of the cDNA sequences
805 assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(0)).size());
806 assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(1)).size());
808 // V12345 mapped to A22222
809 AlignedCodonFrame acf = protein.getCodonFrame(protein.getSequenceAt(0))
811 assertEquals(1, acf.getdnaSeqs().length);
812 assertEquals(cdna.getSequenceAt(1).getDatasetSequence(),
813 acf.getdnaSeqs()[0]);
815 // V12346 mapped to A11111
816 acf = protein.getCodonFrame(protein.getSequenceAt(1)).get(0);
817 assertEquals(1, acf.getdnaSeqs().length);
818 assertEquals(cdna.getSequenceAt(0).getDatasetSequence(),
819 acf.getdnaSeqs()[0]);
823 * Test the method that shows or hides sequence annotations by type(s) and
826 @Test(groups = { "Functional" })
827 public void testShowOrHideSequenceAnnotations()
829 SequenceI seq1 = new Sequence("Seq1", "AAA");
830 SequenceI seq2 = new Sequence("Seq2", "BBB");
831 SequenceI seq3 = new Sequence("Seq3", "CCC");
832 Annotation[] anns = new Annotation[] { new Annotation(2f) };
833 AlignmentAnnotation ann1 = new AlignmentAnnotation("Structure", "ann1",
835 ann1.setSequenceRef(seq1);
836 AlignmentAnnotation ann2 = new AlignmentAnnotation("Structure", "ann2",
838 ann2.setSequenceRef(seq2);
839 AlignmentAnnotation ann3 = new AlignmentAnnotation("Structure", "ann3",
841 AlignmentAnnotation ann4 = new AlignmentAnnotation("Temp", "ann4", anns);
842 ann4.setSequenceRef(seq1);
843 AlignmentAnnotation ann5 = new AlignmentAnnotation("Temp", "ann5", anns);
844 ann5.setSequenceRef(seq2);
845 AlignmentAnnotation ann6 = new AlignmentAnnotation("Temp", "ann6", anns);
846 AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2, seq3 });
847 al.addAnnotation(ann1); // Structure for Seq1
848 al.addAnnotation(ann2); // Structure for Seq2
849 al.addAnnotation(ann3); // Structure for no sequence
850 al.addAnnotation(ann4); // Temp for seq1
851 al.addAnnotation(ann5); // Temp for seq2
852 al.addAnnotation(ann6); // Temp for no sequence
853 List<String> types = new ArrayList<>();
854 List<SequenceI> scope = new ArrayList<>();
857 * Set all sequence related Structure to hidden (ann1, ann2)
859 types.add("Structure");
860 AlignmentUtils.showOrHideSequenceAnnotations(al, types, null, false,
862 assertFalse(ann1.visible);
863 assertFalse(ann2.visible);
864 assertTrue(ann3.visible); // not sequence-related, not affected
865 assertTrue(ann4.visible); // not Structure, not affected
866 assertTrue(ann5.visible); // "
867 assertTrue(ann6.visible); // not sequence-related, not affected
870 * Set Temp in {seq1, seq3} to hidden
876 AlignmentUtils.showOrHideSequenceAnnotations(al, types, scope, false,
878 assertFalse(ann1.visible); // unchanged
879 assertFalse(ann2.visible); // unchanged
880 assertTrue(ann3.visible); // not sequence-related, not affected
881 assertFalse(ann4.visible); // Temp for seq1 hidden
882 assertTrue(ann5.visible); // not in scope, not affected
883 assertTrue(ann6.visible); // not sequence-related, not affected
886 * Set Temp in all sequences to hidden
892 AlignmentUtils.showOrHideSequenceAnnotations(al, types, null, false,
894 assertFalse(ann1.visible); // unchanged
895 assertFalse(ann2.visible); // unchanged
896 assertTrue(ann3.visible); // not sequence-related, not affected
897 assertFalse(ann4.visible); // Temp for seq1 hidden
898 assertFalse(ann5.visible); // Temp for seq2 hidden
899 assertTrue(ann6.visible); // not sequence-related, not affected
902 * Set all types in {seq1, seq3} to visible
908 AlignmentUtils.showOrHideSequenceAnnotations(al, types, scope, true,
910 assertTrue(ann1.visible); // Structure for seq1 set visible
911 assertFalse(ann2.visible); // not in scope, unchanged
912 assertTrue(ann3.visible); // not sequence-related, not affected
913 assertTrue(ann4.visible); // Temp for seq1 set visible
914 assertFalse(ann5.visible); // not in scope, unchanged
915 assertTrue(ann6.visible); // not sequence-related, not affected
918 * Set all types in all scope to hidden
920 AlignmentUtils.showOrHideSequenceAnnotations(al, types, null, true,
922 assertFalse(ann1.visible);
923 assertFalse(ann2.visible);
924 assertTrue(ann3.visible); // not sequence-related, not affected
925 assertFalse(ann4.visible);
926 assertFalse(ann5.visible);
927 assertTrue(ann6.visible); // not sequence-related, not affected
931 * Tests for the method that checks if one sequence cross-references another
933 @Test(groups = { "Functional" })
934 public void testHasCrossRef()
936 assertFalse(AlignmentUtils.hasCrossRef(null, null));
937 SequenceI seq1 = new Sequence("EMBL|A12345", "ABCDEF");
938 assertFalse(AlignmentUtils.hasCrossRef(seq1, null));
939 assertFalse(AlignmentUtils.hasCrossRef(null, seq1));
940 SequenceI seq2 = new Sequence("UNIPROT|V20192", "ABCDEF");
941 assertFalse(AlignmentUtils.hasCrossRef(seq1, seq2));
944 seq1.addDBRef(new DBRefEntry("UNIPROT", "1", "v20193"));
945 assertFalse(AlignmentUtils.hasCrossRef(seq1, seq2));
947 // case-insensitive; version number is ignored
948 seq1.addDBRef(new DBRefEntry("UNIPROT", "1", "v20192"));
949 assertTrue(AlignmentUtils.hasCrossRef(seq1, seq2));
952 seq1.addDBRef(new DBRefEntry("UNIPROT", "1", "V20192"));
953 assertTrue(AlignmentUtils.hasCrossRef(seq1, seq2));
954 // test is one-way only
955 assertFalse(AlignmentUtils.hasCrossRef(seq2, seq1));
959 * Tests for the method that checks if either sequence cross-references the
962 @Test(groups = { "Functional" })
963 public void testHaveCrossRef()
965 assertFalse(AlignmentUtils.hasCrossRef(null, null));
966 SequenceI seq1 = new Sequence("EMBL|A12345", "ABCDEF");
967 assertFalse(AlignmentUtils.haveCrossRef(seq1, null));
968 assertFalse(AlignmentUtils.haveCrossRef(null, seq1));
969 SequenceI seq2 = new Sequence("UNIPROT|V20192", "ABCDEF");
970 assertFalse(AlignmentUtils.haveCrossRef(seq1, seq2));
972 seq1.addDBRef(new DBRefEntry("UNIPROT", "1", "V20192"));
973 assertTrue(AlignmentUtils.haveCrossRef(seq1, seq2));
974 // next is true for haveCrossRef, false for hasCrossRef
975 assertTrue(AlignmentUtils.haveCrossRef(seq2, seq1));
977 // now the other way round
978 seq1.setDBRefs(null);
979 seq2.addDBRef(new DBRefEntry("EMBL", "1", "A12345"));
980 assertTrue(AlignmentUtils.haveCrossRef(seq1, seq2));
981 assertTrue(AlignmentUtils.haveCrossRef(seq2, seq1));
984 seq1.addDBRef(new DBRefEntry("UNIPROT", "1", "V20192"));
985 assertTrue(AlignmentUtils.haveCrossRef(seq1, seq2));
986 assertTrue(AlignmentUtils.haveCrossRef(seq2, seq1));
990 * Test the method that extracts the cds-only part of a dna alignment.
992 @Test(groups = { "Functional" })
993 public void testMakeCdsAlignment()
997 * dna1 --> [4, 6] [10,12] --> pep1
998 * dna2 --> [1, 3] [7, 9] [13,15] --> pep2
1000 SequenceI dna1 = new Sequence("dna1", "aaaGGGcccTTTaaa");
1001 SequenceI dna2 = new Sequence("dna2", "GGGcccTTTaaaCCC");
1002 SequenceI pep1 = new Sequence("pep1", "GF");
1003 SequenceI pep2 = new Sequence("pep2", "GFP");
1004 pep1.addDBRef(new DBRefEntry("UNIPROT", "0", "pep1"));
1005 pep2.addDBRef(new DBRefEntry("UNIPROT", "0", "pep2"));
1006 dna1.createDatasetSequence();
1007 dna2.createDatasetSequence();
1008 pep1.createDatasetSequence();
1009 pep2.createDatasetSequence();
1010 AlignmentI dna = new Alignment(new SequenceI[] { dna1, dna2 });
1011 dna.setDataset(null);
1014 * put a variant feature on dna2 base 8
1015 * - should transfer to cds2 base 5
1017 dna2.addSequenceFeature(new SequenceFeature("variant", "hgmd", 8, 8,
1021 * need a sourceDbRef if we are to construct dbrefs to the CDS
1022 * sequence from the dna contig sequences
1024 DBRefEntry dbref = new DBRefEntry("ENSEMBL", "0", "dna1");
1025 dna1.getDatasetSequence().addDBRef(dbref);
1026 org.testng.Assert.assertEquals(dbref, dna1.getPrimaryDBRefs().get(0));
1027 dbref = new DBRefEntry("ENSEMBL", "0", "dna2");
1028 dna2.getDatasetSequence().addDBRef(dbref);
1029 org.testng.Assert.assertEquals(dbref, dna2.getPrimaryDBRefs().get(0));
1032 * CDS sequences are 'discovered' from dna-to-protein mappings on the alignment
1033 * dataset (e.g. added from dbrefs by CrossRef.findXrefSequences)
1035 MapList mapfordna1 = new MapList(new int[] { 4, 6, 10, 12 }, new int[] {
1037 AlignedCodonFrame acf = new AlignedCodonFrame();
1038 acf.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(),
1040 dna.addCodonFrame(acf);
1041 MapList mapfordna2 = new MapList(new int[] { 1, 3, 7, 9, 13, 15 },
1042 new int[] { 1, 3 }, 3, 1);
1043 acf = new AlignedCodonFrame();
1044 acf.addMap(dna2.getDatasetSequence(), pep2.getDatasetSequence(),
1046 dna.addCodonFrame(acf);
1049 * In this case, mappings originally came from matching Uniprot accessions
1050 * - so need an xref on dna involving those regions.
1051 * These are normally constructed from CDS annotation
1053 DBRefEntry dna1xref = new DBRefEntry("UNIPROT", "ENSEMBL", "pep1",
1054 new Mapping(mapfordna1));
1055 dna1.addDBRef(dna1xref);
1056 assertEquals(2, dna1.getDBRefs().size()); // to self and to pep1
1057 DBRefEntry dna2xref = new DBRefEntry("UNIPROT", "ENSEMBL", "pep2",
1058 new Mapping(mapfordna2));
1059 dna2.addDBRef(dna2xref);
1060 assertEquals(2, dna2.getDBRefs().size()); // to self and to pep2
1063 * execute method under test:
1065 AlignmentI cds = AlignmentUtils.makeCdsAlignment(new SequenceI[] {
1066 dna1, dna2 }, dna.getDataset(), null);
1069 * verify cds sequences
1071 assertEquals(2, cds.getSequences().size());
1072 assertEquals("GGGTTT", cds.getSequenceAt(0).getSequenceAsString());
1073 assertEquals("GGGTTTCCC", cds.getSequenceAt(1).getSequenceAsString());
1076 * verify shared, extended alignment dataset
1078 assertSame(dna.getDataset(), cds.getDataset());
1079 SequenceI cds1Dss = cds.getSequenceAt(0).getDatasetSequence();
1080 SequenceI cds2Dss = cds.getSequenceAt(1).getDatasetSequence();
1081 assertTrue(dna.getDataset().getSequences().contains(cds1Dss));
1082 assertTrue(dna.getDataset().getSequences().contains(cds2Dss));
1085 * verify CDS has a dbref with mapping to peptide
1087 assertNotNull(cds1Dss.getDBRefs());
1088 assertEquals(2, cds1Dss.getDBRefs().size());
1089 dbref = cds1Dss.getDBRefs().get(0);
1090 assertEquals(dna1xref.getSource(), dbref.getSource());
1091 // version is via ensembl's primary ref
1092 assertEquals(dna1xref.getVersion(), dbref.getVersion());
1093 assertEquals(dna1xref.getAccessionId(), dbref.getAccessionId());
1094 assertNotNull(dbref.getMap());
1095 assertSame(pep1.getDatasetSequence(), dbref.getMap().getTo());
1096 MapList cdsMapping = new MapList(new int[] { 1, 6 },
1097 new int[] { 1, 2 }, 3, 1);
1098 assertEquals(cdsMapping, dbref.getMap().getMap());
1101 * verify peptide has added a dbref with reverse mapping to CDS
1103 assertNotNull(pep1.getDBRefs());
1104 // FIXME pep1.getDBRefs() is 1 - is that the correct behaviour ?
1105 assertEquals(2, pep1.getDBRefs().size());
1106 dbref = pep1.getDBRefs().get(1);
1107 assertEquals("ENSEMBL", dbref.getSource());
1108 assertEquals("0", dbref.getVersion());
1109 assertEquals("CDS|dna1", dbref.getAccessionId());
1110 assertNotNull(dbref.getMap());
1111 assertSame(cds1Dss, dbref.getMap().getTo());
1112 assertEquals(cdsMapping.getInverse(), dbref.getMap().getMap());
1115 * verify cDNA has added a dbref with mapping to CDS
1117 assertEquals(3, dna1.getDBRefs().size());
1118 DBRefEntry dbRefEntry = dna1.getDBRefs().get(2);
1119 assertSame(cds1Dss, dbRefEntry.getMap().getTo());
1120 MapList dnaToCdsMapping = new MapList(new int[] { 4, 6, 10, 12 },
1121 new int[] { 1, 6 }, 1, 1);
1122 assertEquals(dnaToCdsMapping, dbRefEntry.getMap().getMap());
1123 assertEquals(3, dna2.getDBRefs().size());
1124 dbRefEntry = dna2.getDBRefs().get(2);
1125 assertSame(cds2Dss, dbRefEntry.getMap().getTo());
1126 dnaToCdsMapping = new MapList(new int[] { 1, 3, 7, 9, 13, 15 },
1127 new int[] { 1, 9 }, 1, 1);
1128 assertEquals(dnaToCdsMapping, dbRefEntry.getMap().getMap());
1131 * verify CDS has added a dbref with mapping to cDNA
1133 assertEquals(2, cds1Dss.getDBRefs().size());
1134 dbRefEntry = cds1Dss.getDBRefs().get(1);
1135 assertSame(dna1.getDatasetSequence(), dbRefEntry.getMap().getTo());
1136 MapList cdsToDnaMapping = new MapList(new int[] { 1, 6 }, new int[] {
1137 4, 6, 10, 12 }, 1, 1);
1138 assertEquals(cdsToDnaMapping, dbRefEntry.getMap().getMap());
1139 assertEquals(2, cds2Dss.getDBRefs().size());
1140 dbRefEntry = cds2Dss.getDBRefs().get(1);
1141 assertSame(dna2.getDatasetSequence(), dbRefEntry.getMap().getTo());
1142 cdsToDnaMapping = new MapList(new int[] { 1, 9 }, new int[] { 1, 3, 7,
1144 assertEquals(cdsToDnaMapping, dbRefEntry.getMap().getMap());
1147 * Verify mappings from CDS to peptide, cDNA to CDS, and cDNA to peptide
1148 * the mappings are on the shared alignment dataset
1149 * 6 mappings, 2*(DNA->CDS), 2*(DNA->Pep), 2*(CDS->Pep)
1151 List<AlignedCodonFrame> cdsMappings = cds.getDataset().getCodonFrames();
1152 assertEquals(6, cdsMappings.size());
1155 * verify that mapping sets for dna and cds alignments are different
1156 * [not current behaviour - all mappings are on the alignment dataset]
1158 // select -> subselect type to test.
1159 // Assert.assertNotSame(dna.getCodonFrames(), cds.getCodonFrames());
1160 // assertEquals(4, dna.getCodonFrames().size());
1161 // assertEquals(4, cds.getCodonFrames().size());
1164 * Two mappings involve pep1 (dna to pep1, cds to pep1)
1165 * Mapping from pep1 to GGGTTT in first new exon sequence
1167 List<AlignedCodonFrame> pep1Mappings = MappingUtils
1168 .findMappingsForSequence(pep1, cdsMappings);
1169 assertEquals(2, pep1Mappings.size());
1170 List<AlignedCodonFrame> mappings = MappingUtils
1171 .findMappingsForSequence(cds.getSequenceAt(0), pep1Mappings);
1172 assertEquals(1, mappings.size());
1175 SearchResultsI sr = MappingUtils.buildSearchResults(pep1, 1, mappings);
1176 assertEquals(1, sr.getResults().size());
1177 SearchResultMatchI m = sr.getResults().get(0);
1178 assertSame(cds1Dss, m.getSequence());
1179 assertEquals(1, m.getStart());
1180 assertEquals(3, m.getEnd());
1182 sr = MappingUtils.buildSearchResults(pep1, 2, mappings);
1183 m = sr.getResults().get(0);
1184 assertSame(cds1Dss, m.getSequence());
1185 assertEquals(4, m.getStart());
1186 assertEquals(6, m.getEnd());
1189 * Two mappings involve pep2 (dna to pep2, cds to pep2)
1190 * Verify mapping from pep2 to GGGTTTCCC in second new exon sequence
1192 List<AlignedCodonFrame> pep2Mappings = MappingUtils
1193 .findMappingsForSequence(pep2, cdsMappings);
1194 assertEquals(2, pep2Mappings.size());
1195 mappings = MappingUtils.findMappingsForSequence(cds.getSequenceAt(1),
1197 assertEquals(1, mappings.size());
1199 sr = MappingUtils.buildSearchResults(pep2, 1, mappings);
1200 assertEquals(1, sr.getResults().size());
1201 m = sr.getResults().get(0);
1202 assertSame(cds2Dss, m.getSequence());
1203 assertEquals(1, m.getStart());
1204 assertEquals(3, m.getEnd());
1206 sr = MappingUtils.buildSearchResults(pep2, 2, mappings);
1207 m = sr.getResults().get(0);
1208 assertSame(cds2Dss, m.getSequence());
1209 assertEquals(4, m.getStart());
1210 assertEquals(6, m.getEnd());
1212 sr = MappingUtils.buildSearchResults(pep2, 3, mappings);
1213 m = sr.getResults().get(0);
1214 assertSame(cds2Dss, m.getSequence());
1215 assertEquals(7, m.getStart());
1216 assertEquals(9, m.getEnd());
1219 * check cds2 acquired a variant feature in position 5
1221 List<SequenceFeature> sfs = cds2Dss.getSequenceFeatures();
1223 assertEquals(1, sfs.size());
1224 assertEquals("variant", sfs.get(0).type);
1225 assertEquals(5, sfs.get(0).begin);
1226 assertEquals(5, sfs.get(0).end);
1230 * Test the method that makes a cds-only alignment from a DNA sequence and its
1231 * product mappings, for the case where there are multiple exon mappings to
1232 * different protein products.
1234 @Test(groups = { "Functional" })
1235 public void testMakeCdsAlignment_multipleProteins()
1237 SequenceI dna1 = new Sequence("dna1", "aaaGGGcccTTTaaa");
1238 SequenceI pep1 = new Sequence("pep1", "GF"); // GGGTTT
1239 SequenceI pep2 = new Sequence("pep2", "KP"); // aaaccc
1240 SequenceI pep3 = new Sequence("pep3", "KF"); // aaaTTT
1241 dna1.createDatasetSequence();
1242 pep1.createDatasetSequence();
1243 pep2.createDatasetSequence();
1244 pep3.createDatasetSequence();
1245 pep1.getDatasetSequence().addDBRef(
1246 new DBRefEntry("EMBLCDS", "2", "A12345"));
1247 pep2.getDatasetSequence().addDBRef(
1248 new DBRefEntry("EMBLCDS", "3", "A12346"));
1249 pep3.getDatasetSequence().addDBRef(
1250 new DBRefEntry("EMBLCDS", "4", "A12347"));
1253 * Create the CDS alignment
1255 AlignmentI dna = new Alignment(new SequenceI[] { dna1 });
1256 dna.setDataset(null);
1259 * Make the mappings from dna to protein
1261 // map ...GGG...TTT to GF
1262 MapList map = new MapList(new int[] { 4, 6, 10, 12 },
1263 new int[] { 1, 2 }, 3, 1);
1264 AlignedCodonFrame acf = new AlignedCodonFrame();
1265 acf.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(), map);
1266 dna.addCodonFrame(acf);
1268 // map aaa...ccc to KP
1269 map = new MapList(new int[] { 1, 3, 7, 9 }, new int[] { 1, 2 }, 3, 1);
1270 acf = new AlignedCodonFrame();
1271 acf.addMap(dna1.getDatasetSequence(), pep2.getDatasetSequence(), map);
1272 dna.addCodonFrame(acf);
1274 // map aaa......TTT to KF
1275 map = new MapList(new int[] { 1, 3, 10, 12 }, new int[] { 1, 2 }, 3, 1);
1276 acf = new AlignedCodonFrame();
1277 acf.addMap(dna1.getDatasetSequence(), pep3.getDatasetSequence(), map);
1278 dna.addCodonFrame(acf);
1281 * execute method under test
1283 AlignmentI cdsal = AlignmentUtils.makeCdsAlignment(
1284 new SequenceI[] { dna1 }, dna.getDataset(), null);
1287 * Verify we have 3 cds sequences, mapped to pep1/2/3 respectively
1289 List<SequenceI> cds = cdsal.getSequences();
1290 assertEquals(3, cds.size());
1293 * verify shared, extended alignment dataset
1295 assertSame(cdsal.getDataset(), dna.getDataset());
1296 assertTrue(dna.getDataset().getSequences()
1297 .contains(cds.get(0).getDatasetSequence()));
1298 assertTrue(dna.getDataset().getSequences()
1299 .contains(cds.get(1).getDatasetSequence()));
1300 assertTrue(dna.getDataset().getSequences()
1301 .contains(cds.get(2).getDatasetSequence()));
1304 * verify aligned cds sequences and their xrefs
1306 SequenceI cdsSeq = cds.get(0);
1307 assertEquals("GGGTTT", cdsSeq.getSequenceAsString());
1308 // assertEquals("dna1|A12345", cdsSeq.getName());
1309 assertEquals("CDS|dna1", cdsSeq.getName());
1310 // assertEquals(1, cdsSeq.getDBRefs().length);
1311 // DBRefEntry cdsRef = cdsSeq.getDBRefs()[0];
1312 // assertEquals("EMBLCDS", cdsRef.getSource());
1313 // assertEquals("2", cdsRef.getVersion());
1314 // assertEquals("A12345", cdsRef.getAccessionId());
1316 cdsSeq = cds.get(1);
1317 assertEquals("aaaccc", cdsSeq.getSequenceAsString());
1318 // assertEquals("dna1|A12346", cdsSeq.getName());
1319 assertEquals("CDS|dna1", cdsSeq.getName());
1320 // assertEquals(1, cdsSeq.getDBRefs().length);
1321 // cdsRef = cdsSeq.getDBRefs()[0];
1322 // assertEquals("EMBLCDS", cdsRef.getSource());
1323 // assertEquals("3", cdsRef.getVersion());
1324 // assertEquals("A12346", cdsRef.getAccessionId());
1326 cdsSeq = cds.get(2);
1327 assertEquals("aaaTTT", cdsSeq.getSequenceAsString());
1328 // assertEquals("dna1|A12347", cdsSeq.getName());
1329 assertEquals("CDS|dna1", cdsSeq.getName());
1330 // assertEquals(1, cdsSeq.getDBRefs().length);
1331 // cdsRef = cdsSeq.getDBRefs()[0];
1332 // assertEquals("EMBLCDS", cdsRef.getSource());
1333 // assertEquals("4", cdsRef.getVersion());
1334 // assertEquals("A12347", cdsRef.getAccessionId());
1337 * Verify there are mappings from each cds sequence to its protein product
1338 * and also to its dna source
1340 List<AlignedCodonFrame> newMappings = cdsal.getCodonFrames();
1343 * 6 mappings involve dna1 (to pep1/2/3, cds1/2/3)
1345 List<AlignedCodonFrame> dnaMappings = MappingUtils
1346 .findMappingsForSequence(dna1, newMappings);
1347 assertEquals(6, dnaMappings.size());
1352 List<AlignedCodonFrame> mappings = MappingUtils
1353 .findMappingsForSequence(pep1, dnaMappings);
1354 assertEquals(1, mappings.size());
1355 assertEquals(1, mappings.get(0).getMappings().size());
1356 assertSame(pep1.getDatasetSequence(), mappings.get(0).getMappings()
1357 .get(0).getMapping().getTo());
1362 List<AlignedCodonFrame> dnaToCds1Mappings = MappingUtils
1363 .findMappingsForSequence(cds.get(0), dnaMappings);
1364 Mapping mapping = dnaToCds1Mappings.get(0).getMappings().get(0)
1366 assertSame(cds.get(0).getDatasetSequence(), mapping.getTo());
1367 assertEquals("G(1) in CDS should map to G(4) in DNA", 4, mapping
1368 .getMap().getToPosition(1));
1373 mappings = MappingUtils.findMappingsForSequence(pep2, dnaMappings);
1374 assertEquals(1, mappings.size());
1375 assertEquals(1, mappings.get(0).getMappings().size());
1376 assertSame(pep2.getDatasetSequence(), mappings.get(0).getMappings()
1377 .get(0).getMapping().getTo());
1382 List<AlignedCodonFrame> dnaToCds2Mappings = MappingUtils
1383 .findMappingsForSequence(cds.get(1), dnaMappings);
1384 mapping = dnaToCds2Mappings.get(0).getMappings().get(0).getMapping();
1385 assertSame(cds.get(1).getDatasetSequence(), mapping.getTo());
1386 assertEquals("c(4) in CDS should map to c(7) in DNA", 7, mapping
1387 .getMap().getToPosition(4));
1392 mappings = MappingUtils.findMappingsForSequence(pep3, dnaMappings);
1393 assertEquals(1, mappings.size());
1394 assertEquals(1, mappings.get(0).getMappings().size());
1395 assertSame(pep3.getDatasetSequence(), mappings.get(0).getMappings()
1396 .get(0).getMapping().getTo());
1401 List<AlignedCodonFrame> dnaToCds3Mappings = MappingUtils
1402 .findMappingsForSequence(cds.get(2), dnaMappings);
1403 mapping = dnaToCds3Mappings.get(0).getMappings().get(0).getMapping();
1404 assertSame(cds.get(2).getDatasetSequence(), mapping.getTo());
1405 assertEquals("T(4) in CDS should map to T(10) in DNA", 10, mapping
1406 .getMap().getToPosition(4));
1409 @Test(groups = { "Functional" })
1410 public void testIsMappable()
1412 SequenceI dna1 = new Sequence("dna1", "cgCAGtgGT");
1413 SequenceI aa1 = new Sequence("aa1", "RSG");
1414 AlignmentI al1 = new Alignment(new SequenceI[] { dna1 });
1415 AlignmentI al2 = new Alignment(new SequenceI[] { aa1 });
1417 assertFalse(AlignmentUtils.isMappable(null, null));
1418 assertFalse(AlignmentUtils.isMappable(al1, null));
1419 assertFalse(AlignmentUtils.isMappable(null, al1));
1420 assertFalse(AlignmentUtils.isMappable(al1, al1));
1421 assertFalse(AlignmentUtils.isMappable(al2, al2));
1423 assertTrue(AlignmentUtils.isMappable(al1, al2));
1424 assertTrue(AlignmentUtils.isMappable(al2, al1));
1428 * Test creating a mapping when the sequences involved do not start at residue
1431 * @throws IOException
1433 @Test(groups = { "Functional" })
1434 public void testMapCdnaToProtein_forSubsequence() throws IOException
1436 SequenceI prot = new Sequence("UNIPROT|V12345", "E-I--Q", 10, 12);
1437 prot.createDatasetSequence();
1439 SequenceI dna = new Sequence("EMBL|A33333", "GAA--AT-C-CAG", 40, 48);
1440 dna.createDatasetSequence();
1442 MapList map = AlignmentUtils.mapCdnaToProtein(prot, dna);
1443 assertEquals(10, map.getToLowest());
1444 assertEquals(12, map.getToHighest());
1445 assertEquals(40, map.getFromLowest());
1446 assertEquals(48, map.getFromHighest());
1450 * Test for the alignSequenceAs method where we have protein mapped to protein
1452 @Test(groups = { "Functional" })
1453 public void testAlignSequenceAs_mappedProteinProtein()
1456 SequenceI alignMe = new Sequence("Match", "MGAASEV");
1457 alignMe.createDatasetSequence();
1458 SequenceI alignFrom = new Sequence("Query", "LQTGYMGAASEVMFSPTRR");
1459 alignFrom.createDatasetSequence();
1461 AlignedCodonFrame acf = new AlignedCodonFrame();
1462 // this is like a domain or motif match of part of a peptide sequence
1463 MapList map = new MapList(new int[] { 6, 12 }, new int[] { 1, 7 }, 1, 1);
1464 acf.addMap(alignFrom.getDatasetSequence(),
1465 alignMe.getDatasetSequence(), map);
1467 AlignmentUtils.alignSequenceAs(alignMe, alignFrom, acf, "-", '-', true,
1469 assertEquals("-----MGAASEV-------", alignMe.getSequenceAsString());
1473 * Test for the alignSequenceAs method where there are trailing unmapped
1474 * residues in the model sequence
1476 @Test(groups = { "Functional" })
1477 public void testAlignSequenceAs_withTrailingPeptide()
1479 // map first 3 codons to KPF; G is a trailing unmapped residue
1480 MapList map = new MapList(new int[] { 1, 9 }, new int[] { 1, 3 }, 3, 1);
1482 checkAlignSequenceAs("AAACCCTTT", "K-PFG", true, true, map,
1487 * Tests for transferring features between mapped sequences
1489 @Test(groups = { "Functional" })
1490 public void testTransferFeatures()
1492 SequenceI dna = new Sequence("dna/20-34", "acgTAGcaaGCCcgt");
1493 SequenceI cds = new Sequence("cds/10-15", "TAGGCC");
1496 dna.addSequenceFeature(new SequenceFeature("type1", "desc1", 1, 2, 1f,
1498 // partial overlap - to [1, 1]
1499 dna.addSequenceFeature(new SequenceFeature("type2", "desc2", 3, 4, 2f,
1501 // exact overlap - to [1, 3]
1502 dna.addSequenceFeature(new SequenceFeature("type3", "desc3", 4, 6, 3f,
1504 // spanning overlap - to [2, 5]
1505 dna.addSequenceFeature(new SequenceFeature("type4", "desc4", 5, 11, 4f,
1507 // exactly overlaps whole mapped range [1, 6]
1508 dna.addSequenceFeature(new SequenceFeature("type5", "desc5", 4, 12, 5f,
1510 // no overlap (internal)
1511 dna.addSequenceFeature(new SequenceFeature("type6", "desc6", 7, 9, 6f,
1513 // no overlap (3' end)
1514 dna.addSequenceFeature(new SequenceFeature("type7", "desc7", 13, 15,
1516 // overlap (3' end) - to [6, 6]
1517 dna.addSequenceFeature(new SequenceFeature("type8", "desc8", 12, 12,
1519 // extended overlap - to [6, +]
1520 dna.addSequenceFeature(new SequenceFeature("type9", "desc9", 12, 13,
1523 MapList map = new MapList(new int[] { 4, 6, 10, 12 },
1524 new int[] { 1, 6 }, 1, 1);
1527 * transferFeatures() will build 'partial overlap' for regions
1528 * that partially overlap 5' or 3' (start or end) of target sequence
1530 AlignmentUtils.transferFeatures(dna, cds, map, null);
1531 List<SequenceFeature> sfs = cds.getSequenceFeatures();
1532 assertEquals(6, sfs.size());
1534 SequenceFeature sf = sfs.get(0);
1535 assertEquals("type2", sf.getType());
1536 assertEquals("desc2", sf.getDescription());
1537 assertEquals(2f, sf.getScore());
1538 assertEquals(1, sf.getBegin());
1539 assertEquals(1, sf.getEnd());
1542 assertEquals("type3", sf.getType());
1543 assertEquals("desc3", sf.getDescription());
1544 assertEquals(3f, sf.getScore());
1545 assertEquals(1, sf.getBegin());
1546 assertEquals(3, sf.getEnd());
1549 assertEquals("type4", sf.getType());
1550 assertEquals(2, sf.getBegin());
1551 assertEquals(5, sf.getEnd());
1554 assertEquals("type5", sf.getType());
1555 assertEquals(1, sf.getBegin());
1556 assertEquals(6, sf.getEnd());
1559 assertEquals("type8", sf.getType());
1560 assertEquals(6, sf.getBegin());
1561 assertEquals(6, sf.getEnd());
1564 assertEquals("type9", sf.getType());
1565 assertEquals(6, sf.getBegin());
1566 assertEquals(6, sf.getEnd());
1570 * Tests for transferring features between mapped sequences
1572 @Test(groups = { "Functional" })
1573 public void testTransferFeatures_withOmit()
1575 SequenceI dna = new Sequence("dna/20-34", "acgTAGcaaGCCcgt");
1576 SequenceI cds = new Sequence("cds/10-15", "TAGGCC");
1578 MapList map = new MapList(new int[] { 4, 6, 10, 12 },
1579 new int[] { 1, 6 }, 1, 1);
1581 // [5, 11] maps to [2, 5]
1582 dna.addSequenceFeature(new SequenceFeature("type4", "desc4", 5, 11, 4f,
1584 // [4, 12] maps to [1, 6]
1585 dna.addSequenceFeature(new SequenceFeature("type5", "desc5", 4, 12, 5f,
1587 // [12, 12] maps to [6, 6]
1588 dna.addSequenceFeature(new SequenceFeature("type8", "desc8", 12, 12,
1591 // desc4 and desc8 are the 'omit these' varargs
1592 AlignmentUtils.transferFeatures(dna, cds, map, null, "type4", "type8");
1593 List<SequenceFeature> sfs = cds.getSequenceFeatures();
1594 assertEquals(1, sfs.size());
1596 SequenceFeature sf = sfs.get(0);
1597 assertEquals("type5", sf.getType());
1598 assertEquals(1, sf.getBegin());
1599 assertEquals(6, sf.getEnd());
1603 * Tests for transferring features between mapped sequences
1605 @Test(groups = { "Functional" })
1606 public void testTransferFeatures_withSelect()
1608 SequenceI dna = new Sequence("dna/20-34", "acgTAGcaaGCCcgt");
1609 SequenceI cds = new Sequence("cds/10-15", "TAGGCC");
1611 MapList map = new MapList(new int[] { 4, 6, 10, 12 },
1612 new int[] { 1, 6 }, 1, 1);
1614 // [5, 11] maps to [2, 5]
1615 dna.addSequenceFeature(new SequenceFeature("type4", "desc4", 5, 11, 4f,
1617 // [4, 12] maps to [1, 6]
1618 dna.addSequenceFeature(new SequenceFeature("type5", "desc5", 4, 12, 5f,
1620 // [12, 12] maps to [6, 6]
1621 dna.addSequenceFeature(new SequenceFeature("type8", "desc8", 12, 12,
1624 // "type5" is the 'select this type' argument
1625 AlignmentUtils.transferFeatures(dna, cds, map, "type5");
1626 List<SequenceFeature> sfs = cds.getSequenceFeatures();
1627 assertEquals(1, sfs.size());
1629 SequenceFeature sf = sfs.get(0);
1630 assertEquals("type5", sf.getType());
1631 assertEquals(1, sf.getBegin());
1632 assertEquals(6, sf.getEnd());
1636 * Test the method that extracts the cds-only part of a dna alignment, for the
1637 * case where the cds should be aligned to match its nucleotide sequence.
1639 @Test(groups = { "Functional" })
1640 public void testMakeCdsAlignment_alternativeTranscripts()
1642 SequenceI dna1 = new Sequence("dna1", "aaaGGGCC-----CTTTaaaGGG");
1643 // alternative transcript of same dna skips CCC codon
1644 SequenceI dna2 = new Sequence("dna2", "aaaGGGCC-----cttTaaaGGG");
1645 // dna3 has no mapping (protein product) so should be ignored here
1646 SequenceI dna3 = new Sequence("dna3", "aaaGGGCCCCCGGGcttTaaaGGG");
1647 SequenceI pep1 = new Sequence("pep1", "GPFG");
1648 SequenceI pep2 = new Sequence("pep2", "GPG");
1649 dna1.createDatasetSequence();
1650 dna2.createDatasetSequence();
1651 dna3.createDatasetSequence();
1652 pep1.createDatasetSequence();
1653 pep2.createDatasetSequence();
1655 AlignmentI dna = new Alignment(new SequenceI[] { dna1, dna2, dna3 });
1656 dna.setDataset(null);
1658 MapList map = new MapList(new int[] { 4, 12, 16, 18 },
1659 new int[] { 1, 4 }, 3, 1);
1660 AlignedCodonFrame acf = new AlignedCodonFrame();
1661 acf.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(), map);
1662 dna.addCodonFrame(acf);
1663 map = new MapList(new int[] { 4, 8, 12, 12, 16, 18 },
1664 new int[] { 1, 3 }, 3, 1);
1665 acf = new AlignedCodonFrame();
1666 acf.addMap(dna2.getDatasetSequence(), pep2.getDatasetSequence(), map);
1667 dna.addCodonFrame(acf);
1669 AlignmentI cds = AlignmentUtils.makeCdsAlignment(new SequenceI[] {
1670 dna1, dna2, dna3 }, dna.getDataset(), null);
1671 List<SequenceI> cdsSeqs = cds.getSequences();
1672 assertEquals(2, cdsSeqs.size());
1673 assertEquals("GGGCCCTTTGGG", cdsSeqs.get(0).getSequenceAsString());
1674 assertEquals("GGGCCTGGG", cdsSeqs.get(1).getSequenceAsString());
1677 * verify shared, extended alignment dataset
1679 assertSame(dna.getDataset(), cds.getDataset());
1680 assertTrue(dna.getDataset().getSequences()
1681 .contains(cdsSeqs.get(0).getDatasetSequence()));
1682 assertTrue(dna.getDataset().getSequences()
1683 .contains(cdsSeqs.get(1).getDatasetSequence()));
1686 * Verify 6 mappings: dna1 to cds1, cds1 to pep1, dna1 to pep1
1687 * and the same for dna2/cds2/pep2
1689 List<AlignedCodonFrame> mappings = cds.getCodonFrames();
1690 assertEquals(6, mappings.size());
1693 * 2 mappings involve pep1
1695 List<AlignedCodonFrame> pep1Mappings = MappingUtils
1696 .findMappingsForSequence(pep1, mappings);
1697 assertEquals(2, pep1Mappings.size());
1700 * Get mapping of pep1 to cds1 and verify it
1701 * maps GPFG to 1-3,4-6,7-9,10-12
1703 List<AlignedCodonFrame> pep1CdsMappings = MappingUtils
1704 .findMappingsForSequence(cds.getSequenceAt(0), pep1Mappings);
1705 assertEquals(1, pep1CdsMappings.size());
1706 SearchResultsI sr = MappingUtils.buildSearchResults(pep1, 1,
1708 assertEquals(1, sr.getResults().size());
1709 SearchResultMatchI m = sr.getResults().get(0);
1710 assertEquals(cds.getSequenceAt(0).getDatasetSequence(), m.getSequence());
1711 assertEquals(1, m.getStart());
1712 assertEquals(3, m.getEnd());
1713 sr = MappingUtils.buildSearchResults(pep1, 2, pep1CdsMappings);
1714 m = sr.getResults().get(0);
1715 assertEquals(4, m.getStart());
1716 assertEquals(6, m.getEnd());
1717 sr = MappingUtils.buildSearchResults(pep1, 3, pep1CdsMappings);
1718 m = sr.getResults().get(0);
1719 assertEquals(7, m.getStart());
1720 assertEquals(9, m.getEnd());
1721 sr = MappingUtils.buildSearchResults(pep1, 4, pep1CdsMappings);
1722 m = sr.getResults().get(0);
1723 assertEquals(10, m.getStart());
1724 assertEquals(12, m.getEnd());
1727 * Get mapping of pep2 to cds2 and verify it
1728 * maps GPG in pep2 to 1-3,4-6,7-9 in second CDS sequence
1730 List<AlignedCodonFrame> pep2Mappings = MappingUtils
1731 .findMappingsForSequence(pep2, mappings);
1732 assertEquals(2, pep2Mappings.size());
1733 List<AlignedCodonFrame> pep2CdsMappings = MappingUtils
1734 .findMappingsForSequence(cds.getSequenceAt(1), pep2Mappings);
1735 assertEquals(1, pep2CdsMappings.size());
1736 sr = MappingUtils.buildSearchResults(pep2, 1, pep2CdsMappings);
1737 assertEquals(1, sr.getResults().size());
1738 m = sr.getResults().get(0);
1739 assertEquals(cds.getSequenceAt(1).getDatasetSequence(), m.getSequence());
1740 assertEquals(1, m.getStart());
1741 assertEquals(3, m.getEnd());
1742 sr = MappingUtils.buildSearchResults(pep2, 2, pep2CdsMappings);
1743 m = sr.getResults().get(0);
1744 assertEquals(4, m.getStart());
1745 assertEquals(6, m.getEnd());
1746 sr = MappingUtils.buildSearchResults(pep2, 3, pep2CdsMappings);
1747 m = sr.getResults().get(0);
1748 assertEquals(7, m.getStart());
1749 assertEquals(9, m.getEnd());
1753 * Test the method that realigns protein to match mapped codon alignment.
1755 @Test(groups = { "Functional" })
1756 public void testAlignProteinAsDna_incompleteStartCodon()
1758 // seq1: incomplete start codon (not mapped), then [3, 11]
1759 SequenceI dna1 = new Sequence("Seq1", "ccAAA-TTT-GGG-");
1760 // seq2 codons are [4, 5], [8, 11]
1761 SequenceI dna2 = new Sequence("Seq2", "ccaAA-ttT-GGG-");
1762 // seq3 incomplete start codon at 'tt'
1763 SequenceI dna3 = new Sequence("Seq3", "ccaaa-ttt-GGG-");
1764 AlignmentI dna = new Alignment(new SequenceI[] { dna1, dna2, dna3 });
1765 dna.setDataset(null);
1767 // prot1 has 'X' for incomplete start codon (not mapped)
1768 SequenceI prot1 = new Sequence("Seq1", "XKFG"); // X for incomplete start
1769 SequenceI prot2 = new Sequence("Seq2", "NG");
1770 SequenceI prot3 = new Sequence("Seq3", "XG"); // X for incomplete start
1771 AlignmentI protein = new Alignment(new SequenceI[] { prot1, prot2,
1773 protein.setDataset(null);
1775 // map dna1 [3, 11] to prot1 [2, 4] KFG
1776 MapList map = new MapList(new int[] { 3, 11 }, new int[] { 2, 4 }, 3, 1);
1777 AlignedCodonFrame acf = new AlignedCodonFrame();
1778 acf.addMap(dna1.getDatasetSequence(), prot1.getDatasetSequence(), map);
1780 // map dna2 [4, 5] [8, 11] to prot2 [1, 2] NG
1781 map = new MapList(new int[] { 4, 5, 8, 11 }, new int[] { 1, 2 }, 3, 1);
1782 acf.addMap(dna2.getDatasetSequence(), prot2.getDatasetSequence(), map);
1784 // map dna3 [9, 11] to prot3 [2, 2] G
1785 map = new MapList(new int[] { 9, 11 }, new int[] { 2, 2 }, 3, 1);
1786 acf.addMap(dna3.getDatasetSequence(), prot3.getDatasetSequence(), map);
1788 ArrayList<AlignedCodonFrame> acfs = new ArrayList<>();
1790 protein.setCodonFrames(acfs);
1793 * verify X is included in the aligned proteins, and placed just
1794 * before the first mapped residue
1795 * CCT is between CCC and TTT
1797 AlignmentUtils.alignProteinAsDna(protein, dna);
1798 assertEquals("XK-FG", prot1.getSequenceAsString());
1799 assertEquals("--N-G", prot2.getSequenceAsString());
1800 assertEquals("---XG", prot3.getSequenceAsString());
1804 * Tests for the method that maps the subset of a dna sequence that has CDS
1805 * (or subtype) feature - case where the start codon is incomplete.
1807 @Test(groups = "Functional")
1808 public void testFindCdsPositions_fivePrimeIncomplete()
1810 SequenceI dnaSeq = new Sequence("dna", "aaagGGCCCaaaTTTttt");
1811 dnaSeq.createDatasetSequence();
1812 SequenceI ds = dnaSeq.getDatasetSequence();
1814 // CDS for dna 5-6 (incomplete codon), 7-9
1815 SequenceFeature sf = new SequenceFeature("CDS", "", 5, 9, 0f, null);
1816 sf.setPhase("2"); // skip 2 bases to start of next codon
1817 ds.addSequenceFeature(sf);
1818 // CDS for dna 13-15
1819 sf = new SequenceFeature("CDS_predicted", "", 13, 15, 0f, null);
1820 ds.addSequenceFeature(sf);
1822 List<int[]> ranges = AlignmentUtils.findCdsPositions(dnaSeq);
1825 * check the mapping starts with the first complete codon
1827 assertEquals(6, MappingUtils.getLength(ranges));
1828 assertEquals(2, ranges.size());
1829 assertEquals(7, ranges.get(0)[0]);
1830 assertEquals(9, ranges.get(0)[1]);
1831 assertEquals(13, ranges.get(1)[0]);
1832 assertEquals(15, ranges.get(1)[1]);
1836 * Tests for the method that maps the subset of a dna sequence that has CDS
1837 * (or subtype) feature.
1839 @Test(groups = "Functional")
1840 public void testFindCdsPositions()
1842 SequenceI dnaSeq = new Sequence("dna", "aaaGGGcccAAATTTttt");
1843 dnaSeq.createDatasetSequence();
1844 SequenceI ds = dnaSeq.getDatasetSequence();
1846 // CDS for dna 10-12
1847 SequenceFeature sf = new SequenceFeature("CDS_predicted", "", 10, 12,
1850 ds.addSequenceFeature(sf);
1852 sf = new SequenceFeature("CDS", "", 4, 6, 0f, null);
1854 ds.addSequenceFeature(sf);
1855 // exon feature should be ignored here
1856 sf = new SequenceFeature("exon", "", 7, 9, 0f, null);
1857 ds.addSequenceFeature(sf);
1859 List<int[]> ranges = AlignmentUtils.findCdsPositions(dnaSeq);
1861 * verify ranges { [4-6], [12-10] }
1862 * note CDS ranges are ordered ascending even if the CDS
1865 assertEquals(6, MappingUtils.getLength(ranges));
1866 assertEquals(2, ranges.size());
1867 assertEquals(4, ranges.get(0)[0]);
1868 assertEquals(6, ranges.get(0)[1]);
1869 assertEquals(10, ranges.get(1)[0]);
1870 assertEquals(12, ranges.get(1)[1]);
1874 * Test the method that computes a map of codon variants for each protein
1875 * position from "sequence_variant" features on dna
1877 @Test(groups = "Functional")
1878 public void testBuildDnaVariantsMap()
1880 SequenceI dna = new Sequence("dna", "atgAAATTTGGGCCCtag");
1881 MapList map = new MapList(new int[] { 1, 18 }, new int[] { 1, 5 }, 3, 1);
1884 * first with no variants on dna
1886 LinkedHashMap<Integer, List<DnaVariant>[]> variantsMap = AlignmentUtils
1887 .buildDnaVariantsMap(dna, map);
1888 assertTrue(variantsMap.isEmpty());
1891 * single allele codon 1, on base 1
1893 SequenceFeature sf1 = new SequenceFeature("sequence_variant", "", 1, 1,
1895 sf1.setValue("alleles", "T");
1896 sf1.setValue("ID", "sequence_variant:rs758803211");
1897 dna.addSequenceFeature(sf1);
1900 * two alleles codon 2, on bases 2 and 3 (distinct variants)
1902 SequenceFeature sf2 = new SequenceFeature("sequence_variant", "", 5, 5,
1904 sf2.setValue("alleles", "T");
1905 sf2.setValue("ID", "sequence_variant:rs758803212");
1906 dna.addSequenceFeature(sf2);
1907 SequenceFeature sf3 = new SequenceFeature("sequence_variant", "", 6, 6,
1909 sf3.setValue("alleles", "G");
1910 sf3.setValue("ID", "sequence_variant:rs758803213");
1911 dna.addSequenceFeature(sf3);
1914 * two alleles codon 3, both on base 2 (one variant)
1916 SequenceFeature sf4 = new SequenceFeature("sequence_variant", "", 8, 8,
1918 sf4.setValue("alleles", "C, G");
1919 sf4.setValue("ID", "sequence_variant:rs758803214");
1920 dna.addSequenceFeature(sf4);
1922 // no alleles on codon 4
1925 * alleles on codon 5 on all 3 bases (distinct variants)
1927 SequenceFeature sf5 = new SequenceFeature("sequence_variant", "", 13,
1929 sf5.setValue("alleles", "C, G"); // (C duplicates given base value)
1930 sf5.setValue("ID", "sequence_variant:rs758803215");
1931 dna.addSequenceFeature(sf5);
1932 SequenceFeature sf6 = new SequenceFeature("sequence_variant", "", 14,
1934 sf6.setValue("alleles", "g, a"); // should force to upper-case
1935 sf6.setValue("ID", "sequence_variant:rs758803216");
1936 dna.addSequenceFeature(sf6);
1938 SequenceFeature sf7 = new SequenceFeature("sequence_variant", "", 15,
1940 sf7.setValue("alleles", "A, T");
1941 sf7.setValue("ID", "sequence_variant:rs758803217");
1942 dna.addSequenceFeature(sf7);
1945 * build map - expect variants on positions 1, 2, 3, 5
1947 variantsMap = AlignmentUtils.buildDnaVariantsMap(dna, map);
1948 assertEquals(4, variantsMap.size());
1951 * protein residue 1: variant on codon (ATG) base 1, not on 2 or 3
1953 List<DnaVariant>[] pep1Variants = variantsMap.get(1);
1954 assertEquals(3, pep1Variants.length);
1955 assertEquals(1, pep1Variants[0].size());
1956 assertEquals("A", pep1Variants[0].get(0).base); // codon[1] base
1957 assertSame(sf1, pep1Variants[0].get(0).variant); // codon[1] variant
1958 assertEquals(1, pep1Variants[1].size());
1959 assertEquals("T", pep1Variants[1].get(0).base); // codon[2] base
1960 assertNull(pep1Variants[1].get(0).variant); // no variant here
1961 assertEquals(1, pep1Variants[2].size());
1962 assertEquals("G", pep1Variants[2].get(0).base); // codon[3] base
1963 assertNull(pep1Variants[2].get(0).variant); // no variant here
1966 * protein residue 2: variants on codon (AAA) bases 2 and 3
1968 List<DnaVariant>[] pep2Variants = variantsMap.get(2);
1969 assertEquals(3, pep2Variants.length);
1970 assertEquals(1, pep2Variants[0].size());
1971 // codon[1] base recorded while processing variant on codon[2]
1972 assertEquals("A", pep2Variants[0].get(0).base);
1973 assertNull(pep2Variants[0].get(0).variant); // no variant here
1974 // codon[2] base and variant:
1975 assertEquals(1, pep2Variants[1].size());
1976 assertEquals("A", pep2Variants[1].get(0).base);
1977 assertSame(sf2, pep2Variants[1].get(0).variant);
1978 // codon[3] base was recorded when processing codon[2] variant
1979 // and then the variant for codon[3] added to it
1980 assertEquals(1, pep2Variants[2].size());
1981 assertEquals("A", pep2Variants[2].get(0).base);
1982 assertSame(sf3, pep2Variants[2].get(0).variant);
1985 * protein residue 3: variants on codon (TTT) base 2 only
1987 List<DnaVariant>[] pep3Variants = variantsMap.get(3);
1988 assertEquals(3, pep3Variants.length);
1989 assertEquals(1, pep3Variants[0].size());
1990 assertEquals("T", pep3Variants[0].get(0).base); // codon[1] base
1991 assertNull(pep3Variants[0].get(0).variant); // no variant here
1992 assertEquals(1, pep3Variants[1].size());
1993 assertEquals("T", pep3Variants[1].get(0).base); // codon[2] base
1994 assertSame(sf4, pep3Variants[1].get(0).variant); // codon[2] variant
1995 assertEquals(1, pep3Variants[2].size());
1996 assertEquals("T", pep3Variants[2].get(0).base); // codon[3] base
1997 assertNull(pep3Variants[2].get(0).variant); // no variant here
2000 * three variants on protein position 5
2002 List<DnaVariant>[] pep5Variants = variantsMap.get(5);
2003 assertEquals(3, pep5Variants.length);
2004 assertEquals(1, pep5Variants[0].size());
2005 assertEquals("C", pep5Variants[0].get(0).base); // codon[1] base
2006 assertSame(sf5, pep5Variants[0].get(0).variant); // codon[1] variant
2007 assertEquals(1, pep5Variants[1].size());
2008 assertEquals("C", pep5Variants[1].get(0).base); // codon[2] base
2009 assertSame(sf6, pep5Variants[1].get(0).variant); // codon[2] variant
2010 assertEquals(1, pep5Variants[2].size());
2011 assertEquals("C", pep5Variants[2].get(0).base); // codon[3] base
2012 assertSame(sf7, pep5Variants[2].get(0).variant); // codon[3] variant
2016 * Tests for the method that computes all peptide variants given codon
2019 @Test(groups = "Functional")
2020 public void testComputePeptideVariants()
2023 * scenario: AAATTTCCC codes for KFP
2025 * GAA -> E source: Ensembl
2026 * CAA -> Q source: dbSNP
2027 * TAA -> STOP source: dnSNP
2028 * AAG synonymous source: COSMIC
2029 * AAT -> N source: Ensembl
2030 * ...TTC synonymous source: dbSNP
2031 * ......CAC,CGC -> H,R source: COSMIC
2032 * (one variant with two alleles)
2034 SequenceI peptide = new Sequence("pep/10-12", "KFP");
2037 * two distinct variants for codon 1 position 1
2038 * second one has clinical significance
2040 String ensembl = "Ensembl";
2041 String dbSnp = "dbSNP";
2042 String cosmic = "COSMIC";
2045 * NB setting "id" (as returned by Ensembl for features in JSON format);
2046 * previously "ID" (as returned for GFF3 format)
2048 SequenceFeature sf1 = new SequenceFeature("sequence_variant", "", 1, 1,
2050 sf1.setValue("alleles", "A,G"); // AAA -> GAA -> K/E
2051 sf1.setValue("id", "var1.125A>G");
2053 SequenceFeature sf2 = new SequenceFeature("sequence_variant", "", 1, 1,
2055 sf2.setValue("alleles", "A,C"); // AAA -> CAA -> K/Q
2056 sf2.setValue("id", "var2");
2057 sf2.setValue("clinical_significance", "Dodgy");
2059 SequenceFeature sf3 = new SequenceFeature("sequence_variant", "", 1, 1,
2061 sf3.setValue("alleles", "A,T"); // AAA -> TAA -> stop codon
2062 sf3.setValue("id", "var3");
2063 sf3.setValue("clinical_significance", "Bad");
2065 SequenceFeature sf4 = new SequenceFeature("sequence_variant", "", 3, 3,
2067 sf4.setValue("alleles", "A,G"); // AAA -> AAG synonymous
2068 sf4.setValue("id", "var4");
2069 sf4.setValue("clinical_significance", "None");
2071 SequenceFeature sf5 = new SequenceFeature("sequence_variant", "", 3, 3,
2073 sf5.setValue("alleles", "A,T"); // AAA -> AAT -> K/N
2074 sf5.setValue("id", "sequence_variant:var5"); // prefix gets stripped off
2075 sf5.setValue("clinical_significance", "Benign");
2077 SequenceFeature sf6 = new SequenceFeature("sequence_variant", "", 6, 6,
2079 sf6.setValue("alleles", "T,C"); // TTT -> TTC synonymous
2080 sf6.setValue("id", "var6");
2082 SequenceFeature sf7 = new SequenceFeature("sequence_variant", "", 8, 8,
2084 sf7.setValue("alleles", "C,A,G"); // CCC -> CAC,CGC -> P/H/R
2085 sf7.setValue("id", "var7");
2086 sf7.setValue("clinical_significance", "Good");
2088 List<DnaVariant> codon1Variants = new ArrayList<>();
2089 List<DnaVariant> codon2Variants = new ArrayList<>();
2090 List<DnaVariant> codon3Variants = new ArrayList<>();
2092 List<DnaVariant> codonVariants[] = new ArrayList[3];
2093 codonVariants[0] = codon1Variants;
2094 codonVariants[1] = codon2Variants;
2095 codonVariants[2] = codon3Variants;
2098 * compute variants for protein position 1
2100 codon1Variants.add(new DnaVariant("A", sf1));
2101 codon1Variants.add(new DnaVariant("A", sf2));
2102 codon1Variants.add(new DnaVariant("A", sf3));
2103 codon2Variants.add(new DnaVariant("A"));
2104 // codon2Variants.add(new DnaVariant("A"));
2105 codon3Variants.add(new DnaVariant("A", sf4));
2106 codon3Variants.add(new DnaVariant("A", sf5));
2107 AlignmentUtils.computePeptideVariants(peptide, 1, codonVariants);
2110 * compute variants for protein position 2
2112 codon1Variants.clear();
2113 codon2Variants.clear();
2114 codon3Variants.clear();
2115 codon1Variants.add(new DnaVariant("T"));
2116 codon2Variants.add(new DnaVariant("T"));
2117 codon3Variants.add(new DnaVariant("T", sf6));
2118 AlignmentUtils.computePeptideVariants(peptide, 2, codonVariants);
2121 * compute variants for protein position 3
2123 codon1Variants.clear();
2124 codon2Variants.clear();
2125 codon3Variants.clear();
2126 codon1Variants.add(new DnaVariant("C"));
2127 codon2Variants.add(new DnaVariant("C", sf7));
2128 codon3Variants.add(new DnaVariant("C"));
2129 AlignmentUtils.computePeptideVariants(peptide, 3, codonVariants);
2132 * verify added sequence features for
2133 * var1 K -> E Ensembl
2137 * var5 K -> N Ensembl
2139 * var7 P -> H COSMIC
2140 * var8 P -> R COSMIC
2142 List<SequenceFeature> sfs = peptide.getSequenceFeatures();
2143 SequenceFeatures.sortFeatures(sfs, true);
2144 assertEquals(8, sfs.size());
2147 * features are sorted by start position ascending, but in no
2148 * particular order where start positions match; asserts here
2149 * simply match the data returned (the order is not important)
2151 // AAA -> AAT -> K/N
2152 SequenceFeature sf = sfs.get(0);
2153 assertEquals(1, sf.getBegin());
2154 assertEquals(1, sf.getEnd());
2155 assertEquals("nonsynonymous_variant", sf.getType());
2156 assertEquals("p.Lys1Asn", sf.getDescription());
2157 assertEquals("var5", sf.getValue("id"));
2158 assertEquals("Benign", sf.getValue("clinical_significance"));
2159 assertEquals("id=var5;clinical_significance=Benign",
2160 sf.getAttributes());
2161 assertEquals(1, sf.links.size());
2163 "p.Lys1Asn var5|http://www.ensembl.org/Homo_sapiens/Variation/Summary?v=var5",
2165 assertEquals(ensembl, sf.getFeatureGroup());
2167 // AAA -> CAA -> K/Q
2169 assertEquals(1, sf.getBegin());
2170 assertEquals(1, sf.getEnd());
2171 assertEquals("nonsynonymous_variant", sf.getType());
2172 assertEquals("p.Lys1Gln", sf.getDescription());
2173 assertEquals("var2", sf.getValue("id"));
2174 assertEquals("Dodgy", sf.getValue("clinical_significance"));
2175 assertEquals("id=var2;clinical_significance=Dodgy", sf.getAttributes());
2176 assertEquals(1, sf.links.size());
2178 "p.Lys1Gln var2|http://www.ensembl.org/Homo_sapiens/Variation/Summary?v=var2",
2180 assertEquals(dbSnp, sf.getFeatureGroup());
2182 // AAA -> GAA -> K/E
2184 assertEquals(1, sf.getBegin());
2185 assertEquals(1, sf.getEnd());
2186 assertEquals("nonsynonymous_variant", sf.getType());
2187 assertEquals("p.Lys1Glu", sf.getDescription());
2188 assertEquals("var1.125A>G", sf.getValue("id"));
2189 assertNull(sf.getValue("clinical_significance"));
2190 assertEquals("id=var1.125A>G", sf.getAttributes());
2191 assertEquals(1, sf.links.size());
2192 // link to variation is urlencoded
2194 "p.Lys1Glu var1.125A>G|http://www.ensembl.org/Homo_sapiens/Variation/Summary?v=var1.125A%3EG",
2196 assertEquals(ensembl, sf.getFeatureGroup());
2198 // AAA -> TAA -> stop codon
2200 assertEquals(1, sf.getBegin());
2201 assertEquals(1, sf.getEnd());
2202 assertEquals("stop_gained", sf.getType());
2203 assertEquals("Aaa/Taa", sf.getDescription());
2204 assertEquals("var3", sf.getValue("id"));
2205 assertEquals("Bad", sf.getValue("clinical_significance"));
2206 assertEquals("id=var3;clinical_significance=Bad", sf.getAttributes());
2207 assertEquals(1, sf.links.size());
2209 "Aaa/Taa var3|http://www.ensembl.org/Homo_sapiens/Variation/Summary?v=var3",
2211 assertEquals(dbSnp, sf.getFeatureGroup());
2213 // AAA -> AAG synonymous
2215 assertEquals(1, sf.getBegin());
2216 assertEquals(1, sf.getEnd());
2217 assertEquals("synonymous_variant", sf.getType());
2218 assertEquals("aaA/aaG", sf.getDescription());
2219 assertEquals("var4", sf.getValue("id"));
2220 assertEquals("None", sf.getValue("clinical_significance"));
2221 assertEquals("id=var4;clinical_significance=None", sf.getAttributes());
2222 assertEquals(1, sf.links.size());
2224 "aaA/aaG var4|http://www.ensembl.org/Homo_sapiens/Variation/Summary?v=var4",
2226 assertEquals(cosmic, sf.getFeatureGroup());
2228 // TTT -> TTC synonymous
2230 assertEquals(2, sf.getBegin());
2231 assertEquals(2, sf.getEnd());
2232 assertEquals("synonymous_variant", sf.getType());
2233 assertEquals("ttT/ttC", sf.getDescription());
2234 assertEquals("var6", sf.getValue("id"));
2235 assertNull(sf.getValue("clinical_significance"));
2236 assertEquals("id=var6", sf.getAttributes());
2237 assertEquals(1, sf.links.size());
2239 "ttT/ttC var6|http://www.ensembl.org/Homo_sapiens/Variation/Summary?v=var6",
2241 assertEquals(dbSnp, sf.getFeatureGroup());
2243 // var7 generates two distinct protein variant features (two alleles)
2244 // CCC -> CGC -> P/R
2246 assertEquals(3, sf.getBegin());
2247 assertEquals(3, sf.getEnd());
2248 assertEquals("nonsynonymous_variant", sf.getType());
2249 assertEquals("p.Pro3Arg", sf.getDescription());
2250 assertEquals("var7", sf.getValue("id"));
2251 assertEquals("Good", sf.getValue("clinical_significance"));
2252 assertEquals("id=var7;clinical_significance=Good", sf.getAttributes());
2253 assertEquals(1, sf.links.size());
2255 "p.Pro3Arg var7|http://www.ensembl.org/Homo_sapiens/Variation/Summary?v=var7",
2257 assertEquals(cosmic, sf.getFeatureGroup());
2259 // CCC -> CAC -> P/H
2261 assertEquals(3, sf.getBegin());
2262 assertEquals(3, sf.getEnd());
2263 assertEquals("nonsynonymous_variant", sf.getType());
2264 assertEquals("p.Pro3His", sf.getDescription());
2265 assertEquals("var7", sf.getValue("id"));
2266 assertEquals("Good", sf.getValue("clinical_significance"));
2267 assertEquals("id=var7;clinical_significance=Good", sf.getAttributes());
2268 assertEquals(1, sf.links.size());
2270 "p.Pro3His var7|http://www.ensembl.org/Homo_sapiens/Variation/Summary?v=var7",
2272 assertEquals(cosmic, sf.getFeatureGroup());
2276 * Tests for the method that maps the subset of a dna sequence that has CDS
2277 * (or subtype) feature, with CDS strand = '-' (reverse)
2279 // test turned off as currently findCdsPositions is not strand-dependent
2280 // left in case it comes around again...
2281 @Test(groups = "Functional", enabled = false)
2282 public void testFindCdsPositions_reverseStrand()
2284 SequenceI dnaSeq = new Sequence("dna", "aaaGGGcccAAATTTttt");
2285 dnaSeq.createDatasetSequence();
2286 SequenceI ds = dnaSeq.getDatasetSequence();
2289 SequenceFeature sf = new SequenceFeature("CDS", "", 4, 6, 0f, null);
2291 ds.addSequenceFeature(sf);
2292 // exon feature should be ignored here
2293 sf = new SequenceFeature("exon", "", 7, 9, 0f, null);
2294 ds.addSequenceFeature(sf);
2295 // CDS for dna 10-12
2296 sf = new SequenceFeature("CDS_predicted", "", 10, 12, 0f, null);
2298 ds.addSequenceFeature(sf);
2300 List<int[]> ranges = AlignmentUtils.findCdsPositions(dnaSeq);
2302 * verify ranges { [12-10], [6-4] }
2304 assertEquals(6, MappingUtils.getLength(ranges));
2305 assertEquals(2, ranges.size());
2306 assertEquals(12, ranges.get(0)[0]);
2307 assertEquals(10, ranges.get(0)[1]);
2308 assertEquals(6, ranges.get(1)[0]);
2309 assertEquals(4, ranges.get(1)[1]);
2313 * Tests for the method that maps the subset of a dna sequence that has CDS
2314 * (or subtype) feature - reverse strand case where the start codon is
2317 @Test(groups = "Functional", enabled = false)
2318 // test turned off as currently findCdsPositions is not strand-dependent
2319 // left in case it comes around again...
2320 public void testFindCdsPositions_reverseStrandThreePrimeIncomplete()
2322 SequenceI dnaSeq = new Sequence("dna", "aaagGGCCCaaaTTTttt");
2323 dnaSeq.createDatasetSequence();
2324 SequenceI ds = dnaSeq.getDatasetSequence();
2327 SequenceFeature sf = new SequenceFeature("CDS", "", 5, 9, 0f, null);
2329 ds.addSequenceFeature(sf);
2330 // CDS for dna 13-15
2331 sf = new SequenceFeature("CDS_predicted", "", 13, 15, 0f, null);
2333 sf.setPhase("2"); // skip 2 bases to start of next codon
2334 ds.addSequenceFeature(sf);
2336 List<int[]> ranges = AlignmentUtils.findCdsPositions(dnaSeq);
2339 * check the mapping starts with the first complete codon
2340 * expect ranges [13, 13], [9, 5]
2342 assertEquals(6, MappingUtils.getLength(ranges));
2343 assertEquals(2, ranges.size());
2344 assertEquals(13, ranges.get(0)[0]);
2345 assertEquals(13, ranges.get(0)[1]);
2346 assertEquals(9, ranges.get(1)[0]);
2347 assertEquals(5, ranges.get(1)[1]);
2350 @Test(groups = "Functional")
2351 public void testAlignAs_alternateTranscriptsUngapped()
2353 SequenceI dna1 = new Sequence("dna1", "cccGGGTTTaaa");
2354 SequenceI dna2 = new Sequence("dna2", "CCCgggtttAAA");
2355 AlignmentI dna = new Alignment(new SequenceI[] { dna1, dna2 });
2356 ((Alignment) dna).createDatasetAlignment();
2357 SequenceI cds1 = new Sequence("cds1", "GGGTTT");
2358 SequenceI cds2 = new Sequence("cds2", "CCCAAA");
2359 AlignmentI cds = new Alignment(new SequenceI[] { cds1, cds2 });
2360 ((Alignment) cds).createDatasetAlignment();
2362 AlignedCodonFrame acf = new AlignedCodonFrame();
2363 MapList map = new MapList(new int[] { 4, 9 }, new int[] { 1, 6 }, 1, 1);
2364 acf.addMap(dna1.getDatasetSequence(), cds1.getDatasetSequence(), map);
2365 map = new MapList(new int[] { 1, 3, 10, 12 }, new int[] { 1, 6 }, 1, 1);
2366 acf.addMap(dna2.getDatasetSequence(), cds2.getDatasetSequence(), map);
2369 * verify CDS alignment is as:
2370 * cccGGGTTTaaa (cdna)
2371 * CCCgggtttAAA (cdna)
2373 * ---GGGTTT--- (cds)
2374 * CCC------AAA (cds)
2376 dna.addCodonFrame(acf);
2377 AlignmentUtils.alignAs(cds, dna);
2378 assertEquals("---GGGTTT", cds.getSequenceAt(0).getSequenceAsString());
2379 assertEquals("CCC------AAA", cds.getSequenceAt(1).getSequenceAsString());
2382 @Test(groups = { "Functional" })
2383 public void testAddMappedPositions()
2385 SequenceI from = new Sequence("dna", "ggAA-ATcc-TT-g");
2386 SequenceI seq1 = new Sequence("cds", "AAATTT");
2387 from.createDatasetSequence();
2388 seq1.createDatasetSequence();
2389 Mapping mapping = new Mapping(seq1, new MapList(
2390 new int[] { 3, 6, 9, 10 }, new int[] { 1, 6 }, 1, 1));
2391 Map<Integer, Map<SequenceI, Character>> map = new TreeMap<>();
2392 AlignmentUtils.addMappedPositions(seq1, from, mapping, map);
2395 * verify map has seq1 residues in columns 3,4,6,7,11,12
2397 assertEquals(6, map.size());
2398 assertEquals('A', map.get(3).get(seq1).charValue());
2399 assertEquals('A', map.get(4).get(seq1).charValue());
2400 assertEquals('A', map.get(6).get(seq1).charValue());
2401 assertEquals('T', map.get(7).get(seq1).charValue());
2402 assertEquals('T', map.get(11).get(seq1).charValue());
2403 assertEquals('T', map.get(12).get(seq1).charValue());
2411 * Test case where the mapping 'from' range includes a stop codon which is
2412 * absent in the 'to' range
2414 @Test(groups = { "Functional" })
2415 public void testAddMappedPositions_withStopCodon()
2417 SequenceI from = new Sequence("dna", "ggAA-ATcc-TT-g");
2418 SequenceI seq1 = new Sequence("cds", "AAATTT");
2419 from.createDatasetSequence();
2420 seq1.createDatasetSequence();
2421 Mapping mapping = new Mapping(seq1, new MapList(
2422 new int[] { 3, 6, 9, 10 }, new int[] { 1, 6 }, 1, 1));
2423 Map<Integer, Map<SequenceI, Character>> map = new TreeMap<>();
2424 AlignmentUtils.addMappedPositions(seq1, from, mapping, map);
2427 * verify map has seq1 residues in columns 3,4,6,7,11,12
2429 assertEquals(6, map.size());
2430 assertEquals('A', map.get(3).get(seq1).charValue());
2431 assertEquals('A', map.get(4).get(seq1).charValue());
2432 assertEquals('A', map.get(6).get(seq1).charValue());
2433 assertEquals('T', map.get(7).get(seq1).charValue());
2434 assertEquals('T', map.get(11).get(seq1).charValue());
2435 assertEquals('T', map.get(12).get(seq1).charValue());
2439 * Test for the case where the products for which we want CDS are specified.
2440 * This is to represent the case where EMBL has CDS mappings to both Uniprot
2441 * and EMBLCDSPROTEIN. makeCdsAlignment() should only return the mappings for
2442 * the protein sequences specified.
2444 @Test(groups = { "Functional" })
2445 public void testMakeCdsAlignment_filterProducts()
2447 SequenceI dna1 = new Sequence("dna1", "aaaGGGcccTTTaaa");
2448 SequenceI dna2 = new Sequence("dna2", "GGGcccTTTaaaCCC");
2449 SequenceI pep1 = new Sequence("Uniprot|pep1", "GF");
2450 SequenceI pep2 = new Sequence("Uniprot|pep2", "GFP");
2451 SequenceI pep3 = new Sequence("EMBL|pep3", "GF");
2452 SequenceI pep4 = new Sequence("EMBL|pep4", "GFP");
2453 dna1.createDatasetSequence();
2454 dna2.createDatasetSequence();
2455 pep1.createDatasetSequence();
2456 pep2.createDatasetSequence();
2457 pep3.createDatasetSequence();
2458 pep4.createDatasetSequence();
2459 AlignmentI dna = new Alignment(new SequenceI[] { dna1, dna2 });
2460 dna.setDataset(null);
2461 AlignmentI emblPeptides = new Alignment(new SequenceI[] { pep3, pep4 });
2462 emblPeptides.setDataset(null);
2464 AlignedCodonFrame acf = new AlignedCodonFrame();
2465 MapList map = new MapList(new int[] { 4, 6, 10, 12 },
2466 new int[] { 1, 2 }, 3, 1);
2467 acf.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(), map);
2468 acf.addMap(dna1.getDatasetSequence(), pep3.getDatasetSequence(), map);
2469 dna.addCodonFrame(acf);
2471 acf = new AlignedCodonFrame();
2472 map = new MapList(new int[] { 1, 3, 7, 9, 13, 15 }, new int[] { 1, 3 },
2474 acf.addMap(dna2.getDatasetSequence(), pep2.getDatasetSequence(), map);
2475 acf.addMap(dna2.getDatasetSequence(), pep4.getDatasetSequence(), map);
2476 dna.addCodonFrame(acf);
2479 * execute method under test to find CDS for EMBL peptides only
2481 AlignmentI cds = AlignmentUtils.makeCdsAlignment(new SequenceI[] {
2482 dna1, dna2 }, dna.getDataset(), emblPeptides.getSequencesArray());
2484 assertEquals(2, cds.getSequences().size());
2485 assertEquals("GGGTTT", cds.getSequenceAt(0).getSequenceAsString());
2486 assertEquals("GGGTTTCCC", cds.getSequenceAt(1).getSequenceAsString());
2489 * verify shared, extended alignment dataset
2491 assertSame(dna.getDataset(), cds.getDataset());
2492 assertTrue(dna.getDataset().getSequences()
2493 .contains(cds.getSequenceAt(0).getDatasetSequence()));
2494 assertTrue(dna.getDataset().getSequences()
2495 .contains(cds.getSequenceAt(1).getDatasetSequence()));
2498 * Verify mappings from CDS to peptide, cDNA to CDS, and cDNA to peptide
2499 * the mappings are on the shared alignment dataset
2501 List<AlignedCodonFrame> cdsMappings = cds.getDataset().getCodonFrames();
2503 * 6 mappings, 2*(DNA->CDS), 2*(DNA->Pep), 2*(CDS->Pep)
2505 assertEquals(6, cdsMappings.size());
2508 * verify that mapping sets for dna and cds alignments are different
2509 * [not current behaviour - all mappings are on the alignment dataset]
2511 // select -> subselect type to test.
2512 // Assert.assertNotSame(dna.getCodonFrames(), cds.getCodonFrames());
2513 // assertEquals(4, dna.getCodonFrames().size());
2514 // assertEquals(4, cds.getCodonFrames().size());
2517 * Two mappings involve pep3 (dna to pep3, cds to pep3)
2518 * Mapping from pep3 to GGGTTT in first new exon sequence
2520 List<AlignedCodonFrame> pep3Mappings = MappingUtils
2521 .findMappingsForSequence(pep3, cdsMappings);
2522 assertEquals(2, pep3Mappings.size());
2523 List<AlignedCodonFrame> mappings = MappingUtils
2524 .findMappingsForSequence(cds.getSequenceAt(0), pep3Mappings);
2525 assertEquals(1, mappings.size());
2528 SearchResultsI sr = MappingUtils.buildSearchResults(pep3, 1, mappings);
2529 assertEquals(1, sr.getResults().size());
2530 SearchResultMatchI m = sr.getResults().get(0);
2531 assertSame(cds.getSequenceAt(0).getDatasetSequence(), m.getSequence());
2532 assertEquals(1, m.getStart());
2533 assertEquals(3, m.getEnd());
2535 sr = MappingUtils.buildSearchResults(pep3, 2, mappings);
2536 m = sr.getResults().get(0);
2537 assertSame(cds.getSequenceAt(0).getDatasetSequence(), m.getSequence());
2538 assertEquals(4, m.getStart());
2539 assertEquals(6, m.getEnd());
2542 * Two mappings involve pep4 (dna to pep4, cds to pep4)
2543 * Verify mapping from pep4 to GGGTTTCCC in second new exon sequence
2545 List<AlignedCodonFrame> pep4Mappings = MappingUtils
2546 .findMappingsForSequence(pep4, cdsMappings);
2547 assertEquals(2, pep4Mappings.size());
2548 mappings = MappingUtils.findMappingsForSequence(cds.getSequenceAt(1),
2550 assertEquals(1, mappings.size());
2552 sr = MappingUtils.buildSearchResults(pep4, 1, mappings);
2553 assertEquals(1, sr.getResults().size());
2554 m = sr.getResults().get(0);
2555 assertSame(cds.getSequenceAt(1).getDatasetSequence(), m.getSequence());
2556 assertEquals(1, m.getStart());
2557 assertEquals(3, m.getEnd());
2559 sr = MappingUtils.buildSearchResults(pep4, 2, mappings);
2560 m = sr.getResults().get(0);
2561 assertSame(cds.getSequenceAt(1).getDatasetSequence(), m.getSequence());
2562 assertEquals(4, m.getStart());
2563 assertEquals(6, m.getEnd());
2565 sr = MappingUtils.buildSearchResults(pep4, 3, mappings);
2566 m = sr.getResults().get(0);
2567 assertSame(cds.getSequenceAt(1).getDatasetSequence(), m.getSequence());
2568 assertEquals(7, m.getStart());
2569 assertEquals(9, m.getEnd());
2573 * Test the method that just copies aligned sequences, provided all sequences
2574 * to be aligned share the aligned sequence's dataset
2576 @Test(groups = "Functional")
2577 public void testAlignAsSameSequences()
2579 SequenceI dna1 = new Sequence("dna1", "cccGGGTTTaaa");
2580 SequenceI dna2 = new Sequence("dna2", "CCCgggtttAAA");
2581 AlignmentI al1 = new Alignment(new SequenceI[] { dna1, dna2 });
2582 ((Alignment) al1).createDatasetAlignment();
2584 SequenceI dna3 = new Sequence(dna1);
2585 SequenceI dna4 = new Sequence(dna2);
2586 assertSame(dna3.getDatasetSequence(), dna1.getDatasetSequence());
2587 assertSame(dna4.getDatasetSequence(), dna2.getDatasetSequence());
2588 String seq1 = "-cc-GG-GT-TT--aaa";
2589 dna3.setSequence(seq1);
2590 String seq2 = "C--C-Cgg--gtt-tAA-A-";
2591 dna4.setSequence(seq2);
2592 AlignmentI al2 = new Alignment(new SequenceI[] { dna3, dna4 });
2593 ((Alignment) al2).createDatasetAlignment();
2595 assertTrue(AlignmentUtils.alignAsSameSequences(al1, al2));
2596 assertEquals(seq1, al1.getSequenceAt(0).getSequenceAsString());
2597 assertEquals(seq2, al1.getSequenceAt(1).getSequenceAsString());
2600 * add another sequence to 'aligned' - should still succeed, since
2601 * unaligned sequences still share a dataset with aligned sequences
2603 SequenceI dna5 = new Sequence("dna5", "CCCgggtttAAA");
2604 dna5.createDatasetSequence();
2605 al2.addSequence(dna5);
2606 assertTrue(AlignmentUtils.alignAsSameSequences(al1, al2));
2607 assertEquals(seq1, al1.getSequenceAt(0).getSequenceAsString());
2608 assertEquals(seq2, al1.getSequenceAt(1).getSequenceAsString());
2611 * add another sequence to 'unaligned' - should fail, since now not
2612 * all unaligned sequences share a dataset with aligned sequences
2614 SequenceI dna6 = new Sequence("dna6", "CCCgggtttAAA");
2615 dna6.createDatasetSequence();
2616 al1.addSequence(dna6);
2617 // JAL-2110 JBP Comment: what's the use case for this behaviour ?
2618 assertFalse(AlignmentUtils.alignAsSameSequences(al1, al2));
2621 @Test(groups = "Functional")
2622 public void testAlignAsSameSequencesMultipleSubSeq()
2624 SequenceI dna1 = new Sequence("dna1", "cccGGGTTTaaa");
2625 SequenceI dna2 = new Sequence("dna2", "CCCgggtttAAA");
2626 SequenceI as1 = dna1.deriveSequence();
2627 SequenceI as2 = dna1.deriveSequence().getSubSequence(3, 7);
2628 SequenceI as3 = dna2.deriveSequence();
2629 as1.insertCharAt(6, 5, '-');
2630 String s_as1 = as1.getSequenceAsString();
2631 as2.insertCharAt(6, 5, '-');
2632 String s_as2 = as2.getSequenceAsString();
2633 as3.insertCharAt(6, 5, '-');
2634 String s_as3 = as3.getSequenceAsString();
2635 AlignmentI aligned = new Alignment(new SequenceI[] { as1, as2, as3 });
2637 // why do we need to cast this still ?
2638 ((Alignment) aligned).createDatasetAlignment();
2639 SequenceI uas1 = dna1.deriveSequence();
2640 SequenceI uas2 = dna1.deriveSequence().getSubSequence(3, 7);
2641 SequenceI uas3 = dna2.deriveSequence();
2642 AlignmentI tobealigned = new Alignment(new SequenceI[] { uas1, uas2,
2644 ((Alignment) tobealigned).createDatasetAlignment();
2646 assertTrue(AlignmentUtils.alignAsSameSequences(tobealigned, aligned));
2647 assertEquals(s_as1, uas1.getSequenceAsString());
2648 assertEquals(s_as2, uas2.getSequenceAsString());
2649 assertEquals(s_as3, uas3.getSequenceAsString());
2652 @Test(groups = { "Functional" })
2653 public void testTransferGeneLoci()
2655 SequenceI from = new Sequence("transcript",
2656 "aaacccgggTTTAAACCCGGGtttaaacccgggttt");
2657 SequenceI to = new Sequence("CDS", "TTTAAACCCGGG");
2658 MapList map = new MapList(new int[] { 1, 12 }, new int[] { 10, 21 }, 1,
2662 * first with nothing to transfer
2664 AlignmentUtils.transferGeneLoci(from, map, to);
2665 assertNull(to.getGeneLoci());
2668 * next with gene loci set on 'from' sequence
2670 int[] exons = new int[] { 100, 105, 155, 164, 210, 229 };
2671 MapList geneMap = new MapList(new int[] { 1, 36 }, exons, 1, 1);
2672 from.setGeneLoci("human", "GRCh38", "7", geneMap);
2673 AlignmentUtils.transferGeneLoci(from, map, to);
2675 GeneLociI toLoci = to.getGeneLoci();
2676 assertNotNull(toLoci);
2677 // DBRefEntry constructor upper-cases 'source'
2678 assertEquals("HUMAN", toLoci.getSpeciesId());
2679 assertEquals("GRCh38", toLoci.getAssemblyId());
2680 assertEquals("7", toLoci.getChromosomeId());
2683 * transcript 'exons' are 1-6, 7-16, 17-36
2684 * CDS 1:12 is transcript 10-21
2685 * transcript 'CDS' is 10-16, 17-21
2686 * which is 'gene' 158-164, 210-214
2688 MapList toMap = toLoci.getMap();
2689 assertEquals(1, toMap.getFromRanges().size());
2690 assertEquals(2, toMap.getFromRanges().get(0).length);
2691 assertEquals(1, toMap.getFromRanges().get(0)[0]);
2692 assertEquals(12, toMap.getFromRanges().get(0)[1]);
2693 assertEquals(2, toMap.getToRanges().size());
2694 assertEquals(2, toMap.getToRanges().get(0).length);
2695 assertEquals(158, toMap.getToRanges().get(0)[0]);
2696 assertEquals(164, toMap.getToRanges().get(0)[1]);
2697 assertEquals(210, toMap.getToRanges().get(1)[0]);
2698 assertEquals(214, toMap.getToRanges().get(1)[1]);
2699 // or summarised as (but toString might change in future):
2700 assertEquals("[ [1, 12] ] 1:1 to [ [158, 164] [210, 214] ]",
2704 * an existing value is not overridden
2706 geneMap = new MapList(new int[] { 1, 36 }, new int[] { 36, 1 }, 1, 1);
2707 from.setGeneLoci("inhuman", "GRCh37", "6", geneMap);
2708 AlignmentUtils.transferGeneLoci(from, map, to);
2709 assertEquals("GRCh38", toLoci.getAssemblyId());
2710 assertEquals("7", toLoci.getChromosomeId());
2711 toMap = toLoci.getMap();
2712 assertEquals("[ [1, 12] ] 1:1 to [ [158, 164] [210, 214] ]",
2717 * Tests for the method that maps nucleotide to protein based on CDS features
2719 @Test(groups = "Functional")
2720 public void testMapCdsToProtein()
2722 SequenceI peptide = new Sequence("pep", "KLQ");
2725 * Case 1: CDS 3 times length of peptide
2726 * NB method only checks lengths match, not translation
2728 SequenceI dna = new Sequence("dna", "AACGacgtCTCCT");
2729 dna.createDatasetSequence();
2730 dna.addSequenceFeature(new SequenceFeature("CDS", "", 1, 4, null));
2731 dna.addSequenceFeature(new SequenceFeature("CDS", "", 9, 13, null));
2732 MapList ml = AlignmentUtils.mapCdsToProtein(dna, peptide);
2733 assertEquals(3, ml.getFromRatio());
2734 assertEquals(1, ml.getToRatio());
2735 assertEquals("[[1, 3]]",
2736 Arrays.deepToString(ml.getToRanges().toArray()));
2737 assertEquals("[[1, 4], [9, 13]]",
2738 Arrays.deepToString(ml.getFromRanges().toArray()));
2741 * Case 2: CDS 3 times length of peptide + stop codon
2742 * (note code does not currently check trailing codon is a stop codon)
2744 dna = new Sequence("dna", "AACGacgtCTCCTCCC");
2745 dna.createDatasetSequence();
2746 dna.addSequenceFeature(new SequenceFeature("CDS", "", 1, 4, null));
2747 dna.addSequenceFeature(new SequenceFeature("CDS", "", 9, 16, null));
2748 ml = AlignmentUtils.mapCdsToProtein(dna, peptide);
2749 assertEquals(3, ml.getFromRatio());
2750 assertEquals(1, ml.getToRatio());
2751 assertEquals("[[1, 3]]",
2752 Arrays.deepToString(ml.getToRanges().toArray()));
2753 assertEquals("[[1, 4], [9, 13]]",
2754 Arrays.deepToString(ml.getFromRanges().toArray()));
2757 * Case 3: CDS longer than 3 * peptide + stop codon - no mapping is made
2759 dna = new Sequence("dna", "AACGacgtCTCCTTGATCA");
2760 dna.createDatasetSequence();
2761 dna.addSequenceFeature(new SequenceFeature("CDS", "", 1, 4, null));
2762 dna.addSequenceFeature(new SequenceFeature("CDS", "", 9, 19, null));
2763 ml = AlignmentUtils.mapCdsToProtein(dna, peptide);
2767 * Case 4: CDS shorter than 3 * peptide - no mapping is made
2769 dna = new Sequence("dna", "AACGacgtCTCC");
2770 dna.createDatasetSequence();
2771 dna.addSequenceFeature(new SequenceFeature("CDS", "", 1, 4, null));
2772 dna.addSequenceFeature(new SequenceFeature("CDS", "", 9, 12, null));
2773 ml = AlignmentUtils.mapCdsToProtein(dna, peptide);
2777 * Case 5: CDS 3 times length of peptide + part codon - mapping is truncated
2779 dna = new Sequence("dna", "AACGacgtCTCCTTG");
2780 dna.createDatasetSequence();
2781 dna.addSequenceFeature(new SequenceFeature("CDS", "", 1, 4, null));
2782 dna.addSequenceFeature(new SequenceFeature("CDS", "", 9, 15, null));
2783 ml = AlignmentUtils.mapCdsToProtein(dna, peptide);
2784 assertEquals(3, ml.getFromRatio());
2785 assertEquals(1, ml.getToRatio());
2786 assertEquals("[[1, 3]]",
2787 Arrays.deepToString(ml.getToRanges().toArray()));
2788 assertEquals("[[1, 4], [9, 13]]",
2789 Arrays.deepToString(ml.getFromRanges().toArray()));
2792 * Case 6: incomplete start codon corresponding to X in peptide
2794 dna = new Sequence("dna", "ACGacgtCTCCTTGG");
2795 dna.createDatasetSequence();
2796 SequenceFeature sf = new SequenceFeature("CDS", "", 1, 3, null);
2797 sf.setPhase("2"); // skip 2 positions (AC) to start of next codon (GCT)
2798 dna.addSequenceFeature(sf);
2799 dna.addSequenceFeature(new SequenceFeature("CDS", "", 8, 15, null));
2800 peptide = new Sequence("pep", "XLQ");
2801 ml = AlignmentUtils.mapCdsToProtein(dna, peptide);
2802 assertEquals("[[2, 3]]",
2803 Arrays.deepToString(ml.getToRanges().toArray()));
2804 assertEquals("[[3, 3], [8, 12]]",
2805 Arrays.deepToString(ml.getFromRanges().toArray()));
2809 * Tests for the method that locates the CDS sequence that has a mapping to
2810 * the given protein. That is, given a transcript-to-peptide mapping, find the
2811 * cds-to-peptide mapping that relates to both, and return the CDS sequence.
2814 public void testFindCdsForProtein()
2816 List<AlignedCodonFrame> mappings = new ArrayList<>();
2817 AlignedCodonFrame acf1 = new AlignedCodonFrame();
2820 SequenceI dna1 = new Sequence("dna1", "cgatATcgGCTATCTATGacg");
2821 dna1.createDatasetSequence();
2823 // NB we currently exclude STOP codon from CDS sequences
2824 // the test would need to change if this changes in future
2825 SequenceI cds1 = new Sequence("cds1", "ATGCTATCT");
2826 cds1.createDatasetSequence();
2828 SequenceI pep1 = new Sequence("pep1", "MLS");
2829 pep1.createDatasetSequence();
2830 List<AlignedCodonFrame> seqMappings = new ArrayList<>();
2831 MapList mapList = new MapList(
2833 { 5, 6, 9, 15 }, new int[] { 1, 3 }, 3, 1);
2834 Mapping dnaToPeptide = new Mapping(pep1.getDatasetSequence(), mapList);
2836 // add dna to peptide mapping
2837 seqMappings.add(acf1);
2838 acf1.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(),
2842 * first case - no dna-to-CDS mapping exists - search fails
2844 SequenceI seq = AlignmentUtils.findCdsForProtein(mappings, dna1,
2845 seqMappings, dnaToPeptide);
2849 * second case - CDS-to-peptide mapping exists but no dna-to-CDS
2852 // todo this test fails if the mapping is added to acf1, not acf2
2853 // need to tidy up use of lists of mappings in AlignedCodonFrame
2854 AlignedCodonFrame acf2 = new AlignedCodonFrame();
2856 MapList cdsToPeptideMapping = new MapList(new int[]
2857 { 1, 9 }, new int[] { 1, 3 }, 3, 1);
2858 acf2.addMap(cds1.getDatasetSequence(), pep1.getDatasetSequence(),
2859 cdsToPeptideMapping);
2860 assertNull(AlignmentUtils.findCdsForProtein(mappings, dna1, seqMappings,
2864 * third case - add dna-to-CDS mapping - CDS is now found!
2866 MapList dnaToCdsMapping = new MapList(new int[] { 5, 6, 9, 15 },
2869 acf1.addMap(dna1.getDatasetSequence(), cds1.getDatasetSequence(),
2871 seq = AlignmentUtils.findCdsForProtein(mappings, dna1, seqMappings,
2873 assertSame(seq, cds1.getDatasetSequence());
2877 * Tests for the method that locates the CDS sequence that has a mapping to
2878 * the given protein. That is, given a transcript-to-peptide mapping, find the
2879 * cds-to-peptide mapping that relates to both, and return the CDS sequence.
2880 * This test is for the case where transcript and CDS are the same length.
2883 public void testFindCdsForProtein_noUTR()
2885 List<AlignedCodonFrame> mappings = new ArrayList<>();
2886 AlignedCodonFrame acf1 = new AlignedCodonFrame();
2889 SequenceI dna1 = new Sequence("dna1", "ATGCTATCTTAA");
2890 dna1.createDatasetSequence();
2892 // NB we currently exclude STOP codon from CDS sequences
2893 // the test would need to change if this changes in future
2894 SequenceI cds1 = new Sequence("cds1", "ATGCTATCT");
2895 cds1.createDatasetSequence();
2897 SequenceI pep1 = new Sequence("pep1", "MLS");
2898 pep1.createDatasetSequence();
2899 List<AlignedCodonFrame> seqMappings = new ArrayList<>();
2900 MapList mapList = new MapList(
2902 { 1, 9 }, new int[] { 1, 3 }, 3, 1);
2903 Mapping dnaToPeptide = new Mapping(pep1.getDatasetSequence(), mapList);
2905 // add dna to peptide mapping
2906 seqMappings.add(acf1);
2907 acf1.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(),
2911 * first case - transcript lacks CDS features - it appears to be
2912 * the CDS sequence and is returned
2914 SequenceI seq = AlignmentUtils.findCdsForProtein(mappings, dna1,
2915 seqMappings, dnaToPeptide);
2916 assertSame(seq, dna1.getDatasetSequence());
2919 * second case - transcript has CDS feature - this means it is
2920 * not returned as a match for CDS (CDS sequences don't have CDS features)
2922 dna1.addSequenceFeature(
2923 new SequenceFeature(SequenceOntologyI.CDS, "cds", 1, 12, null));
2924 seq = AlignmentUtils.findCdsForProtein(mappings, dna1, seqMappings,
2929 * third case - CDS-to-peptide mapping exists but no dna-to-CDS
2932 // todo this test fails if the mapping is added to acf1, not acf2
2933 // need to tidy up use of lists of mappings in AlignedCodonFrame
2934 AlignedCodonFrame acf2 = new AlignedCodonFrame();
2936 MapList cdsToPeptideMapping = new MapList(new int[]
2937 { 1, 9 }, new int[] { 1, 3 }, 3, 1);
2938 acf2.addMap(cds1.getDatasetSequence(), pep1.getDatasetSequence(),
2939 cdsToPeptideMapping);
2940 assertNull(AlignmentUtils.findCdsForProtein(mappings, dna1, seqMappings,
2944 * fourth case - add dna-to-CDS mapping - CDS is now found!
2946 MapList dnaToCdsMapping = new MapList(new int[] { 1, 9 },
2949 acf1.addMap(dna1.getDatasetSequence(), cds1.getDatasetSequence(),
2951 seq = AlignmentUtils.findCdsForProtein(mappings, dna1, seqMappings,
2953 assertSame(seq, cds1.getDatasetSequence());