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 java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.HashSet;
26 import java.util.List;
28 import java.util.Map.Entry;
30 import java.util.TreeMap;
32 import intervalstore.api.IntervalI;
33 import jalview.datamodel.SequenceFeature;
34 import jalview.io.gff.SequenceOntologyFactory;
35 import jalview.io.gff.SequenceOntologyI;
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
48 * map from feature type to structured store of features for that type
49 * null types are permitted (but not a good idea!)
51 private Map<String, FeatureStore> featureStore;
56 public SequenceFeatures()
59 * use a TreeMap so that features are returned in alphabetical order of type
60 * ? wrap as a synchronized map for add and delete operations
62 // featureStore = Collections
63 // .synchronizedSortedMap(new TreeMap<String, FeatureStore>());
64 featureStore = new TreeMap<>();
68 * Constructor given a list of features
70 public SequenceFeatures(List<SequenceFeature> features)
75 for (SequenceFeature feature : features)
86 public boolean add(SequenceFeature sf)
88 String type = sf.getType();
91 jalview.bin.Console.errPrintln("Feature type may not be null: " + sf.toString());
95 if (featureStore.get(type) == null)
97 featureStore.put(type, new FeatureStore());
99 return featureStore.get(type).addFeature(sf);
106 public List<SequenceFeature> findFeatures(int from, int to,
109 List<SequenceFeature> result = new ArrayList<>();
111 for (FeatureStore featureSet : varargToTypes(type))
113 result.addAll(featureSet.findOverlappingFeatures(from, to));
123 public List<SequenceFeature> getAllFeatures(String... type)
125 List<SequenceFeature> result = new ArrayList<>();
127 result.addAll(getPositionalFeatures(type));
129 result.addAll(getNonPositionalFeatures());
138 public List<SequenceFeature> getFeaturesByOntology(String... ontologyTerm)
140 if (ontologyTerm == null || ontologyTerm.length == 0)
142 return new ArrayList<>();
145 Set<String> featureTypes = getFeatureTypes(ontologyTerm);
146 if (featureTypes.isEmpty())
149 * no features of the specified type or any sub-type
151 return new ArrayList<>();
154 return getAllFeatures(
155 featureTypes.toArray(new String[featureTypes.size()]));
162 public int getFeatureCount(boolean positional, String... type)
166 for (FeatureStore featureSet : varargToTypes(type))
168 result += featureSet.getFeatureCount(positional);
177 public int getTotalFeatureLength(String... type)
181 for (FeatureStore featureSet : varargToTypes(type))
183 result += featureSet.getTotalFeatureLength();
192 public List<SequenceFeature> getPositionalFeatures(String... type)
194 List<SequenceFeature> result = new ArrayList<>();
196 for (FeatureStore featureSet : varargToTypes(type))
198 result.addAll(featureSet.getPositionalFeatures());
204 * A convenience method that converts a vararg for feature types to an
205 * Iterable over matched feature sets. If no types are specified, all feature
206 * sets are returned. If one or more types are specified, feature sets for
207 * those types are returned, preserving the order of the types.
212 protected Iterable<FeatureStore> varargToTypes(String... type)
214 if (type == null || type.length == 0)
217 * no vararg parameter supplied - return all
219 return featureStore.values();
222 List<FeatureStore> types = new ArrayList<>();
223 for (String theType : type)
225 if (theType != null && featureStore.containsKey(theType))
227 types.add(featureStore.get(theType));
237 public List<SequenceFeature> getContactFeatures(String... type)
239 List<SequenceFeature> result = new ArrayList<>();
241 for (FeatureStore featureSet : varargToTypes(type))
243 result.addAll(featureSet.getContactFeatures());
252 public List<SequenceFeature> getNonPositionalFeatures(String... type)
254 List<SequenceFeature> result = new ArrayList<>();
256 for (FeatureStore featureSet : varargToTypes(type))
258 result.addAll(featureSet.getNonPositionalFeatures());
267 public boolean delete(SequenceFeature sf)
269 for (FeatureStore featureSet : featureStore.values())
271 if (featureSet.delete(sf))
283 public boolean hasFeatures()
285 for (FeatureStore featureSet : featureStore.values())
287 if (!featureSet.isEmpty())
299 public Set<String> getFeatureGroups(boolean positionalFeatures,
302 Set<String> groups = new HashSet<>();
304 for (FeatureStore featureSet : varargToTypes(type))
306 groups.addAll(featureSet.getFeatureGroups(positionalFeatures));
316 public Set<String> getFeatureTypesForGroups(boolean positionalFeatures,
319 Set<String> result = new HashSet<>();
321 for (Entry<String, FeatureStore> featureType : featureStore.entrySet())
323 Set<String> featureGroups = featureType.getValue()
324 .getFeatureGroups(positionalFeatures);
325 for (String group : groups)
327 if (featureGroups.contains(group))
330 * yes this feature type includes one of the query groups
332 result.add(featureType.getKey());
345 public Set<String> getFeatureTypes(String... soTerm)
347 Set<String> types = new HashSet<>();
348 for (Entry<String, FeatureStore> entry : featureStore.entrySet())
350 String type = entry.getKey();
351 if (!entry.getValue().isEmpty() && isOntologyTerm(type, soTerm))
360 * Answers true if the given type matches one of the specified terms (or is a
361 * sub-type of one in the Sequence Ontology), or if no terms are supplied.
362 * Answers false if filter terms are specified and the given term does not
369 protected boolean isOntologyTerm(String type, String... soTerm)
371 if (soTerm == null || soTerm.length == 0)
375 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
376 for (String term : soTerm)
378 if (type.equals(term) || so.isA(type, term))
390 public float getMinimumScore(String type, boolean positional)
392 return featureStore.containsKey(type)
393 ? featureStore.get(type).getMinimumScore(positional)
401 public float getMaximumScore(String type, boolean positional)
403 return featureStore.containsKey(type)
404 ? featureStore.get(type).getMaximumScore(positional)
409 * A convenience method to sort features by start position ascending (if on
410 * forward strand), or end position descending (if on reverse strand)
413 * @param forwardStrand
415 public static void sortFeatures(List<? extends IntervalI> features,
416 final boolean forwardStrand)
418 Collections.sort(features,
419 forwardStrand ? IntervalI.COMPARE_BEGIN_ASC_END_DESC
420 : IntervalI.COMPARE_END_DESC);
424 * {@inheritDoc} This method is 'semi-optimised': it only inspects features
425 * for types that include the specified group, but has to inspect every
426 * feature of those types for matching feature group. This is efficient unless
427 * a sequence has features that share the same type but are in different
428 * groups - an unlikely case.
430 * For example, if RESNUM feature is created with group = PDBID, then features
431 * would only be retrieved for those sequences associated with the target
435 public List<SequenceFeature> getFeaturesForGroup(boolean positional,
436 String group, String... type)
438 List<SequenceFeature> result = new ArrayList<>();
439 for (FeatureStore featureSet : varargToTypes(type))
441 if (featureSet.getFeatureGroups(positional).contains(group))
443 result.addAll(featureSet.getFeaturesForGroup(positional, group));
453 public boolean shiftFeatures(int fromPosition, int shiftBy)
455 boolean modified = false;
456 for (FeatureStore fs : featureStore.values())
458 modified |= fs.shiftFeatures(fromPosition, shiftBy);
467 public void deleteAll()
469 featureStore.clear();