forked from ANGSD/angsd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeagleReader.cpp
More file actions
154 lines (125 loc) · 3.78 KB
/
Copy pathbeagleReader.cpp
File metadata and controls
154 lines (125 loc) · 3.78 KB
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
#include <zlib.h>
#include <cassert>
#include "analysisFunction.h"
#include "beagleReader.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){
char refToChar[256] = {
0,1,2,3,4,4,4,4,4,4,4,4,4,4,4,4,//15
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,//31
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,//47
0,1,2,3,4,4,4,4,4,4,4,4,4,4,4,4,//63
4,0,4,1,4,4,4,2,4,4,4,4,4,4,4,4,//79
4,4,4,4,3,4,4,4,4,4,4,4,4,4,4,4,//95
4,0,4,1,4,4,4,2,4,4,4,4,4,4,4,4,//111
4,4,4,4,3,4,4,4,4,4,4,4,4,4,4,4,//127
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,//143
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,//159
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,//175
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,//191
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,//207
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,//223
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,//239
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4//255
};
static const char *delims = "\t \n";
static const char *delims2 = "_\t \n";
double **post = new double*[chunksize];
funkyPars * myfunky =allocFunkyPars();
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;
if(changed){
//parse an entire site:
myfunky->refId = lastRefId;
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;
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){
deallocFunkyPars(myfunky);
return(NULL);
}
return(myfunky);
}