Debian Codegraph @

samtools/bam_stat.c.pysam.c

raw symbols SCIP
1 #include "samtools.pysam.h"
2
3 /* bam_stat.c -- flagstat subcommand.
4
5 Copyright (C) 2009, 2011, 2013-2015, 2019, 2021 Genome Research Ltd.
6
7 Author: Heng Li <[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 <unistd.h>
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include <limits.h>
35 #include <getopt.h>
36
37 #include "htslib/sam.h"
38 #include "samtools.h"
39 #include "sam_opts.h"
40
41 typedef struct {
42 long long n_reads[2], n_mapped[2], n_pair_all[2], n_pair_map[2], n_pair_good[2];
43 long long n_sgltn[2], n_read1[2], n_read2[2];
44 long long n_dup[2];
45 long long n_diffchr[2], n_diffhigh[2];
46 long long n_secondary[2], n_supp[2];
47 long long n_primary[2], n_pmapped[2], n_pdup[2];
48 } bam_flagstat_t;
49
50 inline static void flagstat_loop(bam_flagstat_t *s, bam1_core_t *c)
51 {
52 int w = (c->flag & BAM_FQCFAIL)? 1 : 0;
53 ++s->n_reads[w];
54 if (c->flag & BAM_FSECONDARY ) {
55 ++s->n_secondary[w];
56 } else if (c->flag & BAM_FSUPPLEMENTARY ) {
57 ++s->n_supp[w];
58 } else {
59 ++s->n_primary[w];
60
61 if (c->flag & BAM_FPAIRED) {
62 ++s->n_pair_all[w];
63 if ((c->flag & BAM_FPROPER_PAIR) && !(c->flag & BAM_FUNMAP) ) ++s->n_pair_good[w];
64 if (c->flag & BAM_FREAD1) ++s->n_read1[w];
65 if (c->flag & BAM_FREAD2) ++s->n_read2[w];
66 if ((c->flag & BAM_FMUNMAP) && !(c->flag & BAM_FUNMAP)) ++s->n_sgltn[w];
67 if (!(c->flag & BAM_FUNMAP) && !(c->flag & BAM_FMUNMAP)) {
68 ++s->n_pair_map[w];
69 if (c->mtid != c->tid) {
70 ++s->n_diffchr[w];
71 if (c->qual >= 5) ++s->n_diffhigh[w];
72 }
73 }
74 }
75
76 if (!(c->flag & BAM_FUNMAP)) ++s->n_pmapped[w];
77 if (c->flag & BAM_FDUP) ++s->n_pdup[w];
78 }
79 if (!(c->flag & BAM_FUNMAP)) ++s->n_mapped[w];
80 if (c->flag & BAM_FDUP) ++s->n_dup[w];
81 }
82
83 bam_flagstat_t *bam_flagstat_core(samFile *fp, sam_hdr_t *h)
84 {
85 bam_flagstat_t *s;
86 bam1_t *b;
87 bam1_core_t *c;
88 int ret;
89 s = (bam_flagstat_t*)calloc(1, sizeof(bam_flagstat_t));
90 b = bam_init1();
91 c = &b->core;
92 while ((ret = sam_read1(fp, h, b)) >= 0)
93 flagstat_loop(s, c);
94 bam_destroy1(b);
95 if (ret != -1) {
96 free(s);
97 return NULL;
98 }
99 return s;
100 }
101
102 static const char *percent(char *buffer, long long n, long long total)
103 {
104 if (total != 0) sprintf(buffer, "%.2f%%", (float)n / total * 100.0);
105 else strcpy(buffer, "N/A");
106 return buffer;
107 }
108
109 static const char *percent_json(char *buffer, long long n, long long total)
110 {
111 if (total != 0) sprintf(buffer, "%.2f", (float)n / total * 100.0);
112 else strcpy(buffer, "null");
113 return buffer;
114 }
115
116 static void usage_exit(FILE *fp, int exit_status)
117 {
118 fprintf(fp, "Usage: samtools flagstat [options] <in.bam>\n");
119 sam_global_opt_help(fp, "-.---@-.");
120 fprintf(fp, " -O, --");
121 fprintf(fp, "output-fmt FORMAT[,OPT[=VAL]]...\n"
122 " Specify output format (json, tsv)\n");
123 samtools_exit(exit_status);
124 }
125
126 static void out_fmt_default(bam_flagstat_t *s)
127 {
128 char b0[16], b1[16];
129 fprintf(samtools_stdout, "%lld + %lld in total (QC-passed reads + QC-failed reads)\n", s->n_reads[0], s->n_reads[1]);
130 fprintf(samtools_stdout, "%lld + %lld primary\n", s->n_primary[0], s->n_primary[1]);
131 fprintf(samtools_stdout, "%lld + %lld secondary\n", s->n_secondary[0], s->n_secondary[1]);
132 fprintf(samtools_stdout, "%lld + %lld supplementary\n", s->n_supp[0], s->n_supp[1]);
133 fprintf(samtools_stdout, "%lld + %lld duplicates\n", s->n_dup[0], s->n_dup[1]);
134 fprintf(samtools_stdout, "%lld + %lld primary duplicates\n", s->n_pdup[0], s->n_pdup[1]);
135 fprintf(samtools_stdout, "%lld + %lld mapped (%s : %s)\n", s->n_mapped[0], s->n_mapped[1], percent(b0, s->n_mapped[0], s->n_reads[0]), percent(b1, s->n_mapped[1], s->n_reads[1]));
136 fprintf(samtools_stdout, "%lld + %lld primary mapped (%s : %s)\n", s->n_pmapped[0], s->n_pmapped[1], percent(b0, s->n_pmapped[0], s->n_primary[0]), percent(b1, s->n_pmapped[1], s->n_primary[1]));
137 fprintf(samtools_stdout, "%lld + %lld paired in sequencing\n", s->n_pair_all[0], s->n_pair_all[1]);
138 fprintf(samtools_stdout, "%lld + %lld read1\n", s->n_read1[0], s->n_read1[1]);
139 fprintf(samtools_stdout, "%lld + %lld read2\n", s->n_read2[0], s->n_read2[1]);
140 fprintf(samtools_stdout, "%lld + %lld properly paired (%s : %s)\n", s->n_pair_good[0], s->n_pair_good[1], percent(b0, s->n_pair_good[0], s->n_pair_all[0]), percent(b1, s->n_pair_good[1], s->n_pair_all[1]));
141 fprintf(samtools_stdout, "%lld + %lld with itself and mate mapped\n", s->n_pair_map[0], s->n_pair_map[1]);
142 fprintf(samtools_stdout, "%lld + %lld singletons (%s : %s)\n", s->n_sgltn[0], s->n_sgltn[1], percent(b0, s->n_sgltn[0], s->n_pair_all[0]), percent(b1, s->n_sgltn[1], s->n_pair_all[1]));
143 fprintf(samtools_stdout, "%lld + %lld with mate mapped to a different chr\n", s->n_diffchr[0], s->n_diffchr[1]);
144 fprintf(samtools_stdout, "%lld + %lld with mate mapped to a different chr (mapQ>=5)\n", s->n_diffhigh[0], s->n_diffhigh[1]);
145 }
146
147 static void out_fmt_json(bam_flagstat_t *s) {
148 char b0[16], b1[16], p0[16], p1[16], pp0[16], pp1[16], s0[16], s1[16];
149 fprintf(samtools_stdout, "{\n \"QC-passed reads\": { \n"
150 " \"total\": %lld, \n"
151 " \"primary\": %lld, \n"
152 " \"secondary\": %lld, \n"
153 " \"supplementary\": %lld, \n"
154 " \"duplicates\": %lld, \n"
155 " \"primary duplicates\": %lld, \n"
156 " \"mapped\": %lld, \n"
157 " \"mapped %%\": %s, \n"
158 " \"primary mapped\": %lld, \n"
159 " \"primary mapped %%\": %s, \n"
160 " \"paired in sequencing\": %lld, \n"
161 " \"read1\": %lld, \n"
162 " \"read2\": %lld, \n"
163 " \"properly paired\": %lld, \n"
164 " \"properly paired %%\": %s, \n"
165 " \"with itself and mate mapped\": %lld, \n"
166 " \"singletons\": %lld, \n"
167 " \"singletons %%\": %s, \n"
168 " \"with mate mapped to a different chr\": %lld, \n"
169 " \"with mate mapped to a different chr (mapQ >= 5)\": %lld \n"
170 " },"
171 "\n \"QC-failed reads\": { \n"
172 " \"total\": %lld, \n"
173 " \"primary\": %lld, \n"
174 " \"secondary\": %lld, \n"
175 " \"supplementary\": %lld, \n"
176 " \"duplicates\": %lld, \n"
177 " \"primary duplicates\": %lld, \n"
178 " \"mapped\": %lld, \n"
179 " \"mapped %%\": %s, \n"
180 " \"primary mapped\": %lld, \n"
181 " \"primary mapped %%\": %s, \n"
182 " \"paired in sequencing\": %lld, \n"
183 " \"read1\": %lld, \n"
184 " \"read2\": %lld, \n"
185 " \"properly paired\": %lld, \n"
186 " \"properly paired %%\": %s, \n"
187 " \"with itself and mate mapped\": %lld, \n"
188 " \"singletons\": %lld, \n"
189 " \"singletons %%\": %s, \n"
190 " \"with mate mapped to a different chr\": %lld, \n"
191 " \"with mate mapped to a different chr (mapQ >= 5)\": %lld \n"
192 " }\n"
193 "}\n",
194 s->n_reads[0],
195 s->n_primary[0],
196 s->n_secondary[0],
197 s->n_supp[0],
198 s->n_dup[0],
199 s->n_pdup[0],
200 s->n_mapped[0],
201 percent_json(b0, s->n_mapped[0], s->n_reads[0]),
202 s->n_pmapped[0],
203 percent_json(p0, s->n_pmapped[0], s->n_primary[0]),
204 s->n_pair_all[0],
205 s->n_read1[0],
206 s->n_read2[0],
207 s->n_pair_good[0],
208 percent_json(pp0, s->n_pair_good[0], s->n_pair_all[0]),
209 s->n_pair_map[0],
210 s->n_sgltn[0],
211 percent_json(s0, s->n_sgltn[0], s->n_pair_all[0]),
212 s->n_diffchr[0],
213 s->n_diffhigh[0],
214 s->n_reads[1],
215 s->n_primary[1],
216 s->n_secondary[1],
217 s->n_supp[1],
218 s->n_dup[1],
219 s->n_pdup[1],
220 s->n_mapped[1],
221 percent_json(b1, s->n_mapped[1], s->n_reads[1]),
222 s->n_pmapped[1],
223 percent_json(p1, s->n_pmapped[1], s->n_primary[1]),
224 s->n_pair_all[1],
225 s->n_read1[1],
226 s->n_read2[1],
227 s->n_pair_good[1],
228 percent_json(pp1, s->n_pair_good[1], s->n_pair_all[1]),
229 s->n_pair_map[1],
230 s->n_sgltn[1],
231 percent_json(s1, s->n_sgltn[1], s->n_pair_all[1]),
232 s->n_diffchr[1],
233 s->n_diffhigh[1]
234 );
235 }
236
237 static void out_fmt_tsv(bam_flagstat_t *s) {
238 char b0[16], b1[16];
239 fprintf(samtools_stdout, "%lld\t%lld\ttotal (QC-passed reads + QC-failed reads)\n", s->n_reads[0], s->n_reads[1]);
240 fprintf(samtools_stdout, "%lld\t%lld\tprimary\n", s->n_primary[0], s->n_primary[1]);
241 fprintf(samtools_stdout, "%lld\t%lld\tsecondary\n", s->n_secondary[0], s->n_secondary[1]);
242 fprintf(samtools_stdout, "%lld\t%lld\tsupplementary\n", s->n_supp[0], s->n_supp[1]);
243 fprintf(samtools_stdout, "%lld\t%lld\tduplicates\n", s->n_dup[0], s->n_dup[1]);
244 fprintf(samtools_stdout, "%lld\t%lld\tprimary duplicates\n", s->n_pdup[0], s->n_pdup[1]);
245 fprintf(samtools_stdout, "%lld\t%lld\tmapped\n", s->n_mapped[0], s->n_mapped[1]);
246 fprintf(samtools_stdout, "%s\t%s\tmapped %%\n", percent(b0, s->n_mapped[0], s->n_reads[0]), percent(b1, s->n_mapped[1], s->n_reads[1]));
247 fprintf(samtools_stdout, "%lld\t%lld\tprimary mapped\n", s->n_pmapped[0], s->n_pmapped[1]);
248 fprintf(samtools_stdout, "%s\t%s\tprimary mapped %%\n", percent(b0, s->n_pmapped[0], s->n_primary[0]), percent(b1, s->n_pmapped[1], s->n_primary[1]));
249 fprintf(samtools_stdout, "%lld\t%lld\tpaired in sequencing\n", s->n_pair_all[0], s->n_pair_all[1]);
250 fprintf(samtools_stdout, "%lld\t%lld\tread1\n", s->n_read1[0], s->n_read1[1]);
251 fprintf(samtools_stdout, "%lld\t%lld\tread2\n", s->n_read2[0], s->n_read2[1]);
252 fprintf(samtools_stdout, "%lld\t%lld\tproperly paired\n", s->n_pair_good[0], s->n_pair_good[1]);
253 fprintf(samtools_stdout, "%s\t%s\tproperly paired %%\n", percent(b0, s->n_pair_good[0], s->n_pair_all[0]), percent(b1, s->n_pair_good[1], s->n_pair_all[1]));
254 fprintf(samtools_stdout, "%lld\t%lld\twith itself and mate mapped\n", s->n_pair_map[0], s->n_pair_map[1]);
255 fprintf(samtools_stdout, "%lld\t%lld\tsingletons\n", s->n_sgltn[0], s->n_sgltn[1]);
256 fprintf(samtools_stdout, "%s\t%s\tsingletons %%\n", percent(b0, s->n_sgltn[0], s->n_pair_all[0]), percent(b1, s->n_sgltn[1], s->n_pair_all[1]));
257 fprintf(samtools_stdout, "%lld\t%lld\twith mate mapped to a different chr\n", s->n_diffchr[0], s->n_diffchr[1]);
258 fprintf(samtools_stdout, "%lld\t%lld\twith mate mapped to a different chr (mapQ>=5)\n", s->n_diffhigh[0], s->n_diffhigh[1]);
259 }
260
261 /*
262 * Select flagstats output format to print.
263 */
264 static void output_fmt(bam_flagstat_t *s, const char *out_fmt)
265 {
266 if (strcmp(out_fmt, "json") == 0 || strcmp(out_fmt, "JSON") == 0) {
267 out_fmt_json(s);
268 } else if (strcmp(out_fmt, "tsv") == 0 || strcmp(out_fmt, "TSV") == 0) {
269 out_fmt_tsv(s);
270 } else {
271 out_fmt_default(s);
272 }
273 }
274
275 int bam_flagstat(int argc, char *argv[])
276 {
277 samFile *fp;
278 sam_hdr_t *header;
279 bam_flagstat_t *s;
280 const char *out_fmt = "default";
281 int c, status = EXIT_SUCCESS;
282
283 enum {
284 INPUT_FMT_OPTION = CHAR_MAX+1,
285 };
286
287 sam_global_args ga = SAM_GLOBAL_ARGS_INIT;
288 static const struct option lopts[] = {
289 SAM_OPT_GLOBAL_OPTIONS('-', 0, 'O', '-', '-', '@'),
290 {NULL, 0, NULL, 0}
291 };
292
293 while ((c = getopt_long(argc, argv, "@:O:", lopts, NULL)) >= 0) {
294 switch (c) {
295 case 'O':
296 out_fmt = optarg;
297 break;
298 default: if (parse_sam_global_opt(c, optarg, lopts, &ga) == 0) break;
299 /* else fall-through */
300 case '?':
301 usage_exit(samtools_stderr, EXIT_FAILURE);
302 }
303 }
304
305 if (argc != optind+1) {
306 if (argc == optind) usage_exit(samtools_stdout, EXIT_SUCCESS);
307 else usage_exit(samtools_stderr, EXIT_FAILURE);
308 }
309 fp = sam_open_format(argv[optind], "r", &ga.in);
310 if (fp == NULL) {
311 print_error_errno("flagstat", "Cannot open input file \"%s\"", argv[optind]);
312 return 1;
313 }
314 if (ga.nthreads > 0)
315 hts_set_threads(fp, ga.nthreads);
316
317 if (hts_set_opt(fp, CRAM_OPT_REQUIRED_FIELDS,
318 SAM_FLAG | SAM_MAPQ | SAM_RNEXT)) {
319 fprintf(samtools_stderr, "Failed to set CRAM_OPT_REQUIRED_FIELDS value\n");
320 return 1;
321 }
322
323 if (hts_set_opt(fp, CRAM_OPT_DECODE_MD, 0)) {
324 fprintf(samtools_stderr, "Failed to set CRAM_OPT_DECODE_MD value\n");
325 return 1;
326 }
327
328 header = sam_hdr_read(fp);
329 if (header == NULL) {
330 fprintf(samtools_stderr, "Failed to read header for \"%s\"\n", argv[optind]);
331 return 1;
332 }
333
334 s = bam_flagstat_core(fp, header);
335 if (s) {
336 output_fmt(s, out_fmt);
337 free(s);
338 }
339 else {
340 print_error("flagstat", "error reading from \"%s\"", argv[optind]);
341 status = EXIT_FAILURE;
342 }
343
344 sam_hdr_destroy(header);
345 sam_close(fp);
346 sam_global_args_free(&ga);
347 return status;
348 }