1 package jalview.datamodel.features;
3 import jalview.datamodel.SequenceFeature;
5 import java.util.ArrayList;
6 import java.util.Collections;
7 import java.util.Comparator;
11 * A data store for a set of sequence features that supports efficient lookup of
12 * features overlapping a given range. Intended for (but not limited to) storage
13 * of features for one sequence and feature type.
18 public class FeatureStore
20 Comparator<ContiguousI> startOrdering = new RangeComparator(true);
22 Comparator<ContiguousI> endOrdering = new RangeComparator(false);
25 * Non-positional features have no (zero) start/end position.
27 List<SequenceFeature> nonPositionalFeatures;
30 * An ordered list of features, with the promise that no feature in the list
31 * properly contains any other. This constraint allows bounded linear search
32 * of the list for features overlapping a region.
33 * Contact features are not included in this list.
35 List<SequenceFeature> nonNestedFeatures;
38 * contact features ordered by first contact position
40 List<SequenceFeature> contactFeatureStarts;
43 * contact features ordered by second contact position
45 List<SequenceFeature> contactFeatureEnds;
48 * Nested Containment List is used to hold any features that are nested
49 * within (properly contained by) any other feature. This is a recursive tree
50 * which supports depth-first scan for features overlapping a range.
51 * It is used here as a 'catch-all' fallback for features that cannot be put
52 * into a simple ordered list without invalidating the search methods.
54 NCList<SequenceFeature> nestedFeatures;
61 nonNestedFeatures = new ArrayList<SequenceFeature>();
62 // we only construct contactFeatures and the NCList if we need to
66 * Add one entry to the data store
70 public void addFeature(SequenceFeature feature)
72 if (feature.isContactFeature())
74 addContactFeature(feature);
76 else if (feature.isNonPositional())
78 addNonPositionalFeature(feature);
82 boolean added = addNonNestedFeature(feature);
86 * detected a nested feature - put it in the NCList structure
88 addNestedFeature(feature);
94 * Adds the feature to the list of non-positional features (with lazy
95 * instantiation of the list if it is null)
99 protected void addNonPositionalFeature(SequenceFeature feature)
101 if (nonPositionalFeatures == null)
103 nonPositionalFeatures = new ArrayList<SequenceFeature>();
105 nonPositionalFeatures.add(feature);
109 * Adds one feature to the NCList that can manage nested features (creating
110 * the NCList if necessary)
112 protected synchronized void addNestedFeature(SequenceFeature feature)
114 if (nestedFeatures == null)
116 nestedFeatures = new NCList<SequenceFeature>(feature);
120 nestedFeatures.add(feature);
125 * Add a feature to the list of non-nested features, maintaining the ordering
126 * of the list. A check is made for whether the feature is nested in (properly
127 * contained by) an existing feature. If there is no nesting, the feature is
128 * added to the list and the method returns true. If nesting is found, the
129 * feature is not added and the method returns false.
131 * Contact features are added at the position of their first contact point
136 protected boolean addNonNestedFeature(SequenceFeature feature)
138 synchronized (nonNestedFeatures)
140 int insertPosition = binarySearchForAdd(nonNestedFeatures, feature);
143 * fail if we detect feature enclosure - of the new feature by
144 * the one preceding it, or of the next feature by the new one
146 if (insertPosition > 0)
148 if (encloses(nonNestedFeatures.get(insertPosition - 1), feature))
153 if (insertPosition < nonNestedFeatures.size())
155 if (encloses(feature, nonNestedFeatures.get(insertPosition)))
162 * checks passed - add or append the feature
164 if (insertPosition == nonNestedFeatures.size())
166 nonNestedFeatures.add(feature);
170 nonNestedFeatures.add(insertPosition, feature);
177 * Answers true if range1 properly encloses range2, else false
183 protected boolean encloses(ContiguousI range1, ContiguousI range2)
185 int begin1 = range1.getBegin();
186 int begin2 = range2.getBegin();
187 int end1 = range1.getEnd();
188 int end2 = range2.getEnd();
189 if (begin1 == begin2 && end1 > end2)
193 if (begin1 < begin2 && end1 >= end2)
201 * Answers the index of the first element in the given list which follows or
202 * matches the given feature in the sort order. If no such element, answers
203 * the length of the list.
210 protected int binarySearchForAdd(List<SequenceFeature> list, SequenceFeature feature)
212 // TODO binary search!
214 while (i < list.size())
216 if (startOrdering.compare(nonNestedFeatures.get(i), feature) >= 0)
226 * Add a contact feature to the lists that hold them ordered by start (first
227 * contact) and by end (second contact) position, ensuring the lists remain
232 protected synchronized void addContactFeature(SequenceFeature feature)
234 // TODO binary search for insertion points!
235 if (contactFeatureStarts == null)
237 contactFeatureStarts = new ArrayList<SequenceFeature>();
239 if (contactFeatureEnds == null)
241 contactFeatureEnds = new ArrayList<SequenceFeature>();
243 contactFeatureStarts.add(feature);
244 Collections.sort(contactFeatureStarts, startOrdering);
245 contactFeatureEnds.add(feature);
246 Collections.sort(contactFeatureEnds, endOrdering);
250 * Returns a (possibly empty) list of entries whose range overlaps the given
251 * range. The returned list is not ordered. Contact features are included if
252 * either of the contact points lies within the range.
255 * start position of overlap range (inclusive)
257 * end position of overlap range (inclusive)
260 public List<SequenceFeature> findOverlappingFeatures(long start, long end)
262 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
264 findNonNestedFeatures(start, end, result);
266 findContactFeatures(start, end, result);
268 if (nestedFeatures != null)
270 result.addAll(nestedFeatures.findOverlaps(start, end));
277 * Adds contact features to the result list where either the second or the
278 * first contact position lies within the target range.
284 protected void findContactFeatures(long from, long to,
285 List<SequenceFeature> result)
287 if (contactFeatureStarts != null)
289 findContactStartFeatures(from, to, result);
291 if (contactFeatureEnds != null)
293 findContactEndFeatures(from, to, result);
302 protected void findContactEndFeatures(long from, long to,
303 List<SequenceFeature> result)
305 // TODO binary search for startPosition
306 for (int startPosition = 0; startPosition < contactFeatureEnds.size(); startPosition++)
308 SequenceFeature sf = contactFeatureEnds.get(startPosition);
309 if (!sf.isContactFeature())
311 System.err.println("Error! non-contact feature type "
312 + sf.getType() + " in contact features list");
315 int begin = sf.getBegin();
316 if (begin >= from && begin <= to)
319 * this feature's first contact position lies in the search range
320 * so we don't include it in results a second time
324 int end = sf.getEnd();
325 if (end >= from && end <= to)
333 * Returns the index of the first contact feature found whose end (second
334 * contact position) is not before the given start position. If no such
335 * feature is found, returns the length of the contact features list.
340 protected int contactsBinarySearch(long start)
342 // TODO binary search!!
344 while (i < contactFeatureEnds.size())
346 if (contactFeatureEnds.get(i).getEnd() >= start)
357 * Adds features to the result list that are at a single position which lies
358 * within the target range. Non-positional features (start=end=0) and contact
359 * features are excluded.
365 protected void findNonNestedFeatures(long from, long to,
366 List<SequenceFeature> result)
368 int startIndex = binarySearch(nonNestedFeatures, from);
369 findNonNestedFeatures(startIndex, from, to, result);
373 * Scans the list of non-nested features, starting from startIndex, to find
374 * those that overlap the from-to range, and adds them to the result list.
375 * Returns the index of the first feature whose start position is after the
376 * target range (or the length of the whole list if none such feature exists).
384 protected int findNonNestedFeatures(final int startIndex, long from,
386 List<SequenceFeature> result)
389 while (i < nonNestedFeatures.size())
391 SequenceFeature sf = nonNestedFeatures.get(i);
392 if (sf.getBegin() > to)
396 int start = sf.getBegin();
397 int end = sf.getEnd();
398 if (start <= to && end >= from)
408 * Performs a binary search of the (sorted) list to find the index of the
409 * first entry whose end position is not less than the target position (i.e.
410 * skip all features that properly precede the given position)
416 protected int binarySearch(List<SequenceFeature> features, long target)
418 int width = features.size() / 2;
422 int end = features.get(lastpos).getEnd();
433 // todo correct binary search
434 return lastpos > 1 ? lastpos - 2 : 0;
439 * Adds contact features whose start position lies in the from-to range to the
446 protected void findContactStartFeatures(long from, long to,
447 List<SequenceFeature> result)
449 // TODO binary search for startPosition
450 for (int startPosition = 0; startPosition < contactFeatureStarts.size(); startPosition++)
452 SequenceFeature sf = contactFeatureStarts.get(startPosition);
453 if (!sf.isContactFeature())
455 System.err.println("Error! non-contact feature type "
456 + sf.getType() + " in contact features list");
459 int begin = sf.getBegin();
460 if (begin >= from && begin <= to)
468 * Answers a list of all features stored (including any non-positional
469 * features), in no guaranteed order
473 public List<SequenceFeature> getFeatures()
476 * add non-nested features (may be all features for many cases)
478 List<SequenceFeature> result = new ArrayList<SequenceFeature>();
479 result.addAll(nonNestedFeatures);
482 * add any contact features - from the list by start position
484 if (contactFeatureStarts != null)
486 result.addAll(contactFeatureStarts);
490 * add any non-positional features
492 if (nonPositionalFeatures != null)
494 result.addAll(nonPositionalFeatures);
498 * add any nested features
500 if (nestedFeatures != null)
502 result.addAll(nestedFeatures.getEntries());
509 * Answers a list of all contact features. If there are none, returns an
510 * immutable empty list.
514 public List<SequenceFeature> getContactFeatures()
516 if (contactFeatureStarts == null)
518 return Collections.emptyList();
520 return new ArrayList<SequenceFeature>(contactFeatureStarts);
524 * Answers a list of all non-positional features. If there are none, returns
525 * an immutable empty list.
529 public List<SequenceFeature> getNonPositionalFeatures()
531 if (nonPositionalFeatures == null)
533 return Collections.emptyList();
535 return new ArrayList<SequenceFeature>(nonPositionalFeatures);