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;
30 import intervalstore.impl.IntervalStore;
33 * A data store for a set of sequence features that supports efficient lookup of
34 * features overlapping a given range. Intended for (but not limited to) storage
35 * of features for one sequence and feature type.
40 public class FeatureStoreImpl extends FeatureStore
43 public FeatureStoreImpl()
45 features = new IntervalStore<>();
49 * Add a contact feature to the lists that hold them ordered by start (first
50 * contact) and by end (second contact) position, ensuring the lists remain
51 * ordered, and returns true. This method allows duplicate features to be
52 * added, so test before calling to avoid this.
58 protected synchronized boolean addContactFeature(SequenceFeature feature)
60 if (contactFeatureStarts == null)
62 contactFeatureStarts = new ArrayList<>();
63 contactFeatureEnds = new ArrayList<>();
67 * insert into list sorted by start (first contact position):
68 * binary search the sorted list to find the insertion point
70 int insertPosition = findFirstBeginStatic(contactFeatureStarts,
72 contactFeatureStarts.add(insertPosition, feature);
75 * insert into list sorted by end (second contact position):
76 * binary search the sorted list to find the insertion point
78 insertPosition = findFirstEndStatic(contactFeatureEnds,
80 contactFeatureEnds.add(insertPosition, feature);
86 * Adds one feature to the IntervalStore that can manage nested features
87 * (creating the IntervalStore if necessary)
90 protected synchronized void addNestedFeature(SequenceFeature feature)
92 features.add(feature);
96 * Adds contact features to the result list where either the second or the
97 * first contact position lies within the target range
104 protected void findContactFeatures(long from, long to,
105 List<SequenceFeature> result)
107 if (contactFeatureStarts != null)
109 findContactStartOverlaps(from, to, result);
110 findContactEndOverlaps(from, to, result);
115 protected boolean containsFeature(SequenceFeature feature)
117 return features.contains(feature);
121 * Adds to the result list any contact features whose end (second contact
122 * point), but not start (first contact point), lies in the query from-to
130 private void findContactEndOverlaps(long from, long to,
131 List<SequenceFeature> result)
134 * find the first contact feature (if any)
135 * whose end point is not before the target range
137 int index = findFirstEndStatic(contactFeatureEnds, from);
139 int n = contactFeatureEnds.size();
142 SequenceFeature sf = contactFeatureEnds.get(index);
143 if (!sf.isContactFeature())
145 System.err.println("Error! non-contact feature type " + sf.getType()
146 + " in contact features list");
151 int begin = sf.getBegin();
152 if (begin >= from && begin <= to)
155 * this feature's first contact position lies in the search range
156 * so we don't include it in results a second time
162 if (sf.getEnd() > to)
165 * this feature (and all following) has end point after the target range
171 * feature has end >= from and end <= to
172 * i.e. contact end point lies within overlap search range
180 * Adds contact features whose start position lies in the from-to range to the
188 private void findContactStartOverlaps(long from, long to,
189 List<SequenceFeature> result)
191 int index = findFirstBegin(contactFeatureStarts, from);
193 while (index < contactFeatureStarts.size())
195 SequenceFeature sf = contactFeatureStarts.get(index);
196 if (!sf.isContactFeature())
198 System.err.println("Error! non-contact feature " + sf.toString()
199 + " in contact features list");
203 if (sf.getBegin() > to)
206 * this feature's start (and all following) follows the target range
212 * feature has begin >= from and begin <= to
213 * i.e. contact start point lies within overlap search range
221 * Returns a (possibly empty) list of features whose extent overlaps the given
222 * range. The returned list is not ordered. Contact features are included if
223 * either of the contact points lies within the range.
226 * start position of overlap range (inclusive)
228 * end position of overlap range (inclusive)
235 public List<SequenceFeature> findOverlappingFeatures(long start, long end,
236 List<SequenceFeature> result)
238 result = new ArrayList<>();
239 findContactFeatures(start, end, result);
240 findOverlaps(start, end, result);
244 private void findOverlaps(long start, long end,
245 List<SequenceFeature> result)
247 result.addAll(((IntervalStoreI<SequenceFeature>) features)
248 .findOverlaps(start, end));
252 protected int findFirstBegin(List<SequenceFeature> list, long pos)
254 return findFirstBeginStatic(list, pos);
258 * Possibly a bit faster using a static method.
264 private static int findFirstBeginStatic(List<SequenceFeature> list,
267 return BinarySearcher.findFirst(list, f -> f.getBegin() >= pos);
271 protected int findFirstEnd(List<SequenceFeature> list, long pos)
273 return findFirstEndStatic(list, pos);
277 * Possibly a bit faster using a static method.
283 private static int findFirstEndStatic(List<SequenceFeature> list,
286 return BinarySearcher.findFirst(list, f -> f.getEnd() >= pos);