Debian Codegraph @

samtools/sam_opts.c.pysam.c

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