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 java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Locale;
29 import com.stevesoft.pat.Regex;
31 import jalview.api.AlignViewportI;
32 import jalview.api.FinderI;
33 import jalview.datamodel.AlignmentI;
34 import jalview.datamodel.SearchResultMatchI;
35 import jalview.datamodel.SearchResults;
36 import jalview.datamodel.SearchResultsI;
37 import jalview.datamodel.SequenceFeature;
38 import jalview.datamodel.SequenceGroup;
39 import jalview.datamodel.SequenceI;
40 import jalview.datamodel.features.SequenceFeaturesI;
41 import jalview.util.Comparison;
42 import jalview.util.MapList;
45 * Implements the search algorithm for the Find dialog
47 public class Finder implements FinderI
50 * matched residue locations
52 private SearchResultsI searchResults;
55 * sequences matched by id or description
57 private List<SequenceI> idMatches;
60 * the viewport to search over
62 private AlignViewportI viewport;
65 * sequence index in alignment to search from
67 private int sequenceIndex;
70 * position offset in sequence to search from, base 0
71 * (position after start of last match for a 'find next')
73 private int residueIndex;
76 * the true sequence position of the start of the
77 * last sequence searched (when 'ignore hidden regions' does not apply)
79 private int searchedSequenceStartPosition;
82 * when 'ignore hidden regions' applies, this holds the mapping from
83 * the visible sequence positions (1, 2, ...) to true sequence positions
85 private MapList searchedSequenceMap;
87 private String seqToSearch;
90 * Constructor for searching a viewport
94 public Finder(AlignViewportI av)
97 this.sequenceIndex = 0;
98 this.residueIndex = -1;
102 public void findAll(String theSearchString, boolean matchCase,
103 boolean searchDescription, boolean searchFeatureDesc,
104 boolean ignoreHidden)
107 * search from the start
112 doFind(theSearchString, matchCase, searchDescription, searchFeatureDesc,
116 * reset to start for next search
123 public void findNext(String theSearchString, boolean matchCase,
124 boolean searchDescription, boolean searchFeatureDesc,
125 boolean ignoreHidden)
127 doFind(theSearchString, matchCase, searchDescription, searchFeatureDesc,
128 false, ignoreHidden);
130 if (searchResults.isEmpty() && idMatches.isEmpty())
133 * search failed - reset to start for next search
141 * Performs a 'find next' or 'find all'
143 * @param theSearchString
145 * @param searchDescription
147 * @param ignoreHidden
149 protected void doFind(String theSearchString, boolean matchCase,
150 boolean searchDescription, boolean searchFeatureDesc,
151 boolean findAll, boolean ignoreHidden)
153 searchResults = new SearchResults();
154 idMatches = new ArrayList<>();
156 String searchString = matchCase ? theSearchString
157 : theSearchString.toUpperCase(Locale.ROOT);
158 Regex searchPattern = new Regex(searchString);
159 searchPattern.setIgnoreCase(!matchCase);
161 SequenceGroup selection = viewport.getSelectionGroup();
162 if (selection != null && selection.getSize() < 1)
164 selection = null; // ? ignore column-only selection
167 AlignmentI alignment = viewport.getAlignment();
168 int end = alignment.getHeight();
170 getSequence(ignoreHidden);
172 boolean found = false;
173 while ((!found || findAll) && sequenceIndex < end)
175 found = findNextMatch(searchString, searchPattern, searchDescription,
176 searchFeatureDesc, ignoreHidden);
181 * Calculates and saves the sequence string to search. The string is
182 * restricted to the current selection region if there is one, and is saved
183 * with all gaps removed.
185 * If there are hidden columns, and option {@ignoreHidden} is selected, then
186 * only visible positions of the sequence are included, and a mapping is also
187 * constructed from the returned string positions to the true sequence
190 * Note we have to do this each time {@code findNext} or {@code findAll} is
191 * called, in case the alignment, selection group or hidden columns have
192 * changed. In particular, if the sequence at offset {@code sequenceIndex} in
193 * the alignment is (no longer) in the selection group, search is advanced to
194 * the next sequence that is.
196 * Sets sequence string to the empty string if there are no more sequences (in
197 * selection group if any) at or after {@code sequenceIndex}.
199 * Returns true if a sequence could be found, false if end of alignment was
202 * @param ignoreHidden
205 private boolean getSequence(boolean ignoreHidden)
207 AlignmentI alignment = viewport.getAlignment();
208 if (sequenceIndex >= alignment.getHeight())
213 SequenceI seq = alignment.getSequenceAt(sequenceIndex);
214 SequenceGroup selection = viewport.getSelectionGroup();
215 if (selection != null && !selection.contains(seq))
217 if (!nextSequence(ignoreHidden))
221 seq = alignment.getSequenceAt(sequenceIndex);
224 String seqString = null;
227 seqString = getVisibleSequence(seq);
228 this.searchedSequenceStartPosition = 1;
233 int endCol = seq.getLength() - 1;
234 this.searchedSequenceStartPosition = seq.getStart();
235 if (selection != null)
237 startCol = selection.getStartRes();
238 endCol = Math.min(endCol, selection.getEndRes());
239 this.searchedSequenceStartPosition = seq.findPosition(startCol);
241 seqString = seq.getSequenceAsString(startCol, endCol + 1);
245 * remove gaps; note that even if this leaves an empty string, we 'search'
246 * the sequence anyway (for possible match on name or description)
248 String ungapped = AlignSeq.extractGaps(Comparison.GapChars, seqString);
249 this.seqToSearch = ungapped;
255 * Returns a string consisting of only the visible residues of {@code seq}
256 * from alignment column {@ fromColumn}, restricted to the current selection
257 * region if there is one.
259 * As a side-effect, also computes the mapping from the true sequence
260 * positions to the positions (1, 2, ...) of the returned sequence. This is to
261 * allow search matches in the visible sequence to be converted to sequence
267 private String getVisibleSequence(SequenceI seq)
270 * get start / end columns of sequence and convert to base 0
271 * (so as to match the visible column ranges)
273 int seqStartCol = seq.findIndex(seq.getStart()) - 1;
274 int seqEndCol = seq.findIndex(seq.getStart() + seq.getLength() - 1) - 1;
275 Iterator<int[]> visibleColumns = viewport.getViewAsVisibleContigs(true);
276 StringBuilder visibleSeq = new StringBuilder(seqEndCol - seqStartCol);
277 List<int[]> fromRanges = new ArrayList<>();
279 while (visibleColumns.hasNext())
281 int[] range = visibleColumns.next();
282 if (range[0] > seqEndCol)
284 // beyond the end of the sequence
287 if (range[1] < seqStartCol)
289 // before the start of the sequence
292 String subseq = seq.getSequenceAsString(range[0], range[1] + 1);
293 String ungapped = AlignSeq.extractGaps(Comparison.GapChars, subseq);
294 visibleSeq.append(ungapped);
295 if (!ungapped.isEmpty())
298 * visible region includes at least one non-gap character,
299 * so add the range to the mapping being constructed
301 int seqResFrom = seq.findPosition(range[0]);
302 int seqResTo = seqResFrom + ungapped.length() - 1;
303 fromRanges.add(new int[] { seqResFrom, seqResTo });
308 * construct the mapping
309 * from: visible sequence positions 1..length
310 * to: true residue positions of the alignment sequence
312 List<int[]> toRange = Arrays
314 { 1, visibleSeq.length() });
315 searchedSequenceMap = new MapList(fromRanges, toRange, 1, 1);
317 return visibleSeq.toString();
321 * Advances the search to the next sequence in the alignment. Sequences not in
322 * the current selection group (if there is one) are skipped. The
323 * (sub-)sequence to be searched is extracted, gaps removed, and saved, or set
324 * to null if there are no more sequences to search.
326 * Returns true if a sequence could be found, false if end of alignment was
329 * @param ignoreHidden
331 private boolean nextSequence(boolean ignoreHidden)
336 return getSequence(ignoreHidden);
340 * Finds the next match in the given sequence, starting at offset
341 * {@code residueIndex}. Answers true if a match is found, else false.
343 * If a match is found, {@code residueIndex} is advanced to the position after
344 * the start of the matched region, ready for the next search.
346 * If no match is found, {@code sequenceIndex} is advanced ready to search the
350 * @param searchString
351 * @param searchPattern
352 * @param matchDescription
353 * @param ignoreHidden
356 protected boolean findNextMatch(String searchString, Regex searchPattern,
357 boolean matchDescription, boolean matchFeatureDesc,
358 boolean ignoreHidden)
360 if (residueIndex < 0)
363 * at start of sequence; try find by residue number, in sequence id,
364 * or (optionally) in sequence description
366 if (doNonMotifSearches(searchString, searchPattern, matchDescription))
373 * search for next match in sequence string
375 int end = seqToSearch.length();
376 while (residueIndex < end)
378 boolean matched = searchPattern.searchFrom(seqToSearch, residueIndex);
381 if (recordMatch(searchPattern, ignoreHidden))
388 if (matchFeatureDesc)
390 matched = searchSequenceFeatures(residueIndex, searchPattern);
392 residueIndex = Integer.MAX_VALUE;
396 nextSequence(ignoreHidden);
401 * Adds the match held in the <code>searchPattern</code> Regex to the
402 * <code>searchResults</code>, unless it is a subregion of the last match
403 * recorded. <code>residueIndex</code> is advanced to the position after the
404 * start of the matched region, ready for the next search. Answers true if a
405 * match was added, else false.
407 * Matches that lie entirely within hidden regions of the alignment are not
410 * @param searchPattern
411 * @param ignoreHidden
414 protected boolean recordMatch(Regex searchPattern, boolean ignoreHidden)
416 SequenceI seq = viewport.getAlignment().getSequenceAt(sequenceIndex);
419 * convert start/end of the match to sequence coordinates
421 int offset = searchPattern.matchedFrom();
422 int matchStartPosition = this.searchedSequenceStartPosition + offset;
423 int matchEndPosition = matchStartPosition + searchPattern.charsMatched()
427 * update residueIndex to next position after the start of the match
428 * (findIndex returns a value base 1, columnIndex is held base 0)
430 residueIndex = searchPattern.matchedFrom() + 1;
433 * return false if the match is entirely in a hidden region
435 if (allHidden(seq, matchStartPosition, matchEndPosition))
441 * check that this match is not a subset of the previous one (JAL-2302)
443 List<SearchResultMatchI> matches = searchResults.getResults();
444 SearchResultMatchI lastMatch = matches.isEmpty() ? null
445 : matches.get(matches.size() - 1);
447 if (lastMatch == null || !lastMatch.contains(seq, matchStartPosition,
450 addMatch(seq, matchStartPosition, matchEndPosition, ignoreHidden);
458 * Adds one match to the stored list. If hidden residues are being skipped,
459 * then the match may need to be split into contiguous positions of the
460 * sequence (so it does not include skipped residues).
463 * @param matchStartPosition
464 * @param matchEndPosition
465 * @param ignoreHidden
467 private void addMatch(SequenceI seq, int matchStartPosition,
468 int matchEndPosition, boolean ignoreHidden)
475 searchResults.addResult(seq, matchStartPosition, matchEndPosition);
480 * get start-end contiguous ranges in underlying sequence
482 int[] truePositions = searchedSequenceMap
483 .locateInFrom(matchStartPosition, matchEndPosition);
484 searchResults.addResult(seq, truePositions);
488 * Returns true if all residues are hidden, else false
495 private boolean allHidden(SequenceI seq, int fromPos, int toPos)
497 if (!viewport.hasHiddenColumns())
501 for (int res = fromPos; res <= toPos; res++)
503 if (isVisible(seq, res))
512 * Does searches other than for residue patterns. Currently this includes
514 * <li>find residue by position (if search string is a number)</li>
515 * <li>match search string to sequence id</li>
516 * <li>match search string to sequence description (optional)</li>
518 * Answers true if a match is found, else false.
520 * @param searchString
521 * @param searchPattern
522 * @param includeDescription
525 protected boolean doNonMotifSearches(String searchString,
526 Regex searchPattern, boolean includeDescription)
528 SequenceI seq = viewport.getAlignment().getSequenceAt(sequenceIndex);
531 * position sequence search to start of sequence
536 int res = Integer.parseInt(searchString);
537 return searchForResidueNumber(seq, res);
538 } catch (NumberFormatException ex)
540 // search pattern is not a number
543 if (searchSequenceName(seq, searchPattern))
547 if (includeDescription && searchSequenceDescription(seq, searchPattern))
555 * Searches for a match with the sequence features, and if found, adds the
556 * sequence to the list of match ids, (but not as a duplicate). Answers true
557 * if a match was added, else false.
560 * @param searchPattern
563 protected boolean searchSequenceFeatures(int from, Regex searchPattern)
565 boolean matched = false;
566 SequenceI seq = viewport.getAlignment().getSequenceAt(sequenceIndex);
568 SequenceFeaturesI sf = seq.getFeatures();
569 for (SequenceFeature feature : sf.getAllFeatures(null))
571 if (searchPattern.search(feature.type) || (feature.description != null
572 && searchPattern.search(feature.description)))
574 searchResults.addResult(seq, seq.findIndex(feature.getBegin()),
575 seq.findIndex(feature.getEnd()));
583 * Searches for a match with the sequence description, and if found, adds the
584 * sequence to the list of match ids (but not as a duplicate). Answers true if
585 * a match was added, else false.
588 * @param searchPattern
591 protected boolean searchSequenceDescription(SequenceI seq,
594 String desc = seq.getDescription();
595 if (desc != null && searchPattern.search(desc)
596 && !idMatches.contains(seq))
605 * Searches for a match with the sequence name, and if found, adds the
606 * sequence to the list of match ids (but not as a duplicate). Answers true if
607 * a match was added, else false.
610 * @param searchPattern
613 protected boolean searchSequenceName(SequenceI seq, Regex searchPattern)
615 if (searchPattern.search(seq.getName()) && !idMatches.contains(seq))
624 * If the residue position is valid for the sequence, and in a visible column,
625 * adds the position to the search results and returns true, else answers
632 protected boolean searchForResidueNumber(SequenceI seq, int resNo)
634 if (seq.getStart() <= resNo && seq.getEnd() >= resNo)
636 if (isVisible(seq, resNo))
638 searchResults.addResult(seq, resNo, resNo);
646 * Returns true if the residue is in a visible column, else false
652 private boolean isVisible(SequenceI seq, int res)
654 if (!viewport.hasHiddenColumns())
658 int col = seq.findIndex(res); // base 1
659 return viewport.getAlignment().getHiddenColumns().isVisible(col - 1); // base
664 public List<SequenceI> getIdMatches()
670 public SearchResultsI getSearchResults()
672 return searchResults;