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> features,
66 SequenceFeature feature)
68 if (features == null || feature == null)
74 * locate the first entry in the list which does not precede the feature
76 int pos = findFirstBegin(features, feature.begin);
77 int len = features.size();
80 SequenceFeature sf = features.get(pos);
81 if (sf.getBegin() > feature.getBegin())
83 return false; // no match found
85 if (sf.equals(feature))
95 * A helper method to return the maximum of two floats, where a non-NaN value
96 * is treated as 'greater than' a NaN value (unlike Math.max which does the
102 protected static float max(float f1, float f2)
106 return Float.isNaN(f2) ? f1 : f2;
110 return Float.isNaN(f2) ? f1 : Math.max(f1, f2);
115 * A helper method to return the minimum of two floats, where a non-NaN value
116 * is treated as 'less than' a NaN value (unlike Math.min which does the
122 protected static float min(float f1, float f2)
126 return Float.isNaN(f2) ? f1 : f2;
130 return Float.isNaN(f2) ? f1 : Math.min(f1, f2);
135 * Non-positional features have no (zero) start/end position.
136 * Kept as a separate list in case this criterion changes in future.
138 List<SequenceFeature> nonPositionalFeatures;
141 * contact features ordered by first contact position
143 List<SequenceFeature> contactFeatureStarts;
146 * contact features ordered by second contact position
148 List<SequenceFeature> contactFeatureEnds;
151 * IntervalStore holds remaining features and provides efficient
152 * query for features overlapping any given interval
154 Collection<SequenceFeature> features;
157 * Feature groups represented in stored positional features
158 * (possibly including null)
160 Set<String> positionalFeatureGroups;
163 * Feature groups represented in stored non-positional features
164 * (possibly including null)
166 Set<String> nonPositionalFeatureGroups;
169 * the total length of all positional features; contact features count 1 to
170 * the total and 1 to size(), consistent with an average 'feature length' of 1
174 float positionalMinScore;
176 float positionalMaxScore;
178 float nonPositionalMinScore;
180 float nonPositionalMaxScore;
185 public FeatureStore()
187 positionalFeatureGroups = new HashSet<>();
188 nonPositionalFeatureGroups = new HashSet<>();
189 positionalMinScore = Float.NaN;
190 positionalMaxScore = Float.NaN;
191 nonPositionalMinScore = Float.NaN;
192 nonPositionalMaxScore = Float.NaN;
194 // we only construct nonPositionalFeatures, contactFeatures if we need to
198 * Add a contact feature to the lists that hold them ordered by start (first
199 * contact) and by end (second contact) position, ensuring the lists remain
200 * ordered, and returns true. This method allows duplicate features to be
201 * added, so test before calling to avoid this.
206 protected synchronized boolean addContactFeature(SequenceFeature feature)
208 if (contactFeatureStarts == null)
210 contactFeatureStarts = new ArrayList<>();
211 contactFeatureEnds = new ArrayList<>();
215 * insert into list sorted by start (first contact position):
216 * binary search the sorted list to find the insertion point
218 contactFeatureStarts.add(
219 findFirstBegin(contactFeatureStarts, feature.begin), feature);
221 * insert into list sorted by end (second contact position):
222 * binary search the sorted list to find the insertion point
224 contactFeatureEnds.add(findFirstEnd(contactFeatureEnds, feature.end),
231 * Adds one sequence feature to the store, and returns true, unless the
232 * feature is already contained in the store, in which case this method
233 * returns false. Containment is determined by SequenceFeature.equals()
240 public boolean addFeature(SequenceFeature feature)
242 if (contains(feature))
248 * keep a record of feature groups
250 if (!feature.isNonPositional())
252 positionalFeatureGroups.add(feature.getFeatureGroup());
255 if (feature.isContactFeature())
257 addContactFeature(feature);
259 else if (feature.isNonPositional())
261 addNonPositionalFeature(feature);
265 addNestedFeature(feature);
269 * record the total extent of positional features, to make
270 * getTotalFeatureLength possible; we count the length of a
271 * contact feature as 1
273 totalExtent += getFeatureLength(feature);
276 * record the minimum and maximum score for positional
277 * and non-positional features
279 float score = feature.getScore();
280 if (!Float.isNaN(score))
282 if (feature.isNonPositional())
284 nonPositionalMinScore = min(nonPositionalMinScore, score);
285 nonPositionalMaxScore = max(nonPositionalMaxScore, score);
289 positionalMinScore = min(positionalMinScore, score);
290 positionalMaxScore = max(positionalMaxScore, score);
297 private void addFeaturesForGroup(String group,
298 Collection<SequenceFeature> sfs, List<SequenceFeature> result)
304 for (SequenceFeature sf : sfs)
306 String featureGroup = sf.getFeatureGroup();
307 if (group == null && featureGroup == null
308 || group != null && group.equals(featureGroup))
316 * Adds one feature to the IntervalStore that can manage nested features
317 * (creating the IntervalStore if necessary)
319 protected synchronized void addNestedFeature(SequenceFeature feature)
321 features.add(feature);
325 * Adds the feature to the list of non-positional features (with lazy
326 * instantiation of the list if it is null), and returns true. The feature
327 * group is added to the set of distinct feature groups for non-positional
328 * features. This method allows duplicate features, so test before calling to
333 protected boolean addNonPositionalFeature(SequenceFeature feature)
335 if (nonPositionalFeatures == null)
337 nonPositionalFeatures = new ArrayList<>();
340 nonPositionalFeatures.add(feature);
342 nonPositionalFeatureGroups.add(feature.getFeatureGroup());
348 * Answers true if this store contains the given feature (testing by
349 * SequenceFeature.equals), else false
355 public boolean contains(SequenceFeature feature)
357 if (feature.isNonPositional())
359 return nonPositionalFeatures == null ? false
360 : nonPositionalFeatures.contains(feature);
363 if (feature.isContactFeature())
365 return contactFeatureStarts != null
366 && listContains(contactFeatureStarts, feature);
369 return features == null ? false : features.contains(feature);
373 * Deletes the given feature from the store, returning true if it was found
374 * (and deleted), else false. This method makes no assumption that the feature
375 * is in the 'expected' place in the store, in case it has been modified since
382 public synchronized boolean delete(SequenceFeature sf)
384 boolean removed = false;
387 * try contact positions (and if found, delete
388 * from both lists of contact positions)
390 if (!removed && contactFeatureStarts != null)
392 removed = contactFeatureStarts.remove(sf);
395 contactFeatureEnds.remove(sf);
399 boolean removedNonPositional = false;
402 * if not found, try non-positional features
404 if (!removed && nonPositionalFeatures != null)
406 removedNonPositional = nonPositionalFeatures.remove(sf);
407 removed = removedNonPositional;
411 * if not found, try nested features
413 if (!removed && features != null)
415 removed = features.remove(sf);
426 abstract protected void findContactFeatures(long from, long to,
427 List<SequenceFeature> result);
429 abstract protected int findFirstBegin(List<SequenceFeature> list,
432 abstract protected int findFirstEnd(List<SequenceFeature> list, long pos);
435 public List<SequenceFeature> findOverlappingFeatures(long start, long end)
437 return findOverlappingFeatures(start, end, null);
441 public List<SequenceFeature> getContactFeatures()
443 return getContactFeatures(new ArrayList<>());
447 * Answers a list of all contact features. If there are none, returns an
448 * immutable empty list.
454 public List<SequenceFeature> getContactFeatures(
455 List<SequenceFeature> result)
457 if (contactFeatureStarts != null)
459 result.addAll(contactFeatureStarts);
465 * Answers the number of positional (or non-positional) features stored.
466 * Contact features count as 1.
473 public int getFeatureCount(boolean positional)
477 return nonPositionalFeatures == null ? 0
478 : nonPositionalFeatures.size();
481 return (contactFeatureStarts == null ? 0 : contactFeatureStarts.size())
487 * Answers the set of distinct feature groups stored, possibly including null,
488 * as an unmodifiable view of the set. The parameter determines whether the
489 * groups for positional or for non-positional features are returned.
491 * @param positionalFeatures
496 public Set<String> getFeatureGroups(boolean positionalFeatures)
498 if (positionalFeatures)
500 return Collections.unmodifiableSet(positionalFeatureGroups);
504 return nonPositionalFeatureGroups == null
505 ? Collections.<String> emptySet()
506 : Collections.unmodifiableSet(nonPositionalFeatureGroups);
511 public Collection<SequenceFeature> getFeatures()
517 * Answers a list of all either positional or non-positional features whose
518 * feature group matches the given group (which may be null)
526 public List<SequenceFeature> getFeaturesForGroup(boolean positional,
529 List<SequenceFeature> result = new ArrayList<>();
532 * if we know features don't include the target group, no need
533 * to inspect them for matches
535 if (positional && !positionalFeatureGroups.contains(group)
536 || !positional && !nonPositionalFeatureGroups.contains(group))
543 addFeaturesForGroup(group, contactFeatureStarts, result);
544 addFeaturesForGroup(group, features, result);
548 addFeaturesForGroup(group, nonPositionalFeatures, result);
554 * Answers the maximum score held for positional or non-positional features.
555 * This may be Float.NaN if there are no features, are none has a non-NaN
563 public float getMaximumScore(boolean positional)
565 return positional ? positionalMaxScore : nonPositionalMaxScore;
569 * Answers the minimum score held for positional or non-positional features.
570 * This may be Float.NaN if there are no features, are none has a non-NaN
578 public float getMinimumScore(boolean positional)
580 return positional ? positionalMinScore : nonPositionalMinScore;
584 public List<SequenceFeature> getNonPositionalFeatures()
586 return getNonPositionalFeatures(new ArrayList<>());
590 * Answers a list of all non-positional features. If there are none, returns
591 * an immutable empty list.
597 public List<SequenceFeature> getNonPositionalFeatures(
598 List<SequenceFeature> result)
600 if (nonPositionalFeatures != null)
602 result.addAll(nonPositionalFeatures);
608 public List<SequenceFeature> getPositionalFeatures()
610 return getPositionalFeatures(new ArrayList<>());
614 * Answers a list of all positional features stored, in no guaranteed order
620 public List<SequenceFeature> getPositionalFeatures(
621 List<SequenceFeature> result)
625 * add any contact features - from the list by start position
627 if (contactFeatureStarts != null)
629 result.addAll(contactFeatureStarts);
633 * add any nested features
635 if (features != null)
637 result.addAll(features);
644 * Answers the total length of positional features (or zero if there are
645 * none). Contact features contribute a value of 1 to the total.
651 public int getTotalFeatureLength()
657 * Answers true if this store has no features, else false
663 public boolean isEmpty()
665 boolean hasFeatures = (contactFeatureStarts != null
666 && !contactFeatureStarts.isEmpty())
667 || (nonPositionalFeatures != null
668 && !nonPositionalFeatures.isEmpty())
669 || features.size() > 0;
675 * Rescan all features to recompute any cached values after an entry has been
676 * deleted. This is expected to be an infrequent event, so performance here is
679 protected synchronized void rescanAfterDelete()
681 positionalFeatureGroups.clear();
682 nonPositionalFeatureGroups.clear();
684 positionalMinScore = Float.NaN;
685 positionalMaxScore = Float.NaN;
686 nonPositionalMinScore = Float.NaN;
687 nonPositionalMaxScore = Float.NaN;
689 * scan non-positional features for groups and scores
691 if (nonPositionalFeatures != null)
693 for (SequenceFeature sf : nonPositionalFeatures)
695 nonPositionalFeatureGroups.add(sf.getFeatureGroup());
696 float score = sf.getScore();
697 nonPositionalMinScore = min(nonPositionalMinScore, score);
698 nonPositionalMaxScore = max(nonPositionalMaxScore, score);
703 * scan positional features for groups, scores and extents
706 rescanPositional(contactFeatureStarts);
707 rescanPositional(features);
710 private void rescanPositional(Collection<SequenceFeature> sfs)
716 for (SequenceFeature sf : sfs)
718 positionalFeatureGroups.add(sf.getFeatureGroup());
719 float score = sf.getScore();
720 positionalMinScore = min(positionalMinScore, score);
721 positionalMaxScore = max(positionalMaxScore, score);
722 totalExtent += getFeatureLength(sf);
727 * Adds the shift amount to the start and end of all positional features whose
728 * start position is at or after fromPosition. Returns true if at least one
729 * feature was shifted, else false.
731 * @param fromPosition
737 public synchronized boolean shiftFeatures(int fromPosition, int shiftBy)
740 * Because begin and end are final fields (to ensure the data store's
741 * integrity), we have to delete each feature and re-add it as amended.
742 * (Although a simple shift of all values would preserve data integrity!)
744 boolean modified = false;
745 for (SequenceFeature sf : getPositionalFeatures())
747 if (sf.getBegin() >= fromPosition)
750 int newBegin = sf.getBegin() + shiftBy;
751 int newEnd = sf.getEnd() + shiftBy;
754 * sanity check: don't shift left of the first residue
758 newBegin = Math.max(1, newBegin);
759 SequenceFeature sf2 = new SequenceFeature(sf, newBegin, newEnd,
760 sf.getFeatureGroup(), sf.getScore());