JAL-2480 methods to get features for group
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Tue, 23 May 2017 09:54:15 +0000 (10:54 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Tue, 23 May 2017 09:54:15 +0000 (10:54 +0100)
src/jalview/datamodel/features/FeatureStore.java
src/jalview/datamodel/features/SequenceFeatures.java
src/jalview/datamodel/features/SequenceFeaturesI.java

index 432f62d..c6a49db 100644 (file)
@@ -980,4 +980,41 @@ public class FeatureStore
   {
     return positional ? positionalMaxScore : nonPositionalMaxScore;
   }
+
+  /**
+   * Answers a list of all either positional or non-positional features whose
+   * feature group matches the given group (which may be null)
+   * 
+   * @param positional
+   * @param group
+   * @return
+   */
+  public List<SequenceFeature> getFeaturesForGroup(boolean positional,
+          String group)
+  {
+    List<SequenceFeature> result = new ArrayList<SequenceFeature>();
+
+    /*
+     * if we know features don't include the target group, no need
+     * to inspect them for matches
+     */
+    if (positional && !positionalFeatureGroups.contains(group)
+            || !positional && !nonPositionalFeatureGroups.contains(group))
+    {
+      return result;
+    }
+
+    List<SequenceFeature> sfs = positional ? getPositionalFeatures()
+            : getNonPositionalFeatures();
+    for (SequenceFeature sf : sfs)
+    {
+      String featureGroup = sf.getFeatureGroup();
+      if (group == null && featureGroup == null || group != null
+              && group.equals(featureGroup))
+      {
+        result.add(sf);
+      }
+    }
+    return result;
+  }
 }
index af99ada..34470cc 100644 (file)
@@ -425,4 +425,38 @@ public class SequenceFeatures implements SequenceFeaturesI
     Collections.sort(features, forwardStrand ? FORWARD_STRAND
             : REVERSE_STRAND);
   }
+
+  /**
+   * {@inheritDoc} This method is 'semi-optimised': it only inspects features
+   * for types that include the specified group, but has to inspect every
+   * feature of those types for matching feature group. This is efficient unless
+   * a sequence has features that share the same type but are in different
+   * groups - an unlikely case.
+   * <p>
+   * For example, if RESNUM feature is created with group = PDBID, then features
+   * would only be retrieved for those sequences associated with the target
+   * PDBID (group).
+   */
+  @Override
+  public List<SequenceFeature> getFeaturesForGroup(boolean positional,
+          String group, String... type)
+  {
+    List<SequenceFeature> result = new ArrayList<SequenceFeature>();
+    Iterable<String> types = varargToTypes(type);
+
+    for (String featureType : types)
+    {
+      /*
+       * check whether the feature type is present, and also
+       * whether it has features for the specified group
+       */
+      FeatureStore features = featureStore.get(featureType);
+      if (features != null
+              && features.getFeatureGroups(positional).contains(group))
+      {
+        result.addAll(features.getFeaturesForGroup(positional, group));
+      }
+    }
+    return result;
+  }
 }
\ No newline at end of file
index 281743e..c423391 100644 (file)
@@ -46,6 +46,22 @@ public interface SequenceFeaturesI
   List<SequenceFeature> getAllFeatures(String... type);
 
   /**
+   * Answers a list of all positional (or non-positional) features which are in
+   * the specified feature group, optionally restricted to features of specified
+   * types.
+   * 
+   * @param positional
+   *          if true returns positional features, else non-positional features
+   * @param group
+   *          the feature group to be matched (which may be null)
+   * @param type
+   *          optional feature types to filter by
+   * @return
+   */
+  List<SequenceFeature> getFeaturesForGroup(boolean positional,
+          String group, String... type);
+
+  /**
    * Answers a list of all features stored, whose type either matches one of the
    * given ontology terms, or is a specialisation of a term in the Sequence
    * Ontology. Results are returned in no particular guaranteed order.