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.Arrays;
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<Match> matches = new ArrayList<Match>();
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)
63 * start position of matched range (base 1)
65 * end of matched range (inclusive, base 1)
67 public Match(SequenceI seq, int start, int end)
72 * always hold in forwards order, even if given in reverse order
73 * (such as from a mapping to a reverse strand); this avoids
74 * trouble for routines that highlight search results etc
89 * @see jalview.datamodel.SearchResultMatchI#getSequence()
92 public SequenceI getSequence()
98 * @see jalview.datamodel.SearchResultMatchI#getStart()
101 public int getStart()
107 * @see jalview.datamodel.SearchResultMatchI#getEnd()
116 * Returns the string of characters in the matched region, prefixed by the
117 * start position, e.g. "12CGT" or "208K"
120 public String toString()
122 final int from = Math.max(start - 1, 0);
123 String startPosition = String.valueOf(from);
124 return startPosition + getCharacters();
128 * @see jalview.datamodel.SearchResultMatchI#getCharacters()
131 public String getCharacters()
133 char[] chars = sequence.getSequence();
134 // convert start/end to base 0 (with bounds check)
135 final int from = Math.max(start - 1, 0);
136 final int to = Math.min(end, chars.length + 1);
137 return String.valueOf(Arrays.copyOfRange(chars, from, to));
140 public void setSequence(SequenceI seq)
146 * Hashcode is the hashcode of the matched sequence plus a hash of start and
147 * end positions. Match objects that pass the test for equals are guaranteed
148 * to have the same hashcode.
151 public int hashCode()
153 int hash = sequence == null ? 0 : sequence.hashCode();
160 * Two Match objects are equal if they are for the same sequence, start and
164 public boolean equals(Object obj)
166 if (obj == null || !(obj instanceof Match))
170 Match m = (Match) obj;
171 return (this.sequence == m.sequence && this.start == m.start && this.end == m.end);
176 * @see jalview.datamodel.SearchResultsI#addResult(jalview.datamodel.SequenceI, int, int)
179 public void addResult(SequenceI seq, int start, int end)
181 matches.add(new Match(seq, start, end));
185 * @see jalview.datamodel.SearchResultsI#involvesSequence(jalview.datamodel.SequenceI)
188 public boolean involvesSequence(SequenceI sequence)
190 SequenceI ds = sequence.getDatasetSequence();
191 for (Match m : matches)
193 if (m.sequence != null
194 && (m.sequence == sequence || m.sequence == ds))
203 * @see jalview.datamodel.SearchResultsI#getResults(jalview.datamodel.SequenceI, int, int)
206 public int[] getResults(SequenceI sequence, int start, int end)
208 if (matches.isEmpty())
215 int resultLength, matchStart = 0, matchEnd = 0;
217 for (Match m : matches)
220 if (m.sequence == sequence)
223 // locate aligned position
224 matchStart = sequence.findIndex(m.start) - 1;
225 matchEnd = sequence.findIndex(m.end) - 1;
227 else if (m.sequence == sequence.getDatasetSequence())
230 // locate region in local context
231 matchStart = sequence.findIndex(m.start) - 1;
232 matchEnd = sequence.findIndex(m.end) - 1;
236 if (matchStart <= end && matchEnd >= start)
238 if (matchStart < start)
250 result = new int[] { matchStart, matchEnd };
254 resultLength = result.length;
255 tmp = new int[resultLength + 2];
256 System.arraycopy(result, 0, tmp, 0, resultLength);
258 result[resultLength] = matchStart;
259 result[resultLength + 1] = matchEnd;
265 // System.err.println("Outwith bounds!" + matchStart+">"+end +" or "
266 // + matchEnd+"<"+start);
274 * @see jalview.datamodel.SearchResultsI#getSize()
279 return matches.size();
283 * @see jalview.datamodel.SearchResultsI#getResultSequence(int)
286 public SequenceI getResultSequence(int index)
288 return matches.get(index).sequence;
292 * @see jalview.datamodel.SearchResultsI#getResultStart(int)
295 public int getResultStart(int i)
297 return matches.get(i).start;
301 * @see jalview.datamodel.SearchResultsI#getResultEnd(int)
304 public int getResultEnd(int i)
306 return matches.get(i).end;
310 * @see jalview.datamodel.SearchResultsI#isEmpty()
313 public boolean isEmpty()
315 return matches.isEmpty();
319 * @see jalview.datamodel.SearchResultsI#getResults()
322 public List<Match> getResults()
328 * Return the results as a string of characters (bases) prefixed by start
329 * position(s). Meant for use when the context ensures that all matches are to
330 * regions of the same sequence (otherwise the result is meaningless).
335 public String toString()
337 StringBuilder result = new StringBuilder(256);
338 for (SearchResultMatchI m : matches)
340 result.append(m.toString());
342 return result.toString();
346 * Return the results as a string of characters (bases). Meant for use when
347 * the context ensures that all matches are to regions of the same sequence
348 * (otherwise the result is meaningless).
352 public String getCharacters()
354 StringBuilder result = new StringBuilder(256);
355 for (SearchResultMatchI m : matches)
357 result.append(m.getCharacters());
359 return result.toString();
363 * Hashcode is has derived from the list of matches. This ensures that when
364 * two SearchResults objects satisfy the test for equals(), then they have the
368 public int hashCode()
370 return matches.hashCode();
374 * Two SearchResults are considered equal if they contain the same matches in
378 public boolean equals(Object obj)
380 if (obj == null || !(obj instanceof SearchResults))
384 SearchResults sr = (SearchResults) obj;
385 return ((ArrayList<Match>) this.matches).equals(sr.matches);