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)
64 * start position of matched range (base 1)
66 * end of matched range (inclusive, base 1)
68 public Match(SequenceI seq, int start, int end)
73 * always hold in forwards order, even if given in reverse order
74 * (such as from a mapping to a reverse strand); this avoids
75 * trouble for routines that highlight search results etc
90 * @see jalview.datamodel.SearchResultMatchI#getSequence()
93 public SequenceI getSequence()
99 * @see jalview.datamodel.SearchResultMatchI#getStart()
102 public int getStart()
108 * @see jalview.datamodel.SearchResultMatchI#getEnd()
117 * Returns the string of characters in the matched region, prefixed by the
118 * start position, e.g. "12CGT" or "208K"
121 public String toString()
123 final int from = Math.max(start - 1, 0);
124 String startPosition = String.valueOf(from);
125 return startPosition + getCharacters();
129 * @see jalview.datamodel.SearchResultMatchI#getCharacters()
132 public String getCharacters()
134 char[] chars = sequence.getSequence();
135 // convert start/end to base 0 (with bounds check)
136 final int from = Math.max(start - 1, 0);
137 final int to = Math.min(end, chars.length + 1);
138 return String.valueOf(Arrays.copyOfRange(chars, from, to));
141 public void setSequence(SequenceI seq)
147 * Hashcode is the hashcode of the matched sequence plus a hash of start and
148 * end positions. Match objects that pass the test for equals are guaranteed
149 * to have the same hashcode.
152 public int hashCode()
154 int hash = sequence == null ? 0 : sequence.hashCode();
161 * Two Match objects are equal if they are for the same sequence, start and
165 public boolean equals(Object obj)
167 if (obj == null || !(obj instanceof SearchResultMatchI))
171 SearchResultMatchI m = (SearchResultMatchI) obj;
172 return (sequence == m.getSequence() && start == m.getStart() && end == m
178 * @see jalview.datamodel.SearchResultsI#addResult(jalview.datamodel.SequenceI, int, int)
181 public SearchResultMatchI addResult(SequenceI seq, int start, int end)
183 Match m = new Match(seq, start, end);
189 * @see jalview.datamodel.SearchResultsI#involvesSequence(jalview.datamodel.SequenceI)
192 public boolean involvesSequence(SequenceI sequence)
194 SequenceI ds = sequence.getDatasetSequence();
196 for (SearchResultMatchI _m : matches)
199 if (m.sequence != null
200 && (m.sequence == sequence || m.sequence == ds))
209 * @see jalview.datamodel.SearchResultsI#getResults(jalview.datamodel.SequenceI, int, int)
212 public int[] getResults(SequenceI sequence, int start, int end)
214 if (matches.isEmpty())
221 int resultLength, matchStart = 0, matchEnd = 0;
224 for (SearchResultMatchI _m : matches)
229 if (m.sequence == sequence)
232 // locate aligned position
233 matchStart = sequence.findIndex(m.start) - 1;
234 matchEnd = sequence.findIndex(m.end) - 1;
236 else if (m.sequence == sequence.getDatasetSequence())
239 // locate region in local context
240 matchStart = sequence.findIndex(m.start) - 1;
241 matchEnd = sequence.findIndex(m.end) - 1;
245 if (matchStart <= end && matchEnd >= start)
247 if (matchStart < start)
259 result = new int[] { matchStart, matchEnd };
263 resultLength = result.length;
264 tmp = new int[resultLength + 2];
265 System.arraycopy(result, 0, tmp, 0, resultLength);
267 result[resultLength] = matchStart;
268 result[resultLength + 1] = matchEnd;
274 // System.err.println("Outwith bounds!" + matchStart+">"+end +" or "
275 // + matchEnd+"<"+start);
283 public int markColumns(SequenceCollectionI sqcol, BitSet bs)
286 BitSet mask = new BitSet();
287 for (SequenceI s : sqcol.getSequences())
289 int[] cols = getResults(s, sqcol.getStartRes(), sqcol.getEndRes());
292 for (int pair = 0; pair < cols.length; pair += 2)
294 mask.set(cols[pair], cols[pair + 1] + 1);
298 // compute columns that were newly selected
299 BitSet original = (BitSet) bs.clone();
301 count = mask.cardinality() - original.cardinality();
302 // and mark ranges not already marked
308 * @see jalview.datamodel.SearchResultsI#getSize()
313 return matches.size();
317 * @see jalview.datamodel.SearchResultsI#getResultSequence(int)
320 public SequenceI getResultSequence(int index)
322 return matches.get(index).getSequence();
326 * @see jalview.datamodel.SearchResultsI#getResultStart(int)
329 public int getResultStart(int i)
331 return matches.get(i).getStart();
335 * @see jalview.datamodel.SearchResultsI#getResultEnd(int)
338 public int getResultEnd(int i)
340 return matches.get(i).getEnd();
344 * @see jalview.datamodel.SearchResultsI#isEmpty()
347 public boolean isEmpty()
349 return matches.isEmpty();
353 * @see jalview.datamodel.SearchResultsI#getResults()
356 public List<SearchResultMatchI> getResults()
362 * Return the results as a string of characters (bases) prefixed by start
363 * position(s). Meant for use when the context ensures that all matches are to
364 * regions of the same sequence (otherwise the result is meaningless).
369 public String toString()
371 StringBuilder result = new StringBuilder(256);
372 for (SearchResultMatchI m : matches)
374 result.append(m.toString());
376 return result.toString();
380 * Return the results as a string of characters (bases). Meant for use when
381 * the context ensures that all matches are to regions of the same sequence
382 * (otherwise the result is meaningless).
386 public String getCharacters()
388 StringBuilder result = new StringBuilder(256);
389 for (SearchResultMatchI m : matches)
391 result.append(m.getCharacters());
393 return result.toString();
397 * Hashcode is has derived from the list of matches. This ensures that when
398 * two SearchResults objects satisfy the test for equals(), then they have the
402 public int hashCode()
404 return matches.hashCode();
408 * Two SearchResults are considered equal if they contain the same matches in
412 public boolean equals(Object obj)
414 if (obj == null || !(obj instanceof SearchResultsI))
418 SearchResultsI sr = (SearchResultsI) obj;
419 return matches.equals(sr.getResults());