6 #define MC_MAX_SUMQ 3000
7 #define MC_MAX_SUMQP 1e-300
12 double *q2p, *pdg; // pdg -> P(D|g)
17 void mc_init_prior(mc_aux_t *ma, int type, double theta)
20 if (type == MC_PTYPE_COND2) {
21 for (i = 0; i <= 2 * ma->n; ++i)
22 ma->alpha[i] = 2. * (i + 1) / (2 * ma->n + 1) / (2 * ma->n + 2);
25 for (i = 0, sum = 0.; i < 2 * ma->n; ++i)
26 sum += (ma->alpha[i] = theta / (2 * ma->n - i));
27 ma->alpha[2 * ma->n] = 1. - sum;
31 mc_aux_t *mc_init(int n) // FIXME: assuming diploid
35 ma = calloc(1, sizeof(mc_aux_t));
36 ma->n = n; ma->N = 2 * n;
37 ma->q2p = calloc(MC_MAX_SUMQ + 1, sizeof(double));
38 ma->qsum = calloc(4 * ma->n, sizeof(int));
39 ma->bcnt = calloc(4 * ma->n, sizeof(int));
40 ma->pdg = calloc(3 * ma->n, sizeof(double));
41 ma->alpha = calloc(2 * ma->n + 1, sizeof(double));
42 ma->beta = calloc((2 * ma->n + 1) * 3, sizeof(double));
43 for (i = 0; i <= MC_MAX_SUMQ; ++i)
44 ma->q2p[i] = pow(10., -i / 10.);
45 for (i = 0; i <= 2 * ma->n; ++i) { // beta[k][g]=P(g|k/M)
46 double *bi = ma->beta + 3 * i;
47 double f = (double)i / (2 * ma->n);
48 bi[0] = (1. - f) * (1. - f);
49 bi[1] = 2 * f * (1. - f);
52 mc_init_prior(ma, MC_PTYPE_FULL, 1e-3); // the simplest prior
56 void mc_destroy(mc_aux_t *ma)
59 free(ma->qsum); free(ma->bcnt);
60 free(ma->q2p); free(ma->pdg);
61 free(ma->alpha); free(ma->beta);
66 static void sum_err(int *n, const bam_pileup1_t **plp, mc_aux_t *ma)
69 memset(ma->qsum, 0, sizeof(int) * 4 * ma->n);
70 memset(ma->bcnt, 0, sizeof(int) * 4 * ma->n);
71 for (j = 0; j < ma->n; ++j) {
72 int tmp, *qsum = ma->qsum + j * 4;
73 int *bcnt = ma->bcnt + j * 4;
74 for (i = tmp = 0; i < n[j]; ++i) {
75 const bam_pileup1_t *p = plp[j] + i;
77 if (p->is_del || (p->b->core.flag&BAM_FUNMAP)) continue;
78 q = bam1_qual(p->b)[p->qpos];
79 if (p->b->core.qual < q) q = p->b->core.qual;
80 if (q < MC_MIN_QUAL) continue; // small qual
81 b = bam_nt16_nt4_table[(int)bam1_seqi(bam1_seq(p->b), p->qpos)];
82 if (b > 3) continue; // N
90 static void set_allele(int ref, mc_aux_t *ma)
92 int i, j, sum[4], tmp;
93 sum[0] = sum[1] = sum[2] = sum[3] = 0;
94 for (i = 0; i < ma->n; ++i)
95 for (j = 0; j < 4; ++j)
96 sum[j] += ma->qsum[i * 4 + j];
97 for (j = 0; j < 4; ++j) sum[j] = sum[j]<<2 | j;
98 for (i = 1; i < 4; ++i) // insertion sort
99 for (j = i; j > 0 && sum[j] < sum[j-1]; --j)
100 tmp = sum[j], sum[j] = sum[j-1], sum[j-1] = tmp;
101 ma->ref = sum[3]&3; ma->alt = sum[2]&3;
102 if (ref == ma->alt) tmp = ma->ref, ma->ref = ma->alt, ma->alt = tmp;
103 // note that ma->ref might not be ref in case of triallele
106 static void cal_pdg(mc_aux_t *ma)
109 for (j = 0; j < ma->n; ++j) {
110 int pi[3], *qsum, *bcnt;
111 double *pdg = ma->pdg + j * 3;
112 qsum = ma->qsum + j * 4;
113 bcnt = ma->bcnt + j * 4;
114 pi[1] = 3 * (bcnt[ma->ref] + bcnt[ma->alt]);
115 pi[0] = qsum[ma->ref];
116 pi[2] = qsum[ma->alt];
117 for (i = 0; i < 3; ++i)
118 pdg[i] = pi[i] > MC_MAX_SUMQ? MC_MAX_SUMQP : ma->q2p[pi[i]];
121 // return the reference allele frequency
122 double mc_freq0(int ref, int *n, const bam_pileup1_t **plp, mc_aux_t *ma, int *_ref, int *alt)
129 *_ref = ma->ref; *alt = ma->alt;
130 for (i = cnt = 0, f = 0.; i < ma->n; ++i) {
131 int *bcnt = ma->bcnt + i * 4;
132 int x = bcnt[ma->ref] + bcnt[ma->alt];
135 f += (double)bcnt[ma->ref] / x;
140 // f0 is the reference allele frequency
141 double mc_freq_iter(double f0, mc_aux_t *ma)
145 f3[0] = (1.-f0)*(1.-f0); f3[1] = 2.*f0*(1.-f0); f3[2] = f0*f0;
146 for (i = 0, f = 0.; i < ma->n; ++i) {
148 pdg = ma->pdg + i * 3;
149 for (j = 1, up = 0.; j < 3; ++j)
150 up += j * pdg[j] * f3[j];
151 for (j = 0, dn = 0.; j < 3; ++j)
152 dn += pdg[j] * f3[j];
159 double mc_freq_post(mc_aux_t *ma)
163 for (i = 0; i < ma->n; ++i) {
164 double *pdg = ma->pdg + i * 3;
165 long double y = 0., z = 0.;
166 for (k = 0; k <= ma->n * 2; ++k) {
167 double *bk = ma->beta + k * 3;
170 double yk = 0., zk = 0.;
171 for (g = 0; g < 3; ++g) {
172 yk += g * pdg[g] * bk[g];
173 zk += pdg[g] * bk[g];
175 y += yk * ma->alpha[k];
176 z += zk * ma->alpha[k];
178 y += (pdg[1] * bk[1] + 2. * pdg[2] * bk[2]) * ma->alpha[k];
179 z += (pdg[0] * bk[0] + pdg[1] * bk[1] + pdg[2] * bk[2]) * ma->alpha[k];
183 return f / ma->n / 2;
186 double mc_ref_prob(mc_aux_t *ma)
189 long double PD = 0., Pref = 0.;
190 for (k = 0; k <= ma->n * 2; ++k) {
192 double *bk = ma->beta + k * 3;
193 for (i = 0; i < ma->n; ++i) {
194 double *pdg = ma->pdg + i * 3;
195 // int g; double y=0.; for (g = 0; g < 3; ++g) y += pdg[g] * bk[g];
196 x *= pdg[0] * bk[0] + pdg[1] * bk[1] + pdg[2] * bk[2];
198 PD += x * ma->alpha[k];
200 for (k = 0; k <= ma->n * 2; ++k) {
202 for (i = 0; i < ma->n; ++i)
203 x *= ma->pdg[i * 3 + 2] * ma->beta[k * 3 + 2];
204 Pref += x * ma->alpha[k];
209 int mc_call_gt(const mc_aux_t *ma, double f0, int k)
212 double max, f3[3], *pdg = ma->pdg + k * 3;
214 f3[0] = (1.-f0)*(1.-f0); f3[1] = 2.*f0*(1.-f0); f3[2] = f0*f0;
215 for (i = 0, sum = 0.; i < 3; ++i)
216 sum += (g[i] = pdg[i] * f3[i]);
217 for (i = 0, max = -1., max_i = 0; i < 3; ++i) {
219 if (g[i] > max) max = g[i], max_i = i;
221 // printf("***%lg,%lg,%lg,%lg,%lg,%lg\n", g[0], g[1], g[2], pdg[0], pdg[1], pdg[2]);
223 if (max < 1e-308) max = 1e-308;
224 q = (int)(-3.434 * log(max) + .499);