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.HashSet;
29 import java.util.List;
31 import java.util.Map.Entry;
33 import java.util.TreeMap;
35 import intervalstore.api.IntervalI;
38 * A class that stores sequence features in a way that supports efficient
39 * querying by type and location (overlap). Intended for (but not limited to)
40 * storage of features for one sequence.
45 public class SequenceFeatures implements SequenceFeaturesI
49 * map from feature type to structured store of features for that type
50 * null types are permitted (but not a good idea!)
52 private Map<String, FeatureStore> featureStore;
57 public SequenceFeatures()
60 * use a TreeMap so that features are returned in alphabetical order of type
61 * ? wrap as a synchronized map for add and delete operations
63 // featureStore = Collections
64 // .synchronizedSortedMap(new TreeMap<String, FeatureStore>());
65 featureStore = new TreeMap<>();
69 * Constructor given a list of features
71 public SequenceFeatures(List<SequenceFeature> features)
76 for (SequenceFeature feature : features)
87 public boolean add(SequenceFeature sf)
89 String type = sf.getType();
92 System.err.println("Feature type may not be null: " + sf.toString());
96 if (featureStore.get(type) == null)
98 featureStore.put(type, new FeatureStore());
100 return featureStore.get(type).addFeature(sf);
107 public List<SequenceFeature> findFeatures(int from, int to,
110 List<SequenceFeature> result = new ArrayList<>();
112 for (FeatureStore featureSet : varargToTypes(type))
114 result.addAll(featureSet.findOverlappingFeatures(from, to));
124 public List<SequenceFeature> getAllFeatures(String... type)
126 List<SequenceFeature> result = new ArrayList<>();
128 result.addAll(getPositionalFeatures(type));
130 result.addAll(getNonPositionalFeatures());
139 public List<SequenceFeature> getFeaturesByOntology(String... ontologyTerm)
141 if (ontologyTerm == null || ontologyTerm.length == 0)
143 return new ArrayList<>();
146 Set<String> featureTypes = getFeatureTypes(ontologyTerm);
147 if (featureTypes.isEmpty())
150 * no features of the specified type or any sub-type
152 return new ArrayList<>();
155 return getAllFeatures(featureTypes.toArray(new String[featureTypes
163 public int getFeatureCount(boolean positional, String... type)
167 for (FeatureStore featureSet : varargToTypes(type))
169 result += featureSet.getFeatureCount(positional);
178 public int getTotalFeatureLength(String... type)
182 for (FeatureStore featureSet : varargToTypes(type))
184 result += featureSet.getTotalFeatureLength();
193 public List<SequenceFeature> getPositionalFeatures(String... type)
195 List<SequenceFeature> result = new ArrayList<>();
197 for (FeatureStore featureSet : varargToTypes(type))
199 result.addAll(featureSet.getPositionalFeatures());
205 * A convenience method that converts a vararg for feature types to an
206 * Iterable over matched feature sets. If no types are specified, all feature
207 * sets are returned. If one or more types are specified, feature sets for
208 * those types are returned, preserving the order of the types.
213 protected Iterable<FeatureStore> varargToTypes(String... type)
215 if (type == null || type.length == 0)
218 * no vararg parameter supplied - return all
220 return featureStore.values();
223 List<FeatureStore> types = new ArrayList<>();
224 for (String theType : type)
226 if (theType != null && featureStore.containsKey(theType))
228 types.add(featureStore.get(theType));
238 public List<SequenceFeature> getContactFeatures(String... type)
240 List<SequenceFeature> result = new ArrayList<>();
242 for (FeatureStore featureSet : varargToTypes(type))
244 result.addAll(featureSet.getContactFeatures());
253 public List<SequenceFeature> getNonPositionalFeatures(String... type)
255 List<SequenceFeature> result = new ArrayList<>();
257 for (FeatureStore featureSet : varargToTypes(type))
259 result.addAll(featureSet.getNonPositionalFeatures());
268 public boolean delete(SequenceFeature sf)
270 for (FeatureStore featureSet : featureStore.values())
272 if (featureSet.delete(sf))
284 public boolean hasFeatures()
286 for (FeatureStore featureSet : featureStore.values())
288 if (!featureSet.isEmpty())
300 public Set<String> getFeatureGroups(boolean positionalFeatures,
303 Set<String> groups = new HashSet<>();
305 for (FeatureStore featureSet : varargToTypes(type))
307 groups.addAll(featureSet.getFeatureGroups(positionalFeatures));
317 public Set<String> getFeatureTypesForGroups(boolean positionalFeatures,
320 Set<String> result = new HashSet<>();
322 for (Entry<String, FeatureStore> featureType : featureStore.entrySet())
324 Set<String> featureGroups = featureType.getValue().getFeatureGroups(
326 for (String group : groups)
328 if (featureGroups.contains(group))
331 * yes this feature type includes one of the query groups
333 result.add(featureType.getKey());
346 public Set<String> getFeatureTypes(String... soTerm)
348 Set<String> types = new HashSet<>();
349 for (Entry<String, FeatureStore> entry : featureStore.entrySet())
351 String type = entry.getKey();
352 if (!entry.getValue().isEmpty() && isOntologyTerm(type, soTerm))
361 * Answers true if the given type matches one of the specified terms (or is a
362 * sub-type of one in the Sequence Ontology), or if no terms are supplied.
363 * Answers false if filter terms are specified and the given term does not
370 protected boolean isOntologyTerm(String type, String... soTerm)
372 if (soTerm == null || soTerm.length == 0)
376 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
377 for (String term : soTerm)
379 if (type.equals(term) || so.isA(type, term))
391 public float getMinimumScore(String type, boolean positional)
393 return featureStore.containsKey(type) ? featureStore.get(type)
394 .getMinimumScore(positional) : Float.NaN;
401 public float getMaximumScore(String type, boolean positional)
403 return featureStore.containsKey(type) ? featureStore.get(type)
404 .getMaximumScore(positional) : Float.NaN;
408 * A convenience method to sort features by start position ascending (if on
409 * forward strand), or end position descending (if on reverse strand)
412 * @param forwardStrand
414 public static void sortFeatures(List<? extends IntervalI> features,
415 final boolean forwardStrand)
417 IntervalI.sortIntervals(features, forwardStrand);
421 * {@inheritDoc} This method is 'semi-optimised': it only inspects features
422 * for types that include the specified group, but has to inspect every
423 * feature of those types for matching feature group. This is efficient unless
424 * a sequence has features that share the same type but are in different
425 * groups - an unlikely case.
427 * For example, if RESNUM feature is created with group = PDBID, then features
428 * would only be retrieved for those sequences associated with the target
432 public List<SequenceFeature> getFeaturesForGroup(boolean positional,
433 String group, String... type)
435 List<SequenceFeature> result = new ArrayList<>();
436 for (FeatureStore featureSet : varargToTypes(type))
438 if (featureSet.getFeatureGroups(positional).contains(group))
440 result.addAll(featureSet.getFeaturesForGroup(positional, group));
450 public boolean shiftFeatures(int fromPosition, int shiftBy)
452 boolean modified = false;
453 for (FeatureStore fs : featureStore.values())
455 modified |= fs.shiftFeatures(fromPosition, shiftBy);
464 public void deleteAll()
466 featureStore.clear();