*/
package jalview.analysis;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
import java.util.Locale;
+import com.stevesoft.pat.Regex;
+
import jalview.api.AlignViewportI;
+import jalview.api.FeatureRenderer;
import jalview.api.FinderI;
import jalview.datamodel.AlignmentI;
import jalview.datamodel.SearchResultMatchI;
import jalview.datamodel.SearchResults;
import jalview.datamodel.SearchResultsI;
+import jalview.datamodel.SequenceFeature;
import jalview.datamodel.SequenceGroup;
import jalview.datamodel.SequenceI;
+import jalview.datamodel.features.SequenceFeaturesI;
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 com.stevesoft.pat.Regex;
-
/**
* Implements the search algorithm for the Find dialog
*/
private AlignViewportI viewport;
/*
+ * feature renderer model - if available
+ */
+ FeatureRenderer frm = null;
+
+ /*
* sequence index in alignment to search from
*/
private int sequenceIndex;
private int residueIndex;
/*
+ * last feature matched when incrementally searching sequence features
+ */
+ private SequenceFeature lastFeature;
+
+ /*
+ * last sequenceIndex used when lastFeature was discovered
+ */
+ private int lastFeatureSequenceIndex;
+
+ /*
* the true sequence position of the start of the
* last sequence searched (when 'ignore hidden regions' does not apply)
*/
@Override
public void findAll(String theSearchString, boolean matchCase,
- boolean searchDescription, boolean ignoreHidden)
+ boolean searchDescription, boolean searchFeatureDesc,
+ boolean ignoreHidden)
{
/*
* search from the start
*/
+ lastFeature = null;
+ lastFeatureSequenceIndex = 0;
sequenceIndex = 0;
residueIndex = -1;
- doFind(theSearchString, matchCase, searchDescription, true,
- ignoreHidden);
+ doFind(theSearchString, matchCase, searchDescription, searchFeatureDesc,
+ true, ignoreHidden);
/*
* reset to start for next search
*/
sequenceIndex = 0;
residueIndex = -1;
+ lastFeature = null;
+ lastFeatureSequenceIndex = 0;
}
@Override
public void findNext(String theSearchString, boolean matchCase,
- boolean searchDescription, boolean ignoreHidden)
+ boolean searchDescription, boolean searchFeatureDesc,
+ boolean ignoreHidden)
{
- doFind(theSearchString, matchCase, searchDescription, false,
- ignoreHidden);
+ doFind(theSearchString, matchCase, searchDescription, searchFeatureDesc,
+ false, ignoreHidden);
if (searchResults.isEmpty() && idMatches.isEmpty())
{
*/
sequenceIndex = 0;
residueIndex = -1;
+ lastFeature = null;
+ lastFeatureSequenceIndex = 0;
}
}
* @param ignoreHidden
*/
protected void doFind(String theSearchString, boolean matchCase,
- boolean searchDescription, boolean findAll, boolean ignoreHidden)
+ boolean searchDescription, boolean searchFeatureDesc,
+ boolean findAll, boolean ignoreHidden)
{
searchResults = new SearchResults();
idMatches = new ArrayList<>();
while ((!found || findAll) && sequenceIndex < end)
{
found = findNextMatch(searchString, searchPattern, searchDescription,
- ignoreHidden);
+ searchFeatureDesc, ignoreHidden);
}
}
* @return
*/
protected boolean findNextMatch(String searchString, Regex searchPattern,
- boolean matchDescription, boolean ignoreHidden)
+ boolean matchDescription, boolean matchFeatureDesc,
+ boolean ignoreHidden)
{
if (residueIndex < 0)
{
}
else
{
+ if (matchFeatureDesc)
+ {
+ matched = searchSequenceFeatures(residueIndex, searchPattern);
+ if (matched)
+ {
+ return true;
+ }
+ lastFeature = null;
+ }
residueIndex = Integer.MAX_VALUE;
}
}
}
/**
+ * Searches for a match with the sequence features, 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
+ */
+ protected boolean searchSequenceFeatures(int from, Regex searchPattern)
+ {
+ if (lastFeatureSequenceIndex != sequenceIndex)
+ {
+ lastFeatureSequenceIndex = sequenceIndex;
+ lastFeature = null;
+ }
+ SequenceI seq = viewport.getAlignment().getSequenceAt(sequenceIndex);
+ SequenceFeaturesI sf = seq.getFeatures();
+
+ // TODO - stash feature list and search incrementally
+ List<SequenceFeature> allFeatures = null;
+ if (frm != null)
+ {
+ allFeatures = frm.findFeaturesAtResidue(seq, seq.getStart(),
+ seq.getEnd());
+ }
+ else
+ {
+ allFeatures = sf.getAllFeatures(null);
+ }
+ // so we can check we are advancing when debugging
+ long fpos = 0;
+
+ for (SequenceFeature feature : allFeatures)
+ {
+ fpos++;
+ if (lastFeature != null)
+ {
+ // iterate till we find last feature matched
+ if (lastFeature != feature)
+ {
+ continue;
+ }
+ else
+ {
+ lastFeature = null;
+ continue;
+ }
+ }
+
+ if (searchPattern.search(feature.type) || (feature.description != null
+ && searchPattern.search(feature.description)))
+ {
+ searchResults.addResult(seq, feature.getBegin(), feature.getEnd());
+ lastFeature = feature;
+ return true;
+ }
+ }
+ residueIndex = Integer.MAX_VALUE;
+ lastFeature = null;
+ return false;
+ }
+
+ /**
* 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.
{
return searchResults;
}
+
+ @Override
+ public void setFeatureRenderer(FeatureRenderer featureRenderer)
+ {
+ frm = featureRenderer;
+ }
}