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.AlignViewportI;
24 import jalview.api.FinderI;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.Range;
27 import jalview.datamodel.SearchResultMatchI;
28 import jalview.datamodel.SearchResults;
29 import jalview.datamodel.SearchResultsI;
30 import jalview.datamodel.SequenceGroup;
31 import jalview.datamodel.SequenceI;
32 import jalview.datamodel.VisibleContigsIterator;
33 import jalview.util.Comparison;
35 import java.util.List;
36 import java.util.Vector;
38 import com.stevesoft.pat.Regex;
41 * Implements the search algorithm for the Find dialog
43 public class Finder implements FinderI
46 * matched residue locations
48 private SearchResultsI searchResults;
51 * sequences matched by id or description
53 private Vector<SequenceI> idMatches;
56 * the viewport to search over
58 private AlignViewportI viewport;
61 * sequence index in alignment to search from
63 private int sequenceIndex;
66 * column position in sequence to search from, base 0
67 * - absolute column number including any hidden columns
68 * (position after start of last match for a repeat search)
70 private int columnIndex;
73 * Constructor for searching a viewport
77 public Finder(AlignViewportI av)
80 this.sequenceIndex = 0;
81 this.columnIndex = -1;
85 public void findAll(String theSearchString, boolean matchCase,
86 boolean searchDescription)
89 * search from the start
94 doFind(theSearchString, matchCase, searchDescription, true);
97 * reset to start for next search
104 public void findNext(String theSearchString, boolean matchCase,
105 boolean searchDescription)
107 doFind(theSearchString, matchCase, searchDescription, false);
109 if (searchResults.isEmpty() && idMatches.isEmpty())
112 * search failed - reset to start for next search
120 * Performs a 'find next' or 'find all'
122 * @param theSearchString
124 * @param searchDescription
127 protected void doFind(String theSearchString, boolean matchCase,
128 boolean searchDescription, boolean findAll)
130 String searchString = matchCase ? theSearchString
131 : theSearchString.toUpperCase();
132 Regex searchPattern = new Regex(searchString);
133 searchPattern.setIgnoreCase(!matchCase);
135 searchResults = new SearchResults();
136 idMatches = new Vector<>();
138 SequenceGroup selection = viewport.getSelectionGroup();
139 if (selection != null && selection.getSize() < 1)
141 selection = null; // ? ignore column-only selection
144 AlignmentI alignment = viewport.getAlignment();
145 int end = alignment.getHeight();
147 while (sequenceIndex < end)
149 SequenceI seq = alignment.getSequenceAt(sequenceIndex);
150 boolean found = findNextMatch(seq, searchString, searchPattern,
152 if (found && !findAll)
165 * Answers the start-end column range of the visible region of
166 * <code>sequence</code> starting at or after the given <code>column</code>.
167 * If there are no hidden columns, this just returns the remaining width of
168 * the sequence. The range is restricted to the current <code>selection</code>
169 * if there is one. Answers null if there are no visible columns at or after
170 * <code>column</code>.
172 protected Range getNextVisibleSequenceRegion(SequenceI sequence,
175 int seqColStart = column;
176 int seqColEnd = sequence.getLength() - 1;
179 * restrict search to (next) visible column region,
180 * in case there are hidden columns
182 AlignmentI alignment = viewport.getAlignment();
183 VisibleContigsIterator visibleRegions = alignment.getHiddenColumns()
184 .getVisContigsIterator(column, alignment.getWidth(),
186 int[] visible = visibleRegions.hasNext() ? visibleRegions.next() : null;
189 columnIndex = seqColEnd + 1;
192 seqColStart = Math.max(seqColStart, visible[0]);
193 seqColEnd = Math.min(seqColEnd, visible[1]);
196 * restrict search to selected region if there is one
198 SequenceGroup selection = viewport.getSelectionGroup();
199 if (selection != null)
201 int selectionStart = selection.getStartRes();
202 int selectionEnd = selection.getEndRes();
203 if (selectionStart > seqColEnd || selectionEnd < seqColStart)
206 * sequence region doesn't overlap selection region
208 columnIndex = seqColEnd + 1;
211 seqColStart = Math.max(seqColStart, selectionStart);
212 seqColEnd = Math.min(seqColEnd, selectionEnd);
215 return new Range(seqColStart, seqColEnd);
219 * Finds the next match in the given sequence, starting at column at
220 * <code>columnIndex</code>. Answers true if a match is found, else false. If
221 * a match is found, <code>columnIndex</code> is advanced to the column after
222 * the start of the matched region, ready for a search from the next position.
225 * @param searchString
226 * @param searchPattern
227 * @param matchDescription
230 protected boolean findNextMatch(SequenceI seq, String searchString,
231 Regex searchPattern, boolean matchDescription)
233 SequenceGroup selection = viewport.getSelectionGroup();
234 if (selection != null && !selection.contains(seq))
237 * this sequence is not in the selection - advance to next sequence
245 * at start of sequence; try find by residue number, in sequence id,
246 * or (optionally) in sequence description
248 if (doNonMotifSearches(seq, searchString, searchPattern,
256 * search for next match in sequence string
258 int end = seq.getLength();
259 while (columnIndex < end)
261 if (searchNextVisibleRegion(seq, searchPattern))
270 * Searches the sequence, starting from <code>columnIndex</code>, and adds the
271 * next match (if any) to <code>searchResults</code>. The search is restricted
272 * to the next visible column region, and to the <code>selection</code> region
273 * if there is one. Answers true if a match is added, else false.
276 * @param searchPattern
279 protected boolean searchNextVisibleRegion(SequenceI seq, Regex searchPattern)
281 Range visible = getNextVisibleSequenceRegion(seq, columnIndex);
286 String seqString = seq.getSequenceAsString(visible.start, visible.end + 1);
287 String noGaps = AlignSeq.extractGaps(Comparison.GapChars, seqString);
289 if (searchPattern.search(noGaps))
291 int sequenceStartPosition = seq.findPosition(visible.start);
292 recordMatch(seq, searchPattern, sequenceStartPosition);
298 * no match - advance columnIndex past this visible region
299 * so the next visible region (if any) is searched next
301 columnIndex = visible.end + 1;
308 * Adds the match held in the <code>searchPattern</code> Regex to the
309 * <code>searchResults</code>, unless it is a subregion of the last match
310 * recorded. <code>columnIndex</code> is advanced to the position after the
311 * start of the matched region, ready for the next search. Answers true if a
312 * match was added, else false.
315 * @param searchPattern
316 * @param firstResiduePosition
319 protected boolean recordMatch(SequenceI seq, Regex searchPattern,
320 int firstResiduePosition)
323 * get start/end of the match in sequence coordinates
325 int offset = searchPattern.matchedFrom();
326 int matchStartPosition = firstResiduePosition + offset;
327 int matchEndPosition = matchStartPosition
328 + searchPattern.charsMatched() - 1;
331 * update columnIndex to next column after the start of the match
332 * (findIndex returns a value base 1, columnIndex is held base 0)
334 columnIndex = seq.findIndex(matchStartPosition);
337 * check that this match is not a subset of the previous one (JAL-2302)
339 List<SearchResultMatchI> matches = searchResults.getResults();
340 SearchResultMatchI lastMatch = matches.isEmpty() ? null
341 : matches.get(matches.size() - 1);
343 if (lastMatch == null || !lastMatch.contains(seq, matchStartPosition,
346 searchResults.addResult(seq, matchStartPosition, matchEndPosition);
354 * Does searches other than for residue patterns. Currently this includes
356 * <li>find residue by position (if search string is a number)</li>
357 * <li>match search string to sequence id</li>
358 * <li>match search string to sequence description (optional)</li>
360 * Answers true if a match is found, else false.
363 * @param searchString
364 * @param searchPattern
365 * @param includeDescription
368 protected boolean doNonMotifSearches(SequenceI seq, String searchString,
369 Regex searchPattern, boolean includeDescription)
372 * position sequence search to start of sequence
376 if (searchForResidueNumber(seq, searchString))
380 if (searchSequenceName(seq, searchPattern))
384 if (includeDescription && searchSequenceDescription(seq, searchPattern))
392 * Searches for a match with the sequence description, and if found, adds the
393 * sequence to the list of match ids (but not as a duplicate). Answers true if
394 * a match was added, else false.
397 * @param searchPattern
400 protected boolean searchSequenceDescription(SequenceI seq, Regex searchPattern)
402 String desc = seq.getDescription();
403 if (desc != null && searchPattern.search(desc) && !idMatches.contains(seq))
405 idMatches.addElement(seq);
412 * Searches for a match with the sequence name, and if found, adds the
413 * sequence to the list of match ids (but not as a duplicate). Answers true if
414 * a match was added, else false.
417 * @param searchPattern
420 protected boolean searchSequenceName(SequenceI seq, Regex searchPattern)
422 if (searchPattern.search(seq.getName()) && !idMatches.contains(seq))
424 idMatches.addElement(seq);
431 * Tries to interpret the search string as a residue position, and if valid,
432 * adds the position to the search results and returns true, else answers
435 protected boolean searchForResidueNumber(SequenceI seq, String searchString)
439 int res = Integer.parseInt(searchString);
440 if (seq.getStart() <= res && seq.getEnd() >= res)
442 searchResults.addResult(seq, res, res);
445 } catch (NumberFormatException ex)
452 * @see jalview.analysis.FinderI#getIdMatch()
455 public Vector<SequenceI> getIdMatches()
461 * @see jalview.analysis.FinderI#getSearchResults()
464 public SearchResultsI getSearchResults()
466 return searchResults;