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