forked from ANGSD/angsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
beagleReader.cpp
139 lines (113 loc) · 3.33 KB
/
beagleReader.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
#include <cassert>
#include "analysisFunction.h"
#include "beagleReader.h"
#include "aio.h"
beagle_reader::~beagle_reader(){
free(buffer);
}
beagle_reader::beagle_reader(gzFile gz_a,const aMap *revMap_a,int intName_a,int &nInd_a){
gz=gz_a;
revMap=revMap_a;
l=128;
intName = intName_a;
original=buffer =(char *) calloc(l,sizeof(char));
const char *delims = "\t \n";
int nCol=1;
aio::tgets(gz,&buffer,&l);
if(buffer!=original)
original=buffer;
strtok_r(buffer,delims,&buffer);
while(strtok_r(NULL,delims,&buffer))
nCol++;
if(nCol % 3 ){
fprintf(stderr,"\t-> Number of columns should be a multiple of 3, nCol=%d\n",nCol);
exit(0);
}
nInd_a=nCol/3-1;
nInd = nInd_a;
}
void parsepost(char *buffer,double *post,int nInd,const char *delims){
for(int i=0;i<nInd*3;i++){
char *tsk = strtok_r(NULL,delims,&buffer);
assert(tsk!=NULL);
post[i] = atof(tsk);
}
}
funkyPars *beagle_reader::fetch(int chunksize){
static const char *delims = "\t \n";
static const char *delims2 = "_\t \n";
double **post = new double*[chunksize];
funkyPars * myfunky =funkyPars_init();
myfunky->posi = new int[chunksize];
myfunky->major = new char[chunksize];
myfunky->minor = new char[chunksize];
for(int s=0;s<chunksize;s++)
post[s] = new double[nInd*3];
int nSites=0;
static int positions =0;//every site is a new position across different chunks
static int lastRefId =-1;
static int changed =0;
READAGAIN:
if(changed){
//parse an entire site:
//fprintf(stdout,"nSites %d\n",nSites);
myfunky->refId = lastRefId;
// fprintf(stdout,"BUF= %s\n",buffer);
myfunky->posi[nSites] = atoi(strtok_r(NULL,delims2,&buffer))-1;
myfunky->major[nSites] = refToChar[strtok_r(NULL,delims,&buffer)[0]];
myfunky->minor[nSites] = refToChar[strtok_r(NULL,delims,&buffer)[0]];
parsepost(buffer,post[nSites],nInd,delims);
nSites++;
changed =0;
}
buffer=original;
while(aio::tgets(gz,&buffer,&l)) {
if(buffer!=original)
original=buffer;
if(intName){
char *tok = strtok_r(buffer,delims2,&buffer);
aMap ::const_iterator it = revMap->find(tok);
if(it==revMap->end()){
fprintf(stderr,"\t-> Problem finding chr:%s from faifile\n",tok);
exit(0);
}
if(lastRefId==-1)
lastRefId = it->second;
if(lastRefId!=it->second){
changed =1;
lastRefId = it->second;
if(nSites==0) // if chromosome if finish then read next one
goto READAGAIN;
break;
}
lastRefId = it->second;
myfunky->refId = lastRefId;
myfunky->posi[nSites] = atoi(strtok_r(NULL,delims2,&buffer))-1;
}
else{
char *tok = strtok_r(buffer,delims,&buffer);
myfunky->refId = 0;
myfunky->posi[nSites] = positions++;
}
myfunky->major[nSites] = refToChar[strtok_r(NULL,delims,&buffer)[0]];
myfunky->minor[nSites] = refToChar[strtok_r(NULL,delims,&buffer)[0]];
parsepost(buffer,post[nSites],nInd,delims);
buffer=original;
nSites++;
if(nSites>=chunksize)
break;
}
if(nSites<chunksize){
for(int s=nSites;s<chunksize;s++)
delete[] post[s];
}
myfunky->nInd=nInd;
myfunky->post=post;
myfunky->numSites = nSites;
if(nSites==0 & changed==0){
fprintf(stdout,"Done reading beagle\n");
funkyPars_destroy(myfunky);
return(NULL);
}
return(myfunky);
}