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;
26 import java.util.SortedSet;
27 import java.util.TreeSet;
30 * Holds a list of search result matches, where each match is a contiguous
31 * stretch of a single sequence.
33 * @author gmcarstairs amwaterhouse
36 public class SearchResults implements SearchResultsI
40 private SortedSet<SearchResultMatchI> matches = new TreeSet<>();
43 * One match consists of a sequence reference, start and end positions.
44 * Discontiguous ranges in a sequence require two or more Match objects.
46 public class Match implements SearchResultMatchI, Comparable<SearchResultMatchI>
48 final SequenceI sequence;
51 * Start position of match in sequence (base 1)
56 * End position (inclusive) (base 1)
61 * create a Match on a range of sequence. Match always holds region in
62 * forwards order, even if given in reverse order (such as from a mapping to
63 * a reverse strand); this avoids trouble for routines that highlight search
69 * start position of matched range (base 1)
71 * end of matched range (inclusive, base 1)
73 public Match(SequenceI seq, int start, int end)
78 * always hold in forwards order, even if given in reverse order
79 * (such as from a mapping to a reverse strand); this avoids
80 * trouble for routines that highlight search results etc
89 // TODO: JBP could mark match as being specified in reverse direction
91 // by caller ? e.g. visualizing reverse strand highlight
98 public SequenceI getSequence()
104 public int getStart()
116 * Returns a representation as "seqid/start-end"
119 public String toString()
121 StringBuilder sb = new StringBuilder();
122 if (sequence != null)
124 sb.append(sequence.getName()).append("/");
126 sb.append(start).append("-").append(end);
127 return sb.toString();
131 * Hashcode is the hashcode of the matched sequence plus a hash of start and
132 * end positions. Match objects that pass the test for equals are guaranteed
133 * to have the same hashcode.
136 public int hashCode()
138 int hash = sequence == null ? 0 : sequence.hashCode();
145 * Two Match objects are equal if they are for the same sequence, start and
149 public boolean equals(Object obj)
151 if (obj == null || !(obj instanceof SearchResultMatchI))
155 SearchResultMatchI m = (SearchResultMatchI) obj;
156 return (sequence == m.getSequence() && start == m.getStart()
157 && end == m.getEnd());
161 public boolean contains(SequenceI seq, int from, int to)
163 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) || (from<=(end+1) && to >=(end+1)) || (from<=(start-1) && to>=(start-1))));
172 public int compareTo(SearchResultMatchI o)
174 if (start<o.getStart())
178 if (start > o.getStart())
182 if (end < o.getEnd())
186 if (end > o.getEnd())
190 if (sequence!=o.getSequence())
192 int hashc =sequence.hashCode(),oseq=o.getSequence().hashCode();
193 return (hashc < oseq) ? -1 : 1;
201 public SearchResultMatchI addResult(SequenceI seq, int start, int end)
203 Match m = new Match(seq, start, end);
204 if (!matches.contains(m))
213 public void addResult(SequenceI seq, int[] positions)
216 * we only increment the match count by 1 - or not at all,
217 * if the matches are all duplicates of existing
219 int beforeCount = count;
220 for (int i = 0; i < positions.length - 1; i += 2)
222 addResult(seq, positions[i], positions[i + 1]);
224 if (count > beforeCount)
226 count = beforeCount + 1;
232 public boolean appendResult(SequenceI sequence, int start, int end)
235 Match m = new Match(sequence, start, end);
238 if (matches.contains(m))
242 boolean appending=false;
244 // we dynamically maintain an interval to add as we test each range in the list
246 int cstart=start,cend=end;
247 List<SearchResultMatchI> toRemove=new ArrayList<>();
248 for (SearchResultMatchI thatm:matches)
250 if (thatm.getSequence()==sequence)
252 if (thatm.adjacent(sequence, cstart, cend))
254 // update the match to add with the adjacent start/end
255 start = Math.min(m.start, thatm.getStart());
256 end = Math.max(m.end, thatm.getEnd());
257 // and check if we keep or remove the old one
258 if (thatm.getStart()!=start || thatm.getEnd()!=end)
271 matches.removeAll(toRemove);
273 matches.add(new Match(sequence,cstart,cend));
279 public boolean involvesSequence(SequenceI sequence)
281 final int start = sequence.getStart();
282 final int end = sequence.getEnd();
284 SequenceI ds = sequence.getDatasetSequence();
285 for (SearchResultMatchI m : matches)
287 SequenceI matched = m.getSequence();
288 if (matched != null && (matched == sequence || matched == ds)
289 && (m.getEnd() >= start) && (m.getStart() <= end))
298 public int[] getResults(SequenceI sequence, int start, int end)
300 if (matches.isEmpty())
307 int resultLength, matchStart = 0, matchEnd = 0;
310 for (SearchResultMatchI _m : matches)
315 if (m.sequence == sequence
316 || m.sequence == sequence.getDatasetSequence())
319 matchStart = sequence.findIndex(m.start) - 1;
320 matchEnd = m.start == m.end ? matchStart
321 : sequence.findIndex(m.end) - 1;
326 if (matchStart <= end && matchEnd >= start)
328 if (matchStart < start)
340 result = new int[] { matchStart, matchEnd };
344 resultLength = result.length;
345 tmp = new int[resultLength + 2];
346 System.arraycopy(result, 0, tmp, 0, resultLength);
348 result[resultLength] = matchStart;
349 result[resultLength + 1] = matchEnd;
355 // System.err.println("Outwith bounds!" + matchStart+">"+end +" or "
356 // + matchEnd+"<"+start);
364 public int markColumns(SequenceCollectionI sqcol, BitSet bs)
367 BitSet mask = new BitSet();
368 int startRes = sqcol.getStartRes();
369 int endRes = sqcol.getEndRes();
371 for (SequenceI s : sqcol.getSequences())
373 int[] cols = getResults(s, startRes, endRes);
376 for (int pair = 0; pair < cols.length; pair += 2)
378 mask.set(cols[pair], cols[pair + 1] + 1);
382 // compute columns that were newly selected
383 BitSet original = (BitSet) bs.clone();
385 count = mask.cardinality() - original.cardinality();
386 // and mark ranges not already marked
392 public int getCount()
398 public boolean isEmpty()
400 return matches.isEmpty();
404 public List<SearchResultMatchI> getResults()
406 return List.copyOf(matches);
410 * Return the results as a list of matches [seq1/from-to, seq2/from-to, ...]
415 public String toString()
417 return matches == null ? "" : matches.toString();
421 * Hashcode is derived from the list of matches. This ensures that when two
422 * SearchResults objects satisfy the test for equals(), then they have the
425 * @see Match#hashCode()
426 * @see java.util.AbstractList#hashCode()
429 public int hashCode()
431 return matches.hashCode();
435 * Two SearchResults are considered equal if they contain the same matches
436 * (Sequence, start position, end position) in the same order
438 * @see Match#equals(Object)
441 public boolean equals(Object obj)
443 if (obj == null || !(obj instanceof SearchResultsI))
447 SearchResultsI sr = (SearchResultsI) obj;
448 return matches.equals(sr.getResults());
452 public void addSearchResults(SearchResultsI toAdd)
454 matches.addAll(toAdd.getResults());
458 public List<SequenceI> getMatchingSubSequences()
460 List<SequenceI> seqs = new ArrayList<>();
463 * assemble dataset sequences, and template new sequence features,
464 * for the amend features dialog
466 for (SearchResultMatchI match : matches)
468 SequenceI seq = match.getSequence();
469 while (seq.getDatasetSequence() != null)
471 seq = seq.getDatasetSequence();
473 // getSubSequence is index-base0, findIndex returns index-base1
474 seqs.add(seq.getSubSequence(seq.findIndex(match.getStart()) - 1,
475 seq.findIndex(match.getEnd())));