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 public abstract class FeatureStore implements FeatureStoreI
36 * track last start for quick insertion of ordered features
38 protected int lastStart = -1, lastContactStart = -1;
41 * Answers the 'length' of the feature, counting 0 for non-positional features
42 * and 1 for contact features
47 protected static int getFeatureLength(SequenceFeature feature)
49 if (feature.isNonPositional())
53 if (feature.isContactFeature())
57 return 1 + feature.getEnd() - feature.getBegin();
61 * Answers true if the list contains the feature, else false. This method is
62 * optimised for the condition that the list is sorted on feature start
63 * position ascending, and will give unreliable results if this does not hold.
70 public boolean listContains(List<SequenceFeature> list,
71 SequenceFeature feature)
73 if (list == null || feature == null)
78 return (getEquivalentFeatureIndex(list, feature) >= 0);
82 * Binary search for the index (>= 0) of a feature in a list.
86 * @return index if found; -1 if not
88 protected int getEquivalentFeatureIndex(List<SequenceFeature> list,
89 SequenceFeature feature)
93 * locate the first entry in the list which does not precede the feature
95 int begin = feature.begin;
96 int pos = findFirstBegin(list, begin);
97 int len = list.size();
100 SequenceFeature sf = list.get(pos);
101 if (sf.begin > begin)
103 return -1; // no match found
105 if (sf.equals(feature))
115 * A helper method to return the maximum of two floats, where a non-NaN value
116 * is treated as 'greater than' a NaN value (unlike Math.max which does the
122 protected static float max(float f1, float f2)
126 return Float.isNaN(f2) ? f1 : f2;
130 return Float.isNaN(f2) ? f1 : Math.max(f1, f2);
135 * A helper method to return the minimum of two floats, where a non-NaN value
136 * is treated as 'less than' a NaN value (unlike Math.min which does the
142 protected static float min(float f1, float f2)
146 return Float.isNaN(f2) ? f1 : f2;
150 return Float.isNaN(f2) ? f1 : Math.min(f1, f2);
155 * Non-positional features have no (zero) start/end position.
156 * Kept as a separate list in case this criterion changes in future.
158 List<SequenceFeature> nonPositionalFeatures;
161 * contact features ordered by first contact position
163 List<SequenceFeature> contactFeatureStarts;
166 * contact features ordered by second contact position
168 List<SequenceFeature> contactFeatureEnds;
171 * IntervalStore holds remaining features and provides efficient
172 * query for features overlapping any given interval
174 Collection<SequenceFeature> features;
177 * Feature groups represented in stored positional features
178 * (possibly including null)
180 Set<String> positionalFeatureGroups;
183 * Feature groups represented in stored non-positional features
184 * (possibly including null)
186 Set<String> nonPositionalFeatureGroups;
189 * the total length of all positional features; contact features count 1 to
190 * the total and 1 to size(), consistent with an average 'feature length' of 1
194 float positionalMinScore;
196 float positionalMaxScore;
198 float nonPositionalMinScore;
200 float nonPositionalMaxScore;
205 public FeatureStore()
207 positionalFeatureGroups = new HashSet<>();
208 nonPositionalFeatureGroups = new HashSet<>();
209 positionalMinScore = Float.NaN;
210 positionalMaxScore = Float.NaN;
211 nonPositionalMinScore = Float.NaN;
212 nonPositionalMaxScore = Float.NaN;
214 // we only construct nonPositionalFeatures, contactFeatures if we need to
218 * Add a contact feature to the lists that hold them ordered by start (first
219 * contact) and by end (second contact) position, ensuring the lists remain
220 * ordered, and returns true. This method allows duplicate features to be
221 * added, so test before calling to avoid this.
226 protected synchronized boolean addContactFeature(SequenceFeature feature)
228 if (contactFeatureStarts == null)
230 contactFeatureStarts = new ArrayList<>();
231 contactFeatureEnds = new ArrayList<>();
235 * insert into list sorted by start (first contact position):
236 * binary search the sorted list to find the insertion point
238 contactFeatureStarts.add(
239 findFirstBegin(contactFeatureStarts, feature.begin), feature);
241 * insert into list sorted by end (second contact position):
242 * binary search the sorted list to find the insertion point
244 contactFeatureEnds.add(findFirstEnd(contactFeatureEnds, feature.end),
251 * Adds one sequence feature to the store, and returns true, unless the
252 * feature is already contained in the store, in which case this method
253 * returns false. Containment is determined by SequenceFeature.equals()
260 public boolean addFeature(SequenceFeature feature)
262 if (contains(feature))
268 * keep a record of feature groups
270 if (!feature.isNonPositional())
272 positionalFeatureGroups.add(feature.getFeatureGroup());
275 if (feature.isContactFeature())
277 if (feature.begin > lastContactStart)
279 lastContactStart = feature.begin;
281 addContactFeature(feature);
283 else if (feature.isNonPositional())
285 addNonPositionalFeature(feature);
289 addPositionalFeature(feature);
290 if (feature.begin > lastStart)
292 lastStart = feature.begin;
297 * record the total extent of positional features, to make
298 * getTotalFeatureLength possible; we count the length of a
299 * contact feature as 1
301 totalExtent += getFeatureLength(feature);
304 * record the minimum and maximum score for positional
305 * and non-positional features
307 float score = feature.getScore();
308 if (!Float.isNaN(score))
310 if (feature.isNonPositional())
312 nonPositionalMinScore = min(nonPositionalMinScore, score);
313 nonPositionalMaxScore = max(nonPositionalMaxScore, score);
317 positionalMinScore = min(positionalMinScore, score);
318 positionalMaxScore = max(positionalMaxScore, score);
325 private void addFeaturesForGroup(String group,
326 Collection<SequenceFeature> sfs, List<SequenceFeature> result)
332 for (SequenceFeature sf : sfs)
334 String featureGroup = sf.getFeatureGroup();
335 if (group == null && featureGroup == null
336 || group != null && group.equals(featureGroup))
344 * Adds one feature to the IntervalStore that can manage nested features
345 * (creating the IntervalStore if necessary)
347 abstract protected void addPositionalFeature(SequenceFeature feature);
350 * Adds the feature to the list of non-positional features (with lazy
351 * instantiation of the list if it is null), and returns true. The feature
352 * group is added to the set of distinct feature groups for non-positional
353 * features. This method allows duplicate features, so test before calling to
358 protected boolean addNonPositionalFeature(SequenceFeature feature)
360 if (nonPositionalFeatures == null)
362 nonPositionalFeatures = new ArrayList<>();
365 nonPositionalFeatures.add(feature);
367 nonPositionalFeatureGroups.add(feature.getFeatureGroup());
373 * Answers true if this store contains the given feature (testing by
374 * SequenceFeature.equals), else false
380 public boolean contains(SequenceFeature feature)
382 if (feature.isNonPositional())
384 return nonPositionalFeatures == null ? false
385 : nonPositionalFeatures.contains(feature);
388 if (feature.isContactFeature())
390 return contactFeatureStarts != null
391 && feature.begin <= lastContactStart
392 && listContains(contactFeatureStarts, feature);
395 return features == null || feature.begin > lastStart ? false
396 : containsFeature(feature);
400 abstract protected boolean containsFeature(SequenceFeature feature);
403 * Deletes the given feature from the store, returning true if it was found
404 * (and deleted), else false. This method makes no assumption that the feature
405 * is in the 'expected' place in the store, in case it has been modified since
412 public synchronized boolean delete(SequenceFeature sf)
414 boolean removed = false;
417 * try contact positions (and if found, delete
418 * from both lists of contact positions)
420 if (!removed && contactFeatureStarts != null)
422 removed = contactFeatureStarts.remove(sf);
425 contactFeatureEnds.remove(sf);
430 * if not found, try non-positional features
432 if (!removed && nonPositionalFeatures != null)
434 removed = nonPositionalFeatures.remove(sf);
438 * if not found, try nested features
440 if (!removed && features != null)
442 removed = findAndRemoveNonContactFeature(sf);
453 abstract protected boolean findAndRemoveNonContactFeature(SequenceFeature sf);
455 abstract protected void findContactFeatures(long from, long to,
456 List<SequenceFeature> result);
458 abstract protected int findFirstBegin(List<SequenceFeature> list,
461 abstract protected int findFirstEnd(List<SequenceFeature> list, long pos);
464 public List<SequenceFeature> findOverlappingFeatures(long start, long end)
466 return findOverlappingFeatures(start, end, null);
470 public List<SequenceFeature> getContactFeatures()
472 return getContactFeatures(new ArrayList<>());
476 * Answers a list of all contact features. If there are none, returns an
477 * immutable empty list.
483 public List<SequenceFeature> getContactFeatures(
484 List<SequenceFeature> result)
486 if (contactFeatureStarts != null)
488 result.addAll(contactFeatureStarts);
494 * Answers the number of positional (or non-positional) features stored.
495 * Contact features count as 1.
502 public int getFeatureCount(boolean positional)
506 return nonPositionalFeatures == null ? 0
507 : nonPositionalFeatures.size();
510 return (contactFeatureStarts == null ? 0 : contactFeatureStarts.size())
516 * Answers the set of distinct feature groups stored, possibly including null,
517 * as an unmodifiable view of the set. The parameter determines whether the
518 * groups for positional or for non-positional features are returned.
520 * @param positionalFeatures
525 public Set<String> getFeatureGroups(boolean positionalFeatures)
527 if (positionalFeatures)
529 return Collections.unmodifiableSet(positionalFeatureGroups);
533 return nonPositionalFeatureGroups == null
534 ? Collections.<String> emptySet()
535 : Collections.unmodifiableSet(nonPositionalFeatureGroups);
540 public Collection<SequenceFeature> getFeatures()
546 * Answers a list of all either positional or non-positional features whose
547 * feature group matches the given group (which may be null)
555 public List<SequenceFeature> getFeaturesForGroup(boolean positional,
558 List<SequenceFeature> result = new ArrayList<>();
561 * if we know features don't include the target group, no need
562 * to inspect them for matches
564 if (positional && !positionalFeatureGroups.contains(group)
565 || !positional && !nonPositionalFeatureGroups.contains(group))
572 addFeaturesForGroup(group, contactFeatureStarts, result);
573 addFeaturesForGroup(group, features, result);
577 addFeaturesForGroup(group, nonPositionalFeatures, result);
583 * Answers the maximum score held for positional or non-positional features.
584 * This may be Float.NaN if there are no features, are none has a non-NaN
592 public float getMaximumScore(boolean positional)
594 return positional ? positionalMaxScore : nonPositionalMaxScore;
598 * Answers the minimum score held for positional or non-positional features.
599 * This may be Float.NaN if there are no features, are none has a non-NaN
607 public float getMinimumScore(boolean positional)
609 return positional ? positionalMinScore : nonPositionalMinScore;
613 public List<SequenceFeature> getNonPositionalFeatures()
615 return getNonPositionalFeatures(new ArrayList<>());
619 * Answers a list of all non-positional features. If there are none, returns
620 * an immutable empty list.
626 public List<SequenceFeature> getNonPositionalFeatures(
627 List<SequenceFeature> result)
629 if (nonPositionalFeatures != null)
631 result.addAll(nonPositionalFeatures);
637 public List<SequenceFeature> getPositionalFeatures()
639 return getPositionalFeatures(new ArrayList<>());
643 * Answers a list of all positional features stored, in no guaranteed order
649 public List<SequenceFeature> getPositionalFeatures(
650 List<SequenceFeature> result)
654 * add any contact features - from the list by start position
656 if (contactFeatureStarts != null)
658 result.addAll(contactFeatureStarts);
662 * add any nested features
664 if (features != null)
666 result.addAll(features);
673 * Answers the total length of positional features (or zero if there are
674 * none). Contact features contribute a value of 1 to the total.
680 public int getTotalFeatureLength()
686 * Answers true if this store has no features, else false
692 public boolean isEmpty()
694 boolean hasFeatures = (contactFeatureStarts != null
695 && !contactFeatureStarts.isEmpty())
696 || (nonPositionalFeatures != null
697 && !nonPositionalFeatures.isEmpty())
698 || features.size() > 0;
704 * Rescan all features to recompute any cached values after an entry has been
705 * deleted. This is expected to be an infrequent event, so performance here is
708 protected synchronized void rescanAfterDelete()
710 positionalFeatureGroups.clear();
711 nonPositionalFeatureGroups.clear();
713 positionalMinScore = Float.NaN;
714 positionalMaxScore = Float.NaN;
715 nonPositionalMinScore = Float.NaN;
716 nonPositionalMaxScore = Float.NaN;
718 * scan non-positional features for groups and scores
720 if (nonPositionalFeatures != null)
722 List<SequenceFeature> list = nonPositionalFeatures;
723 for (int i = 0, n = list.size(); i < n; i++)
725 SequenceFeature sf = list.get(i);
726 nonPositionalFeatureGroups.add(sf.getFeatureGroup());
727 float score = sf.getScore();
728 nonPositionalMinScore = min(nonPositionalMinScore, score);
729 nonPositionalMaxScore = max(nonPositionalMaxScore, score);
734 * scan positional features for groups, scores and extents
737 rescanPositional(contactFeatureStarts);
738 rescanPositional(features);
741 private void rescanPositional(Collection<SequenceFeature> sfs)
747 for (SequenceFeature sf : sfs)
749 positionalFeatureGroups.add(sf.getFeatureGroup());
750 float score = sf.getScore();
751 positionalMinScore = min(positionalMinScore, score);
752 positionalMaxScore = max(positionalMaxScore, score);
753 totalExtent += getFeatureLength(sf);
758 * Adds the shift amount to the start and end of all positional features whose
759 * start position is at or after fromPosition. Returns true if at least one
760 * feature was shifted, else false.
762 * @param fromPosition
768 public synchronized boolean shiftFeatures(int fromPosition, int shiftBy)
771 * Because begin and end are final fields (to ensure the data store's
772 * integrity), we have to delete each feature and re-add it as amended.
773 * (Although a simple shift of all values would preserve data integrity!)
775 boolean modified = false;
776 List<SequenceFeature> list = getPositionalFeatures();
777 for (int i = 0, n = list.size(); i < n; i++)
779 SequenceFeature sf = list.get(i);
780 if (sf.getBegin() >= fromPosition)
783 int newBegin = sf.getBegin() + shiftBy;
784 int newEnd = sf.getEnd() + shiftBy;
787 * sanity check: don't shift left of the first residue
791 newBegin = Math.max(1, newBegin);
792 SequenceFeature sf2 = new SequenceFeature(sf, newBegin, newEnd,
793 sf.getFeatureGroup(), sf.getScore());