-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathabcFilterSNP.cpp
420 lines (349 loc) · 10.7 KB
/
abcFilterSNP.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
little class that does
1) hwe using genotype likelihoods
2) a) fisher exact
b) GATK approach
c) SB
These are based on guo 2012 BMC genomics 2013 13:666
3) 2 sample wilcox rank sum for baseQ bias
This file should be completely rewritten, much to slow and stupid. But it should give correct results
*/
#include <cmath>
#include <ctype.h>
#include "analysisFunction.h"
#include "shared.h"
#include "fet.c"
#include "chisquare.h" //<- stuff related to calculating pvalues from LRT tests
#include "abc.h"
#include "abcFilterSNP.h"
#include "abcHWE.h"
#include "abcCallGenotypes.h"
#include <htslib/kstring.h>
#include "aio.h"
/*
wilcox manwhitney u test, whatever ,implementation is from wiki
validated with wilcox.test (qscores of major,qscores of minor,exact=T,correct=F)
returns Zscore, for some reason...
*/
double mann(int majD[255],int minD[255]){
double U[255];
double cumLess1 =0;
for(int i=0;i<255;i++){
U[i]=(double) minD[i]*(majD[i]/2.0+cumLess1);
cumLess1 += majD[i];
}
double U1 =0;
for(int i=0;i<255;i++)
U1+= U[i];
//below is a check
double cumLess2 =0;
for(int i=0;i<255;i++){
U[i]=(double) majD[i]*(minD[i]/2.0+cumLess2);
cumLess2 += minD[i];
}
double U2 =0;
for(int i=0;i<255;i++)
U2+= U[i];
double mu=cumLess1*cumLess2/2.0;
double sigu=sqrt((cumLess1*cumLess2*(cumLess1+cumLess2+1))/12.0);
double Z=(std::min(U1,U2)-mu)/sigu;
// fprintf(stderr,"U1:%f U2:%f U1+U2:%f nObs:%f Z:%f p.value:%f\n",U1,U2,U1+U2,cumLess1*cumLess2,Z,2*phi(Z));
return Z;
}
double edgebias(tNode **tn,int nInd,int maj,int min){
// fprintf(stderr,"phi:%f\n",phi(3));
int majD[255];
int minD[255];
memset(majD,0,sizeof(int)*255);
memset(minD,0,sizeof(int)*255);
for(int i=0;i<nInd;i++){
tNode *nd = tn[i];
if(nd==NULL)
continue;
for(int l=0;l<nd->l;l++){
int obB = refToInt[nd->seq[l]];
if(obB==maj){
majD[std::min(nd->posi[l],nd->isop[l])]++;
// fprintf(stdout,"maj\t%d\n",nd->qs[l]);
}else if(obB==min){
minD[std::min(nd->isop[l],nd->posi[l])]++;
//fprintf(stdout,"min\t%d\n",nd->qs[l]);
}
}
}
return mann(majD,minD);
}
double mapQbias(tNode **tn,int nInd,int maj,int min){
// fprintf(stderr,"phi:%f\n",phi(3));
int majD[255];
int minD[255];
memset(majD,0,sizeof(int)*255);
memset(minD,0,sizeof(int)*255);
for(int i=0;i<nInd;i++){
tNode *nd = tn[i];
if(nd==NULL)
continue;
for(int l=0;l<nd->l;l++){
int obB = refToInt[nd->seq[l]];
if(obB==maj){
majD[nd->mapQ[l]]++;
// fprintf(stdout,"maj\t%d\n",nd->qs[l]);
}else if(obB==min){
minD[nd->mapQ[l]]++;
//fprintf(stdout,"min\t%d\n",nd->qs[l]);
}
}
}
return mann(majD,minD);
}
double baseQbias(tNode **tn,int nInd,int maj,int min){
// fprintf(stderr,"phi:%f\n",phi(3));
int majD[255];
int minD[255];
memset(majD,0,sizeof(int)*255);
memset(minD,0,sizeof(int)*255);
for(int i=0;i<nInd;i++){
tNode *nd = tn[i];
if(nd==NULL)
continue;
for(int l=0;l<nd->l;l++){
int obB = refToInt[nd->seq[l]];
if(obB==maj){
majD[nd->qs[l]]++;
// fprintf(stdout,"maj\t%d\n",nd->qs[l]);
}else if(obB==min){
minD[nd->qs[l]]++;
//fprintf(stdout,"min\t%d\n",nd->qs[l]);
}
}
}
return mann(majD,minD);
}
Chisqdist chi(1);
//guo 2012 mutat res 2012, 744(2):154-160
double sb1(int cnts[4]){
double a=cnts[0];double b=cnts[1];double c=cnts[2];double d=cnts[3];
double top=b/(a+b)-d/(c+d);
double bot=(b+d)/(a+b+c+d);
return top/bot;
}
//the gatk way
double sb2(int cnts[4]){
double a=cnts[0];double b=cnts[1];double c=cnts[2];double d=cnts[3];
double en=(b/(a+b))*(c/(c+d));
double to=(a+c)/(a+b+c+d);
double tre=(d/(c+d))*(a/(a+b));
return std::max(en/to,tre/to);
}
//strandbias using fisher
double sb3(int cnts[4]){
double left,right,twotail,prob;
kt_fisher_exact(cnts[0], cnts[1], cnts[2], cnts[3], &left, &right, &twotail);
return twotail;
}
void abcFilterSNP::printArg(FILE *argFile){
fprintf(argFile,"-----BETA---------------\n%s:\n",__FILE__);
fprintf(argFile,"\t-doSnpStat %d\n",doSnpStat);
fprintf(argFile,"\t-edge_pval %f\n",edge_pval);
fprintf(argFile,"\t-mapQ_pval %f\n",mapQ_pval);
fprintf(argFile,"\t-sb_pval %f\n",sb_pval);
fprintf(argFile,"\t-hwe_pval %f\n",hwe_pval);
fprintf(argFile,"\t-qscore_pval %f\n",qscore_pval);
fprintf(argFile,"\t-hetbias_pval %f\n",hetbias_pval);
}
void abcFilterSNP::run(funkyPars *pars){
if(!doSnpStat)
return;
chunkyT *chk = pars->chk;
if(doSnpStat==1){
kstring_t *bufstr = new kstring_t;
bufstr->s=NULL;bufstr->l=bufstr->m=0;
//loop over sites;
kstring_t persite;
persite.s=NULL;persite.l=persite.m=0;
//pull
for(int s=0;s<pars->numSites;s++) {
if(pars->keepSites[s]==0)
continue;
//loop over samples
int cnts[4]={0,0,0,0};
for(int i=0;i<pars->nInd;i++){
tNode *nd = chk->nd[s][i];
if(nd==NULL)
continue;
for(int l=0;l<nd->l;l++){
int obB = refToInt[nd->seq[l]];
// fprintf(stderr,"%c ",nd.seq[l]);
int strand = (isupper(nd->seq[l])==0)<<1;
// fprintf(stderr,"strand:%d\n",strand);
if(obB==4)
continue;
if((obB!=pars->major[s] && obB!=pars->minor[s]) )
continue;
if(obB!=pars->major[s])
strand +=1;
//fprintf(stderr,"strand=%d\n",strand);
cnts[strand]++;
}
}
ksprintf(&persite,"%s\t%d\t%d %d %d %d\t",header->target_name[pars->refId],pars->posi[s]+1, cnts[0],cnts[1],cnts[2],cnts[3]);
ksprintf(&persite,"%f:%f:%f\t",sb1(cnts),sb2(cnts),sb3(cnts));
// fprintf(stderr,"sb4:%f\n",sb3(cnts));
if(sb_pval!=-1 && sb3(cnts)<sb_pval)
pars->keepSites[s] = 0;
funkyHWE *hweStruct = (funkyHWE *) pars->extras[9];//THIS IS VERY NASTY! the ordering within general.cpp is now important
double lrt = 2*hweStruct->like0[s]-2*hweStruct->likeF[s];
double pval;
if(std::isnan(lrt))
pval=lrt;
else if(lrt<0)
pval =1;
else
pval =1- chi.cdf(lrt);
ksprintf(&persite,"%f:%e\t",lrt,pval);
// ksprintf(&persite,"%f:%e\t",lrt,pval);
if(hwe_pval!=-1 && pval<hwe_pval)
pars->keepSites[s] = 0;
double Z = baseQbias(chk->nd[s],pars->nInd,refToInt[pars->major[s]],refToInt[pars->minor[s]]);
ksprintf(&persite,"%f:%e\t",Z,2*phi(Z));
if(qscore_pval!=-1 && 2*phi(Z)<qscore_pval)
pars->keepSites[s] = 0;
Z = mapQbias(chk->nd[s],pars->nInd,refToInt[pars->major[s]],refToInt[pars->minor[s]]);
ksprintf(&persite,"%f:%e\t",Z,2*phi(Z));
if(mapQ_pval!=-1 && 2*phi(Z)<mapQ_pval)
pars->keepSites[s] = 0;
Z = edgebias(chk->nd[s],pars->nInd,refToInt[pars->major[s]],refToInt[pars->minor[s]]);
ksprintf(&persite,"%f:%e",Z,2*phi(Z));
if(2*phi(Z)<edge_pval)
pars->keepSites[s] = 0;
genoCalls *gcw =(genoCalls *) pars->extras[11];
int **gc=NULL;
if(gcw)
gc = gcw->dat;
if(gc){
cnts[0]=cnts[1]=cnts[2]=cnts[3]=0;
int nsampleswithdata =0;
for(int i=0;i<pars->nInd;i++){
if(gc[s][i]!=1)
continue;
tNode *nd = chk->nd[s][i];
if(nd==NULL)
continue;
nsampleswithdata++;
for(int l=0;l<nd->l;l++){
int obB = refToInt[nd->seq[l]];
// fprintf(stderr,"%c ",nd.seq[l]);
int strand = (isupper(nd->seq[l])==0)<<1;
// fprintf(stderr,"strand:%d\n",strand);
if(obB==4)
continue;
if((obB!=pars->major[s] && obB!=pars->minor[s]) )
continue;
if(obB!=pars->major[s])
strand +=1;
//fprintf(stderr,"strand=%d\n",strand);
cnts[strand]++;
}
}
double tsum= cnts[0]+cnts[1]+cnts[2]+cnts[3];
double fA=(cnts[0]+cnts[2])/tsum;
double fa=(cnts[1]+cnts[3])/tsum;
int n = tsum;
ksprintf(&persite,"\t%d %d %d %d %d\t",cnts[0],cnts[1],cnts[2],cnts[3],n);
double lrt = 2*tsum*(fA-0.5)*(fA-0.5) + 2*tsum*(fa-0.5)*(fa-0.5);
double pval;
if(std::isnan(lrt))
pval=lrt;
else if(lrt<0)
pval =1;
else
pval =1- chi.cdf(lrt);
ksprintf(&persite,"%f:%e\t",lrt,pval);
if(hetbias_pval!=-1&&pval<hetbias_pval)
pars->keepSites[s]=0;
}
if(pars->keepSites[s]!=0&&persite.l>0){
ksprintf(&persite,"\n");
ksprintf(bufstr,"%s",persite.s);
}
persite.l=0;
}
free(persite.s);
pars->extras[index] = bufstr;
}
}
void abcFilterSNP::clean(funkyPars *fp){
if(!doSnpStat)
return;
}
void abcFilterSNP::print(funkyPars *pars){
if(!doSnpStat)
return;
kstring_t *bufstr =(kstring_t*) pars->extras[index];
aio::bgzf_write(outfileZ,bufstr->s,bufstr->l);bufstr->l=0;
free(bufstr->s);
delete bufstr;
}
void abcFilterSNP::getOptions(argStruct *arguments){
//default
//from command line
doSnpStat=angsd::getArg("-doSnpStat",doSnpStat,arguments);
if(doSnpStat==0)
return;
int domajorminor=0;
domajorminor = angsd::getArg("-domajorminor",domajorminor,arguments);
if(domajorminor==0){
fprintf(stderr,"\t-> Must supply -doMajorMinor for running dosnpstat (needs to look a distributions of major and minor alleles)\n");
exit(0);
}
//from command line
edge_pval=angsd::getArg("-edge_pval",edge_pval,arguments);
mapQ_pval=angsd::getArg("-mapQ_pval",mapQ_pval,arguments);
sb_pval=angsd::getArg("-sb_pval",sb_pval,arguments);
hwe_pval=angsd::getArg("-hwe_pval",hwe_pval,arguments);
qscore_pval=angsd::getArg("-qscore_pval",qscore_pval,arguments);
hetbias_pval=angsd::getArg("-hetbias_pval",hetbias_pval,arguments);
int doHWE=0;
doHWE=angsd::getArg("-doHWE",doHWE,arguments);
if(doHWE==0){
fprintf(stderr,"must use -doHWE 1, to test for HWE\n");
exit(0);
}
}
abcFilterSNP::abcFilterSNP(const char *outfiles,argStruct *arguments,int inputtype){
doSnpStat=0;
outfileZ = NULL;
edge_pval=mapQ_pval=sb_pval=hwe_pval=qscore_pval=hetbias_pval-1;
if(arguments->argc==2){
if(!strcasecmp(arguments->argv[1],"-doSnpStat")||!strcasecmp(arguments->argv[1],"-doPost")){
printArg(stdout);
exit(0);
}else
return;
}
getOptions(arguments);
if(doSnpStat==0){
shouldRun[index] =0;
return;
}
printArg(arguments->argumentFile);
int dogeno=0;
dogeno=angsd::getArg("-dogeno",dogeno,arguments);
if(doSnpStat){
// fprintf(stderr,"running doSnpStat=%d\n",doSnpStat);
const char *postfix=".snpStat.gz";
outfileZ = aio::openFileBG(outfiles,postfix);
kstring_t bufstr;bufstr.s=NULL;bufstr.l=bufstr.m=0;
ksprintf(&bufstr,"Chromo\tPosition\t+Major +Minor -Major -Minor\tSB1:SB2:SB3\tHWE_LRT:HWE_pval\tbaseQ_Z:baseQ_pval\tmapQ_Z:mapQ_pval\tedge_z:edge_pval");
if(dogeno){
ksprintf(&bufstr,"\t+MajorHet +MinorHet -MajorHet -MinorHet nHet\thetStat:hetStat_pval");
}
ksprintf(&bufstr,"\n");
aio::bgzf_write(outfileZ,bufstr.s,bufstr.l);bufstr.l=0;
free(bufstr.s);
}
}
abcFilterSNP::~abcFilterSNP(){
if(outfileZ!=NULL)
bgzf_close(outfileZ);
}