forked from ANGSD/angsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
abcDstat2.cpp
666 lines (550 loc) · 17.1 KB
/
abcDstat2.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
#include <cmath>
#include <cstdlib>
#include <ctime>
#include "shared.h"
#include "analysisFunction.h"
#include <htslib/kstring.h>
#include "abc.h"
#include "abcDstat2.h"
#include "aio.h"
typedef struct {
double **NUM;
double **DEN;
double ***COMB;
int *BLOCKNUM;
int **NSITE;
int NUMBLOCK;
}funkyAbbababa2;
// shows up when you run ./angsd -doAbbababa2
void abcDstat2::printArg(FILE *argFile){
fprintf(argFile,"--------------\n%s:\n",__FILE__);
fprintf(argFile,"\t-doAbbababa2\t\t%d\trun the abbababa analysis\n",doAbbababa2);
fprintf(argFile,"\t-rmTrans\t\t%d\tremove transitions\n",rmTrans);
fprintf(argFile,"\t-blockSize\t\t%d\tsize of each block in bases\n",blockSize);
fprintf(argFile,"\t-anc\t\t\t%s\tfasta file with outgroup\n",ancName);
fprintf(argFile,"\t-sample\t\t\t%d\tsample a single base\n",sample);
fprintf(argFile,"\t-maxDepth\t\t%d\tmax depth of each site allowed\n",maxDepth);
fprintf(argFile,"\t-sizeFile\t\t%s\tfile with size of populations\n",sizeFile);
fprintf(argFile,"\t-enhance\t\t%d\tonly analyze sites where outgroup H4 is non poly\n",enhance);
fprintf(argFile,"\t-Aanc\t\t\t%d\tset H4 outgroup allele as A in each site\n",Aanc);
fprintf(argFile,"\t-useLast\t\t%d\tuse last group of bam files as outgroup in the D-stat\n",useLast);
fprintf(argFile,"\n");
}
// get you arguments
void abcDstat2::getOptions(argStruct *arguments){
//from command line
// 0: ignore this class, non zero: run this class
doAbbababa2 = angsd::getArg("-doAbbababa2",doAbbababa2,arguments);
if(doAbbababa2==0)
return;
doCount = angsd::getArg("-doCounts",doCount,arguments);
blockSize = angsd::getArg("-blockSize",blockSize,arguments);
ancName = angsd::getArg("-anc",ancName,arguments);
rmTrans = angsd::getArg("-rmTrans",rmTrans,arguments);
sample = angsd::getArg("-sample",sample,arguments);
maxDepth = angsd::getArg("-maxDepth",maxDepth,arguments);
enhance = angsd::getArg("-enhance",enhance,arguments);
Aanc = angsd::getArg("-Aanc",Aanc,arguments);
sizeFile = angsd::getArg("-sizeFile",sizeFile,arguments);
useLast = angsd::getArg("-useLast",useLast,arguments);
if(doAbbababa2){
if(arguments->inputtype!=INPUT_BAM&&arguments->inputtype!=INPUT_PILEUP){
fprintf(stderr,"Error: bam or soap input needed for -doAbbababa2 \n");
exit(0);
}
if(arguments->nInd==3 && ancName==NULL){
fprintf(stderr,"Error: -doAbbababa2 needs at least 4 individual\n");
exit(0);
}
if(doCount==0){
fprintf(stderr,"Error: -doAbbababa2 needs allele counts (use -doCounts 1)\n");
exit(0);
}
}
} //---end of abcDstat2::getOptions(argStruct *arguments)
// Construction
abcDstat2::abcDstat2(const char *outfiles, argStruct *arguments,int inputtype){
//default values
rmTrans = 0;
sample = 0;
outfile=NULL;
ancName=NULL;
sizeFile=NULL;
doAbbababa2=0;
doCount=0;
blockSize=5000000;
block=0;
enhance = 0;
maxDepth=1000;
useLast = 0;
// you are starting before chromosome 0
currentChr=-1;
NbasesPerLine=50;
Aanc=0;
//if you dont use the -doAbbababa2 argument then return
if(arguments->argc==2){
if(!strcasecmp(arguments->argv[1],"-doAbbababa2")){
printArg(stdout);
exit(0);
}else
return;
}
//get all options
getOptions(arguments);
//ignore
if(doAbbababa2==0){
shouldRun[index] = 0;
return;
}
printArg(arguments->argumentFile);
//correctly counting number of individuals
nIndFasta = arguments -> nInd;
//int numPop;
if(sizeFile != NULL){
sizeMat = angsd::getMatrixInt(sizeFile,nIndFasta);
numPop = sizeMat.x;
}
else{
numPop = nIndFasta;
}
if(ancName != NULL && useLast == 0){
nIndFasta += 1;
if(sizeFile == NULL)
numPop = nIndFasta;
}
/*------ get population sizes and indexes for combinations of populations ------*/
/*------------------------------------------------------------------------------*/
// populations sizes from text file
if( (sizeFile != NULL && ancName == NULL) || (sizeFile != NULL && ancName != NULL && useLast == 1) ){
POPSIZE = new int[numPop];
for(int a=0; a<numPop; a++){
POPSIZE[a] = sizeMat.matrix[a][0];
}
}
else if( (sizeFile != NULL && ancName != NULL && useLast == 0) ){
POPSIZE = new int[numPop+1];
for(int a=0; a<numPop; a++)
POPSIZE[a] = sizeMat.matrix[a][0];
POPSIZE[numPop] = 1;
}
else if(sizeFile != NULL && useLast == 1){
POPSIZE = new int[numPop];
for(int a=0; a<numPop-1; a++)
POPSIZE[a] = sizeMat.matrix[a][0];
POPSIZE[numPop-1] = 1;
}
else{ //default with no sizeFile: every individual is a population
POPSIZE = new int[numPop];
for(int i=0; i<numPop; i++)
POPSIZE[i] = 1;
}
//check if the sizes of populations sum to the number of data files
int sumCheck=0;
for(int i=0; i<numPop; i++)
sumCheck += POPSIZE[i];
if(sumCheck != nIndFasta){
fprintf(stderr,"\t-> Error: Num of individuals in file \"%s\" is %d and different from num %d of data files\n",sizeFile,sumCheck,nIndFasta);
exit(0);
}
// cumulative populatios sizes (to get indexes for reading data in ABCD)
CUMPOPSIZE = new int[numPop+1];
CUMPOPSIZE[0] = 0;
int cumCont = 0;
for(int a=0; a<numPop; a++){
cumCont += POPSIZE[a];
CUMPOPSIZE[a+1] = cumCont;
}
//combinations of populations
numComb = (numPop-1)*(numPop-2)*(numPop-3)/2;
//print some information
if((useLast==1) || (useLast==0 && ancName==NULL))
fprintf(stderr,"\t-> %d Populations | %lu trees | %d Individuals\n", numPop, numComb, nIndFasta);
else if(useLast==0 && ancName!=NULL)
fprintf(stderr,"\t-> %d Populations | %lu trees | %d Individuals | %s Outgroup\n", numPop, numComb, nIndFasta, ancName);
SIZEABCD = new int*[numComb]; //indexes for reading ABCD2 data
int cont=0;
for(int i=0; i<numPop-1; i++){
for(int j=0; j<numPop-1; j++){
for(int k=0; k<numPop-1; k++){
if(j>i && j!=k && i!=k){
SIZEABCD[cont] = new int[3];
SIZEABCD[cont][0] = 4*i;
SIZEABCD[cont][1] = 4*j;
SIZEABCD[cont][2] = 4*k;
cont += 1;
}
}
}
}
/*ENDEND get population sizes and indexes for combinations of populations ------*/
/*------------------------------------------------------------------------------*/
//make output files
const char* postfix;
postfix=".abbababa2";
outfile = aio::openFile(outfiles,postfix);
// store large amounts of data for printing fast
bufstr.s=NULL;
bufstr.m=0;
bufstr.l=0;
//alleles pattern variable for printing
COMBprint = new double*[numComb];
for(int m=0; m<numComb; m++){
COMBprint[m] = new double[256];
for(int i=0; i<256; i++)
COMBprint[m][i] = 0;
}
//numerator and denominator for printing
NUMprint = new double[numComb];
DENprint = new double[numComb];
NSITEprint = new double[numComb];
for(int m=0; m<numComb; m++){
NUMprint[m]=0;
DENprint[m]=0;
NSITEprint[m]=0;
}
//print header
fprintf(outfile,"CHR\tBLOCKstart\tBLOCKend\tNumer\tDenom\tnumSites");
for(int a=0;a<4;a++)
for(int b=0;b<4;b++)
for(int c=0;c<4;c++)
for(int d=0;d<4;d++)
fprintf(outfile,"\t%d%d%d%d",a,b,c,d);
fprintf(outfile,"\n");
}//---end of abcDstat2::abcDstat2(const char *outfiles, argStruct *arguments,int inputtype)
//destructor
abcDstat2::~abcDstat2(){
free(ancName);
if(doAbbababa2==0)
return;
printAndEmpty(block*blockSize+1,currentChr);
if(outfile) fclose(outfile);
if(bufstr.s!=NULL)
free(bufstr.s);
if(sizeFile != NULL)
angsd::deleteMatrixInt(sizeMat);
for(int m=0; m<numComb; m++)
delete[] SIZEABCD[m];
delete[] SIZEABCD;
delete[] POPSIZE;
delete[] CUMPOPSIZE;
delete[] DENprint;
delete[] NUMprint;
delete[] NSITEprint;
for(int m=0; m<numComb; m++)
delete[] COMBprint[m];
delete[] COMBprint;
}
void abcDstat2::getBlockNum(int pos){
block=(int)((pos+1)/blockSize);
}
// returns number of blocks in chunk
int abcDstat2::getNumBlocks(funkyPars *pars){
int nBlocks = 1;
int blockHere = -1;// (int)((pars->posi[0]+1)/blockSize);
for(int s = 0 ; s < pars->numSites; s++){
if( pars->keepSites[s]==0 )
continue;
if( pars->posi[s]+1 >= blockHere*blockSize+blockSize ){
nBlocks++;
blockHere = (int)((pars->posi[s]+1)/blockSize);
}
}
return(nBlocks);
}
//clean after yourself
void abcDstat2::clean(funkyPars *pars){
if(doAbbababa2==0)
return;
funkyAbbababa2 *abbababaStruct =(funkyAbbababa2 *) pars->extras[index];
int totBlocks = abbababaStruct->NUMBLOCK;
for(int m=0; m<numComb; m++)
delete[] abbababaStruct->NUM[m];
delete[] abbababaStruct->NUM;
for(int m=0; m<numComb; m++)
delete[] abbababaStruct->DEN[m];
delete[] abbababaStruct->DEN;
for(int m=0; m<numComb; m++){
for(int j=0; j<totBlocks; j++)
delete[] abbababaStruct->COMB[m][j];
delete[] abbababaStruct->COMB[m];
}
for(int m=0; m<numComb; m++)
delete[] abbababaStruct->NSITE[m];
delete[] abbababaStruct->NSITE;
delete[] abbababaStruct->COMB;
delete[] abbababaStruct->BLOCKNUM;
delete abbababaStruct;
}//---end of abcDstat2::clean(funkyPars *pars)
//print and eventually reset counters
void abcDstat2::printAndEmpty(int blockAddress,int theChr){
double denCont=0;
for(int m=0; m<numComb; m++)
denCont += DENprint[m];
if(denCont != 0){
for(int m=0; m<numComb; m++){
fprintf(outfile,"%s\t%d\t%d\t%f\t%f\t%f",header->target_name[theChr],blockAddress-1,blockAddress+blockSize-2,NUMprint[m],DENprint[m],NSITEprint[m]);
for(int i=0;i<256;i++)
fprintf(outfile,"\t%f",COMBprint[m][i]);
fprintf(outfile,"\n");
}
}
for(int m=0; m<numComb; m++){
DENprint[m]=0;
NUMprint[m]=0;
NSITEprint[m]=0;
}
for(int m=0; m<numComb; m++)
for(int i=0; i<256; i++)
COMBprint[m][i]=0;
fflush(outfile);
}//---end of abcDstat2::printAndEmpty(int blockStart,int theChr)
void abcDstat2::print(funkyPars *pars){
if(doAbbababa2==0)
return;
funkyAbbababa2 *abbababaStruct = (funkyAbbababa2 *) pars->extras[index];//new
if(currentChr==-1){//if first chunk
for(int m=0; m<numComb; m++){
DENprint[m] = 0; //
NUMprint[m] = 0; //numerator for current block
NSITEprint[m] = 0;
}
for(int m=0; m<numComb; m++){
for(int i=0;i<256;i++)
COMBprint[m][i]=0;
}
getBlockNum(pars->posi[0]);
if(currentChr > pars->refId)
fprintf(stdout,"Warning. Your regions are not sorted. Becarefull...\n");
currentChr=pars->refId;
//start new block
//block = abbababaStruct->BLOCKNUM[0];
}
while(currentChr!=pars->refId){ //if new chr (not first)
//start new block
printAndEmpty(block*blockSize+1,currentChr);
currentChr=pars->refId;
block = abbababaStruct->BLOCKNUM[0];
//block = (int)((pars->posi[s]+1)/blockSize);
}
for(int b=0;b<abbababaStruct->NUMBLOCK;b++){
if(abbababaStruct->BLOCKNUM[b] > block){
printAndEmpty(block*blockSize+1,pars->refId);
block = abbababaStruct->BLOCKNUM[b];
}
for(int c=0;c<numComb;c++)
for(int pat=0;pat<256;pat++)
COMBprint[c][pat] += abbababaStruct->COMB[c][b][pat];
for(int c=0;c<numComb;c++){
NUMprint[c] += abbababaStruct->NUM[c][b];
DENprint[c] += abbababaStruct->DEN[c][b];
NSITEprint[c] += abbababaStruct->NSITE[c][b];
}
}//---end of for(int s=0;s<pars->numSites;s++)
}//---end of abcDstat2::print(funkyPars *pars)
void abcDstat2::run(funkyPars *pars){
if(doAbbababa2==0)
return;
//number of blocks
int totBlocks = 0;
int blockHere = -1;
for(int s = 0 ; s < pars->numSites; s++){
if( pars->keepSites[s]==0 )
continue;
if( pars->posi[s]+1 >= blockHere*blockSize+blockSize ){
totBlocks++;
blockHere = (int)((pars->posi[s]+1)/blockSize);
}
}
funkyAbbababa2 *abbababaStruct = new funkyAbbababa2; //new structure
//fprintf(stderr,"nindfasta %d\n",nIndFasta);
//abbababaStruct->NUMBLOCK = totBlocks;
abbababaStruct->NUMBLOCK = totBlocks;
//double ABCD[4*nIndFasta];
//for(int i = 0; i < 4*nIndFasta; i++)
// ABCD[i] = 0;
//double ABCD2[numPop*4];
//for(int i=0;i<numPop*4;i++)
// ABCD2[i*4] = 0;
double ***COMB;
COMB = new double**[numComb];
for(int m=0; m<numComb; m++){
COMB[m] = new double*[totBlocks];
for(int i=0; i<totBlocks; i++){
COMB[m][i] = new double[256];
for(int j=0; j<256; j++)
COMB[m][i][j] = 0;
}
}
double **NUM;
NUM = new double*[numComb];
for(int m=0; m<numComb; m++){
NUM[m] = new double[totBlocks];
for(int i=0; i<totBlocks; i++)
NUM[m][i] = 0;
}
double **DEN;
DEN= new double*[numComb];
for(int m=0; m<numComb; m++){
DEN[m] = new double[totBlocks];
for(int i=0; i<totBlocks; i++)
DEN[m][i] = 0;
}
int *BLOCKNUM;
BLOCKNUM = new int[totBlocks];
for(int i=0; i<totBlocks; i++)
BLOCKNUM[i] = 0;
int **NSITE;
NSITE = new int*[numComb];
for(int m=0; m<numComb; m++){
NSITE[m] = new int[totBlocks];
for(int i=0; i<totBlocks; i++)
NSITE[m][i] = 0;
}
//normalizing constants
double somma;
double normc;
int blockIdx = -1;
blockHere = -1;
for(int s=0;s<pars->numSites;s++){
//fprintf(stderr,"nindFasta2 %d\n",nIndFasta);
double ABCD[4*nIndFasta];
double ABCD2[4*numPop];
for(int i = 0; i < 4*nIndFasta; i++)
ABCD[i] = 0;
for(int i=0; i<numPop*4; i++)
ABCD2[i] = 0;
if(pars->keepSites[s]==0)
continue;
if( pars->posi[s]+1 >= blockHere*blockSize + blockSize ){
blockIdx++;
blockHere = (int)((pars->posi[s]+1)/blockSize);
BLOCKNUM[blockIdx] = blockHere;
}
for(int i=0;i<pars->nInd;i++){
//read the data
if(pars->counts[s][i*4] + pars->counts[s][i*4+1] + pars->counts[s][i*4+2] + pars->counts[s][i*4+3] == 0)
continue;//no data
if(pars->counts[s][i*4]<maxDepth && pars->counts[s][i*4+1]<maxDepth && pars->counts[s][i*4+2]<maxDepth && pars->counts[s][i*4+3]<maxDepth){
if(sample==1){
int dep=0;
for( int b = 0; b < 4; b++ )
dep+=pars->counts[s][i*4+b];
int j;
j = std::rand()%dep;
int cumSum=0;
for( int b = 0; b < 4; b++ ){
cumSum+=pars->counts[s][i*4+b];
if(cumSum > j){
ABCD[i*4+b] = 1;
break;
}
}
}
else{
for( int b = 0; b < 4; b++ )//{ //bases
ABCD[i*4+b]=pars->counts[s][i*4+b];
}
}
}
if(ancName != NULL && useLast == 0){
if(pars->anc[s] < 4)
ABCD[pars->nInd * 4 + pars->anc[s]] = 1;
else
continue;
}
//------do all the populationss------
double w[nIndFasta];//weights
for(int p=0; p<numPop; p++){
//---------------building weighted individual 1. written in ABCD2.
somma = 0;
normc = 0;
for(int i=CUMPOPSIZE[p];i<CUMPOPSIZE[p+1];i++){
w[i]=0;
somma = ABCD[i*4+0]+ABCD[i*4+1]+ABCD[i*4+2]+ABCD[i*4+3];
w[i] = (2*somma)/(somma + 1);
normc += w[i];
}
if(normc!=0){
for(int i=CUMPOPSIZE[p];i<CUMPOPSIZE[p+1];i++)
w[i] = w[i]/normc;
}
for(int i=CUMPOPSIZE[p];i<CUMPOPSIZE[p+1];i++)
//---------------building ABCD2 - weighted (pseudo) individuals
for(int al=0;al<4;al++){
for(int i=CUMPOPSIZE[p];i<CUMPOPSIZE[p+1];i++){
ABCD2[p*4 + al] += w[i]*ABCD[i*4+al];
}
}
//normalize
somma = 0;
for(int al=0;al<4;al++)
somma += ABCD2[p*4+al];
if(somma!=0){
for(int al=0;al<4;al++)
ABCD2[p*4+al] /= somma;}
}
/*-ENDENDEND--count WEIGHTED normalized allele combinations -----------------------*/
/*-------------------------------------------------------------------------------- */
if(enhance==1){
int enh=0;
for(int j=0;j<4;j++)
if(ABCD2[ (numPop-1)*4 + j]==0)
enh++;
if(enh!=3)
continue;
}
for(int m=0; m<numComb; m++){
double abba=0;
double baba=0;
double h1=0;
double h12=0;
double h123=0;
double h4=0;
double h1234=0;
int pattern=0;
double siteCont = 0;
for(int i=0;i<4;i++){
h1 = ABCD2[ SIZEABCD[m][0] + i];
if(h1==0){
pattern+=64; continue;}
for(int j=0;j<4;j++){
h12 = h1 * ABCD2[ SIZEABCD[m][1] + j];
if(h12==0){
pattern+=16; continue;}
for(int k=0;k<4;k++){
h123 = h12 * ABCD2[ SIZEABCD[m][2] + k];
if(h123==0){
pattern+=4; continue;}
for(int l=0;l<4;l++){
if( rmTrans==1 && (pattern==40 || pattern==130 || pattern==34 || pattern==136 ||pattern==125 || pattern==215 || pattern==119 || pattern==221) ){
pattern++; continue;}
h4=ABCD2[ (numPop-1)*4 + l];
if(h4==0){
pattern++;
}
else{
h1234 = h123 * h4;
COMB[m][blockIdx][pattern] += h1234;
siteCont += h1234;
if(i==l && j==k && i!=j)
abba += h1234;
if(i==k && j==l && i!=j)
baba += h1234;
pattern++;
}
}
}
}
}
NUM[m][blockIdx] += abba - baba;
DEN[m][blockIdx] += abba + baba;
NSITE[m][blockIdx] += siteCont;
}
}//---end for(int s=0;s<pars->numSites;s++)
abbababaStruct -> NUM=NUM;
abbababaStruct -> DEN=DEN;
abbababaStruct -> COMB=COMB;
abbababaStruct -> BLOCKNUM = BLOCKNUM;
abbababaStruct -> NSITE = NSITE;
pars -> extras[index] = abbababaStruct;
}