annotate PsiCLASS-1.0.2/samtools-0.1.19/bcftools/em.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 <stdlib.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2 #include <string.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3 #include <math.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4 #include "bcf.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5 #include "kmin.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7 static double g_q2p[256];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9 #define ITER_MAX 50
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10 #define ITER_TRY 10
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11 #define EPS 1e-5
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13 extern double kf_gammaq(double, double);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15 /*
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16 Generic routines
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18 // get the 3 genotype likelihoods
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19 static double *get_pdg3(const bcf1_t *b)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 double *pdg;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22 const uint8_t *PL = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23 int i, PL_len = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 // initialize g_q2p if necessary
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25 if (g_q2p[0] == 0.)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26 for (i = 0; i < 256; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27 g_q2p[i] = pow(10., -i / 10.);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28 // set PL and PL_len
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 for (i = 0; i < b->n_gi; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30 if (b->gi[i].fmt == bcf_str2int("PL", 2)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31 PL = (const uint8_t*)b->gi[i].data;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32 PL_len = b->gi[i].len;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33 break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36 if (i == b->n_gi) return 0; // no PL
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37 // fill pdg
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38 pdg = malloc(3 * b->n_smpl * sizeof(double));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39 for (i = 0; i < b->n_smpl; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40 const uint8_t *pi = PL + i * PL_len;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41 double *p = pdg + i * 3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42 p[0] = g_q2p[pi[2]]; p[1] = g_q2p[pi[1]]; p[2] = g_q2p[pi[0]];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 return pdg;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47 // estimate site allele frequency in a very naive and inaccurate way
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48 static double est_freq(int n, const double *pdg)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50 int i, gcnt[3], tmp1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51 // get a rough estimate of the genotype frequency
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52 gcnt[0] = gcnt[1] = gcnt[2] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 for (i = 0; i < n; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54 const double *p = pdg + i * 3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 if (p[0] != 1. || p[1] != 1. || p[2] != 1.) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56 int which = p[0] > p[1]? 0 : 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57 which = p[which] > p[2]? which : 2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58 ++gcnt[which];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61 tmp1 = gcnt[0] + gcnt[1] + gcnt[2];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62 return (tmp1 == 0)? -1.0 : (.5 * gcnt[1] + gcnt[2]) / tmp1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65 /*
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66 Single-locus EM
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 typedef struct {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70 int beg, end;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71 const double *pdg;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72 } minaux1_t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 static double prob1(double f, void *data)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 minaux1_t *a = (minaux1_t*)data;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77 double p = 1., l = 0., f3[3];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78 int i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79 // printf("brent %lg\n", f);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80 if (f < 0 || f > 1) return 1e300;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81 f3[0] = (1.-f)*(1.-f); f3[1] = 2.*f*(1.-f); f3[2] = f*f;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82 for (i = a->beg; i < a->end; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83 const double *pdg = a->pdg + i * 3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84 p *= pdg[0] * f3[0] + pdg[1] * f3[1] + pdg[2] * f3[2];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85 if (p < 1e-200) l -= log(p), p = 1.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 return l - log(p);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90 // one EM iteration for allele frequency estimate
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91 static double freq_iter(double *f, const double *_pdg, int beg, int end)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93 double f0 = *f, f3[3], err;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94 int i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95 // printf("em %lg\n", *f);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96 f3[0] = (1.-f0)*(1.-f0); f3[1] = 2.*f0*(1.-f0); f3[2] = f0*f0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
97 for (i = beg, f0 = 0.; i < end; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
98 const double *pdg = _pdg + i * 3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
99 f0 += (pdg[1] * f3[1] + 2. * pdg[2] * f3[2])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
100 / (pdg[0] * f3[0] + pdg[1] * f3[1] + pdg[2] * f3[2]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
101 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
102 f0 /= (end - beg) * 2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
103 err = fabs(f0 - *f);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
104 *f = f0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
105 return err;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
106 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
107
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
108 /* The following function combines EM and Brent's method. When the signal from
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
109 * the data is strong, EM is faster but sometimes, EM may converge very slowly.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
110 * When this happens, we switch to Brent's method. The idea is learned from
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
111 * Rasmus Nielsen.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
112 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
113 static double freqml(double f0, int beg, int end, const double *pdg)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
114 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
115 int i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
116 double f;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
117 for (i = 0, f = f0; i < ITER_TRY; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
118 if (freq_iter(&f, pdg, beg, end) < EPS) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
119 if (i == ITER_TRY) { // haven't converged yet; try Brent's method
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
120 minaux1_t a;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
121 a.beg = beg; a.end = end; a.pdg = pdg;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
122 kmin_brent(prob1, f0 == f? .5*f0 : f0, f, (void*)&a, EPS, &f);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
123 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
124 return f;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
125 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
126
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
127 // one EM iteration for genotype frequency estimate
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
128 static double g3_iter(double g[3], const double *_pdg, int beg, int end)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
129 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
130 double err, gg[3];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
131 int i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
132 gg[0] = gg[1] = gg[2] = 0.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
133 // printf("%lg,%lg,%lg\n", g[0], g[1], g[2]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
134 for (i = beg; i < end; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
135 double sum, tmp[3];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
136 const double *pdg = _pdg + i * 3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
137 tmp[0] = pdg[0] * g[0]; tmp[1] = pdg[1] * g[1]; tmp[2] = pdg[2] * g[2];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
138 sum = (tmp[0] + tmp[1] + tmp[2]) * (end - beg);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
139 gg[0] += tmp[0] / sum; gg[1] += tmp[1] / sum; gg[2] += tmp[2] / sum;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
140 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
141 err = fabs(gg[0] - g[0]) > fabs(gg[1] - g[1])? fabs(gg[0] - g[0]) : fabs(gg[1] - g[1]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
142 err = err > fabs(gg[2] - g[2])? err : fabs(gg[2] - g[2]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
143 g[0] = gg[0]; g[1] = gg[1]; g[2] = gg[2];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
144 return err;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
145 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
146
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
147 // perform likelihood ratio test
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
148 static double lk_ratio_test(int n, int n1, const double *pdg, double f3[3][3])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
149 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
150 double r;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
151 int i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
152 for (i = 0, r = 1.; i < n1; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
153 const double *p = pdg + i * 3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
154 r *= (p[0] * f3[1][0] + p[1] * f3[1][1] + p[2] * f3[1][2])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
155 / (p[0] * f3[0][0] + p[1] * f3[0][1] + p[2] * f3[0][2]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
156 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
157 for (; i < n; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
158 const double *p = pdg + i * 3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
159 r *= (p[0] * f3[2][0] + p[1] * f3[2][1] + p[2] * f3[2][2])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
160 / (p[0] * f3[0][0] + p[1] * f3[0][1] + p[2] * f3[0][2]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
161 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
162 return r;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
163 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
164
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
165 // x[0]: ref frequency
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
166 // x[1..3]: alt-alt, alt-ref, ref-ref frequenc
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
167 // x[4]: HWE P-value
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
168 // x[5..6]: group1 freq, group2 freq
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
169 // x[7]: 1-degree P-value
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
170 // x[8]: 2-degree P-value
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
171 int bcf_em1(const bcf1_t *b, int n1, int flag, double x[10])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
172 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
173 double *pdg;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
174 int i, n, n2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
175 if (b->n_alleles < 2) return -1; // one allele only
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
176 // initialization
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
177 if (n1 < 0 || n1 > b->n_smpl) n1 = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
178 if (flag & 1<<7) flag |= 7<<5; // compute group freq if LRT is required
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
179 if (flag & 0xf<<1) flag |= 0xf<<1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
180 n = b->n_smpl; n2 = n - n1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
181 pdg = get_pdg3(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
182 if (pdg == 0) return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
183 for (i = 0; i < 10; ++i) x[i] = -1.; // set to negative
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
184 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
185 if ((x[0] = est_freq(n, pdg)) < 0.) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
186 free(pdg);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
187 return -1; // no data
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
188 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
189 x[0] = freqml(x[0], 0, n, pdg);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
190 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
191 if (flag & (0xf<<1|3<<8)) { // estimate the genotype frequency and test HWE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
192 double *g = x + 1, f3[3], r;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
193 f3[0] = g[0] = (1 - x[0]) * (1 - x[0]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
194 f3[1] = g[1] = 2 * x[0] * (1 - x[0]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
195 f3[2] = g[2] = x[0] * x[0];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
196 for (i = 0; i < ITER_MAX; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
197 if (g3_iter(g, pdg, 0, n) < EPS) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
198 // Hardy-Weinberg equilibrium (HWE)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
199 for (i = 0, r = 1.; i < n; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
200 double *p = pdg + i * 3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
201 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]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
202 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
203 x[4] = kf_gammaq(.5, log(r));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
204 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
205 if ((flag & 7<<5) && n1 > 0 && n1 < n) { // group frequency
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
206 x[5] = freqml(x[0], 0, n1, pdg);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
207 x[6] = freqml(x[0], n1, n, pdg);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
208 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
209 if ((flag & 1<<7) && n1 > 0 && n1 < n) { // 1-degree P-value
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
210 double f[3], f3[3][3], tmp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
211 f[0] = x[0]; f[1] = x[5]; f[2] = x[6];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
212 for (i = 0; i < 3; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
213 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];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
214 tmp = log(lk_ratio_test(n, n1, pdg, f3));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
215 if (tmp < 0) tmp = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
216 x[7] = kf_gammaq(.5, tmp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
217 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
218 if ((flag & 3<<8) && n1 > 0 && n1 < n) { // 2-degree P-value
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
219 double g[3][3], tmp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
220 for (i = 0; i < 3; ++i) memcpy(g[i], x + 1, 3 * sizeof(double));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
221 for (i = 0; i < ITER_MAX; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
222 if (g3_iter(g[1], pdg, 0, n1) < EPS) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
223 for (i = 0; i < ITER_MAX; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
224 if (g3_iter(g[2], pdg, n1, n) < EPS) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
225 tmp = log(lk_ratio_test(n, n1, pdg, g));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
226 if (tmp < 0) tmp = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
227 x[8] = kf_gammaq(1., tmp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
228 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
229 // free
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
230 free(pdg);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
231 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
232 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
233
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
234 /*
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
235 Two-locus EM (LD)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
236 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
237
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
238 #define _G1(h, k) ((h>>1&1) + (k>>1&1))
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
239 #define _G2(h, k) ((h&1) + (k&1))
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
240
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
241 // 0: the previous site; 1: the current site
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
242 static int pair_freq_iter(int n, double *pdg[2], double f[4])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
243 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
244 double ff[4];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
245 int i, k, h;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
246 // printf("%lf,%lf,%lf,%lf\n", f[0], f[1], f[2], f[3]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
247 memset(ff, 0, 4 * sizeof(double));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
248 for (i = 0; i < n; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
249 double *p[2], sum, tmp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
250 p[0] = pdg[0] + i * 3; p[1] = pdg[1] + i * 3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
251 for (k = 0, sum = 0.; k < 4; ++k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
252 for (h = 0; h < 4; ++h)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
253 sum += f[k] * f[h] * p[0][_G1(k,h)] * p[1][_G2(k,h)];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
254 for (k = 0; k < 4; ++k) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
255 tmp = f[0] * (p[0][_G1(0,k)] * p[1][_G2(0,k)] + p[0][_G1(k,0)] * p[1][_G2(k,0)])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
256 + f[1] * (p[0][_G1(1,k)] * p[1][_G2(1,k)] + p[0][_G1(k,1)] * p[1][_G2(k,1)])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
257 + f[2] * (p[0][_G1(2,k)] * p[1][_G2(2,k)] + p[0][_G1(k,2)] * p[1][_G2(k,2)])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
258 + f[3] * (p[0][_G1(3,k)] * p[1][_G2(3,k)] + p[0][_G1(k,3)] * p[1][_G2(k,3)]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
259 ff[k] += f[k] * tmp / sum;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
260 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
261 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
262 for (k = 0; k < 4; ++k) f[k] = ff[k] / (2 * n);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
263 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
264 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
265
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
266 double bcf_pair_freq(const bcf1_t *b0, const bcf1_t *b1, double f[4])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
267 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
268 const bcf1_t *b[2];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
269 int i, j, n_smpl;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
270 double *pdg[2], flast[4], r, f0[2];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
271 // initialize others
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
272 if (b0->n_smpl != b1->n_smpl) return -1; // different number of samples
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
273 n_smpl = b0->n_smpl;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
274 b[0] = b0; b[1] = b1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
275 f[0] = f[1] = f[2] = f[3] = -1.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
276 if (b[0]->n_alleles < 2 || b[1]->n_alleles < 2) return -1; // one allele only
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
277 pdg[0] = get_pdg3(b0); pdg[1] = get_pdg3(b1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
278 if (pdg[0] == 0 || pdg[1] == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
279 free(pdg[0]); free(pdg[1]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
280 return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
281 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
282 // set the initial value
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
283 f0[0] = est_freq(n_smpl, pdg[0]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
284 f0[1] = est_freq(n_smpl, pdg[1]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
285 f[0] = (1 - f0[0]) * (1 - f0[1]); f[3] = f0[0] * f0[1];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
286 f[1] = (1 - f0[0]) * f0[1]; f[2] = f0[0] * (1 - f0[1]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
287 // iteration
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
288 for (j = 0; j < ITER_MAX; ++j) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
289 double eps = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
290 memcpy(flast, f, 4 * sizeof(double));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
291 pair_freq_iter(n_smpl, pdg, f);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
292 for (i = 0; i < 4; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
293 double x = fabs(f[i] - flast[i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
294 if (x > eps) eps = x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
295 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
296 if (eps < EPS) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
297 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
298 // free
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
299 free(pdg[0]); free(pdg[1]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
300 { // calculate r^2
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
301 double p[2], q[2], D;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
302 p[0] = f[0] + f[1]; q[0] = 1 - p[0];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
303 p[1] = f[0] + f[2]; q[1] = 1 - p[1];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
304 D = f[0] * f[3] - f[1] * f[2];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
305 r = sqrt(D * D / (p[0] * p[1] * q[0] * q[1]));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
306 // printf("R(%lf,%lf,%lf,%lf)=%lf\n", f[0], f[1], f[2], f[3], r);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
307 if (isnan(r)) r = -1.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
308 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
309 return r;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
310 }