1 package jalview.datamodel.features;
3 import jalview.datamodel.SequenceFeature;
4 import jalview.io.gff.SequenceOntologyFactory;
5 import jalview.io.gff.SequenceOntologyI;
7 import java.util.ArrayList;
8 import java.util.Arrays;
9 import java.util.Collections;
10 import java.util.Comparator;
11 import java.util.HashSet;
12 import java.util.List;
14 import java.util.Map.Entry;
16 import java.util.TreeMap;
19 * A class that stores sequence features in a way that supports efficient
20 * querying by type and location (overlap). Intended for (but not limited to)
21 * storage of features for one sequence.
26 public class SequenceFeatures implements SequenceFeaturesI
29 * a comparator for sorting features by start position ascending
31 private static Comparator<ContiguousI> FORWARD_STRAND = new Comparator<ContiguousI>()
34 public int compare(ContiguousI o1, ContiguousI o2)
36 return Integer.compare(o1.getBegin(), o2.getBegin());
41 * a comparator for sorting features by end position descending
43 private static Comparator<ContiguousI> REVERSE_STRAND = new Comparator<ContiguousI>()
46 public int compare(ContiguousI o1, ContiguousI o2)
48 return Integer.compare(o2.getEnd(), o1.getEnd());
53 * map from feature type to structured store of features for that type
54 * null types are permitted (but not a good idea!)
56 private Map<String, FeatureStore> featureStore;
61 public SequenceFeatures()
64 * use a TreeMap so that features are returned in alphabetical order of type
65 * wrap as a synchronized map for add and delete operations
67 // featureStore = Collections
68 // .synchronizedSortedMap(new TreeMap<String, FeatureStore>());
69 featureStore = new TreeMap<String, FeatureStore>();
76 public boolean add(SequenceFeature sf)
78 String type = sf.getType();
81 System.err.println("Feature type may not be null: " + sf.toString());
85 if (featureStore.get(type) == null)
87 featureStore.put(type, new FeatureStore());
89 return featureStore.get(type).addFeature(sf);
96 public List<SequenceFeature> findFeatures(int from, int to,
99 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
101 for (String featureType : varargToTypes(type))
103 FeatureStore features = featureStore.get(featureType);
104 if (features != null)
106 result.addAll(features.findOverlappingFeatures(from, to));
117 public List<SequenceFeature> getAllFeatures(String... type)
119 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
121 result.addAll(getPositionalFeatures(type));
123 result.addAll(getNonPositionalFeatures(type));
132 public List<SequenceFeature> getFeaturesByOntology(String... ontologyTerm)
134 if (ontologyTerm == null || ontologyTerm.length == 0)
136 return new ArrayList<SequenceFeature>();
139 Set<String> featureTypes = getFeatureTypes(ontologyTerm);
140 return getAllFeatures(featureTypes.toArray(new String[featureTypes
148 public int getFeatureCount(boolean positional, String... type)
152 for (String featureType : varargToTypes(type))
154 FeatureStore featureSet = featureStore.get(featureType);
155 if (featureSet != null)
157 result += featureSet.getFeatureCount(positional);
167 public int getTotalFeatureLength(String... type)
171 for (String featureType : varargToTypes(type))
173 FeatureStore featureSet = featureStore.get(featureType);
174 if (featureSet != null)
176 result += featureSet.getTotalFeatureLength();
187 public List<SequenceFeature> getPositionalFeatures(String... type)
189 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
191 for (String featureType : varargToTypes(type))
193 FeatureStore featureSet = featureStore.get(featureType);
194 if (featureSet != null)
196 result.addAll(featureSet.getPositionalFeatures());
203 * A convenience method that converts a vararg for feature types to an
204 * Iterable, replacing the value with the stored feature types if it is null
210 protected Iterable<String> varargToTypes(String... type)
212 if (type == null || type.length == 0)
215 * no vararg parameter supplied
217 return featureStore.keySet();
221 * else make a copy of the list, and remove any null value just in case,
222 * as it would cause errors looking up the features Map
224 List<String> types = new ArrayList<String>(Arrays.asList(type));
233 public List<SequenceFeature> getContactFeatures(String... type)
235 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
237 for (String featureType : varargToTypes(type))
239 FeatureStore featureSet = featureStore.get(featureType);
240 if (featureSet != null)
242 result.addAll(featureSet.getContactFeatures());
252 public List<SequenceFeature> getNonPositionalFeatures(String... type)
254 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
256 for (String featureType : varargToTypes(type))
258 FeatureStore featureSet = featureStore.get(featureType);
259 if (featureSet != null)
261 result.addAll(featureSet.getNonPositionalFeatures());
271 public boolean delete(SequenceFeature sf)
273 for (FeatureStore featureSet : featureStore.values())
275 if (featureSet.delete(sf))
287 public boolean hasFeatures()
289 for (FeatureStore featureSet : featureStore.values())
291 if (!featureSet.isEmpty())
303 public Set<String> getFeatureGroups(boolean positionalFeatures,
306 Set<String> groups = new HashSet<String>();
308 Iterable<String> types = varargToTypes(type);
310 for (String featureType : types)
312 FeatureStore featureSet = featureStore.get(featureType);
313 if (featureSet != null)
315 groups.addAll(featureSet.getFeatureGroups(positionalFeatures));
326 public Set<String> getFeatureTypesForGroups(boolean positionalFeatures,
329 Set<String> result = new HashSet<String>();
331 for (Entry<String, FeatureStore> featureType : featureStore.entrySet())
333 Set<String> featureGroups = featureType.getValue().getFeatureGroups(
335 for (String group : groups)
337 if (featureGroups.contains(group))
340 * yes this feature type includes one of the query groups
342 result.add(featureType.getKey());
355 public Set<String> getFeatureTypes(String... soTerm)
357 Set<String> types = new HashSet<String>();
358 for (Entry<String, FeatureStore> entry : featureStore.entrySet())
360 String type = entry.getKey();
361 if (!entry.getValue().isEmpty() && isOntologyTerm(type, soTerm))
370 * Answers true if the given type is one of the specified sequence ontology
371 * terms (or a sub-type of one), or if no terms are supplied. Answers false if
372 * filter terms are specified and the given term does not match any of them.
378 protected boolean isOntologyTerm(String type, String... soTerm)
380 if (soTerm == null || soTerm.length == 0)
384 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
385 for (String term : soTerm)
387 if (so.isA(type, term))
399 public float getMinimumScore(String type, boolean positional)
401 return featureStore.containsKey(type) ? featureStore.get(type)
402 .getMinimumScore(positional) : Float.NaN;
409 public float getMaximumScore(String type, boolean positional)
411 return featureStore.containsKey(type) ? featureStore.get(type)
412 .getMaximumScore(positional) : Float.NaN;
416 * A convenience method to sort features by start position ascending (if on
417 * forward strand), or end position descending (if on reverse strand)
420 * @param forwardStrand
422 public static void sortFeatures(List<SequenceFeature> features,
423 final boolean forwardStrand)
425 Collections.sort(features, forwardStrand ? FORWARD_STRAND