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