/usr/include/blasr/algorithms/alignment/SWAlignImpl.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 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | #include <algorithm>
#include <vector>
#include <stdint.h>
#include <iostream>
#include <ostream>
#include "Types.h"
#include "defs.h"
#include "DNASequence.hpp"
#include "matrix/FlatMatrix.hpp"
#include "datastructures/alignment/Path.h"
#include "datastructures/alignment/AlignmentMap.hpp"
#include "datastructures/alignment/AlignmentStats.hpp"
#include "datastructures/alignment/Alignment.hpp"
#include "AlignmentUtils.hpp"
#include "SWAlign.hpp"
template<typename T_QuerySequence, typename T_TargetSequence, typename T_Alignment, typename T_ScoreFn>
int SWAlign(T_QuerySequence &qSeq, T_TargetSequence &tSeq,
std::vector<int> &scoreMat,
std::vector<Arrow> &pathMat,
T_Alignment &alignment,
T_ScoreFn &scoreFn,
AlignmentType alignType,
bool trustSequences,
bool printMatrix
) {
VectorIndex nRows = qSeq.length + 1;
VectorIndex nCols = tSeq.length + 1;
VectorIndex totalMatSize = nRows * nCols;
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 boundary conditions.
//
int r = 0, c = 0;
if (alignType == Global or alignType == ScoreGlobal
or alignType == FrontAnchored or alignType == ScoreFrontAnchored) {
//
// Global alignments penalize gaps at the beginning of both
// sequences.
//
for (c = 0; c < (int) tSeq.length + 1; c++ ){
scoreMat[rc2index(0, c, tSeq.length + 1)] = scoreFn.del * c;
pathMat[rc2index(0, c, tSeq.length + 1)] = Left;
}
for (r = 0; r < (int) qSeq.length + 1; r++ ){
scoreMat[rc2index(r,0, tSeq.length + 1)] = scoreFn.ins * r;
pathMat[rc2index(r, 0, tSeq.length + 1)] = Up;
}
}
else if (alignType == Local or alignType == ScoreLocal or alignType == LocalBoundaries
// end anchoring requires free gap penalties at the
// beginning of sequences.
or alignType == EndAnchored or alignType == ScoreEndAnchored) {
//
// Local alignments may shave off the beginning of either read.
// No penalties at the starts of reads.
//
for (c = 0; c < (int) tSeq.length + 1; c++ ){
scoreMat[rc2index(0, c, tSeq.length + 1)] = 0;
pathMat[rc2index(0, c, tSeq.length + 1)] = NoArrow;
}
for (r = 0; r < (int) qSeq.length + 1; r++ ){
scoreMat[rc2index(r,0, tSeq.length + 1)] = 0;
pathMat[rc2index(r, 0, tSeq.length + 1)] = NoArrow;
}
}
else if (alignType == QueryFit or alignType == ScoreQueryFit) {
//
// Query fit allows free gaps at the beginning and end
// of the target sequence.
//
for (c = 0; c < (int) tSeq.length + 1; c++ ){
scoreMat[rc2index(0, c, tSeq.length + 1)] = 0;
pathMat[rc2index(0, c, tSeq.length + 1)] = Left;
}
for (r = 0; r < (int) qSeq.length + 1; r++ ){
scoreMat[rc2index(r,0, tSeq.length + 1)] = scoreFn.ins * r;
pathMat[rc2index(r, 0, tSeq.length + 1)] = Up;
}
}
else if (alignType == TargetFit or alignType == ScoreTargetFit) {
//
// Query fit allows free gaps at the beginning and end
// of the target sequence.
//
for (c = 0; c < (int) tSeq.length + 1; c++ ){
scoreMat[rc2index(0, c, tSeq.length + 1)] = scoreFn.del * c;
pathMat[rc2index(0, c, tSeq.length + 1)] = Left;
}
for (r = 0; r < (int) qSeq.length + 1; r++ ){
scoreMat[rc2index(r,0, tSeq.length + 1)] = 0;
pathMat[rc2index(r, 0, tSeq.length + 1)] = Up;
}
}
else if (alignType == Overlap or alignType == ScoreOverlap or
alignType == TSuffixQPrefix or alignType == ScoreTSuffixQPrefix) {
//
// Overlap alignments allow a gap at the beginning of the
// query, and at the end of the target.
//
for (r = 0; r < (int) qSeq.length + 1; r++ ){
scoreMat[rc2index(r,0, tSeq.length + 1)] = scoreFn.ins*r;
pathMat[rc2index(r, 0, tSeq.length + 1)] = Up;
}
for (c = 0; c < (int) tSeq.length + 1; c++ ){
scoreMat[rc2index(0, c, tSeq.length + 1)] = 0;
pathMat[rc2index(0, c, tSeq.length + 1)] = Left;
}
}
else if (alignType == TPrefixQSuffix or alignType == ScoreTPrefixQSuffix) {
//
// Overlap alignments allow a gap at the beginning of the
// query, and at the end of the target.
//
for (c = 0; c < (int) tSeq.length + 1; c++ ){
scoreMat[rc2index(0, c, tSeq.length + 1)] = scoreFn.del * c;
pathMat[rc2index(0, c, tSeq.length + 1)] = Left;
}
for (r = 0; r < (int) qSeq.length + 1; r++ ){
scoreMat[rc2index(r,0, tSeq.length + 1)] = 0;
pathMat[rc2index(r, 0, tSeq.length + 1)] = Up;
}
}
pathMat[0] = Diagonal;
int match, qGap, tGap;
//
// Begin matrix pointers after the
int *matchScorePtr = &scoreMat[0];
int *gapQScorePtr = &scoreMat[1];
int *gapTScorePtr = &scoreMat[tSeq.length + 1 ];
int *curScorePtr = &scoreMat[tSeq.length + 2 ];
Arrow *optPathPtr = &pathMat[tSeq.length + 2];
int minScore;
int localMinScore = 0;
int localMinRow = 0;
int localMinCol = 0;
for (r = 0; r < (int) qSeq.length; r++ ){
for (c = 0; c < (int) tSeq.length; c++ ) {
//
// r+1, c+1 is the current row /col in the score and path mat.
//
//match = matchMat[TwoBit[qSeq.seq[r]]][TwoBit[tSeq.seq[c]]] + *matchScorePtr;
// qGap = *gapQScorePtr + gap;
// tGap = *gapTScorePtr + gap;
match = scoreFn.Match(tSeq, c, qSeq, r) + scoreMat[rc2index(r,c,nCols)];
qGap = scoreMat[rc2index(r,c+1, nCols)] + scoreFn.Insertion(tSeq, r+1, qSeq, c);
tGap = scoreMat[rc2index(r+1,c, nCols)] + scoreFn.Deletion(tSeq, r, qSeq, c+1);
minScore = MIN(match, MIN(qGap, tGap));
if (minScore < localMinScore) {
localMinScore = minScore;
localMinRow = r;
localMinCol = c;
}
if (minScore > 0 and
(alignType == Local or alignType == ScoreLocal or
alignType == LocalBoundaries or
alignType == EndAnchored or alignType == ScoreEndAnchored )) {
*curScorePtr = 0;
*optPathPtr = NoArrow;
}
// This staement will get easier when the alignTypes are bitfields.
// Not sure why this explicitly checks all conditions.
else if (alignType == Local or alignType == Global or
alignType == QueryFit or alignType == Overlap or
alignType == TargetFit or alignType == ScoreTargetFit or
alignType == ScoreLocal or alignType == ScoreGlobal or
alignType == ScoreQueryFit or alignType == ScoreOverlap or
alignType == FrontAnchored or alignType == ScoreFrontAnchored or
alignType == EndAnchored or alignType == ScoreEndAnchored or
alignType == LocalBoundaries or
alignType == TPrefixQSuffix or alignType == ScoreTPrefixQSuffix or
alignType == TSuffixQPrefix or alignType == ScoreTSuffixQPrefix ) {
*curScorePtr = minScore;
// scoreMat[rc2index(r+1,c+1, tl)] = minScore;
if (minScore == match) {
*optPathPtr = Diagonal;
//pathMat[rc2index(r+1,c+1,tl)] = Diagonal;
}
else if (minScore == qGap) {
*optPathPtr = Up;
//pathMat[rc2index(r+1,c+1, tl)] = Up;
}
else if (minScore == tGap) {
*optPathPtr = Left;
//pathMat[rc2index(r+1,c+1, tl)] = Left;
}
}
++matchScorePtr;
++gapTScorePtr;
++gapQScorePtr;
++curScorePtr;
++optPathPtr;
}
// Done processing a row.
// This leaves the pointers starting at the first column in the next row
// which is a boundary column. Advance one more.
//
++matchScorePtr;
++gapTScorePtr;
++gapQScorePtr;
++curScorePtr;
++optPathPtr;
}
//
// Now trace back in the pairwise alignment.
//
// The location of the trace back depends on the type of alignment that is done.
int minRow = 0, minCol = 0;
if (alignType == Global or alignType == ScoreGlobal or
alignType == EndAnchored or alignType == ScoreEndAnchored ) {
// start at bottom right of matrix.
r = qSeq.length;
c = tSeq.length;
minRow = r;
minCol = c;
}
else if (alignType == Local or alignType == ScoreLocal or
alignType == FrontAnchored or alignType == ScoreFrontAnchored or
alignType == LocalBoundaries) {
// start at cell that gives the highest score.
r = localMinRow;
c = localMinCol;
minRow = r;
minCol = c;
}
else if (alignType == QueryFit or alignType == Overlap or
alignType == ScoreQueryFit or alignType == ScoreOverlap) {
// Start at the point at the end of the target that gives the highest score, but has the
// end query sequence alignment.
r = nRows-1;
int minScore = scoreMat[rc2index(nRows-1, 1, nCols)];
minCol = 1;
for (c = 2; c < (int) nCols; c++ ) {
if (scoreMat[rc2index(nRows-1, c, nCols)] < minScore) {
minScore = scoreMat[rc2index(nRows-1, c, nCols)];
minCol = c;
}
}
c = minCol;
minRow = nRows - 1;
}
else if (alignType == TargetFit or alignType == ScoreTargetFit) {
// Start at the point at the end of the target that gives the highest score, but has the
// end query sequence alignment.
//
// Always trace back from the end of the target.
//
minCol = nCols-1;
c = nCols-1;
r = 0;
int minScore = scoreMat[rc2index(1, nCols-1, nCols)];
for (r = 2; r < (int) nRows; r++ ) {
if (scoreMat[rc2index(r, nCols-1, nCols)] < minScore) {
minScore = scoreMat[rc2index(r, nCols-1, nCols)];
minRow = r;
}
}
// store where to trace back from in the query.
r = minRow;
}
else if (alignType == TSuffixQPrefix or alignType == ScoreTSuffixQPrefix) {
// Start at the point at the end of the target that gives the highest score, but has the
// end query sequence alignment.
c = nCols - 1;
r = 1;
int minScore = scoreMat[rc2index(1, nCols-1, nCols)];
minRow = 1;
for (r = 2; r < (int) nRows; r++) {
if (scoreMat[rc2index(r, nCols-1, nCols)] < minScore) {
minScore = scoreMat[rc2index(r, nCols-1, nCols)];
minRow = r;
}
}
r = minRow;
minCol = nCols - 1;
}
else if (alignType == TPrefixQSuffix or alignType == ScoreTPrefixQSuffix) {
r = nRows-1;
int minScore = scoreMat[rc2index(nRows-1, 1, nCols)];
minCol = 1;
for (c = 2; c < (int) nCols; c++ ) {
if (scoreMat[rc2index(nRows-1, c, nCols)] < minScore) {
minScore = scoreMat[rc2index(nRows-1, c, nCols)];
minCol = c;
}
}
c = minCol;
minRow = nRows - 1;
}
/*
PrintFlatMatrix(&scoreMat[0], nRows, nCols, cout);
PrintFlatMatrix(&pathMat[0], nRows,nCols, cout);
*/
if (alignType != ScoreGlobal and
alignType != ScoreLocal and
alignType != ScoreQueryFit and
alignType != ScoreOverlap and
alignType != ScoreTPrefixQSuffix and
alignType != ScoreTSuffixQPrefix) {
std::vector<Arrow> optAlignment;
Arrow arrow;
while (((alignType == Global or alignType == FrontAnchored ) and (r > 0 or c > 0)) or // global alignment stops at top corner
((alignType == QueryFit or
alignType == Overlap or
alignType == TSuffixQPrefix) and r > 0) or
((alignType == TPrefixQSuffix) and c > 0) or
(alignType == TargetFit and c > 0) or
// local alignment stops at top corner -or- when new local alignment started.
((alignType == Local or alignType == EndAnchored or alignType == LocalBoundaries)
and r > 0 and c > 0 and pathMat[r*nCols+c] != NoArrow)
) {
arrow = pathMat[rc2index(r, c, nCols)];
//
// When the alignment type is localBoundaries, it is not necessary to store
// the actual alignment. Only the starting positions and lengts will be stored.
//
if (alignType != LocalBoundaries) {
optAlignment.push_back(arrow);
}
if (arrow == Diagonal) {
r--;
c--;
}
else if (arrow == Up) {
r--;
}
else if (arrow == Left) {
c--;
}
}
// remove the boundary condition that is added for global alignment.
if (alignType == LocalBoundaries and alignType != Local and alignType != EndAnchored and optAlignment.size() > 0)
optAlignment.pop_back();
if (optAlignment.size() > 1)
std::reverse(optAlignment.begin(), optAlignment.end());
if (optAlignment.size() > 0)
alignment.ArrowPathToAlignment(optAlignment);
//
// If running a local alignment, the alignment does not
// explicityly encode the gaps at the beginning and ending of the
// alignment. These are stored in the qPos and tPos fields.
//
if (alignType == TSuffixQPrefix or alignType == TPrefixQSuffix) {
alignment.qPos = r;
alignment.tPos = c;
}
else if (alignType == Local or alignType == EndAnchored or alignType == LocalBoundaries) {
alignment.qPos = r;
alignment.tPos = c;
alignment.qLength = localMinRow - alignment.qPos + 1;
alignment.tLength = localMinCol - alignment.tPos + 1;
}
else if (alignType == QueryFit or alignType == TargetFit) {
alignment.qPos = r;
alignment.tPos = c;
}
}
if (printMatrix) {
PrintFlatMatrix( &scoreMat[0], qSeq.length + 1, tSeq.length + 1, std::cout);
std::cout << std::endl;
PrintFlatMatrix( &pathMat[0], qSeq.length + 1, tSeq.length + 1, std::cout);
}
return scoreMat[rc2index(minRow, minCol, nCols)];
}
|