| 1 |
#ifndef NEEDLE_H |
| 2 |
#define NEEDLE_H |
| 3 |
|
| 4 |
#define BOOST_DISABLE_ASSERTS |
| 5 |
#include <boost/dynamic_bitset.hpp> |
| 6 |
#include <boost/multi_array.hpp> |
| 7 |
#include <iostream> |
| 8 |
#include "align.h" |
| 9 |
|
| 10 |
namespace torali |
| 11 |
{ |
| 12 |
|
| 13 |
inline int32_t |
| 14 |
longestHomology(std::string const& s1, std::string const& s2, int32_t scoreThreshold) { |
| 15 |
|
| 16 |
typedef boost::multi_array<int32_t, 2> TMatrix; |
| 17 |
int32_t m = s1.size(); |
| 18 |
int32_t n = s2.size(); |
| 19 |
TMatrix mat(boost::extents[m+1][n+1]); |
| 20 |
|
| 21 |
|
| 22 |
int32_t k = std::abs(scoreThreshold); |
| 23 |
mat[0][0] = 0; |
| 24 |
for(int32_t col = 1; col <= k; ++col) mat[0][col] = mat[0][col-1] - 1; |
| 25 |
for(int32_t row = 1; row <= k; ++row) mat[row][0] = mat[row-1][0] - 1; |
| 26 |
|
| 27 |
|
| 28 |
for(int32_t row = 1; row <= m; ++row) { |
| 29 |
int32_t bestCol = scoreThreshold - 1; |
| 30 |
for(int32_t h = -k; h <= k; ++h) { |
| 31 |
int32_t col = row + h; |
| 32 |
if ((col >= 1) && (col <= n)) { |
| 33 |
mat[row][col] = mat[row-1][col-1] + (s1[row-1] == s2[col-1] ? 0 : -1); |
| 34 |
if ((row - 1 - col >= -k) && (row - 1 - col <= k)) mat[row][col] = std::max(mat[row][col], mat[row-1][col] - 1); |
| 35 |
if ((row - col + 1 >= -k) && (row - col + 1 <= k)) mat[row][col] = std::max(mat[row][col], mat[row][col-1] - 1); |
| 36 |
if (mat[row][col] > bestCol) bestCol = mat[row][col]; |
| 37 |
} |
| 38 |
} |
| 39 |
if (bestCol < scoreThreshold) return row - 1; |
| 40 |
} |
| 41 |
return 0; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
template<typename TAlign, typename TAlignConfig, typename TScoreObject> |
| 46 |
inline bool |
| 47 |
longNeedle(std::string const& s1, std::string const& s2, TAlign& align, TAlignConfig const& ac, TScoreObject const& sc) |
| 48 |
{ |
| 49 |
typedef typename TScoreObject::TValue TScoreValue; |
| 50 |
typedef typename TAlign::index TAIndex; |
| 51 |
|
| 52 |
|
| 53 |
typedef boost::multi_array<int16_t, 2> TMatrix; |
| 54 |
std::size_t m = s1.size(); |
| 55 |
std::size_t n = s2.size(); |
| 56 |
TMatrix mat(boost::extents[m+1][n+1]); |
| 57 |
|
| 58 |
|
| 59 |
mat[0][0] = 0; |
| 60 |
for(std::size_t col = 1; col <= n; ++col) mat[0][col] = mat[0][col-1] + _horizontalGap(ac, 0, m, sc.ge); |
| 61 |
for(std::size_t row = 1; row <= m; ++row) mat[row][0] = mat[row-1][0] + _verticalGap(ac, 0, n, sc.ge); |
| 62 |
|
| 63 |
|
| 64 |
for(std::size_t row = 1; row <= m; ++row) |
| 65 |
for(std::size_t col = 1; col <= n; ++col) |
| 66 |
mat[row][col] = std::max(std::max(mat[row-1][col-1] + (s1[row-1] == s2[col-1] ? sc.match : sc.mismatch), mat[row-1][col] + _verticalGap(ac, col, n, sc.ge)), mat[row][col-1] + _horizontalGap(ac, row, m, sc.ge)); |
| 67 |
|
| 68 |
|
| 69 |
std::string sRev1 = s1; |
| 70 |
reverseComplement(sRev1); |
| 71 |
std::string sRev2 = s2; |
| 72 |
reverseComplement(sRev2); |
| 73 |
|
| 74 |
|
| 75 |
TMatrix rev(boost::extents[m+1][n+1]); |
| 76 |
rev[0][0] = 0; |
| 77 |
for(std::size_t col = 1; col <= n; ++col) rev[0][col] = rev[0][col-1] + _horizontalGap(ac, 0, m, sc.ge); |
| 78 |
for(std::size_t row = 1; row <= m; ++row) rev[row][0] = rev[row-1][0] + _verticalGap(ac, 0, n, sc.ge); |
| 79 |
for(std::size_t row = 1; row <= m; ++row) |
| 80 |
for(std::size_t col = 1; col <= n; ++col) |
| 81 |
rev[row][col] = std::max(std::max(rev[row-1][col-1] + (sRev1[row-1] == sRev2[col-1] ? sc.match : sc.mismatch), rev[row-1][col] + _verticalGap(ac, col, n, sc.ge)), rev[row][col-1] + _horizontalGap(ac, row, m, sc.ge)); |
| 82 |
|
| 83 |
if (mat[m][n] != rev[m][n]) { |
| 84 |
|
| 85 |
return false; |
| 86 |
} else { |
| 87 |
|
| 88 |
TMatrix bestMat(boost::extents[m+1][n+1]); |
| 89 |
for(std::size_t row = 0; row <= m; ++row) { |
| 90 |
bestMat[row][0] = mat[row][0]; |
| 91 |
for(std::size_t col = 1; col <= n; ++col) { |
| 92 |
if (mat[row][col] > bestMat[row][col-1]) bestMat[row][col] = mat[row][col]; |
| 93 |
else bestMat[row][col] = bestMat[row][col-1]; |
| 94 |
} |
| 95 |
} |
| 96 |
TMatrix bestRev(boost::extents[m+1][n+1]); |
| 97 |
for(std::size_t row = 0; row <= m; ++row) { |
| 98 |
bestRev[row][0] = rev[row][0]; |
| 99 |
for(std::size_t col = 1; col <= n; ++col) { |
| 100 |
if (rev[row][col] > bestRev[row][col-1]) bestRev[row][col] = rev[row][col]; |
| 101 |
else bestRev[row][col] = bestRev[row][col-1]; |
| 102 |
} |
| 103 |
} |
| 104 |
TScoreValue bestScore = mat[m][n]; |
| 105 |
std::size_t consLeft = 0; |
| 106 |
std::size_t refLeft = 0; |
| 107 |
for(std::size_t row = 0; row<=m; ++row) { |
| 108 |
for(std::size_t col = 0; col<=n; ++col) { |
| 109 |
if (bestMat[row][col]+bestRev[m-row][n-col] > bestScore) { |
| 110 |
bestScore=bestMat[row][col]+bestRev[m-row][n-col]; |
| 111 |
consLeft = row; |
| 112 |
refLeft = col; |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
std::size_t consRight = m - consLeft; |
| 117 |
std::size_t refRight = 0; |
| 118 |
|
| 119 |
for(std::size_t right = 0; right<=(n-refLeft); ++right) { |
| 120 |
if (mat[consLeft][refLeft] + rev[consRight][right] == bestScore) { |
| 121 |
refRight = right; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
if (bestScore == mat[m][n]) return false; |
| 153 |
|
| 154 |
|
| 155 |
std::size_t rr = consLeft; |
| 156 |
std::size_t cc = refLeft; |
| 157 |
typedef std::vector<char> TTrace; |
| 158 |
TTrace trace; |
| 159 |
while ((rr>0) || (cc>0)) { |
| 160 |
if ((rr>0) && (mat[rr][cc] == mat[rr-1][cc] + _verticalGap(ac, cc, n, sc.ge))) { |
| 161 |
--rr; |
| 162 |
trace.push_back('v'); |
| 163 |
} else if ((cc>0) && (mat[rr][cc] == mat[rr][cc-1] + _horizontalGap(ac, rr, m, sc.ge))) { |
| 164 |
--cc; |
| 165 |
trace.push_back('h'); |
| 166 |
} else { |
| 167 |
--rr; |
| 168 |
--cc; |
| 169 |
trace.push_back('s'); |
| 170 |
} |
| 171 |
} |
| 172 |
TAlign fwd; |
| 173 |
_createAlignment(trace, s1.substr(0, consLeft), s2.substr(0, refLeft), fwd); |
| 174 |
|
| 175 |
|
| 176 |
rr = consRight; |
| 177 |
cc = refRight; |
| 178 |
typedef std::vector<char> TTrace; |
| 179 |
TTrace rtrace; |
| 180 |
while ((rr>0) || (cc>0)) { |
| 181 |
if ((rr>0) && (rev[rr][cc] == rev[rr-1][cc] + _verticalGap(ac, cc, n, sc.ge))) { |
| 182 |
--rr; |
| 183 |
rtrace.push_back('v'); |
| 184 |
} else if ((cc>0) && (rev[rr][cc] == rev[rr][cc-1] + _horizontalGap(ac, rr, m, sc.ge))) { |
| 185 |
--cc; |
| 186 |
rtrace.push_back('h'); |
| 187 |
} else { |
| 188 |
--rr; |
| 189 |
--cc; |
| 190 |
rtrace.push_back('s'); |
| 191 |
} |
| 192 |
} |
| 193 |
TAlign rvs; |
| 194 |
_createAlignment(rtrace, sRev1.substr(0, consRight), sRev2.substr(0, refRight), rvs); |
| 195 |
|
| 196 |
|
| 197 |
std::size_t gapref = (n-refRight) - refLeft; |
| 198 |
std::size_t alilen = fwd.shape()[1] + rvs.shape()[1] + gapref; |
| 199 |
align.resize(boost::extents[2][alilen]); |
| 200 |
TAIndex jEnd = rvs.shape()[1]; |
| 201 |
for(TAIndex i = 0; i < (TAIndex) fwd.shape()[0]; ++i) { |
| 202 |
TAIndex alicol = 0; |
| 203 |
for(;alicol < (TAIndex) fwd.shape()[1]; ++alicol) align[i][alicol]=fwd[i][alicol]; |
| 204 |
for(TAIndex j = refLeft; j < (TAIndex) (n-refRight); ++j, ++alicol) { |
| 205 |
if (i==0) align[i][alicol] = '-'; |
| 206 |
else align[i][alicol] = s2[j]; |
| 207 |
} |
| 208 |
for(TAIndex j = 0; j < (TAIndex) rvs.shape()[1]; ++j, ++alicol) { |
| 209 |
switch (rvs[i][jEnd-j-1]) { |
| 210 |
case 'A': align[i][alicol] = 'T'; break; |
| 211 |
case 'C': align[i][alicol] = 'G'; break; |
| 212 |
case 'G': align[i][alicol] = 'C'; break; |
| 213 |
case 'T': align[i][alicol] = 'A'; break; |
| 214 |
case 'N': align[i][alicol] = 'N'; break; |
| 215 |
case '-': align[i][alicol] = '-'; break; |
| 216 |
default: break; |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
return true; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
template<typename TAlign1, typename TAlign2, typename TAlign, typename TAlignConfig, typename TScoreObject> |
| 226 |
inline int |
| 227 |
needle(TAlign1 const& a1, TAlign2 const& a2, TAlign& align, TAlignConfig const& ac, TScoreObject const& sc) |
| 228 |
{ |
| 229 |
typedef typename TScoreObject::TValue TScoreValue; |
| 230 |
|
| 231 |
|
| 232 |
std::size_t m = _size(a1, 1); |
| 233 |
std::size_t n = _size(a2, 1); |
| 234 |
std::vector<TScoreValue> s(n+1, 0); |
| 235 |
TScoreValue prevsub = 0; |
| 236 |
|
| 237 |
|
| 238 |
std::size_t mf = n+1; |
| 239 |
typedef boost::dynamic_bitset<> TBitSet; |
| 240 |
TBitSet bit3( (m+1) * (n+1), false); |
| 241 |
TBitSet bit4( (m+1) * (n+1), false); |
| 242 |
|
| 243 |
|
| 244 |
typedef boost::multi_array<double, 2> TProfile; |
| 245 |
TProfile p1; |
| 246 |
TProfile p2; |
| 247 |
if ((_size(a1, 0) != 1) || (_size(a2, 0) != 1)) { |
| 248 |
_createProfile(a1, p1); |
| 249 |
_createProfile(a2, p2); |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
for(std::size_t row = 0; row <= m; ++row) { |
| 254 |
for(std::size_t col = 0; col <= n; ++col) { |
| 255 |
|
| 256 |
if ((row == 0) && (col == 0)) { |
| 257 |
s[0] = 0; |
| 258 |
prevsub = 0; |
| 259 |
} else if (row == 0) { |
| 260 |
s[col] = _horizontalGap(ac, 0, m, col * sc.ge); |
| 261 |
bit3[col] = true; |
| 262 |
} else if (col == 0) { |
| 263 |
s[0] = _verticalGap(ac, 0, n, row * sc.ge); |
| 264 |
if (row - 1 == 0) prevsub = 0; |
| 265 |
else prevsub = _verticalGap(ac, 0, n, (row - 1) * sc.ge); |
| 266 |
bit4[row * mf] = true; |
| 267 |
} else { |
| 268 |
|
| 269 |
TScoreValue prevprevsub = prevsub; |
| 270 |
prevsub = s[col]; |
| 271 |
s[col] = std::max(std::max(prevprevsub + _score(a1, a2, p1, p2, row-1, col-1, sc), prevsub + _verticalGap(ac, col, n, sc.ge)), s[col-1] + _horizontalGap(ac, row, m, sc.ge)); |
| 272 |
|
| 273 |
|
| 274 |
if (s[col] == s[col-1] + _horizontalGap(ac, row, m, sc.ge)) bit3[row * mf + col] = true; |
| 275 |
else if (s[col] == prevsub + _verticalGap(ac, col, n, sc.ge)) bit4[row * mf + col] = true; |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
std::size_t row = m; |
| 282 |
std::size_t col = n; |
| 283 |
typedef std::vector<char> TTrace; |
| 284 |
TTrace trace; |
| 285 |
while ((row>0) || (col>0)) { |
| 286 |
if (bit3[row * mf + col]) { |
| 287 |
--col; |
| 288 |
trace.push_back('h'); |
| 289 |
} else if (bit4[row * mf + col]) { |
| 290 |
--row; |
| 291 |
trace.push_back('v'); |
| 292 |
} else { |
| 293 |
--row; |
| 294 |
--col; |
| 295 |
trace.push_back('s'); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
_createAlignment(trace, a1, a2, align); |
| 301 |
|
| 302 |
|
| 303 |
return s[n]; |
| 304 |
} |
| 305 |
|
| 306 |
template<typename TAlign1, typename TAlign2, typename TAlign, typename TAlignConfig> |
| 307 |
inline int |
| 308 |
needle(TAlign1 const& a1, TAlign2 const& a2, TAlign& align, TAlignConfig const& ac) |
| 309 |
{ |
| 310 |
DnaScore<int> dnasc; |
| 311 |
return needle(a1, a2, align, ac, dnasc); |
| 312 |
} |
| 313 |
|
| 314 |
template<typename TAlign1, typename TAlign2, typename TAlign> |
| 315 |
inline int |
| 316 |
needle(TAlign1 const& a1, TAlign2 const& a2, TAlign& align) |
| 317 |
{ |
| 318 |
AlignConfig<false, false> ac; |
| 319 |
return needle(a1, a2, align, ac); |
| 320 |
} |
| 321 |
|
| 322 |
} |
| 323 |
|
| 324 |
#endif |