+ @Test(groups = { "Functional" })
+ public void testRangeContains()
+ {
+ /*
+ * both forward ranges
+ */
+ assertTrue(
+ MappingUtils.rangeContains(new int[]
+ { 1, 10 }, new int[] { 1, 10 }));
+ assertTrue(
+ MappingUtils.rangeContains(new int[]
+ { 1, 10 }, new int[] { 2, 10 }));
+ assertTrue(
+ MappingUtils.rangeContains(new int[]
+ { 1, 10 }, new int[] { 1, 9 }));
+ assertTrue(
+ MappingUtils.rangeContains(new int[]
+ { 1, 10 }, new int[] { 4, 5 }));
+ assertFalse(
+ MappingUtils.rangeContains(new int[]
+ { 1, 10 }, new int[] { 0, 9 }));
+ assertFalse(
+ MappingUtils.rangeContains(new int[]
+ { 1, 10 }, new int[] { -10, -9 }));
+ assertFalse(
+ MappingUtils.rangeContains(new int[]
+ { 1, 10 }, new int[] { 1, 11 }));
+ assertFalse(
+ MappingUtils.rangeContains(new int[]
+ { 1, 10 }, new int[] { 11, 12 }));
+
+ /*
+ * forward range, reverse query
+ */
+ assertTrue(
+ MappingUtils.rangeContains(new int[]
+ { 1, 10 }, new int[] { 10, 1 }));
+ assertTrue(
+ MappingUtils.rangeContains(new int[]
+ { 1, 10 }, new int[] { 9, 1 }));
+ assertTrue(
+ MappingUtils.rangeContains(new int[]
+ { 1, 10 }, new int[] { 10, 2 }));
+ assertTrue(
+ MappingUtils.rangeContains(new int[]
+ { 1, 10 }, new int[] { 5, 5 }));
+ assertFalse(
+ MappingUtils.rangeContains(new int[]
+ { 1, 10 }, new int[] { 11, 1 }));
+ assertFalse(
+ MappingUtils.rangeContains(new int[]
+ { 1, 10 }, new int[] { 10, 0 }));
+
+ /*
+ * reverse range, forward query
+ */
+ assertTrue(
+ MappingUtils.rangeContains(new int[]
+ { 10, 1 }, new int[] { 1, 10 }));
+ assertTrue(
+ MappingUtils.rangeContains(new int[]
+ { 10, 1 }, new int[] { 1, 9 }));
+ assertTrue(
+ MappingUtils.rangeContains(new int[]
+ { 10, 1 }, new int[] { 2, 10 }));
+ assertTrue(
+ MappingUtils.rangeContains(new int[]
+ { 10, 1 }, new int[] { 6, 6 }));
+ assertFalse(
+ MappingUtils.rangeContains(new int[]
+ { 10, 1 }, new int[] { 6, 11 }));
+ assertFalse(
+ MappingUtils.rangeContains(new int[]
+ { 10, 1 }, new int[] { 11, 20 }));
+ assertFalse(
+ MappingUtils.rangeContains(new int[]
+ { 10, 1 }, new int[] { -3, -2 }));
+
+ /*
+ * both reverse
+ */
+ assertTrue(
+ MappingUtils.rangeContains(new int[]
+ { 10, 1 }, new int[] { 10, 1 }));
+ assertTrue(
+ MappingUtils.rangeContains(new int[]
+ { 10, 1 }, new int[] { 9, 1 }));
+ assertTrue(
+ MappingUtils.rangeContains(new int[]
+ { 10, 1 }, new int[] { 10, 2 }));
+ assertTrue(
+ MappingUtils.rangeContains(new int[]
+ { 10, 1 }, new int[] { 3, 3 }));
+ assertFalse(
+ MappingUtils.rangeContains(new int[]
+ { 10, 1 }, new int[] { 11, 1 }));
+ assertFalse(
+ MappingUtils.rangeContains(new int[]
+ { 10, 1 }, new int[] { 10, 0 }));
+ assertFalse(
+ MappingUtils.rangeContains(new int[]
+ { 10, 1 }, new int[] { 12, 11 }));
+ assertFalse(
+ MappingUtils.rangeContains(new int[]
+ { 10, 1 }, new int[] { -5, -8 }));
+
+ /*
+ * bad arguments
+ */
+ assertFalse(
+ MappingUtils.rangeContains(new int[]
+ { 1, 10, 12 }, new int[] { 1, 10 }));
+ assertFalse(
+ MappingUtils.rangeContains(new int[]
+ { 1, 10 }, new int[] { 1 }));
+ assertFalse(MappingUtils.rangeContains(new int[] { 1, 10 }, null));
+ assertFalse(MappingUtils.rangeContains(null, new int[] { 1, 10 }));
+ }
+
+ @Test(groups = "Functional")
+ public void testRemoveEndPositions()
+ {
+ List<int[]> ranges = new ArrayList<>();
+
+ /*
+ * case 1: truncate last range
+ */
+ ranges.add(new int[] { 1, 10 });
+ ranges.add(new int[] { 20, 30 });
+ MappingUtils.removeEndPositions(5, ranges);
+ assertEquals(2, ranges.size());
+ assertEquals(25, ranges.get(1)[1]);
+
+ /*
+ * case 2: remove last range
+ */
+ ranges.clear();
+ ranges.add(new int[] { 1, 10 });
+ ranges.add(new int[] { 20, 22 });
+ MappingUtils.removeEndPositions(3, ranges);
+ assertEquals(1, ranges.size());
+ assertEquals(10, ranges.get(0)[1]);
+
+ /*
+ * case 3: truncate penultimate range
+ */
+ ranges.clear();
+ ranges.add(new int[] { 1, 10 });
+ ranges.add(new int[] { 20, 21 });
+ MappingUtils.removeEndPositions(3, ranges);
+ assertEquals(1, ranges.size());
+ assertEquals(9, ranges.get(0)[1]);
+
+ /*
+ * case 4: remove last two ranges
+ */
+ ranges.clear();
+ ranges.add(new int[] { 1, 10 });
+ ranges.add(new int[] { 20, 20 });
+ ranges.add(new int[] { 30, 30 });
+ MappingUtils.removeEndPositions(3, ranges);
+ assertEquals(1, ranges.size());
+ assertEquals(9, ranges.get(0)[1]);
+ }
+
+ @Test(groups = "Functional")
+ public void testFindOverlap()
+ {
+ List<int[]> ranges = new ArrayList<>();
+ ranges.add(new int[] { 4, 8 });
+ ranges.add(new int[] { 10, 12 });
+ ranges.add(new int[] { 16, 19 });
+
+ int[] overlap = MappingUtils.findOverlap(ranges, 5, 13);
+ assertArrayEquals(overlap, new int[] { 5, 12 });
+ overlap = MappingUtils.findOverlap(ranges, -100, 100);
+ assertArrayEquals(overlap, new int[] { 4, 19 });
+ overlap = MappingUtils.findOverlap(ranges, 7, 17);
+ assertArrayEquals(overlap, new int[] { 7, 17 });
+ overlap = MappingUtils.findOverlap(ranges, 13, 15);
+ assertNull(overlap);
+ }
+
+ /**
+ * Test mapping a sequence group where sequences in and outside the group
+ * share a dataset sequence (e.g. alternative CDS for the same gene)
+ * <p>
+ * This scenario doesn't arise after JAL-3763 changes, but test left as still
+ * valid
+ *
+ * @throws IOException
+ */
+ @Test(groups = { "Functional" })
+ public void testMapSequenceGroup_sharedDataset() throws IOException
+ {
+ /*
+ * Set up dna and protein Seq1/2/3 with mappings (held on the protein
+ * viewport). CDS sequences share the same 'gene' dataset sequence.
+ */
+ SequenceI dna = new Sequence("dna", "aaatttgggcccaaatttgggccc");
+ SequenceI cds1 = new Sequence("cds1/1-6", "aaattt");
+ SequenceI cds2 = new Sequence("cds1/4-9", "tttggg");
+ SequenceI cds3 = new Sequence("cds1/19-24", "gggccc");
+
+ cds1.setDatasetSequence(dna);
+ cds2.setDatasetSequence(dna);
+ cds3.setDatasetSequence(dna);
+
+ SequenceI pep1 = new Sequence("pep1", "KF");
+ SequenceI pep2 = new Sequence("pep2", "FG");
+ SequenceI pep3 = new Sequence("pep3", "GP");
+ pep1.createDatasetSequence();
+ pep2.createDatasetSequence();
+ pep3.createDatasetSequence();
+
+ /*
+ * add mappings from coding positions of dna to respective peptides
+ */
+ AlignedCodonFrame acf = new AlignedCodonFrame();
+ acf.addMap(dna, pep1,
+ new MapList(new int[]
+ { 1, 6 }, new int[] { 1, 2 }, 3, 1));
+ acf.addMap(dna, pep2,
+ new MapList(new int[]
+ { 4, 9 }, new int[] { 1, 2 }, 3, 1));
+ acf.addMap(dna, pep3,
+ new MapList(new int[]
+ { 19, 24 }, new int[] { 1, 2 }, 3, 1));
+
+ List<AlignedCodonFrame> acfList = Arrays
+ .asList(new AlignedCodonFrame[]
+ { acf });
+
+ AlignmentI cdna = new Alignment(new SequenceI[] { cds1, cds2, cds3 });
+ AlignmentI protein = new Alignment(
+ new SequenceI[]
+ { pep1, pep2, pep3 });
+ AlignViewportI cdnaView = new AlignViewport(cdna);
+ AlignViewportI peptideView = new AlignViewport(protein);
+ protein.setCodonFrames(acfList);
+
+ /*
+ * Select pep1 and pep3 in the protein alignment
+ */
+ SequenceGroup sg = new SequenceGroup();
+ sg.setColourText(true);
+ sg.setIdColour(Color.GREEN);
+ sg.setOutlineColour(Color.LIGHT_GRAY);
+ sg.addSequence(pep1, false);
+ sg.addSequence(pep3, false);
+ sg.setEndRes(protein.getWidth() - 1);
+
+ /*
+ * Verify the mapped sequence group in dna is cds1 and cds3
+ */
+ SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
+ peptideView, cdnaView);
+ assertTrue(mappedGroup.getColourText());
+ assertSame(sg.getIdColour(), mappedGroup.getIdColour());
+ assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
+ assertEquals(2, mappedGroup.getSequences().size());
+ assertSame(cds1, mappedGroup.getSequences().get(0));
+ assertSame(cds3, mappedGroup.getSequences().get(1));
+ // columns 1-6 selected (0-5 base zero)
+ assertEquals(0, mappedGroup.getStartRes());
+ assertEquals(5, mappedGroup.getEndRes());
+
+ /*
+ * Select mapping sequence group from dna to protein
+ */
+ sg.clear();
+ sg.addSequence(cds2, false);
+ sg.addSequence(cds1, false);
+ sg.setStartRes(0);
+ sg.setEndRes(cdna.getWidth() - 1);
+ mappedGroup = MappingUtils.mapSequenceGroup(sg, cdnaView, peptideView);
+ assertTrue(mappedGroup.getColourText());
+ assertSame(sg.getIdColour(), mappedGroup.getIdColour());
+ assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour());
+ assertEquals(2, mappedGroup.getSequences().size());
+ assertSame(protein.getSequenceAt(1), mappedGroup.getSequences().get(0));
+ assertSame(protein.getSequenceAt(0), mappedGroup.getSequences().get(1));
+ assertEquals(0, mappedGroup.getStartRes());
+ assertEquals(1, mappedGroup.getEndRes()); // two columns
+ }
+
+ // new for 2.12
+ @Test(groups = "Functional")
+ public void testAddRange()
+ {
+ int[] range = { 1, 5 };
+ List<int[]> ranges = new ArrayList<>();
+
+ // add to empty list:
+ MappingUtils.addRange(range, ranges);
+ assertEquals(1, ranges.size());
+ assertSame(range, ranges.get(0));
+
+ // extend contiguous (same position):
+ MappingUtils.addRange(new int[] { 5, 10 }, ranges);
+ assertEquals(1, ranges.size());
+ assertEquals(1, ranges.get(0)[0]);
+ assertEquals(10, ranges.get(0)[1]);
+
+ // extend contiguous (next position):
+ MappingUtils.addRange(new int[] { 11, 15 }, ranges);
+ assertEquals(1, ranges.size());
+ assertEquals(1, ranges.get(0)[0]);
+ assertEquals(15, ranges.get(0)[1]);
+
+ // change direction: range is not merged:
+ MappingUtils.addRange(new int[] { 16, 10 }, ranges);
+ assertEquals(2, ranges.size());
+ assertEquals(16, ranges.get(1)[0]);
+ assertEquals(10, ranges.get(1)[1]);
+
+ // extend reverse contiguous (same position):
+ MappingUtils.addRange(new int[] { 10, 8 }, ranges);
+ assertEquals(2, ranges.size());
+ assertEquals(16, ranges.get(1)[0]);
+ assertEquals(8, ranges.get(1)[1]);
+
+ // extend reverse contiguous (next position):
+ MappingUtils.addRange(new int[] { 7, 6 }, ranges);
+ assertEquals(2, ranges.size());
+ assertEquals(16, ranges.get(1)[0]);
+ assertEquals(6, ranges.get(1)[1]);
+
+ // change direction: range is not merged:
+ MappingUtils.addRange(new int[] { 6, 9 }, ranges);
+ assertEquals(3, ranges.size());
+ assertEquals(6, ranges.get(2)[0]);
+ assertEquals(9, ranges.get(2)[1]);
+
+ // not contiguous: not merged
+ MappingUtils.addRange(new int[] { 11, 12 }, ranges);
+ assertEquals(4, ranges.size());
+ assertEquals(11, ranges.get(3)[0]);
+ assertEquals(12, ranges.get(3)[1]);
+ }