1 package jalview.datamodel.features;
3 import jalview.datamodel.SequenceFeature;
5 import java.util.ArrayList;
6 import java.util.Collections;
7 import java.util.HashMap;
8 import java.util.HashSet;
11 import java.util.Map.Entry;
15 * A class that stores sequence features in a way that supports efficient
16 * querying by type and location (overlap). Intended for (but not limited to)
17 * storage of features for one sequence.
22 public class SequenceFeatures
26 * map from feature type to structured store of features for that type
27 * null types are permitted (but not a good idea!)
29 private Map<String, FeatureStore> featureStore;
34 public SequenceFeatures()
36 featureStore = new HashMap<String, FeatureStore>();
40 * Adds one sequence feature to the store, and returns true, unless the
41 * feature is already contained in the store, in which case this method
42 * returns false. Containment is determined by SequenceFeature.equals()
47 public boolean add(SequenceFeature sf)
49 String type = sf.getType();
51 if (featureStore.get(type) == null)
53 featureStore.put(type, new FeatureStore());
55 return featureStore.get(type).addFeature(sf);
59 * Returns a (possibly empty) list of features of the given type which overlap
60 * the (inclusive) sequence position range
67 public List<SequenceFeature> findFeatures(String type, int from,
70 FeatureStore features = featureStore.get(type);
73 return Collections.emptyList();
75 return features.findOverlappingFeatures(from, to);
79 * Answers a list of all features stored (including non-positional), in no
80 * particular guaranteed order
84 public List<SequenceFeature> getFeatures()
86 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
87 for (FeatureStore featureSet : featureStore.values())
89 result.addAll(featureSet.getFeatures());
95 * Answers a list of all non-positional features stored, in no particular
100 public List<SequenceFeature> getNonPositionalFeatures()
102 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
103 for (FeatureStore featureSet : featureStore.values())
105 result.addAll(featureSet.getNonPositionalFeatures());
111 * Answers a list of all contact features stored, in no particular guaranteed
116 public List<SequenceFeature> getContactFeatures()
118 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
119 for (FeatureStore featureSet : featureStore.values())
121 result.addAll(featureSet.getContactFeatures());
127 * Answers a list of all features of the given type (including
128 * non-positional), in no particular guaranteed order
132 public List<SequenceFeature> getFeatures(String type)
134 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
135 FeatureStore featureSet = featureStore.get(type);
136 if (featureSet != null)
138 result.addAll(featureSet.getFeatures());
144 * Answers a list of all contact features of the given type, in no particular
149 public List<SequenceFeature> getContactFeatures(String type)
151 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
152 FeatureStore featureSet = featureStore.get(type);
153 if (featureSet != null)
155 result.addAll(featureSet.getContactFeatures());
161 * Answers a list of all non-positional features of the given type, in no
162 * particular guaranteed order
166 public List<SequenceFeature> getNonPositionalFeatures(String type)
168 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
169 FeatureStore featureSet = featureStore.get(type);
170 if (featureSet != null)
172 result.addAll(featureSet.getNonPositionalFeatures());
178 * Deletes the given feature from the store, returning true if it was found
179 * (and deleted), else false. This method makes no assumption that the feature
180 * is in the 'expected' place in the store, in case it has been modified since
185 public boolean delete(SequenceFeature sf)
187 for (FeatureStore featureSet : featureStore.values())
189 if (featureSet.delete(sf))
198 * Answers true if this store contains at least one feature, else false
202 public boolean hasFeatures()
204 for (FeatureStore featureSet : featureStore.values())
206 if (!featureSet.isEmpty())
215 * Returns a set of the distinct feature groups present in the collection. The
216 * set may include null.
220 public Set<String> getFeatureGroups()
222 Set<String> groups = new HashSet<String>();
223 for (FeatureStore featureSet : featureStore.values())
225 groups.addAll(featureSet.getFeatureGroups());
231 * Answers the set of distinct feature types for which there is at least one
232 * feature with the given feature group
237 public Set<String> getFeatureTypesForGroup(String group)
239 Set<String> result = new HashSet<String>();
240 for (Entry<String, FeatureStore> featureType : featureStore.entrySet())
242 if (featureType.getValue().getFeatureGroups().contains(group))
245 * yes this feature type includes the query group
247 result.add(featureType.getKey());