| 1 |
#include <pbcopper/align/Seed.h> |
| 2 |
|
| 3 |
#include <cassert> |
| 4 |
#include <cmath> |
| 5 |
|
| 6 |
#include <algorithm> |
| 7 |
#include <tuple> |
| 8 |
#include <type_traits> |
| 9 |
|
| 10 |
namespace PacBio { |
| 11 |
namespace Align { |
| 12 |
|
| 13 |
Seed::Seed(const uint64_t beginPosH, const uint64_t beginPosV, const uint64_t seedLength) |
| 14 |
: beginPositionH_{beginPosH} |
| 15 |
, beginPositionV_{beginPosV} |
| 16 |
, endPositionH_{beginPosH + seedLength} |
| 17 |
, endPositionV_{beginPosV + seedLength} |
| 18 |
, lowerDiagonal_{static_cast<int64_t>(beginPosH - beginPosV)} |
| 19 |
, upperDiagonal_{static_cast<int64_t>(beginPosH - beginPosV)} |
| 20 |
, score_{0} |
| 21 |
{} |
| 22 |
|
| 23 |
Seed::Seed(const uint64_t beginPosH, const uint64_t beginPosV, const uint64_t endPosH, |
| 24 |
const uint64_t endPosV) |
| 25 |
: beginPositionH_{beginPosH} |
| 26 |
, beginPositionV_{beginPosV} |
| 27 |
, endPositionH_{endPosH} |
| 28 |
, endPositionV_{endPosV} |
| 29 |
, lowerDiagonal_{std::min(static_cast<int64_t>(beginPosH - beginPosV), |
| 30 |
static_cast<int64_t>(endPosH - endPosV))} |
| 31 |
, upperDiagonal_{std::max(static_cast<int64_t>(beginPosH - beginPosV), |
| 32 |
static_cast<int64_t>(endPosH - endPosV))} |
| 33 |
, score_{0} |
| 34 |
{ |
| 35 |
assert(upperDiagonal_ >= lowerDiagonal_); |
| 36 |
} |
| 37 |
|
| 38 |
int64_t Seed::BeginDiagonal() const |
| 39 |
{ |
| 40 |
return static_cast<int64_t>(beginPositionH_ - beginPositionV_); |
| 41 |
} |
| 42 |
|
| 43 |
uint64_t Seed::BeginPositionH() const { return beginPositionH_; } |
| 44 |
|
| 45 |
Seed& Seed::BeginPositionH(const uint64_t beginPosH) |
| 46 |
{ |
| 47 |
beginPositionH_ = beginPosH; |
| 48 |
return *this; |
| 49 |
} |
| 50 |
|
| 51 |
uint64_t Seed::BeginPositionV() const { return beginPositionV_; } |
| 52 |
|
| 53 |
Seed& Seed::BeginPositionV(const uint64_t beginPosV) |
| 54 |
{ |
| 55 |
beginPositionV_ = beginPosV; |
| 56 |
return *this; |
| 57 |
} |
| 58 |
|
| 59 |
int64_t Seed::Diagonal() const |
| 60 |
{ |
| 61 |
if (beginPositionV_ > beginPositionH_) { |
| 62 |
return -static_cast<int64_t>(beginPositionV_ - beginPositionH_); |
| 63 |
} else { |
| 64 |
return static_cast<int64_t>(beginPositionH_ - beginPositionV_); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
int64_t Seed::EndDiagonal() const { return static_cast<int64_t>(endPositionH_ - endPositionV_); } |
| 69 |
|
| 70 |
uint64_t Seed::EndPositionH() const { return endPositionH_; } |
| 71 |
|
| 72 |
Seed& Seed::EndPositionH(const uint64_t endPosH) |
| 73 |
{ |
| 74 |
endPositionH_ = endPosH; |
| 75 |
return *this; |
| 76 |
} |
| 77 |
|
| 78 |
uint64_t Seed::EndPositionV() const { return endPositionV_; } |
| 79 |
|
| 80 |
Seed& Seed::EndPositionV(const uint64_t endPosV) |
| 81 |
{ |
| 82 |
endPositionV_ = endPosV; |
| 83 |
return *this; |
| 84 |
} |
| 85 |
|
| 86 |
int64_t Seed::LowerDiagonal() const { return lowerDiagonal_; } |
| 87 |
|
| 88 |
Seed& Seed::LowerDiagonal(const int64_t lowerDiagonal) |
| 89 |
{ |
| 90 |
lowerDiagonal_ = lowerDiagonal; |
| 91 |
return *this; |
| 92 |
} |
| 93 |
|
| 94 |
int64_t Seed::UpperDiagonal() const { return upperDiagonal_; } |
| 95 |
|
| 96 |
Seed& Seed::UpperDiagonal(const int64_t upperDiagonal) |
| 97 |
{ |
| 98 |
upperDiagonal_ = upperDiagonal; |
| 99 |
return *this; |
| 100 |
} |
| 101 |
|
| 102 |
int32_t Seed::Score() const { return score_; } |
| 103 |
|
| 104 |
Seed& Seed::Score(const int32_t score) |
| 105 |
{ |
| 106 |
score_ = score; |
| 107 |
return *this; |
| 108 |
} |
| 109 |
|
| 110 |
uint64_t Seed::Size() const |
| 111 |
{ |
| 112 |
return std::max(endPositionH_ - beginPositionH_, endPositionV_ - beginPositionV_); |
| 113 |
} |
| 114 |
|
| 115 |
Seed& Seed::operator+=(const Seed& other) |
| 116 |
{ |
| 117 |
// cached initial, input sizes |
| 118 |
const auto initialSize = Size(); |
| 119 |
const auto otherSize = other.Size(); |
| 120 |
|
| 121 |
// calculate new coordinates |
| 122 |
beginPositionH_ = std::min(beginPositionH_, other.beginPositionH_); |
| 123 |
beginPositionV_ = std::min(beginPositionV_, other.beginPositionV_); |
| 124 |
endPositionH_ = std::max(endPositionH_, other.endPositionH_); |
| 125 |
endPositionV_ = std::max(endPositionV_, other.endPositionV_); |
| 126 |
lowerDiagonal_ = std::min(lowerDiagonal_, other.lowerDiagonal_); |
| 127 |
upperDiagonal_ = std::max(upperDiagonal_, other.upperDiagonal_); |
| 128 |
|
| 129 |
// get new size & overlap length |
| 130 |
const auto newSize = Size(); |
| 131 |
const auto overlap = initialSize + otherSize - newSize; |
| 132 |
|
| 133 |
// sanity checks: |
| 134 |
// - new size must be >= each input seed |
| 135 |
// - new size must be <= total input seed lengths |
| 136 |
// - overlap must be <= each input seed |
| 137 |
// |
| 138 |
assert(newSize >= initialSize); |
| 139 |
assert(newSize >= otherSize); |
| 140 |
assert(newSize <= (initialSize + otherSize)); |
| 141 |
assert(overlap <= otherSize); |
| 142 |
assert(overlap <= initialSize); |
| 143 |
|
| 144 |
// adjust score depending on fraction used from each seed |
| 145 |
const double total = initialSize + otherSize - overlap; |
| 146 |
const double intoFraction = (initialSize - 0.5 * overlap) / total; |
| 147 |
const double fromFraction = (otherSize - 0.5 * overlap) / total; |
| 148 |
const auto newScore = |
| 149 |
static_cast<int32_t>(std::round(intoFraction * Score() + fromFraction * other.Score())); |
| 150 |
score_ = newScore; |
| 151 |
return *this; |
| 152 |
} |
| 153 |
|
| 154 |
Seed Seed::operator+(const Seed& other) const |
| 155 |
{ |
| 156 |
Seed s(*this); |
| 157 |
s += other; |
| 158 |
return s; |
| 159 |
} |
| 160 |
|
| 161 |
bool operator<(const Seed& lhs, const Seed& rhs) noexcept |
| 162 |
{ |
| 163 |
return lhs.BeginDiagonal() < rhs.BeginDiagonal(); |
| 164 |
} |
| 165 |
|
| 166 |
bool operator==(const Seed& lhs, const Seed& rhs) noexcept |
| 167 |
{ |
| 168 |
return std::make_tuple(lhs.BeginPositionH(), lhs.BeginPositionV(), lhs.EndPositionH(), |
| 169 |
lhs.EndPositionV(), lhs.LowerDiagonal(), lhs.UpperDiagonal(), |
| 170 |
lhs.Score()) == std::make_tuple(rhs.BeginPositionH(), |
| 171 |
rhs.BeginPositionV(), rhs.EndPositionH(), |
| 172 |
rhs.EndPositionV(), rhs.LowerDiagonal(), |
| 173 |
rhs.UpperDiagonal(), rhs.Score()); |
| 174 |
} |
| 175 |
|
| 176 |
} // namespace Align |
| 177 |
} // namespace PacBio |