1 /* sam_opts.c -- utilities to aid parsing common command line options.
2
3 Copyright (C) 2015, 2019 Genome Research Ltd.
4
5 Author: James Bonfield <jkb@sanger.ac.uk>
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 DEALINGS IN THE SOFTWARE. */
24
25 #include <config.h>
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include "sam_opts.h"
31
32 /*
33 * Processes a standard "global" samtools long option.
34 *
35 * The 'c' value is the return value from a getopt_long() call. It is checked
36 * against the lopt[] array to find the corresponding value as this may have
37 * been reassigned by the individual subcommand.
38 *
39 * Having found the entry, the corresponding long form is used to apply the
40 * option, storing the setting in sam_global_args *ga.
41 *
42 * Returns 0 on success,
43 * -1 on failure.
44 */
parse_sam_global_opt(int c,const char * optarg,const struct option * lopt,sam_global_args * ga)45 int parse_sam_global_opt(int c, const char *optarg, const struct option *lopt,
46 sam_global_args *ga) {
47 int r = 0;
48
49 while (lopt->name) {
50 if (c != lopt->val) {
51 lopt++;
52 continue;
53 }
54
55 if (strcmp(lopt->name, "input-fmt") == 0) {
56 r = hts_parse_format(&ga->in, optarg);
57 break;
58 } else if (strcmp(lopt->name, "input-fmt-option") == 0) {
59 r = hts_opt_add((hts_opt **)&ga->in.specific, optarg);
60 break;
61 } else if (strcmp(lopt->name, "output-fmt") == 0) {
62 r = hts_parse_format(&ga->out, optarg);
63 break;
64 } else if (strcmp(lopt->name, "output-fmt-option") == 0) {
65 r = hts_opt_add((hts_opt **)&ga->out.specific, optarg);
66 break;
67 } else if (strcmp(lopt->name, "reference") == 0) {
68 char *ref = malloc(10 + strlen(optarg) + 1);
69
70 if (!ref) {
71 fprintf(stderr, "Unable to allocate memory in "
72 "parse_sam_global_opt.\n");
73
74 return -1;
75 }
76
77 sprintf(ref, "reference=%s", optarg);
78
79 if (!(ga->reference = strdup(optarg))) {
80 fprintf(stderr, "Unable to allocate memory in "
81 "parse_sam_global_opt.\n");
82
83 return -1;
84 }
85
86 r = hts_opt_add((hts_opt **)&ga->in.specific, ref);
87 r |= hts_opt_add((hts_opt **)&ga->out.specific, ref);
88 free(ref);
89 break;
90 } else if (strcmp(lopt->name, "threads") == 0) {
91 ga->nthreads = atoi(optarg);
92 break;
93 } else if (strcmp(lopt->name, "write-index") == 0) {
94 ga->write_index = 1;
95 break;
96 } else if (strcmp(lopt->name, "verbosity") == 0) {
97 hts_verbose = atoi(optarg);
98 break;
99 }
100 }
101
102 if (!lopt->name) {
103 fprintf(stderr, "Unexpected global option.\n");
104 return -1;
105 }
106
107 /*
108 * SAM format with compression enabled implies SAM.bgzf
109 */
110 if (ga->out.format == sam) {
111 hts_opt *opts = (hts_opt *)ga->out.specific;
112 while (opts) {
113 if (opts->opt == HTS_OPT_COMPRESSION_LEVEL)
114 ga->out.compression = bgzf;
115 opts = opts->next;
116 }
117 }
118
119 return r;
120 }
121
122 /*
123 * Report the usage for global options.
124 *
125 * This accepts a string with one character per SAM_OPT_GLOBAL_OPTIONS option
126 * to determine which options need to be printed and how.
127 * Each character should be one of:
128 * '.' No short option has been assigned. Use --long-opt only.
129 * '-' The long (and short) option has been disabled.
130 * <c> Otherwise the short option is character <c>.
131 */
sam_global_opt_help(FILE * fp,const char * shortopts)132 void sam_global_opt_help(FILE *fp, const char *shortopts) {
133 int i = 0;
134
135 static const struct option lopts[] = {
136 SAM_OPT_GLOBAL_OPTIONS(0,0,0,0,0,0),
137 { NULL, 0, NULL, 0 }
138 };
139
140 for (i = 0; shortopts && shortopts[i] && lopts[i].name; i++) {
141 if (shortopts[i] == '-')
142 continue;
143
144 if (shortopts[i] == '.')
145 fprintf(fp, " --");
146 else
147 fprintf(fp, " -%c, --", shortopts[i]);
148
149 if (strcmp(lopts[i].name, "input-fmt") == 0)
150 fprintf(fp,"input-fmt FORMAT[,OPT[=VAL]]...\n"
151 " Specify input format (SAM, BAM, CRAM)\n");
152 else if (strcmp(lopts[i].name, "input-fmt-option") == 0)
153 fprintf(fp,"input-fmt-option OPT[=VAL]\n"
154 " Specify a single input file format option in the form\n"
155 " of OPTION or OPTION=VALUE\n");
156 else if (strcmp(lopts[i].name, "output-fmt") == 0)
157 fprintf(fp,"output-fmt FORMAT[,OPT[=VAL]]...\n"
158 " Specify output format (SAM, BAM, CRAM)\n");
159 else if (strcmp(lopts[i].name, "output-fmt-option") == 0)
160 fprintf(fp,"output-fmt-option OPT[=VAL]\n"
161 " Specify a single output file format option in the form\n"
162 " of OPTION or OPTION=VALUE\n");
163 else if (strcmp(lopts[i].name, "reference") == 0)
164 fprintf(fp,"reference FILE\n"
165 " Reference sequence FASTA FILE [null]\n");
166 else if (strcmp(lopts[i].name, "threads") == 0)
167 fprintf(fp,"threads INT\n"
168 " Number of additional threads to use [0]\n");
169 else if (strcmp(lopts[i].name, "write-index") == 0)
170 fprintf(fp,"write-index\n"
171 " Automatically index the output files [off]\n");
172 else if (strcmp(lopts[i].name, "verbosity") == 0)
173 fprintf(fp,"verbosity INT\n"
174 " Set level of verbosity\n");
175 }
176 }
177
sam_global_args_init(sam_global_args * ga)178 void sam_global_args_init(sam_global_args *ga) {
179 if (!ga)
180 return;
181
182 memset(ga, 0, sizeof(*ga));
183 }
184
sam_global_args_free(sam_global_args * ga)185 void sam_global_args_free(sam_global_args *ga) {
186 if (ga->in.specific)
187 hts_opt_free(ga->in.specific);
188
189 if (ga->out.specific)
190 hts_opt_free(ga->out.specific);
191
192 if (ga->reference)
193 free(ga->reference);
194 }
195