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
23 * a class providing criteria for performing a binary search of a list
25 abstract static class SearchCriterion
28 * Answers true if the entry passes the search criterion test
33 abstract boolean compare(SequenceFeature entry);
35 static SearchCriterion byStart(final long target)
37 return new SearchCriterion() {
40 boolean compare(SequenceFeature entry)
42 return entry.getBegin() >= target;
47 static SearchCriterion byEnd(final long target)
49 return new SearchCriterion()
53 boolean compare(SequenceFeature entry)
55 return entry.getEnd() >= target;
60 static SearchCriterion byFeature(final ContiguousI to,
61 final Comparator<ContiguousI> rc)
63 return new SearchCriterion()
67 boolean compare(SequenceFeature entry)
69 return rc.compare(entry, to) >= 0;
75 Comparator<ContiguousI> startOrdering = new RangeComparator(true);
77 Comparator<ContiguousI> endOrdering = new RangeComparator(false);
80 * Non-positional features have no (zero) start/end position.
81 * Kept as a separate list in case this criterion changes in future.
83 List<SequenceFeature> nonPositionalFeatures;
86 * An ordered list of features, with the promise that no feature in the list
87 * properly contains any other. This constraint allows bounded linear search
88 * of the list for features overlapping a region.
89 * Contact features are not included in this list.
91 List<SequenceFeature> nonNestedFeatures;
94 * contact features ordered by first contact position
96 List<SequenceFeature> contactFeatureStarts;
99 * contact features ordered by second contact position
101 List<SequenceFeature> contactFeatureEnds;
104 * Nested Containment List is used to hold any features that are nested
105 * within (properly contained by) any other feature. This is a recursive tree
106 * which supports depth-first scan for features overlapping a range.
107 * It is used here as a 'catch-all' fallback for features that cannot be put
108 * into a simple ordered list without invalidating the search methods.
110 NCList<SequenceFeature> nestedFeatures;
113 * Feature groups represented in stored positional features
114 * (possibly including null)
116 Set<String> positionalFeatureGroups;
119 * Feature groups represented in stored non-positional features
120 * (possibly including null)
122 Set<String> nonPositionalFeatureGroups;
127 public FeatureStore()
129 nonNestedFeatures = new ArrayList<SequenceFeature>();
130 positionalFeatureGroups = new HashSet<String>();
132 // we only construct nonPositionalFeatures, contactFeatures
133 // or the NCList if we need to
137 * Adds one sequence feature to the store, and returns true, unless the
138 * feature is already contained in the store, in which case this method
139 * returns false. Containment is determined by SequenceFeature.equals()
144 public boolean addFeature(SequenceFeature feature)
147 * keep a record of feature groups
149 if (!feature.isNonPositional())
151 positionalFeatureGroups.add(feature.getFeatureGroup());
154 boolean added = false;
156 if (feature.isContactFeature())
158 added = addContactFeature(feature);
160 else if (feature.isNonPositional())
162 added = addNonPositionalFeature(feature);
166 if (!nonNestedFeatures.contains(feature))
168 added = addNonNestedFeature(feature);
172 * detected a nested feature - put it in the NCList structure
174 added = addNestedFeature(feature);
183 * Adds the feature to the list of non-positional features (with lazy
184 * instantiation of the list if it is null), and returns true. If the
185 * non-positional features already include the new feature (by equality test),
186 * then it is not added, and this method returns false. The feature group is
187 * added to the set of distinct feature groups for non-positional features.
191 protected boolean addNonPositionalFeature(SequenceFeature feature)
193 if (nonPositionalFeatures == null)
195 nonPositionalFeatures = new ArrayList<SequenceFeature>();
196 nonPositionalFeatureGroups = new HashSet<String>();
198 if (nonPositionalFeatures.contains(feature))
203 nonPositionalFeatures.add(feature);
205 nonPositionalFeatureGroups.add(feature.getFeatureGroup());
211 * Adds one feature to the NCList that can manage nested features (creating
212 * the NCList if necessary), and returns true. If the feature is already
213 * stored in the NCList (by equality test), then it is not added, and this
214 * method returns false.
216 protected synchronized boolean addNestedFeature(SequenceFeature feature)
218 if (nestedFeatures == null)
220 nestedFeatures = new NCList<SequenceFeature>(feature);
223 return nestedFeatures.add(feature, false);
227 * Add a feature to the list of non-nested features, maintaining the ordering
228 * of the list. A check is made for whether the feature is nested in (properly
229 * contained by) an existing feature. If there is no nesting, the feature is
230 * added to the list and the method returns true. If nesting is found, the
231 * feature is not added and the method returns false.
236 protected boolean addNonNestedFeature(SequenceFeature feature)
238 synchronized (nonNestedFeatures)
241 * find the first stored feature which doesn't precede the new one
243 int insertPosition = binarySearch(nonNestedFeatures,
244 SearchCriterion.byFeature(feature, startOrdering));
247 * fail if we detect feature enclosure - of the new feature by
248 * the one preceding it, or of the next feature by the new one
250 if (insertPosition > 0)
252 if (encloses(nonNestedFeatures.get(insertPosition - 1), feature))
257 if (insertPosition < nonNestedFeatures.size())
259 if (encloses(feature, nonNestedFeatures.get(insertPosition)))
266 * checks passed - add the feature
268 nonNestedFeatures.add(insertPosition, feature);
275 * Answers true if range1 properly encloses range2, else false
281 protected boolean encloses(ContiguousI range1, ContiguousI range2)
283 int begin1 = range1.getBegin();
284 int begin2 = range2.getBegin();
285 int end1 = range1.getEnd();
286 int end2 = range2.getEnd();
287 if (begin1 == begin2 && end1 > end2)
291 if (begin1 < begin2 && end1 >= end2)
299 * Add a contact feature to the lists that hold them ordered by start (first
300 * contact) and by end (second contact) position, ensuring the lists remain
301 * ordered, and returns true. If the contact feature lists already contain the
302 * given feature (by test for equality), does not add it and returns false.
307 protected synchronized boolean addContactFeature(SequenceFeature feature)
309 if (contactFeatureStarts == null)
311 contactFeatureStarts = new ArrayList<SequenceFeature>();
313 if (contactFeatureEnds == null)
315 contactFeatureEnds = new ArrayList<SequenceFeature>();
318 // TODO binary search for insertion points!
319 if (contactFeatureStarts.contains(feature))
324 contactFeatureStarts.add(feature);
325 Collections.sort(contactFeatureStarts, startOrdering);
326 contactFeatureEnds.add(feature);
327 Collections.sort(contactFeatureEnds, endOrdering);
333 * Returns a (possibly empty) list of features whose extent overlaps the given
334 * range. The returned list is not ordered. Contact features are included if
335 * either of the contact points lies within the range.
338 * start position of overlap range (inclusive)
340 * end position of overlap range (inclusive)
343 public List<SequenceFeature> findOverlappingFeatures(long start, long end)
345 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
347 findNonNestedFeatures(start, end, result);
349 findContactFeatures(start, end, result);
351 if (nestedFeatures != null)
353 result.addAll(nestedFeatures.findOverlaps(start, end));
360 * Adds contact features to the result list where either the second or the
361 * first contact position lies within the target range
367 protected void findContactFeatures(long from, long to,
368 List<SequenceFeature> result)
370 if (contactFeatureStarts != null)
372 findContactStartFeatures(from, to, result);
374 if (contactFeatureEnds != null)
376 findContactEndFeatures(from, to, result);
381 * Adds to the result list any contact features whose end (second contact
382 * point), but not start (first contact point), lies in the query from-to
389 protected void findContactEndFeatures(long from, long to,
390 List<SequenceFeature> result)
393 * find the first contact feature (if any) that does not lie
394 * entirely before the target range
396 int startPosition = binarySearch(contactFeatureEnds,
397 SearchCriterion.byEnd(from));
398 for (; startPosition < contactFeatureEnds.size(); startPosition++)
400 SequenceFeature sf = contactFeatureEnds.get(startPosition);
401 if (!sf.isContactFeature())
403 System.err.println("Error! non-contact feature type "
404 + sf.getType() + " in contact features list");
408 int begin = sf.getBegin();
409 if (begin >= from && begin <= to)
412 * this feature's first contact position lies in the search range
413 * so we don't include it in results a second time
418 int end = sf.getEnd();
419 if (end >= from && end <= to)
431 * Adds non-nested features to the result list that lie within the target
432 * range. Non-positional features (start=end=0), contact features and nested
433 * features are excluded.
439 protected void findNonNestedFeatures(long from, long to,
440 List<SequenceFeature> result)
442 int startIndex = binarySearch(nonNestedFeatures,
443 SearchCriterion.byEnd(from));
445 findNonNestedFeatures(startIndex, from, to, result);
449 * Scans the list of non-nested features, starting from startIndex, to find
450 * those that overlap the from-to range, and adds them to the result list.
451 * Returns the index of the first feature whose start position is after the
452 * target range (or the length of the whole list if none such feature exists).
460 protected int findNonNestedFeatures(final int startIndex, long from,
461 long to, List<SequenceFeature> result)
464 while (i < nonNestedFeatures.size())
466 SequenceFeature sf = nonNestedFeatures.get(i);
467 if (sf.getBegin() > to)
471 int start = sf.getBegin();
472 int end = sf.getEnd();
473 if (start <= to && end >= from)
483 * Adds contact features whose start position lies in the from-to range to the
490 protected void findContactStartFeatures(long from, long to,
491 List<SequenceFeature> result)
493 int startPosition = binarySearch(contactFeatureStarts,
494 SearchCriterion.byStart(from));
496 for (; startPosition < contactFeatureStarts.size(); startPosition++)
498 SequenceFeature sf = contactFeatureStarts.get(startPosition);
499 if (!sf.isContactFeature())
501 System.err.println("Error! non-contact feature type "
502 + sf.getType() + " in contact features list");
505 int begin = sf.getBegin();
506 if (begin >= from && begin <= to)
514 * Answers a list of all positional features stored, in no guaranteed order
518 public List<SequenceFeature> getPositionalFeatures()
521 * add non-nested features (may be all features for many cases)
523 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
524 result.addAll(nonNestedFeatures);
527 * add any contact features - from the list by start position
529 if (contactFeatureStarts != null)
531 result.addAll(contactFeatureStarts);
535 * add any nested features
537 if (nestedFeatures != null)
539 result.addAll(nestedFeatures.getEntries());
546 * Answers a list of all contact features. If there are none, returns an
547 * immutable empty list.
551 public List<SequenceFeature> getContactFeatures()
553 if (contactFeatureStarts == null)
555 return Collections.emptyList();
557 return new ArrayList<SequenceFeature>(contactFeatureStarts);
561 * Answers a list of all non-positional features. If there are none, returns
562 * an immutable empty list.
566 public List<SequenceFeature> getNonPositionalFeatures()
568 if (nonPositionalFeatures == null)
570 return Collections.emptyList();
572 return new ArrayList<SequenceFeature>(nonPositionalFeatures);
576 * Deletes the given feature from the store, returning true if it was found
577 * (and deleted), else false. This method makes no assumption that the feature
578 * is in the 'expected' place in the store, in case it has been modified since
583 public synchronized boolean delete(SequenceFeature sf)
586 * try the non-nested positional features first
588 boolean removed = nonNestedFeatures.remove(sf);
591 * if not found, try contact positions (and if found, delete
592 * from both lists of contact positions)
594 if (!removed && contactFeatureStarts != null)
596 removed = contactFeatureStarts.remove(sf);
599 contactFeatureEnds.remove(sf);
603 boolean removedNonPositional = false;
606 * if not found, try non-positional features
608 if (!removed && nonPositionalFeatures != null)
610 removedNonPositional = nonPositionalFeatures.remove(sf);
611 removed = removedNonPositional;
615 * if not found, try nested features
617 if (!removed && nestedFeatures != null)
619 removed = nestedFeatures.delete(sf);
624 rebuildFeatureGroups(sf.getFeatureGroup(), removedNonPositional);
631 * Check whether the given feature group is still represented, in either
632 * positional or non-positional features, and if not, remove it from the set
635 * @param featureGroup
636 * @param nonPositional
638 protected void rebuildFeatureGroups(String featureGroup,
639 boolean nonPositional)
641 if (nonPositional && nonPositionalFeatures != null)
643 boolean found = false;
644 for (SequenceFeature sf : nonPositionalFeatures)
646 String group = sf.getFeatureGroup();
647 if (featureGroup == group
648 || (featureGroup != null && featureGroup.equals(group)))
656 nonPositionalFeatureGroups.remove(featureGroup);
659 else if (!findFeatureGroup(featureGroup))
661 positionalFeatureGroups.remove(featureGroup);
666 * Scans all positional features to check whether the given feature group is
667 * found, and returns true if found, else false
669 * @param featureGroup
672 protected boolean findFeatureGroup(String featureGroup)
674 for (SequenceFeature sf : getPositionalFeatures())
676 String group = sf.getFeatureGroup();
677 if (group == featureGroup
678 || (group != null && group.equals(featureGroup)))
687 * Answers true if this store has no features, else false
691 public boolean isEmpty()
693 boolean hasFeatures = !nonNestedFeatures.isEmpty()
694 || (contactFeatureStarts != null && !contactFeatureStarts
696 || (nonPositionalFeatures != null && !nonPositionalFeatures
698 || (nestedFeatures != null && nestedFeatures.size() > 0);
704 * Answers the set of distinct feature groups stored, possibly including null,
705 * as an unmodifiable view of the set. The parameter determines whether the
706 * groups for positional or for non-positional features are returned.
708 * @param positionalFeatures
711 public Set<String> getFeatureGroups(boolean positionalFeatures)
713 if (positionalFeatures)
715 return Collections.unmodifiableSet(positionalFeatureGroups);
719 return nonPositionalFeatureGroups == null ? Collections
720 .<String> emptySet() : Collections
721 .unmodifiableSet(nonPositionalFeatureGroups);
726 * Performs a binary search of the (sorted) list to find the index of the
727 * first entry which returns true for the given comparator function. Returns
728 * the length of the list if there is no such entry.
734 protected int binarySearch(List<SequenceFeature> features,
738 int end = features.size() - 1;
739 int matched = features.size();
743 int mid = (start + end) / 2;
744 SequenceFeature entry = features.get(mid);
745 boolean compare = sc.compare(entry);