1 package jalview.datamodel.features;
3 import jalview.datamodel.ContiguousI;
4 import jalview.datamodel.SequenceFeature;
5 import jalview.io.gff.SequenceOntologyFactory;
6 import jalview.io.gff.SequenceOntologyI;
8 import java.util.ArrayList;
9 import java.util.Arrays;
10 import java.util.Collections;
11 import java.util.Comparator;
12 import java.util.HashSet;
13 import java.util.List;
15 import java.util.Map.Entry;
17 import java.util.TreeMap;
20 * A class that stores sequence features in a way that supports efficient
21 * querying by type and location (overlap). Intended for (but not limited to)
22 * storage of features for one sequence.
27 public class SequenceFeatures implements SequenceFeaturesI
30 * a comparator for sorting features by start position ascending
32 private static Comparator<ContiguousI> FORWARD_STRAND = new Comparator<ContiguousI>()
35 public int compare(ContiguousI o1, ContiguousI o2)
37 return Integer.compare(o1.getBegin(), o2.getBegin());
42 * a comparator for sorting features by end position descending
44 private static Comparator<ContiguousI> REVERSE_STRAND = new Comparator<ContiguousI>()
47 public int compare(ContiguousI o1, ContiguousI o2)
49 return Integer.compare(o2.getEnd(), o1.getEnd());
54 * map from feature type to structured store of features for that type
55 * null types are permitted (but not a good idea!)
57 private Map<String, FeatureStore> featureStore;
62 public SequenceFeatures()
65 * use a TreeMap so that features are returned in alphabetical order of type
66 * ? wrap as a synchronized map for add and delete operations
68 // featureStore = Collections
69 // .synchronizedSortedMap(new TreeMap<String, FeatureStore>());
70 featureStore = new TreeMap<String, FeatureStore>();
74 * Constructor given a list of features
76 public SequenceFeatures(List<SequenceFeature> features)
81 for (SequenceFeature feature : features)
92 public boolean add(SequenceFeature sf)
94 String type = sf.getType();
97 System.err.println("Feature type may not be null: " + sf.toString());
101 if (featureStore.get(type) == null)
103 featureStore.put(type, new FeatureStore());
105 return featureStore.get(type).addFeature(sf);
112 public List<SequenceFeature> findFeatures(int from, int to,
115 List<SequenceFeature> result = new ArrayList<>();
117 for (FeatureStore featureSet : varargToTypes(type))
119 result.addAll(featureSet.findOverlappingFeatures(from, to));
129 public List<SequenceFeature> getAllFeatures(String... type)
131 List<SequenceFeature> result = new ArrayList<>();
133 result.addAll(getPositionalFeatures(type));
135 result.addAll(getNonPositionalFeatures());
144 public List<SequenceFeature> getFeaturesByOntology(String... ontologyTerm)
146 if (ontologyTerm == null || ontologyTerm.length == 0)
148 return new ArrayList<>();
151 Set<String> featureTypes = getFeatureTypes(ontologyTerm);
152 return getAllFeatures(featureTypes.toArray(new String[featureTypes
160 public int getFeatureCount(boolean positional, String... type)
164 for (FeatureStore featureSet : varargToTypes(type))
166 result += featureSet.getFeatureCount(positional);
175 public int getTotalFeatureLength(String... type)
179 for (FeatureStore featureSet : varargToTypes(type))
181 result += featureSet.getTotalFeatureLength();
190 public List<SequenceFeature> getPositionalFeatures(String... type)
192 List<SequenceFeature> result = new ArrayList<>();
194 for (FeatureStore featureSet : varargToTypes(type))
196 result.addAll(featureSet.getPositionalFeatures());
202 * A convenience method that converts a vararg for feature types to an
203 * Iterable over matched feature sets in key order
208 protected Iterable<FeatureStore> varargToTypes(String... type)
210 if (type == null || type.length == 0)
213 * no vararg parameter supplied - return all
215 return featureStore.values();
218 List<FeatureStore> types = new ArrayList<>();
219 List<String> args = Arrays.asList(type);
220 for (Entry<String, FeatureStore> featureType : featureStore.entrySet())
222 if (args.contains(featureType.getKey()))
224 types.add(featureType.getValue());
234 public List<SequenceFeature> getContactFeatures(String... type)
236 List<SequenceFeature> result = new ArrayList<>();
238 for (FeatureStore featureSet : varargToTypes(type))
240 result.addAll(featureSet.getContactFeatures());
249 public List<SequenceFeature> getNonPositionalFeatures(String... type)
251 List<SequenceFeature> result = new ArrayList<>();
253 for (FeatureStore featureSet : varargToTypes(type))
255 result.addAll(featureSet.getNonPositionalFeatures());
264 public boolean delete(SequenceFeature sf)
266 for (FeatureStore featureSet : featureStore.values())
268 if (featureSet.delete(sf))
280 public boolean hasFeatures()
282 for (FeatureStore featureSet : featureStore.values())
284 if (!featureSet.isEmpty())
296 public Set<String> getFeatureGroups(boolean positionalFeatures,
299 Set<String> groups = new HashSet<>();
301 for (FeatureStore featureSet : varargToTypes(type))
303 groups.addAll(featureSet.getFeatureGroups(positionalFeatures));
313 public Set<String> getFeatureTypesForGroups(boolean positionalFeatures,
316 Set<String> result = new HashSet<>();
318 for (Entry<String, FeatureStore> featureType : featureStore.entrySet())
320 Set<String> featureGroups = featureType.getValue().getFeatureGroups(
322 for (String group : groups)
324 if (featureGroups.contains(group))
327 * yes this feature type includes one of the query groups
329 result.add(featureType.getKey());
342 public Set<String> getFeatureTypes(String... soTerm)
344 Set<String> types = new HashSet<>();
345 for (Entry<String, FeatureStore> entry : featureStore.entrySet())
347 String type = entry.getKey();
348 if (!entry.getValue().isEmpty() && isOntologyTerm(type, soTerm))
357 * Answers true if the given type is one of the specified sequence ontology
358 * terms (or a sub-type of one), or if no terms are supplied. Answers false if
359 * filter terms are specified and the given term does not match any of them.
365 protected boolean isOntologyTerm(String type, String... soTerm)
367 if (soTerm == null || soTerm.length == 0)
371 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
372 for (String term : soTerm)
374 if (so.isA(type, term))
386 public float getMinimumScore(String type, boolean positional)
388 return featureStore.containsKey(type) ? featureStore.get(type)
389 .getMinimumScore(positional) : Float.NaN;
396 public float getMaximumScore(String type, boolean positional)
398 return featureStore.containsKey(type) ? featureStore.get(type)
399 .getMaximumScore(positional) : Float.NaN;
403 * A convenience method to sort features by start position ascending (if on
404 * forward strand), or end position descending (if on reverse strand)
407 * @param forwardStrand
409 public static void sortFeatures(List<SequenceFeature> features,
410 final boolean forwardStrand)
412 Collections.sort(features, forwardStrand ? FORWARD_STRAND
417 * {@inheritDoc} This method is 'semi-optimised': it only inspects features
418 * for types that include the specified group, but has to inspect every
419 * feature of those types for matching feature group. This is efficient unless
420 * a sequence has features that share the same type but are in different
421 * groups - an unlikely case.
423 * For example, if RESNUM feature is created with group = PDBID, then features
424 * would only be retrieved for those sequences associated with the target
428 public List<SequenceFeature> getFeaturesForGroup(boolean positional,
429 String group, String... type)
431 List<SequenceFeature> result = new ArrayList<>();
432 for (FeatureStore featureSet : varargToTypes(type))
434 if (featureSet.getFeatureGroups(positional).contains(group))
436 result.addAll(featureSet.getFeaturesForGroup(positional, group));
446 public boolean shiftFeatures(int shift)
448 boolean modified = false;
449 for (FeatureStore fs : featureStore.values())
451 modified |= fs.shiftFeatures(shift);