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)
53 public Match(SequenceI seq, int start, int end)
60 public SequenceI getSequence()
76 * Returns the string of characters in the matched region.
79 public String toString()
81 char[] chars = sequence.getSequence();
82 return String.valueOf(Arrays.copyOfRange(chars, start - 1, end));
87 * This method replaces the old search results which merely held an alignment
88 * index of search matches. This broke when sequences were moved around the
98 public void addResult(SequenceI seq, int start, int end)
100 matches.add(new Match(seq, start, end));
104 * Quickly check if the given sequence is referred to in the search results
107 * (specific alignment sequence or a dataset sequence)
108 * @return true if the results involve sequence
110 public boolean involvesSequence(SequenceI sequence)
112 SequenceI ds = sequence.getDatasetSequence();
113 for (Match m : matches)
115 if (m.sequence != null
116 && (m.sequence == sequence || m.sequence == ds))
125 * This Method returns the search matches which lie between the start and end
126 * points of the sequence in question. It is optimised for returning objects
127 * for drawing on SequenceCanvas
129 public int[] getResults(SequenceI sequence, int start, int end)
131 if (matches.isEmpty())
138 int resultLength, matchStart = 0, matchEnd = 0;
140 for (Match m : matches)
143 if (m.sequence == sequence)
146 // locate aligned position
147 matchStart = sequence.findIndex(m.start) - 1;
148 matchEnd = sequence.findIndex(m.end) - 1;
150 else if (m.sequence == sequence.getDatasetSequence())
153 // locate region in local context
154 matchStart = sequence.findIndex(m.start) - 1;
155 matchEnd = sequence.findIndex(m.end) - 1;
159 if (matchStart <= end && matchEnd >= start)
161 if (matchStart < start)
174 { matchStart, matchEnd };
178 resultLength = result.length;
179 tmp = new int[resultLength + 2];
180 System.arraycopy(result, 0, tmp, 0, resultLength);
182 result[resultLength] = matchStart;
183 result[resultLength + 1] = matchEnd;
189 // System.err.println("Outwith bounds!" + matchStart+">"+end +" or "
190 // + matchEnd+"<"+start);
199 return matches.size();
202 public SequenceI getResultSequence(int index)
204 return matches.get(index).sequence;
208 * Returns the start position of the i'th match in the search results.
213 public int getResultStart(int i)
215 return matches.get(i).start;
219 * Returns the end position of the i'th match in the search results.
224 public int getResultEnd(int i)
226 return matches.get(i).end;
230 * Returns true if no search result matches are held.
234 public boolean isEmpty()
236 return matches.isEmpty();
240 * Returns the list of matches.
244 public List<Match> getResults()
250 * Return the results as a string of characters. Meant for use when the
251 * context ensures that all matches are to regions of the same sequence
252 * (otherwise the result is meaningless).
257 public String toString()
259 StringBuilder result = new StringBuilder(256);
260 for (Match m : matches)
262 result.append(m.toString());
264 return result.toString();