Debian Codegraph @

bcftools/mpileup.c.pysam.c

raw symbols SCIP
1 #include "bcftools.pysam.h"
2
3 /* mpileup.c -- mpileup subcommand. Previously bam_plcmd.c from samtools
4
5 Copyright (C) 2008-2024 Genome Research Ltd.
6 Portions copyright (C) 2009-2012 Broad Institute.
7
8 Author: Heng Li <[email protected]>
9
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files (the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE. */
27
28 #include <math.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <ctype.h>
33 #include <string.h>
34 #include <strings.h>
35 #include <limits.h>
36 #include <inttypes.h>
37 #include <errno.h>
38 #include <sys/stat.h>
39 #include <getopt.h>
40 #include <htslib/sam.h>
41 #include <htslib/faidx.h>
42 #include <htslib/kstring.h>
43 #include <htslib/khash_str2int.h>
44 #include <htslib/hts_os.h>
45 #include <assert.h>
46 #include "regidx.h"
47 #include "bcftools.h"
48 #include "bam2bcf.h"
49 #include "bam_sample.h"
50 #include "gvcf.h"
51
52 #define MPLP_BCF 1
53 #define MPLP_VCF (1<<1)
54 #define MPLP_NO_COMP (1<<2)
55 #define MPLP_NO_ORPHAN (1<<3)
56 #define MPLP_REALN (1<<4)
57 #define MPLP_NO_INDEL (1<<5)
58 #define MPLP_REDO_BAQ (1<<6)
59 #define MPLP_ILLUMINA13 (1<<7)
60 #define MPLP_IGNORE_RG (1<<8)
61 #define MPLP_PRINT_POS (1<<9)
62 #define MPLP_PRINT_MAPQ (1<<10)
63 #define MPLP_PER_SAMPLE (1<<11)
64 #define MPLP_SMART_OVERLAPS (1<<12)
65 #define MPLP_REALN_PARTIAL (1<<13)
66
67 typedef struct _mplp_aux_t mplp_aux_t;
68 typedef struct _mplp_pileup_t mplp_pileup_t;
69
70 // Data shared by all bam files
71 typedef struct {
72 int min_mq, flag, min_baseQ, max_baseQ, delta_baseQ, capQ_thres, max_depth,
73 max_indel_depth, max_read_len, ambig_reads;
74 uint32_t fmt_flag;
75 int rflag_skip_any_unset, rflag_skip_all_unset, rflag_skip_any_set, rflag_skip_all_set, output_type;
76 int openQ, extQ, tandemQ, min_support, indel_win_size; // for indels
77 int seqQ_offset;
78 double min_frac; // for indels
79 double indel_bias, poly_mqual;
80 double del_bias; // compensate for diff deletion vs insertion error rates
81 double vs_ref;
82 char *reg_fname, *pl_list, *fai_fname, *output_fname;
83 int reg_is_file, record_cmd_line, n_threads, clevel;
84 faidx_t *fai;
85 regidx_t *bed, *reg; // bed: skipping regions, reg: index-jump to regions
86 regitr_t *bed_itr, *reg_itr;
87 int bed_logic; // 1: include region, 0: exclude region
88 gvcf_t *gvcf;
89
90 // auxiliary structures for calling
91 bcf_callaux_t *bca;
92 bcf_callret1_t *bcr;
93 bcf_call_t bc;
94 bam_mplp_t iter;
95 mplp_aux_t **mplp_data;
96 int nfiles;
97 char **files;
98 mplp_pileup_t *gplp;
99 int *n_plp;
100 const bam_pileup1_t **plp;
101 bam_smpl_t *bsmpl;
102 kstring_t buf;
103 bcf1_t *bcf_rec;
104 htsFile *bcf_fp;
105 bcf_hdr_t *bcf_hdr;
106 int indels_v20;
107 int edlib;
108 int argc;
109 char **argv;
110 int write_index;
111 char *index_fn;
112 } mplp_conf_t;
113
114 typedef struct {
115 char *ref[2];
116 int ref_id[2];
117 int ref_len[2];
118 } mplp_ref_t;
119
120 #define MPLP_REF_INIT {{NULL,NULL},{-1,-1},{0,0}}
121
122 // Data specific to each bam file
123 struct _mplp_aux_t {
124 samFile *fp;
125 hts_itr_t *iter;
126 bam_hdr_t *h;
127 mplp_ref_t *ref;
128 const mplp_conf_t *conf;
129 int bam_id;
130 hts_idx_t *idx; // maintained only with more than one -r regions
131 };
132
133 // Data passed to htslib/mpileup
134 struct _mplp_pileup_t {
135 int n;
136 int *n_plp, *m_plp;
137 bam_pileup1_t **plp;
138 };
139
140 static int mplp_get_ref(mplp_aux_t *ma, int tid, char **ref, int *ref_len) {
141 mplp_ref_t *r = ma->ref;
142
143 //printf("get ref %d {%d/%p, %d/%p}\n", tid, r->ref_id[0], r->ref[0], r->ref_id[1], r->ref[1]);
144
145 if (!r || !ma->conf->fai) {
146 *ref = NULL;
147 return 0;
148 }
149
150 // Do we need to reference count this so multiple mplp_aux_t can
151 // track which references are in use?
152 // For now we just cache the last two. Sufficient?
153 if (tid == r->ref_id[0]) {
154 *ref = r->ref[0];
155 *ref_len = r->ref_len[0];
156 return 1;
157 }
158 if (tid == r->ref_id[1]) {
159 // Last, swap over
160 int tmp;
161 tmp = r->ref_id[0]; r->ref_id[0] = r->ref_id[1]; r->ref_id[1] = tmp;
162 tmp = r->ref_len[0]; r->ref_len[0] = r->ref_len[1]; r->ref_len[1] = tmp;
163
164 char *tc;
165 tc = r->ref[0]; r->ref[0] = r->ref[1]; r->ref[1] = tc;
166 *ref = r->ref[0];
167 *ref_len = r->ref_len[0];
168 return 1;
169 }
170
171 // New, so migrate to old and load new
172 free(r->ref[1]);
173 r->ref[1] = r->ref[0];
174 r->ref_id[1] = r->ref_id[0];
175 r->ref_len[1] = r->ref_len[0];
176
177 r->ref_id[0] = tid;
178 r->ref[0] = faidx_fetch_seq(ma->conf->fai,
179 ma->h->target_name[r->ref_id[0]],
180 0,
181 INT_MAX,
182 &r->ref_len[0]);
183
184 if (!r->ref[0]) {
185 r->ref[0] = NULL;
186 r->ref_id[0] = -1;
187 r->ref_len[0] = 0;
188 *ref = NULL;
189 return 0;
190 }
191
192 *ref = r->ref[0];
193 *ref_len = r->ref_len[0];
194 return 1;
195 }
196
197 static int mplp_func(void *data, bam1_t *b)
198 {
199 char *ref;
200 mplp_aux_t *ma = (mplp_aux_t*)data;
201 int ret, ref_len;
202 while (1)
203 {
204 int has_ref;
205 ret = ma->iter? sam_itr_next(ma->fp, ma->iter, b) : sam_read1(ma->fp, ma->h, b);
206 if (ret < 0) break;
207 // The 'B' cigar operation is not part of the specification, considering as obsolete.
208 // bam_remove_B(b);
209 if (b->core.tid < 0 || (b->core.flag&BAM_FUNMAP)) continue; // exclude unmapped reads
210 if (ma->conf->rflag_skip_any_unset && (ma->conf->rflag_skip_any_unset&b->core.flag)!=ma->conf->rflag_skip_any_unset) continue;
211 if (ma->conf->rflag_skip_all_set && (ma->conf->rflag_skip_all_set&b->core.flag)==ma->conf->rflag_skip_all_set) continue;
212 if (ma->conf->rflag_skip_all_unset && !(ma->conf->rflag_skip_all_unset&b->core.flag)) continue;
213 if (ma->conf->rflag_skip_any_set && ma->conf->rflag_skip_any_set&b->core.flag) continue;
214 if (ma->conf->bed)
215 {
216 // test overlap
217 regitr_t *itr = ma->conf->bed_itr;
218 int beg = b->core.pos, end = bam_endpos(b)-1;
219 int overlap = regidx_overlap(ma->conf->bed, ma->h->target_name[b->core.tid],beg,end, itr);
220 if ( !ma->conf->bed_logic && !overlap )
221 {
222 // exclude only reads which are fully contained in the region
223 while ( regitr_overlap(itr) )
224 {
225 if ( beg < itr->beg ) { overlap = 1; break; }
226 if ( end > itr->end ) { overlap = 1; break; }
227 }
228 }
229 if ( !overlap ) continue;
230 }
231 if ( bam_smpl_get_sample_id(ma->conf->bsmpl,ma->bam_id,b)<0 ) continue;
232 if (ma->conf->flag & MPLP_ILLUMINA13) {
233 int i;
234 uint8_t *qual = bam_get_qual(b);
235 for (i = 0; i < b->core.l_qseq; ++i)
236 qual[i] = qual[i] > 31? qual[i] - 31 : 0;
237 }
238
239 if (ma->conf->fai && b->core.tid >= 0) {
240 has_ref = mplp_get_ref(ma, b->core.tid, &ref, &ref_len);
241 if (has_ref && ref_len <= b->core.pos) { // exclude reads outside of the reference sequence
242 fprintf(bcftools_stderr,"[%s] Skipping because %"PRId64" is outside of %d [ref:%d]\n",
243 __func__, (int64_t) b->core.pos, ref_len, b->core.tid);
244 continue;
245 }
246 } else {
247 has_ref = 0;
248 }
249
250 // Allow sufficient room for bam_aux_append of ZQ tag without
251 // a realloc and consequent breakage of pileup's cached pointers.
252 if (has_ref && (ma->conf->flag &MPLP_REALN) && !bam_aux_get(b, "ZQ")) {
253 // Doing sam_prob_realn later is problematic as it adds to
254 // the tag list (ZQ or BQ), which causes a realloc of b->data.
255 // This happens after pileup has built a hash table on the
256 // read name. It's a deficiency in pileup IMO.
257
258 // We could implement a new sam_prob_realn that returns ZQ
259 // somewhere else and cache it ourselves (pileup clientdata),
260 // but for now we simply use a workaround.
261 //
262 // We create a fake tag of the correct length, which we remove
263 // just prior calling sam_prob_realn so we can guarantee there is
264 // room. (We can't just make room now as bam_copy1 removes it
265 // again).
266 if (b->core.l_qseq > 500) {
267 uint8_t *ZQ = malloc((uint32_t)b->core.l_qseq+1);
268 memset(ZQ, '@', b->core.l_qseq);
269 ZQ[b->core.l_qseq] = 0;
270 bam_aux_append(b, "_Q", 'Z', b->core.l_qseq+1, ZQ);
271 free(ZQ);
272 } else {
273 static uint8_t ZQ[501] =
274 "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
275 "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
276 "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
277 "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
278 "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
279 "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
280 "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
281 "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
282 "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
283 "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";
284 ZQ[b->core.l_qseq] = 0;
285 bam_aux_append(b, "_Q", 'Z', b->core.l_qseq+1, ZQ);
286 ZQ[b->core.l_qseq] = '@';
287 }
288 }
289
290 if (has_ref && ma->conf->capQ_thres > 10) {
291 int q = sam_cap_mapq(b, ref, ref_len, ma->conf->capQ_thres);
292 if (q < 0) continue; // skip
293 else if (b->core.qual > q) b->core.qual = q;
294 }
295 if (b->core.qual < ma->conf->min_mq) continue;
296 else if ((ma->conf->flag&MPLP_NO_ORPHAN) && (b->core.flag&BAM_FPAIRED) && !(b->core.flag&BAM_FPROPER_PAIR)) continue;
297
298 return ret;
299 };
300 return ret;
301 }
302
303 // Called once per new bam added to the pileup.
304 // We cache sample information here so we don't have to keep recomputing this
305 // on each and every pileup column. If FMT/SCR annotation is requested, a flag
306 // is set to indicate the presence of a soft clip.
307 static int pileup_constructor(void *data, const bam1_t *b, bam_pileup_cd *cd)
308 {
309 cd->p = calloc(1,sizeof(plp_cd_t));
310
311 PLP_NM(cd) = PLP_NM_UNSET;
312
313 mplp_aux_t *ma = (mplp_aux_t *)data;
314 int n = bam_smpl_get_sample_id(ma->conf->bsmpl, ma->bam_id, (bam1_t *)b);
315 PLP_SET_SAMPLE_ID(cd, n);
316
317 // Whether read has a soft-clip is used in mplp_realn's heuristics.
318 // TODO: consider whether clip length is beneficial to use?
319 int i;
320 for (i=0; i<b->core.n_cigar; i++) {
321 int cig = bam_get_cigar(b)[i] & BAM_CIGAR_MASK;
322 if (cig == BAM_CSOFT_CLIP) {
323 PLP_SET_SOFT_CLIP(cd);
324 break;
325 }
326 }
327
328 if (ma->conf->flag & MPLP_REALN) {
329 int i;
330 // int tot_ins = 0;
331 // int p = 0;
332 uint32_t *cigar = bam_get_cigar(b);
333 for (i=0; i<b->core.n_cigar; i++) {
334 int cig = cigar[i] & BAM_CIGAR_MASK;
335 // if (bam_cigar_type(cig) & 2)
336 // p += cigar[i] >> BAM_CIGAR_SHIFT;
337 if (cig == BAM_CINS || cig == BAM_CDEL || cig == BAM_CREF_SKIP) {
338 // tot_ins += cigar[i] >> BAM_CIGAR_SHIFT;
339 // Possible further optimsation, check tot_ins==1 later
340 // (and remove break) so we can detect single bp indels.
341 // We may want to focus BAQ on more complex regions only.
342 PLP_SET_INDEL(cd);
343 break;
344 }
345
346 // TODO: proper p->cd struct and have cd->i as a size rather
347 // than a flag.
348
349 // Then aggregate together the sizes and if just 1 size for all
350 // reads or 2 sizes for approx 50/50 split in all reads, then
351 // treat this as a well-aligned variant and don't run BAQ.
352 }
353 }
354
355 return 0;
356 }
357 static int pileup_destructor(void *data, const bam1_t *b, bam_pileup_cd *cd)
358 {
359 free(cd->p);
360 return 0;
361 }
362
363 static void group_smpl(mplp_pileup_t *m, bam_smpl_t *bsmpl, int n, int *n_plp, const bam_pileup1_t **plp)
364 {
365 int i, j;
366 memset(m->n_plp, 0, m->n * sizeof(int));
367 for (i = 0; i < n; ++i) // iterate over all bams
368 {
369 for (j = 0; j < n_plp[i]; ++j) // iterate over all reads available at this position
370 {
371 const bam_pileup1_t *p = plp[i] + j;
372 int id = PLP_SAMPLE_ID(&(p->cd));
373 if (m->n_plp[id] == m->m_plp[id])
374 {
375 m->m_plp[id] = m->m_plp[id]? m->m_plp[id]<<1 : 8;
376 m->plp[id] = (bam_pileup1_t*) realloc(m->plp[id], sizeof(bam_pileup1_t) * m->m_plp[id]);
377 }
378 m->plp[id][m->n_plp[id]++] = *p;
379 }
380 }
381 }
382
383 static void flush_bcf_records(mplp_conf_t *conf, htsFile *fp, bcf_hdr_t *hdr, bcf1_t *rec)
384 {
385 if ( !conf->gvcf )
386 {
387 if ( rec && bcf_write1(fp, hdr, rec)!=0 ) error("[%s] Error: failed to write the record to %s\n", __func__,conf->output_fname?conf->output_fname:"standard output");
388 return;
389 }
390
391 if ( !rec )
392 {
393 gvcf_write(conf->gvcf, fp, hdr, NULL, 0);
394 return;
395 }
396
397 int is_ref = 0;
398 if ( rec->n_allele==1 ) is_ref = 1;
399 else if ( rec->n_allele==2 )
400 {
401 // second allele is mpileup's X, not a variant
402 if ( rec->d.allele[1][0]=='<' && rec->d.allele[1][1]=='*' && rec->d.allele[1][2]=='>' ) is_ref = 1;
403 }
404 rec = gvcf_write(conf->gvcf, fp, hdr, rec, is_ref);
405 if ( rec && bcf_write1(fp,hdr,rec)!=0 ) error("[%s] Error: failed to write the record to %s\n", __func__,conf->output_fname?conf->output_fname:"standard output");
406 }
407
408 /*
409 * Loops for an indel at this position.
410 *
411 * Only reads that overlap an indel loci get realigned. This considerably
412 * reduces the cost of running BAQ while keeping the main benefits.
413 *
414 * TODO: also consider only realigning reads that don't span the indel
415 * by more than a certain amount either-side. Ie focus BAQ only on reads
416 * ending adjacent to the indel, where the alignment is most likely to
417 * be wrong. (2nd TODO: do this based on sequence context; STRs bad, unique
418 * data good.)
419 *
420 * NB: this may sadly realign after we've already used the data. Hmm...
421 */
422 static void mplp_realn(int n, int *n_plp, const bam_pileup1_t **plp,
423 int flag, int max_read_len,
424 char *ref, int ref_len, int pos) {
425 int i, j, has_indel = 0, has_clip = 0, nt = 0;
426 int min_indel = INT_MAX, max_indel = INT_MIN;
427
428 // Is an indel present.
429 // NB: don't bother even checking if very long as almost guaranteed
430 // to have indel (and likely soft-clips too).
431 for (i = 0; i < n; i++) { // iterate over bams
432 nt += n_plp[i];
433 for (j = 0; j < n_plp[i]; j++) { // iterate over reads
434 bam_pileup1_t *p = (bam_pileup1_t *)plp[i] + j;
435 has_indel += (PLP_HAS_INDEL(&p->cd) || p->indel) ? 1 : 0;
436 // Has_clip is almost always true for very long reads
437 // (eg PacBio CCS), but these rarely matter as the clip
438 // is likely a long way from this indel.
439 has_clip += (PLP_HAS_SOFT_CLIP(&p->cd)) ? 1 : 0;
440 if (max_indel < p->indel)
441 max_indel = p->indel;
442 if (min_indel > p->indel)
443 min_indel = p->indel;
444 }
445 }
446
447 if (flag & MPLP_REALN_PARTIAL) {
448 if (has_indel == 0 ||
449 (has_clip < 0.2*nt && max_indel == min_indel &&
450 (has_indel < 0.1*nt /*|| has_indel > 0.9*nt*/ || has_indel == 1)))
451 return;
452 }
453
454 // Realign
455 for (i = 0; i < n; i++) { // iterate over bams
456 for (j = 0; j < n_plp[i]; j++) { // iterate over reads
457 const bam_pileup1_t *p = plp[i] + j;
458 bam1_t *b = p->b;
459
460 // Avoid doing multiple times.
461 //
462 // Note we cannot modify p->cd.i here with a PLP_SET macro
463 // because the cd item is held by mpileup in an lbnode_t
464 // struct and copied over to the pileup struct for each
465 // iteration, essentially making p->cd.i read only.
466 //
467 // We could use our own structure (p->cd.p), allocated during
468 // the constructor, but for simplicity we play dirty and
469 // abuse an unused flag bit instead.
470 if ( PLP_IS_REALN(&(p->cd)) ) continue;
471 PLP_SET_REALN(&(p->cd));
472
473 if (b->core.l_qseq > max_read_len)
474 continue;
475
476 // Check p->cigar_ind and see what cigar elements are before
477 // and after. How close is this location to the end of the
478 // read? Only realign if we don't span by more than X bases.
479 //
480 // Again, best only done on deeper data as BAQ helps
481 // disproportionately more on shallow data sets.
482 //
483 // This rescues some of the false negatives that are caused by
484 // systematic reduction in quality due to sample vs ref alignment.
485
486 // At deep coverage we skip realigning more reads as we have sufficient depth.
487 // This rescues for false negatives. At shallow depth we pay for this with
488 // more FP so are more stringent on spanning size.
489 #define REALN_DIST (40+10*(nt<40)+10*(nt<20))
490 uint32_t *cig = bam_get_cigar(b);
491 int ncig = b->core.n_cigar;
492
493 // Don't realign reads where indel is in middle?
494 // On long read data we don't care about soft-clips at the ends.
495 // For short read data, we always calc BAQ on these as they're
496 // a common source of false positives.
497 if ((flag & MPLP_REALN_PARTIAL) && nt > 15 && ncig > 1) {
498 // Left & right cigar op match.
499 int lr = b->core.l_qseq > 500;
500 int lm = 0, rm = 0, k, nm = 0;
501 for (k = 0; k < ncig; k++) {
502 int cop = bam_cigar_op(cig[k]);
503 if (lr && (cop == BAM_CHARD_CLIP || cop == BAM_CSOFT_CLIP))
504 continue;
505
506 if (cop == BAM_CMATCH || cop == BAM_CDIFF ||
507 cop == BAM_CEQUAL) {
508 lm += bam_cigar_oplen(cig[k]);
509 nm++;
510 } else {
511 break;
512 }
513 }
514
515 // if everything is a match (or sequence (mis)match) then move on
516 // because we don't have an indel in the middle
517 if (nm != ncig) {
518 for (k = ncig-1; k >= 0; k--) {
519 int cop = bam_cigar_op(cig[k]);
520 if (lr && (cop == BAM_CHARD_CLIP || cop == BAM_CSOFT_CLIP))
521 continue;
522
523 if (cop == BAM_CMATCH || cop == BAM_CDIFF ||
524 cop == BAM_CEQUAL)
525 rm += bam_cigar_oplen(cig[k]);
526 else
527 break;
528 }
529
530 if (lm >= REALN_DIST*4 && rm >= REALN_DIST*4)
531 continue;
532
533 if (lm >= REALN_DIST && rm >= REALN_DIST &&
534 has_clip < (0.15+0.05*(nt>20))*nt)
535 continue;
536 }
537 }
538
539 if (b->core.l_qseq > 500) {
540 // don't do BAQ on long-read data if it's going to
541 // cause us to have a large band-with and costly in CPU
542 int rl = bam_cigar2rlen(b->core.n_cigar, bam_get_cigar(b));
543 if (abs(rl - b->core.l_qseq) * b->core.l_qseq >= 500000)
544 continue;
545 }
546
547 // Fudge: make room for ZQ tag.
548 uint8_t *_Q = bam_aux_get(b, "_Q");
549 if (_Q) bam_aux_del(b, _Q);
550 sam_prob_realn(b, ref, ref_len, (flag & MPLP_REDO_BAQ) ? 7 : 3);
551 }
552 }
553
554 return;
555 }
556
557 static int mpileup_reg(mplp_conf_t *conf, uint32_t beg, uint32_t end)
558 {
559 bam_hdr_t *hdr = conf->mplp_data[0]->h; // header of first file in input list
560
561 int ret, i, tid, pos, ref_len;
562 char *ref;
563
564 while ( (ret=bam_mplp_auto(conf->iter, &tid, &pos, conf->n_plp, conf->plp)) > 0)
565 {
566 if ( pos<beg || pos>end ) continue;
567 if ( conf->bed && tid >= 0 )
568 {
569 int overlap = regidx_overlap(conf->bed, hdr->target_name[tid], pos, pos, NULL);
570 if ( !conf->bed_logic ) overlap = overlap ? 0 : 1;
571 if ( !overlap ) continue;
572 }
573 int has_ref = mplp_get_ref(conf->mplp_data[0], tid, &ref, &ref_len);
574 if (has_ref && (conf->flag & MPLP_REALN))
575 mplp_realn(conf->nfiles, conf->n_plp, conf->plp, conf->flag, conf->max_read_len, ref, ref_len, pos);
576
577 int total_depth, _ref0, ref16;
578 for (i = total_depth = 0; i < conf->nfiles; ++i) total_depth += conf->n_plp[i];
579 group_smpl(conf->gplp, conf->bsmpl, conf->nfiles, conf->n_plp, conf->plp);
580 _ref0 = (ref && pos < ref_len)? ref[pos] : 'N';
581 ref16 = seq_nt16_table[_ref0];
582 bcf_callaux_clean(conf->bca, &conf->bc);
583 for (i = 0; i < conf->gplp->n; ++i)
584 bcf_call_glfgen(conf->gplp->n_plp[i], conf->gplp->plp[i], ref16, conf->bca, conf->bcr + i);
585 conf->bc.tid = tid; conf->bc.pos = pos;
586 bcf_call_combine(conf->gplp->n, conf->bcr, conf->bca, ref16, &conf->bc);
587 bcf_clear1(conf->bcf_rec);
588 bcf_call2bcf(&conf->bc, conf->bcf_rec, conf->bcr, conf->fmt_flag, conf->bca, 0);
589 flush_bcf_records(conf, conf->bcf_fp, conf->bcf_hdr, conf->bcf_rec);
590
591 // call indels; todo: subsampling with total_depth>max_indel_depth instead of ignoring?
592 // check me: rghash in bcf_call_gap_prep() should have no effect, reads mplp_func already excludes them
593 if (!(conf->flag&MPLP_NO_INDEL) && total_depth < conf->max_indel_depth)
594 {
595 bcf_callaux_clean(conf->bca, &conf->bc);
596 conf->bca->chr = tid>=0 ? hdr->target_name[tid] : NULL;
597 int iret;
598 if (conf->edlib)
599 iret = bcf_edlib_gap_prep(conf->gplp->n, conf->gplp->n_plp, conf->gplp->plp, pos, conf->bca, ref, ref_len);
600 else if ( conf->indels_v20 )
601 iret = bcf_iaux_gap_prep(conf->gplp->n, conf->gplp->n_plp, conf->gplp->plp, pos, conf->bca, ref);
602 else
603 iret = bcf_call_gap_prep(conf->gplp->n, conf->gplp->n_plp, conf->gplp->plp, pos, conf->bca, ref);
604 if ( iret>=0 )
605 {
606 for (i = 0; i < conf->gplp->n; ++i)
607 bcf_call_glfgen(conf->gplp->n_plp[i], conf->gplp->plp[i], -1, conf->bca, conf->bcr + i);
608 if (bcf_call_combine(conf->gplp->n, conf->bcr, conf->bca, -1, &conf->bc) >= 0)
609 {
610 bcf_clear1(conf->bcf_rec);
611 bcf_call2bcf(&conf->bc, conf->bcf_rec, conf->bcr, conf->fmt_flag, conf->bca, ref);
612 flush_bcf_records(conf, conf->bcf_fp, conf->bcf_hdr, conf->bcf_rec);
613 }
614 }
615 }
616 }
617 return ret;
618 }
619
620 static int mpileup(mplp_conf_t *conf)
621 {
622 if (conf->nfiles == 0) {
623 fprintf(bcftools_stderr,"[%s] no input file/data given\n", __func__);
624 bcftools_exit(EXIT_FAILURE);
625 }
626
627 mplp_ref_t mp_ref = MPLP_REF_INIT;
628 conf->gplp = (mplp_pileup_t *) calloc(1,sizeof(mplp_pileup_t));
629 conf->mplp_data = (mplp_aux_t**) calloc(conf->nfiles, sizeof(mplp_aux_t*));
630 conf->plp = (const bam_pileup1_t**) calloc(conf->nfiles, sizeof(bam_pileup1_t*));
631 conf->n_plp = (int*) calloc(conf->nfiles, sizeof(int));
632
633 // Allow to run mpileup on multiple regions in one go. This comes at cost: the bai index
634 // must be kept in the memory for the whole time which can be a problem with many bams.
635 // Therefore if none or only one region is requested, we initialize the bam iterator as
636 // before and free the index. Only when multiple regions are queried, we keep the index.
637 int nregs = 0;
638 if ( conf->reg_fname )
639 {
640 if ( conf->reg_is_file )
641 {
642 conf->reg = regidx_init(conf->reg_fname,NULL,NULL,0,NULL);
643 if ( !conf->reg ) {
644 fprintf(bcftools_stderr,"Could not parse the regions: %s\n", conf->reg_fname);
645 bcftools_exit(EXIT_FAILURE);
646 }
647 }
648 else
649 {
650 conf->reg = regidx_init(NULL,regidx_parse_reg,NULL,sizeof(char*),NULL);
651 if ( regidx_insert_list(conf->reg,conf->reg_fname,',') !=0 ) {
652 fprintf(bcftools_stderr,"Could not parse the regions: %s\n", conf->reg_fname);
653 bcftools_exit(EXIT_FAILURE);
654 }
655 }
656 nregs = regidx_nregs(conf->reg);
657 if ( nregs )
658 {
659 // the regions list can be empty, see #2250
660 conf->reg_itr = regitr_init(conf->reg);
661 regitr_loop(conf->reg_itr); // region iterator now positioned at the first region
662 }
663 }
664
665 // read the header of each file in the list and initialize data
666 // beware: mpileup has always assumed that tid's are consistent in the headers, add sanity check at least!
667 bam_hdr_t *hdr = NULL; // header of first file in input list
668 int i;
669 for (i = 0; i < conf->nfiles; ++i) {
670 bam_hdr_t *h_tmp;
671 conf->mplp_data[i] = (mplp_aux_t*) calloc(1, sizeof(mplp_aux_t));
672 conf->mplp_data[i]->fp = sam_open(conf->files[i], "rb");
673 if ( !conf->mplp_data[i]->fp )
674 {
675 fprintf(bcftools_stderr, "[%s] failed to open %s: %s\n", __func__, conf->files[i], strerror(errno));
676 bcftools_exit(EXIT_FAILURE);
677 }
678 if (hts_set_opt(conf->mplp_data[i]->fp, CRAM_OPT_DECODE_MD, 1)) {
679 fprintf(bcftools_stderr, "Failed to set CRAM_OPT_DECODE_MD value\n");
680 bcftools_exit(EXIT_FAILURE);
681 }
682 if (conf->fai_fname && hts_set_fai_filename(conf->mplp_data[i]->fp, conf->fai_fname) != 0) {
683 fprintf(bcftools_stderr, "[%s] failed to process %s: %s\n",
684 __func__, conf->fai_fname, strerror(errno));
685 bcftools_exit(EXIT_FAILURE);
686 }
687 conf->mplp_data[i]->conf = conf;
688 conf->mplp_data[i]->ref = &mp_ref;
689 h_tmp = sam_hdr_read(conf->mplp_data[i]->fp);
690 if ( !h_tmp ) {
691 fprintf(bcftools_stderr,"[%s] fail to read the header of %s\n", __func__, conf->files[i]);
692 bcftools_exit(EXIT_FAILURE);
693 }
694 conf->mplp_data[i]->h = i ? hdr : h_tmp; // for j==0, "h" has not been set yet
695 conf->mplp_data[i]->bam_id = bam_smpl_add_bam(conf->bsmpl,h_tmp->text,conf->files[i]);
696 if ( conf->mplp_data[i]->bam_id<0 )
697 {
698 // no usable readgroups in this bam, it can be skipped
699 sam_close(conf->mplp_data[i]->fp);
700 free(conf->mplp_data[i]);
701 bam_hdr_destroy(h_tmp);
702 free(conf->files[i]);
703 if ( i+1<conf->nfiles ) memmove(&conf->files[i],&conf->files[i+1],sizeof(*conf->files)*(conf->nfiles-i-1));
704 conf->nfiles--;
705 i--;
706 continue;
707 }
708 if (conf->reg && nregs) {
709 hts_idx_t *idx = sam_index_load(conf->mplp_data[i]->fp, conf->files[i]);
710 if (idx == NULL) {
711 fprintf(bcftools_stderr, "[%s] fail to load index for %s\n", __func__, conf->files[i]);
712 bcftools_exit(EXIT_FAILURE);
713 }
714 conf->buf.l = 0;
715 ksprintf(&conf->buf,"%s:%u-%u",conf->reg_itr->seq,conf->reg_itr->beg+1,conf->reg_itr->end+1);
716 conf->mplp_data[i]->iter = sam_itr_querys(idx, conf->mplp_data[i]->h, conf->buf.s);
717 if ( !conf->mplp_data[i]->iter )
718 {
719 conf->mplp_data[i]->iter = sam_itr_querys(idx, conf->mplp_data[i]->h, conf->reg_itr->seq);
720 if ( conf->mplp_data[i]->iter ) {
721 fprintf(bcftools_stderr,"[E::%s] fail to parse region '%s'\n", __func__, conf->buf.s);
722 bcftools_exit(EXIT_FAILURE);
723 }
724 fprintf(bcftools_stderr,"[E::%s] the sequence \"%s\" not found: %s\n",__func__,conf->reg_itr->seq,conf->files[i]);
725 bcftools_exit(EXIT_FAILURE);
726 }
727 if ( nregs==1 ) // no need to keep the index in memory
728 hts_idx_destroy(idx);
729 else
730 conf->mplp_data[i]->idx = idx;
731 }
732
733 if ( !hdr ) hdr = h_tmp; /* save the header of first file in list */
734 else {
735 // FIXME: check consistency between h and h_tmp
736 bam_hdr_destroy(h_tmp);
737
738 // we store only the first file's header; it's (alleged to be)
739 // compatible with the i-th file's target_name lookup needs
740 conf->mplp_data[i]->h = hdr;
741 }
742 }
743 if ( !hdr ) {
744 fprintf(bcftools_stderr, "[%s] failed to find a file header with usable read groups\n", __func__);
745 bcftools_exit(EXIT_FAILURE);
746 }
747 // allocate data storage proportionate to number of samples being studied sm->n
748 bam_smpl_get_samples(conf->bsmpl, &conf->gplp->n);
749 conf->gplp->n_plp = (int*) calloc(conf->gplp->n, sizeof(int));
750 conf->gplp->m_plp = (int*) calloc(conf->gplp->n, sizeof(int));
751 conf->gplp->plp = (bam_pileup1_t**) calloc(conf->gplp->n, sizeof(bam_pileup1_t*));
752
753 fprintf(bcftools_stderr, "[%s] %d samples in %d input files\n", __func__, conf->gplp->n, conf->nfiles);
754 // write the VCF header
755 char wmode[8];
756 set_wmode(wmode,conf->output_type,conf->output_fname,conf->clevel);
757 conf->bcf_fp = hts_open(conf->output_fname ? conf->output_fname : "-", wmode);
758 if (conf->bcf_fp == NULL) {
759 fprintf(bcftools_stderr, "[%s] failed to write to %s: %s\n", __func__, conf->output_fname? conf->output_fname : "standard output", strerror(errno));
760 bcftools_exit(EXIT_FAILURE);
761 }
762 if ( conf->n_threads ) hts_set_threads(conf->bcf_fp, conf->n_threads);
763
764 // BCF header creation
765 conf->bcf_hdr = bcf_hdr_init("w");
766 conf->buf.l = 0;
767
768 if (conf->record_cmd_line)
769 {
770 ksprintf(&conf->buf, "##bcftoolsVersion=%s+htslib-%s\n",bcftools_version(),hts_version());
771 bcf_hdr_append(conf->bcf_hdr, conf->buf.s);
772
773 conf->buf.l = 0;
774 ksprintf(&conf->buf, "##bcftoolsCommand=mpileup");
775 for (i=1; i<conf->argc; i++) ksprintf(&conf->buf, " %s", conf->argv[i]);
776 kputc('\n', &conf->buf);
777 bcf_hdr_append(conf->bcf_hdr, conf->buf.s);
778 }
779
780 if (conf->fai_fname)
781 {
782 conf->buf.l = 0;
783 ksprintf(&conf->buf, "##reference=file://%s\n", conf->fai_fname);
784 bcf_hdr_append(conf->bcf_hdr, conf->buf.s);
785 }
786
787 // Translate BAM @SQ tags to BCF ##contig tags
788 // todo: use/write new BAM header manipulation routines, fill also UR, M5
789 for (i=0; i<hdr->n_targets; i++)
790 {
791 conf->buf.l = 0;
792 ksprintf(&conf->buf, "##contig=<ID=%s,length=%d>", hdr->target_name[i], hdr->target_len[i]);
793 bcf_hdr_append(conf->bcf_hdr, conf->buf.s);
794 }
795 conf->buf.l = 0;
796
797 bcf_hdr_append(conf->bcf_hdr,"##ALT=<ID=*,Description=\"Represents allele(s) other than observed.\">");
798 bcf_hdr_append(conf->bcf_hdr,"##INFO=<ID=INDEL,Number=0,Type=Flag,Description=\"Indicates that the variant is an INDEL.\">");
799 if ( conf->fmt_flag&B2B_INFO_IDV )
800 bcf_hdr_append(conf->bcf_hdr,"##INFO=<ID=IDV,Number=1,Type=Integer,Description=\"Maximum number of raw reads supporting an indel\">");
801 if ( conf->fmt_flag&B2B_INFO_IMF )
802 bcf_hdr_append(conf->bcf_hdr,"##INFO=<ID=IMF,Number=1,Type=Float,Description=\"Maximum fraction of raw reads supporting an indel\">");
803 bcf_hdr_append(conf->bcf_hdr,"##INFO=<ID=DP,Number=1,Type=Integer,Description=\"Raw read depth\">");
804 if ( conf->fmt_flag&B2B_INFO_VDB )
805 bcf_hdr_append(conf->bcf_hdr,"##INFO=<ID=VDB,Number=1,Type=Float,Description=\"Variant Distance Bias for filtering splice-site artefacts in RNA-seq data (bigger is better)\",Version=\"3\">");
806
807 if ( conf->fmt_flag&B2B_INFO_RPBZ )
808 bcf_hdr_append(conf->bcf_hdr,"##INFO=<ID=RPBZ,Number=1,Type=Float,Description=\"Mann-Whitney U-z test of Read Position Bias (closer to 0 is better)\">");
809 if ( conf->fmt_flag&B2B_INFO_MQBZ )
810 bcf_hdr_append(conf->bcf_hdr,"##INFO=<ID=MQBZ,Number=1,Type=Float,Description=\"Mann-Whitney U-z test of Mapping Quality Bias (closer to 0 is better)\">");
811 if ( conf->fmt_flag&B2B_INFO_BQBZ )
812 bcf_hdr_append(conf->bcf_hdr,"##INFO=<ID=BQBZ,Number=1,Type=Float,Description=\"Mann-Whitney U-z test of Base Quality Bias (closer to 0 is better)\">");
813 if ( conf->fmt_flag&B2B_INFO_MQSBZ )
814 bcf_hdr_append(conf->bcf_hdr,"##INFO=<ID=MQSBZ,Number=1,Type=Float,Description=\"Mann-Whitney U-z test of Mapping Quality vs Strand Bias (closer to 0 is better)\">");
815 if ( conf->fmt_flag&B2B_INFO_MIN_PL_SUM )
816 bcf_hdr_append(conf->bcf_hdr,"##INFO=<ID=MIN_PL_SUM,Number=1,Type=Integer,Description=\"Sum of min PLs across all samples before normalization (experimental)\">");
817 if ( conf->fmt_flag&B2B_INFO_NM )
818 bcf_hdr_append(conf->bcf_hdr,"##INFO=<ID=NM,Number=2,Type=Float,Description=\"Average number of mismatches in ref and alt reads (approximate, experimental, make me localized?)\">");
819 if ( conf->fmt_flag&B2B_INFO_NMBZ )
820 bcf_hdr_append(conf->bcf_hdr,"##INFO=<ID=NMBZ,Number=1,Type=Float,Description=\"Mann-Whitney U-z test of Number of Mismatches within supporting reads (closer to 0 is better; approximate, experimental, make me localized?)\">");
821 if ( conf->fmt_flag&B2B_FMT_NMBZ )
822 bcf_hdr_append(conf->bcf_hdr,"##FORMAT=<ID=NMBZ,Number=1,Type=Float,Description=\"Mann-Whitney U-z test of Number of Mismatches within supporting reads (closer to 0 is better)\">");
823 if ( conf->fmt_flag&B2B_INFO_SCBZ )
824 bcf_hdr_append(conf->bcf_hdr,"##INFO=<ID=SCBZ,Number=1,Type=Float,Description=\"Mann-Whitney U-z test of Soft-Clip Length Bias (closer to 0 is better)\">");
825 if ( conf->fmt_flag&B2B_INFO_FS )
826 bcf_hdr_append(conf->bcf_hdr,"##INFO=<ID=FS,Number=1,Type=Float,Description=\"Fisher's exact test P-value to detect strand bias\">");
827 if ( conf->fmt_flag&B2B_INFO_SGB )
828 bcf_hdr_append(conf->bcf_hdr,"##INFO=<ID=SGB,Number=1,Type=Float,Description=\"Segregation based metric, http://samtools.github.io/bcftools/rd-SegBias.pdf\">");
829 if ( conf->fmt_flag&B2B_INFO_MQ0F )
830 bcf_hdr_append(conf->bcf_hdr,"##INFO=<ID=MQ0F,Number=1,Type=Float,Description=\"Fraction of MQ0 reads (smaller is better)\">");
831 bcf_hdr_append(conf->bcf_hdr,"##INFO=<ID=I16,Number=16,Type=Float,Description=\"Auxiliary tag used for calling, see description of bcf_callret1_t in bam2bcf.h\">");
832 bcf_hdr_append(conf->bcf_hdr,"##INFO=<ID=QS,Number=R,Type=Float,Description=\"Auxiliary tag used for calling\">");
833 bcf_hdr_append(conf->bcf_hdr,"##FORMAT=<ID=PL,Number=G,Type=Integer,Description=\"List of Phred-scaled genotype likelihoods\">");
834 if ( conf->fmt_flag&B2B_FMT_DP )
835 bcf_hdr_append(conf->bcf_hdr,"##FORMAT=<ID=DP,Number=1,Type=Integer,Description=\"Number of high-quality bases\">");
836 if ( conf->fmt_flag&B2B_FMT_DV )
837 bcf_hdr_append(conf->bcf_hdr,"##FORMAT=<ID=DV,Number=1,Type=Integer,Description=\"Number of high-quality non-reference bases\">");
838 if ( conf->fmt_flag&B2B_FMT_DPR )
839 bcf_hdr_append(conf->bcf_hdr,"##FORMAT=<ID=DPR,Number=R,Type=Integer,Description=\"Number of high-quality bases observed for each allele\">");
840 if ( conf->fmt_flag&B2B_INFO_DPR )
841 bcf_hdr_append(conf->bcf_hdr,"##INFO=<ID=DPR,Number=R,Type=Integer,Description=\"Number of high-quality bases observed for each allele\">");
842 if ( conf->fmt_flag&B2B_FMT_DP4 )
843 bcf_hdr_append(conf->bcf_hdr,"##FORMAT=<ID=DP4,Number=4,Type=Integer,Description=\"Number of high-quality ref-fwd, ref-reverse, alt-fwd and alt-reverse bases\">");
844 if ( conf->fmt_flag&B2B_FMT_SP )
845 bcf_hdr_append(conf->bcf_hdr,"##FORMAT=<ID=SP,Number=1,Type=Integer,Description=\"Phred-scaled strand bias P-value\">");
846 if ( conf->fmt_flag&B2B_FMT_AD )
847 bcf_hdr_append(conf->bcf_hdr,"##FORMAT=<ID=AD,Number=R,Type=Integer,Description=\"Allelic depths (high-quality bases)\">");
848 if ( conf->fmt_flag&B2B_FMT_ADF )
849 bcf_hdr_append(conf->bcf_hdr,"##FORMAT=<ID=ADF,Number=R,Type=Integer,Description=\"Allelic depths on the forward strand (high-quality bases)\">");
850 if ( conf->fmt_flag&B2B_FMT_ADR )
851 bcf_hdr_append(conf->bcf_hdr,"##FORMAT=<ID=ADR,Number=R,Type=Integer,Description=\"Allelic depths on the reverse strand (high-quality bases)\">");
852 if ( conf->fmt_flag&B2B_FMT_QS )
853 bcf_hdr_append(conf->bcf_hdr,"##FORMAT=<ID=QS,Number=R,Type=Integer,Description=\"Phred-score allele quality sum used by `call -mG` and `+trio-dnm`\">");
854 if ( conf->fmt_flag&B2B_INFO_AD )
855 bcf_hdr_append(conf->bcf_hdr,"##INFO=<ID=AD,Number=R,Type=Integer,Description=\"Total allelic depths (high-quality bases)\">");
856 if ( conf->fmt_flag&B2B_INFO_ADF )
857 bcf_hdr_append(conf->bcf_hdr,"##INFO=<ID=ADF,Number=R,Type=Integer,Description=\"Total allelic depths on the forward strand (high-quality bases)\">");
858 if ( conf->fmt_flag&B2B_INFO_SCR )
859 bcf_hdr_append(conf->bcf_hdr,"##INFO=<ID=SCR,Number=1,Type=Integer,Description=\"Number of soft-clipped reads (at high-quality bases)\">");
860 if ( conf->fmt_flag&B2B_FMT_SCR )
861 bcf_hdr_append(conf->bcf_hdr,"##FORMAT=<ID=SCR,Number=1,Type=Integer,Description=\"Per-sample number of soft-clipped reads (at high-quality bases)\">");
862 if ( conf->fmt_flag&B2B_INFO_ADR )
863 bcf_hdr_append(conf->bcf_hdr,"##INFO=<ID=ADR,Number=R,Type=Integer,Description=\"Total allelic depths on the reverse strand (high-quality bases)\">");
864 if ( conf->gvcf )
865 gvcf_update_header(conf->gvcf, conf->bcf_hdr);
866
867 int nsmpl;
868 const char **smpl = bam_smpl_get_samples(conf->bsmpl, &nsmpl);
869 for (i=0; i<nsmpl; i++)
870 bcf_hdr_add_sample(conf->bcf_hdr, smpl[i]);
871 if ( bcf_hdr_write(conf->bcf_fp, conf->bcf_hdr)!=0 ) error("[%s] Error: failed to write the header to %s\n",__func__,conf->output_fname?conf->output_fname:"standard output");
872 if ( init_index2(conf->bcf_fp,conf->bcf_hdr,conf->output_fname,
873 &conf->index_fn, conf->write_index) < 0 )
874 error("Error: failed to initialise index for %s\n",conf->output_fname);
875
876 conf->bca = bcf_call_init(-1., conf->min_baseQ, conf->max_baseQ,
877 conf->delta_baseQ);
878 conf->bcr = (bcf_callret1_t*) calloc(nsmpl, sizeof(bcf_callret1_t));
879 conf->bca->openQ = conf->openQ, conf->bca->extQ = conf->extQ, conf->bca->tandemQ = conf->tandemQ;
880 conf->bca->indel_bias = conf->indel_bias;
881 conf->bca->del_bias = conf->del_bias;
882 conf->bca->min_frac = conf->min_frac;
883 conf->bca->min_support = conf->min_support;
884 conf->bca->per_sample_flt = conf->flag & MPLP_PER_SAMPLE;
885 conf->bca->fmt_flag = conf->fmt_flag;
886 conf->bca->ambig_reads = conf->ambig_reads;
887 conf->bca->indel_win_size = conf->indel_win_size;
888 conf->bca->indels_v20 = conf->indels_v20;
889 conf->bca->edlib = conf->edlib;
890 conf->bca->seqQ_offset = conf->seqQ_offset;
891 conf->bca->poly_mqual = conf->poly_mqual;
892 conf->bca->vs_ref = conf->vs_ref;
893
894 conf->bc.bcf_hdr = conf->bcf_hdr;
895 conf->bc.n = nsmpl;
896 conf->bc.PL = (int32_t*) malloc(15 * nsmpl * sizeof(*conf->bc.PL));
897 conf->bc.QS = (int32_t*) malloc(nsmpl*sizeof(*conf->bc.QS)*B2B_MAX_ALLELES);
898 for (i=0; i<nsmpl; i++)
899 conf->bcr[i].QS = conf->bc.QS + i*B2B_MAX_ALLELES;
900 if (conf->fmt_flag)
901 {
902 assert( sizeof(float)==sizeof(int32_t) );
903 conf->bc.DP4 = (int32_t*) malloc(nsmpl * sizeof(int32_t) * 4);
904 conf->bc.fmt_arr = (uint8_t*) malloc(nsmpl * sizeof(float)); // all fmt_flag fields, float and int32
905 if ( conf->fmt_flag&(B2B_INFO_DPR|B2B_FMT_DPR|B2B_INFO_AD|B2B_INFO_ADF|B2B_INFO_ADR|B2B_FMT_AD|B2B_FMT_ADF|B2B_FMT_ADR) )
906 {
907 // first B2B_MAX_ALLELES fields for total numbers, the rest per-sample
908 conf->bc.ADR = (int32_t*) malloc((nsmpl+1)*B2B_MAX_ALLELES*sizeof(int32_t));
909 conf->bc.ADF = (int32_t*) malloc((nsmpl+1)*B2B_MAX_ALLELES*sizeof(int32_t));
910 for (i=0; i<nsmpl; i++)
911 {
912 conf->bcr[i].ADR = conf->bc.ADR + (i+1)*B2B_MAX_ALLELES;
913 conf->bcr[i].ADF = conf->bc.ADF + (i+1)*B2B_MAX_ALLELES;
914 }
915 }
916 if ( conf->fmt_flag&(B2B_INFO_SCR|B2B_FMT_SCR) )
917 conf->bc.SCR = (int32_t*) malloc((nsmpl+1)*sizeof(*conf->bc.SCR));
918 }
919 int nnmbz = (conf->fmt_flag&B2B_FMT_NMBZ) ? nsmpl + 1 : 1;
920 conf->bc.ref_nm = (int32_t*) malloc(sizeof(*conf->bc.ref_nm) * nnmbz * B2B_N_NM);
921 conf->bc.alt_nm = (int32_t*) malloc(sizeof(*conf->bc.alt_nm) * nnmbz * B2B_N_NM);
922 conf->bc.mwu_nm = (float*) malloc((nsmpl+1)*sizeof(*conf->bc.mwu_nm));
923 conf->bca->ref_nm = conf->bc.ref_nm; // this is just to make the arrays available in bcf_call_glfgen()
924 conf->bca->alt_nm = conf->bc.alt_nm;
925 if ( conf->fmt_flag&B2B_FMT_NMBZ )
926 {
927 for (i=0; i<nsmpl; i++) conf->bcr[i].ref_nm = conf->bc.ref_nm + (i+1)*B2B_N_NM;
928 for (i=0; i<nsmpl; i++) conf->bcr[i].alt_nm = conf->bc.alt_nm + (i+1)*B2B_N_NM;
929 }
930
931 // init mpileup
932 conf->iter = bam_mplp_init(conf->nfiles, mplp_func, (void**)conf->mplp_data);
933 if ( conf->flag & MPLP_SMART_OVERLAPS ) bam_mplp_init_overlaps(conf->iter);
934 fprintf(bcftools_stderr, "[%s] maximum number of reads per input file set to -d %d\n", __func__, conf->max_depth);
935 if ( (double)conf->max_depth * conf->nfiles > 1<<20)
936 fprintf(bcftools_stderr, "Warning: Potential memory hog, up to %.0fM reads in the pileup!\n", (double)conf->max_depth*conf->nfiles);
937 if ( (double)conf->max_depth * conf->nfiles / nsmpl < 250 )
938 fprintf(bcftools_stderr, "Note: The maximum per-sample depth with -d %d is %.1fx\n", conf->max_depth,(double)conf->max_depth * conf->nfiles / nsmpl);
939 bam_mplp_set_maxcnt(conf->iter, conf->max_depth);
940 conf->max_indel_depth = conf->max_indel_depth * nsmpl;
941 conf->bcf_rec = bcf_init1();
942 bam_mplp_constructor(conf->iter, pileup_constructor);
943 bam_mplp_destructor(conf->iter, pileup_destructor);
944
945
946 // Run mpileup for multiple regions
947 int ret = 0;
948 if ( nregs )
949 {
950 int ireg = 0;
951 do
952 {
953 // first region is already positioned
954 if ( ireg++ > 0 )
955 {
956 conf->buf.l = 0;
957 ksprintf(&conf->buf,"%s:%u-%u",conf->reg_itr->seq,conf->reg_itr->beg+1,conf->reg_itr->end+1);
958
959 for (i=0; i<conf->nfiles; i++)
960 {
961 hts_itr_destroy(conf->mplp_data[i]->iter);
962 conf->mplp_data[i]->iter = sam_itr_querys(conf->mplp_data[i]->idx, conf->mplp_data[i]->h, conf->buf.s);
963 if ( !conf->mplp_data[i]->iter )
964 {
965 conf->mplp_data[i]->iter = sam_itr_querys(conf->mplp_data[i]->idx, conf->mplp_data[i]->h, conf->reg_itr->seq);
966 if ( conf->mplp_data[i]->iter ) {
967 fprintf(bcftools_stderr,"[E::%s] fail to parse region '%s'\n", __func__, conf->buf.s);
968 bcftools_exit(EXIT_FAILURE);
969 }
970 fprintf(bcftools_stderr,"[E::%s] the sequence \"%s\" not found: %s\n",__func__,conf->reg_itr->seq,conf->files[i]);
971 bcftools_exit(EXIT_FAILURE);
972 }
973 bam_mplp_reset(conf->iter);
974 }
975 }
976 ret = mpileup_reg(conf,conf->reg_itr->beg,conf->reg_itr->end);
977 if ( ret<0 ) break;
978 }
979 while ( regitr_loop(conf->reg_itr) );
980 }
981 else if ( !conf->reg )
982 ret = mpileup_reg(conf,0,UINT32_MAX);
983 if ( ret<0 )
984 {
985 fprintf(bcftools_stderr, "[%s] failed to read from input file\n", __func__);
986 bcftools_exit(EXIT_FAILURE);
987 }
988
989 flush_bcf_records(conf, conf->bcf_fp, conf->bcf_hdr, NULL);
990
991 // clean up
992 free(conf->bc.tmp.s);
993 bcf_destroy1(conf->bcf_rec);
994 if (conf->bcf_fp)
995 {
996 if ( conf->write_index )
997 {
998 if ( bcf_idx_save(conf->bcf_fp)<0 )
999 {
1000 if ( hts_close(conf->bcf_fp)!=0 ) error("[%s] Error: close failed .. %s\n", __func__,conf->output_fname);
1001 error("Error: cannot write to index %s\n",conf->index_fn);
1002 }
1003 free(conf->index_fn);
1004 }
1005 if ( hts_close(conf->bcf_fp)!=0 ) error("[%s] Error: close failed .. %s\n", __func__,conf->output_fname);
1006 bcf_hdr_destroy(conf->bcf_hdr);
1007 bcf_call_destroy(conf->bca);
1008 free(conf->bc.PL);
1009 free(conf->bc.DP4);
1010 free(conf->bc.ADR);
1011 free(conf->bc.ADF);
1012 free(conf->bc.SCR);
1013 free(conf->bc.QS);
1014 free(conf->bc.ref_nm);
1015 free(conf->bc.alt_nm);
1016 free(conf->bc.fmt_arr);
1017 free(conf->bc.mwu_nm);
1018 free(conf->bcr);
1019 }
1020 if ( conf->gvcf ) gvcf_destroy(conf->gvcf);
1021 free(conf->buf.s);
1022 for (i = 0; i < conf->gplp->n; ++i) free(conf->gplp->plp[i]);
1023 free(conf->gplp->plp); free(conf->gplp->n_plp); free(conf->gplp->m_plp); free(conf->gplp);
1024 bam_mplp_destroy(conf->iter);
1025 bam_hdr_destroy(hdr);
1026 for (i = 0; i < conf->nfiles; ++i) {
1027 if ( nregs>1 ) hts_idx_destroy(conf->mplp_data[i]->idx);
1028 sam_close(conf->mplp_data[i]->fp);
1029 if ( conf->mplp_data[i]->iter) hts_itr_destroy(conf->mplp_data[i]->iter);
1030 free(conf->mplp_data[i]);
1031 }
1032 if ( conf->reg_itr ) regitr_destroy(conf->reg_itr);
1033 free(conf->mplp_data); free(conf->plp); free(conf->n_plp);
1034 free(mp_ref.ref[0]);
1035 free(mp_ref.ref[1]);
1036 return 0;
1037 }
1038
1039 static int is_url(const char *s)
1040 {
1041 static const char uri_scheme_chars[] =
1042 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+.-";
1043 return s[strspn(s, uri_scheme_chars)] == ':';
1044 }
1045
1046 #define MAX_PATH_LEN 1024
1047 int read_file_list(const char *file_list,int *n,char **argv[])
1048 {
1049 char buf[MAX_PATH_LEN];
1050 int len, nfiles = 0;
1051 char **files = NULL;
1052 struct stat sb;
1053
1054 *n = 0;
1055 *argv = NULL;
1056
1057 FILE *fh = fopen(file_list,"r");
1058 if ( !fh )
1059 {
1060 fprintf(bcftools_stderr,"%s: %s\n", file_list,strerror(errno));
1061 return 1;
1062 }
1063
1064 files = (char**) calloc(nfiles,sizeof(char*));
1065 nfiles = 0;
1066 while ( fgets(buf,MAX_PATH_LEN,fh) )
1067 {
1068 // allow empty lines and trailing spaces
1069 len = strlen(buf);
1070 while ( len>0 && isspace(buf[len-1]) ) len--;
1071 if ( !len ) continue;
1072
1073 // check sanity of the file list
1074 buf[len] = 0;
1075 if (! (is_url(buf) || stat(buf, &sb) == 0))
1076 {
1077 // no such file, check if it is safe to print its name
1078 int i, safe_to_print = 1;
1079 for (i=0; i<len; i++)
1080 if (!isprint(buf[i])) { safe_to_print = 0; break; }
1081 if ( safe_to_print )
1082 fprintf(bcftools_stderr,"The file list \"%s\" appears broken, could not locate: %s\n", file_list,buf);
1083 else
1084 fprintf(bcftools_stderr,"Does the file \"%s\" really contain a list of files and do all exist?\n", file_list);
1085 return 1;
1086 }
1087
1088 nfiles++;
1089 files = (char**) realloc(files,nfiles*sizeof(char*));
1090 files[nfiles-1] = strdup(buf);
1091 }
1092 if ( fclose(fh)!=0 ) error("[%s] Error: close failed .. %s\n", __func__,file_list);
1093 if ( !nfiles )
1094 {
1095 fprintf(bcftools_stderr,"No files read from %s\n", file_list);
1096 return 1;
1097 }
1098 *argv = files;
1099 *n = nfiles;
1100 return 0;
1101 }
1102 #undef MAX_PATH_LEN
1103
1104 #define SET_FMT_FLAG(str,bit,msg) \
1105 if (!strcasecmp(tag,str) || !strcasecmp(tag,"FMT/"str) || !strcasecmp(tag,"FORMAT/"str)) \
1106 { \
1107 if ( *msg ) fprintf(bcftools_stderr,"%s",msg); \
1108 if ( exclude ) \
1109 *flag &= ~bit; \
1110 else \
1111 *flag |= bit; \
1112 free(tags[i]); \
1113 continue; \
1114 }
1115 #define SET_INFO_FLAG(str,bit,msg) if (!strcasecmp(tag,"INFO/"str)) \
1116 { \
1117 if ( exclude ) \
1118 *flag &= ~bit; \
1119 else \
1120 *flag |= bit; \
1121 free(tags[i]); \
1122 continue; \
1123 }
1124
1125 void parse_format_flag(uint32_t *flag, const char *str)
1126 {
1127 int i, n_tags;
1128 char **tags = hts_readlist(str, 0, &n_tags);
1129 for(i=0; i<n_tags; i++)
1130 {
1131 int exclude = tags[i][0]=='-' ? 1 : 0;
1132 char *tag = exclude ? tags[i]+1 : tags[i];
1133 SET_FMT_FLAG("AD", B2B_FMT_AD, "");
1134 SET_FMT_FLAG("ADF", B2B_FMT_ADF, "");
1135 SET_FMT_FLAG("ADR", B2B_FMT_ADR, "");
1136 SET_FMT_FLAG("DP", B2B_FMT_DP, "");
1137 SET_FMT_FLAG("DP4", B2B_FMT_DP4, "[warning] tag DP4 functional, but deprecated. Please switch to `ADF` and `ADR` in future.\n");
1138 SET_FMT_FLAG("DPR", B2B_FMT_DPR, "[warning] tag DPR functional, but deprecated. Please switch to `AD` in future.\n");
1139 SET_FMT_FLAG("DV", B2B_FMT_DV, "[warning] tag DV functional, but deprecated. Please switch to `AD` in future.\n");
1140 SET_FMT_FLAG("NMBZ", B2B_FMT_NMBZ, "");
1141 SET_FMT_FLAG("QS", B2B_FMT_QS, "");
1142 SET_FMT_FLAG("SP", B2B_FMT_SP, "");
1143 SET_FMT_FLAG("SCR", B2B_FMT_SCR, "");
1144 SET_INFO_FLAG("DPR", B2B_INFO_DPR, "[warning] tag INFO/DPR functional, but deprecated. Please switch to `INFO/AD` in future.\n");
1145 SET_INFO_FLAG("AD", B2B_INFO_AD, "");
1146 SET_INFO_FLAG("ADF", B2B_INFO_ADF, "");
1147 SET_INFO_FLAG("ADR", B2B_INFO_ADR, "");
1148 SET_INFO_FLAG("BQBZ", B2B_INFO_BQBZ, "");
1149 SET_INFO_FLAG("FS", B2B_INFO_FS, "");
1150 SET_INFO_FLAG("IDV", B2B_INFO_IDV, "");
1151 SET_INFO_FLAG("IMF", B2B_INFO_IMF, "");
1152 SET_INFO_FLAG("MIN_PL_SUM", B2B_INFO_MIN_PL_SUM, "");
1153 SET_INFO_FLAG("MQ0F", B2B_INFO_MQ0F, "");
1154 SET_INFO_FLAG("MQBZ", B2B_INFO_MQBZ, "");
1155 SET_INFO_FLAG("NM", B2B_INFO_NM, "");
1156 SET_INFO_FLAG("NMBZ", B2B_INFO_NMBZ, "");
1157 SET_INFO_FLAG("RPBZ", B2B_INFO_RPBZ, "");
1158 SET_INFO_FLAG("SCBZ", B2B_INFO_SCBZ, "");
1159 SET_INFO_FLAG("SCR", B2B_INFO_SCR, "");
1160 SET_INFO_FLAG("SGB", B2B_INFO_SGB, "");
1161 SET_INFO_FLAG("VDB", B2B_INFO_VDB, "");
1162 fprintf(bcftools_stderr,"Could not parse tag \"%s\" in \"%s\"\n", tag, str);
1163 bcftools_exit(EXIT_FAILURE);
1164 }
1165 if (n_tags) free(tags);
1166 }
1167
1168 // todo: make it possible to turn off some annotations or change the defaults,
1169 // specifically RPB, VDB, MWU, SGB tests. It would be good to do some
1170 // benchmarking first to see if it's worth it.
1171 static void list_annotations(FILE *fp)
1172 {
1173 fprintf(fp,
1174 "Annotations added by default are in this list prefixed with \"*\". To suppress their output, run with\n"
1175 "e.g. \"-a -FORMAT/AD\".\n"
1176 "\n"
1177 "FORMAT annotation tags available (\"FORMAT/\" prefix is optional):\n"
1178 "\n"
1179 "* FORMAT/AD .. Allelic depth (Number=R,Type=Integer)\n"
1180 " FORMAT/ADF .. Allelic depths on the forward strand (Number=R,Type=Integer)\n"
1181 " FORMAT/ADR .. Allelic depths on the reverse strand (Number=R,Type=Integer)\n"
1182 " FORMAT/DP .. Number of high-quality bases (Number=1,Type=Integer)\n"
1183 " FORMAT/NMBZ .. Mann-Whitney U-z test of Number of Mismatches within supporting reads (Number=1,Type=Float)\n"
1184 " FORMAT/QS .. Allele phred-score quality sum for use with `call -mG` and +trio-dnm (Number=R,Type=Integer)\n"
1185 " FORMAT/SP .. Phred-scaled strand bias P-value (Number=1,Type=Integer)\n"
1186 " FORMAT/SCR .. Number of soft-clipped reads (Number=1,Type=Integer)\n"
1187 "\n"
1188 "INFO annotation tags available:\n"
1189 "\n"
1190 " INFO/AD .. Total allelic depth (Number=R,Type=Integer)\n"
1191 " INFO/ADF .. Total allelic depths on the forward strand (Number=R,Type=Integer)\n"
1192 " INFO/ADR .. Total allelic depths on the reverse strand (Number=R,Type=Integer)\n"
1193 "* INFO/BQBZ .. Mann-Whitney U test of Base Quality Bias (Number=1,Type=Float)\n"
1194 " INFO/FS .. Fisher's exact test P-value to detect strand bias (Number=1,Type=Float)\n"
1195 "* INFO/IDV .. Maximum number of raw reads supporting an indel (Number=1,Type=Integer)\n"
1196 "* INFO/IMF .. Maximum fraction of raw reads supporting an indel (Number=1,Type=Float)\n"
1197 " INFO/MIN_PL_SUM\n"
1198 " .. Sum of min PL across all samples before normalization, experimental (Number=1,Type=Integer)\n"
1199 "* INFO/MQ0F .. Fraction of reads with zero mapping quality (Number=1,Type=Float)\n"
1200 "* INFO/MQBZ .. Mann-Whitney U test of Mapping Quality Bias (Number=1,Type=Float)\n"
1201 "* INFO/MQSBZ .. Mann-Whitney U-z test of Mapping Quality vs Strand Bias (Number=1,Type=Float)\n"
1202 " INFO/NM .. Approximate average number of mismatches in ref and alt reads, experimental (Number=2,Type=Float)\n"
1203 " INFO/NMBZ .. Mann-Whitney U-z test of Number of Mismatches within supporting reads (Number=1,Type=Float)\n"
1204 "* INFO/RPBZ .. Mann-Whitney U test of Read Position Bias (Number=1,Type=Float)\n"
1205 "* INFO/SCBZ .. Mann-Whitney U-z test of Soft-Clip Length Bias (Number=1,Type=Float)\n"
1206 " INFO/SCR .. Number of soft-clipped reads (Number=1,Type=Integer)\n"
1207 "* INFO/SGB .. Segregation based metric, http://samtools.github.io/bcftools/rd-SegBias.pdf (Number=1,Type=Float)\n"
1208 "* INFO/VDB .. Variant Distance Bias for filtering splice-site artefacts in RNA-seq data (Number=1,Type=Float)\n"
1209 "\n");
1210 }
1211
1212 static void print_usage(FILE *fp, const mplp_conf_t *mplp)
1213 {
1214 char *tmp_skip_all_set = bam_flag2str(mplp->rflag_skip_all_set);
1215 char *tmp_skip_any_unset = bam_flag2str(mplp->rflag_skip_any_unset);
1216 char *tmp_skip_all_unset = bam_flag2str(mplp->rflag_skip_all_unset);
1217 char *tmp_skip_any_set = bam_flag2str(mplp->rflag_skip_any_set);
1218
1219 // Display usage information, formatted for the standard 80 columns.
1220 // (The unusual string formatting here aids the readability of this
1221 // source code in 80 columns, to the extent that's possible.)
1222
1223 fprintf(fp,
1224 "\n"
1225 "Usage: bcftools mpileup [options] in1.bam [in2.bam [...]]\n"
1226 "\n"
1227 "Input options:\n"
1228 " -6, --illumina1.3+ Quality is in the Illumina-1.3+ encoding\n"
1229 " -A, --count-orphans Include anomalous read pairs, with flag PAIRED but not PROPER_PAIR set\n"
1230 " -b, --bam-list FILE List of input BAM filenames, one per line\n"
1231 " -B, --no-BAQ Disable BAQ (per-Base Alignment Quality)\n"
1232 " -C, --adjust-MQ INT Adjust mapping quality [0]\n"
1233 " -D, --full-BAQ Apply BAQ everywhere, not just in problematic regions\n"
1234 " -d, --max-depth INT Max raw per-file depth; avoids excessive memory usage [%d]\n", mplp->max_depth);
1235 fprintf(fp,
1236 " -E, --redo-BAQ Recalculate BAQ on the fly, ignore existing BQs\n"
1237 " -f, --fasta-ref FILE Faidx indexed reference sequence file\n"
1238 " --no-reference Do not require fasta reference file\n"
1239 " -G, --read-groups FILE Select or exclude read groups listed in the file\n"
1240 " -q, --min-MQ INT Skip alignments with mapQ smaller than INT [%d]\n", mplp->min_mq);
1241 fprintf(fp,
1242 " -Q, --min-BQ INT Skip bases with baseQ/BAQ smaller than INT [%d]\n", mplp->min_baseQ);
1243 fprintf(fp,
1244 " --max-BQ INT Limit baseQ/BAQ to no more than INT [%d]\n", mplp->max_baseQ);
1245 fprintf(fp,
1246 " --delta-BQ INT Use neighbour_qual + INT if less than qual [%d]\n", mplp->delta_baseQ);
1247 fprintf(fp,
1248 " -r, --regions REG[,...] Comma separated list of regions in which pileup is generated\n"
1249 " -R, --regions-file FILE Restrict to regions listed in a file\n"
1250 " --ignore-RG Ignore RG tags (one BAM = one sample)\n"
1251 " --ls, --skip-all-set STR|INT Skip reads with all of the bits set []\n");
1252 fprintf(fp,
1253 " --ns, --skip-any-set STR|INT Skip reads with any of the bits set [%s]\n", tmp_skip_any_set);
1254 fprintf(fp,
1255 " --lu, --skip-all-unset STR|INT Skip reads with all of the bits unset []\n"
1256 " --nu, --skip-any-unset STR|INT Skip reads with any of the bits unset []\n");
1257 fprintf(fp,
1258 " -s, --samples LIST Comma separated list of samples to include\n"
1259 " -S, --samples-file FILE File of samples to include\n"
1260 " -t, --targets REG[,...] Similar to -r but streams rather than index-jumps\n"
1261 " -T, --targets-file FILE Similar to -R but streams rather than index-jumps\n"
1262 " -x, --ignore-overlaps Disable read-pair overlap detection\n"
1263 " --seed INT Random number seed used for sampling deep regions [0]\n"
1264 "\n"
1265 "Output options:\n"
1266 " -a, --annotate LIST Optional tags to output; '\\?' to list available tags []\n"
1267 " -g, --gvcf INT[,...] Group non-variant sites into gVCF blocks according\n"
1268 " To minimum per-sample DP\n"
1269 " --no-version Do not append version and command line to the header\n"
1270 " -o, --output FILE Write output to FILE [standard output]\n"
1271 " -O, --output-type TYPE 'b' compressed BCF; 'u' uncompressed BCF;\n"
1272 " 'z' compressed VCF; 'v' uncompressed VCF; 0-9 compression level [v]\n"
1273 " --threads INT Use multithreading with INT worker threads [0]\n"
1274 " -W, --write-index[=FMT] Automatically index the output files [off]\n"
1275 "\n"
1276 "SNP/INDEL genotype likelihoods options:\n"
1277 " -X, --config STR Specify platform profile (use \"-X list\" for details)\n"
1278 " -e, --ext-prob INT Phred-scaled gap extension seq error probability [%d]\n", mplp->extQ);
1279 fprintf(fp,
1280 " -F, --gap-frac FLOAT Minimum fraction of gapped reads [%g]\n", mplp->min_frac);
1281 fprintf(fp,
1282 " -h, --tandem-qual INT Coefficient for homopolymer errors [%d]\n", mplp->tandemQ);
1283 fprintf(fp,
1284 " -I, --skip-indels Do not perform indel calling\n"
1285 " -L, --max-idepth INT Maximum per-file depth for INDEL calling [%d]\n", mplp->max_indel_depth);
1286 fprintf(fp,
1287 " -m, --min-ireads INT Minimum number gapped reads for indel candidates [%d]\n", mplp->min_support);
1288 fprintf(fp,
1289 " -M, --max-read-len INT Maximum length of read to pass to BAQ algorithm [%d]\n", mplp->max_read_len);
1290 fprintf(fp,
1291 " -o, --open-prob INT Phred-scaled gap open seq error probability [%d]\n", mplp->openQ);
1292 fprintf(fp,
1293 " -p, --per-sample-mF Apply -m and -F per-sample for increased sensitivity\n"
1294 " -P, --platforms STR Comma separated list of platforms for indels [all]\n"
1295 " --ar, --ambig-reads STR What to do with ambiguous indel reads: drop,incAD,incAD0 [drop]\n");
1296 fprintf(fp,
1297 " --indel-bias FLOAT Raise to favour recall over precision [%.2f]\n", mplp->indel_bias);
1298 fprintf(fp,
1299 " --del-bias FLOAT Relative likelihood of insertion to deletion [%.2f]\n", mplp->del_bias);
1300 fprintf(fp,
1301 " --score-vs-ref FLOAT\n"
1302 " Ratio of score vs ref (1) or 2nd-best allele (0) [%.2f]\n", mplp->vs_ref);
1303 fprintf(fp,
1304 " --indel-size INT Approximate maximum indel size considered [%d]\n", mplp->indel_win_size);
1305 fprintf(fp,
1306 " --indels-2.0 New EXPERIMENTAL indel calling model (diploid reference consensus)\n"
1307 " --indels-cns New EXPERIMENTAL indel calling model with edlib\n"
1308 " --seqq-offset Indel-cns tuning for indel seq-qual scores [120]\n"
1309 " --no-indels-cns Disable CNS mode, to use after a -X profile\n"
1310 " --poly-mqual (Edlib mode) Use minimum quality within homopolymers\n");
1311 fprintf(fp,"\n");
1312 fprintf(fp,
1313 "Notes: Assuming diploid individuals.\n\n"
1314 "Example:\n"
1315 " # See also http://samtools.github.io/bcftools/howtos/variant-calling.html\n"
1316 " bcftools mpileup -Ou -f reference.fa alignments.bam | bcftools call -mv -Ob -o calls.bcf\n"
1317 "\n");
1318
1319 free(tmp_skip_all_set);
1320 free(tmp_skip_any_unset);
1321 free(tmp_skip_all_unset);
1322 free(tmp_skip_any_set);
1323 }
1324
1325 static void print_profiles(void) {
1326 fprintf(bcftools_stdout,
1327 "Configuration profiles activated with -X, --config:\n\n"
1328 "1.12\n"
1329 " -Q13 -h100 -m1 -F0.002\n\n"
1330 "bgi, bgi-1.20\n"
1331 " --indels-cns -B --indel-size 80 -F0.1 --indel-bias 0.9 --seqq-offset 120\n\n"
1332 "illumina-1.18\n"
1333 " --indel-size 110\n\n"
1334 "illumina\n"
1335 "illumina-1.20\n"
1336 " --indels-cns --indel-size 110\n\n"
1337 "ont\n"
1338 " -B -Q5 --max-BQ 30 -I\n\n"
1339 "ont-sup, ont-sup-1.20\n"
1340 " --indels-cns -B -Q1 --max-BQ 35 -F0.2 -o15 -e1 -h110 --delta-BQ 99\\\n"
1341 " --del-bias 0.4 --indel-bias 0.7 --poly-mqual --seqq-offset 130\\\n"
1342 " --indel-size 80\n\n"
1343 "pacbio-ccs-1.18\n"
1344 " -D -Q5 --max-BQ 50 -F0.1 -o25 -e1 --delta-BQ 10 \\\n"
1345 " -M99999 --indel-size 110\n\n"
1346 "pacbio-ccs, pacbio-ccs-1.20\n"
1347 " --indels-cns -B -Q5 --max-BQ 50 -F0.1 -o25 -e1 -h300 --delta-BQ 10 \\\n"
1348 " --del-bias 0.4 --poly-mqual --indel-bias 0.9 --seqq-offset 118\\\n"
1349 " --indel-size 80 --score-vs-ref 0.7\n\n"
1350 "ultima, ultima-1.20\n"
1351 " --indels-cns -B -Q1 --max-BQ 30 -F0.15 -o20 -e10 -h250 --delta-BQ 10 \\\n"
1352 " --del-bias 0.3 --indel-bias 0.7 --poly-mqual --seqq-offset 140 \\\n"
1353 " --indel-size 80 --score-vs-ref 0.3\n\n"
1354 "\n");
1355 }
1356
1357 int main_mpileup(int argc, char *argv[])
1358 {
1359 int c, i, ret = 1;
1360 const char *file_list = NULL;
1361 char **fn = NULL;
1362 int nfiles = 0, use_orphan = 0, noref = 0;
1363 mplp_conf_t mplp;
1364 memset(&mplp, 0, sizeof(mplp_conf_t));
1365 mplp.min_baseQ = 1;
1366 mplp.max_baseQ = 60;
1367 mplp.delta_baseQ = 30;
1368 mplp.capQ_thres = 0;
1369 mplp.max_depth = 250; mplp.max_indel_depth = 250;
1370 mplp.openQ = 40; mplp.extQ = 20; mplp.tandemQ = 500;
1371 mplp.min_frac = 0.05; mplp.indel_bias = 1.0; mplp.min_support = 2;
1372 mplp.vs_ref = 0;
1373 mplp.flag = MPLP_NO_ORPHAN | MPLP_REALN | MPLP_REALN_PARTIAL
1374 | MPLP_SMART_OVERLAPS;
1375 mplp.argc = argc; mplp.argv = argv;
1376 mplp.rflag_skip_any_set = BAM_FUNMAP | BAM_FSECONDARY | BAM_FQCFAIL | BAM_FDUP;
1377 mplp.output_fname = NULL;
1378 mplp.output_type = FT_VCF;
1379 mplp.record_cmd_line = 1;
1380 mplp.n_threads = 0;
1381 mplp.bsmpl = bam_smpl_init();
1382 // the default to be changed in future, see also parse_format_flag()
1383 mplp.fmt_flag = B2B_INFO_BQBZ|B2B_INFO_IDV|B2B_INFO_IMF|B2B_INFO_MQ0F|B2B_INFO_MQBZ|B2B_INFO_MQSBZ|B2B_INFO_RPBZ|B2B_INFO_SCBZ|B2B_INFO_SGB|B2B_INFO_VDB|B2B_FMT_AD;
1384 mplp.max_read_len = 500;
1385 mplp.ambig_reads = B2B_DROP;
1386 mplp.indel_win_size = 110;
1387 mplp.poly_mqual = 0;
1388 mplp.seqQ_offset = 120;
1389 mplp.clevel = -1;
1390 mplp.del_bias = 0; // even insertion and deletion likelhoods.
1391 hts_srand48(0);
1392
1393 static const struct option lopts[] =
1394 {
1395 {"nu", required_argument, NULL, 16},
1396 {"lu", required_argument, NULL, 17},
1397 {"rf", required_argument, NULL, 17}, // old --rf, --incl-flags = --lu, --skip-all-unset
1398 {"ns", required_argument, NULL, 18},
1399 {"ff", required_argument, NULL, 18}, // old --ff, --excl-flags = --ns, --skip-any-set
1400 {"ls", required_argument, NULL, 19},
1401 {"skip-any-unset", required_argument, NULL, 16},
1402 {"skip-all-unset", required_argument, NULL, 17},
1403 {"skip-any-set", required_argument, NULL, 18},
1404 {"skip-all-set", required_argument, NULL, 19},
1405 {"output", required_argument, NULL, 3},
1406 {"open-prob", required_argument, NULL, 4},
1407 {"ignore-RG", no_argument, NULL, 5},
1408 {"ignore-rg", no_argument, NULL, 5},
1409 {"gvcf", required_argument, NULL, 'g'},
1410 {"no-reference", no_argument, NULL, 7},
1411 {"no-version", no_argument, NULL, 8},
1412 {"threads",required_argument,NULL,9},
1413 {"illumina1.3+", no_argument, NULL, '6'},
1414 {"count-orphans", no_argument, NULL, 'A'},
1415 {"bam-list", required_argument, NULL, 'b'},
1416 {"no-BAQ", no_argument, NULL, 'B'},
1417 {"no-baq", no_argument, NULL, 'B'},
1418 {"full-BAQ", no_argument, NULL, 'D'},
1419 {"full-baq", no_argument, NULL, 'D'},
1420 {"adjust-MQ", required_argument, NULL, 'C'},
1421 {"adjust-mq", required_argument, NULL, 'C'},
1422 {"max-depth", required_argument, NULL, 'd'},
1423 {"redo-BAQ", no_argument, NULL, 'E'},
1424 {"redo-baq", no_argument, NULL, 'E'},
1425 {"fasta-ref", required_argument, NULL, 'f'},
1426 {"read-groups", required_argument, NULL, 'G'},
1427 {"region", required_argument, NULL, 'r'},
1428 {"regions", required_argument, NULL, 'r'},
1429 {"regions-file", required_argument, NULL, 'R'},
1430 {"targets", required_argument, NULL, 't'},
1431 {"targets-file", required_argument, NULL, 'T'},
1432 {"min-MQ", required_argument, NULL, 'q'},
1433 {"min-mq", required_argument, NULL, 'q'},
1434 {"min-BQ", required_argument, NULL, 'Q'},
1435 {"min-bq", required_argument, NULL, 'Q'},
1436 {"max-bq", required_argument, NULL, 11},
1437 {"max-BQ", required_argument, NULL, 11},
1438 {"delta-BQ", required_argument, NULL, 12},
1439 {"ignore-overlaps", no_argument, NULL, 'x'},
1440 {"output-type", required_argument, NULL, 'O'},
1441 {"samples", required_argument, NULL, 's'},
1442 {"samples-file", required_argument, NULL, 'S'},
1443 {"annotate", required_argument, NULL, 'a'},
1444 {"ext-prob", required_argument, NULL, 'e'},
1445 {"gap-frac", required_argument, NULL, 'F'},
1446 {"indel-bias", required_argument, NULL, 10},
1447 {"indel-size", required_argument, NULL, 15},
1448 {"indels-2.0", no_argument, NULL, 20},
1449 {"indels-cns", no_argument, NULL, 22},
1450 {"no-indels-cns", no_argument, NULL, 25},
1451 {"tandem-qual", required_argument, NULL, 'h'},
1452 {"skip-indels", no_argument, NULL, 'I'},
1453 {"max-idepth", required_argument, NULL, 'L'},
1454 {"min-ireads", required_argument, NULL, 'm'},
1455 {"per-sample-mF", no_argument, NULL, 'p'},
1456 {"per-sample-mf", no_argument, NULL, 'p'},
1457 {"platforms", required_argument, NULL, 'P'},
1458 {"max-read-len", required_argument, NULL, 'M'},
1459 {"config", required_argument, NULL, 'X'},
1460 {"seed", required_argument, NULL, 13},
1461 {"ambig-reads", required_argument, NULL, 14},
1462 {"ar", required_argument, NULL, 14},
1463 {"write-index",optional_argument,NULL,'W'},
1464 {"del-bias", required_argument, NULL, 23},
1465 {"poly-mqual", no_argument, NULL, 24},
1466 {"no-poly-mqual", no_argument, NULL, 26},
1467 {"score-vs-ref",required_argument, NULL, 27},
1468 {"seqq-offset", required_argument, NULL, 28},
1469 {NULL, 0, NULL, 0}
1470 };
1471 while ((c = getopt_long(argc, argv, "Ag:f:r:R:q:Q:C:BDd:L:b:P:po:e:h:Im:F:EG:6O:xa:s:S:t:T:M:X:UW::",lopts,NULL)) >= 0) {
1472 switch (c) {
1473 case 'x': mplp.flag &= ~MPLP_SMART_OVERLAPS; break;
1474 case 16 :
1475 mplp.rflag_skip_any_unset = bam_str2flag(optarg);
1476 if ( mplp.rflag_skip_any_unset <0 ) {
1477 fprintf(bcftools_stderr,"Could not parse --nf %s\n", optarg);
1478 goto err;
1479 }
1480 break;
1481 case 17 :
1482 mplp.rflag_skip_all_unset = bam_str2flag(optarg);
1483 if ( mplp.rflag_skip_all_unset<0 ) {
1484 fprintf(bcftools_stderr,"Could not parse --if %s\n", optarg);
1485 goto err;
1486 }
1487 break;
1488 case 18 :
1489 mplp.rflag_skip_any_set = bam_str2flag(optarg);
1490 if ( mplp.rflag_skip_any_set <0 ) {
1491 fprintf(bcftools_stderr,"Could not parse --ef %s\n", optarg);
1492 goto err;
1493 }
1494 break;
1495 case 19 :
1496 mplp.rflag_skip_all_set = bam_str2flag(optarg);
1497 if ( mplp.rflag_skip_all_set <0 ) {
1498 fprintf(bcftools_stderr,"Could not parse --df %s\n", optarg);
1499 goto err;
1500 }
1501 break;
1502 case 3 : mplp.output_fname = optarg; break;
1503 case 4 : mplp.openQ = atoi(optarg); break;
1504 case 5 : bam_smpl_ignore_readgroups(mplp.bsmpl); break;
1505 case 'g':
1506 mplp.gvcf = gvcf_init(optarg);
1507 if ( !mplp.gvcf ) error("Could not parse: --gvcf %s\n", optarg);
1508 break;
1509 case 'f':
1510 mplp.fai = fai_load(optarg);
1511 if (mplp.fai == NULL)
1512 goto err;
1513 mplp.fai_fname = optarg;
1514 break;
1515 case 7 : noref = 1; break;
1516 case 8 : mplp.record_cmd_line = 0; break;
1517 case 9 : mplp.n_threads = strtol(optarg, 0, 0); break;
1518 case 'd': mplp.max_depth = atoi(optarg); break;
1519 case 'r': mplp.reg_fname = strdup(optarg); break;
1520 case 'R': mplp.reg_fname = strdup(optarg); mplp.reg_is_file = 1; break;
1521 case 't':
1522 // In the original version the whole BAM was streamed which is inefficient
1523 // with few BED intervals and big BAMs. Todo: devise a heuristic to determine
1524 // best strategy, that is streaming or jumping.
1525 if ( optarg[0]=='^' ) optarg++;
1526 else mplp.bed_logic = 1;
1527 mplp.bed = regidx_init(NULL,regidx_parse_reg,NULL,0,NULL);
1528 mplp.bed_itr = regitr_init(mplp.bed);
1529 if ( regidx_insert_list(mplp.bed,optarg,',') !=0 )
1530 {
1531 fprintf(bcftools_stderr,"Could not parse the targets: %s\n", optarg);
1532 bcftools_exit(EXIT_FAILURE);
1533 }
1534 break;
1535 case 'T':
1536 if ( optarg[0]=='^' ) optarg++;
1537 else mplp.bed_logic = 1;
1538 mplp.bed = regidx_init(optarg,NULL,NULL,0,NULL);
1539 if (!mplp.bed) {
1540 fprintf(bcftools_stderr, "bcftools mpileup: Could not read file \"%s\"", optarg);
1541 goto err;
1542 }
1543 break;
1544 case 'P': mplp.pl_list = strdup(optarg); break;
1545 case 'p': mplp.flag |= MPLP_PER_SAMPLE; break;
1546 case 'B': mplp.flag &= ~MPLP_REALN; break;
1547 case 'D': mplp.flag &= ~MPLP_REALN_PARTIAL; break;
1548 case 'I': mplp.flag |= MPLP_NO_INDEL; break;
1549 case 'E': mplp.flag |= MPLP_REDO_BAQ; break;
1550 case '6': mplp.flag |= MPLP_ILLUMINA13; break;
1551 case 's': if ( bam_smpl_add_samples(mplp.bsmpl,optarg,0)<0 ) error("Could not read samples: %s\n",optarg); break;
1552 case 'S': if ( bam_smpl_add_samples(mplp.bsmpl,optarg,1)<0 ) error("Could not read samples: %s\n",optarg); break;
1553 case 'O':
1554 switch (optarg[0]) {
1555 case 'b': mplp.output_type = FT_BCF_GZ; break;
1556 case 'u': mplp.output_type = FT_BCF; break;
1557 case 'z': mplp.output_type = FT_VCF_GZ; break;
1558 case 'v': mplp.output_type = FT_VCF; break;
1559 default:
1560 {
1561 char *tmp;
1562 mplp.clevel = strtol(optarg,&tmp,10);
1563 if ( *tmp || mplp.clevel<0 || mplp.clevel>9 ) error("The output type \"%s\" not recognised\n", optarg);
1564 }
1565 }
1566 if ( optarg[1] )
1567 {
1568 char *tmp;
1569 mplp.clevel = strtol(optarg+1,&tmp,10);
1570 if ( *tmp || mplp.clevel<0 || mplp.clevel>9 ) error("Could not parse argument: --output-type %s\n", optarg+1);
1571 }
1572 break;
1573 case 'C': mplp.capQ_thres = atoi(optarg); break;
1574 case 'q': mplp.min_mq = atoi(optarg); break;
1575 case 'Q': mplp.min_baseQ = atoi(optarg); break;
1576 case 11: mplp.max_baseQ = atoi(optarg); break;
1577 case 12: mplp.delta_baseQ = atoi(optarg); break;
1578 case 'b': file_list = optarg; break;
1579 case 'o': {
1580 char *end;
1581 long value = strtol(optarg, &end, 10);
1582 // Distinguish between -o INT and -o FILE (a bit of a hack!)
1583 if (*end == '\0') mplp.openQ = value;
1584 else mplp.output_fname = optarg;
1585 }
1586 break;
1587 case 'e': mplp.extQ = atoi(optarg); break;
1588 case 'h': mplp.tandemQ = atoi(optarg); break;
1589 case 10: // --indel-bias (inverted so higher => more indels called)
1590 if (atof(optarg) < 1e-2)
1591 mplp.indel_bias = 1/1e2;
1592 else
1593 mplp.indel_bias = 1/atof(optarg);
1594 break;
1595 case 27:
1596 mplp.vs_ref = atof(optarg);
1597 //if (mplp.vs_ref < 0) mplp.vs_ref = 0;
1598 if (mplp.vs_ref > 1) mplp.vs_ref = 1;
1599 break;
1600 case 15: {
1601 char *tmp;
1602 mplp.indel_win_size = strtol(optarg,&tmp,10);
1603 if ( *tmp ) error("Could not parse argument: --indel-size %s\n", optarg);
1604 if ( mplp.indel_win_size < 20 )
1605 {
1606 mplp.indel_win_size = 20;
1607 fprintf(bcftools_stderr,"Warning: running with --indel-size %d, the requested value is too small\n",mplp.indel_win_size);
1608 }
1609 }
1610 break;
1611 case 20: mplp.indels_v20 = 1; mplp.edlib = 0; break;
1612 case 'W':
1613 if (!(mplp.write_index = write_index_parse(optarg)))
1614 error("Unsupported index format '%s'\n", optarg);
1615 break;
1616 case 22: mplp.edlib = 1; mplp.indels_v20 = 0; break;
1617 case 25: mplp.edlib = 0; break;
1618 case 28:
1619 mplp.seqQ_offset = atoi(optarg);
1620 if (mplp.seqQ_offset < 100)
1621 mplp.seqQ_offset = 100;
1622 if (mplp.seqQ_offset > 200)
1623 mplp.seqQ_offset = 200;
1624 break;
1625 case 23: mplp.del_bias = atof(optarg); break;
1626 case 24: mplp.poly_mqual = 1; break;
1627 case 26: mplp.poly_mqual = 0; break;
1628 case 'A': use_orphan = 1; break;
1629 case 'F': mplp.min_frac = atof(optarg); break;
1630 case 'm': mplp.min_support = atoi(optarg); break;
1631 case 'L': mplp.max_indel_depth = atoi(optarg); break;
1632 case 'G': bam_smpl_add_readgroups(mplp.bsmpl, optarg, 1); break;
1633 case 'a':
1634 if (optarg[0]=='?') {
1635 list_annotations(bcftools_stderr);
1636 goto err;
1637 }
1638 parse_format_flag(&mplp.fmt_flag,optarg);
1639 break;
1640 case 'M': mplp.max_read_len = atoi(optarg); break;
1641 case 'X':
1642 if (strcasecmp(optarg, "pacbio-ccs-1.18") == 0) {
1643 mplp.min_frac = 0.1;
1644 mplp.min_baseQ = 5;
1645 mplp.max_baseQ = 50;
1646 mplp.delta_baseQ = 10;
1647 mplp.openQ = 25;
1648 mplp.extQ = 1;
1649 mplp.flag |= MPLP_REALN_PARTIAL;
1650 mplp.max_read_len = 99999;
1651
1652 } else if (strcasecmp(optarg, "pacbio-ccs") == 0 ||
1653 strcasecmp(optarg, "pacbio-ccs-1.20") == 0) {
1654 mplp.min_frac = 0.1;
1655 mplp.min_baseQ = 5;
1656 mplp.max_baseQ = 50;
1657 mplp.delta_baseQ = 10;
1658 mplp.tandemQ = 300;
1659 mplp.openQ = 25;
1660 mplp.extQ = 1;
1661 mplp.flag &= ~MPLP_REALN;
1662 mplp.del_bias = 0.4;
1663 mplp.indel_bias = 1/.9;
1664 mplp.seqQ_offset = 118;
1665 mplp.poly_mqual = 1;
1666 mplp.edlib = 1;
1667 mplp.vs_ref = 0.7;
1668 mplp.indel_win_size = 80;
1669
1670 } else if (strcasecmp(optarg, "ont") == 0) {
1671 fprintf(bcftools_stderr, "With old ONT data may be beneficial to also run bcftools call with "
1672 "a higher -P, eg -P0.01 or -P 0.1\n");
1673 mplp.min_baseQ = 5;
1674 mplp.max_baseQ = 30;
1675 mplp.flag &= ~MPLP_REALN;
1676 mplp.flag |= MPLP_NO_INDEL;
1677
1678 } else if (strcasecmp(optarg, "ont-sup") == 0 ||
1679 strcasecmp(optarg, "ont-sup-1.20") == 0) {
1680 mplp.min_frac = 0.2;
1681 mplp.min_baseQ = 1;
1682 mplp.max_baseQ = 35;
1683 mplp.delta_baseQ = 99;
1684 mplp.openQ = 15;
1685 mplp.extQ = 1;
1686 mplp.flag &= ~MPLP_REALN;
1687 mplp.max_read_len = 9999999;
1688 mplp.del_bias = 0.4;
1689 mplp.poly_mqual = 1;
1690 mplp.edlib = 1;
1691 // If we increase -h then we can increase bias denominator too
1692 mplp.tandemQ = 110;
1693 mplp.indel_bias = 1/0.7;
1694 mplp.seqQ_offset = 130;
1695 mplp.indel_win_size = 80;
1696
1697 } else if (strcasecmp(optarg, "ultima") == 0 ||
1698 strcasecmp(optarg, "ultima-1.20") == 0) {
1699 mplp.min_frac = 0.15;
1700 mplp.min_baseQ = 1;
1701 mplp.max_baseQ = 30;
1702 mplp.delta_baseQ = 10;
1703 mplp.openQ = 20;
1704 mplp.extQ = 10;
1705 mplp.tandemQ = 250;
1706 mplp.flag &= ~MPLP_REALN;
1707 mplp.del_bias = 0.3;
1708 mplp.poly_mqual = 1;
1709 mplp.edlib = 1;
1710 mplp.indel_bias = 1/0.7;
1711 mplp.seqQ_offset = 140;
1712 mplp.vs_ref = 0.3;
1713 mplp.indel_win_size = 80;
1714
1715 } else if (strcasecmp(optarg, "1.12") == 0) {
1716 // 1.12 and earlier
1717 mplp.min_frac = 0.002;
1718 mplp.min_support = 1;
1719 mplp.min_baseQ = 13;
1720 mplp.tandemQ = 100;
1721 mplp.flag &= ~MPLP_REALN_PARTIAL;
1722 mplp.flag |= MPLP_REALN;
1723
1724 } else if (strcasecmp(optarg, "illumina-1.18") == 0) {
1725 mplp.indel_win_size = 110;
1726 mplp.flag |= MPLP_REALN_PARTIAL;
1727
1728 } else if (strcasecmp(optarg, "illumina") == 0 ||
1729 strcasecmp(optarg, "illumina-1.20") == 0) {
1730 mplp.edlib = 1;
1731 mplp.indel_win_size = 110;
1732 mplp.flag |= MPLP_REALN_PARTIAL;
1733 mplp.indel_bias = 1;
1734 mplp.seqQ_offset = 125;
1735 //mplp.indel_win_size = 80; TEST?
1736
1737 } else if (strcasecmp(optarg, "bgi") == 0 ||
1738 strcasecmp(optarg, "bgi-1.20") == 0) {
1739 mplp.min_frac = 0.1;
1740 mplp.edlib = 1;
1741 mplp.indel_bias = 1;
1742 mplp.seqQ_offset = 120;
1743 mplp.flag |= MPLP_REALN_PARTIAL;
1744 mplp.indel_win_size = 80;
1745
1746 } else if (strcasecmp(optarg, "list") == 0 ||
1747 strcasecmp(optarg, "help") == 0) {
1748 print_profiles();
1749 goto err;
1750 } else {
1751 fprintf(bcftools_stderr, "Unknown configuration name '%s'\n"
1752 "Please use '-X list' to show available choices.\n",
1753 optarg);
1754 goto err;
1755 }
1756 break;
1757 case 13: hts_srand48(atoi(optarg)); break;
1758 case 14:
1759 if ( !strcasecmp(optarg,"drop") ) mplp.ambig_reads = B2B_DROP;
1760 else if ( !strcasecmp(optarg,"incAD") ) mplp.ambig_reads = B2B_INC_AD;
1761 else if ( !strcasecmp(optarg,"incAD0") ) mplp.ambig_reads = B2B_INC_AD0;
1762 else error("The option to --ambig-reads not recognised: %s\n",optarg);
1763 break;
1764 default:
1765 fprintf(bcftools_stderr,"Invalid option: '%c'\n", c);
1766 goto err;
1767 }
1768 }
1769
1770 if ( mplp.gvcf && !(mplp.fmt_flag&B2B_FMT_DP) )
1771 {
1772 fprintf(bcftools_stderr,"[warning] The -a DP option is required with --gvcf, switching on.\n");
1773 mplp.fmt_flag |= B2B_FMT_DP;
1774 }
1775 if ( mplp.flag&(MPLP_BCF|MPLP_VCF|MPLP_NO_COMP) )
1776 {
1777 if ( mplp.flag&MPLP_VCF )
1778 {
1779 if ( mplp.flag&MPLP_NO_COMP ) mplp.output_type = FT_VCF;
1780 else mplp.output_type = FT_VCF_GZ;
1781 }
1782 else if ( mplp.flag&MPLP_BCF )
1783 {
1784 if ( mplp.flag&MPLP_NO_COMP ) mplp.output_type = FT_BCF;
1785 else mplp.output_type = FT_BCF_GZ;
1786 }
1787 }
1788 if ( !(mplp.flag&MPLP_REALN) && mplp.flag&MPLP_REDO_BAQ )
1789 {
1790 fprintf(bcftools_stderr,"Error: The -B option cannot be combined with -E\n");
1791 goto err;
1792 }
1793 if (use_orphan) mplp.flag &= ~MPLP_NO_ORPHAN;
1794 if (argc == 1)
1795 {
1796 print_usage(bcftools_stderr, &mplp);
1797 goto err;
1798 }
1799 if (!mplp.fai && !noref) {
1800 fprintf(bcftools_stderr,"Error: mpileup requires the --fasta-ref option by default; use --no-reference to run without a fasta reference\n");
1801 goto err;
1802 }
1803
1804 if (file_list)
1805 {
1806 if ( read_file_list(file_list,&nfiles,&fn) )
1807 goto err;
1808 mplp.files = fn;
1809 mplp.nfiles = nfiles;
1810 }
1811 else
1812 {
1813 mplp.nfiles = argc - optind;
1814 mplp.files = (char**) malloc(mplp.nfiles*sizeof(char*));
1815 for (i=0; i<mplp.nfiles; i++) mplp.files[i] = strdup(argv[optind+i]);
1816 }
1817 ret = mpileup(&mplp);
1818
1819 for (i=0; i<mplp.nfiles; i++) free(mplp.files[i]);
1820 free(mplp.files);
1821 free(mplp.reg_fname); free(mplp.pl_list);
1822 if (mplp.fai) fai_destroy(mplp.fai);
1823 if (mplp.bed) regidx_destroy(mplp.bed);
1824 if (mplp.bed_itr) regitr_destroy(mplp.bed_itr);
1825 if (mplp.reg) regidx_destroy(mplp.reg);
1826
1827 err:
1828 bam_smpl_destroy(mplp.bsmpl);
1829
1830 return ret;
1831 }