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;
34 import intervalstore.impl.BinarySearcher;
35 import intervalstore.impl.BinarySearcher.Compare;
37 public class FeatureStore
40 * track last start for quick insertion of ordered features
42 protected int lastStart = -1;
44 protected int lastContactStart = -1;
47 * Non-positional features have no (zero) start/end position.
48 * Kept as a separate list in case this criterion changes in future.
50 List<SequenceFeature> nonPositionalFeatures;
53 * contact features ordered by first contact position
55 List<SequenceFeature> contactFeatureStarts;
58 * contact features ordered by second contact position
60 List<SequenceFeature> contactFeatureEnds;
63 * IntervalStore holds remaining features and provides efficient
64 * query for features overlapping any given interval
66 IntervalStoreI<SequenceFeature> features;
69 * Feature groups represented in stored positional features
70 * (possibly including null)
72 Set<String> positionalFeatureGroups;
75 * Feature groups represented in stored non-positional features
76 * (possibly including null)
78 Set<String> nonPositionalFeatureGroups;
81 * the total length of all positional features; contact features count 1 to
82 * the total and 1 to size(), consistent with an average 'feature length' of 1
86 float positionalMinScore;
88 float positionalMaxScore;
90 float nonPositionalMinScore;
92 float nonPositionalMaxScore;
94 public final static int INTERVAL_STORE_DEFAULT = -1;
97 * original NCList-based IntervalStore
99 public final static int INTERVAL_STORE_NCLIST_OBJECT = 0;
102 * linked-list IntervalStore
104 public final static int INTERVAL_STORE_LINKED_LIST = 1;
107 * NCList as array buffer IntervalStore
109 public final static int INTERVAL_STORE_NCARRAY = 3;
111 static final int intervalStoreJavaOption = INTERVAL_STORE_NCLIST_OBJECT;
113 private final static boolean useJSOption = false;
115 private final static boolean isJSLinkedTest = true;
117 static final int intervalStoreJSOption = (!useJSOption
118 ? intervalStoreJavaOption
120 ? INTERVAL_STORE_LINKED_LIST
121 : INTERVAL_STORE_NCARRAY);
123 // TODO: compare performance in real situations using
124 // INTERVAL_STORE_LINKED_LIST;
127 * Answers the 'length' of the feature, counting 0 for non-positional features
128 * and 1 for contact features
133 protected static int getFeatureLength(SequenceFeature feature)
135 if (feature.isNonPositional())
139 if (feature.isContactFeature())
143 return 1 + feature.getEnd() - feature.getBegin();
147 * Answers true if the list contains the feature, else false. This method is
148 * optimised for the condition that the list is sorted on feature start
149 * position ascending, and will give unreliable results if this does not hold.
155 public boolean listContains(List<SequenceFeature> list,
156 SequenceFeature feature)
158 if (list == null || feature == null)
164 * locate the first entry in the list which does not precede the feature
166 int begin = feature.begin;
167 int pos = BinarySearcher.findFirst(list, true, Compare.GE, begin);
168 int len = list.size();
171 SequenceFeature sf = list.get(pos);
172 if (sf.begin > begin)
174 return false; // no match found
176 if (sf.equals(feature))
186 * A helper method to return the maximum of two floats, where a non-NaN value
187 * is treated as 'greater than' a NaN value (unlike Math.max which does the
193 protected static float max(float f1, float f2)
197 return Float.isNaN(f2) ? f1 : f2;
201 return Float.isNaN(f2) ? f1 : Math.max(f1, f2);
206 * A helper method to return the minimum of two floats, where a non-NaN value
207 * is treated as 'less than' a NaN value (unlike Math.min which does the
213 protected static float min(float f1, float f2)
217 return Float.isNaN(f2) ? f1 : f2;
221 return Float.isNaN(f2) ? f1 : Math.min(f1, f2);
226 * standard constructor
228 public FeatureStore()
230 this(INTERVAL_STORE_DEFAULT);
234 * constructor for testing only
236 public FeatureStore(int intervalStoreType)
240 // ? new intervalstore.nonc.IntervalStore<>(true)
241 // : new intervalstore.impl.IntervalStore<>();
242 getIntervalStore(intervalStoreType);
243 positionalFeatureGroups = new HashSet<>();
244 nonPositionalFeatureGroups = new HashSet<>();
245 positionalMinScore = Float.NaN;
246 positionalMaxScore = Float.NaN;
247 nonPositionalMinScore = Float.NaN;
248 nonPositionalMaxScore = Float.NaN;
250 // we only construct nonPositionalFeatures, contactFeatures if we need to
253 private IntervalStoreI<SequenceFeature> getIntervalStore(int type)
255 switch (type != INTERVAL_STORE_DEFAULT ? type : //
257 ? intervalStoreJSOption
258 : intervalStoreJavaOption)
261 case INTERVAL_STORE_NCLIST_OBJECT:
262 return new intervalstore.impl.IntervalStore<>();
263 case INTERVAL_STORE_NCARRAY:
264 // TODO: Couldn't figure out how to get rid of this warning
265 return new intervalstore.nonc.IntervalStoreImpl<>();
266 case INTERVAL_STORE_LINKED_LIST:
267 return new intervalstore.nonc.IntervalStore0Impl<>();
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<>();
285 contactFeatureEnds = new ArrayList<>();
289 * insert into list sorted by start (first contact position):
290 * binary search the sorted list to find the insertion point
292 int insertAt = BinarySearcher.findFirst(contactFeatureStarts, true,
293 Compare.GE, feature.begin);
294 contactFeatureStarts.add(insertAt, feature);
296 * insert into list sorted by end (second contact position):
297 * binary search the sorted list to find the insertion point
299 contactFeatureEnds.add(findFirstEnd(contactFeatureEnds, feature.end),
306 * Adds one sequence feature to the store, and returns true, unless the
307 * feature is already contained in the store, in which case this method
308 * returns false. Containment is determined by SequenceFeature.equals()
313 public boolean addFeature(SequenceFeature feature)
315 if (feature.isContactFeature())
317 if (containsContactFeature(feature))
321 positionalFeatureGroups.add(feature.getFeatureGroup());
322 if (feature.begin > lastContactStart)
324 lastContactStart = feature.begin;
326 addContactFeature(feature);
328 else if (feature.isNonPositional())
330 if (containsNonPositionalFeature(feature))
335 addNonPositionalFeature(feature);
339 if (!features.add(feature, false))
343 positionalFeatureGroups.add(feature.getFeatureGroup());
344 if (feature.begin > lastStart)
346 lastStart = feature.begin;
351 * record the total extent of positional features, to make
352 * getTotalFeatureLength possible; we count the length of a
353 * contact feature as 1
355 totalExtent += getFeatureLength(feature);
358 * record the minimum and maximum score for positional
359 * and non-positional features
361 float score = feature.getScore();
362 if (!Float.isNaN(score))
364 if (feature.isNonPositional())
366 nonPositionalMinScore = min(nonPositionalMinScore, score);
367 nonPositionalMaxScore = max(nonPositionalMaxScore, score);
371 positionalMinScore = min(positionalMinScore, score);
372 positionalMaxScore = max(positionalMaxScore, score);
379 private void addFeaturesForGroup(String group,
380 Collection<SequenceFeature> sfs, List<SequenceFeature> result)
386 for (SequenceFeature sf : sfs)
388 String featureGroup = sf.getFeatureGroup();
389 if (group == null && featureGroup == null
390 || group != null && group.equals(featureGroup))
398 * Adds the feature to the list of non-positional features (with lazy
399 * instantiation of the list if it is null), and returns true. The feature
400 * group is added to the set of distinct feature groups for non-positional
401 * features. This method allows duplicate features, so test before calling to
406 protected boolean addNonPositionalFeature(SequenceFeature feature)
408 if (nonPositionalFeatures == null)
410 nonPositionalFeatures = new ArrayList<>();
413 nonPositionalFeatures.add(feature);
415 nonPositionalFeatureGroups.add(feature.getFeatureGroup());
421 * Answers true if this store contains the given feature (testing by
422 * SequenceFeature.equals), else false
427 public boolean contains(SequenceFeature feature)
429 if (feature.isNonPositional())
431 return containsNonPositionalFeature(feature);
434 if (feature.isContactFeature())
436 return containsContactFeature(feature);
439 return containsPositionalFeature(feature);
443 private boolean containsPositionalFeature(SequenceFeature feature)
445 return features == null || feature.begin > lastStart ? false
446 : features.contains(feature);
450 * Answers true if this store already contains a contact feature equal to the
451 * given feature (by {@code SequenceFeature.equals()} test), else false
456 private boolean containsContactFeature(SequenceFeature feature)
458 return contactFeatureStarts != null && feature.begin <= lastContactStart
459 && listContains(contactFeatureStarts, feature);
463 * Answers true if this store already contains a non-positional feature equal
464 * to the given feature (by {@code SequenceFeature.equals()} test), else false
469 private boolean containsNonPositionalFeature(SequenceFeature feature)
471 return nonPositionalFeatures == null ? false
472 : nonPositionalFeatures.contains(feature);
476 * Deletes the given feature from the store, returning true if it was found
477 * (and deleted), else false. This method makes no assumption that the feature
478 * is in the 'expected' place in the store, in case it has been modified since
483 public synchronized boolean delete(SequenceFeature sf)
485 boolean removed = false;
488 * try contact positions (and if found, delete
489 * from both lists of contact positions)
491 if (!removed && contactFeatureStarts != null)
493 removed = contactFeatureStarts.remove(sf);
496 contactFeatureEnds.remove(sf);
501 * if not found, try non-positional features
503 if (!removed && nonPositionalFeatures != null)
505 removed = nonPositionalFeatures.remove(sf);
509 * if not found, try nested features
511 if (!removed && features != null)
513 removed = features.remove(sf);
524 public List<SequenceFeature> findOverlappingFeatures(long start, long end)
526 return findOverlappingFeatures(start, end, null);
529 public List<SequenceFeature> getContactFeatures()
531 return getContactFeatures(new ArrayList<>());
535 * Answers a list of all contact features. If there are none, returns an
536 * immutable empty list.
540 public List<SequenceFeature> getContactFeatures(
541 List<SequenceFeature> result)
543 if (contactFeatureStarts != null)
545 result.addAll(contactFeatureStarts);
551 * Answers the number of positional (or non-positional) features stored.
552 * Contact features count as 1.
557 public int getFeatureCount(boolean positional)
561 return nonPositionalFeatures == null ? 0
562 : nonPositionalFeatures.size();
565 return (contactFeatureStarts == null ? 0 : contactFeatureStarts.size())
570 * Answers the set of distinct feature groups stored, possibly including null,
571 * as an unmodifiable view of the set. The parameter determines whether the
572 * groups for positional or for non-positional features are returned.
574 * @param positionalFeatures
577 public Set<String> getFeatureGroups(boolean positionalFeatures)
579 if (positionalFeatures)
581 return Collections.unmodifiableSet(positionalFeatureGroups);
585 return nonPositionalFeatureGroups == null
586 ? Collections.<String> emptySet()
587 : Collections.unmodifiableSet(nonPositionalFeatureGroups);
591 public Collection<SequenceFeature> getFeatures()
597 * Answers a list of all either positional or non-positional features whose
598 * feature group matches the given group (which may be null)
604 public List<SequenceFeature> getFeaturesForGroup(boolean positional,
607 List<SequenceFeature> result = new ArrayList<>();
610 * if we know features don't include the target group, no need
611 * to inspect them for matches
613 if (positional && !positionalFeatureGroups.contains(group)
614 || !positional && !nonPositionalFeatureGroups.contains(group))
621 addFeaturesForGroup(group, contactFeatureStarts, result);
622 addFeaturesForGroup(group, features, result);
626 addFeaturesForGroup(group, nonPositionalFeatures, result);
632 * Answers the maximum score held for positional or non-positional features.
633 * This may be Float.NaN if there are no features, are none has a non-NaN
639 public float getMaximumScore(boolean positional)
641 return positional ? positionalMaxScore : nonPositionalMaxScore;
645 * Answers the minimum score held for positional or non-positional features.
646 * This may be Float.NaN if there are no features, are none has a non-NaN
652 public float getMinimumScore(boolean positional)
654 return positional ? positionalMinScore : nonPositionalMinScore;
657 public List<SequenceFeature> getNonPositionalFeatures()
659 return getNonPositionalFeatures(new ArrayList<>());
663 * Answers a list of all non-positional features. If there are none, returns
664 * an immutable empty list.
668 public List<SequenceFeature> getNonPositionalFeatures(
669 List<SequenceFeature> result)
671 if (nonPositionalFeatures != null)
673 result.addAll(nonPositionalFeatures);
678 public List<SequenceFeature> getPositionalFeatures()
680 return getPositionalFeatures(new ArrayList<>());
684 * Answers a list of all positional features stored, in no guaranteed order
688 public List<SequenceFeature> getPositionalFeatures(
689 List<SequenceFeature> result)
693 * add any contact features - from the list by start position
695 if (contactFeatureStarts != null)
697 result.addAll(contactFeatureStarts);
701 * add any nested features
703 if (features != null)
705 result.addAll(features);
712 * Answers the total length of positional features (or zero if there are
713 * none). Contact features contribute a value of 1 to the total.
717 public int getTotalFeatureLength()
723 * Answers true if this store has no features, else false
727 public boolean isEmpty()
729 boolean hasFeatures = (contactFeatureStarts != null
730 && !contactFeatureStarts.isEmpty())
731 || (nonPositionalFeatures != null
732 && !nonPositionalFeatures.isEmpty())
733 || features.size() > 0;
739 * Rescan all features to recompute any cached values after an entry has been
740 * deleted. This is expected to be an infrequent event, so performance here is
743 protected synchronized void rescanAfterDelete()
745 positionalFeatureGroups.clear();
746 nonPositionalFeatureGroups.clear();
748 positionalMinScore = Float.NaN;
749 positionalMaxScore = Float.NaN;
750 nonPositionalMinScore = Float.NaN;
751 nonPositionalMaxScore = Float.NaN;
753 * scan non-positional features for groups and scores
755 if (nonPositionalFeatures != null)
757 List<SequenceFeature> list = nonPositionalFeatures;
758 for (int i = 0, n = list.size(); i < n; i++)
760 SequenceFeature sf = list.get(i);
761 nonPositionalFeatureGroups.add(sf.getFeatureGroup());
762 float score = sf.getScore();
763 nonPositionalMinScore = min(nonPositionalMinScore, score);
764 nonPositionalMaxScore = max(nonPositionalMaxScore, score);
769 * scan positional features for groups, scores and extents
772 rescanPositional(contactFeatureStarts);
773 rescanPositional(features);
776 private void rescanPositional(Collection<SequenceFeature> sfs)
782 for (SequenceFeature sf : sfs)
784 positionalFeatureGroups.add(sf.getFeatureGroup());
785 float score = sf.getScore();
786 positionalMinScore = min(positionalMinScore, score);
787 positionalMaxScore = max(positionalMaxScore, score);
788 totalExtent += getFeatureLength(sf);
793 * Adds the shift amount to the start and end of all positional features whose
794 * start position is at or after fromPosition. Returns true if at least one
795 * feature was shifted, else false.
797 * @param fromPosition
801 public synchronized boolean shiftFeatures(int fromPosition, int shiftBy)
804 * Because begin and end are final fields (to ensure the data store's
805 * integrity), we have to delete each feature and re-add it as amended.
806 * (Although a simple shift of all values would preserve data integrity!)
808 boolean modified = false;
809 List<SequenceFeature> list = getPositionalFeatures();
810 for (int i = 0, n = list.size(); i < n; i++)
812 SequenceFeature sf = list.get(i);
813 if (sf.getBegin() >= fromPosition)
816 int newBegin = sf.getBegin() + shiftBy;
817 int newEnd = sf.getEnd() + shiftBy;
820 * sanity check: don't shift left of the first residue
824 newBegin = Math.max(1, newBegin);
825 SequenceFeature sf2 = new SequenceFeature(sf, newBegin, newEnd,
826 sf.getFeatureGroup(), sf.getScore());
836 * Answers the position (0, 1...) in the list of the first entry whose end
837 * position is not less than {@ pos}. If no such entry is found, answers the
838 * length of the list.
844 protected int findFirstEnd(List<SequenceFeature> list, long pos)
846 return BinarySearcher.findFirst(list, false, Compare.GE, (int) pos);
850 * Adds contact features to the result list where either the second or the
851 * first contact position lies within the target range
857 protected void findContactFeatures(long from, long to,
858 List<SequenceFeature> result)
860 if (contactFeatureStarts != null)
862 findContactStartOverlaps(from, to, result);
863 findContactEndOverlaps(from, to, result);
868 * Adds to the result list any contact features whose end (second contact
869 * point), but not start (first contact point), lies in the query from-to
876 private void findContactEndOverlaps(long from, long to,
877 List<SequenceFeature> result)
880 * find the first contact feature (if any)
881 * whose end point is not before the target range
883 int index = findFirstEnd(contactFeatureEnds, from);
885 int n = contactFeatureEnds.size();
888 SequenceFeature sf = contactFeatureEnds.get(index);
889 if (!sf.isContactFeature())
891 System.err.println("Error! non-contact feature type " + sf.getType()
892 + " in contact features list");
897 int begin = sf.getBegin();
898 if (begin >= from && begin <= to)
901 * this feature's first contact position lies in the search range
902 * so we don't include it in results a second time
908 if (sf.getEnd() > to)
911 * this feature (and all following) has end point after the target range
917 * feature has end >= from and end <= to
918 * i.e. contact end point lies within overlap search range
926 * Adds contact features whose start position lies in the from-to range to the
933 private void findContactStartOverlaps(long from, long to,
934 List<SequenceFeature> result)
936 int index = BinarySearcher.findFirst(contactFeatureStarts, true,
937 Compare.GE, (int) from);
939 while (index < contactFeatureStarts.size())
941 SequenceFeature sf = contactFeatureStarts.get(index);
942 if (!sf.isContactFeature())
944 System.err.println("Error! non-contact feature " + sf.toString()
945 + " in contact features list");
949 if (sf.getBegin() > to)
952 * this feature's start (and all following) follows the target range
958 * feature has begin >= from and begin <= to
959 * i.e. contact start point lies within overlap search range
967 * Returns a (possibly empty) list of features whose extent overlaps the given
968 * range. The returned list is not ordered. Contact features are included if
969 * either of the contact points lies within the range. If the {@code result}
970 * parameter is not null, new entries are added to this list and the (possibly
971 * extended) list returned.
974 * start position of overlap range (inclusive)
976 * end position of overlap range (inclusive)
980 public List<SequenceFeature> findOverlappingFeatures(long start, long end,
981 List<SequenceFeature> result)
985 result = new ArrayList<>();
988 findContactFeatures(start, end, result);
989 features.findOverlaps(start, end, result);