*/
package jalview.analysis;
+import java.util.Locale;
+
+import jalview.api.AlignViewportI;
+import jalview.api.FinderI;
import jalview.datamodel.AlignmentI;
import jalview.datamodel.SearchResultMatchI;
import jalview.datamodel.SearchResults;
import jalview.datamodel.SequenceGroup;
import jalview.datamodel.SequenceI;
import jalview.util.Comparison;
+import jalview.util.MapList;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
import java.util.List;
-import java.util.Vector;
import com.stevesoft.pat.Regex;
-public class Finder
+/**
+ * Implements the search algorithm for the Find dialog
+ */
+public class Finder implements FinderI
{
- /**
- * Implements the search algorithms for the Find dialog box.
+ /*
+ * matched residue locations
*/
- SearchResultsI searchResults;
+ private SearchResultsI searchResults;
- AlignmentI alignment;
+ /*
+ * sequences matched by id or description
+ */
+ private List<SequenceI> idMatches;
- SequenceGroup selection = null;
+ /*
+ * the viewport to search over
+ */
+ private AlignViewportI viewport;
- Vector<SequenceI> idMatch = null;
+ /*
+ * sequence index in alignment to search from
+ */
+ private int sequenceIndex;
- boolean caseSensitive = false;
+ /*
+ * position offset in sequence to search from, base 0
+ * (position after start of last match for a 'find next')
+ */
+ private int residueIndex;
- private boolean includeDescription = false;
+ /*
+ * the true sequence position of the start of the
+ * last sequence searched (when 'ignore hidden regions' does not apply)
+ */
+ private int searchedSequenceStartPosition;
- boolean findAll = false;
+ /*
+ * when 'ignore hidden regions' applies, this holds the mapping from
+ * the visible sequence positions (1, 2, ...) to true sequence positions
+ */
+ private MapList searchedSequenceMap;
- Regex regex = null;
+ private String seqToSearch;
/**
- * holds last-searched position between calls to find(false)
+ * Constructor for searching a viewport
+ *
+ * @param av
*/
- int seqIndex = 0, resIndex = -1;
+ public Finder(AlignViewportI av)
+ {
+ this.viewport = av;
+ this.sequenceIndex = 0;
+ this.residueIndex = -1;
+ }
- public Finder(AlignmentI alignment, SequenceGroup selection)
+ @Override
+ public void findAll(String theSearchString, boolean matchCase,
+ boolean searchDescription, boolean ignoreHidden)
{
- this.alignment = alignment;
- this.selection = selection;
+ /*
+ * 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)
+ {
+ doFind(theSearchString, matchCase, searchDescription, false,
+ ignoreHidden);
+
+ if (searchResults.isEmpty() && idMatches.isEmpty())
+ {
+ /*
+ * search failed - reset to start for next search
+ */
+ sequenceIndex = 0;
+ residueIndex = -1;
+ }
}
/**
- * restart search at given sequence and residue on alignment and (optionally)
- * contained in selection
+ * Performs a 'find next' or 'find all'
*
- * @param alignment
- * @param selectionGroup
- * @param seqIndex
- * @param resIndex
+ * @param theSearchString
+ * @param matchCase
+ * @param searchDescription
+ * @param findAll
+ * @param ignoreHidden
*/
- public Finder(AlignmentI alignment, SequenceGroup selectionGroup,
- int seqIndex, int resIndex)
+ protected void doFind(String theSearchString, boolean matchCase,
+ boolean searchDescription, boolean findAll, boolean ignoreHidden)
{
- this(alignment, selectionGroup);
- this.seqIndex = seqIndex;
- this.resIndex = resIndex;
- }
+ searchResults = new SearchResults();
+ idMatches = new ArrayList<>();
- public boolean find(String searchString)
- {
- boolean hasResults = false;
- if (!caseSensitive)
+ String searchString = matchCase ? theSearchString
+ : theSearchString.toUpperCase(Locale.ROOT);
+ Regex searchPattern = new Regex(searchString);
+ searchPattern.setIgnoreCase(!matchCase);
+
+ SequenceGroup selection = viewport.getSelectionGroup();
+ if (selection != null && selection.getSize() < 1)
{
- searchString = searchString.toUpperCase();
+ selection = null; // ? ignore column-only selection
}
- regex = new Regex(searchString);
- regex.setIgnoreCase(!caseSensitive);
- searchResults = new SearchResults();
- idMatch = new Vector<SequenceI>();
- String item = null;
- boolean found = false;
+
+ AlignmentI alignment = viewport.getAlignment();
int end = alignment.getHeight();
- // /////////////////////////////////////////////
+ getSequence(ignoreHidden);
+
+ boolean found = false;
+ while ((!found || findAll) && sequenceIndex < end)
+ {
+ found = findNextMatch(searchString, searchPattern, searchDescription,
+ ignoreHidden);
+ }
+ }
- if (selection != null)
+ /**
+ * 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.
+ * <p>
+ * 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.
+ * <p>
+ * 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.
+ * <p>
+ * Sets sequence string to the empty string if there are no more sequences (in
+ * selection group if any) at or after {@code sequenceIndex}.
+ * <p>
+ * 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())
{
- if ((selection.getSize() < 1)
- || ((selection.getEndRes() - selection.getStartRes()) < 2))
+ seqToSearch = "";
+ return false;
+ }
+ SequenceI seq = alignment.getSequenceAt(sequenceIndex);
+ SequenceGroup selection = viewport.getSelectionGroup();
+ if (selection != null && !selection.contains(seq))
+ {
+ if (!nextSequence(ignoreHidden))
{
- selection = null;
+ return false;
}
+ seq = alignment.getSequenceAt(sequenceIndex);
}
- SearchResultMatchI lastm = null;
- while (!found && (seqIndex < end))
+ String seqString = null;
+ if (ignoreHidden)
{
- SequenceI seq = alignment.getSequenceAt(seqIndex);
-
- if ((selection != null && selection.getSize() > 0)
- && !selection.getSequences(null).contains(seq))
+ seqString = getVisibleSequence(seq);
+ this.searchedSequenceStartPosition = 1;
+ }
+ else
+ {
+ int startCol = 0;
+ int endCol = seq.getLength() - 1;
+ this.searchedSequenceStartPosition = seq.getStart();
+ if (selection != null)
{
- seqIndex++;
- resIndex = -1;
-
- continue;
+ startCol = selection.getStartRes();
+ endCol = Math.min(endCol, selection.getEndRes());
+ this.searchedSequenceStartPosition = seq.findPosition(startCol);
}
- if (resIndex < 0)
- {
- resIndex = 0;
- // test for one off matches - sequence position and sequence ID
- // //// is the searchString a residue number?
- try
- {
- int res = Integer.parseInt(searchString);
- // possibly a residue number - check if valid for seq
- if (seq.getEnd() >= res)
- {
- searchResults.addResult(seq, res, res);
- hasResults = true;
- // resIndex=seq.getLength();
- // seqIndex++;
- if (!findAll)
- {
- found = true;
- break;
- }
- }
- } catch (NumberFormatException ex)
- {
- }
-
- if (regex.search(seq.getName()) && !idMatch.contains(seq))
- {
- idMatch.addElement(seq);
- hasResults = true;
- if (!findAll)
- {
- // stop and return the match
- found = true;
- break;
- }
- }
+ seqString = seq.getSequenceAsString(startCol, endCol + 1);
+ }
- if (isIncludeDescription() && seq.getDescription() != null
- && regex.search(seq.getDescription())
- && !idMatch.contains(seq))
- {
- idMatch.addElement(seq);
- hasResults = true;
- if (!findAll)
- {
- // stop and return the match
- found = true;
- break;
- }
- }
- }
- item = seq.getSequenceAsString();
+ /*
+ * 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 ((selection != null)
- && (selection.getEndRes() < alignment.getWidth() - 1))
- {
- item = item.substring(0, selection.getEndRes() + 1);
- }
+ return true;
+ }
- // /Shall we ignore gaps???? - JBPNote: Add Flag for forcing this or not
- StringBuilder noGapsSB = new StringBuilder();
- int insertCount = 0;
- List<Integer> spaces = new ArrayList<Integer>();
+ /**
+ * 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.
+ * <p>
+ * 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
+ * @return
+ */
+ private String getVisibleSequence(SequenceI seq)
+ {
+ /*
+ * get start / end columns of sequence and convert to base 0
+ * (so as to match the visible column ranges)
+ */
+ int seqStartCol = seq.findIndex(seq.getStart()) - 1;
+ int seqEndCol = seq.findIndex(seq.getStart() + seq.getLength() - 1) - 1;
+ Iterator<int[]> visibleColumns = viewport.getViewAsVisibleContigs(true);
+ StringBuilder visibleSeq = new StringBuilder(seqEndCol - seqStartCol);
+ List<int[]> fromRanges = new ArrayList<>();
- for (int j = 0; j < item.length(); j++)
+ while (visibleColumns.hasNext())
+ {
+ int[] range = visibleColumns.next();
+ if (range[0] > seqEndCol)
{
- if (!Comparison.isGap(item.charAt(j)))
- {
- noGapsSB.append(item.charAt(j));
- spaces.add(Integer.valueOf(insertCount));
- }
- else
- {
- insertCount++;
- }
+ // beyond the end of the sequence
+ break;
}
-
- String noGaps = noGapsSB.toString();
- for (int r = resIndex; r < noGaps.length(); r++)
+ if (range[1] < seqStartCol)
{
-
- if (regex.searchFrom(noGaps, r))
- {
- resIndex = regex.matchedFrom();
-
- if ((selection != null && selection.getSize() > 0)
- && (resIndex + spaces.get(resIndex) < selection
- .getStartRes()))
- {
- continue;
- }
- // if invalid string used, then regex has no matched to/from
- int sres = seq.findPosition(resIndex + spaces.get(resIndex));
- int eres = seq.findPosition(regex.matchedTo() - 1
- + (spaces.get(regex.matchedTo() - 1)));
- // only add result if not contained in previous result
- if (lastm == null
- || (lastm.getSequence() != seq || (!(lastm.getStart() <= sres && lastm
- .getEnd() >= eres))))
- {
- lastm = searchResults.addResult(seq, sres, eres);
- }
- hasResults = true;
- if (!findAll)
- {
- // thats enough, break and display the result
- found = true;
- resIndex++;
-
- break;
- }
-
- r = resIndex;
- }
- else
- {
- break;
- }
+ // before the start of the sequence
+ continue;
}
-
- if (!found)
+ String subseq = seq.getSequenceAsString(range[0], range[1] + 1);
+ String ungapped = AlignSeq.extractGaps(Comparison.GapChars, subseq);
+ visibleSeq.append(ungapped);
+ if (!ungapped.isEmpty())
{
- seqIndex++;
- resIndex = -1;
+ /*
+ * 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 });
}
}
- /**
- * We now search the Id string in the main search loop. for (int id = 0; id
- * < alignment.getHeight(); id++) { if
- * (regex.search(alignment.getSequenceAt(id).getName())) {
- * idMatch.addElement(alignment.getSequenceAt(id)); hasResults = true; } }
+ /*
+ * construct the mapping
+ * from: visible sequence positions 1..length
+ * to: true residue positions of the alignment sequence
*/
- return hasResults;
- }
+ List<int[]> toRange = Arrays
+ .asList(new int[]
+ { 1, visibleSeq.length() });
+ searchedSequenceMap = new MapList(fromRanges, toRange, 1, 1);
- /**
- * @return the alignment
- */
- public AlignmentI getAlignment()
- {
- return alignment;
+ return visibleSeq.toString();
}
/**
- * @param alignment
- * the alignment to set
+ * 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.
+ * <p>
+ * Returns true if a sequence could be found, false if end of alignment was
+ * reached
+ *
+ * @param ignoreHidden
*/
- public void setAlignment(AlignmentI alignment)
+ private boolean nextSequence(boolean ignoreHidden)
{
- this.alignment = alignment;
- }
+ sequenceIndex++;
+ residueIndex = -1;
- /**
- * @return the caseSensitive
- */
- public boolean isCaseSensitive()
- {
- return caseSensitive;
+ return getSequence(ignoreHidden);
}
/**
- * @param caseSensitive
- * the caseSensitive to set
+ * Finds the next match in the given sequence, starting at offset
+ * {@code residueIndex}. Answers true if a match is found, else false.
+ * <p>
+ * If a match is found, {@code residueIndex} is advanced to the position after
+ * the start of the matched region, ready for the next search.
+ * <p>
+ * 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
*/
- public void setCaseSensitive(boolean caseSensitive)
+ protected boolean findNextMatch(String searchString,
+ Regex searchPattern, boolean matchDescription,
+ boolean ignoreHidden)
{
- this.caseSensitive = caseSensitive;
- }
+ if (residueIndex < 0)
+ {
+ /*
+ * at start of sequence; try find by residue number, in sequence id,
+ * or (optionally) in sequence description
+ */
+ if (doNonMotifSearches(searchString, searchPattern,
+ matchDescription))
+ {
+ return true;
+ }
+ }
- /**
- * @return the findAll
- */
- public boolean isFindAll()
- {
- return findAll;
- }
+ /*
+ * 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))
+ {
+ return true;
+ }
+ }
+ else
+ {
+ residueIndex = Integer.MAX_VALUE;
+ }
+ }
- /**
- * @param findAll
- * the findAll to set
- */
- public void setFindAll(boolean findAll)
- {
- this.findAll = findAll;
+ nextSequence(ignoreHidden);
+ return false;
}
/**
- * @return the selection
+ * Adds the match held in the <code>searchPattern</code> Regex to the
+ * <code>searchResults</code>, unless it is a subregion of the last match
+ * recorded. <code>residueIndex</code> 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.
+ * <p>
+ * Matches that lie entirely within hidden regions of the alignment are not
+ * added.
+ *
+ * @param searchPattern
+ * @param ignoreHidden
+ * @return
*/
- public jalview.datamodel.SequenceGroup getSelection()
+ protected boolean recordMatch(Regex searchPattern, boolean ignoreHidden)
{
- return selection;
+ 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 = searchPattern.matchedFrom()+1;
+
+ /*
+ * return false if the match is entirely in a hidden region
+ */
+ if (allHidden(seq, matchStartPosition, matchEndPosition))
+ {
+ return false;
+ }
+
+ /*
+ * check that this match is not a subset of the previous one (JAL-2302)
+ */
+ List<SearchResultMatchI> 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;
}
/**
- * @param selection
- * the selection to set
+ * 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 matchStartPosition
+ * @param matchEndPosition
+ * @param ignoreHidden
*/
- public void setSelection(jalview.datamodel.SequenceGroup selection)
+ private void addMatch(SequenceI seq, int matchStartPosition,
+ int matchEndPosition, boolean ignoreHidden)
{
- this.selection = selection;
+ if (!ignoreHidden)
+ {
+ /*
+ * simple case
+ */
+ searchResults.addResult(seq, matchStartPosition, matchEndPosition);
+ return;
+ }
+
+ /*
+ * get start-end contiguous ranges in underlying sequence
+ */
+ int[] truePositions = searchedSequenceMap
+ .locateInFrom(matchStartPosition, matchEndPosition);
+ searchResults.addResult(seq, truePositions);
}
/**
- * Returns the (possibly empty) list of matching sequences (when search
- * includes searching sequence names)
+ * Returns true if all residues are hidden, else false
*
+ * @param seq
+ * @param fromPos
+ * @param toPos
* @return
*/
- public Vector<SequenceI> getIdMatch()
+ private boolean allHidden(SequenceI seq, int fromPos, int toPos)
{
- return idMatch;
+ if (!viewport.hasHiddenColumns())
+ {
+ return false;
+ }
+ for (int res = fromPos; res <= toPos; res++)
+ {
+ if (isVisible(seq, res))
+ {
+ return false;
+ }
+ }
+ return true;
}
/**
- * @return the regex
+ * Does searches other than for residue patterns. Currently this includes
+ * <ul>
+ * <li>find residue by position (if search string is a number)</li>
+ * <li>match search string to sequence id</li>
+ * <li>match search string to sequence description (optional)</li>
+ * </ul>
+ * Answers true if a match is found, else false.
+ *
+ * @param searchString
+ * @param searchPattern
+ * @param includeDescription
+ * @return
*/
- public com.stevesoft.pat.Regex getRegex()
+ protected boolean doNonMotifSearches(String searchString,
+ Regex searchPattern, boolean includeDescription)
{
- return regex;
- }
+ SequenceI seq = viewport.getAlignment().getSequenceAt(sequenceIndex);
- /**
- * @return the searchResults
- */
- public SearchResultsI getSearchResults()
- {
- return searchResults;
+ /*
+ * position sequence search to start of sequence
+ */
+ residueIndex = 0;
+ try
+ {
+ int res = Integer.parseInt(searchString);
+ 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;
}
/**
- * @return the resIndex
+ * 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 seq
+ * @param searchPattern
+ * @return
*/
- public int getResIndex()
+ protected boolean searchSequenceDescription(SequenceI seq, Regex searchPattern)
{
- return resIndex;
+ String desc = seq.getDescription();
+ if (desc != null && searchPattern.search(desc) && !idMatches.contains(seq))
+ {
+ idMatches.add(seq);
+ return true;
+ }
+ return false;
}
/**
- * @param resIndex
- * the resIndex to set
+ * 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 seq
+ * @param searchPattern
+ * @return
*/
- public void setResIndex(int resIndex)
+ protected boolean searchSequenceName(SequenceI seq, Regex searchPattern)
{
- this.resIndex = resIndex;
+ if (searchPattern.search(seq.getName()) && !idMatches.contains(seq))
+ {
+ idMatches.add(seq);
+ return true;
+ }
+ return false;
}
/**
- * @return the seqIndex
+ * 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 int getSeqIndex()
+ protected boolean searchForResidueNumber(SequenceI seq, int resNo)
{
- return seqIndex;
+ if (seq.getStart() <= resNo && seq.getEnd() >= resNo)
+ {
+ if (isVisible(seq, resNo))
+ {
+ searchResults.addResult(seq, resNo, resNo);
+ return true;
+ }
+ }
+ return false;
}
/**
- * @param seqIndex
- * the seqIndex to set
+ * Returns true if the residue is in a visible column, else false
+ *
+ * @param seq
+ * @param res
+ * @return
*/
- public void setSeqIndex(int seqIndex)
+ private boolean isVisible(SequenceI seq, int res)
{
- this.seqIndex = seqIndex;
+ if (!viewport.hasHiddenColumns())
+ {
+ return true;
+ }
+ int col = seq.findIndex(res); // base 1
+ return viewport.getAlignment().getHiddenColumns().isVisible(col - 1); // base 0
}
- public boolean isIncludeDescription()
+ @Override
+ public List<SequenceI> getIdMatches()
{
- return includeDescription;
+ return idMatches;
}
- public void setIncludeDescription(boolean includeDescription)
+ @Override
+ public SearchResultsI getSearchResults()
{
- this.includeDescription = includeDescription;
+ return searchResults;
}
}