-
Notifications
You must be signed in to change notification settings - Fork 4
/
estimate_ld.c
433 lines (375 loc) · 10.4 KB
/
estimate_ld.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "estimate_ld.h"
/*
* Excoffier-Slatkin EM algorithm for estimating haplotype frequencies.
* This code implements the special case of the algorithm for two
* biallelic loci. With two biallelic loci, there are 4 types of
* gamete, which I represent as follows:
*
* AB Ab aB ab
* 0 1 2 3
*
* Here A and a are the two alleles at the first locus and B and
* b are the alleles at the other locus. The numbers below the
* gamete symbols are used below as indexes into arrays.
*
* Phenotypes at the 1st locus: AA, Aa, and aa are numbered 0, 1, and 2.
*
* Phenotypes at the 2nd locus: BB, Bb, and bb are numbered 0, 1, and 2.
*
* Input:
*
* hh is a vector of 4 doubles, into which results will be put
*
* h is a vector of 4 haplotype frequencies, indexed as shown above.
*
* x is a 3X3 matrix of phenotype counts. The phenotypes are coded
* as explained above. Thus, x[1][2] is the number of copies of
* the phenotype Aa/BB.
*
* n is the sample size and should equal the sum of x.
*
* Function returns a revised estimate of h after a single EM step.
*/
void esem_step(double *hh, double *h, int x[][3]) {
/*
* g is a 4X4 matrix of genotype frequencies. g[0][3]
* is the frequency of the genotype that combines gamete 0 (AB)
* with gamete 3 (ab).
*
* p is a 3X3 matrix of phenotype frequencies, recoded as
* described for the input matrix x.
*/
double g[4][4], p[3][3], n;
int i, j;
/* Set only lower triangle of g. Upper triangle unused. */
for(i=0; i<4; ++i) {
g[i][i] = h[i]*h[i];
for(j=0; j<i; ++j) {
g[i][j] = 2*h[i]*h[j];
}
}
p[0][0] = g[0][0];
p[0][1] = g[1][0];
p[0][2] = g[1][1];
p[1][0] = g[2][0];
p[1][1] = g[3][0]+g[2][1];
p[1][2] = g[3][1];
p[2][0] = g[2][2];
p[2][1] = g[3][2];
p[2][2] = g[3][3];
hh[0] = 2*x[0][0] + x[0][1] + x[1][0] + x[1][1]*g[3][0]/p[1][1];
hh[1] = x[0][1] + 2*x[0][2] + x[1][1]*g[2][1]/p[1][1] + x[1][2];
hh[2] = x[1][0] + x[1][1]*g[2][1]/p[1][1] + 2*x[2][0] + x[2][1];
hh[3] = x[1][1]*g[3][0]/p[1][1] + x[1][2] + x[2][1] + 2*x[2][2];
/* convert gamete counts counts to relative frequencies*/
n = hh[0]+hh[1]+hh[2]+hh[3];
hh[0] /= n;
hh[1] /= n;
hh[2] /= n;
hh[3] /= n;
return;
}
/*
* On return x[a][b] is the number of copies of the genotype for
* which Y[i]=a and Z[i] = b.
*/
void count_genotypes(int x[][3], int len, const int *Y, const int *Z) {
register int i;
memset(&(x[0][0]), 0, 9*sizeof(x[0][0]));
for(i=0; i<len; ++i) {
#ifndef NDEBUG
if(!(Y[i]>=0 && Y[i]<=2))
printf("BAD Y[%d]: %d len=%d\n", i, Y[i], len);
if(!(Z[i]>=0 && Z[i]<=2))
printf("BAD Z[%d]: %d len=%d\n", i, Z[i], len);
#endif
assert(Y[i]>=0 && Y[i]<=2);
assert(Z[i]>=0 && Z[i]<=2);
x[Y[i]][Z[i]] += 1;
}
}
/*
* On return x[a][b] is the number of copies of the gamete for
* which y[i]=a and z[i] = b.
*/
void count_gametes(int x[][2], int len, const int *y, const int *z) {
register int i;
memset(&(x[0][0]), 0, 4*sizeof(x[0][0]));
for(i=0; i<len; ++i) {
assert(y[i]==0 || y[i]==1);
assert(z[i]==0 || z[i]==1);
x[y[i]][z[i]] += 1;
}
}
/*
* Excoffier-Slatkin EM algorithm for estimating haplotype frequencies.
* Input:
*
* Y is vector of genotype values at 1st locus, coded as 0, 1, and 2
* to represent genotypes aa, aA, and AA.
*
* Z is the corresponding vector for 2nd locus.
*
* h holds haplotype frequencies. Initialize these as you please.
* On return, h will contain the frequencies estimated by EM.
*
* tol controls convergence. Algorithm stops when sum of absolute
* differences between new and old haplotype frequencies is <= tol.
*
* Function returns 0 on success; -1 on failure.
*/
int esem(int len, const int *Y, const int *Z, double *h, const double tol,
const int max_itr) {
int i, j;
int x[3][3];
double dh=1.0, hh[4];
count_genotypes(x, len, Y, Z);
for(i=0; i<max_itr; ++i) {
esem_step(hh, h, x);
dh = 0.0;
for(j=0; j<4; ++j) {
dh += fabs(h[j]-hh[j]);
h[j] = hh[j];
}
if(dh <= tol)
break;
}
#ifndef NDEBUG
if(!__finite(dh)) {
printf("NOT FINITE in esem: dh=%g\n", dh);
for(i=0; i<3; i++)
printf("%4d %4d %4d\n", x[i][0], x[i][1], x[i][2]);
}
#endif
if( dh > tol)
return -1;
return 0;
}
/*
* Use Excoffier-Slatkin EM algorithm to estimate r.
* Input:
*
* Y is vector of genotype values at 1st locus, coded as 0, 1, and 2
* to represent genotypes aa, aA, and AA.
*
* Z is the corresponding vector for 2nd locus.
*
* Function sets *r; returns 0 on success and -1 on failure.
*
*/
int esem_r(double *result, int len, const int *Y, const int *Z) {
/*
* tol controls convergence. Algorithm stops when sum of absolute
* differences between new and old haplotype frequencies is <= tol.
*
* max_itr is the maximum number of EM steps.
*
* h is the vector of haplotype frequencies
*
* hh holds the haplotype frequencies estimated by esem.
*/
const double tol = 1e-7;
const int max_itr = 1000;
int rval;
double h[4], pA, pB, qA, qB, D, denom;
h[0]=h[1]=h[2]=h[3]=0.25;
rval = esem(len, Y, Z, h, tol, max_itr);
pA = h[0]+h[1];
pB = h[0]+h[2];
qA = 1.0 - pA;
qB = 1.0 - pB;
D = h[0]*h[3] - h[1]*h[2];
denom = pA*qA*pB*qB;
if(denom != 0)
*result = D/sqrt(denom);
else {
assert(D == 0);
*result = 0.0;
}
#ifndef NDEBUG
if(!__finite(*result)) {
printf("esem_r returning bad val\n");
printf("h=[%g,%g,%g,%g]\n", h[0], h[1], h[2], h[3]);
}
#endif
return rval;
}
/* Trick the optimizer */
void do_nothing(double x) {
}
/*
* Calculate r from two vectors of indicator variables.
* On input, xdata[i] = 1 if the ith gamete carries A a locus 1. It
* equals 0 otherwise. ydata[i] is defined similarly for allele B at
* the 2nd locus. This function is used only for debugging.
*/
double get_r_gamete(int len, int *xdata, int *ydata) {
register int i, iy, iz;
double h[] = {0.0, 0.0, 0.0, 0.0};
double pA, pB, D, denom;
/*
* Count gamete types.
* h[0] counts AB
* h[1] counts Ab
* h[2] counts aB
* h[3] counts ab
*/
for(i=0; i < len; ++i) {
iy = xdata[i];
iz = ydata[i];
++h[3 - 2*iy -iz];
}
D = h[0] + h[1] + h[2] + h[3];
h[0] /= D;
h[1] /= D;
h[2] /= D;
h[3] /= D;
pA = h[0] + h[1];
pB = h[0] + h[2];
D = h[0]*h[3] - h[1]*h[2];
denom = pA*(1-pA)*pB*(1-pB);
assert(denom >= 0.0);
if(denom == 0)
return 0.0;
return D/sqrt(denom);
}
void print_gamete_table(FILE *f, int len, int *xdata, int *ydata) {
int i, x[2][2];
count_gametes(x, len, xdata, ydata);
fprintf(f, "%3s: %4d %4d\n", "x\\y", 0, 1);
for(i=0; i<2; ++i)
fprintf(f, "%3i: %4d %4d\n", i, x[i][0], x[i][1]);
return;
}
void print_genotype_table(FILE *f, int len, int *xdata, int *ydata) {
int i, x[3][3];
count_genotypes(x, len, xdata, ydata);
fprintf(f, "%3s: %4d %4d %4d\n", "x\\y", 0, 1, 2);
for(i=0; i<3; ++i)
fprintf(f, "%3i: %4d %4d %4d\n", i, x[i][0], x[i][1], x[i][2]);
return;
}
/*
* Calculate r from two data vectors. This function is used only for debugging.
*/
double get_r_corr(int len, int *xdata, int *ydata) {
double vx, vy, vxvy, cov, r, nsqr;
int sumx, sumy, sumxx, sumyy, sumxy;
register int i, n=0, x, y;
sumx = sumy = sumxx = sumyy = sumxy = 0;
for(i=0; i<len; ++i) {
x = xdata[i];
y = ydata[i];
#if 0
if(x==MISSING || y==MISSING)
continue;
#endif
n += 1;
sumx += x;
sumy += y;
sumxx += x*x;
sumyy += y*y;
sumxy += x*y;
}
/* Calculate numerators in integer arithmetic to
* avoid roundoff.
*/
nsqr = (double) n*(n-1);
cov = (n*sumxy - sumx*sumy)/nsqr;
vx = (n*sumxx - sumx*sumx)/nsqr;
vy = (n*sumyy - sumy*sumy)/nsqr;
vxvy = vx*vy;
if(vxvy != 0)
r = cov/sqrt(vxvy);
else {
assert(cov == 0);
r = 0.0;
}
return r;
}
/*
* Calculate r from two data vectors.
*/
double get_r_corr_genotype(int len, int *xdata, int *ydata) {
int count[3][3];
double vx, vy, cov, r, nsqr, vxvy;
int sumx, sumy, sumxx, sumyy, sumxy;
register int i, j;
if(len<2)
return 0;
count_genotypes(count, len, xdata, ydata);
sumx = sumy = sumxx = sumyy = sumxy = 0;
for(i=0; i<3; ++i) {
for(j=0; j<3; ++j) {
sumx += count[i][j]*i;
sumy += count[i][j]*j;
sumxx += count[i][j]*i*i;
sumyy += count[i][j]*j*j;
sumxy += count[i][j]*i*j;
}
}
/* Calculate numerators in integer arithmetic to
* avoid roundoff.
*/
nsqr = (double) len*(len-1);
cov = (len*sumxy - sumx*sumy)/nsqr;
vx = (len*sumxx - sumx*sumx)/nsqr;
vy = (len*sumyy - sumy*sumy)/nsqr;
vxvy = vx*vy;
if(vxvy != 0)
r = cov/sqrt(vxvy);
else {
assert(cov == 0);
r = 0.0;
}
return r;
}
/*
* Calculate D from r, pA, and pB.
*/
double r_to_D(double r, double pA, double pB) {
return r*sqrt(pA*(1.0-pA)*pB*(1.0-pB));
}
/*
* Find Dprime from D, pA, and pB.
*/
double D_to_Dprime(double D, double pA, double pB) {
double a;
const double qA=1.0-pA, qB=1.0-pB;
if (D < 0) {
/*a = fmax(-pA*pB, -qA*qB); */ /* usual definition */
a = -fmax(-pA*pB, -qA*qB); /* retain sign of D */
}else{
a = fmin(pA*qB, qA*pB);
}
return D/a;
}
/*
* Find Dprime from r, pA, and pB.
*/
double r_to_Dprime(double r, double pA, double pB) {
return D_to_Dprime(r_to_D(r, pA, pB), pA, pB);
}
/*
* Find D from Dprime, pA, and pB.
* Modified to allow the sign of Dprime to equal that of D.
*/
double Dprime_to_D(double Dp, double pA, double pB) {
const double qA = 1.0 - pA;
const double qB = 1.0 - pB;
double a;
if (Dp < 0) {
#if 0
a = fmax(-pA*pB, -qA*qB); /* for usual definition of Dp */
#else
a = -fmax(-pA*pB, -qA*qB); /* allow for negative Dp */
#endif
}else
a = fmin(pA*qB, qA*pB);
return Dp*a;
}