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.Arrays;
27 import java.util.BitSet;
28 import java.util.Collections;
29 import java.util.Comparator;
30 import java.util.HashSet;
31 import java.util.List;
34 import intervalstore.api.IntervalStoreI;
35 import intervalstore.impl.BinarySearcher;
36 import intervalstore.impl.IntervalStore;
40 * A data store for a set of sequence features that supports efficient lookup of
41 * features overlapping a given range. Intended for (but not limited to) storage
42 * of features for one sequence and feature type.
47 public class FeatureStore
50 * Non-positional features have no (zero) start/end position.
51 * Kept as a separate list in case this criterion changes in future.
53 List<SequenceFeature> nonPositionalFeatures;
56 * contact features ordered by first contact position
58 List<SequenceFeature> contactFeatureStarts;
61 * contact features ordered by second contact position
63 List<SequenceFeature> contactFeatureEnds;
66 * IntervalStore holds remaining features and provides efficient
67 * query for features overlapping any given interval
69 IntervalStoreI<SequenceFeature> features;
72 * Feature groups represented in stored positional features
73 * (possibly including null)
75 Set<String> positionalFeatureGroups;
78 * Feature groups represented in stored non-positional features
79 * (possibly including null)
81 Set<String> nonPositionalFeatureGroups;
84 * the total length of all positional features; contact features count 1 to
85 * the total and 1 to size(), consistent with an average 'feature length' of 1
89 float positionalMinScore;
91 float positionalMaxScore;
93 float nonPositionalMinScore;
95 float nonPositionalMaxScore;
97 private ArrayList<SequenceFeature> featuresList;
102 public FeatureStore()
104 features = new IntervalStore<>();
105 featuresList = new ArrayList<>();
106 positionalFeatureGroups = new HashSet<>();
107 nonPositionalFeatureGroups = new HashSet<>();
108 positionalMinScore = Float.NaN;
109 positionalMaxScore = Float.NaN;
110 nonPositionalMinScore = Float.NaN;
111 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()
125 public boolean addFeature(SequenceFeature feature)
127 if (contains(feature))
133 * keep a record of feature groups
135 if (!feature.isNonPositional())
137 positionalFeatureGroups.add(feature.getFeatureGroup());
140 if (feature.isContactFeature())
142 addContactFeature(feature);
144 else if (feature.isNonPositional())
146 addNonPositionalFeature(feature);
150 addNestedFeature(feature);
154 * record the total extent of positional features, to make
155 * getTotalFeatureLength possible; we count the length of a
156 * contact feature as 1
158 totalExtent += getFeatureLength(feature);
161 * record the minimum and maximum score for positional
162 * and non-positional features
164 float score = feature.getScore();
165 if (!Float.isNaN(score))
167 if (feature.isNonPositional())
169 nonPositionalMinScore = min(nonPositionalMinScore, score);
170 nonPositionalMaxScore = max(nonPositionalMaxScore, score);
174 positionalMinScore = min(positionalMinScore, score);
175 positionalMaxScore = max(positionalMaxScore, score);
183 * Answers true if this store contains the given feature (testing by
184 * SequenceFeature.equals), else false
189 public boolean contains(SequenceFeature feature)
191 if (feature.isNonPositional())
193 return nonPositionalFeatures == null ? false
194 : nonPositionalFeatures.contains(feature);
197 if (feature.isContactFeature())
199 return contactFeatureStarts == null ? false
200 : listContains(contactFeatureStarts, feature);
203 return features == null ? false : features.contains(feature);
207 * Answers the 'length' of the feature, counting 0 for non-positional features
208 * and 1 for contact features
213 protected static int getFeatureLength(SequenceFeature feature)
215 if (feature.isNonPositional())
219 if (feature.isContactFeature())
223 return 1 + feature.getEnd() - feature.getBegin();
227 * Adds the feature to the list of non-positional features (with lazy
228 * instantiation of the list if it is null), and returns true. The feature
229 * group is added to the set of distinct feature groups for non-positional
230 * features. This method allows duplicate features, so test before calling to
235 protected boolean addNonPositionalFeature(SequenceFeature feature)
237 if (nonPositionalFeatures == null)
239 nonPositionalFeatures = new ArrayList<>();
242 nonPositionalFeatures.add(feature);
244 nonPositionalFeatureGroups.add(feature.getFeatureGroup());
250 * Adds one feature to the IntervalStore that can manage nested features
251 * (creating the IntervalStore if necessary)
253 protected synchronized void addNestedFeature(SequenceFeature feature)
255 if (features == null)
257 features = new IntervalStore<>();
259 features.add(feature);
260 featuresList.add(feature);
264 * Add a contact feature to the lists that hold them ordered by start (first
265 * contact) and by end (second contact) position, ensuring the lists remain
266 * ordered, and returns true. This method allows duplicate features to be
267 * added, so test before calling to avoid this.
272 protected synchronized boolean addContactFeature(SequenceFeature feature)
274 if (contactFeatureStarts == null)
276 contactFeatureStarts = new ArrayList<>();
278 if (contactFeatureEnds == null)
280 contactFeatureEnds = new ArrayList<>();
284 * insert into list sorted by start (first contact position):
285 * binary search the sorted list to find the insertion point
287 int insertPosition = BinarySearcher.findFirst(contactFeatureStarts,
288 f -> f.getBegin() >= feature.getBegin());
289 contactFeatureStarts.add(insertPosition, feature);
292 * insert into list sorted by end (second contact position):
293 * binary search the sorted list to find the insertion point
295 insertPosition = BinarySearcher.findFirst(contactFeatureEnds,
296 f -> f.getEnd() >= feature.getEnd());
297 contactFeatureEnds.add(insertPosition, feature);
303 * Answers true if the list contains the feature, else false. This method is
304 * optimised for the condition that the list is sorted on feature start
305 * position ascending, and will give unreliable results if this does not hold.
311 protected static boolean listContains(List<SequenceFeature> features,
312 SequenceFeature feature)
314 if (features == null || feature == null)
320 * locate the first entry in the list which does not precede the feature
322 // int pos = binarySearch(features,
323 // SearchCriterion.byFeature(feature, RangeComparator.BY_START_POSITION));
324 int pos = BinarySearcher.findFirst(features,
325 val -> val.getBegin() >= feature.getBegin());
326 int len = features.size();
329 SequenceFeature sf = features.get(pos);
330 if (sf.getBegin() > feature.getBegin())
332 return false; // no match found
334 if (sf.equals(feature))
344 * Returns a (possibly empty) list of features whose extent overlaps the given
345 * range. The returned list is not ordered. Contact features are included if
346 * either of the contact points lies within the range.
349 * start position of overlap range (inclusive)
351 * end position of overlap range (inclusive)
355 public List<SequenceFeature> findOverlappingFeatures(long start, long end)
357 List<SequenceFeature> result = new ArrayList<>();
359 findContactFeatures(start, end, result);
361 if (features != null)
363 result.addAll(features.findOverlaps(start, end));
370 * Adds contact features to the result list where either the second or the
371 * first contact position lies within the target range
377 protected void findContactFeatures(long from, long to,
378 List<SequenceFeature> result)
380 if (contactFeatureStarts != null)
382 findContactStartOverlaps(from, to, result);
384 if (contactFeatureEnds != null)
386 findContactEndOverlaps(from, to, result);
391 * Adds to the result list any contact features whose end (second contact
392 * point), but not start (first contact point), lies in the query from-to
399 protected void findContactEndOverlaps(long from, long to,
400 List<SequenceFeature> result)
403 * find the first contact feature (if any)
404 * whose end point is not before the target range
406 int index = BinarySearcher.findFirst(contactFeatureEnds,
407 f -> f.getEnd() >= from);
409 while (index < contactFeatureEnds.size())
411 SequenceFeature sf = contactFeatureEnds.get(index);
412 if (!sf.isContactFeature())
414 System.err.println("Error! non-contact feature type " + sf.getType()
415 + " in contact features list");
420 int begin = sf.getBegin();
421 if (begin >= from && begin <= to)
424 * this feature's first contact position lies in the search range
425 * so we don't include it in results a second time
431 if (sf.getEnd() > to)
434 * this feature (and all following) has end point after the target range
440 * feature has end >= from and end <= to
441 * i.e. contact end point lies within overlap search range
449 * Adds contact features whose start position lies in the from-to range to the
456 protected void findContactStartOverlaps(long from, long to,
457 List<SequenceFeature> result)
459 int index = BinarySearcher.findFirst(contactFeatureStarts,
460 f -> f.getBegin() >= from);
462 while (index < contactFeatureStarts.size())
464 SequenceFeature sf = contactFeatureStarts.get(index);
465 if (!sf.isContactFeature())
467 System.err.println("Error! non-contact feature " + sf.toString()
468 + " in contact features list");
472 if (sf.getBegin() > to)
475 * this feature's start (and all following) follows the target range
481 * feature has begin >= from and begin <= to
482 * i.e. contact start point lies within overlap search range
490 * Answers a list of all positional features stored, in no guaranteed order
495 public List<SequenceFeature> getPositionalFeatures()
497 List<SequenceFeature> result = new ArrayList<>();
500 * add any contact features - from the list by start position
502 if (contactFeatureStarts != null)
504 result.addAll(contactFeatureStarts);
508 * add any nested features
510 if (features != null)
512 result.addAll(features);
519 * Answers a list of all contact features. If there are none, returns an
520 * immutable empty list.
525 public List<SequenceFeature> getContactFeatures()
527 if (contactFeatureStarts == null)
529 return Collections.emptyList();
531 return new ArrayList<>(contactFeatureStarts);
535 * Answers a list of all non-positional features. If there are none, returns
536 * an immutable empty list.
541 public List<SequenceFeature> getNonPositionalFeatures()
543 if (nonPositionalFeatures == null)
545 return Collections.emptyList();
547 return new ArrayList<>(nonPositionalFeatures);
551 * Deletes the given feature from the store, returning true if it was found
552 * (and deleted), else false. This method makes no assumption that the feature
553 * is in the 'expected' place in the store, in case it has been modified since
559 public synchronized boolean delete(SequenceFeature sf)
561 boolean removed = false;
564 * try contact positions (and if found, delete
565 * from both lists of contact positions)
567 if (!removed && contactFeatureStarts != null)
569 removed = contactFeatureStarts.remove(sf);
572 contactFeatureEnds.remove(sf);
576 boolean removedNonPositional = false;
579 * if not found, try non-positional features
581 if (!removed && nonPositionalFeatures != null)
583 removedNonPositional = nonPositionalFeatures.remove(sf);
584 removed = removedNonPositional;
588 * if not found, try nested features
590 if (!removed && features != null)
592 removed = features.remove(sf);
593 featuresList.remove(sf);
605 * Rescan all features to recompute any cached values after an entry has been
606 * deleted. This is expected to be an infrequent event, so performance here is
609 protected synchronized void rescanAfterDelete()
611 positionalFeatureGroups.clear();
612 nonPositionalFeatureGroups.clear();
614 positionalMinScore = Float.NaN;
615 positionalMaxScore = Float.NaN;
616 nonPositionalMinScore = Float.NaN;
617 nonPositionalMaxScore = Float.NaN;
619 * scan non-positional features for groups and scores
621 for (SequenceFeature sf : getNonPositionalFeatures())
623 nonPositionalFeatureGroups.add(sf.getFeatureGroup());
624 float score = sf.getScore();
625 nonPositionalMinScore = min(nonPositionalMinScore, score);
626 nonPositionalMaxScore = max(nonPositionalMaxScore, score);
630 * scan positional features for groups, scores and extents
632 for (SequenceFeature sf : getPositionalFeatures())
634 positionalFeatureGroups.add(sf.getFeatureGroup());
635 float score = sf.getScore();
636 positionalMinScore = min(positionalMinScore, score);
637 positionalMaxScore = max(positionalMaxScore, score);
638 totalExtent += getFeatureLength(sf);
643 * A helper method to return the minimum of two floats, where a non-NaN value
644 * is treated as 'less than' a NaN value (unlike Math.min which does the
650 protected static float min(float f1, float f2)
654 return Float.isNaN(f2) ? f1 : f2;
658 return Float.isNaN(f2) ? f1 : Math.min(f1, f2);
663 * A helper method to return the maximum of two floats, where a non-NaN value
664 * is treated as 'greater than' a NaN value (unlike Math.max which does the
670 protected static float max(float f1, float f2)
674 return Float.isNaN(f2) ? f1 : f2;
678 return Float.isNaN(f2) ? f1 : Math.max(f1, f2);
683 * Answers true if this store has no features, else false
688 public boolean isEmpty()
690 boolean hasFeatures = (contactFeatureStarts != null
691 && !contactFeatureStarts.isEmpty())
692 || (nonPositionalFeatures != null
693 && !nonPositionalFeatures.isEmpty())
694 || (features != null && features.size() > 0);
700 * Answers the set of distinct feature groups stored, possibly including null,
701 * as an unmodifiable view of the set. The parameter determines whether the
702 * groups for positional or for non-positional features are returned.
704 * @param positionalFeatures
708 public Set<String> getFeatureGroups(boolean positionalFeatures)
710 if (positionalFeatures)
712 return Collections.unmodifiableSet(positionalFeatureGroups);
716 return nonPositionalFeatureGroups == null
717 ? Collections.<String> emptySet()
718 : Collections.unmodifiableSet(nonPositionalFeatureGroups);
723 * Answers the number of positional (or non-positional) features stored.
724 * Contact features count as 1.
730 public int getFeatureCount(boolean positional)
734 return nonPositionalFeatures == null ? 0
735 : nonPositionalFeatures.size();
740 if (contactFeatureStarts != null)
742 // note a contact feature (start/end) counts as one
743 size += contactFeatureStarts.size();
746 if (features != null)
748 size += features.size();
755 * Answers the total length of positional features (or zero if there are
756 * none). Contact features contribute a value of 1 to the total.
761 public int getTotalFeatureLength()
767 * Answers the minimum score held for positional or non-positional features.
768 * This may be Float.NaN if there are no features, are none has a non-NaN
775 public float getMinimumScore(boolean positional)
777 return positional ? positionalMinScore : nonPositionalMinScore;
781 * Answers the maximum score held for positional or non-positional features.
782 * This may be Float.NaN if there are no features, are none has a non-NaN
789 public float getMaximumScore(boolean positional)
791 return positional ? positionalMaxScore : nonPositionalMaxScore;
795 * Answers a list of all either positional or non-positional features whose
796 * feature group matches the given group (which may be null)
803 public List<SequenceFeature> getFeaturesForGroup(boolean positional,
806 List<SequenceFeature> result = new ArrayList<>();
809 * if we know features don't include the target group, no need
810 * to inspect them for matches
812 if (positional && !positionalFeatureGroups.contains(group)
813 || !positional && !nonPositionalFeatureGroups.contains(group))
818 List<SequenceFeature> sfs = positional ? getPositionalFeatures()
819 : getNonPositionalFeatures();
820 for (SequenceFeature sf : sfs)
822 String featureGroup = sf.getFeatureGroup();
823 if (group == null && featureGroup == null
824 || group != null && group.equals(featureGroup))
833 * Adds the shift amount to the start and end of all positional features whose
834 * start position is at or after fromPosition. Returns true if at least one
835 * feature was shifted, else false.
837 * @param fromPosition
842 public synchronized boolean shiftFeatures(int fromPosition, int shiftBy)
845 * Because begin and end are final fields (to ensure the data store's
846 * integrity), we have to delete each feature and re-add it as amended.
847 * (Although a simple shift of all values would preserve data integrity!)
849 boolean modified = false;
850 for (SequenceFeature sf : getPositionalFeatures())
852 if (sf.getBegin() >= fromPosition)
855 int newBegin = sf.getBegin() + shiftBy;
856 int newEnd = sf.getEnd() + shiftBy;
859 * sanity check: don't shift left of the first residue
863 newBegin = Math.max(1, newBegin);
864 SequenceFeature sf2 = new SequenceFeature(sf, newBegin, newEnd,
865 sf.getFeatureGroup(), sf.getScore());
875 * Find all features containing this position.
878 * @return list of SequenceFeatures
879 * @author Bob Hanson 2019.07.30
882 public List<SequenceFeature> findOverlappingFeatures(int pos,
883 List<SequenceFeature> result)
887 result = new ArrayList<>();
890 if (contactFeatureStarts != null)
892 findContacts(contactFeatureStarts, pos, result, true);
893 findContacts(contactFeatureEnds, pos, result, false);
895 if (featuresList != null)
897 findOverlaps(featuresList, pos, result);
903 * Binary search for contact start or end at a given (Overview) position.
910 * @author Bob Hanson 2019.07.30
912 private static void findContacts(List<SequenceFeature> l, int pos,
913 List<SequenceFeature> result, boolean isStart)
916 int high = l.size() - 1;
919 int mid = (low + high) >>> 1;
920 SequenceFeature f = l.get(mid);
921 switch (Long.signum((isStart ? f.begin : f.end) - pos))
932 // could be "5" in 12345556788 ?
933 while (++mid <= high && (f = l.get(mid)) != null
934 && (isStart ? f.begin : f.end) == pos)
938 while (--m >= low && (f = l.get(m)) != null
939 && (isStart ? f.begin : f.end) == pos)
948 BitSet bs = new BitSet();
951 * Double binary sort with bitset correlation
958 private void findOverlaps(List<SequenceFeature> features, int pos,
959 List<SequenceFeature> result)
961 int n = featuresList.size();
964 checkOne(featuresList.get(0), pos, result);
967 if (orderedFeatureStarts == null)
971 SequenceFeature sf = findClosestFeature(orderedFeatureStarts, pos);
981 private void linkFeatures(SequenceFeature[] intervals)
983 if (intervals.length < 2)
987 int maxEnd = intervals[0].end;
988 for (int i = 1, n = intervals.length; i < n; i++)
990 SequenceFeature ithis = intervals[i];
991 if (ithis.begin <= maxEnd)
993 ithis.containedBy = getContainedBy(intervals[i - 1], ithis);
995 if (ithis.end > maxEnd)
1002 private SequenceFeature getContainedBy(SequenceFeature sf,
1003 SequenceFeature sf0)
1005 int begin = sf0.begin;
1008 if (begin <= sf.end)
1010 System.out.println("\nFS found " + sf0.index + ":" + sf0
1011 + "\nFS in " + sf.index + ":" + sf);
1014 sf = sf.containedBy;
1019 private SequenceFeature findClosestFeature(SequenceFeature[] l, int pos)
1022 int high = l.length - 1;
1025 int mid = (low + high) >>> 1;
1026 SequenceFeature f = l[mid];
1027 switch (Long.signum(f.begin - pos))
1037 while (++mid <= high && l[mid].begin == pos)
1046 return (high < 0 || low >= l.length ? null : l[high]);
1049 private void checkOne(SequenceFeature sf, int pos,
1050 List<SequenceFeature> result)
1052 if (sf.begin <= pos && sf.end >= pos)
1060 * contact features ordered by first contact position
1062 private SequenceFeature[] orderedFeatureStarts;
1064 private void rebuildArrays(int n)
1066 if (startComp == null)
1068 startComp = new StartComparator();
1070 orderedFeatureStarts = new SequenceFeature[n];
1072 for (int i = n; --i >= 0;)
1074 SequenceFeature sf = featuresList.get(i);
1076 orderedFeatureStarts[i] = sf;
1078 Arrays.sort(orderedFeatureStarts, startComp);
1079 linkFeatures(orderedFeatureStarts);
1082 class StartComparator implements Comparator<SequenceFeature>
1088 public int compare(SequenceFeature o1, SequenceFeature o2)
1092 return (p1 < p2 ? -1 : p1 > p2 ? 1 : 0);
1097 static StartComparator startComp;
1099 // class EndComparator implements Comparator<SequenceFeature>
1105 // public int compare(SequenceFeature o1, SequenceFeature o2)
1109 // int val = (p1 < p2 ? 1 : p1 > p2 ? -1 : 0);
1115 // static EndComparator endComp;