X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FSearchResults.java;h=a270e3730794fb86701eed87f153ac4a4758e65c;hb=3d0101179759ef157b088ea135423cd909512d9f;hp=2f62991c85e2eabf71475c60544d8bc6aeb08792;hpb=52402128d98c6744eb6348dd788fe6e8cd34575c;p=jalview.git diff --git a/src/jalview/datamodel/SearchResults.java b/src/jalview/datamodel/SearchResults.java index 2f62991..a270e37 100755 --- a/src/jalview/datamodel/SearchResults.java +++ b/src/jalview/datamodel/SearchResults.java @@ -21,7 +21,7 @@ package jalview.datamodel; import java.util.ArrayList; -import java.util.Arrays; +import java.util.BitSet; import java.util.List; /** @@ -34,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. @@ -55,7 +55,10 @@ public class SearchResults implements SearchResultsI int end; /** - * Constructor + * create a Match on a range of sequence. Match always holds region 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 * * @param seq * a sequence @@ -80,6 +83,9 @@ public class SearchResults implements SearchResultsI } else { + // TODO: JBP could mark match as being specified in reverse direction + // for use + // by caller ? e.g. visualizing reverse strand highlight this.start = end; this.end = start; } @@ -113,28 +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)); + StringBuilder sb = new StringBuilder(); + if (sequence != null) + { + sb.append(sequence.getName()).append("/"); + } + sb.append(start).append("-").append(end); + return sb.toString(); } public void setSequence(SequenceI seq) @@ -163,12 +159,13 @@ public class SearchResults implements SearchResultsI @Override public boolean equals(Object obj) { - if (obj == null || !(obj instanceof Match)) + if (obj == null || !(obj instanceof SearchResultMatchI)) { return false; } - Match m = (Match) obj; - return (this.sequence == m.sequence && this.start == m.start && this.end == m.end); + SearchResultMatchI m = (SearchResultMatchI) obj; + return (sequence == m.getSequence() && start == m.getStart() + && end == m.getEnd()); } } @@ -176,9 +173,11 @@ public class SearchResults implements SearchResultsI * @see jalview.datamodel.SearchResultsI#addResult(jalview.datamodel.SequenceI, int, int) */ @Override - public void addResult(SequenceI seq, int start, int end) + public SearchResultMatchI addResult(SequenceI seq, int start, int end) { - matches.add(new Match(seq, start, end)); + Match m = new Match(seq, start, end); + matches.add(m); + return m; } /* (non-Javadoc) @@ -188,10 +187,10 @@ public class SearchResults implements SearchResultsI public boolean involvesSequence(SequenceI sequence) { SequenceI ds = sequence.getDatasetSequence(); - for (Match m : matches) + for (SearchResultMatchI _m : matches) { - if (m.sequence != null - && (m.sequence == sequence || m.sequence == ds)) + SequenceI matched = _m.getSequence(); + if (matched != null && (matched == sequence || matched == ds)) { return true; } @@ -214,8 +213,11 @@ public class SearchResults implements SearchResultsI int[] tmp = null; int resultLength, matchStart = 0, matchEnd = 0; boolean mfound; - for (Match m : matches) + Match m; + for (SearchResultMatchI _m : matches) { + m = (Match) _m; + mfound = false; if (m.sequence == sequence) { @@ -262,7 +264,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); } } @@ -270,40 +272,38 @@ public class SearchResults implements SearchResultsI return result; } - /* (non-Javadoc) - * @see jalview.datamodel.SearchResultsI#getSize() - */ - @Override - public int getSize() - { - return matches.size(); - } - - /* (non-Javadoc) - * @see jalview.datamodel.SearchResultsI#getResultSequence(int) - */ @Override - public SequenceI getResultSequence(int index) + public int markColumns(SequenceCollectionI sqcol, BitSet bs) { - return matches.get(index).sequence; - } - - /* (non-Javadoc) - * @see jalview.datamodel.SearchResultsI#getResultStart(int) - */ - @Override - public int getResultStart(int i) - { - return matches.get(i).start; + int count = 0; + BitSet mask = new BitSet(); + for (SequenceI s : sqcol.getSequences()) + { + int[] cols = getResults(s, sqcol.getStartRes(), sqcol.getEndRes()); + if (cols != null) + { + for (int pair = 0; pair < cols.length; pair += 2) + { + mask.set(cols[pair], cols[pair + 1] + 1); + } + } + } + // compute columns that were newly selected + BitSet original = (BitSet) bs.clone(); + original.and(mask); + count = mask.cardinality() - original.cardinality(); + // and mark ranges not already marked + bs.or(mask); + return count; } /* (non-Javadoc) - * @see jalview.datamodel.SearchResultsI#getResultEnd(int) + * @see jalview.datamodel.SearchResultsI#getSize() */ @Override - public int getResultEnd(int i) + public int getSize() { - return matches.get(i).end; + return matches.size(); } /* (non-Javadoc) @@ -319,50 +319,29 @@ public class SearchResults implements SearchResultsI * @see jalview.datamodel.SearchResultsI#getResults() */ @Override - public List getResults() + public List getResults() { return matches; } /** - * 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 matches == null ? "" : matches.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(); - } - - /** - * 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() @@ -377,11 +356,11 @@ public class SearchResults implements SearchResultsI @Override public boolean equals(Object obj) { - if (obj == null || !(obj instanceof SearchResults)) + if (obj == null || !(obj instanceof SearchResultsI)) { return false; } - SearchResults sr = (SearchResults) obj; - return ((ArrayList) this.matches).equals(sr.matches); + SearchResultsI sr = (SearchResultsI) obj; + return matches.equals(sr.getResults()); } }