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.impl.BinarySearcher;
31 * A data store for a set of sequence features that supports efficient lookup of
32 * features overlapping a given range. Intended for (but not limited to) storage
33 * of features for one sequence and feature type.
38 public class FeatureStoreImpl extends FeatureStore
41 public FeatureStoreImpl()
46 public FeatureStoreImpl(int option)
52 * Add a contact feature to the lists that hold them ordered by start (first
53 * contact) and by end (second contact) position, ensuring the lists remain
54 * ordered, and returns true. This method allows duplicate features to be
55 * added, so test before calling to avoid this.
61 protected synchronized boolean addContactFeature(SequenceFeature feature)
63 if (contactFeatureStarts == null)
65 contactFeatureStarts = new ArrayList<>();
66 contactFeatureEnds = new ArrayList<>();
70 * insert into list sorted by start (first contact position):
71 * binary search the sorted list to find the insertion point
73 int insertPosition = findFirstBegin(contactFeatureStarts,
75 contactFeatureStarts.add(insertPosition, feature);
78 * insert into list sorted by end (second contact position):
79 * binary search the sorted list to find the insertion point
81 insertPosition = findFirstEnd(contactFeatureEnds,
83 contactFeatureEnds.add(insertPosition, feature);
89 * Adds one feature to the IntervalStore that can manage nested features
90 * (creating the IntervalStore if necessary)
93 protected synchronized boolean addPositionalFeature(
94 SequenceFeature feature)
96 return features.add(feature);
100 * Adds contact features to the result list where either the second or the
101 * first contact position lies within the target range
108 protected void findContactFeatures(long from, long to,
109 List<SequenceFeature> result)
111 if (contactFeatureStarts != null)
113 findContactStartOverlaps(from, to, result);
114 findContactEndOverlaps(from, to, result);
119 protected boolean containsFeature(SequenceFeature feature)
121 return features.contains(feature);
125 * Adds to the result list any contact features whose end (second contact
126 * point), but not start (first contact point), lies in the query from-to
134 private void findContactEndOverlaps(long from, long to,
135 List<SequenceFeature> result)
138 * find the first contact feature (if any)
139 * whose end point is not before the target range
141 int index = findFirstEnd(contactFeatureEnds, from);
143 int n = contactFeatureEnds.size();
146 SequenceFeature sf = contactFeatureEnds.get(index);
147 if (!sf.isContactFeature())
149 System.err.println("Error! non-contact feature type " + sf.getType()
150 + " in contact features list");
155 int begin = sf.getBegin();
156 if (begin >= from && begin <= to)
159 * this feature's first contact position lies in the search range
160 * so we don't include it in results a second time
166 if (sf.getEnd() > to)
169 * this feature (and all following) has end point after the target range
175 * feature has end >= from and end <= to
176 * i.e. contact end point lies within overlap search range
184 * Adds contact features whose start position lies in the from-to range to the
192 private void findContactStartOverlaps(long from, long to,
193 List<SequenceFeature> result)
195 int index = findFirstBegin(contactFeatureStarts, from);
197 while (index < contactFeatureStarts.size())
199 SequenceFeature sf = contactFeatureStarts.get(index);
200 if (!sf.isContactFeature())
202 System.err.println("Error! non-contact feature " + sf.toString()
203 + " in contact features list");
207 if (sf.getBegin() > to)
210 * this feature's start (and all following) follows the target range
216 * feature has begin >= from and begin <= to
217 * i.e. contact start point lies within overlap search range
225 * Returns a (possibly empty) list of features whose extent overlaps the given
226 * range. The returned list is not ordered. Contact features are included if
227 * either of the contact points lies within the range.
230 * start position of overlap range (inclusive)
232 * end position of overlap range (inclusive)
239 public List<SequenceFeature> findOverlappingFeatures(long start, long end,
240 List<SequenceFeature> result)
242 result = new ArrayList<>();
243 findContactFeatures(start, end, result);
244 findOverlaps(start, end, result);
248 private void findOverlaps(long start, long end,
249 List<SequenceFeature> result)
251 result.addAll(features
252 .findOverlaps(start, end));
256 protected int findFirstBegin(List<SequenceFeature> list, long pos)
258 return BinarySearcher.findFirst(list, (int) pos,
259 BinarySearcher.fbegin);
263 protected int findFirstEnd(List<SequenceFeature> list, long pos)
265 return BinarySearcher.findFirst(list, (int) pos, BinarySearcher.fend);
269 protected boolean findAndRemoveNonContactFeature(SequenceFeature sf)
271 return features.remove(sf);