JAL-3397 impl.IntervalStore and nonc.IntervalStore unified api
[jalview.git] / src / jalview / datamodel / features / SequenceFeatures.java
index 6c83013..e747d5f 100644 (file)
@@ -23,10 +23,10 @@ package jalview.datamodel.features;
 import jalview.datamodel.SequenceFeature;
 import jalview.io.gff.SequenceOntologyFactory;
 import jalview.io.gff.SequenceOntologyI;
-import jalview.util.Platform;
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -46,12 +46,11 @@ import intervalstore.api.IntervalI;
  */
 public class SequenceFeatures implements SequenceFeaturesI
 {
-
   /*
    * map from feature type to structured store of features for that type
    * null types are permitted (but not a good idea!)
    */
-  private Map<String, FeatureStoreI> featureStore;
+  private Map<String, FeatureStore> featureStore;
 
   /**
    * Constructor
@@ -63,7 +62,7 @@ public class SequenceFeatures implements SequenceFeaturesI
      * ? wrap as a synchronized map for add and delete operations
      */
     // featureStore = Collections
-    // .synchronizedSortedMap(new TreeMap<String, FeatureStoreI>());
+    // .synchronizedSortedMap(new TreeMap<String, FeatureStore>());
     featureStore = new TreeMap<>();
   }
 
@@ -97,19 +96,11 @@ public class SequenceFeatures implements SequenceFeaturesI
 
     if (featureStore.get(type) == null)
     {
-      featureStore.put(type, newFeatureStore());
+      featureStore.put(type, new FeatureStore());
     }
     return featureStore.get(type).addFeature(sf);
   }
 
-  private FeatureStoreI newFeatureStore()
-  {
-    return (//
-    Platform.isJS()//
-            ? new FeatureStoreJS()
-            : new FeatureStoreImpl());
-  }
-
   /**
    * {@inheritDoc}
    */
@@ -118,13 +109,9 @@ public class SequenceFeatures implements SequenceFeaturesI
           String... type)
   {
     List<SequenceFeature> result = new ArrayList<>();
-    for (FeatureStoreI featureSet : varargToTypes(type))
+    for (FeatureStore featureSet : varargToTypes(type))
     {
-      // System.err.println("SF findFeature " + System.currentTimeMillis()
-      // + " " + from + " " + to + " "
-      // + featureSet.getPositionalFeatures().get(0).type);
-      //
-      result.addAll(featureSet.findOverlappingFeatures(from, to, null));
+      featureSet.findOverlappingFeatures(from, to, result);
     }
     return result;
   }
@@ -176,7 +163,7 @@ public class SequenceFeatures implements SequenceFeaturesI
   {
     int result = 0;
 
-    for (FeatureStoreI featureSet : varargToTypes(type))
+    for (FeatureStore featureSet : varargToTypes(type))
     {
       result += featureSet.getFeatureCount(positional);
     }
@@ -191,7 +178,7 @@ public class SequenceFeatures implements SequenceFeaturesI
   {
     int result = 0;
 
-    for (FeatureStoreI featureSet : varargToTypes(type))
+    for (FeatureStore featureSet : varargToTypes(type))
     {
       result += featureSet.getTotalFeatureLength();
     }
@@ -206,7 +193,7 @@ public class SequenceFeatures implements SequenceFeaturesI
   {
     List<SequenceFeature> result = new ArrayList<>();
 
-    for (FeatureStoreI featureSet : varargToTypes(type))
+    for (FeatureStore featureSet : varargToTypes(type))
     {
       featureSet.getPositionalFeatures(result);
     }
@@ -220,7 +207,7 @@ public class SequenceFeatures implements SequenceFeaturesI
    * @param type
    * @return
    */
-  protected Iterable<FeatureStoreI> varargToTypes(String... type)
+  protected Iterable<FeatureStore> varargToTypes(String... type)
   {
     if (type == null || type.length == 0)
     {
@@ -230,9 +217,9 @@ public class SequenceFeatures implements SequenceFeaturesI
       return featureStore.values();
     }
 
-    List<FeatureStoreI> types = new ArrayList<>();
+    List<FeatureStore> types = new ArrayList<>();
     List<String> args = Arrays.asList(type);
-    for (Entry<String, FeatureStoreI> featureType : featureStore.entrySet())
+    for (Entry<String, FeatureStore> featureType : featureStore.entrySet())
     {
       if (args.contains(featureType.getKey()))
       {
@@ -250,7 +237,7 @@ public class SequenceFeatures implements SequenceFeaturesI
   {
     List<SequenceFeature> result = new ArrayList<>();
 
-    for (FeatureStoreI featureSet : varargToTypes(type))
+    for (FeatureStore featureSet : varargToTypes(type))
     {
       featureSet.getContactFeatures(result);
     }
@@ -265,7 +252,7 @@ public class SequenceFeatures implements SequenceFeaturesI
   {
     List<SequenceFeature> result = new ArrayList<>();
 
-    for (FeatureStoreI featureSet : varargToTypes(type))
+    for (FeatureStore featureSet : varargToTypes(type))
     {
       featureSet.getNonPositionalFeatures(result);
     }
@@ -278,7 +265,7 @@ public class SequenceFeatures implements SequenceFeaturesI
   @Override
   public boolean delete(SequenceFeature sf)
   {
-    for (FeatureStoreI featureSet : featureStore.values())
+    for (FeatureStore featureSet : featureStore.values())
     {
       if (featureSet.delete(sf))
       {
@@ -294,7 +281,7 @@ public class SequenceFeatures implements SequenceFeaturesI
   @Override
   public boolean hasFeatures()
   {
-    for (FeatureStoreI featureSet : featureStore.values())
+    for (FeatureStore featureSet : featureStore.values())
     {
       if (!featureSet.isEmpty())
       {
@@ -313,7 +300,7 @@ public class SequenceFeatures implements SequenceFeaturesI
   {
     Set<String> groups = new HashSet<>();
 
-    for (FeatureStoreI featureSet : varargToTypes(type))
+    for (FeatureStore featureSet : varargToTypes(type))
     {
       groups.addAll(featureSet.getFeatureGroups(positionalFeatures));
     }
@@ -330,7 +317,7 @@ public class SequenceFeatures implements SequenceFeaturesI
   {
     Set<String> result = new HashSet<>();
 
-    for (Entry<String, FeatureStoreI> featureType : featureStore.entrySet())
+    for (Entry<String, FeatureStore> featureType : featureStore.entrySet())
     {
       Set<String> featureGroups = featureType.getValue()
               .getFeatureGroups(positionalFeatures);
@@ -357,7 +344,7 @@ public class SequenceFeatures implements SequenceFeaturesI
   public Set<String> getFeatureTypes(String... soTerm)
   {
     Set<String> types = new HashSet<>();
-    for (Entry<String, FeatureStoreI> entry : featureStore.entrySet())
+    for (Entry<String, FeatureStore> entry : featureStore.entrySet())
     {
       String type = entry.getKey();
       if (!entry.getValue().isEmpty() && isOntologyTerm(type, soTerm))
@@ -427,7 +414,9 @@ public class SequenceFeatures implements SequenceFeaturesI
   public static void sortFeatures(List<? extends IntervalI> features,
           final boolean forwardStrand)
   {
-    IntervalI.sortIntervals(features, forwardStrand);
+    Collections.sort(features,
+            forwardStrand ? IntervalI.COMPARE_BEGIN_ASC
+                    : IntervalI.COMPARE_END_DESC);
   }
 
   /**
@@ -446,7 +435,7 @@ public class SequenceFeatures implements SequenceFeaturesI
           String group, String... type)
   {
     List<SequenceFeature> result = new ArrayList<>();
-    for (FeatureStoreI featureSet : varargToTypes(type))
+    for (FeatureStore featureSet : varargToTypes(type))
     {
       if (featureSet.getFeatureGroups(positional).contains(group))
       {
@@ -463,7 +452,7 @@ public class SequenceFeatures implements SequenceFeaturesI
   public boolean shiftFeatures(int fromPosition, int shiftBy)
   {
     boolean modified = false;
-    for (FeatureStoreI fs : featureStore.values())
+    for (FeatureStore fs : featureStore.values())
     {
       modified |= fs.shiftFeatures(fromPosition, shiftBy);
     }
@@ -479,58 +468,14 @@ public class SequenceFeatures implements SequenceFeaturesI
     featureStore.clear();
   }
 
-  /**
-   * Simplified find for features associated with a given position.
-   * 
-   * JavaScript set to not use IntervalI, but easily testable by setting false
-   * to true in javadoc
-   * 
-   * FeatureRenderer has checked already that featureStore does contain type.
-   * 
-   * @author Bob Hanson 2019.07.30
-   */
   @Override
   public List<SequenceFeature> findFeatures(int pos, String type,
           List<SequenceFeature> list)
   {
-    FeatureStoreI fs = featureStore.get(type);
+    FeatureStore fs = featureStore.get(type);
     return fs.findOverlappingFeatures(pos, pos, list);
   }
 
-  // Chrome; developer console closed
-
-  // BH 2019.08.01 useIntervalStore true, redraw false:
-  // Platform: timer mark 13.848 0.367 overviewrender 16000 pixels row:14
-  // Platform: timer mark 15.391 0.39 overviewrender 16000 pixels row:14
-  // Platform: timer mark 16.498 0.39 overviewrender 16000 pixels row:14
-  // Platform: timer mark 17.596 0.401 overviewrender 16000 pixels row:14
-  // Platform: timer mark 18.738 0.363 overviewrender 16000 pixels row:14
-  // Platform: timer mark 19.659 0.358 overviewrender 16000 pixels row:14
-  // Platform: timer mark 20.737 0.359 overviewrender 16000 pixels row:14
-  // Platform: timer mark 21.797 0.391 overviewrender 16000 pixels row:14
-  // Platform: timer mark 22.851 0.361 overviewrender 16000 pixels row:14
-  // Platform: timer mark 24.019 0.395 overviewrender 16000 pixels row:14
-
-  // BH 2019.08.01 useIntervalStore false, redraw false:
-  // Platform: timer mark 19.011 0.181 overviewrender 16000 pixels row:14
-  // Platform: timer mark 20.311 0.183 overviewrender 16000 pixels row:14
-  // Platform: timer mark 21.368 0.175 overviewrender 16000 pixels row:14
-  // Platform: timer mark 22.347 0.178 overviewrender 16000 pixels row:14
-  // Platform: timer mark 23.605 0.216 overviewrender 16000 pixels row:14
-  // Platform: timer mark 24.836 0.191 overviewrender 16000 pixels row:14
-  // Platform: timer mark 26.016 0.181 overviewrender 16000 pixels row:14
-  // Platform: timer mark 27.278 0.178 overviewrender 16000 pixels row:14
-  // Platform: timer mark 28.158 0.181 overviewrender 16000 pixels row:14
-  // Platform: timer mark 29.227 0.196 overviewrender 16000 pixels row:14
-  // Platform: timer mark 30.1 0.171 overviewrender 16000 pixels row:14
-  // Platform: timer mark 31.684 0.196 overviewrender 16000 pixels row:14
-  // Platform: timer mark 32.779 0.18 overviewrender 16000 pixels row:14
-  // Platform: timer mark 52.355 0.185 overviewrender 16000 pixels row:14
-  // Platform: timer mark 53.829 0.186 overviewrender 16000 pixels row:14
-
-  /**
-   * @author Bob Hanson 2019.08.01
-   */
   @Override
   public boolean hasFeatures(String type)
   {