forked from aalbrechtsen/fastNGSadmix
-
Notifications
You must be signed in to change notification settings - Fork 4
/
readplinkV3.cpp
336 lines (266 loc) · 7.31 KB
/
readplinkV3.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
/*
Copyright (C) 2020 Emil Jorsboe - emil.jorsboe@bio.ku.dk
Kristian Hanghoj
Anders Albrechtsen
fastNGSadmix is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. For more see LICENSE file.
log:
g++ readplinkV3.cpp -lz -lpthread -O3 -o readplinkV3
debug:
g++ readplinkV3.cpp -lz -lpthread -ggdb -O3 -o readplinkV3
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <vector>
#include "readplinkV3.h"
#include <string>
void dallocFamFile(famFile &f){
delete [] f.sex;
delete [] f.phenotype;
}
void dallocBimFile(bimFile &b){
delete [] b.chr;
delete [] b.recombrate;
delete [] b.position;
delete [] b.minor;
delete [] b.major;
}
// bed file reader modified from snpMatrix by Clayton and Hintak Leung 2007
/*
output will be genotype[ind][nsites]
g \in {0,1,2,3},counts of allele2, 3=NA/missing
*/
unsigned char **readbed(const char* file, int nrow,int ncol) {
int i;
const unsigned char recode[4] = {'\x01', '\x00', '\x02', '\x03'};
const unsigned char mask = '\x03';
FILE *in = fopen(file, "r");
if (!in){
fprintf(stderr,"Couldn't open input file: %s\n", file);
exit(0);
}
unsigned char start[3];
if (fread(start, 1, 3, in)!=3){
fprintf(stderr,"Failed to read first 3 bytes");
exit(0);
}
if (start[0]!='\x6C' || start[1]!='\x1B'){
fprintf(stderr,"Input file does not appear to be a .bed file (%X, %X)",
start[0], start[1]);
exit(0);
}
/* Create output object */
unsigned char** results =(unsigned char**) calloc(nrow,sizeof(unsigned char*));
for(i=0;i<nrow;i++)
results[i] =(unsigned char*) calloc(ncol,sizeof(unsigned char));
/* Read in data */
int snp_major = start[2];
int part=0, ij=0, j=0;i=0;
while (1) {
unsigned char byte;
if (!part) {
if (feof(in) || !fread(&byte, 1, 1, in)) {
printf("Unexpected end of file reached");
exit(0);
}
part = 4;
}
unsigned char code = byte & mask;
byte = byte >> 2;
part--;
unsigned char tmp = recode[code];
if(tmp==0)
results[i][j] = 3;
else
results[i][j] =tmp-1;
assert(results[i][j]>=0 && results[i][j]<=3);
if (snp_major) {
ij++;
i++;
if (i==nrow) {
i = part = 0;
j++;
if (j==ncol)
break;
}
}
else {
ij += nrow;
j++;
if (j==ncol){
j = part = 0;
i++;
if (i==nrow)
break;
ij = i;
}
}
}
fclose(in);
return results;
}
// reads fam file
famFile readFamFile(const char* fname, int lines) {
FILE* pFile;
char buffer [10000];
const char *delims = "\t \n";
pFile = fopen (fname , "r");
if (pFile == NULL) {
perror ("Error opening fam file");
exit(0);
}
famFile fam;
fam.individuals = lines;
fam.sex = new int[lines];
fam.phenotype = new double[lines];
int famIndex=0;
while(fgets(buffer,sizeof(buffer), pFile) != NULL){
char* familyID = strtok(buffer,delims);
std::string familyIDstring(familyID, strlen(familyID));
fam.familyID.push_back(familyIDstring);
char* individualID = strtok(NULL,delims);
std::string individualIDstring(individualID, strlen(individualID));
fam.individualID.push_back(individualIDstring);
char* paternalID = strtok(NULL,delims);
std::string paternalIDstring(paternalID, strlen(paternalID));
fam.paternalID.push_back(paternalIDstring);
char* maternalID = strtok(NULL,delims);
std::string maternalIDstring(maternalID, strlen(maternalID));
fam.maternalID.push_back(maternalIDstring);
fam.sex[famIndex] = atoi(strtok(NULL,delims));
fam.phenotype[famIndex] = atof(strtok(NULL,delims));
famIndex++;
}
//close file and return fam file
fclose (pFile);
return(fam);
}
// read bim file with chr_pos ids
bimFile readBimFile(const char* fname,int lines) {
FILE* pFile;
char buffer [10000];
const char *delims = "\t \n";
pFile = fopen (fname , "r");
if (pFile == NULL) {
perror ("Error opening fam file");
exit(0);
}
bimFile bim;
bim.sites = lines;
bim.chr = new int[lines];
bim.recombrate = new double[lines];
bim.position = new int[lines];
bim.minor = new char[lines];
bim.major = new char[lines];
// chr_pos id here
int bimIndex=0;
while(fgets(buffer,sizeof(buffer), pFile) != NULL){
char* chrChar = strtok(buffer,delims);
bim.chr[bimIndex] = atoi(chrChar);
char* rs = strtok(NULL,delims);
std::string rsString(rs, strlen(rs));
bim.rs.push_back(rsString);
bim.recombrate[bimIndex] = atof(strtok(NULL,delims));
char* posChar = strtok(NULL,delims);
bim.position[bimIndex] = atoi(posChar);
bim.minor[bimIndex] = strtok(NULL,delims)[0];
bim.major[bimIndex] = strtok(NULL,delims)[0];
std::string chrString(chrChar,strlen(chrChar));
std::string posString(posChar,strlen(posChar));
std::string bimID = chrString + "_" + posString;
// for concatenating strings now also with alleles
if( tolower(bim.minor[bimIndex]) < tolower(bim.major[bimIndex]) ){
bimID = bimID + "_" + bim.minor[bimIndex] + "_" + bim.major[bimIndex];
} else{
bimID = bimID + "_" + bim.major[bimIndex] + "_" + bim.minor[bimIndex];
}
bim.id.push_back(bimID);
bimIndex++;
}
//closing file and returning bim file
fclose (pFile);
return(bim);
}
//counts line of file
int nlines(const char *fname){
FILE *in =NULL;
if(!((in=fopen(fname,"r")))){
fprintf(stderr,"Problem opening file: %s\n",fname);
return 0;
}
int c;
int n=0;
while ( (c=fgetc(in)) != EOF )
if ( c == '\n' ) n++;
fclose(in);
return n;
}
void kill_plink(plink &p){
for(int i=0;i<p.x;i++){
free(p.d[i]);
}
dallocBimFile(p.bim);
dallocFamFile(p.fam);
free(p.d);
}
plink readplink(const char *str){
plink p;
char *fname =(char*) malloc(strlen(str)+5);
strcpy(fname,str);strcpy(fname+strlen(fname),".bim");
int ncol = nlines(fname);
bimFile bim;
bim = readBimFile(fname,ncol);
strcpy(fname+strlen(str),".fam");
int nrow = nlines(fname);
famFile fam;
fam = readFamFile(fname,nrow);
// nrow=3;
if(ncol==0||nrow==0){
fprintf(stderr,"Invalid binary plink file! \n");
exit(0);
}
strcpy(fname+strlen(str),".bed");
unsigned char **dat = readbed(fname,nrow,ncol);
p.x=nrow;
p.y=ncol;
p.d=dat;
p.bim=bim;
p.fam=fam;
free(fname);
return p;
}
#ifdef __WITH_MAIN__
int main(int argc,char **argv){
int i,j;
if(argc==1){
fprintf(stderr,"Supply prefix for plink binary files\n");
return 0;
}
char *fname =(char*) malloc(strlen(argv[1])+5);
strcpy(fname,argv[1]);strcpy(fname+strlen(fname),".bim");
int ncol = nlines(fname);
strcpy(fname+strlen(argv[1]),".fam");
int nrow = nlines(fname);
if(ncol==0||nrow==0)
return 0;
fprintf(stderr,"ncol:%d\tnrow:%d\n",ncol,nrow);
strcpy(fname+strlen(argv[1]),".bed");
unsigned char **dat = readbed(fname,nrow,ncol);
#if 1
for(i=0;i<ncol;i++){
for(j=0;j<nrow;j++)
fprintf(stdout,"%d ",dat[j][i]);
fprintf(stdout,"\n");
}
#endif
for(i=0;i<nrow;i++)
free(dat[i]);
free(dat);
free(fname);
return 0;
}
#endif