annotate PsiCLASS-1.0.2/samtools-0.1.19/bam_plcmd.c @ 0:903fc43d6227 draft default tip

Uploaded
author lsong10
date Fri, 26 Mar 2021 16:52:45 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1 #include <math.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2 #include <stdio.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3 #include <unistd.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4 #include <ctype.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5 #include <string.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6 #include <errno.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7 #include <sys/stat.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8 #include <getopt.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9 #include "sam.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10 #include "faidx.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11 #include "kstring.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12 #include "sam_header.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14 static inline int printw(int c, FILE *fp)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16 char buf[16];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17 int l, x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18 if (c == 0) return fputc('0', fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19 for (l = 0, x = c < 0? -c : c; x > 0; x /= 10) buf[l++] = x%10 + '0';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20 if (c < 0) buf[l++] = '-';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 buf[l] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22 for (x = 0; x < l/2; ++x) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23 int y = buf[x]; buf[x] = buf[l-1-x]; buf[l-1-x] = y;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25 fputs(buf, fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 static inline void pileup_seq(const bam_pileup1_t *p, int pos, int ref_len, const char *ref)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31 int j;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32 if (p->is_head) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33 putchar('^');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34 putchar(p->b->core.qual > 93? 126 : p->b->core.qual + 33);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36 if (!p->is_del) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37 int c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos)];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38 if (ref) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39 int rb = pos < ref_len? ref[pos] : 'N';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40 if (c == '=' || bam_nt16_table[c] == bam_nt16_table[rb]) c = bam1_strand(p->b)? ',' : '.';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41 else c = bam1_strand(p->b)? tolower(c) : toupper(c);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 if (c == '=') c = bam1_strand(p->b)? ',' : '.';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 else c = bam1_strand(p->b)? tolower(c) : toupper(c);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 putchar(c);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47 } else putchar(p->is_refskip? (bam1_strand(p->b)? '<' : '>') : '*');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48 if (p->indel > 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49 putchar('+'); printw(p->indel, stdout);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50 for (j = 1; j <= p->indel; ++j) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51 int c = bam_nt16_rev_table[bam1_seqi(bam1_seq(p->b), p->qpos + j)];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52 putchar(bam1_strand(p->b)? tolower(c) : toupper(c));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54 } else if (p->indel < 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 printw(p->indel, stdout);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56 for (j = 1; j <= -p->indel; ++j) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57 int c = (ref && (int)pos+j < ref_len)? ref[pos+j] : 'N';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58 putchar(bam1_strand(p->b)? tolower(c) : toupper(c));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61 if (p->is_tail) putchar('$');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64 #include <assert.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65 #include "bam2bcf.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66 #include "sample.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68 #define MPLP_GLF 0x10
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 #define MPLP_NO_COMP 0x20
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70 #define MPLP_NO_ORPHAN 0x40
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71 #define MPLP_REALN 0x80
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72 #define MPLP_NO_INDEL 0x400
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73 #define MPLP_REDO_BAQ 0x800
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 #define MPLP_ILLUMINA13 0x1000
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75 #define MPLP_IGNORE_RG 0x2000
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 #define MPLP_PRINT_POS 0x4000
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77 #define MPLP_PRINT_MAPQ 0x8000
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78 #define MPLP_PER_SAMPLE 0x10000
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80 void *bed_read(const char *fn);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81 void bed_destroy(void *_h);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82 int bed_overlap(const void *_h, const char *chr, int beg, int end);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84 typedef struct {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85 int max_mq, min_mq, flag, min_baseQ, capQ_thres, max_depth, max_indel_depth, fmt_flag;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86 int rflag_require, rflag_filter;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 int openQ, extQ, tandemQ, min_support; // for indels
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88 double min_frac; // for indels
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89 char *reg, *pl_list, *fai_fname;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90 faidx_t *fai;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91 void *bed, *rghash;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92 } mplp_conf_t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94 typedef struct {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95 bamFile fp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96 bam_iter_t iter;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
97 bam_header_t *h;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
98 int ref_id;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
99 char *ref;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
100 const mplp_conf_t *conf;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
101 } mplp_aux_t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
102
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
103 typedef struct {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
104 int n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
105 int *n_plp, *m_plp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
106 bam_pileup1_t **plp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
107 } mplp_pileup_t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
108
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
109 static int mplp_func(void *data, bam1_t *b)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
110 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
111 extern int bam_realn(bam1_t *b, const char *ref);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
112 extern int bam_prob_realn_core(bam1_t *b, const char *ref, int);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
113 extern int bam_cap_mapQ(bam1_t *b, char *ref, int thres);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
114 mplp_aux_t *ma = (mplp_aux_t*)data;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
115 int ret, skip = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
116 do {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
117 int has_ref;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
118 ret = ma->iter? bam_iter_read(ma->fp, ma->iter, b) : bam_read1(ma->fp, b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
119 if (ret < 0) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
120 if (b->core.tid < 0 || (b->core.flag&BAM_FUNMAP)) { // exclude unmapped reads
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
121 skip = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
122 continue;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
123 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
124 if (ma->conf->rflag_require && !(ma->conf->rflag_require&b->core.flag)) { skip = 1; continue; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
125 if (ma->conf->rflag_filter && ma->conf->rflag_filter&b->core.flag) { skip = 1; continue; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
126 if (ma->conf->bed) { // test overlap
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
127 skip = !bed_overlap(ma->conf->bed, ma->h->target_name[b->core.tid], b->core.pos, bam_calend(&b->core, bam1_cigar(b)));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
128 if (skip) continue;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
129 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
130 if (ma->conf->rghash) { // exclude read groups
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
131 uint8_t *rg = bam_aux_get(b, "RG");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
132 skip = (rg && bcf_str2id(ma->conf->rghash, (const char*)(rg+1)) >= 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
133 if (skip) continue;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
134 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
135 if (ma->conf->flag & MPLP_ILLUMINA13) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
136 int i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
137 uint8_t *qual = bam1_qual(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
138 for (i = 0; i < b->core.l_qseq; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
139 qual[i] = qual[i] > 31? qual[i] - 31 : 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
140 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
141 has_ref = (ma->ref && ma->ref_id == b->core.tid)? 1 : 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
142 skip = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
143 if (has_ref && (ma->conf->flag&MPLP_REALN)) bam_prob_realn_core(b, ma->ref, (ma->conf->flag & MPLP_REDO_BAQ)? 7 : 3);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
144 if (has_ref && ma->conf->capQ_thres > 10) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
145 int q = bam_cap_mapQ(b, ma->ref, ma->conf->capQ_thres);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
146 if (q < 0) skip = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
147 else if (b->core.qual > q) b->core.qual = q;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
148 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
149 else if (b->core.qual < ma->conf->min_mq) skip = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
150 else if ((ma->conf->flag&MPLP_NO_ORPHAN) && (b->core.flag&1) && !(b->core.flag&2)) skip = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
151 } while (skip);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
152 return ret;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
153 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
154
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
155 static void group_smpl(mplp_pileup_t *m, bam_sample_t *sm, kstring_t *buf,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
156 int n, char *const*fn, int *n_plp, const bam_pileup1_t **plp, int ignore_rg)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
157 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
158 int i, j;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
159 memset(m->n_plp, 0, m->n * sizeof(int));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
160 for (i = 0; i < n; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
161 for (j = 0; j < n_plp[i]; ++j) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
162 const bam_pileup1_t *p = plp[i] + j;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
163 uint8_t *q;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
164 int id = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
165 q = ignore_rg? 0 : bam_aux_get(p->b, "RG");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
166 if (q) id = bam_smpl_rg2smid(sm, fn[i], (char*)q+1, buf);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
167 if (id < 0) id = bam_smpl_rg2smid(sm, fn[i], 0, buf);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
168 if (id < 0 || id >= m->n) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
169 assert(q); // otherwise a bug
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
170 fprintf(stderr, "[%s] Read group %s used in file %s but absent from the header or an alignment missing read group.\n", __func__, (char*)q+1, fn[i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
171 exit(1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
172 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
173 if (m->n_plp[id] == m->m_plp[id]) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
174 m->m_plp[id] = m->m_plp[id]? m->m_plp[id]<<1 : 8;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
175 m->plp[id] = realloc(m->plp[id], sizeof(bam_pileup1_t) * m->m_plp[id]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
176 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
177 m->plp[id][m->n_plp[id]++] = *p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
178 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
179 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
180 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
181
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
182 static int mpileup(mplp_conf_t *conf, int n, char **fn)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
183 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
184 extern void *bcf_call_add_rg(void *rghash, const char *hdtext, const char *list);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
185 extern void bcf_call_del_rghash(void *rghash);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
186 mplp_aux_t **data;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
187 int i, tid, pos, *n_plp, tid0 = -1, beg0 = 0, end0 = 1u<<29, ref_len, ref_tid = -1, max_depth, max_indel_depth;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
188 const bam_pileup1_t **plp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
189 bam_mplp_t iter;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
190 bam_header_t *h = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
191 char *ref;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
192 void *rghash = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
193
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
194 bcf_callaux_t *bca = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
195 bcf_callret1_t *bcr = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
196 bcf_call_t bc;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
197 bcf_t *bp = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
198 bcf_hdr_t *bh = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
199
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
200 bam_sample_t *sm = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
201 kstring_t buf;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
202 mplp_pileup_t gplp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
203
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
204 memset(&gplp, 0, sizeof(mplp_pileup_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
205 memset(&buf, 0, sizeof(kstring_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
206 memset(&bc, 0, sizeof(bcf_call_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
207 data = calloc(n, sizeof(void*));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
208 plp = calloc(n, sizeof(void*));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
209 n_plp = calloc(n, sizeof(int*));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
210 sm = bam_smpl_init();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
211
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
212 // read the header and initialize data
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
213 for (i = 0; i < n; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
214 bam_header_t *h_tmp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
215 data[i] = calloc(1, sizeof(mplp_aux_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
216 data[i]->fp = strcmp(fn[i], "-") == 0? bam_dopen(fileno(stdin), "r") : bam_open(fn[i], "r");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
217 if ( !data[i]->fp )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
218 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
219 fprintf(stderr, "[%s] failed to open %s: %s\n", __func__, fn[i], strerror(errno));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
220 exit(1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
221 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
222 data[i]->conf = conf;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
223 h_tmp = bam_header_read(data[i]->fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
224 if ( !h_tmp ) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
225 fprintf(stderr,"[%s] fail to read the header of %s\n", __func__, fn[i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
226 exit(1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
227 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
228 data[i]->h = i? h : h_tmp; // for i==0, "h" has not been set yet
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
229 bam_smpl_add(sm, fn[i], (conf->flag&MPLP_IGNORE_RG)? 0 : h_tmp->text);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
230 rghash = bcf_call_add_rg(rghash, h_tmp->text, conf->pl_list);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
231 if (conf->reg) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
232 int beg, end;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
233 bam_index_t *idx;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
234 idx = bam_index_load(fn[i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
235 if (idx == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
236 fprintf(stderr, "[%s] fail to load index for %s\n", __func__, fn[i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
237 exit(1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
238 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
239 if (bam_parse_region(h_tmp, conf->reg, &tid, &beg, &end) < 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
240 fprintf(stderr, "[%s] malformatted region or wrong seqname for %s\n", __func__, fn[i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
241 exit(1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
242 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
243 if (i == 0) tid0 = tid, beg0 = beg, end0 = end;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
244 data[i]->iter = bam_iter_query(idx, tid, beg, end);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
245 bam_index_destroy(idx);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
246 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
247 if (i == 0) h = h_tmp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
248 else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
249 // FIXME: to check consistency
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
250 bam_header_destroy(h_tmp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
251 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
252 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
253 gplp.n = sm->n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
254 gplp.n_plp = calloc(sm->n, sizeof(int));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
255 gplp.m_plp = calloc(sm->n, sizeof(int));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
256 gplp.plp = calloc(sm->n, sizeof(void*));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
257
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
258 fprintf(stderr, "[%s] %d samples in %d input files\n", __func__, sm->n, n);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
259 // write the VCF header
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
260 if (conf->flag & MPLP_GLF) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
261 kstring_t s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
262 bh = calloc(1, sizeof(bcf_hdr_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
263 s.l = s.m = 0; s.s = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
264 bp = bcf_open("-", (conf->flag&MPLP_NO_COMP)? "wu" : "w");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
265 for (i = 0; i < h->n_targets; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
266 kputs(h->target_name[i], &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
267 kputc('\0', &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
268 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
269 bh->l_nm = s.l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
270 bh->name = malloc(s.l);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
271 memcpy(bh->name, s.s, s.l);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
272 s.l = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
273 for (i = 0; i < sm->n; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
274 kputs(sm->smpl[i], &s); kputc('\0', &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
275 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
276 bh->l_smpl = s.l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
277 bh->sname = malloc(s.l);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
278 memcpy(bh->sname, s.s, s.l);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
279 s.l = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
280 ksprintf(&s, "##samtoolsVersion=%s\n", BAM_VERSION);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
281 if (conf->fai_fname) ksprintf(&s, "##reference=file://%s\n", conf->fai_fname);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
282 h->dict = sam_header_parse2(h->text);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
283 int nseq;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
284 const char *tags[] = {"SN","LN","UR","M5",NULL};
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
285 char **tbl = sam_header2tbl_n(h->dict, "SQ", tags, &nseq);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
286 for (i=0; i<nseq; i++)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
287 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
288 ksprintf(&s, "##contig=<ID=%s", tbl[4*i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
289 if ( tbl[4*i+1] ) ksprintf(&s, ",length=%s", tbl[4*i+1]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
290 if ( tbl[4*i+2] ) ksprintf(&s, ",URL=%s", tbl[4*i+2]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
291 if ( tbl[4*i+3] ) ksprintf(&s, ",md5=%s", tbl[4*i+3]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
292 kputs(">\n", &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
293 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
294 if (tbl) free(tbl);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
295 bh->txt = s.s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
296 bh->l_txt = 1 + s.l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
297 bcf_hdr_sync(bh);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
298 bcf_hdr_write(bp, bh);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
299 bca = bcf_call_init(-1., conf->min_baseQ);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
300 bcr = calloc(sm->n, sizeof(bcf_callret1_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
301 bca->rghash = rghash;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
302 bca->openQ = conf->openQ, bca->extQ = conf->extQ, bca->tandemQ = conf->tandemQ;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
303 bca->min_frac = conf->min_frac;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
304 bca->min_support = conf->min_support;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
305 bca->per_sample_flt = conf->flag & MPLP_PER_SAMPLE;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
306 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
307 if (tid0 >= 0 && conf->fai) { // region is set
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
308 ref = faidx_fetch_seq(conf->fai, h->target_name[tid0], 0, 0x7fffffff, &ref_len);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
309 ref_tid = tid0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
310 for (i = 0; i < n; ++i) data[i]->ref = ref, data[i]->ref_id = tid0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
311 } else ref_tid = -1, ref = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
312 iter = bam_mplp_init(n, mplp_func, (void**)data);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
313 max_depth = conf->max_depth;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
314 if (max_depth * sm->n > 1<<20)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
315 fprintf(stderr, "(%s) Max depth is above 1M. Potential memory hog!\n", __func__);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
316 if (max_depth * sm->n < 8000) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
317 max_depth = 8000 / sm->n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
318 fprintf(stderr, "<%s> Set max per-file depth to %d\n", __func__, max_depth);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
319 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
320 max_indel_depth = conf->max_indel_depth * sm->n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
321 bam_mplp_set_maxcnt(iter, max_depth);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
322 while (bam_mplp_auto(iter, &tid, &pos, n_plp, plp) > 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
323 if (conf->reg && (pos < beg0 || pos >= end0)) continue; // out of the region requested
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
324 if (conf->bed && tid >= 0 && !bed_overlap(conf->bed, h->target_name[tid], pos, pos+1)) continue;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
325 if (tid != ref_tid) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
326 free(ref); ref = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
327 if (conf->fai) ref = faidx_fetch_seq(conf->fai, h->target_name[tid], 0, 0x7fffffff, &ref_len);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
328 for (i = 0; i < n; ++i) data[i]->ref = ref, data[i]->ref_id = tid;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
329 ref_tid = tid;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
330 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
331 if (conf->flag & MPLP_GLF) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
332 int total_depth, _ref0, ref16;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
333 bcf1_t *b = calloc(1, sizeof(bcf1_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
334 for (i = total_depth = 0; i < n; ++i) total_depth += n_plp[i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
335 group_smpl(&gplp, sm, &buf, n, fn, n_plp, plp, conf->flag & MPLP_IGNORE_RG);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
336 _ref0 = (ref && pos < ref_len)? ref[pos] : 'N';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
337 ref16 = bam_nt16_table[_ref0];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
338 for (i = 0; i < gplp.n; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
339 bcf_call_glfgen(gplp.n_plp[i], gplp.plp[i], ref16, bca, bcr + i);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
340 bcf_call_combine(gplp.n, bcr, bca, ref16, &bc);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
341 bcf_call2bcf(tid, pos, &bc, b, bcr, conf->fmt_flag, 0, 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
342 bcf_write(bp, bh, b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
343 bcf_destroy(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
344 // call indels
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
345 if (!(conf->flag&MPLP_NO_INDEL) && total_depth < max_indel_depth && bcf_call_gap_prep(gplp.n, gplp.n_plp, gplp.plp, pos, bca, ref, rghash) >= 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
346 for (i = 0; i < gplp.n; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
347 bcf_call_glfgen(gplp.n_plp[i], gplp.plp[i], -1, bca, bcr + i);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
348 if (bcf_call_combine(gplp.n, bcr, bca, -1, &bc) >= 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
349 b = calloc(1, sizeof(bcf1_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
350 bcf_call2bcf(tid, pos, &bc, b, bcr, conf->fmt_flag, bca, ref);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
351 bcf_write(bp, bh, b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
352 bcf_destroy(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
353 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
354 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
355 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
356 printf("%s\t%d\t%c", h->target_name[tid], pos + 1, (ref && pos < ref_len)? ref[pos] : 'N');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
357 for (i = 0; i < n; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
358 int j, cnt;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
359 for (j = cnt = 0; j < n_plp[i]; ++j) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
360 const bam_pileup1_t *p = plp[i] + j;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
361 if (bam1_qual(p->b)[p->qpos] >= conf->min_baseQ) ++cnt;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
362 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
363 printf("\t%d\t", cnt);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
364 if (n_plp[i] == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
365 printf("*\t*"); // FIXME: printf() is very slow...
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
366 if (conf->flag & MPLP_PRINT_POS) printf("\t*");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
367 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
368 for (j = 0; j < n_plp[i]; ++j) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
369 const bam_pileup1_t *p = plp[i] + j;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
370 if (bam1_qual(p->b)[p->qpos] >= conf->min_baseQ)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
371 pileup_seq(plp[i] + j, pos, ref_len, ref);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
372 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
373 putchar('\t');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
374 for (j = 0; j < n_plp[i]; ++j) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
375 const bam_pileup1_t *p = plp[i] + j;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
376 int c = bam1_qual(p->b)[p->qpos];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
377 if (c >= conf->min_baseQ) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
378 c = c + 33 < 126? c + 33 : 126;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
379 putchar(c);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
380 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
381 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
382 if (conf->flag & MPLP_PRINT_MAPQ) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
383 putchar('\t');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
384 for (j = 0; j < n_plp[i]; ++j) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
385 int c = plp[i][j].b->core.qual + 33;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
386 if (c > 126) c = 126;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
387 putchar(c);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
388 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
389 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
390 if (conf->flag & MPLP_PRINT_POS) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
391 putchar('\t');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
392 for (j = 0; j < n_plp[i]; ++j) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
393 if (j > 0) putchar(',');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
394 printf("%d", plp[i][j].qpos + 1); // FIXME: printf() is very slow...
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
395 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
396 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
397 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
398 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
399 putchar('\n');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
400 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
401 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
402
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
403 bcf_close(bp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
404 bam_smpl_destroy(sm); free(buf.s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
405 for (i = 0; i < gplp.n; ++i) free(gplp.plp[i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
406 free(gplp.plp); free(gplp.n_plp); free(gplp.m_plp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
407 bcf_call_del_rghash(rghash);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
408 bcf_hdr_destroy(bh); bcf_call_destroy(bca); free(bc.PL); free(bcr);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
409 bam_mplp_destroy(iter);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
410 bam_header_destroy(h);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
411 for (i = 0; i < n; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
412 bam_close(data[i]->fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
413 if (data[i]->iter) bam_iter_destroy(data[i]->iter);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
414 free(data[i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
415 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
416 free(data); free(plp); free(ref); free(n_plp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
417 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
418 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
419
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
420 #define MAX_PATH_LEN 1024
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
421 int read_file_list(const char *file_list,int *n,char **argv[])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
422 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
423 char buf[MAX_PATH_LEN];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
424 int len, nfiles = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
425 char **files = NULL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
426 struct stat sb;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
427
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
428 *n = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
429 *argv = NULL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
430
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
431 FILE *fh = fopen(file_list,"r");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
432 if ( !fh )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
433 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
434 fprintf(stderr,"%s: %s\n", file_list,strerror(errno));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
435 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
436 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
437
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
438 files = calloc(nfiles,sizeof(char*));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
439 nfiles = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
440 while ( fgets(buf,MAX_PATH_LEN,fh) )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
441 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
442 // allow empty lines and trailing spaces
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
443 len = strlen(buf);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
444 while ( len>0 && isspace(buf[len-1]) ) len--;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
445 if ( !len ) continue;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
446
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
447 // check sanity of the file list
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
448 buf[len] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
449 if (stat(buf, &sb) != 0)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
450 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
451 // no such file, check if it is safe to print its name
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
452 int i, safe_to_print = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
453 for (i=0; i<len; i++)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
454 if (!isprint(buf[i])) { safe_to_print = 0; break; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
455 if ( safe_to_print )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
456 fprintf(stderr,"The file list \"%s\" appears broken, could not locate: %s\n", file_list,buf);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
457 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
458 fprintf(stderr,"Does the file \"%s\" really contain a list of files and do all exist?\n", file_list);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
459 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
460 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
461
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
462 nfiles++;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
463 files = realloc(files,nfiles*sizeof(char*));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
464 files[nfiles-1] = strdup(buf);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
465 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
466 fclose(fh);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
467 if ( !nfiles )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
468 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
469 fprintf(stderr,"No files read from %s\n", file_list);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
470 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
471 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
472 *argv = files;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
473 *n = nfiles;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
474 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
475 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
476 #undef MAX_PATH_LEN
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
477
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
478 int bam_mpileup(int argc, char *argv[])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
479 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
480 int c;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
481 const char *file_list = NULL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
482 char **fn = NULL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
483 int nfiles = 0, use_orphan = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
484 mplp_conf_t mplp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
485 memset(&mplp, 0, sizeof(mplp_conf_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
486 mplp.max_mq = 60;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
487 mplp.min_baseQ = 13;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
488 mplp.capQ_thres = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
489 mplp.max_depth = 250; mplp.max_indel_depth = 250;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
490 mplp.openQ = 40; mplp.extQ = 20; mplp.tandemQ = 100;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
491 mplp.min_frac = 0.002; mplp.min_support = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
492 mplp.flag = MPLP_NO_ORPHAN | MPLP_REALN;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
493 static struct option lopts[] =
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
494 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
495 {"rf",1,0,1}, // require flag
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
496 {"ff",1,0,2}, // filter flag
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
497 {0,0,0,0}
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
498 };
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
499 while ((c = getopt_long(argc, argv, "Agf:r:l:M:q:Q:uaRC:BDSd:L:b:P:po:e:h:Im:F:EG:6OsV1:2:",lopts,NULL)) >= 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
500 switch (c) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
501 case 1 : mplp.rflag_require = strtol(optarg,0,0); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
502 case 2 : mplp.rflag_filter = strtol(optarg,0,0); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
503 case 'f':
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
504 mplp.fai = fai_load(optarg);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
505 if (mplp.fai == 0) return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
506 mplp.fai_fname = optarg;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
507 break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
508 case 'd': mplp.max_depth = atoi(optarg); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
509 case 'r': mplp.reg = strdup(optarg); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
510 case 'l': mplp.bed = bed_read(optarg); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
511 case 'P': mplp.pl_list = strdup(optarg); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
512 case 'p': mplp.flag |= MPLP_PER_SAMPLE; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
513 case 'g': mplp.flag |= MPLP_GLF; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
514 case 'u': mplp.flag |= MPLP_NO_COMP | MPLP_GLF; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
515 case 'a': mplp.flag |= MPLP_NO_ORPHAN | MPLP_REALN; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
516 case 'B': mplp.flag &= ~MPLP_REALN; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
517 case 'D': mplp.fmt_flag |= B2B_FMT_DP; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
518 case 'S': mplp.fmt_flag |= B2B_FMT_SP; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
519 case 'V': mplp.fmt_flag |= B2B_FMT_DV; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
520 case 'I': mplp.flag |= MPLP_NO_INDEL; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
521 case 'E': mplp.flag |= MPLP_REDO_BAQ; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
522 case '6': mplp.flag |= MPLP_ILLUMINA13; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
523 case 'R': mplp.flag |= MPLP_IGNORE_RG; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
524 case 's': mplp.flag |= MPLP_PRINT_MAPQ; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
525 case 'O': mplp.flag |= MPLP_PRINT_POS; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
526 case 'C': mplp.capQ_thres = atoi(optarg); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
527 case 'M': mplp.max_mq = atoi(optarg); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
528 case 'q': mplp.min_mq = atoi(optarg); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
529 case 'Q': mplp.min_baseQ = atoi(optarg); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
530 case 'b': file_list = optarg; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
531 case 'o': mplp.openQ = atoi(optarg); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
532 case 'e': mplp.extQ = atoi(optarg); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
533 case 'h': mplp.tandemQ = atoi(optarg); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
534 case 'A': use_orphan = 1; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
535 case 'F': mplp.min_frac = atof(optarg); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
536 case 'm': mplp.min_support = atoi(optarg); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
537 case 'L': mplp.max_indel_depth = atoi(optarg); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
538 case 'G': {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
539 FILE *fp_rg;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
540 char buf[1024];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
541 mplp.rghash = bcf_str2id_init();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
542 if ((fp_rg = fopen(optarg, "r")) == 0)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
543 fprintf(stderr, "(%s) Fail to open file %s. Continue anyway.\n", __func__, optarg);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
544 while (!feof(fp_rg) && fscanf(fp_rg, "%s", buf) > 0) // this is not a good style, but forgive me...
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
545 bcf_str2id_add(mplp.rghash, strdup(buf));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
546 fclose(fp_rg);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
547 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
548 break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
549 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
550 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
551 if (use_orphan) mplp.flag &= ~MPLP_NO_ORPHAN;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
552 if (argc == 1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
553 fprintf(stderr, "\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
554 fprintf(stderr, "Usage: samtools mpileup [options] in1.bam [in2.bam [...]]\n\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
555 fprintf(stderr, "Input options:\n\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
556 fprintf(stderr, " -6 assume the quality is in the Illumina-1.3+ encoding\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
557 fprintf(stderr, " -A count anomalous read pairs\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
558 fprintf(stderr, " -B disable BAQ computation\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
559 fprintf(stderr, " -b FILE list of input BAM filenames, one per line [null]\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
560 fprintf(stderr, " -C INT parameter for adjusting mapQ; 0 to disable [0]\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
561 fprintf(stderr, " -d INT max per-BAM depth to avoid excessive memory usage [%d]\n", mplp.max_depth);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
562 fprintf(stderr, " -E recalculate extended BAQ on the fly thus ignoring existing BQs\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
563 fprintf(stderr, " -f FILE faidx indexed reference sequence file [null]\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
564 fprintf(stderr, " -G FILE exclude read groups listed in FILE [null]\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
565 fprintf(stderr, " -l FILE list of positions (chr pos) or regions (BED) [null]\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
566 fprintf(stderr, " -M INT cap mapping quality at INT [%d]\n", mplp.max_mq);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
567 fprintf(stderr, " -r STR region in which pileup is generated [null]\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
568 fprintf(stderr, " -R ignore RG tags\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
569 fprintf(stderr, " -q INT skip alignments with mapQ smaller than INT [%d]\n", mplp.min_mq);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
570 fprintf(stderr, " -Q INT skip bases with baseQ/BAQ smaller than INT [%d]\n", mplp.min_baseQ);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
571 fprintf(stderr, " --rf INT required flags: skip reads with mask bits unset []\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
572 fprintf(stderr, " --ff INT filter flags: skip reads with mask bits set []\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
573 fprintf(stderr, "\nOutput options:\n\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
574 fprintf(stderr, " -D output per-sample DP in BCF (require -g/-u)\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
575 fprintf(stderr, " -g generate BCF output (genotype likelihoods)\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
576 fprintf(stderr, " -O output base positions on reads (disabled by -g/-u)\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
577 fprintf(stderr, " -s output mapping quality (disabled by -g/-u)\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
578 fprintf(stderr, " -S output per-sample strand bias P-value in BCF (require -g/-u)\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
579 fprintf(stderr, " -u generate uncompress BCF output\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
580 fprintf(stderr, "\nSNP/INDEL genotype likelihoods options (effective with `-g' or `-u'):\n\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
581 fprintf(stderr, " -e INT Phred-scaled gap extension seq error probability [%d]\n", mplp.extQ);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
582 fprintf(stderr, " -F FLOAT minimum fraction of gapped reads for candidates [%g]\n", mplp.min_frac);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
583 fprintf(stderr, " -h INT coefficient for homopolymer errors [%d]\n", mplp.tandemQ);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
584 fprintf(stderr, " -I do not perform indel calling\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
585 fprintf(stderr, " -L INT max per-sample depth for INDEL calling [%d]\n", mplp.max_indel_depth);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
586 fprintf(stderr, " -m INT minimum gapped reads for indel candidates [%d]\n", mplp.min_support);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
587 fprintf(stderr, " -o INT Phred-scaled gap open sequencing error probability [%d]\n", mplp.openQ);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
588 fprintf(stderr, " -p apply -m and -F per-sample to increase sensitivity\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
589 fprintf(stderr, " -P STR comma separated list of platforms for indels [all]\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
590 fprintf(stderr, "\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
591 fprintf(stderr, "Notes: Assuming diploid individuals.\n\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
592 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
593 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
594 bam_no_B = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
595 if (file_list) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
596 if ( read_file_list(file_list,&nfiles,&fn) ) return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
597 mpileup(&mplp,nfiles,fn);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
598 for (c=0; c<nfiles; c++) free(fn[c]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
599 free(fn);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
600 } else mpileup(&mplp, argc - optind, argv + optind);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
601 if (mplp.rghash) bcf_str2id_thorough_destroy(mplp.rghash);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
602 free(mplp.reg); free(mplp.pl_list);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
603 if (mplp.fai) fai_destroy(mplp.fai);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
604 if (mplp.bed) bed_destroy(mplp.bed);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
605 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
606 }