| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#include <config.h> |
| 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 <errno.h> |
| 37 |
#include <sys/stat.h> |
| 38 |
#include <getopt.h> |
| 39 |
#include <inttypes.h> |
| 40 |
#include <htslib/sam.h> |
| 41 |
#include <htslib/faidx.h> |
| 42 |
#include <htslib/kstring.h> |
| 43 |
#include <htslib/klist.h> |
| 44 |
#include <htslib/khash_str2int.h> |
| 45 |
#include <htslib/cram.h> |
| 46 |
#include "samtools.h" |
| 47 |
#include "bedidx.h" |
| 48 |
#include "sam_opts.h" |
| 49 |
#include "bam_plbuf.h" |
| 50 |
|
| 51 |
#define dummy_free(p) |
| 52 |
KLIST_INIT(auxlist, char *, dummy_free) |
| 53 |
|
| 54 |
int pileup_seq(kstring_t *ks_seq, const bam_pileup1_t *p, hts_pos_t pos, |
| 55 |
hts_pos_t ref_len, const char *ref, kstring_t *ks_mod, |
| 56 |
int rev_del, int no_ins, int no_ins_mods, |
| 57 |
int no_del, int no_ends) |
| 58 |
{ |
| 59 |
no_ins_mods |= no_ins; |
| 60 |
int j, err = 0; |
| 61 |
hts_base_mod_state *m = p->cd.p; |
| 62 |
if (!no_ends && p->is_head) { |
| 63 |
err |= kputc_('^', ks_seq) < 0; |
| 64 |
err |= kputc_(p->b->core.qual > 93 ? 126 : p->b->core.qual + 33, |
| 65 |
ks_seq) < 0; |
| 66 |
} |
| 67 |
if (!p->is_del) { |
| 68 |
|
| 69 |
const char seq_nt_str_lc[] = ",acmgrsvtwyhkdbn"; |
| 70 |
const char seq_nt_str_uc[] = ".ACMGRSVTWYHKDBN"; |
| 71 |
int c = p->qpos < p->b->core.l_qseq |
| 72 |
? bam_seqi(bam_get_seq(p->b), p->qpos) |
| 73 |
: 15 ; |
| 74 |
if (ref) { |
| 75 |
int rb = pos < ref_len |
| 76 |
? seq_nt16_table[(uint8_t)(ref[pos])] |
| 77 |
: 15; |
| 78 |
if (c == rb) |
| 79 |
c = 0; |
| 80 |
} |
| 81 |
c = bam_is_rev(p->b) |
| 82 |
? seq_nt_str_lc[c] |
| 83 |
: seq_nt_str_uc[c]; |
| 84 |
err |= kputc_(c, ks_seq) < 0; |
| 85 |
|
| 86 |
if (m) { |
| 87 |
int nm; |
| 88 |
hts_base_mod mod[256]; |
| 89 |
if ((nm = bam_mods_at_qpos(p->b, p->qpos, m, mod, 256)) > 0) { |
| 90 |
err |= kputc_('[', ks_seq) < 0; |
| 91 |
int j; |
| 92 |
for (j = 0; j < nm && j < 256; j++) { |
| 93 |
char qual[20]; |
| 94 |
if (mod[j].qual >= 0) |
| 95 |
sprintf(qual, "%d", mod[j].qual); |
| 96 |
else |
| 97 |
*qual = 0; |
| 98 |
if (mod[j].modified_base < 0) |
| 99 |
|
| 100 |
err |= ksprintf(ks_seq, "%c(%d)%s", |
| 101 |
"+-"[mod[j].strand], |
| 102 |
-mod[j].modified_base, qual) < 0; |
| 103 |
else |
| 104 |
err |= ksprintf(ks_seq, "%c%c%s", "+-"[mod[j].strand], |
| 105 |
mod[j].modified_base, qual) < 0; |
| 106 |
} |
| 107 |
err |= kputc_(']', ks_seq) < 0; |
| 108 |
} |
| 109 |
} |
| 110 |
} else { |
| 111 |
err |= kputc_(p->is_refskip |
| 112 |
? (bam_is_rev(p->b)? '<' : '>') |
| 113 |
: ((bam_is_rev(p->b) && rev_del) ? '#' : '*'), |
| 114 |
ks_seq) < 0; |
| 115 |
} |
| 116 |
|
| 117 |
int del_len = -p->indel; |
| 118 |
if (p->indel > 0) { |
| 119 |
int len = bam_plp_insertion_mod(p, m && !no_ins_mods ? m : NULL, |
| 120 |
ks_mod, &del_len); |
| 121 |
if (len < 0) { |
| 122 |
print_error("mpileup", "bam_plp_insertion() failed"); |
| 123 |
return -1; |
| 124 |
} |
| 125 |
if (no_ins < 2) { |
| 126 |
err |= kputc_('+', ks_seq) < 0; |
| 127 |
err |= kputuw(len, ks_seq) < 0; |
| 128 |
} |
| 129 |
if (!no_ins) { |
| 130 |
kstring_t *ks = ks_mod; |
| 131 |
if (bam_is_rev(p->b)) { |
| 132 |
char pad = rev_del ? '#' : '*'; |
| 133 |
int in_mod = 0; |
| 134 |
for (j = 0; j < ks->l; j++) { |
| 135 |
if (ks->s[j] == '[') in_mod = 1; |
| 136 |
else if (ks->s[j] == ']') in_mod = 0; |
| 137 |
err |= kputc_(ks->s[j] != '*' |
| 138 |
? (in_mod ? ks->s[j] : tolower(ks->s[j])) |
| 139 |
: pad, ks_seq) < 0; |
| 140 |
} |
| 141 |
} else { |
| 142 |
int in_mod = 0; |
| 143 |
for (j = 0; j < ks->l; j++) { |
| 144 |
if (ks->s[j] == '[') in_mod = 1; |
| 145 |
if (ks->s[j] == ']') in_mod = 0; |
| 146 |
err |= kputc_(in_mod ? ks->s[j] : toupper(ks->s[j]), |
| 147 |
ks_seq) < 0; |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
if (del_len > 0) { |
| 154 |
if (no_del < 2) |
| 155 |
err |= kputw(-del_len, ks_seq) < 0; |
| 156 |
if (!no_del) { |
| 157 |
for (j = 1; j <= del_len; ++j) { |
| 158 |
int c = (ref && (int)pos+j < ref_len)? ref[pos+j] : 'N'; |
| 159 |
err |= kputc_(bam_is_rev(p->b)? tolower(c) : toupper(c), |
| 160 |
ks_seq) < 0; |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
if (!no_ends && p->is_tail) |
| 166 |
err |= kputc_('$', ks_seq) < 0; |
| 167 |
|
| 168 |
return -err; |
| 169 |
} |
| 170 |
|
| 171 |
#include "sample.h" |
| 172 |
|
| 173 |
#define MPLP_NO_COMP (1<<2) |
| 174 |
#define MPLP_NO_ORPHAN (1<<3) |
| 175 |
#define MPLP_REALN (1<<4) |
| 176 |
#define MPLP_NO_INDEL (1<<5) |
| 177 |
#define MPLP_REDO_BAQ (1<<6) |
| 178 |
#define MPLP_ILLUMINA13 (1<<7) |
| 179 |
#define MPLP_IGNORE_RG (1<<8) |
| 180 |
#define MPLP_SMART_OVERLAPS (1<<10) |
| 181 |
|
| 182 |
#define MPLP_PRINT_MAPQ_CHAR (1<<11) |
| 183 |
#define MPLP_PRINT_QPOS (1<<12) |
| 184 |
|
| 185 |
#define MPLP_PRINT_QNAME (1<<13) |
| 186 |
#define MPLP_PRINT_FLAG (1<<14) |
| 187 |
#define MPLP_PRINT_RNAME (1<<15) |
| 188 |
#define MPLP_PRINT_POS (1<<16) |
| 189 |
#define MPLP_PRINT_MAPQ (1<<17) |
| 190 |
#define MPLP_PRINT_CIGAR (1<<18) |
| 191 |
#define MPLP_PRINT_RNEXT (1<<19) |
| 192 |
#define MPLP_PRINT_PNEXT (1<<20) |
| 193 |
#define MPLP_PRINT_TLEN (1<<21) |
| 194 |
#define MPLP_PRINT_SEQ (1<<22) |
| 195 |
#define MPLP_PRINT_QUAL (1<<23) |
| 196 |
#define MPLP_PRINT_RLEN (1<<24) |
| 197 |
|
| 198 |
#define MPLP_PRINT_MODS (1<<25) |
| 199 |
#define MPLP_PRINT_QPOS5 (1<<26) |
| 200 |
|
| 201 |
#define MPLP_PRINT_LAST (1<<27) // terminator for loop |
| 202 |
|
| 203 |
#define MPLP_MAX_DEPTH 8000 |
| 204 |
#define MPLP_MAX_INDEL_DEPTH 250 |
| 205 |
|
| 206 |
typedef struct { |
| 207 |
int min_mq, flag, min_baseQ, capQ_thres, max_depth, max_indel_depth, all, rev_del; |
| 208 |
int rflag_require, rflag_filter; |
| 209 |
char *reg, *pl_list, *fai_fname, *output_fname; |
| 210 |
faidx_t *fai; |
| 211 |
void *bed, *rghash, *auxlist; |
| 212 |
int argc; |
| 213 |
char **argv; |
| 214 |
char sep, empty, no_ins, no_ins_mods, no_del, no_ends; |
| 215 |
sam_global_args ga; |
| 216 |
} mplp_conf_t; |
| 217 |
|
| 218 |
typedef struct { |
| 219 |
char *ref[2]; |
| 220 |
int ref_id[2]; |
| 221 |
hts_pos_t ref_len[2]; |
| 222 |
} mplp_ref_t; |
| 223 |
|
| 224 |
#define MPLP_REF_INIT {{NULL,NULL},{-1,-1},{0,0}} |
| 225 |
|
| 226 |
typedef struct { |
| 227 |
samFile *fp; |
| 228 |
hts_itr_t *iter; |
| 229 |
sam_hdr_t *h; |
| 230 |
mplp_ref_t *ref; |
| 231 |
const mplp_conf_t *conf; |
| 232 |
} mplp_aux_t; |
| 233 |
|
| 234 |
typedef struct { |
| 235 |
int n; |
| 236 |
int *n_plp, *m_plp; |
| 237 |
bam_pileup1_t **plp; |
| 238 |
} mplp_pileup_t; |
| 239 |
|
| 240 |
static int build_auxlist(mplp_conf_t *conf, char *optstring) { |
| 241 |
if (!optstring) |
| 242 |
return 0; |
| 243 |
|
| 244 |
void *colhash = khash_str2int_init(); |
| 245 |
if (!colhash) |
| 246 |
return 1; |
| 247 |
|
| 248 |
struct active_cols { |
| 249 |
char *name; |
| 250 |
int supported; |
| 251 |
}; |
| 252 |
|
| 253 |
const struct active_cols colnames[12] = { |
| 254 |
{"QNAME", 1}, {"FLAG", 1}, {"RNAME", 1}, {"POS", 1}, {"MAPQ", 1}, |
| 255 |
{"CIGAR", 0}, {"RNEXT", 1}, {"PNEXT", 1}, {"TLEN", 0}, {"SEQ", 0}, |
| 256 |
{"QUAL", 0}, {"RLEN", 1}, |
| 257 |
}; |
| 258 |
|
| 259 |
int i, f = MPLP_PRINT_QNAME, colno = sizeof(colnames)/sizeof(*colnames); |
| 260 |
for (i = 0; i < colno; i++, f <<= 1) |
| 261 |
if (colnames[i].supported) |
| 262 |
khash_str2int_set(colhash, colnames[i].name, f); |
| 263 |
|
| 264 |
conf->auxlist = kl_init(auxlist); |
| 265 |
if (!conf->auxlist) |
| 266 |
return 1; |
| 267 |
|
| 268 |
char *save_p; |
| 269 |
char *tag = strtok_r(optstring, ",", &save_p); |
| 270 |
while (tag) { |
| 271 |
if (khash_str2int_get(colhash, tag, &f) == 0) { |
| 272 |
conf->flag |= f; |
| 273 |
} else { |
| 274 |
if (strlen(tag) != 2) { |
| 275 |
fprintf(stderr, "[%s] tag '%s' has more than two characters or not supported\n", __func__, tag); |
| 276 |
} else { |
| 277 |
char **tag_p = kl_pushp(auxlist, conf->auxlist); |
| 278 |
*tag_p = tag; |
| 279 |
} |
| 280 |
} |
| 281 |
tag = strtok_r(NULL, ",", &save_p); |
| 282 |
} |
| 283 |
|
| 284 |
khash_str2int_destroy(colhash); |
| 285 |
|
| 286 |
return 0; |
| 287 |
} |
| 288 |
|
| 289 |
static int mplp_get_ref(mplp_aux_t *ma, int tid, char **ref, hts_pos_t *ref_len) { |
| 290 |
mplp_ref_t *r = ma->ref; |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
if (!r || !ma->conf->fai) { |
| 295 |
*ref = NULL; |
| 296 |
return 0; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
if (tid == r->ref_id[0]) { |
| 303 |
*ref = r->ref[0]; |
| 304 |
*ref_len = r->ref_len[0]; |
| 305 |
return 1; |
| 306 |
} |
| 307 |
if (tid == r->ref_id[1]) { |
| 308 |
|
| 309 |
int tmp_id; |
| 310 |
hts_pos_t tmp_len; |
| 311 |
tmp_id = r->ref_id[0]; r->ref_id[0] = r->ref_id[1]; r->ref_id[1] = tmp_id; |
| 312 |
tmp_len = r->ref_len[0]; r->ref_len[0] = r->ref_len[1]; r->ref_len[1] = tmp_len; |
| 313 |
|
| 314 |
char *tc; |
| 315 |
tc = r->ref[0]; r->ref[0] = r->ref[1]; r->ref[1] = tc; |
| 316 |
*ref = r->ref[0]; |
| 317 |
*ref_len = r->ref_len[0]; |
| 318 |
return 1; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
free(r->ref[1]); |
| 323 |
r->ref[1] = r->ref[0]; |
| 324 |
r->ref_id[1] = r->ref_id[0]; |
| 325 |
r->ref_len[1] = r->ref_len[0]; |
| 326 |
|
| 327 |
r->ref_id[0] = tid; |
| 328 |
r->ref[0] = faidx_fetch_seq64(ma->conf->fai, |
| 329 |
sam_hdr_tid2name(ma->h, r->ref_id[0]), |
| 330 |
0, |
| 331 |
HTS_POS_MAX, |
| 332 |
&r->ref_len[0]); |
| 333 |
|
| 334 |
if (!r->ref[0]) { |
| 335 |
r->ref[0] = NULL; |
| 336 |
r->ref_id[0] = -1; |
| 337 |
r->ref_len[0] = 0; |
| 338 |
*ref = NULL; |
| 339 |
return 0; |
| 340 |
} |
| 341 |
|
| 342 |
*ref = r->ref[0]; |
| 343 |
*ref_len = r->ref_len[0]; |
| 344 |
return 1; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
static |
| 350 |
int pileup_cd_create(void *data, const bam1_t *b, bam_pileup_cd *cd) { |
| 351 |
int ret; |
| 352 |
hts_base_mod_state *m = hts_base_mod_state_alloc(); |
| 353 |
ret = bam_parse_basemod(b, m); |
| 354 |
cd->p = m; |
| 355 |
return ret; |
| 356 |
} |
| 357 |
|
| 358 |
static |
| 359 |
int pileup_cd_destroy(void *data, const bam1_t *b, bam_pileup_cd *cd) { |
| 360 |
hts_base_mod_state_free(cd->p); |
| 361 |
return 0; |
| 362 |
} |
| 363 |
|
| 364 |
static void |
| 365 |
print_empty_pileup(FILE *fp, const mplp_conf_t *conf, const char *tname, |
| 366 |
hts_pos_t pos, int n, const char *ref, hts_pos_t ref_len) |
| 367 |
{ |
| 368 |
int i; |
| 369 |
fprintf(fp, "%s\t%"PRIhts_pos"\t%c", tname, pos+1, (ref && pos < ref_len)? ref[pos] : 'N'); |
| 370 |
for (i = 0; i < n; ++i) { |
| 371 |
fputs("\t0\t*\t*", fp); |
| 372 |
int flag_value = MPLP_PRINT_MAPQ_CHAR; |
| 373 |
while(flag_value < MPLP_PRINT_LAST) { |
| 374 |
if (flag_value != MPLP_PRINT_MODS && (conf->flag & flag_value)) |
| 375 |
fputs("\t*", fp); |
| 376 |
flag_value <<= 1; |
| 377 |
} |
| 378 |
if (conf->auxlist) { |
| 379 |
int t = 0; |
| 380 |
while(t++ < ((klist_t(auxlist) *)conf->auxlist)->size) |
| 381 |
fputs("\t*", fp); |
| 382 |
} |
| 383 |
} |
| 384 |
putc('\n', fp); |
| 385 |
} |
| 386 |
|
| 387 |
static int mplp_func(void *data, bam1_t *b) |
| 388 |
{ |
| 389 |
char *ref; |
| 390 |
mplp_aux_t *ma = (mplp_aux_t*)data; |
| 391 |
int ret, skip = 0; |
| 392 |
hts_pos_t ref_len; |
| 393 |
|
| 394 |
do { |
| 395 |
int has_ref; |
| 396 |
ret = ma->iter? sam_itr_next(ma->fp, ma->iter, b) : sam_read1(ma->fp, ma->h, b); |
| 397 |
if (ret < 0) break; |
| 398 |
|
| 399 |
|
| 400 |
if (b->core.tid < 0 || (b->core.flag&BAM_FUNMAP)) { |
| 401 |
skip = 1; |
| 402 |
continue; |
| 403 |
} |
| 404 |
if (ma->conf->rflag_require && !(ma->conf->rflag_require&b->core.flag)) { skip = 1; continue; } |
| 405 |
if (ma->conf->rflag_filter && ma->conf->rflag_filter&b->core.flag) { skip = 1; continue; } |
| 406 |
if (ma->conf->bed && ma->conf->all == 0) { |
| 407 |
skip = !bed_overlap(ma->conf->bed, sam_hdr_tid2name(ma->h, b->core.tid), b->core.pos, bam_endpos(b)); |
| 408 |
if (skip) continue; |
| 409 |
} |
| 410 |
if (ma->conf->rghash) { |
| 411 |
uint8_t *rg = bam_aux_get(b, "RG"); |
| 412 |
skip = (rg && khash_str2int_get(ma->conf->rghash, (const char*)(rg+1), NULL)==0); |
| 413 |
if (skip) continue; |
| 414 |
} |
| 415 |
if (ma->conf->flag & MPLP_ILLUMINA13) { |
| 416 |
int i; |
| 417 |
uint8_t *qual = bam_get_qual(b); |
| 418 |
for (i = 0; i < b->core.l_qseq; ++i) |
| 419 |
qual[i] = qual[i] > 31? qual[i] - 31 : 0; |
| 420 |
} |
| 421 |
|
| 422 |
if (ma->conf->fai && b->core.tid >= 0) { |
| 423 |
has_ref = mplp_get_ref(ma, b->core.tid, &ref, &ref_len); |
| 424 |
if (has_ref && ref_len <= b->core.pos) { |
| 425 |
fprintf(stderr,"[%s] Skipping because %"PRIhts_pos" is outside of %"PRIhts_pos" [ref:%d]\n", |
| 426 |
__func__, (int64_t) b->core.pos, ref_len, b->core.tid); |
| 427 |
skip = 1; |
| 428 |
continue; |
| 429 |
} |
| 430 |
} else { |
| 431 |
has_ref = 0; |
| 432 |
} |
| 433 |
|
| 434 |
skip = 0; |
| 435 |
if (has_ref && (ma->conf->flag&MPLP_REALN)) sam_prob_realn(b, ref, ref_len, (ma->conf->flag & MPLP_REDO_BAQ)? 7 : 3); |
| 436 |
if (has_ref && ma->conf->capQ_thres > 10) { |
| 437 |
int q = sam_cap_mapq(b, ref, ref_len, ma->conf->capQ_thres); |
| 438 |
if (q < 0) skip = 1; |
| 439 |
else if (b->core.qual > q) b->core.qual = q; |
| 440 |
} |
| 441 |
if (b->core.qual < ma->conf->min_mq) skip = 1; |
| 442 |
else if ((ma->conf->flag&MPLP_NO_ORPHAN) && (b->core.flag&BAM_FPAIRED) && !(b->core.flag&BAM_FPROPER_PAIR)) skip = 1; |
| 443 |
} while (skip); |
| 444 |
return ret; |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
static int mpileup(mplp_conf_t *conf, int nfn, char **fn, char **fn_idx) |
| 455 |
{ |
| 456 |
mplp_aux_t **data; |
| 457 |
int i, tid, *n_plp, tid0 = 0, max_depth; |
| 458 |
hts_pos_t pos, beg0 = 0, end0 = HTS_POS_MAX, ref_len; |
| 459 |
const bam_pileup1_t **plp; |
| 460 |
mplp_ref_t mp_ref = MPLP_REF_INIT; |
| 461 |
bam_mplp_t iter; |
| 462 |
sam_hdr_t *h = NULL; |
| 463 |
char *ref; |
| 464 |
FILE *pileup_fp = NULL; |
| 465 |
|
| 466 |
bam_sample_t *sm = NULL; |
| 467 |
kstring_t buf; |
| 468 |
mplp_pileup_t gplp; |
| 469 |
|
| 470 |
memset(&gplp, 0, sizeof(mplp_pileup_t)); |
| 471 |
memset(&buf, 0, sizeof(kstring_t)); |
| 472 |
data = calloc(nfn, sizeof(mplp_aux_t*)); |
| 473 |
plp = calloc(nfn, sizeof(bam_pileup1_t*)); |
| 474 |
n_plp = calloc(nfn, sizeof(int)); |
| 475 |
sm = bam_smpl_init(); |
| 476 |
|
| 477 |
if (nfn == 0) { |
| 478 |
fprintf(stderr,"[%s] no input file/data given\n", __func__); |
| 479 |
exit(EXIT_FAILURE); |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
refs_t *refs = NULL; |
| 484 |
for (i = 0; i < nfn; ++i) { |
| 485 |
sam_hdr_t *h_tmp; |
| 486 |
data[i] = calloc(1, sizeof(mplp_aux_t)); |
| 487 |
data[i]->fp = sam_open_format(fn[i], "rb", &conf->ga.in); |
| 488 |
if ( !data[i]->fp ) |
| 489 |
{ |
| 490 |
fprintf(stderr, "[%s] failed to open %s: %s\n", __func__, fn[i], strerror(errno)); |
| 491 |
exit(EXIT_FAILURE); |
| 492 |
} |
| 493 |
if (hts_set_opt(data[i]->fp, CRAM_OPT_DECODE_MD, 0)) { |
| 494 |
fprintf(stderr, "Failed to set CRAM_OPT_DECODE_MD value\n"); |
| 495 |
exit(EXIT_FAILURE); |
| 496 |
} |
| 497 |
|
| 498 |
if (!refs && conf->fai_fname) { |
| 499 |
if (hts_set_fai_filename(data[i]->fp, conf->fai_fname) != 0) { |
| 500 |
fprintf(stderr, "[%s] failed to process %s: %s\n", |
| 501 |
__func__, conf->fai_fname, strerror(errno)); |
| 502 |
exit(EXIT_FAILURE); |
| 503 |
} |
| 504 |
refs = cram_get_refs(data[i]->fp); |
| 505 |
} else if (conf->fai_fname) { |
| 506 |
if (hts_set_opt(data[i]->fp, CRAM_OPT_SHARED_REF, refs) != 0) { |
| 507 |
fprintf(stderr, "[%s] failed to process %s: %s\n", |
| 508 |
__func__, conf->fai_fname, strerror(errno)); |
| 509 |
exit(EXIT_FAILURE); |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
data[i]->conf = conf; |
| 514 |
data[i]->ref = &mp_ref; |
| 515 |
h_tmp = sam_hdr_read(data[i]->fp); |
| 516 |
if ( !h_tmp ) { |
| 517 |
fprintf(stderr,"[%s] fail to read the header of %s\n", __func__, fn[i]); |
| 518 |
exit(EXIT_FAILURE); |
| 519 |
} |
| 520 |
bam_smpl_add(sm, fn[i], (conf->flag&MPLP_IGNORE_RG)? 0 : sam_hdr_str(h_tmp)); |
| 521 |
if (conf->reg) { |
| 522 |
hts_idx_t *idx = NULL; |
| 523 |
|
| 524 |
if (fn_idx != NULL) { |
| 525 |
idx = sam_index_load2(data[i]->fp, fn[i], fn_idx[i]); |
| 526 |
} else { |
| 527 |
idx = sam_index_load(data[i]->fp, fn[i]); |
| 528 |
} |
| 529 |
|
| 530 |
if (idx == NULL) { |
| 531 |
fprintf(stderr, "[%s] fail to load index for %s\n", __func__, fn[i]); |
| 532 |
exit(EXIT_FAILURE); |
| 533 |
} |
| 534 |
if ( (data[i]->iter=sam_itr_querys(idx, h_tmp, conf->reg)) == 0) { |
| 535 |
fprintf(stderr, "[E::%s] fail to parse region '%s' with %s\n", __func__, conf->reg, fn[i]); |
| 536 |
exit(EXIT_FAILURE); |
| 537 |
} |
| 538 |
if (i == 0) beg0 = data[i]->iter->beg, end0 = data[i]->iter->end, tid0 = data[i]->iter->tid; |
| 539 |
hts_idx_destroy(idx); |
| 540 |
} |
| 541 |
else |
| 542 |
data[i]->iter = NULL; |
| 543 |
|
| 544 |
if (i == 0) h = data[i]->h = h_tmp; |
| 545 |
else { |
| 546 |
|
| 547 |
sam_hdr_destroy(h_tmp); |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
data[i]->h = h; |
| 552 |
} |
| 553 |
} |
| 554 |
fprintf(stderr, "[%s] %d samples in %d input files\n", |
| 555 |
__func__, sm->n, nfn); |
| 556 |
|
| 557 |
pileup_fp = conf->output_fname? fopen(conf->output_fname, "w") : stdout; |
| 558 |
|
| 559 |
if (pileup_fp == NULL) { |
| 560 |
fprintf(stderr, "[%s] failed to write to %s: %s\n", __func__, conf->output_fname, strerror(errno)); |
| 561 |
exit(EXIT_FAILURE); |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
iter = bam_mplp_init(nfn, mplp_func, (void**)data); |
| 566 |
if (conf->flag & MPLP_PRINT_MODS) { |
| 567 |
bam_mplp_constructor(iter, pileup_cd_create); |
| 568 |
bam_mplp_destructor(iter, pileup_cd_destroy); |
| 569 |
} |
| 570 |
if ( conf->flag & MPLP_SMART_OVERLAPS ) bam_mplp_init_overlaps(iter); |
| 571 |
if ( !conf->max_depth ) { |
| 572 |
max_depth = INT_MAX; |
| 573 |
fprintf(stderr, "[%s] Max depth set to maximum value (%d)\n", __func__, INT_MAX); |
| 574 |
} else { |
| 575 |
max_depth = conf->max_depth; |
| 576 |
if ( max_depth * nfn > 1<<20 ) |
| 577 |
fprintf(stderr, "[%s] Combined max depth is above 1M. Potential memory hog!\n", __func__); |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
bam_mplp_set_maxcnt(iter, max_depth); |
| 582 |
int ret; |
| 583 |
int last_tid = -1; |
| 584 |
hts_pos_t last_pos = -1; |
| 585 |
int one_seq = 0; |
| 586 |
|
| 587 |
|
| 588 |
kstring_t ks_seq = KS_INITIALIZE; |
| 589 |
kstring_t ks_mod = KS_INITIALIZE; |
| 590 |
kstring_t ks_qual = KS_INITIALIZE; |
| 591 |
while ( (ret=bam_mplp64_auto(iter, &tid, &pos, n_plp, plp)) > 0) { |
| 592 |
one_seq = 1; |
| 593 |
if (conf->reg && (pos < beg0 || pos >= end0)) continue; |
| 594 |
if (conf->all) { |
| 595 |
|
| 596 |
while (tid > last_tid) { |
| 597 |
if (last_tid >= 0 && !conf->reg) { |
| 598 |
while (++last_pos < sam_hdr_tid2len(h, last_tid)) { |
| 599 |
if (conf->bed && bed_overlap(conf->bed, sam_hdr_tid2name(h, last_tid), last_pos, last_pos + 1) == 0) |
| 600 |
continue; |
| 601 |
print_empty_pileup(pileup_fp, conf, sam_hdr_tid2name(h, last_tid), last_pos, nfn, ref, ref_len); |
| 602 |
} |
| 603 |
} |
| 604 |
last_tid++; |
| 605 |
last_pos = -1; |
| 606 |
if (conf->all < 2) |
| 607 |
break; |
| 608 |
if (tid > last_tid) |
| 609 |
|
| 610 |
mplp_get_ref(data[0], last_tid, &ref, &ref_len); |
| 611 |
} |
| 612 |
} |
| 613 |
mplp_get_ref(data[0], tid, &ref, &ref_len); |
| 614 |
|
| 615 |
if (conf->all) { |
| 616 |
|
| 617 |
while (++last_pos < pos) { |
| 618 |
if (conf->reg && last_pos < beg0) continue; |
| 619 |
if (conf->bed && bed_overlap(conf->bed, sam_hdr_tid2name(h, tid), last_pos, last_pos + 1) == 0) |
| 620 |
continue; |
| 621 |
print_empty_pileup(pileup_fp, conf, sam_hdr_tid2name(h, tid), last_pos, nfn, ref, ref_len); |
| 622 |
} |
| 623 |
last_tid = tid; |
| 624 |
last_pos = pos; |
| 625 |
} |
| 626 |
if (conf->bed && tid >= 0 && !bed_overlap(conf->bed, sam_hdr_tid2name(h, tid), pos, pos+1)) continue; |
| 627 |
|
| 628 |
fprintf(pileup_fp, "%s\t%"PRIhts_pos"\t%c", sam_hdr_tid2name(h, tid), pos + 1, (ref && pos < ref_len)? ref[pos] : 'N'); |
| 629 |
for (i = 0; i < nfn; ++i) { |
| 630 |
int j, cnt, err = 0; |
| 631 |
ks_clear(&ks_seq); |
| 632 |
ks_clear(&ks_qual); |
| 633 |
ks_clear(&ks_mod); |
| 634 |
for (j = cnt = 0; j < n_plp[i]; ++j) { |
| 635 |
const bam_pileup1_t *p = plp[i] + j; |
| 636 |
int c = p->qpos < p->b->core.l_qseq |
| 637 |
? bam_get_qual(p->b)[p->qpos] |
| 638 |
: 0; |
| 639 |
if (c >= conf->min_baseQ) { |
| 640 |
|
| 641 |
err |= pileup_seq(&ks_seq, plp[i] + j, pos, ref_len, |
| 642 |
ref, &ks_mod, conf->rev_del, |
| 643 |
conf->no_ins, conf->no_ins_mods, |
| 644 |
conf->no_del, conf->no_ends) < 0; |
| 645 |
|
| 646 |
|
| 647 |
err |= kputc_(c+33 < 126 ? c+33 : 126, &ks_qual) < 0; |
| 648 |
cnt++; |
| 649 |
} |
| 650 |
} |
| 651 |
if (err) { |
| 652 |
ret = 1; |
| 653 |
goto fail; |
| 654 |
} |
| 655 |
fprintf(pileup_fp, "\t%d\t", cnt); |
| 656 |
|
| 657 |
if (n_plp[i] == 0) { |
| 658 |
fputs("*\t*", pileup_fp); |
| 659 |
int flag_value = MPLP_PRINT_MAPQ_CHAR; |
| 660 |
while(flag_value < MPLP_PRINT_LAST) { |
| 661 |
if (flag_value != MPLP_PRINT_MODS |
| 662 |
&& (conf->flag & flag_value)) |
| 663 |
fputs("\t*", pileup_fp); |
| 664 |
flag_value <<= 1; |
| 665 |
} |
| 666 |
if (conf->auxlist) { |
| 667 |
int t = 0; |
| 668 |
while(t++ < ((klist_t(auxlist) *)conf->auxlist)->size) |
| 669 |
fputs("\t*", pileup_fp); |
| 670 |
} |
| 671 |
} else { |
| 672 |
if (ks_seq.l) { |
| 673 |
fwrite(ks_seq.s, 1, ks_seq.l, pileup_fp); |
| 674 |
} else { |
| 675 |
putc('*', pileup_fp); |
| 676 |
} |
| 677 |
putc('\t', pileup_fp); |
| 678 |
|
| 679 |
if (ks_qual.l) { |
| 680 |
fwrite(ks_qual.s, 1, ks_qual.l, pileup_fp); |
| 681 |
} else { |
| 682 |
putc('*', pileup_fp); |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
int flag_value = MPLP_PRINT_MAPQ_CHAR; |
| 687 |
while(flag_value < MPLP_PRINT_LAST) { |
| 688 |
if (flag_value != MPLP_PRINT_MODS |
| 689 |
&& (conf->flag & flag_value)) { |
| 690 |
int n = 0; |
| 691 |
putc('\t', pileup_fp); |
| 692 |
for (j = 0; j < n_plp[i]; ++j) { |
| 693 |
const bam_pileup1_t *p = &plp[i][j]; |
| 694 |
int c = p->qpos < p->b->core.l_qseq |
| 695 |
? bam_get_qual(p->b)[p->qpos] |
| 696 |
: 0; |
| 697 |
if ( c < conf->min_baseQ ) continue; |
| 698 |
if (n > 0 && flag_value != MPLP_PRINT_MAPQ_CHAR) putc(',', pileup_fp); |
| 699 |
n++; |
| 700 |
|
| 701 |
switch (flag_value) { |
| 702 |
case MPLP_PRINT_MAPQ_CHAR: |
| 703 |
c = p->b->core.qual + 33; |
| 704 |
if (c > 126) c = 126; |
| 705 |
putc(c, pileup_fp); |
| 706 |
break; |
| 707 |
case MPLP_PRINT_QPOS: |
| 708 |
|
| 709 |
fprintf(pileup_fp, "%d", p->qpos + 1); |
| 710 |
break; |
| 711 |
case MPLP_PRINT_QPOS5: { |
| 712 |
|
| 713 |
int pos5 = bam_is_rev(p->b) |
| 714 |
? p->b->core.l_qseq-p->qpos + p->is_del |
| 715 |
: p->qpos + 1; |
| 716 |
fprintf(pileup_fp, "%d", pos5); |
| 717 |
break; |
| 718 |
} |
| 719 |
case MPLP_PRINT_QNAME: |
| 720 |
fputs(bam_get_qname(p->b), pileup_fp); |
| 721 |
break; |
| 722 |
case MPLP_PRINT_FLAG: |
| 723 |
fprintf(pileup_fp, "%d", p->b->core.flag); |
| 724 |
break; |
| 725 |
case MPLP_PRINT_RNAME: |
| 726 |
if (p->b->core.tid >= 0) |
| 727 |
fputs(sam_hdr_tid2name(h, p->b->core.tid), pileup_fp); |
| 728 |
else |
| 729 |
putc('*', pileup_fp); |
| 730 |
break; |
| 731 |
case MPLP_PRINT_POS: |
| 732 |
fprintf(pileup_fp, "%"PRId64, (int64_t) p->b->core.pos + 1); |
| 733 |
break; |
| 734 |
case MPLP_PRINT_MAPQ: |
| 735 |
fprintf(pileup_fp, "%d", p->b->core.qual); |
| 736 |
break; |
| 737 |
case MPLP_PRINT_RNEXT: |
| 738 |
if (p->b->core.mtid >= 0) |
| 739 |
fputs(sam_hdr_tid2name(h, p->b->core.mtid), pileup_fp); |
| 740 |
else |
| 741 |
putc('*', pileup_fp); |
| 742 |
break; |
| 743 |
case MPLP_PRINT_PNEXT: |
| 744 |
fprintf(pileup_fp, "%"PRId64, (int64_t) p->b->core.mpos + 1); |
| 745 |
break; |
| 746 |
case MPLP_PRINT_RLEN: |
| 747 |
fprintf(pileup_fp, "%d", p->b->core.l_qseq); |
| 748 |
break; |
| 749 |
} |
| 750 |
} |
| 751 |
if (!n) putc('*', pileup_fp); |
| 752 |
} |
| 753 |
flag_value <<= 1; |
| 754 |
} |
| 755 |
|
| 756 |
|
| 757 |
klist_t(auxlist) *auxlist_p = ((klist_t(auxlist) *)conf->auxlist); |
| 758 |
if (auxlist_p && auxlist_p->size) { |
| 759 |
kliter_t(auxlist) *aux; |
| 760 |
for (aux = kl_begin(auxlist_p); aux != kl_end(auxlist_p); aux = kl_next(aux)) { |
| 761 |
int n = 0; |
| 762 |
putc('\t', pileup_fp); |
| 763 |
for (j = 0; j < n_plp[i]; ++j) { |
| 764 |
const bam_pileup1_t *p = &plp[i][j]; |
| 765 |
int c = p->qpos < p->b->core.l_qseq |
| 766 |
? bam_get_qual(p->b)[p->qpos] |
| 767 |
: 0; |
| 768 |
if ( c < conf->min_baseQ ) continue; |
| 769 |
|
| 770 |
if (n > 0) putc(conf->sep, pileup_fp); |
| 771 |
n++; |
| 772 |
uint8_t* tag_u = bam_aux_get(p->b, kl_val(aux)); |
| 773 |
if (!tag_u) { |
| 774 |
putc(conf->empty , pileup_fp); |
| 775 |
continue; |
| 776 |
} |
| 777 |
|
| 778 |
int tag_supported = 0; |
| 779 |
|
| 780 |
|
| 781 |
if (*tag_u == 'Z' || *tag_u == 'H') { |
| 782 |
char *tag_s = bam_aux2Z(tag_u); |
| 783 |
if (!tag_s) continue; |
| 784 |
fputs(tag_s, pileup_fp); |
| 785 |
tag_supported = 1; |
| 786 |
} |
| 787 |
|
| 788 |
|
| 789 |
if (*tag_u == 'I' || *tag_u == 'i' || *tag_u == 'C' || *tag_u == 'c' || *tag_u == 'S' || *tag_u == 's') { |
| 790 |
int64_t tag_i = bam_aux2i(tag_u); |
| 791 |
fprintf(pileup_fp, "%" PRId64 "", tag_i); |
| 792 |
tag_supported = 1; |
| 793 |
} |
| 794 |
|
| 795 |
|
| 796 |
if (*tag_u == 'd' || *tag_u == 'f') { |
| 797 |
double tag_f = bam_aux2f(tag_u); |
| 798 |
fprintf(pileup_fp, "%lf", tag_f); |
| 799 |
tag_supported = 1; |
| 800 |
} |
| 801 |
|
| 802 |
|
| 803 |
if (*tag_u == 'A') { |
| 804 |
char tag_c = bam_aux2A(tag_u); |
| 805 |
putc(tag_c, pileup_fp); |
| 806 |
tag_supported = 1; |
| 807 |
} |
| 808 |
|
| 809 |
if (!tag_supported) putc('*', pileup_fp); |
| 810 |
} |
| 811 |
if (!n) putc('*', pileup_fp); |
| 812 |
} |
| 813 |
} |
| 814 |
} |
| 815 |
} |
| 816 |
putc('\n', pileup_fp); |
| 817 |
} |
| 818 |
|
| 819 |
ks_free(&ks_seq); |
| 820 |
ks_free(&ks_mod); |
| 821 |
ks_free(&ks_qual); |
| 822 |
|
| 823 |
if (ret < 0) { |
| 824 |
print_error("mpileup", "error reading from input file"); |
| 825 |
ret = EXIT_FAILURE; |
| 826 |
goto fail; |
| 827 |
} |
| 828 |
|
| 829 |
if (conf->all) { |
| 830 |
|
| 831 |
if (last_tid < 0 && conf->reg && conf->all > 1) { |
| 832 |
last_tid = tid0; |
| 833 |
last_pos = beg0-1; |
| 834 |
mplp_get_ref(data[0], tid0, &ref, &ref_len); |
| 835 |
} else if (last_tid < 0 && !one_seq && conf->all > 1) { |
| 836 |
last_tid = 0; |
| 837 |
} |
| 838 |
while (last_tid >= 0 && last_tid < sam_hdr_nref(h)) { |
| 839 |
mplp_get_ref(data[0], last_tid, &ref, &ref_len); |
| 840 |
while (++last_pos < sam_hdr_tid2len(h, last_tid)) { |
| 841 |
if (last_pos >= end0) break; |
| 842 |
if (conf->bed && bed_overlap(conf->bed, sam_hdr_tid2name(h, last_tid), last_pos, last_pos + 1) == 0) |
| 843 |
continue; |
| 844 |
print_empty_pileup(pileup_fp, conf, sam_hdr_tid2name(h, last_tid), last_pos, nfn, ref, ref_len); |
| 845 |
} |
| 846 |
last_tid++; |
| 847 |
last_pos = -1; |
| 848 |
if (conf->all < 2 || conf->reg) |
| 849 |
break; |
| 850 |
} |
| 851 |
} |
| 852 |
|
| 853 |
fail: |
| 854 |
|
| 855 |
if (pileup_fp && conf->output_fname) fclose(pileup_fp); |
| 856 |
bam_smpl_destroy(sm); free(buf.s); |
| 857 |
for (i = 0; i < gplp.n; ++i) free(gplp.plp[i]); |
| 858 |
free(gplp.plp); free(gplp.n_plp); free(gplp.m_plp); |
| 859 |
bam_mplp_destroy(iter); |
| 860 |
sam_hdr_destroy(h); |
| 861 |
for (i = 0; i < nfn; ++i) { |
| 862 |
sam_close(data[i]->fp); |
| 863 |
if (data[i]->iter) hts_itr_destroy(data[i]->iter); |
| 864 |
free(data[i]); |
| 865 |
} |
| 866 |
free(data); free(plp); free(n_plp); |
| 867 |
free(mp_ref.ref[0]); |
| 868 |
free(mp_ref.ref[1]); |
| 869 |
return ret; |
| 870 |
} |
| 871 |
|
| 872 |
static int is_url(const char *s) |
| 873 |
{ |
| 874 |
static const char uri_scheme_chars[] = |
| 875 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+.-"; |
| 876 |
return s[strspn(s, uri_scheme_chars)] == ':'; |
| 877 |
} |
| 878 |
|
| 879 |
#define MAX_PATH_LEN 1024 |
| 880 |
int read_file_list(const char *file_list,int *n,char **argv[]) |
| 881 |
{ |
| 882 |
char buf[MAX_PATH_LEN]; |
| 883 |
int len, nfiles = 0; |
| 884 |
char **files = NULL; |
| 885 |
struct stat sb; |
| 886 |
|
| 887 |
*n = 0; |
| 888 |
*argv = NULL; |
| 889 |
|
| 890 |
FILE *fh = fopen(file_list,"r"); |
| 891 |
if ( !fh ) |
| 892 |
{ |
| 893 |
fprintf(stderr,"%s: %s\n", file_list,strerror(errno)); |
| 894 |
return 1; |
| 895 |
} |
| 896 |
|
| 897 |
files = calloc(nfiles,sizeof(char*)); |
| 898 |
nfiles = 0; |
| 899 |
while ( fgets(buf,MAX_PATH_LEN,fh) ) |
| 900 |
{ |
| 901 |
|
| 902 |
len = strlen(buf); |
| 903 |
while ( len>0 && isspace(buf[len-1]) ) len--; |
| 904 |
if ( !len ) continue; |
| 905 |
|
| 906 |
|
| 907 |
buf[len] = 0; |
| 908 |
if (! (is_url(buf) || stat(buf, &sb) == 0)) |
| 909 |
{ |
| 910 |
|
| 911 |
int i, safe_to_print = 1; |
| 912 |
for (i=0; i<len; i++) |
| 913 |
if (!isprint(buf[i])) { safe_to_print = 0; break; } |
| 914 |
if ( safe_to_print ) |
| 915 |
fprintf(stderr,"The file list \"%s\" appears broken, could not locate: %s\n", file_list,buf); |
| 916 |
else |
| 917 |
fprintf(stderr,"Does the file \"%s\" really contain a list of files and do all exist?\n", file_list); |
| 918 |
return 1; |
| 919 |
} |
| 920 |
|
| 921 |
nfiles++; |
| 922 |
files = realloc(files,nfiles*sizeof(char*)); |
| 923 |
files[nfiles-1] = strdup(buf); |
| 924 |
} |
| 925 |
fclose(fh); |
| 926 |
if ( !nfiles ) |
| 927 |
{ |
| 928 |
fprintf(stderr,"No files read from %s\n", file_list); |
| 929 |
return 1; |
| 930 |
} |
| 931 |
*argv = files; |
| 932 |
*n = nfiles; |
| 933 |
return 0; |
| 934 |
} |
| 935 |
#undef MAX_PATH_LEN |
| 936 |
|
| 937 |
static void print_usage(FILE *fp, const mplp_conf_t *mplp) |
| 938 |
{ |
| 939 |
char *tmp_require = bam_flag2str(mplp->rflag_require); |
| 940 |
char *tmp_filter = bam_flag2str(mplp->rflag_filter); |
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
fprintf(fp, |
| 947 |
"\n" |
| 948 |
"Usage: samtools mpileup [options] in1.bam [in2.bam [...]]\n" |
| 949 |
"\n" |
| 950 |
"Input options:\n" |
| 951 |
" -6, --illumina1.3+ quality is in the Illumina-1.3+ encoding\n" |
| 952 |
" -A, --count-orphans do not discard anomalous read pairs\n" |
| 953 |
" -b, --bam-list FILE list of input BAM filenames, one per line\n" |
| 954 |
" -B, --no-BAQ disable BAQ (per-Base Alignment Quality)\n" |
| 955 |
" -C, --adjust-MQ INT adjust mapping quality; recommended:50, disable:0 [0]\n" |
| 956 |
" -d, --max-depth INT max per-file depth; avoids excessive memory usage [%d]\n", mplp->max_depth); |
| 957 |
fprintf(fp, |
| 958 |
" -E, --redo-BAQ recalculate BAQ on the fly, ignore existing BQs\n" |
| 959 |
" -f, --fasta-ref FILE faidx indexed reference sequence file\n" |
| 960 |
" -G, --exclude-RG FILE exclude read groups listed in FILE\n" |
| 961 |
" -l, --positions FILE skip unlisted positions (chr pos) or regions (BED)\n" |
| 962 |
" -q, --min-MQ INT skip alignments with mapQ smaller than INT [%d]\n", mplp->min_mq); |
| 963 |
fprintf(fp, |
| 964 |
" -Q, --min-BQ INT skip bases with baseQ/BAQ smaller than INT [%d]\n", mplp->min_baseQ); |
| 965 |
fprintf(fp, |
| 966 |
" -r, --region REG region in which pileup is generated\n" |
| 967 |
" -R, --ignore-RG ignore RG tags (one BAM = one sample)\n" |
| 968 |
" --rf, --incl-flags STR|INT\n" |
| 969 |
" required flags: only include reads with any of\n" |
| 970 |
" the mask bits set [%s]\n", tmp_require); |
| 971 |
fprintf(fp, |
| 972 |
" --ff, --excl-flags STR|INT\n" |
| 973 |
" filter flags: skip reads with any of the mask bits set\n" |
| 974 |
" [%s]\n", tmp_filter); |
| 975 |
fprintf(fp, |
| 976 |
" -x, --ignore-overlaps-removal, --disable-overlap-removal\n" |
| 977 |
" disable read-pair overlap detection and removal\n" |
| 978 |
" -X, --customized-index use customized index files\n" |
| 979 |
"\n" |
| 980 |
"Output options:\n" |
| 981 |
" -o, --output FILE write output to FILE [standard output]\n" |
| 982 |
" -O, --output-BP output base positions on reads, current orientation\n" |
| 983 |
" --output-BP-5 output base positions on reads, 5' to 3' orientation\n" |
| 984 |
" -M, --output-mods output base modifications\n" |
| 985 |
" -s, --output-MQ output mapping quality\n" |
| 986 |
" --output-QNAME output read names\n" |
| 987 |
" --output-extra STR output extra read fields and read tag values\n" |
| 988 |
" --output-sep CHAR set the separator character for tag lists [,]\n" |
| 989 |
" --output-empty CHAR set the no value character for tag lists [*]\n" |
| 990 |
" --no-output-ins skip insertion sequence after +NUM\n" |
| 991 |
" Use twice for complete insertion removal\n" |
| 992 |
" --no-output-ins-mods don't display base modifications within insertions\n" |
| 993 |
" --no-output-del skip deletion sequence after -NUM\n" |
| 994 |
" Use twice for complete deletion removal\n" |
| 995 |
" --no-output-ends remove ^MQUAL and $ markup in sequence column\n" |
| 996 |
" --reverse-del use '#' character for deletions on the reverse strand\n" |
| 997 |
" -a output all positions (including zero depth)\n" |
| 998 |
" -a -a (or -aa) output absolutely all positions, including unused ref. sequences\n" |
| 999 |
"\n" |
| 1000 |
"Generic options:\n"); |
| 1001 |
sam_global_opt_help(fp, "-.--.--."); |
| 1002 |
|
| 1003 |
fprintf(fp, "\n" |
| 1004 |
"Note that using \"samtools mpileup\" to generate BCF or VCF files has been\n" |
| 1005 |
"removed. To output these formats, please use \"bcftools mpileup\" instead.\n"); |
| 1006 |
|
| 1007 |
free(tmp_require); |
| 1008 |
free(tmp_filter); |
| 1009 |
} |
| 1010 |
|
| 1011 |
int bam_mpileup(int argc, char *argv[]) |
| 1012 |
{ |
| 1013 |
int c; |
| 1014 |
const char *file_list = NULL; |
| 1015 |
char **fn = NULL; |
| 1016 |
int nfiles = 0, use_orphan = 0, has_index_file = 0; |
| 1017 |
mplp_conf_t mplp; |
| 1018 |
memset(&mplp, 0, sizeof(mplp_conf_t)); |
| 1019 |
mplp.min_baseQ = 13; |
| 1020 |
mplp.capQ_thres = 0; |
| 1021 |
mplp.max_depth = MPLP_MAX_DEPTH; |
| 1022 |
mplp.flag = MPLP_NO_ORPHAN | MPLP_REALN | MPLP_SMART_OVERLAPS; |
| 1023 |
mplp.argc = argc; mplp.argv = argv; |
| 1024 |
mplp.rflag_filter = BAM_FUNMAP | BAM_FSECONDARY | BAM_FQCFAIL | BAM_FDUP; |
| 1025 |
mplp.output_fname = NULL; |
| 1026 |
mplp.all = 0; |
| 1027 |
mplp.rev_del = 0; |
| 1028 |
mplp.sep = ','; |
| 1029 |
mplp.empty = '*'; |
| 1030 |
sam_global_args_init(&mplp.ga); |
| 1031 |
|
| 1032 |
static const struct option lopts[] = |
| 1033 |
{ |
| 1034 |
SAM_OPT_GLOBAL_OPTIONS('-', 0, '-', '-', 0, '-'), |
| 1035 |
{"rf", required_argument, NULL, 1}, |
| 1036 |
{"ff", required_argument, NULL, 2}, |
| 1037 |
{"incl-flags", required_argument, NULL, 1}, |
| 1038 |
{"excl-flags", required_argument, NULL, 2}, |
| 1039 |
{"output", required_argument, NULL, 3}, |
| 1040 |
{"output-QNAME", no_argument, NULL, 5}, |
| 1041 |
{"output-qname", no_argument, NULL, 5}, |
| 1042 |
{"illumina1.3+", no_argument, NULL, '6'}, |
| 1043 |
{"count-orphans", no_argument, NULL, 'A'}, |
| 1044 |
{"bam-list", required_argument, NULL, 'b'}, |
| 1045 |
{"no-BAQ", no_argument, NULL, 'B'}, |
| 1046 |
{"no-baq", no_argument, NULL, 'B'}, |
| 1047 |
{"adjust-MQ", required_argument, NULL, 'C'}, |
| 1048 |
{"adjust-mq", required_argument, NULL, 'C'}, |
| 1049 |
{"max-depth", required_argument, NULL, 'd'}, |
| 1050 |
{"redo-BAQ", no_argument, NULL, 'E'}, |
| 1051 |
{"redo-baq", no_argument, NULL, 'E'}, |
| 1052 |
{"fasta-ref", required_argument, NULL, 'f'}, |
| 1053 |
{"exclude-RG", required_argument, NULL, 'G'}, |
| 1054 |
{"exclude-rg", required_argument, NULL, 'G'}, |
| 1055 |
{"positions", required_argument, NULL, 'l'}, |
| 1056 |
{"region", required_argument, NULL, 'r'}, |
| 1057 |
{"ignore-RG", no_argument, NULL, 'R'}, |
| 1058 |
{"ignore-rg", no_argument, NULL, 'R'}, |
| 1059 |
{"min-MQ", required_argument, NULL, 'q'}, |
| 1060 |
{"min-mq", required_argument, NULL, 'q'}, |
| 1061 |
{"min-BQ", required_argument, NULL, 'Q'}, |
| 1062 |
{"min-bq", required_argument, NULL, 'Q'}, |
| 1063 |
|
| 1064 |
{"ignore-overlaps-removal", no_argument, NULL, 'x'}, |
| 1065 |
{"disable-overlap-removal", no_argument, NULL, 'x'}, |
| 1066 |
{"output-mods", no_argument, NULL, 'M'}, |
| 1067 |
{"output-BP", no_argument, NULL, 'O'}, |
| 1068 |
{"output-bp", no_argument, NULL, 'O'}, |
| 1069 |
{"output-BP-5", no_argument, NULL, 14}, |
| 1070 |
{"output-bp-5", no_argument, NULL, 14}, |
| 1071 |
{"output-MQ", no_argument, NULL, 's'}, |
| 1072 |
{"output-mq", no_argument, NULL, 's'}, |
| 1073 |
{"ext-prob", required_argument, NULL, 'e'}, |
| 1074 |
{"gap-frac", required_argument, NULL, 'F'}, |
| 1075 |
{"tandem-qual", required_argument, NULL, 'h'}, |
| 1076 |
{"skip-indels", no_argument, NULL, 'I'}, |
| 1077 |
{"max-idepth", required_argument, NULL, 'L'}, |
| 1078 |
{"min-ireads ", required_argument, NULL, 'm'}, |
| 1079 |
{"per-sample-mF", no_argument, NULL, 'p'}, |
| 1080 |
{"per-sample-mf", no_argument, NULL, 'p'}, |
| 1081 |
{"platforms", required_argument, NULL, 'P'}, |
| 1082 |
{"customized-index", no_argument, NULL, 'X'}, |
| 1083 |
{"reverse-del", no_argument, NULL, 6}, |
| 1084 |
{"output-extra", required_argument, NULL, 7}, |
| 1085 |
{"output-sep", required_argument, NULL, 8}, |
| 1086 |
{"output-empty", required_argument, NULL, 9}, |
| 1087 |
{"no-output-ins", no_argument, NULL, 10}, |
| 1088 |
{"no-output-ins-mods", no_argument, NULL, 11}, |
| 1089 |
{"no-output-del", no_argument, NULL, 12}, |
| 1090 |
{"no-output-ends", no_argument, NULL, 13}, |
| 1091 |
{NULL, 0, NULL, 0} |
| 1092 |
}; |
| 1093 |
|
| 1094 |
while ((c = getopt_long(argc, argv, "Af:r:l:q:Q:RC:Bd:b:o:EG:6OsxXaM",lopts,NULL)) >= 0) { |
| 1095 |
switch (c) { |
| 1096 |
case 'x': mplp.flag &= ~MPLP_SMART_OVERLAPS; break; |
| 1097 |
case 1 : |
| 1098 |
mplp.rflag_require = bam_str2flag(optarg); |
| 1099 |
if ( mplp.rflag_require<0 ) { fprintf(stderr,"Could not parse --rf %s\n", optarg); return 1; } |
| 1100 |
break; |
| 1101 |
case 2 : |
| 1102 |
mplp.rflag_filter = bam_str2flag(optarg); |
| 1103 |
if ( mplp.rflag_filter<0 ) { fprintf(stderr,"Could not parse --ff %s\n", optarg); return 1; } |
| 1104 |
break; |
| 1105 |
case 3 : mplp.output_fname = optarg; break; |
| 1106 |
case 5 : mplp.flag |= MPLP_PRINT_QNAME; break; |
| 1107 |
case 6 : mplp.rev_del = 1; break; |
| 1108 |
case 7 : |
| 1109 |
if (build_auxlist(&mplp, optarg) != 0) { |
| 1110 |
fprintf(stderr,"Could not build aux list using '%s'\n", optarg); |
| 1111 |
return 1; |
| 1112 |
} |
| 1113 |
break; |
| 1114 |
case 8: mplp.sep = optarg[0]; break; |
| 1115 |
case 9: mplp.empty = optarg[0]; break; |
| 1116 |
case 10: mplp.no_ins++; break; |
| 1117 |
case 11: mplp.no_ins_mods = 1; break; |
| 1118 |
case 12: mplp.no_del++; break; |
| 1119 |
case 13: mplp.no_ends = 1; break; |
| 1120 |
case 'f': |
| 1121 |
mplp.fai = fai_load(optarg); |
| 1122 |
if (mplp.fai == NULL) return 1; |
| 1123 |
mplp.fai_fname = optarg; |
| 1124 |
break; |
| 1125 |
case 'd': mplp.max_depth = atoi(optarg); break; |
| 1126 |
case 'r': mplp.reg = strdup(optarg); break; |
| 1127 |
case 'l': |
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
mplp.bed = bed_read(optarg); |
| 1132 |
if (!mplp.bed) { print_error_errno("mpileup", "Could not read file \"%s\"", optarg); return 1; } |
| 1133 |
break; |
| 1134 |
case 'B': mplp.flag &= ~MPLP_REALN; break; |
| 1135 |
case 'X': has_index_file = 1; break; |
| 1136 |
case 'E': mplp.flag |= MPLP_REDO_BAQ; break; |
| 1137 |
case '6': mplp.flag |= MPLP_ILLUMINA13; break; |
| 1138 |
case 'R': mplp.flag |= MPLP_IGNORE_RG; break; |
| 1139 |
case 's': mplp.flag |= MPLP_PRINT_MAPQ_CHAR; break; |
| 1140 |
case 'O': mplp.flag |= MPLP_PRINT_QPOS; break; |
| 1141 |
case 14: mplp.flag |= MPLP_PRINT_QPOS5; break; |
| 1142 |
case 'M': mplp.flag |= MPLP_PRINT_MODS; break; |
| 1143 |
case 'C': mplp.capQ_thres = atoi(optarg); break; |
| 1144 |
case 'q': mplp.min_mq = atoi(optarg); break; |
| 1145 |
case 'Q': mplp.min_baseQ = atoi(optarg); break; |
| 1146 |
case 'b': file_list = optarg; break; |
| 1147 |
case 'o': mplp.output_fname = optarg; break; |
| 1148 |
case 'A': use_orphan = 1; break; |
| 1149 |
case 'G': { |
| 1150 |
FILE *fp_rg; |
| 1151 |
char buf[1024]; |
| 1152 |
mplp.rghash = khash_str2int_init(); |
| 1153 |
if ((fp_rg = fopen(optarg, "r")) == NULL) |
| 1154 |
fprintf(stderr, "[%s] Fail to open file %s. Continue anyway.\n", __func__, optarg); |
| 1155 |
while (!feof(fp_rg) && fscanf(fp_rg, "%s", buf) > 0) |
| 1156 |
khash_str2int_inc(mplp.rghash, strdup(buf)); |
| 1157 |
fclose(fp_rg); |
| 1158 |
} |
| 1159 |
break; |
| 1160 |
case 'a': mplp.all++; break; |
| 1161 |
default: |
| 1162 |
if (parse_sam_global_opt(c, optarg, lopts, &mplp.ga) == 0) break; |
| 1163 |
|
| 1164 |
case '?': |
| 1165 |
print_usage(stderr, &mplp); |
| 1166 |
return 1; |
| 1167 |
} |
| 1168 |
} |
| 1169 |
if (!mplp.fai && mplp.ga.reference) { |
| 1170 |
mplp.fai_fname = mplp.ga.reference; |
| 1171 |
mplp.fai = fai_load(mplp.fai_fname); |
| 1172 |
if (mplp.fai == NULL) return 1; |
| 1173 |
} |
| 1174 |
|
| 1175 |
if ( !(mplp.flag&MPLP_REALN) && mplp.flag&MPLP_REDO_BAQ ) |
| 1176 |
{ |
| 1177 |
fprintf(stderr,"Error: The -B option cannot be combined with -E\n"); |
| 1178 |
return 1; |
| 1179 |
} |
| 1180 |
if (use_orphan) mplp.flag &= ~MPLP_NO_ORPHAN; |
| 1181 |
if (argc == 1) |
| 1182 |
{ |
| 1183 |
print_usage(stderr, &mplp); |
| 1184 |
return 1; |
| 1185 |
} |
| 1186 |
int ret; |
| 1187 |
if (file_list) { |
| 1188 |
if (has_index_file) { |
| 1189 |
fprintf(stderr,"Error: The -b option cannot be combined with -X\n"); |
| 1190 |
return 1; |
| 1191 |
} |
| 1192 |
if ( read_file_list(file_list,&nfiles,&fn) ) return 1; |
| 1193 |
ret = mpileup(&mplp,nfiles,fn,NULL); |
| 1194 |
for (c=0; c<nfiles; c++) free(fn[c]); |
| 1195 |
free(fn); |
| 1196 |
} |
| 1197 |
else { |
| 1198 |
if (has_index_file) { |
| 1199 |
if ((argc - optind)%2 !=0) { |
| 1200 |
fprintf(stderr, "Odd number of filenames detected! Each BAM file should have an index file\n"); |
| 1201 |
return 1; |
| 1202 |
} |
| 1203 |
nfiles = (argc - optind)/2; |
| 1204 |
ret = mpileup(&mplp, nfiles, argv + optind, argv + nfiles + optind); |
| 1205 |
} else { |
| 1206 |
nfiles = argc - optind; |
| 1207 |
ret = mpileup(&mplp, nfiles, argv + optind, NULL); |
| 1208 |
} |
| 1209 |
} |
| 1210 |
if (mplp.rghash) khash_str2int_destroy_free(mplp.rghash); |
| 1211 |
free(mplp.reg); free(mplp.pl_list); |
| 1212 |
if (mplp.fai) fai_destroy(mplp.fai); |
| 1213 |
if (mplp.bed) bed_destroy(mplp.bed); |
| 1214 |
if (mplp.auxlist) kl_destroy(auxlist, (klist_t(auxlist) *)mplp.auxlist); |
| 1215 |
return ret; |
| 1216 |
} |