Debian Codegraph @

bcftools/main.c

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