1 package jalview.datamodel.features;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
9 * An adapted implementation of NCList as described in the paper
12 * Nested Containment List (NCList): a new algorithm for accelerating
13 * interval query of genome alignment and interval databases
14 * - Alexander V. Alekseyenko, Christopher J. Lee
15 * https://doi.org/10.1093/bioinformatics/btl647
18 public class NCList<T extends ContiguousI>
21 * the number of ranges represented
26 * a list, in start position order, of sublists of ranges ordered so
27 * that each contains (or is the same as) the one that follows it
29 private List<NCNode<T>> subranges;
32 * a comparator to sort intervals by start position ascending, with
33 * longer (enclosing) intervals preceding those they enclose
35 Comparator<ContiguousI> intervalSorter = new RangeComparator(true);
38 * Constructor given a list of things that are each located on a contiguous
39 * interval. Note that the constructor may reorder the list.
41 * We assume here that for each range, start <= end. Behaviour for reverse
42 * ordered ranges is undefined.
46 public NCList(List<T> ranges)
55 protected void build(List<T> ranges)
58 * sort by start ascending so that contained intervals
59 * follow their containing interval
61 Collections.sort(ranges, intervalSorter);
63 List<SubList> sublists = findSubranges(ranges);
66 * convert each subrange to an NCNode consisting of a range and
67 * (possibly) its contained NCList
69 for (SubList sublist : sublists)
71 subranges.add(new NCNode<T>(ranges.subList(sublist.startIndex,
72 sublist.endIndex + 1)));
78 public NCList(T entry)
81 List<T> ranges = new ArrayList<T>();
88 subranges = new ArrayList<NCNode<T>>();
92 * Traverses the sorted ranges to identify sublists, within which each
93 * interval contains the one that follows it
98 protected List<SubList> findSubranges(List<T> ranges)
100 List<SubList> sublists = new ArrayList<SubList>();
102 int listStartIndex = 0;
103 long lastEndPos = Long.MAX_VALUE;
105 for (int i = 0; i < ranges.size(); i++)
107 ContiguousI nextInterval = ranges.get(i);
108 long nextStart = nextInterval.getBegin();
109 long nextEnd = nextInterval.getEnd();
110 if (nextStart > lastEndPos || nextEnd > lastEndPos)
113 * this interval is not contained in the preceding one
114 * close off the last sublist
116 sublists.add(new SubList(listStartIndex, i - 1));
119 lastEndPos = nextEnd;
122 sublists.add(new SubList(listStartIndex, ranges.size() - 1));
127 * Adds one entry to the stored set
131 public synchronized void add(T entry)
134 long start = entry.getBegin();
135 long end = entry.getEnd();
139 * - precedes all subranges: add as NCNode on front of list
140 * - follows all subranges: add as NCNode on end of list
141 * - enclosed by a subrange - add recursively to subrange
142 * - encloses one or more subranges - push them inside it
143 * - none of the above - add as a new node and resort nodes list (?)
147 * find the first subrange whose end does not precede entry's start
149 int candidateIndex = findFirstOverlap(start);
150 if (candidateIndex == -1)
153 * all subranges precede this one - add it on the end
155 subranges.add(new NCNode<T>(entry));
160 * search for maximal span of subranges i-k that the new entry
161 * encloses; or a subrange that encloses the new entry
163 boolean enclosing = false;
164 int firstEnclosed = 0;
165 int lastEnclosed = 0;
166 boolean overlapping = false;
168 for (int j = candidateIndex; j < subranges.size(); j++)
170 NCNode<T> subrange = subranges.get(j);
172 if (end < subrange.getStart() && !overlapping && !enclosing)
175 * new entry lies between subranges j-1 j
177 subranges.add(j, new NCNode<T>(entry));
181 if (subrange.getStart() <= start && subrange.getEnd() >= end)
184 * push new entry inside this subrange as it encloses it
190 if (start <= subrange.getStart())
192 if (end >= subrange.getEnd())
195 * new entry encloses this subrange (and possibly preceding ones);
196 * continue to find the maximal list it encloses
209 * entry spans from before this subrange to inside it
214 * entry encloses one or more preceding subranges
216 addEnclosingRange(entry, firstEnclosed, lastEnclosed);
222 * entry spans two subranges but doesn't enclose any
225 subranges.add(j, new NCNode<T>(entry));
237 * drops through to here if new range encloses all others
241 addEnclosingRange(entry, firstEnclosed, lastEnclosed);
246 * Update the tree so that the range of the new entry encloses subranges i to
247 * j (inclusive). That is, replace subranges i-j (inclusive) with a new
248 * subrange that contains them.
254 protected synchronized void addEnclosingRange(T entry, final int i,
257 NCList<T> newNCList = new NCList<T>();
258 newNCList.subranges.addAll(subranges.subList(i, j + 1));
259 NCNode<T> newNode = new NCNode<T>(entry, newNCList);
260 for (int k = j; k >= i; k--)
264 subranges.add(i, newNode);
268 * Returns a (possibly empty) list of items whose extent overlaps the given
272 * start of overlap range (inclusive)
274 * end of overlap range (inclusive)
277 public List<T> findOverlaps(long from, long to)
279 List<T> result = new ArrayList<T>();
281 findOverlaps(from, to, result);
287 * Recursively searches the NCList adding any items that overlap the from-to
288 * range to the result list
294 protected void findOverlaps(long from, long to,
298 * find the first sublist that might overlap, i.e.
299 * the first whose end position is >= from
301 int candidateIndex = findFirstOverlap(from);
303 if (candidateIndex == -1)
308 for (int i = candidateIndex; i < subranges.size(); i++)
310 NCNode<T> candidate = subranges.get(i);
311 if (candidate.getStart() > to)
314 * we are past the end of our target range
318 candidate.findOverlaps(from, to, result);
324 * Search subranges for the first one whose end position is not before the
325 * target range's start position, i.e. the first one that may overlap the
326 * target range. Returns the index in the list of the first such range found,
327 * or -1 if none found.
332 protected int findFirstOverlap(long from)
334 // TODO binary search
335 // for now quick cheat linear search
337 if (subranges != null)
339 for (NCNode<T> subrange : subranges)
341 if (subrange.getEnd() >= from)
352 * Formats the tree as a bracketed list e.g.
355 * [1-100 [10-30 [10-20]], 15-30 [20-20]]
359 public String toString()
361 return subranges.toString();
365 * Returns a string representation of the data where containment is shown bgy
366 * indentation on new lines
370 public String prettyPrint()
372 StringBuilder sb = new StringBuilder(512);
375 prettyPrint(sb, offset, indent);
376 return sb.toString();
384 void prettyPrint(StringBuilder sb, int offset, int indent)
386 boolean first = true;
387 for (NCNode<T> subrange : subranges)
391 sb.append(System.lineSeparator());
394 subrange.prettyPrint(sb, offset, indent);
399 * Answers true if the data held satisfy the rules of construction of an
400 * NCList, else false.
404 public boolean isValid()
406 return isValid(Integer.MIN_VALUE, Integer.MAX_VALUE);
410 * Answers true if the data held satisfy the rules of construction of an
411 * NCList bounded within the given start-end range, else false.
413 * Each subrange must lie within start-end (inclusive). Subranges must be
414 * ordered by start position ascending.
421 boolean isValid(final int start, final int end)
423 int lastStart = start;
424 for (NCNode<T> subrange : subranges)
426 if (subrange.getStart() < lastStart)
428 System.err.println("error in NCList: range " + subrange.toString()
429 + " starts before " + lastStart);
432 if (subrange.getEnd() > end)
434 System.err.println("error in NCList: range " + subrange.toString()
435 + " ends after " + end);
438 lastStart = subrange.getStart();
440 if (!subrange.isValid())
449 * Answers the lowest start position enclosed by the ranges
453 public int getStart()
455 return subranges.isEmpty() ? 0 : subranges.get(0).getStart();
459 * Returns the number of ranges held (deep count)
469 * Returns a list of all entries stored
473 public List<T> getEntries()
475 List<T> result = new ArrayList<T>();
481 * Adds all contained entries to the given list
485 void getEntries(List<T> result)
487 for (NCNode<T> subrange : subranges)
489 subrange.getEntries(result);