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 java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
35 import java.util.TreeMap;
37 import org.testng.annotations.BeforeClass;
38 import org.testng.annotations.Test;
40 import jalview.datamodel.AlignedCodonFrame;
41 import jalview.datamodel.Alignment;
42 import jalview.datamodel.AlignmentAnnotation;
43 import jalview.datamodel.AlignmentI;
44 import jalview.datamodel.Annotation;
45 import jalview.datamodel.DBRefEntry;
46 import jalview.datamodel.GeneLociI;
47 import jalview.datamodel.Mapping;
48 import jalview.datamodel.SearchResultMatchI;
49 import jalview.datamodel.SearchResultsI;
50 import jalview.datamodel.Sequence;
51 import jalview.datamodel.SequenceFeature;
52 import jalview.datamodel.SequenceI;
53 import jalview.gui.JvOptionPane;
54 import jalview.io.AppletFormatAdapter;
55 import jalview.io.DataSourceType;
56 import jalview.io.FileFormat;
57 import jalview.io.FileFormatI;
58 import jalview.io.FormatAdapter;
59 import jalview.io.gff.SequenceOntologyI;
60 import jalview.util.MapList;
61 import jalview.util.MappingUtils;
63 public class AlignmentUtilsTests
65 private static Sequence ts = new Sequence("short",
66 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm");
68 @BeforeClass(alwaysRun = true)
69 public void setUpJvOptionPane()
71 JvOptionPane.setInteractiveMode(false);
72 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
75 @Test(groups = { "Functional" })
76 public void testExpandContext()
78 AlignmentI al = new Alignment(new Sequence[] {});
79 for (int i = 4; i < 14; i += 2)
81 SequenceI s1 = ts.deriveSequence().getSubSequence(i, i + 7);
84 System.out.println(new AppletFormatAdapter()
85 .formatSequences(FileFormat.Clustal, al, true));
86 for (int flnk = -1; flnk < 25; flnk++)
88 AlignmentI exp = AlignmentUtils.expandContext(al, flnk);
89 System.out.println("\nFlank size: " + flnk);
90 System.out.println(new AppletFormatAdapter()
91 .formatSequences(FileFormat.Clustal, exp, true));
95 * Full expansion to complete sequences
97 for (SequenceI sq : exp.getSequences())
99 String ung = sq.getSequenceAsString().replaceAll("-+", "");
100 final String errorMsg = "Flanking sequence not the same as original dataset sequence.\n"
102 + sq.getDatasetSequence().getSequenceAsString();
103 assertTrue(errorMsg, ung.equalsIgnoreCase(
104 sq.getDatasetSequence().getSequenceAsString()));
110 * Last sequence is fully expanded, others have leading gaps to match
112 assertTrue(exp.getSequenceAt(4).getSequenceAsString()
114 assertTrue(exp.getSequenceAt(3).getSequenceAsString()
115 .startsWith("--abc"));
116 assertTrue(exp.getSequenceAt(2).getSequenceAsString()
117 .startsWith("----abc"));
118 assertTrue(exp.getSequenceAt(1).getSequenceAsString()
119 .startsWith("------abc"));
120 assertTrue(exp.getSequenceAt(0).getSequenceAsString()
121 .startsWith("--------abc"));
127 * Test that annotations are correctly adjusted by expandContext
129 @Test(groups = { "Functional" })
130 public void testExpandContext_annotation()
132 AlignmentI al = new Alignment(new Sequence[] {});
133 SequenceI ds = new Sequence("Seq1", "ABCDEFGHI");
135 SequenceI seq1 = ds.deriveSequence().getSubSequence(3, 6);
136 al.addSequence(seq1);
139 * Annotate DEF with 4/5/6 respectively
141 Annotation[] anns = new Annotation[] { new Annotation(4),
142 new Annotation(5), new Annotation(6) };
143 AlignmentAnnotation ann = new AlignmentAnnotation("SS",
144 "secondary structure", anns);
145 seq1.addAlignmentAnnotation(ann);
148 * The annotations array should match aligned positions
150 assertEquals(3, ann.annotations.length);
151 assertEquals(4, ann.annotations[0].value, 0.001);
152 assertEquals(5, ann.annotations[1].value, 0.001);
153 assertEquals(6, ann.annotations[2].value, 0.001);
156 * Check annotation to sequence position mappings before expanding the
157 * sequence; these are set up in Sequence.addAlignmentAnnotation ->
158 * Annotation.setSequenceRef -> createSequenceMappings
160 assertNull(ann.getAnnotationForPosition(1));
161 assertNull(ann.getAnnotationForPosition(2));
162 assertNull(ann.getAnnotationForPosition(3));
163 assertEquals(4, ann.getAnnotationForPosition(4).value, 0.001);
164 assertEquals(5, ann.getAnnotationForPosition(5).value, 0.001);
165 assertEquals(6, ann.getAnnotationForPosition(6).value, 0.001);
166 assertNull(ann.getAnnotationForPosition(7));
167 assertNull(ann.getAnnotationForPosition(8));
168 assertNull(ann.getAnnotationForPosition(9));
171 * Expand the subsequence to the full sequence abcDEFghi
173 AlignmentI expanded = AlignmentUtils.expandContext(al, -1);
174 assertEquals("abcDEFghi",
175 expanded.getSequenceAt(0).getSequenceAsString());
178 * Confirm the alignment and sequence have the same SS annotation,
179 * referencing the expanded sequence
181 ann = expanded.getSequenceAt(0).getAnnotation()[0];
182 assertSame(ann, expanded.getAlignmentAnnotation()[0]);
183 assertSame(expanded.getSequenceAt(0), ann.sequenceRef);
186 * The annotations array should have null values except for annotated
189 assertNull(ann.annotations[0]);
190 assertNull(ann.annotations[1]);
191 assertNull(ann.annotations[2]);
192 assertEquals(4, ann.annotations[3].value, 0.001);
193 assertEquals(5, ann.annotations[4].value, 0.001);
194 assertEquals(6, ann.annotations[5].value, 0.001);
195 assertNull(ann.annotations[6]);
196 assertNull(ann.annotations[7]);
197 assertNull(ann.annotations[8]);
200 * sequence position mappings should be unchanged
202 assertNull(ann.getAnnotationForPosition(1));
203 assertNull(ann.getAnnotationForPosition(2));
204 assertNull(ann.getAnnotationForPosition(3));
205 assertEquals(4, ann.getAnnotationForPosition(4).value, 0.001);
206 assertEquals(5, ann.getAnnotationForPosition(5).value, 0.001);
207 assertEquals(6, ann.getAnnotationForPosition(6).value, 0.001);
208 assertNull(ann.getAnnotationForPosition(7));
209 assertNull(ann.getAnnotationForPosition(8));
210 assertNull(ann.getAnnotationForPosition(9));
214 * Test method that returns a map of lists of sequences by sequence name.
216 * @throws IOException
218 @Test(groups = { "Functional" })
219 public void testGetSequencesByName() throws IOException
221 final String data = ">Seq1Name\nKQYL\n" + ">Seq2Name\nRFPW\n"
222 + ">Seq1Name\nABCD\n";
223 AlignmentI al = loadAlignment(data, FileFormat.Fasta);
224 Map<String, List<SequenceI>> map = AlignmentUtils
225 .getSequencesByName(al);
226 assertEquals(2, map.keySet().size());
227 assertEquals(2, map.get("Seq1Name").size());
228 assertEquals("KQYL", map.get("Seq1Name").get(0).getSequenceAsString());
229 assertEquals("ABCD", map.get("Seq1Name").get(1).getSequenceAsString());
230 assertEquals(1, map.get("Seq2Name").size());
231 assertEquals("RFPW", map.get("Seq2Name").get(0).getSequenceAsString());
235 * Helper method to load an alignment and ensure dataset sequences are set up.
241 * @throws IOException
243 protected AlignmentI loadAlignment(final String data, FileFormatI format)
246 AlignmentI a = new FormatAdapter().readFile(data, DataSourceType.PASTE,
253 * Test mapping of protein to cDNA, for the case where we have no sequence
254 * cross-references, so mappings are made first-served 1-1 where sequences
257 * @throws IOException
259 @Test(groups = { "Functional" })
260 public void testMapProteinAlignmentToCdna_noXrefs() throws IOException
262 List<SequenceI> protseqs = new ArrayList<>();
263 protseqs.add(new Sequence("UNIPROT|V12345", "EIQ"));
264 protseqs.add(new Sequence("UNIPROT|V12346", "EIQ"));
265 protseqs.add(new Sequence("UNIPROT|V12347", "SAR"));
266 AlignmentI protein = new Alignment(protseqs.toArray(new SequenceI[3]));
267 protein.setDataset(null);
269 List<SequenceI> dnaseqs = new ArrayList<>();
270 dnaseqs.add(new Sequence("EMBL|A11111", "TCAGCACGC")); // = SAR
271 dnaseqs.add(new Sequence("EMBL|A22222", "GAGATACAA")); // = EIQ
272 dnaseqs.add(new Sequence("EMBL|A33333", "GAAATCCAG")); // = EIQ
273 dnaseqs.add(new Sequence("EMBL|A44444", "GAAATTCAG")); // = EIQ
274 AlignmentI cdna = new Alignment(dnaseqs.toArray(new SequenceI[4]));
275 cdna.setDataset(null);
277 assertTrue(AlignmentUtils.mapProteinAlignmentToCdna(protein, cdna));
279 // 3 mappings made, each from 1 to 1 sequence
280 assertEquals(3, protein.getCodonFrames().size());
281 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).size());
282 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).size());
283 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(2)).size());
285 // V12345 mapped to A22222
286 AlignedCodonFrame acf = protein.getCodonFrame(protein.getSequenceAt(0))
288 assertEquals(1, acf.getdnaSeqs().length);
289 assertEquals(cdna.getSequenceAt(1).getDatasetSequence(),
290 acf.getdnaSeqs()[0]);
291 Mapping[] protMappings = acf.getProtMappings();
292 assertEquals(1, protMappings.length);
293 MapList mapList = protMappings[0].getMap();
294 assertEquals(3, mapList.getFromRatio());
295 assertEquals(1, mapList.getToRatio());
297 Arrays.equals(new int[]
298 { 1, 9 }, mapList.getFromRanges().get(0)));
299 assertEquals(1, mapList.getFromRanges().size());
301 Arrays.equals(new int[]
302 { 1, 3 }, mapList.getToRanges().get(0)));
303 assertEquals(1, mapList.getToRanges().size());
305 // V12346 mapped to A33333
306 acf = protein.getCodonFrame(protein.getSequenceAt(1)).get(0);
307 assertEquals(1, acf.getdnaSeqs().length);
308 assertEquals(cdna.getSequenceAt(2).getDatasetSequence(),
309 acf.getdnaSeqs()[0]);
311 // V12347 mapped to A11111
312 acf = protein.getCodonFrame(protein.getSequenceAt(2)).get(0);
313 assertEquals(1, acf.getdnaSeqs().length);
314 assertEquals(cdna.getSequenceAt(0).getDatasetSequence(),
315 acf.getdnaSeqs()[0]);
317 // no mapping involving the 'extra' A44444
318 assertTrue(protein.getCodonFrame(cdna.getSequenceAt(3)).isEmpty());
322 * Test for the alignSequenceAs method that takes two sequences and a mapping.
324 @Test(groups = { "Functional" })
325 public void testAlignSequenceAs_withMapping_noIntrons()
327 MapList map = new MapList(new int[] { 1, 6 }, new int[] { 1, 2 }, 3, 1);
330 * No existing gaps in dna:
332 checkAlignSequenceAs("GGGAAA", "-A-L-", false, false, map,
336 * Now introduce gaps in dna but ignore them when realigning.
338 checkAlignSequenceAs("-G-G-G-A-A-A-", "-A-L-", false, false, map,
342 * Now include gaps in dna when realigning. First retaining 'mapped' gaps
343 * only, i.e. those within the exon region.
345 checkAlignSequenceAs("-G-G--G-A--A-A-", "-A-L-", true, false, map,
346 "---G-G--G---A--A-A");
349 * Include all gaps in dna when realigning (within and without the exon
350 * region). The leading gap, and the gaps between codons, are subsumed by
351 * the protein alignment gap.
353 checkAlignSequenceAs("-G-GG--AA-A---", "-A-L-", true, true, map,
354 "---G-GG---AA-A---");
357 * Include only unmapped gaps in dna when realigning (outside the exon
358 * region). The leading gap, and the gaps between codons, are subsumed by
359 * the protein alignment gap.
361 checkAlignSequenceAs("-G-GG--AA-A-", "-A-L-", false, true, map,
366 * Test for the alignSequenceAs method that takes two sequences and a mapping.
368 @Test(groups = { "Functional" })
369 public void testAlignSequenceAs_withMapping_withIntrons()
372 * Exons at codon 2 (AAA) and 4 (TTT)
374 MapList map = new MapList(new int[] { 4, 6, 10, 12 },
379 * Simple case: no gaps in dna
381 checkAlignSequenceAs("GGGAAACCCTTTGGG", "--A-L-", false, false, map,
382 "GGG---AAACCCTTTGGG");
385 * Add gaps to dna - but ignore when realigning.
387 checkAlignSequenceAs("-G-G-G--A--A---AC-CC-T-TT-GG-G-", "--A-L-", false,
388 false, map, "GGG---AAACCCTTTGGG");
391 * Add gaps to dna - include within exons only when realigning.
393 checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-", true,
394 false, map, "GGG---A--A---ACCCT-TTGGG");
397 * Include gaps outside exons only when realigning.
399 checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
400 false, true, map, "-G-G-GAAAC-CCTTT-GG-G-");
403 * Include gaps following first intron if we are 'preserving mapped gaps'
405 checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-", true,
406 true, map, "-G-G-G--A--A---A-C-CC-T-TT-GG-G-");
409 * Include all gaps in dna when realigning.
411 checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-", true,
412 true, map, "-G-G-G--A--A---A-C-CC-T-TT-GG-G-");
416 * Test for the case where not all of the protein sequence is mapped to cDNA.
418 @Test(groups = { "Functional" })
419 public void testAlignSequenceAs_withMapping_withUnmappedProtein()
422 * Exons at codon 2 (AAA) and 4 (TTT) mapped to A and P
424 final MapList map = new MapList(new int[] { 4, 6, 10, 12 },
426 { 1, 1, 3, 3 }, 3, 1);
429 * -L- 'aligns' ccc------
431 checkAlignSequenceAs("gggAAAcccTTTggg", "-A-L-P-", false, false, map,
432 "gggAAAccc------TTTggg");
436 * Helper method that performs and verifies the method under test.
439 * the sequence to be realigned
441 * the sequence whose alignment is to be copied
442 * @param preserveMappedGaps
443 * @param preserveUnmappedGaps
447 protected void checkAlignSequenceAs(final String alignee,
448 final String alignModel, final boolean preserveMappedGaps,
449 final boolean preserveUnmappedGaps, MapList map,
450 final String expected)
452 SequenceI alignMe = new Sequence("Seq1", alignee);
453 alignMe.createDatasetSequence();
454 SequenceI alignFrom = new Sequence("Seq2", alignModel);
455 alignFrom.createDatasetSequence();
456 AlignedCodonFrame acf = new AlignedCodonFrame();
457 acf.addMap(alignMe.getDatasetSequence(), alignFrom.getDatasetSequence(),
460 AlignmentUtils.alignSequenceAs(alignMe, alignFrom, acf, "---", '-',
461 preserveMappedGaps, preserveUnmappedGaps);
462 assertEquals(expected, alignMe.getSequenceAsString());
466 * Test for the alignSequenceAs method where we preserve gaps in introns only.
468 @Test(groups = { "Functional" })
469 public void testAlignSequenceAs_keepIntronGapsOnly()
473 * Intron GGGAAA followed by exon CCCTTT
475 MapList map = new MapList(new int[] { 7, 12 }, new int[] { 1, 2 }, 3,
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(
504 { prot1, prot2, prot3, prot4 });
505 protein.setDataset(null);
507 MapList map = new MapList(new int[] { 1, 12 }, new int[] { 1, 4 }, 3,
509 AlignedCodonFrame acf = new AlignedCodonFrame();
510 acf.addMap(dna1.getDatasetSequence(), prot1.getDatasetSequence(), map);
511 acf.addMap(dna2.getDatasetSequence(), prot2.getDatasetSequence(), map);
512 acf.addMap(dna3.getDatasetSequence(), prot3.getDatasetSequence(), map);
513 ArrayList<AlignedCodonFrame> acfs = new ArrayList<>();
515 protein.setCodonFrames(acfs);
518 * Translated codon order is [1,2,3] [1,3,4] [4,5,6] [4,5,7] [5,6,7] [7,8,9]
519 * [8,9,10] [10,11,12] [11,12,13]
521 AlignmentUtils.alignProteinAsDna(protein, dna);
522 assertEquals("C-H--Y-Q-", prot1.getSequenceAsString());
523 assertEquals("-C--H-Y-Q", prot2.getSequenceAsString());
524 assertEquals("C--H--Y-Q", prot3.getSequenceAsString());
525 assertEquals("R-QSV", prot4.getSequenceAsString());
529 * Test the method that tests whether a CDNA sequence translates to a protein
532 @Test(groups = { "Functional" })
533 public void testTranslatesAs()
535 // null arguments check
536 assertFalse(AlignmentUtils.translatesAs(null, 0, null));
537 assertFalse(AlignmentUtils.translatesAs(new char[] { 't' }, 0, null));
538 assertFalse(AlignmentUtils.translatesAs(null, 0, new char[] { 'a' }));
540 // straight translation
541 assertTrue(AlignmentUtils.translatesAs("tttcccaaaggg".toCharArray(), 0,
542 "FPKG".toCharArray()));
543 // with extra start codon (not in protein)
544 assertTrue(AlignmentUtils.translatesAs("atgtttcccaaaggg".toCharArray(),
545 3, "FPKG".toCharArray()));
546 // with stop codon1 (not in protein)
547 assertTrue(AlignmentUtils.translatesAs("tttcccaaagggtaa".toCharArray(),
548 0, "FPKG".toCharArray()));
549 // with stop codon1 (in protein as *)
550 assertTrue(AlignmentUtils.translatesAs("tttcccaaagggtaa".toCharArray(),
551 0, "FPKG*".toCharArray()));
552 // with stop codon2 (not in protein)
553 assertTrue(AlignmentUtils.translatesAs("tttcccaaagggtag".toCharArray(),
554 0, "FPKG".toCharArray()));
555 // with stop codon3 (not in protein)
556 assertTrue(AlignmentUtils.translatesAs("tttcccaaagggtga".toCharArray(),
557 0, "FPKG".toCharArray()));
558 // with start and stop codon1
559 assertTrue(AlignmentUtils.translatesAs(
560 "atgtttcccaaagggtaa".toCharArray(), 3, "FPKG".toCharArray()));
561 // with start and stop codon1 (in protein as *)
562 assertTrue(AlignmentUtils.translatesAs(
563 "atgtttcccaaagggtaa".toCharArray(), 3, "FPKG*".toCharArray()));
564 // with start and stop codon2
565 assertTrue(AlignmentUtils.translatesAs(
566 "atgtttcccaaagggtag".toCharArray(), 3, "FPKG".toCharArray()));
567 // with start and stop codon3
568 assertTrue(AlignmentUtils.translatesAs(
569 "atgtttcccaaagggtga".toCharArray(), 3, "FPKG".toCharArray()));
571 // with embedded stop codons
572 assertTrue(AlignmentUtils.translatesAs(
573 "atgtttTAGcccaaaTAAgggtga".toCharArray(), 3,
574 "F*PK*G".toCharArray()));
577 assertFalse(AlignmentUtils.translatesAs("tttcccaaaggg".toCharArray(), 0,
578 "FPMG".toCharArray()));
581 assertFalse(AlignmentUtils.translatesAs("tttcccaaagg".toCharArray(), 0,
582 "FPKG".toCharArray()));
585 assertFalse(AlignmentUtils.translatesAs("tttcccaaaggg".toCharArray(), 0,
586 "FPK".toCharArray()));
588 // overlong dna (doesn't end in stop codon)
589 assertFalse(AlignmentUtils.translatesAs("tttcccaaagggttt".toCharArray(),
590 0, "FPKG".toCharArray()));
592 // dna + stop codon + more
593 assertFalse(AlignmentUtils.translatesAs(
594 "tttcccaaagggttaga".toCharArray(), 0, "FPKG".toCharArray()));
597 assertFalse(AlignmentUtils.translatesAs("tttcccaaaggg".toCharArray(), 0,
598 "FPKGQ".toCharArray()));
602 * Test mapping of protein to cDNA, for cases where the cDNA has start and/or
603 * stop codons in addition to the protein coding sequence.
605 * @throws IOException
607 @Test(groups = { "Functional" })
608 public void testMapProteinAlignmentToCdna_withStartAndStopCodons()
611 List<SequenceI> protseqs = new ArrayList<>();
612 protseqs.add(new Sequence("UNIPROT|V12345", "EIQ"));
613 protseqs.add(new Sequence("UNIPROT|V12346", "EIQ"));
614 protseqs.add(new Sequence("UNIPROT|V12347", "SAR"));
615 AlignmentI protein = new Alignment(protseqs.toArray(new SequenceI[3]));
616 protein.setDataset(null);
618 List<SequenceI> dnaseqs = new ArrayList<>();
620 dnaseqs.add(new Sequence("EMBL|A11111", "ATGTCAGCACGC"));
622 dnaseqs.add(new Sequence("EMBL|A22222", "GAGATACAATAA"));
623 // = start +EIQ + stop
624 dnaseqs.add(new Sequence("EMBL|A33333", "ATGGAAATCCAGTAG"));
625 dnaseqs.add(new Sequence("EMBL|A44444", "GAAATTCAG"));
626 AlignmentI cdna = new Alignment(dnaseqs.toArray(new SequenceI[4]));
627 cdna.setDataset(null);
629 assertTrue(AlignmentUtils.mapProteinAlignmentToCdna(protein, cdna));
631 // 3 mappings made, each from 1 to 1 sequence
632 assertEquals(3, protein.getCodonFrames().size());
633 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).size());
634 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).size());
635 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(2)).size());
637 // V12345 mapped from A22222
638 AlignedCodonFrame acf = protein.getCodonFrame(protein.getSequenceAt(0))
640 assertEquals(1, acf.getdnaSeqs().length);
641 assertEquals(cdna.getSequenceAt(1).getDatasetSequence(),
642 acf.getdnaSeqs()[0]);
643 Mapping[] protMappings = acf.getProtMappings();
644 assertEquals(1, protMappings.length);
645 MapList mapList = protMappings[0].getMap();
646 assertEquals(3, mapList.getFromRatio());
647 assertEquals(1, mapList.getToRatio());
649 Arrays.equals(new int[]
650 { 1, 9 }, mapList.getFromRanges().get(0)));
651 assertEquals(1, mapList.getFromRanges().size());
653 Arrays.equals(new int[]
654 { 1, 3 }, mapList.getToRanges().get(0)));
655 assertEquals(1, mapList.getToRanges().size());
657 // V12346 mapped from A33333 starting position 4
658 acf = protein.getCodonFrame(protein.getSequenceAt(1)).get(0);
659 assertEquals(1, acf.getdnaSeqs().length);
660 assertEquals(cdna.getSequenceAt(2).getDatasetSequence(),
661 acf.getdnaSeqs()[0]);
662 protMappings = acf.getProtMappings();
663 assertEquals(1, protMappings.length);
664 mapList = protMappings[0].getMap();
665 assertEquals(3, mapList.getFromRatio());
666 assertEquals(1, mapList.getToRatio());
668 Arrays.equals(new int[]
669 { 4, 12 }, mapList.getFromRanges().get(0)));
670 assertEquals(1, mapList.getFromRanges().size());
672 Arrays.equals(new int[]
673 { 1, 3 }, mapList.getToRanges().get(0)));
674 assertEquals(1, mapList.getToRanges().size());
676 // V12347 mapped to A11111 starting position 4
677 acf = protein.getCodonFrame(protein.getSequenceAt(2)).get(0);
678 assertEquals(1, acf.getdnaSeqs().length);
679 assertEquals(cdna.getSequenceAt(0).getDatasetSequence(),
680 acf.getdnaSeqs()[0]);
681 protMappings = acf.getProtMappings();
682 assertEquals(1, protMappings.length);
683 mapList = protMappings[0].getMap();
684 assertEquals(3, mapList.getFromRatio());
685 assertEquals(1, mapList.getToRatio());
687 Arrays.equals(new int[]
688 { 4, 12 }, mapList.getFromRanges().get(0)));
689 assertEquals(1, mapList.getFromRanges().size());
691 Arrays.equals(new int[]
692 { 1, 3 }, mapList.getToRanges().get(0)));
693 assertEquals(1, mapList.getToRanges().size());
695 // no mapping involving the 'extra' A44444
696 assertTrue(protein.getCodonFrame(cdna.getSequenceAt(3)).isEmpty());
700 * Test mapping of protein to cDNA, for the case where we have some sequence
701 * cross-references. Verify that 1-to-many mappings are made where
702 * cross-references exist and sequences are mappable.
704 * @throws IOException
706 @Test(groups = { "Functional" })
707 public void testMapProteinAlignmentToCdna_withXrefs() throws IOException
709 List<SequenceI> protseqs = new ArrayList<>();
710 protseqs.add(new Sequence("UNIPROT|V12345", "EIQ"));
711 protseqs.add(new Sequence("UNIPROT|V12346", "EIQ"));
712 protseqs.add(new Sequence("UNIPROT|V12347", "SAR"));
713 AlignmentI protein = new Alignment(protseqs.toArray(new SequenceI[3]));
714 protein.setDataset(null);
716 List<SequenceI> dnaseqs = new ArrayList<>();
717 dnaseqs.add(new Sequence("EMBL|A11111", "TCAGCACGC")); // = SAR
718 dnaseqs.add(new Sequence("EMBL|A22222", "ATGGAGATACAA")); // = start + EIQ
719 dnaseqs.add(new Sequence("EMBL|A33333", "GAAATCCAG")); // = EIQ
720 dnaseqs.add(new Sequence("EMBL|A44444", "GAAATTCAG")); // = EIQ
721 dnaseqs.add(new Sequence("EMBL|A55555", "GAGATTCAG")); // = EIQ
722 AlignmentI cdna = new Alignment(dnaseqs.toArray(new SequenceI[5]));
723 cdna.setDataset(null);
725 // Xref A22222 to V12345 (should get mapped)
726 dnaseqs.get(1).addDBRef(new DBRefEntry("UNIPROT", "1", "V12345"));
727 // Xref V12345 to A44444 (should get mapped)
728 protseqs.get(0).addDBRef(new DBRefEntry("EMBL", "1", "A44444"));
729 // Xref A33333 to V12347 (sequence mismatch - should not get mapped)
730 dnaseqs.get(2).addDBRef(new DBRefEntry("UNIPROT", "1", "V12347"));
731 // as V12345 is mapped to A22222 and A44444, this leaves V12346 unmapped.
732 // it should get paired up with the unmapped A33333
733 // A11111 should be mapped to V12347
734 // A55555 is spare and has no xref so is not mapped
736 assertTrue(AlignmentUtils.mapProteinAlignmentToCdna(protein, cdna));
738 // 4 protein mappings made for 3 proteins, 2 to V12345, 1 each to V12346/7
739 assertEquals(3, protein.getCodonFrames().size());
740 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).size());
741 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).size());
742 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(2)).size());
744 // one mapping for each of the first 4 cDNA sequences
745 assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(0)).size());
746 assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(1)).size());
747 assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(2)).size());
748 assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(3)).size());
750 // V12345 mapped to A22222 and A44444
751 AlignedCodonFrame acf = protein.getCodonFrame(protein.getSequenceAt(0))
753 assertEquals(2, acf.getdnaSeqs().length);
754 assertEquals(cdna.getSequenceAt(1).getDatasetSequence(),
755 acf.getdnaSeqs()[0]);
756 assertEquals(cdna.getSequenceAt(3).getDatasetSequence(),
757 acf.getdnaSeqs()[1]);
759 // V12346 mapped to A33333
760 acf = protein.getCodonFrame(protein.getSequenceAt(1)).get(0);
761 assertEquals(1, acf.getdnaSeqs().length);
762 assertEquals(cdna.getSequenceAt(2).getDatasetSequence(),
763 acf.getdnaSeqs()[0]);
765 // V12347 mapped to A11111
766 acf = protein.getCodonFrame(protein.getSequenceAt(2)).get(0);
767 assertEquals(1, acf.getdnaSeqs().length);
768 assertEquals(cdna.getSequenceAt(0).getDatasetSequence(),
769 acf.getdnaSeqs()[0]);
771 // no mapping involving the 'extra' A55555
772 assertTrue(protein.getCodonFrame(cdna.getSequenceAt(4)).isEmpty());
776 * Test mapping of protein to cDNA, for the case where we have some sequence
777 * cross-references. Verify that once we have made an xref mapping we don't
778 * also map un-xrefd sequeces.
780 * @throws IOException
782 @Test(groups = { "Functional" })
783 public void testMapProteinAlignmentToCdna_prioritiseXrefs()
786 List<SequenceI> protseqs = new ArrayList<>();
787 protseqs.add(new Sequence("UNIPROT|V12345", "EIQ"));
788 protseqs.add(new Sequence("UNIPROT|V12346", "EIQ"));
789 AlignmentI protein = new Alignment(
790 protseqs.toArray(new SequenceI[protseqs.size()]));
791 protein.setDataset(null);
793 List<SequenceI> dnaseqs = new ArrayList<>();
794 dnaseqs.add(new Sequence("EMBL|A11111", "GAAATCCAG")); // = EIQ
795 dnaseqs.add(new Sequence("EMBL|A22222", "GAAATTCAG")); // = EIQ
796 AlignmentI cdna = new Alignment(
797 dnaseqs.toArray(new SequenceI[dnaseqs.size()]));
798 cdna.setDataset(null);
800 // Xref A22222 to V12345 (should get mapped)
801 // A11111 should then be mapped to the unmapped V12346
802 dnaseqs.get(1).addDBRef(new DBRefEntry("UNIPROT", "1", "V12345"));
804 assertTrue(AlignmentUtils.mapProteinAlignmentToCdna(protein, cdna));
806 // 2 protein mappings made
807 assertEquals(2, protein.getCodonFrames().size());
808 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).size());
809 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).size());
811 // one mapping for each of the cDNA sequences
812 assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(0)).size());
813 assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(1)).size());
815 // V12345 mapped to A22222
816 AlignedCodonFrame acf = protein.getCodonFrame(protein.getSequenceAt(0))
818 assertEquals(1, acf.getdnaSeqs().length);
819 assertEquals(cdna.getSequenceAt(1).getDatasetSequence(),
820 acf.getdnaSeqs()[0]);
822 // V12346 mapped to A11111
823 acf = protein.getCodonFrame(protein.getSequenceAt(1)).get(0);
824 assertEquals(1, acf.getdnaSeqs().length);
825 assertEquals(cdna.getSequenceAt(0).getDatasetSequence(),
826 acf.getdnaSeqs()[0]);
830 * Test the method that shows or hides sequence annotations by type(s) and
833 @Test(groups = { "Functional" })
834 public void testShowOrHideSequenceAnnotations()
836 SequenceI seq1 = new Sequence("Seq1", "AAA");
837 SequenceI seq2 = new Sequence("Seq2", "BBB");
838 SequenceI seq3 = new Sequence("Seq3", "CCC");
839 Annotation[] anns = new Annotation[] { new Annotation(2f) };
840 AlignmentAnnotation ann1 = new AlignmentAnnotation("Structure", "ann1",
842 ann1.setSequenceRef(seq1);
843 AlignmentAnnotation ann2 = new AlignmentAnnotation("Structure", "ann2",
845 ann2.setSequenceRef(seq2);
846 AlignmentAnnotation ann3 = new AlignmentAnnotation("Structure", "ann3",
848 AlignmentAnnotation ann4 = new AlignmentAnnotation("Temp", "ann4",
850 ann4.setSequenceRef(seq1);
851 AlignmentAnnotation ann5 = new AlignmentAnnotation("Temp", "ann5",
853 ann5.setSequenceRef(seq2);
854 AlignmentAnnotation ann6 = new AlignmentAnnotation("Temp", "ann6",
856 AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2, seq3 });
857 al.addAnnotation(ann1); // Structure for Seq1
858 al.addAnnotation(ann2); // Structure for Seq2
859 al.addAnnotation(ann3); // Structure for no sequence
860 al.addAnnotation(ann4); // Temp for seq1
861 al.addAnnotation(ann5); // Temp for seq2
862 al.addAnnotation(ann6); // Temp for no sequence
863 List<String> types = new ArrayList<>();
864 List<SequenceI> scope = new ArrayList<>();
867 * Set all sequence related Structure to hidden (ann1, ann2)
869 types.add("Structure");
870 AlignmentUtils.showOrHideSequenceAnnotations(al, types, null, false,
872 assertFalse(ann1.visible);
873 assertFalse(ann2.visible);
874 assertTrue(ann3.visible); // not sequence-related, not affected
875 assertTrue(ann4.visible); // not Structure, not affected
876 assertTrue(ann5.visible); // "
877 assertTrue(ann6.visible); // not sequence-related, not affected
880 * Set Temp in {seq1, seq3} to hidden
886 AlignmentUtils.showOrHideSequenceAnnotations(al, types, scope, false,
888 assertFalse(ann1.visible); // unchanged
889 assertFalse(ann2.visible); // unchanged
890 assertTrue(ann3.visible); // not sequence-related, not affected
891 assertFalse(ann4.visible); // Temp for seq1 hidden
892 assertTrue(ann5.visible); // not in scope, not affected
893 assertTrue(ann6.visible); // not sequence-related, not affected
896 * Set Temp in all sequences to hidden
902 AlignmentUtils.showOrHideSequenceAnnotations(al, types, null, false,
904 assertFalse(ann1.visible); // unchanged
905 assertFalse(ann2.visible); // unchanged
906 assertTrue(ann3.visible); // not sequence-related, not affected
907 assertFalse(ann4.visible); // Temp for seq1 hidden
908 assertFalse(ann5.visible); // Temp for seq2 hidden
909 assertTrue(ann6.visible); // not sequence-related, not affected
912 * Set all types in {seq1, seq3} to visible
918 AlignmentUtils.showOrHideSequenceAnnotations(al, types, scope, true,
920 assertTrue(ann1.visible); // Structure for seq1 set visible
921 assertFalse(ann2.visible); // not in scope, unchanged
922 assertTrue(ann3.visible); // not sequence-related, not affected
923 assertTrue(ann4.visible); // Temp for seq1 set visible
924 assertFalse(ann5.visible); // not in scope, unchanged
925 assertTrue(ann6.visible); // not sequence-related, not affected
928 * Set all types in all scope to hidden
930 AlignmentUtils.showOrHideSequenceAnnotations(al, types, null, true,
932 assertFalse(ann1.visible);
933 assertFalse(ann2.visible);
934 assertTrue(ann3.visible); // not sequence-related, not affected
935 assertFalse(ann4.visible);
936 assertFalse(ann5.visible);
937 assertTrue(ann6.visible); // not sequence-related, not affected
941 * Tests for the method that checks if one sequence cross-references another
943 @Test(groups = { "Functional" })
944 public void testHasCrossRef()
946 assertFalse(AlignmentUtils.hasCrossRef(null, null));
947 SequenceI seq1 = new Sequence("EMBL|A12345", "ABCDEF");
948 assertFalse(AlignmentUtils.hasCrossRef(seq1, null));
949 assertFalse(AlignmentUtils.hasCrossRef(null, seq1));
950 SequenceI seq2 = new Sequence("UNIPROT|V20192", "ABCDEF");
951 assertFalse(AlignmentUtils.hasCrossRef(seq1, seq2));
954 seq1.addDBRef(new DBRefEntry("UNIPROT", "1", "v20193"));
955 assertFalse(AlignmentUtils.hasCrossRef(seq1, seq2));
957 // case-insensitive; version number is ignored
958 seq1.addDBRef(new DBRefEntry("UNIPROT", "1", "v20192"));
959 assertTrue(AlignmentUtils.hasCrossRef(seq1, seq2));
962 seq1.addDBRef(new DBRefEntry("UNIPROT", "1", "V20192"));
963 assertTrue(AlignmentUtils.hasCrossRef(seq1, seq2));
964 // test is one-way only
965 assertFalse(AlignmentUtils.hasCrossRef(seq2, seq1));
969 * Tests for the method that checks if either sequence cross-references the
972 @Test(groups = { "Functional" })
973 public void testHaveCrossRef()
975 assertFalse(AlignmentUtils.hasCrossRef(null, null));
976 SequenceI seq1 = new Sequence("EMBL|A12345", "ABCDEF");
977 assertFalse(AlignmentUtils.haveCrossRef(seq1, null));
978 assertFalse(AlignmentUtils.haveCrossRef(null, seq1));
979 SequenceI seq2 = new Sequence("UNIPROT|V20192", "ABCDEF");
980 assertFalse(AlignmentUtils.haveCrossRef(seq1, seq2));
982 seq1.addDBRef(new DBRefEntry("UNIPROT", "1", "V20192"));
983 assertTrue(AlignmentUtils.haveCrossRef(seq1, seq2));
984 // next is true for haveCrossRef, false for hasCrossRef
985 assertTrue(AlignmentUtils.haveCrossRef(seq2, seq1));
987 // now the other way round
988 seq1.setDBRefs(null);
989 seq2.addDBRef(new DBRefEntry("EMBL", "1", "A12345"));
990 assertTrue(AlignmentUtils.haveCrossRef(seq1, seq2));
991 assertTrue(AlignmentUtils.haveCrossRef(seq2, seq1));
994 seq1.addDBRef(new DBRefEntry("UNIPROT", "1", "V20192"));
995 assertTrue(AlignmentUtils.haveCrossRef(seq1, seq2));
996 assertTrue(AlignmentUtils.haveCrossRef(seq2, seq1));
1000 * Test the method that extracts the cds-only part of a dna alignment.
1002 @Test(groups = { "Functional" })
1003 public void testMakeCdsAlignment()
1007 * dna1 --> [4, 6] [10,12] --> pep1
1008 * dna2 --> [1, 3] [7, 9] [13,15] --> pep2
1010 SequenceI dna1 = new Sequence("dna1", "aaaGGGcccTTTaaa");
1011 SequenceI dna2 = new Sequence("dna2", "GGGcccTTTaaaCCC");
1012 SequenceI pep1 = new Sequence("pep1", "GF");
1013 SequenceI pep2 = new Sequence("pep2", "GFP");
1014 pep1.addDBRef(new DBRefEntry("UNIPROT", "0", "pep1"));
1015 pep2.addDBRef(new DBRefEntry("UNIPROT", "0", "pep2"));
1016 dna1.createDatasetSequence();
1017 dna2.createDatasetSequence();
1018 pep1.createDatasetSequence();
1019 pep2.createDatasetSequence();
1020 AlignmentI dna = new Alignment(new SequenceI[] { dna1, dna2 });
1021 dna.setDataset(null);
1024 * put a variant feature on dna2 base 8
1025 * - should transfer to cds2 base 5
1027 dna2.addSequenceFeature(
1028 new SequenceFeature("variant", "hgmd", 8, 8, 0f, null));
1031 * need a sourceDbRef if we are to construct dbrefs to the CDS
1032 * sequence from the dna contig sequences
1034 DBRefEntry dbref = new DBRefEntry("ENSEMBL", "0", "dna1");
1035 dna1.getDatasetSequence().addDBRef(dbref);
1036 org.testng.Assert.assertEquals(dbref, dna1.getPrimaryDBRefs().get(0));
1037 dbref = new DBRefEntry("ENSEMBL", "0", "dna2");
1038 dna2.getDatasetSequence().addDBRef(dbref);
1039 org.testng.Assert.assertEquals(dbref, dna2.getPrimaryDBRefs().get(0));
1042 * CDS sequences are 'discovered' from dna-to-protein mappings on the alignment
1043 * dataset (e.g. added from dbrefs by CrossRef.findXrefSequences)
1045 MapList mapfordna1 = new MapList(new int[] { 4, 6, 10, 12 },
1048 AlignedCodonFrame acf = new AlignedCodonFrame();
1049 acf.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(),
1051 dna.addCodonFrame(acf);
1052 MapList mapfordna2 = new MapList(new int[] { 1, 3, 7, 9, 13, 15 },
1055 acf = new AlignedCodonFrame();
1056 acf.addMap(dna2.getDatasetSequence(), pep2.getDatasetSequence(),
1058 dna.addCodonFrame(acf);
1061 * In this case, mappings originally came from matching Uniprot accessions
1062 * - so need an xref on dna involving those regions.
1063 * These are normally constructed from CDS annotation
1065 DBRefEntry dna1xref = new DBRefEntry("UNIPROT", "ENSEMBL", "pep1",
1066 new Mapping(mapfordna1));
1067 dna1.addDBRef(dna1xref);
1068 assertEquals(2, dna1.getDBRefs().size()); // to self and to pep1
1069 DBRefEntry dna2xref = new DBRefEntry("UNIPROT", "ENSEMBL", "pep2",
1070 new Mapping(mapfordna2));
1071 dna2.addDBRef(dna2xref);
1072 assertEquals(2, dna2.getDBRefs().size()); // to self and to pep2
1075 * execute method under test:
1077 AlignmentI cds = AlignmentUtils
1078 .makeCdsAlignment(new SequenceI[]
1079 { dna1, dna2 }, dna.getDataset(), null);
1082 * verify cds sequences
1084 assertEquals(2, cds.getSequences().size());
1085 assertEquals("GGGTTT", cds.getSequenceAt(0).getSequenceAsString());
1086 assertEquals("GGGTTTCCC", cds.getSequenceAt(1).getSequenceAsString());
1089 * verify shared, extended alignment dataset
1091 assertSame(dna.getDataset(), cds.getDataset());
1092 SequenceI cds1Dss = cds.getSequenceAt(0).getDatasetSequence();
1093 SequenceI cds2Dss = cds.getSequenceAt(1).getDatasetSequence();
1094 assertTrue(dna.getDataset().getSequences().contains(cds1Dss));
1095 assertTrue(dna.getDataset().getSequences().contains(cds2Dss));
1098 * verify CDS has a dbref with mapping to peptide
1100 assertNotNull(cds1Dss.getDBRefs());
1101 assertEquals(2, cds1Dss.getDBRefs().size());
1102 dbref = cds1Dss.getDBRefs().get(0);
1103 assertEquals(dna1xref.getSource(), dbref.getSource());
1104 // version is via ensembl's primary ref
1105 assertEquals(dna1xref.getVersion(), dbref.getVersion());
1106 assertEquals(dna1xref.getAccessionId(), dbref.getAccessionId());
1107 assertNotNull(dbref.getMap());
1108 assertSame(pep1.getDatasetSequence(), dbref.getMap().getTo());
1109 MapList cdsMapping = new MapList(new int[] { 1, 6 }, new int[] { 1, 2 },
1111 assertEquals(cdsMapping, dbref.getMap().getMap());
1114 * verify peptide has added a dbref with reverse mapping to CDS
1116 assertNotNull(pep1.getDBRefs());
1117 // FIXME pep1.getDBRefs() is 1 - is that the correct behaviour ?
1118 assertEquals(2, pep1.getDBRefs().size());
1119 dbref = pep1.getDBRefs().get(1);
1120 assertEquals("ENSEMBL", dbref.getSource());
1121 assertEquals("0", dbref.getVersion());
1122 assertEquals("CDS|dna1", dbref.getAccessionId());
1123 assertNotNull(dbref.getMap());
1124 assertSame(cds1Dss, dbref.getMap().getTo());
1125 assertEquals(cdsMapping.getInverse(), dbref.getMap().getMap());
1128 * verify cDNA has added a dbref with mapping to CDS
1130 assertEquals(3, dna1.getDBRefs().size());
1131 DBRefEntry dbRefEntry = dna1.getDBRefs().get(2);
1132 assertSame(cds1Dss, dbRefEntry.getMap().getTo());
1133 MapList dnaToCdsMapping = new MapList(new int[] { 4, 6, 10, 12 },
1136 assertEquals(dnaToCdsMapping, dbRefEntry.getMap().getMap());
1137 assertEquals(3, dna2.getDBRefs().size());
1138 dbRefEntry = dna2.getDBRefs().get(2);
1139 assertSame(cds2Dss, dbRefEntry.getMap().getTo());
1140 dnaToCdsMapping = new MapList(new int[] { 1, 3, 7, 9, 13, 15 },
1143 assertEquals(dnaToCdsMapping, dbRefEntry.getMap().getMap());
1146 * verify CDS has added a dbref with mapping to cDNA
1148 assertEquals(2, cds1Dss.getDBRefs().size());
1149 dbRefEntry = cds1Dss.getDBRefs().get(1);
1150 assertSame(dna1.getDatasetSequence(), dbRefEntry.getMap().getTo());
1151 MapList cdsToDnaMapping = new MapList(new int[] { 1, 6 },
1153 { 4, 6, 10, 12 }, 1, 1);
1154 assertEquals(cdsToDnaMapping, dbRefEntry.getMap().getMap());
1155 assertEquals(2, cds2Dss.getDBRefs().size());
1156 dbRefEntry = cds2Dss.getDBRefs().get(1);
1157 assertSame(dna2.getDatasetSequence(), dbRefEntry.getMap().getTo());
1158 cdsToDnaMapping = new MapList(new int[] { 1, 9 },
1160 { 1, 3, 7, 9, 13, 15 }, 1, 1);
1161 assertEquals(cdsToDnaMapping, dbRefEntry.getMap().getMap());
1164 * Verify mappings from CDS to peptide, cDNA to CDS, and cDNA to peptide
1165 * the mappings are on the shared alignment dataset
1166 * 6 mappings, 2*(DNA->CDS), 2*(DNA->Pep), 2*(CDS->Pep)
1168 List<AlignedCodonFrame> cdsMappings = cds.getDataset().getCodonFrames();
1169 assertEquals(6, cdsMappings.size());
1172 * verify that mapping sets for dna and cds alignments are different
1173 * [not current behaviour - all mappings are on the alignment dataset]
1175 // select -> subselect type to test.
1176 // Assert.assertNotSame(dna.getCodonFrames(), cds.getCodonFrames());
1177 // assertEquals(4, dna.getCodonFrames().size());
1178 // assertEquals(4, cds.getCodonFrames().size());
1181 * Two mappings involve pep1 (dna to pep1, cds to pep1)
1182 * Mapping from pep1 to GGGTTT in first new exon sequence
1184 List<AlignedCodonFrame> pep1Mappings = MappingUtils
1185 .findMappingsForSequence(pep1, cdsMappings);
1186 assertEquals(2, pep1Mappings.size());
1187 List<AlignedCodonFrame> mappings = MappingUtils
1188 .findMappingsForSequence(cds.getSequenceAt(0), pep1Mappings);
1189 assertEquals(1, mappings.size());
1192 SearchResultsI sr = MappingUtils.buildSearchResults(pep1, 1, mappings);
1193 assertEquals(1, sr.getResults().size());
1194 SearchResultMatchI m = sr.getResults().get(0);
1195 assertSame(cds1Dss, m.getSequence());
1196 assertEquals(1, m.getStart());
1197 assertEquals(3, m.getEnd());
1199 sr = MappingUtils.buildSearchResults(pep1, 2, mappings);
1200 m = sr.getResults().get(0);
1201 assertSame(cds1Dss, m.getSequence());
1202 assertEquals(4, m.getStart());
1203 assertEquals(6, m.getEnd());
1206 * Two mappings involve pep2 (dna to pep2, cds to pep2)
1207 * Verify mapping from pep2 to GGGTTTCCC in second new exon sequence
1209 List<AlignedCodonFrame> pep2Mappings = MappingUtils
1210 .findMappingsForSequence(pep2, cdsMappings);
1211 assertEquals(2, pep2Mappings.size());
1212 mappings = MappingUtils.findMappingsForSequence(cds.getSequenceAt(1),
1214 assertEquals(1, mappings.size());
1216 sr = MappingUtils.buildSearchResults(pep2, 1, mappings);
1217 assertEquals(1, sr.getResults().size());
1218 m = sr.getResults().get(0);
1219 assertSame(cds2Dss, m.getSequence());
1220 assertEquals(1, m.getStart());
1221 assertEquals(3, m.getEnd());
1223 sr = MappingUtils.buildSearchResults(pep2, 2, mappings);
1224 m = sr.getResults().get(0);
1225 assertSame(cds2Dss, m.getSequence());
1226 assertEquals(4, m.getStart());
1227 assertEquals(6, m.getEnd());
1229 sr = MappingUtils.buildSearchResults(pep2, 3, mappings);
1230 m = sr.getResults().get(0);
1231 assertSame(cds2Dss, m.getSequence());
1232 assertEquals(7, m.getStart());
1233 assertEquals(9, m.getEnd());
1236 * check cds2 acquired a variant feature in position 5
1238 List<SequenceFeature> sfs = cds2Dss.getSequenceFeatures();
1240 assertEquals(1, sfs.size());
1241 assertEquals("variant", sfs.get(0).type);
1242 assertEquals(5, sfs.get(0).begin);
1243 assertEquals(5, sfs.get(0).end);
1247 * Test the method that makes a cds-only alignment from a DNA sequence and its
1248 * product mappings, for the case where there are multiple exon mappings to
1249 * different protein products.
1251 @Test(groups = { "Functional" })
1252 public void testMakeCdsAlignment_multipleProteins()
1254 SequenceI dna1 = new Sequence("dna1", "aaaGGGcccTTTaaa");
1255 SequenceI pep1 = new Sequence("pep1", "GF"); // GGGTTT
1256 SequenceI pep2 = new Sequence("pep2", "KP"); // aaaccc
1257 SequenceI pep3 = new Sequence("pep3", "KF"); // aaaTTT
1258 dna1.createDatasetSequence();
1259 pep1.createDatasetSequence();
1260 pep2.createDatasetSequence();
1261 pep3.createDatasetSequence();
1262 pep1.getDatasetSequence()
1263 .addDBRef(new DBRefEntry("EMBLCDS", "2", "A12345"));
1264 pep2.getDatasetSequence()
1265 .addDBRef(new DBRefEntry("EMBLCDS", "3", "A12346"));
1266 pep3.getDatasetSequence()
1267 .addDBRef(new DBRefEntry("EMBLCDS", "4", "A12347"));
1270 * Create the CDS alignment
1272 AlignmentI dna = new Alignment(new SequenceI[] { dna1 });
1273 dna.setDataset(null);
1276 * Make the mappings from dna to protein
1278 // map ...GGG...TTT to GF
1279 MapList map = new MapList(new int[] { 4, 6, 10, 12 },
1282 AlignedCodonFrame acf = new AlignedCodonFrame();
1283 acf.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(), map);
1284 dna.addCodonFrame(acf);
1286 // map aaa...ccc to KP
1287 map = new MapList(new int[] { 1, 3, 7, 9 }, new int[] { 1, 2 }, 3, 1);
1288 acf = new AlignedCodonFrame();
1289 acf.addMap(dna1.getDatasetSequence(), pep2.getDatasetSequence(), map);
1290 dna.addCodonFrame(acf);
1292 // map aaa......TTT to KF
1293 map = new MapList(new int[] { 1, 3, 10, 12 }, new int[] { 1, 2 }, 3, 1);
1294 acf = new AlignedCodonFrame();
1295 acf.addMap(dna1.getDatasetSequence(), pep3.getDatasetSequence(), map);
1296 dna.addCodonFrame(acf);
1299 * execute method under test
1301 AlignmentI cdsal = AlignmentUtils
1302 .makeCdsAlignment(new SequenceI[]
1303 { dna1 }, dna.getDataset(), null);
1306 * Verify we have 3 cds sequences, mapped to pep1/2/3 respectively
1308 List<SequenceI> cds = cdsal.getSequences();
1309 assertEquals(3, cds.size());
1312 * verify shared, extended alignment dataset
1314 assertSame(cdsal.getDataset(), dna.getDataset());
1315 assertTrue(dna.getDataset().getSequences()
1316 .contains(cds.get(0).getDatasetSequence()));
1317 assertTrue(dna.getDataset().getSequences()
1318 .contains(cds.get(1).getDatasetSequence()));
1319 assertTrue(dna.getDataset().getSequences()
1320 .contains(cds.get(2).getDatasetSequence()));
1323 * verify aligned cds sequences and their xrefs
1325 SequenceI cdsSeq = cds.get(0);
1326 assertEquals("GGGTTT", cdsSeq.getSequenceAsString());
1327 // assertEquals("dna1|A12345", cdsSeq.getName());
1328 assertEquals("CDS|dna1", cdsSeq.getName());
1329 // assertEquals(1, cdsSeq.getDBRefs().length);
1330 // DBRefEntry cdsRef = cdsSeq.getDBRefs()[0];
1331 // assertEquals("EMBLCDS", cdsRef.getSource());
1332 // assertEquals("2", cdsRef.getVersion());
1333 // assertEquals("A12345", cdsRef.getAccessionId());
1335 cdsSeq = cds.get(1);
1336 assertEquals("aaaccc", cdsSeq.getSequenceAsString());
1337 // assertEquals("dna1|A12346", cdsSeq.getName());
1338 assertEquals("CDS|dna1", cdsSeq.getName());
1339 // assertEquals(1, cdsSeq.getDBRefs().length);
1340 // cdsRef = cdsSeq.getDBRefs()[0];
1341 // assertEquals("EMBLCDS", cdsRef.getSource());
1342 // assertEquals("3", cdsRef.getVersion());
1343 // assertEquals("A12346", cdsRef.getAccessionId());
1345 cdsSeq = cds.get(2);
1346 assertEquals("aaaTTT", cdsSeq.getSequenceAsString());
1347 // assertEquals("dna1|A12347", cdsSeq.getName());
1348 assertEquals("CDS|dna1", cdsSeq.getName());
1349 // assertEquals(1, cdsSeq.getDBRefs().length);
1350 // cdsRef = cdsSeq.getDBRefs()[0];
1351 // assertEquals("EMBLCDS", cdsRef.getSource());
1352 // assertEquals("4", cdsRef.getVersion());
1353 // assertEquals("A12347", cdsRef.getAccessionId());
1356 * Verify there are mappings from each cds sequence to its protein product
1357 * and also to its dna source
1359 List<AlignedCodonFrame> newMappings = cdsal.getCodonFrames();
1362 * 6 mappings involve dna1 (to pep1/2/3, cds1/2/3)
1364 List<AlignedCodonFrame> dnaMappings = MappingUtils
1365 .findMappingsForSequence(dna1, newMappings);
1366 assertEquals(6, dnaMappings.size());
1371 List<AlignedCodonFrame> mappings = MappingUtils
1372 .findMappingsForSequence(pep1, dnaMappings);
1373 assertEquals(1, mappings.size());
1374 assertEquals(1, mappings.get(0).getMappings().size());
1375 assertSame(pep1.getDatasetSequence(),
1376 mappings.get(0).getMappings().get(0).getMapping().getTo());
1381 List<AlignedCodonFrame> dnaToCds1Mappings = MappingUtils
1382 .findMappingsForSequence(cds.get(0), dnaMappings);
1383 Mapping mapping = dnaToCds1Mappings.get(0).getMappings().get(0)
1385 assertSame(cds.get(0).getDatasetSequence(), mapping.getTo());
1386 assertEquals("G(1) in CDS should map to G(4) in DNA", 4,
1387 mapping.getMap().getToPosition(1));
1392 mappings = MappingUtils.findMappingsForSequence(pep2, dnaMappings);
1393 assertEquals(1, mappings.size());
1394 assertEquals(1, mappings.get(0).getMappings().size());
1395 assertSame(pep2.getDatasetSequence(),
1396 mappings.get(0).getMappings().get(0).getMapping().getTo());
1401 List<AlignedCodonFrame> dnaToCds2Mappings = MappingUtils
1402 .findMappingsForSequence(cds.get(1), dnaMappings);
1403 mapping = dnaToCds2Mappings.get(0).getMappings().get(0).getMapping();
1404 assertSame(cds.get(1).getDatasetSequence(), mapping.getTo());
1405 assertEquals("c(4) in CDS should map to c(7) in DNA", 7,
1406 mapping.getMap().getToPosition(4));
1411 mappings = MappingUtils.findMappingsForSequence(pep3, dnaMappings);
1412 assertEquals(1, mappings.size());
1413 assertEquals(1, mappings.get(0).getMappings().size());
1414 assertSame(pep3.getDatasetSequence(),
1415 mappings.get(0).getMappings().get(0).getMapping().getTo());
1420 List<AlignedCodonFrame> dnaToCds3Mappings = MappingUtils
1421 .findMappingsForSequence(cds.get(2), dnaMappings);
1422 mapping = dnaToCds3Mappings.get(0).getMappings().get(0).getMapping();
1423 assertSame(cds.get(2).getDatasetSequence(), mapping.getTo());
1424 assertEquals("T(4) in CDS should map to T(10) in DNA", 10,
1425 mapping.getMap().getToPosition(4));
1428 @Test(groups = { "Functional" })
1429 public void testIsMappable()
1431 SequenceI dna1 = new Sequence("dna1", "cgCAGtgGT");
1432 SequenceI aa1 = new Sequence("aa1", "RSG");
1433 AlignmentI al1 = new Alignment(new SequenceI[] { dna1 });
1434 AlignmentI al2 = new Alignment(new SequenceI[] { aa1 });
1436 assertFalse(AlignmentUtils.isMappable(null, null));
1437 assertFalse(AlignmentUtils.isMappable(al1, null));
1438 assertFalse(AlignmentUtils.isMappable(null, al1));
1439 assertFalse(AlignmentUtils.isMappable(al1, al1));
1440 assertFalse(AlignmentUtils.isMappable(al2, al2));
1442 assertTrue(AlignmentUtils.isMappable(al1, al2));
1443 assertTrue(AlignmentUtils.isMappable(al2, al1));
1447 * Test creating a mapping when the sequences involved do not start at residue
1450 * @throws IOException
1452 @Test(groups = { "Functional" })
1453 public void testMapCdnaToProtein_forSubsequence() throws IOException
1455 SequenceI prot = new Sequence("UNIPROT|V12345", "E-I--Q", 10, 12);
1456 prot.createDatasetSequence();
1458 SequenceI dna = new Sequence("EMBL|A33333", "GAA--AT-C-CAG", 40, 48);
1459 dna.createDatasetSequence();
1461 MapList map = AlignmentUtils.mapCdnaToProtein(prot, dna);
1462 assertEquals(10, map.getToLowest());
1463 assertEquals(12, map.getToHighest());
1464 assertEquals(40, map.getFromLowest());
1465 assertEquals(48, map.getFromHighest());
1469 * Test for the alignSequenceAs method where we have protein mapped to protein
1471 @Test(groups = { "Functional" })
1472 public void testAlignSequenceAs_mappedProteinProtein()
1475 SequenceI alignMe = new Sequence("Match", "MGAASEV");
1476 alignMe.createDatasetSequence();
1477 SequenceI alignFrom = new Sequence("Query", "LQTGYMGAASEVMFSPTRR");
1478 alignFrom.createDatasetSequence();
1480 AlignedCodonFrame acf = new AlignedCodonFrame();
1481 // this is like a domain or motif match of part of a peptide sequence
1482 MapList map = new MapList(new int[] { 6, 12 }, new int[] { 1, 7 }, 1,
1484 acf.addMap(alignFrom.getDatasetSequence(), alignMe.getDatasetSequence(),
1487 AlignmentUtils.alignSequenceAs(alignMe, alignFrom, acf, "-", '-', true,
1489 assertEquals("-----MGAASEV-------", alignMe.getSequenceAsString());
1493 * Test for the alignSequenceAs method where there are trailing unmapped
1494 * residues in the model sequence
1496 @Test(groups = { "Functional" })
1497 public void testAlignSequenceAs_withTrailingPeptide()
1499 // map first 3 codons to KPF; G is a trailing unmapped residue
1500 MapList map = new MapList(new int[] { 1, 9 }, new int[] { 1, 3 }, 3, 1);
1502 checkAlignSequenceAs("AAACCCTTT", "K-PFG", true, true, map,
1507 * Tests for transferring features between mapped sequences
1509 @Test(groups = { "Functional" })
1510 public void testTransferFeatures()
1512 SequenceI dna = new Sequence("dna/20-34", "acgTAGcaaGCCcgt");
1513 SequenceI cds = new Sequence("cds/10-15", "TAGGCC");
1516 dna.addSequenceFeature(
1517 new SequenceFeature("type1", "desc1", 1, 2, 1f, null));
1518 // partial overlap - to [1, 1]
1519 dna.addSequenceFeature(
1520 new SequenceFeature("type2", "desc2", 3, 4, 2f, null));
1521 // exact overlap - to [1, 3]
1522 dna.addSequenceFeature(
1523 new SequenceFeature("type3", "desc3", 4, 6, 3f, null));
1524 // spanning overlap - to [2, 5]
1525 dna.addSequenceFeature(
1526 new SequenceFeature("type4", "desc4", 5, 11, 4f, null));
1527 // exactly overlaps whole mapped range [1, 6]
1528 dna.addSequenceFeature(
1529 new SequenceFeature("type5", "desc5", 4, 12, 5f, null));
1530 // no overlap (internal)
1531 dna.addSequenceFeature(
1532 new SequenceFeature("type6", "desc6", 7, 9, 6f, null));
1533 // no overlap (3' end)
1534 dna.addSequenceFeature(
1535 new SequenceFeature("type7", "desc7", 13, 15, 7f, null));
1536 // overlap (3' end) - to [6, 6]
1537 dna.addSequenceFeature(
1538 new SequenceFeature("type8", "desc8", 12, 12, 8f, null));
1539 // extended overlap - to [6, +]
1540 dna.addSequenceFeature(
1541 new SequenceFeature("type9", "desc9", 12, 13, 9f, null));
1543 MapList map = new MapList(new int[] { 4, 6, 10, 12 },
1548 * transferFeatures() will build 'partial overlap' for regions
1549 * that partially overlap 5' or 3' (start or end) of target sequence
1551 AlignmentUtils.transferFeatures(dna, cds, map, null);
1552 List<SequenceFeature> sfs = cds.getSequenceFeatures();
1553 assertEquals(6, sfs.size());
1555 SequenceFeature sf = sfs.get(0);
1556 assertEquals("type2", sf.getType());
1557 assertEquals("desc2", sf.getDescription());
1558 assertEquals(2f, sf.getScore());
1559 assertEquals(1, sf.getBegin());
1560 assertEquals(1, sf.getEnd());
1563 assertEquals("type3", sf.getType());
1564 assertEquals("desc3", sf.getDescription());
1565 assertEquals(3f, sf.getScore());
1566 assertEquals(1, sf.getBegin());
1567 assertEquals(3, sf.getEnd());
1570 assertEquals("type4", sf.getType());
1571 assertEquals(2, sf.getBegin());
1572 assertEquals(5, sf.getEnd());
1575 assertEquals("type5", sf.getType());
1576 assertEquals(1, sf.getBegin());
1577 assertEquals(6, sf.getEnd());
1580 assertEquals("type8", sf.getType());
1581 assertEquals(6, sf.getBegin());
1582 assertEquals(6, sf.getEnd());
1585 assertEquals("type9", sf.getType());
1586 assertEquals(6, sf.getBegin());
1587 assertEquals(6, sf.getEnd());
1591 * Tests for transferring features between mapped sequences
1593 @Test(groups = { "Functional" })
1594 public void testTransferFeatures_withOmit()
1596 SequenceI dna = new Sequence("dna/20-34", "acgTAGcaaGCCcgt");
1597 SequenceI cds = new Sequence("cds/10-15", "TAGGCC");
1599 MapList map = new MapList(new int[] { 4, 6, 10, 12 },
1603 // [5, 11] maps to [2, 5]
1604 dna.addSequenceFeature(
1605 new SequenceFeature("type4", "desc4", 5, 11, 4f, null));
1606 // [4, 12] maps to [1, 6]
1607 dna.addSequenceFeature(
1608 new SequenceFeature("type5", "desc5", 4, 12, 5f, null));
1609 // [12, 12] maps to [6, 6]
1610 dna.addSequenceFeature(
1611 new SequenceFeature("type8", "desc8", 12, 12, 8f, null));
1613 // desc4 and desc8 are the 'omit these' varargs
1614 AlignmentUtils.transferFeatures(dna, cds, map, null, "type4", "type8");
1615 List<SequenceFeature> sfs = cds.getSequenceFeatures();
1616 assertEquals(1, sfs.size());
1618 SequenceFeature sf = sfs.get(0);
1619 assertEquals("type5", sf.getType());
1620 assertEquals(1, sf.getBegin());
1621 assertEquals(6, sf.getEnd());
1625 * Tests for transferring features between mapped sequences
1627 @Test(groups = { "Functional" })
1628 public void testTransferFeatures_withSelect()
1630 SequenceI dna = new Sequence("dna/20-34", "acgTAGcaaGCCcgt");
1631 SequenceI cds = new Sequence("cds/10-15", "TAGGCC");
1633 MapList map = new MapList(new int[] { 4, 6, 10, 12 },
1637 // [5, 11] maps to [2, 5]
1638 dna.addSequenceFeature(
1639 new SequenceFeature("type4", "desc4", 5, 11, 4f, null));
1640 // [4, 12] maps to [1, 6]
1641 dna.addSequenceFeature(
1642 new SequenceFeature("type5", "desc5", 4, 12, 5f, null));
1643 // [12, 12] maps to [6, 6]
1644 dna.addSequenceFeature(
1645 new SequenceFeature("type8", "desc8", 12, 12, 8f, null));
1647 // "type5" is the 'select this type' argument
1648 AlignmentUtils.transferFeatures(dna, cds, map, "type5");
1649 List<SequenceFeature> sfs = cds.getSequenceFeatures();
1650 assertEquals(1, sfs.size());
1652 SequenceFeature sf = sfs.get(0);
1653 assertEquals("type5", sf.getType());
1654 assertEquals(1, sf.getBegin());
1655 assertEquals(6, sf.getEnd());
1659 * Test the method that extracts the cds-only part of a dna alignment, for the
1660 * case where the cds should be aligned to match its nucleotide sequence.
1662 @Test(groups = { "Functional" })
1663 public void testMakeCdsAlignment_alternativeTranscripts()
1665 SequenceI dna1 = new Sequence("dna1", "aaaGGGCC-----CTTTaaaGGG");
1666 // alternative transcript of same dna skips CCC codon
1667 SequenceI dna2 = new Sequence("dna2", "aaaGGGCC-----cttTaaaGGG");
1668 // dna3 has no mapping (protein product) so should be ignored here
1669 SequenceI dna3 = new Sequence("dna3", "aaaGGGCCCCCGGGcttTaaaGGG");
1670 SequenceI pep1 = new Sequence("pep1", "GPFG");
1671 SequenceI pep2 = new Sequence("pep2", "GPG");
1672 dna1.createDatasetSequence();
1673 dna2.createDatasetSequence();
1674 dna3.createDatasetSequence();
1675 pep1.createDatasetSequence();
1676 pep2.createDatasetSequence();
1678 AlignmentI dna = new Alignment(new SequenceI[] { dna1, dna2, dna3 });
1679 dna.setDataset(null);
1681 MapList map = new MapList(new int[] { 4, 12, 16, 18 },
1684 AlignedCodonFrame acf = new AlignedCodonFrame();
1685 acf.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(), map);
1686 dna.addCodonFrame(acf);
1687 map = new MapList(new int[] { 4, 8, 12, 12, 16, 18 },
1690 acf = new AlignedCodonFrame();
1691 acf.addMap(dna2.getDatasetSequence(), pep2.getDatasetSequence(), map);
1692 dna.addCodonFrame(acf);
1694 AlignmentI cds = AlignmentUtils
1695 .makeCdsAlignment(new SequenceI[]
1696 { dna1, dna2, dna3 }, dna.getDataset(), null);
1697 List<SequenceI> cdsSeqs = cds.getSequences();
1698 assertEquals(2, cdsSeqs.size());
1699 assertEquals("GGGCCCTTTGGG", cdsSeqs.get(0).getSequenceAsString());
1700 assertEquals("GGGCCTGGG", cdsSeqs.get(1).getSequenceAsString());
1703 * verify shared, extended alignment dataset
1705 assertSame(dna.getDataset(), cds.getDataset());
1706 assertTrue(dna.getDataset().getSequences()
1707 .contains(cdsSeqs.get(0).getDatasetSequence()));
1708 assertTrue(dna.getDataset().getSequences()
1709 .contains(cdsSeqs.get(1).getDatasetSequence()));
1712 * Verify 6 mappings: dna1 to cds1, cds1 to pep1, dna1 to pep1
1713 * and the same for dna2/cds2/pep2
1715 List<AlignedCodonFrame> mappings = cds.getCodonFrames();
1716 assertEquals(6, mappings.size());
1719 * 2 mappings involve pep1
1721 List<AlignedCodonFrame> pep1Mappings = MappingUtils
1722 .findMappingsForSequence(pep1, mappings);
1723 assertEquals(2, pep1Mappings.size());
1726 * Get mapping of pep1 to cds1 and verify it
1727 * maps GPFG to 1-3,4-6,7-9,10-12
1729 List<AlignedCodonFrame> pep1CdsMappings = MappingUtils
1730 .findMappingsForSequence(cds.getSequenceAt(0), pep1Mappings);
1731 assertEquals(1, pep1CdsMappings.size());
1732 SearchResultsI sr = MappingUtils.buildSearchResults(pep1, 1,
1734 assertEquals(1, sr.getResults().size());
1735 SearchResultMatchI m = sr.getResults().get(0);
1736 assertEquals(cds.getSequenceAt(0).getDatasetSequence(),
1738 assertEquals(1, m.getStart());
1739 assertEquals(3, m.getEnd());
1740 sr = MappingUtils.buildSearchResults(pep1, 2, pep1CdsMappings);
1741 m = sr.getResults().get(0);
1742 assertEquals(4, m.getStart());
1743 assertEquals(6, m.getEnd());
1744 sr = MappingUtils.buildSearchResults(pep1, 3, pep1CdsMappings);
1745 m = sr.getResults().get(0);
1746 assertEquals(7, m.getStart());
1747 assertEquals(9, m.getEnd());
1748 sr = MappingUtils.buildSearchResults(pep1, 4, pep1CdsMappings);
1749 m = sr.getResults().get(0);
1750 assertEquals(10, m.getStart());
1751 assertEquals(12, m.getEnd());
1754 * Get mapping of pep2 to cds2 and verify it
1755 * maps GPG in pep2 to 1-3,4-6,7-9 in second CDS sequence
1757 List<AlignedCodonFrame> pep2Mappings = MappingUtils
1758 .findMappingsForSequence(pep2, mappings);
1759 assertEquals(2, pep2Mappings.size());
1760 List<AlignedCodonFrame> pep2CdsMappings = MappingUtils
1761 .findMappingsForSequence(cds.getSequenceAt(1), pep2Mappings);
1762 assertEquals(1, pep2CdsMappings.size());
1763 sr = MappingUtils.buildSearchResults(pep2, 1, pep2CdsMappings);
1764 assertEquals(1, sr.getResults().size());
1765 m = sr.getResults().get(0);
1766 assertEquals(cds.getSequenceAt(1).getDatasetSequence(),
1768 assertEquals(1, m.getStart());
1769 assertEquals(3, m.getEnd());
1770 sr = MappingUtils.buildSearchResults(pep2, 2, pep2CdsMappings);
1771 m = sr.getResults().get(0);
1772 assertEquals(4, m.getStart());
1773 assertEquals(6, m.getEnd());
1774 sr = MappingUtils.buildSearchResults(pep2, 3, pep2CdsMappings);
1775 m = sr.getResults().get(0);
1776 assertEquals(7, m.getStart());
1777 assertEquals(9, m.getEnd());
1781 * Test the method that realigns protein to match mapped codon alignment.
1783 @Test(groups = { "Functional" })
1784 public void testAlignProteinAsDna_incompleteStartCodon()
1786 // seq1: incomplete start codon (not mapped), then [3, 11]
1787 SequenceI dna1 = new Sequence("Seq1", "ccAAA-TTT-GGG-");
1788 // seq2 codons are [4, 5], [8, 11]
1789 SequenceI dna2 = new Sequence("Seq2", "ccaAA-ttT-GGG-");
1790 // seq3 incomplete start codon at 'tt'
1791 SequenceI dna3 = new Sequence("Seq3", "ccaaa-ttt-GGG-");
1792 AlignmentI dna = new Alignment(new SequenceI[] { dna1, dna2, dna3 });
1793 dna.setDataset(null);
1795 // prot1 has 'X' for incomplete start codon (not mapped)
1796 SequenceI prot1 = new Sequence("Seq1", "XKFG"); // X for incomplete start
1797 SequenceI prot2 = new Sequence("Seq2", "NG");
1798 SequenceI prot3 = new Sequence("Seq3", "XG"); // X for incomplete start
1799 AlignmentI protein = new Alignment(
1801 { prot1, prot2, prot3 });
1802 protein.setDataset(null);
1804 // map dna1 [3, 11] to prot1 [2, 4] KFG
1805 MapList map = new MapList(new int[] { 3, 11 }, new int[] { 2, 4 }, 3,
1807 AlignedCodonFrame acf = new AlignedCodonFrame();
1808 acf.addMap(dna1.getDatasetSequence(), prot1.getDatasetSequence(), map);
1810 // map dna2 [4, 5] [8, 11] to prot2 [1, 2] NG
1811 map = new MapList(new int[] { 4, 5, 8, 11 }, new int[] { 1, 2 }, 3, 1);
1812 acf.addMap(dna2.getDatasetSequence(), prot2.getDatasetSequence(), map);
1814 // map dna3 [9, 11] to prot3 [2, 2] G
1815 map = new MapList(new int[] { 9, 11 }, new int[] { 2, 2 }, 3, 1);
1816 acf.addMap(dna3.getDatasetSequence(), prot3.getDatasetSequence(), map);
1818 ArrayList<AlignedCodonFrame> acfs = new ArrayList<>();
1820 protein.setCodonFrames(acfs);
1823 * verify X is included in the aligned proteins, and placed just
1824 * before the first mapped residue
1825 * CCT is between CCC and TTT
1827 AlignmentUtils.alignProteinAsDna(protein, dna);
1828 assertEquals("XK-FG", prot1.getSequenceAsString());
1829 assertEquals("--N-G", prot2.getSequenceAsString());
1830 assertEquals("---XG", prot3.getSequenceAsString());
1834 * Tests for the method that maps the subset of a dna sequence that has CDS
1835 * (or subtype) feature - case where the start codon is incomplete.
1837 @Test(groups = "Functional")
1838 public void testFindCdsPositions_fivePrimeIncomplete()
1840 SequenceI dnaSeq = new Sequence("dna", "aaagGGCCCaaaTTTttt");
1841 dnaSeq.createDatasetSequence();
1842 SequenceI ds = dnaSeq.getDatasetSequence();
1844 // CDS for dna 5-6 (incomplete codon), 7-9
1845 SequenceFeature sf = new SequenceFeature("CDS", "", 5, 9, 0f, null);
1846 sf.setPhase("2"); // skip 2 bases to start of next codon
1847 ds.addSequenceFeature(sf);
1848 // CDS for dna 13-15
1849 sf = new SequenceFeature("CDS_predicted", "", 13, 15, 0f, null);
1850 ds.addSequenceFeature(sf);
1852 List<int[]> ranges = AlignmentUtils.findCdsPositions(dnaSeq);
1855 * check the mapping starts with the first complete codon
1857 assertEquals(6, MappingUtils.getLength(ranges));
1858 assertEquals(2, ranges.size());
1859 assertEquals(7, ranges.get(0)[0]);
1860 assertEquals(9, ranges.get(0)[1]);
1861 assertEquals(13, ranges.get(1)[0]);
1862 assertEquals(15, ranges.get(1)[1]);
1866 * Tests for the method that maps the subset of a dna sequence that has CDS
1867 * (or subtype) feature.
1869 @Test(groups = "Functional")
1870 public void testFindCdsPositions()
1872 SequenceI dnaSeq = new Sequence("dna", "aaaGGGcccAAATTTttt");
1873 dnaSeq.createDatasetSequence();
1874 SequenceI ds = dnaSeq.getDatasetSequence();
1876 // CDS for dna 10-12
1877 SequenceFeature sf = new SequenceFeature("CDS_predicted", "", 10, 12,
1880 ds.addSequenceFeature(sf);
1882 sf = new SequenceFeature("CDS", "", 4, 6, 0f, null);
1884 ds.addSequenceFeature(sf);
1885 // exon feature should be ignored here
1886 sf = new SequenceFeature("exon", "", 7, 9, 0f, null);
1887 ds.addSequenceFeature(sf);
1889 List<int[]> ranges = AlignmentUtils.findCdsPositions(dnaSeq);
1891 * verify ranges { [4-6], [12-10] }
1892 * note CDS ranges are ordered ascending even if the CDS
1895 assertEquals(6, MappingUtils.getLength(ranges));
1896 assertEquals(2, ranges.size());
1897 assertEquals(4, ranges.get(0)[0]);
1898 assertEquals(6, ranges.get(0)[1]);
1899 assertEquals(10, ranges.get(1)[0]);
1900 assertEquals(12, ranges.get(1)[1]);
1904 * Tests for the method that maps the subset of a dna sequence that has CDS
1905 * (or subtype) feature, with CDS strand = '-' (reverse)
1907 // test turned off as currently findCdsPositions is not strand-dependent
1908 // left in case it comes around again...
1909 @Test(groups = "Functional", enabled = false)
1910 public void testFindCdsPositions_reverseStrand()
1912 SequenceI dnaSeq = new Sequence("dna", "aaaGGGcccAAATTTttt");
1913 dnaSeq.createDatasetSequence();
1914 SequenceI ds = dnaSeq.getDatasetSequence();
1917 SequenceFeature sf = new SequenceFeature("CDS", "", 4, 6, 0f, null);
1919 ds.addSequenceFeature(sf);
1920 // exon feature should be ignored here
1921 sf = new SequenceFeature("exon", "", 7, 9, 0f, null);
1922 ds.addSequenceFeature(sf);
1923 // CDS for dna 10-12
1924 sf = new SequenceFeature("CDS_predicted", "", 10, 12, 0f, null);
1926 ds.addSequenceFeature(sf);
1928 List<int[]> ranges = AlignmentUtils.findCdsPositions(dnaSeq);
1930 * verify ranges { [12-10], [6-4] }
1932 assertEquals(6, MappingUtils.getLength(ranges));
1933 assertEquals(2, ranges.size());
1934 assertEquals(12, ranges.get(0)[0]);
1935 assertEquals(10, ranges.get(0)[1]);
1936 assertEquals(6, ranges.get(1)[0]);
1937 assertEquals(4, ranges.get(1)[1]);
1941 * Tests for the method that maps the subset of a dna sequence that has CDS
1942 * (or subtype) feature - reverse strand case where the start codon is
1945 @Test(groups = "Functional", enabled = false)
1946 // test turned off as currently findCdsPositions is not strand-dependent
1947 // left in case it comes around again...
1948 public void testFindCdsPositions_reverseStrandThreePrimeIncomplete()
1950 SequenceI dnaSeq = new Sequence("dna", "aaagGGCCCaaaTTTttt");
1951 dnaSeq.createDatasetSequence();
1952 SequenceI ds = dnaSeq.getDatasetSequence();
1955 SequenceFeature sf = new SequenceFeature("CDS", "", 5, 9, 0f, null);
1957 ds.addSequenceFeature(sf);
1958 // CDS for dna 13-15
1959 sf = new SequenceFeature("CDS_predicted", "", 13, 15, 0f, null);
1961 sf.setPhase("2"); // skip 2 bases to start of next codon
1962 ds.addSequenceFeature(sf);
1964 List<int[]> ranges = AlignmentUtils.findCdsPositions(dnaSeq);
1967 * check the mapping starts with the first complete codon
1968 * expect ranges [13, 13], [9, 5]
1970 assertEquals(6, MappingUtils.getLength(ranges));
1971 assertEquals(2, ranges.size());
1972 assertEquals(13, ranges.get(0)[0]);
1973 assertEquals(13, ranges.get(0)[1]);
1974 assertEquals(9, ranges.get(1)[0]);
1975 assertEquals(5, ranges.get(1)[1]);
1978 @Test(groups = "Functional")
1979 public void testAlignAs_alternateTranscriptsUngapped()
1981 SequenceI dna1 = new Sequence("dna1", "cccGGGTTTaaa");
1982 SequenceI dna2 = new Sequence("dna2", "CCCgggtttAAA");
1983 AlignmentI dna = new Alignment(new SequenceI[] { dna1, dna2 });
1984 ((Alignment) dna).createDatasetAlignment();
1985 SequenceI cds1 = new Sequence("cds1", "GGGTTT");
1986 SequenceI cds2 = new Sequence("cds2", "CCCAAA");
1987 AlignmentI cds = new Alignment(new SequenceI[] { cds1, cds2 });
1988 ((Alignment) cds).createDatasetAlignment();
1990 AlignedCodonFrame acf = new AlignedCodonFrame();
1991 MapList map = new MapList(new int[] { 4, 9 }, new int[] { 1, 6 }, 1, 1);
1992 acf.addMap(dna1.getDatasetSequence(), cds1.getDatasetSequence(), map);
1993 map = new MapList(new int[] { 1, 3, 10, 12 }, new int[] { 1, 6 }, 1, 1);
1994 acf.addMap(dna2.getDatasetSequence(), cds2.getDatasetSequence(), map);
1997 * verify CDS alignment is as:
1998 * cccGGGTTTaaa (cdna)
1999 * CCCgggtttAAA (cdna)
2001 * ---GGGTTT--- (cds)
2002 * CCC------AAA (cds)
2004 dna.addCodonFrame(acf);
2005 AlignmentUtils.alignAs(cds, dna);
2006 assertEquals("---GGGTTT", cds.getSequenceAt(0).getSequenceAsString());
2007 assertEquals("CCC------AAA",
2008 cds.getSequenceAt(1).getSequenceAsString());
2011 @Test(groups = { "Functional" })
2012 public void testAddMappedPositions()
2014 SequenceI from = new Sequence("dna", "ggAA-ATcc-TT-g");
2015 SequenceI seq1 = new Sequence("cds", "AAATTT");
2016 from.createDatasetSequence();
2017 seq1.createDatasetSequence();
2018 Mapping mapping = new Mapping(seq1,
2019 new MapList(new int[]
2020 { 3, 6, 9, 10 }, new int[] { 1, 6 }, 1, 1));
2021 Map<Integer, Map<SequenceI, Character>> map = new TreeMap<>();
2022 AlignmentUtils.addMappedPositions(seq1, from, mapping, map);
2025 * verify map has seq1 residues in columns 3,4,6,7,11,12
2027 assertEquals(6, map.size());
2028 assertEquals('A', map.get(3).get(seq1).charValue());
2029 assertEquals('A', map.get(4).get(seq1).charValue());
2030 assertEquals('A', map.get(6).get(seq1).charValue());
2031 assertEquals('T', map.get(7).get(seq1).charValue());
2032 assertEquals('T', map.get(11).get(seq1).charValue());
2033 assertEquals('T', map.get(12).get(seq1).charValue());
2041 * Test case where the mapping 'from' range includes a stop codon which is
2042 * absent in the 'to' range
2044 @Test(groups = { "Functional" })
2045 public void testAddMappedPositions_withStopCodon()
2047 SequenceI from = new Sequence("dna", "ggAA-ATcc-TT-g");
2048 SequenceI seq1 = new Sequence("cds", "AAATTT");
2049 from.createDatasetSequence();
2050 seq1.createDatasetSequence();
2051 Mapping mapping = new Mapping(seq1,
2052 new MapList(new int[]
2053 { 3, 6, 9, 10 }, new int[] { 1, 6 }, 1, 1));
2054 Map<Integer, Map<SequenceI, Character>> map = new TreeMap<>();
2055 AlignmentUtils.addMappedPositions(seq1, from, mapping, map);
2058 * verify map has seq1 residues in columns 3,4,6,7,11,12
2060 assertEquals(6, map.size());
2061 assertEquals('A', map.get(3).get(seq1).charValue());
2062 assertEquals('A', map.get(4).get(seq1).charValue());
2063 assertEquals('A', map.get(6).get(seq1).charValue());
2064 assertEquals('T', map.get(7).get(seq1).charValue());
2065 assertEquals('T', map.get(11).get(seq1).charValue());
2066 assertEquals('T', map.get(12).get(seq1).charValue());
2070 * Test for the case where the products for which we want CDS are specified.
2071 * This is to represent the case where EMBL has CDS mappings to both Uniprot
2072 * and EMBLCDSPROTEIN. makeCdsAlignment() should only return the mappings for
2073 * the protein sequences specified.
2075 @Test(groups = { "Functional" })
2076 public void testMakeCdsAlignment_filterProducts()
2078 SequenceI dna1 = new Sequence("dna1", "aaaGGGcccTTTaaa");
2079 SequenceI dna2 = new Sequence("dna2", "GGGcccTTTaaaCCC");
2080 SequenceI pep1 = new Sequence("Uniprot|pep1", "GF");
2081 SequenceI pep2 = new Sequence("Uniprot|pep2", "GFP");
2082 SequenceI pep3 = new Sequence("EMBL|pep3", "GF");
2083 SequenceI pep4 = new Sequence("EMBL|pep4", "GFP");
2084 dna1.createDatasetSequence();
2085 dna2.createDatasetSequence();
2086 pep1.createDatasetSequence();
2087 pep2.createDatasetSequence();
2088 pep3.createDatasetSequence();
2089 pep4.createDatasetSequence();
2090 AlignmentI dna = new Alignment(new SequenceI[] { dna1, dna2 });
2091 dna.setDataset(null);
2092 AlignmentI emblPeptides = new Alignment(new SequenceI[] { pep3, pep4 });
2093 emblPeptides.setDataset(null);
2095 AlignedCodonFrame acf = new AlignedCodonFrame();
2096 MapList map = new MapList(new int[] { 4, 6, 10, 12 },
2099 acf.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(), map);
2100 acf.addMap(dna1.getDatasetSequence(), pep3.getDatasetSequence(), map);
2101 dna.addCodonFrame(acf);
2103 acf = new AlignedCodonFrame();
2104 map = new MapList(new int[] { 1, 3, 7, 9, 13, 15 }, new int[] { 1, 3 },
2106 acf.addMap(dna2.getDatasetSequence(), pep2.getDatasetSequence(), map);
2107 acf.addMap(dna2.getDatasetSequence(), pep4.getDatasetSequence(), map);
2108 dna.addCodonFrame(acf);
2111 * execute method under test to find CDS for EMBL peptides only
2113 AlignmentI cds = AlignmentUtils
2114 .makeCdsAlignment(new SequenceI[]
2115 { dna1, dna2 }, dna.getDataset(),
2116 emblPeptides.getSequencesArray());
2118 assertEquals(2, cds.getSequences().size());
2119 assertEquals("GGGTTT", cds.getSequenceAt(0).getSequenceAsString());
2120 assertEquals("GGGTTTCCC", cds.getSequenceAt(1).getSequenceAsString());
2123 * verify shared, extended alignment dataset
2125 assertSame(dna.getDataset(), cds.getDataset());
2126 assertTrue(dna.getDataset().getSequences()
2127 .contains(cds.getSequenceAt(0).getDatasetSequence()));
2128 assertTrue(dna.getDataset().getSequences()
2129 .contains(cds.getSequenceAt(1).getDatasetSequence()));
2132 * Verify mappings from CDS to peptide, cDNA to CDS, and cDNA to peptide
2133 * the mappings are on the shared alignment dataset
2135 List<AlignedCodonFrame> cdsMappings = cds.getDataset().getCodonFrames();
2137 * 6 mappings, 2*(DNA->CDS), 2*(DNA->Pep), 2*(CDS->Pep)
2139 assertEquals(6, cdsMappings.size());
2142 * verify that mapping sets for dna and cds alignments are different
2143 * [not current behaviour - all mappings are on the alignment dataset]
2145 // select -> subselect type to test.
2146 // Assert.assertNotSame(dna.getCodonFrames(), cds.getCodonFrames());
2147 // assertEquals(4, dna.getCodonFrames().size());
2148 // assertEquals(4, cds.getCodonFrames().size());
2151 * Two mappings involve pep3 (dna to pep3, cds to pep3)
2152 * Mapping from pep3 to GGGTTT in first new exon sequence
2154 List<AlignedCodonFrame> pep3Mappings = MappingUtils
2155 .findMappingsForSequence(pep3, cdsMappings);
2156 assertEquals(2, pep3Mappings.size());
2157 List<AlignedCodonFrame> mappings = MappingUtils
2158 .findMappingsForSequence(cds.getSequenceAt(0), pep3Mappings);
2159 assertEquals(1, mappings.size());
2162 SearchResultsI sr = MappingUtils.buildSearchResults(pep3, 1, mappings);
2163 assertEquals(1, sr.getResults().size());
2164 SearchResultMatchI m = sr.getResults().get(0);
2165 assertSame(cds.getSequenceAt(0).getDatasetSequence(), m.getSequence());
2166 assertEquals(1, m.getStart());
2167 assertEquals(3, m.getEnd());
2169 sr = MappingUtils.buildSearchResults(pep3, 2, mappings);
2170 m = sr.getResults().get(0);
2171 assertSame(cds.getSequenceAt(0).getDatasetSequence(), m.getSequence());
2172 assertEquals(4, m.getStart());
2173 assertEquals(6, m.getEnd());
2176 * Two mappings involve pep4 (dna to pep4, cds to pep4)
2177 * Verify mapping from pep4 to GGGTTTCCC in second new exon sequence
2179 List<AlignedCodonFrame> pep4Mappings = MappingUtils
2180 .findMappingsForSequence(pep4, cdsMappings);
2181 assertEquals(2, pep4Mappings.size());
2182 mappings = MappingUtils.findMappingsForSequence(cds.getSequenceAt(1),
2184 assertEquals(1, mappings.size());
2186 sr = MappingUtils.buildSearchResults(pep4, 1, mappings);
2187 assertEquals(1, sr.getResults().size());
2188 m = sr.getResults().get(0);
2189 assertSame(cds.getSequenceAt(1).getDatasetSequence(), m.getSequence());
2190 assertEquals(1, m.getStart());
2191 assertEquals(3, m.getEnd());
2193 sr = MappingUtils.buildSearchResults(pep4, 2, mappings);
2194 m = sr.getResults().get(0);
2195 assertSame(cds.getSequenceAt(1).getDatasetSequence(), m.getSequence());
2196 assertEquals(4, m.getStart());
2197 assertEquals(6, m.getEnd());
2199 sr = MappingUtils.buildSearchResults(pep4, 3, mappings);
2200 m = sr.getResults().get(0);
2201 assertSame(cds.getSequenceAt(1).getDatasetSequence(), m.getSequence());
2202 assertEquals(7, m.getStart());
2203 assertEquals(9, m.getEnd());
2207 * Test the method that just copies aligned sequences, provided all sequences
2208 * to be aligned share the aligned sequence's dataset
2210 @Test(groups = "Functional")
2211 public void testAlignAsSameSequences()
2213 SequenceI dna1 = new Sequence("dna1", "cccGGGTTTaaa");
2214 SequenceI dna2 = new Sequence("dna2", "CCCgggtttAAA");
2215 AlignmentI al1 = new Alignment(new SequenceI[] { dna1, dna2 });
2216 ((Alignment) al1).createDatasetAlignment();
2218 SequenceI dna3 = new Sequence(dna1);
2219 SequenceI dna4 = new Sequence(dna2);
2220 assertSame(dna3.getDatasetSequence(), dna1.getDatasetSequence());
2221 assertSame(dna4.getDatasetSequence(), dna2.getDatasetSequence());
2222 String seq1 = "-cc-GG-GT-TT--aaa";
2223 dna3.setSequence(seq1);
2224 String seq2 = "C--C-Cgg--gtt-tAA-A-";
2225 dna4.setSequence(seq2);
2226 AlignmentI al2 = new Alignment(new SequenceI[] { dna3, dna4 });
2227 ((Alignment) al2).createDatasetAlignment();
2230 * alignment removes gapped columns (two internal, two trailing)
2232 assertTrue(AlignmentUtils.alignAsSameSequences(al1, al2));
2233 String aligned1 = "-cc-GG-GTTT-aaa";
2234 assertEquals(aligned1, al1.getSequenceAt(0).getSequenceAsString());
2235 String aligned2 = "C--C-Cgg-gtttAAA";
2236 assertEquals(aligned2, al1.getSequenceAt(1).getSequenceAsString());
2239 * add another sequence to 'aligned' - should still succeed, since
2240 * unaligned sequences still share a dataset with aligned sequences
2242 SequenceI dna5 = new Sequence("dna5", "CCCgggtttAAA");
2243 dna5.createDatasetSequence();
2244 al2.addSequence(dna5);
2245 assertTrue(AlignmentUtils.alignAsSameSequences(al1, al2));
2246 assertEquals(aligned1, al1.getSequenceAt(0).getSequenceAsString());
2247 assertEquals(aligned2, al1.getSequenceAt(1).getSequenceAsString());
2250 * add another sequence to 'unaligned' - should fail, since now not
2251 * all unaligned sequences share a dataset with aligned sequences
2253 SequenceI dna6 = new Sequence("dna6", "CCCgggtttAAA");
2254 dna6.createDatasetSequence();
2255 al1.addSequence(dna6);
2256 // JAL-2110 JBP Comment: what's the use case for this behaviour ?
2257 assertFalse(AlignmentUtils.alignAsSameSequences(al1, al2));
2260 @Test(groups = "Functional")
2261 public void testAlignAsSameSequencesMultipleSubSeq()
2263 SequenceI dna1 = new Sequence("dna1", "cccGGGTTTaaa");
2264 SequenceI dna2 = new Sequence("dna2", "CCCgggtttAAA");
2265 SequenceI as1 = dna1.deriveSequence(); // cccGGGTTTaaa/1-12
2266 SequenceI as2 = dna1.deriveSequence().getSubSequence(3, 7); // GGGT/4-7
2267 SequenceI as3 = dna2.deriveSequence(); // CCCgggtttAAA/1-12
2268 as1.insertCharAt(6, 5, '-');
2269 assertEquals("cccGGG-----TTTaaa", as1.getSequenceAsString());
2270 as2.insertCharAt(6, 5, '-');
2271 assertEquals("GGGT-----", as2.getSequenceAsString());
2272 as3.insertCharAt(3, 5, '-');
2273 assertEquals("CCC-----gggtttAAA", as3.getSequenceAsString());
2274 AlignmentI aligned = new Alignment(new SequenceI[] { as1, as2, as3 });
2276 // why do we need to cast this still ?
2277 ((Alignment) aligned).createDatasetAlignment();
2278 SequenceI uas1 = dna1.deriveSequence();
2279 SequenceI uas2 = dna1.deriveSequence().getSubSequence(3, 7);
2280 SequenceI uas3 = dna2.deriveSequence();
2281 AlignmentI tobealigned = new Alignment(
2283 { uas1, uas2, uas3 });
2284 ((Alignment) tobealigned).createDatasetAlignment();
2287 * alignAs lines up dataset sequences and removes empty columns (two)
2289 assertTrue(AlignmentUtils.alignAsSameSequences(tobealigned, aligned));
2290 assertEquals("cccGGG---TTTaaa", uas1.getSequenceAsString());
2291 assertEquals("GGGT", uas2.getSequenceAsString());
2292 assertEquals("CCC---gggtttAAA", uas3.getSequenceAsString());
2295 @Test(groups = { "Functional" })
2296 public void testTransferGeneLoci()
2298 SequenceI from = new Sequence("transcript",
2299 "aaacccgggTTTAAACCCGGGtttaaacccgggttt");
2300 SequenceI to = new Sequence("CDS", "TTTAAACCCGGG");
2301 MapList map = new MapList(new int[] { 1, 12 }, new int[] { 10, 21 }, 1,
2305 * first with nothing to transfer
2307 AlignmentUtils.transferGeneLoci(from, map, to);
2308 assertNull(to.getGeneLoci());
2311 * next with gene loci set on 'from' sequence
2313 int[] exons = new int[] { 100, 105, 155, 164, 210, 229 };
2314 MapList geneMap = new MapList(new int[] { 1, 36 }, exons, 1, 1);
2315 from.setGeneLoci("human", "GRCh38", "7", geneMap);
2316 AlignmentUtils.transferGeneLoci(from, map, to);
2318 GeneLociI toLoci = to.getGeneLoci();
2319 assertNotNull(toLoci);
2320 // DBRefEntry constructor upper-cases 'source'
2321 assertEquals("HUMAN", toLoci.getSpeciesId());
2322 assertEquals("GRCh38", toLoci.getAssemblyId());
2323 assertEquals("7", toLoci.getChromosomeId());
2326 * transcript 'exons' are 1-6, 7-16, 17-36
2327 * CDS 1:12 is transcript 10-21
2328 * transcript 'CDS' is 10-16, 17-21
2329 * which is 'gene' 158-164, 210-214
2331 MapList toMap = toLoci.getMapping();
2332 assertEquals(1, toMap.getFromRanges().size());
2333 assertEquals(2, toMap.getFromRanges().get(0).length);
2334 assertEquals(1, toMap.getFromRanges().get(0)[0]);
2335 assertEquals(12, toMap.getFromRanges().get(0)[1]);
2336 assertEquals(2, toMap.getToRanges().size());
2337 assertEquals(2, toMap.getToRanges().get(0).length);
2338 assertEquals(158, toMap.getToRanges().get(0)[0]);
2339 assertEquals(164, toMap.getToRanges().get(0)[1]);
2340 assertEquals(210, toMap.getToRanges().get(1)[0]);
2341 assertEquals(214, toMap.getToRanges().get(1)[1]);
2342 // or summarised as (but toString might change in future):
2343 assertEquals("[ [1, 12] ] 1:1 to [ [158, 164] [210, 214] ]",
2347 * an existing value is not overridden
2349 geneMap = new MapList(new int[] { 1, 36 }, new int[] { 36, 1 }, 1, 1);
2350 from.setGeneLoci("inhuman", "GRCh37", "6", geneMap);
2351 AlignmentUtils.transferGeneLoci(from, map, to);
2352 assertEquals("GRCh38", toLoci.getAssemblyId());
2353 assertEquals("7", toLoci.getChromosomeId());
2354 toMap = toLoci.getMapping();
2355 assertEquals("[ [1, 12] ] 1:1 to [ [158, 164] [210, 214] ]",
2360 * Tests for the method that maps nucleotide to protein based on CDS features
2362 @Test(groups = "Functional")
2363 public void testMapCdsToProtein()
2365 SequenceI peptide = new Sequence("pep", "KLQ");
2368 * Case 1: CDS 3 times length of peptide
2369 * NB method only checks lengths match, not translation
2371 SequenceI dna = new Sequence("dna", "AACGacgtCTCCT");
2372 dna.createDatasetSequence();
2373 dna.addSequenceFeature(new SequenceFeature("CDS", "", 1, 4, null));
2374 dna.addSequenceFeature(new SequenceFeature("CDS", "", 9, 13, null));
2375 MapList ml = AlignmentUtils.mapCdsToProtein(dna, peptide);
2376 assertEquals(3, ml.getFromRatio());
2377 assertEquals(1, ml.getToRatio());
2378 assertEquals("[[1, 3]]",
2379 Arrays.deepToString(ml.getToRanges().toArray()));
2380 assertEquals("[[1, 4], [9, 13]]",
2381 Arrays.deepToString(ml.getFromRanges().toArray()));
2384 * Case 2: CDS 3 times length of peptide + stop codon
2385 * (note code does not currently check trailing codon is a stop codon)
2387 dna = new Sequence("dna", "AACGacgtCTCCTCCC");
2388 dna.createDatasetSequence();
2389 dna.addSequenceFeature(new SequenceFeature("CDS", "", 1, 4, null));
2390 dna.addSequenceFeature(new SequenceFeature("CDS", "", 9, 16, null));
2391 ml = AlignmentUtils.mapCdsToProtein(dna, peptide);
2392 assertEquals(3, ml.getFromRatio());
2393 assertEquals(1, ml.getToRatio());
2394 assertEquals("[[1, 3]]",
2395 Arrays.deepToString(ml.getToRanges().toArray()));
2396 assertEquals("[[1, 4], [9, 13]]",
2397 Arrays.deepToString(ml.getFromRanges().toArray()));
2400 * Case 3: CDS longer than 3 * peptide + stop codon - no mapping is made
2402 dna = new Sequence("dna", "AACGacgtCTCCTTGATCA");
2403 dna.createDatasetSequence();
2404 dna.addSequenceFeature(new SequenceFeature("CDS", "", 1, 4, null));
2405 dna.addSequenceFeature(new SequenceFeature("CDS", "", 9, 19, null));
2406 ml = AlignmentUtils.mapCdsToProtein(dna, peptide);
2410 * Case 4: CDS shorter than 3 * peptide - no mapping is made
2412 dna = new Sequence("dna", "AACGacgtCTCC");
2413 dna.createDatasetSequence();
2414 dna.addSequenceFeature(new SequenceFeature("CDS", "", 1, 4, null));
2415 dna.addSequenceFeature(new SequenceFeature("CDS", "", 9, 12, null));
2416 ml = AlignmentUtils.mapCdsToProtein(dna, peptide);
2420 * Case 5: CDS 3 times length of peptide + part codon - mapping is truncated
2422 dna = new Sequence("dna", "AACGacgtCTCCTTG");
2423 dna.createDatasetSequence();
2424 dna.addSequenceFeature(new SequenceFeature("CDS", "", 1, 4, null));
2425 dna.addSequenceFeature(new SequenceFeature("CDS", "", 9, 15, null));
2426 ml = AlignmentUtils.mapCdsToProtein(dna, peptide);
2427 assertEquals(3, ml.getFromRatio());
2428 assertEquals(1, ml.getToRatio());
2429 assertEquals("[[1, 3]]",
2430 Arrays.deepToString(ml.getToRanges().toArray()));
2431 assertEquals("[[1, 4], [9, 13]]",
2432 Arrays.deepToString(ml.getFromRanges().toArray()));
2435 * Case 6: incomplete start codon corresponding to X in peptide
2437 dna = new Sequence("dna", "ACGacgtCTCCTTGG");
2438 dna.createDatasetSequence();
2439 SequenceFeature sf = new SequenceFeature("CDS", "", 1, 3, null);
2440 sf.setPhase("2"); // skip 2 positions (AC) to start of next codon (GCT)
2441 dna.addSequenceFeature(sf);
2442 dna.addSequenceFeature(new SequenceFeature("CDS", "", 8, 15, null));
2443 peptide = new Sequence("pep", "XLQ");
2444 ml = AlignmentUtils.mapCdsToProtein(dna, peptide);
2445 assertEquals("[[2, 3]]",
2446 Arrays.deepToString(ml.getToRanges().toArray()));
2447 assertEquals("[[3, 3], [8, 12]]",
2448 Arrays.deepToString(ml.getFromRanges().toArray()));
2452 * Tests for the method that locates the CDS sequence that has a mapping to
2453 * the given protein. That is, given a transcript-to-peptide mapping, find the
2454 * cds-to-peptide mapping that relates to both, and return the CDS sequence.
2457 public void testFindCdsForProtein()
2459 List<AlignedCodonFrame> mappings = new ArrayList<>();
2460 AlignedCodonFrame acf1 = new AlignedCodonFrame();
2463 SequenceI dna1 = new Sequence("dna1", "cgatATcgGCTATCTATGacg");
2464 dna1.createDatasetSequence();
2466 // NB we currently exclude STOP codon from CDS sequences
2467 // the test would need to change if this changes in future
2468 SequenceI cds1 = new Sequence("cds1", "ATGCTATCT");
2469 cds1.createDatasetSequence();
2471 SequenceI pep1 = new Sequence("pep1", "MLS");
2472 pep1.createDatasetSequence();
2473 List<AlignedCodonFrame> seqMappings = new ArrayList<>();
2474 MapList mapList = new MapList(new int[] { 5, 6, 9, 15 },
2477 Mapping dnaToPeptide = new Mapping(pep1.getDatasetSequence(), mapList);
2479 // add dna to peptide mapping
2480 seqMappings.add(acf1);
2481 acf1.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(),
2485 * first case - no dna-to-CDS mapping exists - search fails
2487 SequenceI seq = AlignmentUtils.findCdsForProtein(mappings, dna1,
2488 seqMappings, dnaToPeptide);
2492 * second case - CDS-to-peptide mapping exists but no dna-to-CDS
2495 // todo this test fails if the mapping is added to acf1, not acf2
2496 // need to tidy up use of lists of mappings in AlignedCodonFrame
2497 AlignedCodonFrame acf2 = new AlignedCodonFrame();
2499 MapList cdsToPeptideMapping = new MapList(new int[] { 1, 9 },
2502 acf2.addMap(cds1.getDatasetSequence(), pep1.getDatasetSequence(),
2503 cdsToPeptideMapping);
2504 assertNull(AlignmentUtils.findCdsForProtein(mappings, dna1, seqMappings,
2508 * third case - add dna-to-CDS mapping - CDS is now found!
2510 MapList dnaToCdsMapping = new MapList(new int[] { 5, 6, 9, 15 },
2513 acf1.addMap(dna1.getDatasetSequence(), cds1.getDatasetSequence(),
2515 seq = AlignmentUtils.findCdsForProtein(mappings, dna1, seqMappings,
2517 assertSame(seq, cds1.getDatasetSequence());
2521 * Tests for the method that locates the CDS sequence that has a mapping to
2522 * the given protein. That is, given a transcript-to-peptide mapping, find the
2523 * cds-to-peptide mapping that relates to both, and return the CDS sequence.
2524 * This test is for the case where transcript and CDS are the same length.
2527 public void testFindCdsForProtein_noUTR()
2529 List<AlignedCodonFrame> mappings = new ArrayList<>();
2530 AlignedCodonFrame acf1 = new AlignedCodonFrame();
2533 SequenceI dna1 = new Sequence("dna1", "ATGCTATCTTAA");
2534 dna1.createDatasetSequence();
2536 // NB we currently exclude STOP codon from CDS sequences
2537 // the test would need to change if this changes in future
2538 SequenceI cds1 = new Sequence("cds1", "ATGCTATCT");
2539 cds1.createDatasetSequence();
2541 SequenceI pep1 = new Sequence("pep1", "MLS");
2542 pep1.createDatasetSequence();
2543 List<AlignedCodonFrame> seqMappings = new ArrayList<>();
2544 MapList mapList = new MapList(new int[] { 1, 9 }, new int[] { 1, 3 }, 3,
2546 Mapping dnaToPeptide = new Mapping(pep1.getDatasetSequence(), mapList);
2548 // add dna to peptide mapping
2549 seqMappings.add(acf1);
2550 acf1.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(),
2554 * first case - transcript lacks CDS features - it appears to be
2555 * the CDS sequence and is returned
2557 SequenceI seq = AlignmentUtils.findCdsForProtein(mappings, dna1,
2558 seqMappings, dnaToPeptide);
2559 assertSame(seq, dna1.getDatasetSequence());
2562 * second case - transcript has CDS feature - this means it is
2563 * not returned as a match for CDS (CDS sequences don't have CDS features)
2565 dna1.addSequenceFeature(
2566 new SequenceFeature(SequenceOntologyI.CDS, "cds", 1, 12, null));
2567 seq = AlignmentUtils.findCdsForProtein(mappings, dna1, seqMappings,
2572 * third case - CDS-to-peptide mapping exists but no dna-to-CDS
2575 // todo this test fails if the mapping is added to acf1, not acf2
2576 // need to tidy up use of lists of mappings in AlignedCodonFrame
2577 AlignedCodonFrame acf2 = new AlignedCodonFrame();
2579 MapList cdsToPeptideMapping = new MapList(new int[] { 1, 9 },
2582 acf2.addMap(cds1.getDatasetSequence(), pep1.getDatasetSequence(),
2583 cdsToPeptideMapping);
2584 assertNull(AlignmentUtils.findCdsForProtein(mappings, dna1, seqMappings,
2588 * fourth case - add dna-to-CDS mapping - CDS is now found!
2590 MapList dnaToCdsMapping = new MapList(new int[] { 1, 9 },
2593 acf1.addMap(dna1.getDatasetSequence(), cds1.getDatasetSequence(),
2595 seq = AlignmentUtils.findCdsForProtein(mappings, dna1, seqMappings,
2597 assertSame(seq, cds1.getDatasetSequence());