2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.datamodel.features;
23 import jalview.datamodel.SequenceFeature;
24 import jalview.io.gff.SequenceOntologyFactory;
25 import jalview.io.gff.SequenceOntologyI;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.Collections;
30 import java.util.Comparator;
31 import java.util.HashSet;
32 import java.util.List;
34 import java.util.Map.Entry;
36 import java.util.TreeMap;
38 import intervalstore.api.IntervalI;
41 * A class that stores sequence features in a way that supports efficient
42 * querying by type and location (overlap). Intended for (but not limited to)
43 * storage of features for one sequence.
48 public class SequenceFeatures implements SequenceFeaturesI
51 * a comparator for sorting features by start position ascending
53 private static Comparator<IntervalI> FORWARD_STRAND = new Comparator<IntervalI>()
56 public int compare(IntervalI o1, IntervalI o2)
58 return Integer.compare(o1.getBegin(), o2.getBegin());
63 * a comparator for sorting features by end position descending
65 private static Comparator<IntervalI> REVERSE_STRAND = new Comparator<IntervalI>()
68 public int compare(IntervalI o1, IntervalI o2)
70 return Integer.compare(o2.getEnd(), o1.getEnd());
75 * map from feature type to structured store of features for that type
76 * null types are permitted (but not a good idea!)
78 private Map<String, FeatureStore> featureStore;
83 public SequenceFeatures()
86 * use a TreeMap so that features are returned in alphabetical order of type
87 * ? wrap as a synchronized map for add and delete operations
89 // featureStore = Collections
90 // .synchronizedSortedMap(new TreeMap<String, FeatureStore>());
91 featureStore = new TreeMap<>();
95 * Constructor given a list of features
97 public SequenceFeatures(List<SequenceFeature> features)
100 if (features != null)
102 for (SequenceFeature feature : features)
113 public boolean add(SequenceFeature sf)
115 String type = sf.getType();
118 System.err.println("Feature type may not be null: " + sf.toString());
122 if (featureStore.get(type) == null)
124 featureStore.put(type, new FeatureStore());
126 return featureStore.get(type).addFeature(sf);
133 public List<SequenceFeature> findFeatures(int from, int to,
136 List<SequenceFeature> result = new ArrayList<>();
138 for (FeatureStore featureSet : varargToTypes(type))
140 result.addAll(featureSet.findOverlappingFeatures(from, to));
150 public List<SequenceFeature> getAllFeatures(String... type)
152 List<SequenceFeature> result = new ArrayList<>();
154 result.addAll(getPositionalFeatures(type));
156 result.addAll(getNonPositionalFeatures());
165 public List<SequenceFeature> getFeaturesByOntology(String... ontologyTerm)
167 if (ontologyTerm == null || ontologyTerm.length == 0)
169 return new ArrayList<>();
172 Set<String> featureTypes = getFeatureTypes(ontologyTerm);
173 if (featureTypes.isEmpty())
176 * no features of the specified type or any sub-type
178 return new ArrayList<>();
181 return getAllFeatures(featureTypes.toArray(new String[featureTypes
189 public int getFeatureCount(boolean positional, String... type)
193 for (FeatureStore featureSet : varargToTypes(type))
195 result += featureSet.getFeatureCount(positional);
204 public int getTotalFeatureLength(String... type)
208 for (FeatureStore featureSet : varargToTypes(type))
210 result += featureSet.getTotalFeatureLength();
219 public List<SequenceFeature> getPositionalFeatures(String... type)
221 List<SequenceFeature> result = new ArrayList<>();
223 for (FeatureStore featureSet : varargToTypes(type))
225 result.addAll(featureSet.getPositionalFeatures());
231 * A convenience method that converts a vararg for feature types to an
232 * Iterable over matched feature sets in key order
237 protected Iterable<FeatureStore> varargToTypes(String... type)
239 if (type == null || type.length == 0)
242 * no vararg parameter supplied - return all
244 return featureStore.values();
247 List<FeatureStore> types = new ArrayList<>();
248 List<String> args = Arrays.asList(type);
249 for (Entry<String, FeatureStore> featureType : featureStore.entrySet())
251 if (args.contains(featureType.getKey()))
253 types.add(featureType.getValue());
263 public List<SequenceFeature> getContactFeatures(String... type)
265 List<SequenceFeature> result = new ArrayList<>();
267 for (FeatureStore featureSet : varargToTypes(type))
269 result.addAll(featureSet.getContactFeatures());
278 public List<SequenceFeature> getNonPositionalFeatures(String... type)
280 List<SequenceFeature> result = new ArrayList<>();
282 for (FeatureStore featureSet : varargToTypes(type))
284 result.addAll(featureSet.getNonPositionalFeatures());
293 public boolean delete(SequenceFeature sf)
295 for (FeatureStore featureSet : featureStore.values())
297 if (featureSet.delete(sf))
309 public boolean hasFeatures()
311 for (FeatureStore featureSet : featureStore.values())
313 if (!featureSet.isEmpty())
325 public Set<String> getFeatureGroups(boolean positionalFeatures,
328 Set<String> groups = new HashSet<>();
330 for (FeatureStore featureSet : varargToTypes(type))
332 groups.addAll(featureSet.getFeatureGroups(positionalFeatures));
342 public Set<String> getFeatureTypesForGroups(boolean positionalFeatures,
345 Set<String> result = new HashSet<>();
347 for (Entry<String, FeatureStore> featureType : featureStore.entrySet())
349 Set<String> featureGroups = featureType.getValue().getFeatureGroups(
351 for (String group : groups)
353 if (featureGroups.contains(group))
356 * yes this feature type includes one of the query groups
358 result.add(featureType.getKey());
371 public Set<String> getFeatureTypes(String... soTerm)
373 Set<String> types = new HashSet<>();
374 for (Entry<String, FeatureStore> entry : featureStore.entrySet())
376 String type = entry.getKey();
377 if (!entry.getValue().isEmpty() && isOntologyTerm(type, soTerm))
386 * Answers true if the given type matches one of the specified terms (or is a
387 * sub-type of one in the Sequence Ontology), or if no terms are supplied.
388 * Answers false if filter terms are specified and the given term does not
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 (type.equals(term) || 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<>();
462 for (FeatureStore featureSet : varargToTypes(type))
464 if (featureSet.getFeatureGroups(positional).contains(group))
466 result.addAll(featureSet.getFeaturesForGroup(positional, group));
476 public boolean shiftFeatures(int shift)
478 boolean modified = false;
479 for (FeatureStore fs : featureStore.values())
481 modified |= fs.shiftFeatures(shift);