-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathshared.hpp
377 lines (331 loc) · 9.01 KB
/
shared.hpp
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
/// TEMPLATES
#include <string> //for str operations
#include <unistd.h>
// a general matrix style structure
template <typename T>
struct matrix{
int x;
int y;
T** data;
};
// a general array style structure
template <typename T>
struct array{
int x;
T* data;
};
template <typename T>
T *collapse(std::vector<T> &v){
T *tmp = new T[v.size()];
for(int i=0;i<v.size();i++)
tmp[i] = v[i];
return tmp;
}
//function to cleanup our generic matrix structure
template <typename T>
void cleanup(matrix<T> &m){//using a reference to avoid copying the data
for(int i=0;i<m.x;i++)
delete [] m.data[i];
delete [] m.data;
}
/// FUNCTIONS
array<int> getStart(int nsites, int firstbase, int block_size) {
// note that firstbase and nsites are 1-based
int len = nsites-firstbase+1;
int nwin = len/block_size;
if ( (len % block_size)!=0) nwin=nwin+1;
array<int> start;
start.x=nwin;
int *tStart= new int [nwin];
for (int i=0; i<nwin; i++) {
tStart[i]=(i)*block_size;
}
// if you dont start from beginning
if (firstbase>1) {
for (int i=0; i<nwin; i++) {
tStart[i]=tStart[i]+firstbase-1; // -1 because firstbase is 1-based
}
}
start.data=tStart;
return start;
}
array<int> getEnd(int nsites, int firstbase, int block_size) {
// note that firstbase and nsites are 1-based
int len = nsites-firstbase+1;
int nwin = len/block_size;
if ( (len % block_size)!=0) nwin=nwin+1;
array<int> end;
end.x=nwin;
int *tEnd= new int [nwin];
for (int i=0; i<nwin; i++) {
tEnd[i]=(i)*block_size+block_size-1;
}
tEnd[nwin-1]=nsites-1; // nsites is 1 based
// if you dont start from beginning
if (firstbase>0) {
for (int i=0; i<nwin; i++) {
tEnd[i]=tEnd[i]+firstbase-1;
}
}
end.data=tEnd;
return end;
}
// get the filesize of a file
size_t fsize(const char* fname){
struct stat st ;
stat(fname,&st);
return st.st_size;
}
// find out if a file exists
int fexists(const char* str) {
struct stat buffer ;
return (stat(str, &buffer )==0 );
}
// to append names
char *append(const char* a,const char *b){
char *c =(char *) malloc((strlen(a)+strlen(b)+1)*sizeof(char));
strcpy(c,a);
strncat(c,b,strlen(b));
return c;
}
// a nice wrapper for getting files
FILE *getFILE(const char* fname,const char* mode) {
FILE *fp;
int writeFile = 0;
for(size_t i=0;i<strlen(mode);i++)
if(mode[i]=='w')
writeFile = 1;
if(writeFile && fexists(fname)){
fprintf(stderr,"\t-> File exists: %s exiting...\n",fname);
exit(0);
}
if(strcmp(fname, "-") == 0){
if(writeFile)
fp = stdout;
else
fp = stdin;
} else {
if(NULL==(fp=fopen(fname,mode))){
fprintf(stderr,"\t->Error opening FILE handle for file:%s exiting\n",fname);
exit(0);
}
}
if(isatty(fileno(fp))) {
fprintf(stderr, "Your stdin/stdout is not a pipe, this might not be what you want!\n");
exit(0);
}
return fp;
}
// read a file of prior into an array
array<double> readArray(const char *fname, int nInd) {
FILE *fp = getFILE(fname,"r");
size_t filesize =fsize(fname);
if(filesize==0){
fprintf(stderr,"file:%s looks empty\n",fname);
exit(0);
}
int len = 2*nInd+1;
char *buf = new char[filesize];
double *tmp = new double[len];
fread(buf,sizeof(char),filesize,fp);
tmp[0] = atof(strtok(buf,"\t \n"));
for(int i=1;i<(len);i++)
tmp[i] = atof(strtok(NULL,"\t \n"));
fclose(fp);
delete [] buf;
array<double> ret;
ret.x = len;
ret.data = tmp;
return ret;
}
// read 2d sfs
matrix<double> readPrior12(const char *fname, int nrow, int ncol) {
FILE *fp = getFILE(fname,"r");
size_t filesize =fsize(fname);
if(filesize==0){
fprintf(stderr,"file:%s looks empty\n",fname);
exit(0);
}
double *tmp = new double[nrow*ncol];
char *buf = new char[filesize];
fread(buf,sizeof(char),filesize,fp);
tmp[0] = atof(strtok(buf,"\t \n"));
for(int i=1;i<(nrow*ncol);i++)
tmp[i] = atof(strtok(NULL,"\t \n"));
fclose(fp);
delete [] buf;
array<double> allvalues;
allvalues.x = nrow*ncol;
allvalues.data = tmp;
int index=0;
double **data = new double*[nrow];
for(int i=0;i<nrow;i++){
double *tmp2 = new double[ncol];
for(int k=0;k<ncol;k++) {
tmp2[k]=allvalues.data[index];
index=index+1;
}
data[i]= tmp2;
}
matrix<double> ret;
ret.x=nrow;
ret.y=ncol;
ret.data=data;
delete [] allvalues.data;
return ret;
}
// read a file of posterior probabilities into a matrix but only for a specific subsets of positions (0-based notation)
matrix<double> readFileSub(char *fname, int nInd, int start, int end) {
FILE *fp = getFILE(fname,"r");
size_t filesize =fsize(fname);
int n_categ = 2*nInd+1;
if( strcmp(fname,"-")!=0 ) {
if( filesize % (n_categ*sizeof(float)) != 2*sizeof(float) ) {
fprintf(stderr,"\n\t-> Possible error reading SFS, binary file might be broken...\n");
exit(-1);
}
}
int nsites = end-start+1;
double **data = new double*[nsites];
float float_tmp = 0;
// Locate data to read
fseek(fp, (2+n_categ*start)*sizeof(float), SEEK_SET);
// Read data
for(int i=0; i<nsites; i++) {
double *tmp = new double[n_categ];
for(int c=0; c<n_categ; c++){
fread(&float_tmp, sizeof(float), 1, fp);
tmp[c] = (double) float_tmp;
}
data[i] = tmp;
}
fclose(fp);
matrix<double> ret;
ret.x = nsites;
ret.y = n_categ;
ret.data = data;
return ret;
}
// read genotype posterior probabilities from angsd (-dogeno 64), but only for a specific subsets of positions (0-based notation)
matrix<double> readEstiSub(char *fname, int nInd, int start, int end) {
FILE *fp = getFILE(fname,"rb");
size_t filesize =fsize(fname);
if( strcmp(fname,"-")!=0 && (filesize %(sizeof(double)*3*nInd)) ) {
fprintf(stderr,"\n\t-> Possible error read GENO, binary file might be broken...\n");
exit(-1);
}
int nsites = end-start+1;
double **data = new double*[nsites];
fseek(fp, sizeof(double)*(3*nInd)*start, SEEK_SET);
for(int i=0; i<nsites; i++) {
double *tmp = new double[3*nInd];
fread(tmp,sizeof(double),3*nInd,fp);
data[i]= tmp;
}
fclose(fp);
matrix<double> ret;
ret.x = nsites;
ret.y = 3*nInd;
ret.data = data;
return ret;
}
// read genotype quality (boolean), analysis only on sites to be kept (1) and discard the rest (0)
array<int> readGenoQuality(const char *fname, int nsites) {
// nsites is how many sites you want
FILE *fp = getFILE(fname,"r");
size_t filesize =fsize(fname);
if( strcmp(fname,"-")!=0 && filesize==0){
fprintf(stderr,"file:%s looks empty\n",fname);
exit(0);
}
int *tmp = new int[nsites];
char *buf = new char[filesize];
fread(buf,sizeof(char),filesize,fp);
tmp[0] = atoi(strtok(buf,"\t \n"));
for(int i=1;i<(nsites);i++)
tmp[i] = atoi(strtok(NULL,"\t \n"));
fclose(fp);
array<int> allvalues;
allvalues.x = nsites;
allvalues.data = tmp;
return allvalues;
}
// return max value position of a row from a matrix of geno likes or post probs for a specific individual
int maxposarr(matrix<double> &m, int row, int start, int end) {
double val = m.data[row][start];
for (int i = start; i < end; i++) {
if (m.data[row][i] > val) {
start = i;
val = m.data[row][i];
}
}
return start;
}
// normalize SFS and exp it if log
void normSFS(matrix<double> &sfs, bool islog) {
int nsites = sfs.x;
int ncol = sfs.y;
double somma = 0.0;
for (int j=0; j<nsites; j++) {
// get the sum of values (do exp if they are in log scale)
somma = 0;
for(int i=0; i<ncol; i++) {
if (islog) {
somma = somma + exp(sfs.data[j][i]);
} else {
somma = somma + sfs.data[j][i];
}
}
// divide each value for the sum
for(int i=0; i<ncol; i++) {
if (islog) {
sfs.data[j][i] = exp(sfs.data[j][i]) / somma;
} else {
sfs.data[j][i] = sfs.data[j][i] / somma;
}
}
}
}
// get probability of being variable from posterior probabilities of sample allele frequencies
void getPvar(matrix<double> &sfs, array<double> pvar) {
for (int i=0; i<sfs.x; i++)
pvar.data[i]=1-sfs.data[i][0]-sfs.data[i][2*(sfs.y-1)];
}
// write an array of doubles into a file
void writearray(array<double> &m,FILE *fp) {
for(int i=0;i<m.x;i++)
fprintf(fp,"%f\t",m.data[i]);
fprintf(fp,"\n");
}
// write an array of ints into a file
void writearrayInt(array<int> &m,FILE *fp) {
for(int i=0;i<m.x;i++)
fprintf(fp,"%d\t",m.data[i]);
fprintf(fp,"\n");
}
// write a matrix of doubles into a file
void writematrix(matrix<double> &m,FILE *fp) {
for(int i=0;i<m.x;i++){
for(int j=0;j<m.y;j++)
fprintf(fp,"%f\t",m.data[i][j]);
fprintf(fp,"\n");
}
}
// write a matrix of ints into a file
void writematrixInt(matrix<int> &m,FILE *fp) {
for(int i=0;i<m.x;i++){
for(int j=0;j<m.y;j++)
fprintf(fp,"%d\t",m.data[i][j]);
fprintf(fp,"\n");
}
}
// add prior
void addPrior(matrix<double> &sfs, array<double> prior) {
int nsites = sfs.x;
int ncol = sfs.y;
for (int j=0; j<nsites; j++) {
for(int i=0; i<ncol; i++) {
sfs.data[j][i] = sfs.data[j][i]*prior.data[i];
}
}
}