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 ArrayList<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.
45 implements SearchResultMatchI, Comparable<SearchResultMatchI>
47 final SequenceI sequence;
50 * Start position of match in sequence (base 1)
55 * End position (inclusive) (base 1)
60 * create a Match on a range of sequence. Match always holds region in
61 * forwards order, even if given in reverse order (such as from a mapping to
62 * a reverse strand); this avoids trouble for routines that highlight search
68 * start position of matched range (base 1)
70 * end of matched range (inclusive, base 1)
72 public Match(SequenceI seq, int start, int end)
77 * always hold in forwards order, even if given in reverse order
78 * (such as from a mapping to a reverse strand); this avoids
79 * trouble for routines that highlight search results etc
88 // TODO: JBP could mark match as being specified in reverse direction
90 // by caller ? e.g. visualizing reverse strand highlight
97 public SequenceI getSequence()
103 public int getStart()
115 * Returns a representation as "seqid/start-end"
118 public String toString()
120 StringBuilder sb = new StringBuilder();
121 if (sequence != null)
123 sb.append(sequence.getName()).append("/");
125 sb.append(start).append("-").append(end);
126 return sb.toString();
130 * Hashcode is the hashcode of the matched sequence plus a hash of start and
131 * end positions. Match objects that pass the test for equals are guaranteed
132 * to have the same hashcode.
135 public int hashCode()
137 int hash = sequence == null ? 0 : sequence.hashCode();
144 * Two Match objects are equal if they are for the same sequence, start and
148 public boolean equals(Object obj)
150 if (obj == null || !(obj instanceof SearchResultMatchI))
154 SearchResultMatchI m = (SearchResultMatchI) obj;
155 return (sequence == m.getSequence() && start == m.getStart()
156 && end == m.getEnd());
160 public boolean contains(SequenceI seq, int from, int to)
162 return (sequence == seq && start <= from && end >= to);
166 public boolean adjacent(SequenceI seq, int from, int to)
168 return (sequence == seq && ((start <= from && end >= to)
169 || (from <= (end + 1) && to >= (end + 1))
170 || (from <= (start - 1) && to >= (start - 1))));
174 public int compareTo(SearchResultMatchI o)
176 if (start < o.getStart())
180 if (start > o.getStart())
184 if (end < o.getEnd())
188 if (end > o.getEnd())
192 if (sequence != o.getSequence())
194 int hashc = sequence.hashCode(), oseq = o.getSequence().hashCode();
195 return (hashc < oseq) ? -1 : 1;
203 public SearchResultMatchI addResult(SequenceI seq, int start, int end)
205 Match m = new Match(seq, start, end);
206 if (!matches.contains(m))
215 public void addResult(SequenceI seq, int[] positions)
218 * we only increment the match count by 1 - or not at all,
219 * if the matches are all duplicates of existing
221 int beforeCount = count;
222 for (int i = 0; i < positions.length - 1; i += 2)
224 addResult(seq, positions[i], positions[i + 1]);
226 if (count > beforeCount)
228 count = beforeCount + 1;
233 public boolean appendResult(SequenceI sequence, int start, int end)
236 Match m = new Match(sequence, start, end);
238 boolean appending = false;
240 // we dynamically maintain an interval to add as we test each range in the
243 int cstart = start, cend = end;
244 List<SearchResultMatchI> toRemove = new ArrayList<>();
245 for (SearchResultMatchI thatm : matches)
247 if (thatm.getSequence() == sequence)
249 if (thatm.contains(sequence, cstart, cend))
251 // found a match containing the current range. nothing else to do
252 // except report if we operated on the list
255 if (thatm.adjacent(sequence, cstart, cend))
257 // update the match to add with the adjacent start/end
258 start = Math.min(m.start, thatm.getStart());
259 end = Math.max(m.end, thatm.getEnd());
260 // and check if we keep or remove the old one
261 if (thatm.getStart() != start || thatm.getEnd() != end)
276 matches.removeAll(toRemove);
278 matches.add(new Match(sequence, cstart, cend));
285 public boolean involvesSequence(SequenceI sequence)
287 final int start = sequence.getStart();
288 final int end = sequence.getEnd();
290 SequenceI ds = sequence.getDatasetSequence();
291 for (SearchResultMatchI m : matches)
293 SequenceI matched = m.getSequence();
294 if (matched != null && (matched == sequence || matched == ds)
295 && (m.getEnd() >= start) && (m.getStart() <= end))
304 public int[] getResults(SequenceI sequence, int start, int end)
306 if (matches.isEmpty())
313 int resultLength, matchStart = 0, matchEnd = 0;
316 for (SearchResultMatchI _m : matches)
321 if (m.sequence == sequence
322 || m.sequence == sequence.getDatasetSequence())
325 matchStart = sequence.findIndex(m.start) - 1;
326 matchEnd = m.start == m.end ? matchStart
327 : sequence.findIndex(m.end) - 1;
332 if (matchStart <= end && matchEnd >= start)
334 if (matchStart < start)
346 result = new int[] { matchStart, matchEnd };
350 resultLength = result.length;
351 tmp = new int[resultLength + 2];
352 System.arraycopy(result, 0, tmp, 0, resultLength);
354 result[resultLength] = matchStart;
355 result[resultLength + 1] = matchEnd;
361 // jalview.bin.Console.errPrintln("Outwith bounds!" +
362 // matchStart+">"+end +" or "
363 // + matchEnd+"<"+start);
371 public int markColumns(SequenceCollectionI sqcol, BitSet bs)
374 BitSet mask = new BitSet();
375 int startRes = sqcol.getStartRes();
376 int endRes = sqcol.getEndRes();
378 for (SequenceI s : sqcol.getSequences())
380 int[] cols = getResults(s, startRes, endRes);
383 for (int pair = 0; pair < cols.length; pair += 2)
385 mask.set(cols[pair], cols[pair + 1] + 1);
389 // compute columns that were newly selected
390 BitSet original = (BitSet) bs.clone();
392 count = mask.cardinality() - original.cardinality();
393 // and mark ranges not already marked
399 public int getCount()
405 public boolean isEmpty()
407 return matches.isEmpty();
411 public List<SearchResultMatchI> getResults()
417 * Return the results as a list of matches [seq1/from-to, seq2/from-to, ...]
422 public String toString()
424 return matches == null ? "" : matches.toString();
428 * Hashcode is derived from the list of matches. This ensures that when two
429 * SearchResults objects satisfy the test for equals(), then they have the
432 * @see Match#hashCode()
433 * @see java.util.AbstractList#hashCode()
436 public int hashCode()
438 return matches.hashCode();
442 * Two SearchResults are considered equal if they contain the same matches
443 * (Sequence, start position, end position) in the same order
445 * @see Match#equals(Object)
448 public boolean equals(Object obj)
450 if (obj == null || !(obj instanceof SearchResultsI))
454 SearchResultsI sr = (SearchResultsI) obj;
455 return matches.equals(sr.getResults());
459 public void addSearchResults(SearchResultsI toAdd)
461 matches.addAll(toAdd.getResults());
465 public List<SequenceI> getMatchingSubSequences()
467 List<SequenceI> seqs = new ArrayList<>();
470 * assemble dataset sequences, and template new sequence features,
471 * for the amend features dialog
473 for (SearchResultMatchI match : matches)
475 SequenceI seq = match.getSequence();
476 while (seq.getDatasetSequence() != null)
478 seq = seq.getDatasetSequence();
480 // getSubSequence is index-base0, findIndex returns index-base1
481 seqs.add(seq.getSubSequence(seq.findIndex(match.getStart()) - 1,
482 seq.findIndex(match.getEnd())));