Debian Codegraph @

bcftools/em.c.pysam.c

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