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 java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.HashSet;
26 import java.util.List;
29 import intervalstore.api.IntervalStoreI;
30 import intervalstore.impl.BinarySearcher;
31 import intervalstore.impl.BinarySearcher.Compare;
32 import intervalstore.impl.IntervalStore;
33 import jalview.datamodel.SequenceFeature;
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
86 * range covered by positional features
88 int minExtent, maxExtent;
90 float positionalMinScore;
92 float positionalMaxScore;
94 float nonPositionalMinScore;
96 float nonPositionalMaxScore;
101 public FeatureStore()
103 features = new IntervalStore<>();
104 positionalFeatureGroups = new HashSet<>();
105 nonPositionalFeatureGroups = new HashSet<>();
106 positionalMinScore = Float.NaN;
107 positionalMaxScore = Float.NaN;
108 nonPositionalMinScore = Float.NaN;
109 nonPositionalMaxScore = Float.NaN;
113 // we only construct nonPositionalFeatures, contactFeatures if we need to
117 * Adds one sequence feature to the store, and returns true, unless the
118 * feature is already contained in the store, in which case this method
119 * returns false. Containment is determined by SequenceFeature.equals()
124 public boolean addFeature(SequenceFeature feature)
126 if (contains(feature))
132 * keep a record of feature groups
134 if (!feature.isNonPositional())
136 positionalFeatureGroups.add(feature.getFeatureGroup());
139 minExtent = feature.begin;
140 maxExtent = feature.end;
144 minExtent = Math.min(feature.begin, minExtent);
145 maxExtent = Math.max(feature.end, maxExtent);
149 if (feature.isContactFeature())
151 addContactFeature(feature);
153 else if (feature.isNonPositional())
155 addNonPositionalFeature(feature);
159 addNestedFeature(feature);
163 * record the total extent of positional features, to make
164 * getTotalFeatureLength possible; we count the length of a
165 * contact feature as 1
167 totalExtent += getFeatureLength(feature);
170 * record the minimum and maximum score for positional
171 * and non-positional features
173 float score = feature.getScore();
174 if (!Float.isNaN(score))
176 if (feature.isNonPositional())
178 nonPositionalMinScore = min(nonPositionalMinScore, score);
179 nonPositionalMaxScore = max(nonPositionalMaxScore, score);
183 positionalMinScore = min(positionalMinScore, score);
184 positionalMaxScore = max(positionalMaxScore, score);
192 * Answers true if this store contains the given feature (testing by
193 * SequenceFeature.equals), else false
198 public boolean contains(SequenceFeature feature)
200 if (feature.isNonPositional())
202 return nonPositionalFeatures == null ? false
203 : nonPositionalFeatures.contains(feature);
206 if (feature.isContactFeature())
208 return contactFeatureStarts == null ? false
209 : listContains(contactFeatureStarts, feature);
212 return features == null ? false : features.contains(feature);
216 * Answers the 'length' of the feature, counting 0 for non-positional features
217 * and 1 for contact features
222 protected static int getFeatureLength(SequenceFeature feature)
224 if (feature.isNonPositional())
228 if (feature.isContactFeature())
232 return 1 + feature.getEnd() - feature.getBegin();
236 * Adds the feature to the list of non-positional features (with lazy
237 * instantiation of the list if it is null), and returns true. The feature
238 * group is added to the set of distinct feature groups for non-positional
239 * features. This method allows duplicate features, so test before calling to
244 protected boolean addNonPositionalFeature(SequenceFeature feature)
246 if (nonPositionalFeatures == null)
248 nonPositionalFeatures = new ArrayList<>();
251 nonPositionalFeatures.add(feature);
253 nonPositionalFeatureGroups.add(feature.getFeatureGroup());
259 * Adds one feature to the IntervalStore that can manage nested features
260 * (creating the IntervalStore if necessary)
262 protected synchronized void addNestedFeature(SequenceFeature feature)
264 if (features == null)
266 features = new IntervalStore<>();
268 features.add(feature);
272 * Add a contact feature to the lists that hold them ordered by start (first
273 * contact) and by end (second contact) position, ensuring the lists remain
274 * ordered, and returns true. This method allows duplicate features to be
275 * added, so test before calling to avoid this.
280 protected synchronized boolean addContactFeature(SequenceFeature feature)
282 if (contactFeatureStarts == null)
284 contactFeatureStarts = new ArrayList<>();
286 if (contactFeatureEnds == null)
288 contactFeatureEnds = new ArrayList<>();
292 * insert into list sorted by start (first contact position):
293 * binary search the sorted list to find the insertion point
295 int insertPosition = BinarySearcher.findFirst(contactFeatureStarts,
296 true, Compare.GE, feature.getBegin());
297 contactFeatureStarts.add(insertPosition, feature);
300 * insert into list sorted by end (second contact position):
301 * binary search the sorted list to find the insertion point
303 insertPosition = BinarySearcher.findFirst(contactFeatureEnds, false,
304 Compare.GE, feature.getEnd());
305 contactFeatureEnds.add(insertPosition, feature);
311 * Answers true if the list contains the feature, else false. This method is
312 * optimised for the condition that the list is sorted on feature start
313 * position ascending, and will give unreliable results if this does not hold.
319 protected static boolean listContains(List<SequenceFeature> features,
320 SequenceFeature feature)
322 if (features == null || feature == null)
328 * locate the first entry in the list which does not precede the feature
330 // int pos = binarySearch(features,
331 // SearchCriterion.byFeature(feature, RangeComparator.BY_START_POSITION));
332 int pos = BinarySearcher.findFirst(features, true, Compare.GE,
334 int len = features.size();
337 SequenceFeature sf = features.get(pos);
338 if (sf.getBegin() > feature.getBegin())
340 return false; // no match found
342 if (sf.equals(feature))
352 * Returns a (possibly empty) list of features whose extent overlaps the given
353 * range. The returned list is not ordered. Contact features are included if
354 * either of the contact points lies within the range.
357 * start position of overlap range (inclusive)
359 * end position of overlap range (inclusive)
362 public List<SequenceFeature> findOverlappingFeatures(long start, long end)
364 List<SequenceFeature> result = new ArrayList<>();
366 findContactFeatures(start, end, result);
368 if (features != null)
370 result.addAll(features.findOverlaps(start, end));
377 * Adds contact features to the result list where either the second or the
378 * first contact position lies within the target range
384 protected void findContactFeatures(long from, long to,
385 List<SequenceFeature> result)
387 if (contactFeatureStarts != null)
389 findContactStartOverlaps(from, to, result);
391 if (contactFeatureEnds != null)
393 findContactEndOverlaps(from, to, result);
398 * Adds to the result list any contact features whose end (second contact
399 * point), but not start (first contact point), lies in the query from-to
406 protected void findContactEndOverlaps(long from, long to,
407 List<SequenceFeature> result)
410 * find the first contact feature (if any)
411 * whose end point is not before the target range
413 int index = BinarySearcher.findFirst(contactFeatureEnds, false,
414 Compare.GE, (int) from);
416 while (index < contactFeatureEnds.size())
418 SequenceFeature sf = contactFeatureEnds.get(index);
419 if (!sf.isContactFeature())
421 System.err.println("Error! non-contact feature type " + sf.getType()
422 + " in contact features list");
427 int begin = sf.getBegin();
428 if (begin >= from && begin <= to)
431 * this feature's first contact position lies in the search range
432 * so we don't include it in results a second time
438 if (sf.getEnd() > to)
441 * this feature (and all following) has end point after the target range
447 * feature has end >= from and end <= to
448 * i.e. contact end point lies within overlap search range
456 * Adds contact features whose start position lies in the from-to range to the
463 protected void findContactStartOverlaps(long from, long to,
464 List<SequenceFeature> result)
466 int index = BinarySearcher.findFirst(contactFeatureStarts, true,
467 Compare.GE, (int) from);
469 while (index < contactFeatureStarts.size())
471 SequenceFeature sf = contactFeatureStarts.get(index);
472 if (!sf.isContactFeature())
474 System.err.println("Error! non-contact feature " + sf.toString()
475 + " in contact features list");
479 if (sf.getBegin() > to)
482 * this feature's start (and all following) follows the target range
488 * feature has begin >= from and begin <= to
489 * i.e. contact start point lies within overlap search range
497 * Answers a list of all positional features stored, in no guaranteed order
501 public List<SequenceFeature> getPositionalFeatures()
503 List<SequenceFeature> result = new ArrayList<>();
506 * add any contact features - from the list by start position
508 if (contactFeatureStarts != null)
510 result.addAll(contactFeatureStarts);
514 * add any nested features
516 if (features != null)
518 result.addAll(features);
525 * Answers a list of all contact features. If there are none, returns an
526 * immutable empty list.
530 public List<SequenceFeature> getContactFeatures()
532 if (contactFeatureStarts == null)
534 return Collections.emptyList();
536 return new ArrayList<>(contactFeatureStarts);
540 * Answers a list of all non-positional features. If there are none, returns
541 * an immutable empty list.
545 public List<SequenceFeature> getNonPositionalFeatures()
547 if (nonPositionalFeatures == null)
549 return Collections.emptyList();
551 return new ArrayList<>(nonPositionalFeatures);
555 * Deletes the given feature from the store, returning true if it was found
556 * (and deleted), else false. This method makes no assumption that the feature
557 * is in the 'expected' place in the store, in case it has been modified since
562 public synchronized boolean delete(SequenceFeature sf)
564 boolean removed = false;
567 * try contact positions (and if found, delete
568 * from both lists of contact positions)
570 if (!removed && contactFeatureStarts != null)
572 removed = contactFeatureStarts.remove(sf);
575 contactFeatureEnds.remove(sf);
579 boolean removedNonPositional = false;
582 * if not found, try non-positional features
584 if (!removed && nonPositionalFeatures != null)
586 removedNonPositional = nonPositionalFeatures.remove(sf);
587 removed = removedNonPositional;
591 * if not found, try nested features
593 if (!removed && features != null)
595 removed = features.remove(sf);
607 * Rescan all features to recompute any cached values after an entry has been
608 * deleted. This is expected to be an infrequent event, so performance here is
611 protected synchronized void rescanAfterDelete()
613 positionalFeatureGroups.clear();
614 nonPositionalFeatureGroups.clear();
616 positionalMinScore = Float.NaN;
617 positionalMaxScore = Float.NaN;
618 nonPositionalMinScore = Float.NaN;
619 nonPositionalMaxScore = Float.NaN;
622 * scan non-positional features for groups and scores
624 for (SequenceFeature sf : getNonPositionalFeatures())
626 nonPositionalFeatureGroups.add(sf.getFeatureGroup());
627 float score = sf.getScore();
628 nonPositionalMinScore = min(nonPositionalMinScore, score);
629 nonPositionalMaxScore = max(nonPositionalMaxScore, score);
633 * scan positional features for groups, scores and extents
635 for (SequenceFeature sf : getPositionalFeatures())
637 positionalFeatureGroups.add(sf.getFeatureGroup());
638 float score = sf.getScore();
639 positionalMinScore = min(positionalMinScore, score);
640 positionalMaxScore = max(positionalMaxScore, score);
641 if (totalExtent == 0)
643 minExtent = sf.begin;
644 maxExtent = sf.begin;
648 minExtent = Math.min(sf.begin, minExtent);
649 maxExtent = Math.max(sf.end, maxExtent);
651 totalExtent += getFeatureLength(sf);
656 * A helper method to return the minimum of two floats, where a non-NaN value
657 * is treated as 'less than' a NaN value (unlike Math.min which does the
663 protected static float min(float f1, float f2)
667 return Float.isNaN(f2) ? f1 : f2;
671 return Float.isNaN(f2) ? f1 : Math.min(f1, f2);
676 * A helper method to return the maximum of two floats, where a non-NaN value
677 * is treated as 'greater than' a NaN value (unlike Math.max which does the
683 protected static float max(float f1, float f2)
687 return Float.isNaN(f2) ? f1 : f2;
691 return Float.isNaN(f2) ? f1 : Math.max(f1, f2);
696 * Answers true if this store has no features, else false
700 public boolean isEmpty()
702 boolean hasFeatures = (contactFeatureStarts != null
703 && !contactFeatureStarts.isEmpty())
704 || (nonPositionalFeatures != null
705 && !nonPositionalFeatures.isEmpty())
706 || (features != null && features.size() > 0);
712 * Answers the set of distinct feature groups stored, possibly including null,
713 * as an unmodifiable view of the set. The parameter determines whether the
714 * groups for positional or for non-positional features are returned.
716 * @param positionalFeatures
719 public Set<String> getFeatureGroups(boolean positionalFeatures)
721 if (positionalFeatures)
723 return Collections.unmodifiableSet(positionalFeatureGroups);
727 return nonPositionalFeatureGroups == null
728 ? Collections.<String> emptySet()
729 : Collections.unmodifiableSet(nonPositionalFeatureGroups);
734 * Answers the number of positional (or non-positional) features stored.
735 * Contact features count as 1.
740 public int getFeatureCount(boolean positional)
744 return nonPositionalFeatures == null ? 0
745 : nonPositionalFeatures.size();
750 if (contactFeatureStarts != null)
752 // note a contact feature (start/end) counts as one
753 size += contactFeatureStarts.size();
756 if (features != null)
758 size += features.size();
765 * Answers the total length of positional features (or zero if there are
766 * none). Contact features contribute a value of 1 to the total.
770 public int getTotalFeatureLength()
776 * Answers the minimum score held for positional or non-positional features.
777 * This may be Float.NaN if there are no features, are none has a non-NaN
783 public float getMinimumScore(boolean positional)
785 return positional ? positionalMinScore : nonPositionalMinScore;
789 * Answers the maximum score held for positional or non-positional features.
790 * This may be Float.NaN if there are no features, are none has a non-NaN
796 public float getMaximumScore(boolean positional)
798 return positional ? positionalMaxScore : nonPositionalMaxScore;
802 * Answers a list of all either positional or non-positional features whose
803 * feature group matches the given group (which may be null)
809 public List<SequenceFeature> getFeaturesForGroup(boolean positional,
812 List<SequenceFeature> result = new ArrayList<>();
815 * if we know features don't include the target group, no need
816 * to inspect them for matches
818 if (positional && !positionalFeatureGroups.contains(group)
819 || !positional && !nonPositionalFeatureGroups.contains(group))
824 List<SequenceFeature> sfs = positional ? getPositionalFeatures()
825 : getNonPositionalFeatures();
826 for (SequenceFeature sf : sfs)
828 String featureGroup = sf.getFeatureGroup();
829 if (group == null && featureGroup == null
830 || group != null && group.equals(featureGroup))
839 * Adds the shift amount to the start and end of all positional features whose
840 * start position is at or after fromPosition. Returns true if at least one
841 * feature was shifted, else false.
843 * @param fromPosition
847 public synchronized boolean shiftFeatures(int fromPosition, int shiftBy)
850 * Because begin and end are final fields (to ensure the data store's
851 * integrity), we have to delete each feature and re-add it as amended.
852 * (Although a simple shift of all values would preserve data integrity!)
854 boolean modified = false;
855 for (SequenceFeature sf : getPositionalFeatures())
857 if (sf.getBegin() >= fromPosition)
860 int newBegin = sf.getBegin() + shiftBy;
861 int newEnd = sf.getEnd() + shiftBy;
864 * sanity check: don't shift left of the first residue
868 newBegin = Math.max(1, newBegin);
869 SequenceFeature sf2 = new SequenceFeature(sf, newBegin, newEnd,
870 sf.getFeatureGroup(), sf.getScore());
879 public int getBegin()