Debian Codegraph @

src/data/IntervalTree.cpp

raw
1 #include <pbcopper/data/IntervalTree.h>
2
3 #include <boost/algorithm/string.hpp>
4
5 #include <algorithm>
6 #include <vector>
7
8 namespace PacBio {
9 namespace Data {
10
11 bool IntervalTree::WeakIntervalOrdering::operator()(const Interval& lhs,
12 const Interval& rhs) const noexcept
13 {
14 return lhs.Start() < rhs.Start();
15 }
16
17 void IntervalTree::Insert(const Interval& interval)
18 {
19 auto it = storage.insert(interval);
20
21 // check to see if we overlap the previous element,
22 // if we do, start our merge loop from there
23 if (it != begin()) {
24 const_iterator prev = std::prev(it);
25
26 if (prev->Overlaps(*it)) {
27 it = prev;
28 }
29 }
30
31 while (it != end()) {
32 const_iterator nx = std::next(it);
33
34 if (nx != end() && it->Overlaps(*nx)) {
35 const auto u = it->Union(*nx);
36 it = storage.erase(it);
37 it = storage.erase(it);
38 it = storage.insert(it, u);
39 } else {
40 break;
41 }
42 }
43 }
44
45 IntervalTree IntervalTree::Gaps() const
46 {
47 IntervalTree gaps;
48
49 for (auto it = begin(); it != end(); ++it) {
50 const_iterator nx = std::next(it);
51
52 if (nx == end()) {
53 break;
54 }
55
56 gaps.Insert(Interval(it->End(), nx->Start()));
57 }
58
59 return gaps;
60 }
61
62 IntervalTree IntervalTree::Gaps(const Interval& interval) const
63 {
64 auto left = begin();
65 auto right = end();
66
67 // if we're empty (left == right == end()) or we don't overlap the interval
68 // return just the provided interval
69 if (left == right || !interval.Overlaps(Interval(left->Start(), (--right)->End()))) {
70 IntervalTree gaps;
71 gaps.Insert(interval);
72 return gaps;
73 }
74
75 IntervalTree gaps = Gaps();
76
77 if (interval.Start() < left->Start()) {
78 gaps.Insert(Interval(interval.Start(), left->Start()));
79 }
80
81 if (right->End() < interval.End()) {
82 gaps.Insert(Interval(right->End(), interval.End()));
83 }
84
85 return gaps;
86 }
87
88 bool IntervalTree::Contains(const Position value) const
89 {
90 const_iterator it = std::lower_bound(begin(), end(), Interval(value, value + 1));
91
92 if (it != begin()) {
93 it = std::prev(it);
94 }
95
96 for (; it != end() && it->Start() <= value; ++it) {
97 if (it->Contains(value)) {
98 return true;
99 }
100 }
101
102 return false;
103 }
104
105 IntervalTree::iterator IntervalTree::begin() { return storage.begin(); }
106 IntervalTree::const_iterator IntervalTree::begin() const { return storage.begin(); }
107 IntervalTree::iterator IntervalTree::end() { return storage.end(); }
108 IntervalTree::const_iterator IntervalTree::end() const { return storage.end(); }
109
110 size_t IntervalTree::size() const { return storage.size(); }
111
112 IntervalTree IntervalTree::FromString(const std::string& str)
113 {
114 std::vector<std::string> components;
115 boost::split(components, str, boost::is_any_of(","));
116 IntervalTree tree;
117 for (const auto& component : components) {
118 tree.Insert(Interval::FromString(component));
119 }
120 return tree;
121 }
122
123 } // namespace Data
124 } // namespace PacBio