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;
26 public class SearchResults
29 private List<Match> matches = new ArrayList<Match>();
32 * This method replaces the old search results which merely held an alignment
33 * index of search matches. This broke when sequences were moved around the
43 public void addResult(SequenceI seq, int start, int end)
45 matches.add(new Match(seq, start, end));
49 * Quickly check if the given sequence is referred to in the search results
52 * (specific alignment sequence or a dataset sequence)
53 * @return true if the results involve sequence
55 public boolean involvesSequence(SequenceI sequence)
57 SequenceI ds = sequence.getDatasetSequence();
58 for (Match m : matches)
60 if (m.sequence != null
61 && (m.sequence == sequence || m.sequence == ds))
70 * This Method returns the search matches which lie between the start and end
71 * points of the sequence in question. It is optimised for returning objects
72 * for drawing on SequenceCanvas
74 public int[] getResults(SequenceI sequence, int start, int end)
76 if (matches.isEmpty())
83 int resultLength, matchStart = 0, matchEnd = 0;
85 for (Match m : matches)
88 if (m.sequence == sequence)
91 // locate aligned position
92 matchStart = sequence.findIndex(m.start) - 1;
93 matchEnd = sequence.findIndex(m.end) - 1;
95 else if (m.sequence == sequence.getDatasetSequence())
98 // locate region in local context
99 matchStart = sequence.findIndex(m.start) - 1;
100 matchEnd = sequence.findIndex(m.end) - 1;
104 if (matchStart <= end && matchEnd >= start)
106 if (matchStart < start)
119 { matchStart, matchEnd };
123 resultLength = result.length;
124 tmp = new int[resultLength + 2];
125 System.arraycopy(result, 0, tmp, 0, resultLength);
127 result[resultLength] = matchStart;
128 result[resultLength + 1] = matchEnd;
134 // System.err.println("Outwith bounds!" + matchStart+">"+end +" or "
135 // + matchEnd+"<"+start);
144 return matches.size();
147 public SequenceI getResultSequence(int index)
149 return matches.get(index).sequence;
152 public int getResultStart(int index)
154 return matches.get(index).start;
157 public int getResultEnd(int index)
159 return matches.get(index).end;
170 public Match(SequenceI seq, int start, int end)
179 * Returns true if no search result matches are held.
183 public boolean isEmpty()
185 return matches.isEmpty();