/usr/include/blasr/algorithms/alignment/OneGapAlignment.hpp is in libblasr-dev 0~20151014+gitbe5d1bf-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 | #ifndef _BLASR_ONEGAP_ALIGNMENT_HPP_
#define _BLASR_ONEGAP_ALIGNMENT_HPP_
#include <limits.h>
#include "Types.h"
#include "FASTQSequence.hpp"
#include "matrix/FlatMatrix.hpp"
#include "datastructures/alignment/Path.h"
#include "datastructures/alignment/Alignment.hpp"
/*
Perform gapped alignment that aligns the entire query sequence to
leftTarget, rightTarget, or between the two with a gap in between.
The gap between leftTarget and rightTarget is an affine gap.
*/
template<typename T_QuerySequence, typename T_RefSequence, typename T_ScoreFunction>
int OneGapAlign(T_QuerySequence &query,
T_RefSequence &leftTarget,
T_RefSequence &rightTarget,
DNALength distanceBetweenLeftAndRightTarget,
T_ScoreFunction &scoreFn,
Alignment &alignment,
FlatMatrix2D<int> & scoreMat, FlatMatrix2D<Arrow> & pathMat,
FlatMatrix2D<int> &affineScoreMat, FlatMatrix2D<Arrow> &affinePathMat) {
/*
Perform alignment that spans what is effectively two pairs of
matrices. This is implemented as a single matrix, however paths
may only transition through the boundary between the two through
the affine portion of the matrices.
leftTarget rightTarget
affine |==============|
| x------>x |
| ^ | |
|===|=======|==|
| |
regular |===|=+||===|=+|
| x || | |
| || v |
|======||======|
*/
UInt nQueryRows, nLeftTargetCols, nRightTargetCols;
UInt nTargetCols;
nQueryRows = query.length + 1;
nLeftTargetCols = leftTarget.length + 1;
nRightTargetCols = rightTarget.length;
nTargetCols = nLeftTargetCols + nRightTargetCols;
//
// Create the matrices
//
affineScoreMat.Grow(nQueryRows, nTargetCols);
affinePathMat.Grow(nQueryRows, nTargetCols);
scoreMat.Grow(nQueryRows, nTargetCols);
pathMat.Grow(nQueryRows, nTargetCols);
//
// Initialize to undefined for debugging purposes.
//
affineScoreMat.Initialize(0);
affinePathMat.Initialize(NoArrow);
scoreMat.Initialize(0);
pathMat.Initialize(NoArrow);
// Initialize insertion and deletion strips
UInt i, j;
scoreMat[0][0] = 0;
pathMat[0][0] = NoArrow;
affineScoreMat[0][0] = 0;
// always drop down to 0,0 at end of affine gap.
affinePathMat[0][0] = AffineDelOpen;
for (i = 1; i < nQueryRows; i++) {
scoreMat[i][0] = scoreMat[i-1][0] + scoreFn.ins;
pathMat[i][0] = Up;
//
// Affine gaps can only start here.
//
affineScoreMat[i][0] = scoreMat[i][0];
affinePathMat[i][0] = AffineDelOpen;
}
for (j = 1; j < nTargetCols; j++) {
scoreMat[0][j] = scoreMat[0][j-1] + scoreFn.del;
pathMat[0][j] = Left;
//
// Allow free affine transition across first row.
//
affineScoreMat[0][j] = 0;
affinePathMat[0][j] = Left;
}
// Now run the alignment.
// i and j index over positions in the query/target sequences, or
// [0,len(seq)). Since the score mat are of length len(seq) + 1,
// the indices of the score matrices are [1, len(seq) + 1)
for (i = 0; i < query.length; i++) {
//
// First align a row of the left target, allowing for transition
// up to the big affine gap. No transitions down from affine gap
// are allowed.
//
for (j = 0; j < leftTarget.length; j++) {
//
// First, assign the non-affine score.
//
int matchScore, insScore, delScore;
// Remember mapping:
// scoreMat[i+1][j+1] = position to fill
// scoreMat[i+1][j] ==> back one column
// scoreMat[i][j+1] ==> up one row.
matchScore = scoreMat[i][j] + scoreFn.Match(leftTarget, j, query, i);
insScore = scoreMat[i][j+1] + scoreFn.Insertion(leftTarget, j, query, i);
delScore = scoreMat[i+1][j] + scoreFn.Deletion(leftTarget, j, query, i);
int minScore = min(matchScore, min(insScore, delScore));
scoreMat[i+1][j+1] = minScore;
// set path.
if (matchScore == minScore) {
pathMat[i+1][j+1] = Diagonal;
}
else if (insScore == minScore) {
pathMat[i+1][j+1] = Up;
}
else {
assert(delScore == minScore);
pathMat[i+1][j+1] = Left;
}
//
// Next, assign the affine score
//
if (affineScoreMat[i+1][j] < scoreMat[i+1][j+1]) {
affineScoreMat[i+1][j+1] = affineScoreMat[i+1][j];
affinePathMat[i+1][j+1] = Left;
}
else {
// Allow free gap open... maybe this will change.
affineScoreMat[i+1][j+1] = scoreMat[i+1][j+1];
affinePathMat[i+1][j+1] = AffineDelOpen;
}
}
//
// Now align the right target, allowing a jump over the divide.
//
int affineCloseScore;
j = 0;
//
// A match here may only be preceded by an affine gap close.
//
int matchScore, delScore, insScore, minScore;
matchScore = affineScoreMat[i][leftTarget.length] + scoreFn.Match(rightTarget, j, query, i);
//
// Cannot have a non-affine deletion here.
delScore = INT_MAX;
//
// The insertion is a horizontal move, so that is all allowed.
//
insScore = scoreFn.Insertion(rightTarget, j, query, i - 1);
minScore = min(matchScore, insScore);
UInt targetCol = leftTarget.length;
assert(scoreMat[i+1][targetCol+1] == 0);
assert(pathMat[i+1][targetCol+1] == NoArrow);
scoreMat[i+1][targetCol+1] = minScore;
if (minScore == matchScore) {
pathMat[i+1][targetCol+1] = AffineLongDelClose;
}
else {
assert(minScore == insScore);
pathMat[i+1][targetCol+1] = Up;
}
//
// The affine matrix on the right side can only progress forward.
//
affineScoreMat[i+1][targetCol+1] = affineScoreMat[i+1][targetCol];
affinePathMat[i+1][targetCol+1] = AffineLongDelLeft;
for (j = 1; j < rightTarget.length; j++) {
targetCol = leftTarget.length + j;
matchScore = scoreMat[i][targetCol] + scoreFn.Match(rightTarget, j, query, i);
insScore = scoreMat[i][targetCol+1] + scoreFn.Insertion(rightTarget, j, query, i);
delScore = scoreMat[i+1][targetCol] + scoreFn.Deletion(rightTarget, j, query, i);
affineCloseScore = affineScoreMat[i][targetCol] + scoreFn.Match(rightTarget, j, query, i);
minScore = min(matchScore, min(insScore, min(delScore, affineCloseScore)));
scoreMat[i+1][targetCol+1] = minScore;
if (minScore == matchScore) {
pathMat[i+1][targetCol+1] = Diagonal;
}
else if (minScore == insScore) {
pathMat[i+1][targetCol+1] = Up;
}
else if (minScore == delScore) {
pathMat[i+1][targetCol+1] = Left;
}
else {
assert(minScore == affineCloseScore);
pathMat[i+1][targetCol+1] = AffineLongDelClose;
}
//
// As with before, the affine matrix on the right side can
// only progress forward.
//
affineScoreMat[i+1][targetCol+1] = affineScoreMat[i+1][targetCol];
affinePathMat[i+1][targetCol+1] = Left;
} // done aligning right target
} // done aligning full query
//
// Now build the alignment string
//
i = nQueryRows - 1;
j = nTargetCols - 1;
vector<Arrow> optAlignment;
int REGULAR = 0;
int AFFINE = 1;
int curMatrix = REGULAR;
Arrow arrow;
/*
cout << "score " << endl;
PrintFlatMatrix(scoreMat.matrix, scoreMat.nRows, scoreMat.nCols, cout, 3);
cout << "path " << endl;
PrintFlatMatrix(pathMat.matrix, scoreMat.nRows, scoreMat.nCols, cout, 3);
cout << "affine score " << endl;
PrintFlatMatrix(affineScoreMat.matrix, scoreMat.nRows, scoreMat.nCols, cout, 3);
cout << "affine path " << endl;
PrintFlatMatrix(affinePathMat.matrix, scoreMat.nRows, scoreMat.nCols, cout, 3);
*/
int optScore = scoreMat[i][j];
while (i > 0 or j > 0 or curMatrix == AFFINE) {
if (curMatrix == REGULAR) {
arrow = pathMat[i][j];
if (arrow == Diagonal) {
optAlignment.push_back(arrow);
i--;
j--;
}
else if (arrow == Left) {
optAlignment.push_back(arrow);
j--;
}
else if (arrow == Up) {
optAlignment.push_back(arrow);
i--;
}
else if (arrow == AffineLongDelClose) {
optAlignment.push_back(Left);
j--;
i--;
curMatrix = AFFINE;
}
}
else {
// in affine matrix
arrow = affinePathMat[i][j];
if (arrow == Left or arrow == AffineLongDelLeft) {
optAlignment.push_back(arrow);
j--;
}
else if (arrow == AffineDelOpen) {
//
// no change in i nor j, and this does not result in an
// arrow.
// Drop down to the regular alignment matrix.
//
curMatrix = REGULAR;
}
}
assert(arrow != NoArrow);
//
// Check for wrap around.
//
assert(i != UINT_MAX);
assert(j != UINT_MAX);
} // done tracing alignment path.
std::reverse(optAlignment.begin(), optAlignment.end());
alignment.LongGapArrowPathToAlignment(optAlignment, distanceBetweenLeftAndRightTarget);
return optScore;
}
//
// Create a version that does not need reusable mapping buffers.
//
template<typename T_QuerySequence, typename T_RefSequence, typename T_ScoreFunction>
int OneGapAlign(T_QuerySequence &query,
T_RefSequence &leftTarget,
T_RefSequence &rightTarget,
DNALength distanceBetweenLeftAndRightTarget,
T_ScoreFunction &scoreFn,
Alignment &alignment) {
FlatMatrix2D<int> scoreMat;
FlatMatrix2D<int> affineScoreMat;
FlatMatrix2D<Arrow> pathMat;
FlatMatrix2D<Arrow> affinePathMat;
return OneGapAlign(query, leftTarget, rightTarget,
distanceBetweenLeftAndRightTarget,
scoreFn,
alignment,
scoreMat, pathMat,
affineScoreMat, affinePathMat);
}
template<typename T_QuerySequence, typename T_RefSequence, typename T_ScoreFunction, typename T_BufferList>
int OneGapAlign(T_QuerySequence &query,
T_RefSequence &leftTarget,
T_RefSequence &rightTarget,
DNALength distanceBetweenLeftAndRightTarget,
T_ScoreFunction &scoreFn,
T_BufferList &buffers,
Alignment &alignment) {
return OneGapAlign(query, leftTarget, rightTarget, distanceBetweenLeftAndRightTarget, alignment,
buffers.scoreMat, buffers.pathMat,
buffers.affineScoreMat, buffers.affinePathMat);
}
template<typename T_QuerySequence, typename T_RefSequence, typename T_ScoreFunction, typename T_BufferList>
int OneGapAlign(T_QuerySequence &query,
T_RefSequence &reference,
T_ScoreFunction &scoreFunction,
T_BufferList &buffers,
Alignment &alignment) {
T_RefSequence leftReference, rightReference;
UInt leftReferenceLength = min(reference.length, query.length);
leftReference.ReferenceSubstring(reference, 0, leftReferenceLength);
UInt rightReferenceLength = min(reference.length - leftReferenceLength, query.length);
rightReference.ReferenceSubstring(reference, reference.length - rightReferenceLength, rightReferenceLength);
DNALength distanceBetweenLeftAndRight = reference.length - rightReferenceLength - leftReferenceLength;
assert(distanceBetweenLeftAndRight >= 0);
return OneGapAlign(query,
leftReference,
rightReference,
distanceBetweenLeftAndRight,
scoreFunction, alignment);
}
#endif // _BLASR_ONEGAP_ALIGNMENT_HPP_
|