annotate PsiCLASS-1.0.2/samtools-0.1.19/bcftools/prob1.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 <math.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2 #include <stdlib.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3 #include <string.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4 #include <stdio.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5 #include <errno.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6 #include <assert.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7 #include <limits.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8 #include <zlib.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9 #include "prob1.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10 #include "kstring.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12 #include "kseq.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13 KSTREAM_INIT(gzFile, gzread, 16384)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15 #define MC_MAX_EM_ITER 16
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16 #define MC_EM_EPS 1e-5
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17 #define MC_DEF_INDEL 0.15
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19 gzFile bcf_p1_fp_lk;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 unsigned char seq_nt4_table[256] = {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 /*'-'*/, 4, 4,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26 4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27 4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28 4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38 };
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40 struct __bcf_p1aux_t {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41 int n, M, n1, is_indel;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42 uint8_t *ploidy; // haploid or diploid ONLY
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 double *q2p, *pdg; // pdg -> P(D|g)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 double *phi, *phi_indel;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45 double *z, *zswap; // aux for afs
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 double *z1, *z2, *phi1, *phi2; // only calculated when n1 is set
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47 double **hg; // hypergeometric distribution
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48 double *lf; // log factorial
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49 double t, t1, t2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50 double *afs, *afs1; // afs: accumulative AFS; afs1: site posterior distribution
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51 const uint8_t *PL; // point to PL
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52 int PL_len;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 };
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 void bcf_p1_indel_prior(bcf_p1aux_t *ma, double x)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57 int i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58 for (i = 0; i < ma->M; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59 ma->phi_indel[i] = ma->phi[i] * x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60 ma->phi_indel[ma->M] = 1. - ma->phi[ma->M] * x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63 static void init_prior(int type, double theta, int M, double *phi)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65 int i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66 if (type == MC_PTYPE_COND2) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67 for (i = 0; i <= M; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68 phi[i] = 2. * (i + 1) / (M + 1) / (M + 2);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 } else if (type == MC_PTYPE_FLAT) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70 for (i = 0; i <= M; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71 phi[i] = 1. / (M + 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73 double sum;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 for (i = 0, sum = 0.; i < M; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75 sum += (phi[i] = theta / (M - i));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 phi[M] = 1. - sum;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80 void bcf_p1_init_prior(bcf_p1aux_t *ma, int type, double theta)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82 init_prior(type, theta, ma->M, ma->phi);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83 bcf_p1_indel_prior(ma, MC_DEF_INDEL);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86 void bcf_p1_init_subprior(bcf_p1aux_t *ma, int type, double theta)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88 if (ma->n1 <= 0 || ma->n1 >= ma->M) return;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89 init_prior(type, theta, 2*ma->n1, ma->phi1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90 init_prior(type, theta, 2*(ma->n - ma->n1), ma->phi2);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93 int bcf_p1_read_prior(bcf_p1aux_t *ma, const char *fn)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95 gzFile fp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96 kstring_t s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
97 kstream_t *ks;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
98 long double sum;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
99 int dret, k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
100 memset(&s, 0, sizeof(kstring_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
101 fp = strcmp(fn, "-")? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
102 ks = ks_init(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
103 memset(ma->phi, 0, sizeof(double) * (ma->M + 1));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
104 while (ks_getuntil(ks, '\n', &s, &dret) >= 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
105 if (strstr(s.s, "[afs] ") == s.s) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
106 char *p = s.s + 6;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
107 for (k = 0; k <= ma->M; ++k) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
108 int x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
109 double y;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
110 x = strtol(p, &p, 10);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
111 if (x != k && (errno == EINVAL || errno == ERANGE)) return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
112 ++p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
113 y = strtod(p, &p);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
114 if (y == 0. && (errno == EINVAL || errno == ERANGE)) return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
115 ma->phi[ma->M - k] += y;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
116 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
117 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
118 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
119 ks_destroy(ks);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
120 gzclose(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
121 free(s.s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
122 for (sum = 0., k = 0; k <= ma->M; ++k) sum += ma->phi[k];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
123 fprintf(stderr, "[prior]");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
124 for (k = 0; k <= ma->M; ++k) ma->phi[k] /= sum;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
125 for (k = 0; k <= ma->M; ++k) fprintf(stderr, " %d:%.3lg", k, ma->phi[ma->M - k]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
126 fputc('\n', stderr);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
127 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));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
128 fprintf(stderr, "[%s] heterozygosity=%lf, ", __func__, (double)sum);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
129 for (sum = 0., k = 1; k <= ma->M; ++k) sum += k * ma->phi[ma->M - k] / ma->M;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
130 fprintf(stderr, "theta=%lf\n", (double)sum);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
131 bcf_p1_indel_prior(ma, MC_DEF_INDEL);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
132 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
133 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
134
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
135 bcf_p1aux_t *bcf_p1_init(int n, uint8_t *ploidy)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
136 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
137 bcf_p1aux_t *ma;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
138 int i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
139 ma = calloc(1, sizeof(bcf_p1aux_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
140 ma->n1 = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
141 ma->n = n; ma->M = 2 * n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
142 if (ploidy) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
143 ma->ploidy = malloc(n);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
144 memcpy(ma->ploidy, ploidy, n);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
145 for (i = 0, ma->M = 0; i < n; ++i) ma->M += ploidy[i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
146 if (ma->M == 2 * n) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
147 free(ma->ploidy);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
148 ma->ploidy = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
149 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
150 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
151 ma->q2p = calloc(256, sizeof(double));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
152 ma->pdg = calloc(3 * ma->n, sizeof(double));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
153 ma->phi = calloc(ma->M + 1, sizeof(double));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
154 ma->phi_indel = calloc(ma->M + 1, sizeof(double));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
155 ma->phi1 = calloc(ma->M + 1, sizeof(double));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
156 ma->phi2 = calloc(ma->M + 1, sizeof(double));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
157 ma->z = calloc(ma->M + 1, sizeof(double));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
158 ma->zswap = calloc(ma->M + 1, sizeof(double));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
159 ma->z1 = calloc(ma->M + 1, sizeof(double)); // actually we do not need this large
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
160 ma->z2 = calloc(ma->M + 1, sizeof(double));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
161 ma->afs = calloc(ma->M + 1, sizeof(double));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
162 ma->afs1 = calloc(ma->M + 1, sizeof(double));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
163 ma->lf = calloc(ma->M + 1, sizeof(double));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
164 for (i = 0; i < 256; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
165 ma->q2p[i] = pow(10., -i / 10.);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
166 for (i = 0; i <= ma->M; ++i) ma->lf[i] = lgamma(i + 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
167 bcf_p1_init_prior(ma, MC_PTYPE_FULL, 1e-3); // the simplest prior
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
168 return ma;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
169 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
170
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
171 int bcf_p1_get_M(bcf_p1aux_t *b) { return b->M; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
172
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
173 int bcf_p1_set_n1(bcf_p1aux_t *b, int n1)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
174 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
175 if (n1 == 0 || n1 >= b->n) return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
176 if (b->M != b->n * 2) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
177 fprintf(stderr, "[%s] unable to set `n1' when there are haploid samples.\n", __func__);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
178 return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
179 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
180 b->n1 = n1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
181 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
182 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
183
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
184 void bcf_p1_set_ploidy(bcf1_t *b, bcf_p1aux_t *ma)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
185 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
186 // bcf_p1aux_t fields are not visible outside of prob1.c, hence this wrapper.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
187 // Ideally, this should set ploidy per site to allow pseudo-autosomal regions
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
188 b->ploidy = ma->ploidy;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
189 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
190
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
191 void bcf_p1_destroy(bcf_p1aux_t *ma)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
192 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
193 if (ma) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
194 int k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
195 free(ma->lf);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
196 if (ma->hg && ma->n1 > 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
197 for (k = 0; k <= 2*ma->n1; ++k) free(ma->hg[k]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
198 free(ma->hg);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
199 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
200 free(ma->ploidy); free(ma->q2p); free(ma->pdg);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
201 free(ma->phi); free(ma->phi_indel); free(ma->phi1); free(ma->phi2);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
202 free(ma->z); free(ma->zswap); free(ma->z1); free(ma->z2);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
203 free(ma->afs); free(ma->afs1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
204 free(ma);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
205 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
206 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
207
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
208 extern double kf_gammap(double s, double z);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
209 int test16(bcf1_t *b, anno16_t *a);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
210
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
211 // Wigginton 2005, PMID: 15789306
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
212 // written by Jan Wigginton
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
213 double calc_hwe(int obs_hom1, int obs_hom2, int obs_hets)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
214 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
215 if (obs_hom1 + obs_hom2 + obs_hets == 0 ) return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
216
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
217 assert(obs_hom1 >= 0 && obs_hom2 >= 0 && obs_hets >= 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
218
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
219 int obs_homc = obs_hom1 < obs_hom2 ? obs_hom2 : obs_hom1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
220 int obs_homr = obs_hom1 < obs_hom2 ? obs_hom1 : obs_hom2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
221
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
222 int rare_copies = 2 * obs_homr + obs_hets;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
223 int genotypes = obs_hets + obs_homc + obs_homr;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
224
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
225 double *het_probs = (double*) calloc(rare_copies+1, sizeof(double));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
226
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
227 /* start at midpoint */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
228 int mid = rare_copies * (2 * genotypes - rare_copies) / (2 * genotypes);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
229
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
230 /* check to ensure that midpoint and rare alleles have same parity */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
231 if ((rare_copies & 1) ^ (mid & 1)) mid++;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
232
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
233 int curr_hets = mid;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
234 int curr_homr = (rare_copies - mid) / 2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
235 int curr_homc = genotypes - curr_hets - curr_homr;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
236
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
237 het_probs[mid] = 1.0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
238 double sum = het_probs[mid];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
239 for (curr_hets = mid; curr_hets > 1; curr_hets -= 2)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
240 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
241 het_probs[curr_hets - 2] = het_probs[curr_hets] * curr_hets * (curr_hets - 1.0) / (4.0 * (curr_homr + 1.0) * (curr_homc + 1.0));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
242 sum += het_probs[curr_hets - 2];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
243
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
244 /* 2 fewer heterozygotes for next iteration -> add one rare, one common homozygote */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
245 curr_homr++;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
246 curr_homc++;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
247 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
248
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
249 curr_hets = mid;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
250 curr_homr = (rare_copies - mid) / 2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
251 curr_homc = genotypes - curr_hets - curr_homr;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
252 for (curr_hets = mid; curr_hets <= rare_copies - 2; curr_hets += 2)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
253 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
254 het_probs[curr_hets + 2] = het_probs[curr_hets] * 4.0 * curr_homr * curr_homc /((curr_hets + 2.0) * (curr_hets + 1.0));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
255 sum += het_probs[curr_hets + 2];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
256
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
257 /* add 2 heterozygotes for next iteration -> subtract one rare, one common homozygote */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
258 curr_homr--;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
259 curr_homc--;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
260 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
261 int i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
262 for (i = 0; i <= rare_copies; i++) het_probs[i] /= sum;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
263
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
264 /* p-value calculation for p_hwe */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
265 double p_hwe = 0.0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
266 for (i = 0; i <= rare_copies; i++)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
267 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
268 if (het_probs[i] > het_probs[obs_hets])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
269 continue;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
270 p_hwe += het_probs[i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
271 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
272
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
273 p_hwe = p_hwe > 1.0 ? 1.0 : p_hwe;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
274 free(het_probs);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
275 return p_hwe;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
276
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
277 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
278
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
279
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
280 static void _bcf1_set_ref(bcf1_t *b, int idp)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
281 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
282 kstring_t s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
283 int old_n_gi = b->n_gi;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
284 s.m = b->m_str; s.l = b->l_str - 1; s.s = b->str;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
285 kputs(":GT", &s); kputc('\0', &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
286 b->m_str = s.m; b->l_str = s.l; b->str = s.s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
287 bcf_sync(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
288
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
289 // Call GTs
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
290 int isample, an = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
291 for (isample = 0; isample < b->n_smpl; isample++)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
292 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
293 if ( idp>=0 && ((uint16_t*)b->gi[idp].data)[isample]==0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
294 ((uint8_t*)b->gi[old_n_gi].data)[isample] = 1<<7;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
295 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
296 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
297 ((uint8_t*)b->gi[old_n_gi].data)[isample] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
298 an += b->ploidy ? b->ploidy[isample] : 2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
299 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
300 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
301 bcf_fit_alt(b,1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
302 b->qual = 999;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
303
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
304 // Prepare BCF for output: ref, alt, filter, info, format
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
305 memset(&s, 0, sizeof(kstring_t)); kputc('\0', &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
306 kputs(b->ref, &s); kputc('\0', &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
307 kputs(b->alt, &s); kputc('\0', &s); kputc('\0', &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
308 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
309 ksprintf(&s, "AN=%d;", an);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
310 kputs(b->info, &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
311 anno16_t a;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
312 int has_I16 = test16(b, &a) >= 0? 1 : 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
313 if (has_I16 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
314 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
315 if ( a.is_tested) ksprintf(&s, ";PV4=%.2g,%.2g,%.2g,%.2g", a.p[0], a.p[1], a.p[2], a.p[3]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
316 ksprintf(&s, ";DP4=%d,%d,%d,%d;MQ=%d", a.d[0], a.d[1], a.d[2], a.d[3], a.mq);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
317 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
318 kputc('\0', &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
319 rm_info(&s, "I16=");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
320 rm_info(&s, "QS=");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
321 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
322 kputs(b->fmt, &s); kputc('\0', &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
323 free(b->str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
324 b->m_str = s.m; b->l_str = s.l; b->str = s.s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
325 bcf_sync(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
326 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
327
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
328 int call_multiallelic_gt(bcf1_t *b, bcf_p1aux_t *ma, double threshold, int var_only)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
329 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
330 int nals = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
331 char *p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
332 for (p=b->alt; *p; p++)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
333 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
334 if ( *p=='X' || p[0]=='.' ) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
335 if ( p[0]==',' ) nals++;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
336 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
337 if ( b->alt[0] && !*p ) nals++;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
338
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
339 if ( nals>4 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
340 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
341 if ( *b->ref=='N' ) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
342 fprintf(stderr,"Not ready for this, more than 4 alleles at %d: %s, %s\n", b->pos+1, b->ref,b->alt);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
343 exit(1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
344 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
345
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
346 // find PL, DV and DP FORMAT indexes
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
347 uint8_t *pl = NULL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
348 int i, npl = 0, idp = -1, idv = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
349 for (i = 0; i < b->n_gi; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
350 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
351 if (b->gi[i].fmt == bcf_str2int("PL", 2))
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
352 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
353 pl = (uint8_t*)b->gi[i].data;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
354 npl = b->gi[i].len;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
355 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
356 else if (b->gi[i].fmt == bcf_str2int("DP", 2)) idp=i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
357 else if (b->gi[i].fmt == bcf_str2int("DV", 2)) idv=i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
358 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
359 if ( nals==1 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
360 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
361 if ( !var_only ) _bcf1_set_ref(b, idp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
362 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
363 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
364 if ( !pl ) return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
365
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
366 assert(ma->q2p[0] == 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
367
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
368 // Init P(D|G)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
369 int npdg = nals*(nals+1)/2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
370 double *pdg,*_pdg;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
371 _pdg = pdg = malloc(sizeof(double)*ma->n*npdg);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
372 for (i=0; i<ma->n; i++)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
373 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
374 int j;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
375 double sum = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
376 for (j=0; j<npdg; j++)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
377 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
378 //_pdg[j] = pow(10,-0.1*pl[j]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
379 _pdg[j] = ma->q2p[pl[j]];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
380 sum += _pdg[j];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
381 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
382 if ( sum )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
383 for (j=0; j<npdg; j++) _pdg[j] /= sum;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
384 _pdg += npdg;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
385 pl += npl;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
386 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
387
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
388 if ((p = strstr(b->info, "QS=")) == 0) { fprintf(stderr,"INFO/QS is required with -m, exiting\n"); exit(1); }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
389 double qsum[4];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
390 if ( sscanf(p+3,"%lf,%lf,%lf,%lf",&qsum[0],&qsum[1],&qsum[2],&qsum[3])!=4 ) { fprintf(stderr,"Could not parse %s\n",p); exit(1); }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
391
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
392
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
393 // Calculate the most likely combination of alleles, remembering the most and second most likely set
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
394 int ia,ib,ic, max_als=0, max_als2=0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
395 double ref_lk = 0, max_lk = INT_MIN, max_lk2 = INT_MIN, lk_sum = INT_MIN, lk_sums[3];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
396 for (ia=0; ia<nals; ia++)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
397 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
398 double lk_tot = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
399 int iaa = (ia+1)*(ia+2)/2-1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
400 int isample;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
401 for (isample=0; isample<ma->n; isample++)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
402 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
403 double *p = pdg + isample*npdg;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
404 // assert( log(p[iaa]) <= 0 );
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
405 lk_tot += log(p[iaa]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
406 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
407 if ( ia==0 ) ref_lk = lk_tot;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
408 if ( max_lk<lk_tot ) { max_lk2 = max_lk; max_als2 = max_als; max_lk = lk_tot; max_als = 1<<ia; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
409 else if ( max_lk2<lk_tot ) { max_lk2 = lk_tot; max_als2 = 1<<ia; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
410 lk_sum = lk_tot>lk_sum ? lk_tot + log(1+exp(lk_sum-lk_tot)) : lk_sum + log(1+exp(lk_tot-lk_sum));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
411 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
412 lk_sums[0] = lk_sum;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
413 if ( nals>1 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
414 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
415 for (ia=0; ia<nals; ia++)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
416 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
417 if ( qsum[ia]==0 ) continue;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
418 int iaa = (ia+1)*(ia+2)/2-1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
419 for (ib=0; ib<ia; ib++)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
420 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
421 if ( qsum[ib]==0 ) continue;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
422 double lk_tot = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
423 double fa = qsum[ia]/(qsum[ia]+qsum[ib]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
424 double fb = qsum[ib]/(qsum[ia]+qsum[ib]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
425 double fab = 2*fa*fb; fa *= fa; fb *= fb;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
426 int isample, ibb = (ib+1)*(ib+2)/2-1, iab = iaa - ia + ib;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
427 for (isample=0; isample<ma->n; isample++)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
428 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
429 double *p = pdg + isample*npdg;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
430 //assert( log(fa*p[iaa] + fb*p[ibb] + fab*p[iab]) <= 0 );
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
431 if ( b->ploidy && b->ploidy[isample]==1 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
432 lk_tot += log(fa*p[iaa] + fb*p[ibb]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
433 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
434 lk_tot += log(fa*p[iaa] + fb*p[ibb] + fab*p[iab]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
435 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
436 if ( max_lk<lk_tot ) { max_lk2 = max_lk; max_als2 = max_als; max_lk = lk_tot; max_als = 1<<ia|1<<ib; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
437 else if ( max_lk2<lk_tot ) { max_lk2 = lk_tot; max_als2 = 1<<ia|1<<ib; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
438 lk_sum = lk_tot>lk_sum ? lk_tot + log(1+exp(lk_sum-lk_tot)) : lk_sum + log(1+exp(lk_tot-lk_sum));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
439 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
440 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
441 lk_sums[1] = lk_sum;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
442 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
443 if ( nals>2 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
444 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
445 for (ia=0; ia<nals; ia++)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
446 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
447 if ( qsum[ia]==0 ) continue;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
448 int iaa = (ia+1)*(ia+2)/2-1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
449 for (ib=0; ib<ia; ib++)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
450 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
451 if ( qsum[ib]==0 ) continue;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
452 int ibb = (ib+1)*(ib+2)/2-1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
453 int iab = iaa - ia + ib;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
454 for (ic=0; ic<ib; ic++)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
455 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
456 if ( qsum[ic]==0 ) continue;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
457 double lk_tot = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
458 double fa = qsum[ia]/(qsum[ia]+qsum[ib]+qsum[ic]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
459 double fb = qsum[ib]/(qsum[ia]+qsum[ib]+qsum[ic]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
460 double fc = qsum[ic]/(qsum[ia]+qsum[ib]+qsum[ic]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
461 double fab = 2*fa*fb, fac = 2*fa*fc, fbc = 2*fb*fc; fa *= fa; fb *= fb; fc *= fc;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
462 int isample, icc = (ic+1)*(ic+2)/2-1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
463 int iac = iaa - ia + ic, ibc = ibb - ib + ic;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
464 for (isample=0; isample<ma->n; isample++)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
465 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
466 double *p = pdg + isample*npdg;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
467 //assert( log(fa*p[iaa] + fb*p[ibb] + fc*p[icc] + fab*p[iab] + fac*p[iac] + fbc*p[ibc]) <= 0 );
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
468 if ( b->ploidy && b->ploidy[isample]==1 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
469 lk_tot += log(fa*p[iaa] + fb*p[ibb] + fc*p[icc]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
470 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
471 lk_tot += log(fa*p[iaa] + fb*p[ibb] + fc*p[icc] + fab*p[iab] + fac*p[iac] + fbc*p[ibc]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
472 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
473 if ( max_lk<lk_tot ) { max_lk2 = max_lk; max_als2 = max_als; max_lk = lk_tot; max_als = 1<<ia|1<<ib|1<<ic; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
474 else if ( max_lk2<lk_tot ) { max_lk2 = lk_tot; max_als2 = 1<<ia|1<<ib|1<<ic; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
475 lk_sum = lk_tot>lk_sum ? lk_tot + log(1+exp(lk_sum-lk_tot)) : lk_sum + log(1+exp(lk_tot-lk_sum));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
476 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
477 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
478 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
479 lk_sums[2] = lk_sum;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
480 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
481
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
482 // Should we add another allele, does it increase the likelihood significantly?
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
483 int n1=0, n2=0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
484 for (i=0; i<nals; i++) if ( max_als&1<<i) n1++;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
485 for (i=0; i<nals; i++) if ( max_als2&1<<i) n2++;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
486 if ( n2<n1 && kf_gammap(1,2.0*(max_lk-max_lk2))<threshold )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
487 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
488 // the threshold not exceeded, use the second most likely set with fewer alleles
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
489 max_lk = max_lk2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
490 max_als = max_als2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
491 n1 = n2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
492 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
493 lk_sum = lk_sums[n1-1];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
494
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
495 // Get the BCF record ready for GT and GQ
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
496 kstring_t s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
497 int old_n_gi = b->n_gi;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
498 s.m = b->m_str; s.l = b->l_str - 1; s.s = b->str;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
499 kputs(":GT:GQ", &s); kputc('\0', &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
500 b->m_str = s.m; b->l_str = s.l; b->str = s.s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
501 bcf_sync(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
502
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
503 // Call GTs
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
504 int isample, gts=0, ac[4] = {0,0,0,0};
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
505 int nRR = 0, nAA = 0, nRA = 0, max_dv = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
506 for (isample = 0; isample < b->n_smpl; isample++)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
507 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
508 int ploidy = b->ploidy ? b->ploidy[isample] : 2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
509 double *p = pdg + isample*npdg;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
510 int ia, als = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
511 double lk = 0, lk_s = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
512 for (ia=0; ia<nals; ia++)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
513 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
514 if ( !(max_als&1<<ia) ) continue;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
515 int iaa = (ia+1)*(ia+2)/2-1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
516 double _lk = p[iaa]*qsum[ia]*qsum[ia];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
517 if ( _lk > lk ) { lk = _lk; als = ia<<3 | ia; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
518 lk_s += _lk;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
519 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
520 if ( ploidy==2 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
521 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
522 for (ia=0; ia<nals; ia++)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
523 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
524 if ( !(max_als&1<<ia) ) continue;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
525 int iaa = (ia+1)*(ia+2)/2-1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
526 for (ib=0; ib<ia; ib++)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
527 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
528 if ( !(max_als&1<<ib) ) continue;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
529 int iab = iaa - ia + ib;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
530 double _lk = 2*qsum[ia]*qsum[ib]*p[iab];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
531 if ( _lk > lk ) { lk = _lk; als = ib<<3 | ia; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
532 lk_s += _lk;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
533 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
534 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
535 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
536 lk = -log(1-lk/lk_s)/0.2302585;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
537 int dp = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
538 if ( idp>=0 && (dp=((uint16_t*)b->gi[idp].data)[isample])==0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
539 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
540 // no coverage
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
541 ((uint8_t*)b->gi[old_n_gi].data)[isample] = 1<<7;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
542 ((uint8_t*)b->gi[old_n_gi+1].data)[isample] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
543 continue;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
544 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
545 if ( lk>99 ) lk = 99;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
546 ((uint8_t*)b->gi[old_n_gi].data)[isample] = als;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
547 ((uint8_t*)b->gi[old_n_gi+1].data)[isample] = (int)lk;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
548
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
549 // For MDV annotation
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
550 int dv;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
551 if ( als && idv>=0 && (dv=((uint16_t*)b->gi[idv].data)[isample]) )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
552 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
553 if ( max_dv < dv ) max_dv = dv;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
554 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
555
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
556 // For HWE annotation; multiple ALT alleles treated as one
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
557 if ( !als ) nRR++;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
558 else if ( !(als>>3&7) || !(als&7) ) nRA++;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
559 else nAA++;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
560
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
561 gts |= 1<<(als>>3&7) | 1<<(als&7);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
562 ac[ als>>3&7 ]++;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
563 ac[ als&7 ]++;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
564 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
565 free(pdg);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
566 bcf_fit_alt(b,max_als);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
567
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
568 // The VCF spec is ambiguous about QUAL: is it the probability of anything else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
569 // (that is QUAL(non-ref) = P(ref)+P(any non-ref other than ALT)) or is it
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
570 // QUAL(non-ref)=P(ref) and QUAL(ref)=1-P(ref)? Assuming the latter.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
571 b->qual = gts>1 ? -4.343*(ref_lk - lk_sum) : -4.343*log(1-exp(ref_lk - lk_sum));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
572 if ( b->qual>999 ) b->qual = 999;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
573
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
574 // Prepare BCF for output: ref, alt, filter, info, format
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
575 memset(&s, 0, sizeof(kstring_t)); kputc('\0', &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
576 kputs(b->ref, &s); kputc('\0', &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
577 kputs(b->alt, &s); kputc('\0', &s); kputc('\0', &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
578 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
579 int an=0, nalts=0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
580 for (i=0; i<nals; i++)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
581 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
582 an += ac[i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
583 if ( i>0 && ac[i] ) nalts++;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
584 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
585 ksprintf(&s, "AN=%d;", an);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
586 if ( nalts )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
587 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
588 kputs("AC=", &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
589 for (i=1; i<nals; i++)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
590 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
591 if ( !(gts&1<<i) ) continue;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
592 nalts--;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
593 ksprintf(&s,"%d", ac[i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
594 if ( nalts>0 ) kputc(',', &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
595 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
596 kputc(';', &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
597 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
598 kputs(b->info, &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
599 anno16_t a;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
600 int has_I16 = test16(b, &a) >= 0? 1 : 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
601 if (has_I16 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
602 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
603 if ( a.is_tested) ksprintf(&s, ";PV4=%.2g,%.2g,%.2g,%.2g", a.p[0], a.p[1], a.p[2], a.p[3]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
604 ksprintf(&s, ";DP4=%d,%d,%d,%d;MQ=%d", a.d[0], a.d[1], a.d[2], a.d[3], a.mq);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
605 ksprintf(&s, ";QBD=%e", b->qual/(a.d[0] + a.d[1] + a.d[2] + a.d[3]));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
606 if ( max_dv ) ksprintf(&s, ";MDV=%d", max_dv);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
607 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
608 if ( nAA+nRA )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
609 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
610 double hwe = calc_hwe(nAA, nRR, nRA);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
611 ksprintf(&s, ";HWE=%e", hwe);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
612 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
613 kputc('\0', &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
614 rm_info(&s, "I16=");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
615 rm_info(&s, "QS=");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
616 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
617 kputs(b->fmt, &s); kputc('\0', &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
618 free(b->str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
619 b->m_str = s.m; b->l_str = s.l; b->str = s.s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
620 bcf_sync(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
621
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
622 return gts;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
623 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
624
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
625 static int cal_pdg(const bcf1_t *b, bcf_p1aux_t *ma)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
626 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
627 int i, j;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
628 long *p, tmp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
629 p = alloca(b->n_alleles * sizeof(long));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
630 memset(p, 0, sizeof(long) * b->n_alleles);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
631 for (j = 0; j < ma->n; ++j) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
632 const uint8_t *pi = ma->PL + j * ma->PL_len;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
633 double *pdg = ma->pdg + j * 3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
634 pdg[0] = ma->q2p[pi[2]]; pdg[1] = ma->q2p[pi[1]]; pdg[2] = ma->q2p[pi[0]];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
635 for (i = 0; i < b->n_alleles; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
636 p[i] += (int)pi[(i+1)*(i+2)/2-1];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
637 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
638 for (i = 0; i < b->n_alleles; ++i) p[i] = p[i]<<4 | i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
639 for (i = 1; i < b->n_alleles; ++i) // insertion sort
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
640 for (j = i; j > 0 && p[j] < p[j-1]; --j)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
641 tmp = p[j], p[j] = p[j-1], p[j-1] = tmp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
642 for (i = b->n_alleles - 1; i >= 0; --i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
643 if ((p[i]&0xf) == 0) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
644 return i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
645 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
646
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
647
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
648 int bcf_p1_call_gt(const bcf_p1aux_t *ma, double f0, int k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
649 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
650 double sum, g[3];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
651 double max, f3[3], *pdg = ma->pdg + k * 3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
652 int q, i, max_i, ploidy;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
653 ploidy = ma->ploidy? ma->ploidy[k] : 2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
654 if (ploidy == 2) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
655 f3[0] = (1.-f0)*(1.-f0); f3[1] = 2.*f0*(1.-f0); f3[2] = f0*f0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
656 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
657 f3[0] = 1. - f0; f3[1] = 0; f3[2] = f0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
658 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
659 for (i = 0, sum = 0.; i < 3; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
660 sum += (g[i] = pdg[i] * f3[i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
661 for (i = 0, max = -1., max_i = 0; i < 3; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
662 g[i] /= sum;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
663 if (g[i] > max) max = g[i], max_i = i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
664 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
665 max = 1. - max;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
666 if (max < 1e-308) max = 1e-308;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
667 q = (int)(-4.343 * log(max) + .499);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
668 if (q > 99) q = 99;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
669 return q<<2|max_i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
670 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
671
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
672 #define TINY 1e-20
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
673
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
674 static void mc_cal_y_core(bcf_p1aux_t *ma, int beg)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
675 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
676 double *z[2], *tmp, *pdg;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
677 int _j, last_min, last_max;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
678 assert(beg == 0 || ma->M == ma->n*2);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
679 z[0] = ma->z;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
680 z[1] = ma->zswap;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
681 pdg = ma->pdg;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
682 memset(z[0], 0, sizeof(double) * (ma->M + 1));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
683 memset(z[1], 0, sizeof(double) * (ma->M + 1));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
684 z[0][0] = 1.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
685 last_min = last_max = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
686 ma->t = 0.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
687 if (ma->M == ma->n * 2) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
688 int M = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
689 for (_j = beg; _j < ma->n; ++_j) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
690 int k, j = _j - beg, _min = last_min, _max = last_max, M0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
691 double p[3], sum;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
692 M0 = M; M += 2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
693 pdg = ma->pdg + _j * 3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
694 p[0] = pdg[0]; p[1] = 2. * pdg[1]; p[2] = pdg[2];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
695 for (; _min < _max && z[0][_min] < TINY; ++_min) z[0][_min] = z[1][_min] = 0.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
696 for (; _max > _min && z[0][_max] < TINY; --_max) z[0][_max] = z[1][_max] = 0.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
697 _max += 2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
698 if (_min == 0) k = 0, z[1][k] = (M0-k+1) * (M0-k+2) * p[0] * z[0][k];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
699 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];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
700 for (k = _min < 2? 2 : _min; k <= _max; ++k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
701 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];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
702 for (k = _min, sum = 0.; k <= _max; ++k) sum += z[1][k];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
703 ma->t += log(sum / (M * (M - 1.)));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
704 for (k = _min; k <= _max; ++k) z[1][k] /= sum;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
705 if (_min >= 1) z[1][_min-1] = 0.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
706 if (_min >= 2) z[1][_min-2] = 0.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
707 if (j < ma->n - 1) z[1][_max+1] = z[1][_max+2] = 0.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
708 if (_j == ma->n1 - 1) { // set pop1; ma->n1==-1 when unset
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
709 ma->t1 = ma->t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
710 memcpy(ma->z1, z[1], sizeof(double) * (ma->n1 * 2 + 1));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
711 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
712 tmp = z[0]; z[0] = z[1]; z[1] = tmp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
713 last_min = _min; last_max = _max;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
714 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
715 //for (_j = 0; _j < last_min; ++_j) z[0][_j] = 0.; // TODO: are these necessary?
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
716 //for (_j = last_max + 1; _j < ma->M; ++_j) z[0][_j] = 0.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
717 } else { // this block is very similar to the block above; these two might be merged in future
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
718 int j, M = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
719 for (j = 0; j < ma->n; ++j) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
720 int k, M0, _min = last_min, _max = last_max;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
721 double p[3], sum;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
722 pdg = ma->pdg + j * 3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
723 for (; _min < _max && z[0][_min] < TINY; ++_min) z[0][_min] = z[1][_min] = 0.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
724 for (; _max > _min && z[0][_max] < TINY; --_max) z[0][_max] = z[1][_max] = 0.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
725 M0 = M;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
726 M += ma->ploidy[j];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
727 if (ma->ploidy[j] == 1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
728 p[0] = pdg[0]; p[1] = pdg[2];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
729 _max++;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
730 if (_min == 0) k = 0, z[1][k] = (M0+1-k) * p[0] * z[0][k];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
731 for (k = _min < 1? 1 : _min; k <= _max; ++k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
732 z[1][k] = (M0+1-k) * p[0] * z[0][k] + k * p[1] * z[0][k-1];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
733 for (k = _min, sum = 0.; k <= _max; ++k) sum += z[1][k];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
734 ma->t += log(sum / M);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
735 for (k = _min; k <= _max; ++k) z[1][k] /= sum;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
736 if (_min >= 1) z[1][_min-1] = 0.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
737 if (j < ma->n - 1) z[1][_max+1] = 0.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
738 } else if (ma->ploidy[j] == 2) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
739 p[0] = pdg[0]; p[1] = 2 * pdg[1]; p[2] = pdg[2];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
740 _max += 2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
741 if (_min == 0) k = 0, z[1][k] = (M0-k+1) * (M0-k+2) * p[0] * z[0][k];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
742 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];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
743 for (k = _min < 2? 2 : _min; k <= _max; ++k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
744 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];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
745 for (k = _min, sum = 0.; k <= _max; ++k) sum += z[1][k];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
746 ma->t += log(sum / (M * (M - 1.)));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
747 for (k = _min; k <= _max; ++k) z[1][k] /= sum;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
748 if (_min >= 1) z[1][_min-1] = 0.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
749 if (_min >= 2) z[1][_min-2] = 0.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
750 if (j < ma->n - 1) z[1][_max+1] = z[1][_max+2] = 0.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
751 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
752 tmp = z[0]; z[0] = z[1]; z[1] = tmp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
753 last_min = _min; last_max = _max;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
754 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
755 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
756 if (z[0] != ma->z) memcpy(ma->z, z[0], sizeof(double) * (ma->M + 1));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
757 if (bcf_p1_fp_lk)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
758 gzwrite(bcf_p1_fp_lk, ma->z, sizeof(double) * (ma->M + 1));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
759 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
760
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
761 static void mc_cal_y(bcf_p1aux_t *ma)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
762 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
763 if (ma->n1 > 0 && ma->n1 < ma->n && ma->M == ma->n * 2) { // NB: ma->n1 is ineffective when there are haploid samples
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
764 int k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
765 long double x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
766 memset(ma->z1, 0, sizeof(double) * (2 * ma->n1 + 1));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
767 memset(ma->z2, 0, sizeof(double) * (2 * (ma->n - ma->n1) + 1));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
768 ma->t1 = ma->t2 = 0.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
769 mc_cal_y_core(ma, ma->n1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
770 ma->t2 = ma->t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
771 memcpy(ma->z2, ma->z, sizeof(double) * (2 * (ma->n - ma->n1) + 1));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
772 mc_cal_y_core(ma, 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
773 // rescale z
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
774 x = expl(ma->t - (ma->t1 + ma->t2));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
775 for (k = 0; k <= ma->M; ++k) ma->z[k] *= x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
776 } else mc_cal_y_core(ma, 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
777 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
778
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
779 #define CONTRAST_TINY 1e-30
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
780
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
781 extern double kf_gammaq(double s, double z); // incomplete gamma function for chi^2 test
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
782
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
783 static inline double chi2_test(int a, int b, int c, int d)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
784 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
785 double x, z;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
786 x = (double)(a+b) * (c+d) * (b+d) * (a+c);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
787 if (x == 0.) return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
788 z = a * d - b * c;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
789 return kf_gammaq(.5, .5 * z * z * (a+b+c+d) / x);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
790 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
791
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
792 // chi2=(a+b+c+d)(ad-bc)^2/[(a+b)(c+d)(a+c)(b+d)]
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
793 static inline double contrast2_aux(const bcf_p1aux_t *p1, double sum, int k1, int k2, double x[3])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
794 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
795 double p = p1->phi[k1+k2] * p1->z1[k1] * p1->z2[k2] / sum * p1->hg[k1][k2];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
796 int n1 = p1->n1, n2 = p1->n - p1->n1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
797 if (p < CONTRAST_TINY) return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
798 if (.5*k1/n1 < .5*k2/n2) x[1] += p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
799 else if (.5*k1/n1 > .5*k2/n2) x[2] += p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
800 else x[0] += p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
801 return p * chi2_test(k1, k2, (n1<<1) - k1, (n2<<1) - k2);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
802 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
803
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
804 static double contrast2(bcf_p1aux_t *p1, double ret[3])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
805 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
806 int k, k1, k2, k10, k20, n1, n2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
807 double sum;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
808 // get n1 and n2
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
809 n1 = p1->n1; n2 = p1->n - p1->n1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
810 if (n1 <= 0 || n2 <= 0) return 0.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
811 if (p1->hg == 0) { // initialize the hypergeometric distribution
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
812 /* NB: the hg matrix may take a lot of memory when there are many samples. There is a way
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
813 to avoid precomputing this matrix, but it is slower and quite intricate. The following
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
814 computation in this block can be accelerated with a similar strategy, but perhaps this
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
815 is not a serious concern for now. */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
816 double tmp = lgamma(2*(n1+n2)+1) - (lgamma(2*n1+1) + lgamma(2*n2+1));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
817 p1->hg = calloc(2*n1+1, sizeof(void*));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
818 for (k1 = 0; k1 <= 2*n1; ++k1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
819 p1->hg[k1] = calloc(2*n2+1, sizeof(double));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
820 for (k2 = 0; k2 <= 2*n2; ++k2)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
821 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));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
822 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
823 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
824 { // compute
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
825 long double suml = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
826 for (k = 0; k <= p1->M; ++k) suml += p1->phi[k] * p1->z[k];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
827 sum = suml;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
828 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
829 { // get the max k1 and k2
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
830 double max;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
831 int max_k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
832 for (k = 0, max = 0, max_k = -1; k <= 2*n1; ++k) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
833 double x = p1->phi1[k] * p1->z1[k];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
834 if (x > max) max = x, max_k = k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
835 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
836 k10 = max_k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
837 for (k = 0, max = 0, max_k = -1; k <= 2*n2; ++k) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
838 double x = p1->phi2[k] * p1->z2[k];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
839 if (x > max) max = x, max_k = k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
840 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
841 k20 = max_k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
842 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
843 { // 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.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
844 double x[3], y;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
845 long double z = 0., L[2];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
846 x[0] = x[1] = x[2] = 0; L[0] = L[1] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
847 for (k1 = k10; k1 >= 0; --k1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
848 for (k2 = k20; k2 >= 0; --k2) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
849 if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
850 else z += y;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
851 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
852 for (k2 = k20 + 1; k2 <= 2*n2; ++k2) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
853 if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
854 else z += y;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
855 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
856 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
857 ret[0] = x[0]; ret[1] = x[1]; ret[2] = x[2];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
858 x[0] = x[1] = x[2] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
859 for (k1 = k10 + 1; k1 <= 2*n1; ++k1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
860 for (k2 = k20; k2 >= 0; --k2) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
861 if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
862 else z += y;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
863 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
864 for (k2 = k20 + 1; k2 <= 2*n2; ++k2) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
865 if ((y = contrast2_aux(p1, sum, k1, k2, x)) < 0) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
866 else z += y;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
867 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
868 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
869 ret[0] += x[0]; ret[1] += x[1]; ret[2] += x[2];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
870 if (ret[0] + ret[1] + ret[2] < 0.95) { // in case of bad things happened
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
871 ret[0] = ret[1] = ret[2] = 0; L[0] = L[1] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
872 for (k1 = 0, z = 0.; k1 <= 2*n1; ++k1)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
873 for (k2 = 0; k2 <= 2*n2; ++k2)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
874 if ((y = contrast2_aux(p1, sum, k1, k2, ret)) >= 0) z += y;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
875 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...
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
876 z = 1.0, ret[0] = ret[1] = ret[2] = 1./3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
877 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
878 return (double)z;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
879 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
880 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
881
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
882 static double mc_cal_afs(bcf_p1aux_t *ma, double *p_ref_folded, double *p_var_folded)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
883 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
884 int k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
885 long double sum = 0., sum2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
886 double *phi = ma->is_indel? ma->phi_indel : ma->phi;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
887 memset(ma->afs1, 0, sizeof(double) * (ma->M + 1));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
888 mc_cal_y(ma);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
889 // compute AFS
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
890 for (k = 0, sum = 0.; k <= ma->M; ++k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
891 sum += (long double)phi[k] * ma->z[k];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
892 for (k = 0; k <= ma->M; ++k) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
893 ma->afs1[k] = phi[k] * ma->z[k] / sum;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
894 if (isnan(ma->afs1[k]) || isinf(ma->afs1[k])) return -1.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
895 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
896 // compute folded variant probability
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
897 for (k = 0, sum = 0.; k <= ma->M; ++k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
898 sum += (long double)(phi[k] + phi[ma->M - k]) / 2. * ma->z[k];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
899 for (k = 1, sum2 = 0.; k < ma->M; ++k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
900 sum2 += (long double)(phi[k] + phi[ma->M - k]) / 2. * ma->z[k];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
901 *p_var_folded = sum2 / sum;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
902 *p_ref_folded = (phi[k] + phi[ma->M - k]) / 2. * (ma->z[ma->M] + ma->z[0]) / sum;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
903 // the expected frequency
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
904 for (k = 0, sum = 0.; k <= ma->M; ++k) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
905 ma->afs[k] += ma->afs1[k];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
906 sum += k * ma->afs1[k];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
907 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
908 return sum / ma->M;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
909 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
910
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
911 int bcf_p1_cal(const bcf1_t *b, int do_contrast, bcf_p1aux_t *ma, bcf_p1rst_t *rst)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
912 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
913 int i, k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
914 long double sum = 0.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
915 ma->is_indel = bcf_is_indel(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
916 rst->perm_rank = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
917 // set PL and PL_len
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
918 for (i = 0; i < b->n_gi; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
919 if (b->gi[i].fmt == bcf_str2int("PL", 2)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
920 ma->PL = (uint8_t*)b->gi[i].data;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
921 ma->PL_len = b->gi[i].len;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
922 break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
923 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
924 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
925 if (i == b->n_gi) return -1; // no PL
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
926 if (b->n_alleles < 2) return -1; // FIXME: find a better solution
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
927 //
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
928 rst->rank0 = cal_pdg(b, ma);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
929 rst->f_exp = mc_cal_afs(ma, &rst->p_ref_folded, &rst->p_var_folded);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
930 rst->p_ref = ma->afs1[ma->M];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
931 for (k = 0, sum = 0.; k < ma->M; ++k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
932 sum += ma->afs1[k];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
933 rst->p_var = (double)sum;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
934 { // compute the allele count
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
935 double max = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
936 rst->ac = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
937 for (k = 0; k <= ma->M; ++k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
938 if (max < ma->z[k]) max = ma->z[k], rst->ac = k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
939 rst->ac = ma->M - rst->ac;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
940 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
941 // calculate f_flat and f_em
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
942 for (k = 0, sum = 0.; k <= ma->M; ++k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
943 sum += (long double)ma->z[k];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
944 rst->f_flat = 0.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
945 for (k = 0; k <= ma->M; ++k) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
946 double p = ma->z[k] / sum;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
947 rst->f_flat += k * p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
948 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
949 rst->f_flat /= ma->M;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
950 { // estimate equal-tail credible interval (95% level)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
951 int l, h;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
952 double p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
953 for (i = 0, p = 0.; i <= ma->M; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
954 if (p + ma->afs1[i] > 0.025) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
955 else p += ma->afs1[i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
956 l = i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
957 for (i = ma->M, p = 0.; i >= 0; --i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
958 if (p + ma->afs1[i] > 0.025) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
959 else p += ma->afs1[i];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
960 h = i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
961 rst->cil = (double)(ma->M - h) / ma->M; rst->cih = (double)(ma->M - l) / ma->M;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
962 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
963 if (ma->n1 > 0) { // compute LRT
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
964 double max0, max1, max2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
965 for (k = 0, max0 = -1; k <= ma->M; ++k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
966 if (max0 < ma->z[k]) max0 = ma->z[k];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
967 for (k = 0, max1 = -1; k <= ma->n1 * 2; ++k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
968 if (max1 < ma->z1[k]) max1 = ma->z1[k];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
969 for (k = 0, max2 = -1; k <= ma->M - ma->n1 * 2; ++k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
970 if (max2 < ma->z2[k]) max2 = ma->z2[k];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
971 rst->lrt = log(max1 * max2 / max0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
972 rst->lrt = rst->lrt < 0? 1 : kf_gammaq(.5, rst->lrt);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
973 } else rst->lrt = -1.0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
974 rst->cmp[0] = rst->cmp[1] = rst->cmp[2] = rst->p_chi2 = -1.0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
975 if (do_contrast && rst->p_var > 0.5) // skip contrast2() if the locus is a strong non-variant
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
976 rst->p_chi2 = contrast2(ma, rst->cmp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
977 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
978 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
979
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
980 void bcf_p1_dump_afs(bcf_p1aux_t *ma)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
981 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
982 int k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
983 fprintf(stderr, "[afs]");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
984 for (k = 0; k <= ma->M; ++k)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
985 fprintf(stderr, " %d:%.3lf", k, ma->afs[ma->M - k]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
986 fprintf(stderr, "\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
987 memset(ma->afs, 0, sizeof(double) * (ma->M + 1));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
988 }