JAL-2446 add call to SequenceFeatures.delete(); use varargs for type
[jalview.git] / src / jalview / datamodel / features / SequenceFeatures.java
index 227d8fd..6165d0a 100644 (file)
@@ -3,10 +3,13 @@ package jalview.datamodel.features;
 import jalview.datamodel.SequenceFeature;
 
 import java.util.ArrayList;
-import java.util.Collections;
+import java.util.Arrays;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
 
 /**
  * A class that stores sequence features in a way that supports efficient
@@ -53,120 +56,126 @@ public class SequenceFeatures
   }
 
   /**
-   * Returns a (possibly empty) list of features of the given type which overlap
-   * the (inclusive) sequence position range
+   * Returns a (possibly empty) list of features, optionally restricted to
+   * specified types, which overlap the given (inclusive) sequence position
+   * range
    * 
-   * @param type
    * @param from
    * @param to
+   * @param type
    * @return
    */
-  public List<SequenceFeature> findFeatures(String type, int from,
-          int to)
-  {
-    FeatureStore features = featureStore.get(type);
-    if (features == null)
-    {
-      return Collections.emptyList();
-    }
-    return features.findOverlappingFeatures(from, to);
-  }
-
-  /**
-   * Answers a list of all features stored (including non-positional), in no
-   * particular guaranteed order
-   * 
-   * @return
-   */
-  public List<SequenceFeature> getFeatures()
+  public List<SequenceFeature> findFeatures(int from, int to,
+          String... type)
   {
     List<SequenceFeature> result = new ArrayList<SequenceFeature>();
-    for (FeatureStore featureSet : featureStore.values())
+
+    for (String featureType : varargToTypes(type))
     {
-      result.addAll(featureSet.getFeatures());
+      FeatureStore features = featureStore.get(featureType);
+      if (features != null)
+      {
+        result.addAll(features.findOverlappingFeatures(from, to));
+      }
     }
+
     return result;
   }
 
   /**
-   * Answers a list of all non-positional features stored, in no particular
-   * guaranteed order
+   * Answers a list of all features stored, optionally restricted to specified
+   * types, in no particular guaranteed order
    * 
+   * @param type
    * @return
    */
-  public List<SequenceFeature> getNonPositionalFeatures()
+  public List<SequenceFeature> getAllFeatures(String... type)
   {
     List<SequenceFeature> result = new ArrayList<SequenceFeature>();
-    for (FeatureStore featureSet : featureStore.values())
-    {
-      result.addAll(featureSet.getNonPositionalFeatures());
-    }
+
+    result.addAll(getPositionalFeatures(type));
+
+    result.addAll(getNonPositionalFeatures(type));
+
     return result;
   }
 
   /**
-   * Answers a list of all contact features stored, in no particular guaranteed
-   * order
+   * Answers a list of all positional features, optionally restricted to
+   * specified types, in no particular guaranteed order
    * 
+   * @param type
    * @return
    */
-  public List<SequenceFeature> getContactFeatures()
+  public List<SequenceFeature> getPositionalFeatures(String... type)
   {
     List<SequenceFeature> result = new ArrayList<SequenceFeature>();
-    for (FeatureStore featureSet : featureStore.values())
+
+    for (String featureType : varargToTypes(type))
     {
-      result.addAll(featureSet.getContactFeatures());
+      FeatureStore featureSet = featureStore.get(featureType);
+      if (featureSet != null)
+      {
+        result.addAll(featureSet.getPositionalFeatures());
+      }
     }
     return result;
   }
 
   /**
-   * Answers a list of all features of the given type (including
-   * non-positional), in no particular guaranteed order
+   * A convenience method that converts a vararg for feature types to an
+   * Iterable, replacing the value with the stored feature types if it is null
+   * or empty
    * 
+   * @param type
    * @return
    */
-  public List<SequenceFeature> getFeatures(String type)
+  protected Iterable<String> varargToTypes(String... type)
   {
-    List<SequenceFeature> result = new ArrayList<SequenceFeature>();
-    FeatureStore featureSet = featureStore.get(type);
-    if (featureSet != null)
-    {
-      result.addAll(featureSet.getFeatures());
-    }
-    return result;
+    return type == null || type.length == 0 ? featureStore
+            .keySet() : Arrays.asList(type);
   }
 
   /**
-   * Answers a list of all contact features of the given type, in no particular
-   * guaranteed order
+   * Answers a list of all contact features, optionally restricted to specified
+   * types, in no particular guaranteed order
    * 
    * @return
    */
-  public List<SequenceFeature> getContactFeatures(String type)
+  public List<SequenceFeature> getContactFeatures(String... type)
   {
     List<SequenceFeature> result = new ArrayList<SequenceFeature>();
-    FeatureStore featureSet = featureStore.get(type);
-    if (featureSet != null)
+
+    for (String featureType : varargToTypes(type))
     {
-      result.addAll(featureSet.getContactFeatures());
+      FeatureStore featureSet = featureStore.get(featureType);
+      if (featureSet != null)
+      {
+        result.addAll(featureSet.getContactFeatures());
+      }
     }
     return result;
   }
 
   /**
-   * Answers a list of all non-positional features of the given type, in no
-   * particular guaranteed order
+   * Answers a list of all non-positional features, optionally restricted to
+   * specified types, in no particular guaranteed order
    * 
+   * @param type
+   *          if no type is specified, all are returned
    * @return
    */
-  public List<SequenceFeature> getNonPositionalFeatures(String type)
+  public List<SequenceFeature> getNonPositionalFeatures(String... type)
   {
     List<SequenceFeature> result = new ArrayList<SequenceFeature>();
-    FeatureStore featureSet = featureStore.get(type);
-    if (featureSet != null)
+
+    for (String featureType : varargToTypes(type))
     {
-      result.addAll(featureSet.getNonPositionalFeatures());
+      FeatureStore featureSet = featureStore.get(featureType);
+      if (featureSet != null)
+      {
+        result.addAll(featureSet.getNonPositionalFeatures());
+      }
     }
     return result;
   }
@@ -207,4 +216,69 @@ public class SequenceFeatures
     }
     return false;
   }
+
+  /**
+   * Returns a set of the distinct feature groups present in the collection. The
+   * set may include null. The boolean parameter determines whether the groups
+   * for positional or for non-positional features are returned. The optional
+   * type parameter may be used to restrict to groups for specified feature
+   * types.
+   * 
+   * @param positionalFeatures
+   * @param type
+   * @return
+   */
+  public Set<String> getFeatureGroups(boolean positionalFeatures,
+          String... type)
+  {
+    Set<String> groups = new HashSet<String>();
+
+    Iterable<String> types = varargToTypes(type);
+
+    for (String featureType : types)
+    {
+      FeatureStore featureSet = featureStore.get(featureType);
+      if (featureSet != null)
+      {
+        groups.addAll(featureSet.getFeatureGroups(positionalFeatures));
+      }
+    }
+
+    return groups;
+  }
+
+  /**
+   * Answers the set of distinct feature types for which there is at least one
+   * feature with one of the given feature group(s). The parameter determines
+   * whether the groups for positional or for non-positional features are
+   * returned.
+   * 
+   * @param positionalFeatures
+   * @param groups
+   * @return
+   */
+  public Set<String> getFeatureTypesForGroups(boolean positionalFeatures,
+          String... groups)
+  {
+    Set<String> result = new HashSet<String>();
+
+    for (Entry<String, FeatureStore> featureType : featureStore.entrySet())
+    {
+      Set<String> featureGroups = featureType.getValue().getFeatureGroups(
+              positionalFeatures);
+      for (String group : groups)
+      {
+        if (featureGroups.contains(group))
+        {
+          /*
+           * yes this feature type includes one of the query groups
+           */
+          result.add(featureType.getKey());
+          break;
+        }
+      }
+    }
+
+    return result;
+  }
 }