X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=test%2Fjalview%2Futil%2FMappingUtilsTest.java;h=b84e770ad970f29e6bb004492612bdba0b5a4c61;hb=80ff57c095b792b8e8e123ddb0eff4d1780d6cbd;hp=853ebd5eed5d0056ebed320923bd2d03ea9b1be7;hpb=b8058f3f849f44740a695c83e96bdca3a197af5c;p=jalview.git diff --git a/test/jalview/util/MappingUtilsTest.java b/test/jalview/util/MappingUtilsTest.java index 853ebd5..b84e770 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; @@ -34,13 +33,16 @@ import jalview.datamodel.AlignedCodonFrame; import jalview.datamodel.Alignment; import jalview.datamodel.AlignmentI; import jalview.datamodel.ColumnSelection; -import jalview.datamodel.SearchResults; -import jalview.datamodel.SearchResults.Match; +import jalview.datamodel.SearchResultMatchI; +import jalview.datamodel.SearchResultsI; import jalview.datamodel.Sequence; import jalview.datamodel.SequenceGroup; import jalview.datamodel.SequenceI; import jalview.gui.AlignViewport; -import jalview.io.AppletFormatAdapter; +import jalview.gui.JvOptionPane; +import jalview.io.DataSourceType; +import jalview.io.FileFormat; +import jalview.io.FileFormatI; import jalview.io.FormatAdapter; import java.awt.Color; @@ -49,10 +51,19 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class MappingUtilsTest { + + @BeforeClass(alwaysRun = true) + public void setUpJvOptionPane() + { + JvOptionPane.setInteractiveMode(false); + JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION); + } + private AlignViewportI dnaView; private AlignViewportI proteinView; @@ -82,9 +93,9 @@ public class MappingUtilsTest /* * Check protein residue 12 maps to codon 5-7, 13 to codon 8-10 */ - SearchResults sr = MappingUtils.buildSearchResults(aseq1, 12, acfList); + SearchResultsI sr = MappingUtils.buildSearchResults(aseq1, 12, acfList); assertEquals(1, sr.getResults().size()); - Match m = sr.getResults().get(0); + SearchResultMatchI m = sr.getResults().get(0); assertEquals(seq1.getDatasetSequence(), m.getSequence()); assertEquals(5, m.getStart()); assertEquals(7, m.getEnd()); @@ -135,9 +146,9 @@ public class MappingUtilsTest /* * Check protein residue 8 maps to [6, 8, 9] */ - SearchResults sr = MappingUtils.buildSearchResults(aseq1, 8, acfList); + SearchResultsI sr = MappingUtils.buildSearchResults(aseq1, 8, acfList); assertEquals(2, sr.getResults().size()); - Match m = sr.getResults().get(0); + SearchResultMatchI m = sr.getResults().get(0); assertEquals(seq1.getDatasetSequence(), m.getSequence()); assertEquals(6, m.getStart()); assertEquals(6, m.getEnd()); @@ -198,10 +209,10 @@ public class MappingUtilsTest * viewport). */ AlignmentI cdna = loadAlignment(">Seq1\nACG\n>Seq2\nTGA\n>Seq3\nTAC\n", - "FASTA"); + FileFormat.Fasta); cdna.setDataset(null); AlignmentI protein = loadAlignment(">Seq1\nK\n>Seq2\nL\n>Seq3\nQ\n", - "FASTA"); + FileFormat.Fasta); protein.setDataset(null); AlignedCodonFrame acf = new AlignedCodonFrame(); MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 1 }, 3, 1); @@ -269,11 +280,11 @@ public class MappingUtilsTest * @return * @throws IOException */ - protected AlignmentI loadAlignment(final String data, String format) + protected AlignmentI loadAlignment(final String data, FileFormatI format) throws IOException { AlignmentI a = new FormatAdapter().readFile(data, - AppletFormatAdapter.PASTE, format); + DataSourceType.PASTE, format); a.setDataset(null); return a; } @@ -352,11 +363,11 @@ public class MappingUtilsTest */ AlignmentI cdna = loadAlignment(">Seq1/10-18\nAC-GctGtC-T\n" + ">Seq2/20-27\nTc-GA-G-T-Tc\n" + ">Seq3/30-38\nTtTT-AaCGg-\n", - "FASTA"); + FileFormat.Fasta); cdna.setDataset(null); AlignmentI protein = loadAlignment( ">Seq1/40-41\n-K-P\n>Seq2/50-51\nL--Q\n>Seq3/60-61\nG--S\n", - "FASTA"); + FileFormat.Fasta); protein.setDataset(null); // map first dna to first protein seq @@ -467,10 +478,11 @@ public class MappingUtilsTest * viewport). */ AlignmentI cdna = loadAlignment( - ">Seq1\nACGGCA\n>Seq2\nTGACAG\n>Seq3\nTACGTA\n", "FASTA"); + ">Seq1\nACGGCA\n>Seq2\nTGACAG\n>Seq3\nTACGTA\n", + FileFormat.Fasta); cdna.setDataset(null); AlignmentI protein = loadAlignment(">Seq1\nKA\n>Seq2\nLQ\n>Seq3\nQV\n", - "FASTA"); + FileFormat.Fasta); protein.setDataset(null); AlignedCodonFrame acf = new AlignedCodonFrame(); MapList map = new MapList(new int[] { 1, 6 }, new int[] { 1, 2 }, 3, 1); @@ -550,10 +562,10 @@ public class MappingUtilsTest */ AlignmentI cdna = loadAlignment( ">Seq1\nA-CG-GC--AT-CA\n>Seq2\n-TG-AC-AG-T-AT\n>Seq3\n-T--ACG-TAAT-G\n", - "FASTA"); + FileFormat.Fasta); cdna.setDataset(null); AlignmentI protein = loadAlignment( - ">Seq1\n-KA-S\n>Seq2\n--L-QY\n>Seq3\nQ-V-M\n", "FASTA"); + ">Seq1\n-KA-S\n>Seq2\n--L-QY\n>Seq3\nQ-V-M\n", FileFormat.Fasta); protein.setDataset(null); AlignedCodonFrame acf = new AlignedCodonFrame(); MapList map = new MapList(new int[] { 1, 9 }, new int[] { 1, 3 }, 3, 1); @@ -704,6 +716,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 +880,7 @@ public class MappingUtilsTest public void testMapColumnSelection_hiddenColumns() throws IOException { setupMappedAlignments(); - + ColumnSelection proteinSelection = new ColumnSelection(); /* @@ -800,8 +888,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 +904,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 +916,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 +928,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,7 +942,8 @@ 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))); @@ -862,6 +954,10 @@ public class MappingUtilsTest 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 }); @@ -870,6 +966,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" }) @@ -911,4 +1016,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)); + } + }