comparison pyPRADA_1.2/tools/bwa-0.5.7-mh/bwtsw2_aux.c @ 0:acc2ca1a3ba4

Uploaded
author siyuan
date Thu, 20 Feb 2014 00:44:58 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:acc2ca1a3ba4
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <math.h>
4 #ifdef HAVE_CONFIG_H
5 #include "config.h"
6 #endif
7 #ifdef HAVE_PTHREAD
8 #include <pthread.h>
9 #endif
10 #include "bntseq.h"
11 #include "bwt_lite.h"
12 #include "utils.h"
13 #include "bwtsw2.h"
14 #include "stdaln.h"
15 #include "kstring.h"
16
17 #include "kseq.h"
18 KSEQ_INIT(gzFile, gzread)
19
20 #include "ksort.h"
21 #define __left_lt(a, b) ((a).end > (b).end)
22 KSORT_INIT(hit, bsw2hit_t, __left_lt)
23
24 extern unsigned char nst_nt4_table[256];
25
26 unsigned char nt_comp_table[256] = {
27 'N','N','N','N', 'N','N','N','N', 'N','N','N','N', 'N','N','N','N',
28 'N','N','N','N', 'N','N','N','N', 'N','N','N','N', 'N','N','N','N',
29 'N','N','N','N', 'N','N','N','N', 'N','N','N','N', 'N','N','N','N',
30 'N','N','N','N', 'N','N','N','N', 'N','N','N','N', 'N','N','N','N',
31 'N','T','V','G', 'H','N','N','C', 'D','N','N','M', 'N','K','N','N',
32 'N','N','Y','S', 'A','N','B','W', 'X','R','N','N', 'N','N','N','N',
33 'n','t','v','g', 'h','n','n','c', 'd','n','n','m', 'n','k','n','n',
34 'n','n','y','s', 'a','n','b','w', 'x','r','n','N', 'N','N','N','N',
35 'N','N','N','N', 'N','N','N','N', 'N','N','N','N', 'N','N','N','N',
36 'N','N','N','N', 'N','N','N','N', 'N','N','N','N', 'N','N','N','N',
37 'N','N','N','N', 'N','N','N','N', 'N','N','N','N', 'N','N','N','N',
38 'N','N','N','N', 'N','N','N','N', 'N','N','N','N', 'N','N','N','N',
39 'N','N','N','N', 'N','N','N','N', 'N','N','N','N', 'N','N','N','N',
40 'N','N','N','N', 'N','N','N','N', 'N','N','N','N', 'N','N','N','N',
41 'N','N','N','N', 'N','N','N','N', 'N','N','N','N', 'N','N','N','N',
42 'N','N','N','N', 'N','N','N','N', 'N','N','N','N', 'N','N','N','N'
43 };
44
45 extern int bsw2_resolve_duphits(const bwt_t *bwt, bwtsw2_t *b, int IS);
46 extern int bsw2_resolve_query_overlaps(bwtsw2_t *b, float mask_level);
47
48 bsw2opt_t *bsw2_init_opt()
49 {
50 bsw2opt_t *o = (bsw2opt_t*)calloc(1, sizeof(bsw2opt_t));
51 o->a = 1; o->b = 3; o->q = 5; o->r = 2; o->t = 30;
52 o->bw = 50;
53 o->z = 1; o->is = 3; o->t_seeds = 5; o->hard_clip = 0;
54 o->mask_level = 0.50f; o->yita = 5.5f; o->coef = 5.5f;
55 o->qr = o->q + o->r; o->n_threads = 1; o->chunk_size = 10000000;
56 return o;
57 }
58
59 void bsw2_destroy(bwtsw2_t *b)
60 {
61 int i;
62 if (b == 0) return;
63 if (b->cigar)
64 for (i = 0; i < b->n; ++i) free(b->cigar[i]);
65 free(b->cigar); free(b->n_cigar); free(b->hits);
66 free(b);
67 }
68
69 #define __gen_ap(par, opt) do { \
70 int i; \
71 for (i = 0; i < 25; ++i) (par).matrix[i] = -(opt)->b; \
72 for (i = 0; i < 4; ++i) (par).matrix[i*5+i] = (opt)->a; \
73 (par).gap_open = (opt)->q; (par).gap_ext = (opt)->r; \
74 (par).gap_end = (opt)->r; \
75 (par).row = 5; (par).band_width = opt->bw; \
76 } while (0)
77
78 #define __rpac(pac, l, i) (pac[(l-i-1)>>2] >> (~(l-i-1)&3)*2 & 0x3)
79
80 void bsw2_extend_left(const bsw2opt_t *opt, bwtsw2_t *b, uint8_t *_query, int lq, uint8_t *pac, uint32_t l_pac, int is_rev, uint8_t *_mem)
81 {
82 int i, matrix[25];
83 bwtint_t k;
84 uint8_t *target = 0, *query;
85 AlnParam par;
86
87 par.matrix = matrix;
88 __gen_ap(par, opt);
89 query = calloc(lq, 1);
90 // sort according to the descending order of query end
91 ks_introsort(hit, b->n, b->hits);
92 target = calloc(((lq + 1) / 2 * opt->a + opt->r) / opt->r + lq, 1);
93 // reverse _query
94 for (i = 0; i < lq; ++i) query[lq - i - 1] = _query[i];
95 // core loop
96 for (i = 0; i < b->n; ++i) {
97 bsw2hit_t *p = b->hits + i;
98 int lt = ((p->beg + 1) / 2 * opt->a + opt->r) / opt->r + lq;
99 int score, j;
100 path_t path;
101 p->n_seeds = 1;
102 if (p->l || p->k == 0) continue;
103 for (j = score = 0; j < i; ++j) {
104 bsw2hit_t *q = b->hits + j;
105 if (q->beg <= p->beg && q->k <= p->k && q->k + q->len >= p->k + p->len) {
106 if (q->n_seeds < (1<<14) - 2) ++q->n_seeds;
107 ++score;
108 }
109 }
110 if (score) continue;
111 if (lt > p->k) lt = p->k;
112 if (is_rev) {
113 for (k = p->k - 1, j = 0; k > 0 && j < lt; --k) // FIXME: k=0 not considered!
114 target[j++] = __rpac(pac, l_pac, k);
115 } else {
116 for (k = p->k - 1, j = 0; k > 0 && j < lt; --k) // FIXME: k=0 not considered!
117 target[j++] = pac[k>>2] >> (~k&3)*2 & 0x3;
118 }
119 lt = j;
120 score = aln_extend_core(target, lt, query + lq - p->beg, p->beg, &par, &path, 0, p->G, _mem);
121 if (score > p->G) { // extensible
122 p->G = score;
123 p->len += path.i;
124 p->beg -= path.j;
125 p->k -= path.i;
126 }
127 }
128 free(query); free(target);
129 }
130
131 void bsw2_extend_rght(const bsw2opt_t *opt, bwtsw2_t *b, uint8_t *query, int lq, uint8_t *pac, uint32_t l_pac, int is_rev, uint8_t *_mem)
132 {
133 int i, matrix[25];
134 uint32_t k;
135 uint8_t *target;
136 AlnParam par;
137
138 par.matrix = matrix;
139 __gen_ap(par, opt);
140 target = calloc(((lq + 1) / 2 * opt->a + opt->r) / opt->r + lq, 1);
141 for (i = 0; i < b->n; ++i) {
142 bsw2hit_t *p = b->hits + i;
143 int lt = ((lq - p->beg + 1) / 2 * opt->a + opt->r) / opt->r + lq;
144 int j, score;
145 path_t path;
146 if (p->l) continue;
147 if (is_rev) {
148 for (k = p->k, j = 0; k < p->k + lt && k < l_pac; ++k)
149 target[j++] = __rpac(pac, l_pac, k);
150 } else {
151 for (k = p->k, j = 0; k < p->k + lt && k < l_pac; ++k)
152 target[j++] = pac[k>>2] >> (~k&3)*2 & 0x3;
153 }
154 lt = j;
155 score = aln_extend_core(target, lt, query + p->beg, lq - p->beg, &par, &path, 0, 1, _mem);
156 // if (score < p->G) fprintf(stderr, "[bsw2_extend_hits] %d < %d\n", score, p->G);
157 if (score >= p->G) {
158 p->G = score;
159 p->len = path.i;
160 p->end = path.j + p->beg;
161 }
162 }
163 free(target);
164 }
165
166 /* generate CIGAR array(s) in b->cigar[] */
167 static void gen_cigar(const bsw2opt_t *opt, int lq, uint8_t *seq[2], uint8_t *pac, bwtsw2_t *b)
168 {
169 uint8_t *target;
170 int i, matrix[25];
171 AlnParam par;
172 path_t *path;
173
174 par.matrix = matrix;
175 __gen_ap(par, opt);
176 i = ((lq + 1) / 2 * opt->a + opt->r) / opt->r + lq; // maximum possible target length
177 target = calloc(i, 1);
178 path = calloc(i + lq, sizeof(path_t));
179 // memory clean up for b
180 if (b->n < b->max) {
181 b->max = b->n;
182 b->hits = realloc(b->hits, b->n * sizeof(bsw2hit_t));
183 }
184 if (b->cigar) free(b->cigar);
185 if (b->n_cigar) free(b->n_cigar);
186 b->cigar = (uint32_t**)calloc(b->max, sizeof(void*));
187 b->n_cigar = (int*)calloc(b->max, sizeof(int));
188 // generate CIGAR
189 for (i = 0; i < b->n; ++i) {
190 bsw2hit_t *p = b->hits + i;
191 uint8_t *query;
192 uint32_t k;
193 int score, path_len, beg, end;
194 if (p->l) continue;
195 beg = (p->flag & 0x10)? lq - p->end : p->beg;
196 end = (p->flag & 0x10)? lq - p->beg : p->end;
197 query = seq[(p->flag & 0x10)? 1 : 0] + beg;
198 for (k = p->k; k < p->k + p->len; ++k) // in principle, no out-of-boundary here
199 target[k - p->k] = pac[k>>2] >> (~k&3)*2 & 0x3;
200 score = aln_global_core(target, p->len, query, end - beg, &par, path, &path_len);
201 b->cigar[i] = aln_path2cigar32(path, path_len, &b->n_cigar[i]);
202 if (beg != 0 || end < lq) { // write soft clipping
203 b->cigar[i] = realloc(b->cigar[i], 4 * (b->n_cigar[i] + 2));
204 if (beg != 0) {
205 memmove(b->cigar[i] + 1, b->cigar[i], b->n_cigar[i] * 4);
206 b->cigar[i][0] = beg<<4 | 4;
207 ++b->n_cigar[i];
208 }
209 if (end < lq) {
210 b->cigar[i][b->n_cigar[i]] = (lq - end)<<4 | 4;
211 ++b->n_cigar[i];
212 }
213 }
214 }
215 free(target); free(path);
216 }
217
218 /* this is for the debugging purpose only */
219 void bsw2_debug_hits(const bwtsw2_t *b)
220 {
221 int i;
222 printf("# raw hits: %d\n", b->n);
223 for (i = 0; i < b->n; ++i) {
224 bsw2hit_t *p = b->hits + i;
225 if (p->l == 0)
226 printf("%d, %d, %d, %u, %u\n", p->G, p->beg, p->end, p->k, p->l);
227 }
228 }
229
230 static void merge_hits(bwtsw2_t *b[2], int l, int is_reverse)
231 {
232 int i;
233 if (b[0]->n + b[1]->n > b[0]->max) {
234 b[0]->max = b[0]->n + b[1]->n;
235 b[0]->hits = realloc(b[0]->hits, b[0]->max * sizeof(bsw2hit_t));
236 }
237 for (i = 0; i < b[1]->n; ++i) {
238 bsw2hit_t *p = b[0]->hits + b[0]->n + i;
239 *p = b[1]->hits[i];
240 if (is_reverse) {
241 int x = p->beg;
242 p->beg = l - p->end;
243 p->end = l - x;
244 p->flag |= 0x10;
245 }
246 }
247 b[0]->n += b[1]->n;
248 bsw2_destroy(b[1]);
249 b[1] = 0;
250 }
251
252 static bwtsw2_t *bsw2_aln1_core(const bsw2opt_t *opt, const bntseq_t *bns, uint8_t *pac, const bwt_t *target, int l, uint8_t *seq[2], int is_rev, bsw2global_t *pool)
253 {
254 extern void bsw2_chain_filter(const bsw2opt_t *opt, int len, bwtsw2_t *b[2]);
255 bwtsw2_t *b[2], **bb[2];
256 int k;
257 for (k = 0; k < 2; ++k) {
258 bwtl_t *query = bwtl_seq2bwtl(l, seq[k]);
259 bb[k] = bsw2_core(opt, query, target, pool);
260 bwtl_destroy(query);
261 }
262 b[0] = bb[0][1]; b[1] = bb[1][1];
263 bsw2_chain_filter(opt, l, b);
264 for (k = 0; k < 2; ++k) {
265 bsw2_extend_left(opt, bb[k][1], seq[k], l, pac, bns->l_pac, is_rev, pool->aln_mem);
266 merge_hits(bb[k], l, 0);
267 bsw2_resolve_duphits(0, bb[k][0], 0);
268 bsw2_extend_rght(opt, bb[k][0], seq[k], l, pac, bns->l_pac, is_rev, pool->aln_mem);
269 b[k] = bb[k][0];
270 free(bb[k]);
271 }
272 merge_hits(b, l, 1);
273 bsw2_resolve_query_overlaps(b[0], opt->mask_level);
274 return b[0];
275 }
276
277 /* set ->flag to records the origin of the hit (to forward bwt or reverse bwt) */
278 static void flag_fr(bwtsw2_t *b[2])
279 {
280 int i, j;
281 for (i = 0; i < b[0]->n; ++i) {
282 bsw2hit_t *p = b[0]->hits + i;
283 p->flag |= 0x10000;
284 }
285 for (i = 0; i < b[1]->n; ++i) {
286 bsw2hit_t *p = b[1]->hits + i;
287 p->flag |= 0x20000;
288 }
289 for (i = 0; i < b[0]->n; ++i) {
290 bsw2hit_t *p = b[0]->hits + i;
291 for (j = 0; j < b[1]->n; ++j) {
292 bsw2hit_t *q = b[1]->hits + i;
293 if (q->beg == p->beg && q->end == p->end && q->k == p->k && q->len == p->len && q->G == p->G) {
294 q->flag |= 0x30000; p->flag |= 0x30000;
295 break;
296 }
297 }
298 }
299 }
300
301 typedef struct {
302 int l, tid;
303 char *name, *seq, *qual, *sam;
304 } bsw2seq1_t;
305
306 typedef struct {
307 int n, max;
308 bsw2seq1_t *seq;
309 } bsw2seq_t;
310
311 #ifdef HAVE_PTHREAD
312 static pthread_mutex_t g_dbwtsw_lock = PTHREAD_MUTEX_INITIALIZER;
313 #endif
314
315 static int fix_cigar(const char *qname, const bntseq_t *bns, bsw2hit_t *p, int n_cigar, uint32_t *cigar)
316 {
317 // FIXME: this routine does not work if the query bridge three reference sequences
318 int32_t coor, refl, lq;
319 int x, y, i, seqid;
320 bns_coor_pac2real(bns, p->k, p->len, &seqid);
321 coor = p->k - bns->anns[seqid].offset;
322 refl = bns->anns[seqid].len;
323 x = coor; y = 0;
324 // test if the alignment goes beyond the boundary
325 for (i = 0; i < n_cigar; ++i) {
326 int op = cigar[i]&0xf, ln = cigar[i]>>4;
327 if (op == 1 || op == 4 || op == 5) y += ln;
328 else if (op == 2) x += ln;
329 else x += ln, y += ln;
330 }
331 lq = y; // length of the query sequence
332 if (x > refl) { // then fix it
333 int j, nc, mq[2], nlen[2];
334 uint32_t *cn, kk = 0;
335 nc = mq[0] = mq[1] = nlen[0] = nlen[1] = 0;
336 cn = calloc(n_cigar + 3, 4);
337 x = coor; y = 0;
338 for (i = j = 0; i < n_cigar; ++i) {
339 int op = cigar[i]&0xf, ln = cigar[i]>>4;
340 if (op == 4 || op == 5 || op == 1) { // ins or clipping
341 y += ln;
342 cn[j++] = cigar[i];
343 } else if (op == 2) { // del
344 if (x + ln >= refl && nc == 0) {
345 cn[j++] = (uint32_t)(lq - y)<<4 | 4;
346 nc = j;
347 cn[j++] = (uint32_t)y<<4 | 4;
348 kk = p->k + (x + ln - refl);
349 nlen[0] = x - coor;
350 nlen[1] = p->len - nlen[0] - ln;
351 } else cn[j++] = cigar[i];
352 x += ln;
353 } else if (op == 0) { // match
354 if (x + ln >= refl && nc == 0) {
355 // FIXME: not consider a special case where a split right between M and I
356 cn[j++] = (uint32_t)(refl - x)<<4 | 0; // write M
357 cn[j++] = (uint32_t)(lq - y - (refl - x))<<4 | 4; // write S
358 nc = j;
359 mq[0] += refl - x;
360 cn[j++] = (uint32_t)(y + (refl - x))<<4 | 4;
361 if (x + ln - refl) cn[j++] = (uint32_t)(x + ln - refl)<<4 | 0;
362 mq[1] += x + ln - refl;
363 kk = bns->anns[seqid].offset + refl;
364 nlen[0] = refl - coor;
365 nlen[1] = p->len - nlen[0];
366 } else {
367 cn[j++] = cigar[i];
368 mq[nc?1:0] += ln;
369 }
370 x += ln; y += ln;
371 }
372 }
373 if (mq[0] > mq[1]) { // then take the first alignment
374 n_cigar = nc;
375 memcpy(cigar, cn, 4 * nc);
376 p->len = nlen[0];
377 } else {
378 p->k = kk; p->len = nlen[1];
379 n_cigar = j - nc;
380 memcpy(cigar, cn + nc, 4 * (j - nc));
381 }
382 free(cn);
383 }
384 return n_cigar;
385 }
386
387 /* generate SAM lines for a sequence in ks with alignment stored in
388 * b. ks->name and ks->seq will be freed and set to NULL in the end. */
389 static void print_hits(const bntseq_t *bns, const bsw2opt_t *opt, bsw2seq1_t *ks, bwtsw2_t *b)
390 {
391 int i, k;
392 kstring_t str;
393 memset(&str, 0, sizeof(kstring_t));
394 for (i = 0; i < b->n; ++i) {
395 bsw2hit_t *p = b->hits + i;
396 int32_t seqid = -1, coor = -1;
397 int j, qual, nn = 0;
398 int beg, end;
399 if (p->l == 0) {
400 b->n_cigar[i] = fix_cigar(ks->name, bns, p, b->n_cigar[i], b->cigar[i]);
401 nn = bns_coor_pac2real(bns, p->k, p->len, &seqid);
402 coor = p->k - bns->anns[seqid].offset;
403 }
404 ksprintf(&str, "%s\t%d", ks->name, p->flag&0x10);
405 ksprintf(&str, "\t%s\t%d", seqid>=0? bns->anns[seqid].name : "*", coor + 1);
406 if (p->l == 0) {
407 { // estimate mapping quality
408 float c = 1.0;
409 int subo = p->G2 > opt->t? p->G2 : opt->t;
410 if (p->flag>>16 == 1 || p->flag>>16 == 2) c *= .5;
411 if (p->n_seeds < 2) c *= .2;
412 qual = (int)(c * (p->G - subo) * (250.0 / p->G + 0.03 / opt->a) + .499);
413 if (qual > 250) qual = 250;
414 if (p->flag&1) qual = 0;
415 }
416 ksprintf(&str, "\t%d\t", qual);
417 for (k = 0; k < b->n_cigar[i]; ++k)
418 ksprintf(&str, "%d%c", b->cigar[i][k]>>4, (opt->hard_clip? "MIDNHHP" : "MIDNSHP")[b->cigar[i][k]&0xf]);
419 } else ksprintf(&str, "\t0\t*");
420 ksprintf(&str, "\t*\t0\t0\t");
421 beg = 0; end = ks->l;
422 if (opt->hard_clip) {
423 if ((b->cigar[i][0]&0xf) == 4) beg += b->cigar[i][0]>>4;
424 if ((b->cigar[i][b->n_cigar[i]-1]&0xf) == 4) end -= b->cigar[i][b->n_cigar[i]-1]>>4;
425 }
426 for (j = beg; j < end; ++j) {
427 if (p->flag&0x10) kputc(nt_comp_table[(int)ks->seq[ks->l - 1 - j]], &str);
428 else kputc(ks->seq[j], &str);
429 }
430 if (ks->qual) {
431 kputc('\t', &str);
432 for (j = beg; j < end; ++j) {
433 if (p->flag&0x10) kputc(ks->qual[ks->l - 1 - j], &str);
434 else kputc(ks->qual[j], &str);
435 }
436 } else ksprintf(&str, "\t*");
437 ksprintf(&str, "\tAS:i:%d\tXS:i:%d\tXF:i:%d\tXE:i:%d\tXN:i:%d", p->G, p->G2, p->flag>>16, p->n_seeds, nn);
438 if (p->l) ksprintf(&str, "\tXI:i:%d", p->l - p->k + 1);
439 kputc('\n', &str);
440 }
441 ks->sam = str.s;
442 free(ks->seq); ks->seq = 0;
443 free(ks->qual); ks->qual = 0;
444 free(ks->name); ks->name = 0;
445 }
446
447 /* Core routine to align reads in _seq. It is separated from
448 * process_seqs() to realize multi-threading */
449 static void bsw2_aln_core(int tid, bsw2seq_t *_seq, const bsw2opt_t *_opt, const bntseq_t *bns, uint8_t *pac, bwt_t * const target[2])
450 {
451 int x;
452 bsw2opt_t opt = *_opt;
453 bsw2global_t *pool = bsw2_global_init();
454 for (x = 0; x < _seq->n; ++x) {
455 bsw2seq1_t *p = _seq->seq + x;
456 uint8_t *seq[2], *rseq[2];
457 int i, l, k;
458 bwtsw2_t *b[2];
459 l = p->l;
460
461 #ifdef HAVE_PTHREAD
462 if (_opt->n_threads > 1) {
463 pthread_mutex_lock(&g_dbwtsw_lock);
464 if (p->tid < 0) p->tid = tid;
465 else if (p->tid != tid) {
466 pthread_mutex_unlock(&g_dbwtsw_lock);
467 continue;
468 } // in pinciple else should not happen
469 pthread_mutex_unlock(&g_dbwtsw_lock);
470 }
471 #endif
472
473 // set opt->t
474 opt.t = _opt->t;
475 if (opt.t < log(l) * opt.coef) opt.t = (int)(log(l) * opt.coef + .499);
476 if (pool->max_l < l) { // then enlarge working space for aln_extend_core()
477 int tmp = ((l + 1) / 2 * opt.a + opt.r) / opt.r + l;
478 pool->max_l = l;
479 pool->aln_mem = realloc(pool->aln_mem, (tmp + 2) * 24);
480 }
481 // set opt->bw
482 opt.bw = _opt->bw;
483 k = (l * opt.a - 2 * opt.q) / (2 * opt.r + opt.a);
484 i = (l * opt.a - opt.a - opt.t) / opt.r;
485 if (k > i) k = i;
486 if (k < 1) k = 1; // I do not know if k==0 causes troubles
487 opt.bw = _opt->bw < k? _opt->bw : k;
488 // set seq[2] and rseq[2]
489 seq[0] = calloc(l * 4, 1);
490 seq[1] = seq[0] + l;
491 rseq[0] = seq[1] + l; rseq[1] = rseq[0] + l;
492 // convert sequences to 2-bit representation
493 for (i = k = 0; i < l; ++i) {
494 int c = nst_nt4_table[(int)p->seq[i]];
495 if (c >= 4) { c = (int)(drand48() * 4); ++k; } // FIXME: ambiguous bases are not properly handled
496 seq[0][i] = c;
497 seq[1][l-1-i] = 3 - c;
498 rseq[0][l-1-i] = c;
499 rseq[1][i] = 3 - c;
500 }
501 if (l - k < opt.t) {
502 free(seq[0]); continue;
503 }
504 // alignment
505 b[0] = bsw2_aln1_core(&opt, bns, pac, target[0], l, seq, 0, pool);
506 for (k = 0; k < b[0]->n; ++k)
507 if (b[0]->hits[k].n_seeds < opt.t_seeds) break;
508 if (k < b[0]->n) {
509 b[1] = bsw2_aln1_core(&opt, bns, pac, target[1], l, rseq, 1, pool);
510 for (i = 0; i < b[1]->n; ++i) {
511 bsw2hit_t *p = b[1]->hits + i;
512 int x = p->beg;
513 p->beg = l - p->end;
514 p->end = l - x;
515 if (p->l == 0) p->k = bns->l_pac - (p->k + p->len);
516 }
517 flag_fr(b);
518 merge_hits(b, l, 0);
519 bsw2_resolve_duphits(0, b[0], 0);
520 bsw2_resolve_query_overlaps(b[0], opt.mask_level);
521 } else b[1] = 0;
522 // generate CIGAR and print SAM
523 gen_cigar(&opt, l, seq, pac, b[0]);
524 print_hits(bns, &opt, p, b[0]);
525 // free
526 free(seq[0]);
527 bsw2_destroy(b[0]);
528 }
529 bsw2_global_destroy(pool);
530 }
531
532 #ifdef HAVE_PTHREAD
533 typedef struct {
534 int tid;
535 bsw2seq_t *_seq;
536 const bsw2opt_t *_opt;
537 const bntseq_t *bns;
538 uint8_t *pac;
539 bwt_t *target[2];
540 } thread_aux_t;
541
542 /* another interface to bsw2_aln_core() to facilitate pthread_create() */
543 static void *worker(void *data)
544 {
545 thread_aux_t *p = (thread_aux_t*)data;
546 bsw2_aln_core(p->tid, p->_seq, p->_opt, p->bns, p->pac, p->target);
547 return 0;
548 }
549 #endif
550
551 /* process sequences stored in _seq, generate SAM lines for these
552 * sequences and reset _seq afterwards. */
553 static void process_seqs(bsw2seq_t *_seq, const bsw2opt_t *opt, const bntseq_t *bns, uint8_t *pac, bwt_t * const target[2])
554 {
555 int i;
556
557 #ifdef HAVE_PTHREAD
558 if (opt->n_threads <= 1) {
559 bsw2_aln_core(0, _seq, opt, bns, pac, target);
560 } else {
561 pthread_t *tid;
562 pthread_attr_t attr;
563 thread_aux_t *data;
564 int j;
565 pthread_attr_init(&attr);
566 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
567 data = (thread_aux_t*)calloc(opt->n_threads, sizeof(thread_aux_t));
568 tid = (pthread_t*)calloc(opt->n_threads, sizeof(pthread_t));
569 for (j = 0; j < opt->n_threads; ++j) {
570 thread_aux_t *p = data + j;
571 p->tid = j; p->_seq = _seq; p->_opt = opt; p->bns = bns;
572 p->pac = pac; p->target[0] = target[0]; p->target[1] = target[1];
573 pthread_create(&tid[j], &attr, worker, p);
574 }
575 for (j = 0; j < opt->n_threads; ++j) pthread_join(tid[j], 0);
576 free(data); free(tid);
577 }
578 #else
579 bsw2_aln_core(0, _seq, opt, bns, pac, target);
580 #endif
581
582 // print and reset
583 for (i = 0; i < _seq->n; ++i) {
584 bsw2seq1_t *p = _seq->seq + i;
585 if (p->sam) printf("%s", p->sam);
586 free(p->name); free(p->seq); free(p->qual); free(p->sam);
587 p->tid = -1; p->l = 0;
588 p->name = p->seq = p->qual = p->sam = 0;
589 }
590 fflush(stdout);
591 _seq->n = 0;
592 }
593
594 void bsw2_aln(const bsw2opt_t *opt, const bntseq_t *bns, bwt_t * const target[2], const char *fn)
595 {
596 gzFile fp;
597 kseq_t *ks;
598 int l, size = 0;
599 uint8_t *pac;
600 bsw2seq_t *_seq;
601
602 pac = calloc(bns->l_pac/4+1, 1);
603 if (pac == 0) {
604 fprintf(stderr, "[bsw2_aln] insufficient memory!\n");
605 return;
606 }
607 for (l = 0; l < bns->n_seqs; ++l)
608 printf("@SQ\tSN:%s\tLN:%d\n", bns->anns[l].name, bns->anns[l].len);
609 fread(pac, 1, bns->l_pac/4+1, bns->fp_pac);
610 fp = xzopen(fn, "r");
611 ks = kseq_init(fp);
612 _seq = calloc(1, sizeof(bsw2seq_t));
613 while ((l = kseq_read(ks)) >= 0) {
614 bsw2seq1_t *p;
615 if (_seq->n == _seq->max) {
616 _seq->max = _seq->max? _seq->max<<1 : 1024;
617 _seq->seq = realloc(_seq->seq, _seq->max * sizeof(bsw2seq1_t));
618 }
619 p = &_seq->seq[_seq->n++];
620 p->tid = -1;
621 p->l = l;
622 p->name = strdup(ks->name.s);
623 p->seq = strdup(ks->seq.s);
624 p->qual = ks->qual.l? strdup(ks->qual.s) : 0;
625 p->sam = 0;
626 size += l;
627 if (size > opt->chunk_size) {
628 fprintf(stderr, "[bsw2_aln] read %d sequences (%d bp)...\n", _seq->n, size);
629 process_seqs(_seq, opt, bns, pac, target);
630 size = 0;
631 }
632 }
633 fprintf(stderr, "[bsw2_aln] read %d sequences (%d bp)...\n", _seq->n, size);
634 process_seqs(_seq, opt, bns, pac, target);
635 free(_seq->seq); free(_seq);
636 kseq_destroy(ks);
637 gzclose(fp);
638 free(pac);
639 }