X-Git-Url: http://source.jalview.org/gitweb/?p=jalview.git;a=blobdiff_plain;f=test%2Fjalview%2Futil%2FMappingUtilsTest.java;h=08673aeceed6777fe9125b093369f70ecfa6c08e;hp=6780af7b8c3b42ae51c45fbfa967d2c636f36e30;hb=9c16178c0601ea3169d7a5d5d623dafa45c54f85;hpb=7d67fb613ec026dc9a265e351e7fab542e3f1d61 diff --git a/test/jalview/util/MappingUtilsTest.java b/test/jalview/util/MappingUtilsTest.java index 6780af7..08673ae 100644 --- a/test/jalview/util/MappingUtilsTest.java +++ b/test/jalview/util/MappingUtilsTest.java @@ -22,10 +22,24 @@ package jalview.util; import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertFalse; +import static org.testng.AssertJUnit.assertNull; import static org.testng.AssertJUnit.assertSame; import static org.testng.AssertJUnit.assertTrue; +import static org.testng.AssertJUnit.fail; +import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals; + +import java.awt.Color; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; + +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; import jalview.api.AlignViewportI; +import jalview.bin.Cache; import jalview.commands.EditCommand; import jalview.commands.EditCommand.Action; import jalview.commands.EditCommand.Edit; @@ -33,27 +47,34 @@ import jalview.datamodel.AlignedCodonFrame; import jalview.datamodel.Alignment; import jalview.datamodel.AlignmentI; import jalview.datamodel.ColumnSelection; +import jalview.datamodel.HiddenColumns; 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.gui.JvOptionPane; import jalview.io.DataSourceType; import jalview.io.FileFormat; import jalview.io.FileFormatI; import jalview.io.FormatAdapter; -import java.awt.Color; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import org.testng.annotations.Test; - public class MappingUtilsTest { + @BeforeClass(alwaysRun = true) + public void setUp() + { + Cache.initLogger(); + } + + @BeforeClass(alwaysRun = true) + public void setUpJvOptionPane() + { + JvOptionPane.setInteractiveMode(false); + JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION); + } + private AlignViewportI dnaView; private AlignViewportI proteinView; @@ -77,8 +98,9 @@ public class MappingUtilsTest MapList map = new MapList(new int[] { 5, 10 }, new int[] { 12, 13 }, 3, 1); acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map); - List acfList = Arrays.asList(new AlignedCodonFrame[] - { acf }); + List acfList = Arrays + .asList(new AlignedCodonFrame[] + { acf }); /* * Check protein residue 12 maps to codon 5-7, 13 to codon 8-10 @@ -127,11 +149,14 @@ public class MappingUtilsTest * Map dna bases [6, 8, 9], [11, 13, 115] to protein residues 8 and 9 */ AlignedCodonFrame acf = new AlignedCodonFrame(); - MapList map = new MapList(new int[] { 6, 6, 8, 9, 11, 11, 13, 13, 15, - 15 }, new int[] { 8, 9 }, 3, 1); + MapList map = new MapList( + new int[] + { 6, 6, 8, 9, 11, 11, 13, 13, 15, 15 }, new int[] { 8, 9 }, 3, + 1); acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map); - List acfList = Arrays.asList(new AlignedCodonFrame[] - { acf }); + List acfList = Arrays + .asList(new AlignedCodonFrame[] + { acf }); /* * Check protein residue 8 maps to [6, 8, 9] @@ -171,8 +196,8 @@ public class MappingUtilsTest for (int i = 5; i < 18; i++) { sr = MappingUtils.buildSearchResults(seq1, i, acfList); - int residue = (i == 6 || i == 8 || i == 9) ? 8 : (i == 11 || i == 13 - || i == 15 ? 9 : 0); + int residue = (i == 6 || i == 8 || i == 9) ? 8 + : (i == 11 || i == 13 || i == 15 ? 9 : 0); if (residue == 0) { assertEquals(0, sr.getResults().size()); @@ -208,18 +233,19 @@ public class MappingUtilsTest MapList map = new MapList(new int[] { 1, 3 }, new int[] { 1, 1 }, 3, 1); for (int seq = 0; seq < 3; seq++) { - acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), protein - .getSequenceAt(seq).getDatasetSequence(), map); + acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), + protein.getSequenceAt(seq).getDatasetSequence(), map); } - List acfList = Arrays.asList(new AlignedCodonFrame[] - { acf }); + List acfList = Arrays + .asList(new AlignedCodonFrame[] + { acf }); - AlignViewportI dnaView = new AlignViewport(cdna); - AlignViewportI proteinView = new AlignViewport(protein); + AlignViewportI theDnaView = new AlignViewport(cdna); + AlignViewportI theProteinView = new AlignViewport(protein); protein.setCodonFrames(acfList); /* - * Select Seq1 and Seq3 in the protein (startRes=endRes=0) + * Select Seq1 and Seq3 in the protein */ SequenceGroup sg = new SequenceGroup(); sg.setColourText(true); @@ -227,12 +253,13 @@ public class MappingUtilsTest sg.setOutlineColour(Color.LIGHT_GRAY); sg.addSequence(protein.getSequenceAt(0), false); sg.addSequence(protein.getSequenceAt(2), false); + sg.setEndRes(protein.getWidth() - 1); /* * Verify the mapped sequence group in dna */ SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg, - proteinView, dnaView); + theProteinView, theDnaView); assertTrue(mappedGroup.getColourText()); assertSame(sg.getIdColour(), mappedGroup.getIdColour()); assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour()); @@ -240,7 +267,7 @@ public class MappingUtilsTest assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0)); assertSame(cdna.getSequenceAt(2), mappedGroup.getSequences().get(1)); assertEquals(0, mappedGroup.getStartRes()); - assertEquals(2, mappedGroup.getEndRes()); + assertEquals(2, mappedGroup.getEndRes()); // 3 columns (1 codon) /* * Verify mapping sequence group from dna to protein @@ -250,7 +277,7 @@ public class MappingUtilsTest sg.addSequence(cdna.getSequenceAt(0), false); sg.setStartRes(0); sg.setEndRes(2); - mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView); + mappedGroup = MappingUtils.mapSequenceGroup(sg, theDnaView, theProteinView); assertTrue(mappedGroup.getColourText()); assertSame(sg.getIdColour(), mappedGroup.getIdColour()); assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour()); @@ -273,8 +300,8 @@ public class MappingUtilsTest protected AlignmentI loadAlignment(final String data, FileFormatI format) throws IOException { - AlignmentI a = new FormatAdapter().readFile(data, - DataSourceType.PASTE, format); + AlignmentI a = new FormatAdapter().readFile(data, DataSourceType.PASTE, + format); a.setDataset(null); return a; } @@ -290,51 +317,62 @@ public class MappingUtilsTest setupMappedAlignments(); ColumnSelection colsel = new ColumnSelection(); + HiddenColumns hidden = new HiddenColumns(); /* * Column 0 in protein picks up Seq2/L, Seq3/G which map to cols 0-4 and 0-3 * in dna respectively, overall 0-4 */ colsel.addElement(0); - ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, - proteinView, dnaView); + ColumnSelection cs = new ColumnSelection(); + HiddenColumns hs = new HiddenColumns(); + MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView, + cs, hs); assertEquals("[0, 1, 2, 3, 4]", cs.getSelected().toString()); /* * Column 1 in protein picks up Seq1/K which maps to cols 0-3 in dna */ + cs.clear(); colsel.clear(); colsel.addElement(1); - cs = MappingUtils.mapColumnSelection(colsel, proteinView, dnaView); + MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView, + cs, hs); assertEquals("[0, 1, 2, 3]", cs.getSelected().toString()); /* * Column 2 in protein picks up gaps only - no mapping */ + cs.clear(); colsel.clear(); colsel.addElement(2); - cs = MappingUtils.mapColumnSelection(colsel, proteinView, dnaView); + MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView, + cs, hs); assertEquals("[]", cs.getSelected().toString()); /* * Column 3 in protein picks up Seq1/P, Seq2/Q, Seq3/S which map to columns * 6-9, 6-10, 5-8 respectively, overall to 5-10 */ + cs.clear(); colsel.clear(); colsel.addElement(3); - cs = MappingUtils.mapColumnSelection(colsel, proteinView, dnaView); + MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView, + cs, hs); assertEquals("[5, 6, 7, 8, 9, 10]", cs.getSelected().toString()); /* * Combine selection of columns 1 and 3 to get a discontiguous mapped * selection */ + cs.clear(); colsel.clear(); colsel.addElement(1); colsel.addElement(3); - cs = MappingUtils.mapColumnSelection(colsel, proteinView, dnaView); - assertEquals("[0, 1, 2, 3, 5, 6, 7, 8, 9, 10]", cs.getSelected() - .toString()); + MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView, + cs, hs); + assertEquals("[0, 1, 2, 3, 5, 6, 7, 8, 9, 10]", + cs.getSelected().toString()); } /** @@ -363,23 +401,27 @@ public class MappingUtilsTest // map first dna to first protein seq AlignedCodonFrame acf = new AlignedCodonFrame(); MapList map = new MapList(new int[] { 10, 12, 15, 15, 17, 18 }, - new int[] { 40, 41 }, 3, 1); - acf.addMap(cdna.getSequenceAt(0).getDatasetSequence(), protein - .getSequenceAt(0).getDatasetSequence(), map); + new int[] + { 40, 41 }, 3, 1); + acf.addMap(cdna.getSequenceAt(0).getDatasetSequence(), + protein.getSequenceAt(0).getDatasetSequence(), map); // map second dna to second protein seq - map = new MapList(new int[] { 20, 20, 22, 23, 24, 26 }, new int[] { 50, - 51 }, 3, 1); - acf.addMap(cdna.getSequenceAt(1).getDatasetSequence(), protein - .getSequenceAt(1).getDatasetSequence(), map); + map = new MapList(new int[] { 20, 20, 22, 23, 24, 26 }, + new int[] + { 50, 51 }, 3, 1); + acf.addMap(cdna.getSequenceAt(1).getDatasetSequence(), + protein.getSequenceAt(1).getDatasetSequence(), map); // map third dna to third protein seq - map = new MapList(new int[] { 30, 30, 32, 34, 36, 37 }, new int[] { 60, - 61 }, 3, 1); - acf.addMap(cdna.getSequenceAt(2).getDatasetSequence(), protein - .getSequenceAt(2).getDatasetSequence(), map); - List acfList = Arrays.asList(new AlignedCodonFrame[] - { acf }); + map = new MapList(new int[] { 30, 30, 32, 34, 36, 37 }, + new int[] + { 60, 61 }, 3, 1); + acf.addMap(cdna.getSequenceAt(2).getDatasetSequence(), + protein.getSequenceAt(2).getDatasetSequence(), map); + List acfList = Arrays + .asList(new AlignedCodonFrame[] + { acf }); dnaView = new AlignViewport(cdna); proteinView = new AlignViewport(protein); @@ -397,14 +439,17 @@ public class MappingUtilsTest setupMappedAlignments(); ColumnSelection colsel = new ColumnSelection(); + HiddenColumns hidden = new HiddenColumns(); /* * Column 0 in dna picks up first bases which map to residue 1, columns 0-1 * in protein. */ + ColumnSelection cs = new ColumnSelection(); + HiddenColumns hs = new HiddenColumns(); colsel.addElement(0); - ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, dnaView, - proteinView); + MappingUtils.mapColumnSelection(colsel, hidden, dnaView, proteinView, + cs, hs); assertEquals("[0, 1]", cs.getSelected().toString()); /* @@ -414,7 +459,9 @@ public class MappingUtilsTest colsel.addElement(3); colsel.addElement(4); colsel.addElement(5); - cs = MappingUtils.mapColumnSelection(colsel, dnaView, proteinView); + cs.clear(); + MappingUtils.mapColumnSelection(colsel, hidden, dnaView, proteinView, + cs, hs); assertEquals("[0, 1, 3]", cs.getSelected().toString()); } @@ -422,8 +469,10 @@ public class MappingUtilsTest public void testMapColumnSelection_null() throws IOException { setupMappedAlignments(); - ColumnSelection cs = MappingUtils.mapColumnSelection(null, dnaView, - proteinView); + ColumnSelection cs = new ColumnSelection(); + HiddenColumns hs = new HiddenColumns(); + MappingUtils.mapColumnSelection(null, null, dnaView, proteinView, cs, + hs); assertTrue("mapped selection not empty", cs.getSelected().isEmpty()); } @@ -435,24 +484,21 @@ public class MappingUtilsTest public void testFlattenRanges() { assertEquals("[1, 2, 3, 4]", - Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 4 }))); - assertEquals( - "[1, 2, 3, 4]", - Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 2, 3, - 4 }))); - assertEquals( - "[1, 2, 3, 4]", - Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 1, 2, - 2, 3, 3, 4, 4 }))); - assertEquals( - "[1, 2, 3, 4, 7, 8, 9, 12]", - Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 4, 7, - 9, 12, 12 }))); + Arrays.toString(MappingUtils.flattenRanges(new int[] + { 1, 4 }))); + assertEquals("[1, 2, 3, 4]", + Arrays.toString(MappingUtils.flattenRanges(new int[] + { 1, 2, 3, 4 }))); + assertEquals("[1, 2, 3, 4]", + Arrays.toString(MappingUtils.flattenRanges(new int[] + { 1, 1, 2, 2, 3, 3, 4, 4 }))); + assertEquals("[1, 2, 3, 4, 7, 8, 9, 12]", + Arrays.toString(MappingUtils.flattenRanges(new int[] + { 1, 4, 7, 9, 12, 12 }))); // trailing unpaired start position is ignored: - assertEquals( - "[1, 2, 3, 4, 7, 8, 9, 12]", - Arrays.toString(MappingUtils.flattenRanges(new int[] { 1, 4, 7, - 9, 12, 12, 15 }))); + assertEquals("[1, 2, 3, 4, 7, 8, 9, 12]", + Arrays.toString(MappingUtils.flattenRanges(new int[] + { 1, 4, 7, 9, 12, 12, 15 }))); } /** @@ -478,14 +524,15 @@ public class MappingUtilsTest MapList map = new MapList(new int[] { 1, 6 }, new int[] { 1, 2 }, 3, 1); for (int seq = 0; seq < 3; seq++) { - acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), protein - .getSequenceAt(seq).getDatasetSequence(), map); + acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), + protein.getSequenceAt(seq).getDatasetSequence(), map); } - List acfList = Arrays.asList(new AlignedCodonFrame[] - { acf }); + List acfList = Arrays + .asList(new AlignedCodonFrame[] + { acf }); - AlignViewportI dnaView = new AlignViewport(cdna); - AlignViewportI proteinView = new AlignViewport(protein); + AlignViewportI theDnaView = new AlignViewport(cdna); + AlignViewportI theProteinView = new AlignViewport(protein); protein.setCodonFrames(acfList); /* @@ -505,7 +552,7 @@ public class MappingUtilsTest * Verify the mapped sequence group in dna */ SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg, - proteinView, dnaView); + theProteinView, theDnaView); assertTrue(mappedGroup.getColourText()); assertSame(sg.getIdColour(), mappedGroup.getIdColour()); assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour()); @@ -526,7 +573,7 @@ public class MappingUtilsTest // select columns 2 and 3 in DNA which span protein columns 0 and 1 sg.setStartRes(2); sg.setEndRes(3); - mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView); + mappedGroup = MappingUtils.mapSequenceGroup(sg, theDnaView, theProteinView); assertTrue(mappedGroup.getColourText()); assertSame(sg.getIdColour(), mappedGroup.getIdColour()); assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour()); @@ -551,31 +598,32 @@ public class MappingUtilsTest * viewport). */ 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", + ">Cds11\nA-CG-GC--AT-CA\n>Cds2\n-TG-AC-AG-T-AT\n>Cds3\n-T--ACG-TAAT-G\n", FileFormat.Fasta); cdna.setDataset(null); AlignmentI protein = loadAlignment( - ">Seq1\n-KA-S\n>Seq2\n--L-QY\n>Seq3\nQ-V-M\n", FileFormat.Fasta); + ">Pep1\n-KA-S\n>Pep2\n--L-QY\n>Pep3\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); for (int seq = 0; seq < 3; seq++) { - acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), protein - .getSequenceAt(seq).getDatasetSequence(), map); + acf.addMap(cdna.getSequenceAt(seq).getDatasetSequence(), + protein.getSequenceAt(seq).getDatasetSequence(), map); } - List acfList = Arrays.asList(new AlignedCodonFrame[] - { acf }); + List acfList = Arrays + .asList(new AlignedCodonFrame[] + { acf }); - AlignViewportI dnaView = new AlignViewport(cdna); - AlignViewportI proteinView = new AlignViewport(protein); + AlignViewportI theDnaView = new AlignViewport(cdna); + AlignViewportI theProteinView = new AlignViewport(protein); protein.setCodonFrames(acfList); /* - * Select Seq1 and Seq2 in the protein, column 1 (K/-). Expect mapped - * sequence group to cover Seq1, columns 0-3 (ACG). Because the selection - * only includes a gap in Seq2 there is no mappable selection region in the - * corresponding DNA. + * Select Pep1 and Pep2 in the protein, column 1 (K/-). Expect mapped + * sequence group to cover Cds1, columns 0-3 (ACG). Although the selection + * only includes a gap in Cds2, mapped Cds2 is included with 'no columns' */ SequenceGroup sg = new SequenceGroup(); sg.setColourText(true); @@ -590,14 +638,15 @@ public class MappingUtilsTest * Verify the mapped sequence group in dna */ SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg, - proteinView, dnaView); + theProteinView, theDnaView); assertTrue(mappedGroup.getColourText()); assertSame(sg.getIdColour(), mappedGroup.getIdColour()); assertSame(sg.getOutlineColour(), mappedGroup.getOutlineColour()); - assertEquals(1, mappedGroup.getSequences().size()); + assertEquals(2, mappedGroup.getSequences().size()); assertSame(cdna.getSequenceAt(0), mappedGroup.getSequences().get(0)); - // Seq2 in protein has a gap in column 1 - ignored - // Seq1 has K which should map to columns 0-3 in Seq1 + assertSame(cdna.getSequenceAt(1), mappedGroup.getSequences().get(1)); + // Pep2 in protein has a gap in column 1 - doesn't map to any column + // Pep1 has K which should map to columns 0-3 in Cds1 assertEquals(0, mappedGroup.getStartRes()); assertEquals(3, mappedGroup.getEndRes()); @@ -607,7 +656,7 @@ public class MappingUtilsTest */ sg.setStartRes(2); sg.setEndRes(4); - mappedGroup = MappingUtils.mapSequenceGroup(sg, proteinView, dnaView); + mappedGroup = MappingUtils.mapSequenceGroup(sg, theProteinView, theDnaView); assertEquals(1, mappedGroup.getStartRes()); assertEquals(13, mappedGroup.getEndRes()); @@ -620,19 +669,19 @@ public class MappingUtilsTest // select columns 4,5 - includes Seq1:codon2 (A) only sg.setStartRes(4); sg.setEndRes(5); - mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView); + mappedGroup = MappingUtils.mapSequenceGroup(sg, theDnaView, theProteinView); assertEquals(2, mappedGroup.getStartRes()); assertEquals(2, mappedGroup.getEndRes()); // add Seq2 to dna selection cols 4-5 include codons 1 and 2 (LQ) sg.addSequence(cdna.getSequenceAt(1), false); - mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView); + mappedGroup = MappingUtils.mapSequenceGroup(sg, theDnaView, theProteinView); assertEquals(2, mappedGroup.getStartRes()); assertEquals(4, mappedGroup.getEndRes()); // add Seq3 to dna selection cols 4-5 include codon 1 (Q) sg.addSequence(cdna.getSequenceAt(2), false); - mappedGroup = MappingUtils.mapSequenceGroup(sg, dnaView, proteinView); + mappedGroup = MappingUtils.mapSequenceGroup(sg, theDnaView, theProteinView); assertEquals(0, mappedGroup.getStartRes()); assertEquals(4, mappedGroup.getEndRes()); } @@ -660,7 +709,7 @@ public class MappingUtilsTest AlignedCodonFrame acf3 = new AlignedCodonFrame(); acf3.addMap(seq3.getDatasetSequence(), seq1.getDatasetSequence(), map); - List mappings = new ArrayList(); + List mappings = new ArrayList<>(); mappings.add(acf1); mappings.add(acf2); mappings.add(acf3); @@ -668,8 +717,8 @@ public class MappingUtilsTest /* * Seq1 has three mappings */ - List result = MappingUtils.findMappingsForSequence( - seq1, mappings); + List result = MappingUtils + .findMappingsForSequence(seq1, mappings); assertEquals(3, result.size()); assertTrue(result.contains(acf1)); assertTrue(result.contains(acf2)); @@ -735,7 +784,7 @@ public class MappingUtilsTest AlignedCodonFrame acf4 = new AlignedCodonFrame(); acf4.addMap(seq3.getDatasetSequence(), seq4.getDatasetSequence(), map); - List mappings = new ArrayList(); + List mappings = new ArrayList<>(); mappings.add(acf1); mappings.add(acf2); mappings.add(acf3); @@ -746,22 +795,22 @@ public class MappingUtilsTest */ List result = MappingUtils .findMappingsForSequenceAndOthers(null, mappings, - Arrays.asList(new SequenceI[] { seq1, seq2 })); + Arrays.asList(new SequenceI[] + { seq1, seq2 })); assertTrue(result.isEmpty()); result = MappingUtils.findMappingsForSequenceAndOthers(seq1, null, - Arrays.asList(new SequenceI[] { seq1, seq2 })); + 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() })); + 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)); @@ -790,9 +839,10 @@ public class MappingUtilsTest dna.createDatasetSequence(); protein.createDatasetSequence(); AlignedCodonFrame acf = new AlignedCodonFrame(); - MapList map = new MapList(new int[] { 8, 16 }, new int[] { 5, 7 }, 3, 1); + MapList map = new MapList(new int[] { 8, 16 }, new int[] { 5, 7 }, 3, + 1); acf.addMap(dna.getDatasetSequence(), protein.getDatasetSequence(), map); - List mappings = new ArrayList(); + List mappings = new ArrayList<>(); mappings.add(acf); AlignmentI prot = new Alignment(new SequenceI[] { protein }); @@ -805,7 +855,8 @@ public class MappingUtilsTest */ EditCommand ec = new EditCommand(); final Edit edit = ec.new Edit(Action.INSERT_GAP, - new SequenceI[] { protein }, 4, 2, '-'); + new SequenceI[] + { protein }, 4, 2, '-'); ec.appendEdit(edit, prot, true, null); /* @@ -831,34 +882,29 @@ public class MappingUtilsTest public void testFlattenRanges_reverseStrand() { assertEquals("[4, 3, 2, 1]", - Arrays.toString(MappingUtils.flattenRanges(new int[] { 4, 1 }))); - assertEquals( - "[4, 3, 2, 1]", - Arrays.toString(MappingUtils.flattenRanges(new int[] { 4, 3, 2, - 1 }))); - assertEquals( - "[4, 3, 2, 1]", - Arrays.toString(MappingUtils.flattenRanges(new int[] { 4, 4, 3, - 3, 2, 2, 1, 1 }))); - assertEquals( - "[12, 9, 8, 7, 4, 3, 2, 1]", - Arrays.toString(MappingUtils.flattenRanges(new int[] { 12, 12, - 9, 7, 4, 1 }))); + Arrays.toString(MappingUtils.flattenRanges(new int[] + { 4, 1 }))); + assertEquals("[4, 3, 2, 1]", + Arrays.toString(MappingUtils.flattenRanges(new int[] + { 4, 3, 2, 1 }))); + assertEquals("[4, 3, 2, 1]", + Arrays.toString(MappingUtils.flattenRanges(new int[] + { 4, 4, 3, 3, 2, 2, 1, 1 }))); + assertEquals("[12, 9, 8, 7, 4, 3, 2, 1]", + Arrays.toString(MappingUtils.flattenRanges(new int[] + { 12, 12, 9, 7, 4, 1 }))); // forwards and backwards anyone? - assertEquals( - "[4, 5, 6, 3, 2, 1]", - Arrays.toString(MappingUtils.flattenRanges(new int[] { 4, 6, 3, - 1 }))); + assertEquals("[4, 5, 6, 3, 2, 1]", + Arrays.toString(MappingUtils.flattenRanges(new int[] + { 4, 6, 3, 1 }))); // backwards and forwards - assertEquals( - "[3, 2, 1, 4, 5, 6]", - Arrays.toString(MappingUtils.flattenRanges(new int[] { 3, 1, 4, - 6 }))); + assertEquals("[3, 2, 1, 4, 5, 6]", + Arrays.toString(MappingUtils.flattenRanges(new int[] + { 3, 1, 4, 6 }))); // trailing unpaired start position is ignored: - assertEquals( - "[12, 9, 8, 7, 4, 3, 2]", - Arrays.toString(MappingUtils.flattenRanges(new int[] { 12, 12, - 9, 7, 4, 2, 1 }))); + assertEquals("[12, 9, 8, 7, 4, 3, 2]", + Arrays.toString(MappingUtils.flattenRanges(new int[] + { 12, 12, 9, 7, 4, 2, 1 }))); } /** @@ -872,72 +918,83 @@ public class MappingUtilsTest setupMappedAlignments(); ColumnSelection proteinSelection = new ColumnSelection(); + HiddenColumns hiddenCols = new HiddenColumns(); /* * Column 0 in protein picks up Seq2/L, Seq3/G which map to cols 0-4 and 0-3 * in dna respectively, overall 0-4 */ - proteinSelection.hideColumns(0); - ColumnSelection dnaSelection = MappingUtils.mapColumnSelection( - proteinSelection, proteinView, dnaView); + proteinSelection.hideSelectedColumns(0, hiddenCols); + ColumnSelection dnaSelection = new ColumnSelection(); + HiddenColumns dnaHidden = new HiddenColumns(); + MappingUtils.mapColumnSelection(proteinSelection, hiddenCols, + proteinView, dnaView, dnaSelection, dnaHidden); assertEquals("[]", dnaSelection.getSelected().toString()); - List hidden = dnaSelection.getHiddenColumns(); - assertEquals(1, hidden.size()); - assertEquals("[0, 4]", Arrays.toString(hidden.get(0))); + Iterator regions = dnaHidden.iterator(); + assertEquals(1, dnaHidden.getNumberOfRegions()); + assertEquals("[0, 4]", Arrays.toString(regions.next())); /* * Column 1 in protein picks up Seq1/K which maps to cols 0-3 in dna */ - proteinSelection.revealAllHiddenColumns(); + dnaSelection = new ColumnSelection(); + dnaHidden = new HiddenColumns(); + hiddenCols.revealAllHiddenColumns(proteinSelection); // the unhidden columns are now marked selected! assertEquals("[0]", proteinSelection.getSelected().toString()); // deselect these or hideColumns will be expanded to include 0 proteinSelection.clear(); - proteinSelection.hideColumns(1); - dnaSelection = MappingUtils.mapColumnSelection(proteinSelection, - proteinView, dnaView); - hidden = dnaSelection.getHiddenColumns(); - assertEquals(1, hidden.size()); - assertEquals("[0, 3]", Arrays.toString(hidden.get(0))); + proteinSelection.hideSelectedColumns(1, hiddenCols); + MappingUtils.mapColumnSelection(proteinSelection, hiddenCols, + proteinView, dnaView, dnaSelection, dnaHidden); + regions = dnaHidden.iterator(); + assertEquals(1, dnaHidden.getNumberOfRegions()); + assertEquals("[0, 3]", Arrays.toString(regions.next())); /* * Column 2 in protein picks up gaps only - no mapping */ - proteinSelection.revealAllHiddenColumns(); + dnaSelection = new ColumnSelection(); + dnaHidden = new HiddenColumns(); + hiddenCols.revealAllHiddenColumns(proteinSelection); proteinSelection.clear(); - proteinSelection.hideColumns(2); - dnaSelection = MappingUtils.mapColumnSelection(proteinSelection, - proteinView, dnaView); - assertTrue(dnaSelection.getHiddenColumns().isEmpty()); + proteinSelection.hideSelectedColumns(2, hiddenCols); + MappingUtils.mapColumnSelection(proteinSelection, hiddenCols, + proteinView, dnaView, dnaSelection, dnaHidden); + assertEquals(0, dnaHidden.getNumberOfRegions()); /* * Column 3 in protein picks up Seq1/P, Seq2/Q, Seq3/S which map to columns * 6-9, 6-10, 5-8 respectively, overall to 5-10 */ - proteinSelection.revealAllHiddenColumns(); + dnaSelection = new ColumnSelection(); + dnaHidden = new HiddenColumns(); + hiddenCols.revealAllHiddenColumns(proteinSelection); proteinSelection.clear(); - proteinSelection.hideColumns(3); // 5-10 hidden in dna + proteinSelection.hideSelectedColumns(3, hiddenCols); // 5-10 hidden in dna proteinSelection.addElement(1); // 0-3 selected in dna - dnaSelection = MappingUtils.mapColumnSelection(proteinSelection, - proteinView, dnaView); + MappingUtils.mapColumnSelection(proteinSelection, hiddenCols, + proteinView, dnaView, dnaSelection, dnaHidden); assertEquals("[0, 1, 2, 3]", dnaSelection.getSelected().toString()); - hidden = dnaSelection.getHiddenColumns(); - assertEquals(1, hidden.size()); - assertEquals("[5, 10]", Arrays.toString(hidden.get(0))); + regions = dnaHidden.iterator(); + assertEquals(1, dnaHidden.getNumberOfRegions()); + assertEquals("[5, 10]", Arrays.toString(regions.next())); /* * Combine hiding columns 1 and 3 to get discontiguous hidden columns */ - proteinSelection.revealAllHiddenColumns(); + dnaSelection = new ColumnSelection(); + dnaHidden = new HiddenColumns(); + hiddenCols.revealAllHiddenColumns(proteinSelection); proteinSelection.clear(); - proteinSelection.hideColumns(1); - proteinSelection.hideColumns(3); - 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))); + proteinSelection.hideSelectedColumns(1, hiddenCols); + proteinSelection.hideSelectedColumns(3, hiddenCols); + MappingUtils.mapColumnSelection(proteinSelection, hiddenCols, + proteinView, dnaView, dnaSelection, dnaHidden); + regions = dnaHidden.iterator(); + assertEquals(2, dnaHidden.getNumberOfRegions()); + assertEquals("[0, 3]", Arrays.toString(regions.next())); + assertEquals("[5, 10]", Arrays.toString(regions.next())); } @Test(groups = { "Functional" }) @@ -948,7 +1005,7 @@ public class MappingUtilsTest /* * [start, end] ranges */ - List ranges = new ArrayList(); + List ranges = new ArrayList<>(); assertEquals(0, MappingUtils.getLength(ranges)); ranges.add(new int[] { 1, 1 }); assertEquals(1, MappingUtils.getLength(ranges)); @@ -971,7 +1028,7 @@ public class MappingUtilsTest public void testContains() { assertFalse(MappingUtils.contains(null, 1)); - List ranges = new ArrayList(); + List ranges = new ArrayList<>(); assertFalse(MappingUtils.contains(ranges, 1)); ranges.add(new int[] { 1, 4 }); @@ -1109,4 +1166,316 @@ public class MappingUtilsTest assertEquals("[12, 11, 8, 4]", Arrays.toString(ranges)); } + @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 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 testListToArray() + { + List ranges = new ArrayList<>(); + + int[] result = MappingUtils.listToArray(ranges); + assertEquals(result.length, 0); + ranges.add(new int[] {24, 12}); + result = MappingUtils.listToArray(ranges); + assertEquals(result.length, 2); + assertEquals(result[0], 24); + assertEquals(result[1], 12); + ranges.add(new int[] {-7, 30}); + result = MappingUtils.listToArray(ranges); + assertEquals(result.length, 4); + assertEquals(result[0], 24); + assertEquals(result[1], 12); + assertEquals(result[2], -7); + assertEquals(result[3], 30); + try + { + MappingUtils.listToArray(null); + fail("Expected exception"); + } catch (NullPointerException e) + { + // expected + } + } + + /** + * Test mapping a sequence group where sequences in and outside the group + * share a dataset sequence (e.g. alternative CDS for the same gene) + *

+ * 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 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 + } + + @Test(groups = "Functional") + public void testFindOverlap() + { + List 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); + } }