JAL-1805 test envirionment separation
[jalview.git] / test / jalview / util / MappingUtilsTest.java
1 package jalview.util;
2
3
4 import static org.testng.AssertJUnit.assertEquals;
5 import static org.testng.AssertJUnit.assertSame;
6 import static org.testng.AssertJUnit.assertTrue;
7
8 import jalview.api.AlignViewportI;
9 import jalview.datamodel.AlignedCodonFrame;
10 import jalview.datamodel.AlignmentI;
11 import jalview.datamodel.ColumnSelection;
12 import jalview.datamodel.SearchResults;
13 import jalview.datamodel.SearchResults.Match;
14 import jalview.datamodel.Sequence;
15 import jalview.datamodel.SequenceGroup;
16 import jalview.datamodel.SequenceI;
17 import jalview.gui.AlignViewport;
18 import jalview.io.AppletFormatAdapter;
19 import jalview.io.FormatAdapter;
20
21 import java.awt.Color;
22 import java.io.IOException;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Set;
28
29 import org.testng.annotations.Test;
30
31 public class MappingUtilsTest
32 {
33   private AlignViewportI dnaView;
34   private AlignViewportI proteinView;
35
36   /**
37    * Simple test of mapping with no intron involved.
38    */
39   @Test(groups ={ "Functional" })
40   public void testBuildSearchResults()
41   {
42     final Sequence seq1 = new Sequence("Seq1", "C-G-TA-GC");
43     seq1.createDatasetSequence();
44
45     final Sequence aseq1 = new Sequence("Seq1", "-P-R");
46     aseq1.createDatasetSequence();
47
48     /*
49      * Map dna bases 1-6 to protein residues 1-2
50      */
51     AlignedCodonFrame acf = new AlignedCodonFrame();
52     MapList map = new MapList(new int[]
53     { 1, 6 }, new int[]
54     { 1, 2 }, 3, 1);
55     acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
56     Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
57
58     /*
59      * Check protein residue 1 maps to codon 1-3, 2 to codon 4-6
60      */
61     SearchResults sr = MappingUtils.buildSearchResults(aseq1, 1, acfList);
62     assertEquals(1, sr.getResults().size());
63     Match m = sr.getResults().get(0);
64     assertEquals(seq1.getDatasetSequence(), m.getSequence());
65     assertEquals(1, m.getStart());
66     assertEquals(3, m.getEnd());
67     sr = MappingUtils.buildSearchResults(aseq1, 2, acfList);
68     assertEquals(1, sr.getResults().size());
69     m = sr.getResults().get(0);
70     assertEquals(seq1.getDatasetSequence(), m.getSequence());
71     assertEquals(4, m.getStart());
72     assertEquals(6, m.getEnd());
73
74     /*
75      * Check inverse mappings, from codons 1-3, 4-6 to protein 1, 2
76      */
77     for (int i = 1; i < 7; i++)
78     {
79       sr = MappingUtils.buildSearchResults(seq1, i, acfList);
80       assertEquals(1, sr.getResults().size());
81       m = sr.getResults().get(0);
82       assertEquals(aseq1.getDatasetSequence(), m.getSequence());
83       int residue = i > 3 ? 2 : 1;
84       assertEquals(residue, m.getStart());
85       assertEquals(residue, m.getEnd());
86     }
87   }
88
89   /**
90    * Simple test of mapping with introns involved.
91    */
92   @Test(groups ={ "Functional" })
93   public void testBuildSearchResults_withIntron()
94   {
95     final Sequence seq1 = new Sequence("Seq1", "C-G-TAGA-GCAGCTT");
96     seq1.createDatasetSequence();
97   
98     final Sequence aseq1 = new Sequence("Seq1", "-P-R");
99     aseq1.createDatasetSequence();
100   
101     /*
102      * Map dna bases [2, 4, 5], [7, 9, 11] to protein residues 1 and 2
103      */
104     AlignedCodonFrame acf = new AlignedCodonFrame();
105     MapList map = new MapList(new int[]
106     { 2, 2, 4, 5, 7, 7, 9, 9, 11, 11 }, new int[]
107     { 1, 2 }, 3, 1);
108     acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
109     Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
110   
111     /*
112      * Check protein residue 1 maps to [2, 4, 5]
113      */
114     SearchResults sr = MappingUtils.buildSearchResults(aseq1, 1, acfList);
115     assertEquals(2, sr.getResults().size());
116     Match m = sr.getResults().get(0);
117     assertEquals(seq1.getDatasetSequence(), m.getSequence());
118     assertEquals(2, m.getStart());
119     assertEquals(2, m.getEnd());
120     m = sr.getResults().get(1);
121     assertEquals(seq1.getDatasetSequence(), m.getSequence());
122     assertEquals(4, m.getStart());
123     assertEquals(5, m.getEnd());
124
125     /*
126      * Check protein residue 2 maps to [7, 9, 11]
127      */
128     sr = MappingUtils.buildSearchResults(aseq1, 2, acfList);
129     assertEquals(3, sr.getResults().size());
130     m = sr.getResults().get(0);
131     assertEquals(seq1.getDatasetSequence(), m.getSequence());
132     assertEquals(7, m.getStart());
133     assertEquals(7, m.getEnd());
134     m = sr.getResults().get(1);
135     assertEquals(seq1.getDatasetSequence(), m.getSequence());
136     assertEquals(9, m.getStart());
137     assertEquals(9, m.getEnd());
138     m = sr.getResults().get(2);
139     assertEquals(seq1.getDatasetSequence(), m.getSequence());
140     assertEquals(11, m.getStart());
141     assertEquals(11, m.getEnd());
142   
143     /*
144      * Check inverse mappings, from codons to protein
145      */
146     for (int i = 1; i < 14; i++)
147     {
148       sr = MappingUtils.buildSearchResults(seq1, i, acfList);
149       int residue = (i == 2 || i == 4 || i == 5) ? 1 : (i == 7 || i == 9
150               || i == 11 ? 2 : 0);
151       if (residue == 0)
152       {
153         assertEquals(0, sr.getResults().size());
154         continue;
155       }
156       assertEquals(1, sr.getResults().size());
157       m = sr.getResults().get(0);
158       assertEquals(aseq1.getDatasetSequence(), m.getSequence());
159       assertEquals(residue, m.getStart());
160       assertEquals(residue, m.getEnd());
161     }
162   }
163
164   /**
165    * Test mapping a sequence group made of entire sequences.
166    * 
167    * @throws IOException
168    */
169   @Test(groups ={ "Functional" })
170   public void testMapSequenceGroup_sequences() throws IOException
171   {
172     /*
173      * Set up dna and protein Seq1/2/3 with mappings (held on the protein
174      * viewport).
175      */
176     AlignmentI cdna = loadAlignment(">Seq1\nACG\n>Seq2\nTGA\n>Seq3\nTAC\n",
177             "FASTA");
178     cdna.setDataset(null);
179     AlignmentI protein = loadAlignment(">Seq1\nK\n>Seq2\nL\n>Seq3\nQ\n",
180             "FASTA");
181     protein.setDataset(null);
182     AlignedCodonFrame acf = new AlignedCodonFrame();
183     MapList map = new MapList(new int[]
184     { 1, 3 }, new int[]
185     { 1, 1 }, 3, 1);
186     for (int seq = 0; seq < 3; seq++)
187     {
188       acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), protein
189               .getSequenceAt(seq).getDatasetSequence(), map);
190     }
191     Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
192
193     AlignViewportI dnaView = new AlignViewport(cdna);
194     AlignViewportI proteinView = new AlignViewport(protein);
195     protein.setCodonFrames(acfList);
196
197     /*
198      * Select Seq1 and Seq3 in the protein (startRes=endRes=0)
199      */
200     SequenceGroup sg = new SequenceGroup();
201     sg.setColourText(true);
202     sg.setIdColour(Color.GREEN);
203     sg.setOutlineColour(Color.LIGHT_GRAY);
204     sg.addSequence(protein.getSequenceAt(0), false);
205     sg.addSequence(protein.getSequenceAt(2), false);
206
207     /*
208      * Verify the mapped sequence group in dna
209      */
210     SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg, proteinView, dnaView);
211     assertTrue(mappedGroup.getColourText());
212     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
213     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
214     assertEquals(2, mappedGroup.getSequences().size());
215     assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
216     assertSame(cdna.getSequenceAt(2), mappedGroup.getSequences().get(1));
217     assertEquals(0, mappedGroup.getStartRes());
218     assertEquals(2, mappedGroup.getEndRes());
219
220     /*
221      * Verify mapping sequence group from dna to protein
222      */
223     sg.clear();
224     sg.addSequence(cdna.getSequenceAt(1), false);
225     sg.addSequence(cdna.getSequenceAt(0), false);
226     sg.setStartRes(0);
227     sg.setEndRes(2);
228     mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
229     assertTrue(mappedGroup.getColourText());
230     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
231     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
232     assertEquals(2, mappedGroup.getSequences().size());
233     assertSame(protein.getSequenceAt(1), mappedGroup.getSequences().get(0));
234     assertSame(protein.getSequenceAt(0), mappedGroup.getSequences().get(1));
235     assertEquals(0, mappedGroup.getStartRes());
236     assertEquals(0, mappedGroup.getEndRes());
237   }
238
239   /**
240    * Helper method to load an alignment and ensure dataset sequences are set up.
241    * 
242    * @param data
243    * @param format
244    *          TODO
245    * @return
246    * @throws IOException
247    */
248   protected AlignmentI loadAlignment(final String data, String format)
249           throws IOException
250   {
251     AlignmentI a = new FormatAdapter().readFile(data,
252             AppletFormatAdapter.PASTE, format);
253     a.setDataset(null);
254     return a;
255   }
256
257   /**
258    * Test mapping a column selection in protein to its dna equivalent
259    * 
260    * @throws IOException
261    */
262   @Test(groups ={ "Functional" })
263   public void testMapColumnSelection_proteinToDna() throws IOException
264   {
265     setupMappedAlignments();
266   
267     ColumnSelection colsel = new ColumnSelection();
268
269     /*
270      * Column 0 in protein picks up Seq2/L, Seq3/G which map to cols 0-4 and 0-3
271      * in dna respectively, overall 0-4
272      */
273     colsel.addElement(0);
274     ColumnSelection cs = MappingUtils.mapColumnSelection(colsel,
275             proteinView, dnaView);
276     assertEquals("[0, 1, 2, 3, 4]", cs.getSelected().toString());
277
278     /*
279      * Column 1 in protein picks up Seq1/K which maps to cols 0-3 in dna
280      */
281     colsel.clear();
282     colsel.addElement(1);
283     cs = MappingUtils.mapColumnSelection(colsel, proteinView, dnaView);
284     assertEquals("[0, 1, 2, 3]", cs.getSelected().toString());
285
286     /*
287      * Column 2 in protein picks up gaps only - no mapping
288      */
289     colsel.clear();
290     colsel.addElement(2);
291     cs = MappingUtils.mapColumnSelection(colsel, proteinView, dnaView);
292     assertEquals("[]", cs.getSelected().toString());
293
294     /*
295      * Column 3 in protein picks up Seq1/P, Seq2/Q, Seq3/S which map to columns
296      * 6-9, 6-10, 5-8 respectively, overall to 5-10
297      */
298     colsel.clear();
299     colsel.addElement(3);
300     cs = MappingUtils.mapColumnSelection(colsel, proteinView, dnaView);
301     assertEquals("[5, 6, 7, 8, 9, 10]", cs.getSelected().toString());
302
303     /*
304      * Combine selection of columns 1 and 3 to get a discontiguous mapped
305      * selection
306      */
307     colsel.clear();
308     colsel.addElement(1);
309     colsel.addElement(3);
310     cs = MappingUtils.mapColumnSelection(colsel, proteinView, dnaView);
311     assertEquals("[0, 1, 2, 3, 5, 6, 7, 8, 9, 10]", cs.getSelected()
312             .toString());
313   }
314
315   /**
316    * @throws IOException
317    */
318   protected void setupMappedAlignments() throws IOException
319   {
320     /*
321      * Set up dna and protein Seq1/2/3 with mappings (held on the protein
322      * viewport). Lower case for introns.
323      */
324     AlignmentI cdna = loadAlignment(">Seq1\nAC-GctGtC-T\n"
325             + ">Seq2\nTc-GA-G-T-Tc\n" + ">Seq3\nTtTT-AaCGg-\n",
326             "FASTA");
327     cdna.setDataset(null);
328     AlignmentI protein = loadAlignment(
329             ">Seq1\n-K-P\n>Seq2\nL--Q\n>Seq3\nG--S\n",
330             "FASTA");
331     protein.setDataset(null);
332     AlignedCodonFrame acf = new AlignedCodonFrame();
333     MapList map = new MapList(new int[]
334     { 1, 3, 6, 6, 8, 9 }, new int[]
335     { 1, 2 }, 3, 1);
336     acf.addMap(cdna.getSequenceAt(0).getDatasetSequence(), protein
337             .getSequenceAt(0).getDatasetSequence(), map);
338     map = new MapList(new int[]
339     { 1, 1, 3, 4, 5, 7 }, new int[]
340     { 1, 2 }, 3, 1);
341     acf.addMap(cdna.getSequenceAt(1).getDatasetSequence(), protein
342             .getSequenceAt(1).getDatasetSequence(), map);
343     map = new MapList(new int[]
344     { 1, 1, 3, 4, 5, 5, 7, 8 }, new int[]
345     { 1, 2 }, 3, 1);
346     acf.addMap(cdna.getSequenceAt(2).getDatasetSequence(), protein
347             .getSequenceAt(2).getDatasetSequence(), map);
348     Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
349   
350     dnaView = new AlignViewport(cdna);
351     proteinView = new AlignViewport(protein);
352     protein.setCodonFrames(acfList);
353   }
354
355   /**
356    * Test mapping a column selection in dna to its protein equivalent
357    * 
358    * @throws IOException
359    */
360   @Test(groups ={ "Functional" })
361   public void testMapColumnSelection_dnaToProtein() throws IOException
362   {
363     setupMappedAlignments();
364   
365     ColumnSelection colsel = new ColumnSelection();
366   
367     /*
368      * Column 0 in dna picks up first bases which map to residue 1, columns 0-1
369      * in protein.
370      */
371     colsel.addElement(0);
372     ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, dnaView,
373             proteinView);
374     assertEquals("[0, 1]", cs.getSelected().toString());
375
376     /*
377      * Columns 3-5 in dna map to the first residues in protein Seq1, Seq2, and
378      * the first two in Seq3. Overall to columns 0, 1, 3 (col2 is all gaps).
379      */
380     colsel.addElement(3);
381     colsel.addElement(4);
382     colsel.addElement(5);
383     cs = MappingUtils.mapColumnSelection(colsel, dnaView, proteinView);
384     assertEquals("[0, 1, 3]", cs.getSelected().toString());
385   }
386
387   @Test(groups ={ "Functional" })
388   public void testMapColumnSelection_null() throws IOException
389   {
390     setupMappedAlignments();
391     ColumnSelection cs = MappingUtils.mapColumnSelection(null, dnaView,
392             proteinView);
393     assertTrue("mapped selection not empty", cs.getSelected().isEmpty());
394   }
395
396   /**
397    * Tests for the method that converts a series of [start, end] ranges to
398    * single positions
399    */
400   @Test(groups ={ "Functional" })
401   public void testFlattenRanges()
402   {
403     assertEquals("[1, 2, 3, 4]",
404             Arrays.toString(MappingUtils.flattenRanges(new int[]
405             { 1, 4 })));
406     assertEquals("[1, 2, 3, 4]",
407             Arrays.toString(MappingUtils.flattenRanges(new int[]
408             { 1, 2, 3, 4 })));
409     assertEquals("[1, 2, 3, 4]",
410             Arrays.toString(MappingUtils.flattenRanges(new int[]
411             { 1, 1, 2, 2, 3, 3, 4, 4 })));
412     assertEquals("[1, 2, 3, 4, 7, 8, 9, 12]",
413             Arrays.toString(MappingUtils.flattenRanges(new int[]
414             { 1, 4, 7, 9, 12, 12 })));
415     // unpaired start position is ignored:
416     assertEquals("[1, 2, 3, 4, 7, 8, 9, 12]",
417             Arrays.toString(MappingUtils.flattenRanges(new int[]
418             { 1, 4, 7, 9, 12, 12, 15 })));
419   }
420
421   /**
422    * Test mapping a sequence group made of entire columns.
423    * 
424    * @throws IOException
425    */
426   @Test(groups ={ "Functional" })
427   public void testMapSequenceGroup_columns() throws IOException
428   {
429     /*
430      * Set up dna and protein Seq1/2/3 with mappings (held on the protein
431      * viewport).
432      */
433     AlignmentI cdna = loadAlignment(
434             ">Seq1\nACGGCA\n>Seq2\nTGACAG\n>Seq3\nTACGTA\n",
435             "FASTA");
436     cdna.setDataset(null);
437     AlignmentI protein = loadAlignment(">Seq1\nKA\n>Seq2\nLQ\n>Seq3\nQV\n",
438             "FASTA");
439     protein.setDataset(null);
440     AlignedCodonFrame acf = new AlignedCodonFrame();
441     MapList map = new MapList(new int[]
442     { 1, 6 }, new int[]
443     { 1, 2 }, 3, 1);
444     for (int seq = 0; seq < 3; seq++)
445     {
446       acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), protein
447               .getSequenceAt(seq).getDatasetSequence(), map);
448     }
449     Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
450   
451     AlignViewportI dnaView = new AlignViewport(cdna);
452     AlignViewportI proteinView = new AlignViewport(protein);
453     protein.setCodonFrames(acfList);
454   
455     /*
456      * Select all sequences, column 2 in the protein
457      */
458     SequenceGroup sg = new SequenceGroup();
459     sg.setColourText(true);
460     sg.setIdColour(Color.GREEN);
461     sg.setOutlineColour(Color.LIGHT_GRAY);
462     sg.addSequence(protein.getSequenceAt(0), false);
463     sg.addSequence(protein.getSequenceAt(1), false);
464     sg.addSequence(protein.getSequenceAt(2), false);
465     sg.setStartRes(1);
466     sg.setEndRes(1);
467   
468     /*
469      * Verify the mapped sequence group in dna
470      */
471     SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg, proteinView, dnaView);
472     assertTrue(mappedGroup.getColourText());
473     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
474     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
475     assertEquals(3, mappedGroup.getSequences().size());
476     assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
477     assertSame(cdna.getSequenceAt(1), mappedGroup.getSequences().get(1));
478     assertSame(cdna.getSequenceAt(2), mappedGroup.getSequences().get(2));
479     assertEquals(3, mappedGroup.getStartRes());
480     assertEquals(5, mappedGroup.getEndRes());
481   
482     /*
483      * Verify mapping sequence group from dna to protein
484      */
485     sg.clear();
486     sg.addSequence(cdna.getSequenceAt(0), false);
487     sg.addSequence(cdna.getSequenceAt(1), false);
488     sg.addSequence(cdna.getSequenceAt(2), false);
489     // select columns 2 and 3 in DNA which span protein columns 0 and 1
490     sg.setStartRes(2);
491     sg.setEndRes(3);
492     mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
493     assertTrue(mappedGroup.getColourText());
494     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
495     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
496     assertEquals(3, mappedGroup.getSequences().size());
497     assertSame(protein.getSequenceAt(0), mappedGroup.getSequences().get(0));
498     assertSame(protein.getSequenceAt(1), mappedGroup.getSequences().get(1));
499     assertSame(protein.getSequenceAt(2), mappedGroup.getSequences().get(2));
500     assertEquals(0, mappedGroup.getStartRes());
501     assertEquals(1, mappedGroup.getEndRes());
502   }
503
504   /**
505    * Test mapping a sequence group made of a sequences/columns region.
506    * 
507    * @throws IOException
508    */
509   @Test(groups ={ "Functional" })
510   public void testMapSequenceGroup_region() throws IOException
511   {
512     /*
513      * Set up gapped dna and protein Seq1/2/3 with mappings (held on the protein
514      * viewport).
515      */
516     AlignmentI cdna = loadAlignment(
517             ">Seq1\nA-CG-GC--AT-CA\n>Seq2\n-TG-AC-AG-T-AT\n>Seq3\n-T--ACG-TAAT-G\n",
518             "FASTA");
519     cdna.setDataset(null);
520     AlignmentI protein = loadAlignment(
521             ">Seq1\n-KA-S\n>Seq2\n--L-QY\n>Seq3\nQ-V-M\n", "FASTA");
522     protein.setDataset(null);
523     AlignedCodonFrame acf = new AlignedCodonFrame();
524     MapList map = new MapList(new int[]
525     { 1, 9 }, new int[]
526     { 1, 3 }, 3, 1);
527     for (int seq = 0; seq < 3; seq++)
528     {
529       acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), protein
530               .getSequenceAt(seq).getDatasetSequence(), map);
531     }
532     Set<AlignedCodonFrame> acfList = Collections.singleton(acf);
533   
534     AlignViewportI dnaView = new AlignViewport(cdna);
535     AlignViewportI proteinView = new AlignViewport(protein);
536     protein.setCodonFrames(acfList);
537   
538     /*
539      * Select Seq1 and Seq2 in the protein, column 1 (K/-). Expect mapped
540      * sequence group to cover Seq1, columns 0-3 (ACG). Because the selection
541      * only includes a gap in Seq2 there is no mappable selection region in the
542      * corresponding DNA.
543      */
544     SequenceGroup sg = new SequenceGroup();
545     sg.setColourText(true);
546     sg.setIdColour(Color.GREEN);
547     sg.setOutlineColour(Color.LIGHT_GRAY);
548     sg.addSequence(protein.getSequenceAt(0), false);
549     sg.addSequence(protein.getSequenceAt(1), false);
550     sg.setStartRes(1);
551     sg.setEndRes(1);
552   
553     /*
554      * Verify the mapped sequence group in dna
555      */
556     SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg, proteinView, dnaView);
557     assertTrue(mappedGroup.getColourText());
558     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
559     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
560     assertEquals(1, mappedGroup.getSequences().size());
561     assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
562     // Seq2 in protein has a gap in column 1 - ignored
563     // Seq1 has K which should map to columns 0-3 in Seq1
564     assertEquals(0, mappedGroup.getStartRes());
565     assertEquals(3, mappedGroup.getEndRes());
566   
567     /*
568      * Now select cols 2-4 in protein. These cover Seq1:AS Seq2:LQ Seq3:VM which
569      * extend over DNA columns 3-12, 1-7, 6-13 respectively, or 1-13 overall.
570      */
571     sg.setStartRes(2);
572     sg.setEndRes(4);
573     mappedGroup = MappingUtils.mapSequenceGroup(sg, proteinView, dnaView);
574     assertEquals(1, mappedGroup.getStartRes());
575     assertEquals(13, mappedGroup.getEndRes());
576
577     /*
578      * Verify mapping sequence group from dna to protein
579      */
580     sg.clear();
581     sg.addSequence(cdna.getSequenceAt(0), false);
582
583     // select columns 4,5 - includes Seq1:codon2 (A) only
584     sg.setStartRes(4);
585     sg.setEndRes(5);
586     mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
587     assertEquals(2, mappedGroup.getStartRes());
588     assertEquals(2, mappedGroup.getEndRes());
589
590     // add Seq2 to dna selection cols 4-5 include codons 1 and 2 (LQ)
591     sg.addSequence(cdna.getSequenceAt(1), false);
592     mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
593     assertEquals(2, mappedGroup.getStartRes());
594     assertEquals(4, mappedGroup.getEndRes());
595
596     // add Seq3 to dna selection cols 4-5 include codon 1 (Q)
597     sg.addSequence(cdna.getSequenceAt(2), false);
598     mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
599     assertEquals(0, mappedGroup.getStartRes());
600     assertEquals(4, mappedGroup.getEndRes());
601   }
602
603   @Test(groups ={ "Functional" })
604   public void testFindMappingsForSequence()
605   {
606     SequenceI seq1 = new Sequence("Seq1", "ABC");
607     SequenceI seq2 = new Sequence("Seq2", "ABC");
608     SequenceI seq3 = new Sequence("Seq3", "ABC");
609     SequenceI seq4 = new Sequence("Seq4", "ABC");
610     seq1.createDatasetSequence();
611     seq2.createDatasetSequence();
612     seq3.createDatasetSequence();
613     seq4.createDatasetSequence();
614
615     /*
616      * Create mappings from seq1 to seq2, seq2 to seq1, seq3 to seq1
617      */
618     AlignedCodonFrame acf1 = new AlignedCodonFrame();
619     MapList map = new MapList(new int[]
620     { 1, 3 }, new int[]
621     { 1, 3 },1, 1);
622     acf1.addMap(seq1.getDatasetSequence(), seq2.getDatasetSequence(), map);
623     AlignedCodonFrame acf2 = new AlignedCodonFrame();
624     acf2.addMap(seq2.getDatasetSequence(), seq1.getDatasetSequence(), map);
625     AlignedCodonFrame acf3 = new AlignedCodonFrame();
626     acf3.addMap(seq3.getDatasetSequence(), seq1.getDatasetSequence(), map);
627     
628     Set<AlignedCodonFrame> mappings = new HashSet<AlignedCodonFrame>();
629     mappings.add(acf1);
630     mappings.add(acf2);
631     mappings.add(acf3);
632     
633     /*
634      * Seq1 has three mappings
635      */
636     List<AlignedCodonFrame> result = MappingUtils.findMappingsForSequence(
637             seq1, mappings);
638     assertEquals(3, result.size());
639     assertTrue(result.contains(acf1));
640     assertTrue(result.contains(acf2));
641     assertTrue(result.contains(acf3));
642
643     /*
644      * Seq2 has two mappings
645      */
646     result = MappingUtils.findMappingsForSequence(seq2, mappings);
647     assertEquals(2, result.size());
648     assertTrue(result.contains(acf1));
649     assertTrue(result.contains(acf2));
650
651     /*
652      * Seq3 has one mapping
653      */
654     result = MappingUtils.findMappingsForSequence(seq3, mappings);
655     assertEquals(1, result.size());
656     assertTrue(result.contains(acf3));
657
658     /*
659      * Seq4 has no mappings
660      */
661     result = MappingUtils.findMappingsForSequence(seq4, mappings);
662     assertEquals(0, result.size());
663
664     result = MappingUtils.findMappingsForSequence(null, mappings);
665     assertEquals(0, result.size());
666
667     result = MappingUtils.findMappingsForSequence(seq1, null);
668     assertEquals(0, result.size());
669
670     result = MappingUtils.findMappingsForSequence(null, null);
671     assertEquals(0, result.size());
672 }
673 }