Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package jalview.analysis

File AlignmentUtilsTests.java

 

Code metrics

8
1,154
44
1
2,563
1,598
48
0.04
26.23
44
1.09

Classes

Class Line # Actions
AlignmentUtilsTests 67 1,154 48
0.9237147692.4%
 

Contributing tests

This file is covered by 37 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
20    */
21    package jalview.analysis;
22   
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;
29   
30    import jalview.analysis.AlignmentUtils.DnaVariant;
31    import jalview.datamodel.AlignedCodonFrame;
32    import jalview.datamodel.Alignment;
33    import jalview.datamodel.AlignmentAnnotation;
34    import jalview.datamodel.AlignmentI;
35    import jalview.datamodel.Annotation;
36    import jalview.datamodel.DBRefEntry;
37    import jalview.datamodel.GeneLociI;
38    import jalview.datamodel.Mapping;
39    import jalview.datamodel.SearchResultMatchI;
40    import jalview.datamodel.SearchResultsI;
41    import jalview.datamodel.Sequence;
42    import jalview.datamodel.SequenceFeature;
43    import jalview.datamodel.SequenceI;
44    import jalview.datamodel.features.SequenceFeatures;
45    import jalview.gui.JvOptionPane;
46    import jalview.io.AppletFormatAdapter;
47    import jalview.io.DataSourceType;
48    import jalview.io.FileFormat;
49    import jalview.io.FileFormatI;
50    import jalview.io.FormatAdapter;
51    import jalview.io.gff.SequenceOntologyI;
52    import jalview.util.MapList;
53    import jalview.util.MappingUtils;
54    import jalview.ws.params.InvalidArgumentException;
55   
56    import java.io.IOException;
57    import java.util.ArrayList;
58    import java.util.Arrays;
59    import java.util.LinkedHashMap;
60    import java.util.List;
61    import java.util.Map;
62    import java.util.TreeMap;
63   
64    import org.testng.annotations.BeforeClass;
65    import org.testng.annotations.Test;
66   
 
67    public class AlignmentUtilsTests
68    {
69    private static Sequence ts = new Sequence("short",
70    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm");
71   
 
72  1 toggle @BeforeClass(alwaysRun = true)
73    public void setUpJvOptionPane()
74    {
75  1 JvOptionPane.setInteractiveMode(false);
76  1 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
77    }
78   
 
79  1 toggle @Test(groups = { "Functional" })
80    public void testExpandContext()
81    {
82  1 AlignmentI al = new Alignment(new Sequence[] {});
83  6 for (int i = 4; i < 14; i += 2)
84    {
85  5 SequenceI s1 = ts.deriveSequence().getSubSequence(i, i + 7);
86  5 al.addSequence(s1);
87    }
88  1 System.out.println(new AppletFormatAdapter().formatSequences(
89    FileFormat.Clustal,
90    al, true));
91  27 for (int flnk = -1; flnk < 25; flnk++)
92    {
93  26 AlignmentI exp = AlignmentUtils.expandContext(al, flnk);
94  26 System.out.println("\nFlank size: " + flnk);
95  26 System.out.println(new AppletFormatAdapter().formatSequences(
96    FileFormat.Clustal, exp, true));
97  26 if (flnk == -1)
98    {
99    /*
100    * Full expansion to complete sequences
101    */
102  1 for (SequenceI sq : exp.getSequences())
103    {
104  5 String ung = sq.getSequenceAsString().replaceAll("-+", "");
105  5 final String errorMsg = "Flanking sequence not the same as original dataset sequence.\n"
106    + ung
107    + "\n"
108    + sq.getDatasetSequence().getSequenceAsString();
109  5 assertTrue(errorMsg, ung.equalsIgnoreCase(sq.getDatasetSequence()
110    .getSequenceAsString()));
111    }
112    }
113  25 else if (flnk == 24)
114    {
115    /*
116    * Last sequence is fully expanded, others have leading gaps to match
117    */
118  1 assertTrue(exp.getSequenceAt(4).getSequenceAsString()
119    .startsWith("abc"));
120  1 assertTrue(exp.getSequenceAt(3).getSequenceAsString()
121    .startsWith("--abc"));
122  1 assertTrue(exp.getSequenceAt(2).getSequenceAsString()
123    .startsWith("----abc"));
124  1 assertTrue(exp.getSequenceAt(1).getSequenceAsString()
125    .startsWith("------abc"));
126  1 assertTrue(exp.getSequenceAt(0).getSequenceAsString()
127    .startsWith("--------abc"));
128    }
129    }
130    }
131   
132    /**
133    * Test that annotations are correctly adjusted by expandContext
134    */
 
135  1 toggle @Test(groups = { "Functional" })
136    public void testExpandContext_annotation()
137    {
138  1 AlignmentI al = new Alignment(new Sequence[] {});
139  1 SequenceI ds = new Sequence("Seq1", "ABCDEFGHI");
140    // subsequence DEF:
141  1 SequenceI seq1 = ds.deriveSequence().getSubSequence(3, 6);
142  1 al.addSequence(seq1);
143   
144    /*
145    * Annotate DEF with 4/5/6 respectively
146    */
147  1 Annotation[] anns = new Annotation[] { new Annotation(4),
148    new Annotation(5), new Annotation(6) };
149  1 AlignmentAnnotation ann = new AlignmentAnnotation("SS",
150    "secondary structure", anns);
151  1 seq1.addAlignmentAnnotation(ann);
152   
153    /*
154    * The annotations array should match aligned positions
155    */
156  1 assertEquals(3, ann.annotations.length);
157  1 assertEquals(4, ann.annotations[0].value, 0.001);
158  1 assertEquals(5, ann.annotations[1].value, 0.001);
159  1 assertEquals(6, ann.annotations[2].value, 0.001);
160   
161    /*
162    * Check annotation to sequence position mappings before expanding the
163    * sequence; these are set up in Sequence.addAlignmentAnnotation ->
164    * Annotation.setSequenceRef -> createSequenceMappings
165    */
166  1 assertNull(ann.getAnnotationForPosition(1));
167  1 assertNull(ann.getAnnotationForPosition(2));
168  1 assertNull(ann.getAnnotationForPosition(3));
169  1 assertEquals(4, ann.getAnnotationForPosition(4).value, 0.001);
170  1 assertEquals(5, ann.getAnnotationForPosition(5).value, 0.001);
171  1 assertEquals(6, ann.getAnnotationForPosition(6).value, 0.001);
172  1 assertNull(ann.getAnnotationForPosition(7));
173  1 assertNull(ann.getAnnotationForPosition(8));
174  1 assertNull(ann.getAnnotationForPosition(9));
175   
176    /*
177    * Expand the subsequence to the full sequence abcDEFghi
178    */
179  1 AlignmentI expanded = AlignmentUtils.expandContext(al, -1);
180  1 assertEquals("abcDEFghi", expanded.getSequenceAt(0)
181    .getSequenceAsString());
182   
183    /*
184    * Confirm the alignment and sequence have the same SS annotation,
185    * referencing the expanded sequence
186    */
187  1 ann = expanded.getSequenceAt(0).getAnnotation()[0];
188  1 assertSame(ann, expanded.getAlignmentAnnotation()[0]);
189  1 assertSame(expanded.getSequenceAt(0), ann.sequenceRef);
190   
191    /*
192    * The annotations array should have null values except for annotated
193    * positions
194    */
195  1 assertNull(ann.annotations[0]);
196  1 assertNull(ann.annotations[1]);
197  1 assertNull(ann.annotations[2]);
198  1 assertEquals(4, ann.annotations[3].value, 0.001);
199  1 assertEquals(5, ann.annotations[4].value, 0.001);
200  1 assertEquals(6, ann.annotations[5].value, 0.001);
201  1 assertNull(ann.annotations[6]);
202  1 assertNull(ann.annotations[7]);
203  1 assertNull(ann.annotations[8]);
204   
205    /*
206    * sequence position mappings should be unchanged
207    */
208  1 assertNull(ann.getAnnotationForPosition(1));
209  1 assertNull(ann.getAnnotationForPosition(2));
210  1 assertNull(ann.getAnnotationForPosition(3));
211  1 assertEquals(4, ann.getAnnotationForPosition(4).value, 0.001);
212  1 assertEquals(5, ann.getAnnotationForPosition(5).value, 0.001);
213  1 assertEquals(6, ann.getAnnotationForPosition(6).value, 0.001);
214  1 assertNull(ann.getAnnotationForPosition(7));
215  1 assertNull(ann.getAnnotationForPosition(8));
216  1 assertNull(ann.getAnnotationForPosition(9));
217    }
218   
219    /**
220    * Test method that returns a map of lists of sequences by sequence name.
221    *
222    * @throws IOException
223    */
 
224  1 toggle @Test(groups = { "Functional" })
225    public void testGetSequencesByName() throws IOException
226    {
227  1 final String data = ">Seq1Name\nKQYL\n" + ">Seq2Name\nRFPW\n"
228    + ">Seq1Name\nABCD\n";
229  1 AlignmentI al = loadAlignment(data, FileFormat.Fasta);
230  1 Map<String, List<SequenceI>> map = AlignmentUtils
231    .getSequencesByName(al);
232  1 assertEquals(2, map.keySet().size());
233  1 assertEquals(2, map.get("Seq1Name").size());
234  1 assertEquals("KQYL", map.get("Seq1Name").get(0).getSequenceAsString());
235  1 assertEquals("ABCD", map.get("Seq1Name").get(1).getSequenceAsString());
236  1 assertEquals(1, map.get("Seq2Name").size());
237  1 assertEquals("RFPW", map.get("Seq2Name").get(0).getSequenceAsString());
238    }
239   
240    /**
241    * Helper method to load an alignment and ensure dataset sequences are set up.
242    *
243    * @param data
244    * @param format
245    * TODO
246    * @return
247    * @throws IOException
248    */
 
249  1 toggle protected AlignmentI loadAlignment(final String data, FileFormatI format)
250    throws IOException
251    {
252  1 AlignmentI a = new FormatAdapter().readFile(data,
253    DataSourceType.PASTE, format);
254  1 a.setDataset(null);
255  1 return a;
256    }
257   
258    /**
259    * Test mapping of protein to cDNA, for the case where we have no sequence
260    * cross-references, so mappings are made first-served 1-1 where sequences
261    * translate.
262    *
263    * @throws IOException
264    */
 
265  1 toggle @Test(groups = { "Functional" })
266    public void testMapProteinAlignmentToCdna_noXrefs() throws IOException
267    {
268  1 List<SequenceI> protseqs = new ArrayList<>();
269  1 protseqs.add(new Sequence("UNIPROT|V12345", "EIQ"));
270  1 protseqs.add(new Sequence("UNIPROT|V12346", "EIQ"));
271  1 protseqs.add(new Sequence("UNIPROT|V12347", "SAR"));
272  1 AlignmentI protein = new Alignment(protseqs.toArray(new SequenceI[3]));
273  1 protein.setDataset(null);
274   
275  1 List<SequenceI> dnaseqs = new ArrayList<>();
276  1 dnaseqs.add(new Sequence("EMBL|A11111", "TCAGCACGC")); // = SAR
277  1 dnaseqs.add(new Sequence("EMBL|A22222", "GAGATACAA")); // = EIQ
278  1 dnaseqs.add(new Sequence("EMBL|A33333", "GAAATCCAG")); // = EIQ
279  1 dnaseqs.add(new Sequence("EMBL|A44444", "GAAATTCAG")); // = EIQ
280  1 AlignmentI cdna = new Alignment(dnaseqs.toArray(new SequenceI[4]));
281  1 cdna.setDataset(null);
282   
283  1 assertTrue(AlignmentUtils.mapProteinAlignmentToCdna(protein, cdna));
284   
285    // 3 mappings made, each from 1 to 1 sequence
286  1 assertEquals(3, protein.getCodonFrames().size());
287  1 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).size());
288  1 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).size());
289  1 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(2)).size());
290   
291    // V12345 mapped to A22222
292  1 AlignedCodonFrame acf = protein.getCodonFrame(protein.getSequenceAt(0))
293    .get(0);
294  1 assertEquals(1, acf.getdnaSeqs().length);
295  1 assertEquals(cdna.getSequenceAt(1).getDatasetSequence(),
296    acf.getdnaSeqs()[0]);
297  1 Mapping[] protMappings = acf.getProtMappings();
298  1 assertEquals(1, protMappings.length);
299  1 MapList mapList = protMappings[0].getMap();
300  1 assertEquals(3, mapList.getFromRatio());
301  1 assertEquals(1, mapList.getToRatio());
302  1 assertTrue(Arrays.equals(new int[] { 1, 9 }, mapList.getFromRanges()
303    .get(0)));
304  1 assertEquals(1, mapList.getFromRanges().size());
305  1 assertTrue(Arrays.equals(new int[] { 1, 3 },
306    mapList.getToRanges().get(0)));
307  1 assertEquals(1, mapList.getToRanges().size());
308   
309    // V12346 mapped to A33333
310  1 acf = protein.getCodonFrame(protein.getSequenceAt(1)).get(0);
311  1 assertEquals(1, acf.getdnaSeqs().length);
312  1 assertEquals(cdna.getSequenceAt(2).getDatasetSequence(),
313    acf.getdnaSeqs()[0]);
314   
315    // V12347 mapped to A11111
316  1 acf = protein.getCodonFrame(protein.getSequenceAt(2)).get(0);
317  1 assertEquals(1, acf.getdnaSeqs().length);
318  1 assertEquals(cdna.getSequenceAt(0).getDatasetSequence(),
319    acf.getdnaSeqs()[0]);
320   
321    // no mapping involving the 'extra' A44444
322  1 assertTrue(protein.getCodonFrame(cdna.getSequenceAt(3)).isEmpty());
323    }
324   
325    /**
326    * Test for the alignSequenceAs method that takes two sequences and a mapping.
327    */
 
328  1 toggle @Test(groups = { "Functional" })
329    public void testAlignSequenceAs_withMapping_noIntrons()
330    {
331  1 MapList map = new MapList(new int[] { 1, 6 }, new int[] { 1, 2 }, 3, 1);
332   
333    /*
334    * No existing gaps in dna:
335    */
336  1 checkAlignSequenceAs("GGGAAA", "-A-L-", false, false, map,
337    "---GGG---AAA");
338   
339    /*
340    * Now introduce gaps in dna but ignore them when realigning.
341    */
342  1 checkAlignSequenceAs("-G-G-G-A-A-A-", "-A-L-", false, false, map,
343    "---GGG---AAA");
344   
345    /*
346    * Now include gaps in dna when realigning. First retaining 'mapped' gaps
347    * only, i.e. those within the exon region.
348    */
349  1 checkAlignSequenceAs("-G-G--G-A--A-A-", "-A-L-", true, false, map,
350    "---G-G--G---A--A-A");
351   
352    /*
353    * Include all gaps in dna when realigning (within and without the exon
354    * region). The leading gap, and the gaps between codons, are subsumed by
355    * the protein alignment gap.
356    */
357  1 checkAlignSequenceAs("-G-GG--AA-A---", "-A-L-", true, true, map,
358    "---G-GG---AA-A---");
359   
360    /*
361    * Include only unmapped gaps in dna when realigning (outside the exon
362    * region). The leading gap, and the gaps between codons, are subsumed by
363    * the protein alignment gap.
364    */
365  1 checkAlignSequenceAs("-G-GG--AA-A-", "-A-L-", false, true, map,
366    "---GGG---AAA---");
367    }
368   
369    /**
370    * Test for the alignSequenceAs method that takes two sequences and a mapping.
371    */
 
372  1 toggle @Test(groups = { "Functional" })
373    public void testAlignSequenceAs_withMapping_withIntrons()
374    {
375    /*
376    * Exons at codon 2 (AAA) and 4 (TTT)
377    */
378  1 MapList map = new MapList(new int[] { 4, 6, 10, 12 },
379    new int[] { 1, 2 }, 3, 1);
380   
381    /*
382    * Simple case: no gaps in dna
383    */
384  1 checkAlignSequenceAs("GGGAAACCCTTTGGG", "--A-L-", false, false, map,
385    "GGG---AAACCCTTTGGG");
386   
387    /*
388    * Add gaps to dna - but ignore when realigning.
389    */
390  1 checkAlignSequenceAs("-G-G-G--A--A---AC-CC-T-TT-GG-G-", "--A-L-",
391    false, false, map, "GGG---AAACCCTTTGGG");
392   
393    /*
394    * Add gaps to dna - include within exons only when realigning.
395    */
396  1 checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
397    true, false, map, "GGG---A--A---ACCCT-TTGGG");
398   
399    /*
400    * Include gaps outside exons only when realigning.
401    */
402  1 checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
403    false, true, map, "-G-G-GAAAC-CCTTT-GG-G-");
404   
405    /*
406    * Include gaps following first intron if we are 'preserving mapped gaps'
407    */
408  1 checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
409    true, true, map, "-G-G-G--A--A---A-C-CC-T-TT-GG-G-");
410   
411    /*
412    * Include all gaps in dna when realigning.
413    */
414  1 checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
415    true, true, map, "-G-G-G--A--A---A-C-CC-T-TT-GG-G-");
416    }
417   
418    /**
419    * Test for the case where not all of the protein sequence is mapped to cDNA.
420    */
 
421  1 toggle @Test(groups = { "Functional" })
422    public void testAlignSequenceAs_withMapping_withUnmappedProtein()
423    {
424    /*
425    * Exons at codon 2 (AAA) and 4 (TTT) mapped to A and P
426    */
427  1 final MapList map = new MapList(new int[] { 4, 6, 10, 12 }, new int[] {
428    1, 1, 3, 3 }, 3, 1);
429   
430    /*
431    * -L- 'aligns' ccc------
432    */
433  1 checkAlignSequenceAs("gggAAAcccTTTggg", "-A-L-P-", false, false, map,
434    "gggAAAccc------TTTggg");
435    }
436   
437    /**
438    * Helper method that performs and verifies the method under test.
439    *
440    * @param alignee
441    * the sequence to be realigned
442    * @param alignModel
443    * the sequence whose alignment is to be copied
444    * @param preserveMappedGaps
445    * @param preserveUnmappedGaps
446    * @param map
447    * @param expected
448    */
 
449  14 toggle protected void checkAlignSequenceAs(final String alignee,
450    final String alignModel, final boolean preserveMappedGaps,
451    final boolean preserveUnmappedGaps, MapList map,
452    final String expected)
453    {
454  14 SequenceI alignMe = new Sequence("Seq1", alignee);
455  14 alignMe.createDatasetSequence();
456  14 SequenceI alignFrom = new Sequence("Seq2", alignModel);
457  14 alignFrom.createDatasetSequence();
458  14 AlignedCodonFrame acf = new AlignedCodonFrame();
459  14 acf.addMap(alignMe.getDatasetSequence(),
460    alignFrom.getDatasetSequence(), map);
461   
462  14 AlignmentUtils.alignSequenceAs(alignMe, alignFrom, acf, "---", '-',
463    preserveMappedGaps, preserveUnmappedGaps);
464  14 assertEquals(expected, alignMe.getSequenceAsString());
465    }
466   
467    /**
468    * Test for the alignSequenceAs method where we preserve gaps in introns only.
469    */
 
470  1 toggle @Test(groups = { "Functional" })
471    public void testAlignSequenceAs_keepIntronGapsOnly()
472    {
473   
474    /*
475    * Intron GGGAAA followed by exon CCCTTT
476    */
477  1 MapList map = new MapList(new int[] { 7, 12 }, new int[] { 1, 2 }, 3, 1);
478   
479  1 checkAlignSequenceAs("GG-G-AA-A-C-CC-T-TT", "AL", false, true, map,
480    "GG-G-AA-ACCCTTT");
481    }
482   
483    /**
484    * Test the method that realigns protein to match mapped codon alignment.
485    */
 
486  1 toggle @Test(groups = { "Functional" })
487    public void testAlignProteinAsDna()
488    {
489    // seq1 codons are [1,2,3] [4,5,6] [7,8,9] [10,11,12]
490  1 SequenceI dna1 = new Sequence("Seq1", "TGCCATTACCAG-");
491    // seq2 codons are [1,3,4] [5,6,7] [8,9,10] [11,12,13]
492  1 SequenceI dna2 = new Sequence("Seq2", "T-GCCATTACCAG");
493    // seq3 codons are [1,2,3] [4,5,7] [8,9,10] [11,12,13]
494  1 SequenceI dna3 = new Sequence("Seq3", "TGCCA-TTACCAG");
495  1 AlignmentI dna = new Alignment(new SequenceI[] { dna1, dna2, dna3 });
496  1 dna.setDataset(null);
497   
498    // protein alignment will be realigned like dna
499  1 SequenceI prot1 = new Sequence("Seq1", "CHYQ");
500  1 SequenceI prot2 = new Sequence("Seq2", "CHYQ");
501  1 SequenceI prot3 = new Sequence("Seq3", "CHYQ");
502  1 SequenceI prot4 = new Sequence("Seq4", "R-QSV"); // unmapped, unchanged
503  1 AlignmentI protein = new Alignment(new SequenceI[] { prot1, prot2,
504    prot3, prot4 });
505  1 protein.setDataset(null);
506   
507  1 MapList map = new MapList(new int[] { 1, 12 }, new int[] { 1, 4 }, 3, 1);
508  1 AlignedCodonFrame acf = new AlignedCodonFrame();
509  1 acf.addMap(dna1.getDatasetSequence(), prot1.getDatasetSequence(), map);
510  1 acf.addMap(dna2.getDatasetSequence(), prot2.getDatasetSequence(), map);
511  1 acf.addMap(dna3.getDatasetSequence(), prot3.getDatasetSequence(), map);
512  1 ArrayList<AlignedCodonFrame> acfs = new ArrayList<>();
513  1 acfs.add(acf);
514  1 protein.setCodonFrames(acfs);
515   
516    /*
517    * Translated codon order is [1,2,3] [1,3,4] [4,5,6] [4,5,7] [5,6,7] [7,8,9]
518    * [8,9,10] [10,11,12] [11,12,13]
519    */
520  1 AlignmentUtils.alignProteinAsDna(protein, dna);
521  1 assertEquals("C-H--Y-Q-", prot1.getSequenceAsString());
522  1 assertEquals("-C--H-Y-Q", prot2.getSequenceAsString());
523  1 assertEquals("C--H--Y-Q", prot3.getSequenceAsString());
524  1 assertEquals("R-QSV", prot4.getSequenceAsString());
525    }
526   
527    /**
528    * Test the method that tests whether a CDNA sequence translates to a protein
529    * sequence
530    */
 
531  1 toggle @Test(groups = { "Functional" })
532    public void testTranslatesAs()
533    {
534    // null arguments check
535  1 assertFalse(AlignmentUtils.translatesAs(null, 0, null));
536  1 assertFalse(AlignmentUtils.translatesAs(new char[] { 't' }, 0, null));
537  1 assertFalse(AlignmentUtils.translatesAs(null, 0, new char[] { 'a' }));
538   
539    // straight translation
540  1 assertTrue(AlignmentUtils.translatesAs("tttcccaaaggg".toCharArray(), 0,
541    "FPKG".toCharArray()));
542    // with extra start codon (not in protein)
543  1 assertTrue(AlignmentUtils.translatesAs("atgtttcccaaaggg".toCharArray(),
544    3, "FPKG".toCharArray()));
545    // with stop codon1 (not in protein)
546  1 assertTrue(AlignmentUtils.translatesAs("tttcccaaagggtaa".toCharArray(),
547    0, "FPKG".toCharArray()));
548    // with stop codon1 (in protein as *)
549  1 assertTrue(AlignmentUtils.translatesAs("tttcccaaagggtaa".toCharArray(),
550    0, "FPKG*".toCharArray()));
551    // with stop codon2 (not in protein)
552  1 assertTrue(AlignmentUtils.translatesAs("tttcccaaagggtag".toCharArray(),
553    0, "FPKG".toCharArray()));
554    // with stop codon3 (not in protein)
555  1 assertTrue(AlignmentUtils.translatesAs("tttcccaaagggtga".toCharArray(),
556    0, "FPKG".toCharArray()));
557    // with start and stop codon1
558  1 assertTrue(AlignmentUtils.translatesAs(
559    "atgtttcccaaagggtaa".toCharArray(), 3, "FPKG".toCharArray()));
560    // with start and stop codon1 (in protein as *)
561  1 assertTrue(AlignmentUtils.translatesAs(
562    "atgtttcccaaagggtaa".toCharArray(), 3, "FPKG*".toCharArray()));
563    // with start and stop codon2
564  1 assertTrue(AlignmentUtils.translatesAs(
565    "atgtttcccaaagggtag".toCharArray(), 3, "FPKG".toCharArray()));
566    // with start and stop codon3
567  1 assertTrue(AlignmentUtils.translatesAs(
568    "atgtttcccaaagggtga".toCharArray(), 3, "FPKG".toCharArray()));
569   
570    // with embedded stop codons
571  1 assertTrue(AlignmentUtils.translatesAs(
572    "atgtttTAGcccaaaTAAgggtga".toCharArray(), 3,
573    "F*PK*G".toCharArray()));
574   
575    // wrong protein
576  1 assertFalse(AlignmentUtils.translatesAs("tttcccaaaggg".toCharArray(),
577    0, "FPMG".toCharArray()));
578   
579    // truncated dna
580  1 assertFalse(AlignmentUtils.translatesAs("tttcccaaagg".toCharArray(), 0,
581    "FPKG".toCharArray()));
582   
583    // truncated protein
584  1 assertFalse(AlignmentUtils.translatesAs("tttcccaaaggg".toCharArray(),
585    0, "FPK".toCharArray()));
586   
587    // overlong dna (doesn't end in stop codon)
588  1 assertFalse(AlignmentUtils.translatesAs(
589    "tttcccaaagggttt".toCharArray(), 0, "FPKG".toCharArray()));
590   
591    // dna + stop codon + more
592  1 assertFalse(AlignmentUtils.translatesAs(
593    "tttcccaaagggttaga".toCharArray(), 0, "FPKG".toCharArray()));
594   
595    // overlong protein
596  1 assertFalse(AlignmentUtils.translatesAs("tttcccaaaggg".toCharArray(),
597    0, "FPKGQ".toCharArray()));
598    }
599   
600    /**
601    * Test mapping of protein to cDNA, for cases where the cDNA has start and/or
602    * stop codons in addition to the protein coding sequence.
603    *
604    * @throws IOException
605    */
 
606  1 toggle @Test(groups = { "Functional" })
607    public void testMapProteinAlignmentToCdna_withStartAndStopCodons()
608    throws IOException
609    {
610  1 List<SequenceI> protseqs = new ArrayList<>();
611  1 protseqs.add(new Sequence("UNIPROT|V12345", "EIQ"));
612  1 protseqs.add(new Sequence("UNIPROT|V12346", "EIQ"));
613  1 protseqs.add(new Sequence("UNIPROT|V12347", "SAR"));
614  1 AlignmentI protein = new Alignment(protseqs.toArray(new SequenceI[3]));
615  1 protein.setDataset(null);
616   
617  1 List<SequenceI> dnaseqs = new ArrayList<>();
618    // start + SAR:
619  1 dnaseqs.add(new Sequence("EMBL|A11111", "ATGTCAGCACGC"));
620    // = EIQ + stop
621  1 dnaseqs.add(new Sequence("EMBL|A22222", "GAGATACAATAA"));
622    // = start +EIQ + stop
623  1 dnaseqs.add(new Sequence("EMBL|A33333", "ATGGAAATCCAGTAG"));
624  1 dnaseqs.add(new Sequence("EMBL|A44444", "GAAATTCAG"));
625  1 AlignmentI cdna = new Alignment(dnaseqs.toArray(new SequenceI[4]));
626  1 cdna.setDataset(null);
627   
628  1 assertTrue(AlignmentUtils.mapProteinAlignmentToCdna(protein, cdna));
629   
630    // 3 mappings made, each from 1 to 1 sequence
631  1 assertEquals(3, protein.getCodonFrames().size());
632  1 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).size());
633  1 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).size());
634  1 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(2)).size());
635   
636    // V12345 mapped from A22222
637  1 AlignedCodonFrame acf = protein.getCodonFrame(protein.getSequenceAt(0))
638    .get(0);
639  1 assertEquals(1, acf.getdnaSeqs().length);
640  1 assertEquals(cdna.getSequenceAt(1).getDatasetSequence(),
641    acf.getdnaSeqs()[0]);
642  1 Mapping[] protMappings = acf.getProtMappings();
643  1 assertEquals(1, protMappings.length);
644  1 MapList mapList = protMappings[0].getMap();
645  1 assertEquals(3, mapList.getFromRatio());
646  1 assertEquals(1, mapList.getToRatio());
647  1 assertTrue(Arrays.equals(new int[] { 1, 9 }, mapList.getFromRanges()
648    .get(0)));
649  1 assertEquals(1, mapList.getFromRanges().size());
650  1 assertTrue(Arrays.equals(new int[] { 1, 3 },
651    mapList.getToRanges().get(0)));
652  1 assertEquals(1, mapList.getToRanges().size());
653   
654    // V12346 mapped from A33333 starting position 4
655  1 acf = protein.getCodonFrame(protein.getSequenceAt(1)).get(0);
656  1 assertEquals(1, acf.getdnaSeqs().length);
657  1 assertEquals(cdna.getSequenceAt(2).getDatasetSequence(),
658    acf.getdnaSeqs()[0]);
659  1 protMappings = acf.getProtMappings();
660  1 assertEquals(1, protMappings.length);
661  1 mapList = protMappings[0].getMap();
662  1 assertEquals(3, mapList.getFromRatio());
663  1 assertEquals(1, mapList.getToRatio());
664  1 assertTrue(Arrays.equals(new int[] { 4, 12 }, mapList.getFromRanges()
665    .get(0)));
666  1 assertEquals(1, mapList.getFromRanges().size());
667  1 assertTrue(Arrays.equals(new int[] { 1, 3 },
668    mapList.getToRanges().get(0)));
669  1 assertEquals(1, mapList.getToRanges().size());
670   
671    // V12347 mapped to A11111 starting position 4
672  1 acf = protein.getCodonFrame(protein.getSequenceAt(2)).get(0);
673  1 assertEquals(1, acf.getdnaSeqs().length);
674  1 assertEquals(cdna.getSequenceAt(0).getDatasetSequence(),
675    acf.getdnaSeqs()[0]);
676  1 protMappings = acf.getProtMappings();
677  1 assertEquals(1, protMappings.length);
678  1 mapList = protMappings[0].getMap();
679  1 assertEquals(3, mapList.getFromRatio());
680  1 assertEquals(1, mapList.getToRatio());
681  1 assertTrue(Arrays.equals(new int[] { 4, 12 }, mapList.getFromRanges()
682    .get(0)));
683  1 assertEquals(1, mapList.getFromRanges().size());
684  1 assertTrue(Arrays.equals(new int[] { 1, 3 },
685    mapList.getToRanges().get(0)));
686  1 assertEquals(1, mapList.getToRanges().size());
687   
688    // no mapping involving the 'extra' A44444
689  1 assertTrue(protein.getCodonFrame(cdna.getSequenceAt(3)).isEmpty());
690    }
691   
692    /**
693    * Test mapping of protein to cDNA, for the case where we have some sequence
694    * cross-references. Verify that 1-to-many mappings are made where
695    * cross-references exist and sequences are mappable.
696    *
697    * @throws IOException
698    */
 
699  1 toggle @Test(groups = { "Functional" })
700    public void testMapProteinAlignmentToCdna_withXrefs() throws IOException
701    {
702  1 List<SequenceI> protseqs = new ArrayList<>();
703  1 protseqs.add(new Sequence("UNIPROT|V12345", "EIQ"));
704  1 protseqs.add(new Sequence("UNIPROT|V12346", "EIQ"));
705  1 protseqs.add(new Sequence("UNIPROT|V12347", "SAR"));
706  1 AlignmentI protein = new Alignment(protseqs.toArray(new SequenceI[3]));
707  1 protein.setDataset(null);
708   
709  1 List<SequenceI> dnaseqs = new ArrayList<>();
710  1 dnaseqs.add(new Sequence("EMBL|A11111", "TCAGCACGC")); // = SAR
711  1 dnaseqs.add(new Sequence("EMBL|A22222", "ATGGAGATACAA")); // = start + EIQ
712  1 dnaseqs.add(new Sequence("EMBL|A33333", "GAAATCCAG")); // = EIQ
713  1 dnaseqs.add(new Sequence("EMBL|A44444", "GAAATTCAG")); // = EIQ
714  1 dnaseqs.add(new Sequence("EMBL|A55555", "GAGATTCAG")); // = EIQ
715  1 AlignmentI cdna = new Alignment(dnaseqs.toArray(new SequenceI[5]));
716  1 cdna.setDataset(null);
717   
718    // Xref A22222 to V12345 (should get mapped)
719  1 dnaseqs.get(1).addDBRef(new DBRefEntry("UNIPROT", "1", "V12345"));
720    // Xref V12345 to A44444 (should get mapped)
721  1 protseqs.get(0).addDBRef(new DBRefEntry("EMBL", "1", "A44444"));
722    // Xref A33333 to V12347 (sequence mismatch - should not get mapped)
723  1 dnaseqs.get(2).addDBRef(new DBRefEntry("UNIPROT", "1", "V12347"));
724    // as V12345 is mapped to A22222 and A44444, this leaves V12346 unmapped.
725    // it should get paired up with the unmapped A33333
726    // A11111 should be mapped to V12347
727    // A55555 is spare and has no xref so is not mapped
728   
729  1 assertTrue(AlignmentUtils.mapProteinAlignmentToCdna(protein, cdna));
730   
731    // 4 protein mappings made for 3 proteins, 2 to V12345, 1 each to V12346/7
732  1 assertEquals(3, protein.getCodonFrames().size());
733  1 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).size());
734  1 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).size());
735  1 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(2)).size());
736   
737    // one mapping for each of the first 4 cDNA sequences
738  1 assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(0)).size());
739  1 assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(1)).size());
740  1 assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(2)).size());
741  1 assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(3)).size());
742   
743    // V12345 mapped to A22222 and A44444
744  1 AlignedCodonFrame acf = protein.getCodonFrame(protein.getSequenceAt(0))
745    .get(0);
746  1 assertEquals(2, acf.getdnaSeqs().length);
747  1 assertEquals(cdna.getSequenceAt(1).getDatasetSequence(),
748    acf.getdnaSeqs()[0]);
749  1 assertEquals(cdna.getSequenceAt(3).getDatasetSequence(),
750    acf.getdnaSeqs()[1]);
751   
752    // V12346 mapped to A33333
753  1 acf = protein.getCodonFrame(protein.getSequenceAt(1)).get(0);
754  1 assertEquals(1, acf.getdnaSeqs().length);
755  1 assertEquals(cdna.getSequenceAt(2).getDatasetSequence(),
756    acf.getdnaSeqs()[0]);
757   
758    // V12347 mapped to A11111
759  1 acf = protein.getCodonFrame(protein.getSequenceAt(2)).get(0);
760  1 assertEquals(1, acf.getdnaSeqs().length);
761  1 assertEquals(cdna.getSequenceAt(0).getDatasetSequence(),
762    acf.getdnaSeqs()[0]);
763   
764    // no mapping involving the 'extra' A55555
765  1 assertTrue(protein.getCodonFrame(cdna.getSequenceAt(4)).isEmpty());
766    }
767   
768    /**
769    * Test mapping of protein to cDNA, for the case where we have some sequence
770    * cross-references. Verify that once we have made an xref mapping we don't
771    * also map un-xrefd sequeces.
772    *
773    * @throws IOException
774    */
 
775  1 toggle @Test(groups = { "Functional" })
776    public void testMapProteinAlignmentToCdna_prioritiseXrefs()
777    throws IOException
778    {
779  1 List<SequenceI> protseqs = new ArrayList<>();
780  1 protseqs.add(new Sequence("UNIPROT|V12345", "EIQ"));
781  1 protseqs.add(new Sequence("UNIPROT|V12346", "EIQ"));
782  1 AlignmentI protein = new Alignment(
783    protseqs.toArray(new SequenceI[protseqs.size()]));
784  1 protein.setDataset(null);
785   
786  1 List<SequenceI> dnaseqs = new ArrayList<>();
787  1 dnaseqs.add(new Sequence("EMBL|A11111", "GAAATCCAG")); // = EIQ
788  1 dnaseqs.add(new Sequence("EMBL|A22222", "GAAATTCAG")); // = EIQ
789  1 AlignmentI cdna = new Alignment(dnaseqs.toArray(new SequenceI[dnaseqs
790    .size()]));
791  1 cdna.setDataset(null);
792   
793    // Xref A22222 to V12345 (should get mapped)
794    // A11111 should then be mapped to the unmapped V12346
795  1 dnaseqs.get(1).addDBRef(new DBRefEntry("UNIPROT", "1", "V12345"));
796   
797  1 assertTrue(AlignmentUtils.mapProteinAlignmentToCdna(protein, cdna));
798   
799    // 2 protein mappings made
800  1 assertEquals(2, protein.getCodonFrames().size());
801  1 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).size());
802  1 assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).size());
803   
804    // one mapping for each of the cDNA sequences
805  1 assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(0)).size());
806  1 assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(1)).size());
807   
808    // V12345 mapped to A22222
809  1 AlignedCodonFrame acf = protein.getCodonFrame(protein.getSequenceAt(0))
810    .get(0);
811  1 assertEquals(1, acf.getdnaSeqs().length);
812  1 assertEquals(cdna.getSequenceAt(1).getDatasetSequence(),
813    acf.getdnaSeqs()[0]);
814   
815    // V12346 mapped to A11111
816  1 acf = protein.getCodonFrame(protein.getSequenceAt(1)).get(0);
817  1 assertEquals(1, acf.getdnaSeqs().length);
818  1 assertEquals(cdna.getSequenceAt(0).getDatasetSequence(),
819    acf.getdnaSeqs()[0]);
820    }
821   
822    /**
823    * Test the method that shows or hides sequence annotations by type(s) and
824    * selection group.
825    */
 
826  1 toggle @Test(groups = { "Functional" })
827    public void testShowOrHideSequenceAnnotations()
828    {
829  1 SequenceI seq1 = new Sequence("Seq1", "AAA");
830  1 SequenceI seq2 = new Sequence("Seq2", "BBB");
831  1 SequenceI seq3 = new Sequence("Seq3", "CCC");
832  1 Annotation[] anns = new Annotation[] { new Annotation(2f) };
833  1 AlignmentAnnotation ann1 = new AlignmentAnnotation("Structure", "ann1",
834    anns);
835  1 ann1.setSequenceRef(seq1);
836  1 AlignmentAnnotation ann2 = new AlignmentAnnotation("Structure", "ann2",
837    anns);
838  1 ann2.setSequenceRef(seq2);
839  1 AlignmentAnnotation ann3 = new AlignmentAnnotation("Structure", "ann3",
840    anns);
841  1 AlignmentAnnotation ann4 = new AlignmentAnnotation("Temp", "ann4", anns);
842  1 ann4.setSequenceRef(seq1);
843  1 AlignmentAnnotation ann5 = new AlignmentAnnotation("Temp", "ann5", anns);
844  1 ann5.setSequenceRef(seq2);
845  1 AlignmentAnnotation ann6 = new AlignmentAnnotation("Temp", "ann6", anns);
846  1 AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2, seq3 });
847  1 al.addAnnotation(ann1); // Structure for Seq1
848  1 al.addAnnotation(ann2); // Structure for Seq2
849  1 al.addAnnotation(ann3); // Structure for no sequence
850  1 al.addAnnotation(ann4); // Temp for seq1
851  1 al.addAnnotation(ann5); // Temp for seq2
852  1 al.addAnnotation(ann6); // Temp for no sequence
853  1 List<String> types = new ArrayList<>();
854  1 List<SequenceI> scope = new ArrayList<>();
855   
856    /*
857    * Set all sequence related Structure to hidden (ann1, ann2)
858    */
859  1 types.add("Structure");
860  1 AlignmentUtils.showOrHideSequenceAnnotations(al, types, null, false,
861    false);
862  1 assertFalse(ann1.visible);
863  1 assertFalse(ann2.visible);
864  1 assertTrue(ann3.visible); // not sequence-related, not affected
865  1 assertTrue(ann4.visible); // not Structure, not affected
866  1 assertTrue(ann5.visible); // "
867  1 assertTrue(ann6.visible); // not sequence-related, not affected
868   
869    /*
870    * Set Temp in {seq1, seq3} to hidden
871    */
872  1 types.clear();
873  1 types.add("Temp");
874  1 scope.add(seq1);
875  1 scope.add(seq3);
876  1 AlignmentUtils.showOrHideSequenceAnnotations(al, types, scope, false,
877    false);
878  1 assertFalse(ann1.visible); // unchanged
879  1 assertFalse(ann2.visible); // unchanged
880  1 assertTrue(ann3.visible); // not sequence-related, not affected
881  1 assertFalse(ann4.visible); // Temp for seq1 hidden
882  1 assertTrue(ann5.visible); // not in scope, not affected
883  1 assertTrue(ann6.visible); // not sequence-related, not affected
884   
885    /*
886    * Set Temp in all sequences to hidden
887    */
888  1 types.clear();
889  1 types.add("Temp");
890  1 scope.add(seq1);
891  1 scope.add(seq3);
892  1 AlignmentUtils.showOrHideSequenceAnnotations(al, types, null, false,
893    false);
894  1 assertFalse(ann1.visible); // unchanged
895  1 assertFalse(ann2.visible); // unchanged
896  1 assertTrue(ann3.visible); // not sequence-related, not affected
897  1 assertFalse(ann4.visible); // Temp for seq1 hidden
898  1 assertFalse(ann5.visible); // Temp for seq2 hidden
899  1 assertTrue(ann6.visible); // not sequence-related, not affected
900   
901    /*
902    * Set all types in {seq1, seq3} to visible
903    */
904  1 types.clear();
905  1 scope.clear();
906  1 scope.add(seq1);
907  1 scope.add(seq3);
908  1 AlignmentUtils.showOrHideSequenceAnnotations(al, types, scope, true,
909    true);
910  1 assertTrue(ann1.visible); // Structure for seq1 set visible
911  1 assertFalse(ann2.visible); // not in scope, unchanged
912  1 assertTrue(ann3.visible); // not sequence-related, not affected
913  1 assertTrue(ann4.visible); // Temp for seq1 set visible
914  1 assertFalse(ann5.visible); // not in scope, unchanged
915  1 assertTrue(ann6.visible); // not sequence-related, not affected
916   
917    /*
918    * Set all types in all scope to hidden
919    */
920  1 AlignmentUtils.showOrHideSequenceAnnotations(al, types, null, true,
921    false);
922  1 assertFalse(ann1.visible);
923  1 assertFalse(ann2.visible);
924  1 assertTrue(ann3.visible); // not sequence-related, not affected
925  1 assertFalse(ann4.visible);
926  1 assertFalse(ann5.visible);
927  1 assertTrue(ann6.visible); // not sequence-related, not affected
928    }
929   
930    /**
931    * Tests for the method that checks if one sequence cross-references another
932    */
 
933  1 toggle @Test(groups = { "Functional" })
934    public void testHasCrossRef()
935    {
936  1 assertFalse(AlignmentUtils.hasCrossRef(null, null));
937  1 SequenceI seq1 = new Sequence("EMBL|A12345", "ABCDEF");
938  1 assertFalse(AlignmentUtils.hasCrossRef(seq1, null));
939  1 assertFalse(AlignmentUtils.hasCrossRef(null, seq1));
940  1 SequenceI seq2 = new Sequence("UNIPROT|V20192", "ABCDEF");
941  1 assertFalse(AlignmentUtils.hasCrossRef(seq1, seq2));
942   
943    // different ref
944  1 seq1.addDBRef(new DBRefEntry("UNIPROT", "1", "v20193"));
945  1 assertFalse(AlignmentUtils.hasCrossRef(seq1, seq2));
946   
947    // case-insensitive; version number is ignored
948  1 seq1.addDBRef(new DBRefEntry("UNIPROT", "1", "v20192"));
949  1 assertTrue(AlignmentUtils.hasCrossRef(seq1, seq2));
950   
951    // right case!
952  1 seq1.addDBRef(new DBRefEntry("UNIPROT", "1", "V20192"));
953  1 assertTrue(AlignmentUtils.hasCrossRef(seq1, seq2));
954    // test is one-way only
955  1 assertFalse(AlignmentUtils.hasCrossRef(seq2, seq1));
956    }
957   
958    /**
959    * Tests for the method that checks if either sequence cross-references the
960    * other
961    */
 
962  1 toggle @Test(groups = { "Functional" })
963    public void testHaveCrossRef()
964    {
965  1 assertFalse(AlignmentUtils.hasCrossRef(null, null));
966  1 SequenceI seq1 = new Sequence("EMBL|A12345", "ABCDEF");
967  1 assertFalse(AlignmentUtils.haveCrossRef(seq1, null));
968  1 assertFalse(AlignmentUtils.haveCrossRef(null, seq1));
969  1 SequenceI seq2 = new Sequence("UNIPROT|V20192", "ABCDEF");
970  1 assertFalse(AlignmentUtils.haveCrossRef(seq1, seq2));
971   
972  1 seq1.addDBRef(new DBRefEntry("UNIPROT", "1", "V20192"));
973  1 assertTrue(AlignmentUtils.haveCrossRef(seq1, seq2));
974    // next is true for haveCrossRef, false for hasCrossRef
975  1 assertTrue(AlignmentUtils.haveCrossRef(seq2, seq1));
976   
977    // now the other way round
978  1 seq1.setDBRefs(null);
979  1 seq2.addDBRef(new DBRefEntry("EMBL", "1", "A12345"));
980  1 assertTrue(AlignmentUtils.haveCrossRef(seq1, seq2));
981  1 assertTrue(AlignmentUtils.haveCrossRef(seq2, seq1));
982   
983    // now both ways
984  1 seq1.addDBRef(new DBRefEntry("UNIPROT", "1", "V20192"));
985  1 assertTrue(AlignmentUtils.haveCrossRef(seq1, seq2));
986  1 assertTrue(AlignmentUtils.haveCrossRef(seq2, seq1));
987    }
988   
989    /**
990    * Test the method that extracts the cds-only part of a dna alignment.
991    */
 
992  1 toggle @Test(groups = { "Functional" })
993    public void testMakeCdsAlignment()
994    {
995    /*
996    * scenario:
997    * dna1 --> [4, 6] [10,12] --> pep1
998    * dna2 --> [1, 3] [7, 9] [13,15] --> pep2
999    */
1000  1 SequenceI dna1 = new Sequence("dna1", "aaaGGGcccTTTaaa");
1001  1 SequenceI dna2 = new Sequence("dna2", "GGGcccTTTaaaCCC");
1002  1 SequenceI pep1 = new Sequence("pep1", "GF");
1003  1 SequenceI pep2 = new Sequence("pep2", "GFP");
1004  1 pep1.addDBRef(new DBRefEntry("UNIPROT", "0", "pep1"));
1005  1 pep2.addDBRef(new DBRefEntry("UNIPROT", "0", "pep2"));
1006  1 dna1.createDatasetSequence();
1007  1 dna2.createDatasetSequence();
1008  1 pep1.createDatasetSequence();
1009  1 pep2.createDatasetSequence();
1010  1 AlignmentI dna = new Alignment(new SequenceI[] { dna1, dna2 });
1011  1 dna.setDataset(null);
1012   
1013    /*
1014    * put a variant feature on dna2 base 8
1015    * - should transfer to cds2 base 5
1016    */
1017  1 dna2.addSequenceFeature(new SequenceFeature("variant", "hgmd", 8, 8,
1018    0f, null));
1019   
1020    /*
1021    * need a sourceDbRef if we are to construct dbrefs to the CDS
1022    * sequence from the dna contig sequences
1023    */
1024  1 DBRefEntry dbref = new DBRefEntry("ENSEMBL", "0", "dna1");
1025  1 dna1.getDatasetSequence().addDBRef(dbref);
1026  1 org.testng.Assert.assertEquals(dbref, dna1.getPrimaryDBRefs().get(0));
1027  1 dbref = new DBRefEntry("ENSEMBL", "0", "dna2");
1028  1 dna2.getDatasetSequence().addDBRef(dbref);
1029  1 org.testng.Assert.assertEquals(dbref, dna2.getPrimaryDBRefs().get(0));
1030   
1031    /*
1032    * CDS sequences are 'discovered' from dna-to-protein mappings on the alignment
1033    * dataset (e.g. added from dbrefs by CrossRef.findXrefSequences)
1034    */
1035  1 MapList mapfordna1 = new MapList(new int[] { 4, 6, 10, 12 }, new int[] {
1036    1, 2 }, 3, 1);
1037  1 AlignedCodonFrame acf = new AlignedCodonFrame();
1038  1 acf.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(),
1039    mapfordna1);
1040  1 dna.addCodonFrame(acf);
1041  1 MapList mapfordna2 = new MapList(new int[] { 1, 3, 7, 9, 13, 15 },
1042    new int[] { 1, 3 }, 3, 1);
1043  1 acf = new AlignedCodonFrame();
1044  1 acf.addMap(dna2.getDatasetSequence(), pep2.getDatasetSequence(),
1045    mapfordna2);
1046  1 dna.addCodonFrame(acf);
1047   
1048    /*
1049    * In this case, mappings originally came from matching Uniprot accessions
1050    * - so need an xref on dna involving those regions.
1051    * These are normally constructed from CDS annotation
1052    */
1053  1 DBRefEntry dna1xref = new DBRefEntry("UNIPROT", "ENSEMBL", "pep1",
1054    new Mapping(mapfordna1));
1055  1 dna1.addDBRef(dna1xref);
1056  1 assertEquals(2, dna1.getDBRefs().size()); // to self and to pep1
1057  1 DBRefEntry dna2xref = new DBRefEntry("UNIPROT", "ENSEMBL", "pep2",
1058    new Mapping(mapfordna2));
1059  1 dna2.addDBRef(dna2xref);
1060  1 assertEquals(2, dna2.getDBRefs().size()); // to self and to pep2
1061   
1062    /*
1063    * execute method under test:
1064    */
1065  1 AlignmentI cds = AlignmentUtils.makeCdsAlignment(new SequenceI[] {
1066    dna1, dna2 }, dna.getDataset(), null);
1067   
1068    /*
1069    * verify cds sequences
1070    */
1071  1 assertEquals(2, cds.getSequences().size());
1072  1 assertEquals("GGGTTT", cds.getSequenceAt(0).getSequenceAsString());
1073  1 assertEquals("GGGTTTCCC", cds.getSequenceAt(1).getSequenceAsString());
1074   
1075    /*
1076    * verify shared, extended alignment dataset
1077    */
1078  1 assertSame(dna.getDataset(), cds.getDataset());
1079  1 SequenceI cds1Dss = cds.getSequenceAt(0).getDatasetSequence();
1080  1 SequenceI cds2Dss = cds.getSequenceAt(1).getDatasetSequence();
1081  1 assertTrue(dna.getDataset().getSequences().contains(cds1Dss));
1082  1 assertTrue(dna.getDataset().getSequences().contains(cds2Dss));
1083   
1084    /*
1085    * verify CDS has a dbref with mapping to peptide
1086    */
1087  1 assertNotNull(cds1Dss.getDBRefs());
1088  1 assertEquals(2, cds1Dss.getDBRefs().size());
1089  1 dbref = cds1Dss.getDBRefs().get(0);
1090  1 assertEquals(dna1xref.getSource(), dbref.getSource());
1091    // version is via ensembl's primary ref
1092  1 assertEquals(dna1xref.getVersion(), dbref.getVersion());
1093  1 assertEquals(dna1xref.getAccessionId(), dbref.getAccessionId());
1094  1 assertNotNull(dbref.getMap());
1095  1 assertSame(pep1.getDatasetSequence(), dbref.getMap().getTo());
1096  1 MapList cdsMapping = new MapList(new int[] { 1, 6 },
1097    new int[] { 1, 2 }, 3, 1);
1098  1 assertEquals(cdsMapping, dbref.getMap().getMap());
1099   
1100    /*
1101    * verify peptide has added a dbref with reverse mapping to CDS
1102    */
1103  1 assertNotNull(pep1.getDBRefs());
1104    // FIXME pep1.getDBRefs() is 1 - is that the correct behaviour ?
1105  1 assertEquals(2, pep1.getDBRefs().size());
1106  1 dbref = pep1.getDBRefs().get(1);
1107  1 assertEquals("ENSEMBL", dbref.getSource());
1108  1 assertEquals("0", dbref.getVersion());
1109  1 assertEquals("CDS|dna1", dbref.getAccessionId());
1110  1 assertNotNull(dbref.getMap());
1111  1 assertSame(cds1Dss, dbref.getMap().getTo());
1112  1 assertEquals(cdsMapping.getInverse(), dbref.getMap().getMap());
1113   
1114    /*
1115    * verify cDNA has added a dbref with mapping to CDS
1116    */
1117  1 assertEquals(3, dna1.getDBRefs().size());
1118  1 DBRefEntry dbRefEntry = dna1.getDBRefs().get(2);
1119  1 assertSame(cds1Dss, dbRefEntry.getMap().getTo());
1120  1 MapList dnaToCdsMapping = new MapList(new int[] { 4, 6, 10, 12 },
1121    new int[] { 1, 6 }, 1, 1);
1122  1 assertEquals(dnaToCdsMapping, dbRefEntry.getMap().getMap());
1123  1 assertEquals(3, dna2.getDBRefs().size());
1124  1 dbRefEntry = dna2.getDBRefs().get(2);
1125  1 assertSame(cds2Dss, dbRefEntry.getMap().getTo());
1126  1 dnaToCdsMapping = new MapList(new int[] { 1, 3, 7, 9, 13, 15 },
1127    new int[] { 1, 9 }, 1, 1);
1128  1 assertEquals(dnaToCdsMapping, dbRefEntry.getMap().getMap());
1129   
1130    /*
1131    * verify CDS has added a dbref with mapping to cDNA
1132    */
1133  1 assertEquals(2, cds1Dss.getDBRefs().size());
1134  1 dbRefEntry = cds1Dss.getDBRefs().get(1);
1135  1 assertSame(dna1.getDatasetSequence(), dbRefEntry.getMap().getTo());
1136  1 MapList cdsToDnaMapping = new MapList(new int[] { 1, 6 }, new int[] {
1137    4, 6, 10, 12 }, 1, 1);
1138  1 assertEquals(cdsToDnaMapping, dbRefEntry.getMap().getMap());
1139  1 assertEquals(2, cds2Dss.getDBRefs().size());
1140  1 dbRefEntry = cds2Dss.getDBRefs().get(1);
1141  1 assertSame(dna2.getDatasetSequence(), dbRefEntry.getMap().getTo());
1142  1 cdsToDnaMapping = new MapList(new int[] { 1, 9 }, new int[] { 1, 3, 7,
1143    9, 13, 15 }, 1, 1);
1144  1 assertEquals(cdsToDnaMapping, dbRefEntry.getMap().getMap());
1145   
1146    /*
1147    * Verify mappings from CDS to peptide, cDNA to CDS, and cDNA to peptide
1148    * the mappings are on the shared alignment dataset
1149    * 6 mappings, 2*(DNA->CDS), 2*(DNA->Pep), 2*(CDS->Pep)
1150    */
1151  1 List<AlignedCodonFrame> cdsMappings = cds.getDataset().getCodonFrames();
1152  1 assertEquals(6, cdsMappings.size());
1153   
1154    /*
1155    * verify that mapping sets for dna and cds alignments are different
1156    * [not current behaviour - all mappings are on the alignment dataset]
1157    */
1158    // select -> subselect type to test.
1159    // Assert.assertNotSame(dna.getCodonFrames(), cds.getCodonFrames());
1160    // assertEquals(4, dna.getCodonFrames().size());
1161    // assertEquals(4, cds.getCodonFrames().size());
1162   
1163    /*
1164    * Two mappings involve pep1 (dna to pep1, cds to pep1)
1165    * Mapping from pep1 to GGGTTT in first new exon sequence
1166    */
1167  1 List<AlignedCodonFrame> pep1Mappings = MappingUtils
1168    .findMappingsForSequence(pep1, cdsMappings);
1169  1 assertEquals(2, pep1Mappings.size());
1170  1 List<AlignedCodonFrame> mappings = MappingUtils
1171    .findMappingsForSequence(cds.getSequenceAt(0), pep1Mappings);
1172  1 assertEquals(1, mappings.size());
1173   
1174    // map G to GGG
1175  1 SearchResultsI sr = MappingUtils.buildSearchResults(pep1, 1, mappings);
1176  1 assertEquals(1, sr.getResults().size());
1177  1 SearchResultMatchI m = sr.getResults().get(0);
1178  1 assertSame(cds1Dss, m.getSequence());
1179  1 assertEquals(1, m.getStart());
1180  1 assertEquals(3, m.getEnd());
1181    // map F to TTT
1182  1 sr = MappingUtils.buildSearchResults(pep1, 2, mappings);
1183  1 m = sr.getResults().get(0);
1184  1 assertSame(cds1Dss, m.getSequence());
1185  1 assertEquals(4, m.getStart());
1186  1 assertEquals(6, m.getEnd());
1187   
1188    /*
1189    * Two mappings involve pep2 (dna to pep2, cds to pep2)
1190    * Verify mapping from pep2 to GGGTTTCCC in second new exon sequence
1191    */
1192  1 List<AlignedCodonFrame> pep2Mappings = MappingUtils
1193    .findMappingsForSequence(pep2, cdsMappings);
1194  1 assertEquals(2, pep2Mappings.size());
1195  1 mappings = MappingUtils.findMappingsForSequence(cds.getSequenceAt(1),
1196    pep2Mappings);
1197  1 assertEquals(1, mappings.size());
1198    // map G to GGG
1199  1 sr = MappingUtils.buildSearchResults(pep2, 1, mappings);
1200  1 assertEquals(1, sr.getResults().size());
1201  1 m = sr.getResults().get(0);
1202  1 assertSame(cds2Dss, m.getSequence());
1203  1 assertEquals(1, m.getStart());
1204  1 assertEquals(3, m.getEnd());
1205    // map F to TTT
1206  1 sr = MappingUtils.buildSearchResults(pep2, 2, mappings);
1207  1 m = sr.getResults().get(0);
1208  1 assertSame(cds2Dss, m.getSequence());
1209  1 assertEquals(4, m.getStart());
1210  1 assertEquals(6, m.getEnd());
1211    // map P to CCC
1212  1 sr = MappingUtils.buildSearchResults(pep2, 3, mappings);
1213  1 m = sr.getResults().get(0);
1214  1 assertSame(cds2Dss, m.getSequence());
1215  1 assertEquals(7, m.getStart());
1216  1 assertEquals(9, m.getEnd());
1217   
1218    /*
1219    * check cds2 acquired a variant feature in position 5
1220    */
1221  1 List<SequenceFeature> sfs = cds2Dss.getSequenceFeatures();
1222  1 assertNotNull(sfs);
1223  1 assertEquals(1, sfs.size());
1224  1 assertEquals("variant", sfs.get(0).type);
1225  1 assertEquals(5, sfs.get(0).begin);
1226  1 assertEquals(5, sfs.get(0).end);
1227    }
1228   
1229    /**
1230    * Test the method that makes a cds-only alignment from a DNA sequence and its
1231    * product mappings, for the case where there are multiple exon mappings to
1232    * different protein products.
1233    */
 
1234  1 toggle @Test(groups = { "Functional" })
1235    public void testMakeCdsAlignment_multipleProteins()
1236    {
1237  1 SequenceI dna1 = new Sequence("dna1", "aaaGGGcccTTTaaa");
1238  1 SequenceI pep1 = new Sequence("pep1", "GF"); // GGGTTT
1239  1 SequenceI pep2 = new Sequence("pep2", "KP"); // aaaccc
1240  1 SequenceI pep3 = new Sequence("pep3", "KF"); // aaaTTT
1241  1 dna1.createDatasetSequence();
1242  1 pep1.createDatasetSequence();
1243  1 pep2.createDatasetSequence();
1244  1 pep3.createDatasetSequence();
1245  1 pep1.getDatasetSequence().addDBRef(
1246    new DBRefEntry("EMBLCDS", "2", "A12345"));
1247  1 pep2.getDatasetSequence().addDBRef(
1248    new DBRefEntry("EMBLCDS", "3", "A12346"));
1249  1 pep3.getDatasetSequence().addDBRef(
1250    new DBRefEntry("EMBLCDS", "4", "A12347"));
1251   
1252    /*
1253    * Create the CDS alignment
1254    */
1255  1 AlignmentI dna = new Alignment(new SequenceI[] { dna1 });
1256  1 dna.setDataset(null);
1257   
1258    /*
1259    * Make the mappings from dna to protein
1260    */
1261    // map ...GGG...TTT to GF
1262  1 MapList map = new MapList(new int[] { 4, 6, 10, 12 },
1263    new int[] { 1, 2 }, 3, 1);
1264  1 AlignedCodonFrame acf = new AlignedCodonFrame();
1265  1 acf.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(), map);
1266  1 dna.addCodonFrame(acf);
1267   
1268    // map aaa...ccc to KP
1269  1 map = new MapList(new int[] { 1, 3, 7, 9 }, new int[] { 1, 2 }, 3, 1);
1270  1 acf = new AlignedCodonFrame();
1271  1 acf.addMap(dna1.getDatasetSequence(), pep2.getDatasetSequence(), map);
1272  1 dna.addCodonFrame(acf);
1273   
1274    // map aaa......TTT to KF
1275  1 map = new MapList(new int[] { 1, 3, 10, 12 }, new int[] { 1, 2 }, 3, 1);
1276  1 acf = new AlignedCodonFrame();
1277  1 acf.addMap(dna1.getDatasetSequence(), pep3.getDatasetSequence(), map);
1278  1 dna.addCodonFrame(acf);
1279   
1280    /*
1281    * execute method under test
1282    */
1283  1 AlignmentI cdsal = AlignmentUtils.makeCdsAlignment(
1284    new SequenceI[] { dna1 }, dna.getDataset(), null);
1285   
1286    /*
1287    * Verify we have 3 cds sequences, mapped to pep1/2/3 respectively
1288    */
1289  1 List<SequenceI> cds = cdsal.getSequences();
1290  1 assertEquals(3, cds.size());
1291   
1292    /*
1293    * verify shared, extended alignment dataset
1294    */
1295  1 assertSame(cdsal.getDataset(), dna.getDataset());
1296  1 assertTrue(dna.getDataset().getSequences()
1297    .contains(cds.get(0).getDatasetSequence()));
1298  1 assertTrue(dna.getDataset().getSequences()
1299    .contains(cds.get(1).getDatasetSequence()));
1300  1 assertTrue(dna.getDataset().getSequences()
1301    .contains(cds.get(2).getDatasetSequence()));
1302   
1303    /*
1304    * verify aligned cds sequences and their xrefs
1305    */
1306  1 SequenceI cdsSeq = cds.get(0);
1307  1 assertEquals("GGGTTT", cdsSeq.getSequenceAsString());
1308    // assertEquals("dna1|A12345", cdsSeq.getName());
1309  1 assertEquals("CDS|dna1", cdsSeq.getName());
1310    // assertEquals(1, cdsSeq.getDBRefs().length);
1311    // DBRefEntry cdsRef = cdsSeq.getDBRefs()[0];
1312    // assertEquals("EMBLCDS", cdsRef.getSource());
1313    // assertEquals("2", cdsRef.getVersion());
1314    // assertEquals("A12345", cdsRef.getAccessionId());
1315   
1316  1 cdsSeq = cds.get(1);
1317  1 assertEquals("aaaccc", cdsSeq.getSequenceAsString());
1318    // assertEquals("dna1|A12346", cdsSeq.getName());
1319  1 assertEquals("CDS|dna1", cdsSeq.getName());
1320    // assertEquals(1, cdsSeq.getDBRefs().length);
1321    // cdsRef = cdsSeq.getDBRefs()[0];
1322    // assertEquals("EMBLCDS", cdsRef.getSource());
1323    // assertEquals("3", cdsRef.getVersion());
1324    // assertEquals("A12346", cdsRef.getAccessionId());
1325   
1326  1 cdsSeq = cds.get(2);
1327  1 assertEquals("aaaTTT", cdsSeq.getSequenceAsString());
1328    // assertEquals("dna1|A12347", cdsSeq.getName());
1329  1 assertEquals("CDS|dna1", cdsSeq.getName());
1330    // assertEquals(1, cdsSeq.getDBRefs().length);
1331    // cdsRef = cdsSeq.getDBRefs()[0];
1332    // assertEquals("EMBLCDS", cdsRef.getSource());
1333    // assertEquals("4", cdsRef.getVersion());
1334    // assertEquals("A12347", cdsRef.getAccessionId());
1335   
1336    /*
1337    * Verify there are mappings from each cds sequence to its protein product
1338    * and also to its dna source
1339    */
1340  1 List<AlignedCodonFrame> newMappings = cdsal.getCodonFrames();
1341   
1342    /*
1343    * 6 mappings involve dna1 (to pep1/2/3, cds1/2/3)
1344    */
1345  1 List<AlignedCodonFrame> dnaMappings = MappingUtils
1346    .findMappingsForSequence(dna1, newMappings);
1347  1 assertEquals(6, dnaMappings.size());
1348   
1349    /*
1350    * dna1 to pep1
1351    */
1352  1 List<AlignedCodonFrame> mappings = MappingUtils
1353    .findMappingsForSequence(pep1, dnaMappings);
1354  1 assertEquals(1, mappings.size());
1355  1 assertEquals(1, mappings.get(0).getMappings().size());
1356  1 assertSame(pep1.getDatasetSequence(), mappings.get(0).getMappings()
1357    .get(0).getMapping().getTo());
1358   
1359    /*
1360    * dna1 to cds1
1361    */
1362  1 List<AlignedCodonFrame> dnaToCds1Mappings = MappingUtils
1363    .findMappingsForSequence(cds.get(0), dnaMappings);
1364  1 Mapping mapping = dnaToCds1Mappings.get(0).getMappings().get(0)
1365    .getMapping();
1366  1 assertSame(cds.get(0).getDatasetSequence(), mapping.getTo());
1367  1 assertEquals("G(1) in CDS should map to G(4) in DNA", 4, mapping
1368    .getMap().getToPosition(1));
1369   
1370    /*
1371    * dna1 to pep2
1372    */
1373  1 mappings = MappingUtils.findMappingsForSequence(pep2, dnaMappings);
1374  1 assertEquals(1, mappings.size());
1375  1 assertEquals(1, mappings.get(0).getMappings().size());
1376  1 assertSame(pep2.getDatasetSequence(), mappings.get(0).getMappings()
1377    .get(0).getMapping().getTo());
1378   
1379    /*
1380    * dna1 to cds2
1381    */
1382  1 List<AlignedCodonFrame> dnaToCds2Mappings = MappingUtils
1383    .findMappingsForSequence(cds.get(1), dnaMappings);
1384  1 mapping = dnaToCds2Mappings.get(0).getMappings().get(0).getMapping();
1385  1 assertSame(cds.get(1).getDatasetSequence(), mapping.getTo());
1386  1 assertEquals("c(4) in CDS should map to c(7) in DNA", 7, mapping
1387    .getMap().getToPosition(4));
1388   
1389    /*
1390    * dna1 to pep3
1391    */
1392  1 mappings = MappingUtils.findMappingsForSequence(pep3, dnaMappings);
1393  1 assertEquals(1, mappings.size());
1394  1 assertEquals(1, mappings.get(0).getMappings().size());
1395  1 assertSame(pep3.getDatasetSequence(), mappings.get(0).getMappings()
1396    .get(0).getMapping().getTo());
1397   
1398    /*
1399    * dna1 to cds3
1400    */
1401  1 List<AlignedCodonFrame> dnaToCds3Mappings = MappingUtils
1402    .findMappingsForSequence(cds.get(2), dnaMappings);
1403  1 mapping = dnaToCds3Mappings.get(0).getMappings().get(0).getMapping();
1404  1 assertSame(cds.get(2).getDatasetSequence(), mapping.getTo());
1405  1 assertEquals("T(4) in CDS should map to T(10) in DNA", 10, mapping
1406    .getMap().getToPosition(4));
1407    }
1408   
 
1409  1 toggle @Test(groups = { "Functional" })
1410    public void testIsMappable()
1411    {
1412  1 SequenceI dna1 = new Sequence("dna1", "cgCAGtgGT");
1413  1 SequenceI aa1 = new Sequence("aa1", "RSG");
1414  1 AlignmentI al1 = new Alignment(new SequenceI[] { dna1 });
1415  1 AlignmentI al2 = new Alignment(new SequenceI[] { aa1 });
1416   
1417  1 assertFalse(AlignmentUtils.isMappable(null, null));
1418  1 assertFalse(AlignmentUtils.isMappable(al1, null));
1419  1 assertFalse(AlignmentUtils.isMappable(null, al1));
1420  1 assertFalse(AlignmentUtils.isMappable(al1, al1));
1421  1 assertFalse(AlignmentUtils.isMappable(al2, al2));
1422   
1423  1 assertTrue(AlignmentUtils.isMappable(al1, al2));
1424  1 assertTrue(AlignmentUtils.isMappable(al2, al1));
1425    }
1426   
1427    /**
1428    * Test creating a mapping when the sequences involved do not start at residue
1429    * 1
1430    *
1431    * @throws IOException
1432    */
 
1433  1 toggle @Test(groups = { "Functional" })
1434    public void testMapCdnaToProtein_forSubsequence() throws IOException
1435    {
1436  1 SequenceI prot = new Sequence("UNIPROT|V12345", "E-I--Q", 10, 12);
1437  1 prot.createDatasetSequence();
1438   
1439  1 SequenceI dna = new Sequence("EMBL|A33333", "GAA--AT-C-CAG", 40, 48);
1440  1 dna.createDatasetSequence();
1441   
1442  1 MapList map = AlignmentUtils.mapCdnaToProtein(prot, dna);
1443  1 assertEquals(10, map.getToLowest());
1444  1 assertEquals(12, map.getToHighest());
1445  1 assertEquals(40, map.getFromLowest());
1446  1 assertEquals(48, map.getFromHighest());
1447    }
1448   
1449    /**
1450    * Test for the alignSequenceAs method where we have protein mapped to protein
1451    */
 
1452  1 toggle @Test(groups = { "Functional" })
1453    public void testAlignSequenceAs_mappedProteinProtein()
1454    {
1455   
1456  1 SequenceI alignMe = new Sequence("Match", "MGAASEV");
1457  1 alignMe.createDatasetSequence();
1458  1 SequenceI alignFrom = new Sequence("Query", "LQTGYMGAASEVMFSPTRR");
1459  1 alignFrom.createDatasetSequence();
1460   
1461  1 AlignedCodonFrame acf = new AlignedCodonFrame();
1462    // this is like a domain or motif match of part of a peptide sequence
1463  1 MapList map = new MapList(new int[] { 6, 12 }, new int[] { 1, 7 }, 1, 1);
1464  1 acf.addMap(alignFrom.getDatasetSequence(),
1465    alignMe.getDatasetSequence(), map);
1466   
1467  1 AlignmentUtils.alignSequenceAs(alignMe, alignFrom, acf, "-", '-', true,
1468    true);
1469  1 assertEquals("-----MGAASEV-------", alignMe.getSequenceAsString());
1470    }
1471   
1472    /**
1473    * Test for the alignSequenceAs method where there are trailing unmapped
1474    * residues in the model sequence
1475    */
 
1476  1 toggle @Test(groups = { "Functional" })
1477    public void testAlignSequenceAs_withTrailingPeptide()
1478    {
1479    // map first 3 codons to KPF; G is a trailing unmapped residue
1480  1 MapList map = new MapList(new int[] { 1, 9 }, new int[] { 1, 3 }, 3, 1);
1481   
1482  1 checkAlignSequenceAs("AAACCCTTT", "K-PFG", true, true, map,
1483    "AAA---CCCTTT---");
1484    }
1485   
1486    /**
1487    * Tests for transferring features between mapped sequences
1488    */
 
1489  1 toggle @Test(groups = { "Functional" })
1490    public void testTransferFeatures()
1491    {
1492  1 SequenceI dna = new Sequence("dna/20-34", "acgTAGcaaGCCcgt");
1493  1 SequenceI cds = new Sequence("cds/10-15", "TAGGCC");
1494   
1495    // no overlap
1496  1 dna.addSequenceFeature(new SequenceFeature("type1", "desc1", 1, 2, 1f,
1497    null));
1498    // partial overlap - to [1, 1]
1499  1 dna.addSequenceFeature(new SequenceFeature("type2", "desc2", 3, 4, 2f,
1500    null));
1501    // exact overlap - to [1, 3]
1502  1 dna.addSequenceFeature(new SequenceFeature("type3", "desc3", 4, 6, 3f,
1503    null));
1504    // spanning overlap - to [2, 5]
1505  1 dna.addSequenceFeature(new SequenceFeature("type4", "desc4", 5, 11, 4f,
1506    null));
1507    // exactly overlaps whole mapped range [1, 6]
1508  1 dna.addSequenceFeature(new SequenceFeature("type5", "desc5", 4, 12, 5f,
1509    null));
1510    // no overlap (internal)
1511  1 dna.addSequenceFeature(new SequenceFeature("type6", "desc6", 7, 9, 6f,
1512    null));
1513    // no overlap (3' end)
1514  1 dna.addSequenceFeature(new SequenceFeature("type7", "desc7", 13, 15,
1515    7f, null));
1516    // overlap (3' end) - to [6, 6]
1517  1 dna.addSequenceFeature(new SequenceFeature("type8", "desc8", 12, 12,
1518    8f, null));
1519    // extended overlap - to [6, +]
1520  1 dna.addSequenceFeature(new SequenceFeature("type9", "desc9", 12, 13,
1521    9f, null));
1522   
1523  1 MapList map = new MapList(new int[] { 4, 6, 10, 12 },
1524    new int[] { 1, 6 }, 1, 1);
1525   
1526    /*
1527    * transferFeatures() will build 'partial overlap' for regions
1528    * that partially overlap 5' or 3' (start or end) of target sequence
1529    */
1530  1 AlignmentUtils.transferFeatures(dna, cds, map, null);
1531  1 List<SequenceFeature> sfs = cds.getSequenceFeatures();
1532  1 assertEquals(6, sfs.size());
1533   
1534  1 SequenceFeature sf = sfs.get(0);
1535  1 assertEquals("type2", sf.getType());
1536  1 assertEquals("desc2", sf.getDescription());
1537  1 assertEquals(2f, sf.getScore());
1538  1 assertEquals(1, sf.getBegin());
1539  1 assertEquals(1, sf.getEnd());
1540   
1541  1 sf = sfs.get(1);
1542  1 assertEquals("type3", sf.getType());
1543  1 assertEquals("desc3", sf.getDescription());
1544  1 assertEquals(3f, sf.getScore());
1545  1 assertEquals(1, sf.getBegin());
1546  1 assertEquals(3, sf.getEnd());
1547   
1548  1 sf = sfs.get(2);
1549  1 assertEquals("type4", sf.getType());
1550  1 assertEquals(2, sf.getBegin());
1551  1 assertEquals(5, sf.getEnd());
1552   
1553  1 sf = sfs.get(3);
1554  1 assertEquals("type5", sf.getType());
1555  1 assertEquals(1, sf.getBegin());
1556  1 assertEquals(6, sf.getEnd());
1557   
1558  1 sf = sfs.get(4);
1559  1 assertEquals("type8", sf.getType());
1560  1 assertEquals(6, sf.getBegin());
1561  1 assertEquals(6, sf.getEnd());
1562   
1563  1 sf = sfs.get(5);
1564  1 assertEquals("type9", sf.getType());
1565  1 assertEquals(6, sf.getBegin());
1566  1 assertEquals(6, sf.getEnd());
1567    }
1568   
1569    /**
1570    * Tests for transferring features between mapped sequences
1571    */
 
1572  1 toggle @Test(groups = { "Functional" })
1573    public void testTransferFeatures_withOmit()
1574    {
1575  1 SequenceI dna = new Sequence("dna/20-34", "acgTAGcaaGCCcgt");
1576  1 SequenceI cds = new Sequence("cds/10-15", "TAGGCC");
1577   
1578  1 MapList map = new MapList(new int[] { 4, 6, 10, 12 },
1579    new int[] { 1, 6 }, 1, 1);
1580   
1581    // [5, 11] maps to [2, 5]
1582  1 dna.addSequenceFeature(new SequenceFeature("type4", "desc4", 5, 11, 4f,
1583    null));
1584    // [4, 12] maps to [1, 6]
1585  1 dna.addSequenceFeature(new SequenceFeature("type5", "desc5", 4, 12, 5f,
1586    null));
1587    // [12, 12] maps to [6, 6]
1588  1 dna.addSequenceFeature(new SequenceFeature("type8", "desc8", 12, 12,
1589    8f, null));
1590   
1591    // desc4 and desc8 are the 'omit these' varargs
1592  1 AlignmentUtils.transferFeatures(dna, cds, map, null, "type4", "type8");
1593  1 List<SequenceFeature> sfs = cds.getSequenceFeatures();
1594  1 assertEquals(1, sfs.size());
1595   
1596  1 SequenceFeature sf = sfs.get(0);
1597  1 assertEquals("type5", sf.getType());
1598  1 assertEquals(1, sf.getBegin());
1599  1 assertEquals(6, sf.getEnd());
1600    }
1601   
1602    /**
1603    * Tests for transferring features between mapped sequences
1604    */
 
1605  1 toggle @Test(groups = { "Functional" })
1606    public void testTransferFeatures_withSelect()
1607    {
1608  1 SequenceI dna = new Sequence("dna/20-34", "acgTAGcaaGCCcgt");
1609  1 SequenceI cds = new Sequence("cds/10-15", "TAGGCC");
1610   
1611  1 MapList map = new MapList(new int[] { 4, 6, 10, 12 },
1612    new int[] { 1, 6 }, 1, 1);
1613   
1614    // [5, 11] maps to [2, 5]
1615  1 dna.addSequenceFeature(new SequenceFeature("type4", "desc4", 5, 11, 4f,
1616    null));
1617    // [4, 12] maps to [1, 6]
1618  1 dna.addSequenceFeature(new SequenceFeature("type5", "desc5", 4, 12, 5f,
1619    null));
1620    // [12, 12] maps to [6, 6]
1621  1 dna.addSequenceFeature(new SequenceFeature("type8", "desc8", 12, 12,
1622    8f, null));
1623   
1624    // "type5" is the 'select this type' argument
1625  1 AlignmentUtils.transferFeatures(dna, cds, map, "type5");
1626  1 List<SequenceFeature> sfs = cds.getSequenceFeatures();
1627  1 assertEquals(1, sfs.size());
1628   
1629  1 SequenceFeature sf = sfs.get(0);
1630  1 assertEquals("type5", sf.getType());
1631  1 assertEquals(1, sf.getBegin());
1632  1 assertEquals(6, sf.getEnd());
1633    }
1634   
1635    /**
1636    * Test the method that extracts the cds-only part of a dna alignment, for the
1637    * case where the cds should be aligned to match its nucleotide sequence.
1638    */
 
1639  1 toggle @Test(groups = { "Functional" })
1640    public void testMakeCdsAlignment_alternativeTranscripts()
1641    {
1642  1 SequenceI dna1 = new Sequence("dna1", "aaaGGGCC-----CTTTaaaGGG");
1643    // alternative transcript of same dna skips CCC codon
1644  1 SequenceI dna2 = new Sequence("dna2", "aaaGGGCC-----cttTaaaGGG");
1645    // dna3 has no mapping (protein product) so should be ignored here
1646  1 SequenceI dna3 = new Sequence("dna3", "aaaGGGCCCCCGGGcttTaaaGGG");
1647  1 SequenceI pep1 = new Sequence("pep1", "GPFG");
1648  1 SequenceI pep2 = new Sequence("pep2", "GPG");
1649  1 dna1.createDatasetSequence();
1650  1 dna2.createDatasetSequence();
1651  1 dna3.createDatasetSequence();
1652  1 pep1.createDatasetSequence();
1653  1 pep2.createDatasetSequence();
1654   
1655  1 AlignmentI dna = new Alignment(new SequenceI[] { dna1, dna2, dna3 });
1656  1 dna.setDataset(null);
1657   
1658  1 MapList map = new MapList(new int[] { 4, 12, 16, 18 },
1659    new int[] { 1, 4 }, 3, 1);
1660  1 AlignedCodonFrame acf = new AlignedCodonFrame();
1661  1 acf.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(), map);
1662  1 dna.addCodonFrame(acf);
1663  1 map = new MapList(new int[] { 4, 8, 12, 12, 16, 18 },
1664    new int[] { 1, 3 }, 3, 1);
1665  1 acf = new AlignedCodonFrame();
1666  1 acf.addMap(dna2.getDatasetSequence(), pep2.getDatasetSequence(), map);
1667  1 dna.addCodonFrame(acf);
1668   
1669  1 AlignmentI cds = AlignmentUtils.makeCdsAlignment(new SequenceI[] {
1670    dna1, dna2, dna3 }, dna.getDataset(), null);
1671  1 List<SequenceI> cdsSeqs = cds.getSequences();
1672  1 assertEquals(2, cdsSeqs.size());
1673  1 assertEquals("GGGCCCTTTGGG", cdsSeqs.get(0).getSequenceAsString());
1674  1 assertEquals("GGGCCTGGG", cdsSeqs.get(1).getSequenceAsString());
1675   
1676    /*
1677    * verify shared, extended alignment dataset
1678    */
1679  1 assertSame(dna.getDataset(), cds.getDataset());
1680  1 assertTrue(dna.getDataset().getSequences()
1681    .contains(cdsSeqs.get(0).getDatasetSequence()));
1682  1 assertTrue(dna.getDataset().getSequences()
1683    .contains(cdsSeqs.get(1).getDatasetSequence()));
1684   
1685    /*
1686    * Verify 6 mappings: dna1 to cds1, cds1 to pep1, dna1 to pep1
1687    * and the same for dna2/cds2/pep2
1688    */
1689  1 List<AlignedCodonFrame> mappings = cds.getCodonFrames();
1690  1 assertEquals(6, mappings.size());
1691   
1692    /*
1693    * 2 mappings involve pep1
1694    */
1695  1 List<AlignedCodonFrame> pep1Mappings = MappingUtils
1696    .findMappingsForSequence(pep1, mappings);
1697  1 assertEquals(2, pep1Mappings.size());
1698   
1699    /*
1700    * Get mapping of pep1 to cds1 and verify it
1701    * maps GPFG to 1-3,4-6,7-9,10-12
1702    */
1703  1 List<AlignedCodonFrame> pep1CdsMappings = MappingUtils
1704    .findMappingsForSequence(cds.getSequenceAt(0), pep1Mappings);
1705  1 assertEquals(1, pep1CdsMappings.size());
1706  1 SearchResultsI sr = MappingUtils.buildSearchResults(pep1, 1,
1707    pep1CdsMappings);
1708  1 assertEquals(1, sr.getResults().size());
1709  1 SearchResultMatchI m = sr.getResults().get(0);
1710  1 assertEquals(cds.getSequenceAt(0).getDatasetSequence(), m.getSequence());
1711  1 assertEquals(1, m.getStart());
1712  1 assertEquals(3, m.getEnd());
1713  1 sr = MappingUtils.buildSearchResults(pep1, 2, pep1CdsMappings);
1714  1 m = sr.getResults().get(0);
1715  1 assertEquals(4, m.getStart());
1716  1 assertEquals(6, m.getEnd());
1717  1 sr = MappingUtils.buildSearchResults(pep1, 3, pep1CdsMappings);
1718  1 m = sr.getResults().get(0);
1719  1 assertEquals(7, m.getStart());
1720  1 assertEquals(9, m.getEnd());
1721  1 sr = MappingUtils.buildSearchResults(pep1, 4, pep1CdsMappings);
1722  1 m = sr.getResults().get(0);
1723  1 assertEquals(10, m.getStart());
1724  1 assertEquals(12, m.getEnd());
1725   
1726    /*
1727    * Get mapping of pep2 to cds2 and verify it
1728    * maps GPG in pep2 to 1-3,4-6,7-9 in second CDS sequence
1729    */
1730  1 List<AlignedCodonFrame> pep2Mappings = MappingUtils
1731    .findMappingsForSequence(pep2, mappings);
1732  1 assertEquals(2, pep2Mappings.size());
1733  1 List<AlignedCodonFrame> pep2CdsMappings = MappingUtils
1734    .findMappingsForSequence(cds.getSequenceAt(1), pep2Mappings);
1735  1 assertEquals(1, pep2CdsMappings.size());
1736  1 sr = MappingUtils.buildSearchResults(pep2, 1, pep2CdsMappings);
1737  1 assertEquals(1, sr.getResults().size());
1738  1 m = sr.getResults().get(0);
1739  1 assertEquals(cds.getSequenceAt(1).getDatasetSequence(), m.getSequence());
1740  1 assertEquals(1, m.getStart());
1741  1 assertEquals(3, m.getEnd());
1742  1 sr = MappingUtils.buildSearchResults(pep2, 2, pep2CdsMappings);
1743  1 m = sr.getResults().get(0);
1744  1 assertEquals(4, m.getStart());
1745  1 assertEquals(6, m.getEnd());
1746  1 sr = MappingUtils.buildSearchResults(pep2, 3, pep2CdsMappings);
1747  1 m = sr.getResults().get(0);
1748  1 assertEquals(7, m.getStart());
1749  1 assertEquals(9, m.getEnd());
1750    }
1751   
1752    /**
1753    * Test the method that realigns protein to match mapped codon alignment.
1754    */
 
1755  1 toggle @Test(groups = { "Functional" })
1756    public void testAlignProteinAsDna_incompleteStartCodon()
1757    {
1758    // seq1: incomplete start codon (not mapped), then [3, 11]
1759  1 SequenceI dna1 = new Sequence("Seq1", "ccAAA-TTT-GGG-");
1760    // seq2 codons are [4, 5], [8, 11]
1761  1 SequenceI dna2 = new Sequence("Seq2", "ccaAA-ttT-GGG-");
1762    // seq3 incomplete start codon at 'tt'
1763  1 SequenceI dna3 = new Sequence("Seq3", "ccaaa-ttt-GGG-");
1764  1 AlignmentI dna = new Alignment(new SequenceI[] { dna1, dna2, dna3 });
1765  1 dna.setDataset(null);
1766   
1767    // prot1 has 'X' for incomplete start codon (not mapped)
1768  1 SequenceI prot1 = new Sequence("Seq1", "XKFG"); // X for incomplete start
1769  1 SequenceI prot2 = new Sequence("Seq2", "NG");
1770  1 SequenceI prot3 = new Sequence("Seq3", "XG"); // X for incomplete start
1771  1 AlignmentI protein = new Alignment(new SequenceI[] { prot1, prot2,
1772    prot3 });
1773  1 protein.setDataset(null);
1774   
1775    // map dna1 [3, 11] to prot1 [2, 4] KFG
1776  1 MapList map = new MapList(new int[] { 3, 11 }, new int[] { 2, 4 }, 3, 1);
1777  1 AlignedCodonFrame acf = new AlignedCodonFrame();
1778  1 acf.addMap(dna1.getDatasetSequence(), prot1.getDatasetSequence(), map);
1779   
1780    // map dna2 [4, 5] [8, 11] to prot2 [1, 2] NG
1781  1 map = new MapList(new int[] { 4, 5, 8, 11 }, new int[] { 1, 2 }, 3, 1);
1782  1 acf.addMap(dna2.getDatasetSequence(), prot2.getDatasetSequence(), map);
1783   
1784    // map dna3 [9, 11] to prot3 [2, 2] G
1785  1 map = new MapList(new int[] { 9, 11 }, new int[] { 2, 2 }, 3, 1);
1786  1 acf.addMap(dna3.getDatasetSequence(), prot3.getDatasetSequence(), map);
1787   
1788  1 ArrayList<AlignedCodonFrame> acfs = new ArrayList<>();
1789  1 acfs.add(acf);
1790  1 protein.setCodonFrames(acfs);
1791   
1792    /*
1793    * verify X is included in the aligned proteins, and placed just
1794    * before the first mapped residue
1795    * CCT is between CCC and TTT
1796    */
1797  1 AlignmentUtils.alignProteinAsDna(protein, dna);
1798  1 assertEquals("XK-FG", prot1.getSequenceAsString());
1799  1 assertEquals("--N-G", prot2.getSequenceAsString());
1800  1 assertEquals("---XG", prot3.getSequenceAsString());
1801    }
1802   
1803    /**
1804    * Tests for the method that maps the subset of a dna sequence that has CDS
1805    * (or subtype) feature - case where the start codon is incomplete.
1806    */
 
1807  1 toggle @Test(groups = "Functional")
1808    public void testFindCdsPositions_fivePrimeIncomplete()
1809    {
1810  1 SequenceI dnaSeq = new Sequence("dna", "aaagGGCCCaaaTTTttt");
1811  1 dnaSeq.createDatasetSequence();
1812  1 SequenceI ds = dnaSeq.getDatasetSequence();
1813   
1814    // CDS for dna 5-6 (incomplete codon), 7-9
1815  1 SequenceFeature sf = new SequenceFeature("CDS", "", 5, 9, 0f, null);
1816  1 sf.setPhase("2"); // skip 2 bases to start of next codon
1817  1 ds.addSequenceFeature(sf);
1818    // CDS for dna 13-15
1819  1 sf = new SequenceFeature("CDS_predicted", "", 13, 15, 0f, null);
1820  1 ds.addSequenceFeature(sf);
1821   
1822  1 List<int[]> ranges = AlignmentUtils.findCdsPositions(dnaSeq);
1823   
1824    /*
1825    * check the mapping starts with the first complete codon
1826    */
1827  1 assertEquals(6, MappingUtils.getLength(ranges));
1828  1 assertEquals(2, ranges.size());
1829  1 assertEquals(7, ranges.get(0)[0]);
1830  1 assertEquals(9, ranges.get(0)[1]);
1831  1 assertEquals(13, ranges.get(1)[0]);
1832  1 assertEquals(15, ranges.get(1)[1]);
1833    }
1834   
1835    /**
1836    * Tests for the method that maps the subset of a dna sequence that has CDS
1837    * (or subtype) feature.
1838    */
 
1839  1 toggle @Test(groups = "Functional")
1840    public void testFindCdsPositions()
1841    {
1842  1 SequenceI dnaSeq = new Sequence("dna", "aaaGGGcccAAATTTttt");
1843  1 dnaSeq.createDatasetSequence();
1844  1 SequenceI ds = dnaSeq.getDatasetSequence();
1845   
1846    // CDS for dna 10-12
1847  1 SequenceFeature sf = new SequenceFeature("CDS_predicted", "", 10, 12,
1848    0f, null);
1849  1 sf.setStrand("+");
1850  1 ds.addSequenceFeature(sf);
1851    // CDS for dna 4-6
1852  1 sf = new SequenceFeature("CDS", "", 4, 6, 0f, null);
1853  1 sf.setStrand("+");
1854  1 ds.addSequenceFeature(sf);
1855    // exon feature should be ignored here
1856  1 sf = new SequenceFeature("exon", "", 7, 9, 0f, null);
1857  1 ds.addSequenceFeature(sf);
1858   
1859  1 List<int[]> ranges = AlignmentUtils.findCdsPositions(dnaSeq);
1860    /*
1861    * verify ranges { [4-6], [12-10] }
1862    * note CDS ranges are ordered ascending even if the CDS
1863    * features are not
1864    */
1865  1 assertEquals(6, MappingUtils.getLength(ranges));
1866  1 assertEquals(2, ranges.size());
1867  1 assertEquals(4, ranges.get(0)[0]);
1868  1 assertEquals(6, ranges.get(0)[1]);
1869  1 assertEquals(10, ranges.get(1)[0]);
1870  1 assertEquals(12, ranges.get(1)[1]);
1871    }
1872   
1873    /**
1874    * Tests for the method that maps the subset of a dna sequence that has CDS
1875    * (or subtype) feature, with CDS strand = '-' (reverse)
1876    */
1877    // test turned off as currently findCdsPositions is not strand-dependent
1878    // left in case it comes around again...
 
1879  0 toggle @Test(groups = "Functional", enabled = false)
1880    public void testFindCdsPositions_reverseStrand()
1881    {
1882  0 SequenceI dnaSeq = new Sequence("dna", "aaaGGGcccAAATTTttt");
1883  0 dnaSeq.createDatasetSequence();
1884  0 SequenceI ds = dnaSeq.getDatasetSequence();
1885   
1886    // CDS for dna 4-6
1887  0 SequenceFeature sf = new SequenceFeature("CDS", "", 4, 6, 0f, null);
1888  0 sf.setStrand("-");
1889  0 ds.addSequenceFeature(sf);
1890    // exon feature should be ignored here
1891  0 sf = new SequenceFeature("exon", "", 7, 9, 0f, null);
1892  0 ds.addSequenceFeature(sf);
1893    // CDS for dna 10-12
1894  0 sf = new SequenceFeature("CDS_predicted", "", 10, 12, 0f, null);
1895  0 sf.setStrand("-");
1896  0 ds.addSequenceFeature(sf);
1897   
1898  0 List<int[]> ranges = AlignmentUtils.findCdsPositions(dnaSeq);
1899    /*
1900    * verify ranges { [12-10], [6-4] }
1901    */
1902  0 assertEquals(6, MappingUtils.getLength(ranges));
1903  0 assertEquals(2, ranges.size());
1904  0 assertEquals(12, ranges.get(0)[0]);
1905  0 assertEquals(10, ranges.get(0)[1]);
1906  0 assertEquals(6, ranges.get(1)[0]);
1907  0 assertEquals(4, ranges.get(1)[1]);
1908    }
1909   
1910    /**
1911    * Tests for the method that maps the subset of a dna sequence that has CDS
1912    * (or subtype) feature - reverse strand case where the start codon is
1913    * incomplete.
1914    */
 
1915  0 toggle @Test(groups = "Functional", enabled = false)
1916    // test turned off as currently findCdsPositions is not strand-dependent
1917    // left in case it comes around again...
1918    public void testFindCdsPositions_reverseStrandThreePrimeIncomplete()
1919    {
1920  0 SequenceI dnaSeq = new Sequence("dna", "aaagGGCCCaaaTTTttt");
1921  0 dnaSeq.createDatasetSequence();
1922  0 SequenceI ds = dnaSeq.getDatasetSequence();
1923   
1924    // CDS for dna 5-9
1925  0 SequenceFeature sf = new SequenceFeature("CDS", "", 5, 9, 0f, null);
1926  0 sf.setStrand("-");
1927  0 ds.addSequenceFeature(sf);
1928    // CDS for dna 13-15
1929  0 sf = new SequenceFeature("CDS_predicted", "", 13, 15, 0f, null);
1930  0 sf.setStrand("-");
1931  0 sf.setPhase("2"); // skip 2 bases to start of next codon
1932  0 ds.addSequenceFeature(sf);
1933   
1934  0 List<int[]> ranges = AlignmentUtils.findCdsPositions(dnaSeq);
1935   
1936    /*
1937    * check the mapping starts with the first complete codon
1938    * expect ranges [13, 13], [9, 5]
1939    */
1940  0 assertEquals(6, MappingUtils.getLength(ranges));
1941  0 assertEquals(2, ranges.size());
1942  0 assertEquals(13, ranges.get(0)[0]);
1943  0 assertEquals(13, ranges.get(0)[1]);
1944  0 assertEquals(9, ranges.get(1)[0]);
1945  0 assertEquals(5, ranges.get(1)[1]);
1946    }
1947   
 
1948  1 toggle @Test(groups = "Functional")
1949    public void testAlignAs_alternateTranscriptsUngapped()
1950    {
1951  1 SequenceI dna1 = new Sequence("dna1", "cccGGGTTTaaa");
1952  1 SequenceI dna2 = new Sequence("dna2", "CCCgggtttAAA");
1953  1 AlignmentI dna = new Alignment(new SequenceI[] { dna1, dna2 });
1954  1 ((Alignment) dna).createDatasetAlignment();
1955  1 SequenceI cds1 = new Sequence("cds1", "GGGTTT");
1956  1 SequenceI cds2 = new Sequence("cds2", "CCCAAA");
1957  1 AlignmentI cds = new Alignment(new SequenceI[] { cds1, cds2 });
1958  1 ((Alignment) cds).createDatasetAlignment();
1959   
1960  1 AlignedCodonFrame acf = new AlignedCodonFrame();
1961  1 MapList map = new MapList(new int[] { 4, 9 }, new int[] { 1, 6 }, 1, 1);
1962  1 acf.addMap(dna1.getDatasetSequence(), cds1.getDatasetSequence(), map);
1963  1 map = new MapList(new int[] { 1, 3, 10, 12 }, new int[] { 1, 6 }, 1, 1);
1964  1 acf.addMap(dna2.getDatasetSequence(), cds2.getDatasetSequence(), map);
1965   
1966    /*
1967    * verify CDS alignment is as:
1968    * cccGGGTTTaaa (cdna)
1969    * CCCgggtttAAA (cdna)
1970    *
1971    * ---GGGTTT--- (cds)
1972    * CCC------AAA (cds)
1973    */
1974  1 dna.addCodonFrame(acf);
1975  1 AlignmentUtils.alignAs(cds, dna);
1976  1 assertEquals("---GGGTTT", cds.getSequenceAt(0).getSequenceAsString());
1977  1 assertEquals("CCC------AAA", cds.getSequenceAt(1).getSequenceAsString());
1978    }
1979   
 
1980  1 toggle @Test(groups = { "Functional" })
1981    public void testAddMappedPositions()
1982    {
1983  1 SequenceI from = new Sequence("dna", "ggAA-ATcc-TT-g");
1984  1 SequenceI seq1 = new Sequence("cds", "AAATTT");
1985  1 from.createDatasetSequence();
1986  1 seq1.createDatasetSequence();
1987  1 Mapping mapping = new Mapping(seq1, new MapList(
1988    new int[] { 3, 6, 9, 10 }, new int[] { 1, 6 }, 1, 1));
1989  1 Map<Integer, Map<SequenceI, Character>> map = new TreeMap<>();
1990  1 AlignmentUtils.addMappedPositions(seq1, from, mapping, map);
1991   
1992    /*
1993    * verify map has seq1 residues in columns 3,4,6,7,11,12
1994    */
1995  1 assertEquals(6, map.size());
1996  1 assertEquals('A', map.get(3).get(seq1).charValue());
1997  1 assertEquals('A', map.get(4).get(seq1).charValue());
1998  1 assertEquals('A', map.get(6).get(seq1).charValue());
1999  1 assertEquals('T', map.get(7).get(seq1).charValue());
2000  1 assertEquals('T', map.get(11).get(seq1).charValue());
2001  1 assertEquals('T', map.get(12).get(seq1).charValue());
2002   
2003    /*
2004    *
2005    */
2006    }
2007   
2008    /**
2009    * Test case where the mapping 'from' range includes a stop codon which is
2010    * absent in the 'to' range
2011    */
 
2012  1 toggle @Test(groups = { "Functional" })
2013    public void testAddMappedPositions_withStopCodon()
2014    {
2015  1 SequenceI from = new Sequence("dna", "ggAA-ATcc-TT-g");
2016  1 SequenceI seq1 = new Sequence("cds", "AAATTT");
2017  1 from.createDatasetSequence();
2018  1 seq1.createDatasetSequence();
2019  1 Mapping mapping = new Mapping(seq1, new MapList(
2020    new int[] { 3, 6, 9, 10 }, new int[] { 1, 6 }, 1, 1));
2021  1 Map<Integer, Map<SequenceI, Character>> map = new TreeMap<>();
2022  1 AlignmentUtils.addMappedPositions(seq1, from, mapping, map);
2023   
2024    /*
2025    * verify map has seq1 residues in columns 3,4,6,7,11,12
2026    */
2027  1 assertEquals(6, map.size());
2028  1 assertEquals('A', map.get(3).get(seq1).charValue());
2029  1 assertEquals('A', map.get(4).get(seq1).charValue());
2030  1 assertEquals('A', map.get(6).get(seq1).charValue());
2031  1 assertEquals('T', map.get(7).get(seq1).charValue());
2032  1 assertEquals('T', map.get(11).get(seq1).charValue());
2033  1 assertEquals('T', map.get(12).get(seq1).charValue());
2034    }
2035   
2036    /**
2037    * Test for the case where the products for which we want CDS are specified.
2038    * This is to represent the case where EMBL has CDS mappings to both Uniprot
2039    * and EMBLCDSPROTEIN. makeCdsAlignment() should only return the mappings for
2040    * the protein sequences specified.
2041    */
 
2042  1 toggle @Test(groups = { "Functional" })
2043    public void testMakeCdsAlignment_filterProducts()
2044    {
2045  1 SequenceI dna1 = new Sequence("dna1", "aaaGGGcccTTTaaa");
2046  1 SequenceI dna2 = new Sequence("dna2", "GGGcccTTTaaaCCC");
2047  1 SequenceI pep1 = new Sequence("Uniprot|pep1", "GF");
2048  1 SequenceI pep2 = new Sequence("Uniprot|pep2", "GFP");
2049  1 SequenceI pep3 = new Sequence("EMBL|pep3", "GF");
2050  1 SequenceI pep4 = new Sequence("EMBL|pep4", "GFP");
2051  1 dna1.createDatasetSequence();
2052  1 dna2.createDatasetSequence();
2053  1 pep1.createDatasetSequence();
2054  1 pep2.createDatasetSequence();
2055  1 pep3.createDatasetSequence();
2056  1 pep4.createDatasetSequence();
2057  1 AlignmentI dna = new Alignment(new SequenceI[] { dna1, dna2 });
2058  1 dna.setDataset(null);
2059  1 AlignmentI emblPeptides = new Alignment(new SequenceI[] { pep3, pep4 });
2060  1 emblPeptides.setDataset(null);
2061   
2062  1 AlignedCodonFrame acf = new AlignedCodonFrame();
2063  1 MapList map = new MapList(new int[] { 4, 6, 10, 12 },
2064    new int[] { 1, 2 }, 3, 1);
2065  1 acf.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(), map);
2066  1 acf.addMap(dna1.getDatasetSequence(), pep3.getDatasetSequence(), map);
2067  1 dna.addCodonFrame(acf);
2068   
2069  1 acf = new AlignedCodonFrame();
2070  1 map = new MapList(new int[] { 1, 3, 7, 9, 13, 15 }, new int[] { 1, 3 },
2071    3, 1);
2072  1 acf.addMap(dna2.getDatasetSequence(), pep2.getDatasetSequence(), map);
2073  1 acf.addMap(dna2.getDatasetSequence(), pep4.getDatasetSequence(), map);
2074  1 dna.addCodonFrame(acf);
2075   
2076    /*
2077    * execute method under test to find CDS for EMBL peptides only
2078    */
2079  1 AlignmentI cds = AlignmentUtils.makeCdsAlignment(new SequenceI[] {
2080    dna1, dna2 }, dna.getDataset(), emblPeptides.getSequencesArray());
2081   
2082  1 assertEquals(2, cds.getSequences().size());
2083  1 assertEquals("GGGTTT", cds.getSequenceAt(0).getSequenceAsString());
2084  1 assertEquals("GGGTTTCCC", cds.getSequenceAt(1).getSequenceAsString());
2085   
2086    /*
2087    * verify shared, extended alignment dataset
2088    */
2089  1 assertSame(dna.getDataset(), cds.getDataset());
2090  1 assertTrue(dna.getDataset().getSequences()
2091    .contains(cds.getSequenceAt(0).getDatasetSequence()));
2092  1 assertTrue(dna.getDataset().getSequences()
2093    .contains(cds.getSequenceAt(1).getDatasetSequence()));
2094   
2095    /*
2096    * Verify mappings from CDS to peptide, cDNA to CDS, and cDNA to peptide
2097    * the mappings are on the shared alignment dataset
2098    */
2099  1 List<AlignedCodonFrame> cdsMappings = cds.getDataset().getCodonFrames();
2100    /*
2101    * 6 mappings, 2*(DNA->CDS), 2*(DNA->Pep), 2*(CDS->Pep)
2102    */
2103  1 assertEquals(6, cdsMappings.size());
2104   
2105    /*
2106    * verify that mapping sets for dna and cds alignments are different
2107    * [not current behaviour - all mappings are on the alignment dataset]
2108    */
2109    // select -> subselect type to test.
2110    // Assert.assertNotSame(dna.getCodonFrames(), cds.getCodonFrames());
2111    // assertEquals(4, dna.getCodonFrames().size());
2112    // assertEquals(4, cds.getCodonFrames().size());
2113   
2114    /*
2115    * Two mappings involve pep3 (dna to pep3, cds to pep3)
2116    * Mapping from pep3 to GGGTTT in first new exon sequence
2117    */
2118  1 List<AlignedCodonFrame> pep3Mappings = MappingUtils
2119    .findMappingsForSequence(pep3, cdsMappings);
2120  1 assertEquals(2, pep3Mappings.size());
2121  1 List<AlignedCodonFrame> mappings = MappingUtils
2122    .findMappingsForSequence(cds.getSequenceAt(0), pep3Mappings);
2123  1 assertEquals(1, mappings.size());
2124   
2125    // map G to GGG
2126  1 SearchResultsI sr = MappingUtils.buildSearchResults(pep3, 1, mappings);
2127  1 assertEquals(1, sr.getResults().size());
2128  1 SearchResultMatchI m = sr.getResults().get(0);
2129  1 assertSame(cds.getSequenceAt(0).getDatasetSequence(), m.getSequence());
2130  1 assertEquals(1, m.getStart());
2131  1 assertEquals(3, m.getEnd());
2132    // map F to TTT
2133  1 sr = MappingUtils.buildSearchResults(pep3, 2, mappings);
2134  1 m = sr.getResults().get(0);
2135  1 assertSame(cds.getSequenceAt(0).getDatasetSequence(), m.getSequence());
2136  1 assertEquals(4, m.getStart());
2137  1 assertEquals(6, m.getEnd());
2138   
2139    /*
2140    * Two mappings involve pep4 (dna to pep4, cds to pep4)
2141    * Verify mapping from pep4 to GGGTTTCCC in second new exon sequence
2142    */
2143  1 List<AlignedCodonFrame> pep4Mappings = MappingUtils
2144    .findMappingsForSequence(pep4, cdsMappings);
2145  1 assertEquals(2, pep4Mappings.size());
2146  1 mappings = MappingUtils.findMappingsForSequence(cds.getSequenceAt(1),
2147    pep4Mappings);
2148  1 assertEquals(1, mappings.size());
2149    // map G to GGG
2150  1 sr = MappingUtils.buildSearchResults(pep4, 1, mappings);
2151  1 assertEquals(1, sr.getResults().size());
2152  1 m = sr.getResults().get(0);
2153  1 assertSame(cds.getSequenceAt(1).getDatasetSequence(), m.getSequence());
2154  1 assertEquals(1, m.getStart());
2155  1 assertEquals(3, m.getEnd());
2156    // map F to TTT
2157  1 sr = MappingUtils.buildSearchResults(pep4, 2, mappings);
2158  1 m = sr.getResults().get(0);
2159  1 assertSame(cds.getSequenceAt(1).getDatasetSequence(), m.getSequence());
2160  1 assertEquals(4, m.getStart());
2161  1 assertEquals(6, m.getEnd());
2162    // map P to CCC
2163  1 sr = MappingUtils.buildSearchResults(pep4, 3, mappings);
2164  1 m = sr.getResults().get(0);
2165  1 assertSame(cds.getSequenceAt(1).getDatasetSequence(), m.getSequence());
2166  1 assertEquals(7, m.getStart());
2167  1 assertEquals(9, m.getEnd());
2168    }
2169   
2170    /**
2171    * Test the method that just copies aligned sequences, provided all sequences
2172    * to be aligned share the aligned sequence's dataset
2173    */
 
2174  1 toggle @Test(groups = "Functional")
2175    public void testAlignAsSameSequences()
2176    {
2177  1 SequenceI dna1 = new Sequence("dna1", "cccGGGTTTaaa");
2178  1 SequenceI dna2 = new Sequence("dna2", "CCCgggtttAAA");
2179  1 AlignmentI al1 = new Alignment(new SequenceI[] { dna1, dna2 });
2180  1 ((Alignment) al1).createDatasetAlignment();
2181   
2182  1 SequenceI dna3 = new Sequence(dna1);
2183  1 SequenceI dna4 = new Sequence(dna2);
2184  1 assertSame(dna3.getDatasetSequence(), dna1.getDatasetSequence());
2185  1 assertSame(dna4.getDatasetSequence(), dna2.getDatasetSequence());
2186  1 String seq1 = "-cc-GG-GT-TT--aaa";
2187  1 dna3.setSequence(seq1);
2188  1 String seq2 = "C--C-Cgg--gtt-tAA-A-";
2189  1 dna4.setSequence(seq2);
2190  1 AlignmentI al2 = new Alignment(new SequenceI[] { dna3, dna4 });
2191  1 ((Alignment) al2).createDatasetAlignment();
2192   
2193    /*
2194    * alignment removes gapped columns (two internal, two trailing)
2195    */
2196  1 assertTrue(AlignmentUtils.alignAsSameSequences(al1, al2));
2197  1 String aligned1 = "-cc-GG-GTTT-aaa";
2198  1 assertEquals(aligned1,
2199    al1.getSequenceAt(0).getSequenceAsString());
2200  1 String aligned2 = "C--C-Cgg-gtttAAA";
2201  1 assertEquals(aligned2,
2202    al1.getSequenceAt(1).getSequenceAsString());
2203   
2204    /*
2205    * add another sequence to 'aligned' - should still succeed, since
2206    * unaligned sequences still share a dataset with aligned sequences
2207    */
2208  1 SequenceI dna5 = new Sequence("dna5", "CCCgggtttAAA");
2209  1 dna5.createDatasetSequence();
2210  1 al2.addSequence(dna5);
2211  1 assertTrue(AlignmentUtils.alignAsSameSequences(al1, al2));
2212  1 assertEquals(aligned1, al1.getSequenceAt(0).getSequenceAsString());
2213  1 assertEquals(aligned2, al1.getSequenceAt(1).getSequenceAsString());
2214   
2215    /*
2216    * add another sequence to 'unaligned' - should fail, since now not
2217    * all unaligned sequences share a dataset with aligned sequences
2218    */
2219  1 SequenceI dna6 = new Sequence("dna6", "CCCgggtttAAA");
2220  1 dna6.createDatasetSequence();
2221  1 al1.addSequence(dna6);
2222    // JAL-2110 JBP Comment: what's the use case for this behaviour ?
2223  1 assertFalse(AlignmentUtils.alignAsSameSequences(al1, al2));
2224    }
2225   
 
2226  1 toggle @Test(groups = "Functional")
2227    public void testAlignAsSameSequencesMultipleSubSeq()
2228    {
2229  1 SequenceI dna1 = new Sequence("dna1", "cccGGGTTTaaa");
2230  1 SequenceI dna2 = new Sequence("dna2", "CCCgggtttAAA");
2231  1 SequenceI as1 = dna1.deriveSequence(); // cccGGGTTTaaa/1-12
2232  1 SequenceI as2 = dna1.deriveSequence().getSubSequence(3, 7); // GGGT/4-7
2233  1 SequenceI as3 = dna2.deriveSequence(); // CCCgggtttAAA/1-12
2234  1 as1.insertCharAt(6, 5, '-');
2235  1 assertEquals("cccGGG-----TTTaaa", as1.getSequenceAsString());
2236  1 as2.insertCharAt(6, 5, '-');
2237  1 assertEquals("GGGT-----", as2.getSequenceAsString());
2238  1 as3.insertCharAt(3, 5, '-');
2239  1 assertEquals("CCC-----gggtttAAA", as3.getSequenceAsString());
2240  1 AlignmentI aligned = new Alignment(new SequenceI[] { as1, as2, as3 });
2241   
2242    // why do we need to cast this still ?
2243  1 ((Alignment) aligned).createDatasetAlignment();
2244  1 SequenceI uas1 = dna1.deriveSequence();
2245  1 SequenceI uas2 = dna1.deriveSequence().getSubSequence(3, 7);
2246  1 SequenceI uas3 = dna2.deriveSequence();
2247  1 AlignmentI tobealigned = new Alignment(new SequenceI[] { uas1, uas2,
2248    uas3 });
2249  1 ((Alignment) tobealigned).createDatasetAlignment();
2250   
2251    /*
2252    * alignAs lines up dataset sequences and removes empty columns (two)
2253    */
2254  1 assertTrue(AlignmentUtils.alignAsSameSequences(tobealigned, aligned));
2255  1 assertEquals("cccGGG---TTTaaa", uas1.getSequenceAsString());
2256  1 assertEquals("GGGT", uas2.getSequenceAsString());
2257  1 assertEquals("CCC---gggtttAAA", uas3.getSequenceAsString());
2258    }
2259   
 
2260  1 toggle @Test(groups = { "Functional" })
2261    public void testTransferGeneLoci()
2262    {
2263  1 SequenceI from = new Sequence("transcript",
2264    "aaacccgggTTTAAACCCGGGtttaaacccgggttt");
2265  1 SequenceI to = new Sequence("CDS", "TTTAAACCCGGG");
2266  1 MapList map = new MapList(new int[] { 1, 12 }, new int[] { 10, 21 }, 1,
2267    1);
2268   
2269    /*
2270    * first with nothing to transfer
2271    */
2272  1 AlignmentUtils.transferGeneLoci(from, map, to);
2273  1 assertNull(to.getGeneLoci());
2274   
2275    /*
2276    * next with gene loci set on 'from' sequence
2277    */
2278  1 int[] exons = new int[] { 100, 105, 155, 164, 210, 229 };
2279  1 MapList geneMap = new MapList(new int[] { 1, 36 }, exons, 1, 1);
2280  1 from.setGeneLoci("human", "GRCh38", "7", geneMap);
2281  1 AlignmentUtils.transferGeneLoci(from, map, to);
2282   
2283  1 GeneLociI toLoci = to.getGeneLoci();
2284  1 assertNotNull(toLoci);
2285    // DBRefEntry constructor upper-cases 'source'
2286  1 assertEquals("HUMAN", toLoci.getSpeciesId());
2287  1 assertEquals("GRCh38", toLoci.getAssemblyId());
2288  1 assertEquals("7", toLoci.getChromosomeId());
2289   
2290    /*
2291    * transcript 'exons' are 1-6, 7-16, 17-36
2292    * CDS 1:12 is transcript 10-21
2293    * transcript 'CDS' is 10-16, 17-21
2294    * which is 'gene' 158-164, 210-214
2295    */
2296  1 MapList toMap = toLoci.getMapping();
2297  1 assertEquals(1, toMap.getFromRanges().size());
2298  1 assertEquals(2, toMap.getFromRanges().get(0).length);
2299  1 assertEquals(1, toMap.getFromRanges().get(0)[0]);
2300  1 assertEquals(12, toMap.getFromRanges().get(0)[1]);
2301  1 assertEquals(2, toMap.getToRanges().size());
2302  1 assertEquals(2, toMap.getToRanges().get(0).length);
2303  1 assertEquals(158, toMap.getToRanges().get(0)[0]);
2304  1 assertEquals(164, toMap.getToRanges().get(0)[1]);
2305  1 assertEquals(210, toMap.getToRanges().get(1)[0]);
2306  1 assertEquals(214, toMap.getToRanges().get(1)[1]);
2307    // or summarised as (but toString might change in future):
2308  1 assertEquals("[ [1, 12] ] 1:1 to [ [158, 164] [210, 214] ]",
2309    toMap.toString());
2310   
2311    /*
2312    * an existing value is not overridden
2313    */
2314  1 geneMap = new MapList(new int[] { 1, 36 }, new int[] { 36, 1 }, 1, 1);
2315  1 from.setGeneLoci("inhuman", "GRCh37", "6", geneMap);
2316  1 AlignmentUtils.transferGeneLoci(from, map, to);
2317  1 assertEquals("GRCh38", toLoci.getAssemblyId());
2318  1 assertEquals("7", toLoci.getChromosomeId());
2319  1 toMap = toLoci.getMapping();
2320  1 assertEquals("[ [1, 12] ] 1:1 to [ [158, 164] [210, 214] ]",
2321    toMap.toString());
2322    }
2323   
2324    /**
2325    * Tests for the method that maps nucleotide to protein based on CDS features
2326    */
 
2327  1 toggle @Test(groups = "Functional")
2328    public void testMapCdsToProtein()
2329    {
2330  1 SequenceI peptide = new Sequence("pep", "KLQ");
2331   
2332    /*
2333    * Case 1: CDS 3 times length of peptide
2334    * NB method only checks lengths match, not translation
2335    */
2336  1 SequenceI dna = new Sequence("dna", "AACGacgtCTCCT");
2337  1 dna.createDatasetSequence();
2338  1 dna.addSequenceFeature(new SequenceFeature("CDS", "", 1, 4, null));
2339  1 dna.addSequenceFeature(new SequenceFeature("CDS", "", 9, 13, null));
2340  1 MapList ml = AlignmentUtils.mapCdsToProtein(dna, peptide);
2341  1 assertEquals(3, ml.getFromRatio());
2342  1 assertEquals(1, ml.getToRatio());
2343  1 assertEquals("[[1, 3]]",
2344    Arrays.deepToString(ml.getToRanges().toArray()));
2345  1 assertEquals("[[1, 4], [9, 13]]",
2346    Arrays.deepToString(ml.getFromRanges().toArray()));
2347   
2348    /*
2349    * Case 2: CDS 3 times length of peptide + stop codon
2350    * (note code does not currently check trailing codon is a stop codon)
2351    */
2352  1 dna = new Sequence("dna", "AACGacgtCTCCTCCC");
2353  1 dna.createDatasetSequence();
2354  1 dna.addSequenceFeature(new SequenceFeature("CDS", "", 1, 4, null));
2355  1 dna.addSequenceFeature(new SequenceFeature("CDS", "", 9, 16, null));
2356  1 ml = AlignmentUtils.mapCdsToProtein(dna, peptide);
2357  1 assertEquals(3, ml.getFromRatio());
2358  1 assertEquals(1, ml.getToRatio());
2359  1 assertEquals("[[1, 3]]",
2360    Arrays.deepToString(ml.getToRanges().toArray()));
2361  1 assertEquals("[[1, 4], [9, 13]]",
2362    Arrays.deepToString(ml.getFromRanges().toArray()));
2363   
2364    /*
2365    * Case 3: CDS longer than 3 * peptide + stop codon - no mapping is made
2366    */
2367  1 dna = new Sequence("dna", "AACGacgtCTCCTTGATCA");
2368  1 dna.createDatasetSequence();
2369  1 dna.addSequenceFeature(new SequenceFeature("CDS", "", 1, 4, null));
2370  1 dna.addSequenceFeature(new SequenceFeature("CDS", "", 9, 19, null));
2371  1 ml = AlignmentUtils.mapCdsToProtein(dna, peptide);
2372  1 assertNull(ml);
2373   
2374    /*
2375    * Case 4: CDS shorter than 3 * peptide - no mapping is made
2376    */
2377  1 dna = new Sequence("dna", "AACGacgtCTCC");
2378  1 dna.createDatasetSequence();
2379  1 dna.addSequenceFeature(new SequenceFeature("CDS", "", 1, 4, null));
2380  1 dna.addSequenceFeature(new SequenceFeature("CDS", "", 9, 12, null));
2381  1 ml = AlignmentUtils.mapCdsToProtein(dna, peptide);
2382  1 assertNull(ml);
2383   
2384    /*
2385    * Case 5: CDS 3 times length of peptide + part codon - mapping is truncated
2386    */
2387  1 dna = new Sequence("dna", "AACGacgtCTCCTTG");
2388  1 dna.createDatasetSequence();
2389  1 dna.addSequenceFeature(new SequenceFeature("CDS", "", 1, 4, null));
2390  1 dna.addSequenceFeature(new SequenceFeature("CDS", "", 9, 15, null));
2391  1 ml = AlignmentUtils.mapCdsToProtein(dna, peptide);
2392  1 assertEquals(3, ml.getFromRatio());
2393  1 assertEquals(1, ml.getToRatio());
2394  1 assertEquals("[[1, 3]]",
2395    Arrays.deepToString(ml.getToRanges().toArray()));
2396  1 assertEquals("[[1, 4], [9, 13]]",
2397    Arrays.deepToString(ml.getFromRanges().toArray()));
2398   
2399    /*
2400    * Case 6: incomplete start codon corresponding to X in peptide
2401    */
2402  1 dna = new Sequence("dna", "ACGacgtCTCCTTGG");
2403  1 dna.createDatasetSequence();
2404  1 SequenceFeature sf = new SequenceFeature("CDS", "", 1, 3, null);
2405  1 sf.setPhase("2"); // skip 2 positions (AC) to start of next codon (GCT)
2406  1 dna.addSequenceFeature(sf);
2407  1 dna.addSequenceFeature(new SequenceFeature("CDS", "", 8, 15, null));
2408  1 peptide = new Sequence("pep", "XLQ");
2409  1 ml = AlignmentUtils.mapCdsToProtein(dna, peptide);
2410  1 assertEquals("[[2, 3]]",
2411    Arrays.deepToString(ml.getToRanges().toArray()));
2412  1 assertEquals("[[3, 3], [8, 12]]",
2413    Arrays.deepToString(ml.getFromRanges().toArray()));
2414    }
2415   
2416    /**
2417    * Tests for the method that locates the CDS sequence that has a mapping to
2418    * the given protein. That is, given a transcript-to-peptide mapping, find the
2419    * cds-to-peptide mapping that relates to both, and return the CDS sequence.
2420    */
 
2421  0 toggle @Test
2422    public void testFindCdsForProtein()
2423    {
2424  0 List<AlignedCodonFrame> mappings = new ArrayList<>();
2425  0 AlignedCodonFrame acf1 = new AlignedCodonFrame();
2426  0 mappings.add(acf1);
2427   
2428  0 SequenceI dna1 = new Sequence("dna1", "cgatATcgGCTATCTATGacg");
2429  0 dna1.createDatasetSequence();
2430   
2431    // NB we currently exclude STOP codon from CDS sequences
2432    // the test would need to change if this changes in future
2433  0 SequenceI cds1 = new Sequence("cds1", "ATGCTATCT");
2434  0 cds1.createDatasetSequence();
2435   
2436  0 SequenceI pep1 = new Sequence("pep1", "MLS");
2437  0 pep1.createDatasetSequence();
2438  0 List<AlignedCodonFrame> seqMappings = new ArrayList<>();
2439  0 MapList mapList = new MapList(
2440    new int[]
2441    { 5, 6, 9, 15 }, new int[] { 1, 3 }, 3, 1);
2442  0 Mapping dnaToPeptide = new Mapping(pep1.getDatasetSequence(), mapList);
2443   
2444    // add dna to peptide mapping
2445  0 seqMappings.add(acf1);
2446  0 acf1.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(),
2447    mapList);
2448   
2449    /*
2450    * first case - no dna-to-CDS mapping exists - search fails
2451    */
2452  0 SequenceI seq = AlignmentUtils.findCdsForProtein(mappings, dna1,
2453    seqMappings, dnaToPeptide);
2454  0 assertNull(seq);
2455   
2456    /*
2457    * second case - CDS-to-peptide mapping exists but no dna-to-CDS
2458    * - search fails
2459    */
2460    // todo this test fails if the mapping is added to acf1, not acf2
2461    // need to tidy up use of lists of mappings in AlignedCodonFrame
2462  0 AlignedCodonFrame acf2 = new AlignedCodonFrame();
2463  0 mappings.add(acf2);
2464  0 MapList cdsToPeptideMapping = new MapList(new int[]
2465    { 1, 9 }, new int[] { 1, 3 }, 3, 1);
2466  0 acf2.addMap(cds1.getDatasetSequence(), pep1.getDatasetSequence(),
2467    cdsToPeptideMapping);
2468  0 assertNull(AlignmentUtils.findCdsForProtein(mappings, dna1, seqMappings,
2469    dnaToPeptide));
2470   
2471    /*
2472    * third case - add dna-to-CDS mapping - CDS is now found!
2473    */
2474  0 MapList dnaToCdsMapping = new MapList(new int[] { 5, 6, 9, 15 },
2475    new int[]
2476    { 1, 9 }, 1, 1);
2477  0 acf1.addMap(dna1.getDatasetSequence(), cds1.getDatasetSequence(),
2478    dnaToCdsMapping);
2479  0 seq = AlignmentUtils.findCdsForProtein(mappings, dna1, seqMappings,
2480    dnaToPeptide);
2481  0 assertSame(seq, cds1.getDatasetSequence());
2482    }
2483   
2484    /**
2485    * Tests for the method that locates the CDS sequence that has a mapping to
2486    * the given protein. That is, given a transcript-to-peptide mapping, find the
2487    * cds-to-peptide mapping that relates to both, and return the CDS sequence.
2488    * This test is for the case where transcript and CDS are the same length.
2489    */
 
2490  0 toggle @Test
2491    public void testFindCdsForProtein_noUTR()
2492    {
2493  0 List<AlignedCodonFrame> mappings = new ArrayList<>();
2494  0 AlignedCodonFrame acf1 = new AlignedCodonFrame();
2495  0 mappings.add(acf1);
2496   
2497  0 SequenceI dna1 = new Sequence("dna1", "ATGCTATCTTAA");
2498  0 dna1.createDatasetSequence();
2499   
2500    // NB we currently exclude STOP codon from CDS sequences
2501    // the test would need to change if this changes in future
2502  0 SequenceI cds1 = new Sequence("cds1", "ATGCTATCT");
2503  0 cds1.createDatasetSequence();
2504   
2505  0 SequenceI pep1 = new Sequence("pep1", "MLS");
2506  0 pep1.createDatasetSequence();
2507  0 List<AlignedCodonFrame> seqMappings = new ArrayList<>();
2508  0 MapList mapList = new MapList(
2509    new int[]
2510    { 1, 9 }, new int[] { 1, 3 }, 3, 1);
2511  0 Mapping dnaToPeptide = new Mapping(pep1.getDatasetSequence(), mapList);
2512   
2513    // add dna to peptide mapping
2514  0 seqMappings.add(acf1);
2515  0 acf1.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(),
2516    mapList);
2517   
2518    /*
2519    * first case - transcript lacks CDS features - it appears to be
2520    * the CDS sequence and is returned
2521    */
2522  0 SequenceI seq = AlignmentUtils.findCdsForProtein(mappings, dna1,
2523    seqMappings, dnaToPeptide);
2524  0 assertSame(seq, dna1.getDatasetSequence());
2525   
2526    /*
2527    * second case - transcript has CDS feature - this means it is
2528    * not returned as a match for CDS (CDS sequences don't have CDS features)
2529    */
2530  0 dna1.addSequenceFeature(
2531    new SequenceFeature(SequenceOntologyI.CDS, "cds", 1, 12, null));
2532  0 seq = AlignmentUtils.findCdsForProtein(mappings, dna1, seqMappings,
2533    dnaToPeptide);
2534  0 assertNull(seq);
2535   
2536    /*
2537    * third case - CDS-to-peptide mapping exists but no dna-to-CDS
2538    * - search fails
2539    */
2540    // todo this test fails if the mapping is added to acf1, not acf2
2541    // need to tidy up use of lists of mappings in AlignedCodonFrame
2542  0 AlignedCodonFrame acf2 = new AlignedCodonFrame();
2543  0 mappings.add(acf2);
2544  0 MapList cdsToPeptideMapping = new MapList(new int[]
2545    { 1, 9 }, new int[] { 1, 3 }, 3, 1);
2546  0 acf2.addMap(cds1.getDatasetSequence(), pep1.getDatasetSequence(),
2547    cdsToPeptideMapping);
2548  0 assertNull(AlignmentUtils.findCdsForProtein(mappings, dna1, seqMappings,
2549    dnaToPeptide));
2550   
2551    /*
2552    * fourth case - add dna-to-CDS mapping - CDS is now found!
2553    */
2554  0 MapList dnaToCdsMapping = new MapList(new int[] { 1, 9 },
2555    new int[]
2556    { 1, 9 }, 1, 1);
2557  0 acf1.addMap(dna1.getDatasetSequence(), cds1.getDatasetSequence(),
2558    dnaToCdsMapping);
2559  0 seq = AlignmentUtils.findCdsForProtein(mappings, dna1, seqMappings,
2560    dnaToPeptide);
2561  0 assertSame(seq, cds1.getDatasetSequence());
2562    }
2563    }