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.List;
28 * Holds a list of search result matches, where each match is a contiguous
29 * stretch of a single sequence.
34 public class SearchResults
37 private List<Match> matches = new ArrayList<Match>();
44 * Start position of match in sequence (base 1)
49 * End position (inclusive) (base 1)
59 * start position of matched range (base 1)
61 * end of matched range (inclusive, base 1)
63 public Match(SequenceI seq, int start, int end)
70 public SequenceI getSequence()
86 * Returns the string of characters in the matched region.
89 public String toString()
91 char[] chars = sequence.getSequence();
92 // convert start/end to base 0 (with bounds check)
93 final int from = Math.max(start - 1, 0);
94 final int to = Math.min(end, chars.length + 1);
95 return String.valueOf(Arrays.copyOfRange(chars, from, to));
98 public void setSequence(SequenceI seq)
105 * This method replaces the old search results which merely held an alignment
106 * index of search matches. This broke when sequences were moved around the
116 public void addResult(SequenceI seq, int start, int end)
118 matches.add(new Match(seq, start, end));
122 * Quickly check if the given sequence is referred to in the search results
125 * (specific alignment sequence or a dataset sequence)
126 * @return true if the results involve sequence
128 public boolean involvesSequence(SequenceI sequence)
130 SequenceI ds = sequence.getDatasetSequence();
131 for (Match m : matches)
133 if (m.sequence != null
134 && (m.sequence == sequence || m.sequence == ds))
143 * This Method returns the search matches which lie between the start and end
144 * points of the sequence in question. It is optimised for returning objects
145 * for drawing on SequenceCanvas
147 public int[] getResults(SequenceI sequence, int start, int end)
149 if (matches.isEmpty())
156 int resultLength, matchStart = 0, matchEnd = 0;
158 for (Match m : matches)
161 if (m.sequence == sequence)
164 // locate aligned position
165 matchStart = sequence.findIndex(m.start) - 1;
166 matchEnd = sequence.findIndex(m.end) - 1;
168 else if (m.sequence == sequence.getDatasetSequence())
171 // locate region in local context
172 matchStart = sequence.findIndex(m.start) - 1;
173 matchEnd = sequence.findIndex(m.end) - 1;
177 if (matchStart <= end && matchEnd >= start)
179 if (matchStart < start)
192 { matchStart, matchEnd };
196 resultLength = result.length;
197 tmp = new int[resultLength + 2];
198 System.arraycopy(result, 0, tmp, 0, resultLength);
200 result[resultLength] = matchStart;
201 result[resultLength + 1] = matchEnd;
207 // System.err.println("Outwith bounds!" + matchStart+">"+end +" or "
208 // + matchEnd+"<"+start);
217 return matches.size();
220 public SequenceI getResultSequence(int index)
222 return matches.get(index).sequence;
226 * Returns the start position of the i'th match in the search results.
231 public int getResultStart(int i)
233 return matches.get(i).start;
237 * Returns the end position of the i'th match in the search results.
242 public int getResultEnd(int i)
244 return matches.get(i).end;
248 * Returns true if no search result matches are held.
252 public boolean isEmpty()
254 return matches.isEmpty();
258 * Returns the list of matches.
262 public List<Match> getResults()
268 * Return the results as a string of characters. Meant for use when the
269 * context ensures that all matches are to regions of the same sequence
270 * (otherwise the result is meaningless).
275 public String toString()
277 StringBuilder result = new StringBuilder(256);
278 for (Match m : matches)
280 result.append(m.toString());
282 return result.toString();