JAL-1805 test envirionment separation
[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
24 import static org.testng.AssertJUnit.assertEquals;
25 import static org.testng.AssertJUnit.assertFalse;
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.Mapping;
37 import jalview.datamodel.SearchResults;
38 import jalview.datamodel.SearchResults.Match;
39 import jalview.datamodel.Sequence;
40 import jalview.datamodel.SequenceI;
41 import jalview.io.AppletFormatAdapter;
42 import jalview.io.FormatAdapter;
43 import jalview.util.MapList;
44 import jalview.util.MappingUtils;
45
46 import java.io.IOException;
47 import java.util.ArrayList;
48 import java.util.Arrays;
49 import java.util.Collections;
50 import java.util.HashSet;
51 import java.util.Iterator;
52 import java.util.LinkedHashSet;
53 import java.util.List;
54 import java.util.Map;
55 import java.util.Set;
56
57 import org.testng.annotations.Test;
58
59 public class AlignmentUtilsTests
60 {
61   // @formatter:off
62   private static final String TEST_DATA = 
63           "# STOCKHOLM 1.0\n" +
64           "#=GS D.melanogaster.1 AC AY119185.1/838-902\n" +
65           "#=GS D.melanogaster.2 AC AC092237.1/57223-57161\n" +
66           "#=GS D.melanogaster.3 AC AY060611.1/560-627\n" +
67           "D.melanogaster.1          G.AGCC.CU...AUGAUCGA\n" +
68           "#=GR D.melanogaster.1 SS  ................((((\n" +
69           "D.melanogaster.2          C.AUUCAACU.UAUGAGGAU\n" +
70           "#=GR D.melanogaster.2 SS  ................((((\n" +
71           "D.melanogaster.3          G.UGGCGCU..UAUGACGCA\n" +
72           "#=GR D.melanogaster.3 SS  (.(((...(....(((((((\n" +
73           "//";
74
75   private static final String AA_SEQS_1 = 
76           ">Seq1Name\n" +
77           "K-QY--L\n" +
78           ">Seq2Name\n" +
79           "-R-FP-W-\n";
80
81   private static final String CDNA_SEQS_1 = 
82           ">Seq1Name\n" +
83           "AC-GG--CUC-CAA-CT\n" +
84           ">Seq2Name\n" +
85           "-CG-TTA--ACG---AAGT\n";
86
87   private static final String CDNA_SEQS_2 = 
88           ">Seq1Name\n" +
89           "GCTCGUCGTACT\n" +
90           ">Seq2Name\n" +
91           "GGGTCAGGCAGT\n";
92   // @formatter:on
93
94   // public static Sequence ts=new
95   // Sequence("short","ASDASDASDASDASDASDASDASDASDASDASDASDASD");
96   public static Sequence ts = new Sequence("short",
97           "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm");
98
99   @Test(groups ={ "Functional" })
100   public void testExpandContext()
101   {
102     AlignmentI al = new Alignment(new Sequence[] {});
103     for (int i = 4; i < 14; i += 2)
104     {
105       SequenceI s1=ts.deriveSequence().getSubSequence(i, i+7);
106       al.addSequence(s1);
107     }
108     System.out.println(new AppletFormatAdapter().formatSequences("Clustal", al, true));
109     for (int flnk=-1;flnk<25; flnk++)
110     {
111       AlignmentI exp = AlignmentUtils.expandContext(al, flnk);
112       System.out.println("\nFlank size: " + flnk);
113       System.out.println(new AppletFormatAdapter().formatSequences(
114               "Clustal", exp, true));
115       if (flnk == -1)
116       {
117         /*
118          * Full expansion to complete sequences
119          */
120         for (SequenceI sq : exp.getSequences())
121         {
122           String ung = sq.getSequenceAsString().replaceAll("-+", "");
123           final String errorMsg = "Flanking sequence not the same as original dataset sequence.\n"
124                   + ung
125                   + "\n"
126                   + sq.getDatasetSequence().getSequenceAsString();
127           assertTrue(errorMsg, ung.equalsIgnoreCase(sq.getDatasetSequence()
128                   .getSequenceAsString()));
129         }
130       }
131       else if (flnk == 24)
132       {
133         /*
134          * Last sequence is fully expanded, others have leading gaps to match
135          */
136         assertTrue(exp.getSequenceAt(4).getSequenceAsString()
137                 .startsWith("abc"));
138         assertTrue(exp.getSequenceAt(3).getSequenceAsString()
139                 .startsWith("--abc"));
140         assertTrue(exp.getSequenceAt(2).getSequenceAsString()
141                 .startsWith("----abc"));
142         assertTrue(exp.getSequenceAt(1).getSequenceAsString()
143                 .startsWith("------abc"));
144         assertTrue(exp.getSequenceAt(0).getSequenceAsString()
145                 .startsWith("--------abc"));
146       }
147     }
148   }
149
150   /**
151    * Test that annotations are correctly adjusted by expandContext
152    */
153   @Test(groups ={ "Functional" })
154   public void testExpandContext_annotation()
155   {
156     AlignmentI al = new Alignment(new Sequence[]
157     {});
158     SequenceI ds = new Sequence("Seq1", "ABCDEFGHI");
159     // subsequence DEF:
160     SequenceI seq1 = ds.deriveSequence().getSubSequence(3, 6);
161     al.addSequence(seq1);
162
163     /*
164      * Annotate DEF with 4/5/6 respectively
165      */
166     Annotation[] anns = new Annotation[]
167     { new Annotation(4), new Annotation(5), new Annotation(6) };
168     AlignmentAnnotation ann = new AlignmentAnnotation("SS",
169             "secondary structure", anns);
170     seq1.addAlignmentAnnotation(ann);
171
172     /*
173      * The annotations array should match aligned positions
174      */
175     assertEquals(3, ann.annotations.length);
176     assertEquals(4, ann.annotations[0].value, 0.001);
177     assertEquals(5, ann.annotations[1].value, 0.001);
178     assertEquals(6, ann.annotations[2].value, 0.001);
179
180     /*
181      * Check annotation to sequence position mappings before expanding the
182      * sequence; these are set up in Sequence.addAlignmentAnnotation ->
183      * Annotation.setSequenceRef -> createSequenceMappings
184      */
185     assertNull(ann.getAnnotationForPosition(1));
186     assertNull(ann.getAnnotationForPosition(2));
187     assertNull(ann.getAnnotationForPosition(3));
188     assertEquals(4, ann.getAnnotationForPosition(4).value, 0.001);
189     assertEquals(5, ann.getAnnotationForPosition(5).value, 0.001);
190     assertEquals(6, ann.getAnnotationForPosition(6).value, 0.001);
191     assertNull(ann.getAnnotationForPosition(7));
192     assertNull(ann.getAnnotationForPosition(8));
193     assertNull(ann.getAnnotationForPosition(9));
194
195     /*
196      * Expand the subsequence to the full sequence abcDEFghi
197      */
198     AlignmentI expanded = AlignmentUtils.expandContext(al, -1);
199     assertEquals("abcDEFghi", expanded.getSequenceAt(0)
200             .getSequenceAsString());
201
202     /*
203      * Confirm the alignment and sequence have the same SS annotation,
204      * referencing the expanded sequence
205      */
206     ann = expanded.getSequenceAt(0).getAnnotation()[0];
207     assertSame(ann, expanded.getAlignmentAnnotation()[0]);
208     assertSame(expanded.getSequenceAt(0), ann.sequenceRef);
209
210     /*
211      * The annotations array should have null values except for annotated
212      * positions
213      */
214     assertNull(ann.annotations[0]);
215     assertNull(ann.annotations[1]);
216     assertNull(ann.annotations[2]);
217     assertEquals(4, ann.annotations[3].value, 0.001);
218     assertEquals(5, ann.annotations[4].value, 0.001);
219     assertEquals(6, ann.annotations[5].value, 0.001);
220     assertNull(ann.annotations[6]);
221     assertNull(ann.annotations[7]);
222     assertNull(ann.annotations[8]);
223
224     /*
225      * sequence position mappings should be unchanged
226      */
227     assertNull(ann.getAnnotationForPosition(1));
228     assertNull(ann.getAnnotationForPosition(2));
229     assertNull(ann.getAnnotationForPosition(3));
230     assertEquals(4, ann.getAnnotationForPosition(4).value, 0.001);
231     assertEquals(5, ann.getAnnotationForPosition(5).value, 0.001);
232     assertEquals(6, ann.getAnnotationForPosition(6).value, 0.001);
233     assertNull(ann.getAnnotationForPosition(7));
234     assertNull(ann.getAnnotationForPosition(8));
235     assertNull(ann.getAnnotationForPosition(9));
236   }
237
238   /**
239    * Test method that returns a map of lists of sequences by sequence name.
240    * 
241    * @throws IOException
242    */
243   @Test(groups ={ "Functional" })
244   public void testGetSequencesByName() throws IOException
245   {
246     final String data = ">Seq1Name\nKQYL\n" + ">Seq2Name\nRFPW\n"
247             + ">Seq1Name\nABCD\n";
248     AlignmentI al = loadAlignment(data, "FASTA");
249     Map<String, List<SequenceI>> map = AlignmentUtils
250             .getSequencesByName(al);
251     assertEquals(2, map.keySet().size());
252     assertEquals(2, map.get("Seq1Name").size());
253     assertEquals("KQYL", map.get("Seq1Name").get(0).getSequenceAsString());
254     assertEquals("ABCD", map.get("Seq1Name").get(1).getSequenceAsString());
255     assertEquals(1, map.get("Seq2Name").size());
256     assertEquals("RFPW", map.get("Seq2Name").get(0).getSequenceAsString());
257   }
258   /**
259    * Helper method to load an alignment and ensure dataset sequences are set up.
260    * 
261    * @param data
262    * @param format TODO
263    * @return
264    * @throws IOException
265    */
266   protected AlignmentI loadAlignment(final String data, String format) throws IOException
267   {
268     AlignmentI a = new FormatAdapter().readFile(data,
269             AppletFormatAdapter.PASTE, format);
270     a.setDataset(null);
271     return a;
272   }
273   
274   /**
275    * Test mapping of protein to cDNA, for the case where we have no sequence
276    * cross-references, so mappings are made first-served 1-1 where sequences
277    * translate.
278    * 
279    * @throws IOException
280    */
281   @Test(groups ={ "Functional" })
282   public void testMapProteinToCdna_noXrefs() throws IOException
283   {
284     List<SequenceI> protseqs = new ArrayList<SequenceI>();
285     protseqs.add(new Sequence("UNIPROT|V12345", "EIQ"));
286     protseqs.add(new Sequence("UNIPROT|V12346", "EIQ"));
287     protseqs.add(new Sequence("UNIPROT|V12347", "SAR"));
288     AlignmentI protein = new Alignment(protseqs.toArray(new SequenceI[3]));
289     protein.setDataset(null);
290
291     List<SequenceI> dnaseqs = new ArrayList<SequenceI>();
292     dnaseqs.add(new Sequence("EMBL|A11111", "TCAGCACGC")); // = SAR
293     dnaseqs.add(new Sequence("EMBL|A22222", "GAGATACAA")); // = EIQ
294     dnaseqs.add(new Sequence("EMBL|A33333", "GAAATCCAG")); // = EIQ
295     dnaseqs.add(new Sequence("EMBL|A44444", "GAAATTCAG")); // = EIQ
296     AlignmentI cdna = new Alignment(dnaseqs.toArray(new SequenceI[4]));
297     cdna.setDataset(null);
298
299     assertTrue(AlignmentUtils.mapProteinToCdna(protein, cdna));
300
301     // 3 mappings made, each from 1 to 1 sequence
302     assertEquals(3, protein.getCodonFrames().size());
303     assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).size());
304     assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).size());
305     assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(2)).size());
306
307     // V12345 mapped to A22222
308     AlignedCodonFrame acf = protein.getCodonFrame(
309             protein.getSequenceAt(0)).get(0);
310     assertEquals(1, acf.getdnaSeqs().length);
311     assertEquals(cdna.getSequenceAt(1).getDatasetSequence(),
312             acf.getdnaSeqs()[0]);
313     Mapping[] protMappings = acf.getProtMappings();
314     assertEquals(1, protMappings.length);
315     MapList mapList = protMappings[0].getMap();
316     assertEquals(3, mapList.getFromRatio());
317     assertEquals(1, mapList.getToRatio());
318     assertTrue(Arrays.equals(new int[]
319     { 1, 9 }, mapList.getFromRanges().get(0)));
320     assertEquals(1, mapList.getFromRanges().size());
321     assertTrue(Arrays.equals(new int[]
322     { 1, 3 }, mapList.getToRanges().get(0)));
323     assertEquals(1, mapList.getToRanges().size());
324
325     // V12346 mapped to A33333
326     acf = protein.getCodonFrame(protein.getSequenceAt(1)).get(0);
327     assertEquals(1, acf.getdnaSeqs().length);
328     assertEquals(cdna.getSequenceAt(2).getDatasetSequence(),
329             acf.getdnaSeqs()[0]);
330
331     // V12347 mapped to A11111
332     acf = protein.getCodonFrame(protein.getSequenceAt(2)).get(0);
333     assertEquals(1, acf.getdnaSeqs().length);
334     assertEquals(cdna.getSequenceAt(0).getDatasetSequence(),
335             acf.getdnaSeqs()[0]);
336
337     // no mapping involving the 'extra' A44444
338     assertTrue(protein.getCodonFrame(cdna.getSequenceAt(3)).isEmpty());
339   }
340
341   /**
342    * Test for the alignSequenceAs method that takes two sequences and a mapping.
343    */
344   @Test(groups ={ "Functional" })
345   public void testAlignSequenceAs_withMapping_noIntrons()
346   {
347     MapList map = new MapList(new int[]
348     { 1, 6 }, new int[]
349     { 1, 2 }, 3, 1);
350
351     /*
352      * No existing gaps in dna:
353      */
354     checkAlignSequenceAs("GGGAAA", "-A-L-", false, false, map,
355             "---GGG---AAA");
356
357     /*
358      * Now introduce gaps in dna but ignore them when realigning.
359      */
360     checkAlignSequenceAs("-G-G-G-A-A-A-", "-A-L-", false, false, map,
361             "---GGG---AAA");
362
363     /*
364      * Now include gaps in dna when realigning. First retaining 'mapped' gaps
365      * only, i.e. those within the exon region.
366      */
367     checkAlignSequenceAs("-G-G--G-A--A-A-", "-A-L-", true, false, map,
368             "---G-G--G---A--A-A");
369
370     /*
371      * Include all gaps in dna when realigning (within and without the exon
372      * region). The leading gap, and the gaps between codons, are subsumed by
373      * the protein alignment gap.
374      */
375     checkAlignSequenceAs("-G-GG--AA-A-", "-A-L-", true, true, map,
376             "---G-GG---AA-A-");
377
378     /*
379      * Include only unmapped gaps in dna when realigning (outside the exon
380      * region). The leading gap, and the gaps between codons, are subsumed by
381      * the protein alignment gap.
382      */
383     checkAlignSequenceAs("-G-GG--AA-A-", "-A-L-", false, true, map,
384             "---GGG---AAA-");
385   }
386
387   /**
388    * Test for the alignSequenceAs method that takes two sequences and a mapping.
389    */
390   @Test(groups ={ "Functional" })
391   public void testAlignSequenceAs_withMapping_withIntrons()
392   {
393     /*
394      * Exons at codon 2 (AAA) and 4 (TTT)
395      */
396     MapList map = new MapList(new int[]
397     { 4, 6, 10, 12 }, new int[]
398     { 1, 2 }, 3, 1);
399
400     /*
401      * Simple case: no gaps in dna
402      */
403     checkAlignSequenceAs("GGGAAACCCTTTGGG", "--A-L-", false, false, map,
404             "GGG---AAACCCTTTGGG");
405
406     /*
407      * Add gaps to dna - but ignore when realigning.
408      */
409     checkAlignSequenceAs("-G-G-G--A--A---AC-CC-T-TT-GG-G-", "--A-L-",
410             false, false, map, "GGG---AAACCCTTTGGG");
411
412     /*
413      * Add gaps to dna - include within exons only when realigning.
414      */
415     checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
416             true, false, map, "GGG---A--A---ACCCT-TTGGG");
417
418     /*
419      * Include gaps outside exons only when realigning.
420      */
421     checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
422             false, true, map, "-G-G-GAAAC-CCTTT-GG-G-");
423
424     /*
425      * Include gaps following first intron if we are 'preserving mapped gaps'
426      */
427     checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
428             true, true, map, "-G-G-G--A--A---A-C-CC-T-TT-GG-G-");
429
430     /*
431      * Include all gaps in dna when realigning.
432      */
433     checkAlignSequenceAs("-G-G-G--A--A---A-C-CC-T-TT-GG-G-", "--A-L-",
434             true, true, map, "-G-G-G--A--A---A-C-CC-T-TT-GG-G-");
435   }
436
437   /**
438    * Test for the case where not all of the protein sequence is mapped to cDNA.
439    */
440   @Test(groups ={ "Functional" })
441   public void testAlignSequenceAs_withMapping_withUnmappedProtein()
442   {
443     
444     /*
445      * Exons at codon 2 (AAA) and 4 (TTT) mapped to A and P
446      */
447     final MapList map = new MapList(new int[]
448     { 4, 6, 10, 12 }, new int[]
449     { 1, 1, 3, 3 }, 3, 1);
450     
451
452     /*
453      * Expect alignment does nothing (aborts realignment). Change this test
454      * first if different behaviour wanted.
455      */
456     checkAlignSequenceAs("GGGAAACCCTTTGGG", "-A-L-P-", false,
457             false, map, "GGGAAACCCTTTGGG");
458   }
459
460   /**
461    * Helper method that performs and verifies the method under test.
462    * 
463    * @param dnaSeq
464    * @param proteinSeq
465    * @param preserveMappedGaps
466    * @param preserveUnmappedGaps
467    * @param map
468    * @param expected
469    */
470   protected void checkAlignSequenceAs(final String dnaSeq,
471           final String proteinSeq, final boolean preserveMappedGaps,
472           final boolean preserveUnmappedGaps, MapList map,
473           final String expected)
474   {
475     SequenceI dna = new Sequence("Seq1", dnaSeq);
476     dna.createDatasetSequence();
477     SequenceI protein = new Sequence("Seq1", proteinSeq);
478     protein.createDatasetSequence();
479     AlignedCodonFrame acf = new AlignedCodonFrame();
480     acf.addMap(dna.getDatasetSequence(), protein.getDatasetSequence(), map);
481
482     AlignmentUtils.alignSequenceAs(dna, protein, acf, "---", '-',
483             preserveMappedGaps, preserveUnmappedGaps);
484     assertEquals(expected, dna.getSequenceAsString());
485   }
486
487   /**
488    * Test for the alignSequenceAs method where we preserve gaps in introns only.
489    */
490   @Test(groups ={ "Functional" })
491   public void testAlignSequenceAs_keepIntronGapsOnly()
492   {
493
494     /*
495      * Intron GGGAAA followed by exon CCCTTT
496      */
497     MapList map = new MapList(new int[]
498     { 7, 12 }, new int[]
499     { 1, 2 }, 3, 1);
500     
501     checkAlignSequenceAs("GG-G-AA-A-C-CC-T-TT", "AL",
502             false, true, map, "GG-G-AA-ACCCTTT");
503   }
504
505   /**
506    * Test for the method that generates an aligned translated sequence from one
507    * mapping.
508    */
509   @Test(groups ={ "Functional" })
510   public void testGetAlignedTranslation_dnaLikeProtein()
511   {
512     // dna alignment will be replaced
513     SequenceI dna = new Sequence("Seq1", "T-G-CC-A--T-TAC-CAG-");
514     dna.createDatasetSequence();
515     // protein alignment will be 'applied' to dna
516     SequenceI protein = new Sequence("Seq1", "-CH-Y--Q-");
517     protein.createDatasetSequence();
518     MapList map = new MapList(new int[]
519     { 1, 12 }, new int[]
520     { 1, 4 }, 3, 1);
521     AlignedCodonFrame acf = new AlignedCodonFrame();
522     acf.addMap(dna.getDatasetSequence(), protein.getDatasetSequence(), map);
523
524     final SequenceI aligned = AlignmentUtils
525                 .getAlignedTranslation(protein, '-', acf);
526     assertEquals("---TGCCAT---TAC------CAG---", aligned.getSequenceAsString());
527     assertSame(aligned.getDatasetSequence(), dna.getDatasetSequence());
528   }
529
530   /**
531    * Test the method that realigns protein to match mapped codon alignment.
532    */
533   @Test(groups ={ "Functional" })
534   public void testAlignProteinAsDna()
535   {
536     // seq1 codons are [1,2,3] [4,5,6] [7,8,9] [10,11,12]
537     SequenceI dna1 = new Sequence("Seq1", "TGCCATTACCAG-");
538     // seq2 codons are [1,3,4] [5,6,7] [8,9,10] [11,12,13]
539     SequenceI dna2 = new Sequence("Seq2", "T-GCCATTACCAG");
540     // seq3 codons are [1,2,3] [4,5,7] [8,9,10] [11,12,13]
541     SequenceI dna3 = new Sequence("Seq3", "TGCCA-TTACCAG");
542     AlignmentI dna = new Alignment(new SequenceI[]
543     { dna1, dna2, dna3 });
544     dna.setDataset(null);
545
546     // protein alignment will be realigned like dna
547     SequenceI prot1 = new Sequence("Seq1", "CHYQ");
548     SequenceI prot2 = new Sequence("Seq2", "CHYQ");
549     SequenceI prot3 = new Sequence("Seq3", "CHYQ");
550     AlignmentI protein = new Alignment(new SequenceI[]
551     { prot1, prot2, prot3 });
552     protein.setDataset(null);
553
554     MapList map = new MapList(new int[]
555     { 1, 12 }, new int[]
556     { 1, 4 }, 3, 1);
557     AlignedCodonFrame acf = new AlignedCodonFrame();
558     acf.addMap(dna1.getDatasetSequence(), prot1.getDatasetSequence(), map);
559     acf.addMap(dna2.getDatasetSequence(), prot2.getDatasetSequence(), map);
560     acf.addMap(dna3.getDatasetSequence(), prot3.getDatasetSequence(), map);
561     protein.setCodonFrames(Collections.singleton(acf));
562
563     /*
564      * Translated codon order is [1,2,3] [1,3,4] [4,5,6] [4,5,7] [5,6,7] [7,8,9]
565      * [8,9,10] [10,11,12] [11,12,13]
566      */
567     AlignmentUtils.alignProteinAsDna(protein, dna);
568     assertEquals("C-H--Y-Q-", prot1.getSequenceAsString());
569     assertEquals("-C--H-Y-Q", prot2.getSequenceAsString());
570     assertEquals("C--H--Y-Q", prot3.getSequenceAsString());
571   }
572
573   /**
574    * Test the method that tests whether a CDNA sequence translates to a protein
575    * sequence
576    */
577   @Test(groups ={ "Functional" })
578   public void testTranslatesAs()
579   {
580     assertTrue(AlignmentUtils.translatesAs("tttcccaaaggg".toCharArray(), 0,
581             "FPKG".toCharArray()));
582     // with start codon
583     assertTrue(AlignmentUtils.translatesAs("atgtttcccaaaggg".toCharArray(),
584             3, "FPKG".toCharArray()));
585     // with stop codon1
586     assertTrue(AlignmentUtils.translatesAs("tttcccaaagggtaa".toCharArray(),
587             0, "FPKG".toCharArray()));
588     // with stop codon2
589     assertTrue(AlignmentUtils.translatesAs("tttcccaaagggtag".toCharArray(),
590             0, "FPKG".toCharArray()));
591     // with stop codon3
592     assertTrue(AlignmentUtils.translatesAs("tttcccaaagggtga".toCharArray(),
593             0, "FPKG".toCharArray()));
594     // with start and stop codon1
595     assertTrue(AlignmentUtils.translatesAs(
596             "atgtttcccaaaggtaa".toCharArray(), 3, "FPKG".toCharArray()));
597     // with start and stop codon2
598     assertTrue(AlignmentUtils.translatesAs(
599             "atgtttcccaaaggtag".toCharArray(), 3, "FPKG".toCharArray()));
600     // with start and stop codon3
601     assertTrue(AlignmentUtils.translatesAs(
602             "atgtttcccaaaggtga".toCharArray(), 3, "FPKG".toCharArray()));
603
604     // wrong protein
605     assertFalse(AlignmentUtils.translatesAs("tttcccaaaggg".toCharArray(),
606             0,
607             "FPMG".toCharArray()));
608   }
609
610   /**
611    * Test mapping of protein to cDNA, for cases where the cDNA has start and/or
612    * stop codons in addition to the protein coding sequence.
613    * 
614    * @throws IOException
615    */
616   @Test(groups ={ "Functional" })
617   public void testMapProteinToCdna_withStartAndStopCodons()
618           throws IOException
619   {
620     List<SequenceI> protseqs = new ArrayList<SequenceI>();
621     protseqs.add(new Sequence("UNIPROT|V12345", "EIQ"));
622     protseqs.add(new Sequence("UNIPROT|V12346", "EIQ"));
623     protseqs.add(new Sequence("UNIPROT|V12347", "SAR"));
624     AlignmentI protein = new Alignment(protseqs.toArray(new SequenceI[3]));
625     protein.setDataset(null);
626   
627     List<SequenceI> dnaseqs = new ArrayList<SequenceI>();
628     // start + SAR:
629     dnaseqs.add(new Sequence("EMBL|A11111", "ATGTCAGCACGC"));
630     // = EIQ + stop
631     dnaseqs.add(new Sequence("EMBL|A22222", "GAGATACAATAA"));
632     // = start +EIQ + stop
633     dnaseqs.add(new Sequence("EMBL|A33333", "ATGGAAATCCAGTAG"));
634     dnaseqs.add(new Sequence("EMBL|A44444", "GAAATTCAG"));
635     AlignmentI cdna = new Alignment(dnaseqs.toArray(new SequenceI[4]));
636     cdna.setDataset(null);
637   
638     assertTrue(AlignmentUtils.mapProteinToCdna(protein, cdna));
639
640     // 3 mappings made, each from 1 to 1 sequence
641     assertEquals(3, protein.getCodonFrames().size());
642     assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).size());
643     assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).size());
644     assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(2)).size());
645   
646     // V12345 mapped from A22222
647     AlignedCodonFrame acf = protein.getCodonFrame(
648             protein.getSequenceAt(0)).get(0);
649     assertEquals(1, acf.getdnaSeqs().length);
650     assertEquals(cdna.getSequenceAt(1).getDatasetSequence(),
651             acf.getdnaSeqs()[0]);
652     Mapping[] protMappings = acf.getProtMappings();
653     assertEquals(1, protMappings.length);
654     MapList mapList = protMappings[0].getMap();
655     assertEquals(3, mapList.getFromRatio());
656     assertEquals(1, mapList.getToRatio());
657     assertTrue(Arrays.equals(new int[]
658     { 1, 9 }, mapList.getFromRanges().get(0)));
659     assertEquals(1, mapList.getFromRanges().size());
660     assertTrue(Arrays.equals(new int[]
661     { 1, 3 }, mapList.getToRanges().get(0)));
662     assertEquals(1, mapList.getToRanges().size());
663
664     // V12346 mapped from A33333 starting position 4
665     acf = protein.getCodonFrame(protein.getSequenceAt(1)).get(0);
666     assertEquals(1, acf.getdnaSeqs().length);
667     assertEquals(cdna.getSequenceAt(2).getDatasetSequence(),
668             acf.getdnaSeqs()[0]);
669     protMappings = acf.getProtMappings();
670     assertEquals(1, protMappings.length);
671     mapList = protMappings[0].getMap();
672     assertEquals(3, mapList.getFromRatio());
673     assertEquals(1, mapList.getToRatio());
674     assertTrue(Arrays.equals(new int[]
675     { 4, 12 }, mapList.getFromRanges().get(0)));
676     assertEquals(1, mapList.getFromRanges().size());
677     assertTrue(Arrays.equals(new int[]
678     { 1, 3 }, mapList.getToRanges().get(0)));
679     assertEquals(1, mapList.getToRanges().size());
680   
681     // V12347 mapped to A11111 starting position 4
682     acf = protein.getCodonFrame(protein.getSequenceAt(2)).get(0);
683     assertEquals(1, acf.getdnaSeqs().length);
684     assertEquals(cdna.getSequenceAt(0).getDatasetSequence(),
685             acf.getdnaSeqs()[0]);
686     protMappings = acf.getProtMappings();
687     assertEquals(1, protMappings.length);
688     mapList = protMappings[0].getMap();
689     assertEquals(3, mapList.getFromRatio());
690     assertEquals(1, mapList.getToRatio());
691     assertTrue(Arrays.equals(new int[]
692     { 4, 12 }, mapList.getFromRanges().get(0)));
693     assertEquals(1, mapList.getFromRanges().size());
694     assertTrue(Arrays.equals(new int[]
695     { 1, 3 }, mapList.getToRanges().get(0)));
696     assertEquals(1, mapList.getToRanges().size());
697   
698     // no mapping involving the 'extra' A44444
699     assertTrue(protein.getCodonFrame(cdna.getSequenceAt(3)).isEmpty());
700   }
701
702   /**
703    * Test mapping of protein to cDNA, for the case where we have some sequence
704    * cross-references. Verify that 1-to-many mappings are made where
705    * cross-references exist and sequences are mappable.
706    * 
707    * @throws IOException
708    */
709   @Test(groups ={ "Functional" })
710   public void testMapProteinToCdna_withXrefs() throws IOException
711   {
712     List<SequenceI> protseqs = new ArrayList<SequenceI>();
713     protseqs.add(new Sequence("UNIPROT|V12345", "EIQ"));
714     protseqs.add(new Sequence("UNIPROT|V12346", "EIQ"));
715     protseqs.add(new Sequence("UNIPROT|V12347", "SAR"));
716     AlignmentI protein = new Alignment(protseqs.toArray(new SequenceI[3]));
717     protein.setDataset(null);
718   
719     List<SequenceI> dnaseqs = new ArrayList<SequenceI>();
720     dnaseqs.add(new Sequence("EMBL|A11111", "TCAGCACGC")); // = SAR
721     dnaseqs.add(new Sequence("EMBL|A22222", "ATGGAGATACAA")); // = start + EIQ
722     dnaseqs.add(new Sequence("EMBL|A33333", "GAAATCCAG")); // = EIQ
723     dnaseqs.add(new Sequence("EMBL|A44444", "GAAATTCAG")); // = EIQ
724     dnaseqs.add(new Sequence("EMBL|A55555", "GAGATTCAG")); // = EIQ
725     AlignmentI cdna = new Alignment(dnaseqs.toArray(new SequenceI[5]));
726     cdna.setDataset(null);
727   
728     // Xref A22222 to V12345 (should get mapped)
729     dnaseqs.get(1).addDBRef(new DBRefEntry("UNIPROT", "1", "V12345"));
730     // Xref V12345 to A44444 (should get mapped)
731     protseqs.get(0).addDBRef(new DBRefEntry("EMBL", "1", "A44444"));
732     // Xref A33333 to V12347 (sequence mismatch - should not get mapped)
733     dnaseqs.get(2).addDBRef(new DBRefEntry("UNIPROT", "1", "V12347"));
734     // as V12345 is mapped to A22222 and A44444, this leaves V12346 unmapped.
735     // it should get paired up with the unmapped A33333
736     // A11111 should be mapped to V12347
737     // A55555 is spare and has no xref so is not mapped
738
739     assertTrue(AlignmentUtils.mapProteinToCdna(protein, cdna));
740
741     // 4 protein mappings made for 3 proteins, 2 to V12345, 1 each to V12346/7
742     assertEquals(3, protein.getCodonFrames().size());
743     assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).size());
744     assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).size());
745     assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(2)).size());
746
747     // one mapping for each of the first 4 cDNA sequences
748     assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(0)).size());
749     assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(1)).size());
750     assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(2)).size());
751     assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(3)).size());
752   
753     // V12345 mapped to A22222 and A44444
754     AlignedCodonFrame acf = protein.getCodonFrame(
755             protein.getSequenceAt(0)).get(0);
756     assertEquals(2, acf.getdnaSeqs().length);
757     assertEquals(cdna.getSequenceAt(1).getDatasetSequence(),
758             acf.getdnaSeqs()[0]);
759     assertEquals(cdna.getSequenceAt(3).getDatasetSequence(),
760             acf.getdnaSeqs()[1]);
761   
762     // V12346 mapped to A33333
763     acf = protein.getCodonFrame(protein.getSequenceAt(1)).get(0);
764     assertEquals(1, acf.getdnaSeqs().length);
765     assertEquals(cdna.getSequenceAt(2).getDatasetSequence(),
766             acf.getdnaSeqs()[0]);
767   
768     // V12347 mapped to A11111
769     acf = protein.getCodonFrame(protein.getSequenceAt(2)).get(0);
770     assertEquals(1, acf.getdnaSeqs().length);
771     assertEquals(cdna.getSequenceAt(0).getDatasetSequence(),
772             acf.getdnaSeqs()[0]);
773   
774     // no mapping involving the 'extra' A55555
775     assertTrue(protein.getCodonFrame(cdna.getSequenceAt(4)).isEmpty());
776   }
777
778   /**
779    * Test mapping of protein to cDNA, for the case where we have some sequence
780    * cross-references. Verify that once we have made an xref mapping we don't
781    * also map un-xrefd sequeces.
782    * 
783    * @throws IOException
784    */
785   @Test(groups ={ "Functional" })
786   public void testMapProteinToCdna_prioritiseXrefs() throws IOException
787   {
788     List<SequenceI> protseqs = new ArrayList<SequenceI>();
789     protseqs.add(new Sequence("UNIPROT|V12345", "EIQ"));
790     protseqs.add(new Sequence("UNIPROT|V12346", "EIQ"));
791     AlignmentI protein = new Alignment(
792             protseqs.toArray(new SequenceI[protseqs.size()]));
793     protein.setDataset(null);
794   
795     List<SequenceI> dnaseqs = new ArrayList<SequenceI>();
796     dnaseqs.add(new Sequence("EMBL|A11111", "GAAATCCAG")); // = EIQ
797     dnaseqs.add(new Sequence("EMBL|A22222", "GAAATTCAG")); // = EIQ
798     AlignmentI cdna = new Alignment(dnaseqs.toArray(new SequenceI[dnaseqs
799             .size()]));
800     cdna.setDataset(null);
801   
802     // Xref A22222 to V12345 (should get mapped)
803     // A11111 should then be mapped to the unmapped V12346
804     dnaseqs.get(1).addDBRef(new DBRefEntry("UNIPROT", "1", "V12345"));
805   
806     assertTrue(AlignmentUtils.mapProteinToCdna(protein, cdna));
807   
808     // 2 protein mappings made
809     assertEquals(2, protein.getCodonFrames().size());
810     assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(0)).size());
811     assertEquals(1, protein.getCodonFrame(protein.getSequenceAt(1)).size());
812   
813     // one mapping for each of the cDNA sequences
814     assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(0)).size());
815     assertEquals(1, protein.getCodonFrame(cdna.getSequenceAt(1)).size());
816   
817     // V12345 mapped to A22222
818     AlignedCodonFrame acf = protein.getCodonFrame(protein.getSequenceAt(0))
819             .get(0);
820     assertEquals(1, acf.getdnaSeqs().length);
821     assertEquals(cdna.getSequenceAt(1).getDatasetSequence(),
822             acf.getdnaSeqs()[0]);
823   
824     // V12346 mapped to A11111
825     acf = protein.getCodonFrame(protein.getSequenceAt(1)).get(0);
826     assertEquals(1, acf.getdnaSeqs().length);
827     assertEquals(cdna.getSequenceAt(0).getDatasetSequence(),
828             acf.getdnaSeqs()[0]);
829   }
830
831   /**
832    * Test the method that shows or hides sequence annotations by type(s) and
833    * selection group.
834    */
835   @Test(groups ={ "Functional" })
836   public void testShowOrHideSequenceAnnotations()
837   {
838     SequenceI seq1 = new Sequence("Seq1", "AAA");
839     SequenceI seq2 = new Sequence("Seq2", "BBB");
840     SequenceI seq3 = new Sequence("Seq3", "CCC");
841     Annotation[] anns = new Annotation[]
842     { new Annotation(2f) };
843     AlignmentAnnotation ann1 = new AlignmentAnnotation("Structure", "ann1",
844             anns);
845     ann1.setSequenceRef(seq1);
846     AlignmentAnnotation ann2 = new AlignmentAnnotation("Structure", "ann2",
847             anns);
848     ann2.setSequenceRef(seq2);
849     AlignmentAnnotation ann3 = new AlignmentAnnotation("Structure", "ann3",
850             anns);
851     AlignmentAnnotation ann4 = new AlignmentAnnotation("Temp", "ann4", anns);
852     ann4.setSequenceRef(seq1);
853     AlignmentAnnotation ann5 = new AlignmentAnnotation("Temp", "ann5", anns);
854     ann5.setSequenceRef(seq2);
855     AlignmentAnnotation ann6 = new AlignmentAnnotation("Temp", "ann6", anns);
856     AlignmentI al = new Alignment(new SequenceI[] {seq1, seq2, seq3});
857     al.addAnnotation(ann1); // Structure for Seq1
858     al.addAnnotation(ann2); // Structure for Seq2
859     al.addAnnotation(ann3); // Structure for no sequence
860     al.addAnnotation(ann4); // Temp for seq1
861     al.addAnnotation(ann5); // Temp for seq2
862     al.addAnnotation(ann6); // Temp for no sequence
863     List<String> types = new ArrayList<String>();
864     List<SequenceI> scope = new ArrayList<SequenceI>();
865
866     /*
867      * Set all sequence related Structure to hidden (ann1, ann2)
868      */
869     types.add("Structure");
870     AlignmentUtils.showOrHideSequenceAnnotations(al, types, null, false,
871             false);
872     assertFalse(ann1.visible);
873     assertFalse(ann2.visible);
874     assertTrue(ann3.visible); // not sequence-related, not affected
875     assertTrue(ann4.visible); // not Structure, not affected
876     assertTrue(ann5.visible); // "
877     assertTrue(ann6.visible); // not sequence-related, not affected
878
879     /*
880      * Set Temp in {seq1, seq3} to hidden
881      */
882     types.clear();
883     types.add("Temp");
884     scope.add(seq1);
885     scope.add(seq3);
886     AlignmentUtils.showOrHideSequenceAnnotations(al, types, scope, false,
887             false);
888     assertFalse(ann1.visible); // unchanged
889     assertFalse(ann2.visible); // unchanged
890     assertTrue(ann3.visible); // not sequence-related, not affected
891     assertFalse(ann4.visible); // Temp for seq1 hidden
892     assertTrue(ann5.visible); // not in scope, not affected
893     assertTrue(ann6.visible); // not sequence-related, not affected
894
895     /*
896      * Set Temp in all sequences to hidden
897      */
898     types.clear();
899     types.add("Temp");
900     scope.add(seq1);
901     scope.add(seq3);
902     AlignmentUtils.showOrHideSequenceAnnotations(al, types, null, false,
903             false);
904     assertFalse(ann1.visible); // unchanged
905     assertFalse(ann2.visible); // unchanged
906     assertTrue(ann3.visible); // not sequence-related, not affected
907     assertFalse(ann4.visible); // Temp for seq1 hidden
908     assertFalse(ann5.visible); // Temp for seq2 hidden
909     assertTrue(ann6.visible); // not sequence-related, not affected
910
911     /*
912      * Set all types in {seq1, seq3} to visible
913      */
914     types.clear();
915     scope.clear();
916     scope.add(seq1);
917     scope.add(seq3);
918     AlignmentUtils.showOrHideSequenceAnnotations(al, types, scope, true,
919             true);
920     assertTrue(ann1.visible); // Structure for seq1 set visible
921     assertFalse(ann2.visible); // not in scope, unchanged
922     assertTrue(ann3.visible); // not sequence-related, not affected
923     assertTrue(ann4.visible); // Temp for seq1 set visible
924     assertFalse(ann5.visible); // not in scope, unchanged
925     assertTrue(ann6.visible); // not sequence-related, not affected
926
927     /*
928      * Set all types in all scope to hidden
929      */
930     AlignmentUtils.showOrHideSequenceAnnotations(al, types, null, true,
931             false);
932     assertFalse(ann1.visible);
933     assertFalse(ann2.visible);
934     assertTrue(ann3.visible); // not sequence-related, not affected
935     assertFalse(ann4.visible);
936     assertFalse(ann5.visible);
937     assertTrue(ann6.visible); // not sequence-related, not affected
938   }
939
940   /**
941    * Tests for the method that checks if one sequence cross-references another
942    */
943   @Test(groups ={ "Functional" })
944   public void testHasCrossRef()
945   {
946     assertFalse(AlignmentUtils.hasCrossRef(null, null));
947     SequenceI seq1 = new Sequence("EMBL|A12345", "ABCDEF");
948     assertFalse(AlignmentUtils.hasCrossRef(seq1, null));
949     assertFalse(AlignmentUtils.hasCrossRef(null, seq1));
950     SequenceI seq2 = new Sequence("UNIPROT|V20192", "ABCDEF");
951     assertFalse(AlignmentUtils.hasCrossRef(seq1, seq2));
952   
953     // different ref
954     seq1.addDBRef(new DBRefEntry("UNIPROT", "1", "v20193"));
955     assertFalse(AlignmentUtils.hasCrossRef(seq1, seq2));
956   
957     // case-insensitive; version number is ignored
958     seq1.addDBRef(new DBRefEntry("UNIPROT", "1", "v20192"));
959     assertTrue(AlignmentUtils.hasCrossRef(seq1, seq2));
960   
961     // right case!
962     seq1.addDBRef(new DBRefEntry("UNIPROT", "1", "V20192"));
963     assertTrue(AlignmentUtils.hasCrossRef(seq1, seq2));
964     // test is one-way only
965     assertFalse(AlignmentUtils.hasCrossRef(seq2, seq1));
966   }
967
968   /**
969    * Tests for the method that checks if either sequence cross-references the
970    * other
971    */
972   @Test(groups ={ "Functional" })
973   public void testHaveCrossRef()
974   {
975     assertFalse(AlignmentUtils.hasCrossRef(null, null));
976     SequenceI seq1 = new Sequence("EMBL|A12345", "ABCDEF");
977     assertFalse(AlignmentUtils.haveCrossRef(seq1, null));
978     assertFalse(AlignmentUtils.haveCrossRef(null, seq1));
979     SequenceI seq2 = new Sequence("UNIPROT|V20192", "ABCDEF");
980     assertFalse(AlignmentUtils.haveCrossRef(seq1, seq2));
981   
982     seq1.addDBRef(new DBRefEntry("UNIPROT", "1", "V20192"));
983     assertTrue(AlignmentUtils.haveCrossRef(seq1, seq2));
984     // next is true for haveCrossRef, false for hasCrossRef
985     assertTrue(AlignmentUtils.haveCrossRef(seq2, seq1));
986   
987     // now the other way round
988     seq1.setDBRef(null);
989     seq2.addDBRef(new DBRefEntry("EMBL", "1", "A12345"));
990     assertTrue(AlignmentUtils.haveCrossRef(seq1, seq2));
991     assertTrue(AlignmentUtils.haveCrossRef(seq2, seq1));
992   
993     // now both ways
994     seq1.addDBRef(new DBRefEntry("UNIPROT", "1", "V20192"));
995     assertTrue(AlignmentUtils.haveCrossRef(seq1, seq2));
996     assertTrue(AlignmentUtils.haveCrossRef(seq2, seq1));
997   }
998
999   /**
1000    * Test the method that extracts the exon-only part of a dna alignment.
1001    */
1002   @Test(groups ={ "Functional" })
1003   public void testMakeExonAlignment()
1004   {
1005     SequenceI dna1 = new Sequence("dna1", "aaaGGGcccTTTaaa");
1006     SequenceI dna2 = new Sequence("dna2", "GGGcccTTTaaaCCC");
1007     SequenceI pep1 = new Sequence("pep1", "GF");
1008     SequenceI pep2 = new Sequence("pep2", "GFP");
1009     dna1.createDatasetSequence();
1010     dna2.createDatasetSequence();
1011     pep1.createDatasetSequence();
1012     pep2.createDatasetSequence();
1013
1014     Set<AlignedCodonFrame> mappings = new HashSet<AlignedCodonFrame>();
1015     MapList map = new MapList(new int[]
1016     { 4, 6, 10, 12 }, new int[]
1017     { 1, 2 }, 3, 1);
1018     AlignedCodonFrame acf = new AlignedCodonFrame();
1019     acf.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(), map);
1020     mappings.add(acf);
1021     map = new MapList(new int[]
1022     { 1, 3, 7, 9, 13, 15 }, new int[]
1023     { 1, 3 }, 3, 1);
1024     acf = new AlignedCodonFrame();
1025     acf.addMap(dna2.getDatasetSequence(), pep2.getDatasetSequence(), map);
1026     mappings.add(acf);
1027     
1028     AlignmentI exons = AlignmentUtils.makeExonAlignment(new SequenceI[]
1029     { dna1, dna2 }, mappings);
1030     assertEquals(2, exons.getSequences().size());
1031     assertEquals("GGGTTT", exons.getSequenceAt(0).getSequenceAsString());
1032     assertEquals("GGGTTTCCC", exons.getSequenceAt(1).getSequenceAsString());
1033
1034     /*
1035      * Verify updated mappings
1036      */
1037     assertEquals(2, mappings.size());
1038
1039     /*
1040      * Mapping from pep1 to GGGTTT in first new exon sequence
1041      */
1042     List<AlignedCodonFrame> pep1Mapping = MappingUtils
1043             .findMappingsForSequence(pep1, mappings);
1044     assertEquals(1, pep1Mapping.size());
1045     // map G to GGG
1046     SearchResults sr = MappingUtils.buildSearchResults(pep1, 1, mappings);
1047     assertEquals(1, sr.getResults().size());
1048     Match m = sr.getResults().get(0);
1049     assertEquals(exons.getSequenceAt(0).getDatasetSequence(),
1050             m.getSequence());
1051     assertEquals(1, m.getStart());
1052     assertEquals(3, m.getEnd());
1053     // map F to TTT
1054     sr = MappingUtils.buildSearchResults(pep1, 2, mappings);
1055     m = sr.getResults().get(0);
1056     assertEquals(exons.getSequenceAt(0).getDatasetSequence(),
1057             m.getSequence());
1058     assertEquals(4, m.getStart());
1059     assertEquals(6, m.getEnd());
1060
1061     /*
1062      * Mapping from pep2 to GGGTTTCCC in second new exon sequence
1063      */
1064     List<AlignedCodonFrame> pep2Mapping = MappingUtils
1065             .findMappingsForSequence(pep2, mappings);
1066     assertEquals(1, pep2Mapping.size());
1067     // map G to GGG
1068     sr = MappingUtils.buildSearchResults(pep2, 1, mappings);
1069     assertEquals(1, sr.getResults().size());
1070     m = sr.getResults().get(0);
1071     assertEquals(exons.getSequenceAt(1).getDatasetSequence(),
1072             m.getSequence());
1073     assertEquals(1, m.getStart());
1074     assertEquals(3, m.getEnd());
1075     // map F to TTT
1076     sr = MappingUtils.buildSearchResults(pep2, 2, mappings);
1077     m = sr.getResults().get(0);
1078     assertEquals(exons.getSequenceAt(1).getDatasetSequence(),
1079             m.getSequence());
1080     assertEquals(4, m.getStart());
1081     assertEquals(6, m.getEnd());
1082     // map P to CCC
1083     sr = MappingUtils.buildSearchResults(pep2, 3, mappings);
1084     m = sr.getResults().get(0);
1085     assertEquals(exons.getSequenceAt(1).getDatasetSequence(),
1086             m.getSequence());
1087     assertEquals(7, m.getStart());
1088     assertEquals(9, m.getEnd());
1089   }
1090
1091   /**
1092    * Test the method that makes an exon-only sequence from a DNA sequence and
1093    * its product mapping. Test includes the expected case that the DNA sequence
1094    * already has a protein product (Uniprot translation) which in turn has an
1095    * x-ref to the EMBLCDS record.
1096    */
1097   @Test(groups ={ "Functional" })
1098   public void testMakeExonSequences()
1099   {
1100     SequenceI dna1 = new Sequence("dna1", "aaaGGGcccTTTaaa");
1101     SequenceI pep1 = new Sequence("pep1", "GF");
1102     dna1.createDatasetSequence();
1103     pep1.createDatasetSequence();
1104     pep1.getDatasetSequence().addDBRef(
1105             new DBRefEntry("EMBLCDS", "2", "A12345"));
1106
1107     /*
1108      * Make the mapping from dna to protein. The protein sequence has a DBRef to
1109      * EMBLCDS|A12345.
1110      */
1111     Set<AlignedCodonFrame> mappings = new HashSet<AlignedCodonFrame>();
1112     MapList map = new MapList(new int[]
1113     { 4, 6, 10, 12 }, new int[]
1114     { 1, 2 }, 3, 1);
1115     AlignedCodonFrame acf = new AlignedCodonFrame();
1116     acf.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(), map);
1117     mappings.add(acf);
1118
1119     AlignedCodonFrame newMapping = new AlignedCodonFrame();
1120     List<SequenceI> exons = AlignmentUtils.makeExonSequences(dna1, acf,
1121             newMapping);
1122     assertEquals(1, exons.size());
1123     SequenceI exon = exons.get(0);
1124
1125     assertEquals("GGGTTT", exon.getSequenceAsString());
1126     assertEquals("dna1|A12345", exon.getName());
1127     assertEquals(1, exon.getDBRef().length);
1128     DBRefEntry cdsRef = exon.getDBRef()[0];
1129     assertEquals("EMBLCDS", cdsRef.getSource());
1130     assertEquals("2", cdsRef.getVersion());
1131     assertEquals("A12345", cdsRef.getAccessionId());
1132   }
1133
1134   /**
1135    * Test the method that makes an exon-only alignment from a DNA sequence and
1136    * its product mappings, for the case where there are multiple exon mappings
1137    * to different protein products.
1138    */
1139   @Test(groups ={ "Functional" })
1140   public void testMakeExonAlignment_multipleProteins()
1141   {
1142     SequenceI dna1 = new Sequence("dna1", "aaaGGGcccTTTaaa");
1143     SequenceI pep1 = new Sequence("pep1", "GF"); // GGGTTT
1144     SequenceI pep2 = new Sequence("pep2", "KP"); // aaaccc
1145     SequenceI pep3 = new Sequence("pep3", "KF"); // aaaTTT
1146     dna1.createDatasetSequence();
1147     pep1.createDatasetSequence();
1148     pep2.createDatasetSequence();
1149     pep3.createDatasetSequence();
1150     pep1.getDatasetSequence().addDBRef(
1151             new DBRefEntry("EMBLCDS", "2", "A12345"));
1152     pep2.getDatasetSequence().addDBRef(
1153             new DBRefEntry("EMBLCDS", "3", "A12346"));
1154     pep3.getDatasetSequence().addDBRef(
1155             new DBRefEntry("EMBLCDS", "4", "A12347"));
1156
1157     /*
1158      * Make the mappings from dna to protein. Using LinkedHashset is a
1159      * convenience so results are in the input order. There is no assertion that
1160      * the generated exon sequences are in any particular order.
1161      */
1162     Set<AlignedCodonFrame> mappings = new LinkedHashSet<AlignedCodonFrame>();
1163     // map ...GGG...TTT to GF
1164     MapList map = new MapList(new int[]
1165     { 4, 6, 10, 12 }, new int[]
1166     { 1, 2 }, 3, 1);
1167     AlignedCodonFrame acf = new AlignedCodonFrame();
1168     acf.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(), map);
1169     mappings.add(acf);
1170
1171     // map aaa...ccc to KP
1172     map = new MapList(new int[]
1173     { 1, 3, 7, 9 }, new int[]
1174     { 1, 2 }, 3, 1);
1175     acf = new AlignedCodonFrame();
1176     acf.addMap(dna1.getDatasetSequence(), pep2.getDatasetSequence(), map);
1177     mappings.add(acf);
1178
1179     // map aaa......TTT to KF
1180     map = new MapList(new int[]
1181     { 1, 3, 10, 12 }, new int[]
1182     { 1, 2 }, 3, 1);
1183     acf = new AlignedCodonFrame();
1184     acf.addMap(dna1.getDatasetSequence(), pep3.getDatasetSequence(), map);
1185     mappings.add(acf);
1186
1187     /*
1188      * Create the Exon alignment; also replaces the dna-to-protein mappings with
1189      * exon-to-protein and exon-to-dna mappings
1190      */
1191     AlignmentI exal = AlignmentUtils.makeExonAlignment(new SequenceI[]
1192     { dna1 }, mappings);
1193
1194     /*
1195      * Verify we have 3 exon sequences, mapped to pep1/2/3 respectively
1196      */
1197     List<SequenceI> exons = exal.getSequences();
1198     assertEquals(3, exons.size());
1199
1200     SequenceI exon = exons.get(0);
1201     assertEquals("GGGTTT", exon.getSequenceAsString());
1202     assertEquals("dna1|A12345", exon.getName());
1203     assertEquals(1, exon.getDBRef().length);
1204     DBRefEntry cdsRef = exon.getDBRef()[0];
1205     assertEquals("EMBLCDS", cdsRef.getSource());
1206     assertEquals("2", cdsRef.getVersion());
1207     assertEquals("A12345", cdsRef.getAccessionId());
1208
1209     exon = exons.get(1);
1210     assertEquals("aaaccc", exon.getSequenceAsString());
1211     assertEquals("dna1|A12346", exon.getName());
1212     assertEquals(1, exon.getDBRef().length);
1213     cdsRef = exon.getDBRef()[0];
1214     assertEquals("EMBLCDS", cdsRef.getSource());
1215     assertEquals("3", cdsRef.getVersion());
1216     assertEquals("A12346", cdsRef.getAccessionId());
1217
1218     exon = exons.get(2);
1219     assertEquals("aaaTTT", exon.getSequenceAsString());
1220     assertEquals("dna1|A12347", exon.getName());
1221     assertEquals(1, exon.getDBRef().length);
1222     cdsRef = exon.getDBRef()[0];
1223     assertEquals("EMBLCDS", cdsRef.getSource());
1224     assertEquals("4", cdsRef.getVersion());
1225     assertEquals("A12347", cdsRef.getAccessionId());
1226
1227     /*
1228      * Verify there are mappings from each exon sequence to its protein product
1229      * and also to its dna source
1230      */
1231     Iterator<AlignedCodonFrame> newMappingsIterator = mappings.iterator();
1232
1233     // mappings for dna1 - exon1 - pep1
1234     AlignedCodonFrame exonMapping = newMappingsIterator.next();
1235     List<Mapping> dnaMappings = exonMapping.getMappingsForSequence(dna1);
1236     assertEquals(1, dnaMappings.size());
1237     assertSame(exons.get(0).getDatasetSequence(), dnaMappings.get(0)
1238             .getTo());
1239     assertEquals("G(1) in CDS should map to G(4) in DNA", 4, dnaMappings
1240             .get(0).getMap().getToPosition(1));
1241     List<Mapping> peptideMappings = exonMapping
1242             .getMappingsForSequence(pep1);
1243     assertEquals(1, peptideMappings.size());
1244     assertSame(pep1.getDatasetSequence(), peptideMappings.get(0).getTo());
1245
1246     // mappings for dna1 - exon2 - pep2
1247     exonMapping = newMappingsIterator.next();
1248     dnaMappings = exonMapping.getMappingsForSequence(dna1);
1249     assertEquals(1, dnaMappings.size());
1250     assertSame(exons.get(1).getDatasetSequence(), dnaMappings.get(0)
1251             .getTo());
1252     assertEquals("c(4) in CDS should map to c(7) in DNA", 7, dnaMappings
1253             .get(0).getMap().getToPosition(4));
1254     peptideMappings = exonMapping.getMappingsForSequence(pep2);
1255     assertEquals(1, peptideMappings.size());
1256     assertSame(pep2.getDatasetSequence(), peptideMappings.get(0).getTo());
1257
1258     // mappings for dna1 - exon3 - pep3
1259     exonMapping = newMappingsIterator.next();
1260     dnaMappings = exonMapping.getMappingsForSequence(dna1);
1261     assertEquals(1, dnaMappings.size());
1262     assertSame(exons.get(2).getDatasetSequence(), dnaMappings.get(0)
1263             .getTo());
1264     assertEquals("T(4) in CDS should map to T(10) in DNA", 10, dnaMappings
1265             .get(0).getMap().getToPosition(4));
1266     peptideMappings = exonMapping.getMappingsForSequence(pep3);
1267     assertEquals(1, peptideMappings.size());
1268     assertSame(pep3.getDatasetSequence(), peptideMappings.get(0).getTo());
1269   }
1270 }