-
Notifications
You must be signed in to change notification settings - Fork 4
/
pars_align_gapp_seq.h
463 lines (326 loc) · 10.3 KB
/
pars_align_gapp_seq.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
* Copyright (C) 2009-2012 Simon A. Berger
*
* This file is part of papara.
*
* papara is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* papara is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with papara. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstddef>
class pars_align_gapp_seq {
typedef unsigned int pars_state_t;
typedef double score_t;
const static score_t LARGE_VALUE; // = 32000; // score_t must be able to keep LARGE_VALUE + GAP_OPEN + GAP_EXTEND without overflowing!. waiting for c++0x to be able to use std::numeric_limits at compile-time...
const score_t GAP_OPEN; //= 1;
const score_t GAP_EXTEND; // = 1;
const score_t GAP_OPEN_EXTEND;
const score_t MISMATCH_PENALTY;// = 4;
const score_t MATCH_CGAP;
public:
bool g_dump;
enum {
BT_STAY = 0x1,
BT_STAY_L = 0x2,
BT_UP = 0x4
};
class arrays {
public:
size_t m_s;
std::vector<score_t> score;
std::vector<score_t> scoreL;
std::vector<uint8_t> dir;
bool m_haveDir;
arrays( bool haveDir = false ) : m_s(0), m_haveDir(haveDir) {
}
void size( size_t s ) {
if ( m_s < s ) {
std::cout << "arr resize: " << m_s << " -> " << s << "\n";
m_s = s;
score.resize(m_s);
scoreL.resize(m_s);
if ( m_haveDir ) {
dir.resize(m_s);
}
}
}
size_t size() {
return m_s;
}
};
size_t m_ncups;
private:
size_t m_na;
size_t m_nb;
size_t m_ma;
size_t m_mb;
size_t m_msize;
const int *m_a;
const unsigned char *m_b;
const double *m_gapp;
size_t m_aStride;
arrays &m_arr;
ptrdiff_t m_tbStartA;
ptrdiff_t m_tbStartB;
const unsigned int *m_bvtrans;
inline size_t addr( size_t a, size_t b ) {
#if 1
return a + b * m_ma;
#else
return b + a * m_mb;
#endif
}
inline size_t saddr( ptrdiff_t a, ptrdiff_t b ) {
return addr( a + 1, (b + 1));
}
// inline int xsaddr( int a, int b ) {
// return addr( a + 1, (b + 1) % 2 );
// }
inline pars_state_t getSeqA ( size_t a ) {
return pars_state_t(m_a[a * m_aStride]);
}
inline double get_gapp ( size_t a ) {
return m_gapp[a];
}
public:
pars_align_gapp_seq( const int* seqA, const unsigned char* seqB, size_t n_a, size_t n_b, size_t aStride, const double *a_gapp, size_t aAuxStride, arrays &arr, const unsigned int *bvtrans = 0,
score_t gapOpen = 1, score_t gapExtend = 1, score_t mismatch = 3, score_t matchCGap = 10 )
// : LARGE_VALUE(std::numeric_limits<score_t>::max() - 100)
//: LARGE_VALUE(32000),
;
virtual ~pars_align_gapp_seq() ;
// void alignFreeshiftS1 () {
//
//
//
// for ( int ia = 0; ia <= m_na - m_nb - 1; ia++ ) {
// m_arr->scoreL[saddr ( ia, -1 ) ] = 0;
// m_arr->score[saddr ( ia, -1 ) ] = 0;
// }
//
//
// m_arr->score[saddr ( -1, -1 ) ] = 0;
// m_arr->scoreL[saddr ( -1, -1 ) ] = 0;
//
// // NM += pa->lenB * (pa->lenA - pa->lenB);
//
//
// }
void alignFreeshiftS11 () ;
void tracebackCompressed( std::vector< uint8_t>& bvec ) ;
inline int alignFreeshiftS3Sep(int highCutoff) {
#define DUMP_MATRIX 1
bool g_dump = !true;
#define AUX_CGAP ( 0x1 )
score_t best = LARGE_VALUE;
const size_t band_width = m_na - m_nb;
score_t * __restrict sp = m_arr.score.data();
score_t * __restrict sLp = m_arr.scoreL.data();
uint8_t * __restrict dir = m_arr.dir.data();
// assert( dir != 0 );
const bool fill_dir = m_arr.m_haveDir;
if( fill_dir ) {
for ( size_t ib = 0; ib < m_nb; ib++ ) {
pars_state_t cb;// = m_b[ib];
if ( m_bvtrans == 0 ) {
cb = m_b[ib];
} else {
cb = m_bvtrans[m_b[ib]];
}
size_t astart = ib;
score_t last_sp = sp[saddr(astart - 1, ib)];
score_t last_sLp = sLp[saddr(astart - 1, ib)];
// int saddr_0_0 = saddr( astart, ib ); // score address of the current cell
// int saddr_1_1 = saddr( astart-1, ib-1 ); // score address of the upper-left-diagonal cell
// /* score_t *sp_0_0 = &sp[saddr_0_0];
// score_t *sp_1_1 = &sp[saddr_1_1];*/
for ( size_t ia = astart; ia <= ib + band_width; ia++ /*, saddr_0_0++, saddr_1_1++, sp_0_0++, sp_1_1++*/ ) {
const pars_state_t ca = getSeqA( ia );
const double p_nongap = get_gapp( ia );
const double p_gap = 1 - p_nongap;
// determine match or mis-match according to parsimony bits coming from the tree.
const bool match = ( ca & cb ) != 0;
const size_t addr = saddr( ia, ib );
// calculate match score ('go diagonal')
// penalize based on parsimony and gap probability
score_t sd = sp[saddr( ia-1, ib-1 )];
score_t su = sp[saddr( ia, ib-1 )];
su += GAP_OPEN_EXTEND;
if ( !match ) {
sd += MISMATCH_PENALTY;
}
score_t scoreExt = last_sLp + p_nongap * GAP_EXTEND;
score_t scoreOpen = last_sp + p_nongap * GAP_OPEN_EXTEND;
sd += MATCH_CGAP * p_gap;
//su += MATCH_CGAP * p_gap;
if ( scoreExt < scoreOpen ) {
if( fill_dir ) {
dir[addr] = BT_STAY_L;
}
last_sLp = scoreExt;
} else {
if( fill_dir ) {
dir[addr] = 0;
}
last_sLp = scoreOpen;
}
int dsd = 0;
if ( su < sd ) {
sd = su;
//dir[addr] |= BT_UP;
dsd = BT_UP;
} else {
dsd = BT_STAY;
}
if ( last_sLp < sd ) {
sp[addr] = last_sp = last_sLp;
} else {
sp[addr] = last_sp = sd;
if( fill_dir ) {
dir[addr] |= dsd;
}
}
// rowmin = std::min( last_sp, rowmin );
// ct++;
// m_ncups++;
}
// if ( rowmin >= highCutoff ) {
// return INT_MAX;
// }
}
if( g_dump ) {
for( int ib = -1; ib < int(m_nb); ib++ ) {
for( int ia = -1; ia < int(m_na); ia++ ) {
//printf( "\t%d", sp[saddr(ia, ib)] );
int d = 0;
if( fill_dir ) {
d = int(dir[saddr(ia,ib)]);
}
std::cout << std::setw(10) << sp[saddr(ia, ib)] << "," << d;
}
std::cout << "\n";
}
}
// printf( "ct: %zd\n", ct );
// exit(0);
m_tbStartB = m_nb - 1;
m_tbStartA = -1;
for ( size_t a = m_na - 1; a >= m_nb - 1; a-- ) {
score_t s = m_arr.score[saddr ( a, m_tbStartB ) ];
if ( s < best ) {
best = s;
m_tbStartA = a;
}
}
return int(m_arr.score[saddr ( m_tbStartA, m_tbStartB ) ]);
} else {
// back-port to sequential from align_pvec_vec.h
std::fill( sp, sp + m_na, 0 );
sp[band_width+1] = LARGE_VALUE;
for( size_t ib = 0; ib < m_nb; ib++ ) {
pars_state_t bc;
if ( m_bvtrans == 0 ) {
bc = m_b[ib];
} else {
bc = m_bvtrans[m_b[ib]];
}
double last_sl = LARGE_VALUE;
double last_sc = LARGE_VALUE;
double last_sdiag = 0;
size_t astart = ib;
double * __restrict s_iter = &sp[0];
double * __restrict s_iter_next = &sp[1];
last_sdiag = *s_iter;
for( size_t ia = astart; ia <= ib + band_width; ++ia, ++s_iter, ++s_iter_next ) {
const pars_state_t ac = getSeqA(ia);
const double p_nongap = get_gapp( ia );
const double p_gap = 1 - p_nongap;
const bool match = ( ac & bc ) != 0;
const double sm = ((!match) ? last_sdiag + MISMATCH_PENALTY : last_sdiag) + p_gap * MATCH_CGAP;
const double sl_ext = last_sl + p_nongap * GAP_EXTEND;
const double sl_open = last_sc + p_nongap * GAP_OPEN_EXTEND;
last_sl = std::min( sl_ext, sl_open );
const double min_sm_sl = std::min( sm, last_sl );
last_sdiag = *s_iter_next;
const double su = last_sdiag + GAP_OPEN_EXTEND;
const double sc = std::min( min_sm_sl, su );
last_sc = sc;
*s_iter = sc;
}
}
double minscore = LARGE_VALUE;
for( size_t i = 0; i < band_width + 1; ++i ) {
minscore = std::min( minscore, sp[i]);
}
return int(minscore);
// for ( size_t ib = 0; ib < m_nb; ib++ ) {
// pars_state_t cb;// = m_b[ib];
//
// if ( m_bvtrans == 0 ) {
// cb = m_b[ib];
// } else {
// cb = m_bvtrans[m_b[ib]];
// }
//
// size_t astart = ib;
// score_t last_sp = sp[saddr(astart - 1, ib)];
// score_t last_sLp = sLp[saddr(astart - 1, ib)];
//
// for ( size_t ia = astart; ia <= ib + band_width; ia++ /*, saddr_0_0++, saddr_1_1++, sp_0_0++, sp_1_1++*/ ) {
// const pars_state_t ca = getSeqA( ia );
//
// const double p_nongap = get_gapp( ia );
// const double p_gap = 1 - p_nongap;
//
// // determine match or mis-match according to parsimony bits coming from the tree.
// const bool match = ( ca & cb ) != 0;
// const size_t addr = saddr( ia, ib );
//
// // calculate match score ('go diagonal')
// // penalize based on parsimony and gap probability
//
// score_t sd = sp[saddr( ia-1, ib-1 )];
// score_t su = sp[saddr( ia, ib-1 )];
//
// su += GAP_OPEN_EXTEND;
// if ( !match ) {
// sd += MISMATCH_PENALTY;
// }
//
// score_t scoreExt = last_sLp + p_nongap * GAP_EXTEND;
// score_t scoreOpen = last_sp + p_nongap * GAP_OPEN_EXTEND;
//
// sd += MATCH_CGAP * p_gap;
// //su += MATCH_CGAP * p_gap;
//
// last_sLp = std::min( scoreExt, scoreOpen);
//
//
// int dsd = 0;
//
// sd = std::min( sd, su );
// sp[addr] = last_sp = std::min( last_sLp, sd );
//
// }
//
// }
}
}
inline int alignFreeshift(int highCutoff) {
alignFreeshiftS11();
// return alignFreeshiftS3(highCutoff);
return alignFreeshiftS3Sep(highCutoff);
}
};