From a31403382672ec59acdf56e095d6d0136199fb44 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Thu, 3 Dec 2015 16:26:51 +0000 Subject: [PATCH] JAL-653 handle reverse strand mapping in SearchResults matches --- src/jalview/datamodel/SearchResults.java | 18 ++++++++++++++++-- test/jalview/datamodel/SearchResultsTest.java | 23 +++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/jalview/datamodel/SearchResults.java b/src/jalview/datamodel/SearchResults.java index ad0e472..b9db461 100755 --- a/src/jalview/datamodel/SearchResults.java +++ b/src/jalview/datamodel/SearchResults.java @@ -67,8 +67,22 @@ public class SearchResults public Match(SequenceI seq, int start, int end) { sequence = seq; - this.start = start; - this.end = end; + + /* + * always hold in forwards order, even if given in reverse order + * (such as from a mapping to a reverse strand); this avoids + * trouble for routines that highlight search results etc + */ + if (start <= end) + { + this.start = start; + this.end = end; + } + else + { + this.start = end; + this.end = start; + } } public SequenceI getSequence() diff --git a/test/jalview/datamodel/SearchResultsTest.java b/test/jalview/datamodel/SearchResultsTest.java index 3838ac8..ffcaa26 100644 --- a/test/jalview/datamodel/SearchResultsTest.java +++ b/test/jalview/datamodel/SearchResultsTest.java @@ -22,8 +22,11 @@ package jalview.datamodel; 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 jalview.datamodel.SearchResults.Match; + import org.testng.annotations.Test; public class SearchResultsTest @@ -166,4 +169,24 @@ public class SearchResultsTest sr2.addResult(seq1, 6, 8); assertEquals(sr1.hashCode(), sr2.hashCode()); } + + /** + * Verify that SearchResults$Match constructor normalises start/end to the + * 'forwards' direction + */ + @Test(groups = { "Functional" }) + public void testMatchConstructor() + { + SequenceI seq1 = new Sequence("", "abcdefghijklm"); + Match m = new SearchResults().new Match(seq1, 2, 5); + assertSame(seq1, m.getSequence()); + assertEquals(2, m.getStart()); + assertEquals(5, m.getEnd()); + + // now a reverse mapping: + m = new SearchResults().new Match(seq1, 5, 2); + assertSame(seq1, m.getSequence()); + assertEquals(2, m.getStart()); + assertEquals(5, m.getEnd()); + } } -- 1.7.10.2