-
Notifications
You must be signed in to change notification settings - Fork 1
/
gact.cpp
561 lines (503 loc) · 20.4 KB
/
gact.cpp
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/*
Copyright 2018 Yatish Turakhia, Gill Bejerano and William Dally
Copyright 2018 Tong Dong Qiu
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <cstdlib>
#include <queue>
#include <mutex>
#include "align.h"
#include "gact.h"
// only report overlaps with a score higher than SCORE_THRESHOLD
#define SCORE_THRESHOLD 0
#ifdef TIME
#include <chrono>
#endif
int sub_mat[25] = {
1, -1, -1, -1, 0,
-1, 1, -1, -1, 0,
-1, -1, 1, -1, 0,
-1, -1, -1, 1, 0,
0, 0, 0, 0, 0
};
// declared in reference_guided.cpp
extern bool same_file;
extern std::vector<std::string> reference_seqs;
extern std::vector<long long int> reference_lengths;
extern std::vector<std::string> reads_seqs;
extern std::vector<std::string> rev_reads_seqs;
extern std::vector<long long int> reads_lengths;
extern std::vector<std::vector<std::string> > reference_descrips;
extern std::vector<std::vector<std::string> > reads_descrips;
void GACT (char *ref_str, char *query_str, \
int ref_length, int query_length, \
int tile_size, int tile_overlap, \
int ref_pos, int query_pos, int first_tile_score_threshold, \
int ref_id, int query_id, bool complement, \
int match_score, int mismatch_score, \
int gap_open, int gap_extend, \
std::ofstream &fout) {
std::queue<int> BT_states;
// result of one alignment, note that is used to calculate the total score
std::string aligned_ref_str = "";
std::string aligned_query_str = "";
int ref_tile_length = tile_size;
int query_tile_length = tile_size;
int abpos, bbpos;
static int callidx = 0;
// beginning for the left or the right extension
//original starting point for the whole query
int rev_ref_pos = ref_pos;
int rev_query_pos = query_pos;
int i = 0;
int j = 0;
int first_tile_score = 0;
bool first_tile = true;
// not for the first tile
while ((ref_pos > 0) && (query_pos > 0) && (((i > 0) && (j > 0)) || first_tile)) {
//change the tile length if elements less than that of the tile size
ref_tile_length = (ref_pos > tile_size) ? tile_size : ref_pos;
query_tile_length = (query_pos > tile_size) ? tile_size : query_pos;
BT_states = AlignWithBT(\
ref_str + ref_pos - ref_tile_length, \
ref_tile_length, \
query_str + query_pos - query_tile_length, \
query_tile_length, \
match_score, mismatch_score, gap_open, gap_extend, \
query_tile_length, ref_tile_length, false, \
first_tile, (tile_size - tile_overlap));
i = 0;
j = 0;
int tile_score = BT_states.front();
BT_states.pop();
if (first_tile) {
ref_pos = ref_pos - ref_tile_length + BT_states.front();
BT_states.pop();
query_pos = query_pos - query_tile_length + BT_states.front();
BT_states.pop();
rev_ref_pos = ref_pos;
rev_query_pos = query_pos;
first_tile_score = tile_score;
if (tile_score < first_tile_score_threshold) {
break;
}
}
while (!BT_states.empty()) {
first_tile = false;
int state = BT_states.front();
BT_states.pop();
if (state == M) {
aligned_ref_str.insert(0, 1, ref_str[ref_pos - j - 1]);
aligned_query_str.insert(0, 1, query_str[query_pos - i - 1]);
i += 1;
j += 1;
}
if (state == I) {
aligned_ref_str.insert(0, 1, ref_str[ref_pos - j - 1]);
aligned_query_str.insert(0, 1, '-');
j += 1;
}
if (state == D) {
aligned_ref_str.insert(0, 1, '-');
aligned_query_str.insert(0, 1, query_str[query_pos - i - 1]);
i += 1;
}
}
ref_pos -= (j);
query_pos -= (i);
}
abpos = ref_pos;
bbpos = query_pos;
ref_pos = rev_ref_pos;
query_pos = rev_query_pos;
i = tile_size;
j = tile_size;
//starts with the first tile
while ((ref_pos < ref_length) && (query_pos < query_length) && (((i > 0) && (j > 0)) || first_tile)) {
ref_tile_length = (ref_pos + tile_size < ref_length) ? tile_size : ref_length - ref_pos;
query_tile_length = (query_pos + tile_size < query_length) ? tile_size : query_length - query_pos;
BT_states = AlignWithBT (\
ref_str + ref_pos, \
ref_tile_length, \
query_str + query_pos, \
query_tile_length, \
match_score, mismatch_score, gap_open, gap_extend, \
query_tile_length, ref_tile_length, true, \
first_tile, (tile_size - tile_overlap));
i = 0;
j = 0;
int tile_score = BT_states.front();
BT_states.pop();
if (first_tile) {
ref_pos = ref_pos + ref_tile_length - BT_states.front();
BT_states.pop();
query_pos = query_pos + query_tile_length - BT_states.front();
BT_states.pop();
first_tile_score = tile_score;
if (tile_score < first_tile_score_threshold) {
break;
}
}
while (!BT_states.empty()) {
first_tile = false;
int state = BT_states.front();
BT_states.pop();
if (state == M) {
aligned_ref_str += ref_str[ref_pos + j];
aligned_query_str += (query_str[query_pos + i]);
i += 1;
j += 1;
}
if (state == I) {
aligned_ref_str += ref_str[ref_pos + j];
aligned_query_str += '-';
j += 1;
}
if (state == D) {
aligned_ref_str += '-';
aligned_query_str += query_str[query_pos + i];
i += 1;
}
}
ref_pos += (j);
query_pos += (i);
}
int total_score = 0;
bool open = true;
for (uint32_t j = 0; j < aligned_ref_str.length(); j++) {
char ref_nt = aligned_ref_str[j];
char query_nt = aligned_query_str[j];
if (ref_nt == '-' || query_nt == '-') {
total_score += (open) ? gap_open : gap_extend;
open = false;
}
else {
total_score += (query_nt == ref_nt) ? match_score : mismatch_score;
open = true;
}
}
// either print whole name, or only id (integer)
if(!(same_file && ref_id == query_id) && total_score > SCORE_THRESHOLD){
fout
<< "ref_id: " << reference_descrips[ref_id][0]
<< ", query_id: " << reads_descrips[query_id][0]
//<< "ref_id: " << ref_id
//<< ", query_id: " << query_id
<< ", ab: " << abpos
<< ", ae: " << ref_pos
<< ", bb: " << bbpos
<< ", be: " << query_pos
<< ", score: " << total_score
<< ", comp: " << complement << std::endl;//*/
}
callidx++;
} // end GACT()
#ifdef GPU
void GACT_Batch(std::vector<GACT_call> calls, int num_calls, \
bool complement, int offset, GPU_storage *s, \
int match_score, int mismatch_score, \
int gap_open, int gap_extend, \
std::ofstream &fout)
{
int early_terminate = tile_size - tile_overlap;
std::vector<std::queue<int> > BT_statess(BATCH_SIZE);
std::vector<std::string> *reads_seqs_p;
if(complement == true){
reads_seqs_p = &rev_reads_seqs;
}else{
reads_seqs_p = &reads_seqs;
}
printf("GACT_Batch, num_calls: %d, complement: %d\n", num_calls, complement);
//output of the function
std::vector<std::string> aligned_ref_strs(num_calls);
std::vector<std::string> aligned_query_strs(num_calls);
// reserve space for aligned strings
for(int i = 0; i < num_calls; ++i){
aligned_ref_strs[i].reserve(200);
aligned_query_strs[i].reserve(200);
}
int next_callidx = BATCH_SIZE;
int calls_done = 0;
std::vector<int> assignments(BATCH_SIZE); // BATCH thread i will compute GACTcall assignments[i]
std::vector<int> ref_tile_lengths(BATCH_SIZE);
std::vector<int> query_tile_lengths(BATCH_SIZE);
if(num_calls < BATCH_SIZE){
printf("WARNING not enough callidxs for BATCH, I'll keep going anyway\n");
int i = 0;
for(; i < num_calls; ++i){
assignments[i] = i;
}
for(; i < BATCH_SIZE; ++i){
assignments[i] = -1;
}
}else{
for(int i = 0; i < BATCH_SIZE; ++i){
assignments[i] = i;
}
}
// terminate :: 0: continue, 1: terminate
std::vector<char> terminate(BATCH_SIZE);
std::vector<std::string> ref_seqs(BATCH_SIZE);
std::vector<std::string> query_seqs(BATCH_SIZE);
std::vector<int> ref_lens(BATCH_SIZE);
std::vector<int> query_lens(BATCH_SIZE);
std::vector<char> reverses(BATCH_SIZE);
std::vector<char> firsts_b(BATCH_SIZE);
#ifdef TIME
std::chrono::high_resolution_clock::time_point t1, t2;
auto time_gpu = 0, time_loop = 0;
t1 = std::chrono::high_resolution_clock::now();
#endif
while(calls_done < num_calls){
for(int t = 0; t < BATCH_SIZE; ++t){
char next_call = 0;
int callidx = assignments[t];
GACT_call *c = &(calls[callidx]);
if(callidx == -1){
ref_lens[t] = -1;
continue;
}
int ref_pos = c->ref_pos;
int query_pos = c->query_pos;
int ref_length = reference_lengths[c->ref_id];
int query_length = reads_lengths[c->query_id];
// prepare assignments
if(c->reverse == 1){
if(ref_pos <= 0 || query_pos <= 0 || terminate[t]){
// store begin of alignment in ref_bpos and query_bpos
int t1 = c->ref_bpos;
int t2 = c->query_bpos;
c->ref_bpos = ref_pos;
c->query_bpos = query_pos;
ref_pos = t1;
query_pos = t2;
c->ref_pos = t1;
c->query_pos = t2;
c->reverse = 0;
terminate[t] = 0;
}
}else{
if(ref_pos >= ref_length || query_pos >= query_length || terminate[t]){
int total_score = 0;
#ifndef NOSCORE
bool open = true;
for (uint32_t j = 0; j < aligned_ref_strs[callidx].length(); j++) {
char ref_nt = aligned_ref_strs[callidx][j];
char query_nt = aligned_query_strs[callidx][j];
if (ref_nt == '-' || query_nt == '-') {
total_score += (open) ? gap_open : gap_extend;
open = false;
}
else {
total_score += (query_nt == ref_nt) ? match_score : mismatch_score;
open = true;
}
}
#endif
#ifdef NOSCORE
if(!(same_file && c->ref_id == c->query_id))
#else
if(!(same_file && c->ref_id == c->query_id) && total_score > SCORE_THRESHOLD)
#endif
{
// either print whole name, or only id (integer)
fout
<< "ref_id: " << reference_descrips[c->ref_id][0]
<< ", query_id: " << reads_descrips[c->query_id][0]
//<< "ref_id: " << c->ref_id
//<< ", query_id: " << c->query_id
<< ", ab: " << c->ref_bpos
<< ", ae: " << ref_pos
<< ", bb: " << c->query_bpos
<< ", be: " << query_pos
<< ", score: " << total_score
<< ", comp: " << complement << std::endl;
}
calls_done++;
assignments[t] = next_callidx;
if(next_callidx >= num_calls){
assignments[t] = -1;
ref_lens[t] = -1;
continue;
}
next_call = 1;
}
}
if(next_call == 1){
callidx = next_callidx++;
c = &(calls[callidx]);
ref_pos = c->ref_pos;
query_pos = c->query_pos;
ref_length = reference_lengths[c->ref_id];
query_length = reads_lengths[c->query_id];
terminate[t] = 0;
if(ref_pos <= 0 || query_pos <= 0){
c->reverse = 0;
c->ref_bpos = ref_pos;
c->query_bpos = query_pos;
}
}
// prepare batch
// if first tile
firsts_b[t] = c->first;
// if reverse
if(c->reverse == 1){
ref_lens[t] = (ref_pos > tile_size) ? tile_size : ref_pos;
query_lens[t] = (query_pos > tile_size) ? tile_size : query_pos;
ref_seqs[t] = reference_seqs[c->ref_id].substr(ref_pos-ref_lens[t], ref_lens[t]);
query_seqs[t] = reads_seqs_p->at(c->query_id).substr(query_pos-query_lens[t], query_lens[t]);
reverses[t] = 1;
}else{ // else forward
ref_lens[t] = (ref_pos + tile_size < ref_length) ? tile_size : ref_length - ref_pos;
query_lens[t] = (query_pos + tile_size < query_length) ? tile_size : query_length - query_pos;
ref_seqs[t] = reference_seqs[c->ref_id].substr(ref_pos, ref_lens[t]);
query_seqs[t] = reads_seqs_p->at(c->query_id).substr(query_pos, query_lens[t]);
reverses[t] = 0;
}
} // end prepare batch
#ifdef TIME
t2 = std::chrono::high_resolution_clock::now();
time_loop += std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
t1 = std::chrono::high_resolution_clock::now();
#endif
int *out = Align_Batch_GPU(ref_seqs, query_seqs, ref_lens, query_lens, sub_mat, gap_open, gap_extend, ref_lens, query_lens, reverses, firsts_b, early_terminate, tile_size, s, NUM_BLOCKS, THREADS_PER_BLOCK);
#ifdef TIME
t2 = std::chrono::high_resolution_clock::now();
time_gpu += std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
t1 = std::chrono::high_resolution_clock::now();
#endif
// postprocess
for(int t = 0; t < BATCH_SIZE; ++t){
int callidx = assignments[t];
GACT_call *c = &(calls[callidx]);
if(callidx == -1){continue;}
int i = 0;
int j = 0;
int idx = 5;
#ifdef NOSCORE
int *res = out + 5*t;
#else
int *res = out + 2*tile_size*t;
#endif
bool first_tile = c->first;
int ref_pos = c->ref_pos;
int query_pos = c->query_pos;
int ref_tile_length = ref_lens[t];
int query_tile_length = query_lens[t];
int tile_score = res[0];
int first_tile_score;
// if reverse
if(c->reverse == 1){
if (first_tile) {
int t1 = res[3];
int t2 = res[4];
ref_pos = ref_pos - ref_tile_length + t1;
query_pos = query_pos - query_tile_length + t2;
c->ref_bpos = ref_pos;
c->query_bpos = query_pos;
c->first_tile_score = tile_score;
if (tile_score < first_tile_score_threshold) {
terminate[t] = 1;
c->ref_pos = ref_pos;
c->query_pos = query_pos;
continue;
}
}
#ifdef NOSCORE
j = res[1];
i = res[2];
if(i + j > 0){
first_tile = false;
}
#else
int state;
while ((state = res[idx++]) != -1)
{
first_tile = false;
if (state == M) {
aligned_ref_strs[callidx].insert(0, 1, reference_seqs[c->ref_id][ref_pos - j - 1]);
aligned_query_strs[callidx].insert(0, 1, reads_seqs_p->at(c->query_id)[query_pos - i - 1]);
i += 1;
j += 1;
}
if (state == I) {
aligned_ref_strs[callidx].insert(0, 1, reference_seqs[c->ref_id][ref_pos - j - 1]);
aligned_query_strs[callidx].insert(0, 1, '-');
j += 1;
}
if (state == D) {
aligned_ref_strs[callidx].insert(0, 1, '-');
aligned_query_strs[callidx].insert(0, 1, reads_seqs_p->at(c->query_id)[query_pos - i - 1]);
i += 1;
}
}
#endif // NOSCORE
ref_pos -= (j);
query_pos -= (i);
}else{ // else forward
if (first_tile) {
int t1 = res[3];
int t2 = res[4];
ref_pos = ref_pos + ref_tile_length - t1;
query_pos = query_pos + query_tile_length - t2;
c->first_tile_score = tile_score;
if (tile_score < first_tile_score_threshold) {
terminate[t] = 1;
c->ref_pos = ref_pos;
c->query_pos = query_pos;
continue;
}
}
#ifdef NOSCORE
j = res[1];
i = res[2];
if(i + j > 0){
first_tile = false;
}
#else
int state;
while ((state = res[idx++]) != -1)
{
first_tile = false;
if (state == M) {
aligned_ref_strs[callidx] += reference_seqs[c->ref_id][ref_pos + j];
aligned_query_strs[callidx] += (reads_seqs_p->at(c->query_id)[query_pos + i]);
i += 1;
j += 1;
}
if (state == I) {
aligned_ref_strs[callidx] += reference_seqs[c->ref_id][ref_pos + j];
aligned_query_strs[callidx] += '-';
j += 1;
}
if (state == D) {
aligned_ref_strs[callidx] += '-';
aligned_query_strs[callidx] += reads_seqs_p->at(c->query_id)[query_pos + i];
i += 1;
}
}
#endif // NOSCORE
ref_pos += (j);
query_pos += (i);
} // end traceback
c->first = first_tile;
if(i == 0 || j == 0){
terminate[t] = 1;
}
c->ref_pos = ref_pos;
c->query_pos = query_pos;
} // end postprocess
} // end main loop
#ifdef TIME
t2 = std::chrono::high_resolution_clock::now();
time_loop += std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
printf("time_loop: %d ms, time_gpu: %d ms\n", time_loop, time_gpu);
#endif
} // end GACT_Batch()
#endif // GPU