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.assertNull;
26 import static org.testng.AssertJUnit.assertSame;
27 import static org.testng.AssertJUnit.assertTrue;
28 import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;
30 import java.awt.Color;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Iterator;
35 import java.util.List;
37 import org.testng.annotations.BeforeClass;
38 import org.testng.annotations.Test;
40 import jalview.api.AlignViewportI;
41 import jalview.bin.Cache;
42 import jalview.commands.EditCommand;
43 import jalview.commands.EditCommand.Action;
44 import jalview.commands.EditCommand.Edit;
45 import jalview.datamodel.AlignedCodonFrame;
46 import jalview.datamodel.Alignment;
47 import jalview.datamodel.AlignmentI;
48 import jalview.datamodel.ColumnSelection;
49 import jalview.datamodel.HiddenColumns;
50 import jalview.datamodel.SearchResultMatchI;
51 import jalview.datamodel.SearchResultsI;
52 import jalview.datamodel.Sequence;
53 import jalview.datamodel.SequenceGroup;
54 import jalview.datamodel.SequenceI;
55 import jalview.gui.AlignViewport;
56 import jalview.gui.JvOptionPane;
57 import jalview.io.DataSourceType;
58 import jalview.io.FileFormat;
59 import jalview.io.FileFormatI;
60 import jalview.io.FormatAdapter;
62 public class MappingUtilsTest
64 @BeforeClass(alwaysRun = true)
70 @BeforeClass(alwaysRun = true)
71 public void setUpJvOptionPane()
73 JvOptionPane.setInteractiveMode(false);
74 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
77 private AlignViewportI dnaView;
79 private AlignViewportI proteinView;
82 * Simple test of mapping with no intron involved.
84 @Test(groups = { "Functional" })
85 public void testBuildSearchResults()
87 final Sequence seq1 = new Sequence("Seq1/5-10", "C-G-TA-GC");
88 seq1.createDatasetSequence();
90 final Sequence aseq1 = new Sequence("Seq1/12-13", "-P-R");
91 aseq1.createDatasetSequence();
94 * Map dna bases 5-10 to protein residues 12-13
96 AlignedCodonFrame acf = new AlignedCodonFrame();
97 MapList map = new MapList(new int[] { 5, 10 }, new int[] { 12, 13 }, 3,
99 acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
100 List<AlignedCodonFrame> acfList = Arrays
101 .asList(new AlignedCodonFrame[]
105 * Check protein residue 12 maps to codon 5-7, 13 to codon 8-10
107 SearchResultsI sr = MappingUtils.buildSearchResults(aseq1, 12, acfList);
108 assertEquals(1, sr.getResults().size());
109 SearchResultMatchI m = sr.getResults().get(0);
110 assertEquals(seq1.getDatasetSequence(), m.getSequence());
111 assertEquals(5, m.getStart());
112 assertEquals(7, m.getEnd());
113 sr = MappingUtils.buildSearchResults(aseq1, 13, acfList);
114 assertEquals(1, sr.getResults().size());
115 m = sr.getResults().get(0);
116 assertEquals(seq1.getDatasetSequence(), m.getSequence());
117 assertEquals(8, m.getStart());
118 assertEquals(10, m.getEnd());
121 * Check inverse mappings, from codons 5-7, 8-10 to protein 12, 13
123 for (int i = 5; i < 11; i++)
125 sr = MappingUtils.buildSearchResults(seq1, i, acfList);
126 assertEquals(1, sr.getResults().size());
127 m = sr.getResults().get(0);
128 assertEquals(aseq1.getDatasetSequence(), m.getSequence());
129 int residue = i > 7 ? 13 : 12;
130 assertEquals(residue, m.getStart());
131 assertEquals(residue, m.getEnd());
136 * Simple test of mapping with introns involved.
138 @Test(groups = { "Functional" })
139 public void testBuildSearchResults_withIntron()
141 final Sequence seq1 = new Sequence("Seq1/5-17", "c-G-tAGa-GcAgCtt");
142 seq1.createDatasetSequence();
144 final Sequence aseq1 = new Sequence("Seq1/8-9", "-E-D");
145 aseq1.createDatasetSequence();
148 * Map dna bases [6, 8, 9], [11, 13, 115] to protein residues 8 and 9
150 AlignedCodonFrame acf = new AlignedCodonFrame();
151 MapList map = new MapList(
153 { 6, 6, 8, 9, 11, 11, 13, 13, 15, 15 }, new int[] { 8, 9 }, 3,
155 acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map);
156 List<AlignedCodonFrame> acfList = Arrays
157 .asList(new AlignedCodonFrame[]
161 * Check protein residue 8 maps to [6, 8, 9]
163 SearchResultsI sr = MappingUtils.buildSearchResults(aseq1, 8, acfList);
164 assertEquals(2, sr.getResults().size());
165 SearchResultMatchI m = sr.getResults().get(0);
166 assertEquals(seq1.getDatasetSequence(), m.getSequence());
167 assertEquals(6, m.getStart());
168 assertEquals(6, m.getEnd());
169 m = sr.getResults().get(1);
170 assertEquals(seq1.getDatasetSequence(), m.getSequence());
171 assertEquals(8, m.getStart());
172 assertEquals(9, m.getEnd());
175 * Check protein residue 9 maps to [11, 13, 15]
177 sr = MappingUtils.buildSearchResults(aseq1, 9, acfList);
178 assertEquals(3, sr.getResults().size());
179 m = sr.getResults().get(0);
180 assertEquals(seq1.getDatasetSequence(), m.getSequence());
181 assertEquals(11, m.getStart());
182 assertEquals(11, m.getEnd());
183 m = sr.getResults().get(1);
184 assertEquals(seq1.getDatasetSequence(), m.getSequence());
185 assertEquals(13, m.getStart());
186 assertEquals(13, m.getEnd());
187 m = sr.getResults().get(2);
188 assertEquals(seq1.getDatasetSequence(), m.getSequence());
189 assertEquals(15, m.getStart());
190 assertEquals(15, m.getEnd());
193 * Check inverse mappings, from codons to protein
195 for (int i = 5; i < 18; i++)
197 sr = MappingUtils.buildSearchResults(seq1, i, acfList);
198 int residue = (i == 6 || i == 8 || i == 9) ? 8
199 : (i == 11 || i == 13 || i == 15 ? 9 : 0);
202 assertEquals(0, sr.getResults().size());
205 assertEquals(1, sr.getResults().size());
206 m = sr.getResults().get(0);
207 assertEquals(aseq1.getDatasetSequence(), m.getSequence());
208 assertEquals(residue, m.getStart());
209 assertEquals(residue, m.getEnd());
214 * Test mapping a sequence group made of entire sequences.
216 * @throws IOException
218 @Test(groups = { "Functional" })
219 public void testMapSequenceGroup_sequences() throws IOException
222 * Set up dna and protein Seq1/2/3 with mappings (held on the protein
225 AlignmentI cdna = loadAlignment(">Seq1\nACG\n>Seq2\nTGA\n>Seq3\nTAC\n",
227 cdna.setDataset(null);
228 AlignmentI protein = loadAlignment(">Seq1\nK\n>Seq2\nL\n>Seq3\nQ\n",
230 protein.setDataset(null);
231 AlignedCodonFrame acf = new AlignedCodonFrame();
232 MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 1 }, 3, 1);
233 for (int seq = 0; seq < 3; seq++)
235 acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(),
236 protein.getSequenceAt(seq).getDatasetSequence(), map);
238 List<AlignedCodonFrame> acfList = Arrays
239 .asList(new AlignedCodonFrame[]
242 AlignViewportI dnaView = new AlignViewport(cdna);
243 AlignViewportI proteinView = new AlignViewport(protein);
244 protein.setCodonFrames(acfList);
247 * Select Seq1 and Seq3 in the protein (startRes=endRes=0)
249 SequenceGroup sg = new SequenceGroup();
250 sg.setColourText(true);
251 sg.setIdColour(Color.GREEN);
252 sg.setOutlineColour(Color.LIGHT_GRAY);
253 sg.addSequence(protein.getSequenceAt(0), false);
254 sg.addSequence(protein.getSequenceAt(2), false);
257 * Verify the mapped sequence group in dna
259 SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
260 proteinView, dnaView);
261 assertTrue(mappedGroup.getColourText());
262 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
263 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
264 assertEquals(2, mappedGroup.getSequences().size());
265 assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
266 assertSame(cdna.getSequenceAt(2), mappedGroup.getSequences().get(1));
267 assertEquals(0, mappedGroup.getStartRes());
268 assertEquals(2, mappedGroup.getEndRes());
271 * Verify mapping sequence group from dna to protein
274 sg.addSequence(cdna.getSequenceAt(1), false);
275 sg.addSequence(cdna.getSequenceAt(0), false);
278 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
279 assertTrue(mappedGroup.getColourText());
280 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
281 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
282 assertEquals(2, mappedGroup.getSequences().size());
283 assertSame(protein.getSequenceAt(1), mappedGroup.getSequences().get(0));
284 assertSame(protein.getSequenceAt(0), mappedGroup.getSequences().get(1));
285 assertEquals(0, mappedGroup.getStartRes());
286 assertEquals(0, mappedGroup.getEndRes());
290 * Helper method to load an alignment and ensure dataset sequences are set up.
296 * @throws IOException
298 protected AlignmentI loadAlignment(final String data, FileFormatI format)
301 AlignmentI a = new FormatAdapter().readFile(data, DataSourceType.PASTE,
308 * Test mapping a column selection in protein to its dna equivalent
310 * @throws IOException
312 @Test(groups = { "Functional" })
313 public void testMapColumnSelection_proteinToDna() throws IOException
315 setupMappedAlignments();
317 ColumnSelection colsel = new ColumnSelection();
318 HiddenColumns hidden = new HiddenColumns();
321 * Column 0 in protein picks up Seq2/L, Seq3/G which map to cols 0-4 and 0-3
322 * in dna respectively, overall 0-4
324 colsel.addElement(0);
325 ColumnSelection cs = new ColumnSelection();
326 HiddenColumns hs = new HiddenColumns();
327 MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
329 assertEquals("[0, 1, 2, 3, 4]", cs.getSelected().toString());
332 * Column 1 in protein picks up Seq1/K which maps to cols 0-3 in dna
336 colsel.addElement(1);
337 MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
339 assertEquals("[0, 1, 2, 3]", cs.getSelected().toString());
342 * Column 2 in protein picks up gaps only - no mapping
346 colsel.addElement(2);
347 MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
349 assertEquals("[]", cs.getSelected().toString());
352 * Column 3 in protein picks up Seq1/P, Seq2/Q, Seq3/S which map to columns
353 * 6-9, 6-10, 5-8 respectively, overall to 5-10
357 colsel.addElement(3);
358 MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
360 assertEquals("[5, 6, 7, 8, 9, 10]", cs.getSelected().toString());
363 * Combine selection of columns 1 and 3 to get a discontiguous mapped
368 colsel.addElement(1);
369 colsel.addElement(3);
370 MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView,
372 assertEquals("[0, 1, 2, 3, 5, 6, 7, 8, 9, 10]",
373 cs.getSelected().toString());
377 * Set up mappings for tests from 3 dna to 3 protein sequences. Sequences have
378 * offset start positions for a more general test case.
380 * @throws IOException
382 protected void setupMappedAlignments() throws IOException
385 * Map (upper-case = coding):
386 * Seq1/10-18 AC-GctGtC-T to Seq1/40 -K-P
387 * Seq2/20-27 Tc-GA-G-T-T to Seq2/20-27 L--Q
388 * Seq3/30-38 TtTT-AaCGg- to Seq3/60-61\nG--S
390 AlignmentI cdna = loadAlignment(">Seq1/10-18\nAC-GctGtC-T\n"
391 + ">Seq2/20-27\nTc-GA-G-T-Tc\n" + ">Seq3/30-38\nTtTT-AaCGg-\n",
393 cdna.setDataset(null);
394 AlignmentI protein = loadAlignment(
395 ">Seq1/40-41\n-K-P\n>Seq2/50-51\nL--Q\n>Seq3/60-61\nG--S\n",
397 protein.setDataset(null);
399 // map first dna to first protein seq
400 AlignedCodonFrame acf = new AlignedCodonFrame();
401 MapList map = new MapList(new int[] { 10, 12, 15, 15, 17, 18 },
404 acf.addMap(cdna.getSequenceAt(0).getDatasetSequence(),
405 protein.getSequenceAt(0).getDatasetSequence(), map);
407 // map second dna to second protein seq
408 map = new MapList(new int[] { 20, 20, 22, 23, 24, 26 },
411 acf.addMap(cdna.getSequenceAt(1).getDatasetSequence(),
412 protein.getSequenceAt(1).getDatasetSequence(), map);
414 // map third dna to third protein seq
415 map = new MapList(new int[] { 30, 30, 32, 34, 36, 37 },
418 acf.addMap(cdna.getSequenceAt(2).getDatasetSequence(),
419 protein.getSequenceAt(2).getDatasetSequence(), map);
420 List<AlignedCodonFrame> acfList = Arrays
421 .asList(new AlignedCodonFrame[]
424 dnaView = new AlignViewport(cdna);
425 proteinView = new AlignViewport(protein);
426 protein.setCodonFrames(acfList);
430 * Test mapping a column selection in dna to its protein equivalent
432 * @throws IOException
434 @Test(groups = { "Functional" })
435 public void testMapColumnSelection_dnaToProtein() throws IOException
437 setupMappedAlignments();
439 ColumnSelection colsel = new ColumnSelection();
440 HiddenColumns hidden = new HiddenColumns();
443 * Column 0 in dna picks up first bases which map to residue 1, columns 0-1
446 ColumnSelection cs = new ColumnSelection();
447 HiddenColumns hs = new HiddenColumns();
448 colsel.addElement(0);
449 MappingUtils.mapColumnSelection(colsel, hidden, dnaView, proteinView,
451 assertEquals("[0, 1]", cs.getSelected().toString());
454 * Columns 3-5 in dna map to the first residues in protein Seq1, Seq2, and
455 * the first two in Seq3. Overall to columns 0, 1, 3 (col2 is all gaps).
457 colsel.addElement(3);
458 colsel.addElement(4);
459 colsel.addElement(5);
461 MappingUtils.mapColumnSelection(colsel, hidden, dnaView, proteinView,
463 assertEquals("[0, 1, 3]", cs.getSelected().toString());
466 @Test(groups = { "Functional" })
467 public void testMapColumnSelection_null() throws IOException
469 setupMappedAlignments();
470 ColumnSelection cs = new ColumnSelection();
471 HiddenColumns hs = new HiddenColumns();
472 MappingUtils.mapColumnSelection(null, null, dnaView, proteinView, cs,
474 assertTrue("mapped selection not empty", cs.getSelected().isEmpty());
478 * Tests for the method that converts a series of [start, end] ranges to
481 @Test(groups = { "Functional" })
482 public void testFlattenRanges()
484 assertEquals("[1, 2, 3, 4]",
485 Arrays.toString(MappingUtils.flattenRanges(new int[]
487 assertEquals("[1, 2, 3, 4]",
488 Arrays.toString(MappingUtils.flattenRanges(new int[]
490 assertEquals("[1, 2, 3, 4]",
491 Arrays.toString(MappingUtils.flattenRanges(new int[]
492 { 1, 1, 2, 2, 3, 3, 4, 4 })));
493 assertEquals("[1, 2, 3, 4, 7, 8, 9, 12]",
494 Arrays.toString(MappingUtils.flattenRanges(new int[]
495 { 1, 4, 7, 9, 12, 12 })));
496 // trailing unpaired start position is ignored:
497 assertEquals("[1, 2, 3, 4, 7, 8, 9, 12]",
498 Arrays.toString(MappingUtils.flattenRanges(new int[]
499 { 1, 4, 7, 9, 12, 12, 15 })));
503 * Test mapping a sequence group made of entire columns.
505 * @throws IOException
507 @Test(groups = { "Functional" })
508 public void testMapSequenceGroup_columns() throws IOException
511 * Set up dna and protein Seq1/2/3 with mappings (held on the protein
514 AlignmentI cdna = loadAlignment(
515 ">Seq1\nACGGCA\n>Seq2\nTGACAG\n>Seq3\nTACGTA\n",
517 cdna.setDataset(null);
518 AlignmentI protein = loadAlignment(">Seq1\nKA\n>Seq2\nLQ\n>Seq3\nQV\n",
520 protein.setDataset(null);
521 AlignedCodonFrame acf = new AlignedCodonFrame();
522 MapList map = new MapList(new int[] { 1, 6 }, new int[] { 1, 2 }, 3, 1);
523 for (int seq = 0; seq < 3; seq++)
525 acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(),
526 protein.getSequenceAt(seq).getDatasetSequence(), map);
528 List<AlignedCodonFrame> acfList = Arrays
529 .asList(new AlignedCodonFrame[]
532 AlignViewportI dnaView = new AlignViewport(cdna);
533 AlignViewportI proteinView = new AlignViewport(protein);
534 protein.setCodonFrames(acfList);
537 * Select all sequences, column 2 in the protein
539 SequenceGroup sg = new SequenceGroup();
540 sg.setColourText(true);
541 sg.setIdColour(Color.GREEN);
542 sg.setOutlineColour(Color.LIGHT_GRAY);
543 sg.addSequence(protein.getSequenceAt(0), false);
544 sg.addSequence(protein.getSequenceAt(1), false);
545 sg.addSequence(protein.getSequenceAt(2), false);
550 * Verify the mapped sequence group in dna
552 SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
553 proteinView, dnaView);
554 assertTrue(mappedGroup.getColourText());
555 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
556 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
557 assertEquals(3, mappedGroup.getSequences().size());
558 assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
559 assertSame(cdna.getSequenceAt(1), mappedGroup.getSequences().get(1));
560 assertSame(cdna.getSequenceAt(2), mappedGroup.getSequences().get(2));
561 assertEquals(3, mappedGroup.getStartRes());
562 assertEquals(5, mappedGroup.getEndRes());
565 * Verify mapping sequence group from dna to protein
568 sg.addSequence(cdna.getSequenceAt(0), false);
569 sg.addSequence(cdna.getSequenceAt(1), false);
570 sg.addSequence(cdna.getSequenceAt(2), false);
571 // select columns 2 and 3 in DNA which span protein columns 0 and 1
574 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
575 assertTrue(mappedGroup.getColourText());
576 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
577 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
578 assertEquals(3, mappedGroup.getSequences().size());
579 assertSame(protein.getSequenceAt(0), mappedGroup.getSequences().get(0));
580 assertSame(protein.getSequenceAt(1), mappedGroup.getSequences().get(1));
581 assertSame(protein.getSequenceAt(2), mappedGroup.getSequences().get(2));
582 assertEquals(0, mappedGroup.getStartRes());
583 assertEquals(1, mappedGroup.getEndRes());
587 * Test mapping a sequence group made of a sequences/columns region.
589 * @throws IOException
591 @Test(groups = { "Functional" })
592 public void testMapSequenceGroup_region() throws IOException
595 * Set up gapped dna and protein Seq1/2/3 with mappings (held on the protein
598 AlignmentI cdna = loadAlignment(
599 ">Seq1\nA-CG-GC--AT-CA\n>Seq2\n-TG-AC-AG-T-AT\n>Seq3\n-T--ACG-TAAT-G\n",
601 cdna.setDataset(null);
602 AlignmentI protein = loadAlignment(
603 ">Seq1\n-KA-S\n>Seq2\n--L-QY\n>Seq3\nQ-V-M\n",
605 protein.setDataset(null);
606 AlignedCodonFrame acf = new AlignedCodonFrame();
607 MapList map = new MapList(new int[] { 1, 9 }, new int[] { 1, 3 }, 3, 1);
608 for (int seq = 0; seq < 3; seq++)
610 acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(),
611 protein.getSequenceAt(seq).getDatasetSequence(), map);
613 List<AlignedCodonFrame> acfList = Arrays
614 .asList(new AlignedCodonFrame[]
617 AlignViewportI dnaView = new AlignViewport(cdna);
618 AlignViewportI proteinView = new AlignViewport(protein);
619 protein.setCodonFrames(acfList);
622 * Select Seq1 and Seq2 in the protein, column 1 (K/-). Expect mapped
623 * sequence group to cover Seq1, columns 0-3 (ACG). Because the selection
624 * only includes a gap in Seq2 there is no mappable selection region in the
627 SequenceGroup sg = new SequenceGroup();
628 sg.setColourText(true);
629 sg.setIdColour(Color.GREEN);
630 sg.setOutlineColour(Color.LIGHT_GRAY);
631 sg.addSequence(protein.getSequenceAt(0), false);
632 sg.addSequence(protein.getSequenceAt(1), false);
637 * Verify the mapped sequence group in dna
639 SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
640 proteinView, dnaView);
641 assertTrue(mappedGroup.getColourText());
642 assertSame(sg.getIdColour(), mappedGroup.getIdColour());
643 assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
644 assertEquals(1, mappedGroup.getSequences().size());
645 assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0));
646 // Seq2 in protein has a gap in column 1 - ignored
647 // Seq1 has K which should map to columns 0-3 in Seq1
648 assertEquals(0, mappedGroup.getStartRes());
649 assertEquals(3, mappedGroup.getEndRes());
652 * Now select cols 2-4 in protein. These cover Seq1:AS Seq2:LQ Seq3:VM which
653 * extend over DNA columns 3-12, 1-7, 6-13 respectively, or 1-13 overall.
657 mappedGroup = MappingUtils.mapSequenceGroup(sg, proteinView, dnaView);
658 assertEquals(1, mappedGroup.getStartRes());
659 assertEquals(13, mappedGroup.getEndRes());
662 * Verify mapping sequence group from dna to protein
665 sg.addSequence(cdna.getSequenceAt(0), false);
667 // select columns 4,5 - includes Seq1:codon2 (A) only
670 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
671 assertEquals(2, mappedGroup.getStartRes());
672 assertEquals(2, mappedGroup.getEndRes());
674 // add Seq2 to dna selection cols 4-5 include codons 1 and 2 (LQ)
675 sg.addSequence(cdna.getSequenceAt(1), false);
676 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
677 assertEquals(2, mappedGroup.getStartRes());
678 assertEquals(4, mappedGroup.getEndRes());
680 // add Seq3 to dna selection cols 4-5 include codon 1 (Q)
681 sg.addSequence(cdna.getSequenceAt(2), false);
682 mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView);
683 assertEquals(0, mappedGroup.getStartRes());
684 assertEquals(4, mappedGroup.getEndRes());
687 @Test(groups = { "Functional" })
688 public void testFindMappingsForSequence()
690 SequenceI seq1 = new Sequence("Seq1", "ABC");
691 SequenceI seq2 = new Sequence("Seq2", "ABC");
692 SequenceI seq3 = new Sequence("Seq3", "ABC");
693 SequenceI seq4 = new Sequence("Seq4", "ABC");
694 seq1.createDatasetSequence();
695 seq2.createDatasetSequence();
696 seq3.createDatasetSequence();
697 seq4.createDatasetSequence();
700 * Create mappings from seq1 to seq2, seq2 to seq1, seq3 to seq1
702 AlignedCodonFrame acf1 = new AlignedCodonFrame();
703 MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 3 }, 1, 1);
704 acf1.addMap(seq1.getDatasetSequence(), seq2.getDatasetSequence(), map);
705 AlignedCodonFrame acf2 = new AlignedCodonFrame();
706 acf2.addMap(seq2.getDatasetSequence(), seq1.getDatasetSequence(), map);
707 AlignedCodonFrame acf3 = new AlignedCodonFrame();
708 acf3.addMap(seq3.getDatasetSequence(), seq1.getDatasetSequence(), map);
710 List<AlignedCodonFrame> mappings = new ArrayList<>();
716 * Seq1 has three mappings
718 List<AlignedCodonFrame> result = MappingUtils
719 .findMappingsForSequence(seq1, mappings);
720 assertEquals(3, result.size());
721 assertTrue(result.contains(acf1));
722 assertTrue(result.contains(acf2));
723 assertTrue(result.contains(acf3));
726 * Seq2 has two mappings
728 result = MappingUtils.findMappingsForSequence(seq2, mappings);
729 assertEquals(2, result.size());
730 assertTrue(result.contains(acf1));
731 assertTrue(result.contains(acf2));
734 * Seq3 has one mapping
736 result = MappingUtils.findMappingsForSequence(seq3, mappings);
737 assertEquals(1, result.size());
738 assertTrue(result.contains(acf3));
741 * Seq4 has no mappings
743 result = MappingUtils.findMappingsForSequence(seq4, mappings);
744 assertEquals(0, result.size());
746 result = MappingUtils.findMappingsForSequence(null, mappings);
747 assertEquals(0, result.size());
749 result = MappingUtils.findMappingsForSequence(seq1, null);
750 assertEquals(0, result.size());
752 result = MappingUtils.findMappingsForSequence(null, null);
753 assertEquals(0, result.size());
757 * just like the one above, but this time, we provide a set of sequences to
758 * subselect the mapping search
760 @Test(groups = { "Functional" })
761 public void testFindMappingsForSequenceAndOthers()
763 SequenceI seq1 = new Sequence("Seq1", "ABC");
764 SequenceI seq2 = new Sequence("Seq2", "ABC");
765 SequenceI seq3 = new Sequence("Seq3", "ABC");
766 SequenceI seq4 = new Sequence("Seq4", "ABC");
767 seq1.createDatasetSequence();
768 seq2.createDatasetSequence();
769 seq3.createDatasetSequence();
770 seq4.createDatasetSequence();
773 * Create mappings from seq1 to seq2, seq2 to seq1, seq3 to seq1, seq3 to seq4
775 AlignedCodonFrame acf1 = new AlignedCodonFrame();
776 MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 3 }, 1, 1);
777 acf1.addMap(seq1.getDatasetSequence(), seq2.getDatasetSequence(), map);
778 AlignedCodonFrame acf2 = new AlignedCodonFrame();
779 acf2.addMap(seq2.getDatasetSequence(), seq1.getDatasetSequence(), map);
780 AlignedCodonFrame acf3 = new AlignedCodonFrame();
781 acf3.addMap(seq3.getDatasetSequence(), seq1.getDatasetSequence(), map);
782 AlignedCodonFrame acf4 = new AlignedCodonFrame();
783 acf4.addMap(seq3.getDatasetSequence(), seq4.getDatasetSequence(), map);
785 List<AlignedCodonFrame> mappings = new ArrayList<>();
794 List<AlignedCodonFrame> result = MappingUtils
795 .findMappingsForSequenceAndOthers(null, mappings,
796 Arrays.asList(new SequenceI[]
798 assertTrue(result.isEmpty());
800 result = MappingUtils.findMappingsForSequenceAndOthers(seq1, null,
801 Arrays.asList(new SequenceI[]
803 assertTrue(result.isEmpty());
806 * Seq1 has three mappings, but filter argument will only accept
809 result = MappingUtils.findMappingsForSequenceAndOthers(seq1, mappings,
810 Arrays.asList(new SequenceI[]
811 { seq1, seq2, seq1.getDatasetSequence() }));
812 assertEquals(2, result.size());
813 assertTrue(result.contains(acf1));
814 assertTrue(result.contains(acf2));
815 assertFalse("Did not expect to find mapping acf3 - subselect failed",
816 result.contains(acf3));
818 "Did not expect to find mapping acf4 - doesn't involve sequence",
819 result.contains(acf4));
822 * and verify the no filter case
824 result = MappingUtils.findMappingsForSequenceAndOthers(seq1, mappings,
826 assertEquals(3, result.size());
827 assertTrue(result.contains(acf1));
828 assertTrue(result.contains(acf2));
829 assertTrue(result.contains(acf3));
832 @Test(groups = { "Functional" })
833 public void testMapEditCommand()
835 SequenceI dna = new Sequence("Seq1", "---ACG---GCATCA", 8, 16);
836 SequenceI protein = new Sequence("Seq2", "-T-AS", 5, 7);
837 dna.createDatasetSequence();
838 protein.createDatasetSequence();
839 AlignedCodonFrame acf = new AlignedCodonFrame();
840 MapList map = new MapList(new int[] { 8, 16 }, new int[] { 5, 7 }, 3,
842 acf.addMap(dna.getDatasetSequence(), protein.getDatasetSequence(), map);
843 List<AlignedCodonFrame> mappings = new ArrayList<>();
846 AlignmentI prot = new Alignment(new SequenceI[] { protein });
847 prot.setCodonFrames(mappings);
848 AlignmentI nuc = new Alignment(new SequenceI[] { dna });
851 * construct and perform the edit command to turn "-T-AS" in to "-T-A--S"
852 * i.e. insert two gaps at column 4
854 EditCommand ec = new EditCommand();
855 final Edit edit = ec.new Edit(Action.INSERT_GAP,
857 { protein }, 4, 2, '-');
858 ec.appendEdit(edit, prot, true, null);
861 * the mapped edit command should be to insert 6 gaps before base 4 in the
862 * nucleotide sequence, which corresponds to aligned column 12 in the dna
864 EditCommand mappedEdit = MappingUtils.mapEditCommand(ec, false, nuc,
866 assertEquals(1, mappedEdit.getEdits().size());
867 Edit e = mappedEdit.getEdits().get(0);
868 assertEquals(1, e.getSequences().length);
869 assertEquals(dna, e.getSequences()[0]);
870 assertEquals(12, e.getPosition());
871 assertEquals(6, e.getNumber());
875 * Tests for the method that converts a series of [start, end] ranges to
876 * single positions, where the mapping is to a reverse strand i.e. start is
877 * greater than end point mapped to
879 @Test(groups = { "Functional" })
880 public void testFlattenRanges_reverseStrand()
882 assertEquals("[4, 3, 2, 1]",
883 Arrays.toString(MappingUtils.flattenRanges(new int[]
885 assertEquals("[4, 3, 2, 1]",
886 Arrays.toString(MappingUtils.flattenRanges(new int[]
888 assertEquals("[4, 3, 2, 1]",
889 Arrays.toString(MappingUtils.flattenRanges(new int[]
890 { 4, 4, 3, 3, 2, 2, 1, 1 })));
891 assertEquals("[12, 9, 8, 7, 4, 3, 2, 1]",
892 Arrays.toString(MappingUtils.flattenRanges(new int[]
893 { 12, 12, 9, 7, 4, 1 })));
894 // forwards and backwards anyone?
895 assertEquals("[4, 5, 6, 3, 2, 1]",
896 Arrays.toString(MappingUtils.flattenRanges(new int[]
898 // backwards and forwards
899 assertEquals("[3, 2, 1, 4, 5, 6]",
900 Arrays.toString(MappingUtils.flattenRanges(new int[]
902 // trailing unpaired start position is ignored:
903 assertEquals("[12, 9, 8, 7, 4, 3, 2]",
904 Arrays.toString(MappingUtils.flattenRanges(new int[]
905 { 12, 12, 9, 7, 4, 2, 1 })));
909 * Test mapping a column selection including hidden columns
911 * @throws IOException
913 @Test(groups = { "Functional" })
914 public void testMapColumnSelection_hiddenColumns() throws IOException
916 setupMappedAlignments();
918 ColumnSelection proteinSelection = new ColumnSelection();
919 HiddenColumns hiddenCols = new HiddenColumns();
922 * Column 0 in protein picks up Seq2/L, Seq3/G which map to cols 0-4 and 0-3
923 * in dna respectively, overall 0-4
925 proteinSelection.hideSelectedColumns(0, hiddenCols);
926 ColumnSelection dnaSelection = new ColumnSelection();
927 HiddenColumns dnaHidden = new HiddenColumns();
928 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
929 proteinView, dnaView, dnaSelection, dnaHidden);
930 assertEquals("[]", dnaSelection.getSelected().toString());
931 Iterator<int[]> regions = dnaHidden.iterator();
932 assertEquals(1, dnaHidden.getNumberOfRegions());
933 assertEquals("[0, 4]", Arrays.toString(regions.next()));
936 * Column 1 in protein picks up Seq1/K which maps to cols 0-3 in dna
938 dnaSelection = new ColumnSelection();
939 dnaHidden = new HiddenColumns();
940 hiddenCols.revealAllHiddenColumns(proteinSelection);
941 // the unhidden columns are now marked selected!
942 assertEquals("[0]", proteinSelection.getSelected().toString());
943 // deselect these or hideColumns will be expanded to include 0
944 proteinSelection.clear();
945 proteinSelection.hideSelectedColumns(1, hiddenCols);
946 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
947 proteinView, dnaView, dnaSelection, dnaHidden);
948 regions = dnaHidden.iterator();
949 assertEquals(1, dnaHidden.getNumberOfRegions());
950 assertEquals("[0, 3]", Arrays.toString(regions.next()));
953 * Column 2 in protein picks up gaps only - no mapping
955 dnaSelection = new ColumnSelection();
956 dnaHidden = new HiddenColumns();
957 hiddenCols.revealAllHiddenColumns(proteinSelection);
958 proteinSelection.clear();
959 proteinSelection.hideSelectedColumns(2, hiddenCols);
960 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
961 proteinView, dnaView, dnaSelection, dnaHidden);
962 assertEquals(0, dnaHidden.getNumberOfRegions());
965 * Column 3 in protein picks up Seq1/P, Seq2/Q, Seq3/S which map to columns
966 * 6-9, 6-10, 5-8 respectively, overall to 5-10
968 dnaSelection = new ColumnSelection();
969 dnaHidden = new HiddenColumns();
970 hiddenCols.revealAllHiddenColumns(proteinSelection);
971 proteinSelection.clear();
972 proteinSelection.hideSelectedColumns(3, hiddenCols); // 5-10 hidden in dna
973 proteinSelection.addElement(1); // 0-3 selected in dna
974 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
975 proteinView, dnaView, dnaSelection, dnaHidden);
976 assertEquals("[0, 1, 2, 3]", dnaSelection.getSelected().toString());
977 regions = dnaHidden.iterator();
978 assertEquals(1, dnaHidden.getNumberOfRegions());
979 assertEquals("[5, 10]", Arrays.toString(regions.next()));
982 * Combine hiding columns 1 and 3 to get discontiguous hidden columns
984 dnaSelection = new ColumnSelection();
985 dnaHidden = new HiddenColumns();
986 hiddenCols.revealAllHiddenColumns(proteinSelection);
987 proteinSelection.clear();
988 proteinSelection.hideSelectedColumns(1, hiddenCols);
989 proteinSelection.hideSelectedColumns(3, hiddenCols);
990 MappingUtils.mapColumnSelection(proteinSelection, hiddenCols,
991 proteinView, dnaView, dnaSelection, dnaHidden);
992 regions = dnaHidden.iterator();
993 assertEquals(2, dnaHidden.getNumberOfRegions());
994 assertEquals("[0, 3]", Arrays.toString(regions.next()));
995 assertEquals("[5, 10]", Arrays.toString(regions.next()));
998 @Test(groups = { "Functional" })
999 public void testGetLength()
1001 assertEquals(0, MappingUtils.getLength(null));
1004 * [start, end] ranges
1006 List<int[]> ranges = new ArrayList<>();
1007 assertEquals(0, MappingUtils.getLength(ranges));
1008 ranges.add(new int[] { 1, 1 });
1009 assertEquals(1, MappingUtils.getLength(ranges));
1010 ranges.add(new int[] { 2, 10 });
1011 assertEquals(10, MappingUtils.getLength(ranges));
1012 ranges.add(new int[] { 20, 10 });
1013 assertEquals(21, MappingUtils.getLength(ranges));
1016 * [start, end, start, end...] ranges
1019 ranges.add(new int[] { 1, 5, 8, 4 });
1020 ranges.add(new int[] { 8, 2 });
1021 ranges.add(new int[] { 12, 12 });
1022 assertEquals(18, MappingUtils.getLength(ranges));
1025 @Test(groups = { "Functional" })
1026 public void testContains()
1028 assertFalse(MappingUtils.contains(null, 1));
1029 List<int[]> ranges = new ArrayList<>();
1030 assertFalse(MappingUtils.contains(ranges, 1));
1032 ranges.add(new int[] { 1, 4 });
1033 ranges.add(new int[] { 6, 6 });
1034 ranges.add(new int[] { 8, 10 });
1035 ranges.add(new int[] { 30, 20 });
1036 ranges.add(new int[] { -16, -44 });
1038 assertFalse(MappingUtils.contains(ranges, 0));
1039 assertTrue(MappingUtils.contains(ranges, 1));
1040 assertTrue(MappingUtils.contains(ranges, 2));
1041 assertTrue(MappingUtils.contains(ranges, 3));
1042 assertTrue(MappingUtils.contains(ranges, 4));
1043 assertFalse(MappingUtils.contains(ranges, 5));
1045 assertTrue(MappingUtils.contains(ranges, 6));
1046 assertFalse(MappingUtils.contains(ranges, 7));
1048 assertTrue(MappingUtils.contains(ranges, 8));
1049 assertTrue(MappingUtils.contains(ranges, 9));
1050 assertTrue(MappingUtils.contains(ranges, 10));
1052 assertFalse(MappingUtils.contains(ranges, 31));
1053 assertTrue(MappingUtils.contains(ranges, 30));
1054 assertTrue(MappingUtils.contains(ranges, 29));
1055 assertTrue(MappingUtils.contains(ranges, 20));
1056 assertFalse(MappingUtils.contains(ranges, 19));
1058 assertFalse(MappingUtils.contains(ranges, -15));
1059 assertTrue(MappingUtils.contains(ranges, -16));
1060 assertTrue(MappingUtils.contains(ranges, -44));
1061 assertFalse(MappingUtils.contains(ranges, -45));
1065 * Test the method that drops positions from the start of a mapped range
1067 @Test(groups = "Functional")
1068 public void testRemoveStartPositions()
1070 int[] ranges = new int[] { 1, 10 };
1071 int[] adjusted = MappingUtils.removeStartPositions(0, ranges);
1072 assertEquals("[1, 10]", Arrays.toString(adjusted));
1074 adjusted = MappingUtils.removeStartPositions(1, ranges);
1075 assertEquals("[2, 10]", Arrays.toString(adjusted));
1076 assertEquals("[1, 10]", Arrays.toString(ranges));
1079 adjusted = MappingUtils.removeStartPositions(1, ranges);
1080 assertEquals("[3, 10]", Arrays.toString(adjusted));
1081 assertEquals("[2, 10]", Arrays.toString(ranges));
1083 ranges = new int[] { 2, 3, 10, 12 };
1084 adjusted = MappingUtils.removeStartPositions(1, ranges);
1085 assertEquals("[3, 3, 10, 12]", Arrays.toString(adjusted));
1086 assertEquals("[2, 3, 10, 12]", Arrays.toString(ranges));
1088 ranges = new int[] { 2, 2, 8, 12 };
1089 adjusted = MappingUtils.removeStartPositions(1, ranges);
1090 assertEquals("[8, 12]", Arrays.toString(adjusted));
1091 assertEquals("[2, 2, 8, 12]", Arrays.toString(ranges));
1093 ranges = new int[] { 2, 2, 8, 12 };
1094 adjusted = MappingUtils.removeStartPositions(2, ranges);
1095 assertEquals("[9, 12]", Arrays.toString(adjusted));
1096 assertEquals("[2, 2, 8, 12]", Arrays.toString(ranges));
1098 ranges = new int[] { 2, 2, 4, 4, 9, 12 };
1099 adjusted = MappingUtils.removeStartPositions(1, ranges);
1100 assertEquals("[4, 4, 9, 12]", Arrays.toString(adjusted));
1101 assertEquals("[2, 2, 4, 4, 9, 12]", Arrays.toString(ranges));
1103 ranges = new int[] { 2, 2, 4, 4, 9, 12 };
1104 adjusted = MappingUtils.removeStartPositions(2, ranges);
1105 assertEquals("[9, 12]", Arrays.toString(adjusted));
1106 assertEquals("[2, 2, 4, 4, 9, 12]", Arrays.toString(ranges));
1108 ranges = new int[] { 2, 3, 9, 12 };
1109 adjusted = MappingUtils.removeStartPositions(3, ranges);
1110 assertEquals("[10, 12]", Arrays.toString(adjusted));
1111 assertEquals("[2, 3, 9, 12]", Arrays.toString(ranges));
1115 * Test the method that drops positions from the start of a mapped range, on
1116 * the reverse strand
1118 @Test(groups = "Functional")
1119 public void testRemoveStartPositions_reverseStrand()
1121 int[] ranges = new int[] { 10, 1 };
1122 int[] adjusted = MappingUtils.removeStartPositions(0, ranges);
1123 assertEquals("[10, 1]", Arrays.toString(adjusted));
1124 assertEquals("[10, 1]", Arrays.toString(ranges));
1127 adjusted = MappingUtils.removeStartPositions(1, ranges);
1128 assertEquals("[9, 1]", Arrays.toString(adjusted));
1129 assertEquals("[10, 1]", Arrays.toString(ranges));
1132 adjusted = MappingUtils.removeStartPositions(1, ranges);
1133 assertEquals("[8, 1]", Arrays.toString(adjusted));
1134 assertEquals("[9, 1]", Arrays.toString(ranges));
1136 ranges = new int[] { 12, 11, 9, 6 };
1137 adjusted = MappingUtils.removeStartPositions(1, ranges);
1138 assertEquals("[11, 11, 9, 6]", Arrays.toString(adjusted));
1139 assertEquals("[12, 11, 9, 6]", Arrays.toString(ranges));
1141 ranges = new int[] { 12, 12, 8, 4 };
1142 adjusted = MappingUtils.removeStartPositions(1, ranges);
1143 assertEquals("[8, 4]", Arrays.toString(adjusted));
1144 assertEquals("[12, 12, 8, 4]", Arrays.toString(ranges));
1146 ranges = new int[] { 12, 12, 8, 4 };
1147 adjusted = MappingUtils.removeStartPositions(2, ranges);
1148 assertEquals("[7, 4]", Arrays.toString(adjusted));
1149 assertEquals("[12, 12, 8, 4]", Arrays.toString(ranges));
1151 ranges = new int[] { 12, 12, 10, 10, 8, 4 };
1152 adjusted = MappingUtils.removeStartPositions(1, ranges);
1153 assertEquals("[10, 10, 8, 4]", Arrays.toString(adjusted));
1154 assertEquals("[12, 12, 10, 10, 8, 4]", Arrays.toString(ranges));
1156 ranges = new int[] { 12, 12, 10, 10, 8, 4 };
1157 adjusted = MappingUtils.removeStartPositions(2, ranges);
1158 assertEquals("[8, 4]", Arrays.toString(adjusted));
1159 assertEquals("[12, 12, 10, 10, 8, 4]", Arrays.toString(ranges));
1161 ranges = new int[] { 12, 11, 8, 4 };
1162 adjusted = MappingUtils.removeStartPositions(3, ranges);
1163 assertEquals("[7, 4]", Arrays.toString(adjusted));
1164 assertEquals("[12, 11, 8, 4]", Arrays.toString(ranges));
1167 @Test(groups = { "Functional" })
1168 public void testRangeContains()
1171 * both forward ranges
1174 MappingUtils.rangeContains(new int[]
1175 { 1, 10 }, new int[] { 1, 10 }));
1177 MappingUtils.rangeContains(new int[]
1178 { 1, 10 }, new int[] { 2, 10 }));
1180 MappingUtils.rangeContains(new int[]
1181 { 1, 10 }, new int[] { 1, 9 }));
1183 MappingUtils.rangeContains(new int[]
1184 { 1, 10 }, new int[] { 4, 5 }));
1186 MappingUtils.rangeContains(new int[]
1187 { 1, 10 }, new int[] { 0, 9 }));
1189 MappingUtils.rangeContains(new int[]
1190 { 1, 10 }, new int[] { -10, -9 }));
1192 MappingUtils.rangeContains(new int[]
1193 { 1, 10 }, new int[] { 1, 11 }));
1195 MappingUtils.rangeContains(new int[]
1196 { 1, 10 }, new int[] { 11, 12 }));
1199 * forward range, reverse query
1202 MappingUtils.rangeContains(new int[]
1203 { 1, 10 }, new int[] { 10, 1 }));
1205 MappingUtils.rangeContains(new int[]
1206 { 1, 10 }, new int[] { 9, 1 }));
1208 MappingUtils.rangeContains(new int[]
1209 { 1, 10 }, new int[] { 10, 2 }));
1211 MappingUtils.rangeContains(new int[]
1212 { 1, 10 }, new int[] { 5, 5 }));
1214 MappingUtils.rangeContains(new int[]
1215 { 1, 10 }, new int[] { 11, 1 }));
1217 MappingUtils.rangeContains(new int[]
1218 { 1, 10 }, new int[] { 10, 0 }));
1221 * reverse range, forward query
1224 MappingUtils.rangeContains(new int[]
1225 { 10, 1 }, new int[] { 1, 10 }));
1227 MappingUtils.rangeContains(new int[]
1228 { 10, 1 }, new int[] { 1, 9 }));
1230 MappingUtils.rangeContains(new int[]
1231 { 10, 1 }, new int[] { 2, 10 }));
1233 MappingUtils.rangeContains(new int[]
1234 { 10, 1 }, new int[] { 6, 6 }));
1236 MappingUtils.rangeContains(new int[]
1237 { 10, 1 }, new int[] { 6, 11 }));
1239 MappingUtils.rangeContains(new int[]
1240 { 10, 1 }, new int[] { 11, 20 }));
1242 MappingUtils.rangeContains(new int[]
1243 { 10, 1 }, new int[] { -3, -2 }));
1249 MappingUtils.rangeContains(new int[]
1250 { 10, 1 }, new int[] { 10, 1 }));
1252 MappingUtils.rangeContains(new int[]
1253 { 10, 1 }, new int[] { 9, 1 }));
1255 MappingUtils.rangeContains(new int[]
1256 { 10, 1 }, new int[] { 10, 2 }));
1258 MappingUtils.rangeContains(new int[]
1259 { 10, 1 }, new int[] { 3, 3 }));
1261 MappingUtils.rangeContains(new int[]
1262 { 10, 1 }, new int[] { 11, 1 }));
1264 MappingUtils.rangeContains(new int[]
1265 { 10, 1 }, new int[] { 10, 0 }));
1267 MappingUtils.rangeContains(new int[]
1268 { 10, 1 }, new int[] { 12, 11 }));
1270 MappingUtils.rangeContains(new int[]
1271 { 10, 1 }, new int[] { -5, -8 }));
1277 MappingUtils.rangeContains(new int[]
1278 { 1, 10, 12 }, new int[] { 1, 10 }));
1280 MappingUtils.rangeContains(new int[]
1281 { 1, 10 }, new int[] { 1 }));
1282 assertFalse(MappingUtils.rangeContains(new int[] { 1, 10 }, null));
1283 assertFalse(MappingUtils.rangeContains(null, new int[] { 1, 10 }));
1286 @Test(groups = "Functional")
1287 public void testRemoveEndPositions()
1289 List<int[]> ranges = new ArrayList<>();
1292 * case 1: truncate last range
1294 ranges.add(new int[] { 1, 10 });
1295 ranges.add(new int[] { 20, 30 });
1296 MappingUtils.removeEndPositions(5, ranges);
1297 assertEquals(2, ranges.size());
1298 assertEquals(25, ranges.get(1)[1]);
1301 * case 2: remove last range
1304 ranges.add(new int[] { 1, 10 });
1305 ranges.add(new int[] { 20, 22 });
1306 MappingUtils.removeEndPositions(3, ranges);
1307 assertEquals(1, ranges.size());
1308 assertEquals(10, ranges.get(0)[1]);
1311 * case 3: truncate penultimate range
1314 ranges.add(new int[] { 1, 10 });
1315 ranges.add(new int[] { 20, 21 });
1316 MappingUtils.removeEndPositions(3, ranges);
1317 assertEquals(1, ranges.size());
1318 assertEquals(9, ranges.get(0)[1]);
1321 * case 4: remove last two ranges
1324 ranges.add(new int[] { 1, 10 });
1325 ranges.add(new int[] { 20, 20 });
1326 ranges.add(new int[] { 30, 30 });
1327 MappingUtils.removeEndPositions(3, ranges);
1328 assertEquals(1, ranges.size());
1329 assertEquals(9, ranges.get(0)[1]);
1332 @Test(groups = "Functional")
1333 public void testFindOverlap()
1335 List<int[]> ranges = new ArrayList<>();
1336 ranges.add(new int[] { 4, 8 });
1337 ranges.add(new int[] { 10, 12 });
1338 ranges.add(new int[] { 16, 19 });
1340 int[] overlap = MappingUtils.findOverlap(ranges, 5, 13);
1341 assertArrayEquals(overlap, new int[] { 5, 12 });
1342 overlap = MappingUtils.findOverlap(ranges, -100, 100);
1343 assertArrayEquals(overlap, new int[] { 4, 19 });
1344 overlap = MappingUtils.findOverlap(ranges, 7, 17);
1345 assertArrayEquals(overlap, new int[] { 7, 17 });
1346 overlap = MappingUtils.findOverlap(ranges, 13, 15);
1347 assertNull(overlap);