-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcmdargs.c
526 lines (433 loc) · 14.4 KB
/
cmdargs.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
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/*
* cmdargs.c
*
* $Id: cmdargs.c,v 1.2 2006/08/25 03:58:45 sheneman Exp $
*
*****************************************************************************
*
* Copyright (c) 2004, Luke Sheneman
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* + Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* + Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* + The names of its contributors may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*****************************************************************************
*
* AUTHOR:
*
* Luke Sheneman
* sheneman@cs.uidaho.edu
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifdef USE_GNU
#include <getopt.h>
#else
#include "getopt_long.h"
#endif /* USE_GNU*/
#include "clearcut.h"
#include "cmdargs.h"
/*
* NJ_handle_args() -
*
*/
NJ_ARGS *
NJ_handle_args(int argc,
char *argv[]) {
static NJ_ARGS nj_args;
int option_index, c;
struct option NJ_long_options[] = {
/* These options don't set a flag */
{"in", required_argument, NULL, 'i'},
{"out", required_argument, NULL, 'o'},
{"seed", required_argument, NULL, 's'},
{"matrixout", required_argument, NULL, 'm'},
{"ntrees", required_argument, NULL, 'n'},
/* These options set a flag */
{"verbose", no_argument, &(nj_args.verbose_flag), 1},
{"quiet", no_argument, &(nj_args.quiet_flag), 1},
{"distance", no_argument, &(nj_args.input_mode), NJ_INPUT_MODE_DISTANCE},
{"alignment", no_argument, &(nj_args.input_mode), NJ_INPUT_MODE_ALIGNED_SEQUENCES},
{"help", no_argument, &(nj_args.help), 1},
{"version", no_argument, &(nj_args.version), 1},
{"norandom", no_argument, &(nj_args.norandom), 1},
{"shuffle", no_argument, &(nj_args.shuffle), 1},
{"stdin", no_argument, &(nj_args.stdin_flag), 1},
{"stdout", no_argument, &(nj_args.stdout_flag), 1},
{"dna", no_argument, &(nj_args.dna_flag), 1},
{"DNA", no_argument, &(nj_args.dna_flag), 1},
{"protein", no_argument, &(nj_args.protein_flag), 1},
{"neighbor", no_argument, &(nj_args.neighbor), 1},
{"expblen", no_argument, &(nj_args.expblen), 1},
{"expdist", no_argument, &(nj_args.expdist), 1},
{"jukes", no_argument, &(nj_args.jukes_flag), 1},
{"kimura", no_argument, &(nj_args.kimura_flag), 1},
{0, 0, 0, 0}
};
/* initializes options to their default */
nj_args.infilename = NULL;
nj_args.outfilename = NULL;
nj_args.matrixout = NULL;
nj_args.seed = time(0);
nj_args.verbose_flag = 0;
nj_args.quiet_flag = 0;
nj_args.input_mode = NJ_INPUT_MODE_DISTANCE;
nj_args.help = 0;
nj_args.version = 0;
nj_args.norandom = 0;
nj_args.shuffle = 0;
nj_args.stdin_flag = 0;
nj_args.stdout_flag = 0;
nj_args.dna_flag = 0;
nj_args.protein_flag = 0;
nj_args.correction_model = NJ_MODEL_NONE;
nj_args.jukes_flag = 0;
nj_args.kimura_flag = 0;
nj_args.neighbor = 0;
nj_args.ntrees = 1;
nj_args.expblen = 0;
nj_args.expdist = 0;
while(1) {
c = getopt_long(argc,
argv,
"i:o:s:m:n:vqduahVSIOrDPjkNeE",
NJ_long_options,
&option_index);
if(c == -1) {
break;
}
switch(c) {
case 0:
if(NJ_long_options[option_index].flag) {
break;
}
printf("option %s", NJ_long_options[option_index].name);
if(optarg) {
printf(" with arg %s", optarg);
}
printf("\n");
break;
case 'i':
nj_args.infilename = optarg;
break;
case 'o':
nj_args.outfilename = optarg;
break;
case 's':
nj_args.seed = atoi(optarg);
break;
case 'm':
nj_args.matrixout = optarg;
break;
case 'n':
nj_args.ntrees = atoi(optarg);
break;
case 'v':
nj_args.verbose_flag = 1;
break;
case 'q':
nj_args.quiet_flag = 1;
break;
case 'd':
nj_args.input_mode = NJ_INPUT_MODE_DISTANCE;
break;
case 'a':
nj_args.input_mode = NJ_INPUT_MODE_ALIGNED_SEQUENCES;
break;
case 'h':
nj_args.help = 1;
break;
case 'V':
nj_args.version = 1;
break;
case 'S':
nj_args.shuffle = 1;
break;
case 'I':
nj_args.stdin_flag = 1;
break;
case 'O':
nj_args.stdin_flag = 1;
break;
case 'r':
nj_args.norandom = 1;
break;
case 'D':
nj_args.dna_flag = 1;
break;
case 'P':
nj_args.protein_flag = 1;
break;
case 'j':
nj_args.jukes_flag = 1;
break;
case 'k':
nj_args.kimura_flag = 1;
break;
case 'N':
nj_args.neighbor = 1;
break;
case 'e':
nj_args.expblen = 1;
break;
case 'E':
nj_args.expdist = 1;
break;
default:
NJ_usage();
exit(-1);
}
}
if(optind < argc) {
fprintf(stderr, "Clearcut: Unknown command-line argument:\n --> %s\n", argv[optind]);
NJ_usage();
exit(-1);
}
if(nj_args.version) {
printf("Clearcut Version: %s\n", NJ_VERSION);
exit(0);
}
if(nj_args.help) {
NJ_usage();
exit(0);
}
/* if stdin & explicit filename are specified for input */
if(nj_args.stdin_flag) {
if(nj_args.infilename) {
fprintf(stderr, "Clearcut: Ambiguous input source specified. Specify input filename OR stdin.\n");
NJ_usage();
exit(-1);
}
}
/* if stdout & explicit filename are specified for output */
if(nj_args.stdout_flag) {
if(nj_args.outfilename) {
fprintf(stderr, "Clearcut: Ambiguous output specified. Specify output filename OR stdout.\n");
NJ_usage();
exit(-1);
}
}
/* if user did not specify stdin or filename, default to stdin */
if(!nj_args.stdin_flag) {
if(!nj_args.infilename) {
fprintf(stderr, "Clearcut: No input file specified. Using stdin.\n");
nj_args.stdin_flag = 1;
}
}
/* if user did not specify stdout or filename, default to stdout */
if(!nj_args.stdout_flag) {
if(!nj_args.outfilename) {
fprintf(stderr, "Clearcut: No output file specified. Using stdout.\n");
nj_args.stdout_flag = 1;
}
}
/* User must specify distance matrix or alignment */
if(nj_args.input_mode == NJ_INPUT_MODE_UNKNOWN) {
fprintf(stderr, "Clearcut: Must specify input type (--distance | --alignment)\n");
NJ_usage();
exit(-1);
}
/* do not allow protein or DNA options for distance matrix input */
if(nj_args.input_mode == NJ_INPUT_MODE_DISTANCE) {
if(nj_args.dna_flag || nj_args.protein_flag) {
fprintf(stderr, "Clearcut: Ambiguous arguments. (--protein | --DNA) do not apply to distance \n");
NJ_usage();
exit(-1);
}
}
/* make sure different filenames were specified for input and output */
if(!nj_args.stdin_flag && !nj_args.stdout_flag) {
if(!strcmp(nj_args.infilename, nj_args.outfilename)) {
fprintf(stderr, "Clearcut: Input filename and output filename must be unique.\n");
NJ_usage();
exit(-1);
}
}
/* make sure that user specifies DNA or Protein if dealing with alignment input */
if(nj_args.input_mode == NJ_INPUT_MODE_ALIGNED_SEQUENCES) {
if(!nj_args.dna_flag && !nj_args.protein_flag) {
fprintf(stderr, "Clearcut: Must specify protein or DNA for alignment input.\n");
NJ_usage();
exit(-1);
}
}
/* make sure that user does not specify both protein and DNA when dealing with alignment input */
if(nj_args.input_mode == NJ_INPUT_MODE_ALIGNED_SEQUENCES) {
if(nj_args.dna_flag && nj_args.protein_flag) {
fprintf(stderr, "Clearcut: Specifying protein and DNA sequences are mutually exclusive options\n");
NJ_usage();
exit(-1);
}
}
/* make sure verbose and quiet were not specified together */
if(nj_args.verbose_flag && nj_args.quiet_flag) {
fprintf(stderr, "Clearcut: Verbose and Quiet mode are mutually exclusive.\n");
NJ_usage();
exit(-1);
}
/* make sure that a correction model was specified only when providing an alignment */
if(nj_args.input_mode == NJ_INPUT_MODE_DISTANCE) {
if(nj_args.jukes_flag || nj_args.kimura_flag) {
fprintf(stderr, "Clearcut: Only specify correction model for alignment input.\n");
NJ_usage();
exit(-1);
}
} else {
if(nj_args.jukes_flag && nj_args.kimura_flag) {
fprintf(stderr, "Clearcut: Only specify one correction model\n");
NJ_usage();
exit(-1);
} else {
if(nj_args.jukes_flag && !nj_args.kimura_flag) {
nj_args.correction_model = NJ_MODEL_JUKES;
} else if(nj_args.kimura_flag && !nj_args.jukes_flag) {
nj_args.correction_model = NJ_MODEL_KIMURA;
} else {
nj_args.correction_model = NJ_MODEL_NONE; /* DEFAULT */
}
}
}
/* make sure that the number of output trees is reasonable */
if(nj_args.ntrees <= 0) {
fprintf(stderr, "Clearcut: Number of output trees must be a positive integer.\n");
NJ_usage();
exit(-1);
}
/*
* make sure that if exponential distances are specified,
* we are dealing with alignment input
*/
if(nj_args.expdist && nj_args.input_mode != NJ_INPUT_MODE_ALIGNED_SEQUENCES) {
fprintf(stderr, "Clearcut: Exponential notation for distance matrix output requires that input be an alignment\n");
NJ_usage();
exit(-1);
}
return(&nj_args);
}
/*
* NJ_print_args() -
*
*/
void
NJ_print_args(NJ_ARGS *nj_args) {
char input_mode[32];
switch (nj_args->input_mode) {
case NJ_INPUT_MODE_DISTANCE:
sprintf(input_mode, "Distance Matrix");
break;
case NJ_INPUT_MODE_UNALIGNED_SEQUENCES:
sprintf(input_mode, "Unaligned Sequences");
break;
case NJ_INPUT_MODE_ALIGNED_SEQUENCES:
sprintf(input_mode, "Aligned Sequences");
break;
default:
sprintf(input_mode, "UNKNOWN");
break;
}
printf("\n*** Command Line Arguments ***\n");
printf("Input Mode: %s\n", input_mode);
if(nj_args->stdin_flag) {
printf("Input from STDIN\n");
} else {
printf("Input File: %s\n", nj_args->infilename);
}
if(nj_args->stdout_flag) {
printf("Output from STDOUT\n");
} else {
printf("Output File: %s\n", nj_args->outfilename);
}
if(nj_args->input_mode != NJ_INPUT_MODE_DISTANCE) {
if(nj_args->aligned_flag) {
printf("Input Sequences Aligned: YES\n");
} else {
printf("Input Sequences Aligned: NO\n");
}
}
if(nj_args->verbose_flag) {
printf("Verbose Mode: ON\n");
} else {
printf("Verbose Mode: OFF\n");
}
if(nj_args->quiet_flag) {
printf("Quiet Mode: ON\n");
} else {
printf("Quiet Mode: OFF\n");
}
if(nj_args->seed) {
printf("Random Seed: %d\n", nj_args->seed);
}
printf("\n*******\n");
return;
}
/*
* NJ_usage() -
*
* Print a usage message
*
*/
void
NJ_usage(void) {
printf("Usage: clearcut --in=<infilename> --out=<outfilename> [options]...\n");
printf("GENERAL OPTIONS:\n");
printf(" -h, --help Display this information.\n");
printf(" -V, --version Print the version of this program.\n");
printf(" -v, --verbose More output. (Default: OFF)\n");
printf(" -q, --quiet Silent operation. (Default: ON)\n");
printf(" -s, --seed=<seed> Explicitly set the PRNG seed to a specific value.\n");
printf(" -r, --norandom Attempt joins deterministically. (Default: OFF)\n");
printf(" -S, --shuffle Randomly shuffle the distance matrix. (Default: OFF)\n");
printf(" -N, --neighbor Use traditional Neighbor-Joining algorithm. (Default: OFF)\n");
printf("\n");
printf("INPUT OPTIONS:\n");
printf(" -I, --stdin Read input from STDIN.\n");
printf(" -d, --distance Input file is a distance matrix. (Default: ON)\n");
printf(" -a, --alignment Input file is a set of aligned sequences. (Default: OFF)\n");
printf(" -D, --DNA Input alignment are DNA sequences.\n");
printf(" -P, --protein Input alignment are protein sequences.\n");
printf("\n");
printf("CORRECTION MODEL FOR COMPUTING DISTANCE MATRIX (Default: NO Correction):\n");
printf(" -j, --jukes Use Jukes-Cantor correction for computing distance matrix.\n");
printf(" -k, --kimura Use Kimura correction for distance matrix.\n");
printf("\n");
printf("OUTPUT OPTIONS:\n");
printf(" -O, --stdout Output tree to STDOUT.\n");
printf(" -m, --matrixout=<file> Output distance matrix to specified file.\n");
printf(" -n, --ntrees=<n> Output n trees. (Default: 1)\n");
printf(" -e, --expblen Exponential notation for branch lengths. (Default: OFF)\n");
printf(" -E, --expdist Exponential notation in distance output. (Default: OFF)\n");
printf("\n");
printf("EXAMPLES:\n");
printf(" Compute tree by supplying distance matrix via stdin:\n");
printf(" clearcut --distance < distances.txt > treefile.tre\n");
printf("\n");
printf(" Compute tree by supplying an alignment of DNA sequences from a file:\n");
printf(" clearcut --alignment --DNA --in=alignment.txt --out=treefile.tre\n");
return;
}