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
38 private List<SearchResultMatchI> matches = new ArrayList<>();
41 * One match consists of a sequence reference, start and end positions.
42 * Discontiguous ranges in a sequence require two or more Match objects.
44 public class Match implements SearchResultMatchI
46 final SequenceI sequence;
49 * Start position of match in sequence (base 1)
54 * End position (inclusive) (base 1)
59 * create a Match on a range of sequence. Match always holds region in
60 * forwards order, even if given in reverse order (such as from a mapping to
61 * a reverse strand); this avoids trouble for routines that highlight search
67 * start position of matched range (base 1)
69 * end of matched range (inclusive, base 1)
71 public Match(SequenceI seq, int start, int end)
76 * always hold in forwards order, even if given in reverse order
77 * (such as from a mapping to a reverse strand); this avoids
78 * trouble for routines that highlight search results etc
87 // TODO: JBP could mark match as being specified in reverse direction
89 // by caller ? e.g. visualizing reverse strand highlight
96 public SequenceI getSequence()
102 public int getStart()
114 * Returns a representation as "seqid/start-end"
117 public String toString()
119 StringBuilder sb = new StringBuilder();
120 if (sequence != null)
122 sb.append(sequence.getName()).append("/");
124 sb.append(start).append("-").append(end);
125 return sb.toString();
129 * Hashcode is the hashcode of the matched sequence plus a hash of start and
130 * end positions. Match objects that pass the test for equals are guaranteed
131 * to have the same hashcode.
134 public int hashCode()
136 int hash = sequence == null ? 0 : sequence.hashCode();
143 * Two Match objects are equal if they are for the same sequence, start and
147 public boolean equals(Object obj)
149 if (obj == null || !(obj instanceof SearchResultMatchI))
153 SearchResultMatchI m = (SearchResultMatchI) obj;
154 return (sequence == m.getSequence() && start == m.getStart()
155 && end == m.getEnd());
159 public boolean contains(SequenceI seq, int from, int to)
161 return (sequence == seq && start <= from && end >= to);
166 public SearchResultMatchI addResult(SequenceI seq, int start, int end)
168 Match m = new Match(seq, start, end);
169 if (!matches.contains(m))
178 public void addResult(SequenceI seq, int[] positions)
181 * we only increment the match count by 1 - or not at all,
182 * if the matches are all duplicates of existing
184 int beforeCount = count;
185 for (int i = 0; i < positions.length - 1; i += 2)
187 addResult(seq, positions[i], positions[i + 1]);
189 if (count > beforeCount)
191 count = beforeCount + 1;
196 public boolean involvesSequence(SequenceI sequence)
198 final int start = sequence.getStart();
199 final int end = sequence.getEnd();
201 SequenceI ds = sequence.getDatasetSequence();
202 for (SearchResultMatchI m : matches)
204 SequenceI matched = m.getSequence();
205 if (matched != null && (matched == sequence || matched == ds)
206 && (m.getEnd() >= start) && (m.getStart() <= end))
215 public int[] getResults(SequenceI sequence, int start, int end)
217 if (matches.isEmpty())
224 int resultLength, matchStart = 0, matchEnd = 0;
227 for (SearchResultMatchI _m : matches)
232 if (m.sequence == sequence
233 || m.sequence == sequence.getDatasetSequence())
236 matchStart = sequence.findIndex(m.start) - 1;
237 matchEnd = m.start == m.end ? matchStart
238 : sequence.findIndex(m.end) - 1;
243 if (matchStart <= end && matchEnd >= start)
245 if (matchStart < start)
257 result = new int[] { matchStart, matchEnd };
261 resultLength = result.length;
262 tmp = new int[resultLength + 2];
263 System.arraycopy(result, 0, tmp, 0, resultLength);
265 result[resultLength] = matchStart;
266 result[resultLength + 1] = matchEnd;
272 // System.err.println("Outwith bounds!" + matchStart+">"+end +" or "
273 // + matchEnd+"<"+start);
281 public int markColumns(SequenceCollectionI sqcol, BitSet bs)
284 BitSet mask = new BitSet();
285 int startRes = sqcol.getStartRes();
286 int endRes = sqcol.getEndRes();
288 for (SequenceI s : sqcol.getSequences())
290 int[] cols = getResults(s, startRes, endRes);
293 for (int pair = 0; pair < cols.length; pair += 2)
295 mask.set(cols[pair], cols[pair + 1] + 1);
299 // compute columns that were newly selected
300 BitSet original = (BitSet) bs.clone();
302 count = mask.cardinality() - original.cardinality();
303 // and mark ranges not already marked
309 public int getCount()
315 public boolean isEmpty()
317 return matches.isEmpty();
321 public List<SearchResultMatchI> getResults()
327 * Return the results as a list of matches [seq1/from-to, seq2/from-to, ...]
332 public String toString()
334 return matches == null ? "" : matches.toString();
338 * Hashcode is derived from the list of matches. This ensures that when two
339 * SearchResults objects satisfy the test for equals(), then they have the
342 * @see Match#hashCode()
343 * @see java.util.AbstractList#hashCode()
346 public int hashCode()
348 return matches.hashCode();
352 * Two SearchResults are considered equal if they contain the same matches
353 * (Sequence, start position, end position) in the same order
355 * @see Match#equals(Object)
358 public boolean equals(Object obj)
360 if (obj == null || !(obj instanceof SearchResultsI))
364 SearchResultsI sr = (SearchResultsI) obj;
365 return matches.equals(sr.getResults());
369 public void addSearchResults(SearchResultsI toAdd)
371 matches.addAll(toAdd.getResults());