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.BitSet;
26 import java.util.List;
29 * Holds a list of search result matches, where each match is a contiguous
30 * stretch of a single sequence.
32 * @author gmcarstairs amwaterhouse
35 public class SearchResults implements SearchResultsI
38 private List<SearchResultMatchI> matches = new ArrayList<SearchResultMatchI>();
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
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 * @see jalview.datamodel.SearchResultMatchI#getSequence()
99 public SequenceI getSequence()
105 * @see jalview.datamodel.SearchResultMatchI#getStart()
108 public int getStart()
114 * @see jalview.datamodel.SearchResultMatchI#getEnd()
123 * Returns the string of characters in the matched region, prefixed by the
124 * start position, e.g. "12CGT" or "208K"
127 public String toString()
129 final int from = Math.max(start - 1, 0);
130 String startPosition = String.valueOf(from);
131 return startPosition + getCharacters();
135 * @see jalview.datamodel.SearchResultMatchI#getCharacters()
138 public String getCharacters()
140 char[] chars = sequence.getSequence();
141 // convert start/end to base 0 (with bounds check)
142 final int from = Math.max(start - 1, 0);
143 final int to = Math.min(end, chars.length + 1);
144 return String.valueOf(Arrays.copyOfRange(chars, from, to));
147 public void setSequence(SequenceI seq)
153 * Hashcode is the hashcode of the matched sequence plus a hash of start and
154 * end positions. Match objects that pass the test for equals are guaranteed
155 * to have the same hashcode.
158 public int hashCode()
160 int hash = sequence == null ? 0 : sequence.hashCode();
167 * Two Match objects are equal if they are for the same sequence, start and
171 public boolean equals(Object obj)
173 if (obj == null || !(obj instanceof SearchResultMatchI))
177 SearchResultMatchI m = (SearchResultMatchI) obj;
178 return (sequence == m.getSequence() && start == m.getStart() && end == m
184 * @see jalview.datamodel.SearchResultsI#addResult(jalview.datamodel.SequenceI, int, int)
187 public SearchResultMatchI addResult(SequenceI seq, int start, int end)
189 Match m = new Match(seq, start, end);
195 * @see jalview.datamodel.SearchResultsI#involvesSequence(jalview.datamodel.SequenceI)
198 public boolean involvesSequence(SequenceI sequence)
200 SequenceI ds = sequence.getDatasetSequence();
202 for (SearchResultMatchI _m : matches)
205 if (m.sequence != null
206 && (m.sequence == sequence || m.sequence == ds))
215 * @see jalview.datamodel.SearchResultsI#getResults(jalview.datamodel.SequenceI, int, int)
218 public int[] getResults(SequenceI sequence, int start, int end)
220 if (matches.isEmpty())
227 int resultLength, matchStart = 0, matchEnd = 0;
230 for (SearchResultMatchI _m : matches)
235 if (m.sequence == sequence)
238 // locate aligned position
239 matchStart = sequence.findIndex(m.start) - 1;
240 matchEnd = sequence.findIndex(m.end) - 1;
242 else if (m.sequence == sequence.getDatasetSequence())
245 // locate region in local context
246 matchStart = sequence.findIndex(m.start) - 1;
247 matchEnd = sequence.findIndex(m.end) - 1;
251 if (matchStart <= end && matchEnd >= start)
253 if (matchStart < start)
265 result = new int[] { matchStart, matchEnd };
269 resultLength = result.length;
270 tmp = new int[resultLength + 2];
271 System.arraycopy(result, 0, tmp, 0, resultLength);
273 result[resultLength] = matchStart;
274 result[resultLength + 1] = matchEnd;
280 // System.err.println("Outwith bounds!" + matchStart+">"+end +" or "
281 // + matchEnd+"<"+start);
289 public int markColumns(SequenceCollectionI sqcol, BitSet bs)
292 BitSet mask = new BitSet();
293 for (SequenceI s : sqcol.getSequences())
295 int[] cols = getResults(s, sqcol.getStartRes(), sqcol.getEndRes());
298 for (int pair = 0; pair < cols.length; pair += 2)
300 mask.set(cols[pair], cols[pair + 1] + 1);
304 // compute columns that were newly selected
305 BitSet original = (BitSet) bs.clone();
307 count = mask.cardinality() - original.cardinality();
308 // and mark ranges not already marked
314 * @see jalview.datamodel.SearchResultsI#getSize()
319 return matches.size();
323 * @see jalview.datamodel.SearchResultsI#getResultSequence(int)
326 public SequenceI getResultSequence(int index)
328 return matches.get(index).getSequence();
332 * @see jalview.datamodel.SearchResultsI#getResultStart(int)
335 public int getResultStart(int i)
337 return matches.get(i).getStart();
341 * @see jalview.datamodel.SearchResultsI#getResultEnd(int)
344 public int getResultEnd(int i)
346 return matches.get(i).getEnd();
350 * @see jalview.datamodel.SearchResultsI#isEmpty()
353 public boolean isEmpty()
355 return matches.isEmpty();
359 * @see jalview.datamodel.SearchResultsI#getResults()
362 public List<SearchResultMatchI> getResults()
368 * Return the results as a string of characters (bases) prefixed by start
369 * position(s). Meant for use when the context ensures that all matches are to
370 * regions of the same sequence (otherwise the result is meaningless).
375 public String toString()
377 StringBuilder result = new StringBuilder(256);
378 for (SearchResultMatchI m : matches)
380 result.append(m.toString());
382 return result.toString();
386 * Return the results as a string of characters (bases). Meant for use when
387 * the context ensures that all matches are to regions of the same sequence
388 * (otherwise the result is meaningless).
392 public String getCharacters()
394 StringBuilder result = new StringBuilder(256);
395 for (SearchResultMatchI m : matches)
397 result.append(m.getCharacters());
399 return result.toString();
403 * Hashcode is has derived from the list of matches. This ensures that when
404 * two SearchResults objects satisfy the test for equals(), then they have the
408 public int hashCode()
410 return matches.hashCode();
414 * Two SearchResults are considered equal if they contain the same matches in
418 public boolean equals(Object obj)
420 if (obj == null || !(obj instanceof SearchResultsI))
424 SearchResultsI sr = (SearchResultsI) obj;
425 return matches.equals(sr.getResults());