| 1 |
#include <pbcopper/align/AffineAlignment.h> |
| 2 |
|
| 3 |
#include <cassert> |
| 4 |
#include <cfloat> |
| 5 |
|
| 6 |
#include <algorithm> |
| 7 |
#include <string> |
| 8 |
#include <vector> |
| 9 |
|
| 10 |
#include <boost/numeric/ublas/matrix.hpp> |
| 11 |
#include <boost/numeric/ublas/matrix_proxy.hpp> |
| 12 |
|
| 13 |
#include <pbcopper/align/PairwiseAlignment.h> |
| 14 |
#include <pbcopper/utility/MinMax.h> |
| 15 |
#include <pbcopper/utility/SequenceUtils.h> |
| 16 |
|
| 17 |
namespace PacBio { |
| 18 |
namespace Align { |
| 19 |
|
| 20 |
namespace { |
| 21 |
|
| 22 |
class IupacAware; |
| 23 |
class Standard; |
| 24 |
|
| 25 |
inline bool IsIupacPartialMatch(char iupacCode, char b) |
| 26 |
{ |
| 27 |
assert(iupacCode != b); |
| 28 |
|
| 29 |
switch (iupacCode) { |
| 30 |
case 'R': |
| 31 |
return (b == 'A' || b == 'G'); |
| 32 |
case 'Y': |
| 33 |
return (b == 'C' || b == 'T'); |
| 34 |
case 'S': |
| 35 |
return (b == 'G' || b == 'C'); |
| 36 |
case 'W': |
| 37 |
return (b == 'A' || b == 'T'); |
| 38 |
case 'K': |
| 39 |
return (b == 'G' || b == 'T'); |
| 40 |
case 'M': |
| 41 |
return (b == 'A' || b == 'C'); |
| 42 |
default: |
| 43 |
return false; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
template <typename C> |
| 48 |
float MatchScore(char t, char q, float matchScore, float mismatchScore, float partialMatchScore); |
| 49 |
|
| 50 |
template <> |
| 51 |
float MatchScore<Standard>(char t, char q, float matchScore, float mismatchScore, float /*unused*/) |
| 52 |
{ |
| 53 |
return (t == q ? matchScore : mismatchScore); |
| 54 |
} |
| 55 |
|
| 56 |
template <> |
| 57 |
float MatchScore<IupacAware>(char t, char q, float matchScore, float mismatchScore, |
| 58 |
float partialMatchScore) |
| 59 |
{ |
| 60 |
if (t == q) { |
| 61 |
return matchScore; |
| 62 |
} else if (IsIupacPartialMatch(t, q) || IsIupacPartialMatch(q, t)) { |
| 63 |
return partialMatchScore; |
| 64 |
} else { |
| 65 |
return mismatchScore; |
| 66 |
} // NOLINT |
| 67 |
} |
| 68 |
|
| 69 |
template <class C> |
| 70 |
PairwiseAlignment* AlignAffineGeneric(const std::string& target, const std::string& query, |
| 71 |
AffineAlignmentParams params) |
| 72 |
{ |
| 73 |
// Implementation follows the textbook "two-state" affine gap model |
| 74 |
// description from Durbin et. al |
| 75 |
using boost::numeric::ublas::matrix; |
| 76 |
|
| 77 |
int I = query.length(); |
| 78 |
int J = target.length(); |
| 79 |
matrix<float> M(I + 1, J + 1); |
| 80 |
matrix<float> GAP(I + 1, J + 1); |
| 81 |
|
| 82 |
// Initialization |
| 83 |
M(0, 0) = 0; |
| 84 |
GAP(0, 0) = -FLT_MAX; |
| 85 |
for (int i = 1; i <= I; ++i) { |
| 86 |
M(i, 0) = -FLT_MAX; |
| 87 |
GAP(i, 0) = params.GapOpen + (i - 1) * params.GapExtend; |
| 88 |
} |
| 89 |
for (int j = 1; j <= J; ++j) { |
| 90 |
M(0, j) = -FLT_MAX; |
| 91 |
GAP(0, j) = params.GapOpen + (j - 1) * params.GapExtend; |
| 92 |
} |
| 93 |
|
| 94 |
// Main part of the recursion |
| 95 |
for (int i = 1; i <= I; ++i) { |
| 96 |
for (int j = 1; j <= J; ++j) { |
| 97 |
float matchScore = MatchScore<C>(target[j - 1], query[i - 1], params.MatchScore, |
| 98 |
params.MismatchScore, params.PartialMatchScore); |
| 99 |
M(i, j) = std::max(M(i - 1, j - 1), GAP(i - 1, j - 1)) + matchScore; |
| 100 |
GAP(i, j) = |
| 101 |
Utility::Max(M(i, j - 1) + params.GapOpen, GAP(i, j - 1) + params.GapExtend, |
| 102 |
M(i - 1, j) + params.GapOpen, GAP(i - 1, j) + params.GapExtend); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
// Perform the traceback |
| 107 |
const int MATCH_MATRIX = 1; |
| 108 |
const int GAP_MATRIX = 2; |
| 109 |
|
| 110 |
std::string raQuery; |
| 111 |
std::string raTarget; |
| 112 |
int i = I; |
| 113 |
int j = J; |
| 114 |
int mat = (M(I, J) >= GAP(I, J) ? MATCH_MATRIX : GAP_MATRIX); |
| 115 |
int iPrev; |
| 116 |
int jPrev; |
| 117 |
int matPrev; |
| 118 |
while (i > 0 || j > 0) { |
| 119 |
if (mat == MATCH_MATRIX) { |
| 120 |
matPrev = (M(i - 1, j - 1) >= GAP(i - 1, j - 1) ? MATCH_MATRIX : GAP_MATRIX); |
| 121 |
iPrev = i - 1; |
| 122 |
jPrev = j - 1; |
| 123 |
raQuery.push_back(query[iPrev]); |
| 124 |
raTarget.push_back(target[jPrev]); |
| 125 |
} else { |
| 126 |
assert(mat == GAP_MATRIX); |
| 127 |
float s[4]; |
| 128 |
s[0] = (j > 0 ? M(i, j - 1) + params.GapOpen : -FLT_MAX); |
| 129 |
s[1] = (j > 0 ? GAP(i, j - 1) + params.GapExtend : -FLT_MAX); |
| 130 |
s[2] = (i > 0 ? M(i - 1, j) + params.GapOpen : -FLT_MAX); |
| 131 |
s[3] = (i > 0 ? GAP(i - 1, j) + params.GapExtend : -FLT_MAX); |
| 132 |
int argMax = std::max_element(s, s + 4) - s; |
| 133 |
|
| 134 |
matPrev = ((argMax == 0 || argMax == 2) ? MATCH_MATRIX : GAP_MATRIX); |
| 135 |
if (argMax == 0 || argMax == 1) { |
| 136 |
iPrev = i; |
| 137 |
jPrev = j - 1; |
| 138 |
raQuery.push_back('-'); |
| 139 |
raTarget.push_back(target[jPrev]); |
| 140 |
} else { |
| 141 |
iPrev = i - 1; |
| 142 |
jPrev = j; |
| 143 |
raQuery.push_back(query[iPrev]); |
| 144 |
raTarget.push_back('-'); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
// Go to previous square |
| 149 |
i = iPrev; |
| 150 |
j = jPrev; |
| 151 |
mat = matPrev; |
| 152 |
} |
| 153 |
|
| 154 |
assert(raQuery.length() == raTarget.length()); |
| 155 |
return new PairwiseAlignment(Utility::Reversed(raTarget), Utility::Reversed(raQuery)); |
| 156 |
} |
| 157 |
|
| 158 |
} // anonymous namespace |
| 159 |
|
| 160 |
AffineAlignmentParams::AffineAlignmentParams(float matchScore, float mismatchScore, float gapOpen, |
| 161 |
float gapExtend, float partialMatchScore) |
| 162 |
: MatchScore(matchScore) |
| 163 |
, MismatchScore(mismatchScore) |
| 164 |
, GapOpen(gapOpen) |
| 165 |
, GapExtend(gapExtend) |
| 166 |
, PartialMatchScore(partialMatchScore) |
| 167 |
{} |
| 168 |
|
| 169 |
AffineAlignmentParams DefaultAffineAlignmentParams() { return {0, -1.0, -1.0, -0.5, 0}; } |
| 170 |
|
| 171 |
AffineAlignmentParams IupacAwareAffineAlignmentParams() { return {0, -1.0, -1.0, -0.5, -0.25}; } |
| 172 |
|
| 173 |
PairwiseAlignment* AlignAffine(const std::string& target, const std::string& query, |
| 174 |
AffineAlignmentParams params) |
| 175 |
{ |
| 176 |
return AlignAffineGeneric<Standard>(target, query, params); |
| 177 |
} |
| 178 |
|
| 179 |
PairwiseAlignment* AlignAffineIupac(const std::string& target, const std::string& query, |
| 180 |
AffineAlignmentParams params) |
| 181 |
{ |
| 182 |
return AlignAffineGeneric<IupacAware>(target, query, params); |
| 183 |
} |
| 184 |
|
| 185 |
} // namespace Align |
| 186 |
} // namespace PacBio |