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.Arrays;
29 import java.util.Collections;
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
50 * map from feature type to structured store of features for that type
51 * null types are permitted (but not a good idea!)
53 private Map<String, FeatureStore> featureStore;
58 public SequenceFeatures()
61 * use a TreeMap so that features are returned in alphabetical order of type
62 * ? wrap as a synchronized map for add and delete operations
64 // featureStore = Collections
65 // .synchronizedSortedMap(new TreeMap<String, FeatureStore>());
66 featureStore = new TreeMap<>();
70 * Constructor given a list of features
72 public SequenceFeatures(List<SequenceFeature> features)
77 for (SequenceFeature feature : features)
88 public boolean add(SequenceFeature sf)
90 String type = sf.getType();
93 System.err.println("Feature type may not be null: " + sf.toString());
97 if (featureStore.get(type) == null)
99 featureStore.put(type, new FeatureStore());
101 return featureStore.get(type).addFeature(sf);
108 public List<SequenceFeature> findFeatures(int from, int to,
111 List<SequenceFeature> result = new ArrayList<>();
112 for (FeatureStore featureSet : varargToTypes(type))
114 featureSet.findFeatures(from, to, result);
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 featureSet.getPositionalFeatures(result);
204 * A convenience method that converts a vararg for feature types to an
205 * Iterable over matched feature sets in key order
210 protected Iterable<FeatureStore> varargToTypes(String... type)
212 if (type == null || type.length == 0)
215 * no vararg parameter supplied - return all
217 return featureStore.values();
220 List<FeatureStore> types = new ArrayList<>();
221 List<String> args = Arrays.asList(type);
222 for (Entry<String, FeatureStore> featureType : featureStore.entrySet())
224 if (args.contains(featureType.getKey()))
226 types.add(featureType.getValue());
236 public List<SequenceFeature> getContactFeatures(String... type)
238 List<SequenceFeature> result = new ArrayList<>();
240 for (FeatureStore featureSet : varargToTypes(type))
242 featureSet.getContactFeatures(result);
251 public List<SequenceFeature> getNonPositionalFeatures(String... type)
253 List<SequenceFeature> result = new ArrayList<>();
255 for (FeatureStore featureSet : varargToTypes(type))
257 featureSet.getNonPositionalFeatures(result);
266 public boolean delete(SequenceFeature sf)
268 for (FeatureStore featureSet : featureStore.values())
270 if (featureSet.delete(sf))
282 public boolean hasFeatures()
284 for (FeatureStore featureSet : featureStore.values())
286 if (!featureSet.isEmpty())
298 public Set<String> getFeatureGroups(boolean positionalFeatures,
301 Set<String> groups = new HashSet<>();
303 for (FeatureStore featureSet : varargToTypes(type))
305 groups.addAll(featureSet.getFeatureGroups(positionalFeatures));
315 public Set<String> getFeatureTypesForGroups(boolean positionalFeatures,
318 Set<String> result = new HashSet<>();
320 for (Entry<String, FeatureStore> featureType : featureStore.entrySet())
322 Set<String> featureGroups = featureType.getValue()
323 .getFeatureGroups(positionalFeatures);
324 for (String group : groups)
326 if (featureGroups.contains(group))
329 * yes this feature type includes one of the query groups
331 result.add(featureType.getKey());
344 public Set<String> getFeatureTypes(String... soTerm)
346 Set<String> types = new HashSet<>();
347 for (Entry<String, FeatureStore> entry : featureStore.entrySet())
349 String type = entry.getKey();
350 if (!entry.getValue().isEmpty() && isOntologyTerm(type, soTerm))
359 * Answers true if the given type matches one of the specified terms (or is a
360 * sub-type of one in the Sequence Ontology), or if no terms are supplied.
361 * Answers false if filter terms are specified and the given term does not
368 protected boolean isOntologyTerm(String type, String... soTerm)
370 if (soTerm == null || soTerm.length == 0)
374 SequenceOntologyI so = SequenceOntologyFactory.getSequenceOntology();
375 for (String term : soTerm)
377 if (type.equals(term) || so.isA(type, term))
389 public float getMinimumScore(String type, boolean positional)
391 return featureStore.containsKey(type)
392 ? featureStore.get(type).getMinimumScore(positional)
400 public float getMaximumScore(String type, boolean positional)
402 return featureStore.containsKey(type)
403 ? featureStore.get(type).getMaximumScore(positional)
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 Collections.sort(features,
418 forwardStrand ? IntervalI.COMPARE_BEGIN_ASC
419 : IntervalI.COMPARE_END_DESC);
423 * {@inheritDoc} This method is 'semi-optimised': it only inspects features
424 * for types that include the specified group, but has to inspect every
425 * feature of those types for matching feature group. This is efficient unless
426 * a sequence has features that share the same type but are in different
427 * groups - an unlikely case.
429 * For example, if RESNUM feature is created with group = PDBID, then features
430 * would only be retrieved for those sequences associated with the target
434 public List<SequenceFeature> getFeaturesForGroup(boolean positional,
435 String group, String... type)
437 List<SequenceFeature> result = new ArrayList<>();
438 for (FeatureStore featureSet : varargToTypes(type))
440 if (featureSet.getFeatureGroups(positional).contains(group))
442 result.addAll(featureSet.getFeaturesForGroup(positional, group));
452 public boolean shiftFeatures(int fromPosition, int shiftBy)
454 boolean modified = false;
455 for (FeatureStore fs : featureStore.values())
457 modified |= fs.shiftFeatures(fromPosition, shiftBy);
466 public void deleteAll()
468 featureStore.clear();
472 public List<SequenceFeature> findFeatures(int pos, String type,
473 List<SequenceFeature> list)
475 FeatureStore fs = featureStore.get(type);
478 return list == null ? new ArrayList<>() : list;
480 return fs.findFeatures(pos, pos, list);
484 public boolean hasFeatures(String type)
486 return featureStore.containsKey(type)
487 && !featureStore.get(type).isEmpty();