Debian Codegraph @

bcftools/main.c.pysam.c

raw symbols SCIP
1 #include "bcftools.pysam.h"
2
3 /* main.c -- main bcftools command front-end.
4
5 Copyright (C) 2012-2021 Genome Research Ltd.
6
7 Author: Petr Danecek <[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 THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE. */
26
27 #include "config.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <htslib/hts.h>
34 #include "version.h"
35 #include "bcftools.h"
36
37 int main_tabix(int argc, char *argv[]);
38 int main_vcfindex(int argc, char *argv[]);
39 int main_vcfstats(int argc, char *argv[]);
40 int main_vcfisec(int argc, char *argv[]);
41 int main_vcfmerge(int argc, char *argv[]);
42 int main_vcfquery(int argc, char *argv[]);
43 int main_vcffilter(int argc, char *argv[]);
44 int main_vcfsom(int argc, char *argv[]);
45 int main_vcfnorm(int argc, char *argv[]);
46 int main_vcfgtcheck(int argc, char *argv[]);
47 int main_vcfview(int argc, char *argv[]);
48 int main_vcfhead(int argc, char *argv[]);
49 int main_vcfcall(int argc, char *argv[]);
50 int main_vcfannotate(int argc, char *argv[]);
51 int main_vcfroh(int argc, char *argv[]);
52 int main_vcfconcat(int argc, char *argv[]);
53 int main_reheader(int argc, char *argv[]);
54 int main_vcfconvert(int argc, char *argv[]);
55 int main_vcfcnv(int argc, char *argv[]);
56 #if USE_GPL
57 int main_polysomy(int argc, char *argv[]);
58 #endif
59 #ifdef ENABLE_BCF_PLUGINS
60 int main_plugin(int argc, char *argv[]);
61 int count_plugins(void);
62 #endif
63 int main_consensus(int argc, char *argv[]);
64 int main_csq(int argc, char *argv[]);
65 int main_mpileup(int argc, char *argv[]);
66 int main_sort(int argc, char *argv[]);
67
68 typedef struct
69 {
70 int (*func)(int, char*[]);
71 const char *alias, *help;
72 }
73 cmd_t;
74
75 static cmd_t cmds[] =
76 {
77 { .func = NULL,
78 .alias = "Indexing",
79 .help = NULL
80 },
81 { .func = main_vcfindex,
82 .alias = "index",
83 .help = "index VCF/BCF files"
84 },
85 { .func = main_tabix,
86 .alias = "tabix",
87 .help = "-tabix for BGZF'd BED, GFF, SAM, VCF and more" // do not advertise; only keep here for testing
88 },
89
90 { .func = NULL,
91 .alias = "VCF/BCF manipulation",
92 .help = NULL
93 },
94
95 { .func = main_vcfannotate,
96 .alias = "annotate",
97 .help = "annotate and edit VCF/BCF files",
98 },
99 { .func = main_vcfconcat,
100 .alias = "concat",
101 .help = "concatenate VCF/BCF files from the same set of samples"
102 },
103 { .func = main_vcfconvert,
104 .alias = "convert",
105 .help = "convert VCF/BCF files to different formats and back"
106 },
107 { .func = main_vcfhead,
108 .alias = "head",
109 .help = "view VCF/BCF file headers"
110 },
111 { .func = main_vcfisec,
112 .alias = "isec",
113 .help = "intersections of VCF/BCF files"
114 },
115 { .func = main_vcfmerge,
116 .alias = "merge",
117 .help = "merge VCF/BCF files files from non-overlapping sample sets"
118 },
119 { .func = main_vcfnorm,
120 .alias = "norm",
121 .help = "left-align and normalize indels"
122 },
123 #ifdef ENABLE_BCF_PLUGINS
124 { .func = main_plugin,
125 .alias = "plugin",
126 .help = "user-defined plugins"
127 },
128 #endif
129 { .func = main_vcfquery,
130 .alias = "query",
131 .help = "transform VCF/BCF into user-defined formats"
132 },
133 { .func = main_reheader,
134 .alias = "reheader",
135 .help = "modify VCF/BCF header, change sample names"
136 },
137 { .func = main_sort,
138 .alias = "sort",
139 .help = "sort VCF/BCF file"
140 },
141 { .func = main_vcfview,
142 .alias = "view",
143 .help = "VCF/BCF conversion, view, subset and filter VCF/BCF files"
144 },
145
146 { .func = NULL,
147 .alias = "VCF/BCF analysis",
148 .help = NULL
149 },
150
151 { .func = main_vcfcall,
152 .alias = "call",
153 .help = "SNP/indel calling"
154 },
155 { .func = main_consensus,
156 .alias = "consensus",
157 .help = "create consensus sequence by applying VCF variants"
158 },
159 { .func = main_vcfcnv,
160 .alias = "cnv",
161 .help = "HMM CNV calling"
162 },
163 { .func = main_csq,
164 .alias = "csq",
165 .help = "call variation consequences"
166 },
167 { .func = main_vcffilter,
168 .alias = "filter",
169 .help = "filter VCF/BCF files using fixed thresholds"
170 },
171 { .func = main_vcfgtcheck,
172 .alias = "gtcheck",
173 .help = "check sample concordance, detect sample swaps and contamination"
174 },
175 { .func = main_mpileup,
176 .alias = "mpileup",
177 .help = "multi-way pileup producing genotype likelihoods"
178 },
179 #if USE_GPL
180 { .func = main_polysomy,
181 .alias = "polysomy",
182 .help = "detect number of chromosomal copies",
183 },
184 #endif
185 { .func = main_vcfroh,
186 .alias = "roh",
187 .help = "identify runs of autozygosity (HMM)",
188 },
189 { .func = main_vcfstats,
190 .alias = "stats",
191 .help = "produce VCF/BCF stats"
192 },
193
194 { .func = main_vcfsom,
195 .alias = "som",
196 .help = "-filter using Self-Organized Maps (experimental)" // do not advertise
197
198 },
199 { .func = NULL,
200 .alias = NULL,
201 .help = NULL
202 }
203 };
204
205 char *bcftools_version(void)
206 {
207 return BCFTOOLS_VERSION;
208 }
209
210 static void usage(FILE *fp)
211 {
212 fprintf(fp, "\n");
213 fprintf(fp, "Program: bcftools (Tools for variant calling and manipulating VCFs and BCFs)\n");
214 #if USE_GPL
215 fprintf(fp, "License: GNU GPLv3+, due to use of the GNU Scientific Library\n");
216 #endif
217 fprintf(fp, "Version: %s (using htslib %s)\n", bcftools_version(), hts_version());
218 fprintf(fp, "\n");
219 fprintf(fp, "Usage: bcftools [--version|--version-only] [--help] <command> <argument>\n");
220 fprintf(fp, "\n");
221 fprintf(fp, "Commands:\n");
222
223 int i = 0;
224 const char *sep = NULL;
225 while (cmds[i].alias)
226 {
227 if ( !cmds[i].func ) sep = cmds[i].alias;
228 if ( sep )
229 {
230 fprintf(fp, "\n -- %s\n", sep);
231 sep = NULL;
232 }
233 if ( cmds[i].func && cmds[i].help[0]!='-' ) fprintf(fp, " %-12s %s\n", cmds[i].alias, cmds[i].help);
234 i++;
235 }
236 #if ENABLE_BCF_PLUGINS
237 fprintf(fp,"\n -- Plugins (collection of programs for calling, file manipulation & analysis)\n");
238 int nplugins = count_plugins();
239 if ( nplugins )
240 fprintf(fp," %d plugins available, run \"bcftools plugin -lv\" to see a complete list\n", nplugins);
241 else
242 fprintf(fp," 0 plugins available, run \"bcftools plugin -l\" for help\n");
243 #endif
244 fprintf(fp,"\n");
245 fprintf(fp,
246 " Most commands accept VCF, bgzipped VCF, and BCF with the file type detected\n"
247 " automatically even when streaming from a pipe. Indexed VCF and BCF will work\n"
248 " in all situations. Un-indexed VCF and BCF and streams will work in most but\n"
249 " not all situations.\n");
250 fprintf(fp,"\n");
251 }
252
253 // This is a tricky one, but on Windows the filename wildcard expansion is done by
254 // the application and not by the shell, as traditionally it never had a "shell".
255 // Even now, DOS and Powershell do not do this expansion (but bash does).
256 //
257 // This means that Mingw/Msys implements code before main() that takes e.g. "*" and
258 // expands it up to a list of matching filenames. This in turn breaks things like
259 // specifying "*" as a region (all the unmapped reads). We take a hard line here -
260 // filename expansion is the task of the shell, not our application!
261 #ifdef _WIN32
262 int _CRT_glob = 0;
263 #endif
264
265 int bcftools_main(int argc, char *argv[])
266 {
267 if (argc < 2) { usage(bcftools_stderr); return 1; }
268
269 if (strcmp(argv[1], "version") == 0 || strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-v") == 0) {
270 fprintf(bcftools_stdout, "bcftools %s\nUsing htslib %s\nCopyright (C) 2024 Genome Research Ltd.\n", bcftools_version(), hts_version());
271 #if USE_GPL
272 fprintf(bcftools_stdout, "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n");
273 #else
274 fprintf(bcftools_stdout, "License Expat: The MIT/Expat license\n");
275 #endif
276 fprintf(bcftools_stdout, "This is free software: you are free to change and redistribute it.\nThere is NO WARRANTY, to the extent permitted by law.\n");
277 return 0;
278 }
279 else if (strcmp(argv[1], "--version-only") == 0) {
280 fprintf(bcftools_stdout, "%s+htslib-%s\n", bcftools_version(), hts_version());
281 return 0;
282 }
283 else if (strcmp(argv[1], "help") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) {
284 if (argc == 2) { usage(bcftools_stdout); return 0; }
285 // Otherwise change "bcftools help COMMAND [...]" to "bcftools COMMAND";
286 // main_xyz() functions by convention display the subcommand's usage
287 // when invoked without any arguments.
288 argv++;
289 argc = 2;
290 }
291 else if ( argv[1][0]=='+' )
292 {
293 // "bcftools plugin name" can be run as "bcftools +name"
294 argv[1]++;
295 argv[0] = "plugin";
296 argv--;
297 argc++;
298 }
299
300 int i = 0;
301 while (cmds[i].alias)
302 {
303 if (cmds[i].func && strcmp(argv[1],cmds[i].alias)==0)
304 {
305 return cmds[i].func(argc-1,argv+1);
306 }
307 i++;
308 }
309 fprintf(bcftools_stderr, "[E::%s] unrecognized command '%s'\n", __func__, argv[1]);
310 return 1;
311 }
312