]> git.donarmstrong.com Git - samtools.git/blob - bam_stat.c
* samtools-0.1.2-1
[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         s = (bam_flagstat_t*)calloc(1, sizeof(bam_flagstat_t));
37         b = bam_init1();
38         c = &b->core;
39         while (bam_read1(fp, b) >= 0) {
40                 flagstat_loop(s, c);
41         }
42         bam_destroy1(b);
43         return s;
44 }
45 int bam_flagstat(int argc, char *argv[])
46 {
47         bamFile fp;
48         bam_header_t *header;
49         bam_flagstat_t *s;
50         if (argc == optind) {
51                 fprintf(stderr, "Usage: samtools flagstat <in.bam>\n");
52                 return 1;
53         }
54         fp = strcmp(argv[optind], "-")? bam_open(argv[optind], "r") : bam_dopen(fileno(stdin), "r");
55         assert(fp);
56         header = bam_header_read(fp);
57         s = bam_flagstat_core(fp);
58         printf("%lld in total\n", s->n_reads);
59         printf("%lld QC failure\n", s->n_qcfail);
60         printf("%lld duplicates\n", s->n_dup);
61         printf("%lld mapped\n", s->n_mapped);
62         printf("%lld paired in sequencing\n", s->n_pair_all);
63         printf("%lld read1\n", s->n_read1);
64         printf("%lld read2\n", s->n_read2);
65         printf("%lld properly paired\n", s->n_pair_good);
66         printf("%lld with both itself and its mate mapped\n", s->n_pair_map);
67         printf("%lld singletons\n", s->n_sgltn);
68         printf("%lld with its mate mapped to a different chr\n", s->n_diffchr);
69         free(s);
70         bam_header_destroy(header);
71         bam_close(fp);
72         return 0;
73 }