1 package jalview.datamodel.features;
3 import jalview.datamodel.ContiguousI;
4 import jalview.datamodel.SequenceFeature;
6 import java.util.ArrayList;
7 import java.util.Collections;
8 import java.util.Comparator;
9 import java.util.HashSet;
10 import java.util.List;
14 * A data store for a set of sequence features that supports efficient lookup of
15 * features overlapping a given range. Intended for (but not limited to) storage
16 * of features for one sequence and feature type.
21 public class FeatureStore
24 * a class providing criteria for performing a binary search of a list
26 abstract static class SearchCriterion
29 * Answers true if the entry passes the search criterion test
34 abstract boolean compare(SequenceFeature entry);
36 static SearchCriterion byStart(final long target)
38 return new SearchCriterion() {
41 boolean compare(SequenceFeature entry)
43 return entry.getBegin() >= target;
48 static SearchCriterion byEnd(final long target)
50 return new SearchCriterion()
54 boolean compare(SequenceFeature entry)
56 return entry.getEnd() >= target;
61 static SearchCriterion byFeature(final ContiguousI to,
62 final Comparator<ContiguousI> rc)
64 return new SearchCriterion()
68 boolean compare(SequenceFeature entry)
70 return rc.compare(entry, to) >= 0;
77 * Non-positional features have no (zero) start/end position.
78 * Kept as a separate list in case this criterion changes in future.
80 List<SequenceFeature> nonPositionalFeatures;
83 * An ordered list of features, with the promise that no feature in the list
84 * properly contains any other. This constraint allows bounded linear search
85 * of the list for features overlapping a region.
86 * Contact features are not included in this list.
88 List<SequenceFeature> nonNestedFeatures;
91 * contact features ordered by first contact position
93 List<SequenceFeature> contactFeatureStarts;
96 * contact features ordered by second contact position
98 List<SequenceFeature> contactFeatureEnds;
101 * Nested Containment List is used to hold any features that are nested
102 * within (properly contained by) any other feature. This is a recursive tree
103 * which supports depth-first scan for features overlapping a range.
104 * It is used here as a 'catch-all' fallback for features that cannot be put
105 * into a simple ordered list without invalidating the search methods.
107 NCList<SequenceFeature> nestedFeatures;
110 * Feature groups represented in stored positional features
111 * (possibly including null)
113 Set<String> positionalFeatureGroups;
116 * Feature groups represented in stored non-positional features
117 * (possibly including null)
119 Set<String> nonPositionalFeatureGroups;
122 * the total length of all positional features; contact features count 1 to
123 * the total and 1 to size(), consistent with an average 'feature length' of 1
127 float positionalMinScore;
129 float positionalMaxScore;
131 float nonPositionalMinScore;
133 float nonPositionalMaxScore;
138 public FeatureStore()
140 nonNestedFeatures = new ArrayList<SequenceFeature>();
141 positionalFeatureGroups = new HashSet<String>();
142 nonPositionalFeatureGroups = new HashSet<String>();
143 positionalMinScore = Float.NaN;
144 positionalMaxScore = Float.NaN;
145 nonPositionalMinScore = Float.NaN;
146 nonPositionalMaxScore = Float.NaN;
148 // we only construct nonPositionalFeatures, contactFeatures
149 // or the NCList if we need to
153 * Adds one sequence feature to the store, and returns true, unless the
154 * feature is already contained in the store, in which case this method
155 * returns false. Containment is determined by SequenceFeature.equals()
160 public boolean addFeature(SequenceFeature feature)
163 * keep a record of feature groups
165 if (!feature.isNonPositional())
167 positionalFeatureGroups.add(feature.getFeatureGroup());
170 boolean added = false;
172 if (feature.isContactFeature())
174 added = addContactFeature(feature);
176 else if (feature.isNonPositional())
178 added = addNonPositionalFeature(feature);
182 if (!contains(nonNestedFeatures, feature))
184 added = addNonNestedFeature(feature);
188 * detected a nested feature - put it in the NCList structure
190 added = addNestedFeature(feature);
198 * record the total extent of positional features, to make
199 * getTotalFeatureLength possible; we count the length of a
200 * contact feature as 1
202 totalExtent += getFeatureLength(feature);
205 * record the minimum and maximum score for positional
206 * and non-positional features
208 float score = feature.getScore();
209 if (!Float.isNaN(score))
211 if (feature.isNonPositional())
213 nonPositionalMinScore = min(nonPositionalMinScore, score);
214 nonPositionalMaxScore = max(nonPositionalMaxScore, score);
218 positionalMinScore = min(positionalMinScore, score);
219 positionalMaxScore = max(positionalMaxScore, score);
228 * Answers the 'length' of the feature, counting 0 for non-positional features
229 * and 1 for contact features
234 protected static int getFeatureLength(SequenceFeature feature)
236 if (feature.isNonPositional())
240 if (feature.isContactFeature())
244 return 1 + feature.getEnd() - feature.getBegin();
248 * Adds the feature to the list of non-positional features (with lazy
249 * instantiation of the list if it is null), and returns true. If the
250 * non-positional features already include the new feature (by equality test),
251 * then it is not added, and this method returns false. The feature group is
252 * added to the set of distinct feature groups for non-positional features.
256 protected boolean addNonPositionalFeature(SequenceFeature feature)
258 if (nonPositionalFeatures == null)
260 nonPositionalFeatures = new ArrayList<SequenceFeature>();
262 if (nonPositionalFeatures.contains(feature))
267 nonPositionalFeatures.add(feature);
269 nonPositionalFeatureGroups.add(feature.getFeatureGroup());
275 * Adds one feature to the NCList that can manage nested features (creating
276 * the NCList if necessary), and returns true. If the feature is already
277 * stored in the NCList (by equality test), then it is not added, and this
278 * method returns false.
280 protected synchronized boolean addNestedFeature(SequenceFeature feature)
282 if (nestedFeatures == null)
284 nestedFeatures = new NCList<SequenceFeature>(feature);
287 return nestedFeatures.add(feature, false);
291 * Add a feature to the list of non-nested features, maintaining the ordering
292 * of the list. A check is made for whether the feature is nested in (properly
293 * contained by) an existing feature. If there is no nesting, the feature is
294 * added to the list and the method returns true. If nesting is found, the
295 * feature is not added and the method returns false.
300 protected boolean addNonNestedFeature(SequenceFeature feature)
302 synchronized (nonNestedFeatures)
305 * find the first stored feature which doesn't precede the new one
307 int insertPosition = binarySearch(nonNestedFeatures,
308 SearchCriterion.byFeature(feature, RangeComparator.BY_START_POSITION));
311 * fail if we detect feature enclosure - of the new feature by
312 * the one preceding it, or of the next feature by the new one
314 if (insertPosition > 0)
316 if (encloses(nonNestedFeatures.get(insertPosition - 1), feature))
321 if (insertPosition < nonNestedFeatures.size())
323 if (encloses(feature, nonNestedFeatures.get(insertPosition)))
330 * checks passed - add the feature
332 nonNestedFeatures.add(insertPosition, feature);
339 * Answers true if range1 properly encloses range2, else false
345 protected boolean encloses(ContiguousI range1, ContiguousI range2)
347 int begin1 = range1.getBegin();
348 int begin2 = range2.getBegin();
349 int end1 = range1.getEnd();
350 int end2 = range2.getEnd();
351 if (begin1 == begin2 && end1 > end2)
355 if (begin1 < begin2 && end1 >= end2)
363 * Add a contact feature to the lists that hold them ordered by start (first
364 * contact) and by end (second contact) position, ensuring the lists remain
365 * ordered, and returns true. If the contact feature lists already contain the
366 * given feature (by test for equality), does not add it and returns false.
371 protected synchronized boolean addContactFeature(SequenceFeature feature)
373 if (contactFeatureStarts == null)
375 contactFeatureStarts = new ArrayList<SequenceFeature>();
377 if (contactFeatureEnds == null)
379 contactFeatureEnds = new ArrayList<SequenceFeature>();
382 if (contains(contactFeatureStarts, feature))
388 * binary search the sorted list to find the insertion point
390 int insertPosition = binarySearch(contactFeatureStarts,
391 SearchCriterion.byFeature(feature,
392 RangeComparator.BY_START_POSITION));
393 contactFeatureStarts.add(insertPosition, feature);
394 // and resort to mak siccar...just in case insertion point not quite right
395 Collections.sort(contactFeatureStarts, RangeComparator.BY_START_POSITION);
397 insertPosition = binarySearch(contactFeatureStarts,
398 SearchCriterion.byFeature(feature,
399 RangeComparator.BY_END_POSITION));
400 contactFeatureEnds.add(feature);
401 Collections.sort(contactFeatureEnds, RangeComparator.BY_END_POSITION);
407 * Answers true if the list contains the feature, else false. This method is
408 * optimised for the condition that the list is sorted on feature start
409 * position ascending, and will give unreliable results if this does not hold.
415 protected static boolean contains(List<SequenceFeature> features,
416 SequenceFeature feature)
418 if (features == null || feature == null)
424 * locate the first entry in the list which does not precede the feature
426 int pos = binarySearch(features,
427 SearchCriterion.byFeature(feature, RangeComparator.BY_START_POSITION));
428 int len = features.size();
431 SequenceFeature sf = features.get(pos);
432 if (sf.getBegin() > feature.getBegin())
434 return false; // no match found
436 if (sf.equals(feature))
446 * Returns a (possibly empty) list of features whose extent overlaps the given
447 * range. The returned list is not ordered. Contact features are included if
448 * either of the contact points lies within the range.
451 * start position of overlap range (inclusive)
453 * end position of overlap range (inclusive)
456 public List<SequenceFeature> findOverlappingFeatures(long start, long end)
458 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
460 findNonNestedFeatures(start, end, result);
462 findContactFeatures(start, end, result);
464 if (nestedFeatures != null)
466 result.addAll(nestedFeatures.findOverlaps(start, end));
473 * Adds contact features to the result list where either the second or the
474 * first contact position lies within the target range
480 protected void findContactFeatures(long from, long to,
481 List<SequenceFeature> result)
483 if (contactFeatureStarts != null)
485 findContactStartFeatures(from, to, result);
487 if (contactFeatureEnds != null)
489 findContactEndFeatures(from, to, result);
494 * Adds to the result list any contact features whose end (second contact
495 * point), but not start (first contact point), lies in the query from-to
502 protected void findContactEndFeatures(long from, long to,
503 List<SequenceFeature> result)
506 * find the first contact feature (if any) that does not lie
507 * entirely before the target range
509 int startPosition = binarySearch(contactFeatureEnds,
510 SearchCriterion.byEnd(from));
511 for (; startPosition < contactFeatureEnds.size(); startPosition++)
513 SequenceFeature sf = contactFeatureEnds.get(startPosition);
514 if (!sf.isContactFeature())
516 System.err.println("Error! non-contact feature type "
517 + sf.getType() + " in contact features list");
521 int begin = sf.getBegin();
522 if (begin >= from && begin <= to)
525 * this feature's first contact position lies in the search range
526 * so we don't include it in results a second time
531 int end = sf.getEnd();
532 if (end >= from && end <= to)
544 * Adds non-nested features to the result list that lie within the target
545 * range. Non-positional features (start=end=0), contact features and nested
546 * features are excluded.
552 protected void findNonNestedFeatures(long from, long to,
553 List<SequenceFeature> result)
555 int startIndex = binarySearch(nonNestedFeatures,
556 SearchCriterion.byEnd(from));
558 findNonNestedFeatures(startIndex, from, to, result);
562 * Scans the list of non-nested features, starting from startIndex, to find
563 * those that overlap the from-to range, and adds them to the result list.
564 * Returns the index of the first feature whose start position is after the
565 * target range (or the length of the whole list if none such feature exists).
573 protected int findNonNestedFeatures(final int startIndex, long from,
574 long to, List<SequenceFeature> result)
577 while (i < nonNestedFeatures.size())
579 SequenceFeature sf = nonNestedFeatures.get(i);
580 if (sf.getBegin() > to)
584 int start = sf.getBegin();
585 int end = sf.getEnd();
586 if (start <= to && end >= from)
596 * Adds contact features whose start position lies in the from-to range to the
603 protected void findContactStartFeatures(long from, long to,
604 List<SequenceFeature> result)
606 int startPosition = binarySearch(contactFeatureStarts,
607 SearchCriterion.byStart(from));
609 for (; startPosition < contactFeatureStarts.size(); startPosition++)
611 SequenceFeature sf = contactFeatureStarts.get(startPosition);
612 if (!sf.isContactFeature())
614 System.err.println("Error! non-contact feature type "
615 + sf.getType() + " in contact features list");
618 int begin = sf.getBegin();
619 if (begin >= from && begin <= to)
627 * Answers a list of all positional features stored, in no guaranteed order
631 public List<SequenceFeature> getPositionalFeatures()
634 * add non-nested features (may be all features for many cases)
636 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
637 result.addAll(nonNestedFeatures);
640 * add any contact features - from the list by start position
642 if (contactFeatureStarts != null)
644 result.addAll(contactFeatureStarts);
648 * add any nested features
650 if (nestedFeatures != null)
652 result.addAll(nestedFeatures.getEntries());
659 * Answers a list of all contact features. If there are none, returns an
660 * immutable empty list.
664 public List<SequenceFeature> getContactFeatures()
666 if (contactFeatureStarts == null)
668 return Collections.emptyList();
670 return new ArrayList<SequenceFeature>(contactFeatureStarts);
674 * Answers a list of all non-positional features. If there are none, returns
675 * an immutable empty list.
679 public List<SequenceFeature> getNonPositionalFeatures()
681 if (nonPositionalFeatures == null)
683 return Collections.emptyList();
685 return new ArrayList<SequenceFeature>(nonPositionalFeatures);
689 * Deletes the given feature from the store, returning true if it was found
690 * (and deleted), else false. This method makes no assumption that the feature
691 * is in the 'expected' place in the store, in case it has been modified since
696 public synchronized boolean delete(SequenceFeature sf)
699 * try the non-nested positional features first
701 boolean removed = nonNestedFeatures.remove(sf);
704 * if not found, try contact positions (and if found, delete
705 * from both lists of contact positions)
707 if (!removed && contactFeatureStarts != null)
709 removed = contactFeatureStarts.remove(sf);
712 contactFeatureEnds.remove(sf);
716 boolean removedNonPositional = false;
719 * if not found, try non-positional features
721 if (!removed && nonPositionalFeatures != null)
723 removedNonPositional = nonPositionalFeatures.remove(sf);
724 removed = removedNonPositional;
728 * if not found, try nested features
730 if (!removed && nestedFeatures != null)
732 removed = nestedFeatures.delete(sf);
744 * Rescan all features to recompute any cached values after an entry has been
745 * deleted. This is expected to be an infrequent event, so performance here is
748 protected synchronized void rescanAfterDelete()
750 positionalFeatureGroups.clear();
751 nonPositionalFeatureGroups.clear();
753 positionalMinScore = Float.NaN;
754 positionalMaxScore = Float.NaN;
755 nonPositionalMinScore = Float.NaN;
756 nonPositionalMaxScore = Float.NaN;
759 * scan non-positional features for groups and scores
761 for (SequenceFeature sf : getNonPositionalFeatures())
763 nonPositionalFeatureGroups.add(sf.getFeatureGroup());
764 float score = sf.getScore();
765 nonPositionalMinScore = min(nonPositionalMinScore, score);
766 nonPositionalMaxScore = max(nonPositionalMaxScore, score);
770 * scan positional features for groups, scores and extents
772 for (SequenceFeature sf : getPositionalFeatures())
774 positionalFeatureGroups.add(sf.getFeatureGroup());
775 float score = sf.getScore();
776 positionalMinScore = min(positionalMinScore, score);
777 positionalMaxScore = max(positionalMaxScore, score);
778 totalExtent += getFeatureLength(sf);
783 * A helper method to return the minimum of two floats, where a non-NaN value
784 * is treated as 'less than' a NaN value (unlike Math.min which does the
790 protected static float min(float f1, float f2)
794 return Float.isNaN(f2) ? f1 : f2;
798 return Float.isNaN(f2) ? f1 : Math.min(f1, f2);
803 * A helper method to return the maximum of two floats, where a non-NaN value
804 * is treated as 'greater than' a NaN value (unlike Math.max which does the
810 protected static float max(float f1, float f2)
814 return Float.isNaN(f2) ? f1 : f2;
818 return Float.isNaN(f2) ? f1 : Math.max(f1, f2);
823 * Answers true if this store has no features, else false
827 public boolean isEmpty()
829 boolean hasFeatures = !nonNestedFeatures.isEmpty()
830 || (contactFeatureStarts != null && !contactFeatureStarts
832 || (nonPositionalFeatures != null && !nonPositionalFeatures
834 || (nestedFeatures != null && nestedFeatures.size() > 0);
840 * Answers the set of distinct feature groups stored, possibly including null,
841 * as an unmodifiable view of the set. The parameter determines whether the
842 * groups for positional or for non-positional features are returned.
844 * @param positionalFeatures
847 public Set<String> getFeatureGroups(boolean positionalFeatures)
849 if (positionalFeatures)
851 return Collections.unmodifiableSet(positionalFeatureGroups);
855 return nonPositionalFeatureGroups == null ? Collections
856 .<String> emptySet() : Collections
857 .unmodifiableSet(nonPositionalFeatureGroups);
862 * Performs a binary search of the (sorted) list to find the index of the
863 * first entry which returns true for the given comparator function. Returns
864 * the length of the list if there is no such entry.
870 protected static int binarySearch(List<SequenceFeature> features,
874 int end = features.size() - 1;
875 int matched = features.size();
879 int mid = (start + end) / 2;
880 SequenceFeature entry = features.get(mid);
881 boolean compare = sc.compare(entry);
897 * Answers the number of positional (or non-positional) features stored.
898 * Contact features count as 1.
903 public int getFeatureCount(boolean positional)
907 return nonPositionalFeatures == null ? 0 : nonPositionalFeatures
911 int size = nonNestedFeatures.size();
913 if (contactFeatureStarts != null)
915 // note a contact feature (start/end) counts as one
916 size += contactFeatureStarts.size();
919 if (nestedFeatures != null)
921 size += nestedFeatures.size();
928 * Answers the total length of positional features (or zero if there are
929 * none). Contact features contribute a value of 1 to the total.
933 public int getTotalFeatureLength()
939 * Answers the minimum score held for positional or non-positional features.
940 * This may be Float.NaN if there are no features, are none has a non-NaN
946 public float getMinimumScore(boolean positional)
948 return positional ? positionalMinScore : nonPositionalMinScore;
952 * Answers the maximum score held for positional or non-positional features.
953 * This may be Float.NaN if there are no features, are none has a non-NaN
959 public float getMaximumScore(boolean positional)
961 return positional ? positionalMaxScore : nonPositionalMaxScore;
965 * Answers a list of all either positional or non-positional features whose
966 * feature group matches the given group (which may be null)
972 public List<SequenceFeature> getFeaturesForGroup(boolean positional,
975 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
978 * if we know features don't include the target group, no need
979 * to inspect them for matches
981 if (positional && !positionalFeatureGroups.contains(group)
982 || !positional && !nonPositionalFeatureGroups.contains(group))
987 List<SequenceFeature> sfs = positional ? getPositionalFeatures()
988 : getNonPositionalFeatures();
989 for (SequenceFeature sf : sfs)
991 String featureGroup = sf.getFeatureGroup();
992 if (group == null && featureGroup == null || group != null
993 && group.equals(featureGroup))
1002 * Adds the shift value to the start and end of all positional features.
1003 * Returns true if at least one feature was updated, else false.
1008 public synchronized boolean shiftFeatures(int shift)
1011 * Because begin and end are final fields (to ensure the data store's
1012 * integrity), we have to delete each feature and re-add it as amended.
1013 * (Although a simple shift of all values would preserve data integrity!)
1015 boolean modified = false;
1016 for (SequenceFeature sf : getPositionalFeatures())
1019 int newBegin = sf.getBegin() + shift;
1020 int newEnd = sf.getEnd() + shift;
1023 * sanity check: don't shift left of the first residue
1027 newBegin = Math.max(1, newBegin);
1028 SequenceFeature sf2 = new SequenceFeature(sf, newBegin, newEnd,
1029 sf.getFeatureGroup(), sf.getScore());