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<SequenceFeature>();
117 for (String featureType : varargToTypes(type))
119 FeatureStore features = featureStore.get(featureType);
120 if (features != null)
122 result.addAll(features.findOverlappingFeatures(from, to));
133 public List<SequenceFeature> getAllFeatures(String... type)
135 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
137 result.addAll(getPositionalFeatures(type));
139 result.addAll(getNonPositionalFeatures());
148 public List<SequenceFeature> getFeaturesByOntology(String... ontologyTerm)
150 if (ontologyTerm == null || ontologyTerm.length == 0)
152 return new ArrayList<SequenceFeature>();
155 Set<String> featureTypes = getFeatureTypes(ontologyTerm);
156 return getAllFeatures(featureTypes.toArray(new String[featureTypes
164 public int getFeatureCount(boolean positional, String... type)
168 for (String featureType : varargToTypes(type))
170 FeatureStore featureSet = featureStore.get(featureType);
171 if (featureSet != null)
173 result += featureSet.getFeatureCount(positional);
183 public int getTotalFeatureLength(String... type)
187 for (String featureType : varargToTypes(type))
189 FeatureStore featureSet = featureStore.get(featureType);
190 if (featureSet != null)
192 result += featureSet.getTotalFeatureLength();
203 public List<SequenceFeature> getPositionalFeatures(String... type)
205 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
207 for (String featureType : varargToTypes(type))
209 FeatureStore featureSet = featureStore.get(featureType);
210 if (featureSet != null)
212 result.addAll(featureSet.getPositionalFeatures());
219 * A convenience method that converts a vararg for feature types to an
220 * Iterable, replacing the value with the stored feature types if it is null
226 protected Iterable<String> varargToTypes(String... type)
228 if (type == null || type.length == 0)
231 * no vararg parameter supplied
233 return featureStore.keySet();
237 * else make a copy of the list, and remove any null value just in case,
238 * as it would cause errors looking up the features Map
239 * sort in alphabetical order for consistent output behaviour
241 List<String> types = new ArrayList<String>(Arrays.asList(type));
243 Collections.sort(types);
251 public List<SequenceFeature> getContactFeatures(String... type)
253 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
255 for (String featureType : varargToTypes(type))
257 FeatureStore featureSet = featureStore.get(featureType);
258 if (featureSet != null)
260 result.addAll(featureSet.getContactFeatures());
270 public List<SequenceFeature> getNonPositionalFeatures(String... type)
272 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
274 for (String featureType : varargToTypes(type))
276 FeatureStore featureSet = featureStore.get(featureType);
277 if (featureSet != null)
279 result.addAll(featureSet.getNonPositionalFeatures());
289 public boolean delete(SequenceFeature sf)
291 for (FeatureStore featureSet : featureStore.values())
293 if (featureSet.delete(sf))
305 public boolean hasFeatures()
307 for (FeatureStore featureSet : featureStore.values())
309 if (!featureSet.isEmpty())
321 public Set<String> getFeatureGroups(boolean positionalFeatures,
324 Set<String> groups = new HashSet<String>();
326 Iterable<String> types = varargToTypes(type);
328 for (String featureType : types)
330 FeatureStore featureSet = featureStore.get(featureType);
331 if (featureSet != null)
333 groups.addAll(featureSet.getFeatureGroups(positionalFeatures));
344 public Set<String> getFeatureTypesForGroups(boolean positionalFeatures,
347 Set<String> result = new HashSet<String>();
349 for (Entry<String, FeatureStore> featureType : featureStore.entrySet())
351 Set<String> featureGroups = featureType.getValue().getFeatureGroups(
353 for (String group : groups)
355 if (featureGroups.contains(group))
358 * yes this feature type includes one of the query groups
360 result.add(featureType.getKey());
373 public Set<String> getFeatureTypes(String... soTerm)
375 Set<String> types = new HashSet<String>();
376 for (Entry<String, FeatureStore> entry : featureStore.entrySet())
378 String type = entry.getKey();
379 if (!entry.getValue().isEmpty() && isOntologyTerm(type, soTerm))
388 * Answers true if the given type is one of the specified sequence ontology
389 * terms (or a sub-type of one), or if no terms are supplied. Answers false if
390 * filter terms are specified and the given term does not match any of them.
396 protected boolean isOntologyTerm(String type, String... soTerm)
398 if (soTerm == null || soTerm.length == 0)
402 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
403 for (String term : soTerm)
405 if (so.isA(type, term))
417 public float getMinimumScore(String type, boolean positional)
419 return featureStore.containsKey(type) ? featureStore.get(type)
420 .getMinimumScore(positional) : Float.NaN;
427 public float getMaximumScore(String type, boolean positional)
429 return featureStore.containsKey(type) ? featureStore.get(type)
430 .getMaximumScore(positional) : Float.NaN;
434 * A convenience method to sort features by start position ascending (if on
435 * forward strand), or end position descending (if on reverse strand)
438 * @param forwardStrand
440 public static void sortFeatures(List<SequenceFeature> features,
441 final boolean forwardStrand)
443 Collections.sort(features, forwardStrand ? FORWARD_STRAND
448 * {@inheritDoc} This method is 'semi-optimised': it only inspects features
449 * for types that include the specified group, but has to inspect every
450 * feature of those types for matching feature group. This is efficient unless
451 * a sequence has features that share the same type but are in different
452 * groups - an unlikely case.
454 * For example, if RESNUM feature is created with group = PDBID, then features
455 * would only be retrieved for those sequences associated with the target
459 public List<SequenceFeature> getFeaturesForGroup(boolean positional,
460 String group, String... type)
462 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
463 Iterable<String> types = varargToTypes(type);
465 for (String featureType : types)
468 * check whether the feature type is present, and also
469 * whether it has features for the specified group
471 FeatureStore features = featureStore.get(featureType);
473 && features.getFeatureGroups(positional).contains(group))
475 result.addAll(features.getFeaturesForGroup(positional, group));
485 public boolean shiftFeatures(int shift)
487 boolean modified = false;
488 for (FeatureStore fs : featureStore.values())
490 modified |= fs.shiftFeatures(shift);