]> git.donarmstrong.com Git - samtools.git/blob - bam_stat.c
* samtools-0.1.2-20
[samtools.git] / bam_stat.c
1 #include "bam.h"
2
3 typedef struct {
4         long long n_reads, n_mapped, n_pair_all, n_pair_map, n_pair_good;
5         long long n_sgltn, n_read1, n_read2;
6         long long n_qcfail, n_dup;
7         long long n_diffchr, n_diffhigh;
8 } bam_flagstat_t;
9
10 #define flagstat_loop(s, c) do {                                                                                \
11                 ++(s)->n_reads;                                                                                                 \
12                 if ((c)->flag & BAM_FPAIRED) {                                                                  \
13                         ++(s)->n_pair_all;                                                                                      \
14                         if ((c)->flag & BAM_FPROPER_PAIR) ++(s)->n_pair_good;           \
15                         if ((c)->flag & BAM_FREAD1) ++(s)->n_read1;                                     \
16                         if ((c)->flag & BAM_FREAD2) ++(s)->n_read2;                                     \
17                         if ((c)->flag & BAM_FMUNMAP) ++(s)->n_sgltn;                            \
18                         if (!((c)->flag & BAM_FUNMAP) && !((c)->flag & BAM_FMUNMAP)) { \
19                                 ++(s)->n_pair_map;                                                                              \
20                                 if ((c)->mtid != (c)->tid) {                                                    \
21                                         ++(s)->n_diffchr;                                                                       \
22                                         if ((c)->qual >= 5) ++(s)->n_diffhigh;                          \
23                                 }                                                                                                               \
24                         }                                                                                                                       \
25                 }                                                                                                                               \
26                 if (!((c)->flag & BAM_FUNMAP)) ++(s)->n_mapped;                                 \
27                 if ((c)->flag & BAM_FQCFAIL) ++(s)->n_qcfail;                                   \
28                 if ((c)->flag & BAM_FDUP) ++(s)->n_dup;                                                 \
29         } while (0)
30
31 bam_flagstat_t *bam_flagstat_core(bamFile fp)
32 {
33         bam_flagstat_t *s;
34         bam1_t *b;
35         bam1_core_t *c;
36         int ret;
37         s = (bam_flagstat_t*)calloc(1, sizeof(bam_flagstat_t));
38         b = bam_init1();
39         c = &b->core;
40         while ((ret = bam_read1(fp, b)) >= 0)
41                 flagstat_loop(s, c);
42         bam_destroy1(b);
43         if (ret != -1)
44                 fprintf(stderr, "[bam_flagstat_core] Truncated file? Continue anyway.\n");
45         return s;
46 }
47 int bam_flagstat(int argc, char *argv[])
48 {
49         bamFile fp;
50         bam_header_t *header;
51         bam_flagstat_t *s;
52         if (argc == optind) {
53                 fprintf(stderr, "Usage: samtools flagstat <in.bam>\n");
54                 return 1;
55         }
56         fp = strcmp(argv[optind], "-")? bam_open(argv[optind], "r") : bam_dopen(fileno(stdin), "r");
57         assert(fp);
58         header = bam_header_read(fp);
59         s = bam_flagstat_core(fp);
60         printf("%lld in total\n", s->n_reads);
61         printf("%lld QC failure\n", s->n_qcfail);
62         printf("%lld duplicates\n", s->n_dup);
63         printf("%lld mapped (%.2f%%)\n", s->n_mapped, (float)s->n_mapped / s->n_reads * 100.0);
64         printf("%lld paired in sequencing\n", s->n_pair_all);
65         printf("%lld read1\n", s->n_read1);
66         printf("%lld read2\n", s->n_read2);
67         printf("%lld properly paired (%.2f%%)\n", s->n_pair_good, (float)s->n_pair_good / s->n_pair_all * 100.0);
68         printf("%lld with itself and mate mapped\n", s->n_pair_map);
69         printf("%lld singletons (%.2f%%)\n", s->n_sgltn, (float)s->n_sgltn / s->n_pair_all * 100.0);
70         printf("%lld with mate mapped to a different chr\n", s->n_diffchr);
71         printf("%lld with mate mapped to a different chr (mapQ>=5)\n", s->n_diffhigh);
72         free(s);
73         bam_header_destroy(header);
74         bam_close(fp);
75         return 0;
76 }