/usr/include/pbseq/alignment/algorithms/alignment/KBandAlign.hpp is in libblasr-dev 0~20161219-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | #ifndef _BLASR_K_BAND_ALIGN_HPP_
#define _BLASR_K_BAND_ALIGN_HPP_
#include <iosfwd>
#include <algorithm>
#include <vector>
#include <limits.h>
// pbdata
#include "../../../pbdata/defs.h"
#include "../../../pbdata/NucConversion.hpp"
#include "../../../pbdata/matrix/FlatMatrix.hpp"
#include "AlignmentUtils.hpp"
#include "../../datastructures/alignment/Alignment.hpp"
#include "../../statistics/StatUtils.hpp"
class DefaultGuide {
public:
DefaultGuide();
int operator()(int i);
};
template<typename T_QuerySequence, typename T_TargetSequence, typename T_Alignment, typename T_ScoreFn>
int KBandAlign(T_QuerySequence &pqSeq, T_TargetSequence &ptSeq,
int matchMat[5][5], int ins, int del, DNALength k,
std::vector<int> &scoreMat,
std::vector<Arrow> & pathMat,
T_Alignment &alignment,
T_ScoreFn &scoreFn,
AlignmentType alignType=Global,
bool samplePaths=false) {
return KBandAlign(pqSeq, ptSeq, matchMat, ins, del, k, scoreMat, pathMat, alignment, alignType, scoreFn, samplePaths);
}
void SetKBoundedLengths(DNALength tLength, DNALength qLength, DNALength k, DNALength &tLen, DNALength &qLen);
template<typename T_Sequence>
void CreateThreeBitSequence(T_Sequence &origSeq, T_Sequence &threeBitSeq) {
//
// Make a copy of the sequences that is guaranteed to be in 3-bit format.
// This is 2 bits for A,C,T,and G, and an extra bit to signal masked sequence.
//
ResizeSequence(threeBitSeq, origSeq.length);
VectorIndex i;
for (i = 0; i < origSeq.length; i++ ) {
threeBitSeq.seq[i] = ThreeBit[origSeq.seq[i]];
}
}
template<typename T_QuerySequence, typename T_TargetSequence, typename T_Alignment, typename T_ScoreFn>
int KBandAlign(T_QuerySequence &qSeq, T_TargetSequence &tSeq,
int matchMat[5][5], int ins, int del,
DNALength k,
std::vector<int> &scoreMat,
std::vector<Arrow> &pathMat,
T_Alignment &alignment,
AlignmentType alignType,
T_ScoreFn &scoreFn, bool samplePaths=false) {
(void)(matchMat);
DNALength qLen, tLen;
SetKBoundedLengths(tSeq.length, qSeq.length, k, tLen, qLen);
//
//
// Allow for length up to diaonal + k + 1 for boundary.
//
// Allow for width:
// diagonal (1)
// up to k insertions (k)
// up to k deletions (k)
// boundary on left side of matrix (1)
//
DNALength nCols = 2*k + 1;
DNALength totalMatSize = (qLen + 1) * nCols;
alignment.nCells = totalMatSize;
if (scoreMat.size() < totalMatSize) {
scoreMat.resize(totalMatSize);
pathMat.resize(totalMatSize);
}
//
// Initialze matrices
//
std::fill(scoreMat.begin(), scoreMat.begin() + totalMatSize, 0);
std::fill(pathMat.begin(), pathMat.begin() + totalMatSize, NoArrow);
//
// Initialize the boundaries of the score and path matrices.
//
DNALength q, t;
for (q = 1; q <= k && q < qLen + 1; q++) {
scoreMat[rc2index(q, k - q, nCols)] = q * ins;
pathMat[rc2index(q, k - q , nCols)] = Up;
}
if (alignType == Global) {
for (t = 1; t <= k && t < tLen; t++) {
scoreMat[rc2index(0, t + k , nCols)] = t * del;
pathMat[rc2index(0, t + k , nCols)] = Left;
}
}
if (alignType == QueryFit or alignType == Fit) {
for (t = 1; t <= k && t < tLen; t++) {
scoreMat[rc2index(0, t + k , nCols)] = 0;
pathMat[rc2index(0, t + k , nCols)] = Left;
}
}
if (alignType == TargetFit or alignType == Fit) {
for (q = 1; q <= k && q < qLen; q++) {
scoreMat[rc2index(q, 0, nCols)] = 0;
pathMat[rc2index(q, 0, nCols)] = Up;
}
}
//
// Initialize the 0,0 position to be a match.
//
scoreMat[rc2index(0, k, nCols)] = 0;
pathMat[rc2index(0, k, nCols)] = Diagonal;
int matchScore, insScore, delScore;
for (q = 1; q <= qLen; q++) {
for (t = q - k; t < q + k + 1; t++) {
if (t < 1)
continue;
if (t > tLen)
continue;
// On left boundary of k-band.
// do not allow deletions of t.
if (t == q - k) {
delScore = INF_INT;
}
else {
// cur row = q
// cur col = t - q
// prev col therefore t - q - 1
// and offset from diagonal is k + t - q - 1
delScore = scoreMat[rc2index(q, k + t - q - 1, nCols)] + scoreFn.Deletion(tSeq, (DNALength) t-1, qSeq, (DNALength)q-1);
}
// cur row = q
// cur col = t - q
// cur query index = q - 1
// cur target index = t - 1
// therefore match row (up) = q
// match col (left, but since up shifted right) = t - q
assert(rc2index(q - 1, k + t - q, nCols) < scoreMat.size());
assert(t-1 >= 0);
assert(q-1 >= 0);
int tmpMatchScore = scoreFn.Match(tSeq, t-1, qSeq, q-1);
matchScore = scoreMat[rc2index(q - 1, k + t - q, nCols)] + tmpMatchScore;
//
// Possibly on right boundary of k-band, in which
// case do not allow insertions from q.
if (t == q + k ) {
insScore = INF_INT;
}
else {
// cur row = q
// cur col = t - q
// therefore insertion col = t - q + 1
insScore = scoreMat[rc2index(q-1, k + t - q+1, nCols)] + scoreFn.Insertion(tSeq, (DNALength) t-1, qSeq, q-1);
}
int minScore = MIN(matchScore, MIN(insScore, delScore));
size_t curIndex = rc2index(q, k + t - q, nCols);
assert(curIndex < scoreMat.size());
scoreMat[curIndex] = minScore;
int nEqual = 0;
(matchScore == minScore ? nEqual++ : nEqual );
(insScore == minScore ? nEqual++ : nEqual );
(delScore == minScore ? nEqual++ : nEqual );
if (samplePaths == false or nEqual == 1) {
if (minScore == matchScore) {
pathMat[curIndex] = Diagonal;
}
else if (minScore == delScore) {
pathMat[curIndex] = Left;
}
else {
pathMat[curIndex] = Up;
}
}
else {
//
// When there are paths of equal score reaching
//
if (nEqual == 3) {
int v = RandomInt(3);
if (v == 0) { pathMat[curIndex] = Diagonal; }
else if (v == 1) { pathMat[curIndex] = Left; }
else if (v == 2) { pathMat[curIndex] = Up; }
}
else {
assert(nEqual == 2);
int v = RandomInt(2);
if (matchScore == insScore) {
if (v == 0) { pathMat[curIndex] = Diagonal; } else { pathMat[curIndex] = Up; }
}
else if (matchScore == delScore) {
if (v == 0) { pathMat[curIndex] = Diagonal; } else { pathMat[curIndex] = Left; }
}
else if (delScore == insScore) {
if (v == 0) { pathMat[curIndex] = Left; } else { pathMat[curIndex] = Up; }
}
else {
std::cout << "ERROR, counted two values equal to the minimum but cannot find them." << std::endl;
assert(0);
}
}
alignment.nSampledPaths++;
}
}
}
//
// Now create the alignment.
//
q = qLen ;
t = k - (qLen - tLen);
int globalMinScore;
int minLastColScoreIndex=0, minLastRowScoreIndex=0;
globalMinScore = scoreMat[rc2index(q,t,nCols)];
int minLastColScore = globalMinScore, minLastRowScore = globalMinScore;
if (alignType == QueryFit or alignType == Fit) {
DNALength q2,t2;
q2 = qLen;
t2 = k - (qLen - tLen);
bool minScoreSet = false;
for (t2 = q - k; t2 < q2 + k + 1; t2++) {
if (t2 < 1)
continue;
if (t2 > tLen)
continue;
// std::cout << t2 << " " << tLen << " " << " " << scoreMat[rc2index(q2, k+t2-q,nCols)] << " " << minLastRowScore <<std::endl;
if (minScoreSet == false or scoreMat[rc2index(q2, k+t2-q,nCols)] < minLastRowScore){
minScoreSet = true;
minLastRowScore = scoreMat[rc2index(q2,k+t2-q,nCols)];
minLastRowScoreIndex = t2;
}
}
if (minScoreSet) {
t2 = k - (q - minLastRowScoreIndex);
t = t2;
q = q2;
}
}
if (alignType == TargetFit or alignType == Fit) {
// Fit the target inside the query wh
DNALength q2,t2;
q2 = qLen;
t2 = k - (qLen - tLen);
bool minScoreSet = false;
for (q2 = qLen; q2 >= tLen - k and q2 > 0; q2--) {
int index = rc2index(q2, k+tLen-q2, nCols);
if (minScoreSet == false or scoreMat[index] < minLastColScore) {
minLastColScore = scoreMat[index];
minScoreSet = true;
minLastColScoreIndex = q2;
}
}
if (alignType == Fit) {
if (minLastColScore < minLastRowScore) {
t = t2;
q = minLastColScoreIndex;
}
}
else if (alignType == TargetFit) {
t = t2;
q = minLastColScoreIndex;
}
}
std::vector<Arrow> optAlignment;
int optScore = scoreMat[rc2index(q, t, nCols)];
Arrow arrow;
/*
PrintFlatMatrix(&pathMat[0], qLen + 1, nCols, debugOut);
std::cout << std::endl;
ofstream debugOut;
stringstream debugOutName;
debugOutName << "kband_" << kbandcounter << ".table";
debugOut.open(debugOutName.str().c_str());
PrintFlatMatrix(&scoreMat[0], qLen + 1, nCols, debugOut);
kbandcounter++;
*/
/*
std::cout << std::endl;
*/
//
// Use some logic to deal with unsigned types. When t > k, t must
// also be greater than 0, so it's not worth checking to see if it
// hits a boundary.
//
if (alignType == Global or alignType == QueryFit) {
while (q > 0 and (t < k ? (k - t != q) : true)) {
arrow = pathMat[rc2index(q,t, nCols)];
if (arrow == NoArrow) {
break;
}
optAlignment.push_back(arrow);
if (arrow == Diagonal) {
q--;
}
else if (arrow == Up) {
q--;
t++;
}
else if (arrow == Left) {
t--;
}
}
}
else if (alignType == Fit) {
while (q > 0 and (t < k ? (k - t != q) : true) and ( q <= k ? k - q != t : true) ) {
arrow = pathMat[rc2index(q,t, nCols)];
if (arrow == NoArrow) {
break;
}
optAlignment.push_back(arrow);
if (arrow == Diagonal) {
q--;
}
else if (arrow == Up) {
q--;
t++;
}
else if (arrow == Left) {
t--;
}
}
}
else if (alignType == TargetFit) {
while (q > 0 and ( q < k ? k - q != t : true) ) {
arrow = pathMat[rc2index(q,t, nCols)];
if (arrow == NoArrow) {
break;
}
optAlignment.push_back(arrow);
if (arrow == Diagonal) {
q--;
}
else if (arrow == Up) {
q--;
t++;
}
else if (arrow == Left) {
t--;
}
}
}
// remove the boundary condition.
//optAlignment.pop_back();
// qSeq.Free();
// tSeq.Free();
alignment.qPos = q;
//
// Use a little extra logic to deal with the unsignedness of indices.
//
if (t < k) {
alignment.tPos = (k - t) - q;
}
else {
alignment.tPos = (t - k) - q;
}
std::reverse(optAlignment.begin(), optAlignment.end());
alignment.ArrowPathToAlignment(optAlignment);
return optScore;
}
#endif // _BLASR_K_BAND_ALIGN_HPP_
|