-
Notifications
You must be signed in to change notification settings - Fork 51
/
argStruct.cpp
426 lines (392 loc) · 11.2 KB
/
argStruct.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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#include <libgen.h>//for checking if output dir exists 'dirname'
#include <fstream>
#include "version.h"
#include "argStruct.h"
#include "aio.h"
#define ARGS ".arg"
#define LENS 10000
void whatIsTheInput(int type){
switch(type){
case INPUT_BAM:
fprintf(stderr,"\t-> Inputtype is BAM/CRAM\n");
break;
case INPUT_GLF:
fprintf(stderr,"\t-> Inputtype is GLF\n");
break;
case INPUT_BEAGLE:
fprintf(stderr,"\t-> Inputtype is beagle\n");
break;
case INPUT_BGEN:
fprintf(stderr,"\t-> Inputtype is bgen\n");
break;
case INPUT_PILEUP:
fprintf(stderr,"\t-> Inputtype is pileup\n");
break;
case INPUT_VCF_GP:
fprintf(stderr,"\t-> Inputtype is vcf/bcf gp\n");
break;
case INPUT_VCF_GL:
fprintf(stderr,"\t-> Inputtype is vcf/bcf gl\n");
break;
case INPUT_GLF10_TEXT:
fprintf(stderr,"\t-> Inputtype is GLF10 text\n");
break;
default:{
fprintf(stderr,"\t-> Unknown input type\n");
exit(0);
}
}
}
void setInputType(argStruct *args){
#if 0
fprintf(stderr,"bam:%d\n",INPUT_BAM);
fprintf(stderr,"glf:%d\n",INPUT_GLF);
fprintf(stderr,"glf3:%d\n",INPUT_GLF3);
fprintf(stderr,"blg:%d\n",INPUT_BEAGLE);
fprintf(stderr,"plp:%d\n",INPUT_PILEUP);
fprintf(stderr,"vcf_gp:%d\n",INPUT_VCF_GP);
fprintf(stderr,"vcf_gl:%d\n",INPUT_VCF_GL);
fprintf(stderr,"glf10_text:%d\n",INPUT_GLF10_TEXT);
fprintf(stderr,"bgen:%d\n",INPUT_BGEN);
#endif
if(args->fai){
free(args->fai);
args->fai=NULL;
}
args->fai = angsd::getArg("-fai",args->fai,args);
char *tmp =NULL;
tmp = angsd::getArg("-glf",tmp,args);
if(tmp!=NULL){
args->inputtype=INPUT_GLF;
args->infile = tmp;
args->nams.push_back(strdup(args->infile));
return;
}
free(tmp);
tmp = NULL;
tmp = angsd::getArg("-glf3",tmp,args);
if(tmp!=NULL){
args->inputtype=INPUT_GLF3;
args->infile = tmp;
args->nams.push_back(strdup(args->infile));
return;
}
free(tmp);
tmp = NULL;
tmp = angsd::getArg("-glf10_text",tmp,args);
if(tmp!=NULL){
args->inputtype=INPUT_GLF10_TEXT;
args->infile = tmp;
args->nams.push_back(strdup(args->infile));
return;
}
free(tmp);
tmp=NULL;
tmp = angsd::getArg("-vcf-GP",tmp,args);
if(tmp!=NULL){
args->inputtype=INPUT_VCF_GP;
args->infile = tmp;
args->nams.push_back(strdup(args->infile));
return;
}
free(tmp);
tmp=NULL;
tmp = angsd::getArg("-vcf-GL",tmp,args);
if(tmp!=NULL){
args->inputtype=INPUT_VCF_GL;
args->infile = tmp;
args->nams.push_back(strdup(args->infile));
return;
}
free(tmp);
tmp=NULL;
tmp = angsd::getArg("-vcf-PL",tmp,args);
if(tmp!=NULL){
args->inputtype=INPUT_VCF_GL;
args->infile = tmp;
args->nams.push_back(strdup(args->infile));
return;
}
free(tmp);
tmp=NULL;
tmp = angsd::getArg("-pileup",tmp,args);
if(tmp!=NULL){
args->inputtype=INPUT_PILEUP;
args->infile = tmp;
args->nams.push_back(strdup(args->infile));
return;
}
free(tmp);
tmp=NULL;
tmp = angsd::getArg("-beagle",tmp,args);
if(tmp!=NULL){
args->inputtype=INPUT_BEAGLE;
args->infile = tmp;
args->nams.push_back(strdup(args->infile));
return;
}
free(tmp);
tmp=NULL;
tmp = angsd::getArg("-bgen",tmp,args);
if(tmp!=NULL){
args->inputtype=INPUT_BGEN;
args->infile = tmp;
args->nams.push_back(strdup(args->infile));
return;
}
free(tmp);
tmp=NULL;
tmp = angsd::getArg("-i",tmp,args);
if(tmp!=NULL){
args->inputtype=INPUT_BAM;
args->infile = tmp;
args->nams.push_back(strdup(args->infile));
return;
}
int nInd = 0;
nInd = angsd::getArg("-nInd",nInd,args);
free(tmp);
tmp=NULL;
tmp = angsd::getArg("-bam",tmp,args);
tmp = angsd::getArg("-b",tmp,args);
if(tmp!=NULL && args->argc>2){
args->inputtype=INPUT_BAM;
args->infile = tmp;
args->nams = angsd::getFilenames(args->infile,nInd);
return;
}
if(args->inputtype==INPUT_BAM){
}
}
void checkIfDir(char *fname){
char *dirNam2 = strdup(fname);
char *dirNam=dirname(dirNam2);
if(strlen(dirNam)>1 &&!aio::fexists(dirNam)){
fprintf(stderr,"\t Folder: \'%s\' doesn't exist, please create\n",dirNam);
exit(0);
}
free(dirNam2);
}
argStruct *setArgStruct(int argc,char **argv) {
argStruct *arguments = new argStruct;
arguments->cmdline = NULL;
arguments->version = NULL;
arguments->argumentFile = stderr;
arguments->outfiles =NULL;
arguments->fai=arguments->anc=arguments->ref=NULL;
arguments->hd=NULL;
arguments->revMap= NULL;
arguments->argc=argc;
arguments->argv=argv;
arguments->nReads = 50;
arguments->sm=NULL;
arguments->sm=bam_smpl_init();
ASSERT(arguments->sm);
arguments->usedArgs= new int[argc+1];//well here we allocate one more than needed, this is only used in the ./angsd -beagle version
for(int i=0;i<argc;i++)
arguments->usedArgs[i]=0;
arguments->inputtype=-1;
arguments->infile = NULL;
arguments->show =0;
arguments->fai = NULL;
kstring_t kstr;
kstr.s=NULL;kstr.l=kstr.m=0;
ksprintf(&kstr,"angsd version: %s (htslib: %s) build(%s %s)\n",ANGSD_VERSION,hts_version(),__DATE__,__TIME__);
arguments->version = strdup(kstr.s);
kstr.l=0;
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
ksprintf (&kstr, "%s", asctime (timeinfo) );
arguments->datetime = strdup(kstr.s);
kstr.l = 0;
for(int i=0;i<argc;i++)
ksprintf(&kstr,"%s ",argv[i]);
arguments->cmdline = kstr.s;
fprintf(stderr,"\t-> %s",arguments->version);
fprintf(stderr,"\t-> %s\n",arguments->cmdline);
//check output filename
arguments->outfiles = angsd::getArg("-out",arguments->outfiles,arguments);
if(arguments->outfiles==NULL){
if(argc!=2)
fprintf(stderr,"\t-> No \'-out\' argument given, output files will be called \'angsdput\'\n");
arguments->outfiles = strdup("angsdput");
}
checkIfDir(arguments->outfiles);
if(argc<=2)
return arguments;
//print arguments into logfile
if(argc>2)
arguments->argumentFile=aio::openFile(arguments->outfiles,ARGS);
fprintf(arguments->argumentFile,"\t-> %s",arguments->version);
fprintf(arguments->argumentFile,"\t-> %s\n",arguments->cmdline);
setInputType(arguments);
whatIsTheInput(arguments->inputtype);
return arguments;
}
int angsd::getArg(const char* argName,int type,argStruct *arguments){
int argPos = 1;
while(argPos <arguments->argc){
if (strcasecmp(arguments->argv[argPos],argName)==0){
if(arguments->argc==2)
return(-999);
// fprintf(stderr,"HIT %s vs %s\n",argName,arguments->argv[argPos]);
arguments->usedArgs[argPos]=1;
arguments->usedArgs[argPos+1]=1;
if(argPos==arguments->argc-1){
fprintf(stderr,"\t-> Must supply a parameter for: %s\n",argName);
exit(0);
}
return(atoi(arguments->argv[argPos+1]));
}
argPos++;
}
return(type);
}
char* angsd::getArg(const char* argName,char* type,argStruct *arguments){
///fprintf(stderr,"pre %p %s \n",type,argName);
int argPos = 1;
while(argPos <arguments->argc){
if (strcasecmp(arguments->argv[argPos],argName)==0){
if(arguments->argc==2){
return(strdup("-999"));
}
arguments->usedArgs[argPos]=1;
arguments->usedArgs[argPos+1]=1;
if(argPos==arguments->argc-1){
fprintf(stderr,"\t-> Must supply a parameter for: %s\n",argName);
exit(0);
}
return(strdup(arguments->argv[argPos+1])); //VALGRIND says leak. don't care very small DRAGON
}
argPos++;
}
// fprintf(stderr,"post %p\n",type);
return(type);
}
char* angsd::getArg(const char* argName, const char* type,argStruct *arguments){
//fprintf(stderr,"pre %p %s \n",type,argName);
int argPos = 1;
while(argPos <arguments->argc){
if (strcasecmp(arguments->argv[argPos],argName)==0){
if(arguments->argc==2){
return(strdup("-999"));
}
arguments->usedArgs[argPos]=1;
arguments->usedArgs[argPos+1]=1;
if(argPos==arguments->argc-1){
fprintf(stderr,"\t-> Must supply a parameter for: %s\n",argName);
exit(0);
}
return(strdup(arguments->argv[argPos+1])); //VALGRIND says leak. don't care very small DRAGON
}
argPos++;
}
//ASSERT(0==1);
// fprintf(stderr,"post %p\n",type);
return NULL;
}
float angsd::getArg(const char* argName,float type,argStruct *arguments){
int argPos = 1;
while(argPos <arguments->argc){
if (strcasecmp(arguments->argv[argPos],argName)==0){
if(arguments->argc==2)
return(-999);
arguments->usedArgs[argPos]=1;
arguments->usedArgs[argPos+1]=1;
if(argPos==arguments->argc-1){
fprintf(stderr,"\t-> Must supply a parameter for: %s\n",argName);
exit(0);
}
return(atof(arguments->argv[argPos+1]));
}
argPos++;
}
return(type);
}
double angsd::getArg(const char* argName,double type,argStruct *arguments){
int argPos = 1;
while(argPos <arguments->argc){
if (strcasecmp(arguments->argv[argPos],argName)==0){
if(arguments->argc==2)
return(-999);
arguments->usedArgs[argPos]=1;
arguments->usedArgs[argPos+1]=1;
if(argPos==arguments->argc-1){
fprintf(stderr,"\t-> Must supply a parameter for: %s\n",argName);
exit(0);
}
return(atof(arguments->argv[argPos+1]));
}
argPos++;
}
return(type);
}
std::vector<char*> angsd::getFilenames(const char * name,int nInd){
if(strchr(name,'\r')){
fprintf(stderr,"\t\t-> Filelist contains carriage return. Looks like a windows file please remove hidden \'\r\' from filelist\n");
exit(0);
}
if(!aio::fexists(name)){
fprintf(stderr,"[%s]\t-> Problems opening file: %s\n",__FUNCTION__,name);
exit(0);
}
const char* delims = " \t";
std::vector<char*> ret;
std::ifstream pFile(name,std::ios::in);
char buffer[LENS];
while(!pFile.eof()){
pFile.getline(buffer,LENS);
char *tok = strtok(buffer,delims);
while(tok!=NULL){
if(tok[0]!='#')
ret.push_back(strdup(buffer));
tok = strtok(NULL,delims);
}
}
if(nInd>0) {
if(ret.size()<nInd)
fprintf(stderr,"\t-> Number of samples is smaller than subset requested %lu vs %d\n",ret.size(),nInd);
else{
// fprintf(stderr,"\t-> Will remove tail of filename list\n");
for(int ii=nInd;ii<ret.size();ii++)
free(ret[ii]);
ret.erase(ret.begin()+nInd,ret.end());//we don't free this memory, it doesn't really matter
// fprintf(stderr,"\t-> filename list now contains only: %lu\n",ret.size());
}
for(size_t ii=0;ii<ret.size();ii++)
if(strchr(ret[ii],'\r')){
fprintf(stderr,"\t\t-> Filelist: \'%s\' contains carriage return. Looks like a windows file please remove hidden \'\r\' from: \'%s\'\n",name,ret[ii]);
exit(0);
}
#if 0
for(size_t ii=0;ii<ret.size();ii++)
fprintf(stderr,"%zu->%s\n",ii,ret[ii]);
fprintf(stderr,"\n");
#endif
}
return ret;
}
void destroy_argStruct(argStruct *args){
if(args->cmdline)
free(args->cmdline);
if(args->version)
free(args->version);
if(args->datetime)
free(args->datetime);
if(args->sm)
bam_smpl_destroy(args->sm);
for(unsigned i=0;i<args->nams.size();i++)
free(args->nams[i]);
if(args->fai)
free(args->fai);
delete [] args->usedArgs;
free(args->outfiles);
free(args->infile);
if(args->anc)
free(args->anc);
bam_hdr_destroy(args->hd);
if(args->argumentFile!=stderr) fclose(args->argumentFile);
delete args;
}