import jalview.datamodel.SequenceFeature;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
-import intervalstore.api.IntervalStoreI;
import intervalstore.impl.BinarySearcher;
-import intervalstore.impl.IntervalStore;
-/**
- * A data store for a set of sequence features that supports efficient lookup of
- * features overlapping a given range. Intended for (but not limited to) storage
- * of features for one sequence and feature type.
- *
- * @author gmcarstairs
- *
- */
-public class FeatureStore
+public abstract class FeatureStore implements FeatureStoreI
{
+
/*
* Non-positional features have no (zero) start/end position.
* Kept as a separate list in case this criterion changes in future.
* IntervalStore holds remaining features and provides efficient
* query for features overlapping any given interval
*/
- IntervalStoreI<SequenceFeature> features;
+ Collection<SequenceFeature> features;
+
+ @Override
+ public Collection<SequenceFeature> getFeatures()
+ {
+ return features;
+ }
/*
* Feature groups represented in stored positional features
*/
public FeatureStore()
{
- features = new IntervalStore<>();
positionalFeatureGroups = new HashSet<>();
nonPositionalFeatureGroups = new HashSet<>();
positionalMinScore = Float.NaN;
*
* @param feature
*/
+
+ @Override
public boolean addFeature(SequenceFeature feature)
{
if (contains(feature))
* @param feature
* @return
*/
+ @Override
public boolean contains(SequenceFeature feature)
{
if (feature.isNonPositional())
{
- return nonPositionalFeatures == null ? false : nonPositionalFeatures
- .contains(feature);
+ return nonPositionalFeatures == null ? false
+ : nonPositionalFeatures.contains(feature);
}
if (feature.isContactFeature())
{
- return contactFeatureStarts == null ? false : listContains(
- contactFeatureStarts, feature);
+ return contactFeatureStarts != null
+ && listContains(contactFeatureStarts, feature);
}
- return features == null ? false : features
- .contains(feature);
+ return features == null ? false : features.contains(feature);
}
/**
*/
protected synchronized void addNestedFeature(SequenceFeature feature)
{
- if (features == null)
- {
- features = new IntervalStore<>();
- }
features.add(feature);
}
-
/**
* Add a contact feature to the lists that hold them ordered by start (first
* contact) and by end (second contact) position, ensuring the lists remain
if (contactFeatureStarts == null)
{
contactFeatureStarts = new ArrayList<>();
- }
- if (contactFeatureEnds == null)
- {
contactFeatureEnds = new ArrayList<>();
}
f -> f.getBegin() >= feature.getBegin());
contactFeatureStarts.add(insertPosition, feature);
-
/*
* insert into list sorted by end (second contact position):
* binary search the sorted list to find the insertion point
return false;
}
- /**
- * Returns a (possibly empty) list of features whose extent overlaps the given
- * range. The returned list is not ordered. Contact features are included if
- * either of the contact points lies within the range.
- *
- * @param start
- * start position of overlap range (inclusive)
- * @param end
- * end position of overlap range (inclusive)
- * @return
- */
- public List<SequenceFeature> findOverlappingFeatures(long start, long end)
- {
- List<SequenceFeature> result = new ArrayList<>();
-
- findContactFeatures(start, end, result);
-
- if (features != null)
- {
- result.addAll(features.findOverlaps(start, end));
- }
-
- return result;
- }
-
- /**
- * Adds contact features to the result list where either the second or the
- * first contact position lies within the target range
- *
- * @param from
- * @param to
- * @param result
- */
- protected void findContactFeatures(long from, long to,
- List<SequenceFeature> result)
- {
- if (contactFeatureStarts != null)
- {
- findContactStartOverlaps(from, to, result);
- }
- if (contactFeatureEnds != null)
- {
- findContactEndOverlaps(from, to, result);
- }
- }
-
- /**
- * Adds to the result list any contact features whose end (second contact
- * point), but not start (first contact point), lies in the query from-to
- * range
- *
- * @param from
- * @param to
- * @param result
- */
- protected void findContactEndOverlaps(long from, long to,
- List<SequenceFeature> result)
- {
- /*
- * find the first contact feature (if any)
- * whose end point is not before the target range
- */
- int index = BinarySearcher.findFirst(contactFeatureEnds,
- f -> f.getEnd() >= from);
-
- while (index < contactFeatureEnds.size())
- {
- SequenceFeature sf = contactFeatureEnds.get(index);
- if (!sf.isContactFeature())
- {
- System.err.println("Error! non-contact feature type "
- + sf.getType() + " in contact features list");
- index++;
- continue;
- }
-
- int begin = sf.getBegin();
- if (begin >= from && begin <= to)
- {
- /*
- * this feature's first contact position lies in the search range
- * so we don't include it in results a second time
- */
- index++;
- continue;
- }
-
- if (sf.getEnd() > to)
- {
- /*
- * this feature (and all following) has end point after the target range
- */
- break;
- }
-
- /*
- * feature has end >= from and end <= to
- * i.e. contact end point lies within overlap search range
- */
- result.add(sf);
- index++;
- }
- }
-
- /**
- * Adds contact features whose start position lies in the from-to range to the
- * result list
- *
- * @param from
- * @param to
- * @param result
- */
- protected void findContactStartOverlaps(long from, long to,
- List<SequenceFeature> result)
- {
- int index = BinarySearcher.findFirst(contactFeatureStarts,
- f -> f.getBegin() >= from);
-
- while (index < contactFeatureStarts.size())
- {
- SequenceFeature sf = contactFeatureStarts.get(index);
- if (!sf.isContactFeature())
- {
- System.err.println("Error! non-contact feature " + sf.toString()
- + " in contact features list");
- index++;
- continue;
- }
- if (sf.getBegin() > to)
- {
- /*
- * this feature's start (and all following) follows the target range
- */
- break;
- }
-
- /*
- * feature has begin >= from and begin <= to
- * i.e. contact start point lies within overlap search range
- */
- result.add(sf);
- index++;
- }
- }
+ abstract protected void findContactFeatures(long from, long to,
+ List<SequenceFeature> result);
/**
* Answers a list of all positional features stored, in no guaranteed order
*
* @return
*/
- public List<SequenceFeature> getPositionalFeatures()
+
+ @Override
+ public List<SequenceFeature> getPositionalFeatures(
+ List<SequenceFeature> result)
{
- List<SequenceFeature> result = new ArrayList<>();
/*
* add any contact features - from the list by start position
*
* @return
*/
- public List<SequenceFeature> getContactFeatures()
+
+ @Override
+ public List<SequenceFeature> getContactFeatures(
+ List<SequenceFeature> result)
{
- if (contactFeatureStarts == null)
+ if (contactFeatureStarts != null)
{
- return Collections.emptyList();
+ result.addAll(contactFeatureStarts);
}
- return new ArrayList<>(contactFeatureStarts);
+ return result;
}
/**
*
* @return
*/
- public List<SequenceFeature> getNonPositionalFeatures()
+
+ @Override
+ public List<SequenceFeature> getNonPositionalFeatures(
+ List<SequenceFeature> result)
{
- if (nonPositionalFeatures == null)
+ if (nonPositionalFeatures != null)
{
- return Collections.emptyList();
+ result.addAll(nonPositionalFeatures);
}
- return new ArrayList<>(nonPositionalFeatures);
+ return result;
}
/**
*
* @param sf
*/
+
+ @Override
public synchronized boolean delete(SequenceFeature sf)
{
boolean removed = false;
positionalMaxScore = Float.NaN;
nonPositionalMinScore = Float.NaN;
nonPositionalMaxScore = Float.NaN;
-
/*
* scan non-positional features for groups and scores
*/
- for (SequenceFeature sf : getNonPositionalFeatures())
+ if (nonPositionalFeatures != null)
{
- nonPositionalFeatureGroups.add(sf.getFeatureGroup());
- float score = sf.getScore();
- nonPositionalMinScore = min(nonPositionalMinScore, score);
- nonPositionalMaxScore = max(nonPositionalMaxScore, score);
+ for (SequenceFeature sf : nonPositionalFeatures)
+ {
+ nonPositionalFeatureGroups.add(sf.getFeatureGroup());
+ float score = sf.getScore();
+ nonPositionalMinScore = min(nonPositionalMinScore, score);
+ nonPositionalMaxScore = max(nonPositionalMaxScore, score);
+ }
}
/*
* scan positional features for groups, scores and extents
*/
- for (SequenceFeature sf : getPositionalFeatures())
+
+ rescanPositional(contactFeatureStarts);
+ rescanPositional(features);
+ }
+
+ private void rescanPositional(Collection<SequenceFeature> sfs)
+ {
+ if (sfs == null)
+ {
+ return;
+ }
+ for (SequenceFeature sf : sfs)
{
positionalFeatureGroups.add(sf.getFeatureGroup());
float score = sf.getScore();
}
}
+
/**
* Answers true if this store has no features, else false
*
* @return
*/
+
+ @Override
public boolean isEmpty()
{
boolean hasFeatures = (contactFeatureStarts != null
- && !contactFeatureStarts
- .isEmpty())
- || (nonPositionalFeatures != null && !nonPositionalFeatures
- .isEmpty())
- || (features != null && features.size() > 0);
+ && !contactFeatureStarts.isEmpty())
+ || (nonPositionalFeatures != null
+ && !nonPositionalFeatures.isEmpty())
+ || features.size() > 0;
return !hasFeatures;
}
* @param positionalFeatures
* @return
*/
+
+ @Override
public Set<String> getFeatureGroups(boolean positionalFeatures)
{
if (positionalFeatures)
}
else
{
- return nonPositionalFeatureGroups == null ? Collections
- .<String> emptySet() : Collections
- .unmodifiableSet(nonPositionalFeatureGroups);
+ return nonPositionalFeatureGroups == null
+ ? Collections.<String> emptySet()
+ : Collections.unmodifiableSet(nonPositionalFeatureGroups);
}
}
/**
- * Answers the number of positional (or non-positional) features stored.
- * Contact features count as 1.
+ * Answers a list of all either positional or non-positional features whose
+ * feature group matches the given group (which may be null)
*
* @param positional
+ * @param group
* @return
*/
- public int getFeatureCount(boolean positional)
+
+ @Override
+ public List<SequenceFeature> getFeaturesForGroup(boolean positional,
+ String group)
{
- if (!positional)
+ List<SequenceFeature> result = new ArrayList<>();
+
+ /*
+ * if we know features don't include the target group, no need
+ * to inspect them for matches
+ */
+ if (positional && !positionalFeatureGroups.contains(group)
+ || !positional && !nonPositionalFeatureGroups.contains(group))
{
- return nonPositionalFeatures == null ? 0 : nonPositionalFeatures
- .size();
+ return result;
}
- int size = 0;
+ if (positional)
+ {
+ addFeaturesForGroup(group, contactFeatureStarts, result);
+ addFeaturesForGroup(group, features, result);
+ }
+ else
+ {
+ addFeaturesForGroup(group, nonPositionalFeatures, result);
+ }
+ return result;
+ }
- if (contactFeatureStarts != null)
+ private void addFeaturesForGroup(String group,
+ Collection<SequenceFeature> sfs, List<SequenceFeature> result)
+ {
+ if (sfs == null)
+ {
+ return;
+ }
+ for (SequenceFeature sf : sfs)
{
- // note a contact feature (start/end) counts as one
- size += contactFeatureStarts.size();
+ String featureGroup = sf.getFeatureGroup();
+ if (group == null && featureGroup == null
+ || group != null && group.equals(featureGroup))
+ {
+ result.add(sf);
+ }
}
+ }
- if (features != null)
+ /**
+ * Answers the number of positional (or non-positional) features stored.
+ * Contact features count as 1.
+ *
+ * @param positional
+ * @return
+ */
+
+ @Override
+ public int getFeatureCount(boolean positional)
+ {
+ if (!positional)
{
- size += features.size();
+ return nonPositionalFeatures == null ? 0
+ : nonPositionalFeatures.size();
}
- return size;
+ return (contactFeatureStarts == null ? 0 : contactFeatureStarts.size())
+ + features.size();
+
}
+
/**
* Answers the total length of positional features (or zero if there are
* none). Contact features contribute a value of 1 to the total.
*
* @return
*/
+
+ @Override
public int getTotalFeatureLength()
{
return totalExtent;
* @param positional
* @return
*/
+
+ @Override
public float getMinimumScore(boolean positional)
{
return positional ? positionalMinScore : nonPositionalMinScore;
* @param positional
* @return
*/
+
+ @Override
public float getMaximumScore(boolean positional)
{
return positional ? positionalMaxScore : nonPositionalMaxScore;
}
- /**
- * Answers a list of all either positional or non-positional features whose
- * feature group matches the given group (which may be null)
- *
- * @param positional
- * @param group
- * @return
- */
- public List<SequenceFeature> getFeaturesForGroup(boolean positional,
- String group)
- {
- List<SequenceFeature> result = new ArrayList<>();
-
- /*
- * if we know features don't include the target group, no need
- * to inspect them for matches
- */
- if (positional && !positionalFeatureGroups.contains(group)
- || !positional && !nonPositionalFeatureGroups.contains(group))
- {
- return result;
- }
-
- List<SequenceFeature> sfs = positional ? getPositionalFeatures()
- : getNonPositionalFeatures();
- for (SequenceFeature sf : sfs)
- {
- String featureGroup = sf.getFeatureGroup();
- if (group == null && featureGroup == null || group != null
- && group.equals(featureGroup))
- {
- result.add(sf);
- }
- }
- return result;
- }
/**
- * Adds the shift value to the start and end of all positional features.
- * Returns true if at least one feature was updated, else false.
+ * Adds the shift amount to the start and end of all positional features whose
+ * start position is at or after fromPosition. Returns true if at least one
+ * feature was shifted, else false.
*
- * @param shift
+ * @param fromPosition
+ * @param shiftBy
* @return
*/
- public synchronized boolean shiftFeatures(int shift)
+
+ @Override
+ public synchronized boolean shiftFeatures(int fromPosition, int shiftBy)
{
/*
* Because begin and end are final fields (to ensure the data store's
boolean modified = false;
for (SequenceFeature sf : getPositionalFeatures())
{
- modified = true;
- int newBegin = sf.getBegin() + shift;
- int newEnd = sf.getEnd() + shift;
-
- /*
- * sanity check: don't shift left of the first residue
- */
- if (newEnd > 0)
+ if (sf.getBegin() >= fromPosition)
{
- newBegin = Math.max(1, newBegin);
- SequenceFeature sf2 = new SequenceFeature(sf, newBegin, newEnd,
- sf.getFeatureGroup(), sf.getScore());
- addFeature(sf2);
+ modified = true;
+ int newBegin = sf.getBegin() + shiftBy;
+ int newEnd = sf.getEnd() + shiftBy;
+
+ /*
+ * sanity check: don't shift left of the first residue
+ */
+ if (newEnd > 0)
+ {
+ newBegin = Math.max(1, newBegin);
+ SequenceFeature sf2 = new SequenceFeature(sf, newBegin, newEnd,
+ sf.getFeatureGroup(), sf.getScore());
+ addFeature(sf2);
+ }
+ delete(sf);
}
- delete(sf);
}
return modified;
}
+
+
+ @Override
+ public List<SequenceFeature> findOverlappingFeatures(long start, long end)
+ {
+ return findOverlappingFeatures(start, end, null);
+ }
+
+ @Override
+ public List<SequenceFeature> getPositionalFeatures()
+ {
+ return getPositionalFeatures(new ArrayList<>());
+ }
+
+ @Override
+ public List<SequenceFeature> getContactFeatures()
+ {
+ return getContactFeatures(new ArrayList<>());
+ }
+
+ @Override
+ public List<SequenceFeature> getNonPositionalFeatures()
+ {
+ return getNonPositionalFeatures(new ArrayList<>());
+ }
+
}