annotate pyPRADA_1.2/tools/samtools-0.1.16/bcftools/prob1.c @ 0:acc2ca1a3ba4

Uploaded
author siyuan
date Thu, 20 Feb 2014 00:44:58 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
1 #include <math.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
2 #include <stdlib.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
3 #include <string.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
4 #include <stdio.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
5 #include <errno.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
6 #include <assert.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
7 #include "prob1.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
8
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
9 #include "kseq.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
10 KSTREAM_INIT(gzFile, gzread, 16384)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
11
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
12 #define MC_MAX_EM_ITER 16
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
13 #define MC_EM_EPS 1e-5
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
14 #define MC_DEF_INDEL 0.15
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
15
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
16 unsigned char seq_nt4_table[256] = {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
17 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
18 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
19 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 /*'-'*/, 4, 4,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
20 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
21 4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
22 4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
23 4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
24 4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
25 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
26 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
27 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
28 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
29 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
30 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
31 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
32 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
33 };
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
34
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
35 struct __bcf_p1aux_t {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
36 int n, M, n1, is_indel;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
37 uint8_t *ploidy; // haploid or diploid ONLY
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
38 double *q2p, *pdg; // pdg -> P(D|g)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
39 double *phi, *phi_indel;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
40 double *z, *zswap; // aux for afs
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
41 double *z1, *z2, *phi1, *phi2; // only calculated when n1 is set
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
42 double **hg; // hypergeometric distribution
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
43 double *lf; // log factorial
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
44 double t, t1, t2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
45 double *afs, *afs1; // afs: accumulative AFS; afs1: site posterior distribution
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
46 const uint8_t *PL; // point to PL
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
47 int PL_len;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
48 };
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
49
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
50 void bcf_p1_indel_prior(bcf_p1aux_t *ma, double x)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
51 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
52 int i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
53 for (i = 0; i < ma->M; ++i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
54 ma->phi_indel[i] = ma->phi[i] * x;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
55 ma->phi_indel[ma->M] = 1. - ma->phi[ma->M] * x;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
56 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
57
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
58 static void init_prior(int type, double theta, int M, double *phi)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
59 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
60 int i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
61 if (type == MC_PTYPE_COND2) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
62 for (i = 0; i <= M; ++i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
63 phi[i] = 2. * (i + 1) / (M + 1) / (M + 2);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
64 } else if (type == MC_PTYPE_FLAT) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
65 for (i = 0; i <= M; ++i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
66 phi[i] = 1. / (M + 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
67 } else {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
68 double sum;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
69 for (i = 0, sum = 0.; i < M; ++i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
70 sum += (phi[i] = theta / (M - i));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
71 phi[M] = 1. - sum;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
72 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
73 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
74
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
75 void bcf_p1_init_prior(bcf_p1aux_t *ma, int type, double theta)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
76 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
77 init_prior(type, theta, ma->M, ma->phi);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
78 bcf_p1_indel_prior(ma, MC_DEF_INDEL);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
79 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
80
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
81 void bcf_p1_init_subprior(bcf_p1aux_t *ma, int type, double theta)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
82 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
83 if (ma->n1 <= 0 || ma->n1 >= ma->M) return;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
84 init_prior(type, theta, 2*ma->n1, ma->phi1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
85 init_prior(type, theta, 2*(ma->n - ma->n1), ma->phi2);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
86 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
87
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
88 int bcf_p1_read_prior(bcf_p1aux_t *ma, const char *fn)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
89 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
90 gzFile fp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
91 kstring_t s;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
92 kstream_t *ks;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
93 long double sum;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
94 int dret, k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
95 memset(&s, 0, sizeof(kstring_t));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
96 fp = strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
97 ks = ks_init(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
98 memset(ma->phi, 0, sizeof(double) * (ma->M + 1));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
99 while (ks_getuntil(ks, '\n', &s, &dret) >= 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
100 if (strstr(s.s, "[afs] ") == s.s) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
101 char *p = s.s + 6;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
102 for (k = 0; k <= ma->M; ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
103 int x;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
104 double y;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
105 x = strtol(p, &p, 10);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
106 if (x != k && (errno == EINVAL || errno == ERANGE)) return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
107 ++p;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
108 y = strtod(p, &p);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
109 if (y == 0. && (errno == EINVAL || errno == ERANGE)) return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
110 ma->phi[ma->M - k] += y;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
111 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
112 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
113 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
114 ks_destroy(ks);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
115 gzclose(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
116 free(s.s);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
117 for (sum = 0., k = 0; k <= ma->M; ++k) sum += ma->phi[k];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
118 fprintf(stderr, "[prior]");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
119 for (k = 0; k <= ma->M; ++k) ma->phi[k] /= sum;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
120 for (k = 0; k <= ma->M; ++k) fprintf(stderr, " %d:%.3lg", k, ma->phi[ma->M - k]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
121 fputc('\n', stderr);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
122 for (sum = 0., k = 1; k < ma->M; ++k) sum += ma->phi[ma->M - k] * (2.* k * (ma->M - k) / ma->M / (ma->M - 1));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
123 fprintf(stderr, "[%s] heterozygosity=%lf, ", __func__, (double)sum);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
124 for (sum = 0., k = 1; k <= ma->M; ++k) sum += k * ma->phi[ma->M - k] / ma->M;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
125 fprintf(stderr, "theta=%lf\n", (double)sum);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
126 bcf_p1_indel_prior(ma, MC_DEF_INDEL);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
127 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
128 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
129
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
130 bcf_p1aux_t *bcf_p1_init(int n, uint8_t *ploidy)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
131 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
132 bcf_p1aux_t *ma;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
133 int i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
134 ma = calloc(1, sizeof(bcf_p1aux_t));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
135 ma->n1 = -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
136 ma->n = n; ma->M = 2 * n;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
137 if (ploidy) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
138 ma->ploidy = malloc(n);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
139 memcpy(ma->ploidy, ploidy, n);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
140 for (i = 0, ma->M = 0; i < n; ++i) ma->M += ploidy[i];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
141 if (ma->M == 2 * n) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
142 free(ma->ploidy);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
143 ma->ploidy = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
144 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
145 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
146 ma->q2p = calloc(256, sizeof(double));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
147 ma->pdg = calloc(3 * ma->n, sizeof(double));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
148 ma->phi = calloc(ma->M + 1, sizeof(double));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
149 ma->phi_indel = calloc(ma->M + 1, sizeof(double));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
150 ma->phi1 = calloc(ma->M + 1, sizeof(double));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
151 ma->phi2 = calloc(ma->M + 1, sizeof(double));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
152 ma->z = calloc(ma->M + 1, sizeof(double));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
153 ma->zswap = calloc(ma->M + 1, sizeof(double));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
154 ma->z1 = calloc(ma->M + 1, sizeof(double)); // actually we do not need this large
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
155 ma->z2 = calloc(ma->M + 1, sizeof(double));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
156 ma->afs = calloc(ma->M + 1, sizeof(double));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
157 ma->afs1 = calloc(ma->M + 1, sizeof(double));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
158 ma->lf = calloc(ma->M + 1, sizeof(double));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
159 for (i = 0; i < 256; ++i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
160 ma->q2p[i] = pow(10., -i / 10.);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
161 for (i = 0; i <= ma->M; ++i) ma->lf[i] = lgamma(i + 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
162 bcf_p1_init_prior(ma, MC_PTYPE_FULL, 1e-3); // the simplest prior
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
163 return ma;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
164 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
165
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
166 int bcf_p1_set_n1(bcf_p1aux_t *b, int n1)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
167 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
168 if (n1 == 0 || n1 >= b->n) return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
169 if (b->M != b->n * 2) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
170 fprintf(stderr, "[%s] unable to set `n1' when there are haploid samples.\n", __func__);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
171 return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
172 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
173 b->n1 = n1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
174 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
175 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
176
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
177 void bcf_p1_destroy(bcf_p1aux_t *ma)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
178 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
179 if (ma) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
180 int k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
181 free(ma->lf);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
182 if (ma->hg && ma->n1 > 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
183 for (k = 0; k <= 2*ma->n1; ++k) free(ma->hg[k]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
184 free(ma->hg);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
185 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
186 free(ma->ploidy); free(ma->q2p); free(ma->pdg);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
187 free(ma->phi); free(ma->phi_indel); free(ma->phi1); free(ma->phi2);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
188 free(ma->z); free(ma->zswap); free(ma->z1); free(ma->z2);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
189 free(ma->afs); free(ma->afs1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
190 free(ma);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
191 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
192 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
193
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
194 static int cal_pdg(const bcf1_t *b, bcf_p1aux_t *ma)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
195 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
196 int i, j;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
197 long *p, tmp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
198 p = alloca(b->n_alleles * sizeof(long));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
199 memset(p, 0, sizeof(long) * b->n_alleles);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
200 for (j = 0; j < ma->n; ++j) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
201 const uint8_t *pi = ma->PL + j * ma->PL_len;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
202 double *pdg = ma->pdg + j * 3;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
203 pdg[0] = ma->q2p[pi[2]]; pdg[1] = ma->q2p[pi[1]]; pdg[2] = ma->q2p[pi[0]];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
204 for (i = 0; i < b->n_alleles; ++i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
205 p[i] += (int)pi[(i+1)*(i+2)/2-1];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
206 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
207 for (i = 0; i < b->n_alleles; ++i) p[i] = p[i]<<4 | i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
208 for (i = 1; i < b->n_alleles; ++i) // insertion sort
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
209 for (j = i; j > 0 && p[j] < p[j-1]; --j)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
210 tmp = p[j], p[j] = p[j-1], p[j-1] = tmp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
211 for (i = b->n_alleles - 1; i >= 0; --i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
212 if ((p[i]&0xf) == 0) break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
213 return i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
214 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
215
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
216 int bcf_p1_call_gt(const bcf_p1aux_t *ma, double f0, int k)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
217 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
218 double sum, g[3];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
219 double max, f3[3], *pdg = ma->pdg + k * 3;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
220 int q, i, max_i, ploidy;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
221 ploidy = ma->ploidy? ma->ploidy[k] : 2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
222 if (ploidy == 2) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
223 f3[0] = (1.-f0)*(1.-f0); f3[1] = 2.*f0*(1.-f0); f3[2] = f0*f0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
224 } else {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
225 f3[0] = 1. - f0; f3[1] = 0; f3[2] = f0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
226 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
227 for (i = 0, sum = 0.; i < 3; ++i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
228 sum += (g[i] = pdg[i] * f3[i]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
229 for (i = 0, max = -1., max_i = 0; i < 3; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
230 g[i] /= sum;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
231 if (g[i] > max) max = g[i], max_i = i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
232 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
233 max = 1. - max;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
234 if (max < 1e-308) max = 1e-308;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
235 q = (int)(-4.343 * log(max) + .499);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
236 if (q > 99) q = 99;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
237 return q<<2|max_i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
238 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
239
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
240 #define TINY 1e-20
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
241
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
242 static void mc_cal_y_core(bcf_p1aux_t *ma, int beg)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
243 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
244 double *z[2], *tmp, *pdg;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
245 int _j, last_min, last_max;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
246 assert(beg == 0 || ma->M == ma->n*2);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
247 z[0] = ma->z;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
248 z[1] = ma->zswap;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
249 pdg = ma->pdg;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
250 memset(z[0], 0, sizeof(double) * (ma->M + 1));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
251 memset(z[1], 0, sizeof(double) * (ma->M + 1));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
252 z[0][0] = 1.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
253 last_min = last_max = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
254 ma->t = 0.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
255 if (ma->M == ma->n * 2) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
256 int M = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
257 for (_j = beg; _j < ma->n; ++_j) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
258 int k, j = _j - beg, _min = last_min, _max = last_max, M0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
259 double p[3], sum;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
260 M0 = M; M += 2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
261 pdg = ma->pdg + _j * 3;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
262 p[0] = pdg[0]; p[1] = 2. * pdg[1]; p[2] = pdg[2];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
263 for (; _min < _max && z[0][_min] < TINY; ++_min) z[0][_min] = z[1][_min] = 0.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
264 for (; _max > _min && z[0][_max] < TINY; --_max) z[0][_max] = z[1][_max] = 0.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
265 _max += 2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
266 if (_min == 0) k = 0, z[1][k] = (M0-k+1) * (M0-k+2) * p[0] * z[0][k];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
267 if (_min <= 1) k = 1, z[1][k] = (M0-k+1) * (M0-k+2) * p[0] * z[0][k] + k*(M0-k+2) * p[1] * z[0][k-1];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
268 for (k = _min < 2? 2 : _min; k <= _max; ++k)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
269 z[1][k] = (M0-k+1)*(M0-k+2) * p[0] * z[0][k] + k*(M0-k+2) * p[1] * z[0][k-1] + k*(k-1)* p[2] * z[0][k-2];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
270 for (k = _min, sum = 0.; k <= _max; ++k) sum += z[1][k];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
271 ma->t += log(sum / (M * (M - 1.)));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
272 for (k = _min; k <= _max; ++k) z[1][k] /= sum;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
273 if (_min >= 1) z[1][_min-1] = 0.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
274 if (_min >= 2) z[1][_min-2] = 0.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
275 if (j < ma->n - 1) z[1][_max+1] = z[1][_max+2] = 0.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
276 if (_j == ma->n1 - 1) { // set pop1; ma->n1==-1 when unset
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
277 ma->t1 = ma->t;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
278 memcpy(ma->z1, z[1], sizeof(double) * (ma->n1 * 2 + 1));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
279 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
280 tmp = z[0]; z[0] = z[1]; z[1] = tmp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
281 last_min = _min; last_max = _max;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
282 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
283 //for (_j = 0; _j < last_min; ++_j) z[0][_j] = 0.; // TODO: are these necessary?
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
284 //for (_j = last_max + 1; _j < ma->M; ++_j) z[0][_j] = 0.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
285 } else { // this block is very similar to the block above; these two might be merged in future
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
286 int j, M = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
287 for (j = 0; j < ma->n; ++j) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
288 int k, M0, _min = last_min, _max = last_max;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
289 double p[3], sum;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
290 pdg = ma->pdg + j * 3;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
291 for (; _min < _max && z[0][_min] < TINY; ++_min) z[0][_min] = z[1][_min] = 0.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
292 for (; _max > _min && z[0][_max] < TINY; --_max) z[0][_max] = z[1][_max] = 0.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
293 M0 = M;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
294 M += ma->ploidy[j];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
295 if (ma->ploidy[j] == 1) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
296 p[0] = pdg[0]; p[1] = pdg[2];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
297 _max++;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
298 if (_min == 0) k = 0, z[1][k] = (M0+1-k) * p[0] * z[0][k];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
299 for (k = _min < 1? 1 : _min; k <= _max; ++k)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
300 z[1][k] = (M0+1-k) * p[0] * z[0][k] + k * p[1] * z[0][k-1];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
301 for (k = _min, sum = 0.; k <= _max; ++k) sum += z[1][k];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
302 ma->t += log(sum / M);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
303 for (k = _min; k <= _max; ++k) z[1][k] /= sum;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
304 if (_min >= 1) z[1][_min-1] = 0.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
305 if (j < ma->n - 1) z[1][_max+1] = 0.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
306 } else if (ma->ploidy[j] == 2) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
307 p[0] = pdg[0]; p[1] = 2 * pdg[1]; p[2] = pdg[2];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
308 _max += 2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
309 if (_min == 0) k = 0, z[1][k] = (M0-k+1) * (M0-k+2) * p[0] * z[0][k];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
310 if (_min <= 1) k = 1, z[1][k] = (M0-k+1) * (M0-k+2) * p[0] * z[0][k] + k*(M0-k+2) * p[1] * z[0][k-1];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
311 for (k = _min < 2? 2 : _min; k <= _max; ++k)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
312 z[1][k] = (M0-k+1)*(M0-k+2) * p[0] * z[0][k] + k*(M0-k+2) * p[1] * z[0][k-1] + k*(k-1)* p[2] * z[0][k-2];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
313 for (k = _min, sum = 0.; k <= _max; ++k) sum += z[1][k];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
314 ma->t += log(sum / (M * (M - 1.)));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
315 for (k = _min; k <= _max; ++k) z[1][k] /= sum;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
316 if (_min >= 1) z[1][_min-1] = 0.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
317 if (_min >= 2) z[1][_min-2] = 0.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
318 if (j < ma->n - 1) z[1][_max+1] = z[1][_max+2] = 0.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
319 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
320 tmp = z[0]; z[0] = z[1]; z[1] = tmp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
321 last_min = _min; last_max = _max;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
322 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
323 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
324 if (z[0] != ma->z) memcpy(ma->z, z[0], sizeof(double) * (ma->M + 1));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
325 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
326
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
327 static void mc_cal_y(bcf_p1aux_t *ma)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
328 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
329 if (ma->n1 > 0 && ma->n1 < ma->n && ma->M == ma->n * 2) { // NB: ma->n1 is ineffective when there are haploid samples
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
330 int k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
331 long double x;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
332 memset(ma->z1, 0, sizeof(double) * (2 * ma->n1 + 1));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
333 memset(ma->z2, 0, sizeof(double) * (2 * (ma->n - ma->n1) + 1));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
334 ma->t1 = ma->t2 = 0.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
335 mc_cal_y_core(ma, ma->n1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
336 ma->t2 = ma->t;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
337 memcpy(ma->z2, ma->z, sizeof(double) * (2 * (ma->n - ma->n1) + 1));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
338 mc_cal_y_core(ma, 0);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
339 // rescale z
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
340 x = expl(ma->t - (ma->t1 + ma->t2));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
341 for (k = 0; k <= ma->M; ++k) ma->z[k] *= x;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
342 } else mc_cal_y_core(ma, 0);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
343 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
344
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
345 #define CONTRAST_TINY 1e-30
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
346
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
347 extern double kf_gammaq(double s, double z); // incomplete gamma function for chi^2 test
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
348
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
349 static inline double chi2_test(int a, int b, int c, int d)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
350 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
351 double x, z;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
352 x = (double)(a+b) * (c+d) * (b+d) * (a+c);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
353 if (x == 0.) return 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
354 z = a * d - b * c;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
355 return kf_gammaq(.5, .5 * z * z * (a+b+c+d) / x);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
356 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
357
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
358 // chi2=(a+b+c+d)(ad-bc)^2/[(a+b)(c+d)(a+c)(b+d)]
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
359 static inline double contrast2_aux(const bcf_p1aux_t *p1, double sum, int k1, int k2, double x[3])
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
360 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
361 double p = p1->phi[k1+k2] * p1->z1[k1] * p1->z2[k2] / sum * p1->hg[k1][k2];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
362 int n1 = p1->n1, n2 = p1->n - p1->n1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
363 if (p < CONTRAST_TINY) return -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
364 if (.5*k1/n1 < .5*k2/n2) x[1] += p;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
365 else if (.5*k1/n1 > .5*k2/n2) x[2] += p;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
366 else x[0] += p;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
367 return p * chi2_test(k1, k2, (n1<<1) - k1, (n2<<1) - k2);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
368 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
369
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
370 static double contrast2(bcf_p1aux_t *p1, double ret[3])
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
371 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
372 int k, k1, k2, k10, k20, n1, n2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
373 double sum;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
374 // get n1 and n2
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
375 n1 = p1->n1; n2 = p1->n - p1->n1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
376 if (n1 <= 0 || n2 <= 0) return 0.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
377 if (p1->hg == 0) { // initialize the hypergeometric distribution
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
378 /* NB: the hg matrix may take a lot of memory when there are many samples. There is a way
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
379 to avoid precomputing this matrix, but it is slower and quite intricate. The following
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
380 computation in this block can be accelerated with a similar strategy, but perhaps this
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
381 is not a serious concern for now. */
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
382 double tmp = lgamma(2*(n1+n2)+1) - (lgamma(2*n1+1) + lgamma(2*n2+1));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
383 p1->hg = calloc(2*n1+1, sizeof(void*));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
384 for (k1 = 0; k1 <= 2*n1; ++k1) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
385 p1->hg[k1] = calloc(2*n2+1, sizeof(double));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
386 for (k2 = 0; k2 <= 2*n2; ++k2)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
387 p1->hg[k1][k2] = exp(lgamma(k1+k2+1) + lgamma(p1->M-k1-k2+1) - (lgamma(k1+1) + lgamma(k2+1) + lgamma(2*n1-k1+1) + lgamma(2*n2-k2+1) + tmp));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
388 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
389 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
390 { // compute
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
391 long double suml = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
392 for (k = 0; k <= p1->M; ++k) suml += p1->phi[k] * p1->z[k];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
393 sum = suml;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
394 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
395 { // get the max k1 and k2
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
396 double max;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
397 int max_k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
398 for (k = 0, max = 0, max_k = -1; k <= 2*n1; ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
399 double x = p1->phi1[k] * p1->z1[k];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
400 if (x > max) max = x, max_k = k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
401 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
402 k10 = max_k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
403 for (k = 0, max = 0, max_k = -1; k <= 2*n2; ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
404 double x = p1->phi2[k] * p1->z2[k];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
405 if (x > max) max = x, max_k = k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
406 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
407 k20 = max_k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
408 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
409 { // We can do the following with one nested loop, but that is an O(N^2) thing. The following code block is much faster for large N.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
410 double x[3], y;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
411 long double z = 0., L[2];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
412 x[0] = x[1] = x[2] = 0; L[0] = L[1] = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
413 for (k1 = k10; k1 >= 0; --k1) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
414 for (k2 = k20; k2 >= 0; --k2) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
415 if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
416 else z += y;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
417 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
418 for (k2 = k20 + 1; k2 <= 2*n2; ++k2) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
419 if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
420 else z += y;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
421 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
422 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
423 ret[0] = x[0]; ret[1] = x[1]; ret[2] = x[2];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
424 x[0] = x[1] = x[2] = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
425 for (k1 = k10 + 1; k1 <= 2*n1; ++k1) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
426 for (k2 = k20; k2 >= 0; --k2) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
427 if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
428 else z += y;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
429 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
430 for (k2 = k20 + 1; k2 <= 2*n2; ++k2) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
431 if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
432 else z += y;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
433 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
434 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
435 ret[0] += x[0]; ret[1] += x[1]; ret[2] += x[2];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
436 if (ret[0] + ret[1] + ret[2] < 0.95) { // in case of bad things happened
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
437 ret[0] = ret[1] = ret[2] = 0; L[0] = L[1] = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
438 for (k1 = 0, z = 0.; k1 <= 2*n1; ++k1)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
439 for (k2 = 0; k2 <= 2*n2; ++k2)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
440 if ((y = contrast2_aux(p1, sum, k1, k2, ret)) >= 0) z += y;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
441 if (ret[0] + ret[1] + ret[2] < 0.95) // It seems that this may be caused by floating point errors. I do not really understand why...
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
442 z = 1.0, ret[0] = ret[1] = ret[2] = 1./3;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
443 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
444 return (double)z;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
445 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
446 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
447
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
448 static double mc_cal_afs(bcf_p1aux_t *ma, double *p_ref_folded, double *p_var_folded)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
449 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
450 int k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
451 long double sum = 0., sum2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
452 double *phi = ma->is_indel? ma->phi_indel : ma->phi;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
453 memset(ma->afs1, 0, sizeof(double) * (ma->M + 1));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
454 mc_cal_y(ma);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
455 // compute AFS
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
456 for (k = 0, sum = 0.; k <= ma->M; ++k)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
457 sum += (long double)phi[k] * ma->z[k];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
458 for (k = 0; k <= ma->M; ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
459 ma->afs1[k] = phi[k] * ma->z[k] / sum;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
460 if (isnan(ma->afs1[k]) || isinf(ma->afs1[k])) return -1.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
461 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
462 // compute folded variant probability
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
463 for (k = 0, sum = 0.; k <= ma->M; ++k)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
464 sum += (long double)(phi[k] + phi[ma->M - k]) / 2. * ma->z[k];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
465 for (k = 1, sum2 = 0.; k < ma->M; ++k)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
466 sum2 += (long double)(phi[k] + phi[ma->M - k]) / 2. * ma->z[k];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
467 *p_var_folded = sum2 / sum;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
468 *p_ref_folded = (phi[k] + phi[ma->M - k]) / 2. * (ma->z[ma->M] + ma->z[0]) / sum;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
469 // the expected frequency
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
470 for (k = 0, sum = 0.; k <= ma->M; ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
471 ma->afs[k] += ma->afs1[k];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
472 sum += k * ma->afs1[k];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
473 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
474 return sum / ma->M;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
475 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
476
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
477 int bcf_p1_cal(const bcf1_t *b, int do_contrast, bcf_p1aux_t *ma, bcf_p1rst_t *rst)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
478 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
479 int i, k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
480 long double sum = 0.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
481 ma->is_indel = bcf_is_indel(b);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
482 rst->perm_rank = -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
483 // set PL and PL_len
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
484 for (i = 0; i < b->n_gi; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
485 if (b->gi[i].fmt == bcf_str2int("PL", 2)) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
486 ma->PL = (uint8_t*)b->gi[i].data;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
487 ma->PL_len = b->gi[i].len;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
488 break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
489 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
490 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
491 if (i == b->n_gi) return -1; // no PL
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
492 if (b->n_alleles < 2) return -1; // FIXME: find a better solution
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
493 //
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
494 rst->rank0 = cal_pdg(b, ma);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
495 rst->f_exp = mc_cal_afs(ma, &rst->p_ref_folded, &rst->p_var_folded);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
496 rst->p_ref = ma->afs1[ma->M];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
497 for (k = 0, sum = 0.; k < ma->M; ++k)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
498 sum += ma->afs1[k];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
499 rst->p_var = (double)sum;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
500 // calculate f_flat and f_em
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
501 for (k = 0, sum = 0.; k <= ma->M; ++k)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
502 sum += (long double)ma->z[k];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
503 rst->f_flat = 0.;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
504 for (k = 0; k <= ma->M; ++k) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
505 double p = ma->z[k] / sum;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
506 rst->f_flat += k * p;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
507 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
508 rst->f_flat /= ma->M;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
509 { // estimate equal-tail credible interval (95% level)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
510 int l, h;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
511 double p;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
512 for (i = 0, p = 0.; i < ma->M; ++i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
513 if (p + ma->afs1[i] > 0.025) break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
514 else p += ma->afs1[i];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
515 l = i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
516 for (i = ma->M-1, p = 0.; i >= 0; --i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
517 if (p + ma->afs1[i] > 0.025) break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
518 else p += ma->afs1[i];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
519 h = i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
520 rst->cil = (double)(ma->M - h) / ma->M; rst->cih = (double)(ma->M - l) / ma->M;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
521 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
522 rst->cmp[0] = rst->cmp[1] = rst->cmp[2] = rst->p_chi2 = -1.0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
523 if (do_contrast && rst->p_var > 0.5) // skip contrast2() if the locus is a strong non-variant
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
524 rst->p_chi2 = contrast2(ma, rst->cmp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
525 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
526 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
527
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
528 void bcf_p1_dump_afs(bcf_p1aux_t *ma)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
529 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
530 int k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
531 fprintf(stderr, "[afs]");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
532 for (k = 0; k <= ma->M; ++k)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
533 fprintf(stderr, " %d:%.3lf", k, ma->afs[ma->M - k]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
534 fprintf(stderr, "\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
535 memset(ma->afs, 0, sizeof(double) * (ma->M + 1));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
536 }