-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBandedSmithWaterman.H
381 lines (335 loc) · 10.8 KB
/
BandedSmithWaterman.H
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
/****************************************************************
BandedSmithWaterman.H
BOOM : Bioinformatics Object Oriented Modules
Copyright (C)2012 William H. Majoros ([email protected]).
This is OPEN SOURCE SOFTWARE governed by the Gnu General Public
License (GPL) version 3, as described at www.opensource.org.
Implements the Smith-Waterman global alignment algorithm. The
matrix is banded, so that both time and space are conserved by
not allocating & evaluating the whole matrix.
****************************************************************/
#ifndef INCL_BandedSmithWaterman_H
#define INCL_BandedSmithWaterman_H
#include "Alignment.H"
#include "BandedAlignmentMatrix.H"
#include "SubstitutionMatrix.H"
template<class FLT_TYP>
class BandedSmithWaterman
{
public:
BandedSmithWaterman(const Alphabet &,const Sequence &,const Sequence &,
const SubstitutionMatrix<FLT_TYP> &,
FLT_TYP gapOpenPenalty,FLT_TYP gapExtendPenalty,
int bandWidth);
Alignment *fullAlignment();
static int recommendBandWidth(int maxBytes,int xDim);
enum Direction {LEFT,DOWN,DIAGONAL};
private:
struct Cell
{
FLT_TYP scores[3];
Direction links[3];
Direction bestDir();
};
const Alphabet &alphabet;
const Sequence &s1, &s2;
BandedAlignmentMatrix<Cell> alignmentMatrix; // M[column][row]
const SubstitutionMatrix<FLT_TYP> &substitutionMatrix;
const FLT_TYP gapOpenPenalty, gapExtendPenalty;
Alignment *getAlignment(int finalRow,int finalColumn);
Alignment *align(FLT_TYP finalOpenPenalty,
FLT_TYP finalExtendPenalty);
void linkBack(Cell &thisCell,Cell *leftCell,Cell *downCell,
Cell *diagonalCell,FLT_TYP gapOpenPenalty,
FLT_TYP gapExtendPenalty,int x,int y);
void followLink(Direction,int &x,int &y);
};
template<class FLT_TYP>
ostream &operator<<(ostream &,
typename BandedSmithWaterman<FLT_TYP>::Direction);
#include "Stack.H"
#include "Constants.H"
template<class FLT_TYP>
BandedSmithWaterman<FLT_TYP>::BandedSmithWaterman(const Alphabet &alphabet,
const Sequence &s1,
const Sequence &s2,
const SubstitutionMatrix<FLT_TYP> &subst,
FLT_TYP gapOpenPenalty,
FLT_TYP gapExtendPenalty,
int bandWidth)
: s1(s1), s2(s2), alphabet(alphabet),
alignmentMatrix(s1.getLength()+1,s2.getLength()+1,bandWidth),
gapOpenPenalty(gapOpenPenalty), gapExtendPenalty(gapExtendPenalty),
substitutionMatrix(subst)
{
if(s1.getLength()<1 || s2.getLength()<1)
throw "Aligning to a zero-length sequence in BandedSmithWaterman::\
BandedSmithWaterman";
if(bandWidth<0) throw "negative bandwidth";
if(bandWidth>s1.getLength()) bandWidth=s1.getLength();
if(bandWidth>s2.getLength()) bandWidth=s2.getLength();
//cout<<"matrix size: "<<alignmentMatrix.getMatrixSize()<<" bytes"<<endl;
}
template<class FLT_TYP>
typename BandedSmithWaterman<FLT_TYP>::Direction
BandedSmithWaterman<FLT_TYP>::Cell::bestDir()
{
FLT_TYP left=scores[BandedSmithWaterman<FLT_TYP>::LEFT];
FLT_TYP down=scores[BandedSmithWaterman<FLT_TYP>::DOWN];
FLT_TYP diag=scores[BandedSmithWaterman<FLT_TYP>::DIAGONAL];
if(left>down)
if(left>diag)
return BandedSmithWaterman<FLT_TYP>::LEFT;
else
return BandedSmithWaterman<FLT_TYP>::DIAGONAL;
else
if(down>diag)
return BandedSmithWaterman<FLT_TYP>::DOWN;
else
return BandedSmithWaterman<FLT_TYP>::DIAGONAL;
}
template<class FLT_TYP>
ostream &operator<<(ostream &os,
typename BandedSmithWaterman<FLT_TYP>::Direction dir)
{
switch(dir)
{
case BandedSmithWaterman<FLT_TYP>::LEFT:
os<<"LEFT"<<endl;
break;
case BandedSmithWaterman<FLT_TYP>::DOWN:
os<<"DOWN"<<endl;
break;
case BandedSmithWaterman<FLT_TYP>::DIAGONAL:
os<<"DIAGONAL"<<endl;
break;
}
}
template<class FLT_TYP>
Alignment *BandedSmithWaterman<FLT_TYP>::getAlignment(int x,int y)
{
// Trace back from the end cell along the optimal path to
// the origin in the lower left corner of the DP matrix
Cell *cell;
if(!alignmentMatrix.getCell(x,y,cell)) throw "BandedSmithWaterman: bad!";
Direction dir=cell->bestDir();
FLT_TYP alignmentScore=cell->scores[dir];
Stack<Direction> pathStack;
while(x!=0 || y!=0)
{
if(!alignmentMatrix.getCell(x,y,cell))
throw "BandedSmithWaterman: bad! (2)";
pathStack.push(dir);
followLink(dir,x,y);
dir=cell->links[dir];
}
// Reverse the stack contents to produce a forward-oriented
// alignment
Alignment &alignment=*new Alignment(s1,s2,alphabet,alignmentScore);
const int len=pathStack.size();
for(int i=0 ; i<len ; ++i)
{
Direction dir=pathStack.pop();
switch(dir)
{
case LEFT:
alignment+=FIRST_UNMATCHED;
break;
case DOWN:
alignment+=SECOND_UNMATCHED;
break;
case DIAGONAL:
alignment+=MATCH;
break;
}
}
return &alignment;
}
template<class FLT_TYP>
Alignment *BandedSmithWaterman<FLT_TYP>::fullAlignment()
{
return align(gapOpenPenalty,gapExtendPenalty);
}
template<class FLT_TYP>
Alignment *BandedSmithWaterman<FLT_TYP>::align(FLT_TYP finalOpenPenalty,
FLT_TYP finalExtendPenalty)
{
int matrixWidth=alignmentMatrix.getFirstDim();
int matrixHeight=alignmentMatrix.getSecondDim();
// Initialize the bottom row and leftmost column of the dynamic
// programming matrix
Cell *origin;
if(!alignmentMatrix.getCell(0,0,origin)) throw "BandedSmithWaterman";
for(int i=0 ; i<3 ; ++i) origin->scores[i]=0;
for(int i=1 ; i<matrixWidth ; ++i)
{
Cell *bottomCell;
if(!alignmentMatrix.getCell(i,0,bottomCell)) continue;
bottomCell->links[LEFT]=LEFT;
Cell *left;
if(!alignmentMatrix.getCell(i-1,0,left)) throw "BandedSW";
bottomCell->scores[LEFT]=left->scores[LEFT]+gapExtendPenalty;
bottomCell->scores[DOWN]=
bottomCell->scores[DIAGONAL]=NEGATIVE_INFINITY;
}
for(int i=1 ; i<matrixHeight ; ++i)
{
Cell *leftCell;
if(!alignmentMatrix.getCell(0,i,leftCell)) continue;
leftCell->links[DOWN]=DOWN;
Cell *down;
if(!alignmentMatrix.getCell(0,i-1,down)) throw "BandedSmith-W";
leftCell->scores[DOWN]=down->scores[DOWN]+gapExtendPenalty;
leftCell->scores[LEFT]=leftCell->scores[DIAGONAL]=NEGATIVE_INFINITY;
}
// Fill out the main portion of the matrix
int widthMinusOne=matrixWidth-1, heightMinusOne=matrixHeight-1;
for(int x=1 ; x<widthMinusOne ; ++x)
for(int y=1 ; y<heightMinusOne ; ++y)
{
Cell *thisCell;
if(!alignmentMatrix.getCell(x,y,thisCell)) continue;
Cell *leftCell=NULL, *downCell=NULL, *diagonalCell=NULL;
alignmentMatrix.getCell(x-1,y,leftCell);
alignmentMatrix.getCell(x,y-1,downCell);
alignmentMatrix.getCell(x-1,y-1,diagonalCell);
linkBack(*thisCell,leftCell,downCell,diagonalCell,
gapOpenPenalty,gapExtendPenalty,x,y);
}
// Finish up by filling out the top row and rightmost column
// (these use a *potentially* different set of gap penalties to
// accommodate "partial" alignments)
for(int i=1 ; i<matrixWidth ; ++i)
{
Cell *topCell;
if(!alignmentMatrix.getCell(i,heightMinusOne,topCell)) continue;
Cell *topCellLeft=NULL, *topCellDown=NULL, *topCellDiagonal=NULL;
alignmentMatrix.getCell(i-1,heightMinusOne,topCellLeft);
alignmentMatrix.getCell(i,heightMinusOne-1,topCellDown);
alignmentMatrix.getCell(i-1,heightMinusOne-1,topCellDiagonal);
linkBack(*topCell,topCellLeft,topCellDown,topCellDiagonal,
finalOpenPenalty,finalExtendPenalty,i,heightMinusOne);
}
for(int i=1 ; i<matrixHeight ; ++i)
{
Cell *rightmostCell;
if(!alignmentMatrix.getCell(widthMinusOne,i,rightmostCell)) continue;
Cell *rightmostCellLeft=NULL, *rightmostCellDown=NULL,
*rightmostCellDiagonal=NULL;
alignmentMatrix.getCell(widthMinusOne-1,i,rightmostCellLeft);
alignmentMatrix.getCell(widthMinusOne,i-1,rightmostCellDown);
alignmentMatrix.getCell(widthMinusOne-1,i-1,rightmostCellDiagonal);
linkBack(*rightmostCell,rightmostCellLeft,rightmostCellDown,
rightmostCellDiagonal,finalOpenPenalty,finalExtendPenalty,
widthMinusOne,i);
}
return getAlignment(widthMinusOne,heightMinusOne);
}
/************************************************************************
This method handles the actual linking back of one cell to a predecessor
cell, either immediately left, below, or diagonally of this cell. Note
that we do not allow insertions directly next to deletions -- at least
one match must come between adjacent insertions and deletions, although
this could could easily be changed to relax this restriction.
*/
template<class FLT_TYP>
void BandedSmithWaterman<FLT_TYP>::linkBack(Cell &thisCell,
Cell *leftCell,
Cell *downCell,
Cell *diagonalCell,
FLT_TYP gapOpenPenalty,
FLT_TYP gapExtendPenalty,
int x,int y)
{
// LEFT
FLT_TYP leftLeft, leftDiag;
if(leftCell)
{
leftLeft=leftCell->scores[LEFT]+gapExtendPenalty;
leftDiag=leftCell->scores[DIAGONAL]+gapOpenPenalty;
}
else
leftLeft=leftDiag=NEGATIVE_INFINITY;
if(leftLeft>leftDiag)
{
thisCell.scores[LEFT]=leftLeft;
thisCell.links[LEFT]=LEFT;
}
else
{
thisCell.scores[LEFT]=leftDiag;
thisCell.links[LEFT]=DIAGONAL;
}
// DOWN
FLT_TYP downDown, downDiag;
if(downCell)
{
downDown=downCell->scores[DOWN]+gapExtendPenalty;
downDiag=downCell->scores[DIAGONAL]+gapOpenPenalty;
}
else
downDown=downDiag=NEGATIVE_INFINITY;
if(downDown>downDiag)
{
thisCell.scores[DOWN]=downDown;
thisCell.links[DOWN]=DOWN;
}
else
{
thisCell.scores[DOWN]=downDiag;
thisCell.links[DOWN]=DIAGONAL;
}
// DIAGONAL
FLT_TYP matchScore=substitutionMatrix(s1[x-1],s2[y-1]);
FLT_TYP diagLeft, diagDown, diagDiag;
if(diagonalCell)
{
diagLeft=diagonalCell->scores[LEFT]+matchScore;
diagDown=diagonalCell->scores[DOWN]+matchScore;
diagDiag=diagonalCell->scores[DIAGONAL]+matchScore;
}
else
diagLeft=diagDown=diagDiag=NEGATIVE_INFINITY;
if(diagLeft>diagDown)
if(diagLeft>diagDiag)
{ // left is best
thisCell.scores[DIAGONAL]=diagLeft;
thisCell.links[DIAGONAL]=LEFT;
}
else goto DIAGDIAG;
else
if(diagDown>diagDiag)
{ // down is best
thisCell.scores[DIAGONAL]=diagDown;
thisCell.links[DIAGONAL]=DOWN;
}
else // diagonal is best
{
DIAGDIAG:
thisCell.scores[DIAGONAL]=diagDiag;
thisCell.links[DIAGONAL]=DIAGONAL;
}
}
template<class FLT_TYP>
void BandedSmithWaterman<FLT_TYP>::followLink(Direction dir,int &x,int &y)
{
switch(dir)
{
case LEFT:
--x;
break;
case DOWN:
--y;
break;
case DIAGONAL:
--x;
--y;
break;
}
}
template<class FLT_TYP>
int BandedSmithWaterman<FLT_TYP>::recommendBandWidth(int maxBytes,int xDim)
{
return BandedAlignmentMatrix<Cell>::recommendBandWidth(maxBytes,xDim);
}
#endif