From a538d2b13ce875998d2914baf49772da672a1f61 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Thu, 17 Sep 2020 11:19:24 +0100 Subject: [PATCH] JAL-3700 additions and corrections to unit tests (trivial) Conflicts: test/jalview/util/MappingUtilsTest.java --- src/jalview/datamodel/AlignedCodonFrame.java | 8 +- test/jalview/datamodel/AlignedCodonFrameTest.java | 65 ++- test/jalview/util/MappingUtilsTest.java | 478 +++++++++++++-------- 3 files changed, 372 insertions(+), 179 deletions(-) diff --git a/src/jalview/datamodel/AlignedCodonFrame.java b/src/jalview/datamodel/AlignedCodonFrame.java index 25f1c27..96c9792 100644 --- a/src/jalview/datamodel/AlignedCodonFrame.java +++ b/src/jalview/datamodel/AlignedCodonFrame.java @@ -20,13 +20,13 @@ */ package jalview.datamodel; -import jalview.util.MapList; -import jalview.util.MappingUtils; - import java.util.AbstractList; import java.util.ArrayList; import java.util.List; +import jalview.util.MapList; +import jalview.util.MappingUtils; + /** * Stores mapping between the columns of a protein alignment and a DNA alignment * and a list of individual codon to amino acid mappings between sequences. @@ -163,7 +163,7 @@ public class AlignedCodonFrame } /* - * check that each mapped range lieS with the sequence range + * check that each mapped range lies within the sequence range * (necessary for circular CDS - example EMBL:J03321:AAA91567) * and mapped length covers (at least) sequence length */ diff --git a/test/jalview/datamodel/AlignedCodonFrameTest.java b/test/jalview/datamodel/AlignedCodonFrameTest.java index fb4073a..e3fa027 100644 --- a/test/jalview/datamodel/AlignedCodonFrameTest.java +++ b/test/jalview/datamodel/AlignedCodonFrameTest.java @@ -22,20 +22,22 @@ package jalview.datamodel; import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertFalse; +import static org.testng.AssertJUnit.assertNotNull; import static org.testng.AssertJUnit.assertNull; import static org.testng.AssertJUnit.assertSame; import static org.testng.AssertJUnit.assertTrue; import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals; -import jalview.gui.JvOptionPane; -import jalview.util.MapList; - import java.util.Arrays; import java.util.List; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; +import jalview.datamodel.AlignedCodonFrame.SequenceToSequenceMapping; +import jalview.gui.JvOptionPane; +import jalview.util.MapList; + public class AlignedCodonFrameTest { @@ -486,4 +488,61 @@ public class AlignedCodonFrameTest assertEquals(1, acf.getMappingsFromSequence(seq1).size()); assertSame(before, acf.getMappingsFromSequence(seq1).get(0)); } + + @Test(groups = { "Functional" }) + public void testGetCoveringMapping() + { + SequenceI dna = new Sequence("dna", "acttcaATGGCGGACtaattt"); + SequenceI cds = new Sequence("cds/7-15", "ATGGCGGAC"); + cds.setDatasetSequence(dna); + SequenceI pep = new Sequence("pep", "MAD"); + + /* + * with null argument or no mappings + */ + AlignedCodonFrame acf = new AlignedCodonFrame(); + assertNull(acf.getCoveringMapping(null, null)); + assertNull(acf.getCoveringMapping(dna, null)); + assertNull(acf.getCoveringMapping(null, pep)); + assertNull(acf.getCoveringMapping(dna, pep)); + + /* + * with a non-covering mapping e.g. overlapping exon + */ + MapList map = new MapList(new int[] { 7, 9 }, new int[] { + 1, 1 }, 3, 1); + acf.addMap(dna, pep, map); + assertNull(acf.getCoveringMapping(dna, pep)); + + acf = new AlignedCodonFrame(); + MapList map2 = new MapList(new int[] { 13, 18 }, new int[] { + 2, 2 }, 3, 1); + acf.addMap(dna, pep, map2); + assertNull(acf.getCoveringMapping(dna, pep)); + + /* + * with a covering mapping from CDS (dataset) to protein + */ + acf = new AlignedCodonFrame(); + MapList map3 = new MapList(new int[] { 7, 15 }, new int[] { + 1, 3 }, 3, 1); + acf.addMap(dna, pep, map3); + assertNull(acf.getCoveringMapping(dna, pep)); + SequenceToSequenceMapping mapping = acf.getCoveringMapping(cds, pep); + assertNotNull(mapping); + + /* + * with a mapping that extends to stop codon + */ + acf = new AlignedCodonFrame(); + MapList map4 = new MapList(new int[] { 7, 18 }, new int[] { + 1, 3 }, 3, 1); + acf.addMap(dna, pep, map4); + assertNull(acf.getCoveringMapping(dna, pep)); + assertNull(acf.getCoveringMapping(cds, pep)); + SequenceI cds2 = new Sequence("cds/7-18", "ATGGCGGACtaa"); + cds2.setDatasetSequence(dna); + mapping = acf.getCoveringMapping(cds2, pep); + assertNotNull(mapping); + } } diff --git a/test/jalview/util/MappingUtilsTest.java b/test/jalview/util/MappingUtilsTest.java index dd789d6..813edf8 100644 --- a/test/jalview/util/MappingUtilsTest.java +++ b/test/jalview/util/MappingUtilsTest.java @@ -26,6 +26,16 @@ import static org.testng.AssertJUnit.assertSame; import static org.testng.AssertJUnit.assertTrue; import static org.testng.AssertJUnit.fail; +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.commands.EditCommand; import jalview.commands.EditCommand.Action; @@ -47,16 +57,6 @@ 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.Iterator; -import java.util.List; - -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; - public class MappingUtilsTest { @@ -90,8 +90,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 @@ -140,11 +141,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] @@ -184,8 +188,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()); @@ -221,18 +225,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); 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); @@ -240,6 +245,7 @@ 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 @@ -253,7 +259,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 @@ -286,8 +292,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; } @@ -332,8 +338,8 @@ public class MappingUtilsTest cs.clear(); colsel.clear(); colsel.addElement(2); - MappingUtils.mapColumnSelection(colsel, hidden, proteinView, - dnaView, cs, hs); + MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView, + cs, hs); assertEquals("[]", cs.getSelected().toString()); /* @@ -343,8 +349,8 @@ public class MappingUtilsTest cs.clear(); colsel.clear(); colsel.addElement(3); - MappingUtils.mapColumnSelection(colsel, hidden, proteinView, - dnaView, cs, hs); + MappingUtils.mapColumnSelection(colsel, hidden, proteinView, dnaView, + cs, hs); assertEquals("[5, 6, 7, 8, 9, 10]", cs.getSelected().toString()); /* @@ -355,10 +361,10 @@ public class MappingUtilsTest colsel.clear(); colsel.addElement(1); colsel.addElement(3); - MappingUtils.mapColumnSelection(colsel, hidden, proteinView, - dnaView, cs, hs); - 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()); } /** @@ -387,23 +393,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); @@ -466,24 +476,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 }))); } /** @@ -509,11 +516,12 @@ 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); @@ -586,17 +594,19 @@ public class MappingUtilsTest 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); + ">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); 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); @@ -699,8 +709,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)); @@ -777,22 +787,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)); @@ -821,7 +831,8 @@ 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<>(); mappings.add(acf); @@ -836,7 +847,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); /* @@ -862,34 +874,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 }))); } /** @@ -1157,85 +1164,115 @@ public class MappingUtilsTest /* * 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 })); + 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 })); + 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 })); + 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 })); + 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, 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 })); } @@ -1314,4 +1351,101 @@ public class MappingUtilsTest // 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) + * + * @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"); + + /* + * 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 proteinView = 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, + proteinView, 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, proteinView); + 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 + } } -- 1.7.10.2