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;
24 import jalview.util.Platform;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.HashSet;
30 import java.util.List;
33 import intervalstore.api.IntervalStoreI;
35 public abstract class FeatureStore implements FeatureStoreI
39 * track last start for quick insertion of ordered features
41 protected int lastStart = -1;
43 protected int lastContactStart = -1;
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 public final static int INTERVAL_STORE_DEFAULT = -1;
96 * original NCList-based IntervalStore
98 public final static int INTERVAL_STORE_NCLIST_OBJECT = 0;
101 * linked-list IntervalStore
103 public final static int INTERVAL_STORE_LINKED_LIST_PRESORT = 1;
106 * linked-list IntervalStore
108 public final static int INTERVAL_STORE_LINKED_LIST_NO_PRESORT = 2;
111 * NCList as array buffer IntervalStore
113 public final static int INTERVAL_STORE_NCLIST_BUFFER_PRESORT = 3;
116 * NCList as array buffer IntervalStore
118 public final static int INTERVAL_STORE_NCLIST_BUFFER_NO_PRESORT = 4;
120 static final int intervalStoreJavaOption = INTERVAL_STORE_NCLIST_OBJECT;
122 static final int intervalStoreJSOption = INTERVAL_STORE_NCLIST_BUFFER_PRESORT;
125 * Answers the 'length' of the feature, counting 0 for non-positional features
126 * and 1 for contact features
131 protected static int getFeatureLength(SequenceFeature feature)
133 if (feature.isNonPositional())
137 if (feature.isContactFeature())
141 return 1 + feature.getEnd() - feature.getBegin();
145 * Answers true if the list contains the feature, else false. This method is
146 * optimised for the condition that the list is sorted on feature start
147 * position ascending, and will give unreliable results if this does not hold.
154 public boolean listContains(List<SequenceFeature> list,
155 SequenceFeature feature)
157 if (list == null || feature == null)
162 return (getEquivalentFeatureIndex(list, feature) >= 0);
166 * Binary search for the index (>= 0) of a feature in a list.
170 * @return index if found; -1 if not
172 protected int getEquivalentFeatureIndex(List<SequenceFeature> list,
173 SequenceFeature feature)
177 * locate the first entry in the list which does not precede the feature
179 int begin = feature.begin;
180 int pos = findFirstBegin(list, begin);
181 int len = list.size();
184 SequenceFeature sf = list.get(pos);
185 if (sf.begin > begin)
187 return -1; // no match found
189 if (sf.equals(feature))
199 * A helper method to return the maximum of two floats, where a non-NaN value
200 * is treated as 'greater than' a NaN value (unlike Math.max which does the
206 protected static float max(float f1, float f2)
210 return Float.isNaN(f2) ? f1 : f2;
214 return Float.isNaN(f2) ? f1 : Math.max(f1, f2);
219 * A helper method to return the minimum of two floats, where a non-NaN value
220 * is treated as 'less than' a NaN value (unlike Math.min which does the
226 protected static float min(float f1, float f2)
230 return Float.isNaN(f2) ? f1 : f2;
234 return Float.isNaN(f2) ? f1 : Math.min(f1, f2);
239 * standard constructor
241 public FeatureStore()
243 this(INTERVAL_STORE_DEFAULT);
247 * constructor for testing only
249 public FeatureStore(int intervalStoreType)
251 features = getIntervalStore(intervalStoreType);
252 positionalFeatureGroups = new HashSet<>();
253 nonPositionalFeatureGroups = new HashSet<>();
254 positionalMinScore = Float.NaN;
255 positionalMaxScore = Float.NaN;
256 nonPositionalMinScore = Float.NaN;
257 nonPositionalMaxScore = Float.NaN;
259 // we only construct nonPositionalFeatures, contactFeatures if we need to
262 private IntervalStoreI<SequenceFeature> getIntervalStore(int type)
264 switch (type != INTERVAL_STORE_DEFAULT ? type : //
266 ? intervalStoreJSOption
267 : intervalStoreJavaOption)
270 case INTERVAL_STORE_NCLIST_OBJECT:
271 return new intervalstore.impl.IntervalStore<>();
272 case INTERVAL_STORE_NCLIST_BUFFER_PRESORT:
273 return new intervalstore.nonc.IntervalStore<>(true);
274 case INTERVAL_STORE_NCLIST_BUFFER_NO_PRESORT:
275 return new intervalstore.nonc.IntervalStore<>(false);
276 case INTERVAL_STORE_LINKED_LIST_PRESORT:
277 return new intervalstore.nonc.IntervalStore0<>(true);
278 case INTERVAL_STORE_LINKED_LIST_NO_PRESORT:
279 return new intervalstore.nonc.IntervalStore0<>(false);
284 * Add a contact feature to the lists that hold them ordered by start (first
285 * contact) and by end (second contact) position, ensuring the lists remain
286 * ordered, and returns true. This method allows duplicate features to be
287 * added, so test before calling to avoid this.
292 protected synchronized boolean addContactFeature(SequenceFeature feature)
294 if (contactFeatureStarts == null)
296 contactFeatureStarts = new ArrayList<>();
297 contactFeatureEnds = new ArrayList<>();
301 * insert into list sorted by start (first contact position):
302 * binary search the sorted list to find the insertion point
304 contactFeatureStarts.add(
305 findFirstBegin(contactFeatureStarts, feature.begin), feature);
307 * insert into list sorted by end (second contact position):
308 * binary search the sorted list to find the insertion point
310 contactFeatureEnds.add(findFirstEnd(contactFeatureEnds, feature.end),
317 * Adds one sequence feature to the store, and returns true, unless the
318 * feature is already contained in the store, in which case this method
319 * returns false. Containment is determined by SequenceFeature.equals()
326 public boolean addFeature(SequenceFeature feature)
328 // if (contains(feature))
334 // * keep a record of feature groups
336 // if (!feature.isNonPositional())
338 // positionalFeatureGroups.add(feature.getFeatureGroup());
341 if (feature.isContactFeature())
343 if (containsContactFeature(feature))
347 positionalFeatureGroups.add(feature.getFeatureGroup());
348 if (feature.begin > lastContactStart)
350 lastContactStart = feature.begin;
352 addContactFeature(feature);
354 else if (feature.isNonPositional())
356 if (containsNonPositional(feature))
361 addNonPositionalFeature(feature);
365 // allow for check with
366 if (checkContainsPositionalFeatureForAdd(feature)
367 || !addPositionalFeature(feature))
371 positionalFeatureGroups.add(feature.getFeatureGroup());
372 if (feature.begin > lastStart)
374 lastStart = feature.begin;
379 * record the total extent of positional features, to make
380 * getTotalFeatureLength possible; we count the length of a
381 * contact feature as 1
383 totalExtent += getFeatureLength(feature);
386 * record the minimum and maximum score for positional
387 * and non-positional features
389 float score = feature.getScore();
390 if (!Float.isNaN(score))
392 if (feature.isNonPositional())
394 nonPositionalMinScore = min(nonPositionalMinScore, score);
395 nonPositionalMaxScore = max(nonPositionalMaxScore, score);
399 positionalMinScore = min(positionalMinScore, score);
400 positionalMaxScore = max(positionalMaxScore, score);
407 private void addFeaturesForGroup(String group,
408 Collection<SequenceFeature> sfs, List<SequenceFeature> result)
414 for (SequenceFeature sf : sfs)
416 String featureGroup = sf.getFeatureGroup();
417 if (group == null && featureGroup == null
418 || group != null && group.equals(featureGroup))
426 * Adds one feature to the IntervalStore that can manage nested features
427 * (creating the IntervalStore if necessary)
429 * @return true if added -- allowing for late checking during addition
431 abstract protected boolean addPositionalFeature(SequenceFeature feature);
434 * Adds the feature to the list of non-positional features (with lazy
435 * instantiation of the list if it is null), and returns true. The feature
436 * group is added to the set of distinct feature groups for non-positional
437 * features. This method allows duplicate features, so test before calling to
442 protected boolean addNonPositionalFeature(SequenceFeature feature)
444 if (nonPositionalFeatures == null)
446 nonPositionalFeatures = new ArrayList<>();
449 nonPositionalFeatures.add(feature);
451 nonPositionalFeatureGroups.add(feature.getFeatureGroup());
457 * Answers true if this store contains the given feature (testing by
458 * SequenceFeature.equals), else false
464 public boolean contains(SequenceFeature feature)
466 if (feature.isNonPositional())
468 return containsNonPositional(feature);
472 if (feature.isContactFeature())
474 return containsContactFeature(feature);
478 return containsPositionalFeature(feature);
483 * A check that can be overridden if the check is being done during the add
489 protected boolean checkContainsPositionalFeatureForAdd(
490 SequenceFeature feature)
492 return containsPositionalFeature(feature);
495 private boolean containsPositionalFeature(SequenceFeature feature)
497 return features == null || feature.begin > lastStart ? false
498 : containsFeature(feature);
501 private boolean containsContactFeature(SequenceFeature feature)
503 return contactFeatureStarts != null && feature.begin <= lastContactStart
504 && listContains(contactFeatureStarts, feature);
507 private boolean containsNonPositional(SequenceFeature feature)
509 return nonPositionalFeatures == null ? false
510 : nonPositionalFeatures.contains(feature);
513 abstract protected boolean containsFeature(SequenceFeature feature);
516 * Deletes the given feature from the store, returning true if it was found
517 * (and deleted), else false. This method makes no assumption that the feature
518 * is in the 'expected' place in the store, in case it has been modified since
525 public synchronized boolean delete(SequenceFeature sf)
527 boolean removed = false;
530 * try contact positions (and if found, delete
531 * from both lists of contact positions)
533 if (!removed && contactFeatureStarts != null)
535 removed = contactFeatureStarts.remove(sf);
538 contactFeatureEnds.remove(sf);
543 * if not found, try non-positional features
545 if (!removed && nonPositionalFeatures != null)
547 removed = nonPositionalFeatures.remove(sf);
551 * if not found, try nested features
553 if (!removed && features != null)
555 removed = findAndRemoveNonContactFeature(sf);
566 abstract protected boolean findAndRemoveNonContactFeature(SequenceFeature sf);
568 abstract protected void findContactFeatures(long from, long to,
569 List<SequenceFeature> result);
571 abstract protected int findFirstBegin(List<SequenceFeature> list,
574 abstract protected int findFirstEnd(List<SequenceFeature> list, long pos);
577 public List<SequenceFeature> findOverlappingFeatures(long start, long end)
579 return findOverlappingFeatures(start, end, null);
583 public List<SequenceFeature> getContactFeatures()
585 return getContactFeatures(new ArrayList<>());
589 * Answers a list of all contact features. If there are none, returns an
590 * immutable empty list.
596 public List<SequenceFeature> getContactFeatures(
597 List<SequenceFeature> result)
599 if (contactFeatureStarts != null)
601 result.addAll(contactFeatureStarts);
607 * Answers the number of positional (or non-positional) features stored.
608 * Contact features count as 1.
615 public int getFeatureCount(boolean positional)
619 return nonPositionalFeatures == null ? 0
620 : nonPositionalFeatures.size();
623 return (contactFeatureStarts == null ? 0 : contactFeatureStarts.size())
629 * Answers the set of distinct feature groups stored, possibly including null,
630 * as an unmodifiable view of the set. The parameter determines whether the
631 * groups for positional or for non-positional features are returned.
633 * @param positionalFeatures
638 public Set<String> getFeatureGroups(boolean positionalFeatures)
640 if (positionalFeatures)
642 return Collections.unmodifiableSet(positionalFeatureGroups);
646 return nonPositionalFeatureGroups == null
647 ? Collections.<String> emptySet()
648 : Collections.unmodifiableSet(nonPositionalFeatureGroups);
653 public Collection<SequenceFeature> getFeatures()
659 * Answers a list of all either positional or non-positional features whose
660 * feature group matches the given group (which may be null)
668 public List<SequenceFeature> getFeaturesForGroup(boolean positional,
671 List<SequenceFeature> result = new ArrayList<>();
674 * if we know features don't include the target group, no need
675 * to inspect them for matches
677 if (positional && !positionalFeatureGroups.contains(group)
678 || !positional && !nonPositionalFeatureGroups.contains(group))
685 addFeaturesForGroup(group, contactFeatureStarts, result);
686 addFeaturesForGroup(group, features, result);
690 addFeaturesForGroup(group, nonPositionalFeatures, result);
696 * Answers the maximum score held for positional or non-positional features.
697 * This may be Float.NaN if there are no features, are none has a non-NaN
705 public float getMaximumScore(boolean positional)
707 return positional ? positionalMaxScore : nonPositionalMaxScore;
711 * Answers the minimum score held for positional or non-positional features.
712 * This may be Float.NaN if there are no features, are none has a non-NaN
720 public float getMinimumScore(boolean positional)
722 return positional ? positionalMinScore : nonPositionalMinScore;
726 public List<SequenceFeature> getNonPositionalFeatures()
728 return getNonPositionalFeatures(new ArrayList<>());
732 * Answers a list of all non-positional features. If there are none, returns
733 * an immutable empty list.
739 public List<SequenceFeature> getNonPositionalFeatures(
740 List<SequenceFeature> result)
742 if (nonPositionalFeatures != null)
744 result.addAll(nonPositionalFeatures);
750 public List<SequenceFeature> getPositionalFeatures()
752 return getPositionalFeatures(new ArrayList<>());
756 * Answers a list of all positional features stored, in no guaranteed order
762 public List<SequenceFeature> getPositionalFeatures(
763 List<SequenceFeature> result)
767 * add any contact features - from the list by start position
769 if (contactFeatureStarts != null)
771 result.addAll(contactFeatureStarts);
775 * add any nested features
777 if (features != null)
779 result.addAll(features);
786 * Answers the total length of positional features (or zero if there are
787 * none). Contact features contribute a value of 1 to the total.
793 public int getTotalFeatureLength()
799 * Answers true if this store has no features, else false
805 public boolean isEmpty()
807 boolean hasFeatures = (contactFeatureStarts != null
808 && !contactFeatureStarts.isEmpty())
809 || (nonPositionalFeatures != null
810 && !nonPositionalFeatures.isEmpty())
811 || features.size() > 0;
817 * Rescan all features to recompute any cached values after an entry has been
818 * deleted. This is expected to be an infrequent event, so performance here is
821 protected synchronized void rescanAfterDelete()
823 positionalFeatureGroups.clear();
824 nonPositionalFeatureGroups.clear();
826 positionalMinScore = Float.NaN;
827 positionalMaxScore = Float.NaN;
828 nonPositionalMinScore = Float.NaN;
829 nonPositionalMaxScore = Float.NaN;
831 * scan non-positional features for groups and scores
833 if (nonPositionalFeatures != null)
835 List<SequenceFeature> list = nonPositionalFeatures;
836 for (int i = 0, n = list.size(); i < n; i++)
838 SequenceFeature sf = list.get(i);
839 nonPositionalFeatureGroups.add(sf.getFeatureGroup());
840 float score = sf.getScore();
841 nonPositionalMinScore = min(nonPositionalMinScore, score);
842 nonPositionalMaxScore = max(nonPositionalMaxScore, score);
847 * scan positional features for groups, scores and extents
850 rescanPositional(contactFeatureStarts);
851 rescanPositional(features);
854 private void rescanPositional(Collection<SequenceFeature> sfs)
860 for (SequenceFeature sf : sfs)
862 positionalFeatureGroups.add(sf.getFeatureGroup());
863 float score = sf.getScore();
864 positionalMinScore = min(positionalMinScore, score);
865 positionalMaxScore = max(positionalMaxScore, score);
866 totalExtent += getFeatureLength(sf);
871 * Adds the shift amount to the start and end of all positional features whose
872 * start position is at or after fromPosition. Returns true if at least one
873 * feature was shifted, else false.
875 * @param fromPosition
881 public synchronized boolean shiftFeatures(int fromPosition, int shiftBy)
884 * Because begin and end are final fields (to ensure the data store's
885 * integrity), we have to delete each feature and re-add it as amended.
886 * (Although a simple shift of all values would preserve data integrity!)
888 boolean modified = false;
889 List<SequenceFeature> list = getPositionalFeatures();
890 for (int i = 0, n = list.size(); i < n; i++)
892 SequenceFeature sf = list.get(i);
893 if (sf.getBegin() >= fromPosition)
896 int newBegin = sf.getBegin() + shiftBy;
897 int newEnd = sf.getEnd() + shiftBy;
900 * sanity check: don't shift left of the first residue
904 newBegin = Math.max(1, newBegin);
905 SequenceFeature sf2 = new SequenceFeature(sf, newBegin, newEnd,
906 sf.getFeatureGroup(), sf.getScore());