Merge branch 'develop' into JAL-1705_trialMerge
[jalview.git] / test / jalview / util / MappingUtilsTest.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.util;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertSame;
25 import static org.testng.AssertJUnit.assertTrue;
26
27 import jalview.api.AlignViewportI;
28 import jalview.commands.EditCommand;
29 import jalview.commands.EditCommand.Action;
30 import jalview.commands.EditCommand.Edit;
31 import jalview.datamodel.AlignedCodonFrame;
32 import jalview.datamodel.Alignment;
33 import jalview.datamodel.AlignmentI;
34 import jalview.datamodel.ColumnSelection;
35 import jalview.datamodel.SearchResults;
36 import jalview.datamodel.SearchResults.Match;
37 import jalview.datamodel.Sequence;
38 import jalview.datamodel.SequenceGroup;
39 import jalview.datamodel.SequenceI;
40 import jalview.gui.AlignViewport;
41 import jalview.io.AppletFormatAdapter;
42 import jalview.io.FormatAdapter;
43
44 import java.awt.Color;
45 import java.io.IOException;
46 import java.util.ArrayList;
47 import java.util.Arrays;
48 import java.util.List;
49
50 import org.testng.annotations.Test;
51
52 public class MappingUtilsTest
53 {
54   private AlignViewportI dnaView;
55
56   private AlignViewportI proteinView;
57
58   /**
59    * Simple test of mapping with no intron involved.
60    */
61   @Test(groups = { "Functional" })
62   public void testBuildSearchResults()
63   {
64     final Sequence seq1 = new Sequence("Seq1/5-10", "C-G-TA-GC");
65     seq1.createDatasetSequence();
66
67     final Sequence aseq1 = new Sequence("Seq1/12-13", "-P-R");
68     aseq1.createDatasetSequence();
69
70     /*
71      * Map dna bases 5-10 to protein residues 12-13
72      */
73     AlignedCodonFrame acf = new AlignedCodonFrame();
74     MapList map = new MapList(new int[] { 5, 10 }, new int[] { 12, 13 }, 3,
75             1);
76     acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
77     List<AlignedCodonFrame> acfList = Arrays.asList(new AlignedCodonFrame[]
78     { acf });
79
80     /*
81      * Check protein residue 12 maps to codon 5-7, 13 to codon 8-10
82      */
83     SearchResults sr = MappingUtils.buildSearchResults(aseq1, 12, acfList);
84     assertEquals(1, sr.getResults().size());
85     Match m = sr.getResults().get(0);
86     assertEquals(seq1.getDatasetSequence(), m.getSequence());
87     assertEquals(5, m.getStart());
88     assertEquals(7, m.getEnd());
89     sr = MappingUtils.buildSearchResults(aseq1, 13, acfList);
90     assertEquals(1, sr.getResults().size());
91     m = sr.getResults().get(0);
92     assertEquals(seq1.getDatasetSequence(), m.getSequence());
93     assertEquals(8, m.getStart());
94     assertEquals(10, m.getEnd());
95
96     /*
97      * Check inverse mappings, from codons 5-7, 8-10 to protein 12, 13
98      */
99     for (int i = 5; i < 11; i++)
100     {
101       sr = MappingUtils.buildSearchResults(seq1, i, acfList);
102       assertEquals(1, sr.getResults().size());
103       m = sr.getResults().get(0);
104       assertEquals(aseq1.getDatasetSequence(), m.getSequence());
105       int residue = i > 7 ? 13 : 12;
106       assertEquals(residue, m.getStart());
107       assertEquals(residue, m.getEnd());
108     }
109   }
110
111   /**
112    * Simple test of mapping with introns involved.
113    */
114   @Test(groups = { "Functional" })
115   public void testBuildSearchResults_withIntron()
116   {
117     final Sequence seq1 = new Sequence("Seq1/5-17", "c-G-tAGa-GcAgCtt");
118     seq1.createDatasetSequence();
119
120     final Sequence aseq1 = new Sequence("Seq1/8-9", "-E-D");
121     aseq1.createDatasetSequence();
122
123     /*
124      * Map dna bases [6, 8, 9], [11, 13, 115] to protein residues 8 and 9
125      */
126     AlignedCodonFrame acf = new AlignedCodonFrame();
127     MapList map = new MapList(new int[] { 6, 6, 8, 9, 11, 11, 13, 13, 15,
128         15 }, new int[] { 8, 9 }, 3, 1);
129     acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
130     List<AlignedCodonFrame> acfList = Arrays.asList(new AlignedCodonFrame[]
131     { acf });
132
133     /*
134      * Check protein residue 8 maps to [6, 8, 9]
135      */
136     SearchResults sr = MappingUtils.buildSearchResults(aseq1, 8, acfList);
137     assertEquals(2, sr.getResults().size());
138     Match m = sr.getResults().get(0);
139     assertEquals(seq1.getDatasetSequence(), m.getSequence());
140     assertEquals(6, m.getStart());
141     assertEquals(6, m.getEnd());
142     m = sr.getResults().get(1);
143     assertEquals(seq1.getDatasetSequence(), m.getSequence());
144     assertEquals(8, m.getStart());
145     assertEquals(9, m.getEnd());
146
147     /*
148      * Check protein residue 9 maps to [11, 13, 15]
149      */
150     sr = MappingUtils.buildSearchResults(aseq1, 9, acfList);
151     assertEquals(3, sr.getResults().size());
152     m = sr.getResults().get(0);
153     assertEquals(seq1.getDatasetSequence(), m.getSequence());
154     assertEquals(11, m.getStart());
155     assertEquals(11, m.getEnd());
156     m = sr.getResults().get(1);
157     assertEquals(seq1.getDatasetSequence(), m.getSequence());
158     assertEquals(13, m.getStart());
159     assertEquals(13, m.getEnd());
160     m = sr.getResults().get(2);
161     assertEquals(seq1.getDatasetSequence(), m.getSequence());
162     assertEquals(15, m.getStart());
163     assertEquals(15, m.getEnd());
164
165     /*
166      * Check inverse mappings, from codons to protein
167      */
168     for (int i = 5; i < 18; i++)
169     {
170       sr = MappingUtils.buildSearchResults(seq1, i, acfList);
171       int residue = (i == 6 || i == 8 || i == 9) ? 8 : (i == 11 || i == 13
172               || i == 15 ? 9 : 0);
173       if (residue == 0)
174       {
175         assertEquals(0, sr.getResults().size());
176         continue;
177       }
178       assertEquals(1, sr.getResults().size());
179       m = sr.getResults().get(0);
180       assertEquals(aseq1.getDatasetSequence(), m.getSequence());
181       assertEquals(residue, m.getStart());
182       assertEquals(residue, m.getEnd());
183     }
184   }
185
186   /**
187    * Test mapping a sequence group made of entire sequences.
188    * 
189    * @throws IOException
190    */
191   @Test(groups = { "Functional" })
192   public void testMapSequenceGroup_sequences() throws IOException
193   {
194     /*
195      * Set up dna and protein Seq1/2/3 with mappings (held on the protein
196      * viewport).
197      */
198     AlignmentI cdna = loadAlignment(">Seq1\nACG\n>Seq2\nTGA\n>Seq3\nTAC\n",
199             "FASTA");
200     cdna.setDataset(null);
201     AlignmentI protein = loadAlignment(">Seq1\nK\n>Seq2\nL\n>Seq3\nQ\n",
202             "FASTA");
203     protein.setDataset(null);
204     AlignedCodonFrame acf = new AlignedCodonFrame();
205     MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 1 }, 3, 1);
206     for (int seq = 0; seq < 3; seq++)
207     {
208       acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), protein
209               .getSequenceAt(seq).getDatasetSequence(), map);
210     }
211     List<AlignedCodonFrame> acfList = Arrays.asList(new AlignedCodonFrame[]
212     { acf });
213
214     AlignViewportI dnaView = new AlignViewport(cdna);
215     AlignViewportI proteinView = new AlignViewport(protein);
216     protein.setCodonFrames(acfList);
217
218     /*
219      * Select Seq1 and Seq3 in the protein (startRes=endRes=0)
220      */
221     SequenceGroup sg = new SequenceGroup();
222     sg.setColourText(true);
223     sg.setIdColour(Color.GREEN);
224     sg.setOutlineColour(Color.LIGHT_GRAY);
225     sg.addSequence(protein.getSequenceAt(0), false);
226     sg.addSequence(protein.getSequenceAt(2), false);
227
228     /*
229      * Verify the mapped sequence group in dna
230      */
231     SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
232             proteinView, dnaView);
233     assertTrue(mappedGroup.getColourText());
234     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
235     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
236     assertEquals(2, mappedGroup.getSequences().size());
237     assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
238     assertSame(cdna.getSequenceAt(2), mappedGroup.getSequences().get(1));
239     assertEquals(0, mappedGroup.getStartRes());
240     assertEquals(2, mappedGroup.getEndRes());
241
242     /*
243      * Verify mapping sequence group from dna to protein
244      */
245     sg.clear();
246     sg.addSequence(cdna.getSequenceAt(1), false);
247     sg.addSequence(cdna.getSequenceAt(0), false);
248     sg.setStartRes(0);
249     sg.setEndRes(2);
250     mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
251     assertTrue(mappedGroup.getColourText());
252     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
253     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
254     assertEquals(2, mappedGroup.getSequences().size());
255     assertSame(protein.getSequenceAt(1), mappedGroup.getSequences().get(0));
256     assertSame(protein.getSequenceAt(0), mappedGroup.getSequences().get(1));
257     assertEquals(0, mappedGroup.getStartRes());
258     assertEquals(0, mappedGroup.getEndRes());
259   }
260
261   /**
262    * Helper method to load an alignment and ensure dataset sequences are set up.
263    * 
264    * @param data
265    * @param format
266    *          TODO
267    * @return
268    * @throws IOException
269    */
270   protected AlignmentI loadAlignment(final String data, String format)
271           throws IOException
272   {
273     AlignmentI a = new FormatAdapter().readFile(data,
274             AppletFormatAdapter.PASTE, format);
275     a.setDataset(null);
276     return a;
277   }
278
279   /**
280    * Test mapping a column selection in protein to its dna equivalent
281    * 
282    * @throws IOException
283    */
284   @Test(groups = { "Functional" })
285   public void testMapColumnSelection_proteinToDna() throws IOException
286   {
287     setupMappedAlignments();
288
289     ColumnSelection colsel = new ColumnSelection();
290
291     /*
292      * Column 0 in protein picks up Seq2/L, Seq3/G which map to cols 0-4 and 0-3
293      * in dna respectively, overall 0-4
294      */
295     colsel.addElement(0);
296     ColumnSelection cs = MappingUtils.mapColumnSelection(colsel,
297             proteinView, dnaView);
298     assertEquals("[0, 1, 2, 3, 4]", cs.getSelected().toString());
299
300     /*
301      * Column 1 in protein picks up Seq1/K which maps to cols 0-3 in dna
302      */
303     colsel.clear();
304     colsel.addElement(1);
305     cs = MappingUtils.mapColumnSelection(colsel, proteinView, dnaView);
306     assertEquals("[0, 1, 2, 3]", cs.getSelected().toString());
307
308     /*
309      * Column 2 in protein picks up gaps only - no mapping
310      */
311     colsel.clear();
312     colsel.addElement(2);
313     cs = MappingUtils.mapColumnSelection(colsel, proteinView, dnaView);
314     assertEquals("[]", cs.getSelected().toString());
315
316     /*
317      * Column 3 in protein picks up Seq1/P, Seq2/Q, Seq3/S which map to columns
318      * 6-9, 6-10, 5-8 respectively, overall to 5-10
319      */
320     colsel.clear();
321     colsel.addElement(3);
322     cs = MappingUtils.mapColumnSelection(colsel, proteinView, dnaView);
323     assertEquals("[5, 6, 7, 8, 9, 10]", cs.getSelected().toString());
324
325     /*
326      * Combine selection of columns 1 and 3 to get a discontiguous mapped
327      * selection
328      */
329     colsel.clear();
330     colsel.addElement(1);
331     colsel.addElement(3);
332     cs = MappingUtils.mapColumnSelection(colsel, proteinView, dnaView);
333     assertEquals("[0, 1, 2, 3, 5, 6, 7, 8, 9, 10]", cs.getSelected()
334             .toString());
335   }
336
337   /**
338    * Set up mappings for tests from 3 dna to 3 protein sequences. Sequences have
339    * offset start positions for a more general test case.
340    * 
341    * @throws IOException
342    */
343   protected void setupMappedAlignments() throws IOException
344   {
345     /*
346      * Map (upper-case = coding):
347      * Seq1/10-18 AC-GctGtC-T to Seq1/40 -K-P
348      * Seq2/20-27 Tc-GA-G-T-T to Seq2/20-27 L--Q
349      * Seq3/30-38 TtTT-AaCGg- to Seq3/60-61\nG--S
350      */
351     AlignmentI cdna = loadAlignment(">Seq1/10-18\nAC-GctGtC-T\n"
352             + ">Seq2/20-27\nTc-GA-G-T-Tc\n" + ">Seq3/30-38\nTtTT-AaCGg-\n",
353             "FASTA");
354     cdna.setDataset(null);
355     AlignmentI protein = loadAlignment(
356             ">Seq1/40-41\n-K-P\n>Seq2/50-51\nL--Q\n>Seq3/60-61\nG--S\n",
357             "FASTA");
358     protein.setDataset(null);
359
360     // map first dna to first protein seq
361     AlignedCodonFrame acf = new AlignedCodonFrame();
362     MapList map = new MapList(new int[] { 10, 12, 15, 15, 17, 18 },
363             new int[] { 40, 41 }, 3, 1);
364     acf.addMap(cdna.getSequenceAt(0).getDatasetSequence(), protein
365             .getSequenceAt(0).getDatasetSequence(), map);
366
367     // map second dna to second protein seq
368     map = new MapList(new int[] { 20, 20, 22, 23, 24, 26 }, new int[] { 50,
369         51 }, 3, 1);
370     acf.addMap(cdna.getSequenceAt(1).getDatasetSequence(), protein
371             .getSequenceAt(1).getDatasetSequence(), map);
372
373     // map third dna to third protein seq
374     map = new MapList(new int[] { 30, 30, 32, 34, 36, 37 }, new int[] { 60,
375         61 }, 3, 1);
376     acf.addMap(cdna.getSequenceAt(2).getDatasetSequence(), protein
377             .getSequenceAt(2).getDatasetSequence(), map);
378     List<AlignedCodonFrame> acfList = Arrays.asList(new AlignedCodonFrame[]
379     { acf });
380
381     dnaView = new AlignViewport(cdna);
382     proteinView = new AlignViewport(protein);
383     protein.setCodonFrames(acfList);
384   }
385
386   /**
387    * Test mapping a column selection in dna to its protein equivalent
388    * 
389    * @throws IOException
390    */
391   @Test(groups = { "Functional" })
392   public void testMapColumnSelection_dnaToProtein() throws IOException
393   {
394     setupMappedAlignments();
395
396     ColumnSelection colsel = new ColumnSelection();
397
398     /*
399      * Column 0 in dna picks up first bases which map to residue 1, columns 0-1
400      * in protein.
401      */
402     colsel.addElement(0);
403     ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, dnaView,
404             proteinView);
405     assertEquals("[0, 1]", cs.getSelected().toString());
406
407     /*
408      * Columns 3-5 in dna map to the first residues in protein Seq1, Seq2, and
409      * the first two in Seq3. Overall to columns 0, 1, 3 (col2 is all gaps).
410      */
411     colsel.addElement(3);
412     colsel.addElement(4);
413     colsel.addElement(5);
414     cs = MappingUtils.mapColumnSelection(colsel, dnaView, proteinView);
415     assertEquals("[0, 1, 3]", cs.getSelected().toString());
416   }
417
418   @Test(groups = { "Functional" })
419   public void testMapColumnSelection_null() throws IOException
420   {
421     setupMappedAlignments();
422     ColumnSelection cs = MappingUtils.mapColumnSelection(null, dnaView,
423             proteinView);
424     assertTrue("mapped selection not empty", cs.getSelected().isEmpty());
425   }
426
427   /**
428    * Tests for the method that converts a series of [start, end] ranges to
429    * single positions
430    */
431   @Test(groups = { "Functional" })
432   public void testFlattenRanges()
433   {
434     assertEquals("[1, 2, 3, 4]",
435             Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 4 })));
436     assertEquals(
437             "[1, 2, 3, 4]",
438             Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 2, 3,
439                 4 })));
440     assertEquals(
441             "[1, 2, 3, 4]",
442             Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 1, 2,
443                 2, 3, 3, 4, 4 })));
444     assertEquals(
445             "[1, 2, 3, 4, 7, 8, 9, 12]",
446             Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 4, 7,
447                 9, 12, 12 })));
448     // trailing unpaired start position is ignored:
449     assertEquals(
450             "[1, 2, 3, 4, 7, 8, 9, 12]",
451             Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 4, 7,
452                 9, 12, 12, 15 })));
453   }
454
455   /**
456    * Test mapping a sequence group made of entire columns.
457    * 
458    * @throws IOException
459    */
460   @Test(groups = { "Functional" })
461   public void testMapSequenceGroup_columns() throws IOException
462   {
463     /*
464      * Set up dna and protein Seq1/2/3 with mappings (held on the protein
465      * viewport).
466      */
467     AlignmentI cdna = loadAlignment(
468             ">Seq1\nACGGCA\n>Seq2\nTGACAG\n>Seq3\nTACGTA\n", "FASTA");
469     cdna.setDataset(null);
470     AlignmentI protein = loadAlignment(">Seq1\nKA\n>Seq2\nLQ\n>Seq3\nQV\n",
471             "FASTA");
472     protein.setDataset(null);
473     AlignedCodonFrame acf = new AlignedCodonFrame();
474     MapList map = new MapList(new int[] { 1, 6 }, new int[] { 1, 2 }, 3, 1);
475     for (int seq = 0; seq < 3; seq++)
476     {
477       acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), protein
478               .getSequenceAt(seq).getDatasetSequence(), map);
479     }
480     List<AlignedCodonFrame> acfList = Arrays.asList(new AlignedCodonFrame[]
481     { acf });
482
483     AlignViewportI dnaView = new AlignViewport(cdna);
484     AlignViewportI proteinView = new AlignViewport(protein);
485     protein.setCodonFrames(acfList);
486
487     /*
488      * Select all sequences, column 2 in the protein
489      */
490     SequenceGroup sg = new SequenceGroup();
491     sg.setColourText(true);
492     sg.setIdColour(Color.GREEN);
493     sg.setOutlineColour(Color.LIGHT_GRAY);
494     sg.addSequence(protein.getSequenceAt(0), false);
495     sg.addSequence(protein.getSequenceAt(1), false);
496     sg.addSequence(protein.getSequenceAt(2), false);
497     sg.setStartRes(1);
498     sg.setEndRes(1);
499
500     /*
501      * Verify the mapped sequence group in dna
502      */
503     SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
504             proteinView, dnaView);
505     assertTrue(mappedGroup.getColourText());
506     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
507     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
508     assertEquals(3, mappedGroup.getSequences().size());
509     assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
510     assertSame(cdna.getSequenceAt(1), mappedGroup.getSequences().get(1));
511     assertSame(cdna.getSequenceAt(2), mappedGroup.getSequences().get(2));
512     assertEquals(3, mappedGroup.getStartRes());
513     assertEquals(5, mappedGroup.getEndRes());
514
515     /*
516      * Verify mapping sequence group from dna to protein
517      */
518     sg.clear();
519     sg.addSequence(cdna.getSequenceAt(0), false);
520     sg.addSequence(cdna.getSequenceAt(1), false);
521     sg.addSequence(cdna.getSequenceAt(2), false);
522     // select columns 2 and 3 in DNA which span protein columns 0 and 1
523     sg.setStartRes(2);
524     sg.setEndRes(3);
525     mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
526     assertTrue(mappedGroup.getColourText());
527     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
528     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
529     assertEquals(3, mappedGroup.getSequences().size());
530     assertSame(protein.getSequenceAt(0), mappedGroup.getSequences().get(0));
531     assertSame(protein.getSequenceAt(1), mappedGroup.getSequences().get(1));
532     assertSame(protein.getSequenceAt(2), mappedGroup.getSequences().get(2));
533     assertEquals(0, mappedGroup.getStartRes());
534     assertEquals(1, mappedGroup.getEndRes());
535   }
536
537   /**
538    * Test mapping a sequence group made of a sequences/columns region.
539    * 
540    * @throws IOException
541    */
542   @Test(groups = { "Functional" })
543   public void testMapSequenceGroup_region() throws IOException
544   {
545     /*
546      * Set up gapped dna and protein Seq1/2/3 with mappings (held on the protein
547      * viewport).
548      */
549     AlignmentI cdna = loadAlignment(
550             ">Seq1\nA-CG-GC--AT-CA\n>Seq2\n-TG-AC-AG-T-AT\n>Seq3\n-T--ACG-TAAT-G\n",
551             "FASTA");
552     cdna.setDataset(null);
553     AlignmentI protein = loadAlignment(
554             ">Seq1\n-KA-S\n>Seq2\n--L-QY\n>Seq3\nQ-V-M\n", "FASTA");
555     protein.setDataset(null);
556     AlignedCodonFrame acf = new AlignedCodonFrame();
557     MapList map = new MapList(new int[] { 1, 9 }, new int[] { 1, 3 }, 3, 1);
558     for (int seq = 0; seq < 3; seq++)
559     {
560       acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), protein
561               .getSequenceAt(seq).getDatasetSequence(), map);
562     }
563     List<AlignedCodonFrame> acfList = Arrays.asList(new AlignedCodonFrame[]
564     { acf });
565
566     AlignViewportI dnaView = new AlignViewport(cdna);
567     AlignViewportI proteinView = new AlignViewport(protein);
568     protein.setCodonFrames(acfList);
569
570     /*
571      * Select Seq1 and Seq2 in the protein, column 1 (K/-). Expect mapped
572      * sequence group to cover Seq1, columns 0-3 (ACG). Because the selection
573      * only includes a gap in Seq2 there is no mappable selection region in the
574      * corresponding DNA.
575      */
576     SequenceGroup sg = new SequenceGroup();
577     sg.setColourText(true);
578     sg.setIdColour(Color.GREEN);
579     sg.setOutlineColour(Color.LIGHT_GRAY);
580     sg.addSequence(protein.getSequenceAt(0), false);
581     sg.addSequence(protein.getSequenceAt(1), false);
582     sg.setStartRes(1);
583     sg.setEndRes(1);
584
585     /*
586      * Verify the mapped sequence group in dna
587      */
588     SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
589             proteinView, dnaView);
590     assertTrue(mappedGroup.getColourText());
591     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
592     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
593     assertEquals(1, mappedGroup.getSequences().size());
594     assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
595     // Seq2 in protein has a gap in column 1 - ignored
596     // Seq1 has K which should map to columns 0-3 in Seq1
597     assertEquals(0, mappedGroup.getStartRes());
598     assertEquals(3, mappedGroup.getEndRes());
599
600     /*
601      * Now select cols 2-4 in protein. These cover Seq1:AS Seq2:LQ Seq3:VM which
602      * extend over DNA columns 3-12, 1-7, 6-13 respectively, or 1-13 overall.
603      */
604     sg.setStartRes(2);
605     sg.setEndRes(4);
606     mappedGroup = MappingUtils.mapSequenceGroup(sg, proteinView, dnaView);
607     assertEquals(1, mappedGroup.getStartRes());
608     assertEquals(13, mappedGroup.getEndRes());
609
610     /*
611      * Verify mapping sequence group from dna to protein
612      */
613     sg.clear();
614     sg.addSequence(cdna.getSequenceAt(0), false);
615
616     // select columns 4,5 - includes Seq1:codon2 (A) only
617     sg.setStartRes(4);
618     sg.setEndRes(5);
619     mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
620     assertEquals(2, mappedGroup.getStartRes());
621     assertEquals(2, mappedGroup.getEndRes());
622
623     // add Seq2 to dna selection cols 4-5 include codons 1 and 2 (LQ)
624     sg.addSequence(cdna.getSequenceAt(1), false);
625     mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
626     assertEquals(2, mappedGroup.getStartRes());
627     assertEquals(4, mappedGroup.getEndRes());
628
629     // add Seq3 to dna selection cols 4-5 include codon 1 (Q)
630     sg.addSequence(cdna.getSequenceAt(2), false);
631     mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
632     assertEquals(0, mappedGroup.getStartRes());
633     assertEquals(4, mappedGroup.getEndRes());
634   }
635
636   @Test(groups = { "Functional" })
637   public void testFindMappingsForSequence()
638   {
639     SequenceI seq1 = new Sequence("Seq1", "ABC");
640     SequenceI seq2 = new Sequence("Seq2", "ABC");
641     SequenceI seq3 = new Sequence("Seq3", "ABC");
642     SequenceI seq4 = new Sequence("Seq4", "ABC");
643     seq1.createDatasetSequence();
644     seq2.createDatasetSequence();
645     seq3.createDatasetSequence();
646     seq4.createDatasetSequence();
647
648     /*
649      * Create mappings from seq1 to seq2, seq2 to seq1, seq3 to seq1
650      */
651     AlignedCodonFrame acf1 = new AlignedCodonFrame();
652     MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 3 }, 1, 1);
653     acf1.addMap(seq1.getDatasetSequence(), seq2.getDatasetSequence(), map);
654     AlignedCodonFrame acf2 = new AlignedCodonFrame();
655     acf2.addMap(seq2.getDatasetSequence(), seq1.getDatasetSequence(), map);
656     AlignedCodonFrame acf3 = new AlignedCodonFrame();
657     acf3.addMap(seq3.getDatasetSequence(), seq1.getDatasetSequence(), map);
658
659     List<AlignedCodonFrame> mappings = new ArrayList<AlignedCodonFrame>();
660     mappings.add(acf1);
661     mappings.add(acf2);
662     mappings.add(acf3);
663
664     /*
665      * Seq1 has three mappings
666      */
667     List<AlignedCodonFrame> result = MappingUtils.findMappingsForSequence(
668             seq1, mappings);
669     assertEquals(3, result.size());
670     assertTrue(result.contains(acf1));
671     assertTrue(result.contains(acf2));
672     assertTrue(result.contains(acf3));
673
674     /*
675      * Seq2 has two mappings
676      */
677     result = MappingUtils.findMappingsForSequence(seq2, mappings);
678     assertEquals(2, result.size());
679     assertTrue(result.contains(acf1));
680     assertTrue(result.contains(acf2));
681
682     /*
683      * Seq3 has one mapping
684      */
685     result = MappingUtils.findMappingsForSequence(seq3, mappings);
686     assertEquals(1, result.size());
687     assertTrue(result.contains(acf3));
688
689     /*
690      * Seq4 has no mappings
691      */
692     result = MappingUtils.findMappingsForSequence(seq4, mappings);
693     assertEquals(0, result.size());
694
695     result = MappingUtils.findMappingsForSequence(null, mappings);
696     assertEquals(0, result.size());
697
698     result = MappingUtils.findMappingsForSequence(seq1, null);
699     assertEquals(0, result.size());
700
701     result = MappingUtils.findMappingsForSequence(null, null);
702     assertEquals(0, result.size());
703   }
704
705   @Test(groups = { "Functional" })
706   public void testMapEditCommand()
707   {
708     SequenceI dna = new Sequence("Seq1", "---ACG---GCATCA", 8, 16);
709     SequenceI protein = new Sequence("Seq2", "-T-AS", 5, 7);
710     dna.createDatasetSequence();
711     protein.createDatasetSequence();
712     AlignedCodonFrame acf = new AlignedCodonFrame();
713     MapList map = new MapList(new int[] { 8, 16 }, new int[] { 5, 7 }, 3, 1);
714     acf.addMap(dna.getDatasetSequence(), protein.getDatasetSequence(), map);
715     List<AlignedCodonFrame> mappings = new ArrayList<AlignedCodonFrame>();
716     mappings.add(acf);
717
718     AlignmentI prot = new Alignment(new SequenceI[] { protein });
719     prot.setCodonFrames(mappings);
720     AlignmentI nuc = new Alignment(new SequenceI[] { dna });
721
722     /*
723      * construct and perform the edit command to turn "-T-AS" in to "-T-A--S"
724      * i.e. insert two gaps at column 4
725      */
726     EditCommand ec = new EditCommand();
727     final Edit edit = ec.new Edit(Action.INSERT_GAP,
728             new SequenceI[] { protein }, 4, 2, '-');
729     ec.appendEdit(edit, prot, true, null);
730
731     /*
732      * the mapped edit command should be to insert 6 gaps before base 4 in the
733      * nucleotide sequence, which corresponds to aligned column 12 in the dna
734      */
735     EditCommand mappedEdit = MappingUtils.mapEditCommand(ec, false, nuc,
736             '-', mappings);
737     assertEquals(1, mappedEdit.getEdits().size());
738     Edit e = mappedEdit.getEdits().get(0);
739     assertEquals(1, e.getSequences().length);
740     assertEquals(dna, e.getSequences()[0]);
741     assertEquals(12, e.getPosition());
742     assertEquals(6, e.getNumber());
743   }
744
745   /**
746    * Tests for the method that converts a series of [start, end] ranges to
747    * single positions, where the mapping is to a reverse strand i.e. start is
748    * greater than end point mapped to
749    */
750   @Test(groups = { "Functional" })
751   public void testFlattenRanges_reverseStrand()
752   {
753     assertEquals("[4, 3, 2, 1]",
754             Arrays.toString(MappingUtils.flattenRanges(new int[] { 4, 1 })));
755     assertEquals(
756             "[4, 3, 2, 1]",
757             Arrays.toString(MappingUtils.flattenRanges(new int[] { 4, 3, 2,
758                 1 })));
759     assertEquals(
760             "[4, 3, 2, 1]",
761             Arrays.toString(MappingUtils.flattenRanges(new int[] { 4, 4, 3,
762                 3, 2, 2, 1, 1 })));
763     assertEquals(
764             "[12, 9, 8, 7, 4, 3, 2, 1]",
765             Arrays.toString(MappingUtils.flattenRanges(new int[] { 12, 12,
766                 9, 7, 4, 1 })));
767     // forwards and backwards anyone?
768     assertEquals(
769             "[4, 5, 6, 3, 2, 1]",
770             Arrays.toString(MappingUtils.flattenRanges(new int[] { 4, 6, 3,
771                 1 })));
772     // backwards and forwards
773     assertEquals(
774             "[3, 2, 1, 4, 5, 6]",
775             Arrays.toString(MappingUtils.flattenRanges(new int[] { 3, 1, 4,
776                 6 })));
777     // trailing unpaired start position is ignored:
778     assertEquals(
779             "[12, 9, 8, 7, 4, 3, 2]",
780             Arrays.toString(MappingUtils.flattenRanges(new int[] { 12, 12,
781                 9, 7, 4, 2, 1 })));
782   }
783
784   /**
785    * Test mapping a column selection including hidden columns
786    * 
787    * @throws IOException
788    */
789   @Test(groups = { "Functional" })
790   public void testMapColumnSelection_hiddenColumns() throws IOException
791   {
792     setupMappedAlignments();
793   
794     ColumnSelection proteinSelection = new ColumnSelection();
795
796     /*
797      * Column 0 in protein picks up Seq2/L, Seq3/G which map to cols 0-4 and 0-3
798      * in dna respectively, overall 0-4
799      */
800     proteinSelection.hideColumns(0);
801     ColumnSelection dnaSelection = MappingUtils.mapColumnSelection(proteinSelection,
802             proteinView, dnaView);
803     assertEquals("[]", dnaSelection.getSelected().toString());
804     List<int[]> hidden = dnaSelection.getHiddenColumns();
805     assertEquals(1, hidden.size());
806     assertEquals("[0, 4]", Arrays.toString(hidden.get(0)));
807
808     /*
809      * Column 1 in protein picks up Seq1/K which maps to cols 0-3 in dna
810      */
811     proteinSelection.revealAllHiddenColumns();
812     // the unhidden columns are now marked selected!
813     assertEquals("[0]", proteinSelection.getSelected().toString());
814     // deselect these or hideColumns will be expanded to include 0
815     proteinSelection.clear();
816     proteinSelection.hideColumns(1);
817     dnaSelection = MappingUtils.mapColumnSelection(proteinSelection, proteinView, dnaView);
818     hidden = dnaSelection.getHiddenColumns();
819     assertEquals(1, hidden.size());
820     assertEquals("[0, 3]", Arrays.toString(hidden.get(0)));
821
822     /*
823      * Column 2 in protein picks up gaps only - no mapping
824      */
825     proteinSelection.revealAllHiddenColumns();
826     proteinSelection.clear();
827     proteinSelection.hideColumns(2);
828     dnaSelection = MappingUtils.mapColumnSelection(proteinSelection, proteinView, dnaView);
829     assertTrue(dnaSelection.getHiddenColumns().isEmpty());
830
831     /*
832      * Column 3 in protein picks up Seq1/P, Seq2/Q, Seq3/S which map to columns
833      * 6-9, 6-10, 5-8 respectively, overall to 5-10
834      */
835     proteinSelection.revealAllHiddenColumns();
836     proteinSelection.clear();
837     proteinSelection.hideColumns(3); // 5-10 hidden in dna
838     proteinSelection.addElement(1); // 0-3 selected in dna
839     dnaSelection = MappingUtils.mapColumnSelection(proteinSelection, proteinView, dnaView);
840     assertEquals("[0, 1, 2, 3]", dnaSelection.getSelected().toString());
841     hidden = dnaSelection.getHiddenColumns();
842     assertEquals(1, hidden.size());
843     assertEquals("[5, 10]", Arrays.toString(hidden.get(0)));
844
845     /*
846      * Combine hiding columns 1 and 3 to get discontiguous hidden columns
847      */
848     proteinSelection.revealAllHiddenColumns();
849     proteinSelection.clear();
850     proteinSelection.hideColumns(1);
851     proteinSelection.hideColumns(3);
852     dnaSelection = MappingUtils.mapColumnSelection(proteinSelection, proteinView, dnaView);
853     hidden = dnaSelection.getHiddenColumns();
854     assertEquals(2, hidden.size());
855     assertEquals("[0, 3]", Arrays.toString(hidden.get(0)));
856     assertEquals("[5, 10]", Arrays.toString(hidden.get(1)));
857   }
858 }