2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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));
100 * This method replaces the old search results which merely held an alignment
101 * index of search matches. This broke when sequences were moved around the
111 public void addResult(SequenceI seq, int start, int end)
113 matches.add(new Match(seq, start, end));
117 * Quickly check if the given sequence is referred to in the search results
120 * (specific alignment sequence or a dataset sequence)
121 * @return true if the results involve sequence
123 public boolean involvesSequence(SequenceI sequence)
125 SequenceI ds = sequence.getDatasetSequence();
126 for (Match m : matches)
128 if (m.sequence != null
129 && (m.sequence == sequence || m.sequence == ds))
138 * This Method returns the search matches which lie between the start and end
139 * points of the sequence in question. It is optimised for returning objects
140 * for drawing on SequenceCanvas
142 public int[] getResults(SequenceI sequence, int start, int end)
144 if (matches.isEmpty())
151 int resultLength, matchStart = 0, matchEnd = 0;
153 for (Match m : matches)
156 if (m.sequence == sequence)
159 // locate aligned position
160 matchStart = sequence.findIndex(m.start) - 1;
161 matchEnd = sequence.findIndex(m.end) - 1;
163 else if (m.sequence == sequence.getDatasetSequence())
166 // locate region in local context
167 matchStart = sequence.findIndex(m.start) - 1;
168 matchEnd = sequence.findIndex(m.end) - 1;
172 if (matchStart <= end && matchEnd >= start)
174 if (matchStart < start)
187 { matchStart, matchEnd };
191 resultLength = result.length;
192 tmp = new int[resultLength + 2];
193 System.arraycopy(result, 0, tmp, 0, resultLength);
195 result[resultLength] = matchStart;
196 result[resultLength + 1] = matchEnd;
202 // System.err.println("Outwith bounds!" + matchStart+">"+end +" or "
203 // + matchEnd+"<"+start);
212 return matches.size();
215 public SequenceI getResultSequence(int index)
217 return matches.get(index).sequence;
221 * Returns the start position of the i'th match in the search results.
226 public int getResultStart(int i)
228 return matches.get(i).start;
232 * Returns the end position of the i'th match in the search results.
237 public int getResultEnd(int i)
239 return matches.get(i).end;
243 * Returns true if no search result matches are held.
247 public boolean isEmpty()
249 return matches.isEmpty();
253 * Returns the list of matches.
257 public List<Match> getResults()
263 * Return the results as a string of characters. Meant for use when the
264 * context ensures that all matches are to regions of the same sequence
265 * (otherwise the result is meaningless).
270 public String toString()
272 StringBuilder result = new StringBuilder(256);
273 for (Match m : matches)
275 result.append(m.toString());
277 return result.toString();