X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=test%2Fjalview%2Futil%2FMappingUtilsTest.java;h=655aa2aa1c9e8e991bae5008246993ed1b0557bb;hb=528c0f1815bc67b54618ad5b16c2162946974caf;hp=3c4d4f8f59dfbf926a2ee04c9c12a3390ac23d3b;hpb=1e8c7a9ab9f5da589d0aa2482fd2e3361c320d57;p=jalview.git diff --git a/test/jalview/util/MappingUtilsTest.java b/test/jalview/util/MappingUtilsTest.java index 3c4d4f8..655aa2a 100644 --- a/test/jalview/util/MappingUtilsTest.java +++ b/test/jalview/util/MappingUtilsTest.java @@ -24,7 +24,6 @@ import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertFalse; import static org.testng.AssertJUnit.assertSame; import static org.testng.AssertJUnit.assertTrue; -import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals; import jalview.api.AlignViewportI; import jalview.commands.EditCommand; @@ -704,6 +703,82 @@ public class MappingUtilsTest assertEquals(0, result.size()); } + /** + * just like the one above, but this time, we provide a set of sequences to + * subselect the mapping search + */ + @Test(groups = { "Functional" }) + public void testFindMappingsForSequenceAndOthers() + { + SequenceI seq1 = new Sequence("Seq1", "ABC"); + SequenceI seq2 = new Sequence("Seq2", "ABC"); + SequenceI seq3 = new Sequence("Seq3", "ABC"); + SequenceI seq4 = new Sequence("Seq4", "ABC"); + seq1.createDatasetSequence(); + seq2.createDatasetSequence(); + seq3.createDatasetSequence(); + seq4.createDatasetSequence(); + + /* + * Create mappings from seq1 to seq2, seq2 to seq1, seq3 to seq1, seq3 to seq4 + */ + AlignedCodonFrame acf1 = new AlignedCodonFrame(); + MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 3 }, 1, 1); + acf1.addMap(seq1.getDatasetSequence(), seq2.getDatasetSequence(), map); + AlignedCodonFrame acf2 = new AlignedCodonFrame(); + acf2.addMap(seq2.getDatasetSequence(), seq1.getDatasetSequence(), map); + AlignedCodonFrame acf3 = new AlignedCodonFrame(); + acf3.addMap(seq3.getDatasetSequence(), seq1.getDatasetSequence(), map); + AlignedCodonFrame acf4 = new AlignedCodonFrame(); + acf4.addMap(seq3.getDatasetSequence(), seq4.getDatasetSequence(), map); + + List mappings = new ArrayList(); + mappings.add(acf1); + mappings.add(acf2); + mappings.add(acf3); + mappings.add(acf4); + + /* + * test for null args + */ + List result = MappingUtils + .findMappingsForSequenceAndOthers(null, mappings, + Arrays.asList(new SequenceI[] { seq1, seq2 })); + assertTrue(result.isEmpty()); + + result = MappingUtils.findMappingsForSequenceAndOthers(seq1, null, + Arrays.asList(new SequenceI[] { seq1, seq2 })); + assertTrue(result.isEmpty()); + + /* + * Seq1 has three mappings, but filter argument will only accept + * those to seq2 + */ + result = MappingUtils.findMappingsForSequenceAndOthers( + seq1, + mappings, + Arrays.asList(new SequenceI[] { seq1, seq2, + seq1.getDatasetSequence() })); + assertEquals(2, result.size()); + assertTrue(result.contains(acf1)); + assertTrue(result.contains(acf2)); + assertFalse("Did not expect to find mapping acf3 - subselect failed", + result.contains(acf3)); + assertFalse( + "Did not expect to find mapping acf4 - doesn't involve sequence", + result.contains(acf4)); + + /* + * and verify the no filter case + */ + result = MappingUtils.findMappingsForSequenceAndOthers(seq1, mappings, + null); + assertEquals(3, result.size()); + assertTrue(result.contains(acf1)); + assertTrue(result.contains(acf2)); + assertTrue(result.contains(acf3)); + } + @Test(groups = { "Functional" }) public void testMapEditCommand() { @@ -792,7 +867,7 @@ public class MappingUtilsTest public void testMapColumnSelection_hiddenColumns() throws IOException { setupMappedAlignments(); - + ColumnSelection proteinSelection = new ColumnSelection(); /* @@ -800,8 +875,8 @@ public class MappingUtilsTest * in dna respectively, overall 0-4 */ proteinSelection.hideColumns(0); - ColumnSelection dnaSelection = MappingUtils.mapColumnSelection(proteinSelection, - proteinView, dnaView); + ColumnSelection dnaSelection = MappingUtils.mapColumnSelection( + proteinSelection, proteinView, dnaView); assertEquals("[]", dnaSelection.getSelected().toString()); List hidden = dnaSelection.getHiddenColumns(); assertEquals(1, hidden.size()); @@ -816,7 +891,8 @@ public class MappingUtilsTest // deselect these or hideColumns will be expanded to include 0 proteinSelection.clear(); proteinSelection.hideColumns(1); - dnaSelection = MappingUtils.mapColumnSelection(proteinSelection, proteinView, dnaView); + dnaSelection = MappingUtils.mapColumnSelection(proteinSelection, + proteinView, dnaView); hidden = dnaSelection.getHiddenColumns(); assertEquals(1, hidden.size()); assertEquals("[0, 3]", Arrays.toString(hidden.get(0))); @@ -827,7 +903,8 @@ public class MappingUtilsTest proteinSelection.revealAllHiddenColumns(); proteinSelection.clear(); proteinSelection.hideColumns(2); - dnaSelection = MappingUtils.mapColumnSelection(proteinSelection, proteinView, dnaView); + dnaSelection = MappingUtils.mapColumnSelection(proteinSelection, + proteinView, dnaView); assertTrue(dnaSelection.getHiddenColumns().isEmpty()); /* @@ -838,7 +915,8 @@ public class MappingUtilsTest proteinSelection.clear(); proteinSelection.hideColumns(3); // 5-10 hidden in dna proteinSelection.addElement(1); // 0-3 selected in dna - dnaSelection = MappingUtils.mapColumnSelection(proteinSelection, proteinView, dnaView); + dnaSelection = MappingUtils.mapColumnSelection(proteinSelection, + proteinView, dnaView); assertEquals("[0, 1, 2, 3]", dnaSelection.getSelected().toString()); hidden = dnaSelection.getHiddenColumns(); assertEquals(1, hidden.size()); @@ -851,72 +929,22 @@ public class MappingUtilsTest proteinSelection.clear(); proteinSelection.hideColumns(1); proteinSelection.hideColumns(3); - dnaSelection = MappingUtils.mapColumnSelection(proteinSelection, proteinView, dnaView); + dnaSelection = MappingUtils.mapColumnSelection(proteinSelection, + proteinView, dnaView); hidden = dnaSelection.getHiddenColumns(); assertEquals(2, hidden.size()); assertEquals("[0, 3]", Arrays.toString(hidden.get(0))); assertEquals("[5, 10]", Arrays.toString(hidden.get(1))); } - /** - * Tests for the method that removes the trailing stop codon from a mapping - * range i.e. the last 3 positions (whether split or not) - */ - @Test(groups = { "Functional" }) - public void testUnmapStopCodon() - { - List ranges = new ArrayList(); - - // simple case, forward strand: - ranges.add(new int[] { 1, 3 }); - ranges.add(new int[] { 9, 14 }); - MappingUtils.unmapStopCodon(ranges, 9); - assertEquals(2, ranges.size()); - assertArrayEquals(new int[] { 1, 3 }, ranges.get(0)); - assertArrayEquals(new int[] { 9, 11 }, ranges.get(1)); - - // split stop codon, forward strand: - ranges.clear(); - ranges.add(new int[] { 1, 8 }); - ranges.add(new int[] { 10, 10 }); - MappingUtils.unmapStopCodon(ranges, 9); - assertEquals(1, ranges.size()); - assertArrayEquals(new int[] { 1, 6 }, ranges.get(0)); - - // very split stop codon, forward strand: - ranges.clear(); - ranges.add(new int[] { 1, 1 }); - ranges.add(new int[] { 3, 4 }); - ranges.add(new int[] { 6, 6 }); - ranges.add(new int[] { 8, 8 }); - ranges.add(new int[] { 10, 10 }); - MappingUtils.unmapStopCodon(ranges, 6); - assertEquals(2, ranges.size()); - assertArrayEquals(new int[] { 1, 1 }, ranges.get(0)); - assertArrayEquals(new int[] { 3, 4 }, ranges.get(1)); - - // simple case, reverse strand: - ranges.clear(); - ranges.add(new int[] { 12, 10 }); - ranges.add(new int[] { 6, 1 }); - MappingUtils.unmapStopCodon(ranges, 9); - assertEquals(2, ranges.size()); - assertArrayEquals(new int[] { 12, 10 }, ranges.get(0)); - assertArrayEquals(new int[] { 6, 4 }, ranges.get(1)); - - // split stop codon, reverse strand: - ranges.clear(); - ranges.add(new int[] { 12, 6 }); - ranges.add(new int[] { 4, 3 }); - MappingUtils.unmapStopCodon(ranges, 9); - assertEquals(1, ranges.size()); - assertArrayEquals(new int[] { 12, 7 }, ranges.get(0)); - } - @Test(groups = { "Functional" }) public void testGetLength() { assertEquals(0, MappingUtils.getLength(null)); + + /* + * [start, end] ranges + */ List ranges = new ArrayList(); assertEquals(0, MappingUtils.getLength(ranges)); ranges.add(new int[] { 1, 1 }); @@ -925,6 +953,15 @@ public class MappingUtilsTest assertEquals(10, MappingUtils.getLength(ranges)); ranges.add(new int[] { 20, 10 }); assertEquals(21, MappingUtils.getLength(ranges)); + + /* + * [start, end, start, end...] ranges + */ + ranges.clear(); + ranges.add(new int[] { 1, 5, 8, 4 }); + ranges.add(new int[] { 8, 2 }); + ranges.add(new int[] { 12, 12 }); + assertEquals(18, MappingUtils.getLength(ranges)); } @Test(groups = { "Functional" }) @@ -966,4 +1003,107 @@ public class MappingUtilsTest assertFalse(MappingUtils.contains(ranges, -45)); } + /** + * Test the method that drops positions from the start of a mapped range + */ + @Test(groups = "Functional") + public void testRemoveStartPositions() + { + int[] ranges = new int[] { 1, 10 }; + int[] adjusted = MappingUtils.removeStartPositions(0, ranges); + assertEquals("[1, 10]", Arrays.toString(adjusted)); + + adjusted = MappingUtils.removeStartPositions(1, ranges); + assertEquals("[2, 10]", Arrays.toString(adjusted)); + assertEquals("[1, 10]", Arrays.toString(ranges)); + + ranges = adjusted; + adjusted = MappingUtils.removeStartPositions(1, ranges); + assertEquals("[3, 10]", Arrays.toString(adjusted)); + assertEquals("[2, 10]", Arrays.toString(ranges)); + + ranges = new int[] { 2, 3, 10, 12 }; + adjusted = MappingUtils.removeStartPositions(1, ranges); + assertEquals("[3, 3, 10, 12]", Arrays.toString(adjusted)); + assertEquals("[2, 3, 10, 12]", Arrays.toString(ranges)); + + ranges = new int[] { 2, 2, 8, 12 }; + adjusted = MappingUtils.removeStartPositions(1, ranges); + assertEquals("[8, 12]", Arrays.toString(adjusted)); + assertEquals("[2, 2, 8, 12]", Arrays.toString(ranges)); + + ranges = new int[] { 2, 2, 8, 12 }; + adjusted = MappingUtils.removeStartPositions(2, ranges); + assertEquals("[9, 12]", Arrays.toString(adjusted)); + assertEquals("[2, 2, 8, 12]", Arrays.toString(ranges)); + + ranges = new int[] { 2, 2, 4, 4, 9, 12 }; + adjusted = MappingUtils.removeStartPositions(1, ranges); + assertEquals("[4, 4, 9, 12]", Arrays.toString(adjusted)); + assertEquals("[2, 2, 4, 4, 9, 12]", Arrays.toString(ranges)); + + ranges = new int[] { 2, 2, 4, 4, 9, 12 }; + adjusted = MappingUtils.removeStartPositions(2, ranges); + assertEquals("[9, 12]", Arrays.toString(adjusted)); + assertEquals("[2, 2, 4, 4, 9, 12]", Arrays.toString(ranges)); + + ranges = new int[] { 2, 3, 9, 12 }; + adjusted = MappingUtils.removeStartPositions(3, ranges); + assertEquals("[10, 12]", Arrays.toString(adjusted)); + assertEquals("[2, 3, 9, 12]", Arrays.toString(ranges)); + } + + /** + * Test the method that drops positions from the start of a mapped range, on + * the reverse strand + */ + @Test(groups = "Functional") + public void testRemoveStartPositions_reverseStrand() + { + int[] ranges = new int[] { 10, 1 }; + int[] adjusted = MappingUtils.removeStartPositions(0, ranges); + assertEquals("[10, 1]", Arrays.toString(adjusted)); + assertEquals("[10, 1]", Arrays.toString(ranges)); + + ranges = adjusted; + adjusted = MappingUtils.removeStartPositions(1, ranges); + assertEquals("[9, 1]", Arrays.toString(adjusted)); + assertEquals("[10, 1]", Arrays.toString(ranges)); + + ranges = adjusted; + adjusted = MappingUtils.removeStartPositions(1, ranges); + assertEquals("[8, 1]", Arrays.toString(adjusted)); + assertEquals("[9, 1]", Arrays.toString(ranges)); + + ranges = new int[] { 12, 11, 9, 6 }; + adjusted = MappingUtils.removeStartPositions(1, ranges); + assertEquals("[11, 11, 9, 6]", Arrays.toString(adjusted)); + assertEquals("[12, 11, 9, 6]", Arrays.toString(ranges)); + + ranges = new int[] { 12, 12, 8, 4 }; + adjusted = MappingUtils.removeStartPositions(1, ranges); + assertEquals("[8, 4]", Arrays.toString(adjusted)); + assertEquals("[12, 12, 8, 4]", Arrays.toString(ranges)); + + ranges = new int[] { 12, 12, 8, 4 }; + adjusted = MappingUtils.removeStartPositions(2, ranges); + assertEquals("[7, 4]", Arrays.toString(adjusted)); + assertEquals("[12, 12, 8, 4]", Arrays.toString(ranges)); + + ranges = new int[] { 12, 12, 10, 10, 8, 4 }; + adjusted = MappingUtils.removeStartPositions(1, ranges); + assertEquals("[10, 10, 8, 4]", Arrays.toString(adjusted)); + assertEquals("[12, 12, 10, 10, 8, 4]", Arrays.toString(ranges)); + + ranges = new int[] { 12, 12, 10, 10, 8, 4 }; + adjusted = MappingUtils.removeStartPositions(2, ranges); + assertEquals("[8, 4]", Arrays.toString(adjusted)); + assertEquals("[12, 12, 10, 10, 8, 4]", Arrays.toString(ranges)); + + ranges = new int[] { 12, 11, 8, 4 }; + adjusted = MappingUtils.removeStartPositions(3, ranges); + assertEquals("[7, 4]", Arrays.toString(adjusted)); + assertEquals("[12, 11, 8, 4]", Arrays.toString(ranges)); + } + }