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;
* @author gmcarstairs
*
*/
-public class SequenceFeatures
+public class SequenceFeatures implements SequenceFeaturesI
{
/*
}
/**
- * Adds one sequence feature to the store, and returns true, unless the
- * feature is already contained in the store, in which case this method
- * returns false. Containment is determined by SequenceFeature.equals()
- * comparison.
- *
- * @param sf
+ * {@inheritDoc}
*/
+ @Override
public boolean add(SequenceFeature sf)
{
String type = sf.getType();
}
/**
- * Returns a (possibly empty) list of features of the given type which overlap
- * the (inclusive) sequence position range
- *
- * @param type
- * @param from
- * @param to
- * @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 positional features stored, in no particular
- * guaranteed order
- *
- * @return
+ * {@inheritDoc}
*/
- public List<SequenceFeature> getPositionalFeatures()
+ @Override
+ 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.getPositionalFeatures());
+ FeatureStore features = featureStore.get(featureType);
+ if (features != null)
+ {
+ result.addAll(features.findOverlappingFeatures(from, to));
+ }
}
+
return result;
}
/**
- * Answers a list of all features stored, in no particular guaranteed order
- *
- * @return
+ * {@inheritDoc}
*/
- public List<SequenceFeature> getAllFeatures()
+ @Override
+ public List<SequenceFeature> getAllFeatures(String... type)
{
List<SequenceFeature> result = new ArrayList<SequenceFeature>();
- result.addAll(getPositionalFeatures());
+ result.addAll(getPositionalFeatures(type));
- result.addAll(getNonPositionalFeatures());
+ result.addAll(getNonPositionalFeatures(type));
return result;
}
/**
- * Answers a list of all features stored of the specified type, in no
- * particular guaranteed order
- *
- * @return
+ * {@inheritDoc}
*/
- public List<SequenceFeature> getAllFeatures(String type)
+ @Override
+ public int getFeatureCount(boolean positional, String... type)
{
- List<SequenceFeature> result = new ArrayList<SequenceFeature>();
-
- result.addAll(getPositionalFeatures(type));
-
- result.addAll(getNonPositionalFeatures(type));
+ int result = 0;
+ for (String featureType : varargToTypes(type))
+ {
+ FeatureStore featureSet = featureStore.get(featureType);
+ if (featureSet != null)
+ {
+ result += featureSet.getFeatureCount(positional);
+ }
+ }
return result;
}
/**
- * Answers a list of all non-positional features stored, in no particular
- * guaranteed order
- *
- * @return
+ * {@inheritDoc}
*/
- public List<SequenceFeature> getNonPositionalFeatures()
+ @Override
+ public int getTotalFeatureLength(String... type)
{
- List<SequenceFeature> result = new ArrayList<SequenceFeature>();
- for (FeatureStore featureSet : featureStore.values())
+ int result = 0;
+
+ for (String featureType : varargToTypes(type))
{
- result.addAll(featureSet.getNonPositionalFeatures());
+ FeatureStore featureSet = featureStore.get(featureType);
+ if (featureSet != null)
+ {
+ result += featureSet.getTotalFeatureLength();
+ }
}
return result;
+
}
/**
- * Answers a list of all contact features stored, in no particular guaranteed
- * order
- *
- * @return
+ * {@inheritDoc}
*/
- public List<SequenceFeature> getContactFeatures()
+ @Override
+ 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 positional features of the given type, 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> getPositionalFeatures(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.getPositionalFeatures());
- }
- 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
- *
- * @return
+ * {@inheritDoc}
*/
- public List<SequenceFeature> getContactFeatures(String type)
+ @Override
+ 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
- *
- * @return
+ * {@inheritDoc}
*/
- public List<SequenceFeature> getNonPositionalFeatures(String type)
+ @Override
+ 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;
}
/**
- * 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
- * is in the 'expected' place in the store, in case it has been modified since
- * it was added.
- *
- * @param sf
+ * {@inheritDoc}
*/
+ @Override
public boolean delete(SequenceFeature sf)
{
for (FeatureStore featureSet : featureStore.values())
}
/**
- * Answers true if this store contains at least one feature, else false
- *
- * @return
+ * {@inheritDoc}
*/
+ @Override
public boolean hasFeatures()
{
for (FeatureStore featureSet : featureStore.values())
}
/**
- * Returns a set of the distinct feature groups present in the collection. The
- * set may include null. The parameter determines whether the groups for
- * positional or for non-positional features are returned.
- *
- * @param positionalFeatures
- * @return
+ * {@inheritDoc}
*/
- public Set<String> getFeatureGroups(boolean positionalFeatures)
+ @Override
+ public Set<String> getFeatureGroups(boolean positionalFeatures,
+ String... type)
{
Set<String> groups = new HashSet<String>();
- for (FeatureStore featureSet : featureStore.values())
+
+ Iterable<String> types = varargToTypes(type);
+
+ for (String featureType : types)
{
- groups.addAll(featureSet.getFeatureGroups(positionalFeatures));
+ 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
+ * {@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(
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;
+ }
}