JAL-3763 Javadoc, check for null, additional tests
[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 java.awt.Color;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Iterator;
33 import java.util.List;
34
35 import org.testng.annotations.BeforeClass;
36 import org.testng.annotations.Test;
37
38 import jalview.api.AlignViewportI;
39 import jalview.bin.Cache;
40 import jalview.commands.EditCommand;
41 import jalview.commands.EditCommand.Action;
42 import jalview.commands.EditCommand.Edit;
43 import jalview.datamodel.AlignedCodonFrame;
44 import jalview.datamodel.Alignment;
45 import jalview.datamodel.AlignmentI;
46 import jalview.datamodel.ColumnSelection;
47 import jalview.datamodel.HiddenColumns;
48 import jalview.datamodel.SearchResultMatchI;
49 import jalview.datamodel.SearchResultsI;
50 import jalview.datamodel.Sequence;
51 import jalview.datamodel.SequenceGroup;
52 import jalview.datamodel.SequenceI;
53 import jalview.gui.AlignViewport;
54 import jalview.gui.JvOptionPane;
55 import jalview.io.DataSourceType;
56 import jalview.io.FileFormat;
57 import jalview.io.FileFormatI;
58 import jalview.io.FormatAdapter;
59
60 public class MappingUtilsTest
61 {
62   @BeforeClass(alwaysRun = true)
63   public void setUp()
64   {
65     Cache.initLogger();
66   }
67   
68
69   @BeforeClass(alwaysRun = true)
70   public void setUpJvOptionPane()
71   {
72     JvOptionPane.setInteractiveMode(false);
73     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
74   }
75
76   private AlignViewportI dnaView;
77
78   private AlignViewportI proteinView;
79
80   /**
81    * Simple test of mapping with no intron involved.
82    */
83   @Test(groups = { "Functional" })
84   public void testBuildSearchResults()
85   {
86     final Sequence seq1 = new Sequence("Seq1/5-10", "C-G-TA-GC");
87     seq1.createDatasetSequence();
88
89     final Sequence aseq1 = new Sequence("Seq1/12-13", "-P-R");
90     aseq1.createDatasetSequence();
91
92     /*
93      * Map dna bases 5-10 to protein residues 12-13
94      */
95     AlignedCodonFrame acf = new AlignedCodonFrame();
96     MapList map = new MapList(new int[] { 5, 10 }, new int[] { 12, 13 }, 3,
97             1);
98     acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
99     List<AlignedCodonFrame> acfList = Arrays
100             .asList(new AlignedCodonFrame[]
101             { acf });
102
103     /*
104      * Check protein residue 12 maps to codon 5-7, 13 to codon 8-10
105      */
106     SearchResultsI sr = MappingUtils.buildSearchResults(aseq1, 12, acfList);
107     assertEquals(1, sr.getResults().size());
108     SearchResultMatchI m = sr.getResults().get(0);
109     assertEquals(seq1.getDatasetSequence(), m.getSequence());
110     assertEquals(5, m.getStart());
111     assertEquals(7, m.getEnd());
112     sr = MappingUtils.buildSearchResults(aseq1, 13, acfList);
113     assertEquals(1, sr.getResults().size());
114     m = sr.getResults().get(0);
115     assertEquals(seq1.getDatasetSequence(), m.getSequence());
116     assertEquals(8, m.getStart());
117     assertEquals(10, m.getEnd());
118
119     /*
120      * Check inverse mappings, from codons 5-7, 8-10 to protein 12, 13
121      */
122     for (int i = 5; i < 11; i++)
123     {
124       sr = MappingUtils.buildSearchResults(seq1, i, acfList);
125       assertEquals(1, sr.getResults().size());
126       m = sr.getResults().get(0);
127       assertEquals(aseq1.getDatasetSequence(), m.getSequence());
128       int residue = i > 7 ? 13 : 12;
129       assertEquals(residue, m.getStart());
130       assertEquals(residue, m.getEnd());
131     }
132   }
133
134   /**
135    * Simple test of mapping with introns involved.
136    */
137   @Test(groups = { "Functional" })
138   public void testBuildSearchResults_withIntron()
139   {
140     final Sequence seq1 = new Sequence("Seq1/5-17", "c-G-tAGa-GcAgCtt");
141     seq1.createDatasetSequence();
142
143     final Sequence aseq1 = new Sequence("Seq1/8-9", "-E-D");
144     aseq1.createDatasetSequence();
145
146     /*
147      * Map dna bases [6, 8, 9], [11, 13, 115] to protein residues 8 and 9
148      */
149     AlignedCodonFrame acf = new AlignedCodonFrame();
150     MapList map = new MapList(
151             new int[]
152             { 6, 6, 8, 9, 11, 11, 13, 13, 15, 15 }, new int[] { 8, 9 }, 3,
153             1);
154     acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
155     List<AlignedCodonFrame> acfList = Arrays
156             .asList(new AlignedCodonFrame[]
157             { acf });
158
159     /*
160      * Check protein residue 8 maps to [6, 8, 9]
161      */
162     SearchResultsI sr = MappingUtils.buildSearchResults(aseq1, 8, acfList);
163     assertEquals(2, sr.getResults().size());
164     SearchResultMatchI m = sr.getResults().get(0);
165     assertEquals(seq1.getDatasetSequence(), m.getSequence());
166     assertEquals(6, m.getStart());
167     assertEquals(6, m.getEnd());
168     m = sr.getResults().get(1);
169     assertEquals(seq1.getDatasetSequence(), m.getSequence());
170     assertEquals(8, m.getStart());
171     assertEquals(9, m.getEnd());
172
173     /*
174      * Check protein residue 9 maps to [11, 13, 15]
175      */
176     sr = MappingUtils.buildSearchResults(aseq1, 9, acfList);
177     assertEquals(3, sr.getResults().size());
178     m = sr.getResults().get(0);
179     assertEquals(seq1.getDatasetSequence(), m.getSequence());
180     assertEquals(11, m.getStart());
181     assertEquals(11, m.getEnd());
182     m = sr.getResults().get(1);
183     assertEquals(seq1.getDatasetSequence(), m.getSequence());
184     assertEquals(13, m.getStart());
185     assertEquals(13, m.getEnd());
186     m = sr.getResults().get(2);
187     assertEquals(seq1.getDatasetSequence(), m.getSequence());
188     assertEquals(15, m.getStart());
189     assertEquals(15, m.getEnd());
190
191     /*
192      * Check inverse mappings, from codons to protein
193      */
194     for (int i = 5; i < 18; i++)
195     {
196       sr = MappingUtils.buildSearchResults(seq1, i, acfList);
197       int residue = (i == 6 || i == 8 || i == 9) ? 8
198               : (i == 11 || i == 13 || i == 15 ? 9 : 0);
199       if (residue == 0)
200       {
201         assertEquals(0, sr.getResults().size());
202         continue;
203       }
204       assertEquals(1, sr.getResults().size());
205       m = sr.getResults().get(0);
206       assertEquals(aseq1.getDatasetSequence(), m.getSequence());
207       assertEquals(residue, m.getStart());
208       assertEquals(residue, m.getEnd());
209     }
210   }
211
212   /**
213    * Test mapping a sequence group made of entire sequences.
214    * 
215    * @throws IOException
216    */
217   @Test(groups = { "Functional" })
218   public void testMapSequenceGroup_sequences() throws IOException
219   {
220     /*
221      * Set up dna and protein Seq1/2/3 with mappings (held on the protein
222      * viewport).
223      */
224     AlignmentI cdna = loadAlignment(">Seq1\nACG\n>Seq2\nTGA\n>Seq3\nTAC\n",
225             FileFormat.Fasta);
226     cdna.setDataset(null);
227     AlignmentI protein = loadAlignment(">Seq1\nK\n>Seq2\nL\n>Seq3\nQ\n",
228             FileFormat.Fasta);
229     protein.setDataset(null);
230     AlignedCodonFrame acf = new AlignedCodonFrame();
231     MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 1 }, 3, 1);
232     for (int seq = 0; seq < 3; seq++)
233     {
234       acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(),
235               protein.getSequenceAt(seq).getDatasetSequence(), map);
236     }
237     List<AlignedCodonFrame> acfList = Arrays
238             .asList(new AlignedCodonFrame[]
239             { acf });
240
241     AlignViewportI dnaView = new AlignViewport(cdna);
242     AlignViewportI proteinView = new AlignViewport(protein);
243     protein.setCodonFrames(acfList);
244
245     /*
246      * Select Seq1 and Seq3 in the protein
247      */
248     SequenceGroup sg = new SequenceGroup();
249     sg.setColourText(true);
250     sg.setIdColour(Color.GREEN);
251     sg.setOutlineColour(Color.LIGHT_GRAY);
252     sg.addSequence(protein.getSequenceAt(0), false);
253     sg.addSequence(protein.getSequenceAt(2), false);
254     sg.setEndRes(protein.getWidth() - 1);
255
256     /*
257      * Verify the mapped sequence group in dna
258      */
259     SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
260             proteinView, dnaView);
261     assertTrue(mappedGroup.getColourText());
262     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
263     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
264     assertEquals(2, mappedGroup.getSequences().size());
265     assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
266     assertSame(cdna.getSequenceAt(2), mappedGroup.getSequences().get(1));
267     assertEquals(0, mappedGroup.getStartRes());
268     assertEquals(2, mappedGroup.getEndRes()); // 3 columns (1 codon)
269
270     /*
271      * Verify mapping sequence group from dna to protein
272      */
273     sg.clear();
274     sg.addSequence(cdna.getSequenceAt(1), false);
275     sg.addSequence(cdna.getSequenceAt(0), false);
276     sg.setStartRes(0);
277     sg.setEndRes(2);
278     mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
279     assertTrue(mappedGroup.getColourText());
280     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
281     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
282     assertEquals(2, mappedGroup.getSequences().size());
283     assertSame(protein.getSequenceAt(1), mappedGroup.getSequences().get(0));
284     assertSame(protein.getSequenceAt(0), mappedGroup.getSequences().get(1));
285     assertEquals(0, mappedGroup.getStartRes());
286     assertEquals(0, mappedGroup.getEndRes());
287   }
288
289   /**
290    * Helper method to load an alignment and ensure dataset sequences are set up.
291    * 
292    * @param data
293    * @param format
294    *          TODO
295    * @return
296    * @throws IOException
297    */
298   protected AlignmentI loadAlignment(final String data, FileFormatI format)
299           throws IOException
300   {
301     AlignmentI a = new FormatAdapter().readFile(data, DataSourceType.PASTE,
302             format);
303     a.setDataset(null);
304     return a;
305   }
306
307   /**
308    * Test mapping a column selection in protein to its dna equivalent
309    * 
310    * @throws IOException
311    */
312   @Test(groups = { "Functional" })
313   public void testMapColumnSelection_proteinToDna() throws IOException
314   {
315     setupMappedAlignments();
316
317     ColumnSelection colsel = new ColumnSelection();
318     HiddenColumns hidden = new HiddenColumns();
319
320     /*
321      * Column 0 in protein picks up Seq2/L, Seq3/G which map to cols 0-4 and 0-3
322      * in dna respectively, overall 0-4
323      */
324     colsel.addElement(0);
325     ColumnSelection cs = new ColumnSelection();
326     HiddenColumns hs = new HiddenColumns();
327     MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
328             cs, hs);
329     assertEquals("[0, 1, 2, 3, 4]", cs.getSelected().toString());
330
331     /*
332      * Column 1 in protein picks up Seq1/K which maps to cols 0-3 in dna
333      */
334     cs.clear();
335     colsel.clear();
336     colsel.addElement(1);
337     MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
338             cs, hs);
339     assertEquals("[0, 1, 2, 3]", cs.getSelected().toString());
340
341     /*
342      * Column 2 in protein picks up gaps only - no mapping
343      */
344     cs.clear();
345     colsel.clear();
346     colsel.addElement(2);
347     MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
348             cs, hs);
349     assertEquals("[]", cs.getSelected().toString());
350
351     /*
352      * Column 3 in protein picks up Seq1/P, Seq2/Q, Seq3/S which map to columns
353      * 6-9, 6-10, 5-8 respectively, overall to 5-10
354      */
355     cs.clear();
356     colsel.clear();
357     colsel.addElement(3);
358     MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
359             cs, hs);
360     assertEquals("[5, 6, 7, 8, 9, 10]", cs.getSelected().toString());
361
362     /*
363      * Combine selection of columns 1 and 3 to get a discontiguous mapped
364      * selection
365      */
366     cs.clear();
367     colsel.clear();
368     colsel.addElement(1);
369     colsel.addElement(3);
370     MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
371             cs, hs);
372     assertEquals("[0, 1, 2, 3, 5, 6, 7, 8, 9, 10]",
373             cs.getSelected().toString());
374   }
375
376   /**
377    * Set up mappings for tests from 3 dna to 3 protein sequences. Sequences have
378    * offset start positions for a more general test case.
379    * 
380    * @throws IOException
381    */
382   protected void setupMappedAlignments() throws IOException
383   {
384     /*
385      * Map (upper-case = coding):
386      * Seq1/10-18 AC-GctGtC-T to Seq1/40 -K-P
387      * Seq2/20-27 Tc-GA-G-T-T to Seq2/20-27 L--Q
388      * Seq3/30-38 TtTT-AaCGg- to Seq3/60-61\nG--S
389      */
390     AlignmentI cdna = loadAlignment(">Seq1/10-18\nAC-GctGtC-T\n"
391             + ">Seq2/20-27\nTc-GA-G-T-Tc\n" + ">Seq3/30-38\nTtTT-AaCGg-\n",
392             FileFormat.Fasta);
393     cdna.setDataset(null);
394     AlignmentI protein = loadAlignment(
395             ">Seq1/40-41\n-K-P\n>Seq2/50-51\nL--Q\n>Seq3/60-61\nG--S\n",
396             FileFormat.Fasta);
397     protein.setDataset(null);
398
399     // map first dna to first protein seq
400     AlignedCodonFrame acf = new AlignedCodonFrame();
401     MapList map = new MapList(new int[] { 10, 12, 15, 15, 17, 18 },
402             new int[]
403             { 40, 41 }, 3, 1);
404     acf.addMap(cdna.getSequenceAt(0).getDatasetSequence(),
405             protein.getSequenceAt(0).getDatasetSequence(), map);
406
407     // map second dna to second protein seq
408     map = new MapList(new int[] { 20, 20, 22, 23, 24, 26 },
409             new int[]
410             { 50, 51 }, 3, 1);
411     acf.addMap(cdna.getSequenceAt(1).getDatasetSequence(),
412             protein.getSequenceAt(1).getDatasetSequence(), map);
413
414     // map third dna to third protein seq
415     map = new MapList(new int[] { 30, 30, 32, 34, 36, 37 },
416             new int[]
417             { 60, 61 }, 3, 1);
418     acf.addMap(cdna.getSequenceAt(2).getDatasetSequence(),
419             protein.getSequenceAt(2).getDatasetSequence(), map);
420     List<AlignedCodonFrame> acfList = Arrays
421             .asList(new AlignedCodonFrame[]
422             { acf });
423
424     dnaView = new AlignViewport(cdna);
425     proteinView = new AlignViewport(protein);
426     protein.setCodonFrames(acfList);
427   }
428
429   /**
430    * Test mapping a column selection in dna to its protein equivalent
431    * 
432    * @throws IOException
433    */
434   @Test(groups = { "Functional" })
435   public void testMapColumnSelection_dnaToProtein() throws IOException
436   {
437     setupMappedAlignments();
438
439     ColumnSelection colsel = new ColumnSelection();
440     HiddenColumns hidden = new HiddenColumns();
441
442     /*
443      * Column 0 in dna picks up first bases which map to residue 1, columns 0-1
444      * in protein.
445      */
446     ColumnSelection cs = new ColumnSelection();
447     HiddenColumns hs = new HiddenColumns();
448     colsel.addElement(0);
449     MappingUtils.mapColumnSelection(colsel, hidden, dnaView, proteinView,
450             cs, hs);
451     assertEquals("[0, 1]", cs.getSelected().toString());
452
453     /*
454      * Columns 3-5 in dna map to the first residues in protein Seq1, Seq2, and
455      * the first two in Seq3. Overall to columns 0, 1, 3 (col2 is all gaps).
456      */
457     colsel.addElement(3);
458     colsel.addElement(4);
459     colsel.addElement(5);
460     cs.clear();
461     MappingUtils.mapColumnSelection(colsel, hidden, dnaView, proteinView,
462             cs, hs);
463     assertEquals("[0, 1, 3]", cs.getSelected().toString());
464   }
465
466   @Test(groups = { "Functional" })
467   public void testMapColumnSelection_null() throws IOException
468   {
469     setupMappedAlignments();
470     ColumnSelection cs = new ColumnSelection();
471     HiddenColumns hs = new HiddenColumns();
472     MappingUtils.mapColumnSelection(null, null, dnaView, proteinView, cs,
473             hs);
474     assertTrue("mapped selection not empty", cs.getSelected().isEmpty());
475   }
476
477   /**
478    * Tests for the method that converts a series of [start, end] ranges to
479    * single positions
480    */
481   @Test(groups = { "Functional" })
482   public void testFlattenRanges()
483   {
484     assertEquals("[1, 2, 3, 4]",
485             Arrays.toString(MappingUtils.flattenRanges(new int[]
486             { 1, 4 })));
487     assertEquals("[1, 2, 3, 4]",
488             Arrays.toString(MappingUtils.flattenRanges(new int[]
489             { 1, 2, 3, 4 })));
490     assertEquals("[1, 2, 3, 4]",
491             Arrays.toString(MappingUtils.flattenRanges(new int[]
492             { 1, 1, 2, 2, 3, 3, 4, 4 })));
493     assertEquals("[1, 2, 3, 4, 7, 8, 9, 12]",
494             Arrays.toString(MappingUtils.flattenRanges(new int[]
495             { 1, 4, 7, 9, 12, 12 })));
496     // trailing unpaired start position is ignored:
497     assertEquals("[1, 2, 3, 4, 7, 8, 9, 12]",
498             Arrays.toString(MappingUtils.flattenRanges(new int[]
499             { 1, 4, 7, 9, 12, 12, 15 })));
500   }
501
502   /**
503    * Test mapping a sequence group made of entire columns.
504    * 
505    * @throws IOException
506    */
507   @Test(groups = { "Functional" })
508   public void testMapSequenceGroup_columns() throws IOException
509   {
510     /*
511      * Set up dna and protein Seq1/2/3 with mappings (held on the protein
512      * viewport).
513      */
514     AlignmentI cdna = loadAlignment(
515             ">Seq1\nACGGCA\n>Seq2\nTGACAG\n>Seq3\nTACGTA\n",
516             FileFormat.Fasta);
517     cdna.setDataset(null);
518     AlignmentI protein = loadAlignment(">Seq1\nKA\n>Seq2\nLQ\n>Seq3\nQV\n",
519             FileFormat.Fasta);
520     protein.setDataset(null);
521     AlignedCodonFrame acf = new AlignedCodonFrame();
522     MapList map = new MapList(new int[] { 1, 6 }, new int[] { 1, 2 }, 3, 1);
523     for (int seq = 0; seq < 3; seq++)
524     {
525       acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(),
526               protein.getSequenceAt(seq).getDatasetSequence(), map);
527     }
528     List<AlignedCodonFrame> acfList = Arrays
529             .asList(new AlignedCodonFrame[]
530             { acf });
531
532     AlignViewportI dnaView = new AlignViewport(cdna);
533     AlignViewportI proteinView = new AlignViewport(protein);
534     protein.setCodonFrames(acfList);
535
536     /*
537      * Select all sequences, column 2 in the protein
538      */
539     SequenceGroup sg = new SequenceGroup();
540     sg.setColourText(true);
541     sg.setIdColour(Color.GREEN);
542     sg.setOutlineColour(Color.LIGHT_GRAY);
543     sg.addSequence(protein.getSequenceAt(0), false);
544     sg.addSequence(protein.getSequenceAt(1), false);
545     sg.addSequence(protein.getSequenceAt(2), false);
546     sg.setStartRes(1);
547     sg.setEndRes(1);
548
549     /*
550      * Verify the mapped sequence group in dna
551      */
552     SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
553             proteinView, dnaView);
554     assertTrue(mappedGroup.getColourText());
555     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
556     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
557     assertEquals(3, mappedGroup.getSequences().size());
558     assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
559     assertSame(cdna.getSequenceAt(1), mappedGroup.getSequences().get(1));
560     assertSame(cdna.getSequenceAt(2), mappedGroup.getSequences().get(2));
561     assertEquals(3, mappedGroup.getStartRes());
562     assertEquals(5, mappedGroup.getEndRes());
563
564     /*
565      * Verify mapping sequence group from dna to protein
566      */
567     sg.clear();
568     sg.addSequence(cdna.getSequenceAt(0), false);
569     sg.addSequence(cdna.getSequenceAt(1), false);
570     sg.addSequence(cdna.getSequenceAt(2), false);
571     // select columns 2 and 3 in DNA which span protein columns 0 and 1
572     sg.setStartRes(2);
573     sg.setEndRes(3);
574     mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
575     assertTrue(mappedGroup.getColourText());
576     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
577     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
578     assertEquals(3, mappedGroup.getSequences().size());
579     assertSame(protein.getSequenceAt(0), mappedGroup.getSequences().get(0));
580     assertSame(protein.getSequenceAt(1), mappedGroup.getSequences().get(1));
581     assertSame(protein.getSequenceAt(2), mappedGroup.getSequences().get(2));
582     assertEquals(0, mappedGroup.getStartRes());
583     assertEquals(1, mappedGroup.getEndRes());
584   }
585
586   /**
587    * Test mapping a sequence group made of a sequences/columns region.
588    * 
589    * @throws IOException
590    */
591   @Test(groups = { "Functional" })
592   public void testMapSequenceGroup_region() throws IOException
593   {
594     /*
595      * Set up gapped dna and protein Seq1/2/3 with mappings (held on the protein
596      * viewport).
597      */
598     AlignmentI cdna = loadAlignment(
599             ">Seq1\nA-CG-GC--AT-CA\n>Seq2\n-TG-AC-AG-T-AT\n>Seq3\n-T--ACG-TAAT-G\n",
600             FileFormat.Fasta);
601     cdna.setDataset(null);
602     AlignmentI protein = loadAlignment(
603             ">Seq1\n-KA-S\n>Seq2\n--L-QY\n>Seq3\nQ-V-M\n",
604             FileFormat.Fasta);
605     protein.setDataset(null);
606     AlignedCodonFrame acf = new AlignedCodonFrame();
607     MapList map = new MapList(new int[] { 1, 9 }, new int[] { 1, 3 }, 3, 1);
608     for (int seq = 0; seq < 3; seq++)
609     {
610       acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(),
611               protein.getSequenceAt(seq).getDatasetSequence(), map);
612     }
613     List<AlignedCodonFrame> acfList = Arrays
614             .asList(new AlignedCodonFrame[]
615             { acf });
616
617     AlignViewportI dnaView = new AlignViewport(cdna);
618     AlignViewportI proteinView = new AlignViewport(protein);
619     protein.setCodonFrames(acfList);
620
621     /*
622      * Select Seq1 and Seq2 in the protein, column 1 (K/-). Expect mapped
623      * sequence group to cover Seq1, columns 0-3 (ACG). Because the selection
624      * only includes a gap in Seq2 there is no mappable selection region in the
625      * corresponding DNA.
626      */
627     SequenceGroup sg = new SequenceGroup();
628     sg.setColourText(true);
629     sg.setIdColour(Color.GREEN);
630     sg.setOutlineColour(Color.LIGHT_GRAY);
631     sg.addSequence(protein.getSequenceAt(0), false);
632     sg.addSequence(protein.getSequenceAt(1), false);
633     sg.setStartRes(1);
634     sg.setEndRes(1);
635
636     /*
637      * Verify the mapped sequence group in dna
638      */
639     SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
640             proteinView, dnaView);
641     assertTrue(mappedGroup.getColourText());
642     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
643     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
644     assertEquals(1, mappedGroup.getSequences().size());
645     assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
646     // Seq2 in protein has a gap in column 1 - ignored
647     // Seq1 has K which should map to columns 0-3 in Seq1
648     assertEquals(0, mappedGroup.getStartRes());
649     assertEquals(3, mappedGroup.getEndRes());
650
651     /*
652      * Now select cols 2-4 in protein. These cover Seq1:AS Seq2:LQ Seq3:VM which
653      * extend over DNA columns 3-12, 1-7, 6-13 respectively, or 1-13 overall.
654      */
655     sg.setStartRes(2);
656     sg.setEndRes(4);
657     mappedGroup = MappingUtils.mapSequenceGroup(sg, proteinView, dnaView);
658     assertEquals(1, mappedGroup.getStartRes());
659     assertEquals(13, mappedGroup.getEndRes());
660
661     /*
662      * Verify mapping sequence group from dna to protein
663      */
664     sg.clear();
665     sg.addSequence(cdna.getSequenceAt(0), false);
666
667     // select columns 4,5 - includes Seq1:codon2 (A) only
668     sg.setStartRes(4);
669     sg.setEndRes(5);
670     mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
671     assertEquals(2, mappedGroup.getStartRes());
672     assertEquals(2, mappedGroup.getEndRes());
673
674     // add Seq2 to dna selection cols 4-5 include codons 1 and 2 (LQ)
675     sg.addSequence(cdna.getSequenceAt(1), false);
676     mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
677     assertEquals(2, mappedGroup.getStartRes());
678     assertEquals(4, mappedGroup.getEndRes());
679
680     // add Seq3 to dna selection cols 4-5 include codon 1 (Q)
681     sg.addSequence(cdna.getSequenceAt(2), false);
682     mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
683     assertEquals(0, mappedGroup.getStartRes());
684     assertEquals(4, mappedGroup.getEndRes());
685   }
686
687   @Test(groups = { "Functional" })
688   public void testFindMappingsForSequence()
689   {
690     SequenceI seq1 = new Sequence("Seq1", "ABC");
691     SequenceI seq2 = new Sequence("Seq2", "ABC");
692     SequenceI seq3 = new Sequence("Seq3", "ABC");
693     SequenceI seq4 = new Sequence("Seq4", "ABC");
694     seq1.createDatasetSequence();
695     seq2.createDatasetSequence();
696     seq3.createDatasetSequence();
697     seq4.createDatasetSequence();
698
699     /*
700      * Create mappings from seq1 to seq2, seq2 to seq1, seq3 to seq1
701      */
702     AlignedCodonFrame acf1 = new AlignedCodonFrame();
703     MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 3 }, 1, 1);
704     acf1.addMap(seq1.getDatasetSequence(), seq2.getDatasetSequence(), map);
705     AlignedCodonFrame acf2 = new AlignedCodonFrame();
706     acf2.addMap(seq2.getDatasetSequence(), seq1.getDatasetSequence(), map);
707     AlignedCodonFrame acf3 = new AlignedCodonFrame();
708     acf3.addMap(seq3.getDatasetSequence(), seq1.getDatasetSequence(), map);
709
710     List<AlignedCodonFrame> mappings = new ArrayList<>();
711     mappings.add(acf1);
712     mappings.add(acf2);
713     mappings.add(acf3);
714
715     /*
716      * Seq1 has three mappings
717      */
718     List<AlignedCodonFrame> result = MappingUtils
719             .findMappingsForSequence(seq1, mappings);
720     assertEquals(3, result.size());
721     assertTrue(result.contains(acf1));
722     assertTrue(result.contains(acf2));
723     assertTrue(result.contains(acf3));
724
725     /*
726      * Seq2 has two mappings
727      */
728     result = MappingUtils.findMappingsForSequence(seq2, mappings);
729     assertEquals(2, result.size());
730     assertTrue(result.contains(acf1));
731     assertTrue(result.contains(acf2));
732
733     /*
734      * Seq3 has one mapping
735      */
736     result = MappingUtils.findMappingsForSequence(seq3, mappings);
737     assertEquals(1, result.size());
738     assertTrue(result.contains(acf3));
739
740     /*
741      * Seq4 has no mappings
742      */
743     result = MappingUtils.findMappingsForSequence(seq4, mappings);
744     assertEquals(0, result.size());
745
746     result = MappingUtils.findMappingsForSequence(null, mappings);
747     assertEquals(0, result.size());
748
749     result = MappingUtils.findMappingsForSequence(seq1, null);
750     assertEquals(0, result.size());
751
752     result = MappingUtils.findMappingsForSequence(null, null);
753     assertEquals(0, result.size());
754   }
755
756   /**
757    * just like the one above, but this time, we provide a set of sequences to
758    * subselect the mapping search
759    */
760   @Test(groups = { "Functional" })
761   public void testFindMappingsForSequenceAndOthers()
762   {
763     SequenceI seq1 = new Sequence("Seq1", "ABC");
764     SequenceI seq2 = new Sequence("Seq2", "ABC");
765     SequenceI seq3 = new Sequence("Seq3", "ABC");
766     SequenceI seq4 = new Sequence("Seq4", "ABC");
767     seq1.createDatasetSequence();
768     seq2.createDatasetSequence();
769     seq3.createDatasetSequence();
770     seq4.createDatasetSequence();
771
772     /*
773      * Create mappings from seq1 to seq2, seq2 to seq1, seq3 to seq1, seq3 to seq4
774      */
775     AlignedCodonFrame acf1 = new AlignedCodonFrame();
776     MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 3 }, 1, 1);
777     acf1.addMap(seq1.getDatasetSequence(), seq2.getDatasetSequence(), map);
778     AlignedCodonFrame acf2 = new AlignedCodonFrame();
779     acf2.addMap(seq2.getDatasetSequence(), seq1.getDatasetSequence(), map);
780     AlignedCodonFrame acf3 = new AlignedCodonFrame();
781     acf3.addMap(seq3.getDatasetSequence(), seq1.getDatasetSequence(), map);
782     AlignedCodonFrame acf4 = new AlignedCodonFrame();
783     acf4.addMap(seq3.getDatasetSequence(), seq4.getDatasetSequence(), map);
784
785     List<AlignedCodonFrame> mappings = new ArrayList<>();
786     mappings.add(acf1);
787     mappings.add(acf2);
788     mappings.add(acf3);
789     mappings.add(acf4);
790
791     /*
792      * test for null args
793      */
794     List<AlignedCodonFrame> result = MappingUtils
795             .findMappingsForSequenceAndOthers(null, mappings,
796                     Arrays.asList(new SequenceI[]
797                     { seq1, seq2 }));
798     assertTrue(result.isEmpty());
799
800     result = MappingUtils.findMappingsForSequenceAndOthers(seq1, null,
801             Arrays.asList(new SequenceI[]
802             { seq1, seq2 }));
803     assertTrue(result.isEmpty());
804
805     /*
806      * Seq1 has three mappings, but filter argument will only accept
807      * those to seq2
808      */
809     result = MappingUtils.findMappingsForSequenceAndOthers(seq1, mappings,
810             Arrays.asList(new SequenceI[]
811             { seq1, seq2, seq1.getDatasetSequence() }));
812     assertEquals(2, result.size());
813     assertTrue(result.contains(acf1));
814     assertTrue(result.contains(acf2));
815     assertFalse("Did not expect to find mapping acf3 - subselect failed",
816             result.contains(acf3));
817     assertFalse(
818             "Did not expect to find mapping acf4 - doesn't involve sequence",
819             result.contains(acf4));
820
821     /*
822      * and verify the no filter case
823      */
824     result = MappingUtils.findMappingsForSequenceAndOthers(seq1, mappings,
825             null);
826     assertEquals(3, result.size());
827     assertTrue(result.contains(acf1));
828     assertTrue(result.contains(acf2));
829     assertTrue(result.contains(acf3));
830   }
831
832   @Test(groups = { "Functional" })
833   public void testMapEditCommand()
834   {
835     SequenceI dna = new Sequence("Seq1", "---ACG---GCATCA", 8, 16);
836     SequenceI protein = new Sequence("Seq2", "-T-AS", 5, 7);
837     dna.createDatasetSequence();
838     protein.createDatasetSequence();
839     AlignedCodonFrame acf = new AlignedCodonFrame();
840     MapList map = new MapList(new int[] { 8, 16 }, new int[] { 5, 7 }, 3,
841             1);
842     acf.addMap(dna.getDatasetSequence(), protein.getDatasetSequence(), map);
843     List<AlignedCodonFrame> mappings = new ArrayList<>();
844     mappings.add(acf);
845
846     AlignmentI prot = new Alignment(new SequenceI[] { protein });
847     prot.setCodonFrames(mappings);
848     AlignmentI nuc = new Alignment(new SequenceI[] { dna });
849
850     /*
851      * construct and perform the edit command to turn "-T-AS" in to "-T-A--S"
852      * i.e. insert two gaps at column 4
853      */
854     EditCommand ec = new EditCommand();
855     final Edit edit = ec.new Edit(Action.INSERT_GAP,
856             new SequenceI[]
857             { protein }, 4, 2, '-');
858     ec.appendEdit(edit, prot, true, null);
859
860     /*
861      * the mapped edit command should be to insert 6 gaps before base 4 in the
862      * nucleotide sequence, which corresponds to aligned column 12 in the dna
863      */
864     EditCommand mappedEdit = MappingUtils.mapEditCommand(ec, false, nuc,
865             '-', mappings);
866     assertEquals(1, mappedEdit.getEdits().size());
867     Edit e = mappedEdit.getEdits().get(0);
868     assertEquals(1, e.getSequences().length);
869     assertEquals(dna, e.getSequences()[0]);
870     assertEquals(12, e.getPosition());
871     assertEquals(6, e.getNumber());
872   }
873
874   /**
875    * Tests for the method that converts a series of [start, end] ranges to
876    * single positions, where the mapping is to a reverse strand i.e. start is
877    * greater than end point mapped to
878    */
879   @Test(groups = { "Functional" })
880   public void testFlattenRanges_reverseStrand()
881   {
882     assertEquals("[4, 3, 2, 1]",
883             Arrays.toString(MappingUtils.flattenRanges(new int[]
884             { 4, 1 })));
885     assertEquals("[4, 3, 2, 1]",
886             Arrays.toString(MappingUtils.flattenRanges(new int[]
887             { 4, 3, 2, 1 })));
888     assertEquals("[4, 3, 2, 1]",
889             Arrays.toString(MappingUtils.flattenRanges(new int[]
890             { 4, 4, 3, 3, 2, 2, 1, 1 })));
891     assertEquals("[12, 9, 8, 7, 4, 3, 2, 1]",
892             Arrays.toString(MappingUtils.flattenRanges(new int[]
893             { 12, 12, 9, 7, 4, 1 })));
894     // forwards and backwards anyone?
895     assertEquals("[4, 5, 6, 3, 2, 1]",
896             Arrays.toString(MappingUtils.flattenRanges(new int[]
897             { 4, 6, 3, 1 })));
898     // backwards and forwards
899     assertEquals("[3, 2, 1, 4, 5, 6]",
900             Arrays.toString(MappingUtils.flattenRanges(new int[]
901             { 3, 1, 4, 6 })));
902     // trailing unpaired start position is ignored:
903     assertEquals("[12, 9, 8, 7, 4, 3, 2]",
904             Arrays.toString(MappingUtils.flattenRanges(new int[]
905             { 12, 12, 9, 7, 4, 2, 1 })));
906   }
907
908   /**
909    * Test mapping a column selection including hidden columns
910    * 
911    * @throws IOException
912    */
913   @Test(groups = { "Functional" })
914   public void testMapColumnSelection_hiddenColumns() throws IOException
915   {
916     setupMappedAlignments();
917
918     ColumnSelection proteinSelection = new ColumnSelection();
919     HiddenColumns hiddenCols = new HiddenColumns();
920
921     /*
922      * Column 0 in protein picks up Seq2/L, Seq3/G which map to cols 0-4 and 0-3
923      * in dna respectively, overall 0-4
924      */
925     proteinSelection.hideSelectedColumns(0, hiddenCols);
926     ColumnSelection dnaSelection = new ColumnSelection();
927     HiddenColumns dnaHidden = new HiddenColumns();
928     MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
929             proteinView, dnaView, dnaSelection, dnaHidden);
930     assertEquals("[]", dnaSelection.getSelected().toString());
931     Iterator<int[]> regions = dnaHidden.iterator();
932     assertEquals(1, dnaHidden.getNumberOfRegions());
933     assertEquals("[0, 4]", Arrays.toString(regions.next()));
934
935     /*
936      * Column 1 in protein picks up Seq1/K which maps to cols 0-3 in dna
937      */
938     dnaSelection = new ColumnSelection();
939     dnaHidden = new HiddenColumns();
940     hiddenCols.revealAllHiddenColumns(proteinSelection);
941     // the unhidden columns are now marked selected!
942     assertEquals("[0]", proteinSelection.getSelected().toString());
943     // deselect these or hideColumns will be expanded to include 0
944     proteinSelection.clear();
945     proteinSelection.hideSelectedColumns(1, hiddenCols);
946     MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
947             proteinView, dnaView, dnaSelection, dnaHidden);
948     regions = dnaHidden.iterator();
949     assertEquals(1, dnaHidden.getNumberOfRegions());
950     assertEquals("[0, 3]", Arrays.toString(regions.next()));
951
952     /*
953      * Column 2 in protein picks up gaps only - no mapping
954      */
955     dnaSelection = new ColumnSelection();
956     dnaHidden = new HiddenColumns();
957     hiddenCols.revealAllHiddenColumns(proteinSelection);
958     proteinSelection.clear();
959     proteinSelection.hideSelectedColumns(2, hiddenCols);
960     MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
961             proteinView, dnaView, dnaSelection, dnaHidden);
962     assertEquals(0, dnaHidden.getNumberOfRegions());
963
964     /*
965      * Column 3 in protein picks up Seq1/P, Seq2/Q, Seq3/S which map to columns
966      * 6-9, 6-10, 5-8 respectively, overall to 5-10
967      */
968     dnaSelection = new ColumnSelection();
969     dnaHidden = new HiddenColumns();
970     hiddenCols.revealAllHiddenColumns(proteinSelection);
971     proteinSelection.clear();
972     proteinSelection.hideSelectedColumns(3, hiddenCols); // 5-10 hidden in dna
973     proteinSelection.addElement(1); // 0-3 selected in dna
974     MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
975             proteinView, dnaView, dnaSelection, dnaHidden);
976     assertEquals("[0, 1, 2, 3]", dnaSelection.getSelected().toString());
977     regions = dnaHidden.iterator();
978     assertEquals(1, dnaHidden.getNumberOfRegions());
979     assertEquals("[5, 10]", Arrays.toString(regions.next()));
980
981     /*
982      * Combine hiding columns 1 and 3 to get discontiguous hidden columns
983      */
984     dnaSelection = new ColumnSelection();
985     dnaHidden = new HiddenColumns();
986     hiddenCols.revealAllHiddenColumns(proteinSelection);
987     proteinSelection.clear();
988     proteinSelection.hideSelectedColumns(1, hiddenCols);
989     proteinSelection.hideSelectedColumns(3, hiddenCols);
990     MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
991             proteinView, dnaView, dnaSelection, dnaHidden);
992     regions = dnaHidden.iterator();
993     assertEquals(2, dnaHidden.getNumberOfRegions());
994     assertEquals("[0, 3]", Arrays.toString(regions.next()));
995     assertEquals("[5, 10]", Arrays.toString(regions.next()));
996   }
997
998   @Test(groups = { "Functional" })
999   public void testGetLength()
1000   {
1001     assertEquals(0, MappingUtils.getLength(null));
1002
1003     /*
1004      * [start, end] ranges
1005      */
1006     List<int[]> ranges = new ArrayList<>();
1007     assertEquals(0, MappingUtils.getLength(ranges));
1008     ranges.add(new int[] { 1, 1 });
1009     assertEquals(1, MappingUtils.getLength(ranges));
1010     ranges.add(new int[] { 2, 10 });
1011     assertEquals(10, MappingUtils.getLength(ranges));
1012     ranges.add(new int[] { 20, 10 });
1013     assertEquals(21, MappingUtils.getLength(ranges));
1014
1015     /*
1016      * [start, end, start, end...] ranges
1017      */
1018     ranges.clear();
1019     ranges.add(new int[] { 1, 5, 8, 4 });
1020     ranges.add(new int[] { 8, 2 });
1021     ranges.add(new int[] { 12, 12 });
1022     assertEquals(18, MappingUtils.getLength(ranges));
1023   }
1024
1025   @Test(groups = { "Functional" })
1026   public void testContains()
1027   {
1028     assertFalse(MappingUtils.contains(null, 1));
1029     List<int[]> ranges = new ArrayList<>();
1030     assertFalse(MappingUtils.contains(ranges, 1));
1031
1032     ranges.add(new int[] { 1, 4 });
1033     ranges.add(new int[] { 6, 6 });
1034     ranges.add(new int[] { 8, 10 });
1035     ranges.add(new int[] { 30, 20 });
1036     ranges.add(new int[] { -16, -44 });
1037
1038     assertFalse(MappingUtils.contains(ranges, 0));
1039     assertTrue(MappingUtils.contains(ranges, 1));
1040     assertTrue(MappingUtils.contains(ranges, 2));
1041     assertTrue(MappingUtils.contains(ranges, 3));
1042     assertTrue(MappingUtils.contains(ranges, 4));
1043     assertFalse(MappingUtils.contains(ranges, 5));
1044
1045     assertTrue(MappingUtils.contains(ranges, 6));
1046     assertFalse(MappingUtils.contains(ranges, 7));
1047
1048     assertTrue(MappingUtils.contains(ranges, 8));
1049     assertTrue(MappingUtils.contains(ranges, 9));
1050     assertTrue(MappingUtils.contains(ranges, 10));
1051
1052     assertFalse(MappingUtils.contains(ranges, 31));
1053     assertTrue(MappingUtils.contains(ranges, 30));
1054     assertTrue(MappingUtils.contains(ranges, 29));
1055     assertTrue(MappingUtils.contains(ranges, 20));
1056     assertFalse(MappingUtils.contains(ranges, 19));
1057
1058     assertFalse(MappingUtils.contains(ranges, -15));
1059     assertTrue(MappingUtils.contains(ranges, -16));
1060     assertTrue(MappingUtils.contains(ranges, -44));
1061     assertFalse(MappingUtils.contains(ranges, -45));
1062   }
1063
1064   /**
1065    * Test the method that drops positions from the start of a mapped range
1066    */
1067   @Test(groups = "Functional")
1068   public void testRemoveStartPositions()
1069   {
1070     int[] ranges = new int[] { 1, 10 };
1071     int[] adjusted = MappingUtils.removeStartPositions(0, ranges);
1072     assertEquals("[1, 10]", Arrays.toString(adjusted));
1073
1074     adjusted = MappingUtils.removeStartPositions(1, ranges);
1075     assertEquals("[2, 10]", Arrays.toString(adjusted));
1076     assertEquals("[1, 10]", Arrays.toString(ranges));
1077
1078     ranges = adjusted;
1079     adjusted = MappingUtils.removeStartPositions(1, ranges);
1080     assertEquals("[3, 10]", Arrays.toString(adjusted));
1081     assertEquals("[2, 10]", Arrays.toString(ranges));
1082
1083     ranges = new int[] { 2, 3, 10, 12 };
1084     adjusted = MappingUtils.removeStartPositions(1, ranges);
1085     assertEquals("[3, 3, 10, 12]", Arrays.toString(adjusted));
1086     assertEquals("[2, 3, 10, 12]", Arrays.toString(ranges));
1087
1088     ranges = new int[] { 2, 2, 8, 12 };
1089     adjusted = MappingUtils.removeStartPositions(1, ranges);
1090     assertEquals("[8, 12]", Arrays.toString(adjusted));
1091     assertEquals("[2, 2, 8, 12]", Arrays.toString(ranges));
1092
1093     ranges = new int[] { 2, 2, 8, 12 };
1094     adjusted = MappingUtils.removeStartPositions(2, ranges);
1095     assertEquals("[9, 12]", Arrays.toString(adjusted));
1096     assertEquals("[2, 2, 8, 12]", Arrays.toString(ranges));
1097
1098     ranges = new int[] { 2, 2, 4, 4, 9, 12 };
1099     adjusted = MappingUtils.removeStartPositions(1, ranges);
1100     assertEquals("[4, 4, 9, 12]", Arrays.toString(adjusted));
1101     assertEquals("[2, 2, 4, 4, 9, 12]", Arrays.toString(ranges));
1102
1103     ranges = new int[] { 2, 2, 4, 4, 9, 12 };
1104     adjusted = MappingUtils.removeStartPositions(2, ranges);
1105     assertEquals("[9, 12]", Arrays.toString(adjusted));
1106     assertEquals("[2, 2, 4, 4, 9, 12]", Arrays.toString(ranges));
1107
1108     ranges = new int[] { 2, 3, 9, 12 };
1109     adjusted = MappingUtils.removeStartPositions(3, ranges);
1110     assertEquals("[10, 12]", Arrays.toString(adjusted));
1111     assertEquals("[2, 3, 9, 12]", Arrays.toString(ranges));
1112   }
1113
1114   /**
1115    * Test the method that drops positions from the start of a mapped range, on
1116    * the reverse strand
1117    */
1118   @Test(groups = "Functional")
1119   public void testRemoveStartPositions_reverseStrand()
1120   {
1121     int[] ranges = new int[] { 10, 1 };
1122     int[] adjusted = MappingUtils.removeStartPositions(0, ranges);
1123     assertEquals("[10, 1]", Arrays.toString(adjusted));
1124     assertEquals("[10, 1]", Arrays.toString(ranges));
1125
1126     ranges = adjusted;
1127     adjusted = MappingUtils.removeStartPositions(1, ranges);
1128     assertEquals("[9, 1]", Arrays.toString(adjusted));
1129     assertEquals("[10, 1]", Arrays.toString(ranges));
1130
1131     ranges = adjusted;
1132     adjusted = MappingUtils.removeStartPositions(1, ranges);
1133     assertEquals("[8, 1]", Arrays.toString(adjusted));
1134     assertEquals("[9, 1]", Arrays.toString(ranges));
1135
1136     ranges = new int[] { 12, 11, 9, 6 };
1137     adjusted = MappingUtils.removeStartPositions(1, ranges);
1138     assertEquals("[11, 11, 9, 6]", Arrays.toString(adjusted));
1139     assertEquals("[12, 11, 9, 6]", Arrays.toString(ranges));
1140
1141     ranges = new int[] { 12, 12, 8, 4 };
1142     adjusted = MappingUtils.removeStartPositions(1, ranges);
1143     assertEquals("[8, 4]", Arrays.toString(adjusted));
1144     assertEquals("[12, 12, 8, 4]", Arrays.toString(ranges));
1145
1146     ranges = new int[] { 12, 12, 8, 4 };
1147     adjusted = MappingUtils.removeStartPositions(2, ranges);
1148     assertEquals("[7, 4]", Arrays.toString(adjusted));
1149     assertEquals("[12, 12, 8, 4]", Arrays.toString(ranges));
1150
1151     ranges = new int[] { 12, 12, 10, 10, 8, 4 };
1152     adjusted = MappingUtils.removeStartPositions(1, ranges);
1153     assertEquals("[10, 10, 8, 4]", Arrays.toString(adjusted));
1154     assertEquals("[12, 12, 10, 10, 8, 4]", Arrays.toString(ranges));
1155
1156     ranges = new int[] { 12, 12, 10, 10, 8, 4 };
1157     adjusted = MappingUtils.removeStartPositions(2, ranges);
1158     assertEquals("[8, 4]", Arrays.toString(adjusted));
1159     assertEquals("[12, 12, 10, 10, 8, 4]", Arrays.toString(ranges));
1160
1161     ranges = new int[] { 12, 11, 8, 4 };
1162     adjusted = MappingUtils.removeStartPositions(3, ranges);
1163     assertEquals("[7, 4]", Arrays.toString(adjusted));
1164     assertEquals("[12, 11, 8, 4]", Arrays.toString(ranges));
1165   }
1166
1167   @Test(groups = { "Functional" })
1168   public void testRangeContains()
1169   {
1170     /*
1171      * both forward ranges
1172      */
1173     assertTrue(
1174             MappingUtils.rangeContains(new int[]
1175             { 1, 10 }, new int[] { 1, 10 }));
1176     assertTrue(
1177             MappingUtils.rangeContains(new int[]
1178             { 1, 10 }, new int[] { 2, 10 }));
1179     assertTrue(
1180             MappingUtils.rangeContains(new int[]
1181             { 1, 10 }, new int[] { 1, 9 }));
1182     assertTrue(
1183             MappingUtils.rangeContains(new int[]
1184             { 1, 10 }, new int[] { 4, 5 }));
1185     assertFalse(
1186             MappingUtils.rangeContains(new int[]
1187             { 1, 10 }, new int[] { 0, 9 }));
1188     assertFalse(
1189             MappingUtils.rangeContains(new int[]
1190             { 1, 10 }, new int[] { -10, -9 }));
1191     assertFalse(
1192             MappingUtils.rangeContains(new int[]
1193             { 1, 10 }, new int[] { 1, 11 }));
1194     assertFalse(
1195             MappingUtils.rangeContains(new int[]
1196             { 1, 10 }, new int[] { 11, 12 }));
1197
1198     /*
1199      * forward range, reverse query
1200      */
1201     assertTrue(
1202             MappingUtils.rangeContains(new int[]
1203             { 1, 10 }, new int[] { 10, 1 }));
1204     assertTrue(
1205             MappingUtils.rangeContains(new int[]
1206             { 1, 10 }, new int[] { 9, 1 }));
1207     assertTrue(
1208             MappingUtils.rangeContains(new int[]
1209             { 1, 10 }, new int[] { 10, 2 }));
1210     assertTrue(
1211             MappingUtils.rangeContains(new int[]
1212             { 1, 10 }, new int[] { 5, 5 }));
1213     assertFalse(
1214             MappingUtils.rangeContains(new int[]
1215             { 1, 10 }, new int[] { 11, 1 }));
1216     assertFalse(
1217             MappingUtils.rangeContains(new int[]
1218             { 1, 10 }, new int[] { 10, 0 }));
1219
1220     /*
1221      * reverse range, forward query
1222      */
1223     assertTrue(
1224             MappingUtils.rangeContains(new int[]
1225             { 10, 1 }, new int[] { 1, 10 }));
1226     assertTrue(
1227             MappingUtils.rangeContains(new int[]
1228             { 10, 1 }, new int[] { 1, 9 }));
1229     assertTrue(
1230             MappingUtils.rangeContains(new int[]
1231             { 10, 1 }, new int[] { 2, 10 }));
1232     assertTrue(
1233             MappingUtils.rangeContains(new int[]
1234             { 10, 1 }, new int[] { 6, 6 }));
1235     assertFalse(
1236             MappingUtils.rangeContains(new int[]
1237             { 10, 1 }, new int[] { 6, 11 }));
1238     assertFalse(
1239             MappingUtils.rangeContains(new int[]
1240             { 10, 1 }, new int[] { 11, 20 }));
1241     assertFalse(
1242             MappingUtils.rangeContains(new int[]
1243             { 10, 1 }, new int[] { -3, -2 }));
1244
1245     /*
1246      * both reverse
1247      */
1248     assertTrue(
1249             MappingUtils.rangeContains(new int[]
1250             { 10, 1 }, new int[] { 10, 1 }));
1251     assertTrue(
1252             MappingUtils.rangeContains(new int[]
1253             { 10, 1 }, new int[] { 9, 1 }));
1254     assertTrue(
1255             MappingUtils.rangeContains(new int[]
1256             { 10, 1 }, new int[] { 10, 2 }));
1257     assertTrue(
1258             MappingUtils.rangeContains(new int[]
1259             { 10, 1 }, new int[] { 3, 3 }));
1260     assertFalse(
1261             MappingUtils.rangeContains(new int[]
1262             { 10, 1 }, new int[] { 11, 1 }));
1263     assertFalse(
1264             MappingUtils.rangeContains(new int[]
1265             { 10, 1 }, new int[] { 10, 0 }));
1266     assertFalse(
1267             MappingUtils.rangeContains(new int[]
1268             { 10, 1 }, new int[] { 12, 11 }));
1269     assertFalse(
1270             MappingUtils.rangeContains(new int[]
1271             { 10, 1 }, new int[] { -5, -8 }));
1272
1273     /*
1274      * bad arguments
1275      */
1276     assertFalse(
1277             MappingUtils.rangeContains(new int[]
1278             { 1, 10, 12 }, new int[] { 1, 10 }));
1279     assertFalse(
1280             MappingUtils.rangeContains(new int[]
1281             { 1, 10 }, new int[] { 1 }));
1282     assertFalse(MappingUtils.rangeContains(new int[] { 1, 10 }, null));
1283     assertFalse(MappingUtils.rangeContains(null, new int[] { 1, 10 }));
1284   }
1285
1286   @Test(groups = "Functional")
1287   public void testRemoveEndPositions()
1288   {
1289     List<int[]> ranges = new ArrayList<>();
1290
1291     /*
1292      * case 1: truncate last range
1293      */
1294     ranges.add(new int[] { 1, 10 });
1295     ranges.add(new int[] { 20, 30 });
1296     MappingUtils.removeEndPositions(5, ranges);
1297     assertEquals(2, ranges.size());
1298     assertEquals(25, ranges.get(1)[1]);
1299
1300     /*
1301      * case 2: remove last range
1302      */
1303     ranges.clear();
1304     ranges.add(new int[] { 1, 10 });
1305     ranges.add(new int[] { 20, 22 });
1306     MappingUtils.removeEndPositions(3, ranges);
1307     assertEquals(1, ranges.size());
1308     assertEquals(10, ranges.get(0)[1]);
1309
1310     /*
1311      * case 3: truncate penultimate range
1312      */
1313     ranges.clear();
1314     ranges.add(new int[] { 1, 10 });
1315     ranges.add(new int[] { 20, 21 });
1316     MappingUtils.removeEndPositions(3, ranges);
1317     assertEquals(1, ranges.size());
1318     assertEquals(9, ranges.get(0)[1]);
1319
1320     /*
1321      * case 4: remove last two ranges
1322      */
1323     ranges.clear();
1324     ranges.add(new int[] { 1, 10 });
1325     ranges.add(new int[] { 20, 20 });
1326     ranges.add(new int[] { 30, 30 });
1327     MappingUtils.removeEndPositions(3, ranges);
1328     assertEquals(1, ranges.size());
1329     assertEquals(9, ranges.get(0)[1]);
1330   }
1331
1332   /**
1333    * Test mapping a sequence group where sequences in and outside the group
1334    * share a dataset sequence (e.g. alternative CDS for the same gene)
1335    * <p>
1336    * This scenario doesn't arise after JAL-3763 changes, but test left as still valid
1337    * @throws IOException
1338    */
1339   @Test(groups = { "Functional" })
1340   public void testMapSequenceGroup_sharedDataset() throws IOException
1341   {
1342     /*
1343      * Set up dna and protein Seq1/2/3 with mappings (held on the protein
1344      * viewport). CDS sequences share the same 'gene' dataset sequence.
1345      */
1346     SequenceI dna = new Sequence("dna", "aaatttgggcccaaatttgggccc");
1347     SequenceI cds1 = new Sequence("cds1/1-6", "aaattt");
1348     SequenceI cds2 = new Sequence("cds1/4-9", "tttggg");
1349     SequenceI cds3 = new Sequence("cds1/19-24", "gggccc");
1350
1351     cds1.setDatasetSequence(dna);
1352     cds2.setDatasetSequence(dna);
1353     cds3.setDatasetSequence(dna);
1354
1355     SequenceI pep1 = new Sequence("pep1", "KF");
1356     SequenceI pep2 = new Sequence("pep2", "FG");
1357     SequenceI pep3 = new Sequence("pep3", "GP");
1358     pep1.createDatasetSequence();
1359     pep2.createDatasetSequence();
1360     pep3.createDatasetSequence();
1361
1362     /*
1363      * add mappings from coding positions of dna to respective peptides
1364      */
1365     AlignedCodonFrame acf = new AlignedCodonFrame();
1366     acf.addMap(dna, pep1,
1367             new MapList(new int[]
1368             { 1, 6 }, new int[] { 1, 2 }, 3, 1));
1369     acf.addMap(dna, pep2,
1370             new MapList(new int[]
1371             { 4, 9 }, new int[] { 1, 2 }, 3, 1));
1372     acf.addMap(dna, pep3,
1373             new MapList(new int[]
1374             { 19, 24 }, new int[] { 1, 2 }, 3, 1));
1375
1376     List<AlignedCodonFrame> acfList = Arrays
1377             .asList(new AlignedCodonFrame[]
1378             { acf });
1379
1380     AlignmentI cdna = new Alignment(new SequenceI[] { cds1, cds2, cds3 });
1381     AlignmentI protein = new Alignment(
1382             new SequenceI[]
1383             { pep1, pep2, pep3 });
1384     AlignViewportI cdnaView = new AlignViewport(cdna);
1385     AlignViewportI peptideView = new AlignViewport(protein);
1386     protein.setCodonFrames(acfList);
1387
1388     /*
1389      * Select pep1 and pep3 in the protein alignment
1390      */
1391     SequenceGroup sg = new SequenceGroup();
1392     sg.setColourText(true);
1393     sg.setIdColour(Color.GREEN);
1394     sg.setOutlineColour(Color.LIGHT_GRAY);
1395     sg.addSequence(pep1, false);
1396     sg.addSequence(pep3, false);
1397     sg.setEndRes(protein.getWidth() - 1);
1398
1399     /*
1400      * Verify the mapped sequence group in dna is cds1 and cds3
1401      */
1402     SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
1403             peptideView, cdnaView);
1404     assertTrue(mappedGroup.getColourText());
1405     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
1406     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
1407     assertEquals(2, mappedGroup.getSequences().size());
1408     assertSame(cds1, mappedGroup.getSequences().get(0));
1409     assertSame(cds3, mappedGroup.getSequences().get(1));
1410     // columns 1-6 selected (0-5 base zero)
1411     assertEquals(0, mappedGroup.getStartRes());
1412     assertEquals(5, mappedGroup.getEndRes());
1413
1414     /*
1415      * Select mapping sequence group from dna to protein
1416      */
1417     sg.clear();
1418     sg.addSequence(cds2, false);
1419     sg.addSequence(cds1, false);
1420     sg.setStartRes(0);
1421     sg.setEndRes(cdna.getWidth() - 1);
1422     mappedGroup = MappingUtils.mapSequenceGroup(sg, cdnaView, peptideView);
1423     assertTrue(mappedGroup.getColourText());
1424     assertSame(sg.getIdColour(), mappedGroup.getIdColour());
1425     assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
1426     assertEquals(2, mappedGroup.getSequences().size());
1427     assertSame(protein.getSequenceAt(1), mappedGroup.getSequences().get(0));
1428     assertSame(protein.getSequenceAt(0), mappedGroup.getSequences().get(1));
1429     assertEquals(0, mappedGroup.getStartRes());
1430     assertEquals(1, mappedGroup.getEndRes()); // two columns
1431   }
1432 }