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