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;
12 * A class that stores sequence features in a way that supports efficient
13 * querying by type and location (overlap). Intended for (but not limited to)
14 * storage of features for one sequence.
19 public class SequenceFeatures
23 * map from feature type to structured store of features for that type
24 * null types are permitted (but not a good idea!)
26 private Map<String, FeatureStore> featureStore;
31 public SequenceFeatures()
33 featureStore = new HashMap<String, FeatureStore>();
37 * Adds one sequence feature to the store, and returns true, unless the
38 * feature is already contained in the store, in which case this method
39 * returns false. Containment is determined by SequenceFeature.equals()
44 public boolean add(SequenceFeature sf)
46 String type = sf.getType();
48 if (featureStore.get(type) == null)
50 featureStore.put(type, new FeatureStore());
52 return featureStore.get(type).addFeature(sf);
56 * Returns a (possibly empty) list of features of the given type which overlap
57 * the (inclusive) sequence position range
64 public List<SequenceFeature> findFeatures(String type, int from,
67 FeatureStore features = featureStore.get(type);
70 return Collections.emptyList();
72 return features.findOverlappingFeatures(from, to);
76 * Answers a list of all features stored (including non-positional), in no
77 * particular guaranteed order
81 public List<SequenceFeature> getFeatures()
83 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
84 for (FeatureStore featureSet : featureStore.values())
86 result.addAll(featureSet.getFeatures());
92 * Answers a list of all non-positional features stored, in no particular
97 public List<SequenceFeature> getNonPositionalFeatures()
99 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
100 for (FeatureStore featureSet : featureStore.values())
102 result.addAll(featureSet.getNonPositionalFeatures());
108 * Answers a list of all contact features stored, in no particular guaranteed
113 public List<SequenceFeature> getContactFeatures()
115 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
116 for (FeatureStore featureSet : featureStore.values())
118 result.addAll(featureSet.getContactFeatures());
124 * Answers a list of all features of the given type (including
125 * non-positional), in no particular guaranteed order
129 public List<SequenceFeature> getFeatures(String type)
131 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
132 FeatureStore featureSet = featureStore.get(type);
133 if (featureSet != null)
135 result.addAll(featureSet.getFeatures());
141 * Answers a list of all contact features of the given type, in no particular
146 public List<SequenceFeature> getContactFeatures(String type)
148 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
149 FeatureStore featureSet = featureStore.get(type);
150 if (featureSet != null)
152 result.addAll(featureSet.getContactFeatures());
158 * Answers a list of all non-positional features of the given type, in no
159 * particular guaranteed order
163 public List<SequenceFeature> getNonPositionalFeatures(String type)
165 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
166 FeatureStore featureSet = featureStore.get(type);
167 if (featureSet != null)
169 result.addAll(featureSet.getNonPositionalFeatures());
175 * Deletes the given feature from the store, returning true if it was found
176 * (and deleted), else false. This method makes no assumption that the feature
177 * is in the 'expected' place in the store, in case it has been modified since
182 public boolean delete(SequenceFeature sf)
184 for (FeatureStore featureSet : featureStore.values())
186 if (featureSet.delete(sf))
195 * Answers true if this store contains at least one feature, else false
199 public boolean hasFeatures()
201 for (FeatureStore featureSet : featureStore.values())
203 if (!featureSet.isEmpty())