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.
44 public class Match implements SearchResultMatchI, Comparable<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);
164 public boolean adjacent(SequenceI seq, int from, int to)
166 return (sequence == seq && ((start <= from && end >= to) || (from<=(end+1) && to >=(end+1)) || (from<=(start-1) && to>=(start-1))));
170 public int compareTo(SearchResultMatchI o)
172 if (start<o.getStart())
176 if (start > o.getStart())
180 if (end < o.getEnd())
184 if (end > o.getEnd())
188 if (sequence!=o.getSequence())
190 int hashc =sequence.hashCode(),oseq=o.getSequence().hashCode();
191 return (hashc < oseq) ? -1 : 1;
199 public SearchResultMatchI addResult(SequenceI seq, int start, int end)
201 Match m = new Match(seq, start, end);
202 if (!matches.contains(m))
211 public void addResult(SequenceI seq, int[] positions)
214 * we only increment the match count by 1 - or not at all,
215 * if the matches are all duplicates of existing
217 int beforeCount = count;
218 for (int i = 0; i < positions.length - 1; i += 2)
220 addResult(seq, positions[i], positions[i + 1]);
222 if (count > beforeCount)
224 count = beforeCount + 1;
230 public boolean appendResult(SequenceI sequence, int start, int end)
233 Match m = new Match(sequence, start, end);
235 boolean appending=false;
237 // we dynamically maintain an interval to add as we test each range in the list
239 int cstart=start,cend=end;
240 List<SearchResultMatchI> toRemove=new ArrayList<>();
241 for (SearchResultMatchI thatm:matches)
243 if (thatm.getSequence()==sequence)
245 if (thatm.contains(sequence,cstart,cend))
247 // found a match containing the current range. nothing else to do except report if we operated on the list
250 if (thatm.adjacent(sequence, cstart, cend))
252 // update the match to add with the adjacent start/end
253 start = Math.min(m.start, thatm.getStart());
254 end = Math.max(m.end, thatm.getEnd());
255 // and check if we keep or remove the old one
256 if (thatm.getStart()!=start || thatm.getEnd()!=end)
269 matches.removeAll(toRemove);
271 matches.add(new Match(sequence,cstart,cend));
277 public boolean involvesSequence(SequenceI sequence)
279 final int start = sequence.getStart();
280 final int end = sequence.getEnd();
282 SequenceI ds = sequence.getDatasetSequence();
283 for (SearchResultMatchI m : matches)
285 SequenceI matched = m.getSequence();
286 if (matched != null && (matched == sequence || matched == ds)
287 && (m.getEnd() >= start) && (m.getStart() <= end))
296 public int[] getResults(SequenceI sequence, int start, int end)
298 if (matches.isEmpty())
305 int resultLength, matchStart = 0, matchEnd = 0;
308 for (SearchResultMatchI _m : matches)
313 if (m.sequence == sequence
314 || m.sequence == sequence.getDatasetSequence())
317 matchStart = sequence.findIndex(m.start) - 1;
318 matchEnd = m.start == m.end ? matchStart
319 : sequence.findIndex(m.end) - 1;
324 if (matchStart <= end && matchEnd >= start)
326 if (matchStart < start)
338 result = new int[] { matchStart, matchEnd };
342 resultLength = result.length;
343 tmp = new int[resultLength + 2];
344 System.arraycopy(result, 0, tmp, 0, resultLength);
346 result[resultLength] = matchStart;
347 result[resultLength + 1] = matchEnd;
353 // jalview.bin.Console.errPrintln("Outwith bounds!" + matchStart+">"+end +" or "
354 // + matchEnd+"<"+start);
362 public int markColumns(SequenceCollectionI sqcol, BitSet bs)
365 BitSet mask = new BitSet();
366 int startRes = sqcol.getStartRes();
367 int endRes = sqcol.getEndRes();
369 for (SequenceI s : sqcol.getSequences())
371 int[] cols = getResults(s, startRes, endRes);
374 for (int pair = 0; pair < cols.length; pair += 2)
376 mask.set(cols[pair], cols[pair + 1] + 1);
380 // compute columns that were newly selected
381 BitSet original = (BitSet) bs.clone();
383 count = mask.cardinality() - original.cardinality();
384 // and mark ranges not already marked
390 public int getCount()
396 public boolean isEmpty()
398 return matches.isEmpty();
402 public List<SearchResultMatchI> getResults()
408 * Return the results as a list of matches [seq1/from-to, seq2/from-to, ...]
413 public String toString()
415 return matches == null ? "" : matches.toString();
419 * Hashcode is derived from the list of matches. This ensures that when two
420 * SearchResults objects satisfy the test for equals(), then they have the
423 * @see Match#hashCode()
424 * @see java.util.AbstractList#hashCode()
427 public int hashCode()
429 return matches.hashCode();
433 * Two SearchResults are considered equal if they contain the same matches
434 * (Sequence, start position, end position) in the same order
436 * @see Match#equals(Object)
439 public boolean equals(Object obj)
441 if (obj == null || !(obj instanceof SearchResultsI))
445 SearchResultsI sr = (SearchResultsI) obj;
446 return matches.equals(sr.getResults());
450 public void addSearchResults(SearchResultsI toAdd)
452 matches.addAll(toAdd.getResults());
456 public List<SequenceI> getMatchingSubSequences()
458 List<SequenceI> seqs = new ArrayList<>();
461 * assemble dataset sequences, and template new sequence features,
462 * for the amend features dialog
464 for (SearchResultMatchI match : matches)
466 SequenceI seq = match.getSequence();
467 while (seq.getDatasetSequence() != null)
469 seq = seq.getDatasetSequence();
471 // getSubSequence is index-base0, findIndex returns index-base1
472 seqs.add(seq.getSubSequence(seq.findIndex(match.getStart()) - 1,
473 seq.findIndex(match.getEnd())));