import java.util.List;
import java.util.Set;
-public abstract class FeatureStore implements FeatureStoreI
+import intervalstore.api.IntervalStoreI;
+import intervalstore.impl.BinarySearcher;
+import intervalstore.impl.BinarySearcher.Compare;
+
+public class FeatureStore
{
+ public enum IntervalStoreType
+ {
+ /**
+ * original NCList-based IntervalStore
+ */
+ INTERVAL_STORE_NCLIST,
+
+ /**
+ * linked-list IntervalStore
+ */
+ INTERVAL_STORE_LINKED_LIST,
+
+ /**
+ * NCList as array buffer IntervalStore
+ */
+ INTERVAL_STORE_NCARRAY
+ }
+
+ /*
+ * track largest start for quick insertion of ordered features
+ */
+ protected int maxStart = -1;
+
+ protected int maxContactStart = -1;
+
+ /*
+ * Non-positional features have no (zero) start/end position.
+ * Kept as a separate list in case this criterion changes in future.
+ */
+ List<SequenceFeature> nonPositionalFeatures;
+
+ /*
+ * contact features ordered by first contact position
+ */
+ List<SequenceFeature> contactFeatureStarts;
+
+ /*
+ * contact features ordered by second contact position
+ */
+ List<SequenceFeature> contactFeatureEnds;
+
+ /*
+ * IntervalStore holds remaining features and provides efficient
+ * query for features overlapping any given interval
+ */
+ IntervalStoreI<SequenceFeature> features;
+
+ /*
+ * Feature groups represented in stored positional features
+ * (possibly including null)
+ */
+ Set<String> positionalFeatureGroups;
+
+ /*
+ * Feature groups represented in stored non-positional features
+ * (possibly including null)
+ */
+ Set<String> nonPositionalFeatureGroups;
+
+ /*
+ * the total length of all positional features; contact features count 1 to
+ * the total and 1 to size(), consistent with an average 'feature length' of 1
+ */
+ int totalExtent;
+
+ float positionalMinScore;
+
+ float positionalMaxScore;
+
+ float nonPositionalMinScore;
+
+ float nonPositionalMaxScore;
/**
* Answers the 'length' of the feature, counting 0 for non-positional features
* optimised for the condition that the list is sorted on feature start
* position ascending, and will give unreliable results if this does not hold.
*
- * @param features
+ * @param list
* @param feature
* @return
*/
- @Override
- public boolean listContains(List<SequenceFeature> features,
+ public static boolean listContains(List<SequenceFeature> list,
SequenceFeature feature)
{
- if (features == null || feature == null)
+ if (list == null || feature == null)
{
return false;
}
/*
* locate the first entry in the list which does not precede the feature
*/
- int pos = findFirstBegin(features, feature.begin);
- int len = features.size();
+ int begin = feature.begin;
+ int pos = BinarySearcher.findFirst(list, true, Compare.GE, begin);
+ int len = list.size();
while (pos < len)
{
- SequenceFeature sf = features.get(pos);
- if (sf.getBegin() > feature.getBegin())
+ SequenceFeature sf = list.get(pos);
+ if (sf.begin > begin)
{
return false; // no match found
}
}
}
- /*
- * Non-positional features have no (zero) start/end position.
- * Kept as a separate list in case this criterion changes in future.
- */
- List<SequenceFeature> nonPositionalFeatures;
-
- /*
- * contact features ordered by first contact position
- */
- List<SequenceFeature> contactFeatureStarts;
-
- /*
- * contact features ordered by second contact position
- */
- List<SequenceFeature> contactFeatureEnds;
-
- /*
- * IntervalStore holds remaining features and provides efficient
- * query for features overlapping any given interval
- */
- Collection<SequenceFeature> features;
-
- /*
- * Feature groups represented in stored positional features
- * (possibly including null)
- */
- Set<String> positionalFeatureGroups;
-
- /*
- * Feature groups represented in stored non-positional features
- * (possibly including null)
- */
- Set<String> nonPositionalFeatureGroups;
-
- /*
- * the total length of all positional features; contact features count 1 to
- * the total and 1 to size(), consistent with an average 'feature length' of 1
+ /**
+ * Constructor that defaults to using NCList IntervalStore
*/
- int totalExtent;
-
- float positionalMinScore;
-
- float positionalMaxScore;
-
- float nonPositionalMinScore;
-
- float nonPositionalMaxScore;
+ public FeatureStore()
+ {
+ this(IntervalStoreType.INTERVAL_STORE_NCLIST);
+ }
/**
- * Constructor
+ * Constructor that allows an alternative IntervalStore implementation to be
+ * chosen
*/
- public FeatureStore()
+ public FeatureStore(IntervalStoreType intervalStoreType)
{
+ features = getIntervalStore(intervalStoreType);
positionalFeatureGroups = new HashSet<>();
nonPositionalFeatureGroups = new HashSet<>();
positionalMinScore = Float.NaN;
nonPositionalMinScore = Float.NaN;
nonPositionalMaxScore = Float.NaN;
- // we only construct nonPositionalFeatures, contactFeatures if we need to
+ // only construct nonPositionalFeatures or contactFeatures if needed
+ }
+
+ /**
+ * Returns a new instance of IntervalStoreI of implementation as selected by
+ * the type parameter
+ *
+ * @param type
+ * @return
+ */
+ private IntervalStoreI<SequenceFeature> getIntervalStore(
+ IntervalStoreType type)
+ {
+ switch (type)
+ {
+ default:
+ case INTERVAL_STORE_NCLIST:
+ return new intervalstore.impl.IntervalStore<>();
+ case INTERVAL_STORE_NCARRAY:
+ return new intervalstore.nonc.IntervalStore<>();
+ case INTERVAL_STORE_LINKED_LIST:
+ return new intervalstore.nonc.IntervalStore0<>();
+ }
}
/**
* insert into list sorted by start (first contact position):
* binary search the sorted list to find the insertion point
*/
- contactFeatureStarts.add(
- findFirstBegin(contactFeatureStarts, feature.begin), feature);
+ int insertAt = BinarySearcher.findFirst(contactFeatureStarts, true,
+ Compare.GE, feature.begin);
+ contactFeatureStarts.add(insertAt, feature);
/*
* insert into list sorted by end (second contact position):
* binary search the sorted list to find the insertion point
*
* @param feature
*/
-
- @Override
public boolean addFeature(SequenceFeature feature)
{
- if (contains(feature))
- {
- return false;
- }
-
- /*
- * keep a record of feature groups
- */
- if (!feature.isNonPositional())
- {
- positionalFeatureGroups.add(feature.getFeatureGroup());
- }
-
if (feature.isContactFeature())
{
+ if (containsContactFeature(feature))
+ {
+ return false;
+ }
+ positionalFeatureGroups.add(feature.getFeatureGroup());
+ if (feature.begin > maxContactStart)
+ {
+ maxContactStart = feature.begin;
+ }
addContactFeature(feature);
}
else if (feature.isNonPositional())
{
+ if (containsNonPositionalFeature(feature))
+ {
+ return false;
+ }
+
addNonPositionalFeature(feature);
}
else
{
- addNestedFeature(feature);
+ if (!features.add(feature, false))
+ {
+ return false;
+ }
+ positionalFeatureGroups.add(feature.getFeatureGroup());
+ if (feature.begin > maxStart)
+ {
+ maxStart = feature.begin;
+ }
}
/*
return true;
}
+ /**
+ * A helper method that adds to the result list any features from the
+ * collection provided whose feature group matches the specified group
+ *
+ * @param group
+ * @param sfs
+ * @param result
+ */
private void addFeaturesForGroup(String group,
Collection<SequenceFeature> sfs, List<SequenceFeature> result)
{
}
/**
- * Adds one feature to the IntervalStore that can manage nested features
- * (creating the IntervalStore if necessary)
- */
- abstract protected void addNestedFeature(SequenceFeature feature);
-
- /**
* Adds the feature to the list of non-positional features (with lazy
* instantiation of the list if it is null), and returns true. The feature
* group is added to the set of distinct feature groups for non-positional
* @param feature
* @return
*/
- @Override
public boolean contains(SequenceFeature feature)
{
if (feature.isNonPositional())
{
- return nonPositionalFeatures == null ? false
- : nonPositionalFeatures.contains(feature);
+ return containsNonPositionalFeature(feature);
}
if (feature.isContactFeature())
{
- return contactFeatureStarts != null
- && listContains(contactFeatureStarts, feature);
+ return containsContactFeature(feature);
}
- return features == null ? false : containsFeature(feature);
+ return containsPositionalFeature(feature);
}
+ private boolean containsPositionalFeature(SequenceFeature feature)
+ {
+ return features == null || feature.begin > maxStart ? false
+ : features.contains(feature);
+ }
- abstract protected boolean containsFeature(SequenceFeature feature);
+ /**
+ * Answers true if this store already contains a contact feature equal to the
+ * given feature (by {@code SequenceFeature.equals()} test), else false
+ *
+ * @param feature
+ * @return
+ */
+ private boolean containsContactFeature(SequenceFeature feature)
+ {
+ return contactFeatureStarts != null && feature.begin <= maxContactStart
+ && listContains(contactFeatureStarts, feature);
+ }
+
+ /**
+ * Answers true if this store already contains a non-positional feature equal
+ * to the given feature (by {@code SequenceFeature.equals()} test), else false
+ *
+ * @param feature
+ * @return
+ */
+ private boolean containsNonPositionalFeature(SequenceFeature feature)
+ {
+ return nonPositionalFeatures == null ? false
+ : nonPositionalFeatures.contains(feature);
+ }
/**
* Deletes the given feature from the store, returning true if it was found
*
* @param sf
*/
-
- @Override
public synchronized boolean delete(SequenceFeature sf)
{
boolean removed = false;
}
}
- boolean removedNonPositional = false;
-
/*
* if not found, try non-positional features
*/
if (!removed && nonPositionalFeatures != null)
{
- removedNonPositional = nonPositionalFeatures.remove(sf);
- removed = removedNonPositional;
+ removed = nonPositionalFeatures.remove(sf);
}
/*
return removed;
}
- abstract protected void findContactFeatures(long from, long to,
- List<SequenceFeature> result);
+ public List<SequenceFeature> findFeatures(long start, long end)
+ {
+ return findFeatures(start, end, null);
+ }
- abstract protected int findFirstBegin(List<SequenceFeature> list,
- long pos);
+ /**
+ * 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. If the {@code result}
+ * parameter is not null, new entries are added to this list and the (possibly
+ * extended) list returned.
+ *
+ * @param start
+ * start position of overlap range (inclusive)
+ * @param end
+ * end position of overlap range (inclusive)
+ * @param result
+ * @return
+ */
+ public List<SequenceFeature> findFeatures(long start, long end,
+ List<SequenceFeature> result)
+ {
+ if (result == null)
+ {
+ result = new ArrayList<>();
+ }
- abstract protected int findFirstEnd(List<SequenceFeature> list, long pos);
+ findContactFeatures(start, end, result);
+ features.findOverlaps(start, end, result);
- @Override
- public List<SequenceFeature> findOverlappingFeatures(long start, long end)
- {
- return findOverlappingFeatures(start, end, null);
+ return result;
}
- @Override
+ /**
+ * Returns a (possibly empty) list of stored contact features
+ *
+ * @return
+ */
public List<SequenceFeature> getContactFeatures()
{
- return getContactFeatures(new ArrayList<>());
+ List<SequenceFeature> result = new ArrayList<>();
+ getContactFeatures(result);
+ return result;
}
/**
- * Answers a list of all contact features. If there are none, returns an
- * immutable empty list.
+ * Adds any stored contact features to the result list
*
* @return
*/
-
- @Override
- public List<SequenceFeature> getContactFeatures(
- List<SequenceFeature> result)
+ public void getContactFeatures(List<SequenceFeature> result)
{
if (contactFeatureStarts != null)
{
result.addAll(contactFeatureStarts);
}
- return result;
}
/**
* @param positional
* @return
*/
-
- @Override
public int getFeatureCount(boolean positional)
{
if (!positional)
: nonPositionalFeatures.size();
}
- return (contactFeatureStarts == null ? 0 : contactFeatureStarts.size())
- + features.size();
+ int size = 0;
+ if (contactFeatureStarts != null)
+ {
+ // note a contact feature (start/end) counts as one
+ size += contactFeatureStarts.size();
+ }
+
+ if (features != null)
+ {
+ size += features.size();
+ }
+ return size;
}
/**
* @param positionalFeatures
* @return
*/
-
- @Override
public Set<String> getFeatureGroups(boolean positionalFeatures)
{
if (positionalFeatures)
}
}
- @Override
- public Collection<SequenceFeature> getFeatures()
- {
- return features;
- }
-
/**
* Answers a list of all either positional or non-positional features whose
* feature group matches the given group (which may be null)
* @param group
* @return
*/
-
- @Override
public List<SequenceFeature> getFeaturesForGroup(boolean positional,
String group)
{
* @param positional
* @return
*/
-
- @Override
public float getMaximumScore(boolean positional)
{
return positional ? positionalMaxScore : nonPositionalMaxScore;
* @param positional
* @return
*/
-
- @Override
public float getMinimumScore(boolean positional)
{
return positional ? positionalMinScore : nonPositionalMinScore;
}
- @Override
+ /**
+ * Answers a (possibly empty) list of all non-positional features
+ *
+ * @return
+ */
public List<SequenceFeature> getNonPositionalFeatures()
{
- return getNonPositionalFeatures(new ArrayList<>());
+ List<SequenceFeature> result = new ArrayList<>();
+ getNonPositionalFeatures(result);
+ return result;
}
/**
- * Answers a list of all non-positional features. If there are none, returns
- * an immutable empty list.
+ * Adds any stored non-positional features to the result list
*
* @return
*/
-
- @Override
- public List<SequenceFeature> getNonPositionalFeatures(
- List<SequenceFeature> result)
+ public void getNonPositionalFeatures(List<SequenceFeature> result)
{
if (nonPositionalFeatures != null)
{
result.addAll(nonPositionalFeatures);
}
- return result;
}
- @Override
+ /**
+ * Returns a (possibly empty) list of all positional features stored
+ *
+ * @return
+ */
public List<SequenceFeature> getPositionalFeatures()
{
- return getPositionalFeatures(new ArrayList<>());
+ List<SequenceFeature> result = new ArrayList<>();
+ getPositionalFeatures(result);
+
+ return result;
}
/**
- * Answers a list of all positional features stored, in no guaranteed order
- *
- * @return
+ * Adds all positional features stored to the result list, in no guaranteed
+ * order, and with no check for duplicates
*/
-
- @Override
- public List<SequenceFeature> getPositionalFeatures(
- List<SequenceFeature> result)
+ public void getPositionalFeatures(List<SequenceFeature> result)
{
-
/*
* add any contact features - from the list by start position
*/
{
result.addAll(features);
}
-
- return result;
}
/**
*
* @return
*/
-
- @Override
public int getTotalFeatureLength()
{
return totalExtent;
*
* @return
*/
-
- @Override
public boolean isEmpty()
{
boolean hasFeatures = (contactFeatureStarts != null
&& !contactFeatureStarts.isEmpty())
|| (nonPositionalFeatures != null
&& !nonPositionalFeatures.isEmpty())
- || features.size() > 0;
+ || (features != null && features.size() > 0);
return !hasFeatures;
}
*/
if (nonPositionalFeatures != null)
{
- for (SequenceFeature sf : nonPositionalFeatures)
+ for (int i = 0, n = nonPositionalFeatures.size(); i < n; i++)
{
+ SequenceFeature sf = nonPositionalFeatures.get(i);
nonPositionalFeatureGroups.add(sf.getFeatureGroup());
float score = sf.getScore();
nonPositionalMinScore = min(nonPositionalMinScore, score);
}
}
- /*
- * scan positional features for groups, scores and extents
- */
-
rescanPositional(contactFeatureStarts);
rescanPositional(features);
}
+ /**
+ * Scans the given features and updates cached feature groups, minimum and
+ * maximum feature score, and total feature extent (length) for positional
+ * features
+ *
+ * @param sfs
+ */
private void rescanPositional(Collection<SequenceFeature> sfs)
{
if (sfs == null)
* @param shiftBy
* @return
*/
-
- @Override
public synchronized boolean shiftFeatures(int fromPosition, int shiftBy)
{
/*
* (Although a simple shift of all values would preserve data integrity!)
*/
boolean modified = false;
- for (SequenceFeature sf : getPositionalFeatures())
+ List<SequenceFeature> list = getPositionalFeatures();
+ for (int i = 0, n = list.size(); i < n; i++)
{
+ SequenceFeature sf = list.get(i);
if (sf.getBegin() >= fromPosition)
{
modified = true;
return modified;
}
+ /**
+ * Answers the position (0, 1...) in the list of the first entry whose end
+ * position is not less than {@ pos}. If no such entry is found, answers the
+ * length of the list.
+ *
+ * @param list
+ * @param pos
+ * @return
+ */
+ protected int findFirstEnd(List<SequenceFeature> list, long pos)
+ {
+ return BinarySearcher.findFirst(list, false, Compare.GE, (int) pos);
+ }
+
+ /**
+ * 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);
+ 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
+ */
+ private 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 = findFirstEnd(contactFeatureEnds, from);
+
+ int n = contactFeatureEnds.size();
+ while (index < n)
+ {
+ 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
+ */
+ private void findContactStartOverlaps(long from, long to,
+ List<SequenceFeature> result)
+ {
+ int index = BinarySearcher.findFirst(contactFeatureStarts, true,
+ Compare.GE, (int) 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++;
+ }
+ }
+
}