import java.util.List;
import java.util.Set;
-import intervalstore.impl.BinarySearcher;
-
public abstract class FeatureStore implements FeatureStoreI
{
+ /**
+ * Answers the 'length' of the feature, counting 0 for non-positional features
+ * and 1 for contact features
+ *
+ * @param feature
+ * @return
+ */
+ protected static int getFeatureLength(SequenceFeature feature)
+ {
+ if (feature.isNonPositional())
+ {
+ return 0;
+ }
+ if (feature.isContactFeature())
+ {
+ return 1;
+ }
+ return 1 + feature.getEnd() - feature.getBegin();
+ }
+
+ /**
+ * Answers true if the list contains the feature, else false. This method is
+ * 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 feature
+ * @return
+ */
+ @Override
+ public boolean listContains(List<SequenceFeature> features,
+ SequenceFeature feature)
+ {
+ if (features == 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();
+ while (pos < len)
+ {
+ SequenceFeature sf = features.get(pos);
+ if (sf.getBegin() > feature.getBegin())
+ {
+ return false; // no match found
+ }
+ if (sf.equals(feature))
+ {
+ return true;
+ }
+ pos++;
+ }
+ return false;
+ }
+
+ /**
+ * A helper method to return the maximum of two floats, where a non-NaN value
+ * is treated as 'greater than' a NaN value (unlike Math.max which does the
+ * opposite)
+ *
+ * @param f1
+ * @param f2
+ */
+ protected static float max(float f1, float f2)
+ {
+ if (Float.isNaN(f1))
+ {
+ return Float.isNaN(f2) ? f1 : f2;
+ }
+ else
+ {
+ return Float.isNaN(f2) ? f1 : Math.max(f1, f2);
+ }
+ }
+
+ /**
+ * A helper method to return the minimum of two floats, where a non-NaN value
+ * is treated as 'less than' a NaN value (unlike Math.min which does the
+ * opposite)
+ *
+ * @param f1
+ * @param f2
+ */
+ protected static float min(float f1, float f2)
+ {
+ if (Float.isNaN(f1))
+ {
+ return Float.isNaN(f2) ? f1 : f2;
+ }
+ else
+ {
+ return Float.isNaN(f2) ? f1 : Math.min(f1, f2);
+ }
+ }
+
/*
* Non-positional features have no (zero) start/end position.
* Kept as a separate list in case this criterion changes in future.
*/
Collection<SequenceFeature> features;
- @Override
- public Collection<SequenceFeature> getFeatures()
- {
- return features;
- }
-
/*
* Feature groups represented in stored positional features
* (possibly including null)
}
/**
+ * 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
+ * ordered, and returns true. This method allows duplicate features to be
+ * added, so test before calling to avoid this.
+ *
+ * @param feature
+ * @return
+ */
+ protected synchronized boolean addContactFeature(SequenceFeature feature)
+ {
+ if (contactFeatureStarts == null)
+ {
+ contactFeatureStarts = new ArrayList<>();
+ contactFeatureEnds = new ArrayList<>();
+ }
+
+ /*
+ * 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);
+ /*
+ * insert into list sorted by end (second contact position):
+ * binary search the sorted list to find the insertion point
+ */
+ contactFeatureEnds.add(findFirstEnd(contactFeatureEnds, feature.end),
+ feature);
+
+ return true;
+ }
+
+ /**
* Adds one sequence feature to the store, and returns true, unless the
* feature is already contained in the store, in which case this method
* returns false. Containment is determined by SequenceFeature.equals()
return true;
}
- /**
- * Answers true if this store contains the given feature (testing by
- * SequenceFeature.equals), else false
- *
- * @param feature
- * @return
- */
- @Override
- public boolean contains(SequenceFeature feature)
+ private void addFeaturesForGroup(String group,
+ Collection<SequenceFeature> sfs, List<SequenceFeature> result)
{
- if (feature.isNonPositional())
+ if (sfs == null)
{
- return nonPositionalFeatures == null ? false
- : nonPositionalFeatures.contains(feature);
+ return;
}
-
- if (feature.isContactFeature())
+ for (SequenceFeature sf : sfs)
{
- return contactFeatureStarts != null
- && listContains(contactFeatureStarts, feature);
+ String featureGroup = sf.getFeatureGroup();
+ if (group == null && featureGroup == null
+ || group != null && group.equals(featureGroup))
+ {
+ result.add(sf);
+ }
}
-
- return features == null ? false : features.contains(feature);
}
/**
- * Answers the 'length' of the feature, counting 0 for non-positional features
- * and 1 for contact features
- *
- * @param feature
- * @return
+ * Adds one feature to the IntervalStore that can manage nested features
+ * (creating the IntervalStore if necessary)
*/
- protected static int getFeatureLength(SequenceFeature feature)
+ protected synchronized void addNestedFeature(SequenceFeature feature)
{
- if (feature.isNonPositional())
- {
- return 0;
- }
- if (feature.isContactFeature())
- {
- return 1;
- }
- return 1 + feature.getEnd() - feature.getBegin();
+ features.add(feature);
}
/**
}
/**
- * Adds one feature to the IntervalStore that can manage nested features
- * (creating the IntervalStore if necessary)
- */
- protected synchronized void addNestedFeature(SequenceFeature feature)
- {
- 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
- * ordered, and returns true. This method allows duplicate features to be
- * added, so test before calling to avoid this.
+ * Answers true if this store contains the given feature (testing by
+ * SequenceFeature.equals), else false
*
* @param feature
* @return
*/
- protected synchronized boolean addContactFeature(SequenceFeature feature)
+ @Override
+ public boolean contains(SequenceFeature feature)
{
- if (contactFeatureStarts == null)
+ if (feature.isNonPositional())
{
- contactFeatureStarts = new ArrayList<>();
- contactFeatureEnds = new ArrayList<>();
+ return nonPositionalFeatures == null ? false
+ : nonPositionalFeatures.contains(feature);
}
- /*
- * insert into list sorted by start (first contact position):
- * binary search the sorted list to find the insertion point
- */
- int insertPosition = BinarySearcher.findFirst(contactFeatureStarts,
- 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
- */
- insertPosition = BinarySearcher.findFirst(contactFeatureEnds,
- f -> f.getEnd() >= feature.getEnd());
- contactFeatureEnds.add(insertPosition, feature);
+ if (feature.isContactFeature())
+ {
+ return contactFeatureStarts != null
+ && listContains(contactFeatureStarts, feature);
+ }
- return true;
+ return features == null ? false : features.contains(feature);
}
/**
- * Answers true if the list contains the feature, else false. This method is
- * optimised for the condition that the list is sorted on feature start
- * position ascending, and will give unreliable results if this does not hold.
+ * Deletes the given feature from the store, returning true if it was found
+ * (and deleted), else false. This method makes no assumption that the feature
+ * is in the 'expected' place in the store, in case it has been modified since
+ * it was added.
*
- * @param features
- * @param feature
- * @return
+ * @param sf
*/
- protected static boolean listContains(List<SequenceFeature> features,
- SequenceFeature feature)
+
+ @Override
+ public synchronized boolean delete(SequenceFeature sf)
{
- if (features == null || feature == null)
- {
- return false;
- }
+ boolean removed = false;
/*
- * locate the first entry in the list which does not precede the feature
+ * try contact positions (and if found, delete
+ * from both lists of contact positions)
*/
- // int pos = binarySearch(features,
- // SearchCriterion.byFeature(feature, RangeComparator.BY_START_POSITION));
- int pos = BinarySearcher.findFirst(features,
- val -> val.getBegin() >= feature.getBegin());
- int len = features.size();
- while (pos < len)
+ if (!removed && contactFeatureStarts != null)
{
- SequenceFeature sf = features.get(pos);
- if (sf.getBegin() > feature.getBegin())
- {
- return false; // no match found
- }
- if (sf.equals(feature))
+ removed = contactFeatureStarts.remove(sf);
+ if (removed)
{
- return true;
- }
- pos++;
- }
- return false;
- }
-
- abstract protected void findContactFeatures(long from, long to,
- List<SequenceFeature> result);
-
- /**
- * Answers a list of all positional features stored, in no guaranteed order
- *
- * @return
- */
-
- @Override
- public List<SequenceFeature> getPositionalFeatures(
- List<SequenceFeature> result)
- {
-
- /*
- * add any contact features - from the list by start position
- */
- if (contactFeatureStarts != null)
- {
- result.addAll(contactFeatureStarts);
- }
-
- /*
- * add any nested features
- */
- if (features != null)
- {
- result.addAll(features);
- }
-
- return result;
- }
-
- /**
- * Answers a list of all contact features. If there are none, returns an
- * immutable empty list.
- *
- * @return
- */
-
- @Override
- public List<SequenceFeature> getContactFeatures(
- List<SequenceFeature> result)
- {
- if (contactFeatureStarts != null)
- {
- result.addAll(contactFeatureStarts);
- }
- return result;
- }
-
- /**
- * Answers a list of all non-positional features. If there are none, returns
- * an immutable empty list.
- *
- * @return
- */
-
- @Override
- public List<SequenceFeature> getNonPositionalFeatures(
- List<SequenceFeature> result)
- {
- if (nonPositionalFeatures != null)
- {
- result.addAll(nonPositionalFeatures);
- }
- return result;
- }
-
- /**
- * Deletes the given feature from the store, returning true if it was found
- * (and deleted), else false. This method makes no assumption that the feature
- * is in the 'expected' place in the store, in case it has been modified since
- * it was added.
- *
- * @param sf
- */
-
- @Override
- public synchronized boolean delete(SequenceFeature sf)
- {
- boolean removed = false;
-
- /*
- * try contact positions (and if found, delete
- * from both lists of contact positions)
- */
- if (!removed && contactFeatureStarts != null)
- {
- removed = contactFeatureStarts.remove(sf);
- if (removed)
- {
- contactFeatureEnds.remove(sf);
+ contactFeatureEnds.remove(sf);
}
}
return removed;
}
- /**
- * Rescan all features to recompute any cached values after an entry has been
- * deleted. This is expected to be an infrequent event, so performance here is
- * not critical.
- */
- protected synchronized void rescanAfterDelete()
- {
- positionalFeatureGroups.clear();
- nonPositionalFeatureGroups.clear();
- totalExtent = 0;
- positionalMinScore = Float.NaN;
- positionalMaxScore = Float.NaN;
- nonPositionalMinScore = Float.NaN;
- nonPositionalMaxScore = Float.NaN;
- /*
- * scan non-positional features for groups and scores
- */
- if (nonPositionalFeatures != null)
- {
- for (SequenceFeature sf : nonPositionalFeatures)
- {
- nonPositionalFeatureGroups.add(sf.getFeatureGroup());
- float score = sf.getScore();
- nonPositionalMinScore = min(nonPositionalMinScore, score);
- nonPositionalMaxScore = max(nonPositionalMaxScore, score);
- }
- }
+ abstract protected void findContactFeatures(long from, long to,
+ List<SequenceFeature> result);
- /*
- * scan positional features for groups, scores and extents
- */
+ abstract protected int findFirstBegin(List<SequenceFeature> list,
+ long pos);
- rescanPositional(contactFeatureStarts);
- rescanPositional(features);
- }
+ abstract protected int findFirstEnd(List<SequenceFeature> list, long pos);
- private void rescanPositional(Collection<SequenceFeature> sfs)
+ @Override
+ public List<SequenceFeature> findOverlappingFeatures(long start, long end)
{
- if (sfs == null)
- {
- return;
- }
- for (SequenceFeature sf : sfs)
- {
- positionalFeatureGroups.add(sf.getFeatureGroup());
- float score = sf.getScore();
- positionalMinScore = min(positionalMinScore, score);
- positionalMaxScore = max(positionalMaxScore, score);
- totalExtent += getFeatureLength(sf);
- }
+ return findOverlappingFeatures(start, end, null);
}
- /**
- * A helper method to return the minimum of two floats, where a non-NaN value
- * is treated as 'less than' a NaN value (unlike Math.min which does the
- * opposite)
- *
- * @param f1
- * @param f2
- */
- protected static float min(float f1, float f2)
+ @Override
+ public List<SequenceFeature> getContactFeatures()
{
- if (Float.isNaN(f1))
- {
- return Float.isNaN(f2) ? f1 : f2;
- }
- else
- {
- return Float.isNaN(f2) ? f1 : Math.min(f1, f2);
- }
+ return getContactFeatures(new ArrayList<>());
}
/**
- * A helper method to return the maximum of two floats, where a non-NaN value
- * is treated as 'greater than' a NaN value (unlike Math.max which does the
- * opposite)
+ * Answers a list of all contact features. If there are none, returns an
+ * immutable empty list.
*
- * @param f1
- * @param f2
+ * @return
*/
- protected static float max(float f1, float f2)
+
+ @Override
+ public List<SequenceFeature> getContactFeatures(
+ List<SequenceFeature> result)
{
- if (Float.isNaN(f1))
- {
- return Float.isNaN(f2) ? f1 : f2;
- }
- else
+ if (contactFeatureStarts != null)
{
- return Float.isNaN(f2) ? f1 : Math.max(f1, f2);
+ result.addAll(contactFeatureStarts);
}
+ return result;
}
-
/**
- * Answers true if this store has no features, else false
+ * Answers the number of positional (or non-positional) features stored.
+ * Contact features count as 1.
*
+ * @param positional
* @return
*/
@Override
- public boolean isEmpty()
+ public int getFeatureCount(boolean positional)
{
- boolean hasFeatures = (contactFeatureStarts != null
- && !contactFeatureStarts.isEmpty())
- || (nonPositionalFeatures != null
- && !nonPositionalFeatures.isEmpty())
- || features.size() > 0;
+ if (!positional)
+ {
+ return nonPositionalFeatures == null ? 0
+ : nonPositionalFeatures.size();
+ }
+
+ return (contactFeatureStarts == null ? 0 : contactFeatureStarts.size())
+ + features.size();
- return !hasFeatures;
}
/**
}
}
+ @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)
return result;
}
- private void addFeaturesForGroup(String group,
- Collection<SequenceFeature> sfs, List<SequenceFeature> result)
+ /**
+ * Answers the maximum score held for positional or non-positional features.
+ * This may be Float.NaN if there are no features, are none has a non-NaN
+ * score.
+ *
+ * @param positional
+ * @return
+ */
+
+ @Override
+ public float getMaximumScore(boolean positional)
{
- if (sfs == null)
- {
- return;
- }
- for (SequenceFeature sf : sfs)
+ return positional ? positionalMaxScore : nonPositionalMaxScore;
+ }
+
+ /**
+ * Answers the minimum score held for positional or non-positional features.
+ * This may be Float.NaN if there are no features, are none has a non-NaN
+ * score.
+ *
+ * @param positional
+ * @return
+ */
+
+ @Override
+ public float getMinimumScore(boolean positional)
+ {
+ return positional ? positionalMinScore : nonPositionalMinScore;
+ }
+
+ @Override
+ public List<SequenceFeature> getNonPositionalFeatures()
+ {
+ return getNonPositionalFeatures(new ArrayList<>());
+ }
+
+ /**
+ * Answers a list of all non-positional features. If there are none, returns
+ * an immutable empty list.
+ *
+ * @return
+ */
+
+ @Override
+ public List<SequenceFeature> getNonPositionalFeatures(
+ List<SequenceFeature> result)
+ {
+ if (nonPositionalFeatures != null)
{
- String featureGroup = sf.getFeatureGroup();
- if (group == null && featureGroup == null
- || group != null && group.equals(featureGroup))
- {
- result.add(sf);
- }
+ result.addAll(nonPositionalFeatures);
}
+ return result;
+ }
+
+ @Override
+ public List<SequenceFeature> getPositionalFeatures()
+ {
+ return getPositionalFeatures(new ArrayList<>());
}
/**
- * Answers the number of positional (or non-positional) features stored.
- * Contact features count as 1.
+ * Answers a list of all positional features stored, in no guaranteed order
*
- * @param positional
* @return
*/
@Override
- public int getFeatureCount(boolean positional)
+ public List<SequenceFeature> getPositionalFeatures(
+ List<SequenceFeature> result)
{
- if (!positional)
+
+ /*
+ * add any contact features - from the list by start position
+ */
+ if (contactFeatureStarts != null)
{
- return nonPositionalFeatures == null ? 0
- : nonPositionalFeatures.size();
+ result.addAll(contactFeatureStarts);
}
- return (contactFeatureStarts == null ? 0 : contactFeatureStarts.size())
- + features.size();
+ /*
+ * add any nested features
+ */
+ if (features != null)
+ {
+ result.addAll(features);
+ }
+ return result;
}
-
/**
* Answers the total length of positional features (or zero if there are
* none). Contact features contribute a value of 1 to the total.
}
/**
- * Answers the minimum score held for positional or non-positional features.
- * This may be Float.NaN if there are no features, are none has a non-NaN
- * score.
+ * Answers true if this store has no features, else false
*
- * @param positional
* @return
*/
@Override
- public float getMinimumScore(boolean positional)
+ public boolean isEmpty()
{
- return positional ? positionalMinScore : nonPositionalMinScore;
+ boolean hasFeatures = (contactFeatureStarts != null
+ && !contactFeatureStarts.isEmpty())
+ || (nonPositionalFeatures != null
+ && !nonPositionalFeatures.isEmpty())
+ || features.size() > 0;
+
+ return !hasFeatures;
}
/**
- * Answers the maximum score held for positional or non-positional features.
- * This may be Float.NaN if there are no features, are none has a non-NaN
- * score.
- *
- * @param positional
- * @return
+ * Rescan all features to recompute any cached values after an entry has been
+ * deleted. This is expected to be an infrequent event, so performance here is
+ * not critical.
*/
-
- @Override
- public float getMaximumScore(boolean positional)
+ protected synchronized void rescanAfterDelete()
{
- return positional ? positionalMaxScore : nonPositionalMaxScore;
+ positionalFeatureGroups.clear();
+ nonPositionalFeatureGroups.clear();
+ totalExtent = 0;
+ positionalMinScore = Float.NaN;
+ positionalMaxScore = Float.NaN;
+ nonPositionalMinScore = Float.NaN;
+ nonPositionalMaxScore = Float.NaN;
+ /*
+ * scan non-positional features for groups and scores
+ */
+ if (nonPositionalFeatures != null)
+ {
+ 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
+ */
+
+ 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();
+ positionalMinScore = min(positionalMinScore, score);
+ positionalMaxScore = max(positionalMaxScore, score);
+ totalExtent += getFeatureLength(sf);
+ }
+ }
/**
* Adds the shift amount to the start and end of all positional features whose
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<>());
- }
-
}