(feature);
+ return false;
}
- else
+
+ /*
+ * keep a record of feature groups
+ */
+ if (!feature.isNonPositional())
{
- nestedFeatures.add(feature);
+ positionalFeatureGroups.add(feature.getFeatureGroup());
}
- }
- /**
- * Add a feature to the list of non-nested features, maintaining the ordering
- * of the list. A check is made for whether the feature is nested in (properly
- * contained by) an existing feature. If there is no nesting, the feature is
- * added to the list and the method returns true. If nesting is found, the
- * feature is not added and the method returns false.
- *
- * Contact features are added at the position of their first contact point
- *
- * @param feature
- * @return
- */
- protected boolean addNonNestedFeature(SequenceFeature feature)
- {
- synchronized (nonNestedFeatures)
+ if (feature.isContactFeature())
{
- int insertPosition = binarySearchForAdd(nonNestedFeatures, feature);
-
- /*
- * fail if we detect feature enclosure - of the new feature by
- * the one preceding it, or of the next feature by the new one
- */
- if (insertPosition > 0)
+ if (feature.begin > lastContactStart)
{
- if (encloses(nonNestedFeatures.get(insertPosition - 1), feature))
- {
- return false;
- }
+ lastContactStart = feature.begin;
}
- if (insertPosition < nonNestedFeatures.size())
+ addContactFeature(feature);
+ }
+ else if (feature.isNonPositional())
+ {
+ addNonPositionalFeature(feature);
+ }
+ else
+ {
+ addPositionalFeature(feature);
+ if (feature.begin > lastStart)
{
- if (encloses(feature, nonNestedFeatures.get(insertPosition)))
- {
- return false;
- }
+ lastStart = feature.begin;
}
+ }
- /*
- * checks passed - add or append the feature
- */
- if (insertPosition == nonNestedFeatures.size())
+ /*
+ * record the total extent of positional features, to make
+ * getTotalFeatureLength possible; we count the length of a
+ * contact feature as 1
+ */
+ totalExtent += getFeatureLength(feature);
+
+ /*
+ * record the minimum and maximum score for positional
+ * and non-positional features
+ */
+ float score = feature.getScore();
+ if (!Float.isNaN(score))
+ {
+ if (feature.isNonPositional())
{
- nonNestedFeatures.add(feature);
+ nonPositionalMinScore = min(nonPositionalMinScore, score);
+ nonPositionalMaxScore = max(nonPositionalMaxScore, score);
}
else
{
- nonNestedFeatures.add(insertPosition, feature);
+ positionalMinScore = min(positionalMinScore, score);
+ positionalMaxScore = max(positionalMaxScore, score);
}
- return true;
}
+
+ return true;
}
- /**
- * Answers true if range1 properly encloses range2, else false
- *
- * @param range1
- * @param range2
- * @return
- */
- protected boolean encloses(ContiguousI range1, ContiguousI range2)
+ private void addFeaturesForGroup(String group,
+ Collection sfs, List result)
{
- int begin1 = range1.getBegin();
- int begin2 = range2.getBegin();
- int end1 = range1.getEnd();
- int end2 = range2.getEnd();
- if (begin1 == begin2 && end1 > end2)
+ if (sfs == null)
{
- return true;
+ return;
}
- if (begin1 < begin2 && end1 >= end2)
+ for (SequenceFeature sf : sfs)
{
- return true;
+ String featureGroup = sf.getFeatureGroup();
+ if (group == null && featureGroup == null
+ || group != null && group.equals(featureGroup))
+ {
+ result.add(sf);
+ }
}
- return false;
}
/**
- * Answers the index of the first element in the given list which follows or
- * matches the given feature in the sort order. If no such element, answers
- * the length of the list.
+ * Adds one feature to the IntervalStore that can manage nested features
+ * (creating the IntervalStore if necessary)
+ */
+ abstract protected void addPositionalFeature(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
+ * features. This method allows duplicate features, so test before calling to
+ * prevent this.
*
- * @param list
* @param feature
- *
- * @return
*/
- protected int binarySearchForAdd(List list, SequenceFeature feature)
+ protected boolean addNonPositionalFeature(SequenceFeature feature)
{
- // TODO binary search!
- int i = 0;
- while (i < list.size())
+ if (nonPositionalFeatures == null)
{
- if (startOrdering.compare(nonNestedFeatures.get(i), feature) >= 0)
- {
- break;
- }
- i++;
+ nonPositionalFeatures = new ArrayList<>();
}
- return i;
+
+ nonPositionalFeatures.add(feature);
+
+ nonPositionalFeatureGroups.add(feature.getFeatureGroup());
+
+ return true;
}
/**
- * 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
+ * Answers true if this store contains the given feature (testing by
+ * SequenceFeature.equals), else false
*
* @param feature
+ * @return
*/
- protected synchronized void addContactFeature(SequenceFeature feature)
+ @Override
+ public boolean contains(SequenceFeature feature)
{
- // TODO binary search for insertion points!
- if (contactFeatureStarts == null)
+ if (feature.isNonPositional())
{
- contactFeatureStarts = new ArrayList();
+ return nonPositionalFeatures == null ? false
+ : nonPositionalFeatures.contains(feature);
}
- if (contactFeatureEnds == null)
+
+ if (feature.isContactFeature())
{
- contactFeatureEnds = new ArrayList();
+ return contactFeatureStarts != null
+ && feature.begin <= lastContactStart
+ && listContains(contactFeatureStarts, feature);
}
- contactFeatureStarts.add(feature);
- Collections.sort(contactFeatureStarts, startOrdering);
- contactFeatureEnds.add(feature);
- Collections.sort(contactFeatureEnds, endOrdering);
+
+ return features == null || feature.begin > lastStart ? false
+ : containsFeature(feature);
}
+
+ abstract protected boolean containsFeature(SequenceFeature feature);
+
/**
- * Returns a (possibly empty) list of entries whose range overlaps the given
- * range. The returned list is not ordered. Contact features are included if
- * either of the contact points lies within the range.
+ * 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 start
- * start position of overlap range (inclusive)
- * @param end
- * end position of overlap range (inclusive)
- * @return
+ * @param sf
*/
- public List findOverlappingFeatures(long start, long end)
+
+ @Override
+ public synchronized boolean delete(SequenceFeature sf)
{
- List result = new ArrayList();
+ boolean removed = false;
- findNonNestedFeatures(start, end, result);
+ /*
+ * 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);
+ }
+ }
- findContactFeatures(start, end, result);
+ /*
+ * if not found, try non-positional features
+ */
+ if (!removed && nonPositionalFeatures != null)
+ {
+ removed = nonPositionalFeatures.remove(sf);
+ }
- if (nestedFeatures != null)
+ /*
+ * if not found, try nested features
+ */
+ if (!removed && features != null)
{
- result.addAll(nestedFeatures.findOverlaps(start, end));
+ removed = findAndRemoveNonContactFeature(sf);
}
- return result;
+ if (removed)
+ {
+ rescanAfterDelete();
+ }
+
+ return removed;
+ }
+
+ abstract protected boolean findAndRemoveNonContactFeature(SequenceFeature sf);
+
+ abstract protected void findContactFeatures(long from, long to,
+ List result);
+
+ abstract protected int findFirstBegin(List list,
+ long pos);
+
+ abstract protected int findFirstEnd(List list, long pos);
+
+ @Override
+ public List findOverlappingFeatures(long start, long end)
+ {
+ return findOverlappingFeatures(start, end, null);
+ }
+
+ @Override
+ public List getContactFeatures()
+ {
+ return getContactFeatures(new ArrayList<>());
}
/**
- * Adds contact features to the result list where either the second or the
- * first contact position lies within the target range.
+ * Answers a list of all contact features. If there are none, returns an
+ * immutable empty list.
*
- * @param from
- * @param to
- * @param result
+ * @return
*/
- protected void findContactFeatures(long from, long to,
+
+ @Override
+ public List getContactFeatures(
List result)
{
if (contactFeatureStarts != null)
{
- findContactStartFeatures(from, to, result);
+ result.addAll(contactFeatureStarts);
}
- if (contactFeatureEnds != null)
+ return result;
+ }
+
+ /**
+ * 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)
{
- findContactEndFeatures(from, to, result);
+ return nonPositionalFeatures == null ? 0
+ : nonPositionalFeatures.size();
}
+
+ return (contactFeatureStarts == null ? 0 : contactFeatureStarts.size())
+ + features.size();
+
}
/**
- * @param from
- * @param to
- * @param result
+ * Answers the set of distinct feature groups stored, possibly including null,
+ * as an unmodifiable view of the set. The parameter determines whether the
+ * groups for positional or for non-positional features are returned.
+ *
+ * @param positionalFeatures
+ * @return
*/
- protected void findContactEndFeatures(long from, long to,
- List result)
+
+ @Override
+ public Set getFeatureGroups(boolean positionalFeatures)
{
- // TODO binary search for startPosition
- for (int startPosition = 0; startPosition < contactFeatureEnds.size(); startPosition++)
+ if (positionalFeatures)
{
- SequenceFeature sf = contactFeatureEnds.get(startPosition);
- if (!sf.isContactFeature())
- {
- System.err.println("Error! non-contact feature type "
- + sf.getType() + " in contact features list");
- 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
- */
- continue;
- }
- int end = sf.getEnd();
- if (end >= from && end <= to)
- {
- result.add(sf);
- }
+ return Collections.unmodifiableSet(positionalFeatureGroups);
+ }
+ else
+ {
+ return nonPositionalFeatureGroups == null
+ ? Collections. emptySet()
+ : Collections.unmodifiableSet(nonPositionalFeatureGroups);
}
}
+ @Override
+ public Collection getFeatures()
+ {
+ return features;
+ }
+
/**
- * Returns the index of the first contact feature found whose end (second
- * contact position) is not before the given start position. If no such
- * feature is found, returns the length of the contact features list.
+ * Answers a list of all either positional or non-positional features whose
+ * feature group matches the given group (which may be null)
*
- * @param start
+ * @param positional
+ * @param group
* @return
*/
- protected int contactsBinarySearch(long start)
+
+ @Override
+ public List getFeaturesForGroup(boolean positional,
+ String group)
{
- // TODO binary search!!
- int i = 0;
- while (i < contactFeatureEnds.size())
+ List 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))
{
- if (contactFeatureEnds.get(i).getEnd() >= start)
- {
- break;
- }
- i++;
+ return result;
+ }
+
+ if (positional)
+ {
+ addFeaturesForGroup(group, contactFeatureStarts, result);
+ addFeaturesForGroup(group, features, result);
}
+ else
+ {
+ addFeaturesForGroup(group, nonPositionalFeatures, result);
+ }
+ return 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
+ */
- return i;
+ @Override
+ public float getMaximumScore(boolean positional)
+ {
+ return positional ? positionalMaxScore : nonPositionalMaxScore;
}
/**
- * Adds features to the result list that are at a single position which lies
- * within the target range. Non-positional features (start=end=0) and contact
- * features are excluded.
+ * 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 from
- * @param to
- * @param result
+ * @param positional
+ * @return
*/
- protected void findNonNestedFeatures(long from, long to,
+
+ @Override
+ public float getMinimumScore(boolean positional)
+ {
+ return positional ? positionalMinScore : nonPositionalMinScore;
+ }
+
+ @Override
+ public List 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 getNonPositionalFeatures(
List result)
{
- int startIndex = binarySearch(nonNestedFeatures, from);
- findNonNestedFeatures(startIndex, from, to, result);
+ if (nonPositionalFeatures != null)
+ {
+ result.addAll(nonPositionalFeatures);
+ }
+ return result;
+ }
+
+ @Override
+ public List getPositionalFeatures()
+ {
+ return getPositionalFeatures(new ArrayList<>());
}
/**
- * Scans the list of non-nested features, starting from startIndex, to find
- * those that overlap the from-to range, and adds them to the result list.
- * Returns the index of the first feature whose start position is after the
- * target range (or the length of the whole list if none such feature exists).
+ * Answers a list of all positional features stored, in no guaranteed order
*
- * @param startIndex
- * @param from
- * @param to
- * @param result
* @return
*/
- protected int findNonNestedFeatures(final int startIndex, long from,
- long to,
+
+ @Override
+ public List getPositionalFeatures(
List result)
{
- int i = startIndex;
- while (i < nonNestedFeatures.size())
+
+ /*
+ * add any contact features - from the list by start position
+ */
+ if (contactFeatureStarts != null)
{
- SequenceFeature sf = nonNestedFeatures.get(i);
- if (sf.getBegin() > to)
- {
- break;
- }
- int start = sf.getBegin();
- int end = sf.getEnd();
- if (sf.isContactFeature())
- {
- end = start;
- }
- if (start <= to && end >= from)
- {
- result.add(sf);
- }
- i++;
+ result.addAll(contactFeatureStarts);
+ }
+
+ /*
+ * add any nested features
+ */
+ if (features != null)
+ {
+ result.addAll(features);
}
- return i;
+
+ return result;
}
/**
- * Performs a binary search of the (sorted) list to find the index of the
- * first entry whose end position is not less than the target position (i.e.
- * skip all features that properly precede the given position)
+ * Answers the total length of positional features (or zero if there are
+ * none). Contact features contribute a value of 1 to the total.
*
- * @param features
- * @param target
* @return
*/
- protected int binarySearch(List features, long target)
+
+ @Override
+ public int getTotalFeatureLength()
{
- int width = features.size() / 2;
- int lastpos = width;
- while (width > 0)
+ return totalExtent;
+ }
+
+ /**
+ * 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.size() > 0;
+
+ return !hasFeatures;
+ }
+
+ /**
+ * 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)
{
- int end = features.get(lastpos).getEnd();
- width = width / 2;
- if (end > target)
+ List list = nonPositionalFeatures;
+ for (int i = 0, n = list.size(); i < n; i++)
{
- lastpos -= width;
- }
- else
- {
- lastpos += width;
+ SequenceFeature sf = list.get(i);
+ nonPositionalFeatureGroups.add(sf.getFeatureGroup());
+ float score = sf.getScore();
+ nonPositionalMinScore = min(nonPositionalMinScore, score);
+ nonPositionalMaxScore = max(nonPositionalMaxScore, score);
}
}
- // todo correct binary search
- return lastpos > 1 ? lastpos - 2 : 0;
- // return lastpos;
+
+ /*
+ * scan positional features for groups, scores and extents
+ */
+
+ rescanPositional(contactFeatureStarts);
+ rescanPositional(features);
+ }
+
+ private void rescanPositional(Collection 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 contact features whose start position lies in the from-to range to the
- * result list
+ * 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 from
- * @param to
- * @param result
+ * @param fromPosition
+ * @param shiftBy
+ * @return
*/
- protected void findContactStartFeatures(long from, long to,
- List result)
+
+ @Override
+ public synchronized boolean shiftFeatures(int fromPosition, int shiftBy)
{
- // TODO binary search for startPosition
- for (int startPosition = 0; startPosition < contactFeatureStarts.size(); startPosition++)
+ /*
+ * Because begin and end are final fields (to ensure the data store's
+ * integrity), we have to delete each feature and re-add it as amended.
+ * (Although a simple shift of all values would preserve data integrity!)
+ */
+ boolean modified = false;
+ List list = getPositionalFeatures();
+ for (int i = 0, n = list.size(); i < n; i++)
{
- SequenceFeature sf = contactFeatureStarts.get(startPosition);
- if (!sf.isContactFeature())
+ SequenceFeature sf = list.get(i);
+ if (sf.getBegin() >= fromPosition)
{
- System.err.println("Error! non-contact feature type "
- + sf.getType() + " in contact features list");
- continue;
- }
- int begin = sf.getBegin();
- if (begin >= from && begin <= to)
- {
- result.add(sf);
+ 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);
}
}
+ return modified;
}
+
}