(feature);
- return true;
+ return Float.isNaN(f2) ? f1 : Math.min(f1, f2);
}
- return nestedFeatures.add(feature, false);
}
/**
- * 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
+ * Constructor that defaults to using NCList IntervalStore
*/
- protected boolean addNonNestedFeature(SequenceFeature feature)
+ public FeatureStore()
{
- synchronized (nonNestedFeatures)
- {
- 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 (encloses(nonNestedFeatures.get(insertPosition - 1), feature))
- {
- return false;
- }
- }
- if (insertPosition < nonNestedFeatures.size())
- {
- if (encloses(feature, nonNestedFeatures.get(insertPosition)))
- {
- return false;
- }
- }
-
- /*
- * checks passed - add or append the feature
- */
- if (insertPosition == nonNestedFeatures.size())
- {
- nonNestedFeatures.add(feature);
- }
- else
- {
- nonNestedFeatures.add(insertPosition, feature);
- }
- return true;
- }
+ this(IntervalStoreType.INTERVAL_STORE_NCLIST);
}
/**
- * Answers true if range1 properly encloses range2, else false
- *
- * @param range1
- * @param range2
- * @return
+ * Constructor that allows an alternative IntervalStore implementation to be
+ * chosen
*/
- protected boolean encloses(ContiguousI range1, ContiguousI range2)
+ public FeatureStore(IntervalStoreType intervalStoreType)
{
- int begin1 = range1.getBegin();
- int begin2 = range2.getBegin();
- int end1 = range1.getEnd();
- int end2 = range2.getEnd();
- if (begin1 == begin2 && end1 > end2)
- {
- return true;
- }
- if (begin1 < begin2 && end1 >= end2)
- {
- return true;
- }
- return false;
+ features = getIntervalStore(intervalStoreType);
+ positionalFeatureGroups = new HashSet<>();
+ nonPositionalFeatureGroups = new HashSet<>();
+ positionalMinScore = Float.NaN;
+ positionalMaxScore = Float.NaN;
+ nonPositionalMinScore = Float.NaN;
+ nonPositionalMaxScore = Float.NaN;
+
+ // only construct nonPositionalFeatures or contactFeatures if needed
}
/**
- * 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.
- *
- * @param list
- * @param feature
+ * Returns a new instance of IntervalStoreI of implementation as selected by
+ * the type parameter
*
+ * @param type
* @return
*/
- protected int binarySearchForAdd(List list, SequenceFeature feature)
+ private IntervalStoreI getIntervalStore(
+ IntervalStoreType type)
{
- // TODO binary search!
- int i = 0;
- while (i < list.size())
+ switch (type)
{
- if (startOrdering.compare(nonNestedFeatures.get(i), feature) >= 0)
- {
- break;
- }
- i++;
+ 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<>();
}
- return i;
}
/**
* 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. If the contact feature lists already contain the
- * given feature (by test for equality), does not add it and returns false.
+ * ordered, and returns true. This method allows duplicate features to be
+ * added, so test before calling to avoid this.
*
* @param feature
* @return
@@ -266,422 +267,740 @@ public class FeatureStore
{
if (contactFeatureStarts == null)
{
- contactFeatureStarts = new ArrayList();
- }
- if (contactFeatureEnds == null)
- {
- contactFeatureEnds = new ArrayList();
- }
-
- // TODO binary search for insertion points!
- if (contactFeatureStarts.contains(feature))
- {
- return false;
+ contactFeatureStarts = new ArrayList<>();
+ contactFeatureEnds = new ArrayList<>();
}
- contactFeatureStarts.add(feature);
- Collections.sort(contactFeatureStarts, startOrdering);
- contactFeatureEnds.add(feature);
- Collections.sort(contactFeatureEnds, endOrdering);
+ /*
+ * insert into list sorted by start (first contact position):
+ * binary search the sorted list to find the insertion point
+ */
+ 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
+ */
+ contactFeatureEnds.add(findFirstEnd(contactFeatureEnds, feature.end),
+ feature);
return true;
}
/**
- * 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.
+ * 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()
+ * comparison.
*
- * @param start
- * start position of overlap range (inclusive)
- * @param end
- * end position of overlap range (inclusive)
- * @return
+ * @param feature
*/
- public List findOverlappingFeatures(long start, long end)
+ public boolean addFeature(SequenceFeature feature)
{
- List result = new ArrayList();
+ 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;
+ }
- findNonNestedFeatures(start, end, result);
+ addNonPositionalFeature(feature);
+ }
+ else
+ {
+ if (!features.add(feature, false))
+ {
+ return false;
+ }
+ positionalFeatureGroups.add(feature.getFeatureGroup());
+ if (feature.begin > maxStart)
+ {
+ maxStart = feature.begin;
+ }
+ }
- findContactFeatures(start, end, result);
+ /*
+ * record the total extent of positional features, to make
+ * getTotalFeatureLength possible; we count the length of a
+ * contact feature as 1
+ */
+ totalExtent += getFeatureLength(feature);
- if (nestedFeatures != null)
+ /*
+ * record the minimum and maximum score for positional
+ * and non-positional features
+ */
+ float score = feature.getScore();
+ if (!Float.isNaN(score))
{
- result.addAll(nestedFeatures.findOverlaps(start, end));
+ if (feature.isNonPositional())
+ {
+ nonPositionalMinScore = min(nonPositionalMinScore, score);
+ nonPositionalMaxScore = max(nonPositionalMaxScore, score);
+ }
+ else
+ {
+ positionalMinScore = min(positionalMinScore, score);
+ positionalMaxScore = max(positionalMaxScore, score);
+ }
}
- return result;
+ return true;
}
/**
- * Adds contact features to the result list where either the second or the
- * first contact position lies within the target range.
+ * A helper method that adds to the result list any features from the
+ * collection provided whose feature group matches the specified group
*
- * @param from
- * @param to
+ * @param group
+ * @param sfs
* @param result
*/
- protected void findContactFeatures(long from, long to,
- List result)
+ private void addFeaturesForGroup(String group,
+ Collection sfs, List result)
{
- if (contactFeatureStarts != null)
+ if (sfs == null)
{
- findContactStartFeatures(from, to, result);
+ return;
}
- if (contactFeatureEnds != null)
+ for (SequenceFeature sf : sfs)
{
- findContactEndFeatures(from, to, result);
+ String featureGroup = sf.getFeatureGroup();
+ if (group == null && featureGroup == null
+ || group != null && group.equals(featureGroup))
+ {
+ result.add(sf);
+ }
}
}
/**
- * @param from
- * @param to
- * @param result
+ * 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 feature
*/
- protected void findContactEndFeatures(long from, long to,
- List result)
+ protected boolean addNonPositionalFeature(SequenceFeature feature)
{
- // TODO binary search for startPosition
- for (int startPosition = 0; startPosition < contactFeatureEnds.size(); startPosition++)
+ if (nonPositionalFeatures == null)
{
- 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);
- }
+ nonPositionalFeatures = new ArrayList<>();
}
+
+ nonPositionalFeatures.add(feature);
+
+ nonPositionalFeatureGroups.add(feature.getFeatureGroup());
+
+ return true;
}
/**
- * 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 true if this store contains the given feature (testing by
+ * SequenceFeature.equals), else false
*
- * @param start
+ * @param feature
* @return
*/
- protected int contactsBinarySearch(long start)
+ public boolean contains(SequenceFeature feature)
{
- // TODO binary search!!
- int i = 0;
- while (i < contactFeatureEnds.size())
+ if (feature.isNonPositional())
{
- if (contactFeatureEnds.get(i).getEnd() >= start)
- {
- break;
- }
- i++;
+ return containsNonPositionalFeature(feature);
+ }
+
+ if (feature.isContactFeature())
+ {
+ return containsContactFeature(feature);
}
- return i;
+ return containsPositionalFeature(feature);
}
- /**
- * 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.
- *
- * @param from
- * @param to
- * @param result
- */
- protected void findNonNestedFeatures(long from, long to,
- List result)
+ private boolean containsPositionalFeature(SequenceFeature feature)
{
- int startIndex = binarySearch(nonNestedFeatures, from);
- findNonNestedFeatures(startIndex, from, to, result);
+ return features == null || feature.begin > maxStart ? false
+ : features.contains(feature);
}
/**
- * 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 true if this store already contains a contact feature equal to the
+ * given feature (by {@code SequenceFeature.equals()} test), else false
*
- * @param startIndex
- * @param from
- * @param to
- * @param result
+ * @param feature
* @return
*/
- protected int findNonNestedFeatures(final int startIndex, long from,
- long to,
- List result)
+ private boolean containsContactFeature(SequenceFeature feature)
{
- int i = startIndex;
- while (i < nonNestedFeatures.size())
- {
- SequenceFeature sf = nonNestedFeatures.get(i);
- if (sf.getBegin() > to)
- {
- break;
- }
- int start = sf.getBegin();
- int end = sf.getEnd();
- if (start <= to && end >= from)
- {
- result.add(sf);
- }
- i++;
- }
- return i;
+ return contactFeatureStarts != null && feature.begin <= maxContactStart
+ && listContains(contactFeatureStarts, feature);
}
/**
- * 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 true if this store already contains a non-positional feature equal
+ * to the given feature (by {@code SequenceFeature.equals()} test), else false
*
- * @param features
- * @param target
+ * @param feature
* @return
*/
- protected int binarySearch(List features, long target)
+ private boolean containsNonPositionalFeature(SequenceFeature feature)
{
- int width = features.size() / 2;
- int lastpos = width;
- while (width > 0)
- {
- int end = features.get(lastpos).getEnd();
- width = width / 2;
- if (end > target)
- {
- lastpos -= width;
- }
- else
- {
- lastpos += width;
- }
- }
- // todo correct binary search
- return lastpos > 1 ? lastpos - 2 : 0;
- // return lastpos;
+ return nonPositionalFeatures == null ? false
+ : nonPositionalFeatures.contains(feature);
}
/**
- * Adds contact features whose start position lies in the from-to range to the
- * result list
+ * 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 from
- * @param to
- * @param result
+ * @param sf
*/
- protected void findContactStartFeatures(long from, long to,
- List result)
+ public synchronized boolean delete(SequenceFeature sf)
{
- // TODO binary search for startPosition
- for (int startPosition = 0; startPosition < contactFeatureStarts.size(); startPosition++)
+ boolean removed = false;
+
+ /*
+ * try contact positions (and if found, delete
+ * from both lists of contact positions)
+ */
+ if (!removed && contactFeatureStarts != null)
{
- SequenceFeature sf = contactFeatureStarts.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)
+ removed = contactFeatureStarts.remove(sf);
+ if (removed)
{
- result.add(sf);
+ contactFeatureEnds.remove(sf);
}
}
+
+ /*
+ * if not found, try non-positional features
+ */
+ if (!removed && nonPositionalFeatures != null)
+ {
+ removed = nonPositionalFeatures.remove(sf);
+ }
+
+ /*
+ * if not found, try nested features
+ */
+ if (!removed && features != null)
+ {
+ removed = features.remove(sf);
+ }
+
+ if (removed)
+ {
+ rescanAfterDelete();
+ }
+
+ return removed;
+ }
+
+ public List findFeatures(long start, long end)
+ {
+ return findFeatures(start, end, null);
}
/**
- * Answers a list of all features stored (including any non-positional
- * features), in no guaranteed order
+ * 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 getFeatures()
+ public List findFeatures(long start, long end,
+ List result)
{
- /*
- * add non-nested features (may be all features for many cases)
- */
- List result = new ArrayList();
- result.addAll(nonNestedFeatures);
+ if (result == null)
+ {
+ result = new ArrayList<>();
+ }
- /*
- * add any contact features - from the list by start position
- */
+ findContactFeatures(start, end, result);
+ features.findOverlaps(start, end, result);
+
+ return result;
+ }
+
+ /**
+ * Returns a (possibly empty) list of stored contact features
+ *
+ * @return
+ */
+ public List getContactFeatures()
+ {
+ List result = new ArrayList<>();
+ getContactFeatures(result);
+ return result;
+ }
+
+ /**
+ * Adds any stored contact features to the result list
+ *
+ * @return
+ */
+ public void getContactFeatures(List result)
+ {
if (contactFeatureStarts != null)
{
result.addAll(contactFeatureStarts);
}
+ }
- /*
- * add any non-positional features
- */
- if (nonPositionalFeatures != null)
+ /**
+ * Answers the number of positional (or non-positional) features stored.
+ * Contact features count as 1.
+ *
+ * @param positional
+ * @return
+ */
+ public int getFeatureCount(boolean positional)
+ {
+ if (!positional)
{
- result.addAll(nonPositionalFeatures);
+ return nonPositionalFeatures == null ? 0
+ : nonPositionalFeatures.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;
+ }
+
+ /**
+ * 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
+ */
+ public Set getFeatureGroups(boolean positionalFeatures)
+ {
+ if (positionalFeatures)
+ {
+ return Collections.unmodifiableSet(positionalFeatureGroups);
}
+ else
+ {
+ return nonPositionalFeatureGroups == null
+ ? Collections. emptySet()
+ : Collections.unmodifiableSet(nonPositionalFeatureGroups);
+ }
+ }
+
+ /**
+ * 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 getFeaturesForGroup(boolean positional,
+ String group)
+ {
+ List result = new ArrayList<>();
/*
- * add any nested features
+ * if we know features don't include the target group, no need
+ * to inspect them for matches
*/
- if (nestedFeatures != null)
+ if (positional && !positionalFeatureGroups.contains(group)
+ || !positional && !nonPositionalFeatureGroups.contains(group))
{
- result.addAll(nestedFeatures.getEntries());
+ return result;
}
+ if (positional)
+ {
+ addFeaturesForGroup(group, contactFeatureStarts, result);
+ addFeaturesForGroup(group, features, result);
+ }
+ else
+ {
+ addFeaturesForGroup(group, nonPositionalFeatures, result);
+ }
return result;
}
/**
- * Answers a list of all contact features. If there are none, returns an
- * immutable empty list.
+ * 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
*/
- public List getContactFeatures()
+ public float getMaximumScore(boolean positional)
{
- if (contactFeatureStarts == null)
- {
- return Collections.emptyList();
- }
- return new ArrayList(contactFeatureStarts);
+ return positional ? positionalMaxScore : nonPositionalMaxScore;
}
/**
- * Answers a list of all non-positional features. If there are none, returns
- * an immutable empty list.
+ * 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
+ */
+ public float getMinimumScore(boolean positional)
+ {
+ return positional ? positionalMinScore : nonPositionalMinScore;
+ }
+
+ /**
+ * Answers a (possibly empty) list of all non-positional features
*
* @return
*/
public List getNonPositionalFeatures()
{
- if (nonPositionalFeatures == null)
+ List result = new ArrayList<>();
+ getNonPositionalFeatures(result);
+ return result;
+ }
+
+ /**
+ * Adds any stored non-positional features to the result list
+ *
+ * @return
+ */
+ public void getNonPositionalFeatures(List result)
+ {
+ if (nonPositionalFeatures != null)
{
- return Collections.emptyList();
+ result.addAll(nonPositionalFeatures);
}
- return new ArrayList(nonPositionalFeatures);
}
/**
- * 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.
+ * Returns a (possibly empty) list of all positional features stored
*
- * @param sf
+ * @return
*/
- public synchronized boolean delete(SequenceFeature sf)
+ public List getPositionalFeatures()
{
- /*
- * try the non-nested positional features first
- */
- boolean removed = nonNestedFeatures.remove(sf);
+ List result = new ArrayList<>();
+ getPositionalFeatures(result);
+
+ return result;
+ }
+ /**
+ * Adds all positional features stored to the result list, in no guaranteed
+ * order, and with no check for duplicates
+ */
+ public void getPositionalFeatures(List result)
+ {
/*
- * if not found, try contact positions (and if found, delete
- * from both lists of contact positions)
+ * add any contact features - from the list by start position
*/
- if (!removed && contactFeatureStarts != null)
+ if (contactFeatureStarts != null)
{
- removed = contactFeatureStarts.remove(sf);
- if (removed)
- {
- contactFeatureEnds.remove(sf);
- }
+ result.addAll(contactFeatureStarts);
}
/*
- * if not found, try non-positional features
+ * add any nested features
*/
- if (!removed && nonPositionalFeatures != null)
+ if (features != null)
{
- removed = nonPositionalFeatures.remove(sf);
+ result.addAll(features);
}
+ }
+
+ /**
+ * Answers the total length of positional features (or zero if there are
+ * none). Contact features contribute a value of 1 to the total.
+ *
+ * @return
+ */
+ public int getTotalFeatureLength()
+ {
+ return totalExtent;
+ }
+ /**
+ * Answers true if this store has no features, else false
+ *
+ * @return
+ */
+ public boolean isEmpty()
+ {
+ boolean hasFeatures = (contactFeatureStarts != null
+ && !contactFeatureStarts.isEmpty())
+ || (nonPositionalFeatures != null
+ && !nonPositionalFeatures.isEmpty())
+ || (features != null && 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;
/*
- * if not found, try nested features
+ * scan non-positional features for groups and scores
*/
- if (!removed && nestedFeatures != null)
- {
- removed = nestedFeatures.delete(sf);
- }
-
- if (removed)
+ if (nonPositionalFeatures != null)
{
- /*
- * if this was the only feature for its feature group,
- * remove the group from the stored set
- */
- String featureGroup = sf.getFeatureGroup();
- if (!findFeatureGroup(featureGroup))
+ for (int i = 0, n = nonPositionalFeatures.size(); i < n; i++)
{
- featureGroups.remove(featureGroup);
+ SequenceFeature sf = nonPositionalFeatures.get(i);
+ nonPositionalFeatureGroups.add(sf.getFeatureGroup());
+ float score = sf.getScore();
+ nonPositionalMinScore = min(nonPositionalMinScore, score);
+ nonPositionalMaxScore = max(nonPositionalMaxScore, score);
}
}
- return removed;
+ rescanPositional(contactFeatureStarts);
+ rescanPositional(features);
}
/**
- * Scans all features to check whether the given feature group is found, and
- * returns true if found, else false
+ * Scans the given features and updates cached feature groups, minimum and
+ * maximum feature score, and total feature extent (length) for positional
+ * features
*
- * @param featureGroup
+ * @param sfs
+ */
+ 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 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 fromPosition
+ * @param shiftBy
* @return
*/
- protected boolean findFeatureGroup(String featureGroup)
+ public synchronized boolean shiftFeatures(int fromPosition, int shiftBy)
{
- for (SequenceFeature sf : getFeatures())
+ /*
+ * 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++)
{
- String group = sf.getFeatureGroup();
- if (group == featureGroup
- || (group != null && group.equals(featureGroup)))
+ SequenceFeature sf = list.get(i);
+ if (sf.getBegin() >= fromPosition)
{
- return true;
+ 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 false;
+ return modified;
}
/**
- * Answers true if this store has no features, else false
+ * 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
*/
- public boolean isEmpty()
+ protected int findFirstEnd(List list, long pos)
{
- boolean hasFeatures = !nonNestedFeatures.isEmpty()
- || (contactFeatureStarts != null && !contactFeatureStarts
- .isEmpty())
- || (nonPositionalFeatures != null && !nonPositionalFeatures
- .isEmpty())
- || (nestedFeatures != null && nestedFeatures.size() > 0);
+ return BinarySearcher.findFirst(list, false, Compare.GE, (int) pos);
+ }
- return !hasFeatures;
+ /**
+ * 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 result)
+ {
+ if (contactFeatureStarts != null)
+ {
+ findContactStartOverlaps(from, to, result);
+ findContactEndOverlaps(from, to, result);
+ }
}
/**
- * Answers the set of distinct feature groups stored, possibly including null,
- * as an unmodifiable view of the set.
+ * 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
*
- * @return
+ * @param from
+ * @param to
+ * @param result
+ */
+ private void findContactEndOverlaps(long from, long to,
+ List 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
*/
- public Set getFeatureGroups()
+ private void findContactStartOverlaps(long from, long to,
+ List result)
{
- return Collections.unmodifiableSet(featureGroups);
+ 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++;
+ }
}
+
}