| 1 |
#include <pbcopper/data/GenomicInterval.h> |
| 2 |
|
| 3 |
#include <boost/algorithm/string.hpp> |
| 4 |
|
| 5 |
#include <stdexcept> |
| 6 |
#include <string> |
| 7 |
#include <type_traits> |
| 8 |
#include <vector> |
| 9 |
|
| 10 |
#include <cctype> |
| 11 |
#include <cstdlib> |
| 12 |
#include <cstring> |
| 13 |
|
| 14 |
namespace PacBio { |
| 15 |
namespace Data { |
| 16 |
namespace { |
| 17 |
|
| 18 |
struct RegionStringException : public std::runtime_error |
| 19 |
{ |
| 20 |
RegionStringException(std::string region) |
| 21 |
: std::runtime_error{"[pbcopper] genomic interval ERROR: malformed region string '" + |
| 22 |
region + "'"} |
| 23 |
{} |
| 24 |
}; |
| 25 |
|
| 26 |
// returns sequence name & sets begin/end, from input regionString |
| 27 |
std::string parseRegionString(const std::string& reg, Position* begin, Position* end) |
| 28 |
{ |
| 29 |
std::vector<std::string> parts; |
| 30 |
boost::split(parts, reg, boost::is_any_of(":"), boost::token_compress_on); |
| 31 |
|
| 32 |
if (parts.empty() || parts.size() > 2) { |
| 33 |
throw RegionStringException{reg}; |
| 34 |
} |
| 35 |
|
| 36 |
// given name only, default min,max intervals |
| 37 |
if (parts.size() == 1) { |
| 38 |
*begin = 0; |
| 39 |
*end = 1 << 29; |
| 40 |
} |
| 41 |
|
| 42 |
// parse interval from input |
| 43 |
else if (parts.size() == 2) { |
| 44 |
std::vector<std::string> intervalParts; |
| 45 |
boost::split(intervalParts, parts.at(1), boost::is_any_of("-"), boost::token_compress_on); |
| 46 |
if (intervalParts.empty() || intervalParts.size() > 2) { |
| 47 |
throw RegionStringException{reg}; |
| 48 |
} |
| 49 |
*begin = std::stoi(intervalParts.at(0)); |
| 50 |
*end = std::stoi(intervalParts.at(1)); |
| 51 |
} |
| 52 |
|
| 53 |
return parts.at(0); |
| 54 |
} |
| 55 |
|
| 56 |
} // namespace |
| 57 |
|
| 58 |
GenomicInterval::GenomicInterval(std::string name, Position start, Position stop) |
| 59 |
: name_{std::move(name)}, interval_{std::move(start), std::move(stop)} |
| 60 |
{} |
| 61 |
|
| 62 |
GenomicInterval::GenomicInterval(const std::string& samtoolsRegionString) |
| 63 |
{ |
| 64 |
Position begin = UNMAPPED_POSITION; |
| 65 |
Position end = UNMAPPED_POSITION; |
| 66 |
|
| 67 |
name_ = parseRegionString(samtoolsRegionString, &begin, &end); |
| 68 |
if ((begin == UNMAPPED_POSITION) || (end == UNMAPPED_POSITION)) { |
| 69 |
throw RegionStringException{samtoolsRegionString}; |
| 70 |
} |
| 71 |
interval_ = Data::Interval(begin, end); |
| 72 |
} |
| 73 |
|
| 74 |
bool GenomicInterval::operator==(const GenomicInterval& other) const noexcept |
| 75 |
{ |
| 76 |
return std::tie(name_, interval_) == std::tie(other.name_, other.interval_); |
| 77 |
} |
| 78 |
|
| 79 |
bool GenomicInterval::operator!=(const GenomicInterval& other) const noexcept |
| 80 |
{ |
| 81 |
return !(*this == other); |
| 82 |
} |
| 83 |
|
| 84 |
bool GenomicInterval::ContainedBy(const GenomicInterval& other) const |
| 85 |
{ |
| 86 |
if (name_ != other.name_) { |
| 87 |
return false; |
| 88 |
} |
| 89 |
return interval_.ContainedBy(other.interval_); |
| 90 |
} |
| 91 |
|
| 92 |
bool GenomicInterval::Contains(const GenomicInterval& other) const |
| 93 |
{ |
| 94 |
if (name_ != other.name_) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
return interval_.Contains(other.interval_); |
| 98 |
} |
| 99 |
|
| 100 |
bool GenomicInterval::Intersects(const GenomicInterval& other) const |
| 101 |
{ |
| 102 |
if (name_ != other.name_) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
return interval_.Intersects(other.interval_); |
| 106 |
} |
| 107 |
|
| 108 |
Data::Interval GenomicInterval::Interval() const { return interval_; } |
| 109 |
|
| 110 |
GenomicInterval& GenomicInterval::Interval(Data::Interval interval) |
| 111 |
{ |
| 112 |
interval_ = std::move(interval); |
| 113 |
return *this; |
| 114 |
} |
| 115 |
|
| 116 |
bool GenomicInterval::IsValid() const |
| 117 |
{ |
| 118 |
return (!name_.empty() && (interval_.Start() >= 0) && (interval_.End() >= 0) && |
| 119 |
interval_.IsValid()); |
| 120 |
} |
| 121 |
|
| 122 |
size_t GenomicInterval::Length() const { return interval_.Length(); } |
| 123 |
|
| 124 |
std::string GenomicInterval::Name() const { return name_; } |
| 125 |
|
| 126 |
GenomicInterval& GenomicInterval::Name(std::string name) |
| 127 |
{ |
| 128 |
name_ = std::move(name); |
| 129 |
return *this; |
| 130 |
} |
| 131 |
|
| 132 |
Position GenomicInterval::Start() const { return interval_.Start(); } |
| 133 |
|
| 134 |
GenomicInterval& GenomicInterval::Start(const Position start) |
| 135 |
{ |
| 136 |
interval_.Start(start); |
| 137 |
return *this; |
| 138 |
} |
| 139 |
|
| 140 |
Position GenomicInterval::Stop() const { return interval_.End(); } |
| 141 |
|
| 142 |
GenomicInterval& GenomicInterval::Stop(const Position stop) |
| 143 |
{ |
| 144 |
interval_.End(stop); |
| 145 |
return *this; |
| 146 |
} |
| 147 |
|
| 148 |
} // namespace Data |
| 149 |
} // namespace PacBio |