Debian Codegraph @

samtools/bamtk.c

raw symbols SCIP
1 /* bamtk.c -- main samtools command front-end.
2
3 Copyright (C) 2008-2023 Genome Research Ltd.
4
5 Author: Heng Li <[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
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 <unistd.h>
29 #include <fcntl.h>
30 #include <string.h>
31
32 #include "htslib/hts.h"
33 #include "htslib/hfile.h"
34 #include "samtools.h"
35 #include "version.h"
36
37 int bam_taf2baf(int argc, char *argv[]);
38 int bam_mpileup(int argc, char *argv[]);
39 int bam_merge(int argc, char *argv[]);
40 int bam_index(int argc, char *argv[]);
41 int bam_sort(int argc, char *argv[]);
42 int bam_tview_main(int argc, char *argv[]);
43 int bam_mating(int argc, char *argv[]);
44 int bam_rmdup(int argc, char *argv[]);
45 int bam_flagstat(int argc, char *argv[]);
46 int bam_fillmd(int argc, char *argv[]);
47 int bam_idxstats(int argc, char *argv[]);
48 int bam_markdup(int argc, char *argv[]);
49 int main_samview(int argc, char *argv[]);
50 int main_head(int argc, char *argv[]);
51 int main_reheader(int argc, char *argv[]);
52 int main_cut_target(int argc, char *argv[]);
53 int main_phase(int argc, char *argv[]);
54 int main_cat(int argc, char *argv[]);
55 int main_depth(int argc, char *argv[]);
56 int main_coverage(int argc, char *argv[]);
57 int main_bam2fq(int argc, char *argv[]);
58 int main_pad2unpad(int argc, char *argv[]);
59 int main_bedcov(int argc, char *argv[]);
60 int main_bamshuf(int argc, char *argv[]);
61 int main_stats(int argc, char *argv[]);
62 int main_flags(int argc, char *argv[]);
63 int main_split(int argc, char *argv[]);
64 int main_quickcheck(int argc, char *argv[]);
65 int main_addreplacerg(int argc, char *argv[]);
66 int faidx_main(int argc, char *argv[]);
67 int dict_main(int argc, char *argv[]);
68 int fqidx_main(int argc, char *argv[]);
69 int amplicon_clip_main(int argc, char *argv[]);
70 int main_ampliconstats(int argc, char *argv[]);
71 int main_import(int argc, char *argv[]);
72 int main_samples(int argc, char *argv[]);
73 int main_consensus(int argc, char *argv[]);
74 int main_reference(int argc, char *argv[]);
75 int main_reset(int argc, char *argv[]);
76 int main_cram_size(int argc, char *argv[]);
77
78 const char *samtools_version(void)
79 {
80 return SAMTOOLS_VERSION;
81 }
82
83 // These come out of the config.h file built by autoconf or Makefile
84 const char *samtools_feature_string(void) {
85 const char *fmt =
86
87 #ifdef PACKAGE_URL
88 "build=configure "
89 #else
90 "build=Makefile "
91 #endif
92
93 #ifdef HAVE_CURSES
94 "curses=yes "
95 #else
96 "curses=no "
97 #endif
98 ;
99
100 return fmt;
101 }
102
103 static void long_version(void) {
104 printf("samtools %s\n"
105 "Using htslib %s\n"
106 "Copyright (C) 2024 Genome Research Ltd.\n",
107 samtools_version(), hts_version());
108
109 printf("\nSamtools compilation details:\n");
110 printf(" Features: %s\n", samtools_feature_string());
111 printf(" CC: %s\n", SAMTOOLS_CC);
112 printf(" CPPFLAGS: %s\n", SAMTOOLS_CPPFLAGS);
113 printf(" CFLAGS: %s\n", SAMTOOLS_CFLAGS);
114 printf(" LDFLAGS: %s\n", SAMTOOLS_LDFLAGS);
115 printf(" HTSDIR: %s\n", SAMTOOLS_HTSDIR);
116 printf(" LIBS: %s\n", SAMTOOLS_LIBS);
117 printf(" CURSES_LIB: %s\n", SAMTOOLS_CURSES_LIB);
118
119 printf("\nHTSlib compilation details:\n");
120 printf(" Features: %s\n", hts_feature_string());
121 printf(" CC: %s\n", hts_test_feature(HTS_FEATURE_CC));
122 printf(" CPPFLAGS: %s\n", hts_test_feature(HTS_FEATURE_CPPFLAGS));
123 printf(" CFLAGS: %s\n", hts_test_feature(HTS_FEATURE_CFLAGS));
124 printf(" LDFLAGS: %s\n", hts_test_feature(HTS_FEATURE_LDFLAGS));
125
126 // Plugins and schemes
127 printf("\nHTSlib URL scheme handlers present:\n");
128 const char *plugins[100];
129 int np = 100, i, j;
130
131 if (hfile_list_plugins(plugins, &np) < 0)
132 return;
133
134 for (i = 0; i < np; i++) {
135 const char *sc_list[100];
136 int nschemes = 100;
137 if (hfile_list_schemes(plugins[i], sc_list, &nschemes) < 0)
138 return;
139
140 printf(" %s:\t", plugins[i]);
141 for (j = 0; j < nschemes; j++)
142 printf(" %s%c", sc_list[j], ",\n"[j+1==nschemes]);
143 }
144 }
145
146 static void usage(FILE *fp)
147 {
148 /* Please improve the grouping */
149
150 fprintf(fp,
151 "\n"
152 "Program: samtools (Tools for alignments in the SAM format)\n"
153 "Version: %s (using htslib %s)\n\n", samtools_version(), hts_version());
154 fprintf(fp,
155 "Usage: samtools <command> [options]\n"
156 "\n"
157 "Commands:\n"
158 " -- Indexing\n"
159 " dict create a sequence dictionary file\n"
160 " faidx index/extract FASTA\n"
161 " fqidx index/extract FASTQ\n"
162 " index index alignment\n"
163 "\n"
164 " -- Editing\n"
165 " calmd recalculate MD/NM tags and '=' bases\n"
166 " fixmate fix mate information\n"
167 " reheader replace BAM header\n"
168 " targetcut cut fosmid regions (for fosmid pool only)\n"
169 " addreplacerg adds or replaces RG tags\n"
170 " markdup mark duplicates\n"
171 " ampliconclip clip oligos from the end of reads\n"
172 "\n"
173 " -- File operations\n"
174 " collate shuffle and group alignments by name\n"
175 " cat concatenate BAMs\n"
176 " consensus produce a consensus Pileup/FASTA/FASTQ\n"
177 " merge merge sorted alignments\n"
178 " mpileup multi-way pileup\n"
179 " sort sort alignment file\n"
180 " split splits a file by read group\n"
181 " quickcheck quickly check if SAM/BAM/CRAM file appears intact\n"
182 " fastq converts a BAM to a FASTQ\n"
183 " fasta converts a BAM to a FASTA\n"
184 " import Converts FASTA or FASTQ files to SAM/BAM/CRAM\n"
185 " reference Generates a reference from aligned data\n"
186 " reset Reverts aligner changes in reads\n"
187 "\n"
188 " -- Statistics\n"
189 " bedcov read depth per BED region\n"
190 " coverage alignment depth and percent coverage\n"
191 " depth compute the depth\n"
192 " flagstat simple stats\n"
193 " idxstats BAM index stats\n"
194 " cram-size list CRAM Content-ID and Data-Series sizes\n"
195 " phase phase heterozygotes\n"
196 " stats generate stats (former bamcheck)\n"
197 " ampliconstats generate amplicon specific stats\n"
198 "\n"
199 " -- Viewing\n"
200 " flags explain BAM flags\n"
201 " head header viewer\n"
202 " tview text alignment viewer\n"
203 " view SAM<->BAM<->CRAM conversion\n"
204 " depad convert padded BAM to unpadded BAM\n"
205 " samples list the samples in a set of SAM/BAM/CRAM files\n"
206 "\n"
207 " -- Misc\n"
208 " help [cmd] display this help message or help for [cmd]\n"
209 " version detailed version information\n"
210 "\n");
211 }
212
213 // This is a tricky one, but on Windows the filename wildcard expansion is done by
214 // the application and not by the shell, as traditionally it never had a "shell".
215 // Even now, DOS and Powershell do not do this expansion (but bash does).
216 //
217 // This means that Mingw/Msys implements code before main() that takes e.g. "*" and
218 // expands it up to a list of matching filenames. This in turn breaks things like
219 // specifying "*" as a region (all the unmapped reads). We take a hard line here -
220 // filename expansion is the task of the shell, not our application!
221 #ifdef _WIN32
222 int _CRT_glob = 0;
223 #endif
224
225 int main(int argc, char *argv[])
226 {
227 #ifdef _WIN32
228 setmode(fileno(stdout), O_BINARY);
229 setmode(fileno(stdin), O_BINARY);
230 #endif
231 if (argc < 2) { usage(stderr); return 1; }
232
233 if (strcmp(argv[1], "help") == 0 || strcmp(argv[1], "--help") == 0) {
234 if (argc == 2) { usage(stdout); return 0; }
235
236 // Otherwise change "samtools help COMMAND [...]" to "samtools COMMAND";
237 // main_xyz() functions by convention display the subcommand's usage
238 // when invoked without any arguments.
239 argv++;
240 argc = 2;
241 }
242
243 int ret = 0;
244 if (strcmp(argv[1], "view") == 0) ret = main_samview(argc-1, argv+1);
245 else if (strcmp(argv[1], "import") == 0) ret = main_import(argc-1, argv+1);
246 else if (strcmp(argv[1], "mpileup") == 0) ret = bam_mpileup(argc-1, argv+1);
247 else if (strcmp(argv[1], "merge") == 0) ret = bam_merge(argc-1, argv+1);
248 else if (strcmp(argv[1], "sort") == 0) ret = bam_sort(argc-1, argv+1);
249 else if (strcmp(argv[1], "index") == 0) ret = bam_index(argc-1, argv+1);
250 else if (strcmp(argv[1], "idxstat") == 0 ||
251 strcmp(argv[1], "idxstats") == 0) ret = bam_idxstats(argc-1, argv+1);
252 else if (strcmp(argv[1], "faidx") == 0) ret = faidx_main(argc-1, argv+1);
253 else if (strcmp(argv[1], "fqidx") == 0) ret = fqidx_main(argc-1, argv+1);
254 else if (strcmp(argv[1], "dict") == 0) ret = dict_main(argc-1, argv+1);
255 else if (strcmp(argv[1], "head") == 0) ret = main_head(argc-1, argv+1);
256 else if (strcmp(argv[1], "fixmate") == 0) ret = bam_mating(argc-1, argv+1);
257 else if (strcmp(argv[1], "rmdup") == 0) ret = bam_rmdup(argc-1, argv+1);
258 else if (strcmp(argv[1], "markdup") == 0) ret = bam_markdup(argc-1, argv+1);
259 else if (strcmp(argv[1], "ampliconclip") == 0) ret = amplicon_clip_main(argc-1, argv+1);
260 else if (strcmp(argv[1], "flagstat") == 0 ||
261 strcmp(argv[1], "flagstats") == 0) ret = bam_flagstat(argc-1, argv+1);
262 else if (strcmp(argv[1], "calmd") == 0) ret = bam_fillmd(argc-1, argv+1);
263 else if (strcmp(argv[1], "fillmd") == 0) ret = bam_fillmd(argc-1, argv+1);
264 else if (strcmp(argv[1], "reheader") == 0) ret = main_reheader(argc-1, argv+1);
265 else if (strcmp(argv[1], "cat") == 0) ret = main_cat(argc-1, argv+1);
266 else if (strcmp(argv[1], "targetcut") == 0) ret = main_cut_target(argc-1, argv+1);
267 else if (strcmp(argv[1], "phase") == 0) ret = main_phase(argc-1, argv+1);
268 else if (strcmp(argv[1], "depth") == 0) ret = main_depth(argc-1, argv+1);
269 else if (strcmp(argv[1], "coverage") == 0) ret = main_coverage(argc-1, argv+1);
270 else if (strcmp(argv[1], "bam2fq") == 0 ||
271 strcmp(argv[1], "fastq") == 0 ||
272 strcmp(argv[1], "fasta") == 0) ret = main_bam2fq(argc-1, argv+1);
273 else if (strcmp(argv[1], "pad2unpad") == 0) ret = main_pad2unpad(argc-1, argv+1);
274 else if (strcmp(argv[1], "depad") == 0) ret = main_pad2unpad(argc-1, argv+1);
275 else if (strcmp(argv[1], "bedcov") == 0) ret = main_bedcov(argc-1, argv+1);
276 else if (strcmp(argv[1], "bamshuf") == 0) ret = main_bamshuf(argc-1, argv+1);
277 else if (strcmp(argv[1], "collate") == 0) ret = main_bamshuf(argc-1, argv+1);
278 else if (strcmp(argv[1], "stat") == 0 ||
279 strcmp(argv[1], "stats") == 0) ret = main_stats(argc-1, argv+1);
280 else if (strcmp(argv[1], "flag") == 0 ||
281 strcmp(argv[1], "flags") == 0) ret = main_flags(argc-1, argv+1);
282 else if (strcmp(argv[1], "split") == 0) ret = main_split(argc-1, argv+1);
283 else if (strcmp(argv[1], "quickcheck") == 0) ret = main_quickcheck(argc-1, argv+1);
284 else if (strcmp(argv[1], "addreplacerg") == 0) ret = main_addreplacerg(argc-1, argv+1);
285 else if (strcmp(argv[1], "pileup") == 0) {
286 fprintf(stderr, "[main] The `pileup' command has been removed. Please use `mpileup' instead.\n");
287 return 1;
288 }
289 else if (strcmp(argv[1], "tview") == 0) ret = bam_tview_main(argc-1, argv+1);
290 else if (strcmp(argv[1], "ampliconstats") == 0) ret = main_ampliconstats(argc-1, argv+1);
291 else if (strcmp(argv[1], "samples") == 0) ret = main_samples(argc-1, argv+1);
292 else if (strcmp(argv[1], "consensus") == 0) ret = main_consensus(argc-1, argv+1);
293 else if (strcmp(argv[1], "reference") == 0) ret = main_reference(argc-1, argv+1);
294 else if (strcmp(argv[1], "cram-size") == 0) ret = main_cram_size(argc-1, argv+1);
295 else if (strcmp(argv[1], "version") == 0 || \
296 strcmp(argv[1], "--version") == 0)
297 long_version();
298 else if (strcmp(argv[1], "--version-only") == 0) {
299 printf("%s+htslib-%s\n", samtools_version(), hts_version());
300 }
301 else if (strcmp(argv[1], "reset") == 0) ret = main_reset(argc-1, argv+1);
302 else {
303 fprintf(stderr, "[main] unrecognized command '%s'\n", argv[1]);
304 return 1;
305 }
306
307 // For subcommands that may have produced substantial output on stdout,
308 // make a final check for delayed I/O errors. Ignore EBADF as other code
309 // may have already closed stdout.
310 if (fclose(stdout) != 0 && errno != EBADF) {
311 print_error_errno(argv[1], "closing standard output failed");
312 return 1;
313 }
314
315 return ret;
316 }