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