00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef CGATOOLS_REFERENCE_RANGE_HPP_
00016 #define CGATOOLS_REFERENCE_RANGE_HPP_ 1
00017
00020
00021 #include "cgatools/core.hpp"
00022 #include "cgatools/util/Exception.hpp"
00023
00024 namespace cgatools { namespace reference {
00025
00028 class Location
00029 {
00030 public:
00031 Location()
00032 : chromosome_(0),
00033 offset_(0)
00034 {
00035 }
00036
00037 Location(uint16_t chromosome, uint32_t offset)
00038 : chromosome_(chromosome),
00039 offset_(offset)
00040 {
00041 }
00042
00045 int32_t distanceTo( const Location& other ) const;
00046
00047 uint16_t chromosome_;
00048 uint32_t offset_;
00049 };
00050
00051 inline int32_t Location::distanceTo( const Location& other ) const
00052 {
00053 if (chromosome_!=other.chromosome_)
00054 return std::numeric_limits<int32_t>::max();
00055 return other.offset_-offset_;
00056 }
00057
00058 inline bool operator==(const Location& lhs, const Location& rhs)
00059 {
00060 return lhs.chromosome_ == rhs.chromosome_ &&
00061 lhs.offset_ == rhs.offset_;
00062 }
00063
00064 inline bool operator!=(const Location& lhs, const Location& rhs)
00065 {
00066 return !(lhs == rhs);
00067 }
00068
00069 inline bool operator<(const Location& lhs, const Location& rhs)
00070 {
00071 if (lhs.chromosome_ != rhs.chromosome_)
00072 return lhs.chromosome_ < rhs.chromosome_;
00073 return lhs.offset_ < rhs.offset_;
00074 }
00075
00076 inline bool operator<=(const Location& lhs, const Location& rhs)
00077 {
00078 return !(rhs < lhs);
00079 }
00080
00081 inline bool operator>(const Location& lhs, const Location& rhs)
00082 {
00083 return rhs < lhs;
00084 }
00085
00086 inline bool operator>=(const Location& lhs, const Location& rhs)
00087 {
00088 return !(lhs < rhs);
00089 }
00090
00091 inline std::ostream& operator<<(std::ostream& ostr, const Location& l)
00092 {
00093 return ostr << l.chromosome_<< "," << l.offset_;
00094 }
00095
00098 class Range
00099 {
00100 public:
00101 Range()
00102 : chromosome_(0),
00103 begin_(0),
00104 end_(0)
00105 {
00106 }
00107
00108 Range(uint16_t chromosome, uint32_t begin, uint32_t end)
00109 : chromosome_(chromosome),
00110 begin_(begin),
00111 end_(end)
00112 {
00113 }
00114
00115 Range(const Location& beginLoc, const Location& endLoc)
00116 : chromosome_(beginLoc.chromosome_),
00117 begin_(beginLoc.offset_),
00118 end_(endLoc.offset_)
00119 {
00120 CGA_ASSERT_EQ(beginLoc.chromosome_, endLoc.chromosome_);
00121 }
00122
00125 Location beginLocation() const
00126 {
00127 return Location(chromosome_, begin_);
00128 }
00129
00132 Location endLocation() const
00133 {
00134 return Location(chromosome_, end_);
00135 }
00136
00138 uint32_t length() const
00139 {
00140 return end_-begin_;
00141 }
00142
00144 bool contains (const Location& loc) const
00145 {
00146 return chromosome_ == loc.chromosome_
00147 && loc.offset_ >= begin_ && loc.offset_ < end_;
00148 }
00149
00155 bool intersects (const Range& r) const
00156 {
00157 return chromosome_ == r.chromosome_
00158 && ((r.begin_ < end_ && begin_ < r.end_)
00159 || begin_ == r.begin_ || end_ == r.end_);
00160 }
00161
00164 Range overlappingRange (const Range& rr) const
00165 {
00166 CGA_ASSERT(intersects(rr));
00167 return Range(chromosome_, std::max(begin_, rr.begin_), std::min(end_, rr.end_));
00168 }
00169
00170 uint16_t chromosome_;
00171 uint32_t begin_;
00172
00173 uint32_t end_;
00174
00175 };
00176
00177 inline bool operator==(const Range& lhs, const Range& rhs)
00178 {
00179 return lhs.chromosome_ == rhs.chromosome_ &&
00180 lhs.begin_ == rhs.begin_ &&
00181 lhs.end_ == rhs.end_;
00182 }
00183
00184 inline bool operator!=(const Range& lhs, const Range& rhs)
00185 {
00186 return !(lhs == rhs);
00187 }
00188
00189 inline bool operator<(const Range& lhs, const Range& rhs)
00190 {
00191 if (lhs.chromosome_ != rhs.chromosome_)
00192 return lhs.chromosome_ < rhs.chromosome_;
00193 if (lhs.begin_ != rhs.begin_)
00194 return lhs.begin_ < rhs.begin_;
00195 return lhs.end_ < rhs.end_;
00196 }
00197
00198 inline bool operator<=(const Range& lhs, const Range& rhs)
00199 {
00200 return !(rhs < lhs);
00201 }
00202
00203 inline bool operator>(const Range& lhs, const Range& rhs)
00204 {
00205 return rhs < lhs;
00206 }
00207
00208 inline bool operator>=(const Range& lhs, const Range& rhs)
00209 {
00210 return !(lhs < rhs);
00211 }
00212
00215 struct RangeOverlap
00216 {
00217 bool operator()(const Range& a, const Range& b) const
00218 {
00219 return a.intersects(b);
00220 }
00221 };
00222
00225 struct GetRangeBoundary
00226 {
00227 Location operator()(const Range& r, size_t side) const
00228 {
00229 if (0 == side)
00230 return r.beginLocation();
00231 else
00232 return r.endLocation();
00233 }
00234 };
00235
00236 } }
00237
00238 #endif // CGATOOLS_REFERENCE_RANGE_HPP_