X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2Ffeatures%2FFeatureStore.java;h=fe575e0186e0ee76ed8cd259effc80c8b38d99f9;hb=99b6c1ca13af92cd8867971986a05664b0cd787c;hp=2cdbeb298dab735b9915418a7688311f22841ebd;hpb=5b833c4ea17f7e45fe1532b36219fcbfba3c0e4a;p=jalview.git diff --git a/src/jalview/datamodel/features/FeatureStore.java b/src/jalview/datamodel/features/FeatureStore.java index 2cdbeb2..fe575e0 100644 --- a/src/jalview/datamodel/features/FeatureStore.java +++ b/src/jalview/datamodel/features/FeatureStore.java @@ -29,11 +29,108 @@ import java.util.HashSet; import java.util.List; import java.util.Set; -import intervalstore.impl.BinarySearcher; - public abstract class FeatureStore implements FeatureStoreI { + /** + * Answers the 'length' of the feature, counting 0 for non-positional features + * and 1 for contact features + * + * @param feature + * @return + */ + protected static int getFeatureLength(SequenceFeature feature) + { + if (feature.isNonPositional()) + { + return 0; + } + if (feature.isContactFeature()) + { + return 1; + } + return 1 + feature.getEnd() - feature.getBegin(); + } + + /** + * Answers true if the list contains the feature, else false. This method is + * optimised for the condition that the list is sorted on feature start + * position ascending, and will give unreliable results if this does not hold. + * + * @param features + * @param feature + * @return + */ + @Override + public boolean listContains(List features, + SequenceFeature feature) + { + if (features == null || feature == null) + { + return false; + } + + /* + * locate the first entry in the list which does not precede the feature + */ + int pos = findFirstBegin(features, feature.begin); + int len = features.size(); + while (pos < len) + { + SequenceFeature sf = features.get(pos); + if (sf.getBegin() > feature.getBegin()) + { + return false; // no match found + } + if (sf.equals(feature)) + { + return true; + } + pos++; + } + return false; + } + + /** + * A helper method to return the maximum of two floats, where a non-NaN value + * is treated as 'greater than' a NaN value (unlike Math.max which does the + * opposite) + * + * @param f1 + * @param f2 + */ + protected static float max(float f1, float f2) + { + if (Float.isNaN(f1)) + { + return Float.isNaN(f2) ? f1 : f2; + } + else + { + return Float.isNaN(f2) ? f1 : Math.max(f1, f2); + } + } + + /** + * A helper method to return the minimum of two floats, where a non-NaN value + * is treated as 'less than' a NaN value (unlike Math.min which does the + * opposite) + * + * @param f1 + * @param f2 + */ + protected static float min(float f1, float f2) + { + if (Float.isNaN(f1)) + { + return Float.isNaN(f2) ? f1 : f2; + } + else + { + return Float.isNaN(f2) ? f1 : Math.min(f1, f2); + } + } + /* * Non-positional features have no (zero) start/end position. * Kept as a separate list in case this criterion changes in future. @@ -56,12 +153,6 @@ public abstract class FeatureStore implements FeatureStoreI */ Collection features; - @Override - public Collection getFeatures() - { - return features; - } - /* * Feature groups represented in stored positional features * (possibly including null) @@ -104,6 +195,39 @@ public abstract class FeatureStore implements FeatureStoreI } /** + * Add a contact feature to the lists that hold them ordered by start (first + * contact) and by end (second contact) position, ensuring the lists remain + * ordered, and returns true. This method allows duplicate features to be + * added, so test before calling to avoid this. + * + * @param feature + * @return + */ + protected synchronized boolean addContactFeature(SequenceFeature feature) + { + if (contactFeatureStarts == null) + { + contactFeatureStarts = new ArrayList<>(); + contactFeatureEnds = new ArrayList<>(); + } + + /* + * insert into list sorted by start (first contact position): + * binary search the sorted list to find the insertion point + */ + contactFeatureStarts.add( + findFirstBegin(contactFeatureStarts, feature.begin), feature); + /* + * insert into list sorted by end (second contact position): + * binary search the sorted list to find the insertion point + */ + contactFeatureEnds.add(findFirstEnd(contactFeatureEnds, feature.end), + feature); + + return true; + } + + /** * Adds one sequence feature to the store, and returns true, unless the * feature is already contained in the store, in which case this method * returns false. Containment is determined by SequenceFeature.equals() @@ -170,49 +294,31 @@ public abstract class FeatureStore implements FeatureStoreI return true; } - /** - * Answers true if this store contains the given feature (testing by - * SequenceFeature.equals), else false - * - * @param feature - * @return - */ - @Override - public boolean contains(SequenceFeature feature) + private void addFeaturesForGroup(String group, + Collection sfs, List result) { - if (feature.isNonPositional()) + if (sfs == null) { - return nonPositionalFeatures == null ? false - : nonPositionalFeatures.contains(feature); + return; } - - if (feature.isContactFeature()) + for (SequenceFeature sf : sfs) { - return contactFeatureStarts != null - && listContains(contactFeatureStarts, feature); + String featureGroup = sf.getFeatureGroup(); + if (group == null && featureGroup == null + || group != null && group.equals(featureGroup)) + { + result.add(sf); + } } - - return features == null ? false : features.contains(feature); } /** - * Answers the 'length' of the feature, counting 0 for non-positional features - * and 1 for contact features - * - * @param feature - * @return + * Adds one feature to the IntervalStore that can manage nested features + * (creating the IntervalStore if necessary) */ - protected static int getFeatureLength(SequenceFeature feature) + protected synchronized void addNestedFeature(SequenceFeature feature) { - if (feature.isNonPositional()) - { - return 0; - } - if (feature.isContactFeature()) - { - return 1; - } - return 1 + feature.getEnd() - feature.getBegin(); + features.add(feature); } /** @@ -239,183 +345,54 @@ public abstract class FeatureStore implements FeatureStoreI } /** - * Adds one feature to the IntervalStore that can manage nested features - * (creating the IntervalStore if necessary) - */ - protected synchronized void addNestedFeature(SequenceFeature feature) - { - features.add(feature); - } - /** - * Add a contact feature to the lists that hold them ordered by start (first - * contact) and by end (second contact) position, ensuring the lists remain - * ordered, and returns true. This method allows duplicate features to be - * added, so test before calling to avoid this. + * Answers true if this store contains the given feature (testing by + * SequenceFeature.equals), else false * * @param feature * @return */ - protected synchronized boolean addContactFeature(SequenceFeature feature) + @Override + public boolean contains(SequenceFeature feature) { - if (contactFeatureStarts == null) + if (feature.isNonPositional()) { - contactFeatureStarts = new ArrayList<>(); - contactFeatureEnds = new ArrayList<>(); + return nonPositionalFeatures == null ? false + : nonPositionalFeatures.contains(feature); } - /* - * insert into list sorted by start (first contact position): - * binary search the sorted list to find the insertion point - */ - int insertPosition = BinarySearcher.findFirst(contactFeatureStarts, - f -> f.getBegin() >= feature.getBegin()); - contactFeatureStarts.add(insertPosition, feature); - - /* - * insert into list sorted by end (second contact position): - * binary search the sorted list to find the insertion point - */ - insertPosition = BinarySearcher.findFirst(contactFeatureEnds, - f -> f.getEnd() >= feature.getEnd()); - contactFeatureEnds.add(insertPosition, feature); + if (feature.isContactFeature()) + { + return contactFeatureStarts != null + && listContains(contactFeatureStarts, feature); + } - return true; + return features == null ? false : features.contains(feature); } /** - * Answers true if the list contains the feature, else false. This method is - * optimised for the condition that the list is sorted on feature start - * position ascending, and will give unreliable results if this does not hold. + * Deletes the given feature from the store, returning true if it was found + * (and deleted), else false. This method makes no assumption that the feature + * is in the 'expected' place in the store, in case it has been modified since + * it was added. * - * @param features - * @param feature - * @return + * @param sf */ - protected static boolean listContains(List features, - SequenceFeature feature) + + @Override + public synchronized boolean delete(SequenceFeature sf) { - if (features == null || feature == null) - { - return false; - } + boolean removed = false; /* - * locate the first entry in the list which does not precede the feature + * try contact positions (and if found, delete + * from both lists of contact positions) */ - // int pos = binarySearch(features, - // SearchCriterion.byFeature(feature, RangeComparator.BY_START_POSITION)); - int pos = BinarySearcher.findFirst(features, - val -> val.getBegin() >= feature.getBegin()); - int len = features.size(); - while (pos < len) + if (!removed && contactFeatureStarts != null) { - SequenceFeature sf = features.get(pos); - if (sf.getBegin() > feature.getBegin()) - { - return false; // no match found - } - if (sf.equals(feature)) + removed = contactFeatureStarts.remove(sf); + if (removed) { - return true; - } - pos++; - } - return false; - } - - abstract protected void findContactFeatures(long from, long to, - List result); - - /** - * Answers a list of all positional features stored, in no guaranteed order - * - * @return - */ - - @Override - public List getPositionalFeatures( - List result) - { - - /* - * add any contact features - from the list by start position - */ - if (contactFeatureStarts != null) - { - result.addAll(contactFeatureStarts); - } - - /* - * add any nested features - */ - if (features != null) - { - result.addAll(features); - } - - return result; - } - - /** - * Answers a list of all contact features. If there are none, returns an - * immutable empty list. - * - * @return - */ - - @Override - public List getContactFeatures( - List result) - { - if (contactFeatureStarts != null) - { - result.addAll(contactFeatureStarts); - } - return result; - } - - /** - * Answers a list of all non-positional features. If there are none, returns - * an immutable empty list. - * - * @return - */ - - @Override - public List getNonPositionalFeatures( - List result) - { - if (nonPositionalFeatures != null) - { - result.addAll(nonPositionalFeatures); - } - return result; - } - - /** - * Deletes the given feature from the store, returning true if it was found - * (and deleted), else false. This method makes no assumption that the feature - * is in the 'expected' place in the store, in case it has been modified since - * it was added. - * - * @param sf - */ - - @Override - public synchronized boolean delete(SequenceFeature sf) - { - boolean removed = false; - - /* - * try contact positions (and if found, delete - * from both lists of contact positions) - */ - if (!removed && contactFeatureStarts != null) - { - removed = contactFeatureStarts.remove(sf); - if (removed) - { - contactFeatureEnds.remove(sf); + contactFeatureEnds.remove(sf); } } @@ -446,115 +423,64 @@ public abstract class FeatureStore implements FeatureStoreI return removed; } - /** - * Rescan all features to recompute any cached values after an entry has been - * deleted. This is expected to be an infrequent event, so performance here is - * not critical. - */ - protected synchronized void rescanAfterDelete() - { - positionalFeatureGroups.clear(); - nonPositionalFeatureGroups.clear(); - totalExtent = 0; - positionalMinScore = Float.NaN; - positionalMaxScore = Float.NaN; - nonPositionalMinScore = Float.NaN; - nonPositionalMaxScore = Float.NaN; - /* - * scan non-positional features for groups and scores - */ - if (nonPositionalFeatures != null) - { - for (SequenceFeature sf : nonPositionalFeatures) - { - nonPositionalFeatureGroups.add(sf.getFeatureGroup()); - float score = sf.getScore(); - nonPositionalMinScore = min(nonPositionalMinScore, score); - nonPositionalMaxScore = max(nonPositionalMaxScore, score); - } - } + abstract protected void findContactFeatures(long from, long to, + List result); - /* - * scan positional features for groups, scores and extents - */ + abstract protected int findFirstBegin(List list, + long pos); - rescanPositional(contactFeatureStarts); - rescanPositional(features); - } + abstract protected int findFirstEnd(List list, long pos); - private void rescanPositional(Collection sfs) + @Override + public List findOverlappingFeatures(long start, long end) { - if (sfs == null) - { - return; - } - for (SequenceFeature sf : sfs) - { - positionalFeatureGroups.add(sf.getFeatureGroup()); - float score = sf.getScore(); - positionalMinScore = min(positionalMinScore, score); - positionalMaxScore = max(positionalMaxScore, score); - totalExtent += getFeatureLength(sf); - } + return findOverlappingFeatures(start, end, null); } - /** - * A helper method to return the minimum of two floats, where a non-NaN value - * is treated as 'less than' a NaN value (unlike Math.min which does the - * opposite) - * - * @param f1 - * @param f2 - */ - protected static float min(float f1, float f2) + @Override + public List getContactFeatures() { - if (Float.isNaN(f1)) - { - return Float.isNaN(f2) ? f1 : f2; - } - else - { - return Float.isNaN(f2) ? f1 : Math.min(f1, f2); - } + return getContactFeatures(new ArrayList<>()); } /** - * A helper method to return the maximum of two floats, where a non-NaN value - * is treated as 'greater than' a NaN value (unlike Math.max which does the - * opposite) + * Answers a list of all contact features. If there are none, returns an + * immutable empty list. * - * @param f1 - * @param f2 + * @return */ - protected static float max(float f1, float f2) + + @Override + public List getContactFeatures( + List result) { - if (Float.isNaN(f1)) - { - return Float.isNaN(f2) ? f1 : f2; - } - else + if (contactFeatureStarts != null) { - return Float.isNaN(f2) ? f1 : Math.max(f1, f2); + result.addAll(contactFeatureStarts); } + return result; } - /** - * Answers true if this store has no features, else false + * Answers the number of positional (or non-positional) features stored. + * Contact features count as 1. * + * @param positional * @return */ @Override - public boolean isEmpty() + public int getFeatureCount(boolean positional) { - boolean hasFeatures = (contactFeatureStarts != null - && !contactFeatureStarts.isEmpty()) - || (nonPositionalFeatures != null - && !nonPositionalFeatures.isEmpty()) - || features.size() > 0; + if (!positional) + { + return nonPositionalFeatures == null ? 0 + : nonPositionalFeatures.size(); + } + + return (contactFeatureStarts == null ? 0 : contactFeatureStarts.size()) + + features.size(); - return !hasFeatures; } /** @@ -581,6 +507,12 @@ public abstract class FeatureStore implements FeatureStoreI } } + @Override + public Collection getFeatures() + { + return features; + } + /** * Answers a list of all either positional or non-positional features whose * feature group matches the given group (which may be null) @@ -618,47 +550,96 @@ public abstract class FeatureStore implements FeatureStoreI return result; } - private void addFeaturesForGroup(String group, - Collection sfs, List result) + /** + * Answers the maximum score held for positional or non-positional features. + * This may be Float.NaN if there are no features, are none has a non-NaN + * score. + * + * @param positional + * @return + */ + + @Override + public float getMaximumScore(boolean positional) { - if (sfs == null) - { - return; - } - for (SequenceFeature sf : sfs) + return positional ? positionalMaxScore : nonPositionalMaxScore; + } + + /** + * Answers the minimum score held for positional or non-positional features. + * This may be Float.NaN if there are no features, are none has a non-NaN + * score. + * + * @param positional + * @return + */ + + @Override + public float getMinimumScore(boolean positional) + { + return positional ? positionalMinScore : nonPositionalMinScore; + } + + @Override + public List getNonPositionalFeatures() + { + return getNonPositionalFeatures(new ArrayList<>()); + } + + /** + * Answers a list of all non-positional features. If there are none, returns + * an immutable empty list. + * + * @return + */ + + @Override + public List getNonPositionalFeatures( + List result) + { + if (nonPositionalFeatures != null) { - String featureGroup = sf.getFeatureGroup(); - if (group == null && featureGroup == null - || group != null && group.equals(featureGroup)) - { - result.add(sf); - } + result.addAll(nonPositionalFeatures); } + return result; + } + + @Override + public List getPositionalFeatures() + { + return getPositionalFeatures(new ArrayList<>()); } /** - * Answers the number of positional (or non-positional) features stored. - * Contact features count as 1. + * Answers a list of all positional features stored, in no guaranteed order * - * @param positional * @return */ @Override - public int getFeatureCount(boolean positional) + public List getPositionalFeatures( + List result) { - if (!positional) + + /* + * add any contact features - from the list by start position + */ + if (contactFeatureStarts != null) { - return nonPositionalFeatures == null ? 0 - : nonPositionalFeatures.size(); + result.addAll(contactFeatureStarts); } - return (contactFeatureStarts == null ? 0 : contactFeatureStarts.size()) - + features.size(); + /* + * add any nested features + */ + if (features != null) + { + result.addAll(features); + } + return result; } - /** * Answers the total length of positional features (or zero if there are * none). Contact features contribute a value of 1 to the total. @@ -673,35 +654,74 @@ public abstract class FeatureStore implements FeatureStoreI } /** - * Answers the minimum score held for positional or non-positional features. - * This may be Float.NaN if there are no features, are none has a non-NaN - * score. + * Answers true if this store has no features, else false * - * @param positional * @return */ @Override - public float getMinimumScore(boolean positional) + public boolean isEmpty() { - return positional ? positionalMinScore : nonPositionalMinScore; + boolean hasFeatures = (contactFeatureStarts != null + && !contactFeatureStarts.isEmpty()) + || (nonPositionalFeatures != null + && !nonPositionalFeatures.isEmpty()) + || features.size() > 0; + + return !hasFeatures; } /** - * Answers the maximum score held for positional or non-positional features. - * This may be Float.NaN if there are no features, are none has a non-NaN - * score. - * - * @param positional - * @return + * Rescan all features to recompute any cached values after an entry has been + * deleted. This is expected to be an infrequent event, so performance here is + * not critical. */ - - @Override - public float getMaximumScore(boolean positional) + protected synchronized void rescanAfterDelete() { - return positional ? positionalMaxScore : nonPositionalMaxScore; + positionalFeatureGroups.clear(); + nonPositionalFeatureGroups.clear(); + totalExtent = 0; + positionalMinScore = Float.NaN; + positionalMaxScore = Float.NaN; + nonPositionalMinScore = Float.NaN; + nonPositionalMaxScore = Float.NaN; + /* + * scan non-positional features for groups and scores + */ + if (nonPositionalFeatures != null) + { + for (SequenceFeature sf : nonPositionalFeatures) + { + nonPositionalFeatureGroups.add(sf.getFeatureGroup()); + float score = sf.getScore(); + nonPositionalMinScore = min(nonPositionalMinScore, score); + nonPositionalMaxScore = max(nonPositionalMaxScore, score); + } + } + + /* + * scan positional features for groups, scores and extents + */ + + rescanPositional(contactFeatureStarts); + rescanPositional(features); } + private void rescanPositional(Collection sfs) + { + if (sfs == null) + { + return; + } + for (SequenceFeature sf : sfs) + { + positionalFeatureGroups.add(sf.getFeatureGroup()); + float score = sf.getScore(); + positionalMinScore = min(positionalMinScore, score); + positionalMaxScore = max(positionalMaxScore, score); + totalExtent += getFeatureLength(sf); + } + } /** * Adds the shift amount to the start and end of all positional features whose @@ -746,29 +766,4 @@ public abstract class FeatureStore implements FeatureStoreI return modified; } - - @Override - public List findOverlappingFeatures(long start, long end) - { - return findOverlappingFeatures(start, end, null); - } - - @Override - public List getPositionalFeatures() - { - return getPositionalFeatures(new ArrayList<>()); - } - - @Override - public List getContactFeatures() - { - return getContactFeatures(new ArrayList<>()); - } - - @Override - public List getNonPositionalFeatures() - { - return getNonPositionalFeatures(new ArrayList<>()); - } - }