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