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.HashSet;
31 import java.util.List;
33 import java.util.Map.Entry;
35 import java.util.TreeMap;
37 import intervalstore.api.IntervalI;
40 * A class that stores sequence features in a way that supports efficient
41 * querying by type and location (overlap). Intended for (but not limited to)
42 * storage of features for one sequence.
47 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, FeatureStoreI> featureStore;
56 private static boolean useIntervalStore = !Platform.isJS();
61 public SequenceFeatures()
64 * use a TreeMap so that features are returned in alphabetical order of type
65 * ? wrap as a synchronized map for add and delete operations
67 // featureStore = Collections
68 // .synchronizedSortedMap(new TreeMap<String, FeatureStoreI>());
69 featureStore = new TreeMap<>();
73 * Constructor given a list of features
75 public SequenceFeatures(List<SequenceFeature> features)
80 for (SequenceFeature feature : features)
91 public boolean add(SequenceFeature sf)
93 String type = sf.getType();
96 System.err.println("Feature type may not be null: " + sf.toString());
100 if (featureStore.get(type) == null)
102 featureStore.put(type, newFeatureStore());
104 return featureStore.get(type).addFeature(sf);
107 private FeatureStoreI newFeatureStore()
109 // TODO Auto-generated method stub
110 return (useIntervalStore ? new FeatureStoreImpl() : new FeatureStoreJS());
117 public List<SequenceFeature> findFeatures(int from, int to,
120 List<SequenceFeature> result = new ArrayList<>();
121 for (FeatureStoreI featureSet : varargToTypes(type))
123 result.addAll(featureSet.findOverlappingFeatures(from, to, null));
132 public List<SequenceFeature> getAllFeatures(String... type)
134 List<SequenceFeature> result = new ArrayList<>();
136 result.addAll(getPositionalFeatures(type));
138 result.addAll(getNonPositionalFeatures());
147 public List<SequenceFeature> getFeaturesByOntology(String... ontologyTerm)
149 if (ontologyTerm == null || ontologyTerm.length == 0)
151 return new ArrayList<>();
154 Set<String> featureTypes = getFeatureTypes(ontologyTerm);
155 if (featureTypes.isEmpty())
158 * no features of the specified type or any sub-type
160 return new ArrayList<>();
163 return getAllFeatures(featureTypes.toArray(new String[featureTypes
171 public int getFeatureCount(boolean positional, String... type)
175 for (FeatureStoreI featureSet : varargToTypes(type))
177 result += featureSet.getFeatureCount(positional);
186 public int getTotalFeatureLength(String... type)
190 for (FeatureStoreI featureSet : varargToTypes(type))
192 result += featureSet.getTotalFeatureLength();
201 public List<SequenceFeature> getPositionalFeatures(String... type)
203 List<SequenceFeature> result = new ArrayList<>();
205 for (FeatureStoreI featureSet : varargToTypes(type))
207 featureSet.getPositionalFeatures(result);
213 * A convenience method that converts a vararg for feature types to an
214 * Iterable over matched feature sets in key order
219 protected Iterable<FeatureStoreI> varargToTypes(String... type)
221 if (type == null || type.length == 0)
224 * no vararg parameter supplied - return all
226 return featureStore.values();
230 List<FeatureStoreI> types = new ArrayList<>();
231 List<String> args = Arrays.asList(type);
232 for (Entry<String, FeatureStoreI> featureType : featureStore.entrySet())
234 if (args.contains(featureType.getKey()))
236 types.add(featureType.getValue());
246 public List<SequenceFeature> getContactFeatures(String... type)
248 List<SequenceFeature> result = new ArrayList<>();
250 for (FeatureStoreI featureSet : varargToTypes(type))
252 featureSet.getContactFeatures(result);
261 public List<SequenceFeature> getNonPositionalFeatures(String... type)
263 List<SequenceFeature> result = new ArrayList<>();
265 for (FeatureStoreI featureSet : varargToTypes(type))
267 featureSet.getNonPositionalFeatures(result);
276 public boolean delete(SequenceFeature sf)
278 for (FeatureStoreI featureSet : featureStore.values())
280 if (featureSet.delete(sf))
292 public boolean hasFeatures()
294 for (FeatureStoreI featureSet : featureStore.values())
296 if (!featureSet.isEmpty())
308 public Set<String> getFeatureGroups(boolean positionalFeatures,
311 Set<String> groups = new HashSet<>();
313 for (FeatureStoreI featureSet : varargToTypes(type))
315 groups.addAll(featureSet.getFeatureGroups(positionalFeatures));
325 public Set<String> getFeatureTypesForGroups(boolean positionalFeatures,
328 Set<String> result = new HashSet<>();
330 for (Entry<String, FeatureStoreI> featureType : featureStore.entrySet())
332 Set<String> featureGroups = featureType.getValue().getFeatureGroups(
334 for (String group : groups)
336 if (featureGroups.contains(group))
339 * yes this feature type includes one of the query groups
341 result.add(featureType.getKey());
354 public Set<String> getFeatureTypes(String... soTerm)
356 Set<String> types = new HashSet<>();
357 for (Entry<String, FeatureStoreI> entry : featureStore.entrySet())
359 String type = entry.getKey();
360 if (!entry.getValue().isEmpty() && isOntologyTerm(type, soTerm))
369 * Answers true if the given type matches one of the specified terms (or is a
370 * sub-type of one in the Sequence Ontology), or if no terms are supplied.
371 * Answers false if filter terms are specified and the given term does not
378 protected boolean isOntologyTerm(String type, String... soTerm)
380 if (soTerm == null || soTerm.length == 0)
384 SequenceOntologyI so = SequenceOntologyFactory.getSequenceOntology();
385 for (String term : soTerm)
387 if (type.equals(term) || so.isA(type, term))
399 public float getMinimumScore(String type, boolean positional)
401 return featureStore.containsKey(type) ? featureStore.get(type)
402 .getMinimumScore(positional) : Float.NaN;
409 public float getMaximumScore(String type, boolean positional)
411 return featureStore.containsKey(type) ? featureStore.get(type)
412 .getMaximumScore(positional) : Float.NaN;
416 * A convenience method to sort features by start position ascending (if on
417 * forward strand), or end position descending (if on reverse strand)
420 * @param forwardStrand
422 public static void sortFeatures(List<? extends IntervalI> features,
423 final boolean forwardStrand)
425 IntervalI.sortIntervals(features, forwardStrand);
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 (FeatureStoreI 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 (FeatureStoreI fs : featureStore.values())
463 modified |= fs.shiftFeatures(fromPosition, shiftBy);
472 public void deleteAll()
474 featureStore.clear();
478 * Simplified find for features associated with a given position.
480 * JavaScript set to not use IntervalI, but easily testable by setting false
483 * FeatureRenderer has checked already that featureStore does contain type.
485 * @author Bob Hanson 2019.07.30
488 public List<SequenceFeature> findFeatures(int pos, String type,
489 List<SequenceFeature> list)
491 FeatureStoreI fs = featureStore.get(type);
492 return fs.findOverlappingFeatures(pos, pos, list);
495 // Chrome; developer console closed
497 // BH 2019.08.01 useIntervalStore true, redraw false:
498 // Platform: timer mark 13.848 0.367 overviewrender 16000 pixels row:14
499 // Platform: timer mark 15.391 0.39 overviewrender 16000 pixels row:14
500 // Platform: timer mark 16.498 0.39 overviewrender 16000 pixels row:14
501 // Platform: timer mark 17.596 0.401 overviewrender 16000 pixels row:14
502 // Platform: timer mark 18.738 0.363 overviewrender 16000 pixels row:14
503 // Platform: timer mark 19.659 0.358 overviewrender 16000 pixels row:14
504 // Platform: timer mark 20.737 0.359 overviewrender 16000 pixels row:14
505 // Platform: timer mark 21.797 0.391 overviewrender 16000 pixels row:14
506 // Platform: timer mark 22.851 0.361 overviewrender 16000 pixels row:14
507 // Platform: timer mark 24.019 0.395 overviewrender 16000 pixels row:14
509 // BH 2019.08.01 useIntervalStore false, redraw false:
510 // Platform: timer mark 19.011 0.181 overviewrender 16000 pixels row:14
511 // Platform: timer mark 20.311 0.183 overviewrender 16000 pixels row:14
512 // Platform: timer mark 21.368 0.175 overviewrender 16000 pixels row:14
513 // Platform: timer mark 22.347 0.178 overviewrender 16000 pixels row:14
514 // Platform: timer mark 23.605 0.216 overviewrender 16000 pixels row:14
515 // Platform: timer mark 24.836 0.191 overviewrender 16000 pixels row:14
516 // Platform: timer mark 26.016 0.181 overviewrender 16000 pixels row:14
517 // Platform: timer mark 27.278 0.178 overviewrender 16000 pixels row:14
518 // Platform: timer mark 28.158 0.181 overviewrender 16000 pixels row:14
519 // Platform: timer mark 29.227 0.196 overviewrender 16000 pixels row:14
520 // Platform: timer mark 30.1 0.171 overviewrender 16000 pixels row:14
521 // Platform: timer mark 31.684 0.196 overviewrender 16000 pixels row:14
522 // Platform: timer mark 32.779 0.18 overviewrender 16000 pixels row:14
523 // Platform: timer mark 52.355 0.185 overviewrender 16000 pixels row:14
524 // Platform: timer mark 53.829 0.186 overviewrender 16000 pixels row:14
529 * @author Bob Hanson 2019.08.01
532 public boolean hasFeatures(String type)
534 return featureStore.containsKey(type);