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());
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
223 * sort in alphabetical order for consistent output behaviour
225 List<String> types = new ArrayList<String>(Arrays.asList(type));
227 Collections.sort(types);
235 public List<SequenceFeature> getContactFeatures(String... type)
237 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
239 for (String featureType : varargToTypes(type))
241 FeatureStore featureSet = featureStore.get(featureType);
242 if (featureSet != null)
244 result.addAll(featureSet.getContactFeatures());
254 public List<SequenceFeature> getNonPositionalFeatures(String... type)
256 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
258 for (String featureType : varargToTypes(type))
260 FeatureStore featureSet = featureStore.get(featureType);
261 if (featureSet != null)
263 result.addAll(featureSet.getNonPositionalFeatures());
273 public boolean delete(SequenceFeature sf)
275 for (FeatureStore featureSet : featureStore.values())
277 if (featureSet.delete(sf))
289 public boolean hasFeatures()
291 for (FeatureStore featureSet : featureStore.values())
293 if (!featureSet.isEmpty())
305 public Set<String> getFeatureGroups(boolean positionalFeatures,
308 Set<String> groups = new HashSet<String>();
310 Iterable<String> types = varargToTypes(type);
312 for (String featureType : types)
314 FeatureStore featureSet = featureStore.get(featureType);
315 if (featureSet != null)
317 groups.addAll(featureSet.getFeatureGroups(positionalFeatures));
328 public Set<String> getFeatureTypesForGroups(boolean positionalFeatures,
331 Set<String> result = new HashSet<String>();
333 for (Entry<String, FeatureStore> featureType : featureStore.entrySet())
335 Set<String> featureGroups = featureType.getValue().getFeatureGroups(
337 for (String group : groups)
339 if (featureGroups.contains(group))
342 * yes this feature type includes one of the query groups
344 result.add(featureType.getKey());
357 public Set<String> getFeatureTypes(String... soTerm)
359 Set<String> types = new HashSet<String>();
360 for (Entry<String, FeatureStore> entry : featureStore.entrySet())
362 String type = entry.getKey();
363 if (!entry.getValue().isEmpty() && isOntologyTerm(type, soTerm))
372 * Answers true if the given type is one of the specified sequence ontology
373 * terms (or a sub-type of one), or if no terms are supplied. Answers false if
374 * filter terms are specified and the given term does not match any of them.
380 protected boolean isOntologyTerm(String type, String... soTerm)
382 if (soTerm == null || soTerm.length == 0)
386 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
387 for (String term : soTerm)
389 if (so.isA(type, term))
401 public float getMinimumScore(String type, boolean positional)
403 return featureStore.containsKey(type) ? featureStore.get(type)
404 .getMinimumScore(positional) : Float.NaN;
411 public float getMaximumScore(String type, boolean positional)
413 return featureStore.containsKey(type) ? featureStore.get(type)
414 .getMaximumScore(positional) : Float.NaN;
418 * A convenience method to sort features by start position ascending (if on
419 * forward strand), or end position descending (if on reverse strand)
422 * @param forwardStrand
424 public static void sortFeatures(List<SequenceFeature> features,
425 final boolean forwardStrand)
427 Collections.sort(features, forwardStrand ? FORWARD_STRAND
432 * {@inheritDoc} This method is 'semi-optimised': it only inspects features
433 * for types that include the specified group, but has to inspect every
434 * feature of those types for matching feature group. This is efficient unless
435 * a sequence has features that share the same type but are in different
436 * groups - an unlikely case.
438 * For example, if RESNUM feature is created with group = PDBID, then features
439 * would only be retrieved for those sequences associated with the target
443 public List<SequenceFeature> getFeaturesForGroup(boolean positional,
444 String group, String... type)
446 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
447 Iterable<String> types = varargToTypes(type);
449 for (String featureType : types)
452 * check whether the feature type is present, and also
453 * whether it has features for the specified group
455 FeatureStore features = featureStore.get(featureType);
457 && features.getFeatureGroups(positional).contains(group))
459 result.addAll(features.getFeaturesForGroup(positional, group));
469 public boolean shiftFeatures(int shift)
471 boolean modified = false;
472 for (FeatureStore fs : featureStore.values())
474 modified |= fs.shiftFeatures(shift);