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;
25 import java.util.ArrayList;
26 import java.util.List;
28 import intervalstore.api.IntervalStoreI;
29 import intervalstore.impl.BinarySearcher;
32 * A data store for a set of sequence features that supports efficient lookup of
33 * features overlapping a given range. Intended for (but not limited to) storage
34 * of features for one sequence and feature type.
39 public class FeatureStoreImpl extends FeatureStore
43 * Default constructor uses NCList
45 public FeatureStoreImpl()
50 public FeatureStoreImpl(boolean useNCList)
52 features = (useNCList ? new intervalstore.impl.IntervalStore<>()
53 : new intervalstore.nonc.IntervalStore<>(false));
57 * Add a contact feature to the lists that hold them ordered by start (first
58 * contact) and by end (second contact) position, ensuring the lists remain
59 * ordered, and returns true. This method allows duplicate features to be
60 * added, so test before calling to avoid this.
66 protected synchronized boolean addContactFeature(SequenceFeature feature)
68 if (contactFeatureStarts == null)
70 contactFeatureStarts = new ArrayList<>();
71 contactFeatureEnds = new ArrayList<>();
75 * insert into list sorted by start (first contact position):
76 * binary search the sorted list to find the insertion point
78 int insertPosition = findFirstBeginStatic(contactFeatureStarts,
80 contactFeatureStarts.add(insertPosition, feature);
83 * insert into list sorted by end (second contact position):
84 * binary search the sorted list to find the insertion point
86 insertPosition = findFirstEndStatic(contactFeatureEnds,
88 contactFeatureEnds.add(insertPosition, feature);
94 * Adds one feature to the IntervalStore that can manage nested features
95 * (creating the IntervalStore if necessary)
98 protected synchronized void addPositionalFeature(SequenceFeature feature)
100 features.add(feature);
104 * Adds contact features to the result list where either the second or the
105 * first contact position lies within the target range
112 protected void findContactFeatures(long from, long to,
113 List<SequenceFeature> result)
115 if (contactFeatureStarts != null)
117 findContactStartOverlaps(from, to, result);
118 findContactEndOverlaps(from, to, result);
123 protected boolean containsFeature(SequenceFeature feature)
125 return features.contains(feature);
129 * Adds to the result list any contact features whose end (second contact
130 * point), but not start (first contact point), lies in the query from-to
138 private void findContactEndOverlaps(long from, long to,
139 List<SequenceFeature> result)
142 * find the first contact feature (if any)
143 * whose end point is not before the target range
145 int index = findFirstEndStatic(contactFeatureEnds, from);
147 int n = contactFeatureEnds.size();
150 SequenceFeature sf = contactFeatureEnds.get(index);
151 if (!sf.isContactFeature())
153 System.err.println("Error! non-contact feature type " + sf.getType()
154 + " in contact features list");
159 int begin = sf.getBegin();
160 if (begin >= from && begin <= to)
163 * this feature's first contact position lies in the search range
164 * so we don't include it in results a second time
170 if (sf.getEnd() > to)
173 * this feature (and all following) has end point after the target range
179 * feature has end >= from and end <= to
180 * i.e. contact end point lies within overlap search range
188 * Adds contact features whose start position lies in the from-to range to the
196 private void findContactStartOverlaps(long from, long to,
197 List<SequenceFeature> result)
199 int index = findFirstBegin(contactFeatureStarts, from);
201 while (index < contactFeatureStarts.size())
203 SequenceFeature sf = contactFeatureStarts.get(index);
204 if (!sf.isContactFeature())
206 System.err.println("Error! non-contact feature " + sf.toString()
207 + " in contact features list");
211 if (sf.getBegin() > to)
214 * this feature's start (and all following) follows the target range
220 * feature has begin >= from and begin <= to
221 * i.e. contact start point lies within overlap search range
229 * Returns a (possibly empty) list of features whose extent overlaps the given
230 * range. The returned list is not ordered. Contact features are included if
231 * either of the contact points lies within the range.
234 * start position of overlap range (inclusive)
236 * end position of overlap range (inclusive)
243 public List<SequenceFeature> findOverlappingFeatures(long start, long end,
244 List<SequenceFeature> result)
246 result = new ArrayList<>();
247 findContactFeatures(start, end, result);
248 findOverlaps(start, end, result);
252 private void findOverlaps(long start, long end,
253 List<SequenceFeature> result)
255 result.addAll(((IntervalStoreI<SequenceFeature>) features)
256 .findOverlaps(start, end));
260 protected int findFirstBegin(List<SequenceFeature> list, long pos)
262 return findFirstBeginStatic(list, pos);
266 * Possibly a bit faster using a static method.
272 private static int findFirstBeginStatic(List<SequenceFeature> list,
275 return BinarySearcher.findFirst(list, f -> f.getBegin() >= pos);
279 protected int findFirstEnd(List<SequenceFeature> list, long pos)
281 return findFirstEndStatic(list, pos);
285 * Possibly a bit faster using a static method.
291 private static int findFirstEndStatic(List<SequenceFeature> list,
294 return BinarySearcher.findFirst(list, f -> f.getEnd() >= pos);
298 protected boolean findAndRemoveNonContactFeature(SequenceFeature sf)
300 return features.remove(sf);