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;
29 * An adaption of FeatureStore that is efficient and lightweight, accelerating
30 * processing speed in JavaScript.
32 * It could be used in Java as well, with significant acceleration, but all this
33 * is so fast anyway that it probably will not be noticed in Java to speed it up
34 * by a factor of two or three. So for now, at least, this implementation is
35 * just in JavaScript. The flag for this is in SequenceFeatures.
37 * This implementation uses the IntervalStore developed by Bob Hanson, found at
38 * https://github.com/BobHanson/IntervalStoreJ, forked from the one developed by
39 * Mungo Carstairs at https://github.com/bartongroup/IntervalStoreJ.
41 * See the discussion folder at https://github.com/BobHanson/IntervalStoreJ for
45 * @author Bob Hanson 2019.08.03-2019.08.16
48 public class FeatureStoreJS extends FeatureStore
52 public FeatureStoreJS()
57 public FeatureStoreJS(int option)
63 * Add a contact feature to the lists that hold them ordered by start (first
64 * contact) and by end (second contact) position, ensuring the lists remain
65 * ordered. This method allows duplicate features to be added, so test before
66 * calling to avoid this.
72 protected synchronized boolean addContactFeature(SequenceFeature feature)
74 if (contactFeatureStarts == null)
76 contactFeatureStarts = new ArrayList<>();
77 contactFeatureEnds = new ArrayList<>();
79 contactFeatureStarts.add(
80 findFirstBegin(contactFeatureStarts, feature.begin), feature);
81 contactFeatureEnds.add(findFirstEnd(contactFeatureEnds, feature.end),
87 * Add a feature to the IntervalStore, not allowing for duplicates.
90 * @return false if could not add it (late check for duplicate)
93 protected synchronized boolean addPositionalFeature(
94 SequenceFeature feature)
96 return features.add(feature, false);
100 * Initial check in FeatureStore.add(feature) that in other implementations
101 * does a containment check, but in this implementation just returns false to
102 * indicate that we should continue. This implementation will do this check as
103 * part of the add() method for greater efficiency (one binary search instead
106 * @return false -- meaning "maybe not contained; continue adding"
109 protected boolean checkContainsPositionalFeatureForAdd(
110 SequenceFeature feature)
116 * Check to see if a feature (or its equivalent based on
117 * IntervalI.equalsInterval) is already in this store. This method should be
118 * avoided except when necessary, as it involves a binary search with identity
121 * @return true if this feature or its equivalent (based on equalsInterval) is
122 * present already in the collection.
125 protected boolean containsFeature(SequenceFeature feature)
127 return features.contains(feature);
131 protected boolean findAndRemoveNonContactFeature(SequenceFeature sf)
133 return features.remove(sf);
137 * Add contact features to the result list where either the second or the
138 * first contact position lies within the target range, inclusively.
145 protected void findContactFeatures(long from, long to,
146 List<SequenceFeature> result)
148 getContactStartOverlaps(from, to, result);
149 getContactEndOverlaps(from, to, result);
153 * Locate the first feature start in a standard ArrayList that is at or after
159 protected int findFirstBegin(List<SequenceFeature> list, long pos)
161 int matched = list.size();
162 int end = matched - 1;
163 int start = (end < 0 || list.get(end).begin < pos ? matched : 0);
166 int mid = (start + end) / 2;
167 if (list.get(mid).begin >= pos)
181 * Locate the feature end in a standard ArrayList that is after or at this
187 protected int findFirstEnd(List<SequenceFeature> list, long pos)
189 int matched = list.size();
190 int end = matched - 1;
194 int mid = (start + end) / 2;
195 if (list.get(mid).end >= pos)
209 * Returns a (possibly empty) list of features whose extent overlaps the given
210 * range. The returned list is ordered as follows:
212 * (1) ContactFeature starts
214 * (2) ContactFeature ends (that are not also starts)
216 * (3) noncontact SequenceFeatures, in reverse start order
218 * (This last reverse order is for efficiency in processing only.)
223 * start position of overlap range (inclusive)
225 * end position of overlap range (inclusive)
228 * optional result list; for highest efficiency, provide this
230 * @return result same as result parameter, or a new ArrayList if that is null
234 public List<SequenceFeature> findOverlappingFeatures(long start, long end,
235 List<SequenceFeature> result)
239 result = new ArrayList<>();
241 if (contactFeatureStarts != null)
245 getContactPointStarts(contactFeatureStarts, start, result);
246 getContactPointEnds(contactFeatureEnds, end, result);
250 findContactFeatures(start, end, result);
253 if (features.size() > 0)
255 features.findOverlaps(start, end, result);
261 * Adds to the result list any contact features having end (second contact
262 * point), but not start (first contact point), in the query from-to range
269 private void getContactEndOverlaps(long from, long to,
270 List<SequenceFeature> result)
272 // find the first contact feature (if any)
273 // with end point not before the target range
275 for (int i = findFirstEnd(contactFeatureEnds,
276 from), n = contactFeatureEnds.size(); i < n; i++)
278 SequenceFeature sf = contactFeatureEnds.get(i);
279 if (sf.begin >= from && sf.begin <= to)
281 // this feature's first contact position lies in the search range
282 // so we don't include it in results a second time
288 // this feature (and all following) has end point after the target range
292 // feature has end >= from and end <= to
293 // i.e. contact end point lies within overlap search range
299 * Binary search for contact start or end matching a specific position. This
300 * efficient search was designed specifically for rapid return for the
301 * OverviewPanel. It's implementation sped processing of that panel by 2300%.
308 * @author Bob Hanson 2019.07.30
310 private void getContactPointStarts(List<SequenceFeature> l, long pos,
311 List<SequenceFeature> result)
314 int high = l.size() - 1;
317 int mid = (low + high) >>> 1;
318 SequenceFeature f = l.get(mid);
319 switch (Long.signum(f.begin - pos))
330 // could be "5" in 12345556788 ?
331 while (++mid <= high && (f = l.get(mid)) != null && f.begin == pos)
335 while (--m >= low && (f = l.get(m)) != null && f.begin == pos)
344 private void getContactPointEnds(List<SequenceFeature> l, long pos,
345 List<SequenceFeature> result)
348 int high = l.size() - 1;
351 int mid = (low + high) >>> 1;
352 SequenceFeature f = l.get(mid);
353 switch (Long.signum(f.end - pos))
363 if (f.begin != f.end)
367 // could be "5" in 12345556788 ?
368 while (++mid <= high && (f = l.get(mid)) != null
371 if (f.begin != f.end)
376 while (--m >= low && (f = l.get(m)) != null
379 if (f.begin != f.end)
390 * Adds contact features whose start position lies in the from-to range to the
398 private void getContactStartOverlaps(long from, long to,
399 List<SequenceFeature> result)
401 for (int i = findFirstBegin(contactFeatureStarts,
402 from), n = contactFeatureStarts.size(); i < n; i++)
404 SequenceFeature sf = contactFeatureStarts.get(i);