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.Mapping;
38 import jalview.datamodel.SearchResultMatchI;
39 import jalview.datamodel.SearchResultsI;
40 import jalview.datamodel.Sequence;
41 import jalview.datamodel.SequenceFeature;
42 import jalview.datamodel.SequenceI;
43 import jalview.datamodel.features.SequenceFeatures;
44 import jalview.gui.JvOptionPane;
45 import jalview.io.AppletFormatAdapter;
46 import jalview.io.DataSourceType;
47 import jalview.io.FileFormat;
48 import jalview.io.FileFormatI;
49 import jalview.io.FormatAdapter;
50 import jalview.io.gff.SequenceOntologyI;
51 import jalview.util.MapList;
52 import jalview.util.MappingUtils;
54 import java.io.IOException;
55 import java.util.ArrayList;
56 import java.util.Arrays;
57 import java.util.LinkedHashMap;
58 import java.util.List;
60 import java.util.TreeMap;
62 import org.testng.annotations.BeforeClass;
63 import org.testng.annotations.Test;
65 public class AlignmentUtilsTests
68 @BeforeClass(alwaysRun = true)
69 public void setUpJvOptionPane()
71 JvOptionPane.setInteractiveMode(false);
72 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
75 public static Sequence ts = new Sequence("short",
76 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm");
78 @Test(groups = { "Functional" })
79 public void testExpandContext()
81 AlignmentI al = new Alignment(new Sequence[] {});
82 for (int i = 4; i < 14; i += 2)
84 SequenceI s1 = ts.deriveSequence().getSubSequence(i, i + 7);
87 System.out.println(new AppletFormatAdapter().formatSequences(
90 for (int flnk = -1; flnk < 25; flnk++)
92 AlignmentI exp = AlignmentUtils.expandContext(al, flnk);
93 System.out.println("\nFlank size: " + flnk);
94 System.out.println(new AppletFormatAdapter().formatSequences(
95 FileFormat.Clustal, exp, true));
99 * Full expansion to complete sequences
101 for (SequenceI sq : exp.getSequences())
103 String ung = sq.getSequenceAsString().replaceAll("-+", "");
104 final String errorMsg = "Flanking sequence not the same as original dataset sequence.\n"
107 + sq.getDatasetSequence().getSequenceAsString();
108 assertTrue(errorMsg, ung.equalsIgnoreCase(sq.getDatasetSequence()
109 .getSequenceAsString()));
115 * Last sequence is fully expanded, others have leading gaps to match
117 assertTrue(exp.getSequenceAt(4).getSequenceAsString()
119 assertTrue(exp.getSequenceAt(3).getSequenceAsString()
120 .startsWith("--abc"));
121 assertTrue(exp.getSequenceAt(2).getSequenceAsString()
122 .startsWith("----abc"));
123 assertTrue(exp.getSequenceAt(1).getSequenceAsString()
124 .startsWith("------abc"));
125 assertTrue(exp.getSequenceAt(0).getSequenceAsString()
126 .startsWith("--------abc"));
132 * Test that annotations are correctly adjusted by expandContext
134 @Test(groups = { "Functional" })
135 public void testExpandContext_annotation()
137 AlignmentI al = new Alignment(new Sequence[] {});
138 SequenceI ds = new Sequence("Seq1", "ABCDEFGHI");
140 SequenceI seq1 = ds.deriveSequence().getSubSequence(3, 6);
141 al.addSequence(seq1);
144 * Annotate DEF with 4/5/6 respectively
146 Annotation[] anns = new Annotation[] { new Annotation(4),
147 new Annotation(5), new Annotation(6) };
148 AlignmentAnnotation ann = new AlignmentAnnotation("SS",
149 "secondary structure", anns);
150 seq1.addAlignmentAnnotation(ann);
153 * The annotations array should match aligned positions
155 assertEquals(3, ann.annotations.length);
156 assertEquals(4, ann.annotations[0].value, 0.001);
157 assertEquals(5, ann.annotations[1].value, 0.001);
158 assertEquals(6, ann.annotations[2].value, 0.001);
161 * Check annotation to sequence position mappings before expanding the
162 * sequence; these are set up in Sequence.addAlignmentAnnotation ->
163 * Annotation.setSequenceRef -> createSequenceMappings
165 assertNull(ann.getAnnotationForPosition(1));
166 assertNull(ann.getAnnotationForPosition(2));
167 assertNull(ann.getAnnotationForPosition(3));
168 assertEquals(4, ann.getAnnotationForPosition(4).value, 0.001);
169 assertEquals(5, ann.getAnnotationForPosition(5).value, 0.001);
170 assertEquals(6, ann.getAnnotationForPosition(6).value, 0.001);
171 assertNull(ann.getAnnotationForPosition(7));
172 assertNull(ann.getAnnotationForPosition(8));
173 assertNull(ann.getAnnotationForPosition(9));
176 * Expand the subsequence to the full sequence abcDEFghi
178 AlignmentI expanded = AlignmentUtils.expandContext(al, -1);
179 assertEquals("abcDEFghi", expanded.getSequenceAt(0)
180 .getSequenceAsString());
183 * Confirm the alignment and sequence have the same SS annotation,
184 * referencing the expanded sequence
186 ann = expanded.getSequenceAt(0).getAnnotation()[0];
187 assertSame(ann, expanded.getAlignmentAnnotation()[0]);
188 assertSame(expanded.getSequenceAt(0), ann.sequenceRef);
191 * The annotations array should have null values except for annotated
194 assertNull(ann.annotations[0]);
195 assertNull(ann.annotations[1]);
196 assertNull(ann.annotations[2]);
197 assertEquals(4, ann.annotations[3].value, 0.001);
198 assertEquals(5, ann.annotations[4].value, 0.001);
199 assertEquals(6, ann.annotations[5].value, 0.001);
200 assertNull(ann.annotations[6]);
201 assertNull(ann.annotations[7]);
202 assertNull(ann.annotations[8]);
205 * sequence position mappings should be unchanged
207 assertNull(ann.getAnnotationForPosition(1));
208 assertNull(ann.getAnnotationForPosition(2));
209 assertNull(ann.getAnnotationForPosition(3));
210 assertEquals(4, ann.getAnnotationForPosition(4).value, 0.001);
211 assertEquals(5, ann.getAnnotationForPosition(5).value, 0.001);
212 assertEquals(6, ann.getAnnotationForPosition(6).value, 0.001);
213 assertNull(ann.getAnnotationForPosition(7));
214 assertNull(ann.getAnnotationForPosition(8));
215 assertNull(ann.getAnnotationForPosition(9));
219 * Test method that returns a map of lists of sequences by sequence name.
221 * @throws IOException
223 @Test(groups = { "Functional" })
224 public void testGetSequencesByName() throws IOException
226 final String data = ">Seq1Name\nKQYL\n" + ">Seq2Name\nRFPW\n"
227 + ">Seq1Name\nABCD\n";
228 AlignmentI al = loadAlignment(data, FileFormat.Fasta);
229 Map<String, List<SequenceI>> map = AlignmentUtils
230 .getSequencesByName(al);
231 assertEquals(2, map.keySet().size());
232 assertEquals(2, map.get("Seq1Name").size());
233 assertEquals("KQYL", map.get("Seq1Name").get(0).getSequenceAsString());
234 assertEquals("ABCD", map.get("Seq1Name").get(1).getSequenceAsString());
235 assertEquals(1, map.get("Seq2Name").size());
236 assertEquals("RFPW", map.get("Seq2Name").get(0).getSequenceAsString());
240 * Helper method to load an alignment and ensure dataset sequences are set up.
246 * @throws IOException
248 protected AlignmentI loadAlignment(final String data, FileFormatI format)
251 AlignmentI a = new FormatAdapter().readFile(data,
252 DataSourceType.PASTE, format);
258 * Test mapping of protein to cDNA, for the case where we have no sequence
259 * cross-references, so mappings are made first-served 1-1 where sequences
262 * @throws IOException
264 @Test(groups = { "Functional" })
265 public void testMapProteinAlignmentToCdna_noXrefs() throws IOException
267 List<SequenceI> protseqs = new ArrayList<>();
268 protseqs.add(new Sequence("UNIPROT|V12345", "EIQ"));
269 protseqs.add(new Sequence("UNIPROT|V12346", "EIQ"));
270 protseqs.add(new Sequence("UNIPROT|V12347", "SAR"));
271 AlignmentI protein = new Alignment(protseqs.toArray(new SequenceI[3]));
272 protein.setDataset(null);
274 List<SequenceI> dnaseqs = new ArrayList<>();
275 dnaseqs.add(new Sequence("EMBL|A11111", "TCAGCACGC")); // = SAR
276 dnaseqs.add(new Sequence("EMBL|A22222", "GAGATACAA")); // = EIQ
277 dnaseqs.add(new Sequence("EMBL|A33333", "GAAATCCAG")); // = EIQ
278 dnaseqs.add(new Sequence("EMBL|A44444", "GAAATTCAG")); // = EIQ
279 AlignmentI cdna = new Alignment(dnaseqs.toArray(new SequenceI[4]));
280 cdna.setDataset(null);
282 assertTrue(AlignmentUtils.mapProteinAlignmentToCdna(protein, cdna));
284 // 3 mappings made, each from 1 to 1 sequence
285 assertEquals(3, protein.getCodonFrames().size());
286 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).size());
287 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).size());
288 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(2)).size());
290 // V12345 mapped to A22222
291 AlignedCodonFrame acf = protein.getCodonFrame(protein.getSequenceAt(0))
293 assertEquals(1, acf.getdnaSeqs().length);
294 assertEquals(cdna.getSequenceAt(1).getDatasetSequence(),
295 acf.getdnaSeqs()[0]);
296 Mapping[] protMappings = acf.getProtMappings();
297 assertEquals(1, protMappings.length);
298 MapList mapList = protMappings[0].getMap();
299 assertEquals(3, mapList.getFromRatio());
300 assertEquals(1, mapList.getToRatio());
301 assertTrue(Arrays.equals(new int[] { 1, 9 }, mapList.getFromRanges()
303 assertEquals(1, mapList.getFromRanges().size());
304 assertTrue(Arrays.equals(new int[] { 1, 3 },
305 mapList.getToRanges().get(0)));
306 assertEquals(1, mapList.getToRanges().size());
308 // V12346 mapped to A33333
309 acf = protein.getCodonFrame(protein.getSequenceAt(1)).get(0);
310 assertEquals(1, acf.getdnaSeqs().length);
311 assertEquals(cdna.getSequenceAt(2).getDatasetSequence(),
312 acf.getdnaSeqs()[0]);
314 // V12347 mapped to A11111
315 acf = protein.getCodonFrame(protein.getSequenceAt(2)).get(0);
316 assertEquals(1, acf.getdnaSeqs().length);
317 assertEquals(cdna.getSequenceAt(0).getDatasetSequence(),
318 acf.getdnaSeqs()[0]);
320 // no mapping involving the 'extra' A44444
321 assertTrue(protein.getCodonFrame(cdna.getSequenceAt(3)).isEmpty());
325 * Test for the alignSequenceAs method that takes two sequences and a mapping.
327 @Test(groups = { "Functional" })
328 public void testAlignSequenceAs_withMapping_noIntrons()
330 MapList map = new MapList(new int[] { 1, 6 }, new int[] { 1, 2 }, 3, 1);
333 * No existing gaps in dna:
335 checkAlignSequenceAs("GGGAAA", "-A-L-", false, false, map,
339 * Now introduce gaps in dna but ignore them when realigning.
341 checkAlignSequenceAs("-G-G-G-A-A-A-", "-A-L-", false, false, map,
345 * Now include gaps in dna when realigning. First retaining 'mapped' gaps
346 * only, i.e. those within the exon region.
348 checkAlignSequenceAs("-G-G--G-A--A-A-", "-A-L-", true, false, map,
349 "---G-G--G---A--A-A");
352 * Include all gaps in dna when realigning (within and without the exon
353 * region). The leading gap, and the gaps between codons, are subsumed by
354 * the protein alignment gap.
356 checkAlignSequenceAs("-G-GG--AA-A---", "-A-L-", true, true, map,
357 "---G-GG---AA-A---");
360 * Include only unmapped gaps in dna when realigning (outside the exon
361 * region). The leading gap, and the gaps between codons, are subsumed by
362 * the protein alignment gap.
364 checkAlignSequenceAs("-G-GG--AA-A-", "-A-L-", false, true, map,
369 * Test for the alignSequenceAs method that takes two sequences and a mapping.
371 @Test(groups = { "Functional" })
372 public void testAlignSequenceAs_withMapping_withIntrons()
375 * Exons at codon 2 (AAA) and 4 (TTT)
377 MapList map = new MapList(new int[] { 4, 6, 10, 12 },
378 new int[] { 1, 2 }, 3, 1);
381 * Simple case: no gaps in dna
383 checkAlignSequenceAs("GGGAAACCCTTTGGG", "--A-L-", false, false, map,
384 "GGG---AAACCCTTTGGG");
387 * Add gaps to dna - but ignore when realigning.
389 checkAlignSequenceAs("-G-G-G--A--A---AC-CC-T-TT-GG-G-", "--A-L-",
390 false, false, map, "GGG---AAACCCTTTGGG");
393 * Add gaps to dna - include within exons only when realigning.
395 checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
396 true, false, map, "GGG---A--A---ACCCT-TTGGG");
399 * Include gaps outside exons only when realigning.
401 checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
402 false, true, map, "-G-G-GAAAC-CCTTT-GG-G-");
405 * Include gaps following first intron if we are 'preserving mapped gaps'
407 checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
408 true, true, map, "-G-G-G--A--A---A-C-CC-T-TT-GG-G-");
411 * Include all gaps in dna when realigning.
413 checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
414 true, true, map, "-G-G-G--A--A---A-C-CC-T-TT-GG-G-");
418 * Test for the case where not all of the protein sequence is mapped to cDNA.
420 @Test(groups = { "Functional" })
421 public void testAlignSequenceAs_withMapping_withUnmappedProtein()
424 * Exons at codon 2 (AAA) and 4 (TTT) mapped to A and P
426 final MapList map = new MapList(new int[] { 4, 6, 10, 12 }, new int[] {
430 * -L- 'aligns' ccc------
432 checkAlignSequenceAs("gggAAAcccTTTggg", "-A-L-P-", false, false, map,
433 "gggAAAccc------TTTggg");
437 * Helper method that performs and verifies the method under test.
440 * the sequence to be realigned
442 * the sequence whose alignment is to be copied
443 * @param preserveMappedGaps
444 * @param preserveUnmappedGaps
448 protected void checkAlignSequenceAs(final String alignee,
449 final String alignModel, final boolean preserveMappedGaps,
450 final boolean preserveUnmappedGaps, MapList map,
451 final String expected)
453 SequenceI alignMe = new Sequence("Seq1", alignee);
454 alignMe.createDatasetSequence();
455 SequenceI alignFrom = new Sequence("Seq2", alignModel);
456 alignFrom.createDatasetSequence();
457 AlignedCodonFrame acf = new AlignedCodonFrame();
458 acf.addMap(alignMe.getDatasetSequence(),
459 alignFrom.getDatasetSequence(), map);
461 AlignmentUtils.alignSequenceAs(alignMe, alignFrom, acf, "---", '-',
462 preserveMappedGaps, preserveUnmappedGaps);
463 assertEquals(expected, alignMe.getSequenceAsString());
467 * Test for the alignSequenceAs method where we preserve gaps in introns only.
469 @Test(groups = { "Functional" })
470 public void testAlignSequenceAs_keepIntronGapsOnly()
474 * Intron GGGAAA followed by exon CCCTTT
476 MapList map = new MapList(new int[] { 7, 12 }, new int[] { 1, 2 }, 3, 1);
478 checkAlignSequenceAs("GG-G-AA-A-C-CC-T-TT", "AL", false, true, map,
483 * Test the method that realigns protein to match mapped codon alignment.
485 @Test(groups = { "Functional" })
486 public void testAlignProteinAsDna()
488 // seq1 codons are [1,2,3] [4,5,6] [7,8,9] [10,11,12]
489 SequenceI dna1 = new Sequence("Seq1", "TGCCATTACCAG-");
490 // seq2 codons are [1,3,4] [5,6,7] [8,9,10] [11,12,13]
491 SequenceI dna2 = new Sequence("Seq2", "T-GCCATTACCAG");
492 // seq3 codons are [1,2,3] [4,5,7] [8,9,10] [11,12,13]
493 SequenceI dna3 = new Sequence("Seq3", "TGCCA-TTACCAG");
494 AlignmentI dna = new Alignment(new SequenceI[] { dna1, dna2, dna3 });
495 dna.setDataset(null);
497 // protein alignment will be realigned like dna
498 SequenceI prot1 = new Sequence("Seq1", "CHYQ");
499 SequenceI prot2 = new Sequence("Seq2", "CHYQ");
500 SequenceI prot3 = new Sequence("Seq3", "CHYQ");
501 SequenceI prot4 = new Sequence("Seq4", "R-QSV"); // unmapped, unchanged
502 AlignmentI protein = new Alignment(new SequenceI[] { prot1, prot2,
504 protein.setDataset(null);
506 MapList map = new MapList(new int[] { 1, 12 }, new int[] { 1, 4 }, 3, 1);
507 AlignedCodonFrame acf = new AlignedCodonFrame();
508 acf.addMap(dna1.getDatasetSequence(), prot1.getDatasetSequence(), map);
509 acf.addMap(dna2.getDatasetSequence(), prot2.getDatasetSequence(), map);
510 acf.addMap(dna3.getDatasetSequence(), prot3.getDatasetSequence(), map);
511 ArrayList<AlignedCodonFrame> acfs = new ArrayList<>();
513 protein.setCodonFrames(acfs);
516 * Translated codon order is [1,2,3] [1,3,4] [4,5,6] [4,5,7] [5,6,7] [7,8,9]
517 * [8,9,10] [10,11,12] [11,12,13]
519 AlignmentUtils.alignProteinAsDna(protein, dna);
520 assertEquals("C-H--Y-Q-", prot1.getSequenceAsString());
521 assertEquals("-C--H-Y-Q", prot2.getSequenceAsString());
522 assertEquals("C--H--Y-Q", prot3.getSequenceAsString());
523 assertEquals("R-QSV", prot4.getSequenceAsString());
527 * Test the method that tests whether a CDNA sequence translates to a protein
530 @Test(groups = { "Functional" })
531 public void testTranslatesAs()
533 // null arguments check
534 assertFalse(AlignmentUtils.translatesAs(null, 0, null));
535 assertFalse(AlignmentUtils.translatesAs(new char[] { 't' }, 0, null));
536 assertFalse(AlignmentUtils.translatesAs(null, 0, new char[] { 'a' }));
538 // straight translation
539 assertTrue(AlignmentUtils.translatesAs("tttcccaaaggg".toCharArray(), 0,
540 "FPKG".toCharArray()));
541 // with extra start codon (not in protein)
542 assertTrue(AlignmentUtils.translatesAs("atgtttcccaaaggg".toCharArray(),
543 3, "FPKG".toCharArray()));
544 // with stop codon1 (not in protein)
545 assertTrue(AlignmentUtils.translatesAs("tttcccaaagggtaa".toCharArray(),
546 0, "FPKG".toCharArray()));
547 // with stop codon1 (in protein as *)
548 assertTrue(AlignmentUtils.translatesAs("tttcccaaagggtaa".toCharArray(),
549 0, "FPKG*".toCharArray()));
550 // with stop codon2 (not in protein)
551 assertTrue(AlignmentUtils.translatesAs("tttcccaaagggtag".toCharArray(),
552 0, "FPKG".toCharArray()));
553 // with stop codon3 (not in protein)
554 assertTrue(AlignmentUtils.translatesAs("tttcccaaagggtga".toCharArray(),
555 0, "FPKG".toCharArray()));
556 // with start and stop codon1
557 assertTrue(AlignmentUtils.translatesAs(
558 "atgtttcccaaagggtaa".toCharArray(), 3, "FPKG".toCharArray()));
559 // with start and stop codon1 (in protein as *)
560 assertTrue(AlignmentUtils.translatesAs(
561 "atgtttcccaaagggtaa".toCharArray(), 3, "FPKG*".toCharArray()));
562 // with start and stop codon2
563 assertTrue(AlignmentUtils.translatesAs(
564 "atgtttcccaaagggtag".toCharArray(), 3, "FPKG".toCharArray()));
565 // with start and stop codon3
566 assertTrue(AlignmentUtils.translatesAs(
567 "atgtttcccaaagggtga".toCharArray(), 3, "FPKG".toCharArray()));
569 // with embedded stop codons
570 assertTrue(AlignmentUtils.translatesAs(
571 "atgtttTAGcccaaaTAAgggtga".toCharArray(), 3,
572 "F*PK*G".toCharArray()));
575 assertFalse(AlignmentUtils.translatesAs("tttcccaaaggg".toCharArray(),
576 0, "FPMG".toCharArray()));
579 assertFalse(AlignmentUtils.translatesAs("tttcccaaagg".toCharArray(), 0,
580 "FPKG".toCharArray()));
583 assertFalse(AlignmentUtils.translatesAs("tttcccaaaggg".toCharArray(),
584 0, "FPK".toCharArray()));
586 // overlong dna (doesn't end in stop codon)
587 assertFalse(AlignmentUtils.translatesAs(
588 "tttcccaaagggttt".toCharArray(), 0, "FPKG".toCharArray()));
590 // dna + stop codon + more
591 assertFalse(AlignmentUtils.translatesAs(
592 "tttcccaaagggttaga".toCharArray(), 0, "FPKG".toCharArray()));
595 assertFalse(AlignmentUtils.translatesAs("tttcccaaaggg".toCharArray(),
596 0, "FPKGQ".toCharArray()));
600 * Test mapping of protein to cDNA, for cases where the cDNA has start and/or
601 * stop codons in addition to the protein coding sequence.
603 * @throws IOException
605 @Test(groups = { "Functional" })
606 public void testMapProteinAlignmentToCdna_withStartAndStopCodons()
609 List<SequenceI> protseqs = new ArrayList<>();
610 protseqs.add(new Sequence("UNIPROT|V12345", "EIQ"));
611 protseqs.add(new Sequence("UNIPROT|V12346", "EIQ"));
612 protseqs.add(new Sequence("UNIPROT|V12347", "SAR"));
613 AlignmentI protein = new Alignment(protseqs.toArray(new SequenceI[3]));
614 protein.setDataset(null);
616 List<SequenceI> dnaseqs = new ArrayList<>();
618 dnaseqs.add(new Sequence("EMBL|A11111", "ATGTCAGCACGC"));
620 dnaseqs.add(new Sequence("EMBL|A22222", "GAGATACAATAA"));
621 // = start +EIQ + stop
622 dnaseqs.add(new Sequence("EMBL|A33333", "ATGGAAATCCAGTAG"));
623 dnaseqs.add(new Sequence("EMBL|A44444", "GAAATTCAG"));
624 AlignmentI cdna = new Alignment(dnaseqs.toArray(new SequenceI[4]));
625 cdna.setDataset(null);
627 assertTrue(AlignmentUtils.mapProteinAlignmentToCdna(protein, cdna));
629 // 3 mappings made, each from 1 to 1 sequence
630 assertEquals(3, protein.getCodonFrames().size());
631 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).size());
632 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).size());
633 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(2)).size());
635 // V12345 mapped from A22222
636 AlignedCodonFrame acf = protein.getCodonFrame(protein.getSequenceAt(0))
638 assertEquals(1, acf.getdnaSeqs().length);
639 assertEquals(cdna.getSequenceAt(1).getDatasetSequence(),
640 acf.getdnaSeqs()[0]);
641 Mapping[] protMappings = acf.getProtMappings();
642 assertEquals(1, protMappings.length);
643 MapList mapList = protMappings[0].getMap();
644 assertEquals(3, mapList.getFromRatio());
645 assertEquals(1, mapList.getToRatio());
646 assertTrue(Arrays.equals(new int[] { 1, 9 }, mapList.getFromRanges()
648 assertEquals(1, mapList.getFromRanges().size());
649 assertTrue(Arrays.equals(new int[] { 1, 3 },
650 mapList.getToRanges().get(0)));
651 assertEquals(1, mapList.getToRanges().size());
653 // V12346 mapped from A33333 starting position 4
654 acf = protein.getCodonFrame(protein.getSequenceAt(1)).get(0);
655 assertEquals(1, acf.getdnaSeqs().length);
656 assertEquals(cdna.getSequenceAt(2).getDatasetSequence(),
657 acf.getdnaSeqs()[0]);
658 protMappings = acf.getProtMappings();
659 assertEquals(1, protMappings.length);
660 mapList = protMappings[0].getMap();
661 assertEquals(3, mapList.getFromRatio());
662 assertEquals(1, mapList.getToRatio());
663 assertTrue(Arrays.equals(new int[] { 4, 12 }, mapList.getFromRanges()
665 assertEquals(1, mapList.getFromRanges().size());
666 assertTrue(Arrays.equals(new int[] { 1, 3 },
667 mapList.getToRanges().get(0)));
668 assertEquals(1, mapList.getToRanges().size());
670 // V12347 mapped to A11111 starting position 4
671 acf = protein.getCodonFrame(protein.getSequenceAt(2)).get(0);
672 assertEquals(1, acf.getdnaSeqs().length);
673 assertEquals(cdna.getSequenceAt(0).getDatasetSequence(),
674 acf.getdnaSeqs()[0]);
675 protMappings = acf.getProtMappings();
676 assertEquals(1, protMappings.length);
677 mapList = protMappings[0].getMap();
678 assertEquals(3, mapList.getFromRatio());
679 assertEquals(1, mapList.getToRatio());
680 assertTrue(Arrays.equals(new int[] { 4, 12 }, mapList.getFromRanges()
682 assertEquals(1, mapList.getFromRanges().size());
683 assertTrue(Arrays.equals(new int[] { 1, 3 },
684 mapList.getToRanges().get(0)));
685 assertEquals(1, mapList.getToRanges().size());
687 // no mapping involving the 'extra' A44444
688 assertTrue(protein.getCodonFrame(cdna.getSequenceAt(3)).isEmpty());
692 * Test mapping of protein to cDNA, for the case where we have some sequence
693 * cross-references. Verify that 1-to-many mappings are made where
694 * cross-references exist and sequences are mappable.
696 * @throws IOException
698 @Test(groups = { "Functional" })
699 public void testMapProteinAlignmentToCdna_withXrefs() throws IOException
701 List<SequenceI> protseqs = new ArrayList<>();
702 protseqs.add(new Sequence("UNIPROT|V12345", "EIQ"));
703 protseqs.add(new Sequence("UNIPROT|V12346", "EIQ"));
704 protseqs.add(new Sequence("UNIPROT|V12347", "SAR"));
705 AlignmentI protein = new Alignment(protseqs.toArray(new SequenceI[3]));
706 protein.setDataset(null);
708 List<SequenceI> dnaseqs = new ArrayList<>();
709 dnaseqs.add(new Sequence("EMBL|A11111", "TCAGCACGC")); // = SAR
710 dnaseqs.add(new Sequence("EMBL|A22222", "ATGGAGATACAA")); // = start + EIQ
711 dnaseqs.add(new Sequence("EMBL|A33333", "GAAATCCAG")); // = EIQ
712 dnaseqs.add(new Sequence("EMBL|A44444", "GAAATTCAG")); // = EIQ
713 dnaseqs.add(new Sequence("EMBL|A55555", "GAGATTCAG")); // = EIQ
714 AlignmentI cdna = new Alignment(dnaseqs.toArray(new SequenceI[5]));
715 cdna.setDataset(null);
717 // Xref A22222 to V12345 (should get mapped)
718 dnaseqs.get(1).addDBRef(new DBRefEntry("UNIPROT", "1", "V12345"));
719 // Xref V12345 to A44444 (should get mapped)
720 protseqs.get(0).addDBRef(new DBRefEntry("EMBL", "1", "A44444"));
721 // Xref A33333 to V12347 (sequence mismatch - should not get mapped)
722 dnaseqs.get(2).addDBRef(new DBRefEntry("UNIPROT", "1", "V12347"));
723 // as V12345 is mapped to A22222 and A44444, this leaves V12346 unmapped.
724 // it should get paired up with the unmapped A33333
725 // A11111 should be mapped to V12347
726 // A55555 is spare and has no xref so is not mapped
728 assertTrue(AlignmentUtils.mapProteinAlignmentToCdna(protein, cdna));
730 // 4 protein mappings made for 3 proteins, 2 to V12345, 1 each to V12346/7
731 assertEquals(3, protein.getCodonFrames().size());
732 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).size());
733 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).size());
734 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(2)).size());
736 // one mapping for each of the first 4 cDNA sequences
737 assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(0)).size());
738 assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(1)).size());
739 assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(2)).size());
740 assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(3)).size());
742 // V12345 mapped to A22222 and A44444
743 AlignedCodonFrame acf = protein.getCodonFrame(protein.getSequenceAt(0))
745 assertEquals(2, acf.getdnaSeqs().length);
746 assertEquals(cdna.getSequenceAt(1).getDatasetSequence(),
747 acf.getdnaSeqs()[0]);
748 assertEquals(cdna.getSequenceAt(3).getDatasetSequence(),
749 acf.getdnaSeqs()[1]);
751 // V12346 mapped to A33333
752 acf = protein.getCodonFrame(protein.getSequenceAt(1)).get(0);
753 assertEquals(1, acf.getdnaSeqs().length);
754 assertEquals(cdna.getSequenceAt(2).getDatasetSequence(),
755 acf.getdnaSeqs()[0]);
757 // V12347 mapped to A11111
758 acf = protein.getCodonFrame(protein.getSequenceAt(2)).get(0);
759 assertEquals(1, acf.getdnaSeqs().length);
760 assertEquals(cdna.getSequenceAt(0).getDatasetSequence(),
761 acf.getdnaSeqs()[0]);
763 // no mapping involving the 'extra' A55555
764 assertTrue(protein.getCodonFrame(cdna.getSequenceAt(4)).isEmpty());
768 * Test mapping of protein to cDNA, for the case where we have some sequence
769 * cross-references. Verify that once we have made an xref mapping we don't
770 * also map un-xrefd sequeces.
772 * @throws IOException
774 @Test(groups = { "Functional" })
775 public void testMapProteinAlignmentToCdna_prioritiseXrefs()
778 List<SequenceI> protseqs = new ArrayList<>();
779 protseqs.add(new Sequence("UNIPROT|V12345", "EIQ"));
780 protseqs.add(new Sequence("UNIPROT|V12346", "EIQ"));
781 AlignmentI protein = new Alignment(
782 protseqs.toArray(new SequenceI[protseqs.size()]));
783 protein.setDataset(null);
785 List<SequenceI> dnaseqs = new ArrayList<>();
786 dnaseqs.add(new Sequence("EMBL|A11111", "GAAATCCAG")); // = EIQ
787 dnaseqs.add(new Sequence("EMBL|A22222", "GAAATTCAG")); // = EIQ
788 AlignmentI cdna = new Alignment(dnaseqs.toArray(new SequenceI[dnaseqs
790 cdna.setDataset(null);
792 // Xref A22222 to V12345 (should get mapped)
793 // A11111 should then be mapped to the unmapped V12346
794 dnaseqs.get(1).addDBRef(new DBRefEntry("UNIPROT", "1", "V12345"));
796 assertTrue(AlignmentUtils.mapProteinAlignmentToCdna(protein, cdna));
798 // 2 protein mappings made
799 assertEquals(2, protein.getCodonFrames().size());
800 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).size());
801 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).size());
803 // one mapping for each of the cDNA sequences
804 assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(0)).size());
805 assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(1)).size());
807 // V12345 mapped to A22222
808 AlignedCodonFrame acf = protein.getCodonFrame(protein.getSequenceAt(0))
810 assertEquals(1, acf.getdnaSeqs().length);
811 assertEquals(cdna.getSequenceAt(1).getDatasetSequence(),
812 acf.getdnaSeqs()[0]);
814 // V12346 mapped to A11111
815 acf = protein.getCodonFrame(protein.getSequenceAt(1)).get(0);
816 assertEquals(1, acf.getdnaSeqs().length);
817 assertEquals(cdna.getSequenceAt(0).getDatasetSequence(),
818 acf.getdnaSeqs()[0]);
822 * Test the method that shows or hides sequence annotations by type(s) and
825 @Test(groups = { "Functional" })
826 public void testShowOrHideSequenceAnnotations()
828 SequenceI seq1 = new Sequence("Seq1", "AAA");
829 SequenceI seq2 = new Sequence("Seq2", "BBB");
830 SequenceI seq3 = new Sequence("Seq3", "CCC");
831 Annotation[] anns = new Annotation[] { new Annotation(2f) };
832 AlignmentAnnotation ann1 = new AlignmentAnnotation("Structure", "ann1",
834 ann1.setSequenceRef(seq1);
835 AlignmentAnnotation ann2 = new AlignmentAnnotation("Structure", "ann2",
837 ann2.setSequenceRef(seq2);
838 AlignmentAnnotation ann3 = new AlignmentAnnotation("Structure", "ann3",
840 AlignmentAnnotation ann4 = new AlignmentAnnotation("Temp", "ann4", anns);
841 ann4.setSequenceRef(seq1);
842 AlignmentAnnotation ann5 = new AlignmentAnnotation("Temp", "ann5", anns);
843 ann5.setSequenceRef(seq2);
844 AlignmentAnnotation ann6 = new AlignmentAnnotation("Temp", "ann6", anns);
845 AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2, seq3 });
846 al.addAnnotation(ann1); // Structure for Seq1
847 al.addAnnotation(ann2); // Structure for Seq2
848 al.addAnnotation(ann3); // Structure for no sequence
849 al.addAnnotation(ann4); // Temp for seq1
850 al.addAnnotation(ann5); // Temp for seq2
851 al.addAnnotation(ann6); // Temp for no sequence
852 List<String> types = new ArrayList<>();
853 List<SequenceI> scope = new ArrayList<>();
856 * Set all sequence related Structure to hidden (ann1, ann2)
858 types.add("Structure");
859 AlignmentUtils.showOrHideSequenceAnnotations(al, types, null, false,
861 assertFalse(ann1.visible);
862 assertFalse(ann2.visible);
863 assertTrue(ann3.visible); // not sequence-related, not affected
864 assertTrue(ann4.visible); // not Structure, not affected
865 assertTrue(ann5.visible); // "
866 assertTrue(ann6.visible); // not sequence-related, not affected
869 * Set Temp in {seq1, seq3} to hidden
875 AlignmentUtils.showOrHideSequenceAnnotations(al, types, scope, false,
877 assertFalse(ann1.visible); // unchanged
878 assertFalse(ann2.visible); // unchanged
879 assertTrue(ann3.visible); // not sequence-related, not affected
880 assertFalse(ann4.visible); // Temp for seq1 hidden
881 assertTrue(ann5.visible); // not in scope, not affected
882 assertTrue(ann6.visible); // not sequence-related, not affected
885 * Set Temp in all sequences to hidden
891 AlignmentUtils.showOrHideSequenceAnnotations(al, types, null, false,
893 assertFalse(ann1.visible); // unchanged
894 assertFalse(ann2.visible); // unchanged
895 assertTrue(ann3.visible); // not sequence-related, not affected
896 assertFalse(ann4.visible); // Temp for seq1 hidden
897 assertFalse(ann5.visible); // Temp for seq2 hidden
898 assertTrue(ann6.visible); // not sequence-related, not affected
901 * Set all types in {seq1, seq3} to visible
907 AlignmentUtils.showOrHideSequenceAnnotations(al, types, scope, true,
909 assertTrue(ann1.visible); // Structure for seq1 set visible
910 assertFalse(ann2.visible); // not in scope, unchanged
911 assertTrue(ann3.visible); // not sequence-related, not affected
912 assertTrue(ann4.visible); // Temp for seq1 set visible
913 assertFalse(ann5.visible); // not in scope, unchanged
914 assertTrue(ann6.visible); // not sequence-related, not affected
917 * Set all types in all scope to hidden
919 AlignmentUtils.showOrHideSequenceAnnotations(al, types, null, true,
921 assertFalse(ann1.visible);
922 assertFalse(ann2.visible);
923 assertTrue(ann3.visible); // not sequence-related, not affected
924 assertFalse(ann4.visible);
925 assertFalse(ann5.visible);
926 assertTrue(ann6.visible); // not sequence-related, not affected
930 * Tests for the method that checks if one sequence cross-references another
932 @Test(groups = { "Functional" })
933 public void testHasCrossRef()
935 assertFalse(AlignmentUtils.hasCrossRef(null, null));
936 SequenceI seq1 = new Sequence("EMBL|A12345", "ABCDEF");
937 assertFalse(AlignmentUtils.hasCrossRef(seq1, null));
938 assertFalse(AlignmentUtils.hasCrossRef(null, seq1));
939 SequenceI seq2 = new Sequence("UNIPROT|V20192", "ABCDEF");
940 assertFalse(AlignmentUtils.hasCrossRef(seq1, seq2));
943 seq1.addDBRef(new DBRefEntry("UNIPROT", "1", "v20193"));
944 assertFalse(AlignmentUtils.hasCrossRef(seq1, seq2));
946 // case-insensitive; version number is ignored
947 seq1.addDBRef(new DBRefEntry("UNIPROT", "1", "v20192"));
948 assertTrue(AlignmentUtils.hasCrossRef(seq1, seq2));
951 seq1.addDBRef(new DBRefEntry("UNIPROT", "1", "V20192"));
952 assertTrue(AlignmentUtils.hasCrossRef(seq1, seq2));
953 // test is one-way only
954 assertFalse(AlignmentUtils.hasCrossRef(seq2, seq1));
958 * Tests for the method that checks if either sequence cross-references the
961 @Test(groups = { "Functional" })
962 public void testHaveCrossRef()
964 assertFalse(AlignmentUtils.hasCrossRef(null, null));
965 SequenceI seq1 = new Sequence("EMBL|A12345", "ABCDEF");
966 assertFalse(AlignmentUtils.haveCrossRef(seq1, null));
967 assertFalse(AlignmentUtils.haveCrossRef(null, seq1));
968 SequenceI seq2 = new Sequence("UNIPROT|V20192", "ABCDEF");
969 assertFalse(AlignmentUtils.haveCrossRef(seq1, seq2));
971 seq1.addDBRef(new DBRefEntry("UNIPROT", "1", "V20192"));
972 assertTrue(AlignmentUtils.haveCrossRef(seq1, seq2));
973 // next is true for haveCrossRef, false for hasCrossRef
974 assertTrue(AlignmentUtils.haveCrossRef(seq2, seq1));
976 // now the other way round
977 seq1.setDBRefs(null);
978 seq2.addDBRef(new DBRefEntry("EMBL", "1", "A12345"));
979 assertTrue(AlignmentUtils.haveCrossRef(seq1, seq2));
980 assertTrue(AlignmentUtils.haveCrossRef(seq2, seq1));
983 seq1.addDBRef(new DBRefEntry("UNIPROT", "1", "V20192"));
984 assertTrue(AlignmentUtils.haveCrossRef(seq1, seq2));
985 assertTrue(AlignmentUtils.haveCrossRef(seq2, seq1));
989 * Test the method that extracts the cds-only part of a dna alignment.
991 @Test(groups = { "Functional" })
992 public void testMakeCdsAlignment()
996 * dna1 --> [4, 6] [10,12] --> pep1
997 * dna2 --> [1, 3] [7, 9] [13,15] --> pep2
999 SequenceI dna1 = new Sequence("dna1", "aaaGGGcccTTTaaa");
1000 SequenceI dna2 = new Sequence("dna2", "GGGcccTTTaaaCCC");
1001 SequenceI pep1 = new Sequence("pep1", "GF");
1002 SequenceI pep2 = new Sequence("pep2", "GFP");
1003 pep1.addDBRef(new DBRefEntry("UNIPROT", "0", "pep1"));
1004 pep2.addDBRef(new DBRefEntry("UNIPROT", "0", "pep2"));
1005 dna1.createDatasetSequence();
1006 dna2.createDatasetSequence();
1007 pep1.createDatasetSequence();
1008 pep2.createDatasetSequence();
1009 AlignmentI dna = new Alignment(new SequenceI[] { dna1, dna2 });
1010 dna.setDataset(null);
1013 * put a variant feature on dna2 base 8
1014 * - should transfer to cds2 base 5
1016 dna2.addSequenceFeature(new SequenceFeature("variant", "hgmd", 8, 8,
1020 * need a sourceDbRef if we are to construct dbrefs to the CDS
1021 * sequence from the dna contig sequences
1023 DBRefEntry dbref = new DBRefEntry("ENSEMBL", "0", "dna1");
1024 dna1.getDatasetSequence().addDBRef(dbref);
1025 org.testng.Assert.assertEquals(dbref, dna1.getPrimaryDBRefs().get(0));
1026 dbref = new DBRefEntry("ENSEMBL", "0", "dna2");
1027 dna2.getDatasetSequence().addDBRef(dbref);
1028 org.testng.Assert.assertEquals(dbref, dna2.getPrimaryDBRefs().get(0));
1031 * CDS sequences are 'discovered' from dna-to-protein mappings on the alignment
1032 * dataset (e.g. added from dbrefs by CrossRef.findXrefSequences)
1034 MapList mapfordna1 = new MapList(new int[] { 4, 6, 10, 12 }, new int[] {
1036 AlignedCodonFrame acf = new AlignedCodonFrame();
1037 acf.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(),
1039 dna.addCodonFrame(acf);
1040 MapList mapfordna2 = new MapList(new int[] { 1, 3, 7, 9, 13, 15 },
1041 new int[] { 1, 3 }, 3, 1);
1042 acf = new AlignedCodonFrame();
1043 acf.addMap(dna2.getDatasetSequence(), pep2.getDatasetSequence(),
1045 dna.addCodonFrame(acf);
1048 * In this case, mappings originally came from matching Uniprot accessions - so need an xref on dna involving those regions. These are normally constructed from CDS annotation
1050 DBRefEntry dna1xref = new DBRefEntry("UNIPROT", "ENSEMBL", "pep1",
1051 new Mapping(mapfordna1));
1052 dna1.getDatasetSequence().addDBRef(dna1xref);
1053 DBRefEntry dna2xref = new DBRefEntry("UNIPROT", "ENSEMBL", "pep2",
1054 new Mapping(mapfordna2));
1055 dna2.getDatasetSequence().addDBRef(dna2xref);
1058 * execute method under test:
1060 AlignmentI cds = AlignmentUtils.makeCdsAlignment(new SequenceI[] {
1061 dna1, dna2 }, dna.getDataset(), null);
1064 * verify cds sequences
1066 assertEquals(2, cds.getSequences().size());
1067 assertEquals("GGGTTT", cds.getSequenceAt(0).getSequenceAsString());
1068 assertEquals("GGGTTTCCC", cds.getSequenceAt(1).getSequenceAsString());
1071 * verify shared, extended alignment dataset
1073 assertSame(dna.getDataset(), cds.getDataset());
1074 SequenceI cds1Dss = cds.getSequenceAt(0).getDatasetSequence();
1075 SequenceI cds2Dss = cds.getSequenceAt(1).getDatasetSequence();
1076 assertTrue(dna.getDataset().getSequences().contains(cds1Dss));
1077 assertTrue(dna.getDataset().getSequences().contains(cds2Dss));
1080 * verify CDS has a dbref with mapping to peptide
1082 assertNotNull(cds1Dss.getDBRefs());
1083 assertEquals(2, cds1Dss.getDBRefs().length);
1084 dbref = cds1Dss.getDBRefs()[0];
1085 assertEquals(dna1xref.getSource(), dbref.getSource());
1086 // version is via ensembl's primary ref
1087 assertEquals(dna1xref.getVersion(), dbref.getVersion());
1088 assertEquals(dna1xref.getAccessionId(), dbref.getAccessionId());
1089 assertNotNull(dbref.getMap());
1090 assertSame(pep1.getDatasetSequence(), dbref.getMap().getTo());
1091 MapList cdsMapping = new MapList(new int[] { 1, 6 },
1092 new int[] { 1, 2 }, 3, 1);
1093 assertEquals(cdsMapping, dbref.getMap().getMap());
1096 * verify peptide has added a dbref with reverse mapping to CDS
1098 assertNotNull(pep1.getDBRefs());
1099 // FIXME pep1.getDBRefs() is 1 - is that the correct behaviour ?
1100 assertEquals(2, pep1.getDBRefs().length);
1101 dbref = pep1.getDBRefs()[1];
1102 assertEquals("ENSEMBL", dbref.getSource());
1103 assertEquals("0", dbref.getVersion());
1104 assertEquals("CDS|dna1", dbref.getAccessionId());
1105 assertNotNull(dbref.getMap());
1106 assertSame(cds1Dss, dbref.getMap().getTo());
1107 assertEquals(cdsMapping.getInverse(), dbref.getMap().getMap());
1110 * Verify mappings from CDS to peptide, cDNA to CDS, and cDNA to peptide
1111 * the mappings are on the shared alignment dataset
1112 * 6 mappings, 2*(DNA->CDS), 2*(DNA->Pep), 2*(CDS->Pep)
1114 List<AlignedCodonFrame> cdsMappings = cds.getDataset().getCodonFrames();
1115 assertEquals(6, cdsMappings.size());
1118 * verify that mapping sets for dna and cds alignments are different
1119 * [not current behaviour - all mappings are on the alignment dataset]
1121 // select -> subselect type to test.
1122 // Assert.assertNotSame(dna.getCodonFrames(), cds.getCodonFrames());
1123 // assertEquals(4, dna.getCodonFrames().size());
1124 // assertEquals(4, cds.getCodonFrames().size());
1127 * Two mappings involve pep1 (dna to pep1, cds to pep1)
1128 * Mapping from pep1 to GGGTTT in first new exon sequence
1130 List<AlignedCodonFrame> pep1Mappings = MappingUtils
1131 .findMappingsForSequence(pep1, cdsMappings);
1132 assertEquals(2, pep1Mappings.size());
1133 List<AlignedCodonFrame> mappings = MappingUtils
1134 .findMappingsForSequence(cds.getSequenceAt(0), pep1Mappings);
1135 assertEquals(1, mappings.size());
1138 SearchResultsI sr = MappingUtils.buildSearchResults(pep1, 1, mappings);
1139 assertEquals(1, sr.getResults().size());
1140 SearchResultMatchI m = sr.getResults().get(0);
1141 assertSame(cds1Dss, m.getSequence());
1142 assertEquals(1, m.getStart());
1143 assertEquals(3, m.getEnd());
1145 sr = MappingUtils.buildSearchResults(pep1, 2, mappings);
1146 m = sr.getResults().get(0);
1147 assertSame(cds1Dss, m.getSequence());
1148 assertEquals(4, m.getStart());
1149 assertEquals(6, m.getEnd());
1152 * Two mappings involve pep2 (dna to pep2, cds to pep2)
1153 * Verify mapping from pep2 to GGGTTTCCC in second new exon sequence
1155 List<AlignedCodonFrame> pep2Mappings = MappingUtils
1156 .findMappingsForSequence(pep2, cdsMappings);
1157 assertEquals(2, pep2Mappings.size());
1158 mappings = MappingUtils.findMappingsForSequence(cds.getSequenceAt(1),
1160 assertEquals(1, mappings.size());
1162 sr = MappingUtils.buildSearchResults(pep2, 1, mappings);
1163 assertEquals(1, sr.getResults().size());
1164 m = sr.getResults().get(0);
1165 assertSame(cds2Dss, m.getSequence());
1166 assertEquals(1, m.getStart());
1167 assertEquals(3, m.getEnd());
1169 sr = MappingUtils.buildSearchResults(pep2, 2, mappings);
1170 m = sr.getResults().get(0);
1171 assertSame(cds2Dss, m.getSequence());
1172 assertEquals(4, m.getStart());
1173 assertEquals(6, m.getEnd());
1175 sr = MappingUtils.buildSearchResults(pep2, 3, mappings);
1176 m = sr.getResults().get(0);
1177 assertSame(cds2Dss, m.getSequence());
1178 assertEquals(7, m.getStart());
1179 assertEquals(9, m.getEnd());
1182 * check cds2 acquired a variant feature in position 5
1184 List<SequenceFeature> sfs = cds2Dss.getSequenceFeatures();
1186 assertEquals(1, sfs.size());
1187 assertEquals("variant", sfs.get(0).type);
1188 assertEquals(5, sfs.get(0).begin);
1189 assertEquals(5, sfs.get(0).end);
1193 * Test the method that makes a cds-only alignment from a DNA sequence and its
1194 * product mappings, for the case where there are multiple exon mappings to
1195 * different protein products.
1197 @Test(groups = { "Functional" })
1198 public void testMakeCdsAlignment_multipleProteins()
1200 SequenceI dna1 = new Sequence("dna1", "aaaGGGcccTTTaaa");
1201 SequenceI pep1 = new Sequence("pep1", "GF"); // GGGTTT
1202 SequenceI pep2 = new Sequence("pep2", "KP"); // aaaccc
1203 SequenceI pep3 = new Sequence("pep3", "KF"); // aaaTTT
1204 dna1.createDatasetSequence();
1205 pep1.createDatasetSequence();
1206 pep2.createDatasetSequence();
1207 pep3.createDatasetSequence();
1208 pep1.getDatasetSequence().addDBRef(
1209 new DBRefEntry("EMBLCDS", "2", "A12345"));
1210 pep2.getDatasetSequence().addDBRef(
1211 new DBRefEntry("EMBLCDS", "3", "A12346"));
1212 pep3.getDatasetSequence().addDBRef(
1213 new DBRefEntry("EMBLCDS", "4", "A12347"));
1216 * Create the CDS alignment
1218 AlignmentI dna = new Alignment(new SequenceI[] { dna1 });
1219 dna.setDataset(null);
1222 * Make the mappings from dna to protein
1224 // map ...GGG...TTT to GF
1225 MapList map = new MapList(new int[] { 4, 6, 10, 12 },
1226 new int[] { 1, 2 }, 3, 1);
1227 AlignedCodonFrame acf = new AlignedCodonFrame();
1228 acf.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(), map);
1229 dna.addCodonFrame(acf);
1231 // map aaa...ccc to KP
1232 map = new MapList(new int[] { 1, 3, 7, 9 }, new int[] { 1, 2 }, 3, 1);
1233 acf = new AlignedCodonFrame();
1234 acf.addMap(dna1.getDatasetSequence(), pep2.getDatasetSequence(), map);
1235 dna.addCodonFrame(acf);
1237 // map aaa......TTT to KF
1238 map = new MapList(new int[] { 1, 3, 10, 12 }, new int[] { 1, 2 }, 3, 1);
1239 acf = new AlignedCodonFrame();
1240 acf.addMap(dna1.getDatasetSequence(), pep3.getDatasetSequence(), map);
1241 dna.addCodonFrame(acf);
1244 * execute method under test
1246 AlignmentI cdsal = AlignmentUtils.makeCdsAlignment(
1247 new SequenceI[] { dna1 }, dna.getDataset(), null);
1250 * Verify we have 3 cds sequences, mapped to pep1/2/3 respectively
1252 List<SequenceI> cds = cdsal.getSequences();
1253 assertEquals(3, cds.size());
1256 * verify shared, extended alignment dataset
1258 assertSame(cdsal.getDataset(), dna.getDataset());
1259 assertTrue(dna.getDataset().getSequences()
1260 .contains(cds.get(0).getDatasetSequence()));
1261 assertTrue(dna.getDataset().getSequences()
1262 .contains(cds.get(1).getDatasetSequence()));
1263 assertTrue(dna.getDataset().getSequences()
1264 .contains(cds.get(2).getDatasetSequence()));
1267 * verify aligned cds sequences and their xrefs
1269 SequenceI cdsSeq = cds.get(0);
1270 assertEquals("GGGTTT", cdsSeq.getSequenceAsString());
1271 // assertEquals("dna1|A12345", cdsSeq.getName());
1272 assertEquals("CDS|dna1", cdsSeq.getName());
1273 // assertEquals(1, cdsSeq.getDBRefs().length);
1274 // DBRefEntry cdsRef = cdsSeq.getDBRefs()[0];
1275 // assertEquals("EMBLCDS", cdsRef.getSource());
1276 // assertEquals("2", cdsRef.getVersion());
1277 // assertEquals("A12345", cdsRef.getAccessionId());
1279 cdsSeq = cds.get(1);
1280 assertEquals("aaaccc", cdsSeq.getSequenceAsString());
1281 // assertEquals("dna1|A12346", cdsSeq.getName());
1282 assertEquals("CDS|dna1", cdsSeq.getName());
1283 // assertEquals(1, cdsSeq.getDBRefs().length);
1284 // cdsRef = cdsSeq.getDBRefs()[0];
1285 // assertEquals("EMBLCDS", cdsRef.getSource());
1286 // assertEquals("3", cdsRef.getVersion());
1287 // assertEquals("A12346", cdsRef.getAccessionId());
1289 cdsSeq = cds.get(2);
1290 assertEquals("aaaTTT", cdsSeq.getSequenceAsString());
1291 // assertEquals("dna1|A12347", cdsSeq.getName());
1292 assertEquals("CDS|dna1", cdsSeq.getName());
1293 // assertEquals(1, cdsSeq.getDBRefs().length);
1294 // cdsRef = cdsSeq.getDBRefs()[0];
1295 // assertEquals("EMBLCDS", cdsRef.getSource());
1296 // assertEquals("4", cdsRef.getVersion());
1297 // assertEquals("A12347", cdsRef.getAccessionId());
1300 * Verify there are mappings from each cds sequence to its protein product
1301 * and also to its dna source
1303 List<AlignedCodonFrame> newMappings = cdsal.getCodonFrames();
1306 * 6 mappings involve dna1 (to pep1/2/3, cds1/2/3)
1308 List<AlignedCodonFrame> dnaMappings = MappingUtils
1309 .findMappingsForSequence(dna1, newMappings);
1310 assertEquals(6, dnaMappings.size());
1315 List<AlignedCodonFrame> mappings = MappingUtils
1316 .findMappingsForSequence(pep1, dnaMappings);
1317 assertEquals(1, mappings.size());
1318 assertEquals(1, mappings.get(0).getMappings().size());
1319 assertSame(pep1.getDatasetSequence(), mappings.get(0).getMappings()
1320 .get(0).getMapping().getTo());
1325 List<AlignedCodonFrame> dnaToCds1Mappings = MappingUtils
1326 .findMappingsForSequence(cds.get(0), dnaMappings);
1327 Mapping mapping = dnaToCds1Mappings.get(0).getMappings().get(0)
1329 assertSame(cds.get(0).getDatasetSequence(), mapping.getTo());
1330 assertEquals("G(1) in CDS should map to G(4) in DNA", 4, mapping
1331 .getMap().getToPosition(1));
1336 mappings = MappingUtils.findMappingsForSequence(pep2, dnaMappings);
1337 assertEquals(1, mappings.size());
1338 assertEquals(1, mappings.get(0).getMappings().size());
1339 assertSame(pep2.getDatasetSequence(), mappings.get(0).getMappings()
1340 .get(0).getMapping().getTo());
1345 List<AlignedCodonFrame> dnaToCds2Mappings = MappingUtils
1346 .findMappingsForSequence(cds.get(1), dnaMappings);
1347 mapping = dnaToCds2Mappings.get(0).getMappings().get(0).getMapping();
1348 assertSame(cds.get(1).getDatasetSequence(), mapping.getTo());
1349 assertEquals("c(4) in CDS should map to c(7) in DNA", 7, mapping
1350 .getMap().getToPosition(4));
1355 mappings = MappingUtils.findMappingsForSequence(pep3, dnaMappings);
1356 assertEquals(1, mappings.size());
1357 assertEquals(1, mappings.get(0).getMappings().size());
1358 assertSame(pep3.getDatasetSequence(), mappings.get(0).getMappings()
1359 .get(0).getMapping().getTo());
1364 List<AlignedCodonFrame> dnaToCds3Mappings = MappingUtils
1365 .findMappingsForSequence(cds.get(2), dnaMappings);
1366 mapping = dnaToCds3Mappings.get(0).getMappings().get(0).getMapping();
1367 assertSame(cds.get(2).getDatasetSequence(), mapping.getTo());
1368 assertEquals("T(4) in CDS should map to T(10) in DNA", 10, mapping
1369 .getMap().getToPosition(4));
1372 @Test(groups = { "Functional" })
1373 public void testIsMappable()
1375 SequenceI dna1 = new Sequence("dna1", "cgCAGtgGT");
1376 SequenceI aa1 = new Sequence("aa1", "RSG");
1377 AlignmentI al1 = new Alignment(new SequenceI[] { dna1 });
1378 AlignmentI al2 = new Alignment(new SequenceI[] { aa1 });
1380 assertFalse(AlignmentUtils.isMappable(null, null));
1381 assertFalse(AlignmentUtils.isMappable(al1, null));
1382 assertFalse(AlignmentUtils.isMappable(null, al1));
1383 assertFalse(AlignmentUtils.isMappable(al1, al1));
1384 assertFalse(AlignmentUtils.isMappable(al2, al2));
1386 assertTrue(AlignmentUtils.isMappable(al1, al2));
1387 assertTrue(AlignmentUtils.isMappable(al2, al1));
1391 * Test creating a mapping when the sequences involved do not start at residue
1394 * @throws IOException
1396 @Test(groups = { "Functional" })
1397 public void testMapCdnaToProtein_forSubsequence() throws IOException
1399 SequenceI prot = new Sequence("UNIPROT|V12345", "E-I--Q", 10, 12);
1400 prot.createDatasetSequence();
1402 SequenceI dna = new Sequence("EMBL|A33333", "GAA--AT-C-CAG", 40, 48);
1403 dna.createDatasetSequence();
1405 MapList map = AlignmentUtils.mapCdnaToProtein(prot, dna);
1406 assertEquals(10, map.getToLowest());
1407 assertEquals(12, map.getToHighest());
1408 assertEquals(40, map.getFromLowest());
1409 assertEquals(48, map.getFromHighest());
1413 * Test for the alignSequenceAs method where we have protein mapped to protein
1415 @Test(groups = { "Functional" })
1416 public void testAlignSequenceAs_mappedProteinProtein()
1419 SequenceI alignMe = new Sequence("Match", "MGAASEV");
1420 alignMe.createDatasetSequence();
1421 SequenceI alignFrom = new Sequence("Query", "LQTGYMGAASEVMFSPTRR");
1422 alignFrom.createDatasetSequence();
1424 AlignedCodonFrame acf = new AlignedCodonFrame();
1425 // this is like a domain or motif match of part of a peptide sequence
1426 MapList map = new MapList(new int[] { 6, 12 }, new int[] { 1, 7 }, 1, 1);
1427 acf.addMap(alignFrom.getDatasetSequence(),
1428 alignMe.getDatasetSequence(), map);
1430 AlignmentUtils.alignSequenceAs(alignMe, alignFrom, acf, "-", '-', true,
1432 assertEquals("-----MGAASEV-------", alignMe.getSequenceAsString());
1436 * Test for the alignSequenceAs method where there are trailing unmapped
1437 * residues in the model sequence
1439 @Test(groups = { "Functional" })
1440 public void testAlignSequenceAs_withTrailingPeptide()
1442 // map first 3 codons to KPF; G is a trailing unmapped residue
1443 MapList map = new MapList(new int[] { 1, 9 }, new int[] { 1, 3 }, 3, 1);
1445 checkAlignSequenceAs("AAACCCTTT", "K-PFG", true, true, map,
1450 * Tests for transferring features between mapped sequences
1452 @Test(groups = { "Functional" })
1453 public void testTransferFeatures()
1455 SequenceI dna = new Sequence("dna/20-34", "acgTAGcaaGCCcgt");
1456 SequenceI cds = new Sequence("cds/10-15", "TAGGCC");
1459 dna.addSequenceFeature(new SequenceFeature("type1", "desc1", 1, 2, 1f,
1461 // partial overlap - to [1, 1]
1462 dna.addSequenceFeature(new SequenceFeature("type2", "desc2", 3, 4, 2f,
1464 // exact overlap - to [1, 3]
1465 dna.addSequenceFeature(new SequenceFeature("type3", "desc3", 4, 6, 3f,
1467 // spanning overlap - to [2, 5]
1468 dna.addSequenceFeature(new SequenceFeature("type4", "desc4", 5, 11, 4f,
1470 // exactly overlaps whole mapped range [1, 6]
1471 dna.addSequenceFeature(new SequenceFeature("type5", "desc5", 4, 12, 5f,
1473 // no overlap (internal)
1474 dna.addSequenceFeature(new SequenceFeature("type6", "desc6", 7, 9, 6f,
1476 // no overlap (3' end)
1477 dna.addSequenceFeature(new SequenceFeature("type7", "desc7", 13, 15,
1479 // overlap (3' end) - to [6, 6]
1480 dna.addSequenceFeature(new SequenceFeature("type8", "desc8", 12, 12,
1482 // extended overlap - to [6, +]
1483 dna.addSequenceFeature(new SequenceFeature("type9", "desc9", 12, 13,
1486 MapList map = new MapList(new int[] { 4, 6, 10, 12 },
1487 new int[] { 1, 6 }, 1, 1);
1490 * transferFeatures() will build 'partial overlap' for regions
1491 * that partially overlap 5' or 3' (start or end) of target sequence
1493 AlignmentUtils.transferFeatures(dna, cds, map, null);
1494 List<SequenceFeature> sfs = cds.getSequenceFeatures();
1495 assertEquals(6, sfs.size());
1497 SequenceFeature sf = sfs.get(0);
1498 assertEquals("type2", sf.getType());
1499 assertEquals("desc2", sf.getDescription());
1500 assertEquals(2f, sf.getScore());
1501 assertEquals(1, sf.getBegin());
1502 assertEquals(1, sf.getEnd());
1505 assertEquals("type3", sf.getType());
1506 assertEquals("desc3", sf.getDescription());
1507 assertEquals(3f, sf.getScore());
1508 assertEquals(1, sf.getBegin());
1509 assertEquals(3, sf.getEnd());
1512 assertEquals("type4", sf.getType());
1513 assertEquals(2, sf.getBegin());
1514 assertEquals(5, sf.getEnd());
1517 assertEquals("type5", sf.getType());
1518 assertEquals(1, sf.getBegin());
1519 assertEquals(6, sf.getEnd());
1522 assertEquals("type8", sf.getType());
1523 assertEquals(6, sf.getBegin());
1524 assertEquals(6, sf.getEnd());
1527 assertEquals("type9", sf.getType());
1528 assertEquals(6, sf.getBegin());
1529 assertEquals(6, sf.getEnd());
1533 * Tests for transferring features between mapped sequences
1535 @Test(groups = { "Functional" })
1536 public void testTransferFeatures_withOmit()
1538 SequenceI dna = new Sequence("dna/20-34", "acgTAGcaaGCCcgt");
1539 SequenceI cds = new Sequence("cds/10-15", "TAGGCC");
1541 MapList map = new MapList(new int[] { 4, 6, 10, 12 },
1542 new int[] { 1, 6 }, 1, 1);
1544 // [5, 11] maps to [2, 5]
1545 dna.addSequenceFeature(new SequenceFeature("type4", "desc4", 5, 11, 4f,
1547 // [4, 12] maps to [1, 6]
1548 dna.addSequenceFeature(new SequenceFeature("type5", "desc5", 4, 12, 5f,
1550 // [12, 12] maps to [6, 6]
1551 dna.addSequenceFeature(new SequenceFeature("type8", "desc8", 12, 12,
1554 // desc4 and desc8 are the 'omit these' varargs
1555 AlignmentUtils.transferFeatures(dna, cds, map, null, "type4", "type8");
1556 List<SequenceFeature> sfs = cds.getSequenceFeatures();
1557 assertEquals(1, sfs.size());
1559 SequenceFeature sf = sfs.get(0);
1560 assertEquals("type5", sf.getType());
1561 assertEquals(1, sf.getBegin());
1562 assertEquals(6, sf.getEnd());
1566 * Tests for transferring features between mapped sequences
1568 @Test(groups = { "Functional" })
1569 public void testTransferFeatures_withSelect()
1571 SequenceI dna = new Sequence("dna/20-34", "acgTAGcaaGCCcgt");
1572 SequenceI cds = new Sequence("cds/10-15", "TAGGCC");
1574 MapList map = new MapList(new int[] { 4, 6, 10, 12 },
1575 new int[] { 1, 6 }, 1, 1);
1577 // [5, 11] maps to [2, 5]
1578 dna.addSequenceFeature(new SequenceFeature("type4", "desc4", 5, 11, 4f,
1580 // [4, 12] maps to [1, 6]
1581 dna.addSequenceFeature(new SequenceFeature("type5", "desc5", 4, 12, 5f,
1583 // [12, 12] maps to [6, 6]
1584 dna.addSequenceFeature(new SequenceFeature("type8", "desc8", 12, 12,
1587 // "type5" is the 'select this type' argument
1588 AlignmentUtils.transferFeatures(dna, cds, map, "type5");
1589 List<SequenceFeature> sfs = cds.getSequenceFeatures();
1590 assertEquals(1, sfs.size());
1592 SequenceFeature sf = sfs.get(0);
1593 assertEquals("type5", sf.getType());
1594 assertEquals(1, sf.getBegin());
1595 assertEquals(6, sf.getEnd());
1599 * Test the method that extracts the cds-only part of a dna alignment, for the
1600 * case where the cds should be aligned to match its nucleotide sequence.
1602 @Test(groups = { "Functional" })
1603 public void testMakeCdsAlignment_alternativeTranscripts()
1605 SequenceI dna1 = new Sequence("dna1", "aaaGGGCC-----CTTTaaaGGG");
1606 // alternative transcript of same dna skips CCC codon
1607 SequenceI dna2 = new Sequence("dna2", "aaaGGGCC-----cttTaaaGGG");
1608 // dna3 has no mapping (protein product) so should be ignored here
1609 SequenceI dna3 = new Sequence("dna3", "aaaGGGCCCCCGGGcttTaaaGGG");
1610 SequenceI pep1 = new Sequence("pep1", "GPFG");
1611 SequenceI pep2 = new Sequence("pep2", "GPG");
1612 dna1.createDatasetSequence();
1613 dna2.createDatasetSequence();
1614 dna3.createDatasetSequence();
1615 pep1.createDatasetSequence();
1616 pep2.createDatasetSequence();
1618 AlignmentI dna = new Alignment(new SequenceI[] { dna1, dna2, dna3 });
1619 dna.setDataset(null);
1621 MapList map = new MapList(new int[] { 4, 12, 16, 18 },
1622 new int[] { 1, 4 }, 3, 1);
1623 AlignedCodonFrame acf = new AlignedCodonFrame();
1624 acf.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(), map);
1625 dna.addCodonFrame(acf);
1626 map = new MapList(new int[] { 4, 8, 12, 12, 16, 18 },
1627 new int[] { 1, 3 }, 3, 1);
1628 acf = new AlignedCodonFrame();
1629 acf.addMap(dna2.getDatasetSequence(), pep2.getDatasetSequence(), map);
1630 dna.addCodonFrame(acf);
1632 AlignmentI cds = AlignmentUtils.makeCdsAlignment(new SequenceI[] {
1633 dna1, dna2, dna3 }, dna.getDataset(), null);
1634 List<SequenceI> cdsSeqs = cds.getSequences();
1635 assertEquals(2, cdsSeqs.size());
1636 assertEquals("GGGCCCTTTGGG", cdsSeqs.get(0).getSequenceAsString());
1637 assertEquals("GGGCCTGGG", cdsSeqs.get(1).getSequenceAsString());
1640 * verify shared, extended alignment dataset
1642 assertSame(dna.getDataset(), cds.getDataset());
1643 assertTrue(dna.getDataset().getSequences()
1644 .contains(cdsSeqs.get(0).getDatasetSequence()));
1645 assertTrue(dna.getDataset().getSequences()
1646 .contains(cdsSeqs.get(1).getDatasetSequence()));
1649 * Verify 6 mappings: dna1 to cds1, cds1 to pep1, dna1 to pep1
1650 * and the same for dna2/cds2/pep2
1652 List<AlignedCodonFrame> mappings = cds.getCodonFrames();
1653 assertEquals(6, mappings.size());
1656 * 2 mappings involve pep1
1658 List<AlignedCodonFrame> pep1Mappings = MappingUtils
1659 .findMappingsForSequence(pep1, mappings);
1660 assertEquals(2, pep1Mappings.size());
1663 * Get mapping of pep1 to cds1 and verify it
1664 * maps GPFG to 1-3,4-6,7-9,10-12
1666 List<AlignedCodonFrame> pep1CdsMappings = MappingUtils
1667 .findMappingsForSequence(cds.getSequenceAt(0), pep1Mappings);
1668 assertEquals(1, pep1CdsMappings.size());
1669 SearchResultsI sr = MappingUtils.buildSearchResults(pep1, 1,
1671 assertEquals(1, sr.getResults().size());
1672 SearchResultMatchI m = sr.getResults().get(0);
1673 assertEquals(cds.getSequenceAt(0).getDatasetSequence(), m.getSequence());
1674 assertEquals(1, m.getStart());
1675 assertEquals(3, m.getEnd());
1676 sr = MappingUtils.buildSearchResults(pep1, 2, pep1CdsMappings);
1677 m = sr.getResults().get(0);
1678 assertEquals(4, m.getStart());
1679 assertEquals(6, m.getEnd());
1680 sr = MappingUtils.buildSearchResults(pep1, 3, pep1CdsMappings);
1681 m = sr.getResults().get(0);
1682 assertEquals(7, m.getStart());
1683 assertEquals(9, m.getEnd());
1684 sr = MappingUtils.buildSearchResults(pep1, 4, pep1CdsMappings);
1685 m = sr.getResults().get(0);
1686 assertEquals(10, m.getStart());
1687 assertEquals(12, m.getEnd());
1690 * Get mapping of pep2 to cds2 and verify it
1691 * maps GPG in pep2 to 1-3,4-6,7-9 in second CDS sequence
1693 List<AlignedCodonFrame> pep2Mappings = MappingUtils
1694 .findMappingsForSequence(pep2, mappings);
1695 assertEquals(2, pep2Mappings.size());
1696 List<AlignedCodonFrame> pep2CdsMappings = MappingUtils
1697 .findMappingsForSequence(cds.getSequenceAt(1), pep2Mappings);
1698 assertEquals(1, pep2CdsMappings.size());
1699 sr = MappingUtils.buildSearchResults(pep2, 1, pep2CdsMappings);
1700 assertEquals(1, sr.getResults().size());
1701 m = sr.getResults().get(0);
1702 assertEquals(cds.getSequenceAt(1).getDatasetSequence(), m.getSequence());
1703 assertEquals(1, m.getStart());
1704 assertEquals(3, m.getEnd());
1705 sr = MappingUtils.buildSearchResults(pep2, 2, pep2CdsMappings);
1706 m = sr.getResults().get(0);
1707 assertEquals(4, m.getStart());
1708 assertEquals(6, m.getEnd());
1709 sr = MappingUtils.buildSearchResults(pep2, 3, pep2CdsMappings);
1710 m = sr.getResults().get(0);
1711 assertEquals(7, m.getStart());
1712 assertEquals(9, m.getEnd());
1716 * Test the method that realigns protein to match mapped codon alignment.
1718 @Test(groups = { "Functional" })
1719 public void testAlignProteinAsDna_incompleteStartCodon()
1721 // seq1: incomplete start codon (not mapped), then [3, 11]
1722 SequenceI dna1 = new Sequence("Seq1", "ccAAA-TTT-GGG-");
1723 // seq2 codons are [4, 5], [8, 11]
1724 SequenceI dna2 = new Sequence("Seq2", "ccaAA-ttT-GGG-");
1725 // seq3 incomplete start codon at 'tt'
1726 SequenceI dna3 = new Sequence("Seq3", "ccaaa-ttt-GGG-");
1727 AlignmentI dna = new Alignment(new SequenceI[] { dna1, dna2, dna3 });
1728 dna.setDataset(null);
1730 // prot1 has 'X' for incomplete start codon (not mapped)
1731 SequenceI prot1 = new Sequence("Seq1", "XKFG"); // X for incomplete start
1732 SequenceI prot2 = new Sequence("Seq2", "NG");
1733 SequenceI prot3 = new Sequence("Seq3", "XG"); // X for incomplete start
1734 AlignmentI protein = new Alignment(new SequenceI[] { prot1, prot2,
1736 protein.setDataset(null);
1738 // map dna1 [3, 11] to prot1 [2, 4] KFG
1739 MapList map = new MapList(new int[] { 3, 11 }, new int[] { 2, 4 }, 3, 1);
1740 AlignedCodonFrame acf = new AlignedCodonFrame();
1741 acf.addMap(dna1.getDatasetSequence(), prot1.getDatasetSequence(), map);
1743 // map dna2 [4, 5] [8, 11] to prot2 [1, 2] NG
1744 map = new MapList(new int[] { 4, 5, 8, 11 }, new int[] { 1, 2 }, 3, 1);
1745 acf.addMap(dna2.getDatasetSequence(), prot2.getDatasetSequence(), map);
1747 // map dna3 [9, 11] to prot3 [2, 2] G
1748 map = new MapList(new int[] { 9, 11 }, new int[] { 2, 2 }, 3, 1);
1749 acf.addMap(dna3.getDatasetSequence(), prot3.getDatasetSequence(), map);
1751 ArrayList<AlignedCodonFrame> acfs = new ArrayList<>();
1753 protein.setCodonFrames(acfs);
1756 * verify X is included in the aligned proteins, and placed just
1757 * before the first mapped residue
1758 * CCT is between CCC and TTT
1760 AlignmentUtils.alignProteinAsDna(protein, dna);
1761 assertEquals("XK-FG", prot1.getSequenceAsString());
1762 assertEquals("--N-G", prot2.getSequenceAsString());
1763 assertEquals("---XG", prot3.getSequenceAsString());
1767 * Tests for the method that maps the subset of a dna sequence that has CDS
1768 * (or subtype) feature - case where the start codon is incomplete.
1770 @Test(groups = "Functional")
1771 public void testFindCdsPositions_fivePrimeIncomplete()
1773 SequenceI dnaSeq = new Sequence("dna", "aaagGGCCCaaaTTTttt");
1774 dnaSeq.createDatasetSequence();
1775 SequenceI ds = dnaSeq.getDatasetSequence();
1777 // CDS for dna 5-6 (incomplete codon), 7-9
1778 SequenceFeature sf = new SequenceFeature("CDS", "", 5, 9, 0f, null);
1779 sf.setPhase("2"); // skip 2 bases to start of next codon
1780 ds.addSequenceFeature(sf);
1781 // CDS for dna 13-15
1782 sf = new SequenceFeature("CDS_predicted", "", 13, 15, 0f, null);
1783 ds.addSequenceFeature(sf);
1785 List<int[]> ranges = AlignmentUtils.findCdsPositions(dnaSeq);
1788 * check the mapping starts with the first complete codon
1790 assertEquals(6, MappingUtils.getLength(ranges));
1791 assertEquals(2, ranges.size());
1792 assertEquals(7, ranges.get(0)[0]);
1793 assertEquals(9, ranges.get(0)[1]);
1794 assertEquals(13, ranges.get(1)[0]);
1795 assertEquals(15, ranges.get(1)[1]);
1799 * Tests for the method that maps the subset of a dna sequence that has CDS
1800 * (or subtype) feature.
1802 @Test(groups = "Functional")
1803 public void testFindCdsPositions()
1805 SequenceI dnaSeq = new Sequence("dna", "aaaGGGcccAAATTTttt");
1806 dnaSeq.createDatasetSequence();
1807 SequenceI ds = dnaSeq.getDatasetSequence();
1809 // CDS for dna 10-12
1810 SequenceFeature sf = new SequenceFeature("CDS_predicted", "", 10, 12,
1813 ds.addSequenceFeature(sf);
1815 sf = new SequenceFeature("CDS", "", 4, 6, 0f, null);
1817 ds.addSequenceFeature(sf);
1818 // exon feature should be ignored here
1819 sf = new SequenceFeature("exon", "", 7, 9, 0f, null);
1820 ds.addSequenceFeature(sf);
1822 List<int[]> ranges = AlignmentUtils.findCdsPositions(dnaSeq);
1824 * verify ranges { [4-6], [12-10] }
1825 * note CDS ranges are ordered ascending even if the CDS
1828 assertEquals(6, MappingUtils.getLength(ranges));
1829 assertEquals(2, ranges.size());
1830 assertEquals(4, ranges.get(0)[0]);
1831 assertEquals(6, ranges.get(0)[1]);
1832 assertEquals(10, ranges.get(1)[0]);
1833 assertEquals(12, ranges.get(1)[1]);
1837 * Test the method that computes a map of codon variants for each protein
1838 * position from "sequence_variant" features on dna
1840 @Test(groups = "Functional")
1841 public void testBuildDnaVariantsMap()
1843 SequenceI dna = new Sequence("dna", "atgAAATTTGGGCCCtag");
1844 MapList map = new MapList(new int[] { 1, 18 }, new int[] { 1, 5 }, 3, 1);
1847 * first with no variants on dna
1849 LinkedHashMap<Integer, List<DnaVariant>[]> variantsMap = AlignmentUtils
1850 .buildDnaVariantsMap(dna, map);
1851 assertTrue(variantsMap.isEmpty());
1854 * single allele codon 1, on base 1
1856 SequenceFeature sf1 = new SequenceFeature("sequence_variant", "", 1, 1,
1858 sf1.setValue("alleles", "T");
1859 sf1.setValue("ID", "sequence_variant:rs758803211");
1860 dna.addSequenceFeature(sf1);
1863 * two alleles codon 2, on bases 2 and 3 (distinct variants)
1865 SequenceFeature sf2 = new SequenceFeature("sequence_variant", "", 5, 5,
1867 sf2.setValue("alleles", "T");
1868 sf2.setValue("ID", "sequence_variant:rs758803212");
1869 dna.addSequenceFeature(sf2);
1870 SequenceFeature sf3 = new SequenceFeature("sequence_variant", "", 6, 6,
1872 sf3.setValue("alleles", "G");
1873 sf3.setValue("ID", "sequence_variant:rs758803213");
1874 dna.addSequenceFeature(sf3);
1877 * two alleles codon 3, both on base 2 (one variant)
1879 SequenceFeature sf4 = new SequenceFeature("sequence_variant", "", 8, 8,
1881 sf4.setValue("alleles", "C, G");
1882 sf4.setValue("ID", "sequence_variant:rs758803214");
1883 dna.addSequenceFeature(sf4);
1885 // no alleles on codon 4
1888 * alleles on codon 5 on all 3 bases (distinct variants)
1890 SequenceFeature sf5 = new SequenceFeature("sequence_variant", "", 13,
1892 sf5.setValue("alleles", "C, G"); // (C duplicates given base value)
1893 sf5.setValue("ID", "sequence_variant:rs758803215");
1894 dna.addSequenceFeature(sf5);
1895 SequenceFeature sf6 = new SequenceFeature("sequence_variant", "", 14,
1897 sf6.setValue("alleles", "g, a"); // should force to upper-case
1898 sf6.setValue("ID", "sequence_variant:rs758803216");
1899 dna.addSequenceFeature(sf6);
1900 SequenceFeature sf7 = new SequenceFeature("sequence_variant", "", 15,
1902 sf7.setValue("alleles", "A, T");
1903 sf7.setValue("ID", "sequence_variant:rs758803217");
1904 dna.addSequenceFeature(sf7);
1907 * build map - expect variants on positions 1, 2, 3, 5
1909 variantsMap = AlignmentUtils.buildDnaVariantsMap(dna, map);
1910 assertEquals(4, variantsMap.size());
1913 * protein residue 1: variant on codon (ATG) base 1, not on 2 or 3
1915 List<DnaVariant>[] pep1Variants = variantsMap.get(1);
1916 assertEquals(3, pep1Variants.length);
1917 assertEquals(1, pep1Variants[0].size());
1918 assertEquals("A", pep1Variants[0].get(0).base); // codon[1] base
1919 assertSame(sf1, pep1Variants[0].get(0).variant); // codon[1] variant
1920 assertEquals(1, pep1Variants[1].size());
1921 assertEquals("T", pep1Variants[1].get(0).base); // codon[2] base
1922 assertNull(pep1Variants[1].get(0).variant); // no variant here
1923 assertEquals(1, pep1Variants[2].size());
1924 assertEquals("G", pep1Variants[2].get(0).base); // codon[3] base
1925 assertNull(pep1Variants[2].get(0).variant); // no variant here
1928 * protein residue 2: variants on codon (AAA) bases 2 and 3
1930 List<DnaVariant>[] pep2Variants = variantsMap.get(2);
1931 assertEquals(3, pep2Variants.length);
1932 assertEquals(1, pep2Variants[0].size());
1933 // codon[1] base recorded while processing variant on codon[2]
1934 assertEquals("A", pep2Variants[0].get(0).base);
1935 assertNull(pep2Variants[0].get(0).variant); // no variant here
1936 // codon[2] base and variant:
1937 assertEquals(1, pep2Variants[1].size());
1938 assertEquals("A", pep2Variants[1].get(0).base);
1939 assertSame(sf2, pep2Variants[1].get(0).variant);
1940 // codon[3] base was recorded when processing codon[2] variant
1941 // and then the variant for codon[3] added to it
1942 assertEquals(1, pep2Variants[2].size());
1943 assertEquals("A", pep2Variants[2].get(0).base);
1944 assertSame(sf3, pep2Variants[2].get(0).variant);
1947 * protein residue 3: variants on codon (TTT) base 2 only
1949 List<DnaVariant>[] pep3Variants = variantsMap.get(3);
1950 assertEquals(3, pep3Variants.length);
1951 assertEquals(1, pep3Variants[0].size());
1952 assertEquals("T", pep3Variants[0].get(0).base); // codon[1] base
1953 assertNull(pep3Variants[0].get(0).variant); // no variant here
1954 assertEquals(1, pep3Variants[1].size());
1955 assertEquals("T", pep3Variants[1].get(0).base); // codon[2] base
1956 assertSame(sf4, pep3Variants[1].get(0).variant); // codon[2] variant
1957 assertEquals(1, pep3Variants[2].size());
1958 assertEquals("T", pep3Variants[2].get(0).base); // codon[3] base
1959 assertNull(pep3Variants[2].get(0).variant); // no variant here
1962 * three variants on protein position 5
1964 List<DnaVariant>[] pep5Variants = variantsMap.get(5);
1965 assertEquals(3, pep5Variants.length);
1966 assertEquals(1, pep5Variants[0].size());
1967 assertEquals("C", pep5Variants[0].get(0).base); // codon[1] base
1968 assertSame(sf5, pep5Variants[0].get(0).variant); // codon[1] variant
1969 assertEquals(1, pep5Variants[1].size());
1970 assertEquals("C", pep5Variants[1].get(0).base); // codon[2] base
1971 assertSame(sf6, pep5Variants[1].get(0).variant); // codon[2] variant
1972 assertEquals(1, pep5Variants[2].size());
1973 assertEquals("C", pep5Variants[2].get(0).base); // codon[3] base
1974 assertSame(sf7, pep5Variants[2].get(0).variant); // codon[3] variant
1978 * Tests for the method that computes all peptide variants given codon
1981 @Test(groups = "Functional")
1982 public void testComputePeptideVariants()
1985 * scenario: AAATTTCCC codes for KFP
1987 * GAA -> E source: Ensembl
1988 * CAA -> Q source: dbSNP
1989 * AAG synonymous source: COSMIC
1990 * AAT -> N source: Ensembl
1991 * ...TTC synonymous source: dbSNP
1992 * ......CAC,CGC -> H,R source: COSMIC
1993 * (one variant with two alleles)
1995 SequenceI peptide = new Sequence("pep/10-12", "KFP");
1998 * two distinct variants for codon 1 position 1
1999 * second one has clinical significance
2001 String ensembl = "Ensembl";
2002 String dbSnp = "dbSNP";
2003 String cosmic = "COSMIC";
2004 SequenceFeature sf1 = new SequenceFeature("sequence_variant", "", 1, 1,
2006 sf1.setValue("alleles", "A,G"); // GAA -> E
2007 sf1.setValue("ID", "var1.125A>G");
2008 SequenceFeature sf2 = new SequenceFeature("sequence_variant", "", 1, 1,
2010 sf2.setValue("alleles", "A,C"); // CAA -> Q
2011 sf2.setValue("ID", "var2");
2012 sf2.setValue("clinical_significance", "Dodgy");
2013 SequenceFeature sf3 = new SequenceFeature("sequence_variant", "", 3, 3,
2015 sf3.setValue("alleles", "A,G"); // synonymous
2016 sf3.setValue("ID", "var3");
2017 sf3.setValue("clinical_significance", "None");
2018 SequenceFeature sf4 = new SequenceFeature("sequence_variant", "", 3, 3,
2020 sf4.setValue("alleles", "A,T"); // AAT -> N
2021 sf4.setValue("ID", "sequence_variant:var4"); // prefix gets stripped off
2022 sf4.setValue("clinical_significance", "Benign");
2023 SequenceFeature sf5 = new SequenceFeature("sequence_variant", "", 6, 6,
2025 sf5.setValue("alleles", "T,C"); // synonymous
2026 sf5.setValue("ID", "var5");
2027 sf5.setValue("clinical_significance", "Bad");
2028 SequenceFeature sf6 = new SequenceFeature("sequence_variant", "", 8, 8,
2030 sf6.setValue("alleles", "C,A,G"); // CAC,CGC -> H,R
2031 sf6.setValue("ID", "var6");
2032 sf6.setValue("clinical_significance", "Good");
2034 List<DnaVariant> codon1Variants = new ArrayList<>();
2035 List<DnaVariant> codon2Variants = new ArrayList<>();
2036 List<DnaVariant> codon3Variants = new ArrayList<>();
2037 List<DnaVariant> codonVariants[] = new ArrayList[3];
2038 codonVariants[0] = codon1Variants;
2039 codonVariants[1] = codon2Variants;
2040 codonVariants[2] = codon3Variants;
2043 * compute variants for protein position 1
2045 codon1Variants.add(new DnaVariant("A", sf1));
2046 codon1Variants.add(new DnaVariant("A", sf2));
2047 codon2Variants.add(new DnaVariant("A"));
2048 codon2Variants.add(new DnaVariant("A"));
2049 codon3Variants.add(new DnaVariant("A", sf3));
2050 codon3Variants.add(new DnaVariant("A", sf4));
2051 AlignmentUtils.computePeptideVariants(peptide, 1, codonVariants);
2054 * compute variants for protein position 2
2056 codon1Variants.clear();
2057 codon2Variants.clear();
2058 codon3Variants.clear();
2059 codon1Variants.add(new DnaVariant("T"));
2060 codon2Variants.add(new DnaVariant("T"));
2061 codon3Variants.add(new DnaVariant("T", sf5));
2062 AlignmentUtils.computePeptideVariants(peptide, 2, codonVariants);
2065 * compute variants for protein position 3
2067 codon1Variants.clear();
2068 codon2Variants.clear();
2069 codon3Variants.clear();
2070 codon1Variants.add(new DnaVariant("C"));
2071 codon2Variants.add(new DnaVariant("C", sf6));
2072 codon3Variants.add(new DnaVariant("C"));
2073 AlignmentUtils.computePeptideVariants(peptide, 3, codonVariants);
2076 * verify added sequence features for
2077 * var1 K -> E Ensembl
2079 * var4 K -> N Ensembl
2080 * var6 P -> H COSMIC
2081 * var6 P -> R COSMIC
2083 List<SequenceFeature> sfs = peptide.getSequenceFeatures();
2084 SequenceFeatures.sortFeatures(sfs, true);
2085 assertEquals(5, sfs.size());
2088 * features are sorted by start position ascending, but in no
2089 * particular order where start positions match; asserts here
2090 * simply match the data returned (the order is not important)
2092 SequenceFeature sf = sfs.get(0);
2093 assertEquals(1, sf.getBegin());
2094 assertEquals(1, sf.getEnd());
2095 assertEquals("p.Lys1Asn", sf.getDescription());
2096 assertEquals("var4", sf.getValue("ID"));
2097 assertEquals("Benign", sf.getValue("clinical_significance"));
2098 assertEquals("ID=var4;clinical_significance=Benign", sf.getAttributes());
2099 assertEquals(1, sf.links.size());
2101 "p.Lys1Asn var4|http://www.ensembl.org/Homo_sapiens/Variation/Summary?v=var4",
2103 assertEquals(ensembl, sf.getFeatureGroup());
2106 assertEquals(1, sf.getBegin());
2107 assertEquals(1, sf.getEnd());
2108 assertEquals("p.Lys1Gln", sf.getDescription());
2109 assertEquals("var2", sf.getValue("ID"));
2110 assertEquals("Dodgy", sf.getValue("clinical_significance"));
2111 assertEquals("ID=var2;clinical_significance=Dodgy", sf.getAttributes());
2112 assertEquals(1, sf.links.size());
2114 "p.Lys1Gln var2|http://www.ensembl.org/Homo_sapiens/Variation/Summary?v=var2",
2116 assertEquals(dbSnp, sf.getFeatureGroup());
2119 assertEquals(1, sf.getBegin());
2120 assertEquals(1, sf.getEnd());
2121 assertEquals("p.Lys1Glu", sf.getDescription());
2122 assertEquals("var1.125A>G", sf.getValue("ID"));
2123 assertNull(sf.getValue("clinical_significance"));
2124 assertEquals("ID=var1.125A>G", sf.getAttributes());
2125 assertEquals(1, sf.links.size());
2126 // link to variation is urlencoded
2128 "p.Lys1Glu var1.125A>G|http://www.ensembl.org/Homo_sapiens/Variation/Summary?v=var1.125A%3EG",
2130 assertEquals(ensembl, sf.getFeatureGroup());
2133 assertEquals(3, sf.getBegin());
2134 assertEquals(3, sf.getEnd());
2135 assertEquals("p.Pro3Arg", sf.getDescription());
2136 assertEquals("var6", sf.getValue("ID"));
2137 assertEquals("Good", sf.getValue("clinical_significance"));
2138 assertEquals("ID=var6;clinical_significance=Good", sf.getAttributes());
2139 assertEquals(1, sf.links.size());
2141 "p.Pro3Arg var6|http://www.ensembl.org/Homo_sapiens/Variation/Summary?v=var6",
2143 assertEquals(cosmic, sf.getFeatureGroup());
2145 // var5 generates two distinct protein variant features
2147 assertEquals(3, sf.getBegin());
2148 assertEquals(3, sf.getEnd());
2149 assertEquals("p.Pro3His", sf.getDescription());
2150 assertEquals("var6", sf.getValue("ID"));
2151 assertEquals("Good", sf.getValue("clinical_significance"));
2152 assertEquals("ID=var6;clinical_significance=Good", sf.getAttributes());
2153 assertEquals(1, sf.links.size());
2155 "p.Pro3His var6|http://www.ensembl.org/Homo_sapiens/Variation/Summary?v=var6",
2157 assertEquals(cosmic, sf.getFeatureGroup());
2161 * Tests for the method that maps the subset of a dna sequence that has CDS
2162 * (or subtype) feature, with CDS strand = '-' (reverse)
2164 // test turned off as currently findCdsPositions is not strand-dependent
2165 // left in case it comes around again...
2166 @Test(groups = "Functional", enabled = false)
2167 public void testFindCdsPositions_reverseStrand()
2169 SequenceI dnaSeq = new Sequence("dna", "aaaGGGcccAAATTTttt");
2170 dnaSeq.createDatasetSequence();
2171 SequenceI ds = dnaSeq.getDatasetSequence();
2174 SequenceFeature sf = new SequenceFeature("CDS", "", 4, 6, 0f, null);
2176 ds.addSequenceFeature(sf);
2177 // exon feature should be ignored here
2178 sf = new SequenceFeature("exon", "", 7, 9, 0f, null);
2179 ds.addSequenceFeature(sf);
2180 // CDS for dna 10-12
2181 sf = new SequenceFeature("CDS_predicted", "", 10, 12, 0f, null);
2183 ds.addSequenceFeature(sf);
2185 List<int[]> ranges = AlignmentUtils.findCdsPositions(dnaSeq);
2187 * verify ranges { [12-10], [6-4] }
2189 assertEquals(6, MappingUtils.getLength(ranges));
2190 assertEquals(2, ranges.size());
2191 assertEquals(12, ranges.get(0)[0]);
2192 assertEquals(10, ranges.get(0)[1]);
2193 assertEquals(6, ranges.get(1)[0]);
2194 assertEquals(4, ranges.get(1)[1]);
2198 * Tests for the method that maps the subset of a dna sequence that has CDS
2199 * (or subtype) feature - reverse strand case where the start codon is
2202 @Test(groups = "Functional", enabled = false)
2203 // test turned off as currently findCdsPositions is not strand-dependent
2204 // left in case it comes around again...
2205 public void testFindCdsPositions_reverseStrandThreePrimeIncomplete()
2207 SequenceI dnaSeq = new Sequence("dna", "aaagGGCCCaaaTTTttt");
2208 dnaSeq.createDatasetSequence();
2209 SequenceI ds = dnaSeq.getDatasetSequence();
2212 SequenceFeature sf = new SequenceFeature("CDS", "", 5, 9, 0f, null);
2214 ds.addSequenceFeature(sf);
2215 // CDS for dna 13-15
2216 sf = new SequenceFeature("CDS_predicted", "", 13, 15, 0f, null);
2218 sf.setPhase("2"); // skip 2 bases to start of next codon
2219 ds.addSequenceFeature(sf);
2221 List<int[]> ranges = AlignmentUtils.findCdsPositions(dnaSeq);
2224 * check the mapping starts with the first complete codon
2225 * expect ranges [13, 13], [9, 5]
2227 assertEquals(6, MappingUtils.getLength(ranges));
2228 assertEquals(2, ranges.size());
2229 assertEquals(13, ranges.get(0)[0]);
2230 assertEquals(13, ranges.get(0)[1]);
2231 assertEquals(9, ranges.get(1)[0]);
2232 assertEquals(5, ranges.get(1)[1]);
2235 @Test(groups = "Functional")
2236 public void testAlignAs_alternateTranscriptsUngapped()
2238 SequenceI dna1 = new Sequence("dna1", "cccGGGTTTaaa");
2239 SequenceI dna2 = new Sequence("dna2", "CCCgggtttAAA");
2240 AlignmentI dna = new Alignment(new SequenceI[] { dna1, dna2 });
2241 ((Alignment) dna).createDatasetAlignment();
2242 SequenceI cds1 = new Sequence("cds1", "GGGTTT");
2243 SequenceI cds2 = new Sequence("cds2", "CCCAAA");
2244 AlignmentI cds = new Alignment(new SequenceI[] { cds1, cds2 });
2245 ((Alignment) cds).createDatasetAlignment();
2247 AlignedCodonFrame acf = new AlignedCodonFrame();
2248 MapList map = new MapList(new int[] { 4, 9 }, new int[] { 1, 6 }, 1, 1);
2249 acf.addMap(dna1.getDatasetSequence(), cds1.getDatasetSequence(), map);
2250 map = new MapList(new int[] { 1, 3, 10, 12 }, new int[] { 1, 6 }, 1, 1);
2251 acf.addMap(dna2.getDatasetSequence(), cds2.getDatasetSequence(), map);
2254 * verify CDS alignment is as:
2255 * cccGGGTTTaaa (cdna)
2256 * CCCgggtttAAA (cdna)
2258 * ---GGGTTT--- (cds)
2259 * CCC------AAA (cds)
2261 dna.addCodonFrame(acf);
2262 AlignmentUtils.alignAs(cds, dna);
2263 assertEquals("---GGGTTT", cds.getSequenceAt(0).getSequenceAsString());
2264 assertEquals("CCC------AAA", cds.getSequenceAt(1).getSequenceAsString());
2267 @Test(groups = { "Functional" })
2268 public void testAddMappedPositions()
2270 SequenceI from = new Sequence("dna", "ggAA-ATcc-TT-g");
2271 SequenceI seq1 = new Sequence("cds", "AAATTT");
2272 from.createDatasetSequence();
2273 seq1.createDatasetSequence();
2274 Mapping mapping = new Mapping(seq1, new MapList(
2275 new int[] { 3, 6, 9, 10 }, new int[] { 1, 6 }, 1, 1));
2276 Map<Integer, Map<SequenceI, Character>> map = new TreeMap<>();
2277 AlignmentUtils.addMappedPositions(seq1, from, mapping, map);
2280 * verify map has seq1 residues in columns 3,4,6,7,11,12
2282 assertEquals(6, map.size());
2283 assertEquals('A', map.get(3).get(seq1).charValue());
2284 assertEquals('A', map.get(4).get(seq1).charValue());
2285 assertEquals('A', map.get(6).get(seq1).charValue());
2286 assertEquals('T', map.get(7).get(seq1).charValue());
2287 assertEquals('T', map.get(11).get(seq1).charValue());
2288 assertEquals('T', map.get(12).get(seq1).charValue());
2296 * Test case where the mapping 'from' range includes a stop codon which is
2297 * absent in the 'to' range
2299 @Test(groups = { "Functional" })
2300 public void testAddMappedPositions_withStopCodon()
2302 SequenceI from = new Sequence("dna", "ggAA-ATcc-TT-g");
2303 SequenceI seq1 = new Sequence("cds", "AAATTT");
2304 from.createDatasetSequence();
2305 seq1.createDatasetSequence();
2306 Mapping mapping = new Mapping(seq1, new MapList(
2307 new int[] { 3, 6, 9, 10 }, new int[] { 1, 6 }, 1, 1));
2308 Map<Integer, Map<SequenceI, Character>> map = new TreeMap<>();
2309 AlignmentUtils.addMappedPositions(seq1, from, mapping, map);
2312 * verify map has seq1 residues in columns 3,4,6,7,11,12
2314 assertEquals(6, map.size());
2315 assertEquals('A', map.get(3).get(seq1).charValue());
2316 assertEquals('A', map.get(4).get(seq1).charValue());
2317 assertEquals('A', map.get(6).get(seq1).charValue());
2318 assertEquals('T', map.get(7).get(seq1).charValue());
2319 assertEquals('T', map.get(11).get(seq1).charValue());
2320 assertEquals('T', map.get(12).get(seq1).charValue());
2324 * Test for the case where the products for which we want CDS are specified.
2325 * This is to represent the case where EMBL has CDS mappings to both Uniprot
2326 * and EMBLCDSPROTEIN. makeCdsAlignment() should only return the mappings for
2327 * the protein sequences specified.
2329 @Test(groups = { "Functional" })
2330 public void testMakeCdsAlignment_filterProducts()
2332 SequenceI dna1 = new Sequence("dna1", "aaaGGGcccTTTaaa");
2333 SequenceI dna2 = new Sequence("dna2", "GGGcccTTTaaaCCC");
2334 SequenceI pep1 = new Sequence("Uniprot|pep1", "GF");
2335 SequenceI pep2 = new Sequence("Uniprot|pep2", "GFP");
2336 SequenceI pep3 = new Sequence("EMBL|pep3", "GF");
2337 SequenceI pep4 = new Sequence("EMBL|pep4", "GFP");
2338 dna1.createDatasetSequence();
2339 dna2.createDatasetSequence();
2340 pep1.createDatasetSequence();
2341 pep2.createDatasetSequence();
2342 pep3.createDatasetSequence();
2343 pep4.createDatasetSequence();
2344 AlignmentI dna = new Alignment(new SequenceI[] { dna1, dna2 });
2345 dna.setDataset(null);
2346 AlignmentI emblPeptides = new Alignment(new SequenceI[] { pep3, pep4 });
2347 emblPeptides.setDataset(null);
2349 AlignedCodonFrame acf = new AlignedCodonFrame();
2350 MapList map = new MapList(new int[] { 4, 6, 10, 12 },
2351 new int[] { 1, 2 }, 3, 1);
2352 acf.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(), map);
2353 acf.addMap(dna1.getDatasetSequence(), pep3.getDatasetSequence(), map);
2354 dna.addCodonFrame(acf);
2356 acf = new AlignedCodonFrame();
2357 map = new MapList(new int[] { 1, 3, 7, 9, 13, 15 }, new int[] { 1, 3 },
2359 acf.addMap(dna2.getDatasetSequence(), pep2.getDatasetSequence(), map);
2360 acf.addMap(dna2.getDatasetSequence(), pep4.getDatasetSequence(), map);
2361 dna.addCodonFrame(acf);
2364 * execute method under test to find CDS for EMBL peptides only
2366 AlignmentI cds = AlignmentUtils.makeCdsAlignment(new SequenceI[] {
2367 dna1, dna2 }, dna.getDataset(), emblPeptides.getSequencesArray());
2369 assertEquals(2, cds.getSequences().size());
2370 assertEquals("GGGTTT", cds.getSequenceAt(0).getSequenceAsString());
2371 assertEquals("GGGTTTCCC", cds.getSequenceAt(1).getSequenceAsString());
2374 * verify shared, extended alignment dataset
2376 assertSame(dna.getDataset(), cds.getDataset());
2377 assertTrue(dna.getDataset().getSequences()
2378 .contains(cds.getSequenceAt(0).getDatasetSequence()));
2379 assertTrue(dna.getDataset().getSequences()
2380 .contains(cds.getSequenceAt(1).getDatasetSequence()));
2383 * Verify mappings from CDS to peptide, cDNA to CDS, and cDNA to peptide
2384 * the mappings are on the shared alignment dataset
2386 List<AlignedCodonFrame> cdsMappings = cds.getDataset().getCodonFrames();
2388 * 6 mappings, 2*(DNA->CDS), 2*(DNA->Pep), 2*(CDS->Pep)
2390 assertEquals(6, cdsMappings.size());
2393 * verify that mapping sets for dna and cds alignments are different
2394 * [not current behaviour - all mappings are on the alignment dataset]
2396 // select -> subselect type to test.
2397 // Assert.assertNotSame(dna.getCodonFrames(), cds.getCodonFrames());
2398 // assertEquals(4, dna.getCodonFrames().size());
2399 // assertEquals(4, cds.getCodonFrames().size());
2402 * Two mappings involve pep3 (dna to pep3, cds to pep3)
2403 * Mapping from pep3 to GGGTTT in first new exon sequence
2405 List<AlignedCodonFrame> pep3Mappings = MappingUtils
2406 .findMappingsForSequence(pep3, cdsMappings);
2407 assertEquals(2, pep3Mappings.size());
2408 List<AlignedCodonFrame> mappings = MappingUtils
2409 .findMappingsForSequence(cds.getSequenceAt(0), pep3Mappings);
2410 assertEquals(1, mappings.size());
2413 SearchResultsI sr = MappingUtils.buildSearchResults(pep3, 1, mappings);
2414 assertEquals(1, sr.getResults().size());
2415 SearchResultMatchI m = sr.getResults().get(0);
2416 assertSame(cds.getSequenceAt(0).getDatasetSequence(), m.getSequence());
2417 assertEquals(1, m.getStart());
2418 assertEquals(3, m.getEnd());
2420 sr = MappingUtils.buildSearchResults(pep3, 2, mappings);
2421 m = sr.getResults().get(0);
2422 assertSame(cds.getSequenceAt(0).getDatasetSequence(), m.getSequence());
2423 assertEquals(4, m.getStart());
2424 assertEquals(6, m.getEnd());
2427 * Two mappings involve pep4 (dna to pep4, cds to pep4)
2428 * Verify mapping from pep4 to GGGTTTCCC in second new exon sequence
2430 List<AlignedCodonFrame> pep4Mappings = MappingUtils
2431 .findMappingsForSequence(pep4, cdsMappings);
2432 assertEquals(2, pep4Mappings.size());
2433 mappings = MappingUtils.findMappingsForSequence(cds.getSequenceAt(1),
2435 assertEquals(1, mappings.size());
2437 sr = MappingUtils.buildSearchResults(pep4, 1, mappings);
2438 assertEquals(1, sr.getResults().size());
2439 m = sr.getResults().get(0);
2440 assertSame(cds.getSequenceAt(1).getDatasetSequence(), m.getSequence());
2441 assertEquals(1, m.getStart());
2442 assertEquals(3, m.getEnd());
2444 sr = MappingUtils.buildSearchResults(pep4, 2, mappings);
2445 m = sr.getResults().get(0);
2446 assertSame(cds.getSequenceAt(1).getDatasetSequence(), m.getSequence());
2447 assertEquals(4, m.getStart());
2448 assertEquals(6, m.getEnd());
2450 sr = MappingUtils.buildSearchResults(pep4, 3, mappings);
2451 m = sr.getResults().get(0);
2452 assertSame(cds.getSequenceAt(1).getDatasetSequence(), m.getSequence());
2453 assertEquals(7, m.getStart());
2454 assertEquals(9, m.getEnd());
2458 * Test the method that just copies aligned sequences, provided all sequences
2459 * to be aligned share the aligned sequence's dataset
2461 @Test(groups = "Functional")
2462 public void testAlignAsSameSequences()
2464 SequenceI dna1 = new Sequence("dna1", "cccGGGTTTaaa");
2465 SequenceI dna2 = new Sequence("dna2", "CCCgggtttAAA");
2466 AlignmentI al1 = new Alignment(new SequenceI[] { dna1, dna2 });
2467 ((Alignment) al1).createDatasetAlignment();
2469 SequenceI dna3 = new Sequence(dna1);
2470 SequenceI dna4 = new Sequence(dna2);
2471 assertSame(dna3.getDatasetSequence(), dna1.getDatasetSequence());
2472 assertSame(dna4.getDatasetSequence(), dna2.getDatasetSequence());
2473 String seq1 = "-cc-GG-GT-TT--aaa";
2474 dna3.setSequence(seq1);
2475 String seq2 = "C--C-Cgg--gtt-tAA-A-";
2476 dna4.setSequence(seq2);
2477 AlignmentI al2 = new Alignment(new SequenceI[] { dna3, dna4 });
2478 ((Alignment) al2).createDatasetAlignment();
2480 assertTrue(AlignmentUtils.alignAsSameSequences(al1, al2));
2481 assertEquals(seq1, al1.getSequenceAt(0).getSequenceAsString());
2482 assertEquals(seq2, al1.getSequenceAt(1).getSequenceAsString());
2485 * add another sequence to 'aligned' - should still succeed, since
2486 * unaligned sequences still share a dataset with aligned sequences
2488 SequenceI dna5 = new Sequence("dna5", "CCCgggtttAAA");
2489 dna5.createDatasetSequence();
2490 al2.addSequence(dna5);
2491 assertTrue(AlignmentUtils.alignAsSameSequences(al1, al2));
2492 assertEquals(seq1, al1.getSequenceAt(0).getSequenceAsString());
2493 assertEquals(seq2, al1.getSequenceAt(1).getSequenceAsString());
2496 * add another sequence to 'unaligned' - should fail, since now not
2497 * all unaligned sequences share a dataset with aligned sequences
2499 SequenceI dna6 = new Sequence("dna6", "CCCgggtttAAA");
2500 dna6.createDatasetSequence();
2501 al1.addSequence(dna6);
2502 // JAL-2110 JBP Comment: what's the use case for this behaviour ?
2503 assertFalse(AlignmentUtils.alignAsSameSequences(al1, al2));
2506 @Test(groups = "Functional")
2507 public void testAlignAsSameSequencesMultipleSubSeq()
2509 SequenceI dna1 = new Sequence("dna1", "cccGGGTTTaaa");
2510 SequenceI dna2 = new Sequence("dna2", "CCCgggtttAAA");
2511 SequenceI as1 = dna1.deriveSequence();
2512 SequenceI as2 = dna1.deriveSequence().getSubSequence(3, 7);
2513 SequenceI as3 = dna2.deriveSequence();
2514 as1.insertCharAt(6, 5, '-');
2515 String s_as1 = as1.getSequenceAsString();
2516 as2.insertCharAt(6, 5, '-');
2517 String s_as2 = as2.getSequenceAsString();
2518 as3.insertCharAt(6, 5, '-');
2519 String s_as3 = as3.getSequenceAsString();
2520 AlignmentI aligned = new Alignment(new SequenceI[] { as1, as2, as3 });
2522 // why do we need to cast this still ?
2523 ((Alignment) aligned).createDatasetAlignment();
2524 SequenceI uas1 = dna1.deriveSequence();
2525 SequenceI uas2 = dna1.deriveSequence().getSubSequence(3, 7);
2526 SequenceI uas3 = dna2.deriveSequence();
2527 AlignmentI tobealigned = new Alignment(new SequenceI[] { uas1, uas2,
2529 ((Alignment) tobealigned).createDatasetAlignment();
2531 assertTrue(AlignmentUtils.alignAsSameSequences(tobealigned, aligned));
2532 assertEquals(s_as1, uas1.getSequenceAsString());
2533 assertEquals(s_as2, uas2.getSequenceAsString());
2534 assertEquals(s_as3, uas3.getSequenceAsString());
2538 * Tests for the method that maps nucleotide to protein based on CDS features
2540 @Test(groups = "Functional")
2541 public void testMapCdsToProtein()
2543 SequenceI peptide = new Sequence("pep", "KLQ");
2546 * Case 1: CDS 3 times length of peptide
2547 * NB method only checks lengths match, not translation
2549 SequenceI dna = new Sequence("dna", "AACGacgtCTCCT");
2550 dna.createDatasetSequence();
2551 dna.addSequenceFeature(new SequenceFeature("CDS", "", 1, 4, null));
2552 dna.addSequenceFeature(new SequenceFeature("CDS", "", 9, 13, null));
2553 MapList ml = AlignmentUtils.mapCdsToProtein(dna, peptide);
2554 assertEquals(3, ml.getFromRatio());
2555 assertEquals(1, ml.getToRatio());
2556 assertEquals("[[1, 3]]",
2557 Arrays.deepToString(ml.getToRanges().toArray()));
2558 assertEquals("[[1, 4], [9, 13]]",
2559 Arrays.deepToString(ml.getFromRanges().toArray()));
2562 * Case 2: CDS 3 times length of peptide + stop codon
2563 * (note code does not currently check trailing codon is a stop codon)
2565 dna = new Sequence("dna", "AACGacgtCTCCTCCC");
2566 dna.createDatasetSequence();
2567 dna.addSequenceFeature(new SequenceFeature("CDS", "", 1, 4, null));
2568 dna.addSequenceFeature(new SequenceFeature("CDS", "", 9, 16, null));
2569 ml = AlignmentUtils.mapCdsToProtein(dna, peptide);
2570 assertEquals(3, ml.getFromRatio());
2571 assertEquals(1, ml.getToRatio());
2572 assertEquals("[[1, 3]]",
2573 Arrays.deepToString(ml.getToRanges().toArray()));
2574 assertEquals("[[1, 4], [9, 13]]",
2575 Arrays.deepToString(ml.getFromRanges().toArray()));
2578 * Case 3: CDS longer than 3 * peptide + stop codon - no mapping is made
2580 dna = new Sequence("dna", "AACGacgtCTCCTTGATCA");
2581 dna.createDatasetSequence();
2582 dna.addSequenceFeature(new SequenceFeature("CDS", "", 1, 4, null));
2583 dna.addSequenceFeature(new SequenceFeature("CDS", "", 9, 19, null));
2584 ml = AlignmentUtils.mapCdsToProtein(dna, peptide);
2588 * Case 4: CDS shorter than 3 * peptide - no mapping is made
2590 dna = new Sequence("dna", "AACGacgtCTCC");
2591 dna.createDatasetSequence();
2592 dna.addSequenceFeature(new SequenceFeature("CDS", "", 1, 4, null));
2593 dna.addSequenceFeature(new SequenceFeature("CDS", "", 9, 12, null));
2594 ml = AlignmentUtils.mapCdsToProtein(dna, peptide);
2598 * Case 5: CDS 3 times length of peptide + part codon - mapping is truncated
2600 dna = new Sequence("dna", "AACGacgtCTCCTTG");
2601 dna.createDatasetSequence();
2602 dna.addSequenceFeature(new SequenceFeature("CDS", "", 1, 4, null));
2603 dna.addSequenceFeature(new SequenceFeature("CDS", "", 9, 15, null));
2604 ml = AlignmentUtils.mapCdsToProtein(dna, peptide);
2605 assertEquals(3, ml.getFromRatio());
2606 assertEquals(1, ml.getToRatio());
2607 assertEquals("[[1, 3]]",
2608 Arrays.deepToString(ml.getToRanges().toArray()));
2609 assertEquals("[[1, 4], [9, 13]]",
2610 Arrays.deepToString(ml.getFromRanges().toArray()));
2613 * Case 6: incomplete start codon corresponding to X in peptide
2615 dna = new Sequence("dna", "ACGacgtCTCCTTGG");
2616 dna.createDatasetSequence();
2617 SequenceFeature sf = new SequenceFeature("CDS", "", 1, 3, null);
2618 sf.setPhase("2"); // skip 2 positions (AC) to start of next codon (GCT)
2619 dna.addSequenceFeature(sf);
2620 dna.addSequenceFeature(new SequenceFeature("CDS", "", 8, 15, null));
2621 peptide = new Sequence("pep", "XLQ");
2622 ml = AlignmentUtils.mapCdsToProtein(dna, peptide);
2623 assertEquals("[[2, 3]]",
2624 Arrays.deepToString(ml.getToRanges().toArray()));
2625 assertEquals("[[3, 3], [8, 12]]",
2626 Arrays.deepToString(ml.getFromRanges().toArray()));
2630 * Tests for the method that locates the CDS sequence that has a mapping to
2631 * the given protein. That is, given a transcript-to-peptide mapping, find the
2632 * cds-to-peptide mapping that relates to both, and return the CDS sequence.
2635 public void testFindCdsForProtein()
2637 List<AlignedCodonFrame> mappings = new ArrayList<>();
2638 AlignedCodonFrame acf1 = new AlignedCodonFrame();
2641 SequenceI dna1 = new Sequence("dna1", "cgatATcgGCTATCTATGacg");
2642 dna1.createDatasetSequence();
2644 // NB we currently exclude STOP codon from CDS sequences
2645 // the test would need to change if this changes in future
2646 SequenceI cds1 = new Sequence("cds1", "ATGCTATCT");
2647 cds1.createDatasetSequence();
2649 SequenceI pep1 = new Sequence("pep1", "MLS");
2650 pep1.createDatasetSequence();
2651 List<AlignedCodonFrame> seqMappings = new ArrayList<>();
2652 MapList mapList = new MapList(
2654 { 5, 6, 9, 15 }, new int[] { 1, 3 }, 3, 1);
2655 Mapping dnaToPeptide = new Mapping(pep1.getDatasetSequence(), mapList);
2657 // add dna to peptide mapping
2658 seqMappings.add(acf1);
2659 acf1.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(),
2663 * first case - no dna-to-CDS mapping exists - search fails
2665 SequenceI seq = AlignmentUtils.findCdsForProtein(mappings, dna1,
2666 seqMappings, dnaToPeptide);
2670 * second case - CDS-to-peptide mapping exists but no dna-to-CDS
2673 // todo this test fails if the mapping is added to acf1, not acf2
2674 // need to tidy up use of lists of mappings in AlignedCodonFrame
2675 AlignedCodonFrame acf2 = new AlignedCodonFrame();
2677 MapList cdsToPeptideMapping = new MapList(new int[]
2678 { 1, 9 }, new int[] { 1, 3 }, 3, 1);
2679 acf2.addMap(cds1.getDatasetSequence(), pep1.getDatasetSequence(),
2680 cdsToPeptideMapping);
2681 assertNull(AlignmentUtils.findCdsForProtein(mappings, dna1, seqMappings,
2685 * third case - add dna-to-CDS mapping - CDS is now found!
2687 MapList dnaToCdsMapping = new MapList(new int[] { 5, 6, 9, 15 },
2690 acf1.addMap(dna1.getDatasetSequence(), cds1.getDatasetSequence(),
2692 seq = AlignmentUtils.findCdsForProtein(mappings, dna1, seqMappings,
2694 assertSame(seq, cds1.getDatasetSequence());
2698 * Tests for the method that locates the CDS sequence that has a mapping to
2699 * the given protein. That is, given a transcript-to-peptide mapping, find the
2700 * cds-to-peptide mapping that relates to both, and return the CDS sequence.
2701 * This test is for the case where transcript and CDS are the same length.
2704 public void testFindCdsForProtein_noUTR()
2706 List<AlignedCodonFrame> mappings = new ArrayList<>();
2707 AlignedCodonFrame acf1 = new AlignedCodonFrame();
2710 SequenceI dna1 = new Sequence("dna1", "ATGCTATCTTAA");
2711 dna1.createDatasetSequence();
2713 // NB we currently exclude STOP codon from CDS sequences
2714 // the test would need to change if this changes in future
2715 SequenceI cds1 = new Sequence("cds1", "ATGCTATCT");
2716 cds1.createDatasetSequence();
2718 SequenceI pep1 = new Sequence("pep1", "MLS");
2719 pep1.createDatasetSequence();
2720 List<AlignedCodonFrame> seqMappings = new ArrayList<>();
2721 MapList mapList = new MapList(
2723 { 1, 9 }, new int[] { 1, 3 }, 3, 1);
2724 Mapping dnaToPeptide = new Mapping(pep1.getDatasetSequence(), mapList);
2726 // add dna to peptide mapping
2727 seqMappings.add(acf1);
2728 acf1.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(),
2732 * first case - transcript lacks CDS features - it appears to be
2733 * the CDS sequence and is returned
2735 SequenceI seq = AlignmentUtils.findCdsForProtein(mappings, dna1,
2736 seqMappings, dnaToPeptide);
2737 assertSame(seq, dna1.getDatasetSequence());
2740 * second case - transcript has CDS feature - this means it is
2741 * not returned as a match for CDS (CDS sequences don't have CDS features)
2743 dna1.addSequenceFeature(
2744 new SequenceFeature(SequenceOntologyI.CDS, "cds", 1, 12, null));
2745 seq = AlignmentUtils.findCdsForProtein(mappings, dna1, seqMappings,
2750 * third case - CDS-to-peptide mapping exists but no dna-to-CDS
2753 // todo this test fails if the mapping is added to acf1, not acf2
2754 // need to tidy up use of lists of mappings in AlignedCodonFrame
2755 AlignedCodonFrame acf2 = new AlignedCodonFrame();
2757 MapList cdsToPeptideMapping = new MapList(new int[]
2758 { 1, 9 }, new int[] { 1, 3 }, 3, 1);
2759 acf2.addMap(cds1.getDatasetSequence(), pep1.getDatasetSequence(),
2760 cdsToPeptideMapping);
2761 assertNull(AlignmentUtils.findCdsForProtein(mappings, dna1, seqMappings,
2765 * fourth case - add dna-to-CDS mapping - CDS is now found!
2767 MapList dnaToCdsMapping = new MapList(new int[] { 1, 9 },
2770 acf1.addMap(dna1.getDatasetSequence(), cds1.getDatasetSequence(),
2772 seq = AlignmentUtils.findCdsForProtein(mappings, dna1, seqMappings,
2774 assertSame(seq, cds1.getDatasetSequence());