2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.datamodel;
23 import java.util.ArrayList;
24 import java.util.BitSet;
25 import java.util.List;
28 * Holds a list of search result matches, where each match is a contiguous
29 * stretch of a single sequence.
31 * @author gmcarstairs amwaterhouse
34 public class SearchResults implements SearchResultsI
37 private List<SearchResultMatchI> matches = new ArrayList<SearchResultMatchI>();
40 * One match consists of a sequence reference, start and end positions.
41 * Discontiguous ranges in a sequence require two or more Match objects.
43 public class Match implements SearchResultMatchI
48 * Start position of match in sequence (base 1)
53 * End position (inclusive) (base 1)
58 * create a Match on a range of sequence. Match always holds region in
59 * forwards order, even if given in reverse order (such as from a mapping to
60 * a reverse strand); this avoids trouble for routines that highlight search
66 * start position of matched range (base 1)
68 * end of matched range (inclusive, base 1)
70 public Match(SequenceI seq, int start, int end)
75 * always hold in forwards order, even if given in reverse order
76 * (such as from a mapping to a reverse strand); this avoids
77 * trouble for routines that highlight search results etc
86 // TODO: JBP could mark match as being specified in reverse direction
88 // by caller ? e.g. visualizing reverse strand highlight
95 * @see jalview.datamodel.SearchResultMatchI#getSequence()
98 public SequenceI getSequence()
104 * @see jalview.datamodel.SearchResultMatchI#getStart()
107 public int getStart()
113 * @see jalview.datamodel.SearchResultMatchI#getEnd()
122 * Returns a representation as "seqid/start-end"
125 public String toString()
127 StringBuilder sb = new StringBuilder();
128 if (sequence != null)
130 sb.append(sequence.getName()).append("/");
132 sb.append(start).append("-").append(end);
133 return sb.toString();
136 public void setSequence(SequenceI seq)
142 * Hashcode is the hashcode of the matched sequence plus a hash of start and
143 * end positions. Match objects that pass the test for equals are guaranteed
144 * to have the same hashcode.
147 public int hashCode()
149 int hash = sequence == null ? 0 : sequence.hashCode();
156 * Two Match objects are equal if they are for the same sequence, start and
160 public boolean equals(Object obj)
162 if (obj == null || !(obj instanceof SearchResultMatchI))
166 SearchResultMatchI m = (SearchResultMatchI) obj;
167 return (sequence == m.getSequence() && start == m.getStart() && end == m
173 * @see jalview.datamodel.SearchResultsI#addResult(jalview.datamodel.SequenceI, int, int)
176 public SearchResultMatchI addResult(SequenceI seq, int start, int end)
178 Match m = new Match(seq, start, end);
184 * @see jalview.datamodel.SearchResultsI#involvesSequence(jalview.datamodel.SequenceI)
187 public boolean involvesSequence(SequenceI sequence)
189 SequenceI ds = sequence.getDatasetSequence();
190 for (SearchResultMatchI _m : matches)
192 SequenceI matched = _m.getSequence();
193 if (matched != null && (matched == sequence || matched == ds))
202 * @see jalview.datamodel.SearchResultsI#getResults(jalview.datamodel.SequenceI, int, int)
205 public int[] getResults(SequenceI sequence, int start, int end)
207 if (matches.isEmpty())
214 int resultLength, matchStart = 0, matchEnd = 0;
217 for (SearchResultMatchI _m : matches)
222 if (m.sequence == sequence)
225 // locate aligned position
226 matchStart = sequence.findIndex(m.start) - 1;
227 matchEnd = sequence.findIndex(m.end) - 1;
229 else if (m.sequence == sequence.getDatasetSequence())
232 // locate region in local context
233 matchStart = sequence.findIndex(m.start) - 1;
234 matchEnd = sequence.findIndex(m.end) - 1;
238 if (matchStart <= end && matchEnd >= start)
240 if (matchStart < start)
252 result = new int[] { matchStart, matchEnd };
256 resultLength = result.length;
257 tmp = new int[resultLength + 2];
258 System.arraycopy(result, 0, tmp, 0, resultLength);
260 result[resultLength] = matchStart;
261 result[resultLength + 1] = matchEnd;
267 // System.err.println("Outwith bounds!" + matchStart+">"+end +" or "
268 // + matchEnd+"<"+start);
276 public int markColumns(SequenceCollectionI sqcol, BitSet bs)
279 BitSet mask = new BitSet();
280 for (SequenceI s : sqcol.getSequences())
282 int[] cols = getResults(s, sqcol.getStartRes(), sqcol.getEndRes());
285 for (int pair = 0; pair < cols.length; pair += 2)
287 mask.set(cols[pair], cols[pair + 1] + 1);
291 // compute columns that were newly selected
292 BitSet original = (BitSet) bs.clone();
294 count = mask.cardinality() - original.cardinality();
295 // and mark ranges not already marked
301 * @see jalview.datamodel.SearchResultsI#getSize()
306 return matches.size();
310 * @see jalview.datamodel.SearchResultsI#isEmpty()
313 public boolean isEmpty()
315 return matches.isEmpty();
319 * @see jalview.datamodel.SearchResultsI#getResults()
322 public List<SearchResultMatchI> getResults()
328 * Return the results as a list of matches [seq1/from-to, seq2/from-to, ...]
333 public String toString()
335 return matches == null ? "" : matches.toString();
339 * Hashcode is derived from the list of matches. This ensures that when two
340 * SearchResults objects satisfy the test for equals(), then they have the
343 * @see Match#hashCode()
344 * @see java.util.AbstractList#hashCode()
347 public int hashCode()
349 return matches.hashCode();
353 * Two SearchResults are considered equal if they contain the same matches in
357 public boolean equals(Object obj)
359 if (obj == null || !(obj instanceof SearchResultsI))
363 SearchResultsI sr = (SearchResultsI) obj;
364 return matches.equals(sr.getResults());