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