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 * Answers the 'length' of the feature, counting 0 for non-positional features
37 * and 1 for contact features
42 protected static int getFeatureLength(SequenceFeature feature)
44 if (feature.isNonPositional())
48 if (feature.isContactFeature())
52 return 1 + feature.getEnd() - feature.getBegin();
56 * Answers true if the list contains the feature, else false. This method is
57 * optimised for the condition that the list is sorted on feature start
58 * position ascending, and will give unreliable results if this does not hold.
65 public boolean listContains(List<SequenceFeature> list,
66 SequenceFeature feature)
68 if (list == null || feature == null)
73 return (getEquivalentFeatureIndex(list, feature) >= 0);
77 * Binary search for the index (>= 0) of a feature in a list.
81 * @return index if found; -1 if not
83 protected int getEquivalentFeatureIndex(List<SequenceFeature> list,
84 SequenceFeature feature)
88 * locate the first entry in the list which does not precede the feature
90 int pos = findFirstBegin(list, feature.begin);
91 int len = list.size();
94 SequenceFeature sf = list.get(pos);
95 if (sf.begin > feature.begin)
97 return -1; // no match found
99 if (sf.equals(feature))
109 * A helper method to return the maximum of two floats, where a non-NaN value
110 * is treated as 'greater than' a NaN value (unlike Math.max which does the
116 protected static float max(float f1, float f2)
120 return Float.isNaN(f2) ? f1 : f2;
124 return Float.isNaN(f2) ? f1 : Math.max(f1, f2);
129 * A helper method to return the minimum of two floats, where a non-NaN value
130 * is treated as 'less than' a NaN value (unlike Math.min which does the
136 protected static float min(float f1, float f2)
140 return Float.isNaN(f2) ? f1 : f2;
144 return Float.isNaN(f2) ? f1 : Math.min(f1, f2);
149 * Non-positional features have no (zero) start/end position.
150 * Kept as a separate list in case this criterion changes in future.
152 List<SequenceFeature> nonPositionalFeatures;
155 * contact features ordered by first contact position
157 List<SequenceFeature> contactFeatureStarts;
160 * contact features ordered by second contact position
162 List<SequenceFeature> contactFeatureEnds;
165 * IntervalStore holds remaining features and provides efficient
166 * query for features overlapping any given interval
168 Collection<SequenceFeature> features;
171 * Feature groups represented in stored positional features
172 * (possibly including null)
174 Set<String> positionalFeatureGroups;
177 * Feature groups represented in stored non-positional features
178 * (possibly including null)
180 Set<String> nonPositionalFeatureGroups;
183 * the total length of all positional features; contact features count 1 to
184 * the total and 1 to size(), consistent with an average 'feature length' of 1
188 float positionalMinScore;
190 float positionalMaxScore;
192 float nonPositionalMinScore;
194 float nonPositionalMaxScore;
199 public FeatureStore()
201 positionalFeatureGroups = new HashSet<>();
202 nonPositionalFeatureGroups = new HashSet<>();
203 positionalMinScore = Float.NaN;
204 positionalMaxScore = Float.NaN;
205 nonPositionalMinScore = Float.NaN;
206 nonPositionalMaxScore = Float.NaN;
208 // we only construct nonPositionalFeatures, contactFeatures if we need to
212 * Add a contact feature to the lists that hold them ordered by start (first
213 * contact) and by end (second contact) position, ensuring the lists remain
214 * ordered, and returns true. This method allows duplicate features to be
215 * added, so test before calling to avoid this.
220 protected synchronized boolean addContactFeature(SequenceFeature feature)
222 if (contactFeatureStarts == null)
224 contactFeatureStarts = new ArrayList<>();
225 contactFeatureEnds = new ArrayList<>();
229 * insert into list sorted by start (first contact position):
230 * binary search the sorted list to find the insertion point
232 contactFeatureStarts.add(
233 findFirstBegin(contactFeatureStarts, feature.begin), feature);
235 * insert into list sorted by end (second contact position):
236 * binary search the sorted list to find the insertion point
238 contactFeatureEnds.add(findFirstEnd(contactFeatureEnds, feature.end),
245 * Adds one sequence feature to the store, and returns true, unless the
246 * feature is already contained in the store, in which case this method
247 * returns false. Containment is determined by SequenceFeature.equals()
254 public boolean addFeature(SequenceFeature feature)
256 if (contains(feature))
262 * keep a record of feature groups
264 if (!feature.isNonPositional())
266 positionalFeatureGroups.add(feature.getFeatureGroup());
269 if (feature.isContactFeature())
271 addContactFeature(feature);
273 else if (feature.isNonPositional())
275 addNonPositionalFeature(feature);
279 addPositionalFeature(feature);
283 * record the total extent of positional features, to make
284 * getTotalFeatureLength possible; we count the length of a
285 * contact feature as 1
287 totalExtent += getFeatureLength(feature);
290 * record the minimum and maximum score for positional
291 * and non-positional features
293 float score = feature.getScore();
294 if (!Float.isNaN(score))
296 if (feature.isNonPositional())
298 nonPositionalMinScore = min(nonPositionalMinScore, score);
299 nonPositionalMaxScore = max(nonPositionalMaxScore, score);
303 positionalMinScore = min(positionalMinScore, score);
304 positionalMaxScore = max(positionalMaxScore, score);
311 private void addFeaturesForGroup(String group,
312 Collection<SequenceFeature> sfs, List<SequenceFeature> result)
318 for (SequenceFeature sf : sfs)
320 String featureGroup = sf.getFeatureGroup();
321 if (group == null && featureGroup == null
322 || group != null && group.equals(featureGroup))
330 * Adds one feature to the IntervalStore that can manage nested features
331 * (creating the IntervalStore if necessary)
333 abstract protected void addPositionalFeature(SequenceFeature feature);
336 * Adds the feature to the list of non-positional features (with lazy
337 * instantiation of the list if it is null), and returns true. The feature
338 * group is added to the set of distinct feature groups for non-positional
339 * features. This method allows duplicate features, so test before calling to
344 protected boolean addNonPositionalFeature(SequenceFeature feature)
346 if (nonPositionalFeatures == null)
348 nonPositionalFeatures = new ArrayList<>();
351 nonPositionalFeatures.add(feature);
353 nonPositionalFeatureGroups.add(feature.getFeatureGroup());
359 * Answers true if this store contains the given feature (testing by
360 * SequenceFeature.equals), else false
366 public boolean contains(SequenceFeature feature)
368 if (feature.isNonPositional())
370 return nonPositionalFeatures == null ? false
371 : nonPositionalFeatures.contains(feature);
374 if (feature.isContactFeature())
376 return contactFeatureStarts != null
377 && listContains(contactFeatureStarts, feature);
380 return features == null ? false : containsFeature(feature);
384 abstract protected boolean containsFeature(SequenceFeature feature);
387 * Deletes the given feature from the store, returning true if it was found
388 * (and deleted), else false. This method makes no assumption that the feature
389 * is in the 'expected' place in the store, in case it has been modified since
396 public synchronized boolean delete(SequenceFeature sf)
398 boolean removed = false;
401 * try contact positions (and if found, delete
402 * from both lists of contact positions)
404 if (!removed && contactFeatureStarts != null)
406 removed = contactFeatureStarts.remove(sf);
409 contactFeatureEnds.remove(sf);
414 * if not found, try non-positional features
416 if (!removed && nonPositionalFeatures != null)
418 removed = nonPositionalFeatures.remove(sf);
422 * if not found, try nested features
424 if (!removed && features != null)
426 removed = findAndRemoveNonContactFeature(sf);
437 abstract protected boolean findAndRemoveNonContactFeature(SequenceFeature sf);
439 abstract protected void findContactFeatures(long from, long to,
440 List<SequenceFeature> result);
442 abstract protected int findFirstBegin(List<SequenceFeature> list,
445 abstract protected int findFirstEnd(List<SequenceFeature> list, long pos);
448 public List<SequenceFeature> findOverlappingFeatures(long start, long end)
450 return findOverlappingFeatures(start, end, null);
454 public List<SequenceFeature> getContactFeatures()
456 return getContactFeatures(new ArrayList<>());
460 * Answers a list of all contact features. If there are none, returns an
461 * immutable empty list.
467 public List<SequenceFeature> getContactFeatures(
468 List<SequenceFeature> result)
470 if (contactFeatureStarts != null)
472 result.addAll(contactFeatureStarts);
478 * Answers the number of positional (or non-positional) features stored.
479 * Contact features count as 1.
486 public int getFeatureCount(boolean positional)
490 return nonPositionalFeatures == null ? 0
491 : nonPositionalFeatures.size();
494 return (contactFeatureStarts == null ? 0 : contactFeatureStarts.size())
500 * Answers the set of distinct feature groups stored, possibly including null,
501 * as an unmodifiable view of the set. The parameter determines whether the
502 * groups for positional or for non-positional features are returned.
504 * @param positionalFeatures
509 public Set<String> getFeatureGroups(boolean positionalFeatures)
511 if (positionalFeatures)
513 return Collections.unmodifiableSet(positionalFeatureGroups);
517 return nonPositionalFeatureGroups == null
518 ? Collections.<String> emptySet()
519 : Collections.unmodifiableSet(nonPositionalFeatureGroups);
524 public Collection<SequenceFeature> getFeatures()
530 * Answers a list of all either positional or non-positional features whose
531 * feature group matches the given group (which may be null)
539 public List<SequenceFeature> getFeaturesForGroup(boolean positional,
542 List<SequenceFeature> result = new ArrayList<>();
545 * if we know features don't include the target group, no need
546 * to inspect them for matches
548 if (positional && !positionalFeatureGroups.contains(group)
549 || !positional && !nonPositionalFeatureGroups.contains(group))
556 addFeaturesForGroup(group, contactFeatureStarts, result);
557 addFeaturesForGroup(group, features, result);
561 addFeaturesForGroup(group, nonPositionalFeatures, result);
567 * Answers the maximum score held for positional or non-positional features.
568 * This may be Float.NaN if there are no features, are none has a non-NaN
576 public float getMaximumScore(boolean positional)
578 return positional ? positionalMaxScore : nonPositionalMaxScore;
582 * Answers the minimum score held for positional or non-positional features.
583 * This may be Float.NaN if there are no features, are none has a non-NaN
591 public float getMinimumScore(boolean positional)
593 return positional ? positionalMinScore : nonPositionalMinScore;
597 public List<SequenceFeature> getNonPositionalFeatures()
599 return getNonPositionalFeatures(new ArrayList<>());
603 * Answers a list of all non-positional features. If there are none, returns
604 * an immutable empty list.
610 public List<SequenceFeature> getNonPositionalFeatures(
611 List<SequenceFeature> result)
613 if (nonPositionalFeatures != null)
615 result.addAll(nonPositionalFeatures);
621 public List<SequenceFeature> getPositionalFeatures()
623 return getPositionalFeatures(new ArrayList<>());
627 * Answers a list of all positional features stored, in no guaranteed order
633 public List<SequenceFeature> getPositionalFeatures(
634 List<SequenceFeature> result)
638 * add any contact features - from the list by start position
640 if (contactFeatureStarts != null)
642 result.addAll(contactFeatureStarts);
646 * add any nested features
648 if (features != null)
650 result.addAll(features);
657 * Answers the total length of positional features (or zero if there are
658 * none). Contact features contribute a value of 1 to the total.
664 public int getTotalFeatureLength()
670 * Answers true if this store has no features, else false
676 public boolean isEmpty()
678 boolean hasFeatures = (contactFeatureStarts != null
679 && !contactFeatureStarts.isEmpty())
680 || (nonPositionalFeatures != null
681 && !nonPositionalFeatures.isEmpty())
682 || features.size() > 0;
688 * Rescan all features to recompute any cached values after an entry has been
689 * deleted. This is expected to be an infrequent event, so performance here is
692 protected synchronized void rescanAfterDelete()
694 positionalFeatureGroups.clear();
695 nonPositionalFeatureGroups.clear();
697 positionalMinScore = Float.NaN;
698 positionalMaxScore = Float.NaN;
699 nonPositionalMinScore = Float.NaN;
700 nonPositionalMaxScore = Float.NaN;
702 * scan non-positional features for groups and scores
704 if (nonPositionalFeatures != null)
706 List<SequenceFeature> list = nonPositionalFeatures;
707 for (int i = 0, n = list.size(); i < n; i++)
709 SequenceFeature sf = list.get(i);
710 nonPositionalFeatureGroups.add(sf.getFeatureGroup());
711 float score = sf.getScore();
712 nonPositionalMinScore = min(nonPositionalMinScore, score);
713 nonPositionalMaxScore = max(nonPositionalMaxScore, score);
718 * scan positional features for groups, scores and extents
721 rescanPositional(contactFeatureStarts);
722 rescanPositional(features);
725 private void rescanPositional(Collection<SequenceFeature> sfs)
731 for (SequenceFeature sf : sfs)
733 positionalFeatureGroups.add(sf.getFeatureGroup());
734 float score = sf.getScore();
735 positionalMinScore = min(positionalMinScore, score);
736 positionalMaxScore = max(positionalMaxScore, score);
737 totalExtent += getFeatureLength(sf);
742 * Adds the shift amount to the start and end of all positional features whose
743 * start position is at or after fromPosition. Returns true if at least one
744 * feature was shifted, else false.
746 * @param fromPosition
752 public synchronized boolean shiftFeatures(int fromPosition, int shiftBy)
755 * Because begin and end are final fields (to ensure the data store's
756 * integrity), we have to delete each feature and re-add it as amended.
757 * (Although a simple shift of all values would preserve data integrity!)
759 boolean modified = false;
760 List<SequenceFeature> list = getPositionalFeatures();
761 for (int i = 0, n = list.size(); i < n; i++)
763 SequenceFeature sf = list.get(i);
764 if (sf.getBegin() >= fromPosition)
767 int newBegin = sf.getBegin() + shiftBy;
768 int newEnd = sf.getEnd() + shiftBy;
771 * sanity check: don't shift left of the first residue
775 newBegin = Math.max(1, newBegin);
776 SequenceFeature sf2 = new SequenceFeature(sf, newBegin, newEnd,
777 sf.getFeatureGroup(), sf.getScore());