7 #include "bcftools/bcf.h"
9 extern void ks_introsort_uint32_t(size_t n, uint32_t a[]);
11 #define CALL_ETA 0.03f
13 #define CALL_DEFTHETA 0.83f
17 bcf_callaux_t *bcf_call_init(double theta, int min_baseQ)
20 if (theta <= 0.) theta = CALL_DEFTHETA;
21 bca = calloc(1, sizeof(bcf_callaux_t));
23 bca->openQ = 40; bca->extQ = 20; bca->tandemQ = 100;
24 bca->min_baseQ = min_baseQ;
25 bca->e = errmod_init(1. - theta);
29 void bcf_call_destroy(bcf_callaux_t *bca)
32 errmod_destroy(bca->e);
33 free(bca->bases); free(bca->inscns); free(bca);
35 /* ref_base is the 4-bit representation of the reference base. It is
36 * negative if we are looking at an indel. */
37 int bcf_call_glfgen(int _n, const bam_pileup1_t *pl, int ref_base, bcf_callaux_t *bca, bcf_callret1_t *r)
39 int i, n, ref4, is_indel, ori_depth = 0;
40 memset(r, 0, sizeof(bcf_callret1_t));
42 ref4 = bam_nt16_nt4_table[ref_base];
44 } else ref4 = 4, is_indel = 1;
45 if (_n == 0) return -1;
46 // enlarge the bases array if necessary
47 if (bca->max_bases < _n) {
49 kroundup32(bca->max_bases);
50 bca->bases = (uint16_t*)realloc(bca->bases, 2 * bca->max_bases);
52 // fill the bases array
53 memset(r, 0, sizeof(bcf_callret1_t));
54 for (i = n = 0; i < _n; ++i) {
55 const bam_pileup1_t *p = pl + i;
56 int q, b, mapQ, baseQ, is_diff, min_dist, seqQ;
58 if (p->is_del || p->is_refskip || (p->b->core.flag&BAM_FUNMAP)) continue;
60 baseQ = q = is_indel? p->aux&0xff : (int)bam1_qual(p->b)[p->qpos]; // base/indel quality
61 seqQ = is_indel? (p->aux>>8&0xff) : 99;
62 if (q < bca->min_baseQ) continue;
63 if (q > seqQ) q = seqQ;
64 mapQ = p->b->core.qual < bca->capQ? p->b->core.qual : bca->capQ;
65 if (q > mapQ) q = mapQ;
69 b = bam1_seqi(bam1_seq(p->b), p->qpos); // base
70 b = bam_nt16_nt4_table[b? b : ref_base]; // b is the 2-bit base
71 is_diff = (ref4 < 4 && b == ref4)? 0 : 1;
76 bca->bases[n++] = q<<5 | (int)bam1_strand(p->b)<<4 | b;
77 // collect annotations
79 ++r->anno[0<<2|is_diff<<1|bam1_strand(p->b)];
80 min_dist = p->b->core.l_qseq - 1 - p->qpos;
81 if (min_dist > p->qpos) min_dist = p->qpos;
82 if (min_dist > CAP_DIST) min_dist = CAP_DIST;
83 r->anno[1<<2|is_diff<<1|0] += baseQ;
84 r->anno[1<<2|is_diff<<1|1] += baseQ * baseQ;
85 r->anno[2<<2|is_diff<<1|0] += mapQ;
86 r->anno[2<<2|is_diff<<1|1] += mapQ * mapQ;
87 r->anno[3<<2|is_diff<<1|0] += min_dist;
88 r->anno[3<<2|is_diff<<1|1] += min_dist * min_dist;
90 r->depth = n; r->ori_depth = ori_depth;
92 errmod_cal(bca->e, n, 5, bca->bases, r->p);
96 int bcf_call_combine(int n, const bcf_callret1_t *calls, int ref_base /*4-bit*/, bcf_call_t *call)
98 int ref4, i, j, qsum[4];
101 call->ori_ref = ref4 = bam_nt16_nt4_table[ref_base];
102 if (ref4 > 4) ref4 = 4;
103 } else call->ori_ref = -1, ref4 = 0;
105 memset(qsum, 0, 4 * sizeof(int));
106 for (i = 0; i < n; ++i)
107 for (j = 0; j < 4; ++j)
108 qsum[j] += calls[i].qsum[j];
109 for (j = 0; j < 4; ++j) qsum[j] = qsum[j] << 2 | j;
110 // find the top 2 alleles
111 for (i = 1; i < 4; ++i) // insertion sort
112 for (j = i; j > 0 && qsum[j] < qsum[j-1]; --j)
113 tmp = qsum[j], qsum[j] = qsum[j-1], qsum[j-1] = tmp;
114 // set the reference allele and alternative allele(s)
115 for (i = 0; i < 5; ++i) call->a[i] = -1;
118 for (i = 3, j = 1; i >= 0; --i) {
119 if ((qsum[i]&3) != ref4) {
120 if (qsum[i]>>2 != 0) call->a[j++] = qsum[i]&3;
124 if (ref_base >= 0) { // for SNPs, find the "unseen" base
125 if (((ref4 < 4 && j < 4) || (ref4 == 4 && j < 5)) && i >= 0)
126 call->unseen = j, call->a[j++] = qsum[i]&3;
130 if (call->n_alleles == 1) return -1; // no reliable supporting read. stop doing anything
135 call->PL = realloc(call->PL, 15 * n);
140 x = call->n_alleles * (call->n_alleles + 1) / 2;
141 // get the possible genotypes
142 for (i = z = 0; i < call->n_alleles; ++i)
143 for (j = i; j < call->n_alleles; ++j)
144 g[z++] = call->a[i] * 5 + call->a[j];
145 for (i = 0; i < n; ++i) {
146 uint8_t *PL = call->PL + x * i;
147 const bcf_callret1_t *r = calls + i;
149 for (j = 0; j < x; ++j)
150 if (min > r->p[g[j]]) min = r->p[g[j]];
152 for (j = 0; j < x; ++j) {
154 y = (int)(r->p[g[j]] - min + .499);
155 if (y > 255) y = 255;
159 // if (ref_base < 0) fprintf(stderr, "%d,%d,%f,%d\n", call->n_alleles, x, sum_min, call->unseen);
160 call->shift = (int)(sum_min + .499);
162 // combine annotations
163 memset(call->anno, 0, 16 * sizeof(int));
164 for (i = call->depth = call->ori_depth = 0, tmp = 0; i < n; ++i) {
165 call->depth += calls[i].depth;
166 call->ori_depth += calls[i].ori_depth;
167 for (j = 0; j < 16; ++j) call->anno[j] += calls[i].anno[j];
172 int bcf_call2bcf(int tid, int pos, bcf_call_t *bc, bcf1_t *b, bcf_callret1_t *bcr, int is_SP,
173 const bcf_callaux_t *bca, const char *ref)
175 extern double kt_fisher_exact(int n11, int n12, int n21, int n22, double *_left, double *_right, double *two);
179 b->tid = tid; b->pos = pos; b->qual = 0;
180 s.s = b->str; s.m = b->m_str; s.l = 0;
182 if (bc->ori_ref < 0) { // an indel
185 for (j = 0; j < bca->indelreg; ++j) kputc(ref[pos+1+j], &s);
189 for (i = 1; i < 4; ++i) {
190 if (bc->a[i] < 0) break;
192 kputc(',', &s); kputc(ref[pos], &s);
194 if (bca->indel_types[bc->a[i]] < 0) { // deletion
195 for (j = -bca->indel_types[bc->a[i]]; j < bca->indelreg; ++j)
196 kputc(ref[pos+1+j], &s);
197 } else { // insertion; cannot be a reference unless a bug
198 char *inscns = &bca->inscns[bc->a[i] * bca->maxins];
199 for (j = 0; j < bca->indel_types[bc->a[i]]; ++j)
200 kputc("ACGTN"[(int)inscns[j]], &s);
201 for (j = 0; j < bca->indelreg; ++j) kputc(ref[pos+1+j], &s);
206 kputc("ACGTN"[bc->ori_ref], &s); kputc('\0', &s);
207 for (i = 1; i < 5; ++i) {
208 if (bc->a[i] < 0) break;
209 if (i > 1) kputc(',', &s);
210 kputc(bc->unseen == i? 'X' : "ACGT"[bc->a[i]], &s);
216 if (bc->ori_ref < 0) kputs("INDEL;", &s);
217 kputs("DP=", &s); kputw(bc->ori_depth, &s); kputs(";I16=", &s);
218 for (i = 0; i < 16; ++i) {
219 if (i) kputc(',', &s);
220 kputw(bc->anno[i], &s);
227 if (is_SP) kputs(":SP", &s);
230 b->m_str = s.m; b->str = s.s; b->l_str = s.l;
232 memcpy(b->gi[0].data, bc->PL, b->gi[0].len * bc->n);
234 uint16_t *dp = (uint16_t*)b->gi[1].data;
235 uint8_t *sp = is_SP? b->gi[2].data : 0;
236 for (i = 0; i < bc->n; ++i) {
237 bcf_callret1_t *p = bcr + i;
238 dp[i] = p->depth < 0xffff? p->depth : 0xffff;
240 if (p->anno[0] + p->anno[1] < 2 || p->anno[2] + p->anno[3] < 2
241 || p->anno[0] + p->anno[2] < 2 || p->anno[1] + p->anno[3] < 2)
245 double left, right, two;
247 kt_fisher_exact(p->anno[0], p->anno[1], p->anno[2], p->anno[3], &left, &right, &two);
248 x = (int)(-4.343 * log(two) + .499);
249 if (x > 255) x = 255;