X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FSearchResults.java;h=d1e3fcc9698ac00016a30a09358507e1289e199d;hb=dc3dd2e76d340b0577259c4e9d64372a1c126aee;hp=2ba16126d651d69fc373ca8407a2e5d801e83cdb;hpb=b9f1ae59bc2a4b45a07f7d9ac08e75b78724cff7;p=jalview.git diff --git a/src/jalview/datamodel/SearchResults.java b/src/jalview/datamodel/SearchResults.java index 2ba1612..d1e3fcc 100755 --- a/src/jalview/datamodel/SearchResults.java +++ b/src/jalview/datamodel/SearchResults.java @@ -21,7 +21,6 @@ package jalview.datamodel; import java.util.ArrayList; -import java.util.Arrays; import java.util.BitSet; import java.util.List; @@ -35,7 +34,7 @@ import java.util.List; public class SearchResults implements SearchResultsI { - private List matches = new ArrayList(); + private List matches = new ArrayList<>(); /** * One match consists of a sequence reference, start and end positions. @@ -43,17 +42,17 @@ public class SearchResults implements SearchResultsI */ public class Match implements SearchResultMatchI { - SequenceI sequence; + final SequenceI sequence; /** * Start position of match in sequence (base 1) */ - int start; + final int start; /** * End position (inclusive) (base 1) */ - int end; + final int end; /** * create a Match on a range of sequence. Match always holds region in @@ -120,33 +119,18 @@ public class SearchResults implements SearchResultsI } /** - * Returns the string of characters in the matched region, prefixed by the - * start position, e.g. "12CGT" or "208K" + * Returns a representation as "seqid/start-end" */ @Override public String toString() { - final int from = Math.max(start - 1, 0); - String startPosition = String.valueOf(from); - return startPosition + getCharacters(); - } - - /* (non-Javadoc) - * @see jalview.datamodel.SearchResultMatchI#getCharacters() - */ - @Override - 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)); - } - - public void setSequence(SequenceI seq) - { - this.sequence = seq; + StringBuilder sb = new StringBuilder(); + if (sequence != null) + { + sb.append(sequence.getName()).append("/"); + } + sb.append(start).append("-").append(end); + return sb.toString(); } /** @@ -175,8 +159,8 @@ public class SearchResults implements SearchResultsI return false; } SearchResultMatchI m = (SearchResultMatchI) obj; - return (sequence == m.getSequence() && start == m.getStart() && end == m - .getEnd()); + return (sequence == m.getSequence() && start == m.getStart() + && end == m.getEnd()); } } @@ -198,12 +182,10 @@ public class SearchResults implements SearchResultsI public boolean involvesSequence(SequenceI sequence) { SequenceI ds = sequence.getDatasetSequence(); - Match m; for (SearchResultMatchI _m : matches) { - m = (Match) _m; - if (m.sequence != null - && (m.sequence == sequence || m.sequence == ds)) + SequenceI matched = _m.getSequence(); + if (matched != null && (matched == sequence || matched == ds)) { return true; } @@ -232,20 +214,15 @@ public class SearchResults implements SearchResultsI m = (Match) _m; mfound = false; - if (m.sequence == sequence) + if (m.sequence == sequence + || m.sequence == sequence.getDatasetSequence()) { mfound = true; - // locate aligned position matchStart = sequence.findIndex(m.start) - 1; - matchEnd = sequence.findIndex(m.end) - 1; - } - else if (m.sequence == sequence.getDatasetSequence()) - { - mfound = true; - // locate region in local context - matchStart = sequence.findIndex(m.start) - 1; - matchEnd = sequence.findIndex(m.end) - 1; + matchEnd = m.start == m.end ? matchStart : sequence + .findIndex(m.end) - 1; } + if (mfound) { if (matchStart <= end && matchEnd >= start) @@ -277,7 +254,7 @@ public class SearchResults implements SearchResultsI else { // debug - // System.err.println("Outwith bounds!" + matchStart+">"+end +" or " + // System.err.println("Outwith bounds!" + matchStart+">"+end +" or " // + matchEnd+"<"+start); } } @@ -290,9 +267,12 @@ public class SearchResults implements SearchResultsI { int count = 0; BitSet mask = new BitSet(); + int startRes = sqcol.getStartRes(); + int endRes = sqcol.getEndRes(); + for (SequenceI s : sqcol.getSequences()) { - int[] cols = getResults(s, sqcol.getStartRes(), sqcol.getEndRes()); + int[] cols = getResults(s, startRes, endRes); if (cols != null) { for (int pair = 0; pair < cols.length; pair += 2) @@ -320,33 +300,6 @@ public class SearchResults implements SearchResultsI } /* (non-Javadoc) - * @see jalview.datamodel.SearchResultsI#getResultSequence(int) - */ - @Override - public SequenceI getResultSequence(int index) - { - return matches.get(index).getSequence(); - } - - /* (non-Javadoc) - * @see jalview.datamodel.SearchResultsI#getResultStart(int) - */ - @Override - public int getResultStart(int i) - { - return matches.get(i).getStart(); - } - - /* (non-Javadoc) - * @see jalview.datamodel.SearchResultsI#getResultEnd(int) - */ - @Override - public int getResultEnd(int i) - { - return matches.get(i).getEnd(); - } - - /* (non-Javadoc) * @see jalview.datamodel.SearchResultsI#isEmpty() */ @Override @@ -365,44 +318,23 @@ public class SearchResults implements SearchResultsI } /** - * 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 the results as a list of matches [seq1/from-to, seq2/from-to, ...] * * @return */ @Override public String toString() { - StringBuilder result = new StringBuilder(256); - for (SearchResultMatchI m : matches) - { - result.append(m.toString()); - } - return result.toString(); - } - - /** - * 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 (SearchResultMatchI m : matches) - { - result.append(m.getCharacters()); - } - return result.toString(); + return matches == null ? "" : matches.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 + * Hashcode is derived from the list of matches. This ensures that when two + * SearchResults objects satisfy the test for equals(), then they have the * same hashcode. + * + * @see Match#hashCode() + * @see java.util.AbstractList#hashCode() */ @Override public int hashCode() @@ -424,4 +356,10 @@ public class SearchResults implements SearchResultsI SearchResultsI sr = (SearchResultsI) obj; return matches.equals(sr.getResults()); } + + @Override + public void addSearchResults(SearchResultsI toAdd) + { + matches.addAll(toAdd.getResults()); + } }