X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FSearchResults.java;h=b9db46123c335bd5f703045adbecd9f6e0107c84;hb=37de9310bec3501cbc6381e0c3dcb282fcaad812;hp=3948ba0bf852ee0cd2798b8a85d3bd9c007c846f;hpb=0c37e7cea9a7028130b6f2164961af24c3c48ec2;p=jalview.git diff --git a/src/jalview/datamodel/SearchResults.java b/src/jalview/datamodel/SearchResults.java index 3948ba0..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() @@ -87,18 +101,27 @@ public class SearchResults } /** - * Returns the string of characters in the matched region. + * Returns the string of characters in the matched region, prefixed by the + * start position, e.g. "12CGT" or "208K" */ @Override public String toString() { + final int from = Math.max(start - 1, 0); + String startPosition = String.valueOf(from); + return startPosition + getCharacters(); + } + + /** + * Returns the string of characters in the matched region. + */ + public String getCharacters() + { char[] chars = sequence.getSequence(); // convert start/end to base 0 (with bounds check) final int from = Math.max(start - 1, 0); final int to = Math.min(end, chars.length + 1); - // return String.valueOf(Arrays.copyOfRange(chars, from, to)); - return String.valueOf(from) - + String.valueOf(Arrays.copyOfRange(chars, from, to)); + return String.valueOf(Arrays.copyOfRange(chars, from, to)); } public void setSequence(SequenceI seq) @@ -223,8 +246,7 @@ public class SearchResults if (result == null) { - result = new int[] - { matchStart, matchEnd }; + result = new int[] { matchStart, matchEnd }; } else { @@ -300,9 +322,9 @@ public class SearchResults } /** - * Return the results as a string of characters. Meant for use when the - * context ensures that all matches are to regions of the same sequence - * (otherwise the result is meaningless). + * Return the results as a string of characters (bases) prefixed by start + * position(s). Meant for use when the context ensures that all matches are to + * regions of the same sequence (otherwise the result is meaningless). * * @return */ @@ -318,6 +340,23 @@ public class SearchResults } /** + * Return the results as a string of characters (bases). Meant for use when + * the context ensures that all matches are to regions of the same sequence + * (otherwise the result is meaningless). + * + * @return + */ + public String getCharacters() + { + StringBuilder result = new StringBuilder(256); + for (Match m : matches) + { + result.append(m.getCharacters()); + } + return result.toString(); + } + + /** * Hashcode is has derived from the list of matches. This ensures that when * two SearchResults objects satisfy the test for equals(), then they have the * same hashcode.