forked from lh3/minigraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.c
459 lines (421 loc) · 15.3 KB
/
map.c
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
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "kthread.h"
#include "kvec.h"
#include "kalloc.h"
#include "mgpriv.h"
#include "bseq.h"
#include "khash.h"
struct mg_tbuf_s {
void *km;
int frag_gap;
};
mg_tbuf_t *mg_tbuf_init(void)
{
mg_tbuf_t *b;
b = (mg_tbuf_t*)calloc(1, sizeof(mg_tbuf_t));
if (!(mg_dbg_flag & MG_DBG_NO_KALLOC)) b->km = km_init();
return b;
}
void mg_tbuf_destroy(mg_tbuf_t *b)
{
if (b == 0) return;
if (b->km) km_destroy(b->km);
free(b);
}
void *mg_tbuf_get_km(mg_tbuf_t *b)
{
return b->km;
}
static void collect_minimizers(void *km, const mg_mapopt_t *opt, const mg_idx_t *gi, int n_segs, const int *qlens, const char **seqs, mg128_v *mv)
{
int i, n, sum = 0;
mv->n = 0;
for (i = n = 0; i < n_segs; ++i) {
size_t j;
mg_sketch(km, seqs[i], qlens[i], gi->w, gi->k, i, mv);
for (j = n; j < mv->n; ++j)
mv->a[j].y += sum << 1;
sum += qlens[i], n = mv->n;
}
}
#include "ksort.h"
#define heap_lt(a, b) ((a).x > (b).x)
KSORT_INIT(heap, mg128_t, heap_lt)
typedef struct {
uint32_t n;
uint32_t q_pos, q_span;
uint32_t seg_id:31, is_tandem:1;
const uint64_t *cr;
} mg_match_t;
static mg_match_t *collect_matches(void *km, int *_n_m, int max_occ, const mg_idx_t *gi, const mg128_v *mv, int64_t *n_a, int *rep_len, int *n_mini_pos, int32_t **mini_pos)
{
int rep_st = 0, rep_en = 0, n_m;
size_t i;
mg_match_t *m;
*n_mini_pos = 0;
KMALLOC(km, *mini_pos, mv->n);
m = (mg_match_t*)kmalloc(km, mv->n * sizeof(mg_match_t));
for (i = 0, n_m = 0, *rep_len = 0, *n_a = 0; i < mv->n; ++i) {
const uint64_t *cr;
mg128_t *p = &mv->a[i];
uint32_t q_pos = (uint32_t)p->y, q_span = p->x & 0xff;
int t;
cr = mg_idx_get(gi, p->x>>8, &t);
if (t >= max_occ) {
int en = (q_pos >> 1) + 1, st = en - q_span;
if (st > rep_en) {
*rep_len += rep_en - rep_st;
rep_st = st, rep_en = en;
} else rep_en = en;
} else {
mg_match_t *q = &m[n_m++];
q->q_pos = q_pos, q->q_span = q_span, q->cr = cr, q->n = t, q->seg_id = p->y >> 32;
q->is_tandem = 0;
if (i > 0 && p->x>>8 == mv->a[i - 1].x>>8) q->is_tandem = 1;
if (i < mv->n - 1 && p->x>>8 == mv->a[i + 1].x>>8) q->is_tandem = 1;
*n_a += q->n;
(*mini_pos)[(*n_mini_pos)++] = q_pos>>1;
}
}
*rep_len += rep_en - rep_st;
*_n_m = n_m;
return m;
}
static mg128_t *collect_seed_hits_heap(void *km, const mg_mapopt_t *opt, int max_occ, const mg_idx_t *gi, const char *qname, const mg128_v *mv, int qlen, int64_t *n_a, int *rep_len,
int *n_mini_pos, int32_t **mini_pos)
{
int i, n_m, heap_size = 0;
int64_t n_for = 0, n_rev = 0;
mg_match_t *m;
mg128_t *a, *heap;
m = collect_matches(km, &n_m, max_occ, gi, mv, n_a, rep_len, n_mini_pos, mini_pos);
heap = (mg128_t*)kmalloc(km, n_m * sizeof(mg128_t));
a = (mg128_t*)kmalloc(km, *n_a * sizeof(mg128_t));
for (i = 0, heap_size = 0; i < n_m; ++i) {
if (m[i].n > 0) {
heap[heap_size].x = m[i].cr[0];
heap[heap_size].y = (uint64_t)i<<32;
++heap_size;
}
}
ks_heapmake_heap(heap_size, heap);
while (heap_size > 0) {
mg_match_t *q = &m[heap->y>>32];
mg128_t *p;
uint64_t r = heap->x;
int32_t rpos = (uint32_t)r >> 1;
if ((r&1) == (q->q_pos&1)) { // forward strand
p = &a[n_for++];
p->x = r>>32<<33 | rpos;
} else { // reverse strand; TODO: more testing needed for this block
p = &a[(*n_a) - (++n_rev)];
p->x = r>>32<<33 | 1ULL<<32 | (gi->g->seg[r>>32].len - (rpos + 1 - q->q_span) - 1);
}
p->y = (uint64_t)q->q_span << 32 | q->q_pos >> 1;
p->y |= (uint64_t)q->seg_id << MG_SEED_SEG_SHIFT;
if (q->is_tandem) p->y |= MG_SEED_TANDEM;
// update the heap
if ((uint32_t)heap->y < q->n - 1) {
++heap[0].y;
heap[0].x = m[heap[0].y>>32].cr[(uint32_t)heap[0].y];
} else {
heap[0] = heap[heap_size - 1];
--heap_size;
}
ks_heapdown_heap(0, heap_size, heap);
}
kfree(km, m);
kfree(km, heap);
// reverse anchors on the reverse strand, as they are in the descending order
if (*n_a > n_for + n_rev) {
memmove(a + n_for, a + (*n_a) - n_rev, n_rev * sizeof(mg128_t));
*n_a = n_for + n_rev;
}
return a;
}
static mg128_t *collect_seed_hits(void *km, const mg_mapopt_t *opt, int max_occ, const mg_idx_t *gi, const char *qname, const mg128_v *mv, int qlen, int64_t *n_a, int *rep_len,
int *n_mini_pos, int32_t **mini_pos)
{
int i, n_m;
mg_match_t *m;
mg128_t *a;
m = collect_matches(km, &n_m, max_occ, gi, mv, n_a, rep_len, n_mini_pos, mini_pos);
a = (mg128_t*)kmalloc(km, *n_a * sizeof(mg128_t));
for (i = 0, *n_a = 0; i < n_m; ++i) {
mg_match_t *q = &m[i];
const uint64_t *r = q->cr;
uint32_t k;
for (k = 0; k < q->n; ++k) {
int32_t rpos = (uint32_t)r[k] >> 1;
mg128_t *p = &a[(*n_a)++];
if ((r[k]&1) == (q->q_pos&1)) // forward strand
p->x = r[k]>>32<<33 | rpos;
else // reverse strand
p->x = r[k]>>32<<33 | 1ULL<<32 | (gi->g->seg[r[k]>>32].len - (rpos + 1 - q->q_span) - 1);
p->y = (uint64_t)q->q_span << 32 | q->q_pos >> 1;
p->y |= (uint64_t)q->seg_id << MG_SEED_SEG_SHIFT;
if (q->is_tandem) p->y |= MG_SEED_TANDEM;
}
}
kfree(km, m);
radix_sort_128x(a, a + (*n_a));
return a;
}
void mg_map_frag(const mg_idx_t *gi, int n_segs, const int *qlens, const char **seqs, mg_gchains_t **gcs, mg_tbuf_t *b, const mg_mapopt_t *opt, const char *qname)
{
int i, rep_len, qlen_sum, n_lc, n_gc, n_mini_pos;
int max_chain_gap_qry, max_chain_gap_ref, is_splice = !!(opt->flag & MG_M_SPLICE), is_sr = !!(opt->flag & MG_M_SR);
uint32_t hash;
int64_t n_a;
uint64_t *u;
int32_t *mini_pos;
mg128_t *a;
mg128_v mv = {0,0,0};
mg_lchain_t *lc;
km_stat_t kmst;
for (i = 0, qlen_sum = 0; i < n_segs; ++i)
qlen_sum += qlens[i], gcs[i] = 0;
if (qlen_sum == 0 || n_segs <= 0 || n_segs > MG_MAX_SEG) return;
if (opt->max_qlen > 0 && qlen_sum > opt->max_qlen) return;
hash = qname? __ac_X31_hash_string(qname) : 0;
hash ^= __ac_Wang_hash(qlen_sum) + __ac_Wang_hash(opt->seed);
hash = __ac_Wang_hash(hash);
collect_minimizers(b->km, opt, gi, n_segs, qlens, seqs, &mv);
if (opt->flag & MG_M_HEAP_SORT) a = collect_seed_hits_heap(b->km, opt, opt->mid_occ, gi, qname, &mv, qlen_sum, &n_a, &rep_len, &n_mini_pos, &mini_pos);
else a = collect_seed_hits(b->km, opt, opt->mid_occ, gi, qname, &mv, qlen_sum, &n_a, &rep_len, &n_mini_pos, &mini_pos);
if (mg_dbg_flag & MG_DBG_SEED) {
fprintf(stderr, "RS\t%d\n", rep_len);
for (i = 0; i < n_a; ++i)
fprintf(stderr, "SD\t%s\t%d\t%c\t%d\t%d\t%d\n", gi->g->seg[a[i].x>>33].name, (int32_t)a[i].x, "+-"[a[i].x>>32&1], (int32_t)a[i].y, (int32_t)(a[i].y>>32&0xff),
i == 0? 0 : ((int32_t)a[i].y - (int32_t)a[i-1].y) - ((int32_t)a[i].x - (int32_t)a[i-1].x));
}
// set max chaining gap on the query and the reference sequence
if (is_sr)
max_chain_gap_qry = qlen_sum > opt->max_gap? qlen_sum : opt->max_gap;
else max_chain_gap_qry = opt->max_gap;
if (opt->max_gap_ref > 0) {
max_chain_gap_ref = opt->max_gap_ref; // always honor mg_mapopt_t::max_gap_ref if set
} else if (opt->max_frag_len > 0) {
max_chain_gap_ref = opt->max_frag_len - qlen_sum;
if (max_chain_gap_ref < opt->max_gap) max_chain_gap_ref = opt->max_gap;
} else max_chain_gap_ref = opt->max_gap;
a = mg_lchain_dp(max_chain_gap_ref, max_chain_gap_qry, opt->bw, opt->max_lc_skip, opt->max_lc_iter, opt->min_lc_cnt, opt->min_lc_score, is_splice, n_segs, n_a, a, &n_lc, &u, b->km);
#if 0 // re-chaining; mostly for short reads
if (opt->max_occ > opt->mid_occ && rep_len > 0) {
int rechain = 0;
if (n_lc > 0) { // test if the best chain has all the segments
int n_chained_segs = 1, max = 0, max_i = -1, max_off = -1, off = 0;
for (i = 0; i < n_lc; ++i) { // find the best chain
if (max < (int)(u[i]>>32)) max = u[i]>>32, max_i = i, max_off = off;
off += (uint32_t)u[i];
}
for (i = 1; i < (int32_t)u[max_i]; ++i) // count the number of segments in the best chain
if ((a[max_off+i].y&MG_SEED_SEG_MASK) != (a[max_off+i-1].y&MG_SEED_SEG_MASK))
++n_chained_segs;
if (n_chained_segs < n_segs)
rechain = 1;
} else rechain = 1;
if (rechain) { // redo chaining with a higher max_occ threshold
kfree(b->km, a);
kfree(b->km, u);
kfree(b->km, mini_pos);
if (opt->flag & MG_M_HEAP_SORT) a = collect_seed_hits_heap(b->km, opt, opt->max_occ, gi, qname, &mv, qlen_sum, &n_a, &rep_len, &n_mini_pos, &mini_pos);
else a = collect_seed_hits(b->km, opt, opt->max_occ, gi, qname, &mv, qlen_sum, &n_a, &rep_len, &n_mini_pos, &mini_pos);
a = mg_lchain_dp(max_chain_gap_ref, max_chain_gap_qry, opt->bw, opt->max_chain_skip, opt->min_lc_cnt, opt->min_lc_score, is_splice, n_segs, n_a, a, &n_lc, &u, b->km);
}
}
#endif
b->frag_gap = max_chain_gap_ref;
kfree(b->km, mv.a);
lc = mg_lchain_gen(b->km, hash, qlen_sum, n_lc, u, a);
for (i = 0; i < n_lc; ++i)
mg_update_anchors(lc[i].cnt, &a[lc[i].off], n_mini_pos, mini_pos);
kfree(b->km, mini_pos);
kfree(b->km, u);
if (mg_dbg_flag & MG_DBG_LCHAIN)
mg_print_lchain(stdout, gi, n_lc, lc, a, qname);
n_gc = mg_gchain1_dp(b->km, gi->g, &n_lc, lc, qlen_sum, max_chain_gap_ref, max_chain_gap_qry, opt->bw, seqs[0], &u);
gcs[0] = mg_gchain_gen(0, b->km, gi->g, n_gc, u, lc, a, hash, opt->min_gc_cnt, opt->min_gc_score);
gcs[0]->rep_len = rep_len;
kfree(b->km, a);
kfree(b->km, lc);
kfree(b->km, u);
mg_gchain_set_parent(b->km, opt->mask_level, gcs[0]->n_gc, gcs[0]->gc, opt->sub_diff, 0);
mg_gchain_flt_sub(opt->pri_ratio, gi->k * 2, opt->best_n, gcs[0]->n_gc, gcs[0]->gc);
mg_gchain_drop_flt(b->km, gcs[0]);
mg_gchain_set_mapq(b->km, gcs[0], qlen_sum, mv.n, opt->min_gc_score);
if (b->km) {
km_stat(b->km, &kmst);
if (mg_dbg_flag & MG_DBG_QNAME)
fprintf(stderr, "QM\t%s\t%d\tcap=%ld,nCore=%ld,largest=%ld\n", qname, qlen_sum, kmst.capacity, kmst.n_cores, kmst.largest);
assert(kmst.n_blocks == kmst.n_cores); // otherwise, there is a memory leak
if (kmst.largest > 1U<<28) {
km_destroy(b->km);
b->km = km_init();
}
}
}
mg_gchains_t *mg_map(const mg_idx_t *gi, int qlen, const char *seq, mg_tbuf_t *b, const mg_mapopt_t *opt, const char *qname)
{
mg_gchains_t *gcs;
mg_map_frag(gi, 1, &qlen, &seq, &gcs, b, opt, qname);
return gcs;
}
/**************************
* Multi-threaded mapping *
**************************/
typedef struct {
int mini_batch_size, n_processed, n_threads, n_fp;
const mg_mapopt_t *opt;
mg_bseq_file_t **fp;
const mg_idx_t *gi;
kstring_t str;
} pipeline_t;
typedef struct {
const pipeline_t *p;
int n_seq, n_frag;
mg_bseq1_t *seq;
int *seg_off, *n_seg;
mg_gchains_t **gcs;
mg_tbuf_t **buf;
} step_t;
static void worker_for(void *_data, long i, int tid) // kt_for() callback
{
step_t *s = (step_t*)_data;
int qlens[MG_MAX_SEG], j, off = s->seg_off[i], pe_ori = s->p->opt->pe_ori;
const char *qseqs[MG_MAX_SEG];
mg_tbuf_t *b = s->buf[tid];
assert(s->n_seg[i] <= MG_MAX_SEG);
if (mg_dbg_flag & MG_DBG_QNAME)
fprintf(stderr, "QR\t%s\t%d\t%d\n", s->seq[off].name, tid, s->seq[off].l_seq);
for (j = 0; j < s->n_seg[i]; ++j) {
if (s->n_seg[i] == 2 && ((j == 0 && (pe_ori>>1&1)) || (j == 1 && (pe_ori&1))))
mg_revcomp_bseq(&s->seq[off + j]);
qlens[j] = s->seq[off + j].l_seq;
qseqs[j] = s->seq[off + j].seq;
}
if (s->p->opt->flag & MG_M_INDEPEND_SEG) {
for (j = 0; j < s->n_seg[i]; ++j)
mg_map_frag(s->p->gi, 1, &qlens[j], &qseqs[j], &s->gcs[off+j], b, s->p->opt, s->seq[off+j].name);
} else {
mg_map_frag(s->p->gi, s->n_seg[i], qlens, qseqs, &s->gcs[off], b, s->p->opt, s->seq[off].name);
}
#if 0 // for paired-end reads
for (j = 0; j < s->n_seg[i]; ++j) // flip the query strand and coordinate to the original read strand
if (s->n_seg[i] == 2 && ((j == 0 && (pe_ori>>1&1)) || (j == 1 && (pe_ori&1)))) {
int k, t;
mg_revcomp_bseq(&s->seq[off + j]);
for (k = 0; k < s->n_reg[off + j]; ++k) {
mg_lchain_t *r = &s->reg[off + j][k];
t = r->qs;
r->qs = qlens[j] - r->qe;
r->qe = qlens[j] - t;
r->v ^= 1;
}
}
#endif
}
static void *worker_pipeline(void *shared, int step, void *in)
{
int i, j, k;
pipeline_t *p = (pipeline_t*)shared;
if (step == 0) { // step 0: read sequences
int with_qual = !(p->opt->flag & MG_M_NO_QUAL);
int with_comment = !!(p->opt->flag & MG_M_COPY_COMMENT);
int frag_mode = (p->n_fp > 1 || !!(p->opt->flag & MG_M_FRAG_MODE));
step_t *s;
s = (step_t*)calloc(1, sizeof(step_t));
if (p->n_fp > 1) s->seq = mg_bseq_read_frag(p->n_fp, p->fp, p->mini_batch_size, with_qual, with_comment, &s->n_seq);
else s->seq = mg_bseq_read(p->fp[0], p->mini_batch_size, with_qual, with_comment, frag_mode, &s->n_seq);
if (s->seq) {
s->p = p;
for (i = 0; i < s->n_seq; ++i)
s->seq[i].rid = p->n_processed++;
s->buf = (mg_tbuf_t**)calloc(p->n_threads, sizeof(mg_tbuf_t*));
for (i = 0; i < p->n_threads; ++i)
s->buf[i] = mg_tbuf_init();
s->seg_off = (int*)calloc(2 * s->n_seq, sizeof(int));
s->n_seg = s->seg_off + s->n_seq; // n_seg, rep_len and frag_gap are allocated together with seg_off
KCALLOC(0, s->gcs, s->n_seq);
for (i = 1, j = 0; i <= s->n_seq; ++i)
if (i == s->n_seq || !frag_mode || !mg_qname_same(s->seq[i-1].name, s->seq[i].name)) {
s->n_seg[s->n_frag] = i - j;
s->seg_off[s->n_frag++] = j;
j = i;
}
return s;
} else free(s);
} else if (step == 1) { // step 1: map
kt_for(p->n_threads, worker_for, in, ((step_t*)in)->n_frag);
return in;
} else if (step == 2) { // step 2: output
void *km = 0;
step_t *s = (step_t*)in;
for (i = 0; i < p->n_threads; ++i) mg_tbuf_destroy(s->buf[i]);
free(s->buf);
if (!(mg_dbg_flag & MG_DBG_NO_KALLOC)) km = km_init();
for (k = 0; k < s->n_frag; ++k) {
int seg_st = s->seg_off[k], seg_en = s->seg_off[k] + s->n_seg[k];
for (i = seg_st; i < seg_en; ++i) {
mg_bseq1_t *t = &s->seq[i];
mg_write_paf(&p->str, p->gi->g, s->gcs[i], t->l_seq, t->name, p->opt->flag, km);
}
if (p->str.l) mg_err_fputs(p->str.s, stdout);
for (i = seg_st; i < seg_en; ++i) {
mg_gchain_free(s->gcs[i]);
free(s->seq[i].seq); free(s->seq[i].name);
if (s->seq[i].qual) free(s->seq[i].qual);
if (s->seq[i].comment) free(s->seq[i].comment);
}
}
free(s->gcs); free(s->seg_off); free(s->seq); // n_seg, rep_len and frag_gap were allocated with seg_off; no memory leak here
if (km) km_destroy(km);
if (mg_verbose >= 3)
fprintf(stderr, "[M::%s::%.3f*%.2f] mapped %d sequences\n", __func__, realtime() - mg_realtime0, cputime() / (realtime() - mg_realtime0), s->n_seq);
free(s);
}
return 0;
}
static mg_bseq_file_t **open_bseqs(int n, const char **fn)
{
mg_bseq_file_t **fp;
int i, j;
fp = (mg_bseq_file_t**)calloc(n, sizeof(mg_bseq_file_t*));
for (i = 0; i < n; ++i) {
if ((fp[i] = mg_bseq_open(fn[i])) == 0) {
if (mg_verbose >= 1)
fprintf(stderr, "ERROR: failed to open file '%s'\n", fn[i]);
for (j = 0; j < i; ++j)
mg_bseq_close(fp[j]);
free(fp);
return 0;
}
}
return fp;
}
int mg_map_file_frag(const mg_idx_t *idx, int n_segs, const char **fn, const mg_mapopt_t *opt, int n_threads)
{
int i, pl_threads;
pipeline_t pl;
if (n_segs < 1) return -1;
memset(&pl, 0, sizeof(pipeline_t));
pl.n_fp = n_segs;
pl.fp = open_bseqs(pl.n_fp, fn);
if (pl.fp == 0) return -1;
pl.opt = opt, pl.gi = idx;
pl.n_threads = n_threads > 1? n_threads : 1;
pl.mini_batch_size = opt->mini_batch_size;
pl_threads = n_threads == 1? 1 : (opt->flag&MG_M_2_IO_THREADS)? 3 : 2;
kt_pipeline(pl_threads, worker_pipeline, &pl, 3);
free(pl.str.s);
for (i = 0; i < pl.n_fp; ++i)
mg_bseq_close(pl.fp[i]);
free(pl.fp);
return 0;
}
int mg_map_file(const mg_idx_t *idx, const char *fn, const mg_mapopt_t *opt, int n_threads)
{
return mg_map_file_frag(idx, 1, &fn, opt, n_threads);
}