1 package jalview.datamodel.features;
3 import jalview.datamodel.SequenceFeature;
5 import java.util.ArrayList;
6 import java.util.Collections;
7 import java.util.Comparator;
8 import java.util.HashSet;
13 * A data store for a set of sequence features that supports efficient lookup of
14 * features overlapping a given range. Intended for (but not limited to) storage
15 * of features for one sequence and feature type.
20 public class FeatureStore
22 Comparator<ContiguousI> startOrdering = new RangeComparator(true);
24 Comparator<ContiguousI> endOrdering = new RangeComparator(false);
27 * Non-positional features have no (zero) start/end position.
28 * Kept as a separate list in case this criterion changes in future.
30 List<SequenceFeature> nonPositionalFeatures;
33 * An ordered list of features, with the promise that no feature in the list
34 * properly contains any other. This constraint allows bounded linear search
35 * of the list for features overlapping a region.
36 * Contact features are not included in this list.
38 List<SequenceFeature> nonNestedFeatures;
41 * contact features ordered by first contact position
43 List<SequenceFeature> contactFeatureStarts;
46 * contact features ordered by second contact position
48 List<SequenceFeature> contactFeatureEnds;
51 * Nested Containment List is used to hold any features that are nested
52 * within (properly contained by) any other feature. This is a recursive tree
53 * which supports depth-first scan for features overlapping a range.
54 * It is used here as a 'catch-all' fallback for features that cannot be put
55 * into a simple ordered list without invalidating the search methods.
57 NCList<SequenceFeature> nestedFeatures;
60 * Feature groups represented in the stored features
61 * (possibly including null)
63 Set<String> featureGroups;
70 nonNestedFeatures = new ArrayList<SequenceFeature>();
71 featureGroups = new HashSet<String>();
73 // we only construct nonPositionalFeatures, contactFeatures
74 // or the NCList if we need to
78 * Adds one sequence feature to the store, and returns true, unless the
79 * feature is already contained in the store, in which case this method
80 * returns false. Containment is determined by SequenceFeature.equals()
85 public boolean addFeature(SequenceFeature feature)
87 featureGroups.add(feature.getFeatureGroup());
89 boolean added = false;
91 if (feature.isContactFeature())
93 added = addContactFeature(feature);
95 else if (feature.isNonPositional())
97 added = addNonPositionalFeature(feature);
101 if (!nonNestedFeatures.contains(feature))
103 added = addNonNestedFeature(feature);
107 * detected a nested feature - put it in the NCList structure
109 added = addNestedFeature(feature);
118 * Adds the feature to the list of non-positional features (with lazy
119 * instantiation of the list if it is null), and returns true. If the
120 * non-positional features already include the new feature (by equality test),
121 * then it is not added, and this method returns false.
125 protected boolean addNonPositionalFeature(SequenceFeature feature)
127 if (nonPositionalFeatures == null)
129 nonPositionalFeatures = new ArrayList<SequenceFeature>();
131 if (nonPositionalFeatures.contains(feature))
135 nonPositionalFeatures.add(feature);
140 * Adds one feature to the NCList that can manage nested features (creating
141 * the NCList if necessary), and returns true. If the feature is already
142 * stored in the NCList (by equality test), then it is not added, and this
143 * method returns false.
145 protected synchronized boolean addNestedFeature(SequenceFeature feature)
147 if (nestedFeatures == null)
149 nestedFeatures = new NCList<SequenceFeature>(feature);
152 return nestedFeatures.add(feature, false);
156 * Add a feature to the list of non-nested features, maintaining the ordering
157 * of the list. A check is made for whether the feature is nested in (properly
158 * contained by) an existing feature. If there is no nesting, the feature is
159 * added to the list and the method returns true. If nesting is found, the
160 * feature is not added and the method returns false.
162 * Contact features are added at the position of their first contact point
167 protected boolean addNonNestedFeature(SequenceFeature feature)
169 synchronized (nonNestedFeatures)
171 int insertPosition = binarySearchForAdd(nonNestedFeatures, feature);
174 * fail if we detect feature enclosure - of the new feature by
175 * the one preceding it, or of the next feature by the new one
177 if (insertPosition > 0)
179 if (encloses(nonNestedFeatures.get(insertPosition - 1), feature))
184 if (insertPosition < nonNestedFeatures.size())
186 if (encloses(feature, nonNestedFeatures.get(insertPosition)))
193 * checks passed - add or append the feature
195 if (insertPosition == nonNestedFeatures.size())
197 nonNestedFeatures.add(feature);
201 nonNestedFeatures.add(insertPosition, feature);
208 * Answers true if range1 properly encloses range2, else false
214 protected boolean encloses(ContiguousI range1, ContiguousI range2)
216 int begin1 = range1.getBegin();
217 int begin2 = range2.getBegin();
218 int end1 = range1.getEnd();
219 int end2 = range2.getEnd();
220 if (begin1 == begin2 && end1 > end2)
224 if (begin1 < begin2 && end1 >= end2)
232 * Answers the index of the first element in the given list which follows or
233 * matches the given feature in the sort order. If no such element, answers
234 * the length of the list.
241 protected int binarySearchForAdd(List<SequenceFeature> list, SequenceFeature feature)
243 // TODO binary search!
245 while (i < list.size())
247 if (startOrdering.compare(nonNestedFeatures.get(i), feature) >= 0)
257 * Add a contact feature to the lists that hold them ordered by start (first
258 * contact) and by end (second contact) position, ensuring the lists remain
259 * ordered, and returns true. If the contact feature lists already contain the
260 * given feature (by test for equality), does not add it and returns false.
265 protected synchronized boolean addContactFeature(SequenceFeature feature)
267 if (contactFeatureStarts == null)
269 contactFeatureStarts = new ArrayList<SequenceFeature>();
271 if (contactFeatureEnds == null)
273 contactFeatureEnds = new ArrayList<SequenceFeature>();
276 // TODO binary search for insertion points!
277 if (contactFeatureStarts.contains(feature))
282 contactFeatureStarts.add(feature);
283 Collections.sort(contactFeatureStarts, startOrdering);
284 contactFeatureEnds.add(feature);
285 Collections.sort(contactFeatureEnds, endOrdering);
291 * Returns a (possibly empty) list of entries whose range overlaps the given
292 * range. The returned list is not ordered. Contact features are included if
293 * either of the contact points lies within the range.
296 * start position of overlap range (inclusive)
298 * end position of overlap range (inclusive)
301 public List<SequenceFeature> findOverlappingFeatures(long start, long end)
303 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
305 findNonNestedFeatures(start, end, result);
307 findContactFeatures(start, end, result);
309 if (nestedFeatures != null)
311 result.addAll(nestedFeatures.findOverlaps(start, end));
318 * Adds contact features to the result list where either the second or the
319 * first contact position lies within the target range.
325 protected void findContactFeatures(long from, long to,
326 List<SequenceFeature> result)
328 if (contactFeatureStarts != null)
330 findContactStartFeatures(from, to, result);
332 if (contactFeatureEnds != null)
334 findContactEndFeatures(from, to, result);
343 protected void findContactEndFeatures(long from, long to,
344 List<SequenceFeature> result)
346 // TODO binary search for startPosition
347 for (int startPosition = 0; startPosition < contactFeatureEnds.size(); startPosition++)
349 SequenceFeature sf = contactFeatureEnds.get(startPosition);
350 if (!sf.isContactFeature())
352 System.err.println("Error! non-contact feature type "
353 + sf.getType() + " in contact features list");
356 int begin = sf.getBegin();
357 if (begin >= from && begin <= to)
360 * this feature's first contact position lies in the search range
361 * so we don't include it in results a second time
365 int end = sf.getEnd();
366 if (end >= from && end <= to)
374 * Returns the index of the first contact feature found whose end (second
375 * contact position) is not before the given start position. If no such
376 * feature is found, returns the length of the contact features list.
381 protected int contactsBinarySearch(long start)
383 // TODO binary search!!
385 while (i < contactFeatureEnds.size())
387 if (contactFeatureEnds.get(i).getEnd() >= start)
398 * Adds features to the result list that are at a single position which lies
399 * within the target range. Non-positional features (start=end=0) and contact
400 * features are excluded.
406 protected void findNonNestedFeatures(long from, long to,
407 List<SequenceFeature> result)
409 int startIndex = binarySearch(nonNestedFeatures, from);
410 findNonNestedFeatures(startIndex, from, to, result);
414 * Scans the list of non-nested features, starting from startIndex, to find
415 * those that overlap the from-to range, and adds them to the result list.
416 * Returns the index of the first feature whose start position is after the
417 * target range (or the length of the whole list if none such feature exists).
425 protected int findNonNestedFeatures(final int startIndex, long from,
427 List<SequenceFeature> result)
430 while (i < nonNestedFeatures.size())
432 SequenceFeature sf = nonNestedFeatures.get(i);
433 if (sf.getBegin() > to)
437 int start = sf.getBegin();
438 int end = sf.getEnd();
439 if (start <= to && end >= from)
449 * Performs a binary search of the (sorted) list to find the index of the
450 * first entry whose end position is not less than the target position (i.e.
451 * skip all features that properly precede the given position)
457 protected int binarySearch(List<SequenceFeature> features, long target)
459 int width = features.size() / 2;
463 int end = features.get(lastpos).getEnd();
474 // todo correct binary search
475 return lastpos > 1 ? lastpos - 2 : 0;
480 * Adds contact features whose start position lies in the from-to range to the
487 protected void findContactStartFeatures(long from, long to,
488 List<SequenceFeature> result)
490 // TODO binary search for startPosition
491 for (int startPosition = 0; startPosition < contactFeatureStarts.size(); startPosition++)
493 SequenceFeature sf = contactFeatureStarts.get(startPosition);
494 if (!sf.isContactFeature())
496 System.err.println("Error! non-contact feature type "
497 + sf.getType() + " in contact features list");
500 int begin = sf.getBegin();
501 if (begin >= from && begin <= to)
509 * Answers a list of all features stored (including any non-positional
510 * features), in no guaranteed order
514 public List<SequenceFeature> getFeatures()
517 * add non-nested features (may be all features for many cases)
519 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
520 result.addAll(nonNestedFeatures);
523 * add any contact features - from the list by start position
525 if (contactFeatureStarts != null)
527 result.addAll(contactFeatureStarts);
531 * add any non-positional features
533 if (nonPositionalFeatures != null)
535 result.addAll(nonPositionalFeatures);
539 * add any nested features
541 if (nestedFeatures != null)
543 result.addAll(nestedFeatures.getEntries());
550 * Answers a list of all contact features. If there are none, returns an
551 * immutable empty list.
555 public List<SequenceFeature> getContactFeatures()
557 if (contactFeatureStarts == null)
559 return Collections.emptyList();
561 return new ArrayList<SequenceFeature>(contactFeatureStarts);
565 * Answers a list of all non-positional features. If there are none, returns
566 * an immutable empty list.
570 public List<SequenceFeature> getNonPositionalFeatures()
572 if (nonPositionalFeatures == null)
574 return Collections.emptyList();
576 return new ArrayList<SequenceFeature>(nonPositionalFeatures);
580 * Deletes the given feature from the store, returning true if it was found
581 * (and deleted), else false. This method makes no assumption that the feature
582 * is in the 'expected' place in the store, in case it has been modified since
587 public synchronized boolean delete(SequenceFeature sf)
590 * try the non-nested positional features first
592 boolean removed = nonNestedFeatures.remove(sf);
595 * if not found, try contact positions (and if found, delete
596 * from both lists of contact positions)
598 if (!removed && contactFeatureStarts != null)
600 removed = contactFeatureStarts.remove(sf);
603 contactFeatureEnds.remove(sf);
608 * if not found, try non-positional features
610 if (!removed && nonPositionalFeatures != null)
612 removed = nonPositionalFeatures.remove(sf);
616 * if not found, try nested features
618 if (!removed && nestedFeatures != null)
620 removed = nestedFeatures.delete(sf);
626 * if this was the only feature for its feature group,
627 * remove the group from the stored set
629 String featureGroup = sf.getFeatureGroup();
630 if (!findFeatureGroup(featureGroup))
632 featureGroups.remove(featureGroup);
640 * Scans all features to check whether the given feature group is found, and
641 * returns true if found, else false
643 * @param featureGroup
646 protected boolean findFeatureGroup(String featureGroup)
648 for (SequenceFeature sf : getFeatures())
650 String group = sf.getFeatureGroup();
651 if (group == featureGroup
652 || (group != null && group.equals(featureGroup)))
661 * Answers true if this store has no features, else false
665 public boolean isEmpty()
667 boolean hasFeatures = !nonNestedFeatures.isEmpty()
668 || (contactFeatureStarts != null && !contactFeatureStarts
670 || (nonPositionalFeatures != null && !nonPositionalFeatures
672 || (nestedFeatures != null && nestedFeatures.size() > 0);
678 * Answers the set of distinct feature groups stored, possibly including null,
679 * as an unmodifiable view of the set.
683 public Set<String> getFeatureGroups()
685 return Collections.unmodifiableSet(featureGroups);