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.Collections;
26 import java.util.List;
27 import java.util.SortedSet;
28 import java.util.TreeSet;
30 import com.google.common.collect.Lists;
33 * Holds a list of search result matches, where each match is a contiguous
34 * stretch of a single sequence.
36 * @author gmcarstairs amwaterhouse
39 public class SearchResults implements SearchResultsI
43 private SortedSet<SearchResultMatchI> matches = new TreeSet<>();
46 * One match consists of a sequence reference, start and end positions.
47 * Discontiguous ranges in a sequence require two or more Match objects.
49 public class Match implements SearchResultMatchI, Comparable<SearchResultMatchI>
51 final SequenceI sequence;
54 * Start position of match in sequence (base 1)
59 * End position (inclusive) (base 1)
64 * create a Match on a range of sequence. Match always holds region in
65 * forwards order, even if given in reverse order (such as from a mapping to
66 * a reverse strand); this avoids trouble for routines that highlight search
72 * start position of matched range (base 1)
74 * end of matched range (inclusive, base 1)
76 public Match(SequenceI seq, int start, int end)
81 * always hold in forwards order, even if given in reverse order
82 * (such as from a mapping to a reverse strand); this avoids
83 * trouble for routines that highlight search results etc
92 // TODO: JBP could mark match as being specified in reverse direction
94 // by caller ? e.g. visualizing reverse strand highlight
101 public SequenceI getSequence()
107 public int getStart()
119 * Returns a representation as "seqid/start-end"
122 public String toString()
124 StringBuilder sb = new StringBuilder();
125 if (sequence != null)
127 sb.append(sequence.getName()).append("/");
129 sb.append(start).append("-").append(end);
130 return sb.toString();
134 * Hashcode is the hashcode of the matched sequence plus a hash of start and
135 * end positions. Match objects that pass the test for equals are guaranteed
136 * to have the same hashcode.
139 public int hashCode()
141 int hash = sequence == null ? 0 : sequence.hashCode();
148 * Two Match objects are equal if they are for the same sequence, start and
152 public boolean equals(Object obj)
154 if (obj == null || !(obj instanceof SearchResultMatchI))
158 SearchResultMatchI m = (SearchResultMatchI) obj;
159 return (sequence == m.getSequence() && start == m.getStart()
160 && end == m.getEnd());
164 public boolean contains(SequenceI seq, int from, int to)
166 return (sequence == seq && start <= from && end >= to);
169 public boolean adjacent(SequenceI seq, int from, int to)
171 return (sequence == seq && ((start <= from && end >= to) || (from<=(end+1) && to >=(end+1)) || (from<=(start-1) && to>=(start-1))));
175 public int compareTo(SearchResultMatchI o)
177 if (start<o.getStart())
181 if (start > o.getStart())
185 if (end < o.getEnd())
189 if (end > o.getEnd())
193 if (sequence!=o.getSequence())
195 int hashc =sequence.hashCode(),oseq=o.getSequence().hashCode();
196 return (hashc < oseq) ? -1 : 1;
204 public SearchResultMatchI addResult(SequenceI seq, int start, int end)
206 Match m = new Match(seq, start, end);
207 if (!matches.contains(m))
216 public void addResult(SequenceI seq, int[] positions)
219 * we only increment the match count by 1 - or not at all,
220 * if the matches are all duplicates of existing
222 int beforeCount = count;
223 for (int i = 0; i < positions.length - 1; i += 2)
225 addResult(seq, positions[i], positions[i + 1]);
227 if (count > beforeCount)
229 count = beforeCount + 1;
235 public boolean appendResult(SequenceI sequence, int start, int end)
238 Match m = new Match(sequence, start, end);
241 if (matches.contains(m))
245 boolean appending=false;
247 // we dynamically maintain an interval to add as we test each range in the list
249 int cstart=start,cend=end;
250 List<SearchResultMatchI> toRemove=new ArrayList<>();
251 for (SearchResultMatchI thatm:matches)
253 if (thatm.getSequence()==sequence)
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)
274 matches.removeAll(toRemove);
276 matches.add(new Match(sequence,cstart,cend));
282 public boolean involvesSequence(SequenceI sequence)
284 final int start = sequence.getStart();
285 final int end = sequence.getEnd();
287 SequenceI ds = sequence.getDatasetSequence();
288 for (SearchResultMatchI m : matches)
290 SequenceI matched = m.getSequence();
291 if (matched != null && (matched == sequence || matched == ds)
292 && (m.getEnd() >= start) && (m.getStart() <= end))
301 public int[] getResults(SequenceI sequence, int start, int end)
303 if (matches.isEmpty())
310 int resultLength, matchStart = 0, matchEnd = 0;
313 for (SearchResultMatchI _m : matches)
318 if (m.sequence == sequence
319 || m.sequence == sequence.getDatasetSequence())
322 matchStart = sequence.findIndex(m.start) - 1;
323 matchEnd = m.start == m.end ? matchStart
324 : sequence.findIndex(m.end) - 1;
329 if (matchStart <= end && matchEnd >= start)
331 if (matchStart < start)
343 result = new int[] { matchStart, matchEnd };
347 resultLength = result.length;
348 tmp = new int[resultLength + 2];
349 System.arraycopy(result, 0, tmp, 0, resultLength);
351 result[resultLength] = matchStart;
352 result[resultLength + 1] = matchEnd;
358 // System.err.println("Outwith bounds!" + matchStart+">"+end +" or "
359 // + matchEnd+"<"+start);
367 public int markColumns(SequenceCollectionI sqcol, BitSet bs)
370 BitSet mask = new BitSet();
371 int startRes = sqcol.getStartRes();
372 int endRes = sqcol.getEndRes();
374 for (SequenceI s : sqcol.getSequences())
376 int[] cols = getResults(s, startRes, endRes);
379 for (int pair = 0; pair < cols.length; pair += 2)
381 mask.set(cols[pair], cols[pair + 1] + 1);
385 // compute columns that were newly selected
386 BitSet original = (BitSet) bs.clone();
388 count = mask.cardinality() - original.cardinality();
389 // and mark ranges not already marked
395 public int getCount()
401 public boolean isEmpty()
403 return matches.isEmpty();
407 public List<SearchResultMatchI> getResults()
409 return List.copyOf(matches);
413 * Return the results as a list of matches [seq1/from-to, seq2/from-to, ...]
418 public String toString()
420 return matches == null ? "" : matches.toString();
424 * Hashcode is derived from the list of matches. This ensures that when two
425 * SearchResults objects satisfy the test for equals(), then they have the
428 * @see Match#hashCode()
429 * @see java.util.AbstractList#hashCode()
432 public int hashCode()
434 return matches.hashCode();
438 * Two SearchResults are considered equal if they contain the same matches
439 * (Sequence, start position, end position) in the same order
441 * @see Match#equals(Object)
444 public boolean equals(Object obj)
446 if (obj == null || !(obj instanceof SearchResultsI))
450 SearchResultsI sr = (SearchResultsI) obj;
451 return matches.equals(sr.getResults());
455 public void addSearchResults(SearchResultsI toAdd)
457 matches.addAll(toAdd.getResults());
461 public List<SequenceI> getMatchingSubSequences()
463 List<SequenceI> seqs = new ArrayList<>();
466 * assemble dataset sequences, and template new sequence features,
467 * for the amend features dialog
469 for (SearchResultMatchI match : matches)
471 SequenceI seq = match.getSequence();
472 while (seq.getDatasetSequence() != null)
474 seq = seq.getDatasetSequence();
476 // getSubSequence is index-base0, findIndex returns index-base1
477 seqs.add(seq.getSubSequence(seq.findIndex(match.getStart()) - 1,
478 seq.findIndex(match.getEnd())));