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.List;
27 * Holds a list of search result matches, where each match is a contiguous
28 * stretch of a single sequence.
33 public class SearchResults
36 private List<Match> matches = new ArrayList<Match>();
46 public Match(SequenceI seq, int start, int end)
53 public SequenceI getSequence()
70 * This method replaces the old search results which merely held an alignment
71 * index of search matches. This broke when sequences were moved around the
81 public void addResult(SequenceI seq, int start, int end)
83 matches.add(new Match(seq, start, end));
87 * Quickly check if the given sequence is referred to in the search results
90 * (specific alignment sequence or a dataset sequence)
91 * @return true if the results involve sequence
93 public boolean involvesSequence(SequenceI sequence)
95 SequenceI ds = sequence.getDatasetSequence();
96 for (Match m : matches)
98 if (m.sequence != null
99 && (m.sequence == sequence || m.sequence == ds))
108 * This Method returns the search matches which lie between the start and end
109 * points of the sequence in question. It is optimised for returning objects
110 * for drawing on SequenceCanvas
112 public int[] getResults(SequenceI sequence, int start, int end)
114 if (matches.isEmpty())
121 int resultLength, matchStart = 0, matchEnd = 0;
123 for (Match m : matches)
126 if (m.sequence == sequence)
129 // locate aligned position
130 matchStart = sequence.findIndex(m.start) - 1;
131 matchEnd = sequence.findIndex(m.end) - 1;
133 else if (m.sequence == sequence.getDatasetSequence())
136 // locate region in local context
137 matchStart = sequence.findIndex(m.start) - 1;
138 matchEnd = sequence.findIndex(m.end) - 1;
142 if (matchStart <= end && matchEnd >= start)
144 if (matchStart < start)
157 { matchStart, matchEnd };
161 resultLength = result.length;
162 tmp = new int[resultLength + 2];
163 System.arraycopy(result, 0, tmp, 0, resultLength);
165 result[resultLength] = matchStart;
166 result[resultLength + 1] = matchEnd;
172 // System.err.println("Outwith bounds!" + matchStart+">"+end +" or "
173 // + matchEnd+"<"+start);
182 return matches.size();
185 public SequenceI getResultSequence(int index)
187 return matches.get(index).sequence;
191 * Returns the start position of the i'th match in the search results.
196 public int getResultStart(int i)
198 return matches.get(i).start;
202 * Returns the end position of the i'th match in the search results.
207 public int getResultEnd(int i)
209 return matches.get(i).end;
213 * Returns true if no search result matches are held.
217 public boolean isEmpty()
219 return matches.isEmpty();
223 * Returns the list of matches.
227 public List<Match> getResults()