-
Notifications
You must be signed in to change notification settings - Fork 1
/
search_variants.c
337 lines (302 loc) · 8.45 KB
/
search_variants.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
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
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include "ga4gh_wrapper.h"
#include "ga4gh_client.h"
static void usage(void)
{
fprintf(stderr, "\n");
fprintf(stderr, "Required arguments:\n");
fprintf(stderr, "--variantSetIds -v <id1,id2,...> The IDs of the variant sets to search over.\n");
fprintf(stderr, "--referenceName -r <string> Only return variants on this reference.\n");
fprintf(stderr, "--start -s <positive int> The beginning of the window(1-bases,inclusive)\n");
fprintf(stderr, " for which overlapping variants should be returned.\n");
fprintf(stderr, "--end -e <positive int> The end of the window(1-bases,inclusive) for which\n");
fprintf(stderr, " overlapping variants should be returned.\n");
fprintf(stderr, "Optional arguments:\n");
fprintf(stderr, "--callSetIds -c <c_id1,c_id2,...> Only return variant calls which belong to call sets\n");
fprintf(stderr, " with these IDs.\n");
fprintf(stderr, "--variantName -n <string> Only return variants which have exactly this name\n");
exit(1);
}
static int count_ids(char *_ids)
{
int i=0;
//assuming there is not ',' at the very end of _ids and already validated not NULL.
int count = 1;
while(_ids[i]!='\0')
{
if(_ids[i]==',')
{
count++;
}
i++;
}
return count;
}
static void set_ids(char *setIds, char**request_, int size)
{
int i=0;
char* tok;
//request_ = (char**)malloc(size*sizeof(char*));
tok=strtok(setIds,",");
while(tok!=NULL)
{
request_[i] = tok;
tok = strtok(NULL,",");
}
}
//supported for one variantSetId
static char* create_request_string(search_variant_request* request,int id,int size_call)
{
char* request_string;
char request_start[20];
char request_end[20];
char* variant_name;
//compute size of request_string
int size = 127; //default characters
int i=0;
size+=strlen(request->variantSetIds[id]);
size+=strlen(request->name);
size+= 5*size_call;
for(i=0; i<size_call;i++)
{
size+=strlen(request->callSetIds[i]);
}
snprintf(request_start, sizeof(request_start),"%ld",request->start);
snprintf(request_end, sizeof(request_end),"%ld",request->end);
size+=strlen(request_start);
size+=strlen(request_end);
request_string = malloc(size);
if(strcmp(request->name,"null")==0)
{
variant_name = "null";
}else
{
variant_name = malloc(strlen(request->name)+4);
strcpy(variant_name,"\"");
strcat(variant_name,request->name);
strcat(variant_name,"\"");
}
strcpy(request_string,"{\"variantSetIds\":[\"");
strcat(request_string,request->variantSetIds[id]);
strcat(request_string,"\"],\"callSetIds\":[");
if(size_call > 0)
{
strcat(request_string,"\"");
strcat(request_string,request->callSetIds[0]);
strcat(request_string,"\"");
for(i=1; i<size_call; i++)
{
strcat(request_string,",\"");
strcat(request_string,request->callSetIds[i]);
strcat(request_string,"\"");
}
}
/*
for(i=0; i<size_call; i++)
{
strcat(request_string,"\"");
strcat(request_string,request->callSetIds[i]);
strcat(request_string,"\",");
//extra ',' at the end, need to fix it....
}
*/
strcat(request_string,"],\"variantName\":");
strcat(request_string,variant_name);
strcat(request_string,",\"referenceName\":\"");
strcat(request_string,request->referenceName);
strcat(request_string,"\",\"start\":");
strcat(request_string,request_start);
strcat(request_string,",\"end\":");
strcat(request_string,request_end);
strcat(request_string,",\"pageToken\":");
strcat(request_string,request->pageToken);
strcat(request_string,"}");
//printf("request string: %s\n",request_string);
return request_string;
}
static char* get_variantSetId_vcf_name(search_variant_request* request,int id)
{
char *vcf_file_name;
vcf_file_name = malloc(strlen(request->variantSetIds[id])+4);
strcpy(vcf_file_name,request->variantSetIds[id]);
strcat(vcf_file_name,".vcf");
return vcf_file_name;
}
static int create_vcf_file(char* name_vcf)
{
FILE* output_file;
output_file = fopen(name_vcf,"w");
//write header
fprintf(output_file,"##fileformat=VCFv4.1\n");
fprintf(output_file,"#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\n");
fclose(output_file);
return 0;
}
static int write_vcf_file(char* response,char* name_vcf)
{
FILE *output_file;
int i = 0;
int j = 0;
int size = 0;
int size_info,size_alt;
Variant *v = (Variant*)malloc(1*sizeof(Variant));
my_parse(response);
my_set_variants();
size = my_variants_size();
output_file = fopen(name_vcf,"a");
for(i=0; i<size; i++)
{
size_info = get_info_size(i);
size_alt = get_alt_size(i);
my_get_variant(i,v);
fprintf(output_file,"%s\t",v->referenceName);
fprintf(output_file,"%ld\t",v->start);
fprintf(output_file,"%s\t",v->id);
fprintf(output_file,"%s\t",v->referenceBases);
fprintf(output_file,"%s",v->alternateBases[0]);
for(j=1; j<size_alt; j++)
{
fprintf(output_file,",%s",v->alternateBases[j]);
}
fprintf(output_file,"\t");
fprintf(output_file,".\t");
fprintf(output_file,"PASS\t");
//consider case size_info = 0 ?
if(size_info!=0&&size_info!=-1)
{
fprintf(output_file,"%s",v->info_key[0]);
fprintf(output_file,"=%s",v->info_value[0]);
for(j = 1; j< size_info; j++)
{
fprintf(output_file,";%s",v->info_key[j]);
fprintf(output_file,"=%s",v->info_value[j]);
}
}else{
fprintf(output_file,".");
}
fprintf(output_file,"\t");
fprintf(output_file,"\n");
if(v->info_key!=NULL)
{
free(v->info_key);
}
if(v->info_value!=NULL)
{
free(v->info_value);
}
if(v->alternateBases!=NULL)
{
free(v->alternateBases);
}
}
fclose(output_file);
return 0;
}
int main_searchvariants(int argc, char* argv[],char *server_url)
{
int cmd;
int i;
search_variant_request *request=(search_variant_request*)malloc(1*sizeof(search_variant_request));
int size_variants = 0;
int size_calls = 0;
request->name = "null";
request->pageToken ="null";
char debug = 0;
static struct option long_options[]={
{"variantSetIds",required_argument,0,'v'},
{"referenceName",required_argument,0,'r'},
{"start",required_argument,0,'s'},
{"end",required_argument,0,'e'},
{"callSetIds",required_argument,0,'c'},
{"variantName",required_argument,0,'n'},
{"debug",no_argument,0,'d'},
{0,0,0,0}
};
//if(argc < 2)
while((cmd=getopt_long(argc,argv,"v:r:s:e:c:n:d",long_options,NULL))!=-1)
{
switch(cmd)
{
case 'v':
if(optarg==NULL||(strcmp(optarg,"")==0))
{
error("--variantSetIds string can't be empty.\n");
}
else
{
size_variants = count_ids(optarg);
request->variantSetIds = (char**)malloc(size_variants*sizeof(char*));
set_ids(optarg,request->variantSetIds,size_variants);
}
break;
case 'r':
if(optarg==NULL||(strcmp(optarg,"")==0))
{
error("--referenceName string can't be empty.\n");
}
else
{
request->referenceName = optarg;
}
break;
case 's': request->start = atol(optarg);
if(request->start < 0)
{
error("--start integer must be no negative.");
}
break;
case 'e': request->end = atol(optarg);
if(request->end < 0)
{
perror("--end integer must be no negative.");
}
break;
case 'c': if(optarg==NULL||(strcmp(optarg,"")==0))
{
size_calls = 0;
}
else
{
size_calls = count_ids(optarg);
request->callSetIds = (char**)malloc(size_calls*sizeof(char*));
set_ids(optarg,request->callSetIds,size_calls);
}
break;
case 'n': request->name = optarg; break;
case 'd': debug = 1; break;
case '?': usage();
default: error("Unknown argument %s\n",optarg);
}
}
if(size_variants==0)
{
usage();
}
start_user(server_url);
char* vcf_file_name;
//process each variantSet
for(i=0; i<size_variants; i++)
{
vcf_file_name = get_variantSetId_vcf_name(request,i);
create_vcf_file(vcf_file_name);
while(strcmp(request->pageToken,"NULL")!=0)
{
user->post_fields = create_request_string(request,i,size_calls);
//printf("post field string: %s \n",user->post_fields);
client_search_request(user,"variants");
//printf("%s\n",user->response);
write_vcf_file(user->response,vcf_file_name);
request->pageToken = get_pageToken();
//printf("%s \n",request->pageToken);
if(debug)
{
printf("%s\n",user->response);
}
}
}
end_user();
return 0;
}