JAL-3253-applet JAL-3397
[jalview.git] / src / jalview / datamodel / features / FeatureStore.java
index 1451892..615b340 100644 (file)
@@ -21,6 +21,7 @@
 package jalview.datamodel.features;
 
 import jalview.datamodel.SequenceFeature;
+import jalview.util.Platform;
 
 import java.util.ArrayList;
 import java.util.Collection;
@@ -29,13 +30,94 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
-public abstract class FeatureStore implements FeatureStoreI
+import intervalstore.api.IntervalStoreI;
+import intervalstore.impl.BinarySearcher;
+import intervalstore.impl.BinarySearcher.Compare;
+
+public class FeatureStore
 {
+  /*
+   * track last start for quick insertion of ordered features
+   */
+  protected int lastStart = -1;
+
+  protected int lastContactStart = -1;
+
+  /*
+   * Non-positional features have no (zero) start/end position.
+   * Kept as a separate list in case this criterion changes in future.
+   */
+  List<SequenceFeature> nonPositionalFeatures;
+
+  /*
+   * contact features ordered by first contact position
+   */
+  List<SequenceFeature> contactFeatureStarts;
+
+  /*
+   * contact features ordered by second contact position
+   */
+  List<SequenceFeature> contactFeatureEnds;
+
+  /*
+   * IntervalStore holds remaining features and provides efficient
+   * query for features overlapping any given interval
+   */
+  IntervalStoreI<SequenceFeature> features;
+
+  /*
+   * Feature groups represented in stored positional features 
+   * (possibly including null)
+   */
+  Set<String> positionalFeatureGroups;
+
+  /*
+   * Feature groups represented in stored non-positional features 
+   * (possibly including null)
+   */
+  Set<String> nonPositionalFeatureGroups;
+
+  /*
+   * the total length of all positional features; contact features count 1 to
+   * the total and 1 to size(), consistent with an average 'feature length' of 1
+   */
+  int totalExtent;
+
+  float positionalMinScore;
+
+  float positionalMaxScore;
+
+  float nonPositionalMinScore;
+
+  float nonPositionalMaxScore;
+
+  public final static int INTERVAL_STORE_DEFAULT = -1;
 
   /**
-   * track last start for quick insertion of ordered features
+   * original NCList-based IntervalStore
    */
-  protected int lastStart = -1, lastContactStart = -1;
+  public final static int INTERVAL_STORE_NCLIST_OBJECT = 0;
+
+  /**
+   * linked-list IntervalStore
+   */
+  public final static int INTERVAL_STORE_LINKED_LIST = 1;
+
+  /**
+   * NCList as array buffer IntervalStore
+   */
+  public final static int INTERVAL_STORE_NCARRAY = 3;
+
+  static final int intervalStoreJavaOption = INTERVAL_STORE_NCLIST_OBJECT;
+
+  private final static boolean isJSLinkedTest = false;
+
+  static final int intervalStoreJSOption = (isJSLinkedTest
+          ? INTERVAL_STORE_LINKED_LIST
+          : INTERVAL_STORE_NCARRAY);
+
+  // TODO: compare performance in real situations using
+  // INTERVAL_STORE_LINKED_LIST;
 
   /**
    * Answers the 'length' of the feature, counting 0 for non-positional features
@@ -66,7 +148,6 @@ public abstract class FeatureStore implements FeatureStoreI
    * @param feature
    * @return
    */
-  @Override
   public boolean listContains(List<SequenceFeature> list,
           SequenceFeature feature)
   {
@@ -75,40 +156,26 @@ public abstract class FeatureStore implements FeatureStoreI
       return false;
     }
 
-    return (getEquivalentFeatureIndex(list, feature) >= 0);
-  }
-
-  /**
-   * Binary search for the index (&gt;= 0) of a feature in a list.
-   * 
-   * @param list
-   * @param feature
-   * @return index if found; -1 if not
-   */
-  protected int getEquivalentFeatureIndex(List<SequenceFeature> list,
-          SequenceFeature feature)
-  {
-
     /*
      * locate the first entry in the list which does not precede the feature
      */
     int begin = feature.begin;
-    int pos = findFirstBegin(list, begin);
+    int pos = BinarySearcher.findFirst(list, true, Compare.GE, begin);
     int len = list.size();
     while (pos < len)
     {
       SequenceFeature sf = list.get(pos);
       if (sf.begin > begin)
       {
-        return -1; // no match found
+        return false; // no match found
       }
       if (sf.equals(feature))
       {
-        return pos;
+        return true;
       }
       pos++;
     }
-    return -1;
+    return false;
   }
 
   /**
@@ -151,59 +218,24 @@ public abstract class FeatureStore implements FeatureStoreI
     }
   }
 
-  /*
-   * Non-positional features have no (zero) start/end position.
-   * Kept as a separate list in case this criterion changes in future.
-   */
-  List<SequenceFeature> nonPositionalFeatures;
-
-  /*
-   * contact features ordered by first contact position
-   */
-  List<SequenceFeature> contactFeatureStarts;
-
-  /*
-   * contact features ordered by second contact position
-   */
-  List<SequenceFeature> contactFeatureEnds;
-
-  /*
-   * IntervalStore holds remaining features and provides efficient
-   * query for features overlapping any given interval
-   */
-  Collection<SequenceFeature> features;
-
-  /*
-   * Feature groups represented in stored positional features 
-   * (possibly including null)
-   */
-  Set<String> positionalFeatureGroups;
-
-  /*
-   * Feature groups represented in stored non-positional features 
-   * (possibly including null)
-   */
-  Set<String> nonPositionalFeatureGroups;
-
-  /*
-   * the total length of all positional features; contact features count 1 to
-   * the total and 1 to size(), consistent with an average 'feature length' of 1
+  /**
+   * standard constructor
    */
-  int totalExtent;
-
-  float positionalMinScore;
-
-  float positionalMaxScore;
-
-  float nonPositionalMinScore;
-
-  float nonPositionalMaxScore;
+  public FeatureStore()
+  {
+    this(INTERVAL_STORE_DEFAULT);
+  }
 
   /**
-   * Constructor
+   * constructor for testing only
    */
-  public FeatureStore()
+  public FeatureStore(int intervalStoreType)
   {
+    features =
+            // Platform.isJS()
+            // ? new intervalstore.nonc.IntervalStore<>(true)
+            // : new intervalstore.impl.IntervalStore<>();
+            getIntervalStore(intervalStoreType);
     positionalFeatureGroups = new HashSet<>();
     nonPositionalFeatureGroups = new HashSet<>();
     positionalMinScore = Float.NaN;
@@ -214,6 +246,23 @@ public abstract class FeatureStore implements FeatureStoreI
     // we only construct nonPositionalFeatures, contactFeatures if we need to
   }
 
+  private IntervalStoreI<SequenceFeature> getIntervalStore(int type)
+  {
+    switch (type != INTERVAL_STORE_DEFAULT ? type : //
+            Platform.isJS() //
+                    ? intervalStoreJSOption
+                    : intervalStoreJavaOption)
+    {
+    default:
+    case INTERVAL_STORE_NCLIST_OBJECT:
+      return new intervalstore.impl.IntervalStore<>();
+    case INTERVAL_STORE_NCARRAY:
+      return new intervalstore.nonc.IntervalStoreImpl();
+    case INTERVAL_STORE_LINKED_LIST:
+      return new intervalstore.nonc.IntervalStore0Impl();
+    }
+  }
+
   /**
    * 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
@@ -235,8 +284,9 @@ public abstract class FeatureStore implements FeatureStoreI
      * 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);
+    int insertAt = BinarySearcher.findFirst(contactFeatureStarts, true,
+            Compare.GE, feature.begin);
+    contactFeatureStarts.add(insertAt, feature);
     /*
      * insert into list sorted by end (second contact position):
      * binary search the sorted list to find the insertion point
@@ -255,23 +305,8 @@ public abstract class FeatureStore implements FeatureStoreI
    * 
    * @param feature
    */
-
-  @Override
   public boolean addFeature(SequenceFeature feature)
   {
-    // if (contains(feature))
-    // {
-    // return false;
-    // }
-
-    // /*
-    // * keep a record of feature groups
-    // */
-    // if (!feature.isNonPositional())
-    // {
-    // positionalFeatureGroups.add(feature.getFeatureGroup());
-    // }
-
     if (feature.isContactFeature())
     {
       if (containsContactFeature(feature))
@@ -287,7 +322,7 @@ public abstract class FeatureStore implements FeatureStoreI
     }
     else if (feature.isNonPositional())
     {
-      if (containsNonPositional(feature))
+      if (containsNonPositionalFeature(feature))
       {
         return false;
       }
@@ -296,14 +331,11 @@ public abstract class FeatureStore implements FeatureStoreI
     }
     else
     {
-      // allow for check with
-      if (checkContainsPositionalFeatureForAdd(feature)
-              || !addPositionalFeature(feature))
+      if (!features.add(feature, false))
       {
         return false;
       }
       positionalFeatureGroups.add(feature.getFeatureGroup());
-      // addPositionalFeature(feature);
       if (feature.begin > lastStart)
       {
         lastStart = feature.begin;
@@ -358,14 +390,6 @@ public abstract class FeatureStore implements FeatureStoreI
   }
 
   /**
-   * Adds one feature to the IntervalStore that can manage nested features
-   * (creating the IntervalStore if necessary)
-   * 
-   * @return true if added -- allowing for late checking during addition
-   */
-  abstract protected boolean addPositionalFeature(SequenceFeature feature);
-
-  /**
    * Adds the feature to the list of non-positional features (with lazy
    * instantiation of the list if it is null), and returns true. The feature
    * group is added to the set of distinct feature groups for non-positional
@@ -395,58 +419,54 @@ public abstract class FeatureStore implements FeatureStoreI
    * @param feature
    * @return
    */
-  @Override
   public boolean contains(SequenceFeature feature)
   {
     if (feature.isNonPositional())
     {
-      return containsNonPositional(feature);
-
+      return containsNonPositionalFeature(feature);
     }
 
     if (feature.isContactFeature())
     {
       return containsContactFeature(feature);
-
     }
 
     return containsPositionalFeature(feature);
 
   }
 
-  /**
-   * A check that can be overridden if the check is being done during the add
-   * operation itself.
-   * 
-   * @param feature
-   * @return
-   */
-  protected boolean checkContainsPositionalFeatureForAdd(
-          SequenceFeature feature)
-  {
-    return containsPositionalFeature(feature);
-  }
-
   private boolean containsPositionalFeature(SequenceFeature feature)
   {
     return features == null || feature.begin > lastStart ? false
-            : containsFeature(feature);
+            : features.contains(feature);
   }
 
+  /**
+   * Answers true if this store already contains a contact feature equal to the
+   * given feature (by {@code SequenceFeature.equals()} test), else false
+   * 
+   * @param feature
+   * @return
+   */
   private boolean containsContactFeature(SequenceFeature feature)
   {
     return contactFeatureStarts != null && feature.begin <= lastContactStart
             && listContains(contactFeatureStarts, feature);
   }
 
-  private boolean containsNonPositional(SequenceFeature feature)
+  /**
+   * Answers true if this store already contains a non-positional feature equal
+   * to the given feature (by {@code SequenceFeature.equals()} test), else false
+   * 
+   * @param feature
+   * @return
+   */
+  private boolean containsNonPositionalFeature(SequenceFeature feature)
   {
     return nonPositionalFeatures == null ? false
             : nonPositionalFeatures.contains(feature);
   }
 
-  abstract protected boolean containsFeature(SequenceFeature feature);
-
   /**
    * 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
@@ -455,8 +475,6 @@ public abstract class FeatureStore implements FeatureStoreI
    * 
    * @param sf
    */
-
-  @Override
   public synchronized boolean delete(SequenceFeature sf)
   {
     boolean removed = false;
@@ -487,7 +505,7 @@ public abstract class FeatureStore implements FeatureStoreI
      */
     if (!removed && features != null)
     {
-      removed = findAndRemoveNonContactFeature(sf);
+      removed = features.remove(sf);
     }
 
     if (removed)
@@ -498,23 +516,11 @@ public abstract class FeatureStore implements FeatureStoreI
     return removed;
   }
 
-  abstract protected boolean findAndRemoveNonContactFeature(SequenceFeature sf);
-
-  abstract protected void findContactFeatures(long from, long to,
-          List<SequenceFeature> result);
-
-  abstract protected int findFirstBegin(List<SequenceFeature> list,
-          long pos);
-
-  abstract protected int findFirstEnd(List<SequenceFeature> list, long pos);
-
-  @Override
   public List<SequenceFeature> findOverlappingFeatures(long start, long end)
   {
     return findOverlappingFeatures(start, end, null);
   }
 
-  @Override
   public List<SequenceFeature> getContactFeatures()
   {
     return getContactFeatures(new ArrayList<>());
@@ -526,8 +532,6 @@ public abstract class FeatureStore implements FeatureStoreI
    * 
    * @return
    */
-
-  @Override
   public List<SequenceFeature> getContactFeatures(
           List<SequenceFeature> result)
   {
@@ -545,8 +549,6 @@ public abstract class FeatureStore implements FeatureStoreI
    * @param positional
    * @return
    */
-
-  @Override
   public int getFeatureCount(boolean positional)
   {
     if (!positional)
@@ -557,7 +559,6 @@ public abstract class FeatureStore implements FeatureStoreI
 
     return (contactFeatureStarts == null ? 0 : contactFeatureStarts.size())
             + features.size();
-
   }
 
   /**
@@ -568,8 +569,6 @@ public abstract class FeatureStore implements FeatureStoreI
    * @param positionalFeatures
    * @return
    */
-
-  @Override
   public Set<String> getFeatureGroups(boolean positionalFeatures)
   {
     if (positionalFeatures)
@@ -584,7 +583,6 @@ public abstract class FeatureStore implements FeatureStoreI
     }
   }
 
-  @Override
   public Collection<SequenceFeature> getFeatures()
   {
     return features;
@@ -598,8 +596,6 @@ public abstract class FeatureStore implements FeatureStoreI
    * @param group
    * @return
    */
-
-  @Override
   public List<SequenceFeature> getFeaturesForGroup(boolean positional,
           String group)
   {
@@ -635,8 +631,6 @@ public abstract class FeatureStore implements FeatureStoreI
    * @param positional
    * @return
    */
-
-  @Override
   public float getMaximumScore(boolean positional)
   {
     return positional ? positionalMaxScore : nonPositionalMaxScore;
@@ -650,14 +644,11 @@ public abstract class FeatureStore implements FeatureStoreI
    * @param positional
    * @return
    */
-
-  @Override
   public float getMinimumScore(boolean positional)
   {
     return positional ? positionalMinScore : nonPositionalMinScore;
   }
 
-  @Override
   public List<SequenceFeature> getNonPositionalFeatures()
   {
     return getNonPositionalFeatures(new ArrayList<>());
@@ -669,8 +660,6 @@ public abstract class FeatureStore implements FeatureStoreI
    * 
    * @return
    */
-
-  @Override
   public List<SequenceFeature> getNonPositionalFeatures(
           List<SequenceFeature> result)
   {
@@ -681,7 +670,6 @@ public abstract class FeatureStore implements FeatureStoreI
     return result;
   }
 
-  @Override
   public List<SequenceFeature> getPositionalFeatures()
   {
     return getPositionalFeatures(new ArrayList<>());
@@ -692,8 +680,6 @@ public abstract class FeatureStore implements FeatureStoreI
    * 
    * @return
    */
-
-  @Override
   public List<SequenceFeature> getPositionalFeatures(
           List<SequenceFeature> result)
   {
@@ -723,8 +709,6 @@ public abstract class FeatureStore implements FeatureStoreI
    * 
    * @return
    */
-
-  @Override
   public int getTotalFeatureLength()
   {
     return totalExtent;
@@ -735,8 +719,6 @@ public abstract class FeatureStore implements FeatureStoreI
    * 
    * @return
    */
-
-  @Override
   public boolean isEmpty()
   {
     boolean hasFeatures = (contactFeatureStarts != null
@@ -811,8 +793,6 @@ public abstract class FeatureStore implements FeatureStoreI
    * @param shiftBy
    * @return
    */
-
-  @Override
   public synchronized boolean shiftFeatures(int fromPosition, int shiftBy)
   {
     /*
@@ -847,4 +827,163 @@ public abstract class FeatureStore implements FeatureStoreI
     return modified;
   }
 
+  /**
+   * Answers the position (0, 1...) in the list of the first entry whose end
+   * position is not less than {@ pos}. If no such entry is found, answers the
+   * length of the list.
+   * 
+   * @param list
+   * @param pos
+   * @return
+   */
+  protected int findFirstEnd(List<SequenceFeature> list, long pos)
+  {
+    return BinarySearcher.findFirst(list, false, Compare.GE, (int) pos);
+  }
+
+  /**
+   * Adds contact features to the result list where either the second or the
+   * first contact position lies within the target range
+   * 
+   * @param from
+   * @param to
+   * @param result
+   */
+  protected void findContactFeatures(long from, long to,
+          List<SequenceFeature> result)
+  {
+    if (contactFeatureStarts != null)
+    {
+      findContactStartOverlaps(from, to, result);
+      findContactEndOverlaps(from, to, result);
+    }
+  }
+
+  /**
+   * Adds to the result list any contact features whose end (second contact
+   * point), but not start (first contact point), lies in the query from-to
+   * range
+   * 
+   * @param from
+   * @param to
+   * @param result
+   */
+  private void findContactEndOverlaps(long from, long to,
+          List<SequenceFeature> result)
+  {
+    /*
+     * find the first contact feature (if any) 
+     * whose end point is not before the target range
+     */
+    int index = findFirstEnd(contactFeatureEnds, from);
+
+    int n = contactFeatureEnds.size();
+    while (index < n)
+    {
+      SequenceFeature sf = contactFeatureEnds.get(index);
+      if (!sf.isContactFeature())
+      {
+        System.err.println("Error! non-contact feature type " + sf.getType()
+                + " in contact features list");
+        index++;
+        continue;
+      }
+
+      int begin = sf.getBegin();
+      if (begin >= from && begin <= to)
+      {
+        /*
+         * this feature's first contact position lies in the search range
+         * so we don't include it in results a second time
+         */
+        index++;
+        continue;
+      }
+
+      if (sf.getEnd() > to)
+      {
+        /*
+         * this feature (and all following) has end point after the target range
+         */
+        break;
+      }
+
+      /*
+       * feature has end >= from and end <= to
+       * i.e. contact end point lies within overlap search range
+       */
+      result.add(sf);
+      index++;
+    }
+  }
+
+  /**
+   * Adds contact features whose start position lies in the from-to range to the
+   * result list
+   * 
+   * @param from
+   * @param to
+   * @param result
+   */
+  private void findContactStartOverlaps(long from, long to,
+          List<SequenceFeature> result)
+  {
+    int index = BinarySearcher.findFirst(contactFeatureStarts, true,
+            Compare.GE, (int) from);
+
+    while (index < contactFeatureStarts.size())
+    {
+      SequenceFeature sf = contactFeatureStarts.get(index);
+      if (!sf.isContactFeature())
+      {
+        System.err.println("Error! non-contact feature " + sf.toString()
+                + " in contact features list");
+        index++;
+        continue;
+      }
+      if (sf.getBegin() > to)
+      {
+        /*
+         * this feature's start (and all following) follows the target range
+         */
+        break;
+      }
+
+      /*
+       * feature has begin >= from and begin <= to
+       * i.e. contact start point lies within overlap search range
+       */
+      result.add(sf);
+      index++;
+    }
+  }
+
+  /**
+   * Returns a (possibly empty) list of features whose extent overlaps the given
+   * range. The returned list is not ordered. Contact features are included if
+   * either of the contact points lies within the range. If the {@code result}
+   * parameter is not null, new entries are added to this list and the (possibly
+   * extended) list returned.
+   * 
+   * @param start
+   *          start position of overlap range (inclusive)
+   * @param end
+   *          end position of overlap range (inclusive)
+   * @param result
+   * @return
+   */
+  public List<SequenceFeature> findOverlappingFeatures(long start, long end,
+          List<SequenceFeature> result)
+  {
+    if (result == null)
+    {
+      result = new ArrayList<>();
+    }
+
+    findContactFeatures(start, end, result);
+    features.findOverlaps(start, end, result);
+
+    return result;
+  }
+
 }