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 if (featureTypes.isEmpty())
155 * no features of the specified type or any sub-type
157 return new ArrayList<>();
160 return getAllFeatures(featureTypes.toArray(new String[featureTypes
168 public int getFeatureCount(boolean positional, String... type)
172 for (FeatureStore featureSet : varargToTypes(type))
174 result += featureSet.getFeatureCount(positional);
183 public int getTotalFeatureLength(String... type)
187 for (FeatureStore featureSet : varargToTypes(type))
189 result += featureSet.getTotalFeatureLength();
198 public List<SequenceFeature> getPositionalFeatures(String... type)
200 List<SequenceFeature> result = new ArrayList<>();
202 for (FeatureStore featureSet : varargToTypes(type))
204 result.addAll(featureSet.getPositionalFeatures());
210 * A convenience method that converts a vararg for feature types to an
211 * Iterable over matched feature sets in key order
216 protected Iterable<FeatureStore> varargToTypes(String... type)
218 if (type == null || type.length == 0)
221 * no vararg parameter supplied - return all
223 return featureStore.values();
226 List<FeatureStore> types = new ArrayList<>();
227 List<String> args = Arrays.asList(type);
228 for (Entry<String, FeatureStore> featureType : featureStore.entrySet())
230 if (args.contains(featureType.getKey()))
232 types.add(featureType.getValue());
242 public List<SequenceFeature> getContactFeatures(String... type)
244 List<SequenceFeature> result = new ArrayList<>();
246 for (FeatureStore featureSet : varargToTypes(type))
248 result.addAll(featureSet.getContactFeatures());
257 public List<SequenceFeature> getNonPositionalFeatures(String... type)
259 List<SequenceFeature> result = new ArrayList<>();
261 for (FeatureStore featureSet : varargToTypes(type))
263 result.addAll(featureSet.getNonPositionalFeatures());
272 public boolean delete(SequenceFeature sf)
274 for (FeatureStore featureSet : featureStore.values())
276 if (featureSet.delete(sf))
288 public boolean hasFeatures()
290 for (FeatureStore featureSet : featureStore.values())
292 if (!featureSet.isEmpty())
304 public Set<String> getFeatureGroups(boolean positionalFeatures,
307 Set<String> groups = new HashSet<>();
309 for (FeatureStore featureSet : varargToTypes(type))
311 groups.addAll(featureSet.getFeatureGroups(positionalFeatures));
321 public Set<String> getFeatureTypesForGroups(boolean positionalFeatures,
324 Set<String> result = new HashSet<>();
326 for (Entry<String, FeatureStore> featureType : featureStore.entrySet())
328 Set<String> featureGroups = featureType.getValue().getFeatureGroups(
330 for (String group : groups)
332 if (featureGroups.contains(group))
335 * yes this feature type includes one of the query groups
337 result.add(featureType.getKey());
350 public Set<String> getFeatureTypes(String... soTerm)
352 Set<String> types = new HashSet<>();
353 for (Entry<String, FeatureStore> entry : featureStore.entrySet())
355 String type = entry.getKey();
356 if (!entry.getValue().isEmpty() && isOntologyTerm(type, soTerm))
365 * Answers true if the given type is one of the specified sequence ontology
366 * terms (or a sub-type of one), or if no terms are supplied. Answers false if
367 * filter terms are specified and the given term does not match any of them.
373 protected boolean isOntologyTerm(String type, String... soTerm)
375 if (soTerm == null || soTerm.length == 0)
379 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
380 for (String term : soTerm)
382 if (so.isA(type, term))
394 public float getMinimumScore(String type, boolean positional)
396 return featureStore.containsKey(type) ? featureStore.get(type)
397 .getMinimumScore(positional) : Float.NaN;
404 public float getMaximumScore(String type, boolean positional)
406 return featureStore.containsKey(type) ? featureStore.get(type)
407 .getMaximumScore(positional) : Float.NaN;
411 * A convenience method to sort features by start position ascending (if on
412 * forward strand), or end position descending (if on reverse strand)
415 * @param forwardStrand
417 public static void sortFeatures(List<SequenceFeature> features,
418 final boolean forwardStrand)
420 Collections.sort(features, forwardStrand ? FORWARD_STRAND
425 * {@inheritDoc} This method is 'semi-optimised': it only inspects features
426 * for types that include the specified group, but has to inspect every
427 * feature of those types for matching feature group. This is efficient unless
428 * a sequence has features that share the same type but are in different
429 * groups - an unlikely case.
431 * For example, if RESNUM feature is created with group = PDBID, then features
432 * would only be retrieved for those sequences associated with the target
436 public List<SequenceFeature> getFeaturesForGroup(boolean positional,
437 String group, String... type)
439 List<SequenceFeature> result = new ArrayList<>();
440 for (FeatureStore featureSet : varargToTypes(type))
442 if (featureSet.getFeatureGroups(positional).contains(group))
444 result.addAll(featureSet.getFeaturesForGroup(positional, group));
454 public boolean shiftFeatures(int shift)
456 boolean modified = false;
457 for (FeatureStore fs : featureStore.values())
459 modified |= fs.shiftFeatures(shift);