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.SequenceFeature;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.HashSet;
28 import java.util.List;
31 import intervalstore.api.IntervalStoreI;
32 import intervalstore.impl.BinarySearcher;
33 import intervalstore.impl.IntervalStore;
36 * A data store for a set of sequence features that supports efficient lookup of
37 * features overlapping a given range. Intended for (but not limited to) storage
38 * of features for one sequence and feature type.
43 public class FeatureStore
46 * Non-positional features have no (zero) start/end position.
47 * Kept as a separate list in case this criterion changes in future.
49 List<SequenceFeature> nonPositionalFeatures;
52 * contact features ordered by first contact position
54 List<SequenceFeature> contactFeatureStarts;
57 * contact features ordered by second contact position
59 List<SequenceFeature> contactFeatureEnds;
62 * IntervalStore holds remaining features and provides efficient
63 * query for features overlapping any given interval
65 IntervalStoreI<SequenceFeature> features;
68 * Feature groups represented in stored positional features
69 * (possibly including null)
71 Set<String> positionalFeatureGroups;
74 * Feature groups represented in stored non-positional features
75 * (possibly including null)
77 Set<String> nonPositionalFeatureGroups;
80 * the total length of all positional features; contact features count 1 to
81 * the total and 1 to size(), consistent with an average 'feature length' of 1
85 float positionalMinScore;
87 float positionalMaxScore;
89 float nonPositionalMinScore;
91 float nonPositionalMaxScore;
93 private SequenceFeature[] temp = new SequenceFeature[3];
95 private boolean isTainted;
100 public FeatureStore()
102 features = new IntervalStore<>();
103 positionalFeatureGroups = new HashSet<>();
104 nonPositionalFeatureGroups = new HashSet<>();
105 positionalMinScore = Float.NaN;
106 positionalMaxScore = Float.NaN;
107 nonPositionalMinScore = Float.NaN;
108 nonPositionalMaxScore = Float.NaN;
110 // we only construct nonPositionalFeatures, contactFeatures if we need to
114 * Adds one sequence feature to the store, and returns true, unless the
115 * feature is already contained in the store, in which case this method
116 * returns false. Containment is determined by SequenceFeature.equals()
121 public boolean addFeature(SequenceFeature feature)
123 if (contains(feature))
129 * keep a record of feature groups
131 if (!feature.isNonPositional())
133 positionalFeatureGroups.add(feature.getFeatureGroup());
136 if (feature.isContactFeature())
138 addContactFeature(feature);
140 else if (feature.isNonPositional())
142 addNonPositionalFeature(feature);
146 addNestedFeature(feature);
150 * record the total extent of positional features, to make
151 * getTotalFeatureLength possible; we count the length of a
152 * contact feature as 1
154 totalExtent += getFeatureLength(feature);
157 * record the minimum and maximum score for positional
158 * and non-positional features
160 float score = feature.getScore();
161 if (!Float.isNaN(score))
163 if (feature.isNonPositional())
165 nonPositionalMinScore = min(nonPositionalMinScore, score);
166 nonPositionalMaxScore = max(nonPositionalMaxScore, score);
170 positionalMinScore = min(positionalMinScore, score);
171 positionalMaxScore = max(positionalMaxScore, score);
179 * Answers true if this store contains the given feature (testing by
180 * SequenceFeature.equals), else false
185 public boolean contains(SequenceFeature feature)
187 if (feature.isNonPositional())
189 return nonPositionalFeatures == null ? false : nonPositionalFeatures
193 if (feature.isContactFeature())
195 return contactFeatureStarts == null ? false : listContains(
196 contactFeatureStarts, feature);
199 return features == null ? false : features
204 * Answers the 'length' of the feature, counting 0 for non-positional features
205 * and 1 for contact features
210 protected static int getFeatureLength(SequenceFeature feature)
212 if (feature.isNonPositional())
216 if (feature.isContactFeature())
220 return 1 + feature.getEnd() - feature.getBegin();
224 * Adds the feature to the list of non-positional features (with lazy
225 * instantiation of the list if it is null), and returns true. The feature
226 * group is added to the set of distinct feature groups for non-positional
227 * features. This method allows duplicate features, so test before calling to
232 protected boolean addNonPositionalFeature(SequenceFeature feature)
234 if (nonPositionalFeatures == null)
236 nonPositionalFeatures = new ArrayList<>();
239 nonPositionalFeatures.add(feature);
241 nonPositionalFeatureGroups.add(feature.getFeatureGroup());
247 * Adds one feature to the IntervalStore that can manage nested features
248 * (creating the IntervalStore if necessary)
250 protected synchronized void addNestedFeature(SequenceFeature feature)
252 if (features == null)
254 features = new IntervalStore<>();
256 features.add(feature);
261 * Add a contact feature to the lists that hold them ordered by start (first
262 * contact) and by end (second contact) position, ensuring the lists remain
263 * ordered, and returns true. This method allows duplicate features to be
264 * added, so test before calling to avoid this.
269 protected synchronized boolean addContactFeature(SequenceFeature feature)
271 if (contactFeatureStarts == null)
273 contactFeatureStarts = new ArrayList<>();
275 if (contactFeatureEnds == null)
277 contactFeatureEnds = new ArrayList<>();
281 * insert into list sorted by start (first contact position):
282 * binary search the sorted list to find the insertion point
284 int insertPosition = BinarySearcher.findFirst(contactFeatureStarts,
285 f -> f.getBegin() >= feature.getBegin());
286 contactFeatureStarts.add(insertPosition, feature);
290 * insert into list sorted by end (second contact position):
291 * binary search the sorted list to find the insertion point
293 insertPosition = BinarySearcher.findFirst(contactFeatureEnds,
294 f -> f.getEnd() >= feature.getEnd());
295 contactFeatureEnds.add(insertPosition, feature);
301 * Answers true if the list contains the feature, else false. This method is
302 * optimised for the condition that the list is sorted on feature start
303 * position ascending, and will give unreliable results if this does not hold.
309 protected static boolean listContains(List<SequenceFeature> features,
310 SequenceFeature feature)
312 if (features == null || feature == null)
318 * locate the first entry in the list which does not precede the feature
320 // int pos = binarySearch(features,
321 // SearchCriterion.byFeature(feature, RangeComparator.BY_START_POSITION));
322 int pos = BinarySearcher.findFirst(features,
323 val -> val.getBegin() >= feature.getBegin());
324 int len = features.size();
327 SequenceFeature sf = features.get(pos);
328 if (sf.getBegin() > feature.getBegin())
330 return false; // no match found
332 if (sf.equals(feature))
342 * Returns a (possibly empty) list of features whose extent overlaps the given
343 * range. The returned list is not ordered. Contact features are included if
344 * either of the contact points lies within the range.
347 * start position of overlap range (inclusive)
349 * end position of overlap range (inclusive)
352 public List<SequenceFeature> findOverlappingFeatures(long start, long end)
354 List<SequenceFeature> result = new ArrayList<>();
356 findContactFeatures(start, end, result);
358 if (features != null)
360 result.addAll(features.findOverlaps(start, end));
367 * Adds contact features to the result list where either the second or the
368 * first contact position lies within the target range
374 protected void findContactFeatures(long from, long to,
375 List<SequenceFeature> result)
377 if (contactFeatureStarts != null)
379 findContactStartOverlaps(from, to, result);
381 if (contactFeatureEnds != null)
383 findContactEndOverlaps(from, to, result);
388 * Adds to the result list any contact features whose end (second contact
389 * point), but not start (first contact point), lies in the query from-to
396 protected void findContactEndOverlaps(long from, long to,
397 List<SequenceFeature> result)
400 * find the first contact feature (if any)
401 * whose end point is not before the target range
403 int index = BinarySearcher.findFirst(contactFeatureEnds,
404 f -> f.getEnd() >= from);
406 while (index < contactFeatureEnds.size())
408 SequenceFeature sf = contactFeatureEnds.get(index);
409 if (!sf.isContactFeature())
411 System.err.println("Error! non-contact feature type "
412 + sf.getType() + " in contact features list");
417 int begin = sf.getBegin();
418 if (begin >= from && begin <= to)
421 * this feature's first contact position lies in the search range
422 * so we don't include it in results a second time
428 if (sf.getEnd() > to)
431 * this feature (and all following) has end point after the target range
437 * feature has end >= from and end <= to
438 * i.e. contact end point lies within overlap search range
446 * Adds contact features whose start position lies in the from-to range to the
453 protected void findContactStartOverlaps(long from, long to,
454 List<SequenceFeature> result)
456 int index = BinarySearcher.findFirst(contactFeatureStarts,
457 f -> f.getBegin() >= from);
459 while (index < contactFeatureStarts.size())
461 SequenceFeature sf = contactFeatureStarts.get(index);
462 if (!sf.isContactFeature())
464 System.err.println("Error! non-contact feature " + sf.toString()
465 + " in contact features list");
469 if (sf.getBegin() > to)
472 * this feature's start (and all following) follows the target range
478 * feature has begin >= from and begin <= to
479 * i.e. contact start point lies within overlap search range
487 * Answers a list of all positional features stored, in no guaranteed order
491 public List<SequenceFeature> getPositionalFeatures()
493 List<SequenceFeature> result = new ArrayList<>();
496 * add any contact features - from the list by start position
498 if (contactFeatureStarts != null)
500 result.addAll(contactFeatureStarts);
504 * add any nested features
506 if (features != null)
508 result.addAll(features);
515 * Answers a list of all contact features. If there are none, returns an
516 * immutable empty list.
520 public List<SequenceFeature> getContactFeatures()
522 if (contactFeatureStarts == null)
524 return Collections.emptyList();
526 return new ArrayList<>(contactFeatureStarts);
530 * Answers a list of all non-positional features. If there are none, returns
531 * an immutable empty list.
535 public List<SequenceFeature> getNonPositionalFeatures()
537 if (nonPositionalFeatures == null)
539 return Collections.emptyList();
541 return new ArrayList<>(nonPositionalFeatures);
545 * Deletes the given feature from the store, returning true if it was found
546 * (and deleted), else false. This method makes no assumption that the feature
547 * is in the 'expected' place in the store, in case it has been modified since
552 public synchronized boolean delete(SequenceFeature sf)
554 boolean removed = false;
557 * try contact positions (and if found, delete
558 * from both lists of contact positions)
560 if (!removed && contactFeatureStarts != null)
562 removed = contactFeatureStarts.remove(sf);
565 contactFeatureEnds.remove(sf);
569 boolean removedNonPositional = false;
572 * if not found, try non-positional features
574 if (!removed && nonPositionalFeatures != null)
576 removedNonPositional = nonPositionalFeatures.remove(sf);
577 removed = removedNonPositional;
581 * if not found, try nested features
583 if (!removed && features != null)
585 removed = features.remove(sf);
597 * Rescan all features to recompute any cached values after an entry has been
598 * deleted. This is expected to be an infrequent event, so performance here is
601 protected synchronized void rescanAfterDelete()
603 positionalFeatureGroups.clear();
604 nonPositionalFeatureGroups.clear();
606 positionalMinScore = Float.NaN;
607 positionalMaxScore = Float.NaN;
608 nonPositionalMinScore = Float.NaN;
609 nonPositionalMaxScore = Float.NaN;
612 * scan non-positional features for groups and scores
614 for (SequenceFeature sf : getNonPositionalFeatures())
616 nonPositionalFeatureGroups.add(sf.getFeatureGroup());
617 float score = sf.getScore();
618 nonPositionalMinScore = min(nonPositionalMinScore, score);
619 nonPositionalMaxScore = max(nonPositionalMaxScore, score);
623 * scan positional features for groups, scores and extents
625 for (SequenceFeature sf : getPositionalFeatures())
627 positionalFeatureGroups.add(sf.getFeatureGroup());
628 float score = sf.getScore();
629 positionalMinScore = min(positionalMinScore, score);
630 positionalMaxScore = max(positionalMaxScore, score);
631 totalExtent += getFeatureLength(sf);
636 * A helper method to return the minimum of two floats, where a non-NaN value
637 * is treated as 'less than' a NaN value (unlike Math.min which does the
643 protected static float min(float f1, float f2)
647 return Float.isNaN(f2) ? f1 : f2;
651 return Float.isNaN(f2) ? f1 : Math.min(f1, f2);
656 * A helper method to return the maximum of two floats, where a non-NaN value
657 * is treated as 'greater than' a NaN value (unlike Math.max which does the
663 protected static float max(float f1, float f2)
667 return Float.isNaN(f2) ? f1 : f2;
671 return Float.isNaN(f2) ? f1 : Math.max(f1, f2);
676 * Answers true if this store has no features, else false
680 public boolean isEmpty()
682 boolean hasFeatures = (contactFeatureStarts != null
683 && !contactFeatureStarts
685 || (nonPositionalFeatures != null && !nonPositionalFeatures
687 || (features != null && features.size() > 0);
693 * Answers the set of distinct feature groups stored, possibly including null,
694 * as an unmodifiable view of the set. The parameter determines whether the
695 * groups for positional or for non-positional features are returned.
697 * @param positionalFeatures
700 public Set<String> getFeatureGroups(boolean positionalFeatures)
702 if (positionalFeatures)
704 return Collections.unmodifiableSet(positionalFeatureGroups);
708 return nonPositionalFeatureGroups == null ? Collections
709 .<String> emptySet() : Collections
710 .unmodifiableSet(nonPositionalFeatureGroups);
715 * Answers the number of positional (or non-positional) features stored.
716 * Contact features count as 1.
721 public int getFeatureCount(boolean positional)
725 return nonPositionalFeatures == null ? 0 : nonPositionalFeatures
731 if (contactFeatureStarts != null)
733 // note a contact feature (start/end) counts as one
734 size += contactFeatureStarts.size();
737 if (features != null)
739 size += features.size();
746 * Answers the total length of positional features (or zero if there are
747 * none). Contact features contribute a value of 1 to the total.
751 public int getTotalFeatureLength()
757 * Answers the minimum score held for positional or non-positional features.
758 * This may be Float.NaN if there are no features, are none has a non-NaN
764 public float getMinimumScore(boolean positional)
766 return positional ? positionalMinScore : nonPositionalMinScore;
770 * Answers the maximum score held for positional or non-positional features.
771 * This may be Float.NaN if there are no features, are none has a non-NaN
777 public float getMaximumScore(boolean positional)
779 return positional ? positionalMaxScore : nonPositionalMaxScore;
783 * Answers a list of all either positional or non-positional features whose
784 * feature group matches the given group (which may be null)
790 public List<SequenceFeature> getFeaturesForGroup(boolean positional,
793 List<SequenceFeature> result = new ArrayList<>();
796 * if we know features don't include the target group, no need
797 * to inspect them for matches
799 if (positional && !positionalFeatureGroups.contains(group)
800 || !positional && !nonPositionalFeatureGroups.contains(group))
805 List<SequenceFeature> sfs = positional ? getPositionalFeatures()
806 : getNonPositionalFeatures();
807 for (SequenceFeature sf : sfs)
809 String featureGroup = sf.getFeatureGroup();
810 if (group == null && featureGroup == null || group != null
811 && group.equals(featureGroup))
820 * Adds the shift amount to the start and end of all positional features whose
821 * start position is at or after fromPosition. Returns true if at least one
822 * feature was shifted, else false.
824 * @param fromPosition
828 public synchronized boolean shiftFeatures(int fromPosition, int shiftBy)
831 * Because begin and end are final fields (to ensure the data store's
832 * integrity), we have to delete each feature and re-add it as amended.
833 * (Although a simple shift of all values would preserve data integrity!)
835 boolean modified = false;
836 for (SequenceFeature sf : getPositionalFeatures())
838 if (sf.getBegin() >= fromPosition)
841 int newBegin = sf.getBegin() + shiftBy;
842 int newEnd = sf.getEnd() + shiftBy;
845 * sanity check: don't shift left of the first residue
849 newBegin = Math.max(1, newBegin);
850 SequenceFeature sf2 = new SequenceFeature(sf, newBegin, newEnd,
851 sf.getFeatureGroup(), sf.getScore());
861 * Find all features containing this position.
862 * Uses isTainted field to know when to reconstruct its temporary array.
865 * @return list of SequenceFeatures
866 * @author Bob Hanson 2019.07.30
868 public void findOverlappingFeatures(int pos, List<SequenceFeature> result)
871 if (contactFeatureStarts != null)
873 findContacts(contactFeatureStarts, pos, result, true);
874 findContacts(contactFeatureEnds, pos, result, false);
876 if (features != null)
878 int n = features.size();
884 temp = new SequenceFeature[n << 1];
886 features.toArray(temp);
888 findOverlaps(temp, n, pos, result);
893 * Binary search for contact start or end at a given (Overview) position.
900 * @author Bob Hanson 2019.07.30
902 private static void findContacts(List<SequenceFeature> l, int pos,
903 List<SequenceFeature> result, boolean isStart)
906 int high = l.size() - 1;
909 int mid = (low + high) >>> 1;
910 SequenceFeature f = l.get(mid);
911 switch (Long.signum((isStart ? f.begin : f.end) - pos))
922 // could be "5" in 12345556788 ?
923 while (++mid <= high && (f = l.get(mid)) != null
924 && (isStart ? f.begin : f.end) == pos)
928 while (--m >= low && (f = l.get(m)) != null
929 && (isStart ? f.begin : f.end) == pos)
939 * Brute force point-interval overlap test
946 private static void findOverlaps(SequenceFeature[] features, int n,
948 List<SequenceFeature> result)
950 // BH I know, brute force. We need a single-position overlap
951 // method for IntervalStore, I think.
952 for (int i = n; --i >= 0;)
954 SequenceFeature f = features[i];
955 if (f.begin <= pos && f.end >= pos)