Debian Codegraph @

bcftools/em.c

raw symbols SCIP
1 /* em.c -- mathematical functions.
2
3 Copyright (C) 2010, 2011 Broad Institute.
4 Portions copyright (C) 2013-2014 Genome Research Ltd.
5
6 Author: Heng Li <[email protected]>
7
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 THE SOFTWARE. */
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <math.h>
29 #include <htslib/vcf.h>
30 #include "kmin.h"
31 #include "call.h"
32
33 #define ITER_MAX 50
34 #define ITER_TRY 10
35 #define EPS 1e-5
36
37 extern double kf_gammaq(double, double);
38
39 /*
40 Generic routines
41 */
42
43 // estimate site allele frequency in a very naive and inaccurate way
44 static double est_freq(int n, const double *pdg)
45 {
46 int i, gcnt[3], tmp1;
47 // get a rough estimate of the genotype frequency
48 gcnt[0] = gcnt[1] = gcnt[2] = 0;
49 for (i = 0; i < n; ++i) {
50 const double *p = pdg + i * 3;
51 if (p[0] != 1. || p[1] != 1. || p[2] != 1.) {
52 int which = p[0] > p[1]? 0 : 1;
53 which = p[which] > p[2]? which : 2;
54 ++gcnt[which];
55 }
56 }
57 tmp1 = gcnt[0] + gcnt[1] + gcnt[2];
58 return (tmp1 == 0)? -1.0 : (.5 * gcnt[1] + gcnt[2]) / tmp1;
59 }
60
61 /*
62 Single-locus EM
63 */
64
65 typedef struct {
66 int beg, end;
67 const double *pdg;
68 } minaux1_t;
69
70 static double prob1(double f, void *data)
71 {
72 minaux1_t *a = (minaux1_t*)data;
73 double p = 1., l = 0., f3[3];
74 int i;
75 // printf("brent %lg\n", f);
76 if (f < 0 || f > 1) return 1e300;
77 f3[0] = (1.-f)*(1.-f); f3[1] = 2.*f*(1.-f); f3[2] = f*f;
78 for (i = a->beg; i < a->end; ++i) {
79 const double *pdg = a->pdg + i * 3;
80 p *= pdg[0] * f3[0] + pdg[1] * f3[1] + pdg[2] * f3[2];
81 if (p < 1e-200) l -= log(p), p = 1.;
82 }
83 return l - log(p);
84 }
85
86 // one EM iteration for allele frequency estimate
87 static double freq_iter(double *f, const double *_pdg, int beg, int end)
88 {
89 double f0 = *f, f3[3], err;
90 int i;
91 // printf("em %lg\n", *f);
92 f3[0] = (1.-f0)*(1.-f0); f3[1] = 2.*f0*(1.-f0); f3[2] = f0*f0;
93 for (i = beg, f0 = 0.; i < end; ++i) {
94 const double *pdg = _pdg + i * 3;
95 f0 += (pdg[1] * f3[1] + 2. * pdg[2] * f3[2])
96 / (pdg[0] * f3[0] + pdg[1] * f3[1] + pdg[2] * f3[2]);
97 }
98 f0 /= (end - beg) * 2;
99 err = fabs(f0 - *f);
100 *f = f0;
101 return err;
102 }
103
104 /* The following function combines EM and Brent's method. When the signal from
105 * the data is strong, EM is faster but sometimes, EM may converge very slowly.
106 * When this happens, we switch to Brent's method. The idea is learned from
107 * Rasmus Nielsen.
108 */
109 static double freqml(double f0, int beg, int end, const double *pdg)
110 {
111 int i;
112 double f;
113 for (i = 0, f = f0; i < ITER_TRY; ++i)
114 if (freq_iter(&f, pdg, beg, end) < EPS) break;
115 if (i == ITER_TRY) { // haven't converged yet; try Brent's method
116 minaux1_t a;
117 a.beg = beg; a.end = end; a.pdg = pdg;
118 kmin_brent(prob1, f0 == f? .5*f0 : f0, f, (void*)&a, EPS, &f);
119 }
120 return f;
121 }
122
123 // one EM iteration for genotype frequency estimate
124 static double g3_iter(double g[3], const double *_pdg, int beg, int end)
125 {
126 double err, gg[3];
127 int i;
128 gg[0] = gg[1] = gg[2] = 0.;
129 // printf("%lg,%lg,%lg\n", g[0], g[1], g[2]);
130 for (i = beg; i < end; ++i) {
131 double sum, tmp[3];
132 const double *pdg = _pdg + i * 3;
133 tmp[0] = pdg[0] * g[0]; tmp[1] = pdg[1] * g[1]; tmp[2] = pdg[2] * g[2];
134 sum = (tmp[0] + tmp[1] + tmp[2]) * (end - beg);
135 gg[0] += tmp[0] / sum; gg[1] += tmp[1] / sum; gg[2] += tmp[2] / sum;
136 }
137 err = fabs(gg[0] - g[0]) > fabs(gg[1] - g[1])? fabs(gg[0] - g[0]) : fabs(gg[1] - g[1]);
138 err = err > fabs(gg[2] - g[2])? err : fabs(gg[2] - g[2]);
139 g[0] = gg[0]; g[1] = gg[1]; g[2] = gg[2];
140 return err;
141 }
142
143 // perform likelihood ratio test
144 static double lk_ratio_test(int n, int n1, const double *pdg, double f3[3][3])
145 {
146 double r;
147 int i;
148 for (i = 0, r = 1.; i < n1; ++i) {
149 const double *p = pdg + i * 3;
150 r *= (p[0] * f3[1][0] + p[1] * f3[1][1] + p[2] * f3[1][2])
151 / (p[0] * f3[0][0] + p[1] * f3[0][1] + p[2] * f3[0][2]);
152 }
153 for (; i < n; ++i) {
154 const double *p = pdg + i * 3;
155 r *= (p[0] * f3[2][0] + p[1] * f3[2][1] + p[2] * f3[2][2])
156 / (p[0] * f3[0][0] + p[1] * f3[0][1] + p[2] * f3[0][2]);
157 }
158 return r;
159 }
160
161 // x[0]: ref frequency
162 // x[1..3]: alt-alt, alt-ref, ref-ref frequenc
163 // x[4]: HWE P-value
164 // x[5..6]: group1 freq, group2 freq
165 // x[7]: 1-degree P-value
166 // x[8]: 2-degree P-value
167 int bcf_em1(call_t *call, const bcf1_t *rec, int n1, int flag, double x[10])
168 {
169 double *pdg;
170 int i, n; //, n2;
171 if (rec->n_allele < 2) return -1; // one allele only
172 // initialization
173 if (n1 < 0 || n1 > rec->n_sample) n1 = 0;
174 if (flag & 1<<7) flag |= 7<<5; // compute group freq if LRT is required
175 if (flag & 0xf<<1) flag |= 0xf<<1;
176 n = rec->n_sample; //n2 = n - n1;
177 pdg = call->pdg;
178 if (pdg == 0) return -1;
179 for (i = 0; i < 10; ++i) x[i] = -1.; // set to negative
180 {
181 if ((x[0] = est_freq(n, pdg)) < 0.) return -1; // no data
182 x[0] = freqml(x[0], 0, n, pdg);
183 }
184 if (flag & (0xf<<1|3<<8)) { // estimate the genotype frequency and test HWE
185 double *g = x + 1, f3[3], r;
186 f3[0] = g[0] = (1 - x[0]) * (1 - x[0]);
187 f3[1] = g[1] = 2 * x[0] * (1 - x[0]);
188 f3[2] = g[2] = x[0] * x[0];
189 for (i = 0; i < ITER_MAX; ++i)
190 if (g3_iter(g, pdg, 0, n) < EPS) break;
191 // Hardy-Weinberg equilibrium (HWE)
192 for (i = 0, r = 1.; i < n; ++i) {
193 double *p = pdg + i * 3;
194 r *= (p[0] * g[0] + p[1] * g[1] + p[2] * g[2]) / (p[0] * f3[0] + p[1] * f3[1] + p[2] * f3[2]);
195 }
196 x[4] = kf_gammaq(.5, log(r));
197 }
198 if ((flag & 7<<5) && n1 > 0 && n1 < n) { // group frequency
199 x[5] = freqml(x[0], 0, n1, pdg);
200 x[6] = freqml(x[0], n1, n, pdg);
201 }
202 if ((flag & 1<<7) && n1 > 0 && n1 < n) { // 1-degree P-value
203 double f[3], f3[3][3], tmp;
204 f[0] = x[0]; f[1] = x[5]; f[2] = x[6];
205 for (i = 0; i < 3; ++i)
206 f3[i][0] = (1-f[i])*(1-f[i]), f3[i][1] = 2*f[i]*(1-f[i]), f3[i][2] = f[i]*f[i];
207 tmp = log(lk_ratio_test(n, n1, pdg, f3));
208 if (tmp < 0) tmp = 0;
209 x[7] = kf_gammaq(.5, tmp);
210 }
211 if ((flag & 3<<8) && n1 > 0 && n1 < n) { // 2-degree P-value
212 double g[3][3], tmp;
213 for (i = 0; i < 3; ++i) memcpy(g[i], x + 1, 3 * sizeof(double));
214 for (i = 0; i < ITER_MAX; ++i)
215 if (g3_iter(g[1], pdg, 0, n1) < EPS) break;
216 for (i = 0; i < ITER_MAX; ++i)
217 if (g3_iter(g[2], pdg, n1, n) < EPS) break;
218 tmp = log(lk_ratio_test(n, n1, pdg, g));
219 if (tmp < 0) tmp = 0;
220 x[8] = kf_gammaq(1., tmp);
221 }
222 return 0;
223 }
224
225 /*
226 Two-locus EM (LD)
227 */
228
229 #define _G1(h, k) ((h>>1&1) + (k>>1&1))
230 #define _G2(h, k) ((h&1) + (k&1))
231
232 #if 0
233 // 0: the previous site; 1: the current site
234 static int pair_freq_iter(int n, double *pdg[2], double f[4])
235 {
236 double ff[4];
237 int i, k, h;
238 // printf("%lf,%lf,%lf,%lf\n", f[0], f[1], f[2], f[3]);
239 memset(ff, 0, 4 * sizeof(double));
240 for (i = 0; i < n; ++i) {
241 double *p[2], sum, tmp;
242 p[0] = pdg[0] + i * 3; p[1] = pdg[1] + i * 3;
243 for (k = 0, sum = 0.; k < 4; ++k)
244 for (h = 0; h < 4; ++h)
245 sum += f[k] * f[h] * p[0][_G1(k,h)] * p[1][_G2(k,h)];
246 for (k = 0; k < 4; ++k) {
247 tmp = f[0] * (p[0][_G1(0,k)] * p[1][_G2(0,k)] + p[0][_G1(k,0)] * p[1][_G2(k,0)])
248 + f[1] * (p[0][_G1(1,k)] * p[1][_G2(1,k)] + p[0][_G1(k,1)] * p[1][_G2(k,1)])
249 + f[2] * (p[0][_G1(2,k)] * p[1][_G2(2,k)] + p[0][_G1(k,2)] * p[1][_G2(k,2)])
250 + f[3] * (p[0][_G1(3,k)] * p[1][_G2(3,k)] + p[0][_G1(k,3)] * p[1][_G2(k,3)]);
251 ff[k] += f[k] * tmp / sum;
252 }
253 }
254 for (k = 0; k < 4; ++k) f[k] = ff[k] / (2 * n);
255 return 0;
256 }
257 #endif
258
259