2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.datamodel.features;
23 import jalview.datamodel.ContiguousI;
24 import jalview.datamodel.SequenceFeature;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.Comparator;
29 import java.util.HashSet;
30 import java.util.List;
34 * A data store for a set of sequence features that supports efficient lookup of
35 * features overlapping a given range. Intended for (but not limited to) storage
36 * of features for one sequence and feature type.
41 public class FeatureStore
44 * a class providing criteria for performing a binary search of a list
46 abstract static class SearchCriterion
49 * Answers true if the entry passes the search criterion test
54 abstract boolean compare(SequenceFeature entry);
57 * serves a search condition for finding the first feature whose start
58 * position follows a given target location
63 static SearchCriterion byStart(final long target)
65 return new SearchCriterion() {
68 boolean compare(SequenceFeature entry)
70 return entry.getBegin() >= target;
76 * serves a search condition for finding the first feature whose end
77 * position is at or follows a given target location
82 static SearchCriterion byEnd(final long target)
84 return new SearchCriterion()
88 boolean compare(SequenceFeature entry)
90 return entry.getEnd() >= target;
96 * serves a search condition for finding the first feature which follows the
97 * given range as determined by a supplied comparator
102 static SearchCriterion byFeature(final ContiguousI to,
103 final Comparator<ContiguousI> rc)
105 return new SearchCriterion()
109 boolean compare(SequenceFeature entry)
111 return rc.compare(entry, to) >= 0;
118 * Non-positional features have no (zero) start/end position.
119 * Kept as a separate list in case this criterion changes in future.
121 List<SequenceFeature> nonPositionalFeatures;
124 * An ordered list of features, with the promise that no feature in the list
125 * properly contains any other. This constraint allows bounded linear search
126 * of the list for features overlapping a region.
127 * Contact features are not included in this list.
129 List<SequenceFeature> nonNestedFeatures;
132 * contact features ordered by first contact position
134 List<SequenceFeature> contactFeatureStarts;
137 * contact features ordered by second contact position
139 List<SequenceFeature> contactFeatureEnds;
142 * Nested Containment List is used to hold any features that are nested
143 * within (properly contained by) any other feature. This is a recursive tree
144 * which supports depth-first scan for features overlapping a range.
145 * It is used here as a 'catch-all' fallback for features that cannot be put
146 * into a simple ordered list without invalidating the search methods.
148 NCList<SequenceFeature> nestedFeatures;
151 * Feature groups represented in stored positional features
152 * (possibly including null)
154 Set<String> positionalFeatureGroups;
157 * Feature groups represented in stored non-positional features
158 * (possibly including null)
160 Set<String> nonPositionalFeatureGroups;
163 * the total length of all positional features; contact features count 1 to
164 * the total and 1 to size(), consistent with an average 'feature length' of 1
168 float positionalMinScore;
170 float positionalMaxScore;
172 float nonPositionalMinScore;
174 float nonPositionalMaxScore;
179 public FeatureStore()
181 nonNestedFeatures = new ArrayList<SequenceFeature>();
182 positionalFeatureGroups = new HashSet<String>();
183 nonPositionalFeatureGroups = new HashSet<String>();
184 positionalMinScore = Float.NaN;
185 positionalMaxScore = Float.NaN;
186 nonPositionalMinScore = Float.NaN;
187 nonPositionalMaxScore = Float.NaN;
189 // we only construct nonPositionalFeatures, contactFeatures
190 // or the NCList if we need to
194 * Adds one sequence feature to the store, and returns true, unless the
195 * feature is already contained in the store, in which case this method
196 * returns false. Containment is determined by SequenceFeature.equals()
201 public boolean addFeature(SequenceFeature feature)
203 if (contains(feature))
209 * keep a record of feature groups
211 if (!feature.isNonPositional())
213 positionalFeatureGroups.add(feature.getFeatureGroup());
216 boolean added = false;
218 if (feature.isContactFeature())
220 added = addContactFeature(feature);
222 else if (feature.isNonPositional())
224 added = addNonPositionalFeature(feature);
228 added = addNonNestedFeature(feature);
232 * detected a nested feature - put it in the NCList structure
234 added = addNestedFeature(feature);
241 * record the total extent of positional features, to make
242 * getTotalFeatureLength possible; we count the length of a
243 * contact feature as 1
245 totalExtent += getFeatureLength(feature);
248 * record the minimum and maximum score for positional
249 * and non-positional features
251 float score = feature.getScore();
252 if (!Float.isNaN(score))
254 if (feature.isNonPositional())
256 nonPositionalMinScore = min(nonPositionalMinScore, score);
257 nonPositionalMaxScore = max(nonPositionalMaxScore, score);
261 positionalMinScore = min(positionalMinScore, score);
262 positionalMaxScore = max(positionalMaxScore, score);
271 * Answers true if this store contains the given feature (testing by
272 * SequenceFeature.equals), else false
277 public boolean contains(SequenceFeature feature)
279 if (feature.isNonPositional())
281 return nonPositionalFeatures == null ? false : nonPositionalFeatures
285 if (feature.isContactFeature())
287 return contactFeatureStarts == null ? false : listContains(
288 contactFeatureStarts, feature);
291 if (listContains(nonNestedFeatures, feature))
296 return nestedFeatures == null ? false : nestedFeatures
301 * Answers the 'length' of the feature, counting 0 for non-positional features
302 * and 1 for contact features
307 protected static int getFeatureLength(SequenceFeature feature)
309 if (feature.isNonPositional())
313 if (feature.isContactFeature())
317 return 1 + feature.getEnd() - feature.getBegin();
321 * Adds the feature to the list of non-positional features (with lazy
322 * instantiation of the list if it is null), and returns true. The feature
323 * group is added to the set of distinct feature groups for non-positional
324 * features. This method allows duplicate features, so test before calling to
329 protected boolean addNonPositionalFeature(SequenceFeature feature)
331 if (nonPositionalFeatures == null)
333 nonPositionalFeatures = new ArrayList<SequenceFeature>();
336 nonPositionalFeatures.add(feature);
338 nonPositionalFeatureGroups.add(feature.getFeatureGroup());
344 * Adds one feature to the NCList that can manage nested features (creating
345 * the NCList if necessary), and returns true. If the feature is already
346 * stored in the NCList (by equality test), then it is not added, and this
347 * method returns false.
349 protected synchronized boolean addNestedFeature(SequenceFeature feature)
351 if (nestedFeatures == null)
353 nestedFeatures = new NCList<>(feature);
356 return nestedFeatures.add(feature, false);
360 * Add a feature to the list of non-nested features, maintaining the ordering
361 * of the list. A check is made for whether the feature is nested in (properly
362 * contained by) an existing feature. If there is no nesting, the feature is
363 * added to the list and the method returns true. If nesting is found, the
364 * feature is not added and the method returns false.
369 protected boolean addNonNestedFeature(SequenceFeature feature)
371 synchronized (nonNestedFeatures)
374 * find the first stored feature which doesn't precede the new one
376 int insertPosition = binarySearch(nonNestedFeatures,
377 SearchCriterion.byFeature(feature, RangeComparator.BY_START_POSITION));
380 * fail if we detect feature enclosure - of the new feature by
381 * the one preceding it, or of the next feature by the new one
383 if (insertPosition > 0)
385 if (encloses(nonNestedFeatures.get(insertPosition - 1), feature))
390 if (insertPosition < nonNestedFeatures.size())
392 if (encloses(feature, nonNestedFeatures.get(insertPosition)))
399 * checks passed - add the feature
401 nonNestedFeatures.add(insertPosition, feature);
408 * Answers true if range1 properly encloses range2, else false
414 protected boolean encloses(ContiguousI range1, ContiguousI range2)
416 int begin1 = range1.getBegin();
417 int begin2 = range2.getBegin();
418 int end1 = range1.getEnd();
419 int end2 = range2.getEnd();
420 if (begin1 == begin2 && end1 > end2)
424 if (begin1 < begin2 && end1 >= end2)
432 * Add a contact feature to the lists that hold them ordered by start (first
433 * contact) and by end (second contact) position, ensuring the lists remain
434 * ordered, and returns true. This method allows duplicate features to be
435 * added, so test before calling to avoid this.
440 protected synchronized boolean addContactFeature(SequenceFeature feature)
442 if (contactFeatureStarts == null)
444 contactFeatureStarts = new ArrayList<SequenceFeature>();
446 if (contactFeatureEnds == null)
448 contactFeatureEnds = new ArrayList<SequenceFeature>();
452 * binary search the sorted list to find the insertion point
454 int insertPosition = binarySearch(contactFeatureStarts,
455 SearchCriterion.byFeature(feature,
456 RangeComparator.BY_START_POSITION));
457 contactFeatureStarts.add(insertPosition, feature);
458 // and resort to mak siccar...just in case insertion point not quite right
459 Collections.sort(contactFeatureStarts, RangeComparator.BY_START_POSITION);
461 insertPosition = binarySearch(contactFeatureStarts,
462 SearchCriterion.byFeature(feature,
463 RangeComparator.BY_END_POSITION));
464 contactFeatureEnds.add(feature);
465 Collections.sort(contactFeatureEnds, RangeComparator.BY_END_POSITION);
471 * Answers true if the list contains the feature, else false. This method is
472 * optimised for the condition that the list is sorted on feature start
473 * position ascending, and will give unreliable results if this does not hold.
479 protected static boolean listContains(List<SequenceFeature> features,
480 SequenceFeature feature)
482 if (features == null || feature == null)
488 * locate the first entry in the list which does not precede the feature
490 int pos = binarySearch(features,
491 SearchCriterion.byFeature(feature, RangeComparator.BY_START_POSITION));
492 int len = features.size();
495 SequenceFeature sf = features.get(pos);
496 if (sf.getBegin() > feature.getBegin())
498 return false; // no match found
500 if (sf.equals(feature))
510 * Returns a (possibly empty) list of features whose extent overlaps the given
511 * range. The returned list is not ordered. Contact features are included if
512 * either of the contact points lies within the range.
515 * start position of overlap range (inclusive)
517 * end position of overlap range (inclusive)
520 public List<SequenceFeature> findOverlappingFeatures(long start, long end)
522 List<SequenceFeature> result = new ArrayList<>();
524 findNonNestedFeatures(start, end, result);
526 findContactFeatures(start, end, result);
528 if (nestedFeatures != null)
530 result.addAll(nestedFeatures.findOverlaps(start, end));
537 * Adds contact features to the result list where either the second or the
538 * first contact position lies within the target range
544 protected void findContactFeatures(long from, long to,
545 List<SequenceFeature> result)
547 if (contactFeatureStarts != null)
549 findContactStartFeatures(from, to, result);
551 if (contactFeatureEnds != null)
553 findContactEndFeatures(from, to, result);
558 * Adds to the result list any contact features whose end (second contact
559 * point), but not start (first contact point), lies in the query from-to
566 protected void findContactEndFeatures(long from, long to,
567 List<SequenceFeature> result)
570 * find the first contact feature (if any) that does not lie
571 * entirely before the target range
573 int startPosition = binarySearch(contactFeatureEnds,
574 SearchCriterion.byEnd(from));
575 for (; startPosition < contactFeatureEnds.size(); startPosition++)
577 SequenceFeature sf = contactFeatureEnds.get(startPosition);
578 if (!sf.isContactFeature())
580 System.err.println("Error! non-contact feature type "
581 + sf.getType() + " in contact features list");
585 int begin = sf.getBegin();
586 if (begin >= from && begin <= to)
589 * this feature's first contact position lies in the search range
590 * so we don't include it in results a second time
595 int end = sf.getEnd();
596 if (end >= from && end <= to)
608 * Adds non-nested features to the result list that lie within the target
609 * range. Non-positional features (start=end=0), contact features and nested
610 * features are excluded.
616 protected void findNonNestedFeatures(long from, long to,
617 List<SequenceFeature> result)
620 * find the first feature whose end position is
621 * after the target range start
623 int startIndex = binarySearch(nonNestedFeatures,
624 SearchCriterion.byEnd(from));
626 final int startIndex1 = startIndex;
628 while (i < nonNestedFeatures.size())
630 SequenceFeature sf = nonNestedFeatures.get(i);
631 if (sf.getBegin() > to)
635 if (sf.getBegin() <= to && sf.getEnd() >= from)
644 * Adds contact features whose start position lies in the from-to range to the
651 protected void findContactStartFeatures(long from, long to,
652 List<SequenceFeature> result)
654 int startPosition = binarySearch(contactFeatureStarts,
655 SearchCriterion.byStart(from));
657 for (; startPosition < contactFeatureStarts.size(); startPosition++)
659 SequenceFeature sf = contactFeatureStarts.get(startPosition);
660 if (!sf.isContactFeature())
662 System.err.println("Error! non-contact feature type "
663 + sf.getType() + " in contact features list");
666 int begin = sf.getBegin();
667 if (begin >= from && begin <= to)
675 * Answers a list of all positional features stored, in no guaranteed order
679 public List<SequenceFeature> getPositionalFeatures()
682 * add non-nested features (may be all features for many cases)
684 List<SequenceFeature> result = new ArrayList<>();
685 result.addAll(nonNestedFeatures);
688 * add any contact features - from the list by start position
690 if (contactFeatureStarts != null)
692 result.addAll(contactFeatureStarts);
696 * add any nested features
698 if (nestedFeatures != null)
700 result.addAll(nestedFeatures.getEntries());
707 * Answers a list of all contact features. If there are none, returns an
708 * immutable empty list.
712 public List<SequenceFeature> getContactFeatures()
714 if (contactFeatureStarts == null)
716 return Collections.emptyList();
718 return new ArrayList<>(contactFeatureStarts);
722 * Answers a list of all non-positional features. If there are none, returns
723 * an immutable empty list.
727 public List<SequenceFeature> getNonPositionalFeatures()
729 if (nonPositionalFeatures == null)
731 return Collections.emptyList();
733 return new ArrayList<>(nonPositionalFeatures);
737 * Deletes the given feature from the store, returning true if it was found
738 * (and deleted), else false. This method makes no assumption that the feature
739 * is in the 'expected' place in the store, in case it has been modified since
744 public synchronized boolean delete(SequenceFeature sf)
747 * try the non-nested positional features first
749 boolean removed = nonNestedFeatures.remove(sf);
752 * if not found, try contact positions (and if found, delete
753 * from both lists of contact positions)
755 if (!removed && contactFeatureStarts != null)
757 removed = contactFeatureStarts.remove(sf);
760 contactFeatureEnds.remove(sf);
764 boolean removedNonPositional = false;
767 * if not found, try non-positional features
769 if (!removed && nonPositionalFeatures != null)
771 removedNonPositional = nonPositionalFeatures.remove(sf);
772 removed = removedNonPositional;
776 * if not found, try nested features
778 if (!removed && nestedFeatures != null)
780 removed = nestedFeatures.delete(sf);
792 * Rescan all features to recompute any cached values after an entry has been
793 * deleted. This is expected to be an infrequent event, so performance here is
796 protected synchronized void rescanAfterDelete()
798 positionalFeatureGroups.clear();
799 nonPositionalFeatureGroups.clear();
801 positionalMinScore = Float.NaN;
802 positionalMaxScore = Float.NaN;
803 nonPositionalMinScore = Float.NaN;
804 nonPositionalMaxScore = Float.NaN;
807 * scan non-positional features for groups and scores
809 for (SequenceFeature sf : getNonPositionalFeatures())
811 nonPositionalFeatureGroups.add(sf.getFeatureGroup());
812 float score = sf.getScore();
813 nonPositionalMinScore = min(nonPositionalMinScore, score);
814 nonPositionalMaxScore = max(nonPositionalMaxScore, score);
818 * scan positional features for groups, scores and extents
820 for (SequenceFeature sf : getPositionalFeatures())
822 positionalFeatureGroups.add(sf.getFeatureGroup());
823 float score = sf.getScore();
824 positionalMinScore = min(positionalMinScore, score);
825 positionalMaxScore = max(positionalMaxScore, score);
826 totalExtent += getFeatureLength(sf);
831 * A helper method to return the minimum of two floats, where a non-NaN value
832 * is treated as 'less than' a NaN value (unlike Math.min which does the
838 protected static float min(float f1, float f2)
842 return Float.isNaN(f2) ? f1 : f2;
846 return Float.isNaN(f2) ? f1 : Math.min(f1, f2);
851 * A helper method to return the maximum of two floats, where a non-NaN value
852 * is treated as 'greater than' a NaN value (unlike Math.max which does the
858 protected static float max(float f1, float f2)
862 return Float.isNaN(f2) ? f1 : f2;
866 return Float.isNaN(f2) ? f1 : Math.max(f1, f2);
871 * Answers true if this store has no features, else false
875 public boolean isEmpty()
877 boolean hasFeatures = !nonNestedFeatures.isEmpty()
878 || (contactFeatureStarts != null && !contactFeatureStarts
880 || (nonPositionalFeatures != null && !nonPositionalFeatures
882 || (nestedFeatures != null && nestedFeatures.size() > 0);
888 * Answers the set of distinct feature groups stored, possibly including null,
889 * as an unmodifiable view of the set. The parameter determines whether the
890 * groups for positional or for non-positional features are returned.
892 * @param positionalFeatures
895 public Set<String> getFeatureGroups(boolean positionalFeatures)
897 if (positionalFeatures)
899 return Collections.unmodifiableSet(positionalFeatureGroups);
903 return nonPositionalFeatureGroups == null ? Collections
904 .<String> emptySet() : Collections
905 .unmodifiableSet(nonPositionalFeatureGroups);
910 * Performs a binary search of the (sorted) list to find the index of the
911 * first entry which returns true for the given comparator function. Returns
912 * the length of the list if there is no such entry.
918 protected static int binarySearch(List<SequenceFeature> features,
922 int end = features.size() - 1;
923 int matched = features.size();
927 int mid = (start + end) / 2;
928 SequenceFeature entry = features.get(mid);
929 boolean compare = sc.compare(entry);
945 * Answers the number of positional (or non-positional) features stored.
946 * Contact features count as 1.
951 public int getFeatureCount(boolean positional)
955 return nonPositionalFeatures == null ? 0 : nonPositionalFeatures
959 int size = nonNestedFeatures.size();
961 if (contactFeatureStarts != null)
963 // note a contact feature (start/end) counts as one
964 size += contactFeatureStarts.size();
967 if (nestedFeatures != null)
969 size += nestedFeatures.size();
976 * Answers the total length of positional features (or zero if there are
977 * none). Contact features contribute a value of 1 to the total.
981 public int getTotalFeatureLength()
987 * Answers the minimum score held for positional or non-positional features.
988 * This may be Float.NaN if there are no features, are none has a non-NaN
994 public float getMinimumScore(boolean positional)
996 return positional ? positionalMinScore : nonPositionalMinScore;
1000 * Answers the maximum score held for positional or non-positional features.
1001 * This may be Float.NaN if there are no features, are none has a non-NaN
1007 public float getMaximumScore(boolean positional)
1009 return positional ? positionalMaxScore : nonPositionalMaxScore;
1013 * Answers a list of all either positional or non-positional features whose
1014 * feature group matches the given group (which may be null)
1020 public List<SequenceFeature> getFeaturesForGroup(boolean positional,
1023 List<SequenceFeature> result = new ArrayList<>();
1026 * if we know features don't include the target group, no need
1027 * to inspect them for matches
1029 if (positional && !positionalFeatureGroups.contains(group)
1030 || !positional && !nonPositionalFeatureGroups.contains(group))
1035 List<SequenceFeature> sfs = positional ? getPositionalFeatures()
1036 : getNonPositionalFeatures();
1037 for (SequenceFeature sf : sfs)
1039 String featureGroup = sf.getFeatureGroup();
1040 if (group == null && featureGroup == null || group != null
1041 && group.equals(featureGroup))
1050 * Adds the shift value to the start and end of all positional features.
1051 * Returns true if at least one feature was updated, else false.
1056 public synchronized boolean shiftFeatures(int shift)
1059 * Because begin and end are final fields (to ensure the data store's
1060 * integrity), we have to delete each feature and re-add it as amended.
1061 * (Although a simple shift of all values would preserve data integrity!)
1063 boolean modified = false;
1064 for (SequenceFeature sf : getPositionalFeatures())
1067 int newBegin = sf.getBegin() + shift;
1068 int newEnd = sf.getEnd() + shift;
1071 * sanity check: don't shift left of the first residue
1075 newBegin = Math.max(1, newBegin);
1076 SequenceFeature sf2 = new SequenceFeature(sf, newBegin, newEnd,
1077 sf.getFeatureGroup(), sf.getScore());