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.ContiguousI;
24 import jalview.datamodel.SequenceFeature;
25 import jalview.io.gff.SequenceOntologyFactory;
26 import jalview.io.gff.SequenceOntologyI;
28 import java.util.ArrayList;
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;
39 * A class that stores sequence features in a way that supports efficient
40 * querying by type and location (overlap). Intended for (but not limited to)
41 * storage of features for one sequence.
46 public class SequenceFeatures implements SequenceFeaturesI
49 * a comparator for sorting features by start position ascending
51 private static Comparator<ContiguousI> FORWARD_STRAND = new Comparator<ContiguousI>()
54 public int compare(ContiguousI o1, ContiguousI o2)
56 return Integer.compare(o1.getBegin(), o2.getBegin());
61 * a comparator for sorting features by end position descending
63 private static Comparator<ContiguousI> REVERSE_STRAND = new Comparator<ContiguousI>()
66 public int compare(ContiguousI o1, ContiguousI o2)
68 return Integer.compare(o2.getEnd(), o1.getEnd());
73 * map from feature type to structured store of features for that type
74 * null types are permitted (but not a good idea!)
76 private Map<String, FeatureStore> featureStore;
81 public SequenceFeatures()
84 * use a TreeMap so that features are returned in alphabetical order of type
85 * ? wrap as a synchronized map for add and delete operations
87 // featureStore = Collections
88 // .synchronizedSortedMap(new TreeMap<String, FeatureStore>());
89 featureStore = new TreeMap<>();
93 * Constructor given a list of features
95 public SequenceFeatures(List<SequenceFeature> features)
100 for (SequenceFeature feature : features)
111 public boolean add(SequenceFeature sf)
113 String type = sf.getType();
116 System.err.println("Feature type may not be null: " + sf.toString());
120 if (featureStore.get(type) == null)
122 featureStore.put(type, new FeatureStore());
124 return featureStore.get(type).addFeature(sf);
131 public List<SequenceFeature> findFeatures(int from, int to,
134 List<SequenceFeature> result = new ArrayList<>();
136 for (FeatureStore featureSet : varargToTypes(type))
138 result.addAll(featureSet.findOverlappingFeatures(from, to));
148 public List<SequenceFeature> getAllFeatures(String... type)
150 List<SequenceFeature> result = new ArrayList<>();
152 result.addAll(getPositionalFeatures(type));
154 result.addAll(getNonPositionalFeatures());
163 public List<SequenceFeature> getFeaturesByOntology(String... ontologyTerm)
165 if (ontologyTerm == null || ontologyTerm.length == 0)
167 return new ArrayList<>();
170 Set<String> featureTypes = getFeatureTypes(ontologyTerm);
171 if (featureTypes.isEmpty())
174 * no features of the specified type or any sub-type
176 return new ArrayList<>();
179 return getAllFeatures(featureTypes.toArray(new String[featureTypes
187 public int getFeatureCount(boolean positional, String... type)
191 for (FeatureStore featureSet : varargToTypes(type))
193 result += featureSet.getFeatureCount(positional);
202 public int getTotalFeatureLength(String... type)
206 for (FeatureStore featureSet : varargToTypes(type))
208 result += featureSet.getTotalFeatureLength();
217 public List<SequenceFeature> getPositionalFeatures(String... type)
219 List<SequenceFeature> result = new ArrayList<>();
221 for (FeatureStore featureSet : varargToTypes(type))
223 result.addAll(featureSet.getPositionalFeatures());
229 * A convenience method that converts a vararg for feature types to an
230 * Iterable over matched feature sets. If no types are specified, all feature
231 * sets are returned. If one or more types are specified, feature sets for
232 * those types are returned, preserving the order of the types.
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 for (String theType : type)
250 if (featureStore.containsKey(theType))
252 types.add(featureStore.get(theType));
262 public List<SequenceFeature> getContactFeatures(String... type)
264 List<SequenceFeature> result = new ArrayList<>();
266 for (FeatureStore featureSet : varargToTypes(type))
268 result.addAll(featureSet.getContactFeatures());
277 public List<SequenceFeature> getNonPositionalFeatures(String... type)
279 List<SequenceFeature> result = new ArrayList<>();
281 for (FeatureStore featureSet : varargToTypes(type))
283 result.addAll(featureSet.getNonPositionalFeatures());
292 public boolean delete(SequenceFeature sf)
294 for (FeatureStore featureSet : featureStore.values())
296 if (featureSet.delete(sf))
308 public boolean hasFeatures()
310 for (FeatureStore featureSet : featureStore.values())
312 if (!featureSet.isEmpty())
324 public Set<String> getFeatureGroups(boolean positionalFeatures,
327 Set<String> groups = new HashSet<>();
329 for (FeatureStore featureSet : varargToTypes(type))
331 groups.addAll(featureSet.getFeatureGroups(positionalFeatures));
341 public Set<String> getFeatureTypesForGroups(boolean positionalFeatures,
344 Set<String> result = new HashSet<>();
346 for (Entry<String, FeatureStore> featureType : featureStore.entrySet())
348 Set<String> featureGroups = featureType.getValue().getFeatureGroups(
350 for (String group : groups)
352 if (featureGroups.contains(group))
355 * yes this feature type includes one of the query groups
357 result.add(featureType.getKey());
370 public Set<String> getFeatureTypes(String... soTerm)
372 Set<String> types = new HashSet<>();
373 for (Entry<String, FeatureStore> entry : featureStore.entrySet())
375 String type = entry.getKey();
376 if (!entry.getValue().isEmpty() && isOntologyTerm(type, soTerm))
385 * Answers true if the given type matches one of the specified terms (or is a
386 * sub-type of one in the Sequence Ontology), or if no terms are supplied.
387 * Answers false if filter terms are specified and the given term does not
394 protected boolean isOntologyTerm(String type, String... soTerm)
396 if (soTerm == null || soTerm.length == 0)
400 SequenceOntologyI so = SequenceOntologyFactory.getInstance();
401 for (String term : soTerm)
403 if (type.equals(term) || so.isA(type, term))
415 public float getMinimumScore(String type, boolean positional)
417 return featureStore.containsKey(type) ? featureStore.get(type)
418 .getMinimumScore(positional) : Float.NaN;
425 public float getMaximumScore(String type, boolean positional)
427 return featureStore.containsKey(type) ? featureStore.get(type)
428 .getMaximumScore(positional) : Float.NaN;
432 * A convenience method to sort features by start position ascending (if on
433 * forward strand), or end position descending (if on reverse strand)
436 * @param forwardStrand
438 public static void sortFeatures(List<SequenceFeature> features,
439 final boolean forwardStrand)
441 Collections.sort(features, forwardStrand ? FORWARD_STRAND
446 * {@inheritDoc} This method is 'semi-optimised': it only inspects features
447 * for types that include the specified group, but has to inspect every
448 * feature of those types for matching feature group. This is efficient unless
449 * a sequence has features that share the same type but are in different
450 * groups - an unlikely case.
452 * For example, if RESNUM feature is created with group = PDBID, then features
453 * would only be retrieved for those sequences associated with the target
457 public List<SequenceFeature> getFeaturesForGroup(boolean positional,
458 String group, String... type)
460 List<SequenceFeature> result = new ArrayList<>();
461 for (FeatureStore featureSet : varargToTypes(type))
463 if (featureSet.getFeatureGroups(positional).contains(group))
465 result.addAll(featureSet.getFeaturesForGroup(positional, group));
475 public boolean shiftFeatures(int fromPosition, int shiftBy)
477 boolean modified = false;
478 for (FeatureStore fs : featureStore.values())
480 modified |= fs.shiftFeatures(fromPosition, shiftBy);
489 public void deleteAll()
491 featureStore.clear();