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;
27 import static org.testng.AssertJUnit.fail;
29 import jalview.api.AlignViewportI;
30 import jalview.commands.EditCommand;
31 import jalview.commands.EditCommand.Action;
32 import jalview.commands.EditCommand.Edit;
33 import jalview.datamodel.AlignedCodonFrame;
34 import jalview.datamodel.Alignment;
35 import jalview.datamodel.AlignmentI;
36 import jalview.datamodel.ColumnSelection;
37 import jalview.datamodel.HiddenColumns;
38 import jalview.datamodel.SearchResultMatchI;
39 import jalview.datamodel.SearchResultsI;
40 import jalview.datamodel.Sequence;
41 import jalview.datamodel.SequenceGroup;
42 import jalview.datamodel.SequenceI;
43 import jalview.gui.AlignViewport;
44 import jalview.gui.JvOptionPane;
45 import jalview.io.DataSourceType;
46 import jalview.io.FileFormat;
47 import jalview.io.FileFormatI;
48 import jalview.io.FormatAdapter;
50 import java.awt.Color;
51 import java.io.IOException;
52 import java.util.ArrayList;
53 import java.util.Arrays;
54 import java.util.Iterator;
55 import java.util.List;
57 import org.testng.annotations.BeforeClass;
58 import org.testng.annotations.Test;
60 public class MappingUtilsTest
63 @BeforeClass(alwaysRun = true)
64 public void setUpJvOptionPane()
66 JvOptionPane.setInteractiveMode(false);
67 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
70 private AlignViewportI dnaView;
72 private AlignViewportI proteinView;
75 * Simple test of mapping with no intron involved.
77 @Test(groups = { "Functional" })
78 public void testBuildSearchResults()
80 final Sequence seq1 = new Sequence("Seq1/5-10", "C-G-TA-GC");
81 seq1.createDatasetSequence();
83 final Sequence aseq1 = new Sequence("Seq1/12-13", "-P-R");
84 aseq1.createDatasetSequence();
87 * Map dna bases 5-10 to protein residues 12-13
89 AlignedCodonFrame acf = new AlignedCodonFrame();
90 MapList map = new MapList(new int[] { 5, 10 }, new int[] { 12, 13 }, 3,
92 acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
93 List<AlignedCodonFrame> acfList = Arrays.asList(new AlignedCodonFrame[]
97 * Check protein residue 12 maps to codon 5-7, 13 to codon 8-10
99 SearchResultsI sr = MappingUtils.buildSearchResults(aseq1, 12, acfList);
100 assertEquals(1, sr.getResults().size());
101 SearchResultMatchI m = sr.getResults().get(0);
102 assertEquals(seq1.getDatasetSequence(), m.getSequence());
103 assertEquals(5, m.getStart());
104 assertEquals(7, m.getEnd());
105 sr = MappingUtils.buildSearchResults(aseq1, 13, acfList);
106 assertEquals(1, sr.getResults().size());
107 m = sr.getResults().get(0);
108 assertEquals(seq1.getDatasetSequence(), m.getSequence());
109 assertEquals(8, m.getStart());
110 assertEquals(10, m.getEnd());
113 * Check inverse mappings, from codons 5-7, 8-10 to protein 12, 13
115 for (int i = 5; i < 11; i++)
117 sr = MappingUtils.buildSearchResults(seq1, i, acfList);
118 assertEquals(1, sr.getResults().size());
119 m = sr.getResults().get(0);
120 assertEquals(aseq1.getDatasetSequence(), m.getSequence());
121 int residue = i > 7 ? 13 : 12;
122 assertEquals(residue, m.getStart());
123 assertEquals(residue, m.getEnd());
128 * Simple test of mapping with introns involved.
130 @Test(groups = { "Functional" })
131 public void testBuildSearchResults_withIntron()
133 final Sequence seq1 = new Sequence("Seq1/5-17", "c-G-tAGa-GcAgCtt");
134 seq1.createDatasetSequence();
136 final Sequence aseq1 = new Sequence("Seq1/8-9", "-E-D");
137 aseq1.createDatasetSequence();
140 * Map dna bases [6, 8, 9], [11, 13, 115] to protein residues 8 and 9
142 AlignedCodonFrame acf = new AlignedCodonFrame();
143 MapList map = new MapList(new int[] { 6, 6, 8, 9, 11, 11, 13, 13, 15,
144 15 }, new int[] { 8, 9 }, 3, 1);
145 acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
146 List<AlignedCodonFrame> acfList = Arrays.asList(new AlignedCodonFrame[]
150 * Check protein residue 8 maps to [6, 8, 9]
152 SearchResultsI sr = MappingUtils.buildSearchResults(aseq1, 8, acfList);
153 assertEquals(2, sr.getResults().size());
154 SearchResultMatchI m = sr.getResults().get(0);
155 assertEquals(seq1.getDatasetSequence(), m.getSequence());
156 assertEquals(6, m.getStart());
157 assertEquals(6, m.getEnd());
158 m = sr.getResults().get(1);
159 assertEquals(seq1.getDatasetSequence(), m.getSequence());
160 assertEquals(8, m.getStart());
161 assertEquals(9, m.getEnd());
164 * Check protein residue 9 maps to [11, 13, 15]
166 sr = MappingUtils.buildSearchResults(aseq1, 9, acfList);
167 assertEquals(3, sr.getResults().size());
168 m = sr.getResults().get(0);
169 assertEquals(seq1.getDatasetSequence(), m.getSequence());
170 assertEquals(11, m.getStart());
171 assertEquals(11, m.getEnd());
172 m = sr.getResults().get(1);
173 assertEquals(seq1.getDatasetSequence(), m.getSequence());
174 assertEquals(13, m.getStart());
175 assertEquals(13, m.getEnd());
176 m = sr.getResults().get(2);
177 assertEquals(seq1.getDatasetSequence(), m.getSequence());
178 assertEquals(15, m.getStart());
179 assertEquals(15, m.getEnd());
182 * Check inverse mappings, from codons to protein
184 for (int i = 5; i < 18; i++)
186 sr = MappingUtils.buildSearchResults(seq1, i, acfList);
187 int residue = (i == 6 || i == 8 || i == 9) ? 8 : (i == 11 || i == 13
191 assertEquals(0, sr.getResults().size());
194 assertEquals(1, sr.getResults().size());
195 m = sr.getResults().get(0);
196 assertEquals(aseq1.getDatasetSequence(), m.getSequence());
197 assertEquals(residue, m.getStart());
198 assertEquals(residue, m.getEnd());
203 * Test mapping a sequence group made of entire sequences.
205 * @throws IOException
207 @Test(groups = { "Functional" })
208 public void testMapSequenceGroup_sequences() throws IOException
211 * Set up dna and protein Seq1/2/3 with mappings (held on the protein
214 AlignmentI cdna = loadAlignment(">Seq1\nACG\n>Seq2\nTGA\n>Seq3\nTAC\n",
216 cdna.setDataset(null);
217 AlignmentI protein = loadAlignment(">Seq1\nK\n>Seq2\nL\n>Seq3\nQ\n",
219 protein.setDataset(null);
220 AlignedCodonFrame acf = new AlignedCodonFrame();
221 MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 1 }, 3, 1);
222 for (int seq = 0; seq < 3; seq++)
224 acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), protein
225 .getSequenceAt(seq).getDatasetSequence(), map);
227 List<AlignedCodonFrame> acfList = Arrays.asList(new AlignedCodonFrame[]
230 AlignViewportI dnaView = new AlignViewport(cdna);
231 AlignViewportI proteinView = new AlignViewport(protein);
232 protein.setCodonFrames(acfList);
235 * Select Seq1 and Seq3 in the protein (startRes=endRes=0)
237 SequenceGroup sg = new SequenceGroup();
238 sg.setColourText(true);
239 sg.setIdColour(Color.GREEN);
240 sg.setOutlineColour(Color.LIGHT_GRAY);
241 sg.addSequence(protein.getSequenceAt(0), false);
242 sg.addSequence(protein.getSequenceAt(2), false);
245 * Verify the mapped sequence group in dna
247 SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
248 proteinView, dnaView);
249 assertTrue(mappedGroup.getColourText());
250 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
251 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
252 assertEquals(2, mappedGroup.getSequences().size());
253 assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
254 assertSame(cdna.getSequenceAt(2), mappedGroup.getSequences().get(1));
255 assertEquals(0, mappedGroup.getStartRes());
256 assertEquals(2, mappedGroup.getEndRes());
259 * Verify mapping sequence group from dna to protein
262 sg.addSequence(cdna.getSequenceAt(1), false);
263 sg.addSequence(cdna.getSequenceAt(0), false);
266 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
267 assertTrue(mappedGroup.getColourText());
268 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
269 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
270 assertEquals(2, mappedGroup.getSequences().size());
271 assertSame(protein.getSequenceAt(1), mappedGroup.getSequences().get(0));
272 assertSame(protein.getSequenceAt(0), mappedGroup.getSequences().get(1));
273 assertEquals(0, mappedGroup.getStartRes());
274 assertEquals(0, mappedGroup.getEndRes());
278 * Helper method to load an alignment and ensure dataset sequences are set up.
284 * @throws IOException
286 protected AlignmentI loadAlignment(final String data, FileFormatI format)
289 AlignmentI a = new FormatAdapter().readFile(data,
290 DataSourceType.PASTE, format);
296 * Test mapping a column selection in protein to its dna equivalent
298 * @throws IOException
300 @Test(groups = { "Functional" })
301 public void testMapColumnSelection_proteinToDna() throws IOException
303 setupMappedAlignments();
305 ColumnSelection colsel = new ColumnSelection();
306 HiddenColumns hidden = new HiddenColumns();
309 * Column 0 in protein picks up Seq2/L, Seq3/G which map to cols 0-4 and 0-3
310 * in dna respectively, overall 0-4
312 colsel.addElement(0);
313 ColumnSelection cs = new ColumnSelection();
314 HiddenColumns hs = new HiddenColumns();
315 MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
317 assertEquals("[0, 1, 2, 3, 4]", cs.getSelected().toString());
320 * Column 1 in protein picks up Seq1/K which maps to cols 0-3 in dna
324 colsel.addElement(1);
325 MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
327 assertEquals("[0, 1, 2, 3]", cs.getSelected().toString());
330 * Column 2 in protein picks up gaps only - no mapping
334 colsel.addElement(2);
335 MappingUtils.mapColumnSelection(colsel, hidden, proteinView,
337 assertEquals("[]", cs.getSelected().toString());
340 * Column 3 in protein picks up Seq1/P, Seq2/Q, Seq3/S which map to columns
341 * 6-9, 6-10, 5-8 respectively, overall to 5-10
345 colsel.addElement(3);
346 MappingUtils.mapColumnSelection(colsel, hidden, proteinView,
348 assertEquals("[5, 6, 7, 8, 9, 10]", cs.getSelected().toString());
351 * Combine selection of columns 1 and 3 to get a discontiguous mapped
356 colsel.addElement(1);
357 colsel.addElement(3);
358 MappingUtils.mapColumnSelection(colsel, hidden, proteinView,
360 assertEquals("[0, 1, 2, 3, 5, 6, 7, 8, 9, 10]", cs.getSelected()
365 * Set up mappings for tests from 3 dna to 3 protein sequences. Sequences have
366 * offset start positions for a more general test case.
368 * @throws IOException
370 protected void setupMappedAlignments() throws IOException
373 * Map (upper-case = coding):
374 * Seq1/10-18 AC-GctGtC-T to Seq1/40 -K-P
375 * Seq2/20-27 Tc-GA-G-T-T to Seq2/20-27 L--Q
376 * Seq3/30-38 TtTT-AaCGg- to Seq3/60-61\nG--S
378 AlignmentI cdna = loadAlignment(">Seq1/10-18\nAC-GctGtC-T\n"
379 + ">Seq2/20-27\nTc-GA-G-T-Tc\n" + ">Seq3/30-38\nTtTT-AaCGg-\n",
381 cdna.setDataset(null);
382 AlignmentI protein = loadAlignment(
383 ">Seq1/40-41\n-K-P\n>Seq2/50-51\nL--Q\n>Seq3/60-61\nG--S\n",
385 protein.setDataset(null);
387 // map first dna to first protein seq
388 AlignedCodonFrame acf = new AlignedCodonFrame();
389 MapList map = new MapList(new int[] { 10, 12, 15, 15, 17, 18 },
390 new int[] { 40, 41 }, 3, 1);
391 acf.addMap(cdna.getSequenceAt(0).getDatasetSequence(), protein
392 .getSequenceAt(0).getDatasetSequence(), map);
394 // map second dna to second protein seq
395 map = new MapList(new int[] { 20, 20, 22, 23, 24, 26 }, new int[] { 50,
397 acf.addMap(cdna.getSequenceAt(1).getDatasetSequence(), protein
398 .getSequenceAt(1).getDatasetSequence(), map);
400 // map third dna to third protein seq
401 map = new MapList(new int[] { 30, 30, 32, 34, 36, 37 }, new int[] { 60,
403 acf.addMap(cdna.getSequenceAt(2).getDatasetSequence(), protein
404 .getSequenceAt(2).getDatasetSequence(), map);
405 List<AlignedCodonFrame> acfList = Arrays.asList(new AlignedCodonFrame[]
408 dnaView = new AlignViewport(cdna);
409 proteinView = new AlignViewport(protein);
410 protein.setCodonFrames(acfList);
414 * Test mapping a column selection in dna to its protein equivalent
416 * @throws IOException
418 @Test(groups = { "Functional" })
419 public void testMapColumnSelection_dnaToProtein() throws IOException
421 setupMappedAlignments();
423 ColumnSelection colsel = new ColumnSelection();
424 HiddenColumns hidden = new HiddenColumns();
427 * Column 0 in dna picks up first bases which map to residue 1, columns 0-1
430 ColumnSelection cs = new ColumnSelection();
431 HiddenColumns hs = new HiddenColumns();
432 colsel.addElement(0);
433 MappingUtils.mapColumnSelection(colsel, hidden, dnaView, proteinView,
435 assertEquals("[0, 1]", cs.getSelected().toString());
438 * Columns 3-5 in dna map to the first residues in protein Seq1, Seq2, and
439 * the first two in Seq3. Overall to columns 0, 1, 3 (col2 is all gaps).
441 colsel.addElement(3);
442 colsel.addElement(4);
443 colsel.addElement(5);
445 MappingUtils.mapColumnSelection(colsel, hidden, dnaView, proteinView,
447 assertEquals("[0, 1, 3]", cs.getSelected().toString());
450 @Test(groups = { "Functional" })
451 public void testMapColumnSelection_null() throws IOException
453 setupMappedAlignments();
454 ColumnSelection cs = new ColumnSelection();
455 HiddenColumns hs = new HiddenColumns();
456 MappingUtils.mapColumnSelection(null, null, dnaView, proteinView, cs,
458 assertTrue("mapped selection not empty", cs.getSelected().isEmpty());
462 * Tests for the method that converts a series of [start, end] ranges to
465 @Test(groups = { "Functional" })
466 public void testFlattenRanges()
468 assertEquals("[1, 2, 3, 4]",
469 Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 4 })));
472 Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 2, 3,
476 Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 1, 2,
479 "[1, 2, 3, 4, 7, 8, 9, 12]",
480 Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 4, 7,
482 // trailing unpaired start position is ignored:
484 "[1, 2, 3, 4, 7, 8, 9, 12]",
485 Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 4, 7,
490 * Test mapping a sequence group made of entire columns.
492 * @throws IOException
494 @Test(groups = { "Functional" })
495 public void testMapSequenceGroup_columns() throws IOException
498 * Set up dna and protein Seq1/2/3 with mappings (held on the protein
501 AlignmentI cdna = loadAlignment(
502 ">Seq1\nACGGCA\n>Seq2\nTGACAG\n>Seq3\nTACGTA\n",
504 cdna.setDataset(null);
505 AlignmentI protein = loadAlignment(">Seq1\nKA\n>Seq2\nLQ\n>Seq3\nQV\n",
507 protein.setDataset(null);
508 AlignedCodonFrame acf = new AlignedCodonFrame();
509 MapList map = new MapList(new int[] { 1, 6 }, new int[] { 1, 2 }, 3, 1);
510 for (int seq = 0; seq < 3; seq++)
512 acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), protein
513 .getSequenceAt(seq).getDatasetSequence(), map);
515 List<AlignedCodonFrame> acfList = Arrays.asList(new AlignedCodonFrame[]
518 AlignViewportI dnaView = new AlignViewport(cdna);
519 AlignViewportI proteinView = new AlignViewport(protein);
520 protein.setCodonFrames(acfList);
523 * Select all sequences, column 2 in the protein
525 SequenceGroup sg = new SequenceGroup();
526 sg.setColourText(true);
527 sg.setIdColour(Color.GREEN);
528 sg.setOutlineColour(Color.LIGHT_GRAY);
529 sg.addSequence(protein.getSequenceAt(0), false);
530 sg.addSequence(protein.getSequenceAt(1), false);
531 sg.addSequence(protein.getSequenceAt(2), false);
536 * Verify the mapped sequence group in dna
538 SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
539 proteinView, dnaView);
540 assertTrue(mappedGroup.getColourText());
541 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
542 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
543 assertEquals(3, mappedGroup.getSequences().size());
544 assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
545 assertSame(cdna.getSequenceAt(1), mappedGroup.getSequences().get(1));
546 assertSame(cdna.getSequenceAt(2), mappedGroup.getSequences().get(2));
547 assertEquals(3, mappedGroup.getStartRes());
548 assertEquals(5, mappedGroup.getEndRes());
551 * Verify mapping sequence group from dna to protein
554 sg.addSequence(cdna.getSequenceAt(0), false);
555 sg.addSequence(cdna.getSequenceAt(1), false);
556 sg.addSequence(cdna.getSequenceAt(2), false);
557 // select columns 2 and 3 in DNA which span protein columns 0 and 1
560 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
561 assertTrue(mappedGroup.getColourText());
562 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
563 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
564 assertEquals(3, mappedGroup.getSequences().size());
565 assertSame(protein.getSequenceAt(0), mappedGroup.getSequences().get(0));
566 assertSame(protein.getSequenceAt(1), mappedGroup.getSequences().get(1));
567 assertSame(protein.getSequenceAt(2), mappedGroup.getSequences().get(2));
568 assertEquals(0, mappedGroup.getStartRes());
569 assertEquals(1, mappedGroup.getEndRes());
573 * Test mapping a sequence group made of a sequences/columns region.
575 * @throws IOException
577 @Test(groups = { "Functional" })
578 public void testMapSequenceGroup_region() throws IOException
581 * Set up gapped dna and protein Seq1/2/3 with mappings (held on the protein
584 AlignmentI cdna = loadAlignment(
585 ">Seq1\nA-CG-GC--AT-CA\n>Seq2\n-TG-AC-AG-T-AT\n>Seq3\n-T--ACG-TAAT-G\n",
587 cdna.setDataset(null);
588 AlignmentI protein = loadAlignment(
589 ">Seq1\n-KA-S\n>Seq2\n--L-QY\n>Seq3\nQ-V-M\n", FileFormat.Fasta);
590 protein.setDataset(null);
591 AlignedCodonFrame acf = new AlignedCodonFrame();
592 MapList map = new MapList(new int[] { 1, 9 }, new int[] { 1, 3 }, 3, 1);
593 for (int seq = 0; seq < 3; seq++)
595 acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), protein
596 .getSequenceAt(seq).getDatasetSequence(), map);
598 List<AlignedCodonFrame> acfList = Arrays.asList(new AlignedCodonFrame[]
601 AlignViewportI dnaView = new AlignViewport(cdna);
602 AlignViewportI proteinView = new AlignViewport(protein);
603 protein.setCodonFrames(acfList);
606 * Select Seq1 and Seq2 in the protein, column 1 (K/-). Expect mapped
607 * sequence group to cover Seq1, columns 0-3 (ACG). Because the selection
608 * only includes a gap in Seq2 there is no mappable selection region in the
611 SequenceGroup sg = new SequenceGroup();
612 sg.setColourText(true);
613 sg.setIdColour(Color.GREEN);
614 sg.setOutlineColour(Color.LIGHT_GRAY);
615 sg.addSequence(protein.getSequenceAt(0), false);
616 sg.addSequence(protein.getSequenceAt(1), false);
621 * Verify the mapped sequence group in dna
623 SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
624 proteinView, dnaView);
625 assertTrue(mappedGroup.getColourText());
626 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
627 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
628 assertEquals(1, mappedGroup.getSequences().size());
629 assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
630 // Seq2 in protein has a gap in column 1 - ignored
631 // Seq1 has K which should map to columns 0-3 in Seq1
632 assertEquals(0, mappedGroup.getStartRes());
633 assertEquals(3, mappedGroup.getEndRes());
636 * Now select cols 2-4 in protein. These cover Seq1:AS Seq2:LQ Seq3:VM which
637 * extend over DNA columns 3-12, 1-7, 6-13 respectively, or 1-13 overall.
641 mappedGroup = MappingUtils.mapSequenceGroup(sg, proteinView, dnaView);
642 assertEquals(1, mappedGroup.getStartRes());
643 assertEquals(13, mappedGroup.getEndRes());
646 * Verify mapping sequence group from dna to protein
649 sg.addSequence(cdna.getSequenceAt(0), false);
651 // select columns 4,5 - includes Seq1:codon2 (A) only
654 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
655 assertEquals(2, mappedGroup.getStartRes());
656 assertEquals(2, mappedGroup.getEndRes());
658 // add Seq2 to dna selection cols 4-5 include codons 1 and 2 (LQ)
659 sg.addSequence(cdna.getSequenceAt(1), false);
660 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
661 assertEquals(2, mappedGroup.getStartRes());
662 assertEquals(4, mappedGroup.getEndRes());
664 // add Seq3 to dna selection cols 4-5 include codon 1 (Q)
665 sg.addSequence(cdna.getSequenceAt(2), false);
666 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
667 assertEquals(0, mappedGroup.getStartRes());
668 assertEquals(4, mappedGroup.getEndRes());
671 @Test(groups = { "Functional" })
672 public void testFindMappingsForSequence()
674 SequenceI seq1 = new Sequence("Seq1", "ABC");
675 SequenceI seq2 = new Sequence("Seq2", "ABC");
676 SequenceI seq3 = new Sequence("Seq3", "ABC");
677 SequenceI seq4 = new Sequence("Seq4", "ABC");
678 seq1.createDatasetSequence();
679 seq2.createDatasetSequence();
680 seq3.createDatasetSequence();
681 seq4.createDatasetSequence();
684 * Create mappings from seq1 to seq2, seq2 to seq1, seq3 to seq1
686 AlignedCodonFrame acf1 = new AlignedCodonFrame();
687 MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 3 }, 1, 1);
688 acf1.addMap(seq1.getDatasetSequence(), seq2.getDatasetSequence(), map);
689 AlignedCodonFrame acf2 = new AlignedCodonFrame();
690 acf2.addMap(seq2.getDatasetSequence(), seq1.getDatasetSequence(), map);
691 AlignedCodonFrame acf3 = new AlignedCodonFrame();
692 acf3.addMap(seq3.getDatasetSequence(), seq1.getDatasetSequence(), map);
694 List<AlignedCodonFrame> mappings = new ArrayList<>();
700 * Seq1 has three mappings
702 List<AlignedCodonFrame> result = MappingUtils.findMappingsForSequence(
704 assertEquals(3, result.size());
705 assertTrue(result.contains(acf1));
706 assertTrue(result.contains(acf2));
707 assertTrue(result.contains(acf3));
710 * Seq2 has two mappings
712 result = MappingUtils.findMappingsForSequence(seq2, mappings);
713 assertEquals(2, result.size());
714 assertTrue(result.contains(acf1));
715 assertTrue(result.contains(acf2));
718 * Seq3 has one mapping
720 result = MappingUtils.findMappingsForSequence(seq3, mappings);
721 assertEquals(1, result.size());
722 assertTrue(result.contains(acf3));
725 * Seq4 has no mappings
727 result = MappingUtils.findMappingsForSequence(seq4, mappings);
728 assertEquals(0, result.size());
730 result = MappingUtils.findMappingsForSequence(null, mappings);
731 assertEquals(0, result.size());
733 result = MappingUtils.findMappingsForSequence(seq1, null);
734 assertEquals(0, result.size());
736 result = MappingUtils.findMappingsForSequence(null, null);
737 assertEquals(0, result.size());
741 * just like the one above, but this time, we provide a set of sequences to
742 * subselect the mapping search
744 @Test(groups = { "Functional" })
745 public void testFindMappingsForSequenceAndOthers()
747 SequenceI seq1 = new Sequence("Seq1", "ABC");
748 SequenceI seq2 = new Sequence("Seq2", "ABC");
749 SequenceI seq3 = new Sequence("Seq3", "ABC");
750 SequenceI seq4 = new Sequence("Seq4", "ABC");
751 seq1.createDatasetSequence();
752 seq2.createDatasetSequence();
753 seq3.createDatasetSequence();
754 seq4.createDatasetSequence();
757 * Create mappings from seq1 to seq2, seq2 to seq1, seq3 to seq1, seq3 to seq4
759 AlignedCodonFrame acf1 = new AlignedCodonFrame();
760 MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 3 }, 1, 1);
761 acf1.addMap(seq1.getDatasetSequence(), seq2.getDatasetSequence(), map);
762 AlignedCodonFrame acf2 = new AlignedCodonFrame();
763 acf2.addMap(seq2.getDatasetSequence(), seq1.getDatasetSequence(), map);
764 AlignedCodonFrame acf3 = new AlignedCodonFrame();
765 acf3.addMap(seq3.getDatasetSequence(), seq1.getDatasetSequence(), map);
766 AlignedCodonFrame acf4 = new AlignedCodonFrame();
767 acf4.addMap(seq3.getDatasetSequence(), seq4.getDatasetSequence(), map);
769 List<AlignedCodonFrame> mappings = new ArrayList<>();
778 List<AlignedCodonFrame> result = MappingUtils
779 .findMappingsForSequenceAndOthers(null, mappings,
780 Arrays.asList(new SequenceI[] { seq1, seq2 }));
781 assertTrue(result.isEmpty());
783 result = MappingUtils.findMappingsForSequenceAndOthers(seq1, null,
784 Arrays.asList(new SequenceI[] { seq1, seq2 }));
785 assertTrue(result.isEmpty());
788 * Seq1 has three mappings, but filter argument will only accept
791 result = MappingUtils.findMappingsForSequenceAndOthers(
794 Arrays.asList(new SequenceI[] { seq1, seq2,
795 seq1.getDatasetSequence() }));
796 assertEquals(2, result.size());
797 assertTrue(result.contains(acf1));
798 assertTrue(result.contains(acf2));
799 assertFalse("Did not expect to find mapping acf3 - subselect failed",
800 result.contains(acf3));
802 "Did not expect to find mapping acf4 - doesn't involve sequence",
803 result.contains(acf4));
806 * and verify the no filter case
808 result = MappingUtils.findMappingsForSequenceAndOthers(seq1, mappings,
810 assertEquals(3, result.size());
811 assertTrue(result.contains(acf1));
812 assertTrue(result.contains(acf2));
813 assertTrue(result.contains(acf3));
816 @Test(groups = { "Functional" })
817 public void testMapEditCommand()
819 SequenceI dna = new Sequence("Seq1", "---ACG---GCATCA", 8, 16);
820 SequenceI protein = new Sequence("Seq2", "-T-AS", 5, 7);
821 dna.createDatasetSequence();
822 protein.createDatasetSequence();
823 AlignedCodonFrame acf = new AlignedCodonFrame();
824 MapList map = new MapList(new int[] { 8, 16 }, new int[] { 5, 7 }, 3, 1);
825 acf.addMap(dna.getDatasetSequence(), protein.getDatasetSequence(), map);
826 List<AlignedCodonFrame> mappings = new ArrayList<>();
829 AlignmentI prot = new Alignment(new SequenceI[] { protein });
830 prot.setCodonFrames(mappings);
831 AlignmentI nuc = new Alignment(new SequenceI[] { dna });
834 * construct and perform the edit command to turn "-T-AS" in to "-T-A--S"
835 * i.e. insert two gaps at column 4
837 EditCommand ec = new EditCommand();
838 final Edit edit = ec.new Edit(Action.INSERT_GAP,
839 new SequenceI[] { protein }, 4, 2, '-');
840 ec.appendEdit(edit, prot, true, null);
843 * the mapped edit command should be to insert 6 gaps before base 4 in the
844 * nucleotide sequence, which corresponds to aligned column 12 in the dna
846 EditCommand mappedEdit = MappingUtils.mapEditCommand(ec, false, nuc,
848 assertEquals(1, mappedEdit.getEdits().size());
849 Edit e = mappedEdit.getEdits().get(0);
850 assertEquals(1, e.getSequences().length);
851 assertEquals(dna, e.getSequences()[0]);
852 assertEquals(12, e.getPosition());
853 assertEquals(6, e.getNumber());
857 * Tests for the method that converts a series of [start, end] ranges to
858 * single positions, where the mapping is to a reverse strand i.e. start is
859 * greater than end point mapped to
861 @Test(groups = { "Functional" })
862 public void testFlattenRanges_reverseStrand()
864 assertEquals("[4, 3, 2, 1]",
865 Arrays.toString(MappingUtils.flattenRanges(new int[] { 4, 1 })));
868 Arrays.toString(MappingUtils.flattenRanges(new int[] { 4, 3, 2,
872 Arrays.toString(MappingUtils.flattenRanges(new int[] { 4, 4, 3,
875 "[12, 9, 8, 7, 4, 3, 2, 1]",
876 Arrays.toString(MappingUtils.flattenRanges(new int[] { 12, 12,
878 // forwards and backwards anyone?
880 "[4, 5, 6, 3, 2, 1]",
881 Arrays.toString(MappingUtils.flattenRanges(new int[] { 4, 6, 3,
883 // backwards and forwards
885 "[3, 2, 1, 4, 5, 6]",
886 Arrays.toString(MappingUtils.flattenRanges(new int[] { 3, 1, 4,
888 // trailing unpaired start position is ignored:
890 "[12, 9, 8, 7, 4, 3, 2]",
891 Arrays.toString(MappingUtils.flattenRanges(new int[] { 12, 12,
896 * Test mapping a column selection including hidden columns
898 * @throws IOException
900 @Test(groups = { "Functional" })
901 public void testMapColumnSelection_hiddenColumns() throws IOException
903 setupMappedAlignments();
905 ColumnSelection proteinSelection = new ColumnSelection();
906 HiddenColumns hiddenCols = new HiddenColumns();
909 * Column 0 in protein picks up Seq2/L, Seq3/G which map to cols 0-4 and 0-3
910 * in dna respectively, overall 0-4
912 proteinSelection.hideSelectedColumns(0, hiddenCols);
913 ColumnSelection dnaSelection = new ColumnSelection();
914 HiddenColumns dnaHidden = new HiddenColumns();
915 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
916 proteinView, dnaView, dnaSelection, dnaHidden);
917 assertEquals("[]", dnaSelection.getSelected().toString());
918 Iterator<int[]> regions = dnaHidden.iterator();
919 assertEquals(1, dnaHidden.getNumberOfRegions());
920 assertEquals("[0, 4]", Arrays.toString(regions.next()));
923 * Column 1 in protein picks up Seq1/K which maps to cols 0-3 in dna
925 dnaSelection = new ColumnSelection();
926 dnaHidden = new HiddenColumns();
927 hiddenCols.revealAllHiddenColumns(proteinSelection);
928 // the unhidden columns are now marked selected!
929 assertEquals("[0]", proteinSelection.getSelected().toString());
930 // deselect these or hideColumns will be expanded to include 0
931 proteinSelection.clear();
932 proteinSelection.hideSelectedColumns(1, hiddenCols);
933 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
934 proteinView, dnaView, dnaSelection, dnaHidden);
935 regions = dnaHidden.iterator();
936 assertEquals(1, dnaHidden.getNumberOfRegions());
937 assertEquals("[0, 3]", Arrays.toString(regions.next()));
940 * Column 2 in protein picks up gaps only - no mapping
942 dnaSelection = new ColumnSelection();
943 dnaHidden = new HiddenColumns();
944 hiddenCols.revealAllHiddenColumns(proteinSelection);
945 proteinSelection.clear();
946 proteinSelection.hideSelectedColumns(2, hiddenCols);
947 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
948 proteinView, dnaView, dnaSelection, dnaHidden);
949 assertEquals(0, dnaHidden.getNumberOfRegions());
952 * Column 3 in protein picks up Seq1/P, Seq2/Q, Seq3/S which map to columns
953 * 6-9, 6-10, 5-8 respectively, overall to 5-10
955 dnaSelection = new ColumnSelection();
956 dnaHidden = new HiddenColumns();
957 hiddenCols.revealAllHiddenColumns(proteinSelection);
958 proteinSelection.clear();
959 proteinSelection.hideSelectedColumns(3, hiddenCols); // 5-10 hidden in dna
960 proteinSelection.addElement(1); // 0-3 selected in dna
961 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
962 proteinView, dnaView, dnaSelection, dnaHidden);
963 assertEquals("[0, 1, 2, 3]", dnaSelection.getSelected().toString());
964 regions = dnaHidden.iterator();
965 assertEquals(1, dnaHidden.getNumberOfRegions());
966 assertEquals("[5, 10]", Arrays.toString(regions.next()));
969 * Combine hiding columns 1 and 3 to get discontiguous hidden columns
971 dnaSelection = new ColumnSelection();
972 dnaHidden = new HiddenColumns();
973 hiddenCols.revealAllHiddenColumns(proteinSelection);
974 proteinSelection.clear();
975 proteinSelection.hideSelectedColumns(1, hiddenCols);
976 proteinSelection.hideSelectedColumns(3, hiddenCols);
977 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
978 proteinView, dnaView, dnaSelection, dnaHidden);
979 regions = dnaHidden.iterator();
980 assertEquals(2, dnaHidden.getNumberOfRegions());
981 assertEquals("[0, 3]", Arrays.toString(regions.next()));
982 assertEquals("[5, 10]", Arrays.toString(regions.next()));
985 @Test(groups = { "Functional" })
986 public void testGetLength()
988 assertEquals(0, MappingUtils.getLength(null));
991 * [start, end] ranges
993 List<int[]> ranges = new ArrayList<>();
994 assertEquals(0, MappingUtils.getLength(ranges));
995 ranges.add(new int[] { 1, 1 });
996 assertEquals(1, MappingUtils.getLength(ranges));
997 ranges.add(new int[] { 2, 10 });
998 assertEquals(10, MappingUtils.getLength(ranges));
999 ranges.add(new int[] { 20, 10 });
1000 assertEquals(21, MappingUtils.getLength(ranges));
1003 * [start, end, start, end...] ranges
1006 ranges.add(new int[] { 1, 5, 8, 4 });
1007 ranges.add(new int[] { 8, 2 });
1008 ranges.add(new int[] { 12, 12 });
1009 assertEquals(18, MappingUtils.getLength(ranges));
1012 @Test(groups = { "Functional" })
1013 public void testContains()
1015 assertFalse(MappingUtils.contains(null, 1));
1016 List<int[]> ranges = new ArrayList<>();
1017 assertFalse(MappingUtils.contains(ranges, 1));
1019 ranges.add(new int[] { 1, 4 });
1020 ranges.add(new int[] { 6, 6 });
1021 ranges.add(new int[] { 8, 10 });
1022 ranges.add(new int[] { 30, 20 });
1023 ranges.add(new int[] { -16, -44 });
1025 assertFalse(MappingUtils.contains(ranges, 0));
1026 assertTrue(MappingUtils.contains(ranges, 1));
1027 assertTrue(MappingUtils.contains(ranges, 2));
1028 assertTrue(MappingUtils.contains(ranges, 3));
1029 assertTrue(MappingUtils.contains(ranges, 4));
1030 assertFalse(MappingUtils.contains(ranges, 5));
1032 assertTrue(MappingUtils.contains(ranges, 6));
1033 assertFalse(MappingUtils.contains(ranges, 7));
1035 assertTrue(MappingUtils.contains(ranges, 8));
1036 assertTrue(MappingUtils.contains(ranges, 9));
1037 assertTrue(MappingUtils.contains(ranges, 10));
1039 assertFalse(MappingUtils.contains(ranges, 31));
1040 assertTrue(MappingUtils.contains(ranges, 30));
1041 assertTrue(MappingUtils.contains(ranges, 29));
1042 assertTrue(MappingUtils.contains(ranges, 20));
1043 assertFalse(MappingUtils.contains(ranges, 19));
1045 assertFalse(MappingUtils.contains(ranges, -15));
1046 assertTrue(MappingUtils.contains(ranges, -16));
1047 assertTrue(MappingUtils.contains(ranges, -44));
1048 assertFalse(MappingUtils.contains(ranges, -45));
1052 * Test the method that drops positions from the start of a mapped range
1054 @Test(groups = "Functional")
1055 public void testRemoveStartPositions()
1057 int[] ranges = new int[] { 1, 10 };
1058 int[] adjusted = MappingUtils.removeStartPositions(0, ranges);
1059 assertEquals("[1, 10]", Arrays.toString(adjusted));
1061 adjusted = MappingUtils.removeStartPositions(1, ranges);
1062 assertEquals("[2, 10]", Arrays.toString(adjusted));
1063 assertEquals("[1, 10]", Arrays.toString(ranges));
1066 adjusted = MappingUtils.removeStartPositions(1, ranges);
1067 assertEquals("[3, 10]", Arrays.toString(adjusted));
1068 assertEquals("[2, 10]", Arrays.toString(ranges));
1070 ranges = new int[] { 2, 3, 10, 12 };
1071 adjusted = MappingUtils.removeStartPositions(1, ranges);
1072 assertEquals("[3, 3, 10, 12]", Arrays.toString(adjusted));
1073 assertEquals("[2, 3, 10, 12]", Arrays.toString(ranges));
1075 ranges = new int[] { 2, 2, 8, 12 };
1076 adjusted = MappingUtils.removeStartPositions(1, ranges);
1077 assertEquals("[8, 12]", Arrays.toString(adjusted));
1078 assertEquals("[2, 2, 8, 12]", Arrays.toString(ranges));
1080 ranges = new int[] { 2, 2, 8, 12 };
1081 adjusted = MappingUtils.removeStartPositions(2, ranges);
1082 assertEquals("[9, 12]", Arrays.toString(adjusted));
1083 assertEquals("[2, 2, 8, 12]", Arrays.toString(ranges));
1085 ranges = new int[] { 2, 2, 4, 4, 9, 12 };
1086 adjusted = MappingUtils.removeStartPositions(1, ranges);
1087 assertEquals("[4, 4, 9, 12]", Arrays.toString(adjusted));
1088 assertEquals("[2, 2, 4, 4, 9, 12]", Arrays.toString(ranges));
1090 ranges = new int[] { 2, 2, 4, 4, 9, 12 };
1091 adjusted = MappingUtils.removeStartPositions(2, ranges);
1092 assertEquals("[9, 12]", Arrays.toString(adjusted));
1093 assertEquals("[2, 2, 4, 4, 9, 12]", Arrays.toString(ranges));
1095 ranges = new int[] { 2, 3, 9, 12 };
1096 adjusted = MappingUtils.removeStartPositions(3, ranges);
1097 assertEquals("[10, 12]", Arrays.toString(adjusted));
1098 assertEquals("[2, 3, 9, 12]", Arrays.toString(ranges));
1102 * Test the method that drops positions from the start of a mapped range, on
1103 * the reverse strand
1105 @Test(groups = "Functional")
1106 public void testRemoveStartPositions_reverseStrand()
1108 int[] ranges = new int[] { 10, 1 };
1109 int[] adjusted = MappingUtils.removeStartPositions(0, ranges);
1110 assertEquals("[10, 1]", Arrays.toString(adjusted));
1111 assertEquals("[10, 1]", Arrays.toString(ranges));
1114 adjusted = MappingUtils.removeStartPositions(1, ranges);
1115 assertEquals("[9, 1]", Arrays.toString(adjusted));
1116 assertEquals("[10, 1]", Arrays.toString(ranges));
1119 adjusted = MappingUtils.removeStartPositions(1, ranges);
1120 assertEquals("[8, 1]", Arrays.toString(adjusted));
1121 assertEquals("[9, 1]", Arrays.toString(ranges));
1123 ranges = new int[] { 12, 11, 9, 6 };
1124 adjusted = MappingUtils.removeStartPositions(1, ranges);
1125 assertEquals("[11, 11, 9, 6]", Arrays.toString(adjusted));
1126 assertEquals("[12, 11, 9, 6]", Arrays.toString(ranges));
1128 ranges = new int[] { 12, 12, 8, 4 };
1129 adjusted = MappingUtils.removeStartPositions(1, ranges);
1130 assertEquals("[8, 4]", Arrays.toString(adjusted));
1131 assertEquals("[12, 12, 8, 4]", Arrays.toString(ranges));
1133 ranges = new int[] { 12, 12, 8, 4 };
1134 adjusted = MappingUtils.removeStartPositions(2, ranges);
1135 assertEquals("[7, 4]", Arrays.toString(adjusted));
1136 assertEquals("[12, 12, 8, 4]", Arrays.toString(ranges));
1138 ranges = new int[] { 12, 12, 10, 10, 8, 4 };
1139 adjusted = MappingUtils.removeStartPositions(1, ranges);
1140 assertEquals("[10, 10, 8, 4]", Arrays.toString(adjusted));
1141 assertEquals("[12, 12, 10, 10, 8, 4]", Arrays.toString(ranges));
1143 ranges = new int[] { 12, 12, 10, 10, 8, 4 };
1144 adjusted = MappingUtils.removeStartPositions(2, ranges);
1145 assertEquals("[8, 4]", Arrays.toString(adjusted));
1146 assertEquals("[12, 12, 10, 10, 8, 4]", Arrays.toString(ranges));
1148 ranges = new int[] { 12, 11, 8, 4 };
1149 adjusted = MappingUtils.removeStartPositions(3, ranges);
1150 assertEquals("[7, 4]", Arrays.toString(adjusted));
1151 assertEquals("[12, 11, 8, 4]", Arrays.toString(ranges));
1154 @Test(groups = { "Functional" })
1155 public void testRangeContains()
1158 * both forward ranges
1160 assertTrue(MappingUtils.rangeContains(new int[] { 1, 10 }, new int[] {
1162 assertTrue(MappingUtils.rangeContains(new int[] { 1, 10 }, new int[] {
1164 assertTrue(MappingUtils.rangeContains(new int[] { 1, 10 }, new int[] {
1166 assertTrue(MappingUtils.rangeContains(new int[] { 1, 10 }, new int[] {
1168 assertFalse(MappingUtils.rangeContains(new int[] { 1, 10 }, new int[] {
1170 assertFalse(MappingUtils.rangeContains(new int[] { 1, 10 }, new int[] {
1172 assertFalse(MappingUtils.rangeContains(new int[] { 1, 10 }, new int[] {
1174 assertFalse(MappingUtils.rangeContains(new int[] { 1, 10 }, new int[] {
1178 * forward range, reverse query
1180 assertTrue(MappingUtils.rangeContains(new int[] { 1, 10 }, new int[] {
1182 assertTrue(MappingUtils.rangeContains(new int[] { 1, 10 }, new int[] {
1184 assertTrue(MappingUtils.rangeContains(new int[] { 1, 10 }, new int[] {
1186 assertTrue(MappingUtils.rangeContains(new int[] { 1, 10 }, new int[] {
1188 assertFalse(MappingUtils.rangeContains(new int[] { 1, 10 }, new int[] {
1190 assertFalse(MappingUtils.rangeContains(new int[] { 1, 10 }, new int[] {
1194 * reverse range, forward query
1196 assertTrue(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1198 assertTrue(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1200 assertTrue(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1202 assertTrue(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1204 assertFalse(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1206 assertFalse(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1208 assertFalse(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1214 assertTrue(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1216 assertTrue(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1218 assertTrue(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1220 assertTrue(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1222 assertFalse(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1224 assertFalse(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1226 assertFalse(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1228 assertFalse(MappingUtils.rangeContains(new int[] { 10, 1 }, new int[] {
1234 assertFalse(MappingUtils.rangeContains(new int[] { 1, 10, 12 },
1237 assertFalse(MappingUtils.rangeContains(new int[] { 1, 10 },
1239 assertFalse(MappingUtils.rangeContains(new int[] { 1, 10 }, null));
1240 assertFalse(MappingUtils.rangeContains(null, new int[] { 1, 10 }));
1243 @Test(groups = "Functional")
1244 public void testRemoveEndPositions()
1246 List<int[]> ranges = new ArrayList<>();
1249 * case 1: truncate last range
1251 ranges.add(new int[] { 1, 10 });
1252 ranges.add(new int[] { 20, 30 });
1253 MappingUtils.removeEndPositions(5, ranges);
1254 assertEquals(2, ranges.size());
1255 assertEquals(25, ranges.get(1)[1]);
1258 * case 2: remove last range
1261 ranges.add(new int[] { 1, 10 });
1262 ranges.add(new int[] { 20, 22 });
1263 MappingUtils.removeEndPositions(3, ranges);
1264 assertEquals(1, ranges.size());
1265 assertEquals(10, ranges.get(0)[1]);
1268 * case 3: truncate penultimate range
1271 ranges.add(new int[] { 1, 10 });
1272 ranges.add(new int[] { 20, 21 });
1273 MappingUtils.removeEndPositions(3, ranges);
1274 assertEquals(1, ranges.size());
1275 assertEquals(9, ranges.get(0)[1]);
1278 * case 4: remove last two ranges
1281 ranges.add(new int[] { 1, 10 });
1282 ranges.add(new int[] { 20, 20 });
1283 ranges.add(new int[] { 30, 30 });
1284 MappingUtils.removeEndPositions(3, ranges);
1285 assertEquals(1, ranges.size());
1286 assertEquals(9, ranges.get(0)[1]);
1289 @Test(groups = "Functional")
1290 public void testListToArray()
1292 List<int[]> ranges = new ArrayList<>();
1294 int[] result = MappingUtils.listToArray(ranges);
1295 assertEquals(result.length, 0);
1296 ranges.add(new int[] {24, 12});
1297 result = MappingUtils.listToArray(ranges);
1298 assertEquals(result.length, 2);
1299 assertEquals(result[0], 24);
1300 assertEquals(result[1], 12);
1301 ranges.add(new int[] {-7, 30});
1302 result = MappingUtils.listToArray(ranges);
1303 assertEquals(result.length, 4);
1304 assertEquals(result[0], 24);
1305 assertEquals(result[1], 12);
1306 assertEquals(result[2], -7);
1307 assertEquals(result[3], 30);
1310 MappingUtils.listToArray(null);
1311 fail("Expected exception");
1312 } catch (NullPointerException e)