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
00043 uint16_t chromosome_;
00044 uint32_t offset_;
00045 };
00046
00047 inline bool operator==(const Location& lhs, const Location& rhs)
00048 {
00049 return lhs.chromosome_ == rhs.chromosome_ &&
00050 lhs.offset_ == rhs.offset_;
00051 }
00052
00053 inline bool operator!=(const Location& lhs, const Location& rhs)
00054 {
00055 return !(lhs == rhs);
00056 }
00057
00058 inline bool operator<(const Location& lhs, const Location& rhs)
00059 {
00060 if (lhs.chromosome_ != rhs.chromosome_)
00061 return lhs.chromosome_ < rhs.chromosome_;
00062 return lhs.offset_ < rhs.offset_;
00063 }
00064
00065 inline bool operator<=(const Location& lhs, const Location& rhs)
00066 {
00067 return !(rhs < lhs);
00068 }
00069
00070 inline bool operator>(const Location& lhs, const Location& rhs)
00071 {
00072 return rhs < lhs;
00073 }
00074
00075 inline bool operator>=(const Location& lhs, const Location& rhs)
00076 {
00077 return !(lhs < rhs);
00078 }
00079
00082 class Range
00083 {
00084 public:
00085 Range()
00086 : chromosome_(0),
00087 begin_(0),
00088 end_(0)
00089 {
00090 }
00091
00092 Range(uint16_t chromosome, uint32_t begin, uint32_t end)
00093 : chromosome_(chromosome),
00094 begin_(begin),
00095 end_(end)
00096 {
00097 }
00098
00099 Range(const Location& beginLoc, const Location& endLoc)
00100 : chromosome_(beginLoc.chromosome_),
00101 begin_(beginLoc.offset_),
00102 end_(endLoc.offset_)
00103 {
00104 CGA_ASSERT_EQ(beginLoc.chromosome_, endLoc.chromosome_);
00105 }
00106
00109 Location beginLocation() const
00110 {
00111 return Location(chromosome_, begin_);
00112 }
00113
00116 Location endLocation() const
00117 {
00118 return Location(chromosome_, end_);
00119 }
00120
00122 uint32_t length() const
00123 {
00124 return end_-begin_;
00125 }
00126
00128 bool contains (const Location& loc) const
00129 {
00130 return chromosome_ == loc.chromosome_
00131 && loc.offset_ >= begin_ && loc.offset_ < end_;
00132 }
00133
00139 bool intersects (const Range& r) const
00140 {
00141 return chromosome_ == r.chromosome_
00142 && ((r.begin_ < end_ && begin_ < r.end_)
00143 || begin_ == r.begin_ || end_ == r.end_);
00144 }
00145
00148 Range overlappingRange (const Range& rr) const
00149 {
00150 CGA_ASSERT(intersects(rr));
00151 return Range(chromosome_, std::max(begin_, rr.begin_), std::min(end_, rr.end_));
00152 }
00153
00154 uint16_t chromosome_;
00155 uint32_t begin_;
00156
00157 uint32_t end_;
00158
00159 };
00160
00161 inline bool operator==(const Range& lhs, const Range& rhs)
00162 {
00163 return lhs.chromosome_ == rhs.chromosome_ &&
00164 lhs.begin_ == rhs.begin_ &&
00165 lhs.end_ == rhs.end_;
00166 }
00167
00168 inline bool operator!=(const Range& lhs, const Range& rhs)
00169 {
00170 return !(lhs == rhs);
00171 }
00172
00173 inline bool operator<(const Range& lhs, const Range& rhs)
00174 {
00175 if (lhs.chromosome_ != rhs.chromosome_)
00176 return lhs.chromosome_ < rhs.chromosome_;
00177 if (lhs.begin_ != rhs.begin_)
00178 return lhs.begin_ < rhs.begin_;
00179 return lhs.end_ < rhs.end_;
00180 }
00181
00182 inline bool operator<=(const Range& lhs, const Range& rhs)
00183 {
00184 return !(rhs < lhs);
00185 }
00186
00187 inline bool operator>(const Range& lhs, const Range& rhs)
00188 {
00189 return rhs < lhs;
00190 }
00191
00192 inline bool operator>=(const Range& lhs, const Range& rhs)
00193 {
00194 return !(lhs < rhs);
00195 }
00196
00197 } }
00198
00199 #endif // CGATOOLS_REFERENCE_RANGE_HPP_