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;
26 import jalview.util.Platform;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Collections;
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 * map from feature type to structured store of features for that type
52 * null types are permitted (but not a good idea!)
54 private Map<String, FeatureStore> featureStore;
59 public SequenceFeatures()
62 * use a TreeMap so that features are returned in alphabetical order of type
63 * ? wrap as a synchronized map for add and delete operations
65 // featureStore = Collections
66 // .synchronizedSortedMap(new TreeMap<String, FeatureStore>());
67 featureStore = new TreeMap<>();
71 * Constructor given a list of features
73 public SequenceFeatures(List<SequenceFeature> features)
78 for (SequenceFeature feature : features)
89 public boolean add(SequenceFeature sf)
91 String type = sf.getType();
94 System.err.println("Feature type may not be null: " + sf.toString());
98 if (featureStore.get(type) == null)
100 featureStore.put(type, new FeatureStore());
102 return featureStore.get(type).addFeature(sf);
109 public List<SequenceFeature> findFeatures(int from, int to,
112 List<SequenceFeature> result = new ArrayList<>();
113 for (FeatureStore featureSet : varargToTypes(type))
115 featureSet.findFeatures(from, to, result);
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(
156 featureTypes.toArray(new String[featureTypes.size()]));
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 featureSet.getPositionalFeatures(result);
205 * A convenience method that converts a vararg for feature types to an
206 * Iterable over matched feature sets in key order
211 protected Iterable<FeatureStore> varargToTypes(String... type)
213 if (type == null || type.length == 0)
216 * no vararg parameter supplied - return all
218 return featureStore.values();
221 List<FeatureStore> types = new ArrayList<>();
222 List<String> args = Arrays.asList(type);
223 for (Entry<String, FeatureStore> featureType : featureStore.entrySet())
225 if (args.contains(featureType.getKey()))
227 types.add(featureType.getValue());
237 public List<SequenceFeature> getContactFeatures(String... type)
239 List<SequenceFeature> result = new ArrayList<>();
241 for (FeatureStore featureSet : varargToTypes(type))
243 featureSet.getContactFeatures(result);
252 public List<SequenceFeature> getNonPositionalFeatures(String... type)
254 List<SequenceFeature> result = new ArrayList<>();
256 for (FeatureStore featureSet : varargToTypes(type))
258 featureSet.getNonPositionalFeatures(result);
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 // BH 2020.03.21 This is the set that orders the list of groups
303 // at the top of the FeatureSettings panel.
304 Set<String> groups = Platform.getJavaOrderedHashSet();
306 for (FeatureStore featureSet : varargToTypes(type))
308 groups.addAll(featureSet.getFeatureGroups(positionalFeatures));
318 public Set<String> getFeatureTypesForGroups(boolean positionalFeatures,
321 // BH 2020.03.21 This set is the one that sets the initial ordering for
322 // feature rendering. We set it to new HashSet<>(16,0.75) to force it to
323 // be backed by a Java hash-ordered HashMap instead of a JavaScript Map.
324 Set<String> result = Platform.getJavaOrderedHashSet();
326 for (Entry<String, FeatureStore> featureType : featureStore.entrySet())
328 Set<String> featureGroups = featureType.getValue()
329 .getFeatureGroups(positionalFeatures);
330 for (String group : groups)
332 if (featureGroups.contains(group))
335 * yes this feature type includes one of the query groups
337 result.add(featureType.getKey());
350 public Set<String> getFeatureTypes(String... soTerm)
352 Set<String> types = new HashSet<>(15, 0.75f);
353 for (Entry<String, FeatureStore> entry : featureStore.entrySet())
355 String type = entry.getKey();
356 if (!entry.getValue().isEmpty() && isOntologyTerm(type, soTerm))
365 * Answers true if the given type matches one of the specified terms (or is a
366 * sub-type of one in the Sequence Ontology), or if no terms are supplied.
367 * Answers false if filter terms are specified and the given term does not
374 protected boolean isOntologyTerm(String type, String... soTerm)
376 if (soTerm == null || soTerm.length == 0)
380 SequenceOntologyI so = SequenceOntologyFactory.getSequenceOntology();
381 for (String term : soTerm)
383 if (type.equals(term) || so.isA(type, term))
395 public float getMinimumScore(String type, boolean positional)
397 return featureStore.containsKey(type)
398 ? featureStore.get(type).getMinimumScore(positional)
406 public float getMaximumScore(String type, boolean positional)
408 return featureStore.containsKey(type)
409 ? featureStore.get(type).getMaximumScore(positional)
414 * A convenience method to sort features by start position ascending (if on
415 * forward strand), or end position descending (if on reverse strand)
418 * @param forwardStrand
420 public static void sortFeatures(List<? extends IntervalI> features,
421 final boolean forwardStrand)
423 Collections.sort(features,
424 forwardStrand ? IntervalI.COMPARE_BEGIN_ASC
425 : IntervalI.COMPARE_END_DESC);
429 * {@inheritDoc} This method is 'semi-optimised': it only inspects features
430 * for types that include the specified group, but has to inspect every
431 * feature of those types for matching feature group. This is efficient unless
432 * a sequence has features that share the same type but are in different
433 * groups - an unlikely case.
435 * For example, if RESNUM feature is created with group = PDBID, then features
436 * would only be retrieved for those sequences associated with the target
440 public List<SequenceFeature> getFeaturesForGroup(boolean positional,
441 String group, String... type)
443 List<SequenceFeature> result = new ArrayList<>();
444 for (FeatureStore featureSet : varargToTypes(type))
446 if (featureSet.getFeatureGroups(positional).contains(group))
448 result.addAll(featureSet.getFeaturesForGroup(positional, group));
458 public boolean shiftFeatures(int fromPosition, int shiftBy)
460 boolean modified = false;
461 for (FeatureStore fs : featureStore.values())
463 modified |= fs.shiftFeatures(fromPosition, shiftBy);
472 public void deleteAll()
474 featureStore.clear();
478 public List<SequenceFeature> findFeatures(int pos, String type,
479 List<SequenceFeature> list)
481 FeatureStore fs = featureStore.get(type);
484 return list == null ? new ArrayList<>() : list;
486 return fs.findFeatures(pos, pos, list);
490 public boolean hasFeatures(String type)
492 return featureStore.containsKey(type)
493 && !featureStore.get(type).isEmpty();