Debian Codegraph @

src/data/Interval.cpp

raw
1 #include <pbcopper/data/Interval.h>
2
3 #include <boost/algorithm/string.hpp>
4 #include <boost/lexical_cast.hpp>
5
6 #include <istream>
7 #include <ostream>
8 #include <sstream>
9 #include <vector>
10
11 namespace PacBio {
12 namespace Data {
13
14 Interval::Interval(const Position val) : data_{val, val + 1} {}
15
16 Interval::Interval(const Position start, const Position end) : data_{start, end} {}
17
18 void Interval::Reset(const Position start, const Position end)
19 {
20 assert(start <= end);
21 *this = Interval{start, end};
22 }
23
24 void Interval::Reset(const Interval& other)
25 {
26 assert(other.Start() <= other.End());
27 *this = other;
28 }
29
30 bool Interval::operator==(const Interval& other) const noexcept { return data_ == other.data_; }
31
32 bool Interval::operator!=(const Interval& other) const noexcept { return !(data_ == other.data_); }
33
34 bool Interval::operator<(const Interval& other) const noexcept { return data_ < other.data_; }
35
36 bool Interval::IsEmpty() const { return Length() == 0; }
37
38 bool Interval::ContainedBy(const Interval& other) const
39 {
40 return boost::icl::within(data_, other.data_);
41 }
42
43 bool Interval::Contains(const Interval& other) const
44 {
45 return boost::icl::contains(data_, other.data_);
46 }
47
48 Position Interval::End() const { return data_.upper(); }
49
50 Interval& Interval::End(const Position& end)
51 {
52 data_ = interval_type{data_.lower(), end};
53 return *this;
54 }
55
56 bool Interval::Intersects(const Interval& other) const
57 {
58 return boost::icl::intersects(data_, other.data_);
59 }
60
61 bool Interval::Overlaps(const Interval& other) const
62 {
63 // if the left of one is in the range of the other
64 return (other.Start() <= Start() && Start() <= other.End()) ||
65 (Start() <= other.End() && other.Start() <= End());
66 }
67
68 bool Interval::IsValid() const { return !boost::icl::is_empty(data_); }
69
70 Data::Position Interval::Length() const { return boost::icl::length(data_); }
71
72 Position Interval::Start() const { return data_.lower(); }
73
74 Interval& Interval::Start(const Position& start)
75 {
76 data_ = interval_type{start, data_.upper()};
77 return *this;
78 }
79
80 Interval Interval::Union(const Interval& other) const
81 {
82 if (Overlaps(other)) {
83 return {std::min(Start(), other.Start()), std::max(End(), other.End())};
84 } else {
85 std::ostringstream msg;
86 msg << "[pbcopper] interval ERROR: cannot merge intervals " << *this << " and " << other
87 << ", because they do not overlap.";
88 throw std::invalid_argument{msg.str()};
89 }
90 }
91
92 Interval Interval::Intersect(const Interval& other) const
93 {
94 if (Overlaps(other)) {
95 return {std::max(Start(), other.Start()), std::min(End(), other.End())};
96 } else {
97 std::ostringstream msg;
98 msg << "[pbcopper] interval ERROR: cannot create the intersection of intervals " << *this
99 << " and " << other << ", because they do not overlap.";
100 throw std::invalid_argument{msg.str()};
101 }
102 }
103
104 Interval Interval::FromString(const std::string& str)
105 {
106 try {
107 std::vector<std::string> components;
108 boost::split(components, str, boost::is_any_of("-"));
109 if (components.size() == 1) {
110 Position left = boost::lexical_cast<Position>(components[0]);
111 return Interval(left, left + 1);
112 } else if (components.size() == 2) {
113 Position left = boost::lexical_cast<Position>(components[0]);
114 Position right = boost::lexical_cast<Position>(components[1]);
115 // if right < left, we have an invalid interval, fall through
116 if (left <= right) {
117 return Interval(left, right + 1);
118 }
119 }
120 } catch (...) {
121 }
122
123 throw std::invalid_argument("[pbcopper] interval ERROR: cannot create an interval from '" +
124 str + "'");
125 }
126
127 Interval Interval::begin() const { return Interval(Start(), End()); }
128
129 Interval Interval::end() const { return Interval(End(), End()); }
130
131 Interval& Interval::operator++()
132 {
133 Position newStart = Start();
134 ++newStart;
135 return Start(newStart);
136 }
137
138 Interval Interval::operator++(int)
139 {
140 Position newStart = Start();
141 return Interval{newStart++, End()};
142 }
143
144 Position Interval::operator*() const { return Start(); }
145
146 std::ostream& operator<<(std::ostream& os, const Interval& interval)
147 {
148 return os << '[' << interval.Start() << ", " << interval.End() << ')';
149 }
150
151 std::istream& operator>>(std::istream& is, Interval& interval)
152 {
153 char delim;
154 Position start{}, end{};
155 is >> std::ws >> delim >> start >> delim >> std::ws >> end >> delim;
156 assert(is.good() && (start <= end));
157
158 interval.Start(start);
159 interval.End(end);
160
161 return is;
162 }
163
164 } // namespace Data
165 } // namespace PacBio