7 #include "bam_maqcns.h"
12 typedef int *indel_list_t;
13 KHASH_MAP_INIT_INT64(64, indel_list_t)
15 #define BAM_PLF_SIMPLE 0x01
16 #define BAM_PLF_CNS 0x02
17 #define BAM_PLF_INDEL_ONLY 0x04
18 #define BAM_PLF_GLF 0x08
19 #define BAM_PLF_VAR_ONLY 0x10
20 #define BAM_PLF_2ND 0x20
21 #define BAM_PLF_RANBASE 0x40
22 #define BAM_PLF_1STBASE 0x80
23 #define BAM_PLF_ALLBASE 0x100
24 #define BAM_PLF_READPOS 0x200
29 bam_maqindel_opt_t *ido;
33 int tid, len, last_pos;
35 int max_depth; // for indel calling, ignore reads with the depth too high. 0 for unlimited
37 glfFile fp_glf; // for glf output only
40 char **__bam_get_lines(const char *fn, int *_n);
41 void bam_init_header_hash(bam_header_t *header);
42 int32_t bam_get_tid(const bam_header_t *header, const char *seq_name);
44 static khash_t(64) *load_pos(const char *fn, bam_header_t *h)
47 int i, j, n, *fields, max_fields;
49 bam_init_header_hash(h);
50 list = __bam_get_lines(fn, &n);
52 max_fields = 0; fields = 0;
53 for (i = 0; i < n; ++i) {
55 int chr, n_fields, ret;
58 n_fields = ksplit_core(str, 0, &max_fields, &fields);
59 if (n_fields < 2) continue;
60 chr = bam_get_tid(h, str + fields[0]);
62 fprintf(stderr, "[load_pos] unknown reference sequence name: %s\n", str + fields[0]);
65 x = (uint64_t)chr << 32 | (atoi(str + fields[1]) - 1);
66 k = kh_put(64, hash, x, &ret);
68 fprintf(stderr, "[load_pos] position %s:%s has been loaded.\n", str+fields[0], str+fields[1]);
74 for (j = 2; j < n_fields; ++j) {
75 char *s = str + fields[j];
76 if ((*s != '+' && *s != '-') || !isdigit(s[1])) break;
78 if (j > 2) { // update kh_val()
80 q = kh_val(hash, k) = (int*)calloc(j - 1, sizeof(int));
81 q[0] = j - 2; z = j; y = 1;
82 for (j = 2; j < z; ++j)
83 q[y++] = atoi(str + fields[j]);
88 free(list); free(fields);
92 // an analogy to pileup_func() below
93 static int glt3_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pu, void *data)
95 pu_data_t *d = (pu_data_t*)data;
96 bam_maqindel_ret_t *r = 0;
97 int rb, *proposed_indels = 0;
102 fprintf(stderr, "[glt3_func] reference sequence is required for generating GLT. Abort!\n");
105 if (d->hash) { // only output a list of sites
106 khint_t k = kh_get(64, d->hash, (uint64_t)tid<<32|pos);
107 if (k == kh_end(d->hash)) return 0;
108 proposed_indels = kh_val(d->hash, k);
111 if (d->fai && (int)tid != d->tid) {
112 if (d->ref) { // then write the end mark
113 g3->rtype = GLF3_RTYPE_END;
114 glf3_write1(d->fp_glf, g3);
116 glf3_ref_write(d->fp_glf, d->h->target_name[tid], d->h->target_len[tid]); // write reference
118 d->ref = fai_fetch(d->fai, d->h->target_name[tid], &d->len);
122 rb = (d->ref && (int)pos < d->len)? d->ref[pos] : 'N';
123 g = bam_maqcns_glfgen(n, pu, bam_nt16_table[rb], d->c);
124 memcpy(g3, g, sizeof(glf1_t));
125 g3->rtype = GLF3_RTYPE_SUB;
126 g3->offset = pos - d->last_pos;
128 glf3_write1(d->fp_glf, g3);
130 int m = (!d->max_depth || d->max_depth>n) ? n : d->max_depth;
132 r = bam_maqindel(m, pos, d->ido, pu, d->ref, proposed_indels[0], proposed_indels+1);
133 else r = bam_maqindel(m, pos, d->ido, pu, d->ref, 0, 0);
135 if (r) { // then write indel line
136 int het = 3 * n, min;
138 if (min > r->gl[0]) min = r->gl[0];
139 if (min > r->gl[1]) min = r->gl[1];
141 g3->rtype = GLF3_RTYPE_INDEL;
142 memset(g3->lk, 0, 10);
143 g3->lk[0] = r->gl[0] - min < 255? r->gl[0] - min : 255;
144 g3->lk[1] = r->gl[1] - min < 255? r->gl[1] - min : 255;
145 g3->lk[2] = het - min < 255? het - min : 255;
147 g3->indel_len[0] = r->indel1;
148 g3->indel_len[1] = r->indel2;
149 g3->min_lk = min < 255? min : 255;
150 g3->max_len = (abs(r->indel1) > abs(r->indel2)? abs(r->indel1) : abs(r->indel2)) + 1;
151 g3->indel_seq[0] = strdup(r->s[0]+1);
152 g3->indel_seq[1] = strdup(r->s[1]+1);
153 glf3_write1(d->fp_glf, g3);
154 bam_maqindel_ret_destroy(r);
161 static void pileup_seq(const bam_pileup1_t *p, int pos, int ref_len, const char *ref)
163 if (p->is_head) printf("^%c", p->b->core.qual > 93? 126 : p->b->core.qual + 33);
165 int j, rb, c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos)];
166 rb = (ref && pos < ref_len)? ref[pos] : 'N';
167 if (c == '=' || toupper(c) == toupper(rb)) c = bam1_strand(p->b)? ',' : '.';
168 else c = bam1_strand(p->b)? tolower(c) : toupper(c);
171 printf("+%d", p->indel);
172 for (j = 1; j <= p->indel; ++j) {
173 c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos + j)];
174 putchar(bam1_strand(p->b)? tolower(c) : toupper(c));
176 } else if (p->indel < 0) {
177 printf("%d", p->indel);
178 for (j = 1; j <= -p->indel; ++j) {
179 c = (ref && (int)pos+j < ref_len)? ref[pos+j] : 'N';
180 putchar(bam1_strand(p->b)? tolower(c) : toupper(c));
184 if (p->is_tail) putchar('$');
187 static int pileup_func(uint32_t tid, uint32_t pos, int n, const bam_pileup1_t *pu, void *data)
189 pu_data_t *d = (pu_data_t*)data;
190 bam_maqindel_ret_t *r = 0;
191 int i, rb, rms_mapq = -1, *proposed_indels = 0;
195 // if GLF is required, suppress -c completely
196 if (d->format & BAM_PLF_GLF) return glt3_func(tid, pos, n, pu, data);
197 // if d->hash is initialized, only output the sites in the hash table
199 khint_t k = kh_get(64, d->hash, (uint64_t)tid<<32|pos);
200 if (k == kh_end(d->hash)) return 0;
201 proposed_indels = kh_val(d->hash, k);
203 // update d->ref if necessary
204 if (d->fai && (int)tid != d->tid) {
206 d->ref = faidx_fetch_seq(d->fai, d->h->target_name[tid], 0, 0x7fffffff, &d->len);
209 rb = (d->ref && (int)pos < d->len)? d->ref[pos] : 'N';
210 // when the indel-only mode is asked for, return if no reads mapped with indels
211 if (d->format & BAM_PLF_INDEL_ONLY) {
212 for (i = 0; i < n; ++i)
213 if (pu[i].indel != 0) break;
214 if (i == n) return 0;
216 // call the consensus and indel
217 if (d->format & BAM_PLF_CNS) { // call consensus
218 if (d->format & (BAM_PLF_RANBASE|BAM_PLF_1STBASE)) { // use a random base or the 1st base as the consensus call
219 const bam_pileup1_t *p = (d->format & BAM_PLF_1STBASE)? pu : pu + (int)(drand48() * n);
220 int q = bam1_qual(p->b)[p->qpos];
221 int mapQ = p->b->core.qual < d->c->cap_mapQ? p->b->core.qual : d->c->cap_mapQ;
222 uint32_t b = bam1_seqi(bam1_seq(p->b), p->qpos);
223 cns = b<<28 | 0xf<<24 | mapQ<<16 | q<<8;
224 } else if (d->format & BAM_PLF_ALLBASE) { // collapse all bases
227 for (i = 0; i < n; ++i) {
228 const bam_pileup1_t *p = pu + i;
229 int q = p->b->core.qual < d->c->cap_mapQ? p->b->core.qual : d->c->cap_mapQ;
230 b |= bam1_seqi(bam1_seq(p->b), p->qpos);
233 rmsQ = (uint64_t)(sqrt((double)rmsQ / n) + .499);
234 cns = b<<28 | 0xf<<24 | rmsQ<<16 | 60<<8;
235 } else cns = bam_maqcns_call(n, pu, d->c);
237 if ((d->format & (BAM_PLF_CNS|BAM_PLF_INDEL_ONLY)) && d->ref && pos < d->len) { // call indels
238 int m = (!d->max_depth || d->max_depth>n) ? n : d->max_depth;
239 if (proposed_indels) // the first element gives the size of the array
240 r = bam_maqindel(m, pos, d->ido, pu, d->ref, proposed_indels[0], proposed_indels+1);
241 else r = bam_maqindel(m, pos, d->ido, pu, d->ref, 0, 0);
243 // when only variant sites are asked for, test if the site is a variant
244 if ((d->format & BAM_PLF_CNS) && (d->format & BAM_PLF_VAR_ONLY)) {
245 if (!(bam_nt16_table[rb] != 15 && cns>>28 != bam_nt16_table[rb])) { // not a SNP
246 if (!(r && (r->gt == 2 || strcmp(r->s[r->gt], "*")))) { // not an indel
247 if (r) bam_maqindel_ret_destroy(r);
252 // print the first 3 columns
253 printf("%s\t%d\t%c\t", d->h->target_name[tid], pos + 1, rb);
254 // print consensus information if required
255 if (d->format & BAM_PLF_CNS) {
256 int ref_q, rb4 = bam_nt16_table[rb];
258 if (rb4 != 15 && cns>>28 != 15 && cns>>28 != rb4) { // a SNP
259 ref_q = ((cns>>24&0xf) == rb4)? cns>>8&0xff : (cns>>8&0xff) + (cns&0xff);
260 if (ref_q > 255) ref_q = 255;
262 rms_mapq = cns>>16&0xff;
263 printf("%c\t%d\t%d\t%d\t", bam_nt16_rev_table[cns>>28], cns>>8&0xff, ref_q, rms_mapq);
265 // print pileup sequences
267 rms_aux = 0; // we need to recalculate rms_mapq when -c is not flagged on the command line
268 for (i = 0; i < n; ++i) {
269 const bam_pileup1_t *p = pu + i;
270 int tmp = p->b->core.qual < d->c->cap_mapQ? p->b->core.qual : d->c->cap_mapQ;
271 rms_aux += tmp * tmp;
272 pileup_seq(p, pos, d->len, d->ref);
275 rms_aux = (uint64_t)(sqrt((double)rms_aux / n) + .499);
276 if (rms_mapq < 0) rms_mapq = rms_aux;
279 for (i = 0; i < n; ++i) {
280 const bam_pileup1_t *p = pu + i;
281 int c = bam1_qual(p->b)[p->qpos] + 33;
282 if (c > 126) c = 126;
285 if (d->format & BAM_PLF_2ND) { // print 2nd calls and qualities
286 const unsigned char *q;
288 for (i = 0; i < n; ++i) {
289 const bam_pileup1_t *p = pu + i;
290 q = bam_aux_get(p->b, "E2");
291 putchar(q? q[p->qpos + 1] : 'N');
294 for (i = 0; i < n; ++i) {
295 const bam_pileup1_t *p = pu + i;
296 q = bam_aux_get(p->b, "U2");
297 putchar(q? q[p->qpos + 1] : '!');
300 // print mapping quality if -s is flagged on the command line
301 if (d->format & BAM_PLF_SIMPLE) {
303 for (i = 0; i < n; ++i) {
304 int c = pu[i].b->core.qual + 33;
305 if (c > 126) c = 126;
309 // print read position
310 if (d->format & BAM_PLF_READPOS) {
312 for (i = 0; i < n; ++i) {
314 int l = pu[i].b->core.l_qseq;
315 printf("%d,", x < l/2? x+1 : -((l-1)-x+1));
319 // print the indel line if r has been calculated. This only happens if:
320 // a) -c or -i are flagged, AND b) the reference sequence is available
322 printf("%s\t%d\t*\t", d->h->target_name[tid], pos + 1);
323 if (r->gt < 2) printf("%s/%s\t", r->s[r->gt], r->s[r->gt]);
324 else printf("%s/%s\t", r->s[0], r->s[1]);
325 printf("%d\t%d\t", r->q_cns, r->q_ref);
326 printf("%d\t%d\t", rms_mapq, n);
327 printf("%s\t%s\t", r->s[0], r->s[1]);
328 //printf("%d\t%d\t", r->gl[0], r->gl[1]);
329 printf("%d\t%d\t%d\t", r->cnt1, r->cnt2, r->cnt_anti);
330 printf("%d\t%d\n", r->cnt_ref, r->cnt_ambi);
331 bam_maqindel_ret_destroy(r);
336 int bam_pileup(int argc, char *argv[])
339 char *fn_list = 0, *fn_fa = 0, *fn_pos = 0;
340 pu_data_t *d = (pu_data_t*)calloc(1, sizeof(pu_data_t));
342 d->tid = -1; d->mask = BAM_DEF_MASK;
343 d->c = bam_maqcns_init();
344 d->c->errmod = BAM_ERRMOD_MAQ2; // change the default model
345 d->ido = bam_maqindel_opt_init();
346 while ((c = getopt(argc, argv, "st:f:cT:N:r:l:d:im:gI:G:vM:S2aR:PA")) >= 0) {
348 case 'a': d->c->errmod = BAM_ERRMOD_SOAP; break;
349 case 'A': d->c->errmod = BAM_ERRMOD_MAQ; break;
350 case 's': d->format |= BAM_PLF_SIMPLE; break;
351 case 't': fn_list = strdup(optarg); break;
352 case 'l': fn_pos = strdup(optarg); break;
353 case 'f': fn_fa = strdup(optarg); break;
354 case 'T': d->c->theta = atof(optarg); break;
355 case 'N': d->c->n_hap = atoi(optarg); break;
356 case 'r': d->c->het_rate = atof(optarg); d->ido->r_snp = d->c->het_rate; break;
357 case 'M': d->c->cap_mapQ = atoi(optarg); break;
358 case 'd': d->max_depth = atoi(optarg); break;
359 case 'c': d->format |= BAM_PLF_CNS; break;
360 case 'i': d->format |= BAM_PLF_INDEL_ONLY; break;
361 case 'v': d->format |= BAM_PLF_VAR_ONLY; break;
362 case 'm': d->mask = strtol(optarg, 0, 0); break;
363 case 'g': d->format |= BAM_PLF_GLF; break;
364 case '2': d->format |= BAM_PLF_2ND; break;
365 case 'P': d->format |= BAM_PLF_READPOS; break;
366 case 'I': d->ido->q_indel = atoi(optarg); break;
367 case 'G': d->ido->r_indel = atof(optarg); break;
368 case 'S': is_SAM = 1; break;
370 if (strcmp(optarg, "random") == 0) d->format |= BAM_PLF_RANBASE;
371 else if (strcmp(optarg, "first") == 0) d->format |= BAM_PLF_1STBASE;
372 else if (strcmp(optarg, "all") == 0) d->format |= BAM_PLF_ALLBASE;
373 else fprintf(stderr, "[bam_pileup] unrecognized -R\n");
375 default: fprintf(stderr, "Unrecognizd option '-%c'.\n", c); return 1;
378 if (d->c->errmod != BAM_ERRMOD_MAQ2) d->c->theta += 0.02;
379 if (d->c->theta > 1.0) d->c->theta = 1.0;
380 if (fn_list) is_SAM = 1;
381 if (optind == argc) {
382 fprintf(stderr, "\n");
383 fprintf(stderr, "Usage: samtools pileup [options] <in.bam>|<in.sam>\n\n");
384 fprintf(stderr, "Option: -s simple (yet incomplete) pileup format\n");
385 fprintf(stderr, " -S the input is in SAM\n");
386 fprintf(stderr, " -A use the MAQ model for SNP calling\n");
387 fprintf(stderr, " -2 output the 2nd best call and quality\n");
388 fprintf(stderr, " -i only show lines/consensus with indels\n");
389 fprintf(stderr, " -m INT filtering reads with bits in INT [%d]\n", d->mask);
390 fprintf(stderr, " -M INT cap mapping quality at INT [%d]\n", d->c->cap_mapQ);
391 fprintf(stderr, " -d INT limit maximum depth for indels [unlimited]\n");
392 fprintf(stderr, " -t FILE list of reference sequences (force -S)\n");
393 fprintf(stderr, " -l FILE list of sites at which pileup is output\n");
394 fprintf(stderr, " -f FILE reference sequence in the FASTA format\n\n");
395 fprintf(stderr, " -c output the SOAPsnp consensus sequence\n");
396 fprintf(stderr, " -v print variants only (for -c)\n");
397 fprintf(stderr, " -g output in the GLFv3 format (suppressing -c/-i/-s)\n");
398 fprintf(stderr, " -T FLOAT theta in maq consensus calling model (for -c/-g) [%f]\n", d->c->theta);
399 fprintf(stderr, " -N INT number of haplotypes in the sample (for -c/-g) [%d]\n", d->c->n_hap);
400 fprintf(stderr, " -r FLOAT prior of a difference between two haplotypes (for -c/-g) [%f]\n", d->c->het_rate);
401 fprintf(stderr, " -G FLOAT prior of an indel between two haplotypes (for -c/-g) [%f]\n", d->ido->r_indel);
402 fprintf(stderr, " -I INT phred prob. of an indel in sequencing/prep. (for -c/-g) [%d]\n", d->ido->q_indel);
403 fprintf(stderr, "\n");
404 free(fn_list); free(fn_fa); free(d);
407 if (d->format & (BAM_PLF_RANBASE|BAM_PLF_1STBASE|BAM_PLF_ALLBASE)) d->format |= BAM_PLF_CNS;
408 if (fn_fa) d->fai = fai_load(fn_fa);
409 if (d->format & (BAM_PLF_CNS|BAM_PLF_GLF)) bam_maqcns_prepare(d->c); // consensus calling
410 if (d->format & BAM_PLF_GLF) { // for glf output
412 h = glf3_header_init();
413 d->fp_glf = bgzf_fdopen(fileno(stdout), "w");
414 glf3_header_write(d->fp_glf, h);
415 glf3_header_destroy(h);
417 if (d->fai == 0 && (d->format & (BAM_PLF_CNS|BAM_PLF_INDEL_ONLY)))
418 fprintf(stderr, "[bam_pileup] indels will not be called when -f is absent.\n");
419 if (fn_fa && is_SAM && fn_list == 0) fn_list = samfaipath(fn_fa);
423 fp = is_SAM? samopen(argv[optind], "r", fn_list) : samopen(argv[optind], "rb", 0);
424 if (fp == 0 || fp->header == 0) {
425 fprintf(stderr, "[bam_pileup] fail to read the header: non-exisiting file or wrong format.\n");
429 if (fn_pos) d->hash = load_pos(fn_pos, d->h);
430 sampileup(fp, d->mask, pileup_func, d);
431 samclose(fp); // d->h will be destroyed here
435 if (d->format & BAM_PLF_GLF) bgzf_close(d->fp_glf);
436 if (fn_pos) { // free the hash table
438 for (k = kh_begin(d->hash); k < kh_end(d->hash); ++k)
439 if (kh_exist(d->hash, k)) free(kh_val(d->hash, k));
440 kh_destroy(64, d->hash);
442 free(fn_pos); free(fn_list); free(fn_fa);
443 if (d->fai) fai_destroy(d->fai);
444 bam_maqcns_destroy(d->c);
445 free(d->ido); free(d->ref); free(d);
457 #define MPLP_GLF 0x10
458 #define MPLP_NO_COMP 0x20
459 #define MPLP_NO_ORPHAN 0x40
460 #define MPLP_REALN 0x80
463 int max_mq, min_mq, flag, min_baseQ, capQ_thres;
472 int min_mq, flag, ref_id, capQ_thres;
482 static int mplp_func(void *data, bam1_t *b)
484 extern int bam_realn(bam1_t *b, const char *ref);
485 extern int bam_prob_realn(bam1_t *b, const char *ref);
486 extern int bam_cap_mapQ(bam1_t *b, char *ref, int thres);
487 mplp_aux_t *ma = (mplp_aux_t*)data;
490 int has_ref = (ma->ref && ma->ref_id == b->core.tid)? 1 : 0;
491 ret = ma->iter? bam_iter_read(ma->fp, ma->iter, b) : bam_read1(ma->fp, b);
494 if (has_ref && (ma->flag&MPLP_REALN)) bam_prob_realn(b, ma->ref);
495 if (has_ref && ma->capQ_thres > 10) {
496 int q = bam_cap_mapQ(b, ma->ref, ma->capQ_thres);
498 else if (b->core.qual > q) b->core.qual = q;
499 } else if (b->core.flag&BAM_FUNMAP) skip = 1;
500 else if (b->core.qual < ma->min_mq) skip = 1;
501 else if ((ma->flag&MPLP_NO_ORPHAN) && (b->core.flag&1) && !(b->core.flag&2)) skip = 1;
506 static void group_smpl(mplp_pileup_t *m, bam_sample_t *sm, kstring_t *buf,
507 int n, char *const*fn, int *n_plp, const bam_pileup1_t **plp)
510 memset(m->n_plp, 0, m->n * sizeof(int));
511 for (i = 0; i < n; ++i) {
512 for (j = 0; j < n_plp[i]; ++j) {
513 const bam_pileup1_t *p = plp[i] + j;
516 q = bam_aux_get(p->b, "RG");
517 if (q) id = bam_smpl_rg2smid(sm, fn[i], (char*)q+1, buf);
518 if (id < 0) id = bam_smpl_rg2smid(sm, fn[i], 0, buf);
519 assert(id >= 0 && id < m->n);
520 if (m->n_plp[id] == m->m_plp[id]) {
521 m->m_plp[id] = m->m_plp[id]? m->m_plp[id]<<1 : 8;
522 m->plp[id] = realloc(m->plp[id], sizeof(bam_pileup1_t) * m->m_plp[id]);
524 m->plp[id][m->n_plp[id]++] = *p;
529 static int mpileup(mplp_conf_t *conf, int n, char **fn)
532 int i, tid, pos, *n_plp, beg0 = 0, end0 = 1u<<29, ref_len, ref_tid;
533 const bam_pileup1_t **plp;
537 khash_t(64) *hash = 0;
539 bcf_callaux_t *bca = 0;
540 bcf_callret1_t *bcr = 0;
545 bam_sample_t *sm = 0;
549 memset(&gplp, 0, sizeof(mplp_pileup_t));
550 memset(&buf, 0, sizeof(kstring_t));
551 memset(&bc, 0, sizeof(bcf_call_t));
552 data = calloc(n, sizeof(void*));
553 plp = calloc(n, sizeof(void*));
554 n_plp = calloc(n, sizeof(int*));
555 sm = bam_smpl_init();
557 // read the header and initialize data
558 for (i = 0; i < n; ++i) {
560 data[i] = calloc(1, sizeof(mplp_aux_t));
561 data[i]->min_mq = conf->min_mq;
562 data[i]->flag = conf->flag;
563 data[i]->capQ_thres = conf->capQ_thres;
564 data[i]->fp = strcmp(fn[i], "-") == 0? bam_dopen(fileno(stdin), "r") : bam_open(fn[i], "r");
565 h_tmp = bam_header_read(data[i]->fp);
566 bam_smpl_add(sm, fn[i], h_tmp->text);
570 idx = bam_index_load(fn[i]);
572 fprintf(stderr, "[%s] fail to load index for %d-th input.\n", __func__, i+1);
575 if (bam_parse_region(h_tmp, conf->reg, &tid, &beg, &end) < 0) {
576 fprintf(stderr, "[%s] malformatted region or wrong seqname for %d-th input.\n", __func__, i+1);
579 if (i == 0) beg0 = beg, end0 = end;
580 data[i]->iter = bam_iter_query(idx, tid, beg, end);
581 bam_index_destroy(idx);
583 if (i == 0) h = h_tmp;
585 // FIXME: to check consistency
586 bam_header_destroy(h_tmp);
590 gplp.n_plp = calloc(sm->n, sizeof(int));
591 gplp.m_plp = calloc(sm->n, sizeof(int));
592 gplp.plp = calloc(sm->n, sizeof(void*));
594 fprintf(stderr, "[%s] %d samples in %d input files\n", __func__, sm->n, n);
595 if (conf->fn_pos) hash = load_pos(conf->fn_pos, h);
596 // write the VCF header
597 if (conf->flag & MPLP_GLF) {
599 bh = calloc(1, sizeof(bcf_hdr_t));
600 s.l = s.m = 0; s.s = 0;
601 bp = bcf_open("-", (conf->flag&MPLP_NO_COMP)? "wu" : "w");
602 for (i = 0; i < h->n_targets; ++i) {
603 kputs(h->target_name[i], &s);
607 bh->name = malloc(s.l);
608 memcpy(bh->name, s.s, s.l);
610 for (i = 0; i < sm->n; ++i) {
611 kputs(sm->smpl[i], &s); kputc('\0', &s);
614 bh->sname = malloc(s.l);
615 memcpy(bh->sname, s.s, s.l);
619 bcf_hdr_write(bp, bh);
620 bca = bcf_call_init(-1., conf->min_baseQ);
621 bcr = calloc(sm->n, sizeof(bcf_callret1_t));
623 ref_tid = -1; ref = 0;
624 iter = bam_mplp_init(n, mplp_func, (void**)data);
625 while (bam_mplp_auto(iter, &tid, &pos, n_plp, plp) > 0) {
626 if (conf->reg && (pos < beg0 || pos >= end0)) continue; // out of the region requested
629 k = kh_get(64, hash, (uint64_t)tid<<32 | pos);
630 if (k == kh_end(hash)) continue;
632 if (tid != ref_tid) {
634 if (conf->fai) ref = fai_fetch(conf->fai, h->target_name[tid], &ref_len);
635 for (i = 0; i < n; ++i) data[i]->ref = ref, data[i]->ref_id = tid;
638 if (conf->flag & MPLP_GLF) {
640 bcf1_t *b = calloc(1, sizeof(bcf1_t));
641 group_smpl(&gplp, sm, &buf, n, fn, n_plp, plp);
642 _ref0 = (ref && pos < ref_len)? ref[pos] : 'N';
643 ref16 = bam_nt16_table[_ref0];
644 for (i = 0; i < gplp.n; ++i)
645 bcf_call_glfgen(gplp.n_plp[i], gplp.plp[i], ref16, bca, bcr + i);
646 bcf_call_combine(gplp.n, bcr, ref16, &bc);
647 bcf_call2bcf(tid, pos, &bc, b);
648 bcf_write(bp, bh, b);
651 printf("%s\t%d\t%c", h->target_name[tid], pos + 1, (ref && pos < ref_len)? ref[pos] : 'N');
652 for (i = 0; i < n; ++i) {
654 printf("\t%d\t", n_plp[i]);
655 if (n_plp[i] == 0) printf("*\t*");
657 for (j = 0; j < n_plp[i]; ++j)
658 pileup_seq(plp[i] + j, pos, ref_len, ref);
660 for (j = 0; j < n_plp[i]; ++j) {
661 const bam_pileup1_t *p = plp[i] + j;
662 int c = bam1_qual(p->b)[p->qpos] + 33;
663 if (c > 126) c = 126;
673 bam_smpl_destroy(sm); free(buf.s);
674 for (i = 0; i < gplp.n; ++i) free(gplp.plp[i]);
675 free(gplp.plp); free(gplp.n_plp); free(gplp.m_plp);
676 if (hash) { // free the hash table
678 for (k = kh_begin(hash); k < kh_end(hash); ++k)
679 if (kh_exist(hash, k)) free(kh_val(hash, k));
680 kh_destroy(64, hash);
682 bcf_hdr_destroy(bh); bcf_call_destroy(bca); free(bc.PL); free(bcr);
683 bam_mplp_destroy(iter);
684 bam_header_destroy(h);
685 for (i = 0; i < n; ++i) {
686 bam_close(data[i]->fp);
687 if (data[i]->iter) bam_iter_destroy(data[i]->iter);
690 free(data); free(plp); free(ref); free(n_plp);
694 int bam_mpileup(int argc, char *argv[])
698 memset(&mplp, 0, sizeof(mplp_conf_t));
702 while ((c = getopt(argc, argv, "gf:r:l:M:q:Q:uaORC:")) >= 0) {
705 mplp.fai = fai_load(optarg);
706 if (mplp.fai == 0) return 1;
708 case 'r': mplp.reg = strdup(optarg); break;
709 case 'l': mplp.fn_pos = strdup(optarg); break;
710 case 'g': mplp.flag |= MPLP_GLF; break;
711 case 'u': mplp.flag |= MPLP_NO_COMP | MPLP_GLF; break;
712 case 'a': mplp.flag |= MPLP_NO_ORPHAN | MPLP_REALN; break;
713 case 'O': mplp.flag |= MPLP_NO_ORPHAN; break;
714 case 'R': mplp.flag |= MPLP_REALN; break;
715 case 'C': mplp.capQ_thres = atoi(optarg); break;
716 case 'M': mplp.max_mq = atoi(optarg); break;
717 case 'q': mplp.min_mq = atoi(optarg); break;
718 case 'Q': mplp.min_baseQ = atoi(optarg); break;
722 fprintf(stderr, "\n");
723 fprintf(stderr, "Usage: samtools mpileup [options] in1.bam [in2.bam [...]]\n\n");
724 fprintf(stderr, "Options: -f FILE reference sequence file [null]\n");
725 fprintf(stderr, " -r STR region in which pileup is generated [null]\n");
726 fprintf(stderr, " -l FILE list of positions (format: chr pos) [null]\n");
727 fprintf(stderr, " -M INT cap mapping quality at INT [%d]\n", mplp.max_mq);
728 fprintf(stderr, " -Q INT min base quality [%d]\n", mplp.min_baseQ);
729 fprintf(stderr, " -q INT filter out alignment with MQ smaller than INT [%d]\n", mplp.min_mq);
730 fprintf(stderr, " -g generate BCF output\n");
731 fprintf(stderr, " -u do not compress BCF output\n");
732 fprintf(stderr, "\n");
733 fprintf(stderr, "Notes: Assuming diploid individuals.\n\n");
736 mpileup(&mplp, argc - optind, argv + optind);
738 if (mplp.fai) fai_destroy(mplp.fai);