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.analysis;
23 import jalview.api.FinderI;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.Range;
26 import jalview.datamodel.SearchResultMatchI;
27 import jalview.datamodel.SearchResults;
28 import jalview.datamodel.SearchResultsI;
29 import jalview.datamodel.SequenceGroup;
30 import jalview.datamodel.SequenceI;
31 import jalview.datamodel.VisibleContigsIterator;
32 import jalview.util.Comparison;
34 import java.util.List;
35 import java.util.Vector;
37 import com.stevesoft.pat.Regex;
40 * Implements the search algorithm for the Find dialog
42 public class Finder implements FinderI
45 * matched residue locations
47 private SearchResultsI searchResults;
50 * sequences matched by id or description
52 private Vector<SequenceI> idMatches;
55 * the alignment to search over
57 private AlignmentI alignment;
60 * (optional) selection to restrict search to
62 private SequenceGroup selection;
65 * sequence index in alignment to search from
67 private int sequenceIndex;
70 * column position in sequence to search from, base 0
71 * - absolute column number including any hidden columns
72 * (position after start of last match for a repeat search)
74 private int columnIndex;
77 * Constructor for searching an alignment
81 public Finder(AlignmentI al)
84 this.sequenceIndex = 0;
85 this.columnIndex = -1;
89 public void findAll(String theSearchString, SequenceGroup sg,
90 boolean matchCase, boolean searchDescription)
93 * search from the start
98 doFind(theSearchString, sg, matchCase, searchDescription, true);
101 * reset to start for next search
108 public void findNext(String theSearchString, SequenceGroup sg,
109 boolean matchCase, boolean searchDescription)
111 doFind(theSearchString, sg, matchCase, searchDescription, false);
113 if (searchResults.isEmpty() && idMatches.isEmpty())
116 * search failed - reset to start for next search
124 * Performs a 'find next' or 'find all', optionally restricted to the
125 * specified selection region
127 * @param theSearchString
128 * @param selectionRegion
130 * @param searchDescription
133 protected void doFind(String theSearchString, SequenceGroup selectionRegion,
134 boolean matchCase, boolean searchDescription, boolean findAll)
136 this.selection = selectionRegion;
137 String searchString = matchCase ? theSearchString
138 : theSearchString.toUpperCase();
139 Regex searchPattern = new Regex(searchString);
140 searchPattern.setIgnoreCase(!matchCase);
142 searchResults = new SearchResults();
143 idMatches = new Vector<>();
145 if (selection != null && selection.getSize() < 1)
147 selection = null; // ? ignore column-only selection
150 int end = alignment.getHeight();
152 while (sequenceIndex < end)
154 SequenceI seq = alignment.getSequenceAt(sequenceIndex);
155 boolean found = findNextMatch(seq, searchString, searchPattern,
157 if (found && !findAll)
170 * Answers the start-end column range of the visible region of
171 * <code>sequence</code> starting at or after the given <code>column</code>.
172 * If there are no hidden columns, this just returns the remaining width of
173 * the sequence. The range is restricted to the current <code>selection</code>
174 * if there is one. Answers null if there are no visible columns at or after
175 * <code>column</code>.
177 protected Range getNextVisibleSequenceRegion(SequenceI sequence,
180 int seqColStart = column;
181 int seqColEnd = sequence.getLength() - 1;
184 * restrict search to (next) visible column region,
185 * in case there are hidden columns
187 VisibleContigsIterator visibleRegions = alignment.getHiddenColumns()
188 .getVisContigsIterator(column, alignment.getWidth(),
190 int[] visible = visibleRegions.hasNext() ? visibleRegions.next() : null;
193 columnIndex = seqColEnd + 1;
196 seqColStart = Math.max(seqColStart, visible[0]);
197 seqColEnd = Math.min(seqColEnd, visible[1]);
200 * restrict search to selected region if there is one
202 if (selection != null)
204 int selectionStart = selection.getStartRes();
205 int selectionEnd = selection.getEndRes();
206 if (selectionStart > seqColEnd || selectionEnd < seqColStart)
209 * sequence region doesn't overlap selection region
211 columnIndex = seqColEnd + 1;
214 seqColStart = Math.max(seqColStart, selectionStart);
215 seqColEnd = Math.min(seqColEnd, selectionEnd);
218 return new Range(seqColStart, seqColEnd);
222 * Finds the next match in the given sequence, starting at column at
223 * <code>columnIndex</code>. Answers true if a match is found, else false. If
224 * a match is found, <code>columnIndex</code> is advanced to the column after
225 * the start of the matched region, ready for a search from the next position.
228 * @param searchString
229 * @param searchPattern
230 * @param matchDescription
233 protected boolean findNextMatch(SequenceI seq, String searchString,
234 Regex searchPattern, boolean matchDescription)
236 if (selection != null && !selection.contains(seq))
239 * this sequence is not in the selection - advance to next sequence
247 * at start of sequence; try find by residue number, in sequence id,
248 * or (optionally) in sequence description
250 if (doNonMotifSearches(seq, searchString, searchPattern,
258 * search for next match in sequence string
260 int end = seq.getLength();
261 while (columnIndex < end)
263 if (searchNextVisibleRegion(seq, searchPattern))
272 * Searches the sequence, starting from <code>columnIndex</code>, and adds the
273 * next match (if any) to <code>searchResults</code>. The search is restricted
274 * to the next visible column region, and to the <code>selection</code> region
275 * if there is one. Answers true if a match is added, else false.
278 * @param searchPattern
281 protected boolean searchNextVisibleRegion(SequenceI seq, Regex searchPattern)
283 Range visible = getNextVisibleSequenceRegion(seq, columnIndex);
288 String seqString = seq.getSequenceAsString(visible.start, visible.end + 1);
289 String noGaps = AlignSeq.extractGaps(Comparison.GapChars, seqString);
291 if (searchPattern.search(noGaps))
293 int sequenceStartPosition = seq.findPosition(visible.start);
294 recordMatch(seq, searchPattern, sequenceStartPosition);
300 * no match - advance columnIndex past this visible region
301 * so the next visible region (if any) is searched next
303 columnIndex = visible.end + 1;
310 * Adds the match held in the <code>searchPattern</code> Regex to the
311 * <code>searchResults</code>, unless it is a subregion of the last match
312 * recorded. <code>columnIndex</code> is advanced to the position after the
313 * start of the matched region, ready for the next search. Answers true if a
314 * match was added, else false.
317 * @param searchPattern
318 * @param firstResiduePosition
321 protected boolean recordMatch(SequenceI seq, Regex searchPattern,
322 int firstResiduePosition)
325 * get start/end of the match in sequence coordinates
327 int offset = searchPattern.matchedFrom();
328 int matchStartPosition = firstResiduePosition + offset;
329 int matchEndPosition = matchStartPosition
330 + searchPattern.charsMatched() - 1;
333 * update columnIndex to next column after the start of the match
334 * (findIndex returns a value base 1, columnIndex is held base 0)
336 columnIndex = seq.findIndex(matchStartPosition);
339 * check that this match is not a subset of the previous one (JAL-2302)
341 List<SearchResultMatchI> matches = searchResults.getResults();
342 SearchResultMatchI lastMatch = matches.isEmpty() ? null
343 : matches.get(matches.size() - 1);
345 if (lastMatch == null || !lastMatch.contains(seq, matchStartPosition,
348 searchResults.addResult(seq, matchStartPosition, matchEndPosition);
356 * Does searches other than for residue patterns. Currently this includes
358 * <li>find residue by position (if search string is a number)</li>
359 * <li>match search string to sequence id</li>
360 * <li>match search string to sequence description (optional)</li>
362 * Answers true if a match is found, else false.
365 * @param searchString
366 * @param searchPattern
367 * @param includeDescription
370 protected boolean doNonMotifSearches(SequenceI seq, String searchString,
371 Regex searchPattern, boolean includeDescription)
374 * position sequence search to start of sequence
378 if (searchForResidueNumber(seq, searchString))
382 if (searchSequenceName(seq, searchPattern))
386 if (includeDescription && searchSequenceDescription(seq, searchPattern))
394 * Searches for a match with the sequence description, and if found, adds the
395 * sequence to the list of match ids (but not as a duplicate). Answers true if
396 * a match was added, else false.
399 * @param searchPattern
402 protected boolean searchSequenceDescription(SequenceI seq, Regex searchPattern)
404 String desc = seq.getDescription();
405 if (desc != null && searchPattern.search(desc) && !idMatches.contains(seq))
407 idMatches.addElement(seq);
414 * Searches for a match with the sequence name, and if found, adds the
415 * sequence to the list of match ids (but not as a duplicate). Answers true if
416 * a match was added, else false.
419 * @param searchPattern
422 protected boolean searchSequenceName(SequenceI seq, Regex searchPattern)
424 if (searchPattern.search(seq.getName()) && !idMatches.contains(seq))
426 idMatches.addElement(seq);
433 * Tries to interpret the search string as a residue position, and if valid,
434 * adds the position to the search results and returns true, else answers
437 protected boolean searchForResidueNumber(SequenceI seq, String searchString)
441 int res = Integer.parseInt(searchString);
442 if (seq.getStart() <= res && seq.getEnd() >= res)
444 searchResults.addResult(seq, res, res);
447 } catch (NumberFormatException ex)
454 * @see jalview.analysis.FinderI#getIdMatch()
457 public Vector<SequenceI> getIdMatches()
463 * @see jalview.analysis.FinderI#getSearchResults()
466 public SearchResultsI getSearchResults()
468 return searchResults;