X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fanalysis%2FFinder.java;h=0996830bf806ef4fcb7aadc31ce04a81138f8298;hb=24de2d6a6f3db1b9d55f367e2bf8ba112e202a8a;hp=adf7ff64bfa49820f5f6000828ccff0593da021c;hpb=85b18307da5f85a9cb5c13bb6be97eaf2c7f7965;p=jalview.git diff --git a/src/jalview/analysis/Finder.java b/src/jalview/analysis/Finder.java index adf7ff6..0996830 100644 --- a/src/jalview/analysis/Finder.java +++ b/src/jalview/analysis/Finder.java @@ -20,6 +20,7 @@ */ package jalview.analysis; +import jalview.api.FinderI; import jalview.datamodel.AlignmentI; import jalview.datamodel.Range; import jalview.datamodel.SearchResultMatchI; @@ -38,7 +39,7 @@ import com.stevesoft.pat.Regex; /** * Implements the search algorithm for the Find dialog */ -public class Finder +public class Finder implements FinderI { /* * matched residue locations @@ -48,7 +49,7 @@ public class Finder /* * sequences matched by id or description */ - private Vector idMatch; + private Vector idMatches; /* * the alignment to search over @@ -61,21 +62,6 @@ public class Finder private SequenceGroup selection; /* - * set true for case-sensitive search (default is false) - */ - private boolean caseSensitive; - - /* - * set true to search sequence description (default is false) - */ - private boolean includeDescription; - - /* - * set true to return all matches (default is next match only) - */ - private boolean findAll; - - /* * sequence index in alignment to search from */ private int sequenceIndex; @@ -83,63 +69,78 @@ public class Finder /* * column position in sequence to search from, base 0 * - absolute column number including any hidden columns - * (position of last match for a repeat search) + * (position after start of last match for a repeat search) */ private int columnIndex; /** - * Constructor to start searching an alignment, optionally restricting results - * to a selection + * Constructor for searching an alignment * * @param al - * @param sel */ - public Finder(AlignmentI al, SequenceGroup sel) + public Finder(AlignmentI al) { - this(al, sel, 0, -1); + this.alignment = al; + this.sequenceIndex = 0; + this.columnIndex = -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 colindex - */ - public Finder(AlignmentI al, SequenceGroup sel, int seqindex, - int colindex) + @Override + public void findAll(String theSearchString, SequenceGroup sg, + boolean matchCase, boolean searchDescription) { - this.alignment = al; - this.selection = sel; - this.sequenceIndex = seqindex; - this.columnIndex = colindex; + /* + * search from the start + */ + sequenceIndex = 0; + columnIndex = -1; + + doFind(theSearchString, sg, matchCase, searchDescription, true); + + /* + * reset to start for next search + */ + sequenceIndex = 0; + columnIndex = -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(). - * - * @param theSearchString - * @return - */ - public void find(String theSearchString) + @Override + public void findNext(String theSearchString, SequenceGroup sg, + boolean matchCase, boolean searchDescription) { - if (findAll) + doFind(theSearchString, sg, matchCase, searchDescription, false); + + if (searchResults.isEmpty() && idMatches.isEmpty()) { + /* + * search failed - reset to start for next search + */ sequenceIndex = 0; columnIndex = -1; } + } - String searchString = caseSensitive ? theSearchString + /** + * Performs a 'find next' or 'find all', optionally restricted to the + * specified selection region + * + * @param theSearchString + * @param selectionRegion + * @param matchCase + * @param searchDescription + * @param findAll + */ + protected void doFind(String theSearchString, SequenceGroup selectionRegion, + boolean matchCase, boolean searchDescription, boolean findAll) + { + this.selection = selectionRegion; + String searchString = matchCase ? theSearchString : theSearchString.toUpperCase(); Regex searchPattern = new Regex(searchString); - searchPattern.setIgnoreCase(!caseSensitive); + searchPattern.setIgnoreCase(!matchCase); + searchResults = new SearchResults(); - idMatch = new Vector<>(); + idMatches = new Vector<>(); if (selection != null && selection.getSize() < 1) { @@ -151,7 +152,8 @@ public class Finder while (sequenceIndex < end) { SequenceI seq = alignment.getSequenceAt(sequenceIndex); - boolean found = findNext(seq, searchString, searchPattern); + boolean found = findNextMatch(seq, searchString, searchPattern, + searchDescription); if (found && !findAll) { return; @@ -225,10 +227,11 @@ public class Finder * @param seq * @param searchString * @param searchPattern + * @param matchDescription * @return */ - protected boolean findNext(SequenceI seq, String searchString, - Regex searchPattern) + protected boolean findNextMatch(SequenceI seq, String searchString, + Regex searchPattern, boolean matchDescription) { if (selection != null && !selection.contains(seq)) { @@ -244,7 +247,8 @@ public class Finder * at start of sequence; try find by residue number, in sequence id, * or (optionally) in sequence description */ - if (doNonMotifSearches(seq, searchString, searchPattern)) + if (doNonMotifSearches(seq, searchString, searchPattern, + matchDescription)) { return true; } @@ -360,10 +364,11 @@ public class Finder * @param seq * @param searchString * @param searchPattern + * @param includeDescription * @return */ protected boolean doNonMotifSearches(SequenceI seq, String searchString, - Regex searchPattern) + Regex searchPattern, boolean includeDescription) { /* * position sequence search to start of sequence @@ -397,9 +402,9 @@ public class Finder protected boolean searchSequenceDescription(SequenceI seq, Regex searchPattern) { String desc = seq.getDescription(); - if (desc != null && searchPattern.search(desc) && !idMatch.contains(seq)) + if (desc != null && searchPattern.search(desc) && !idMatches.contains(seq)) { - idMatch.addElement(seq); + idMatches.addElement(seq); return true; } return false; @@ -416,9 +421,9 @@ public class Finder */ protected boolean searchSequenceName(SequenceI seq, Regex searchPattern) { - if (searchPattern.search(seq.getName()) && !idMatch.contains(seq)) + if (searchPattern.search(seq.getName()) && !idMatches.contains(seq)) { - idMatch.addElement(seq); + idMatches.addElement(seq); return true; } return false; @@ -445,79 +450,21 @@ public class Finder return false; } - /** - * Sets whether the search is case sensitive (default is no) - * - * @param value + /* (non-Javadoc) + * @see jalview.analysis.FinderI#getIdMatch() */ - public void setCaseSensitive(boolean value) + @Override + public Vector getIdMatches() { - this.caseSensitive = value; + return idMatches; } - /** - * Sets whether search returns all matches. Default is to return the next - * match only. - * - * @param value - */ - public void setFindAll(boolean value) - { - this.findAll = value; - } - - /** - * Returns the (possibly empty) list of sequences matched on sequence name or - * description - * - * @return - */ - public Vector getIdMatch() - { - return idMatch; - } - - /** - * Answers the search results (possibly empty) from the last search - * - * @return + /* (non-Javadoc) + * @see jalview.analysis.FinderI#getSearchResults() */ + @Override public SearchResultsI getSearchResults() { return searchResults; } - - /** - * Answers the absolute column position (base 0, including any hidden columns) - * of the start of the last sequence motif (residue pattern) match found. A - * 'Find next' will search from the next position. - * - * @return - */ - public int getColumnIndex() - { - return columnIndex; - } - - /** - * Answers the offset in the alignment (0..) of the sequence in which the last - * match was found (if any) - * - * @return - */ - public int getSequenceIndex() - { - return sequenceIndex; - } - - /** - * Sets whether search also searches in sequence description text (default is - * no) - * - * @param value - */ - public void setIncludeDescription(boolean value) - { - this.includeDescription = value; - } }