2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
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;
28 import jalview.api.AlignViewportI;
29 import jalview.commands.EditCommand;
30 import jalview.commands.EditCommand.Action;
31 import jalview.commands.EditCommand.Edit;
32 import jalview.datamodel.AlignedCodonFrame;
33 import jalview.datamodel.Alignment;
34 import jalview.datamodel.AlignmentI;
35 import jalview.datamodel.ColumnSelection;
36 import jalview.datamodel.HiddenColumns;
37 import jalview.datamodel.SearchResultMatchI;
38 import jalview.datamodel.SearchResultsI;
39 import jalview.datamodel.Sequence;
40 import jalview.datamodel.SequenceGroup;
41 import jalview.datamodel.SequenceI;
42 import jalview.gui.AlignViewport;
43 import jalview.gui.JvOptionPane;
44 import jalview.io.DataSourceType;
45 import jalview.io.FileFormat;
46 import jalview.io.FileFormatI;
47 import jalview.io.FormatAdapter;
49 import java.awt.Color;
50 import java.io.IOException;
51 import java.util.ArrayList;
52 import java.util.Arrays;
53 import java.util.Iterator;
54 import java.util.List;
56 import org.testng.annotations.BeforeClass;
57 import org.testng.annotations.Test;
59 public class MappingUtilsTest
62 @BeforeClass(alwaysRun = true)
63 public void setUpJvOptionPane()
65 JvOptionPane.setInteractiveMode(false);
66 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
69 private AlignViewportI dnaView;
71 private AlignViewportI proteinView;
74 * Simple test of mapping with no intron involved.
76 @Test(groups = { "Functional" })
77 public void testBuildSearchResults()
79 final Sequence seq1 = new Sequence("Seq1/5-10", "C-G-TA-GC");
80 seq1.createDatasetSequence();
82 final Sequence aseq1 = new Sequence("Seq1/12-13", "-P-R");
83 aseq1.createDatasetSequence();
86 * Map dna bases 5-10 to protein residues 12-13
88 AlignedCodonFrame acf = new AlignedCodonFrame();
89 MapList map = new MapList(new int[] { 5, 10 }, new int[] { 12, 13 }, 3,
91 acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
92 List<AlignedCodonFrame> acfList = Arrays.asList(new AlignedCodonFrame[]
96 * Check protein residue 12 maps to codon 5-7, 13 to codon 8-10
98 SearchResultsI sr = MappingUtils.buildSearchResults(aseq1, 12, acfList);
99 assertEquals(1, sr.getResults().size());
100 SearchResultMatchI m = sr.getResults().get(0);
101 assertEquals(seq1.getDatasetSequence(), m.getSequence());
102 assertEquals(5, m.getStart());
103 assertEquals(7, m.getEnd());
104 sr = MappingUtils.buildSearchResults(aseq1, 13, acfList);
105 assertEquals(1, sr.getResults().size());
106 m = sr.getResults().get(0);
107 assertEquals(seq1.getDatasetSequence(), m.getSequence());
108 assertEquals(8, m.getStart());
109 assertEquals(10, m.getEnd());
112 * Check inverse mappings, from codons 5-7, 8-10 to protein 12, 13
114 for (int i = 5; i < 11; i++)
116 sr = MappingUtils.buildSearchResults(seq1, i, acfList);
117 assertEquals(1, sr.getResults().size());
118 m = sr.getResults().get(0);
119 assertEquals(aseq1.getDatasetSequence(), m.getSequence());
120 int residue = i > 7 ? 13 : 12;
121 assertEquals(residue, m.getStart());
122 assertEquals(residue, m.getEnd());
127 * Simple test of mapping with introns involved.
129 @Test(groups = { "Functional" })
130 public void testBuildSearchResults_withIntron()
132 final Sequence seq1 = new Sequence("Seq1/5-17", "c-G-tAGa-GcAgCtt");
133 seq1.createDatasetSequence();
135 final Sequence aseq1 = new Sequence("Seq1/8-9", "-E-D");
136 aseq1.createDatasetSequence();
139 * Map dna bases [6, 8, 9], [11, 13, 115] to protein residues 8 and 9
141 AlignedCodonFrame acf = new AlignedCodonFrame();
142 MapList map = new MapList(new int[] { 6, 6, 8, 9, 11, 11, 13, 13, 15,
143 15 }, new int[] { 8, 9 }, 3, 1);
144 acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
145 List<AlignedCodonFrame> acfList = Arrays.asList(new AlignedCodonFrame[]
149 * Check protein residue 8 maps to [6, 8, 9]
151 SearchResultsI sr = MappingUtils.buildSearchResults(aseq1, 8, acfList);
152 assertEquals(2, sr.getResults().size());
153 SearchResultMatchI m = sr.getResults().get(0);
154 assertEquals(seq1.getDatasetSequence(), m.getSequence());
155 assertEquals(6, m.getStart());
156 assertEquals(6, m.getEnd());
157 m = sr.getResults().get(1);
158 assertEquals(seq1.getDatasetSequence(), m.getSequence());
159 assertEquals(8, m.getStart());
160 assertEquals(9, m.getEnd());
163 * Check protein residue 9 maps to [11, 13, 15]
165 sr = MappingUtils.buildSearchResults(aseq1, 9, acfList);
166 assertEquals(3, sr.getResults().size());
167 m = sr.getResults().get(0);
168 assertEquals(seq1.getDatasetSequence(), m.getSequence());
169 assertEquals(11, m.getStart());
170 assertEquals(11, m.getEnd());
171 m = sr.getResults().get(1);
172 assertEquals(seq1.getDatasetSequence(), m.getSequence());
173 assertEquals(13, m.getStart());
174 assertEquals(13, m.getEnd());
175 m = sr.getResults().get(2);
176 assertEquals(seq1.getDatasetSequence(), m.getSequence());
177 assertEquals(15, m.getStart());
178 assertEquals(15, m.getEnd());
181 * Check inverse mappings, from codons to protein
183 for (int i = 5; i < 18; i++)
185 sr = MappingUtils.buildSearchResults(seq1, i, acfList);
186 int residue = (i == 6 || i == 8 || i == 9) ? 8 : (i == 11 || i == 13
190 assertEquals(0, sr.getResults().size());
193 assertEquals(1, sr.getResults().size());
194 m = sr.getResults().get(0);
195 assertEquals(aseq1.getDatasetSequence(), m.getSequence());
196 assertEquals(residue, m.getStart());
197 assertEquals(residue, m.getEnd());
202 * Test mapping a sequence group made of entire sequences.
204 * @throws IOException
206 @Test(groups = { "Functional" })
207 public void testMapSequenceGroup_sequences() throws IOException
210 * Set up dna and protein Seq1/2/3 with mappings (held on the protein
213 AlignmentI cdna = loadAlignment(">Seq1\nACG\n>Seq2\nTGA\n>Seq3\nTAC\n",
215 cdna.setDataset(null);
216 AlignmentI protein = loadAlignment(">Seq1\nK\n>Seq2\nL\n>Seq3\nQ\n",
218 protein.setDataset(null);
219 AlignedCodonFrame acf = new AlignedCodonFrame();
220 MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 1 }, 3, 1);
221 for (int seq = 0; seq < 3; seq++)
223 acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), protein
224 .getSequenceAt(seq).getDatasetSequence(), map);
226 List<AlignedCodonFrame> acfList = Arrays.asList(new AlignedCodonFrame[]
229 AlignViewportI dnaView = new AlignViewport(cdna);
230 AlignViewportI proteinView = new AlignViewport(protein);
231 protein.setCodonFrames(acfList);
234 * Select Seq1 and Seq3 in the protein (startRes=endRes=0)
236 SequenceGroup sg = new SequenceGroup();
237 sg.setColourText(true);
238 sg.setIdColour(Color.GREEN);
239 sg.setOutlineColour(Color.LIGHT_GRAY);
240 sg.addSequence(protein.getSequenceAt(0), false);
241 sg.addSequence(protein.getSequenceAt(2), false);
244 * Verify the mapped sequence group in dna
246 SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
247 proteinView, dnaView);
248 assertTrue(mappedGroup.getColourText());
249 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
250 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
251 assertEquals(2, mappedGroup.getSequences().size());
252 assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
253 assertSame(cdna.getSequenceAt(2), mappedGroup.getSequences().get(1));
254 assertEquals(0, mappedGroup.getStartRes());
255 assertEquals(2, mappedGroup.getEndRes());
258 * Verify mapping sequence group from dna to protein
261 sg.addSequence(cdna.getSequenceAt(1), false);
262 sg.addSequence(cdna.getSequenceAt(0), false);
265 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
266 assertTrue(mappedGroup.getColourText());
267 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
268 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
269 assertEquals(2, mappedGroup.getSequences().size());
270 assertSame(protein.getSequenceAt(1), mappedGroup.getSequences().get(0));
271 assertSame(protein.getSequenceAt(0), mappedGroup.getSequences().get(1));
272 assertEquals(0, mappedGroup.getStartRes());
273 assertEquals(0, mappedGroup.getEndRes());
277 * Helper method to load an alignment and ensure dataset sequences are set up.
283 * @throws IOException
285 protected AlignmentI loadAlignment(final String data, FileFormatI format)
288 AlignmentI a = new FormatAdapter().readFile(data,
289 DataSourceType.PASTE, format);
295 * Test mapping a column selection in protein to its dna equivalent
297 * @throws IOException
299 @Test(groups = { "Functional" })
300 public void testMapColumnSelection_proteinToDna() throws IOException
302 setupMappedAlignments();
304 ColumnSelection colsel = new ColumnSelection();
305 HiddenColumns hidden = new HiddenColumns();
308 * Column 0 in protein picks up Seq2/L, Seq3/G which map to cols 0-4 and 0-3
309 * in dna respectively, overall 0-4
311 colsel.addElement(0);
312 ColumnSelection cs = new ColumnSelection();
313 HiddenColumns hs = new HiddenColumns();
314 MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
316 assertEquals("[0, 1, 2, 3, 4]", cs.getSelected().toString());
319 * Column 1 in protein picks up Seq1/K which maps to cols 0-3 in dna
323 colsel.addElement(1);
324 MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
326 assertEquals("[0, 1, 2, 3]", cs.getSelected().toString());
329 * Column 2 in protein picks up gaps only - no mapping
333 colsel.addElement(2);
334 MappingUtils.mapColumnSelection(colsel, hidden, proteinView,
336 assertEquals("[]", cs.getSelected().toString());
339 * Column 3 in protein picks up Seq1/P, Seq2/Q, Seq3/S which map to columns
340 * 6-9, 6-10, 5-8 respectively, overall to 5-10
344 colsel.addElement(3);
345 MappingUtils.mapColumnSelection(colsel, hidden, proteinView,
347 assertEquals("[5, 6, 7, 8, 9, 10]", cs.getSelected().toString());
350 * Combine selection of columns 1 and 3 to get a discontiguous mapped
355 colsel.addElement(1);
356 colsel.addElement(3);
357 MappingUtils.mapColumnSelection(colsel, hidden, proteinView,
359 assertEquals("[0, 1, 2, 3, 5, 6, 7, 8, 9, 10]", cs.getSelected()
364 * Set up mappings for tests from 3 dna to 3 protein sequences. Sequences have
365 * offset start positions for a more general test case.
367 * @throws IOException
369 protected void setupMappedAlignments() throws IOException
372 * Map (upper-case = coding):
373 * Seq1/10-18 AC-GctGtC-T to Seq1/40 -K-P
374 * Seq2/20-27 Tc-GA-G-T-T to Seq2/20-27 L--Q
375 * Seq3/30-38 TtTT-AaCGg- to Seq3/60-61\nG--S
377 AlignmentI cdna = loadAlignment(">Seq1/10-18\nAC-GctGtC-T\n"
378 + ">Seq2/20-27\nTc-GA-G-T-Tc\n" + ">Seq3/30-38\nTtTT-AaCGg-\n",
380 cdna.setDataset(null);
381 AlignmentI protein = loadAlignment(
382 ">Seq1/40-41\n-K-P\n>Seq2/50-51\nL--Q\n>Seq3/60-61\nG--S\n",
384 protein.setDataset(null);
386 // map first dna to first protein seq
387 AlignedCodonFrame acf = new AlignedCodonFrame();
388 MapList map = new MapList(new int[] { 10, 12, 15, 15, 17, 18 },
389 new int[] { 40, 41 }, 3, 1);
390 acf.addMap(cdna.getSequenceAt(0).getDatasetSequence(), protein
391 .getSequenceAt(0).getDatasetSequence(), map);
393 // map second dna to second protein seq
394 map = new MapList(new int[] { 20, 20, 22, 23, 24, 26 }, new int[] { 50,
396 acf.addMap(cdna.getSequenceAt(1).getDatasetSequence(), protein
397 .getSequenceAt(1).getDatasetSequence(), map);
399 // map third dna to third protein seq
400 map = new MapList(new int[] { 30, 30, 32, 34, 36, 37 }, new int[] { 60,
402 acf.addMap(cdna.getSequenceAt(2).getDatasetSequence(), protein
403 .getSequenceAt(2).getDatasetSequence(), map);
404 List<AlignedCodonFrame> acfList = Arrays.asList(new AlignedCodonFrame[]
407 dnaView = new AlignViewport(cdna);
408 proteinView = new AlignViewport(protein);
409 protein.setCodonFrames(acfList);
413 * Test mapping a column selection in dna to its protein equivalent
415 * @throws IOException
417 @Test(groups = { "Functional" })
418 public void testMapColumnSelection_dnaToProtein() throws IOException
420 setupMappedAlignments();
422 ColumnSelection colsel = new ColumnSelection();
423 HiddenColumns hidden = new HiddenColumns();
426 * Column 0 in dna picks up first bases which map to residue 1, columns 0-1
429 ColumnSelection cs = new ColumnSelection();
430 HiddenColumns hs = new HiddenColumns();
431 colsel.addElement(0);
432 MappingUtils.mapColumnSelection(colsel, hidden, dnaView, proteinView,
434 assertEquals("[0, 1]", cs.getSelected().toString());
437 * Columns 3-5 in dna map to the first residues in protein Seq1, Seq2, and
438 * the first two in Seq3. Overall to columns 0, 1, 3 (col2 is all gaps).
440 colsel.addElement(3);
441 colsel.addElement(4);
442 colsel.addElement(5);
444 MappingUtils.mapColumnSelection(colsel, hidden, dnaView, proteinView,
446 assertEquals("[0, 1, 3]", cs.getSelected().toString());
449 @Test(groups = { "Functional" })
450 public void testMapColumnSelection_null() throws IOException
452 setupMappedAlignments();
453 ColumnSelection cs = new ColumnSelection();
454 HiddenColumns hs = new HiddenColumns();
455 MappingUtils.mapColumnSelection(null, null, dnaView, proteinView, cs,
457 assertTrue("mapped selection not empty", cs.getSelected().isEmpty());
461 * Tests for the method that converts a series of [start, end] ranges to
464 @Test(groups = { "Functional" })
465 public void testFlattenRanges()
467 assertEquals("[1, 2, 3, 4]",
468 Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 4 })));
471 Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 2, 3,
475 Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 1, 2,
478 "[1, 2, 3, 4, 7, 8, 9, 12]",
479 Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 4, 7,
481 // trailing unpaired start position is ignored:
483 "[1, 2, 3, 4, 7, 8, 9, 12]",
484 Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 4, 7,
489 * Test mapping a sequence group made of entire columns.
491 * @throws IOException
493 @Test(groups = { "Functional" })
494 public void testMapSequenceGroup_columns() throws IOException
497 * Set up dna and protein Seq1/2/3 with mappings (held on the protein
500 AlignmentI cdna = loadAlignment(
501 ">Seq1\nACGGCA\n>Seq2\nTGACAG\n>Seq3\nTACGTA\n",
503 cdna.setDataset(null);
504 AlignmentI protein = loadAlignment(">Seq1\nKA\n>Seq2\nLQ\n>Seq3\nQV\n",
506 protein.setDataset(null);
507 AlignedCodonFrame acf = new AlignedCodonFrame();
508 MapList map = new MapList(new int[] { 1, 6 }, new int[] { 1, 2 }, 3, 1);
509 for (int seq = 0; seq < 3; seq++)
511 acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), protein
512 .getSequenceAt(seq).getDatasetSequence(), map);
514 List<AlignedCodonFrame> acfList = Arrays.asList(new AlignedCodonFrame[]
517 AlignViewportI dnaView = new AlignViewport(cdna);
518 AlignViewportI proteinView = new AlignViewport(protein);
519 protein.setCodonFrames(acfList);
522 * Select all sequences, column 2 in the protein
524 SequenceGroup sg = new SequenceGroup();
525 sg.setColourText(true);
526 sg.setIdColour(Color.GREEN);
527 sg.setOutlineColour(Color.LIGHT_GRAY);
528 sg.addSequence(protein.getSequenceAt(0), false);
529 sg.addSequence(protein.getSequenceAt(1), false);
530 sg.addSequence(protein.getSequenceAt(2), false);
535 * Verify the mapped sequence group in dna
537 SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
538 proteinView, dnaView);
539 assertTrue(mappedGroup.getColourText());
540 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
541 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
542 assertEquals(3, mappedGroup.getSequences().size());
543 assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
544 assertSame(cdna.getSequenceAt(1), mappedGroup.getSequences().get(1));
545 assertSame(cdna.getSequenceAt(2), mappedGroup.getSequences().get(2));
546 assertEquals(3, mappedGroup.getStartRes());
547 assertEquals(5, mappedGroup.getEndRes());
550 * Verify mapping sequence group from dna to protein
553 sg.addSequence(cdna.getSequenceAt(0), false);
554 sg.addSequence(cdna.getSequenceAt(1), false);
555 sg.addSequence(cdna.getSequenceAt(2), false);
556 // select columns 2 and 3 in DNA which span protein columns 0 and 1
559 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
560 assertTrue(mappedGroup.getColourText());
561 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
562 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
563 assertEquals(3, mappedGroup.getSequences().size());
564 assertSame(protein.getSequenceAt(0), mappedGroup.getSequences().get(0));
565 assertSame(protein.getSequenceAt(1), mappedGroup.getSequences().get(1));
566 assertSame(protein.getSequenceAt(2), mappedGroup.getSequences().get(2));
567 assertEquals(0, mappedGroup.getStartRes());
568 assertEquals(1, mappedGroup.getEndRes());
572 * Test mapping a sequence group made of a sequences/columns region.
574 * @throws IOException
576 @Test(groups = { "Functional" })
577 public void testMapSequenceGroup_region() throws IOException
580 * Set up gapped dna and protein Seq1/2/3 with mappings (held on the protein
583 AlignmentI cdna = loadAlignment(
584 ">Seq1\nA-CG-GC--AT-CA\n>Seq2\n-TG-AC-AG-T-AT\n>Seq3\n-T--ACG-TAAT-G\n",
586 cdna.setDataset(null);
587 AlignmentI protein = loadAlignment(
588 ">Seq1\n-KA-S\n>Seq2\n--L-QY\n>Seq3\nQ-V-M\n", FileFormat.Fasta);
589 protein.setDataset(null);
590 AlignedCodonFrame acf = new AlignedCodonFrame();
591 MapList map = new MapList(new int[] { 1, 9 }, new int[] { 1, 3 }, 3, 1);
592 for (int seq = 0; seq < 3; seq++)
594 acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), protein
595 .getSequenceAt(seq).getDatasetSequence(), map);
597 List<AlignedCodonFrame> acfList = Arrays.asList(new AlignedCodonFrame[]
600 AlignViewportI dnaView = new AlignViewport(cdna);
601 AlignViewportI proteinView = new AlignViewport(protein);
602 protein.setCodonFrames(acfList);
605 * Select Seq1 and Seq2 in the protein, column 1 (K/-). Expect mapped
606 * sequence group to cover Seq1, columns 0-3 (ACG). Because the selection
607 * only includes a gap in Seq2 there is no mappable selection region in the
610 SequenceGroup sg = new SequenceGroup();
611 sg.setColourText(true);
612 sg.setIdColour(Color.GREEN);
613 sg.setOutlineColour(Color.LIGHT_GRAY);
614 sg.addSequence(protein.getSequenceAt(0), false);
615 sg.addSequence(protein.getSequenceAt(1), false);
620 * Verify the mapped sequence group in dna
622 SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
623 proteinView, dnaView);
624 assertTrue(mappedGroup.getColourText());
625 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
626 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
627 assertEquals(1, mappedGroup.getSequences().size());
628 assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
629 // Seq2 in protein has a gap in column 1 - ignored
630 // Seq1 has K which should map to columns 0-3 in Seq1
631 assertEquals(0, mappedGroup.getStartRes());
632 assertEquals(3, mappedGroup.getEndRes());
635 * Now select cols 2-4 in protein. These cover Seq1:AS Seq2:LQ Seq3:VM which
636 * extend over DNA columns 3-12, 1-7, 6-13 respectively, or 1-13 overall.
640 mappedGroup = MappingUtils.mapSequenceGroup(sg, proteinView, dnaView);
641 assertEquals(1, mappedGroup.getStartRes());
642 assertEquals(13, mappedGroup.getEndRes());
645 * Verify mapping sequence group from dna to protein
648 sg.addSequence(cdna.getSequenceAt(0), false);
650 // select columns 4,5 - includes Seq1:codon2 (A) only
653 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
654 assertEquals(2, mappedGroup.getStartRes());
655 assertEquals(2, mappedGroup.getEndRes());
657 // add Seq2 to dna selection cols 4-5 include codons 1 and 2 (LQ)
658 sg.addSequence(cdna.getSequenceAt(1), false);
659 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
660 assertEquals(2, mappedGroup.getStartRes());
661 assertEquals(4, mappedGroup.getEndRes());
663 // add Seq3 to dna selection cols 4-5 include codon 1 (Q)
664 sg.addSequence(cdna.getSequenceAt(2), false);
665 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
666 assertEquals(0, mappedGroup.getStartRes());
667 assertEquals(4, mappedGroup.getEndRes());
670 @Test(groups = { "Functional" })
671 public void testFindMappingsForSequence()
673 SequenceI seq1 = new Sequence("Seq1", "ABC");
674 SequenceI seq2 = new Sequence("Seq2", "ABC");
675 SequenceI seq3 = new Sequence("Seq3", "ABC");
676 SequenceI seq4 = new Sequence("Seq4", "ABC");
677 seq1.createDatasetSequence();
678 seq2.createDatasetSequence();
679 seq3.createDatasetSequence();
680 seq4.createDatasetSequence();
683 * Create mappings from seq1 to seq2, seq2 to seq1, seq3 to seq1
685 AlignedCodonFrame acf1 = new AlignedCodonFrame();
686 MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 3 }, 1, 1);
687 acf1.addMap(seq1.getDatasetSequence(), seq2.getDatasetSequence(), map);
688 AlignedCodonFrame acf2 = new AlignedCodonFrame();
689 acf2.addMap(seq2.getDatasetSequence(), seq1.getDatasetSequence(), map);
690 AlignedCodonFrame acf3 = new AlignedCodonFrame();
691 acf3.addMap(seq3.getDatasetSequence(), seq1.getDatasetSequence(), map);
693 List<AlignedCodonFrame> mappings = new ArrayList<>();
699 * Seq1 has three mappings
701 List<AlignedCodonFrame> result = MappingUtils.findMappingsForSequence(
703 assertEquals(3, result.size());
704 assertTrue(result.contains(acf1));
705 assertTrue(result.contains(acf2));
706 assertTrue(result.contains(acf3));
709 * Seq2 has two mappings
711 result = MappingUtils.findMappingsForSequence(seq2, mappings);
712 assertEquals(2, result.size());
713 assertTrue(result.contains(acf1));
714 assertTrue(result.contains(acf2));
717 * Seq3 has one mapping
719 result = MappingUtils.findMappingsForSequence(seq3, mappings);
720 assertEquals(1, result.size());
721 assertTrue(result.contains(acf3));
724 * Seq4 has no mappings
726 result = MappingUtils.findMappingsForSequence(seq4, mappings);
727 assertEquals(0, result.size());
729 result = MappingUtils.findMappingsForSequence(null, mappings);
730 assertEquals(0, result.size());
732 result = MappingUtils.findMappingsForSequence(seq1, null);
733 assertEquals(0, result.size());
735 result = MappingUtils.findMappingsForSequence(null, null);
736 assertEquals(0, result.size());
740 * just like the one above, but this time, we provide a set of sequences to
741 * subselect the mapping search
743 @Test(groups = { "Functional" })
744 public void testFindMappingsForSequenceAndOthers()
746 SequenceI seq1 = new Sequence("Seq1", "ABC");
747 SequenceI seq2 = new Sequence("Seq2", "ABC");
748 SequenceI seq3 = new Sequence("Seq3", "ABC");
749 SequenceI seq4 = new Sequence("Seq4", "ABC");
750 seq1.createDatasetSequence();
751 seq2.createDatasetSequence();
752 seq3.createDatasetSequence();
753 seq4.createDatasetSequence();
756 * Create mappings from seq1 to seq2, seq2 to seq1, seq3 to seq1, seq3 to seq4
758 AlignedCodonFrame acf1 = new AlignedCodonFrame();
759 MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 3 }, 1, 1);
760 acf1.addMap(seq1.getDatasetSequence(), seq2.getDatasetSequence(), map);
761 AlignedCodonFrame acf2 = new AlignedCodonFrame();
762 acf2.addMap(seq2.getDatasetSequence(), seq1.getDatasetSequence(), map);
763 AlignedCodonFrame acf3 = new AlignedCodonFrame();
764 acf3.addMap(seq3.getDatasetSequence(), seq1.getDatasetSequence(), map);
765 AlignedCodonFrame acf4 = new AlignedCodonFrame();
766 acf4.addMap(seq3.getDatasetSequence(), seq4.getDatasetSequence(), map);
768 List<AlignedCodonFrame> mappings = new ArrayList<>();
777 List<AlignedCodonFrame> result = MappingUtils
778 .findMappingsForSequenceAndOthers(null, mappings,
779 Arrays.asList(new SequenceI[] { seq1, seq2 }));
780 assertTrue(result.isEmpty());
782 result = MappingUtils.findMappingsForSequenceAndOthers(seq1, null,
783 Arrays.asList(new SequenceI[] { seq1, seq2 }));
784 assertTrue(result.isEmpty());
787 * Seq1 has three mappings, but filter argument will only accept
790 result = MappingUtils.findMappingsForSequenceAndOthers(
793 Arrays.asList(new SequenceI[] { seq1, seq2,
794 seq1.getDatasetSequence() }));
795 assertEquals(2, result.size());
796 assertTrue(result.contains(acf1));
797 assertTrue(result.contains(acf2));
798 assertFalse("Did not expect to find mapping acf3 - subselect failed",
799 result.contains(acf3));
801 "Did not expect to find mapping acf4 - doesn't involve sequence",
802 result.contains(acf4));
805 * and verify the no filter case
807 result = MappingUtils.findMappingsForSequenceAndOthers(seq1, mappings,
809 assertEquals(3, result.size());
810 assertTrue(result.contains(acf1));
811 assertTrue(result.contains(acf2));
812 assertTrue(result.contains(acf3));
815 @Test(groups = { "Functional" })
816 public void testMapEditCommand()
818 SequenceI dna = new Sequence("Seq1", "---ACG---GCATCA", 8, 16);
819 SequenceI protein = new Sequence("Seq2", "-T-AS", 5, 7);
820 dna.createDatasetSequence();
821 protein.createDatasetSequence();
822 AlignedCodonFrame acf = new AlignedCodonFrame();
823 MapList map = new MapList(new int[] { 8, 16 }, new int[] { 5, 7 }, 3, 1);
824 acf.addMap(dna.getDatasetSequence(), protein.getDatasetSequence(), map);
825 List<AlignedCodonFrame> mappings = new ArrayList<>();
828 AlignmentI prot = new Alignment(new SequenceI[] { protein });
829 prot.setCodonFrames(mappings);
830 AlignmentI nuc = new Alignment(new SequenceI[] { dna });
833 * construct and perform the edit command to turn "-T-AS" in to "-T-A--S"
834 * i.e. insert two gaps at column 4
836 EditCommand ec = new EditCommand();
837 final Edit edit = ec.new Edit(Action.INSERT_GAP,
838 new SequenceI[] { protein }, 4, 2, '-');
839 ec.appendEdit(edit, prot, true, null);
842 * the mapped edit command should be to insert 6 gaps before base 4 in the
843 * nucleotide sequence, which corresponds to aligned column 12 in the dna
845 EditCommand mappedEdit = MappingUtils.mapEditCommand(ec, false, nuc,
847 assertEquals(1, mappedEdit.getEdits().size());
848 Edit e = mappedEdit.getEdits().get(0);
849 assertEquals(1, e.getSequences().length);
850 assertEquals(dna, e.getSequences()[0]);
851 assertEquals(12, e.getPosition());
852 assertEquals(6, e.getNumber());
856 * Tests for the method that converts a series of [start, end] ranges to
857 * single positions, where the mapping is to a reverse strand i.e. start is
858 * greater than end point mapped to
860 @Test(groups = { "Functional" })
861 public void testFlattenRanges_reverseStrand()
863 assertEquals("[4, 3, 2, 1]",
864 Arrays.toString(MappingUtils.flattenRanges(new int[] { 4, 1 })));
867 Arrays.toString(MappingUtils.flattenRanges(new int[] { 4, 3, 2,
871 Arrays.toString(MappingUtils.flattenRanges(new int[] { 4, 4, 3,
874 "[12, 9, 8, 7, 4, 3, 2, 1]",
875 Arrays.toString(MappingUtils.flattenRanges(new int[] { 12, 12,
877 // forwards and backwards anyone?
879 "[4, 5, 6, 3, 2, 1]",
880 Arrays.toString(MappingUtils.flattenRanges(new int[] { 4, 6, 3,
882 // backwards and forwards
884 "[3, 2, 1, 4, 5, 6]",
885 Arrays.toString(MappingUtils.flattenRanges(new int[] { 3, 1, 4,
887 // trailing unpaired start position is ignored:
889 "[12, 9, 8, 7, 4, 3, 2]",
890 Arrays.toString(MappingUtils.flattenRanges(new int[] { 12, 12,
895 * Test mapping a column selection including hidden columns
897 * @throws IOException
899 @Test(groups = { "Functional" })
900 public void testMapColumnSelection_hiddenColumns() throws IOException
902 setupMappedAlignments();
904 ColumnSelection proteinSelection = new ColumnSelection();
905 HiddenColumns hiddenCols = new HiddenColumns();
908 * Column 0 in protein picks up Seq2/L, Seq3/G which map to cols 0-4 and 0-3
909 * in dna respectively, overall 0-4
911 proteinSelection.hideSelectedColumns(0, hiddenCols);
912 ColumnSelection dnaSelection = new ColumnSelection();
913 HiddenColumns dnaHidden = new HiddenColumns();
914 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
915 proteinView, dnaView, dnaSelection, dnaHidden);
916 assertEquals("[]", dnaSelection.getSelected().toString());
917 Iterator<int[]> regions = dnaHidden.iterator();
918 assertEquals(1, dnaHidden.getNumberOfRegions());
919 assertEquals("[0, 4]", Arrays.toString(regions.next()));
922 * Column 1 in protein picks up Seq1/K which maps to cols 0-3 in dna
924 dnaSelection = new ColumnSelection();
925 dnaHidden = new HiddenColumns();
926 hiddenCols.revealAllHiddenColumns(proteinSelection);
927 // the unhidden columns are now marked selected!
928 assertEquals("[0]", proteinSelection.getSelected().toString());
929 // deselect these or hideColumns will be expanded to include 0
930 proteinSelection.clear();
931 proteinSelection.hideSelectedColumns(1, hiddenCols);
932 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
933 proteinView, dnaView, dnaSelection, dnaHidden);
934 regions = dnaHidden.iterator();
935 assertEquals(1, dnaHidden.getNumberOfRegions());
936 assertEquals("[0, 3]", Arrays.toString(regions.next()));
939 * Column 2 in protein picks up gaps only - no mapping
941 dnaSelection = new ColumnSelection();
942 dnaHidden = new HiddenColumns();
943 hiddenCols.revealAllHiddenColumns(proteinSelection);
944 proteinSelection.clear();
945 proteinSelection.hideSelectedColumns(2, hiddenCols);
946 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
947 proteinView, dnaView, dnaSelection, dnaHidden);
948 assertEquals(0, dnaHidden.getNumberOfRegions());
951 * Column 3 in protein picks up Seq1/P, Seq2/Q, Seq3/S which map to columns
952 * 6-9, 6-10, 5-8 respectively, overall to 5-10
954 dnaSelection = new ColumnSelection();
955 dnaHidden = new HiddenColumns();
956 hiddenCols.revealAllHiddenColumns(proteinSelection);
957 proteinSelection.clear();
958 proteinSelection.hideSelectedColumns(3, hiddenCols); // 5-10 hidden in dna
959 proteinSelection.addElement(1); // 0-3 selected in dna
960 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
961 proteinView, dnaView, dnaSelection, dnaHidden);
962 assertEquals("[0, 1, 2, 3]", dnaSelection.getSelected().toString());
963 regions = dnaHidden.iterator();
964 assertEquals(1, dnaHidden.getNumberOfRegions());
965 assertEquals("[5, 10]", Arrays.toString(regions.next()));
968 * Combine hiding columns 1 and 3 to get discontiguous hidden columns
970 dnaSelection = new ColumnSelection();
971 dnaHidden = new HiddenColumns();
972 hiddenCols.revealAllHiddenColumns(proteinSelection);
973 proteinSelection.clear();
974 proteinSelection.hideSelectedColumns(1, hiddenCols);
975 proteinSelection.hideSelectedColumns(3, hiddenCols);
976 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
977 proteinView, dnaView, dnaSelection, dnaHidden);
978 regions = dnaHidden.iterator();
979 assertEquals(2, dnaHidden.getNumberOfRegions());
980 assertEquals("[0, 3]", Arrays.toString(regions.next()));
981 assertEquals("[5, 10]", Arrays.toString(regions.next()));
984 @Test(groups = { "Functional" })
985 public void testGetLength()
987 assertEquals(0, MappingUtils.getLength(null));
990 * [start, end] ranges
992 List<int[]> ranges = new ArrayList<>();
993 assertEquals(0, MappingUtils.getLength(ranges));
994 ranges.add(new int[] { 1, 1 });
995 assertEquals(1, MappingUtils.getLength(ranges));
996 ranges.add(new int[] { 2, 10 });
997 assertEquals(10, MappingUtils.getLength(ranges));
998 ranges.add(new int[] { 20, 10 });
999 assertEquals(21, MappingUtils.getLength(ranges));
1002 * [start, end, start, end...] ranges
1005 ranges.add(new int[] { 1, 5, 8, 4 });
1006 ranges.add(new int[] { 8, 2 });
1007 ranges.add(new int[] { 12, 12 });
1008 assertEquals(18, MappingUtils.getLength(ranges));
1011 @Test(groups = { "Functional" })
1012 public void testContains()
1014 assertFalse(MappingUtils.contains(null, 1));
1015 List<int[]> ranges = new ArrayList<>();
1016 assertFalse(MappingUtils.contains(ranges, 1));
1018 ranges.add(new int[] { 1, 4 });
1019 ranges.add(new int[] { 6, 6 });
1020 ranges.add(new int[] { 8, 10 });
1021 ranges.add(new int[] { 30, 20 });
1022 ranges.add(new int[] { -16, -44 });
1024 assertFalse(MappingUtils.contains(ranges, 0));
1025 assertTrue(MappingUtils.contains(ranges, 1));
1026 assertTrue(MappingUtils.contains(ranges, 2));
1027 assertTrue(MappingUtils.contains(ranges, 3));
1028 assertTrue(MappingUtils.contains(ranges, 4));
1029 assertFalse(MappingUtils.contains(ranges, 5));
1031 assertTrue(MappingUtils.contains(ranges, 6));
1032 assertFalse(MappingUtils.contains(ranges, 7));
1034 assertTrue(MappingUtils.contains(ranges, 8));
1035 assertTrue(MappingUtils.contains(ranges, 9));
1036 assertTrue(MappingUtils.contains(ranges, 10));
1038 assertFalse(MappingUtils.contains(ranges, 31));
1039 assertTrue(MappingUtils.contains(ranges, 30));
1040 assertTrue(MappingUtils.contains(ranges, 29));
1041 assertTrue(MappingUtils.contains(ranges, 20));
1042 assertFalse(MappingUtils.contains(ranges, 19));
1044 assertFalse(MappingUtils.contains(ranges, -15));
1045 assertTrue(MappingUtils.contains(ranges, -16));
1046 assertTrue(MappingUtils.contains(ranges, -44));
1047 assertFalse(MappingUtils.contains(ranges, -45));
1051 * Test the method that drops positions from the start of a mapped range
1053 @Test(groups = "Functional")
1054 public void testRemoveStartPositions()
1056 int[] ranges = new int[] { 1, 10 };
1057 int[] adjusted = MappingUtils.removeStartPositions(0, ranges);
1058 assertEquals("[1, 10]", Arrays.toString(adjusted));
1060 adjusted = MappingUtils.removeStartPositions(1, ranges);
1061 assertEquals("[2, 10]", Arrays.toString(adjusted));
1062 assertEquals("[1, 10]", Arrays.toString(ranges));
1065 adjusted = MappingUtils.removeStartPositions(1, ranges);
1066 assertEquals("[3, 10]", Arrays.toString(adjusted));
1067 assertEquals("[2, 10]", Arrays.toString(ranges));
1069 ranges = new int[] { 2, 3, 10, 12 };
1070 adjusted = MappingUtils.removeStartPositions(1, ranges);
1071 assertEquals("[3, 3, 10, 12]", Arrays.toString(adjusted));
1072 assertEquals("[2, 3, 10, 12]", Arrays.toString(ranges));
1074 ranges = new int[] { 2, 2, 8, 12 };
1075 adjusted = MappingUtils.removeStartPositions(1, ranges);
1076 assertEquals("[8, 12]", Arrays.toString(adjusted));
1077 assertEquals("[2, 2, 8, 12]", Arrays.toString(ranges));
1079 ranges = new int[] { 2, 2, 8, 12 };
1080 adjusted = MappingUtils.removeStartPositions(2, ranges);
1081 assertEquals("[9, 12]", Arrays.toString(adjusted));
1082 assertEquals("[2, 2, 8, 12]", Arrays.toString(ranges));
1084 ranges = new int[] { 2, 2, 4, 4, 9, 12 };
1085 adjusted = MappingUtils.removeStartPositions(1, ranges);
1086 assertEquals("[4, 4, 9, 12]", Arrays.toString(adjusted));
1087 assertEquals("[2, 2, 4, 4, 9, 12]", Arrays.toString(ranges));
1089 ranges = new int[] { 2, 2, 4, 4, 9, 12 };
1090 adjusted = MappingUtils.removeStartPositions(2, ranges);
1091 assertEquals("[9, 12]", Arrays.toString(adjusted));
1092 assertEquals("[2, 2, 4, 4, 9, 12]", Arrays.toString(ranges));
1094 ranges = new int[] { 2, 3, 9, 12 };
1095 adjusted = MappingUtils.removeStartPositions(3, ranges);
1096 assertEquals("[10, 12]", Arrays.toString(adjusted));
1097 assertEquals("[2, 3, 9, 12]", Arrays.toString(ranges));
1101 * Test the method that drops positions from the start of a mapped range, on
1102 * the reverse strand
1104 @Test(groups = "Functional")
1105 public void testRemoveStartPositions_reverseStrand()
1107 int[] ranges = new int[] { 10, 1 };
1108 int[] adjusted = MappingUtils.removeStartPositions(0, ranges);
1109 assertEquals("[10, 1]", Arrays.toString(adjusted));
1110 assertEquals("[10, 1]", Arrays.toString(ranges));
1113 adjusted = MappingUtils.removeStartPositions(1, ranges);
1114 assertEquals("[9, 1]", Arrays.toString(adjusted));
1115 assertEquals("[10, 1]", Arrays.toString(ranges));
1118 adjusted = MappingUtils.removeStartPositions(1, ranges);
1119 assertEquals("[8, 1]", Arrays.toString(adjusted));
1120 assertEquals("[9, 1]", Arrays.toString(ranges));
1122 ranges = new int[] { 12, 11, 9, 6 };
1123 adjusted = MappingUtils.removeStartPositions(1, ranges);
1124 assertEquals("[11, 11, 9, 6]", Arrays.toString(adjusted));
1125 assertEquals("[12, 11, 9, 6]", Arrays.toString(ranges));
1127 ranges = new int[] { 12, 12, 8, 4 };
1128 adjusted = MappingUtils.removeStartPositions(1, ranges);
1129 assertEquals("[8, 4]", Arrays.toString(adjusted));
1130 assertEquals("[12, 12, 8, 4]", Arrays.toString(ranges));
1132 ranges = new int[] { 12, 12, 8, 4 };
1133 adjusted = MappingUtils.removeStartPositions(2, ranges);
1134 assertEquals("[7, 4]", Arrays.toString(adjusted));
1135 assertEquals("[12, 12, 8, 4]", Arrays.toString(ranges));
1137 ranges = new int[] { 12, 12, 10, 10, 8, 4 };
1138 adjusted = MappingUtils.removeStartPositions(1, ranges);
1139 assertEquals("[10, 10, 8, 4]", Arrays.toString(adjusted));
1140 assertEquals("[12, 12, 10, 10, 8, 4]", Arrays.toString(ranges));
1142 ranges = new int[] { 12, 12, 10, 10, 8, 4 };
1143 adjusted = MappingUtils.removeStartPositions(2, ranges);
1144 assertEquals("[8, 4]", Arrays.toString(adjusted));
1145 assertEquals("[12, 12, 10, 10, 8, 4]", Arrays.toString(ranges));
1147 ranges = new int[] { 12, 11, 8, 4 };
1148 adjusted = MappingUtils.removeStartPositions(3, ranges);
1149 assertEquals("[7, 4]", Arrays.toString(adjusted));
1150 assertEquals("[12, 11, 8, 4]", Arrays.toString(ranges));
1153 @Test(groups = { "Functional" })
1154 public void testRangeContains()
1157 * both forward ranges
1159 assertTrue(MappingUtils.rangeContains(new int[] { 1, 10 }, new int[] {
1161 assertTrue(MappingUtils.rangeContains(new int[] { 1, 10 }, new int[] {
1163 assertTrue(MappingUtils.rangeContains(new int[] { 1, 10 }, new int[] {
1165 assertTrue(MappingUtils.rangeContains(new int[] { 1, 10 }, new int[] {
1167 assertFalse(MappingUtils.rangeContains(new int[] { 1, 10 }, new int[] {
1169 assertFalse(MappingUtils.rangeContains(new int[] { 1, 10 }, new int[] {
1171 assertFalse(MappingUtils.rangeContains(new int[] { 1, 10 }, new int[] {
1173 assertFalse(MappingUtils.rangeContains(new int[] { 1, 10 }, new int[] {
1177 * forward range, reverse query
1179 assertTrue(MappingUtils.rangeContains(new int[] { 1, 10 }, new int[] {
1181 assertTrue(MappingUtils.rangeContains(new int[] { 1, 10 }, new int[] {
1183 assertTrue(MappingUtils.rangeContains(new int[] { 1, 10 }, new int[] {
1185 assertTrue(MappingUtils.rangeContains(new int[] { 1, 10 }, new int[] {
1187 assertFalse(MappingUtils.rangeContains(new int[] { 1, 10 }, new int[] {
1189 assertFalse(MappingUtils.rangeContains(new int[] { 1, 10 }, new int[] {
1193 * reverse range, forward query
1195 assertTrue(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1197 assertTrue(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1199 assertTrue(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1201 assertTrue(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1203 assertFalse(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1205 assertFalse(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1207 assertFalse(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1213 assertTrue(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1215 assertTrue(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1217 assertTrue(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1219 assertTrue(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1221 assertFalse(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1223 assertFalse(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1225 assertFalse(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1227 assertFalse(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1233 assertFalse(MappingUtils.rangeContains(new int[] { 1, 10, 12 },
1236 assertFalse(MappingUtils.rangeContains(new int[] { 1, 10 },
1238 assertFalse(MappingUtils.rangeContains(new int[] { 1, 10 }, null));
1239 assertFalse(MappingUtils.rangeContains(null, new int[] { 1, 10 }));
1242 @Test(groups = "Functional")
1243 public void testRemoveEndPositions()
1245 List<int[]> ranges = new ArrayList<>();
1248 * case 1: truncate last range
1250 ranges.add(new int[] { 1, 10 });
1251 ranges.add(new int[] { 20, 30 });
1252 MappingUtils.removeEndPositions(5, ranges);
1253 assertEquals(2, ranges.size());
1254 assertEquals(25, ranges.get(1)[1]);
1257 * case 2: remove last range
1260 ranges.add(new int[] { 1, 10 });
1261 ranges.add(new int[] { 20, 22 });
1262 MappingUtils.removeEndPositions(3, ranges);
1263 assertEquals(1, ranges.size());
1264 assertEquals(10, ranges.get(0)[1]);
1267 * case 3: truncate penultimate range
1270 ranges.add(new int[] { 1, 10 });
1271 ranges.add(new int[] { 20, 21 });
1272 MappingUtils.removeEndPositions(3, ranges);
1273 assertEquals(1, ranges.size());
1274 assertEquals(9, ranges.get(0)[1]);
1277 * case 4: remove last two ranges
1280 ranges.add(new int[] { 1, 10 });
1281 ranges.add(new int[] { 20, 20 });
1282 ranges.add(new int[] { 30, 30 });
1283 MappingUtils.removeEndPositions(3, ranges);
1284 assertEquals(1, ranges.size());
1285 assertEquals(9, ranges.get(0)[1]);