From df0035d38851d468e8f6991ad6ed1c8f6cce2610 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Thu, 22 Oct 2020 10:40:58 +0100 Subject: [PATCH] JAL-3763 test coverage for AlignedCodonFrame.markMappedRegion --- src/jalview/datamodel/AlignedCodonFrame.java | 7 +- src/jalview/datamodel/SearchResults.java | 6 +- test/jalview/datamodel/AlignedCodonFrameTest.java | 155 +++++++++++++++++---- 3 files changed, 139 insertions(+), 29 deletions(-) diff --git a/src/jalview/datamodel/AlignedCodonFrame.java b/src/jalview/datamodel/AlignedCodonFrame.java index 6e83fbb..049a5f4 100644 --- a/src/jalview/datamodel/AlignedCodonFrame.java +++ b/src/jalview/datamodel/AlignedCodonFrame.java @@ -433,7 +433,8 @@ public class AlignedCodonFrame /** * Add search results for regions in other sequences that translate or are - * translated from a particular position in seq + * translated from a particular position in seq (which may be an aligned or + * dataset sequence) * * @param seq * @param index @@ -445,6 +446,10 @@ public class AlignedCodonFrame SearchResultsI results) { SequenceI ds = seq.getDatasetSequence(); + if (ds == null) + { + ds = seq; + } for (SequenceToSequenceMapping ssm : mappings) { ssm.markMappedRegion(ds, index, results); diff --git a/src/jalview/datamodel/SearchResults.java b/src/jalview/datamodel/SearchResults.java index 7c3bba7..5c929fc 100755 --- a/src/jalview/datamodel/SearchResults.java +++ b/src/jalview/datamodel/SearchResults.java @@ -349,8 +349,10 @@ public class SearchResults implements SearchResultsI } /** - * Two SearchResults are considered equal if they contain the same matches in - * the same order. + * Two SearchResults are considered equal if they contain the same matches + * (Sequence, start position, end position) in the same order + * + * @see Match#equals(Object) */ @Override public boolean equals(Object obj) diff --git a/test/jalview/datamodel/AlignedCodonFrameTest.java b/test/jalview/datamodel/AlignedCodonFrameTest.java index e3fa027..0cfc380 100644 --- a/test/jalview/datamodel/AlignedCodonFrameTest.java +++ b/test/jalview/datamodel/AlignedCodonFrameTest.java @@ -100,52 +100,67 @@ public class AlignedCodonFrameTest public void testGetMappedRegion() { // introns lower case, exons upper case - final Sequence seq1 = new Sequence("Seq1", "c-G-TA-gC-gT-T"); - seq1.createDatasetSequence(); - final Sequence seq2 = new Sequence("Seq2", "-TA-gG-Gg-CG-a"); - seq2.createDatasetSequence(); + final Sequence dna1 = new Sequence("Seq1/10-18", "c-G-TA-gC-gT-T"); + dna1.createDatasetSequence(); + final Sequence dna2 = new Sequence("Seq2/20-28", "-TA-gG-Gg-CG-a"); + dna2.createDatasetSequence(); - final Sequence aseq1 = new Sequence("Seq1", "-P-R"); - aseq1.createDatasetSequence(); - final Sequence aseq2 = new Sequence("Seq2", "-LY-Q"); - aseq2.createDatasetSequence(); + final Sequence pep1 = new Sequence("Seq1/3-4", "-P-R"); + pep1.createDatasetSequence(); + final Sequence pep2 = new Sequence("Seq2/7-9", "-LY-Q"); + pep2.createDatasetSequence(); /* * First with no mappings */ AlignedCodonFrame acf = new AlignedCodonFrame(); - assertNull(acf.getMappedRegion(seq1, aseq1, 1)); + assertNull(acf.getMappedRegion(dna1, pep1, 3)); /* * Set up the mappings for the exons (upper-case bases) * Note residue Q is unmapped */ - MapList map = new MapList(new int[] { 2, 4, 6, 6, 8, 9 }, new int[] { - 1, 2 }, 3, 1); - acf.addMap(seq1.getDatasetSequence(), aseq1.getDatasetSequence(), map); - map = new MapList(new int[] { 1, 2, 4, 5, 7, 8 }, new int[] { 1, 2 }, + MapList map1 = new MapList(new int[] { 11, 13, 15, 15, 17, 18 }, new int[] { + 3, 4 }, 3, 1); + acf.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(), map1); + MapList map2 = new MapList(new int[] { 20, 21, 23, 24, 26, 27 }, new int[] { 7, 9 }, 3, 1); - acf.addMap(seq2.getDatasetSequence(), aseq2.getDatasetSequence(), map); + acf.addMap(dna2.getDatasetSequence(), pep2.getDatasetSequence(), map2); - assertArrayEquals(new int[] { 2, 4 }, - acf.getMappedRegion(seq1, aseq1, 1)); - assertArrayEquals(new int[] { 6, 6, 8, 9 }, - acf.getMappedRegion(seq1, aseq1, 2)); - assertArrayEquals(new int[] { 1, 2, 4, 4 }, - acf.getMappedRegion(seq2, aseq2, 1)); - assertArrayEquals(new int[] { 5, 5, 7, 8 }, - acf.getMappedRegion(seq2, aseq2, 2)); + /* + * get codon positions for peptide position + */ + assertArrayEquals(new int[] { 11, 13 }, + acf.getMappedRegion(dna1, pep1, 3)); + assertArrayEquals(new int[] { 15, 15, 17, 18 }, + acf.getMappedRegion(dna1, pep1, 4)); + assertArrayEquals(new int[] { 20, 21, 23, 23 }, + acf.getMappedRegion(dna2, pep2, 7)); + assertArrayEquals(new int[] { 24, 24, 26, 27 }, + acf.getMappedRegion(dna2, pep2, 8)); /* - * No mapping from seq2 to Q + * No mapping from dna2 to Q */ - assertNull(acf.getMappedRegion(seq2, aseq2, 3)); + assertNull(acf.getMappedRegion(dna2, pep2, 9)); /* - * No mapping from sequence 1 to sequence 2 + * No mapping from dna1 to pep2 + */ + assertNull(acf.getMappedRegion(dna1, pep2, 7)); + + /* + * get peptide position for codon position */ - assertNull(acf.getMappedRegion(seq1, aseq2, 1)); + assertArrayEquals(new int[] { 3, 3 }, + acf.getMappedRegion(pep1, dna1, 11)); + assertArrayEquals(new int[] { 3, 3 }, + acf.getMappedRegion(pep1, dna1, 12)); + assertArrayEquals(new int[] { 3, 3 }, + acf.getMappedRegion(pep1, dna1, 13)); + assertNull(acf.getMappedRegion(pep1, dna1, 14)); // intron base, not mapped + } @Test(groups = { "Functional" }) @@ -545,4 +560,92 @@ public class AlignedCodonFrameTest mapping = acf.getCoveringMapping(cds2, pep); assertNotNull(mapping); } + + /** + * Test the method that adds mapped positions to SearchResults + */ + @Test(groups = { "Functional" }) + public void testMarkMappedRegion() + { + // introns lower case, exons upper case + final Sequence dna1 = new Sequence("Seq1/10-18", "c-G-TA-gC-gT-T"); + dna1.createDatasetSequence(); + final Sequence dna2 = new Sequence("Seq2/20-28", "-TA-gG-Gg-CG-a"); + dna2.createDatasetSequence(); + + final Sequence pep1 = new Sequence("Seq1/3-4", "-P-R"); + pep1.createDatasetSequence(); + final Sequence pep2 = new Sequence("Seq2/7-9", "-LY-Q"); + pep2.createDatasetSequence(); + + /* + * First with no mappings + */ + AlignedCodonFrame acf = new AlignedCodonFrame(); + SearchResults sr = new SearchResults(); + acf.markMappedRegion(dna1, 12, sr); + assertTrue(sr.isEmpty()); + + /* + * Set up the mappings for the exons (upper-case bases) + * Note residue Q is unmapped + */ + MapList map1 = new MapList(new int[] { 11, 13, 15, 15, 17, 18 }, new int[] { + 3, 4 }, 3, 1); + acf.addMap(dna1.getDatasetSequence(), pep1.getDatasetSequence(), map1); + MapList map2 = new MapList(new int[] { 20, 21, 23, 24, 26, 27 }, new int[] { 7, 8 }, + 3, 1); + acf.addMap(dna2.getDatasetSequence(), pep2.getDatasetSequence(), map2); + + /* + * intron bases are not mapped + */ + acf.markMappedRegion(dna1, 10, sr); + assertTrue(sr.isEmpty()); + + /* + * Q is not mapped + */ + acf.markMappedRegion(pep2, 9, sr); + assertTrue(sr.isEmpty()); + + /* + * mark peptide position for exon position (of aligned sequence) + */ + acf.markMappedRegion(dna1, 11, sr); + SearchResults expected = new SearchResults(); + expected.addResult(pep1.getDatasetSequence(), 3, 3); + assertEquals(sr, expected); + + /* + * mark peptide position for exon position of dataset sequence - same result + */ + sr = new SearchResults(); + acf.markMappedRegion(dna1.getDatasetSequence(), 11, sr); + assertEquals(sr, expected); + + /* + * marking the same position a second time should not create a duplicate match + */ + acf.markMappedRegion(dna1.getDatasetSequence(), 12, sr); + assertEquals(sr, expected); + + /* + * mark exon positions for peptide position (of aligned sequence) + */ + sr = new SearchResults(); + acf.markMappedRegion(pep2, 7, sr); // codon positions 20, 21, 23 + expected = new SearchResults(); + expected.addResult(dna2.getDatasetSequence(), 20, 21); + expected.addResult(dna2.getDatasetSequence(), 23, 23); + assertEquals(sr, expected); + + /* + * add another codon to the same SearchResults + */ + acf.markMappedRegion(pep1.getDatasetSequence(), 4, sr); // codon positions 15, 17, 18 + expected.addResult(dna1.getDatasetSequence(), 15, 15); + expected.addResult(dna1.getDatasetSequence(), 17, 18); + assertEquals(sr, expected); + } } -- 1.7.10.2