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