-
Notifications
You must be signed in to change notification settings - Fork 51
/
bam_likes.cpp
363 lines (291 loc) · 8.76 KB
/
bam_likes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
This code is very much copied from bam2bcf.c and errmod.c from samtools 1.18
credit to auther of SAMtools
*/
#include <cstdio>
#include <stdint.h>
#include <cstdlib>
#include <cmath>
#include <ctype.h>
#include "mUpPile.h"
#include "aio.h"
extern int refToInt[256];
#define ERR_DEP 0.83f
#if 0
KSORT_INIT_GENERIC(uint16_t)
typedef struct __errmod_coef_t {
double *fk;//fk function of n: fk(n)=0.83^n*0.97+0.03
double *beta;
double *lhet;//lhet function of n,k : lhet(n,k)=(1/2^n)*choose(n,k)
} errmod_coef_t;
typedef struct {
double depcorr;
errmod_coef_t *coef;
} errmod_t;
typedef struct {
double bsum[16];
uint32_t c[16];
} call_aux_t;
errmod_coef_t *cal_coef(double depcorr, double eta)
{
int k, n, q;
long double sum, sum1;
double *lC;
errmod_coef_t *ec;
ec =(errmod_coef_t *) calloc(1, sizeof(errmod_coef_t));
ec->fk = (double*)calloc(256, sizeof(double));
ec->fk[0] = 1.0;
for (n = 1; n != 256; ++n){
ec->fk[n] = pow(1. - depcorr, n) * (1.0 - eta) + eta;
}
ec->beta = (double*)calloc(256 * 256 * 64, sizeof(double));
lC = (double*)calloc(256 * 256, sizeof(double));
for (n = 1; n != 256; ++n) {
double lgn = lgamma(n+1);
for (k = 1; k <= n; ++k)
lC[n<<8|k] = lgn - lgamma(k+1) - lgamma(n-k+1);
}
for (q = 1; q != 64; ++q) {
double e = pow(10.0, -q/10.0);//phredprob
double le = log(e);//log(phredprob)
double le1 = log(1.0 - e); //log(1-phredborg)
for (n = 1; n <= 255; ++n) {
double *beta = ec->beta + (q<<16|n<<8);
sum1 = sum = 0.0;
for (k = n; k >= 0; --k, sum1 = sum) {
sum = sum1 + expl(lC[n<<8|k] + k*le + (n-k)*le1);
beta[k] = -10. / M_LN10 * logl(sum1 / sum);
}
}
}
ec->lhet = (double*)calloc(256 * 256, sizeof(double));
for (n = 0; n < 256; ++n)
for (k = 0; k < 256; ++k)
ec->lhet[n<<8|k] = lC[n<<8|k] - M_LN2 * n;
free(lC);
return ec;
}
//depcorr = 1-0.83=0.17
errmod_t *errmod_init(float depcorr) {
// fprintf(stderr,"[%s]",__FUNCTION__);
errmod_t *em;
em = (errmod_t*)calloc(1, sizeof(errmod_t));
em->depcorr = depcorr;
em->coef = cal_coef(depcorr, 0.03);
return em;
}
void errmod_destroy(errmod_t *em)
{
if (em == 0) return;
free(em->coef->lhet); free(em->coef->fk); free(em->coef->beta);
free(em->coef); free(em);
}
// qual:6, strand:1, base:4
int errmod_cal(const errmod_t *em, int n, int m, uint16_t *bases, float *q)
{
// fprintf(stderr,"n=%d\n",n);
if (n == 0)
return 0; //no data
//reset results array;
memset(q, 0, m * m * sizeof(float));
//set "counts" array
int w[32];
memset(w, 0, 32 * sizeof(int));
for(int i=0;0&&i<32;i++)
fprintf(stderr,"pre w[%d]=%d\n",i,w[i]);
call_aux_t aux;
memset(&aux, 0, sizeof(call_aux_t));
//sample 255 if depth >255
if ((n > 255)) { // then sample 255 bases //THIS MAKES
ks_shuffle(uint16_t, n, bases);
n = 255;
}
ks_introsort(uint16_t, n, bases);
for (int j = n - 1; j >= 0; --j) {
uint16_t b = bases[j];
// fprintf(stderr,"j=%d q=%d strand=%d base=%d\n",j,b>>5,0x1&(b>>4),b&0xf);
// fprintf(stderr,"j=%d q=%d\tc=%c\t",j,(b>>5) + 33,(b>>5) + 33);
//cap quality at [4,63]
int q = b>>5 < 4? 4 : b>>5;
if (q > 63)
q = 63;
int k = b&0x1f;
aux.bsum[k&0xf] += em->coef->fk[w[k]] * em->coef->beta[q<<16|n<<8|aux.c[k&0xf]];
++aux.c[k&0xf];
++w[k];
for(int i=0;0&&i<32;i++)
fprintf(stderr,"w[%d]=%d\n",i,w[i]);
}
if(0){
//floating point inprecision compared with samtools binary output. But is correct
//the genotype like p(data|A1=g1,A2=g2) = p(data|A1=g2,A2=g1)
for (int g1 = 0; g1 <5; ++g1) {//allele1=0,1,2,3,4
for (int g2 = g1; g2<5; ++g2) {//allele2=0,1,2,3,4
if(g1!=g2){ // A1!=A2 - heterozygoues
int cjk = aux.c[g1] + aux.c[g2];//total depth for allele g1+allele g2
//binomial when ignoring non A1/A2 alleles: Bin (n,k,p);n=cjk=#g1+#g2 , k= aux.c[g2] = #g2 ; p=0.5. returns log
q[g1*5+g2] = -4.343 * em->coef->lhet[cjk<<8|aux.c[g2]];
}
for (int k = 0; k <5; ++k){
if(k!=g1 && k!=g2) //if a read has a non A1/A2 alleles it is an error. add the log of the prob of these reads
q[g1*5+g2] += aux.bsum[k];
}
//mirror
if(g1!=g2)
q[g2*5+g1] = q[g1*5+g2];
if (q[g1*5+g2] < 0.0)
q[g1*5+g2] = 0.0;
}
}
return 0;
}
// generate likelihood THIS WORKS PERFECTLY june 4 ande
for (int g1 = 0; g1 <5; ++g1) {//j=0,1,2,3,4
for (int g2 = g1; g2<5; ++g2) {//j=0,1,2,3,4
if(g1==g2){
for (int k = 0; k <5; ++k){
if(k!=g1)
q[g1*5+g2] += aux.bsum[k];
}
}
else{
int other=0;
float tmp1=0;
for (int k = 0; k < 5; ++k)
if (k != g1 && k != g2) {
tmp1 += aux.bsum[k];
other = 1;
}
int cjk = aux.c[g1] + aux.c[g2];
if (other)
q[g1*5+g2] = q[g2*5+g1] = -4.343 * em->coef->lhet[cjk<<8|aux.c[g2]] + tmp1;
else
q[g1*5+g2] = q[g2*5+g1] = -4.343 * em->coef->lhet[cjk<<8|aux.c[g2]]; // all the bases are either j or k
}
if (q[g1*5+g2] < 0.0)
q[g1*5+g2] = 0.0;
}
}
return 0;
//old original almost
for (int j = 0; j != m; ++j) {//j=0,1,2,3,4
// homozygous
for (int k = 0; k != m; ++k){//only updates if k!=j and aux.c[k]!=0
fprintf(stderr,"\t-> j=%d aux.c[%d]=%d aux.bsum[%d]=%f res=%d ",j,k,aux.c[k],k,aux.bsum[k],j*m+j);
if (k != j && aux.c[k]) {
fprintf(stderr,"USING \n");
q[j*m+j] += aux.bsum[k];
}else
fprintf(stderr,"skipping\n");
}
// heterozygous
for (int k = j + 1; k < m; ++k) {//k=1,...,4
float tmp1=0.0;
int isHe=0;
for (int i = 0; i < m; ++i)
if (i != j && i != k) {
tmp1 += aux.bsum[i];
isHe += aux.c[i];
}
int cjk = aux.c[j] + aux.c[k];
fprintf(stderr,"j=%d k=%d RES=%d\n",j,k,j*m+k);
if(1) {
if (isHe)
q[j*m+k] = q[k*m+j] = -4.343 * em->coef->lhet[cjk<<8|aux.c[k]] + tmp1;
else
q[j*m+k] = q[k*m+j] = -4.343 * em->coef->lhet[cjk<<8|aux.c[k]]; // all the bases are either j or k
}else{
q[j*m+k] = q[k*m+j] = -4.343 * em->coef->lhet[cjk<<8|aux.c[k]];
if(isHe)
q[j*m+k] = q[k*m+j] = q[k*m+j]+tmp1;
}
}
//set to zero if negative, shoulnd't happen
for (int k = 0; k != m; ++k)
if (q[j*m+k] < 0.0)
q[j*m+k] = 0.0;
}
// exit(0);
return 0;
}
#endif
//this is global this is nasty
errmod_t *mod = NULL;
//err_dep = 0.83
void bam_likes_init(){
mod = errmod_init(1-ERR_DEP);
}
void bam_likes_destroy(){
// fprintf(stderr,"bam_likes_destroy\n");
if(mod!=NULL)
errmod_destroy(mod);
}
void call_bam(chunkyT *chk,double **lk,int trim,int *keepSites){
// fprintf(stderr,"trim=%d\n",trim);
if(chk==NULL){
if(mod!=NULL)
bam_likes_destroy();
return;
}
aio::doAssert(mod!=NULL,1,AT,"");
for(int s=0;s<chk->nSites;s++){
lk[s] = new double[10*chk->nSamples];
if(keepSites[s]==0)
continue;
//fprintf(stderr,"posi=%d\n",chk->nd[s][0].refPos);
for(int ii=0;ii<10*chk->nSamples;ii++)
lk[s][ii] = -0.0;
//fprintf(stderr,"chr=%s minbaseq=%d\n",chk->refName,minQ);
for(int i=0;i<chk->nSamples;i++){
tNode *nd = chk->nd[s][i];
if(nd==NULL)
continue;
uint16_t bases[nd->l];
int numItems =0;
for(int j=0;j<nd->l;j++) {
// fprintf(stderr,"posi=%d isop=%d +=%d\n",nd->posi[j],nd->isop[j],nd->posi[j]+nd->isop[j]);
int q=nd->qs[j];
// fprintf(stderr,"q[%d]=%c %d\n",j,q+33,q);
//fprintf(stderr,"skipping q=%d mapQ=%d posi=%d isop=%d\n",q,nd.mapQ[j],nd.posi[j],nd.isop[j]);
extern int minQ;
if(q<minQ || nd->posi[j]<trim||nd->isop[j]<trim){
//fprintf(stderr,"skipping q=%d mapQ=%d posi=%d isop=%d ind=%d\n",q,nd->mapQ[j],nd->posi[j],nd->isop[j],i);
continue;
}
/*
extern int minQ;
if(q<minQ || nd.posi[j]<trim||nd.isop[j]<trim){
// fprintf(stderr,"skipping q=%d mapQ=%d posi=%d isop=%d ind=%d\n",q,nd.mapQ[j],nd.posi[j],nd.isop[j],i);
continue;
}
*/
if (q>nd->mapQ[j])//dont allow qscores in read to exceed mapping quality
q=nd->mapQ[j];
if (q > 63) q = 63;
if (q < 4) q = 4;
// fprintf(stderr,"q=%d qshift=%d\n",q,q<<5);
//plug in values
// bca->bases[n++] = q<<5 | (int)bam1_strand(p->b)<<4 | b;
int isLower = islower(nd->seq[j])>0;
//fprintf(stderr,"strandstuff=%d\tshiftstrandstuff=%d\n",isLower, isLower<<4 );
bases[numItems++] = q<<5 | isLower<<4 | refToInt[nd->seq[j]];
// fprintf(stderr,"bases[%d]=%d\n",j,bases[numItems-1]);
}
float likes[25];
for(int j=0;0&&j<25;j++)
likes[j]=0;
if(numItems!=0) {
errmod_cal(mod,numItems,5,bases,likes);
int ord[] = {0,1,2,3,6,7,8,12,13,18};
for(int jj=0;0&&jj<25;jj++)
fprintf(stderr,"jj: %d) asdf: %f %f\n",jj,-log(10.0)*likes[jj]/10.0,likes[jj]);
for(int gg=0;gg<10;gg++){
lk[s][i*10+gg] = -log(10.0) *likes[ord[gg]]/10.0;
// fprintf(stderr,"GOLDEN REITER%f\n",lk[s][i*10+gg]);
}
// exit(0);
}
}
// break;
}
}