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>();
73 * Constructor given a list of features
75 public SequenceFeatures(List<SequenceFeature> features)
80 for (SequenceFeature feature : features)
91 public boolean add(SequenceFeature sf)
93 String type = sf.getType();
96 System.err.println("Feature type may not be null: " + sf.toString());
100 if (featureStore.get(type) == null)
102 featureStore.put(type, new FeatureStore());
104 return featureStore.get(type).addFeature(sf);
111 public List<SequenceFeature> findFeatures(int from, int to,
114 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
116 for (String featureType : varargToTypes(type))
118 FeatureStore features = featureStore.get(featureType);
119 if (features != null)
121 result.addAll(features.findOverlappingFeatures(from, to));
132 public List<SequenceFeature> getAllFeatures(String... type)
134 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
136 result.addAll(getPositionalFeatures(type));
138 result.addAll(getNonPositionalFeatures());
147 public List<SequenceFeature> getFeaturesByOntology(String... ontologyTerm)
149 if (ontologyTerm == null || ontologyTerm.length == 0)
151 return new ArrayList<SequenceFeature>();
154 Set<String> featureTypes = getFeatureTypes(ontologyTerm);
155 return getAllFeatures(featureTypes.toArray(new String[featureTypes
163 public int getFeatureCount(boolean positional, String... type)
167 for (String featureType : varargToTypes(type))
169 FeatureStore featureSet = featureStore.get(featureType);
170 if (featureSet != null)
172 result += featureSet.getFeatureCount(positional);
182 public int getTotalFeatureLength(String... type)
186 for (String featureType : varargToTypes(type))
188 FeatureStore featureSet = featureStore.get(featureType);
189 if (featureSet != null)
191 result += featureSet.getTotalFeatureLength();
202 public List<SequenceFeature> getPositionalFeatures(String... type)
204 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
206 for (String featureType : varargToTypes(type))
208 FeatureStore featureSet = featureStore.get(featureType);
209 if (featureSet != null)
211 result.addAll(featureSet.getPositionalFeatures());
218 * A convenience method that converts a vararg for feature types to an
219 * Iterable, replacing the value with the stored feature types if it is null
225 protected Iterable<String> varargToTypes(String... type)
227 if (type == null || type.length == 0)
230 * no vararg parameter supplied
232 return featureStore.keySet();
236 * else make a copy of the list, and remove any null value just in case,
237 * as it would cause errors looking up the features Map
238 * sort in alphabetical order for consistent output behaviour
240 List<String> types = new ArrayList<String>(Arrays.asList(type));
242 Collections.sort(types);
250 public List<SequenceFeature> getContactFeatures(String... type)
252 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
254 for (String featureType : varargToTypes(type))
256 FeatureStore featureSet = featureStore.get(featureType);
257 if (featureSet != null)
259 result.addAll(featureSet.getContactFeatures());
269 public List<SequenceFeature> getNonPositionalFeatures(String... type)
271 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
273 for (String featureType : varargToTypes(type))
275 FeatureStore featureSet = featureStore.get(featureType);
276 if (featureSet != null)
278 result.addAll(featureSet.getNonPositionalFeatures());
288 public boolean delete(SequenceFeature sf)
290 for (FeatureStore featureSet : featureStore.values())
292 if (featureSet.delete(sf))
304 public boolean hasFeatures()
306 for (FeatureStore featureSet : featureStore.values())
308 if (!featureSet.isEmpty())
320 public Set<String> getFeatureGroups(boolean positionalFeatures,
323 Set<String> groups = new HashSet<String>();
325 Iterable<String> types = varargToTypes(type);
327 for (String featureType : types)
329 FeatureStore featureSet = featureStore.get(featureType);
330 if (featureSet != null)
332 groups.addAll(featureSet.getFeatureGroups(positionalFeatures));
343 public Set<String> getFeatureTypesForGroups(boolean positionalFeatures,
346 Set<String> result = new HashSet<String>();
348 for (Entry<String, FeatureStore> featureType : featureStore.entrySet())
350 Set<String> featureGroups = featureType.getValue().getFeatureGroups(
352 for (String group : groups)
354 if (featureGroups.contains(group))
357 * yes this feature type includes one of the query groups
359 result.add(featureType.getKey());
372 public Set<String> getFeatureTypes(String... soTerm)
374 Set<String> types = new HashSet<String>();
375 for (Entry<String, FeatureStore> entry : featureStore.entrySet())
377 String type = entry.getKey();
378 if (!entry.getValue().isEmpty() && isOntologyTerm(type, soTerm))
387 * Answers true if the given type is one of the specified sequence ontology
388 * terms (or a sub-type of one), or if no terms are supplied. Answers false if
389 * filter terms are specified and the given term does not match any of them.
395 protected boolean isOntologyTerm(String type, String... soTerm)
397 if (soTerm == null || soTerm.length == 0)
401 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
402 for (String term : soTerm)
404 if (so.isA(type, term))
416 public float getMinimumScore(String type, boolean positional)
418 return featureStore.containsKey(type) ? featureStore.get(type)
419 .getMinimumScore(positional) : Float.NaN;
426 public float getMaximumScore(String type, boolean positional)
428 return featureStore.containsKey(type) ? featureStore.get(type)
429 .getMaximumScore(positional) : Float.NaN;
433 * A convenience method to sort features by start position ascending (if on
434 * forward strand), or end position descending (if on reverse strand)
437 * @param forwardStrand
439 public static void sortFeatures(List<SequenceFeature> features,
440 final boolean forwardStrand)
442 Collections.sort(features, forwardStrand ? FORWARD_STRAND
447 * {@inheritDoc} This method is 'semi-optimised': it only inspects features
448 * for types that include the specified group, but has to inspect every
449 * feature of those types for matching feature group. This is efficient unless
450 * a sequence has features that share the same type but are in different
451 * groups - an unlikely case.
453 * For example, if RESNUM feature is created with group = PDBID, then features
454 * would only be retrieved for those sequences associated with the target
458 public List<SequenceFeature> getFeaturesForGroup(boolean positional,
459 String group, String... type)
461 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
462 Iterable<String> types = varargToTypes(type);
464 for (String featureType : types)
467 * check whether the feature type is present, and also
468 * whether it has features for the specified group
470 FeatureStore features = featureStore.get(featureType);
472 && features.getFeatureGroups(positional).contains(group))
474 result.addAll(features.getFeaturesForGroup(positional, group));
484 public boolean shiftFeatures(int shift)
486 boolean modified = false;
487 for (FeatureStore fs : featureStore.values())
489 modified |= fs.shiftFeatures(shift);