*/
package jalview.analysis;
+import jalview.api.AlignViewportI;
+import jalview.api.FinderI;
import jalview.datamodel.AlignmentI;
import jalview.datamodel.Range;
import jalview.datamodel.SearchResultMatchI;
import jalview.datamodel.SequenceI;
import jalview.datamodel.VisibleContigsIterator;
import jalview.util.Comparison;
+import jalview.util.Platform;
import java.util.List;
import java.util.Vector;
/**
* Implements the search algorithm for the Find dialog
*/
-public class Finder
+public class Finder implements FinderI
{
/*
* matched residue locations
/*
* sequences matched by id or description
*/
- private Vector<SequenceI> idMatch;
+ private Vector<SequenceI> idMatches;
/*
- * the alignment to search over
+ * the viewport to search over
*/
- private AlignmentI alignment;
-
- /*
- * (optional) selection to restrict search to
- */
- 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;
+ private AlignViewportI viewport;
/*
* sequence index in alignment to search from
/*
* 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 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.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, 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, 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, boolean matchCase,
+ boolean searchDescription)
{
- if (findAll)
+ doFind(theSearchString, 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'
+ *
+ * @param theSearchString
+ * @param matchCase
+ * @param searchDescription
+ * @param findAll
+ */
+ protected void doFind(String theSearchString, boolean matchCase,
+ boolean searchDescription, boolean findAll)
+ {
+ String searchString = matchCase ? theSearchString
: theSearchString.toUpperCase();
- Regex searchPattern = new Regex(searchString);
- searchPattern.setIgnoreCase(!caseSensitive);
+ Regex searchPattern = Platform.newRegex(searchString, null);
+ searchPattern.setIgnoreCase(!matchCase);
+
searchResults = new SearchResults();
- idMatch = new Vector<>();
+ idMatches = new Vector<>();
+ SequenceGroup selection = viewport.getSelectionGroup();
if (selection != null && selection.getSize() < 1)
{
selection = null; // ? ignore column-only selection
}
+ AlignmentI alignment = viewport.getAlignment();
int end = alignment.getHeight();
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;
* restrict search to (next) visible column region,
* in case there are hidden columns
*/
+ AlignmentI alignment = viewport.getAlignment();
VisibleContigsIterator visibleRegions = alignment.getHiddenColumns()
.getVisContigsIterator(column, alignment.getWidth(),
false);
/*
* restrict search to selected region if there is one
*/
+ SequenceGroup selection = viewport.getSelectionGroup();
if (selection != null)
{
int selectionStart = selection.getStartRes();
* @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)
{
+ SequenceGroup selection = viewport.getSelectionGroup();
if (selection != null && !selection.contains(seq))
{
/*
* 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;
}
* @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
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;
*/
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;
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<SequenceI> 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<SequenceI> 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;
- }
}