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