X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fanalysis%2FFinder.java;h=0d8665ff373add02d627415bc470a70cdefd1961;hb=bc07c82cde95d498cf6919422baf7ad3994e1bf2;hp=d7bf0a270a78cfc627362595b165ddac2380b901;hpb=dfc18d64376ae95cfdbf3c112825587a8891179c;p=jalview.git diff --git a/src/jalview/analysis/Finder.java b/src/jalview/analysis/Finder.java index d7bf0a2..0d8665f 100644 --- a/src/jalview/analysis/Finder.java +++ b/src/jalview/analysis/Finder.java @@ -20,6 +20,8 @@ */ package jalview.analysis; +import jalview.api.AlignViewportI; +import jalview.api.FinderI; import jalview.datamodel.AlignmentI; import jalview.datamodel.SearchResultMatchI; import jalview.datamodel.SearchResults; @@ -27,386 +29,607 @@ import jalview.datamodel.SearchResultsI; import jalview.datamodel.SequenceGroup; import jalview.datamodel.SequenceI; import jalview.util.Comparison; +import jalview.util.MapList; -import java.util.Vector; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; import com.stevesoft.pat.Regex; /** * Implements the search algorithm for the Find dialog */ -public class Finder +public class Finder implements FinderI { /* - * match residue locations + * matched residue locations */ private SearchResultsI searchResults; /* * sequences matched by id or description */ - private Vector idMatch; + private List idMatches; /* - * the alignment to search over + * the viewport to search over */ - private AlignmentI alignment; + private AlignViewportI viewport; /* - * (optional) selection to restrict search to + * sequence index in alignment to search from */ - private SequenceGroup selection; + private int sequenceIndex; /* - * set true for case-sensitive search (default is false) + * position offset in sequence to search from, base 0 + * (position after start of last match for a 'find next') */ - private boolean caseSensitive; + private int residueIndex; /* - * set true to search sequence description (default is false) + * the true sequence position of the start of the + * last sequence searched (when 'ignore hidden regions' does not apply) */ - private boolean includeDescription; + private int searchedSequenceStartPosition; /* - * set true to return all matches (default is next match only) + * when 'ignore hidden regions' applies, this holds the mapping from + * the visible sequence positions (1, 2, ...) to true sequence positions */ - private boolean findAll; + private MapList searchedSequenceMap; - /* - * sequence index in alignment to search from - */ - private int seqIndex; - - /* - * residue position in sequence to search from, base 1 - * (position of last match for a repeat search) - */ - private int resIndex; + private String seqToSearch; /** - * Constructor to start searching an alignment, optionally restricting results - * to a selection + * Constructor for searching a viewport * - * @param al - * @param sel + * @param av */ - public Finder(AlignmentI al, SequenceGroup sel) + public Finder(AlignViewportI av) { - this(al, sel, 0, -1); + this.viewport = av; + this.sequenceIndex = 0; + this.residueIndex = -1; } - /** - * Constructor to resume search at given sequence and residue on alignment and - * (optionally) restricted to a selection - * - * @param al - * @param sel - * @param seqindex - * @param resindex - */ - public Finder(AlignmentI al, SequenceGroup sel, int seqindex, - int resindex) + @Override + public void findAll(String theSearchString, boolean matchCase, + boolean searchDescription, boolean ignoreHidden) + { + /* + * search from the start + */ + sequenceIndex = 0; + residueIndex = -1; + + doFind(theSearchString, matchCase, searchDescription, true, + ignoreHidden); + + /* + * reset to start for next search + */ + sequenceIndex = 0; + residueIndex = -1; + } + + @Override + public void findNext(String theSearchString, boolean matchCase, + boolean searchDescription, boolean ignoreHidden) { - this.alignment = al; - this.selection = sel; - this.seqIndex = seqindex; - this.resIndex = resindex; + doFind(theSearchString, matchCase, searchDescription, false, + ignoreHidden); + + if (searchResults.isEmpty() && idMatches.isEmpty()) + { + /* + * search failed - reset to start for next search + */ + sequenceIndex = 0; + residueIndex = -1; + } } /** - * Performs a find for the given search string. By default the next match is - * found, but if setFindAll(true) has been called, then all matches are found. - * Sequences matched by id or description can be retrieved by getIdMatch(), - * and matched residue patterns by getSearchResults(). + * Performs a 'find next' or 'find all' * * @param theSearchString - * @return + * @param matchCase + * @param searchDescription + * @param findAll + * @param ignoreHidden */ - public void find(String theSearchString) + protected void doFind(String theSearchString, boolean matchCase, + boolean searchDescription, boolean findAll, boolean ignoreHidden) { - String searchString = caseSensitive ? theSearchString.toUpperCase() - : theSearchString; - Regex regex = new Regex(searchString); - regex.setIgnoreCase(!caseSensitive); searchResults = new SearchResults(); - idMatch = new Vector<>(); + idMatches = new ArrayList<>(); + + String searchString = matchCase ? theSearchString + : theSearchString.toUpperCase(); + Regex searchPattern = new Regex(searchString); + searchPattern.setIgnoreCase(!matchCase); + SequenceGroup selection = viewport.getSelectionGroup(); if (selection != null && selection.getSize() < 1) { selection = null; // ? ignore column-only selection } - boolean finished = false; + AlignmentI alignment = viewport.getAlignment(); int end = alignment.getHeight(); - while (!finished && (seqIndex < end)) + getSequence(ignoreHidden); + + boolean found = false; + while ((!found || findAll) && sequenceIndex < end) { - SequenceI seq = alignment.getSequenceAt(seqIndex); + found = findNextMatch(searchString, searchPattern, searchDescription, + ignoreHidden); + } + } - if ((selection != null) && !selection.contains(seq)) + /** + * Calculates and saves the sequence string to search. The string is restricted + * to the current selection region if there is one, and is saved with all gaps + * removed. + *

+ * If there are hidden columns, and option {@ignoreHidden} is selected, then + * only visible positions of the sequence are included, and a mapping is also + * constructed from the returned string positions to the true sequence + * positions. + *

+ * Note we have to do this each time {@code findNext} or {@code findAll} is + * called, in case the alignment, selection group or hidden columns have + * changed. In particular, if the sequence at offset {@code sequenceIndex} in + * the alignment is (no longer) in the selection group, search is advanced to + * the next sequence that is. + *

+ * Sets sequence string to the empty string if there are no more sequences (in + * selection group if any) at or after {@code sequenceIndex}. + *

+ * Returns true if a sequence could be found, false if end of alignment was + * reached + * + * @param ignoreHidden + * @return + */ + private boolean getSequence(boolean ignoreHidden) + { + AlignmentI alignment = viewport.getAlignment(); + if (sequenceIndex >= alignment.getHeight()) + { + seqToSearch = ""; + return false; + } + SequenceI seq = alignment.getSequenceAt(sequenceIndex); + SequenceGroup selection = viewport.getSelectionGroup(); + if (selection != null && !selection.contains(seq)) + { + if (!nextSequence(ignoreHidden)) { - // this sequence is not in the selection - skip to next sequence - seqIndex++; - resIndex = -1; - continue; + return false; } + seq = alignment.getSequenceAt(sequenceIndex); + } - if (resIndex < 0) + String seqString = null; + if (ignoreHidden) + { + seqString = getVisibleSequence(seq); + this.searchedSequenceStartPosition = 1; + } + else + { + int startCol = 0; + int endCol = seq.getLength() - 1; + this.searchedSequenceStartPosition = seq.getStart(); + if (selection != null) { - /* - * at start of sequence; try find by residue number, in sequence id, - * or (optionally) in sequence description - */ - resIndex = 0; - if (doNonMotifSearches(seq, searchString, regex)) - { - return; - } + startCol = selection.getStartRes(); + endCol = Math.min(endCol, selection.getEndRes()); + this.searchedSequenceStartPosition = seq.findPosition(startCol); } + seqString = seq.getSequenceAsString(startCol, endCol + 1); + } - finished = searchSequenceString(seq, regex) && !findAll; + /* + * remove gaps; note that even if this leaves an empty string, we 'search' + * the sequence anyway (for possible match on name or description) + */ + String ungapped = AlignSeq.extractGaps(Comparison.GapChars, seqString); + this.seqToSearch = ungapped; - if (!finished) - { - seqIndex++; - resIndex = -1; - } - } + return true; } /** - * Searches the sequence, starting from resIndex (base 1), and - * adds matches to searchResults. The search is restricted to the - * selection region if there is one. Answers true if any match is - * added, else false. + * Returns a string consisting of only the visible residues of {@code seq} from + * alignment column {@ fromColumn}, restricted to the current selection region + * if there is one. + *

+ * As a side-effect, also computes the mapping from the true sequence positions + * to the positions (1, 2, ...) of the returned sequence. This is to allow + * search matches in the visible sequence to be converted to sequence positions. * * @param seq - * @param regex * @return */ - protected boolean searchSequenceString(SequenceI seq, Regex regex) + private String getVisibleSequence(SequenceI seq) { /* - * Restrict search to selected region if there is one + * get start / end columns of sequence and convert to base 0 + * (so as to match the visible column ranges) */ - int seqColStart = 0; - int seqColEnd = seq.getLength() - 1; - int residueOffset = 0; - if (selection != null) + int seqStartCol = seq.findIndex(seq.getStart()) - 1; + int seqEndCol = seq.findIndex(seq.getStart() + seq.getLength() - 1) - 1; + Iterator visibleColumns = viewport.getViewAsVisibleContigs(true); + StringBuilder visibleSeq = new StringBuilder(seqEndCol - seqStartCol); + List fromRanges = new ArrayList<>(); + + while (visibleColumns.hasNext()) { - int selColEnd = selection.getEndRes(); - int selColStart = selection.getStartRes(); - if (selColStart > seqColEnd) + int[] range = visibleColumns.next(); + if (range[0] > seqEndCol) { - return false; // sequence doesn't reach selection region + // beyond the end of the sequence + break; + } + if (range[1] < seqStartCol) + { + // before the start of the sequence + continue; + } + String subseq = seq.getSequenceAsString(range[0], range[1] + 1); + String ungapped = AlignSeq.extractGaps(Comparison.GapChars, subseq); + visibleSeq.append(ungapped); + if (!ungapped.isEmpty()) + { + /* + * visible region includes at least one non-gap character, + * so add the range to the mapping being constructed + */ + int seqResFrom = seq.findPosition(range[0]); + int seqResTo = seqResFrom + ungapped.length() - 1; + fromRanges.add(new int[] { seqResFrom, seqResTo }); } - seqColStart = selColStart; - seqColEnd = Math.min(seqColEnd, selColEnd); - residueOffset = seq.findPosition(selection.getStartRes()) - - seq.getStart(); } - String seqString = seq.getSequenceAsString(seqColStart, seqColEnd + 1); - String noGaps = AlignSeq.extractGaps(Comparison.GapChars, seqString); + /* + * construct the mapping + * from: visible sequence positions 1..length + * to: true residue positions of the alignment sequence + */ + List toRange = Arrays + .asList(new int[] + { 1, visibleSeq.length() }); + searchedSequenceMap = new MapList(fromRanges, toRange, 1, 1); - SearchResultMatchI lastMatch = null; - boolean found = false; + return visibleSeq.toString(); + } - for (int r = resIndex; r < noGaps.length(); r++) + /** + * Advances the search to the next sequence in the alignment. Sequences not in + * the current selection group (if there is one) are skipped. The (sub-)sequence + * to be searched is extracted, gaps removed, and saved, or set to null if there + * are no more sequences to search. + *

+ * Returns true if a sequence could be found, false if end of alignment was + * reached + * + * @param ignoreHidden + */ + private boolean nextSequence(boolean ignoreHidden) + { + sequenceIndex++; + residueIndex = -1; + + return getSequence(ignoreHidden); + } + + /** + * Finds the next match in the given sequence, starting at offset + * {@code residueIndex}. Answers true if a match is found, else false. + *

+ * If a match is found, {@code residueIndex} is advanced to the position after + * the start of the matched region, ready for the next search. + *

+ * If no match is found, {@code sequenceIndex} is advanced ready to search the + * next sequence. + * + * @param seqToSearch + * @param searchString + * @param searchPattern + * @param matchDescription + * @param ignoreHidden + * @return + */ + protected boolean findNextMatch(String searchString, + Regex searchPattern, boolean matchDescription, + boolean ignoreHidden) + { + if (residueIndex < 0) { /* - * searchFrom position is base 0, r is base 1, - * so search is from the position after the r'th residue + * at start of sequence; try find by residue number, in sequence id, + * or (optionally) in sequence description */ - if (regex.searchFrom(noGaps, r)) + if (doNonMotifSearches(searchString, searchPattern, + matchDescription)) { - resIndex = regex.matchedFrom(); - resIndex += residueOffset; // add back #residues before selection region - int matchStartPosition = resIndex + seq.getStart(); - int matchEndPosition = matchStartPosition + regex.charsMatched() - - 1; - if (lastMatch == null || !lastMatch.contains(seq, - matchStartPosition, matchEndPosition)) - { - lastMatch = searchResults.addResult(seq, matchStartPosition, - matchEndPosition); - found = true; - } - if (!findAll) + return true; + } + } + + /* + * search for next match in sequence string + */ + int end = seqToSearch.length(); + while (residueIndex < end) + { + boolean matched = searchPattern.searchFrom(seqToSearch, residueIndex); + if (matched) + { + if (recordMatch(searchPattern, ignoreHidden)) { - resIndex++; return true; } - r = resIndex; } else { - break; + residueIndex = Integer.MAX_VALUE; } } - return found; + + nextSequence(ignoreHidden); + return false; } /** - * Does searches other than for residue patterns. Currently this includes - *

- * Answers true if a match is found and we are not doing 'find all' (so this - * search action is complete), else false. + * Adds the match held in the searchPattern Regex to the + * searchResults, unless it is a subregion of the last match + * recorded. residueIndex is advanced to the position after the + * start of the matched region, ready for the next search. Answers true if a + * match was added, else false. + *

+ * Matches that lie entirely within hidden regions of the alignment are not + * added. * - * @param seq - * @param searchString - * @param regex + * @param searchPattern + * @param ignoreHidden * @return */ - protected boolean doNonMotifSearches(SequenceI seq, String searchString, - Regex regex) + protected boolean recordMatch(Regex searchPattern, boolean ignoreHidden) { - if (searchForResidueNumber(seq, searchString) && !findAll) - { - return true; - } - if (searchSequenceName(seq, regex) && !findAll) + SequenceI seq = viewport.getAlignment().getSequenceAt(sequenceIndex); + + /* + * convert start/end of the match to sequence coordinates + */ + int offset = searchPattern.matchedFrom(); + int matchStartPosition = this.searchedSequenceStartPosition + offset; + int matchEndPosition = matchStartPosition + + searchPattern.charsMatched() - 1; + + /* + * update residueIndex to next position after the start of the match + * (findIndex returns a value base 1, columnIndex is held base 0) + */ + residueIndex += offset + 1; + + /* + * return false if the match is entirely in a hidden region + */ + if (allHidden(seq, matchStartPosition, matchEndPosition)) { - return true; + return false; } - if (searchSequenceDescription(seq, regex) && !findAll) + + /* + * check that this match is not a subset of the previous one (JAL-2302) + */ + List matches = searchResults.getResults(); + SearchResultMatchI lastMatch = matches.isEmpty() ? null + : matches.get(matches.size() - 1); + + if (lastMatch == null || !lastMatch.contains(seq, matchStartPosition, + matchEndPosition)) { + addMatch(seq, matchStartPosition, matchEndPosition, ignoreHidden); return true; } + return false; } /** - * Searches for a match with the sequence description, if that option was - * requested, and if found, adds the sequence to the list of match ids (but - * not as a duplicate). Answers true if a match was added, else false. + * Adds one match to the stored list. If hidden residues are being skipped, then + * the match may need to be split into contiguous positions of the sequence (so + * it does not include skipped residues). * * @param seq - * @param regex - * @return + * @param matchStartPosition + * @param matchEndPosition + * @param ignoreHidden */ - protected boolean searchSequenceDescription(SequenceI seq, Regex regex) + private void addMatch(SequenceI seq, int matchStartPosition, + int matchEndPosition, boolean ignoreHidden) { - if (!includeDescription) + if (!ignoreHidden) { - return false; + /* + * simple case + */ + searchResults.addResult(seq, matchStartPosition, matchEndPosition); + return; } - String desc = seq.getDescription(); - if (desc != null && regex.search(desc) && !idMatch.contains(seq)) + + /* + * get start-end contiguous ranges in underlying sequence + */ + int[] truePositions = searchedSequenceMap + .locateInFrom(matchStartPosition, matchEndPosition); + for (int i = 0; i < truePositions.length - 1; i += 2) { - idMatch.addElement(seq); - return true; + searchResults.addResult(seq, truePositions[i], truePositions[i + 1]); } - return false; } /** - * Searches for a match with the sequence name, and if found, adds the - * sequence to the list of match ids (but not as a duplicate). Answers true if - * a match was added, else false. + * Returns true if all residues are hidden, else false * * @param seq - * @param regex + * @param fromPos + * @param toPos * @return */ - protected boolean searchSequenceName(SequenceI seq, Regex regex) + private boolean allHidden(SequenceI seq, int fromPos, int toPos) { - if (regex.search(seq.getName()) && !idMatch.contains(seq)) + if (!viewport.hasHiddenColumns()) { - idMatch.addElement(seq); - return true; + return false; } - return false; + for (int res = fromPos; res <= toPos; res++) + { + if (isVisible(seq, res)) + { + return false; + } + } + return true; } /** - * Tries to interpret the search string as a residue position, and if valid, - * adds the position to the search results + * Does searches other than for residue patterns. Currently this includes + *

+ * Answers true if a match is found, else false. + * + * @param searchString + * @param searchPattern + * @param includeDescription + * @return */ - protected boolean searchForResidueNumber(SequenceI seq, String searchString) + protected boolean doNonMotifSearches(String searchString, + Regex searchPattern, boolean includeDescription) { + SequenceI seq = viewport.getAlignment().getSequenceAt(sequenceIndex); + + /* + * position sequence search to start of sequence + */ + residueIndex = 0; try { int res = Integer.parseInt(searchString); - if (seq.getStart() <= res && seq.getEnd() >= res) - { - searchResults.addResult(seq, res, res); - return true; - } + return searchForResidueNumber(seq, res); } catch (NumberFormatException ex) { + // search pattern is not a number + } + + if (searchSequenceName(seq, searchPattern)) + { + return true; + } + if (includeDescription && searchSequenceDescription(seq, searchPattern)) + { + return true; } return false; } /** - * Sets whether the search is case sensitive (default is no) + * Searches for a match with the sequence description, and if found, adds the + * sequence to the list of match ids (but not as a duplicate). Answers true if + * a match was added, else false. * - * @param value + * @param seq + * @param searchPattern + * @return */ - public void setCaseSensitive(boolean value) + protected boolean searchSequenceDescription(SequenceI seq, Regex searchPattern) { - this.caseSensitive = value; + String desc = seq.getDescription(); + if (desc != null && searchPattern.search(desc) && !idMatches.contains(seq)) + { + idMatches.add(seq); + return true; + } + return false; } /** - * Sets whether search returns all matches. Default is to return the next - * match only. + * Searches for a match with the sequence name, and if found, adds the + * sequence to the list of match ids (but not as a duplicate). Answers true if + * a match was added, else false. * - * @param value + * @param seq + * @param searchPattern + * @return */ - public void setFindAll(boolean value) + protected boolean searchSequenceName(SequenceI seq, Regex searchPattern) { - this.findAll = value; + if (searchPattern.search(seq.getName()) && !idMatches.contains(seq)) + { + idMatches.add(seq); + return true; + } + return false; } /** - * Returns the (possibly empty) list of matching sequences (when search - * includes searching sequence names) + * If the residue position is valid for the sequence, and in a visible column, + * adds the position to the search results and returns true, else answers false. * + * @param seq + * @param resNo * @return */ - public Vector getIdMatch() + protected boolean searchForResidueNumber(SequenceI seq, int resNo) { - return idMatch; - } - - /** - * @return the searchResults - */ - public SearchResultsI getSearchResults() - { - return searchResults; + if (seq.getStart() <= resNo && seq.getEnd() >= resNo) + { + if (isVisible(seq, resNo)) + { + searchResults.addResult(seq, resNo, resNo); + return true; + } + } + return false; } /** - * @return the resIndex + * Returns true if the residue is in a visible column, else false + * + * @param seq + * @param res + * @return */ - public int getResIndex() + private boolean isVisible(SequenceI seq, int res) { - return resIndex; + if (!viewport.hasHiddenColumns()) + { + return true; + } + int col = seq.findIndex(res); // base 1 + return viewport.getAlignment().getHiddenColumns().isVisible(col - 1); // base 0 } - /** - * @return the seqIndex - */ - public int getSeqIndex() + @Override + public List getIdMatches() { - return seqIndex; + return idMatches; } - /** - * Sets whether search also searches in sequence description text (default is - * no) - * - * @param value - */ - public void setIncludeDescription(boolean value) + @Override + public SearchResultsI getSearchResults() { - this.includeDescription = value; + return searchResults; } }