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.Collection;
27 import java.util.Collections;
28 import java.util.HashSet;
29 import java.util.List;
32 import intervalstore.api.IntervalStoreI;
33 import intervalstore.impl.BinarySearcher;
34 import intervalstore.impl.BinarySearcher.Compare;
36 public class FeatureStore
38 public enum IntervalStoreType
41 * original NCList-based IntervalStore
43 INTERVAL_STORE_NCLIST,
46 * linked-list IntervalStore
48 INTERVAL_STORE_LINKED_LIST,
51 * NCList as array buffer IntervalStore
53 INTERVAL_STORE_NCARRAY
57 * track largest start for quick insertion of ordered features
59 protected int maxStart = -1;
61 protected int maxContactStart = -1;
64 * Non-positional features have no (zero) start/end position.
65 * Kept as a separate list in case this criterion changes in future.
67 List<SequenceFeature> nonPositionalFeatures;
70 * contact features ordered by first contact position
72 List<SequenceFeature> contactFeatureStarts;
75 * contact features ordered by second contact position
77 List<SequenceFeature> contactFeatureEnds;
80 * IntervalStore holds remaining features and provides efficient
81 * query for features overlapping any given interval
83 IntervalStoreI<SequenceFeature> features;
86 * Feature groups represented in stored positional features
87 * (possibly including null)
89 Set<String> positionalFeatureGroups;
92 * Feature groups represented in stored non-positional features
93 * (possibly including null)
95 Set<String> nonPositionalFeatureGroups;
98 * the total length of all positional features; contact features count 1 to
99 * the total and 1 to size(), consistent with an average 'feature length' of 1
103 float positionalMinScore;
105 float positionalMaxScore;
107 float nonPositionalMinScore;
109 float nonPositionalMaxScore;
112 * Answers the 'length' of the feature, counting 0 for non-positional features
113 * and 1 for contact features
118 protected static int getFeatureLength(SequenceFeature feature)
120 if (feature.isNonPositional())
124 if (feature.isContactFeature())
128 return 1 + feature.getEnd() - feature.getBegin();
132 * Answers true if the list contains the feature, else false. This method is
133 * optimised for the condition that the list is sorted on feature start
134 * position ascending, and will give unreliable results if this does not hold.
140 public static boolean listContains(List<SequenceFeature> list,
141 SequenceFeature feature)
143 if (list == null || feature == null)
149 * locate the first entry in the list which does not precede the feature
151 int begin = feature.begin;
152 int pos = BinarySearcher.findFirst(list, true, Compare.GE, begin);
153 int len = list.size();
156 SequenceFeature sf = list.get(pos);
157 if (sf.begin > begin)
159 return false; // no match found
161 if (sf.equals(feature))
171 * A helper method to return the maximum of two floats, where a non-NaN value
172 * is treated as 'greater than' a NaN value (unlike Math.max which does the
178 protected static float max(float f1, float f2)
182 return Float.isNaN(f2) ? f1 : f2;
186 return Float.isNaN(f2) ? f1 : Math.max(f1, f2);
191 * A helper method to return the minimum of two floats, where a non-NaN value
192 * is treated as 'less than' a NaN value (unlike Math.min which does the
198 protected static float min(float f1, float f2)
202 return Float.isNaN(f2) ? f1 : f2;
206 return Float.isNaN(f2) ? f1 : Math.min(f1, f2);
211 * Constructor that defaults to using NCList IntervalStore
213 public FeatureStore()
215 this(IntervalStoreType.INTERVAL_STORE_NCLIST);
219 * Constructor that allows an alternative IntervalStore implementation to be
222 public FeatureStore(IntervalStoreType intervalStoreType)
224 features = getIntervalStore(intervalStoreType);
225 positionalFeatureGroups = new HashSet<>();
226 nonPositionalFeatureGroups = new HashSet<>();
227 positionalMinScore = Float.NaN;
228 positionalMaxScore = Float.NaN;
229 nonPositionalMinScore = Float.NaN;
230 nonPositionalMaxScore = Float.NaN;
232 // only construct nonPositionalFeatures or contactFeatures if needed
236 * Returns a new instance of IntervalStoreI of implementation as selected by
242 private IntervalStoreI<SequenceFeature> getIntervalStore(
243 IntervalStoreType type)
248 case INTERVAL_STORE_NCLIST:
249 return new intervalstore.impl.IntervalStore<>();
250 case INTERVAL_STORE_NCARRAY:
251 return new intervalstore.nonc.IntervalStore<>();
252 case INTERVAL_STORE_LINKED_LIST:
253 return new intervalstore.nonc.IntervalStore0<>();
258 * Add a contact feature to the lists that hold them ordered by start (first
259 * contact) and by end (second contact) position, ensuring the lists remain
260 * ordered, and returns true. This method allows duplicate features to be
261 * added, so test before calling to avoid this.
266 protected synchronized boolean addContactFeature(SequenceFeature feature)
268 if (contactFeatureStarts == null)
270 contactFeatureStarts = new ArrayList<>();
271 contactFeatureEnds = new ArrayList<>();
275 * insert into list sorted by start (first contact position):
276 * binary search the sorted list to find the insertion point
278 int insertAt = BinarySearcher.findFirst(contactFeatureStarts, true,
279 Compare.GE, feature.begin);
280 contactFeatureStarts.add(insertAt, feature);
282 * insert into list sorted by end (second contact position):
283 * binary search the sorted list to find the insertion point
285 contactFeatureEnds.add(findFirstEnd(contactFeatureEnds, feature.end),
292 * Adds one sequence feature to the store, and returns true, unless the
293 * feature is already contained in the store, in which case this method
294 * returns false. Containment is determined by SequenceFeature.equals()
299 public boolean addFeature(SequenceFeature feature)
301 if (feature.isContactFeature())
303 if (containsContactFeature(feature))
307 positionalFeatureGroups.add(feature.getFeatureGroup());
308 if (feature.begin > maxContactStart)
310 maxContactStart = feature.begin;
312 addContactFeature(feature);
314 else if (feature.isNonPositional())
316 if (containsNonPositionalFeature(feature))
321 addNonPositionalFeature(feature);
325 if (!features.add(feature, false))
329 positionalFeatureGroups.add(feature.getFeatureGroup());
330 if (feature.begin > maxStart)
332 maxStart = feature.begin;
337 * record the total extent of positional features, to make
338 * getTotalFeatureLength possible; we count the length of a
339 * contact feature as 1
341 totalExtent += getFeatureLength(feature);
344 * record the minimum and maximum score for positional
345 * and non-positional features
347 float score = feature.getScore();
348 if (!Float.isNaN(score))
350 if (feature.isNonPositional())
352 nonPositionalMinScore = min(nonPositionalMinScore, score);
353 nonPositionalMaxScore = max(nonPositionalMaxScore, score);
357 positionalMinScore = min(positionalMinScore, score);
358 positionalMaxScore = max(positionalMaxScore, score);
366 * A helper method that adds to the result list any features from the
367 * collection provided whose feature group matches the specified group
373 private void addFeaturesForGroup(String group,
374 Collection<SequenceFeature> sfs, List<SequenceFeature> result)
380 for (SequenceFeature sf : sfs)
382 String featureGroup = sf.getFeatureGroup();
383 if (group == null && featureGroup == null
384 || group != null && group.equals(featureGroup))
392 * Adds the feature to the list of non-positional features (with lazy
393 * instantiation of the list if it is null), and returns true. The feature
394 * group is added to the set of distinct feature groups for non-positional
395 * features. This method allows duplicate features, so test before calling to
400 protected boolean addNonPositionalFeature(SequenceFeature feature)
402 if (nonPositionalFeatures == null)
404 nonPositionalFeatures = new ArrayList<>();
407 nonPositionalFeatures.add(feature);
409 nonPositionalFeatureGroups.add(feature.getFeatureGroup());
415 * Answers true if this store contains the given feature (testing by
416 * SequenceFeature.equals), else false
421 public boolean contains(SequenceFeature feature)
423 if (feature.isNonPositional())
425 return containsNonPositionalFeature(feature);
428 if (feature.isContactFeature())
430 return containsContactFeature(feature);
433 return containsPositionalFeature(feature);
436 private boolean containsPositionalFeature(SequenceFeature feature)
438 return features == null || feature.begin > maxStart ? false
439 : features.contains(feature);
443 * Answers true if this store already contains a contact feature equal to the
444 * given feature (by {@code SequenceFeature.equals()} test), else false
449 private boolean containsContactFeature(SequenceFeature feature)
451 return contactFeatureStarts != null && feature.begin <= maxContactStart
452 && listContains(contactFeatureStarts, feature);
456 * Answers true if this store already contains a non-positional feature equal
457 * to the given feature (by {@code SequenceFeature.equals()} test), else false
462 private boolean containsNonPositionalFeature(SequenceFeature feature)
464 return nonPositionalFeatures == null ? false
465 : nonPositionalFeatures.contains(feature);
469 * Deletes the given feature from the store, returning true if it was found
470 * (and deleted), else false. This method makes no assumption that the feature
471 * is in the 'expected' place in the store, in case it has been modified since
476 public synchronized boolean delete(SequenceFeature sf)
478 boolean removed = false;
481 * try contact positions (and if found, delete
482 * from both lists of contact positions)
484 if (!removed && contactFeatureStarts != null)
486 removed = contactFeatureStarts.remove(sf);
489 contactFeatureEnds.remove(sf);
494 * if not found, try non-positional features
496 if (!removed && nonPositionalFeatures != null)
498 removed = nonPositionalFeatures.remove(sf);
502 * if not found, try nested features
504 if (!removed && features != null)
506 removed = features.remove(sf);
517 public List<SequenceFeature> findFeatures(long start, long end)
519 return findFeatures(start, end, null);
523 * Returns a (possibly empty) list of features whose extent overlaps the given
524 * range. The returned list is not ordered. Contact features are included if
525 * either of the contact points lies within the range. If the {@code result}
526 * parameter is not null, new entries are added to this list and the (possibly
527 * extended) list returned.
530 * start position of overlap range (inclusive)
532 * end position of overlap range (inclusive)
536 public List<SequenceFeature> findFeatures(long start, long end,
537 List<SequenceFeature> result)
541 result = new ArrayList<>();
544 findContactFeatures(start, end, result);
545 features.findOverlaps(start, end, result);
551 * Returns a (possibly empty) list of stored contact features
555 public List<SequenceFeature> getContactFeatures()
557 List<SequenceFeature> result = new ArrayList<>();
558 getContactFeatures(result);
563 * Adds any stored contact features to the result list
567 public void getContactFeatures(List<SequenceFeature> result)
569 if (contactFeatureStarts != null)
571 result.addAll(contactFeatureStarts);
576 * Answers the number of positional (or non-positional) features stored.
577 * Contact features count as 1.
582 public int getFeatureCount(boolean positional)
586 return nonPositionalFeatures == null ? 0
587 : nonPositionalFeatures.size();
592 if (contactFeatureStarts != null)
594 // note a contact feature (start/end) counts as one
595 size += contactFeatureStarts.size();
598 if (features != null)
600 size += features.size();
606 * Answers the set of distinct feature groups stored, possibly including null,
607 * as an unmodifiable view of the set. The parameter determines whether the
608 * groups for positional or for non-positional features are returned.
610 * @param positionalFeatures
613 public Set<String> getFeatureGroups(boolean positionalFeatures)
615 if (positionalFeatures)
617 return Collections.unmodifiableSet(positionalFeatureGroups);
621 return nonPositionalFeatureGroups == null
622 ? Collections.<String> emptySet()
623 : Collections.unmodifiableSet(nonPositionalFeatureGroups);
628 * Answers a list of all either positional or non-positional features whose
629 * feature group matches the given group (which may be null)
635 public List<SequenceFeature> getFeaturesForGroup(boolean positional,
638 List<SequenceFeature> result = new ArrayList<>();
641 * if we know features don't include the target group, no need
642 * to inspect them for matches
644 if (positional && !positionalFeatureGroups.contains(group)
645 || !positional && !nonPositionalFeatureGroups.contains(group))
652 addFeaturesForGroup(group, contactFeatureStarts, result);
653 addFeaturesForGroup(group, features, result);
657 addFeaturesForGroup(group, nonPositionalFeatures, result);
663 * Answers the maximum score held for positional or non-positional features.
664 * This may be Float.NaN if there are no features, are none has a non-NaN
670 public float getMaximumScore(boolean positional)
672 return positional ? positionalMaxScore : nonPositionalMaxScore;
676 * Answers the minimum score held for positional or non-positional features.
677 * This may be Float.NaN if there are no features, are none has a non-NaN
683 public float getMinimumScore(boolean positional)
685 return positional ? positionalMinScore : nonPositionalMinScore;
689 * Answers a (possibly empty) list of all non-positional features
693 public List<SequenceFeature> getNonPositionalFeatures()
695 List<SequenceFeature> result = new ArrayList<>();
696 getNonPositionalFeatures(result);
701 * Adds any stored non-positional features to the result list
705 public void getNonPositionalFeatures(List<SequenceFeature> result)
707 if (nonPositionalFeatures != null)
709 result.addAll(nonPositionalFeatures);
714 * Returns a (possibly empty) list of all positional features stored
718 public List<SequenceFeature> getPositionalFeatures()
720 List<SequenceFeature> result = new ArrayList<>();
721 getPositionalFeatures(result);
727 * Adds all positional features stored to the result list, in no guaranteed
728 * order, and with no check for duplicates
730 public void getPositionalFeatures(List<SequenceFeature> result)
733 * add any contact features - from the list by start position
735 if (contactFeatureStarts != null)
737 result.addAll(contactFeatureStarts);
741 * add any nested features
743 if (features != null)
745 result.addAll(features);
750 * Answers the total length of positional features (or zero if there are
751 * none). Contact features contribute a value of 1 to the total.
755 public int getTotalFeatureLength()
761 * Answers true if this store has no features, else false
765 public boolean isEmpty()
767 boolean hasFeatures = (contactFeatureStarts != null
768 && !contactFeatureStarts.isEmpty())
769 || (nonPositionalFeatures != null
770 && !nonPositionalFeatures.isEmpty())
771 || (features != null && features.size() > 0);
777 * Rescan all features to recompute any cached values after an entry has been
778 * deleted. This is expected to be an infrequent event, so performance here is
781 protected synchronized void rescanAfterDelete()
783 positionalFeatureGroups.clear();
784 nonPositionalFeatureGroups.clear();
786 positionalMinScore = Float.NaN;
787 positionalMaxScore = Float.NaN;
788 nonPositionalMinScore = Float.NaN;
789 nonPositionalMaxScore = Float.NaN;
791 * scan non-positional features for groups and scores
793 if (nonPositionalFeatures != null)
795 for (int i = 0, n = nonPositionalFeatures.size(); i < n; i++)
797 SequenceFeature sf = nonPositionalFeatures.get(i);
798 nonPositionalFeatureGroups.add(sf.getFeatureGroup());
799 float score = sf.getScore();
800 nonPositionalMinScore = min(nonPositionalMinScore, score);
801 nonPositionalMaxScore = max(nonPositionalMaxScore, score);
805 rescanPositional(contactFeatureStarts);
806 rescanPositional(features);
810 * Scans the given features and updates cached feature groups, minimum and
811 * maximum feature score, and total feature extent (length) for positional
816 private void rescanPositional(Collection<SequenceFeature> sfs)
822 for (SequenceFeature sf : sfs)
824 positionalFeatureGroups.add(sf.getFeatureGroup());
825 float score = sf.getScore();
826 positionalMinScore = min(positionalMinScore, score);
827 positionalMaxScore = max(positionalMaxScore, score);
828 totalExtent += getFeatureLength(sf);
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
841 public synchronized boolean shiftFeatures(int fromPosition, int shiftBy)
844 * Because begin and end are final fields (to ensure the data store's
845 * integrity), we have to delete each feature and re-add it as amended.
846 * (Although a simple shift of all values would preserve data integrity!)
848 boolean modified = false;
849 List<SequenceFeature> list = getPositionalFeatures();
850 for (int i = 0, n = list.size(); i < n; i++)
852 SequenceFeature sf = list.get(i);
853 if (sf.getBegin() >= fromPosition)
856 int newBegin = sf.getBegin() + shiftBy;
857 int newEnd = sf.getEnd() + shiftBy;
860 * sanity check: don't shift left of the first residue
864 newBegin = Math.max(1, newBegin);
865 SequenceFeature sf2 = new SequenceFeature(sf, newBegin, newEnd,
866 sf.getFeatureGroup(), sf.getScore());
876 * Answers the position (0, 1...) in the list of the first entry whose end
877 * position is not less than {@ pos}. If no such entry is found, answers the
878 * length of the list.
884 protected int findFirstEnd(List<SequenceFeature> list, long pos)
886 return BinarySearcher.findFirst(list, false, Compare.GE, (int) pos);
890 * Adds contact features to the result list where either the second or the
891 * first contact position lies within the target range
897 protected void findContactFeatures(long from, long to,
898 List<SequenceFeature> result)
900 if (contactFeatureStarts != null)
902 findContactStartOverlaps(from, to, result);
903 findContactEndOverlaps(from, to, result);
908 * Adds to the result list any contact features whose end (second contact
909 * point), but not start (first contact point), lies in the query from-to
916 private void findContactEndOverlaps(long from, long to,
917 List<SequenceFeature> result)
920 * find the first contact feature (if any)
921 * whose end point is not before the target range
923 int index = findFirstEnd(contactFeatureEnds, from);
925 int n = contactFeatureEnds.size();
928 SequenceFeature sf = contactFeatureEnds.get(index);
929 if (!sf.isContactFeature())
931 System.err.println("Error! non-contact feature type " + sf.getType()
932 + " in contact features list");
937 int begin = sf.getBegin();
938 if (begin >= from && begin <= to)
941 * this feature's first contact position lies in the search range
942 * so we don't include it in results a second time
948 if (sf.getEnd() > to)
951 * this feature (and all following) has end point after the target range
957 * feature has end >= from and end <= to
958 * i.e. contact end point lies within overlap search range
966 * Adds contact features whose start position lies in the from-to range to the
973 private void findContactStartOverlaps(long from, long to,
974 List<SequenceFeature> result)
976 int index = BinarySearcher.findFirst(contactFeatureStarts, true,
977 Compare.GE, (int) from);
979 while (index < contactFeatureStarts.size())
981 SequenceFeature sf = contactFeatureStarts.get(index);
982 if (!sf.isContactFeature())
984 System.err.println("Error! non-contact feature " + sf.toString()
985 + " in contact features list");
989 if (sf.getBegin() > to)
992 * this feature's start (and all following) follows the target range
998 * feature has begin >= from and begin <= to
999 * i.e. contact start point lies within overlap search range