package jalview.datamodel.features; import jalview.datamodel.SequenceFeature; 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; /** * 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 { /* * map from feature type to structured store of features for that type * null types are permitted (but not a good idea!) */ private Map featureStore; /** * Constructor */ public SequenceFeatures() { featureStore = new HashMap(); } /** * 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 */ public boolean add(SequenceFeature sf) { String type = sf.getType(); if (featureStore.get(type) == null) { featureStore.put(type, new FeatureStore()); } return featureStore.get(type).addFeature(sf); } /** * Returns a (possibly empty) list of features, optionally restricted to * specified types, which overlap the given (inclusive) sequence position * range * * @param from * @param to * @param type * @return */ public List findFeatures(int from, int to, String... type) { List result = new ArrayList(); for (String featureType : varargToTypes(type)) { FeatureStore features = featureStore.get(featureType); if (features != null) { result.addAll(features.findOverlappingFeatures(from, to)); } } return result; } /** * Answers a list of all features stored, optionally restricted to specified * types, in no particular guaranteed order * * @param type * @return */ public List getAllFeatures(String... type) { List result = new ArrayList(); result.addAll(getPositionalFeatures(type)); result.addAll(getNonPositionalFeatures(type)); return result; } /** * Answers a list of all positional features, optionally restricted to * specified types, in no particular guaranteed order * * @param type * @return */ public List getPositionalFeatures(String... type) { List result = new ArrayList(); 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 * @return */ protected Iterable varargToTypes(String... type) { return type == null || type.length == 0 ? featureStore .keySet() : Arrays.asList(type); } /** * Answers a list of all contact features, optionally restricted to specified * types, in no particular guaranteed order * * @return */ public List getContactFeatures(String... type) { List result = new ArrayList(); for (String featureType : varargToTypes(type)) { FeatureStore featureSet = featureStore.get(featureType); if (featureSet != null) { result.addAll(featureSet.getContactFeatures()); } } return result; } /** * 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 getNonPositionalFeatures(String... type) { List result = new ArrayList(); for (String featureType : varargToTypes(type)) { 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 */ public boolean delete(SequenceFeature sf) { for (FeatureStore featureSet : featureStore.values()) { if (featureSet.delete(sf)) { return true; } } return false; } /** * Answers true if this store contains at least one feature, else false * * @return */ public boolean hasFeatures() { for (FeatureStore featureSet : featureStore.values()) { if (!featureSet.isEmpty()) { return true; } } 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 getFeatureGroups(boolean positionalFeatures, String... type) { Set groups = new HashSet(); Iterable 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 getFeatureTypesForGroups(boolean positionalFeatures, String... groups) { Set result = new HashSet(); for (Entry featureType : featureStore.entrySet()) { Set 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; } }