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;
14 * A class that stores sequence features in a way that supports efficient
15 * querying by type and location (overlap). Intended for (but not limited to)
16 * storage of features for one sequence.
21 public class SequenceFeatures
25 * map from feature type to structured store of features for that type
26 * null types are permitted (but not a good idea!)
28 private Map<String, FeatureStore> featureStore;
33 public SequenceFeatures()
35 featureStore = new HashMap<String, FeatureStore>();
39 * Adds one sequence feature to the store, and returns true, unless the
40 * feature is already contained in the store, in which case this method
41 * returns false. Containment is determined by SequenceFeature.equals()
46 public boolean add(SequenceFeature sf)
48 String type = sf.getType();
50 if (featureStore.get(type) == null)
52 featureStore.put(type, new FeatureStore());
54 return featureStore.get(type).addFeature(sf);
58 * Returns a (possibly empty) list of features of the given type which overlap
59 * the (inclusive) sequence position range
66 public List<SequenceFeature> findFeatures(String type, int from,
69 FeatureStore features = featureStore.get(type);
72 return Collections.emptyList();
74 return features.findOverlappingFeatures(from, to);
78 * Answers a list of all features stored (including non-positional), in no
79 * particular guaranteed order
83 public List<SequenceFeature> getFeatures()
85 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
86 for (FeatureStore featureSet : featureStore.values())
88 result.addAll(featureSet.getFeatures());
94 * Answers a list of all non-positional features stored, in no particular
99 public List<SequenceFeature> getNonPositionalFeatures()
101 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
102 for (FeatureStore featureSet : featureStore.values())
104 result.addAll(featureSet.getNonPositionalFeatures());
110 * Answers a list of all contact features stored, in no particular guaranteed
115 public List<SequenceFeature> getContactFeatures()
117 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
118 for (FeatureStore featureSet : featureStore.values())
120 result.addAll(featureSet.getContactFeatures());
126 * Answers a list of all features of the given type (including
127 * non-positional), in no particular guaranteed order
131 public List<SequenceFeature> getFeatures(String type)
133 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
134 FeatureStore featureSet = featureStore.get(type);
135 if (featureSet != null)
137 result.addAll(featureSet.getFeatures());
143 * Answers a list of all contact features of the given type, in no particular
148 public List<SequenceFeature> getContactFeatures(String type)
150 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
151 FeatureStore featureSet = featureStore.get(type);
152 if (featureSet != null)
154 result.addAll(featureSet.getContactFeatures());
160 * Answers a list of all non-positional features of the given type, in no
161 * particular guaranteed order
165 public List<SequenceFeature> getNonPositionalFeatures(String type)
167 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
168 FeatureStore featureSet = featureStore.get(type);
169 if (featureSet != null)
171 result.addAll(featureSet.getNonPositionalFeatures());
177 * Deletes the given feature from the store, returning true if it was found
178 * (and deleted), else false. This method makes no assumption that the feature
179 * is in the 'expected' place in the store, in case it has been modified since
184 public boolean delete(SequenceFeature sf)
186 for (FeatureStore featureSet : featureStore.values())
188 if (featureSet.delete(sf))
197 * Answers true if this store contains at least one feature, else false
201 public boolean hasFeatures()
203 for (FeatureStore featureSet : featureStore.values())
205 if (!featureSet.isEmpty())
214 * Returns a set of the distinct feature groups present in the collection. The
215 * set may include null.
219 public Set<String> getFeatureGroups()
221 Set<String> groups = new HashSet<String>();
222 for (FeatureStore featureSet : featureStore.values())
224 groups.addAll(featureSet.getFeatureGroups());