forked from 7methylg/VCF-GP-to-DS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_dosage.c
85 lines (63 loc) · 1.46 KB
/
convert_dosage.c
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char* argv[]){
FILE *fp;
char command[500];
snprintf(command, 500, "bcftools query -f '%%INFO/AF\n[%%GP\n]' %s", argv[1]);
fp = popen(command , "r");
if (fp == NULL) {
printf("Unable to execute bcftools command");
}
FILE *info;
char file[70];
snprintf(file, 70, "%s.infos", argv[1]);
info = fopen(file, "r");
FILE *vcf;
char final[70];
snprintf(final, 70, "dosage-%s", argv[1]);
vcf = fopen(final, "a");
int line_read = -1;
int indivs = atoi(argv[2]);
float dosage_line[indivs];
char line[50];
char info_line[847400];
char *info_l = info_line;
char* extra;
float GP[3];
float af;
while(fgets(line, 50 , fp) != NULL){
if (line_read == -1){
af = (float) atof(line);
//printf("maf %f\n", af);
}
else{
GP[0] = strtof(strtok(line, ","), &extra);
//printf("first: %f\n", GP[0]);
GP[1] = strtof(strtok(NULL, ","), &extra);
//printf("second: %f\n", GP[1]);
GP[2] = strtof(strtok(NULL, ","), &extra);
//printf("third: %f\n", GP[2]);
if (af >= .5){
dosage_line[line_read] = GP[0] + GP[0] + GP[1];
}
else{
dosage_line[line_read] = GP[2] + GP[2] + GP[1];
}
}
line_read ++;
if (line_read == indivs){
fgets(info_line, 847400, info);
fprintf(vcf, "%s\t.\tDS", strtok(info_line, "\n"));
for (int i=0; i<=indivs-1; i++){
fprintf(vcf, "\t%f", dosage_line[i]);
}
fputs("\n", vcf);
line_read = -1;
}
}
fclose(info);
fclose(vcf);
pclose(fp);
return 0;
}