import jalview.datamodel.SequenceFeature;
-import java.util.Collections;
+import java.util.ArrayList;
+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;
-public class SequenceFeatures
+/**
+ * A class that stores sequence features in a way that supports efficient
+ * querying by type and location (overlap). Intended for (but not limited to)
+ * storage of features for one sequence.
+ *
+ * @author gmcarstairs
+ *
+ */
+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, FeatureStore> featureStore;
}
/**
- * Add one sequence feature to the store
- *
- * @param sf
+ * {@inheritDoc}
*/
- public void add(SequenceFeature sf)
+ @Override
+ public boolean add(SequenceFeature sf)
{
String type = sf.getType();
+
if (featureStore.get(type) == null)
{
featureStore.put(type, new FeatureStore());
}
- featureStore.get(type).addFeature(sf);
+ return featureStore.get(type).addFeature(sf);
}
/**
- * Returns a (possibly empty) list of features of the given type which overlap
- * the (inclusive) sequence position range
+ * {@inheritDoc}
+ */
+ @Override
+ public List<SequenceFeature> findFeatures(int from, int to,
+ String... type)
+ {
+ List<SequenceFeature> result = new ArrayList<SequenceFeature>();
+
+ for (String featureType : varargToTypes(type))
+ {
+ FeatureStore features = featureStore.get(featureType);
+ if (features != null)
+ {
+ result.addAll(features.findOverlappingFeatures(from, to));
+ }
+ }
+
+ return result;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public List<SequenceFeature> getAllFeatures(String... type)
+ {
+ List<SequenceFeature> result = new ArrayList<SequenceFeature>();
+
+ result.addAll(getPositionalFeatures(type));
+
+ result.addAll(getNonPositionalFeatures(type));
+
+ return result;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int getFeatureCount(boolean positional, String... type)
+ {
+ int result = 0;
+
+ for (String featureType : varargToTypes(type))
+ {
+ FeatureStore featureSet = featureStore.get(featureType);
+ if (featureSet != null)
+ {
+ result += featureSet.getFeatureCount(positional);
+ }
+ }
+ return result;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int getTotalFeatureLength(String... type)
+ {
+ int result = 0;
+
+ for (String featureType : varargToTypes(type))
+ {
+ FeatureStore featureSet = featureStore.get(featureType);
+ if (featureSet != null)
+ {
+ result += featureSet.getTotalFeatureLength();
+ }
+ }
+ return result;
+
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public List<SequenceFeature> getPositionalFeatures(String... type)
+ {
+ List<SequenceFeature> result = new ArrayList<SequenceFeature>();
+
+ for (String featureType : varargToTypes(type))
+ {
+ FeatureStore featureSet = featureStore.get(featureType);
+ if (featureSet != null)
+ {
+ result.addAll(featureSet.getPositionalFeatures());
+ }
+ }
+ return result;
+ }
+
+ /**
+ * 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
- * @param from
- * @param to
* @return
*/
- public List<SequenceFeature> findFeatures(String type, int from,
- int to)
+ protected Iterable<String> varargToTypes(String... type)
+ {
+ return type == null || type.length == 0 ? featureStore
+ .keySet() : Arrays.asList(type);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public List<SequenceFeature> getContactFeatures(String... type)
+ {
+ List<SequenceFeature> result = new ArrayList<SequenceFeature>();
+
+ for (String featureType : varargToTypes(type))
+ {
+ FeatureStore featureSet = featureStore.get(featureType);
+ if (featureSet != null)
+ {
+ result.addAll(featureSet.getContactFeatures());
+ }
+ }
+ return result;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public List<SequenceFeature> getNonPositionalFeatures(String... type)
{
- FeatureStore features = featureStore.get(type);
- if (features == null)
+ List<SequenceFeature> result = new ArrayList<SequenceFeature>();
+
+ for (String featureType : varargToTypes(type))
+ {
+ FeatureStore featureSet = featureStore.get(featureType);
+ if (featureSet != null)
+ {
+ result.addAll(featureSet.getNonPositionalFeatures());
+ }
+ }
+ return result;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean delete(SequenceFeature sf)
+ {
+ for (FeatureStore featureSet : featureStore.values())
+ {
+ if (featureSet.delete(sf))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean hasFeatures()
+ {
+ for (FeatureStore featureSet : featureStore.values())
{
- return Collections.emptyList();
+ if (!featureSet.isEmpty())
+ {
+ return true;
+ }
}
- return features.findOverlappingFeatures(from, to);
+ return false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ 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;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ 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;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Set<String> getFeatureTypes()
+ {
+ Set<String> types = new HashSet<String>();
+ for (Entry<String, FeatureStore> entry : featureStore.entrySet())
+ {
+ if (!entry.getValue().isEmpty())
+ {
+ types.add(entry.getKey());
+ }
+ }
+ return types;
+ }
+
+ /**
+ * Answers the minimum score held for positional or non-positional features
+ * for the specified type. This may be Float.NaN if there are no features, or
+ * none has a non-NaN score.
+ *
+ * @param type
+ * @param positional
+ * @return
+ */
+ public float getMinimumScore(String type, boolean positional)
+ {
+ return featureStore.containsKey(type) ? featureStore.get(type)
+ .getMinimumScore(positional) : Float.NaN;
+ }
+
+ /**
+ * Answers the maximum score held for positional or non-positional features
+ * for the specified type. This may be Float.NaN if there are no features, or
+ * none has a non-NaN score.
+ *
+ * @param type
+ * @param positional
+ * @return
+ */
+ public float getMaximumScore(String type, boolean positional)
+ {
+ return featureStore.containsKey(type) ? featureStore.get(type)
+ .getMaximumScore(positional) : Float.NaN;
}
}