]> git.donarmstrong.com Git - samtools.git/blob - misc/bamcheck.c
Added -f, -F options. Count and plot indels per cycle for each mate pair separately.
[samtools.git] / misc / bamcheck.c
1 /* 
2     Author: petr.danecek@sanger
3     gcc -Wall -Winline -g -O2 -I ~/git/samtools bamcheck.c -o bamcheck -lm -lz -L ~/git/samtools -lbam
4
5     Assumptions, approximations and other issues:
6         - GC-depth graph does not split reads, the starting position determines which bin is incremented.
7             There are small overlaps between bins (max readlen-1). However, the bins are big (20k).
8         - coverage distribution ignores softclips and deletions
9         - some stats require sorted BAMs
10         - GC content graph can have an untidy, step-like pattern when BAM contains multiple read lengths.
11         - 'bases mapped' (stats->nbases_mapped) is calculated from read lengths given by BAM (core.l_qseq)
12         - With the -t option, the whole reads are used. Except for the number of mapped bases (cigar)
13             counts, no splicing is done, no indels or soft clips are considered, even small overlap is
14             good enough to include the read in the stats.
15
16 */
17
18 #define BAMCHECK_VERSION "2012-04-24"
19
20 #define _ISOC99_SOURCE
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include <math.h>
26 #include <ctype.h>
27 #include <getopt.h>
28 #include <errno.h>
29 #include "faidx.h"
30 #include "khash.h"
31 #include "sam.h"
32 #include "razf.h"
33
34 #define BWA_MIN_RDLEN 35
35 #define IS_PAIRED(bam) ((bam)->core.flag&BAM_FPAIRED && !((bam)->core.flag&BAM_FUNMAP) && !((bam)->core.flag&BAM_FMUNMAP))
36 #define IS_UNMAPPED(bam) ((bam)->core.flag&BAM_FUNMAP)
37 #define IS_REVERSE(bam) ((bam)->core.flag&BAM_FREVERSE)
38 #define IS_MATE_REVERSE(bam) ((bam)->core.flag&BAM_FMREVERSE)
39 #define IS_READ1(bam) ((bam)->core.flag&BAM_FREAD1)
40 #define IS_READ2(bam) ((bam)->core.flag&BAM_FREAD2)
41 #define IS_DUP(bam) ((bam)->core.flag&BAM_FDUP)
42
43 typedef struct 
44 {
45     int32_t line_len, line_blen;
46     int64_t len;
47     uint64_t offset;
48
49 faidx1_t;
50 KHASH_MAP_INIT_STR(s, faidx1_t)
51 KHASH_MAP_INIT_STR(str, int)
52 struct __faidx_t {
53     RAZF *rz;
54     int n, m;
55     char **name;
56     khash_t(s) *hash;
57 };
58
59 typedef struct
60 {
61     float gc;
62     uint32_t depth;
63 }
64 gc_depth_t;
65
66 // For coverage distribution, a simple pileup
67 typedef struct 
68 {
69     int64_t pos;
70     int size, start;
71     int *buffer;
72
73 round_buffer_t;
74
75 typedef struct { uint32_t from, to; } pos_t;
76 typedef struct
77 {
78     int npos,mpos,cpos;
79     pos_t *pos;
80 }
81 regions_t;
82
83 typedef struct
84 {
85     // Parameters
86     int trim_qual;      // bwa trim quality
87
88     // Dimensions of the quality histogram holder (quals_1st,quals_2nd), GC content holder (gc_1st,gc_2nd),
89     //  insert size histogram holder
90     int nquals;         // The number of quality bins 
91     int nbases;         // The maximum sequence length the allocated array can hold
92     int nisize;         // The maximum insert size that the allocated array can hold
93     int ngc;            // The size of gc_1st and gc_2nd
94     int nindels;        // The maximum indel length for indel distribution
95
96     // Arrays for the histogram data
97     uint64_t *quals_1st, *quals_2nd;
98     uint64_t *gc_1st, *gc_2nd;
99     uint64_t *isize_inward, *isize_outward, *isize_other;
100     uint64_t *acgt_cycles;
101     uint64_t *read_lengths;
102     uint64_t *insertions, *deletions;
103     uint64_t *ins_cycles_1st, *ins_cycles_2nd, *del_cycles_1st, *del_cycles_2nd;
104
105     // The extremes encountered
106     int max_len;            // Maximum read length
107     int max_qual;           // Maximum quality
108     float isize_main_bulk;  // There are always some unrealistically big insert sizes, report only the main part
109     int is_sorted;
110
111     // Summary numbers
112     uint64_t total_len;
113     uint64_t total_len_dup;
114     uint64_t nreads_1st;
115     uint64_t nreads_2nd;
116     uint64_t nreads_dup;
117     uint64_t nreads_unmapped;
118     uint64_t nreads_unpaired;
119     uint64_t nreads_paired;
120     uint64_t nreads_mq0;
121     uint64_t nbases_mapped;
122     uint64_t nbases_mapped_cigar;
123     uint64_t nbases_trimmed;  // bwa trimmed bases
124     uint64_t nmismatches;
125
126     // GC-depth related data
127     uint32_t ngcd, igcd;        // The maximum number of GC depth bins and index of the current bin
128     gc_depth_t *gcd;            // The GC-depth bins holder
129     int gcd_bin_size;           // The size of GC-depth bin
130     int32_t tid, gcd_pos;       // Position of the current bin
131     int32_t pos;                // Position of the last read
132
133     // Coverage distribution related data
134     int ncov;                       // The number of coverage bins
135     uint64_t *cov;                  // The coverage frequencies
136     int cov_min,cov_max,cov_step;   // Minimum, maximum coverage and size of the coverage bins
137     round_buffer_t cov_rbuf;        // Pileup round buffer
138
139     // Mismatches by read cycle 
140     uint8_t *rseq_buf;             // A buffer for reference sequence to check the mismatches against
141     int nref_seq;               // The size of the buffer
142     int32_t rseq_pos;           // The coordinate of the first base in the buffer
143     int32_t rseq_len;           // The used part of the buffer
144     uint64_t *mpc_buf;          // Mismatches per cycle
145
146     // Filters
147     int filter_readlen;
148
149     // Target regions
150     int nregions, reg_from,reg_to;
151     regions_t *regions;
152
153     // Auxiliary data
154     int flag_require, flag_filter;
155     double sum_qual;            // For calculating average quality value 
156     samfile_t *sam;             // Unused
157     faidx_t *fai;               // Reference sequence for GC-depth graph
158     int argc;                   // Command line arguments to be printed on the output
159     char **argv;
160 }
161 stats_t;
162
163 void error(const char *format, ...);
164 void bam_init_header_hash(bam_header_t *header);
165 int is_in_regions(bam1_t *bam_line, stats_t *stats);
166
167
168 // Coverage distribution methods
169 inline int coverage_idx(int min, int max, int n, int step, int depth)
170 {
171     if ( depth < min )
172         return 0;
173
174     if ( depth > max )
175         return n-1;
176
177     return 1 + (depth - min) / step;
178 }
179
180 inline int round_buffer_lidx2ridx(int offset, int size, int64_t refpos, int64_t pos)
181 {
182     return (offset + (pos-refpos) % size) % size;
183 }
184
185 void round_buffer_flush(stats_t *stats, int64_t pos)
186 {
187     int ibuf,idp;
188
189     if ( pos==stats->cov_rbuf.pos ) 
190         return;
191
192     int64_t new_pos = pos;
193     if ( pos==-1 || pos - stats->cov_rbuf.pos >= stats->cov_rbuf.size )
194     {
195         // Flush the whole buffer, but in sequential order, 
196         pos = stats->cov_rbuf.pos + stats->cov_rbuf.size - 1;
197     }
198
199     if ( pos < stats->cov_rbuf.pos ) 
200         error("Expected coordinates in ascending order, got %ld after %ld\n", pos,stats->cov_rbuf.pos);
201
202     int ifrom = stats->cov_rbuf.start;
203     int ito = round_buffer_lidx2ridx(stats->cov_rbuf.start,stats->cov_rbuf.size,stats->cov_rbuf.pos,pos-1);
204     if ( ifrom>ito )
205     {
206         for (ibuf=ifrom; ibuf<stats->cov_rbuf.size; ibuf++)
207         {
208             if ( !stats->cov_rbuf.buffer[ibuf] )
209                 continue;
210             idp = coverage_idx(stats->cov_min,stats->cov_max,stats->ncov,stats->cov_step,stats->cov_rbuf.buffer[ibuf]);
211             stats->cov[idp]++;
212             stats->cov_rbuf.buffer[ibuf] = 0;
213         }
214         ifrom = 0;
215     }
216     for (ibuf=ifrom; ibuf<=ito; ibuf++)
217     {
218         if ( !stats->cov_rbuf.buffer[ibuf] )
219             continue;
220         idp = coverage_idx(stats->cov_min,stats->cov_max,stats->ncov,stats->cov_step,stats->cov_rbuf.buffer[ibuf]);
221         stats->cov[idp]++;
222         stats->cov_rbuf.buffer[ibuf] = 0;
223     }
224     stats->cov_rbuf.start = (new_pos==-1) ? 0 : round_buffer_lidx2ridx(stats->cov_rbuf.start,stats->cov_rbuf.size,stats->cov_rbuf.pos,pos);
225     stats->cov_rbuf.pos   = new_pos;
226 }
227
228 void round_buffer_insert_read(round_buffer_t *rbuf, int64_t from, int64_t to)
229 {
230     if ( to-from >= rbuf->size )
231         error("The read length too big (%d), please increase the buffer length (currently %d)\n", to-from+1,rbuf->size);
232     if ( from < rbuf->pos )
233         error("The reads are not sorted (%ld comes after %ld).\n", from,rbuf->pos);
234
235     int ifrom,ito,ibuf;
236     ifrom = round_buffer_lidx2ridx(rbuf->start,rbuf->size,rbuf->pos,from);
237     ito   = round_buffer_lidx2ridx(rbuf->start,rbuf->size,rbuf->pos,to);
238     if ( ifrom>ito )
239     {
240         for (ibuf=ifrom; ibuf<rbuf->size; ibuf++)
241             rbuf->buffer[ibuf]++;
242         ifrom = 0;
243     }
244     for (ibuf=ifrom; ibuf<=ito; ibuf++)
245         rbuf->buffer[ibuf]++;
246 }
247
248 // Calculate the number of bases in the read trimmed by BWA
249 int bwa_trim_read(int trim_qual, uint8_t *quals, int len, int reverse) 
250 {
251     if ( len<BWA_MIN_RDLEN ) return 0;
252
253     // Although the name implies that the read cannot be trimmed to more than BWA_MIN_RDLEN,
254     //  the calculation can in fact trim it to (BWA_MIN_RDLEN-1). (bwa_trim_read in bwa/bwaseqio.c).
255     int max_trimmed = len - BWA_MIN_RDLEN + 1;
256     int l, sum=0, max_sum=0, max_l=0;
257
258     for (l=0; l<max_trimmed; l++)
259     {
260         sum += trim_qual - quals[ reverse ? l : len-1-l ];
261         if ( sum<0 ) break;
262         if ( sum>max_sum )
263         {
264             max_sum = sum;
265             // This is the correct way, but bwa clips from some reason one base less
266             // max_l   = l+1;
267             max_l   = l;
268         }
269     }
270     return max_l;
271 }
272
273
274 void count_indels(stats_t *stats,bam1_t *bam_line) 
275 {
276     int is_fwd = IS_REVERSE(bam_line) ? 0 : 1;
277     int is_1st = IS_READ1(bam_line) ? 1 : 0;
278     int icig;
279     int icycle = 0;
280     int read_len = bam_line->core.l_qseq;
281     for (icig=0; icig<bam_line->core.n_cigar; icig++) 
282     {
283         // Conversion from uint32_t to MIDNSHP
284         //  0123456
285         //  MIDNSHP
286         int cig  = bam1_cigar(bam_line)[icig] & BAM_CIGAR_MASK;
287         int ncig = bam1_cigar(bam_line)[icig] >> BAM_CIGAR_SHIFT;
288
289         if ( cig==1 )
290         {
291             int idx = is_fwd ? icycle : read_len-icycle;
292             if ( idx<0 ) error("FIXME: read_len=%d vs icycle=%d\n", read_len,icycle);
293             if ( idx >= stats->nbases || idx<0 ) error("FIXME: %d vs %d\n", idx,stats->nbases);
294             if ( is_1st ) 
295                 stats->ins_cycles_1st[idx]++;
296             else
297                 stats->ins_cycles_2nd[idx]++;
298             icycle += ncig;
299             if ( ncig<=stats->nindels )
300                 stats->insertions[ncig-1]++;
301             continue;
302         }
303         if ( cig==2 )
304         {
305             int idx = is_fwd ? icycle-1 : read_len-icycle-1;
306             if ( idx<0 ) continue;  // discard meaningless deletions
307             if ( idx >= stats->nbases ) error("FIXME: %d vs %d\n", idx,stats->nbases);
308             if ( is_1st )
309                 stats->del_cycles_1st[idx]++;
310             else
311                 stats->del_cycles_2nd[idx]++;
312             if ( ncig<=stats->nindels )
313                 stats->deletions[ncig-1]++;
314             continue;
315         }
316         icycle += ncig;
317     }
318 }
319
320 void count_mismatches_per_cycle(stats_t *stats,bam1_t *bam_line) 
321 {
322     int is_fwd = IS_REVERSE(bam_line) ? 0 : 1;
323     int icig,iread=0,icycle=0;
324     int iref = bam_line->core.pos - stats->rseq_pos;
325     int read_len   = bam_line->core.l_qseq;
326     uint8_t *read  = bam1_seq(bam_line);
327     uint8_t *quals = bam1_qual(bam_line);
328     uint64_t *mpc_buf = stats->mpc_buf;
329     for (icig=0; icig<bam_line->core.n_cigar; icig++) 
330     {
331         // Conversion from uint32_t to MIDNSHP
332         //  0123456
333         //  MIDNSHP
334         int cig  = bam1_cigar(bam_line)[icig] & BAM_CIGAR_MASK;
335         int ncig = bam1_cigar(bam_line)[icig] >> BAM_CIGAR_SHIFT;
336         if ( cig==1 )
337         {
338             iread  += ncig;
339             icycle += ncig;
340             continue;
341         }
342         if ( cig==2 )
343         {
344             iref += ncig;
345             continue;
346         }
347         if ( cig==4 )
348         {
349             icycle += ncig;
350             // Soft-clips are present in the sequence, but the position of the read marks a start of non-clipped sequence
351             //   iref += ncig;
352             iread  += ncig;
353             continue;
354         }
355         if ( cig==5 )
356         {
357             icycle += ncig;
358             continue;
359         }
360         if ( cig!=0 )
361             error("TODO: cigar %d, %s\n", cig,bam1_qname(bam_line));
362        
363         if ( ncig+iref > stats->rseq_len )
364             error("FIXME: %d+%d > %d, %s, %s:%d\n",ncig,iref,stats->rseq_len, bam1_qname(bam_line),stats->sam->header->target_name[bam_line->core.tid],bam_line->core.pos+1);
365
366         int im;
367         for (im=0; im<ncig; im++)
368         {
369             uint8_t cread = bam1_seqi(read,iread);
370             uint8_t cref  = stats->rseq_buf[iref];
371
372             // ---------------15
373             // =ACMGRSVTWYHKDBN
374             if ( cread==15 )
375             {
376                 int idx = is_fwd ? icycle : read_len-icycle-1;
377                 if ( idx>stats->max_len )
378                     error("mpc: %d>%d\n",idx,stats->max_len);
379                 idx = idx*stats->nquals;
380                 if ( idx>=stats->nquals*stats->nbases )
381                     error("FIXME: mpc_buf overflow\n");
382                 mpc_buf[idx]++;
383             }
384             else if ( cref && cread && cref!=cread )
385             {
386                 uint8_t qual = quals[iread] + 1;
387                 if ( qual>=stats->nquals )
388                     error("TODO: quality too high %d>=%d\n", quals[iread],stats->nquals);
389
390                 int idx = is_fwd ? icycle : read_len-icycle-1;
391                 if ( idx>stats->max_len )
392                     error("mpc: %d>%d\n",idx,stats->max_len);
393
394                 idx = idx*stats->nquals + qual;
395                 if ( idx>=stats->nquals*stats->nbases )
396                     error("FIXME: mpc_buf overflow\n");
397                 mpc_buf[idx]++;
398             }
399
400             iref++;
401             iread++;
402             icycle++;
403         }
404     }
405 }
406
407 void read_ref_seq(stats_t *stats,int32_t tid,int32_t pos)
408 {
409     khash_t(s) *h;
410     khiter_t iter;
411     faidx1_t val;
412     char *chr, c;
413     faidx_t *fai = stats->fai;
414
415     h = fai->hash;
416     chr = stats->sam->header->target_name[tid];
417
418     // ID of the sequence name
419     iter = kh_get(s, h, chr);
420     if (iter == kh_end(h)) 
421         error("No such reference sequence [%s]?\n", chr);
422     val = kh_value(h, iter);
423
424     // Check the boundaries
425     if (pos >= val.len)
426         error("Was the bam file mapped with the reference sequence supplied?"
427               " A read mapped beyond the end of the chromosome (%s:%d, chromosome length %d).\n", chr,pos,val.len);
428     int size = stats->nref_seq;
429     // The buffer extends beyond the chromosome end. Later the rest will be filled with N's.
430     if (size+pos > val.len) size = val.len-pos;
431
432     // Position the razf reader
433     razf_seek(fai->rz, val.offset + pos / val.line_blen * val.line_len + pos % val.line_blen, SEEK_SET);
434
435     uint8_t *ptr = stats->rseq_buf;
436     int nread = 0;
437     while ( nread<size && razf_read(fai->rz,&c,1) && !fai->rz->z_err )
438     {
439         if ( !isgraph(c) )
440             continue;
441
442         // Conversion between uint8_t coding and ACGT
443         //      -12-4---8-------
444         //      =ACMGRSVTWYHKDBN
445         if ( c=='A' || c=='a' )
446             *ptr = 1;
447         else if ( c=='C' || c=='c' )
448             *ptr = 2;
449         else if ( c=='G' || c=='g' )
450             *ptr = 4;
451         else if ( c=='T' || c=='t' )
452             *ptr = 8;
453         else
454             *ptr = 0;
455         ptr++;
456         nread++;
457     }
458     if ( nread < stats->nref_seq )
459     {
460         memset(ptr,0, stats->nref_seq - nread);
461         nread = stats->nref_seq;
462     }
463     stats->rseq_len = nread;
464     stats->rseq_pos = pos;
465     stats->tid      = tid;
466 }
467
468 float fai_gc_content(stats_t *stats)
469 {
470     uint32_t gc,count,c;
471     int i,size = stats->rseq_len;
472
473     // Count GC content
474     gc = count = 0;
475     for (i=0; i<size; i++)
476     {
477         c = stats->rseq_buf[i];
478         if ( c==2 || c==4 )
479         {
480             gc++;
481             count++;
482         }
483         else if ( c==1 || c==8 )
484             count++;
485     }
486     return count ? (float)gc/count : 0;
487 }
488
489
490 void realloc_buffers(stats_t *stats, int seq_len)
491 {
492     int n = 2*(1 + seq_len - stats->nbases) + stats->nbases;
493
494     stats->quals_1st = realloc(stats->quals_1st, n*stats->nquals*sizeof(uint64_t));
495     if ( !stats->quals_1st )
496         error("Could not realloc buffers, the sequence too long: %d (%ld)\n", seq_len,n*stats->nquals*sizeof(uint64_t));
497     memset(stats->quals_1st + stats->nbases*stats->nquals, 0, (n-stats->nbases)*stats->nquals*sizeof(uint64_t));
498
499     stats->quals_2nd = realloc(stats->quals_2nd, n*stats->nquals*sizeof(uint64_t));
500     if ( !stats->quals_2nd )
501         error("Could not realloc buffers, the sequence too long: %d (2x%ld)\n", seq_len,n*stats->nquals*sizeof(uint64_t));
502     memset(stats->quals_2nd + stats->nbases*stats->nquals, 0, (n-stats->nbases)*stats->nquals*sizeof(uint64_t));
503
504     if ( stats->mpc_buf )
505     {
506         stats->mpc_buf = realloc(stats->mpc_buf, n*stats->nquals*sizeof(uint64_t));
507         if ( !stats->mpc_buf )
508             error("Could not realloc buffers, the sequence too long: %d (%ld)\n", seq_len,n*stats->nquals*sizeof(uint64_t));
509         memset(stats->mpc_buf + stats->nbases*stats->nquals, 0, (n-stats->nbases)*stats->nquals*sizeof(uint64_t));
510     }
511
512     stats->acgt_cycles = realloc(stats->acgt_cycles, n*4*sizeof(uint64_t));
513     if ( !stats->acgt_cycles )
514         error("Could not realloc buffers, the sequence too long: %d (%ld)\n", seq_len,n*4*sizeof(uint64_t));
515     memset(stats->acgt_cycles + stats->nbases*4, 0, (n-stats->nbases)*4*sizeof(uint64_t));
516
517     stats->read_lengths = realloc(stats->read_lengths, n*sizeof(uint64_t));
518     if ( !stats->read_lengths )
519         error("Could not realloc buffers, the sequence too long: %d (%ld)\n", seq_len,n*sizeof(uint64_t));
520     memset(stats->read_lengths + stats->nbases, 0, (n-stats->nbases)*sizeof(uint64_t));
521
522     stats->insertions = realloc(stats->insertions, n*sizeof(uint64_t));
523     if ( !stats->insertions )
524         error("Could not realloc buffers, the sequence too long: %d (%ld)\n", seq_len,n*sizeof(uint64_t));
525     memset(stats->insertions + stats->nbases, 0, (n-stats->nbases)*sizeof(uint64_t));
526
527     stats->deletions = realloc(stats->deletions, n*sizeof(uint64_t));
528     if ( !stats->deletions )
529         error("Could not realloc buffers, the sequence too long: %d (%ld)\n", seq_len,n*sizeof(uint64_t));
530     memset(stats->deletions + stats->nbases, 0, (n-stats->nbases)*sizeof(uint64_t));
531
532     stats->ins_cycles_1st = realloc(stats->ins_cycles_1st, (n+1)*sizeof(uint64_t));
533     if ( !stats->ins_cycles_1st )
534         error("Could not realloc buffers, the sequence too long: %d (%ld)\n", seq_len,(n+1)*sizeof(uint64_t));
535     memset(stats->ins_cycles_1st + stats->nbases + 1, 0, (n+1-stats->nbases)*sizeof(uint64_t));
536
537     stats->ins_cycles_2nd = realloc(stats->ins_cycles_2nd, (n+1)*sizeof(uint64_t));
538     if ( !stats->ins_cycles_2nd )
539         error("Could not realloc buffers, the sequence too long: %d (%ld)\n", seq_len,(n+1)*sizeof(uint64_t));
540     memset(stats->ins_cycles_2nd + stats->nbases + 1, 0, (n+1-stats->nbases)*sizeof(uint64_t));
541
542     stats->del_cycles_1st = realloc(stats->del_cycles_1st, (n+1)*sizeof(uint64_t));
543     if ( !stats->del_cycles_1st )
544         error("Could not realloc buffers, the sequence too long: %d (%ld)\n", seq_len,(n+1)*sizeof(uint64_t));
545     memset(stats->del_cycles_1st + stats->nbases + 1, 0, (n+1-stats->nbases)*sizeof(uint64_t));
546
547     stats->del_cycles_2nd = realloc(stats->del_cycles_2nd, (n+1)*sizeof(uint64_t));
548     if ( !stats->del_cycles_2nd )
549         error("Could not realloc buffers, the sequence too long: %d (%ld)\n", seq_len,(n+1)*sizeof(uint64_t));
550     memset(stats->del_cycles_2nd + stats->nbases + 1, 0, (n+1-stats->nbases)*sizeof(uint64_t));
551
552     stats->nbases = n;
553
554     // Realloc the coverage distribution buffer
555     int *rbuffer = calloc(sizeof(int),seq_len*5);
556     n = stats->cov_rbuf.size-stats->cov_rbuf.start;
557     memcpy(rbuffer,stats->cov_rbuf.buffer+stats->cov_rbuf.start,n);
558     if ( stats->cov_rbuf.start>1 )
559         memcpy(rbuffer+n,stats->cov_rbuf.buffer,stats->cov_rbuf.start);
560     stats->cov_rbuf.start = 0;
561     free(stats->cov_rbuf.buffer);
562     stats->cov_rbuf.buffer = rbuffer;
563     stats->cov_rbuf.size = seq_len*5;
564 }
565
566 void collect_stats(bam1_t *bam_line, stats_t *stats)
567 {
568     if ( stats->flag_require && (bam_line->core.flag & stats->flag_require)!=stats->flag_require )
569         return;
570     if ( stats->flag_filter && (bam_line->core.flag & stats->flag_filter) )
571         return;
572
573     if ( !is_in_regions(bam_line,stats) )
574         return;
575
576     int seq_len = bam_line->core.l_qseq;
577     if ( !seq_len ) return;
578     if ( stats->filter_readlen!=-1 && seq_len!=stats->filter_readlen ) return;
579     if ( seq_len >= stats->nbases )
580         realloc_buffers(stats,seq_len);
581     if ( stats->max_len<seq_len )
582         stats->max_len = seq_len;
583
584     stats->read_lengths[seq_len]++;
585
586     // Count GC and ACGT per cycle
587     uint8_t base, *seq  = bam1_seq(bam_line);
588     int gc_count  = 0;
589     int i;
590     int reverse = IS_REVERSE(bam_line);
591     for (i=0; i<seq_len; i++)
592     {
593         // Conversion from uint8_t coding to ACGT
594         //      -12-4---8-------
595         //      =ACMGRSVTWYHKDBN
596         //       01 2   3
597         base = bam1_seqi(seq,i);
598         base /= 2;
599         if ( base==1 || base==2 ) gc_count++;
600         else if ( base>2 ) base=3;
601         if ( 4*(reverse ? seq_len-i-1 : i) + base >= stats->nbases*4 ) 
602             error("FIXME: acgt_cycles\n");
603         stats->acgt_cycles[ 4*(reverse ? seq_len-i-1 : i) + base ]++;
604     }
605     int gc_idx_min = gc_count*(stats->ngc-1)/seq_len;
606     int gc_idx_max = (gc_count+1)*(stats->ngc-1)/seq_len;
607     if ( gc_idx_max >= stats->ngc ) gc_idx_max = stats->ngc - 1;
608
609     // Determine which array (1st or 2nd read) will these stats go to,
610     //  trim low quality bases from end the same way BWA does, 
611     //  fill GC histogram
612     uint64_t *quals;
613     uint8_t *bam_quals = bam1_qual(bam_line);
614     if ( bam_line->core.flag&BAM_FREAD2 )
615     {
616         quals  = stats->quals_2nd;
617         stats->nreads_2nd++;
618         for (i=gc_idx_min; i<gc_idx_max; i++)
619             stats->gc_2nd[i]++;
620     }
621     else
622     {
623         quals = stats->quals_1st;
624         stats->nreads_1st++;
625         for (i=gc_idx_min; i<gc_idx_max; i++)
626             stats->gc_1st[i]++;
627     }
628     if ( stats->trim_qual>0 ) 
629         stats->nbases_trimmed += bwa_trim_read(stats->trim_qual, bam_quals, seq_len, reverse);
630
631     // Quality histogram and average quality
632     for (i=0; i<seq_len; i++)
633     {
634         uint8_t qual = bam_quals[ reverse ? seq_len-i-1 : i];
635         if ( qual>=stats->nquals )
636             error("TODO: quality too high %d>=%d\n", quals[i],stats->nquals);
637         if ( qual>stats->max_qual )
638             stats->max_qual = qual;
639
640         quals[ i*stats->nquals+qual ]++;
641         stats->sum_qual += qual;
642     }
643
644     // Look at the flags and increment appropriate counters (mapped, paired, etc)
645     if ( IS_UNMAPPED(bam_line) )
646         stats->nreads_unmapped++;
647     else
648     {
649         if ( !bam_line->core.qual )
650             stats->nreads_mq0++;
651
652         count_indels(stats,bam_line);
653
654         // The insert size is tricky, because for long inserts the libraries are
655         // prepared differently and the pairs point in other direction. BWA does
656         // not set the paired flag for them.  Similar thing is true also for 454
657         // reads. Therefore, do the insert size stats for all mapped reads.
658         int32_t isize = bam_line->core.isize;
659         if ( isize<0 ) isize = -isize;
660         if ( IS_PAIRED(bam_line) && isize!=0 )
661         {
662             stats->nreads_paired++;
663             if ( isize >= stats->nisize )
664                 isize=stats->nisize-1;
665
666             int pos_fst = bam_line->core.mpos - bam_line->core.pos;
667             int is_fst  = IS_READ1(bam_line) ? 1 : -1;
668             int is_fwd  = IS_REVERSE(bam_line) ? -1 : 1;
669             int is_mfwd = IS_MATE_REVERSE(bam_line) ? -1 : 1;
670
671             if ( is_fwd*is_mfwd>0 )
672                 stats->isize_other[isize]++;
673             else if ( is_fst*pos_fst>0 )
674             {
675                 if ( is_fst*is_fwd>0 )
676                     stats->isize_inward[isize]++;
677                 else
678                     stats->isize_outward[isize]++;
679             }
680             else if ( is_fst*pos_fst<0 )
681             {
682                 if ( is_fst*is_fwd>0 )
683                     stats->isize_outward[isize]++;
684                 else
685                     stats->isize_inward[isize]++;
686             }
687         }
688         else
689             stats->nreads_unpaired++;
690
691         // Number of mismatches 
692         uint8_t *nm = bam_aux_get(bam_line,"NM");
693         if (nm) 
694             stats->nmismatches += bam_aux2i(nm);
695
696         // Number of mapped bases from cigar 
697         // Conversion from uint32_t to MIDNSHP
698         //  012-4--
699         //  MIDNSHP
700         if ( bam_line->core.n_cigar == 0) 
701             error("FIXME: mapped read with no cigar?\n");
702         int readlen=seq_len;
703         if ( stats->regions )
704         {
705             // Count only on-target bases
706             int iref = bam_line->core.pos + 1;
707             for (i=0; i<bam_line->core.n_cigar; i++)
708             {
709                 int cig  = bam1_cigar(bam_line)[i]&BAM_CIGAR_MASK;
710                 int ncig = bam1_cigar(bam_line)[i]>>BAM_CIGAR_SHIFT;
711                 if ( cig==2 ) readlen += ncig;
712                 else if ( cig==0 ) 
713                 {
714                     if ( iref < stats->reg_from ) ncig -= stats->reg_from-iref;
715                     else if ( iref+ncig-1 > stats->reg_to ) ncig -= iref+ncig-1 - stats->reg_to;
716                     if ( ncig<0 ) ncig = 0;
717                     stats->nbases_mapped_cigar += ncig;
718                     iref += bam1_cigar(bam_line)[i]>>BAM_CIGAR_SHIFT;
719                 }
720                 else if ( cig==1 )
721                 {
722                     iref += ncig;
723                     if ( iref>=stats->reg_from && iref<=stats->reg_to )
724                         stats->nbases_mapped_cigar += ncig;
725                 }
726             }
727         }
728         else
729         {
730             // Count the whole read
731             for (i=0; i<bam_line->core.n_cigar; i++) 
732             {
733                 if ( (bam1_cigar(bam_line)[i]&BAM_CIGAR_MASK)==0 || (bam1_cigar(bam_line)[i]&BAM_CIGAR_MASK)==1 )
734                     stats->nbases_mapped_cigar += bam1_cigar(bam_line)[i]>>BAM_CIGAR_SHIFT;
735                 if ( (bam1_cigar(bam_line)[i]&BAM_CIGAR_MASK)==2 )
736                     readlen += bam1_cigar(bam_line)[i]>>BAM_CIGAR_SHIFT;
737             }
738         }
739         stats->nbases_mapped += seq_len;
740
741         if ( stats->tid==bam_line->core.tid && bam_line->core.pos<stats->pos )
742             stats->is_sorted = 0;
743         stats->pos = bam_line->core.pos;
744
745         if ( stats->is_sorted )
746         {
747             if ( stats->tid==-1 || stats->tid!=bam_line->core.tid )
748                 round_buffer_flush(stats,-1);
749
750             // Mismatches per cycle and GC-depth graph
751             if ( stats->fai )
752             {
753                 // First pass, new chromosome or sequence spanning beyond the end of the buffer 
754                 if ( stats->rseq_pos==-1 || stats->tid != bam_line->core.tid || stats->rseq_pos+stats->rseq_len < bam_line->core.pos+readlen )
755                 {
756                     read_ref_seq(stats,bam_line->core.tid,bam_line->core.pos);
757
758                     // Get the reference GC content for this bin. Note that in the current implementation
759                     //  the GCD bins overlap by seq_len-1. Because the bin size is by default 20k and the 
760                     //  expected read length only ~100bp, it shouldn't really matter.
761                     stats->gcd_pos = bam_line->core.pos;
762                     stats->igcd++;
763
764                     if ( stats->igcd >= stats->ngcd )
765                         error("The genome too long or the GCD bin overlaps too big [%ud]\n", stats->igcd);
766
767                     stats->gcd[ stats->igcd ].gc = fai_gc_content(stats);
768                 }
769                 count_mismatches_per_cycle(stats,bam_line);
770             }
771             else if ( stats->gcd_pos==-1 || stats->tid != bam_line->core.tid || bam_line->core.pos - stats->gcd_pos > stats->gcd_bin_size )
772             {
773                 // First pass or a new chromosome
774                 stats->tid     = bam_line->core.tid;
775                 stats->gcd_pos = bam_line->core.pos;
776                 stats->igcd++;
777
778                 if ( stats->igcd >= stats->ngcd )
779                 {
780                     uint32_t n = 2*(1 + stats->ngcd);
781                     stats->gcd = realloc(stats->gcd, n*sizeof(gc_depth_t));
782                     if ( !stats->gcd )
783                         error("Could not realloc GCD buffer, too many chromosomes or the genome too long?? [%u %u]\n", stats->ngcd,n);
784                     memset(&(stats->gcd[stats->ngcd]),0,(n-stats->ngcd)*sizeof(gc_depth_t));
785                 }
786             }
787
788             stats->gcd[ stats->igcd ].depth++;
789             // When no reference sequence is given, approximate the GC from the read (much shorter window, but otherwise OK)
790             if ( !stats->fai )
791                 stats->gcd[ stats->igcd ].gc += (float) gc_count / seq_len;
792
793             // Coverage distribution graph
794             round_buffer_flush(stats,bam_line->core.pos);
795             round_buffer_insert_read(&(stats->cov_rbuf),bam_line->core.pos,bam_line->core.pos+seq_len-1);
796         }
797     }
798
799     stats->total_len += seq_len;
800     if ( IS_DUP(bam_line) )
801     {
802         stats->total_len_dup += seq_len;
803         stats->nreads_dup++;
804     }
805 }
806
807 // Sort by GC and depth
808 #define GCD_t(x) ((gc_depth_t *)x)
809 static int gcd_cmp(const void *a, const void *b)
810 {
811     if ( GCD_t(a)->gc < GCD_t(b)->gc ) return -1;
812     if ( GCD_t(a)->gc > GCD_t(b)->gc ) return 1;
813     if ( GCD_t(a)->depth < GCD_t(b)->depth ) return -1;
814     if ( GCD_t(a)->depth > GCD_t(b)->depth ) return 1;
815     return 0;
816 }
817 #undef GCD_t
818
819 float gcd_percentile(gc_depth_t *gcd, int N, int p)
820 {
821     float n,d;
822     int k;
823
824     n = p*(N+1)/100;
825     k = n;
826     if ( k<=0 ) 
827         return gcd[0].depth;
828     if ( k>=N ) 
829         return gcd[N-1].depth;
830
831     d = n - k;
832     return gcd[k-1].depth + d*(gcd[k].depth - gcd[k-1].depth);
833 }
834
835 void output_stats(stats_t *stats)
836 {
837     // Calculate average insert size and standard deviation (from the main bulk data only)
838     int isize, ibulk=0;
839     uint64_t nisize=0, nisize_inward=0, nisize_outward=0, nisize_other=0;
840     for (isize=1; isize<stats->nisize; isize++)
841     {
842         // Each pair was counted twice
843         stats->isize_inward[isize] *= 0.5;
844         stats->isize_outward[isize] *= 0.5;
845         stats->isize_other[isize] *= 0.5;
846
847         nisize_inward += stats->isize_inward[isize];
848         nisize_outward += stats->isize_outward[isize];
849         nisize_other += stats->isize_other[isize];
850         nisize += stats->isize_inward[isize] + stats->isize_outward[isize] + stats->isize_other[isize];
851     }
852
853     double bulk=0, avg_isize=0, sd_isize=0;
854     for (isize=1; isize<stats->nisize; isize++)
855     {
856         bulk += stats->isize_inward[isize] + stats->isize_outward[isize] + stats->isize_other[isize];
857         avg_isize += isize * (stats->isize_inward[isize] + stats->isize_outward[isize] + stats->isize_other[isize]);
858
859         if ( bulk/nisize > stats->isize_main_bulk )
860         {
861             ibulk  = isize+1;
862             nisize = bulk;
863             break;
864         }
865     }
866     avg_isize /= nisize ? nisize : 1;
867     for (isize=1; isize<ibulk; isize++)
868         sd_isize += (stats->isize_inward[isize] + stats->isize_outward[isize] + stats->isize_other[isize]) * (isize-avg_isize)*(isize-avg_isize) / nisize;
869     sd_isize = sqrt(sd_isize);
870
871
872     printf("# This file was produced by bamcheck (%s)\n",BAMCHECK_VERSION);
873     printf("# The command line was:  %s",stats->argv[0]);
874     int i;
875     for (i=1; i<stats->argc; i++)
876         printf(" %s",stats->argv[i]);
877     printf("\n");
878     printf("# Summary Numbers. Use `grep ^SN | cut -f 2-` to extract this part.\n");
879     printf("SN\tsequences:\t%ld\n", (long)(stats->nreads_1st+stats->nreads_2nd));
880     printf("SN\tis paired:\t%d\n", stats->nreads_1st&&stats->nreads_2nd ? 1 : 0);
881     printf("SN\tis sorted:\t%d\n", stats->is_sorted ? 1 : 0);
882     printf("SN\t1st fragments:\t%ld\n", (long)stats->nreads_1st);
883     printf("SN\tlast fragments:\t%ld\n", (long)stats->nreads_2nd);
884     printf("SN\treads mapped:\t%ld\n", (long)(stats->nreads_paired+stats->nreads_unpaired));
885     printf("SN\treads unmapped:\t%ld\n", (long)stats->nreads_unmapped);
886     printf("SN\treads unpaired:\t%ld\n", (long)stats->nreads_unpaired);
887     printf("SN\treads paired:\t%ld\n", (long)stats->nreads_paired);
888     printf("SN\treads duplicated:\t%ld\n", (long)stats->nreads_dup);
889     printf("SN\treads MQ0:\t%ld\n", (long)stats->nreads_mq0);
890     printf("SN\ttotal length:\t%ld\n", (long)stats->total_len);
891     printf("SN\tbases mapped:\t%ld\n", (long)stats->nbases_mapped);
892     printf("SN\tbases mapped (cigar):\t%ld\n", (long)stats->nbases_mapped_cigar);
893     printf("SN\tbases trimmed:\t%ld\n", (long)stats->nbases_trimmed);
894     printf("SN\tbases duplicated:\t%ld\n", (long)stats->total_len_dup);
895     printf("SN\tmismatches:\t%ld\n", (long)stats->nmismatches);
896     printf("SN\terror rate:\t%e\n", (float)stats->nmismatches/stats->nbases_mapped_cigar);
897     float avg_read_length = (stats->nreads_1st+stats->nreads_2nd)?stats->total_len/(stats->nreads_1st+stats->nreads_2nd):0;
898     printf("SN\taverage length:\t%.0f\n", avg_read_length);
899     printf("SN\tmaximum length:\t%d\n", stats->max_len);
900     printf("SN\taverage quality:\t%.1f\n", stats->total_len?stats->sum_qual/stats->total_len:0);
901     printf("SN\tinsert size average:\t%.1f\n", avg_isize);
902     printf("SN\tinsert size standard deviation:\t%.1f\n", sd_isize);
903     printf("SN\tinward oriented pairs:\t%ld\n", (long)nisize_inward);
904     printf("SN\toutward oriented pairs:\t%ld\n", (long)nisize_outward);
905     printf("SN\tpairs with other orientation:\t%ld\n", (long)nisize_other);
906
907     int ibase,iqual;
908     if ( stats->max_len<stats->nbases ) stats->max_len++;
909     if ( stats->max_qual+1<stats->nquals ) stats->max_qual++;
910     printf("# First Fragment Qualitites. Use `grep ^FFQ | cut -f 2-` to extract this part.\n");
911     printf("# Columns correspond to qualities and rows to cycles. First column is the cycle number.\n");
912     for (ibase=0; ibase<stats->max_len; ibase++)
913     {
914         printf("FFQ\t%d",ibase+1);
915         for (iqual=0; iqual<=stats->max_qual; iqual++)
916         {
917             printf("\t%ld", (long)stats->quals_1st[ibase*stats->nquals+iqual]);
918         }
919         printf("\n");
920     }
921     printf("# Last Fragment Qualitites. Use `grep ^LFQ | cut -f 2-` to extract this part.\n");
922     printf("# Columns correspond to qualities and rows to cycles. First column is the cycle number.\n");
923     for (ibase=0; ibase<stats->max_len; ibase++)
924     {
925         printf("LFQ\t%d",ibase+1);
926         for (iqual=0; iqual<=stats->max_qual; iqual++)
927         {
928             printf("\t%ld", (long)stats->quals_2nd[ibase*stats->nquals+iqual]);
929         }
930         printf("\n");
931     }
932     if ( stats->mpc_buf )
933     {
934         printf("# Mismatches per cycle and quality. Use `grep ^MPC | cut -f 2-` to extract this part.\n");
935         printf("# Columns correspond to qualities, rows to cycles. First column is the cycle number, second\n");
936         printf("# is the number of N's and the rest is the number of mismatches\n");
937         for (ibase=0; ibase<stats->max_len; ibase++)
938         {
939             printf("MPC\t%d",ibase+1);
940             for (iqual=0; iqual<=stats->max_qual; iqual++)
941             {
942                 printf("\t%ld", (long)stats->mpc_buf[ibase*stats->nquals+iqual]);
943             }
944             printf("\n");
945         }
946     }
947     printf("# GC Content of first fragments. Use `grep ^GCF | cut -f 2-` to extract this part.\n");
948     int ibase_prev = 0;
949     for (ibase=0; ibase<stats->ngc; ibase++)
950     {
951         if ( stats->gc_1st[ibase]==stats->gc_1st[ibase_prev] ) continue;
952         printf("GCF\t%.2f\t%ld\n", (ibase+ibase_prev)*0.5*100./(stats->ngc-1), (long)stats->gc_1st[ibase_prev]);
953         ibase_prev = ibase;
954     }
955     printf("# GC Content of last fragments. Use `grep ^GCL | cut -f 2-` to extract this part.\n");
956     ibase_prev = 0;
957     for (ibase=0; ibase<stats->ngc; ibase++)
958     {
959         if ( stats->gc_2nd[ibase]==stats->gc_2nd[ibase_prev] ) continue;
960         printf("GCL\t%.2f\t%ld\n", (ibase+ibase_prev)*0.5*100./(stats->ngc-1), (long)stats->gc_2nd[ibase_prev]);
961         ibase_prev = ibase;
962     }
963     printf("# ACGT content per cycle. Use `grep ^GCC | cut -f 2-` to extract this part. The columns are: cycle, and A,C,G,T counts [%%]\n");
964     for (ibase=0; ibase<stats->max_len; ibase++)
965     {
966         uint64_t *ptr = &(stats->acgt_cycles[ibase*4]);
967         uint64_t  sum = ptr[0]+ptr[1]+ptr[2]+ptr[3];
968         if ( ! sum ) continue;
969         printf("GCC\t%d\t%.2f\t%.2f\t%.2f\t%.2f\n", ibase,100.*ptr[0]/sum,100.*ptr[1]/sum,100.*ptr[2]/sum,100.*ptr[3]/sum);
970     }
971     printf("# Insert sizes. Use `grep ^IS | cut -f 2-` to extract this part. The columns are: pairs total, inward oriented pairs, outward oriented pairs, other pairs\n");
972     for (isize=1; isize<ibulk; isize++)
973         printf("IS\t%d\t%ld\t%ld\t%ld\t%ld\n", isize, (long)(stats->isize_inward[isize]+stats->isize_outward[isize]+stats->isize_other[isize]),
974             (long)stats->isize_inward[isize], (long)stats->isize_outward[isize], (long)stats->isize_other[isize]);
975
976     printf("# Read lengths. Use `grep ^RL | cut -f 2-` to extract this part. The columns are: read length, count\n");
977     int ilen;
978     for (ilen=0; ilen<stats->max_len; ilen++)
979     {
980         if ( stats->read_lengths[ilen]>0 )
981             printf("RL\t%d\t%ld\n", ilen, (long)stats->read_lengths[ilen]);
982     }
983
984     printf("# Indel distribution. Use `grep ^ID | cut -f 2-` to extract this part. The columns are: length, number of insertions, number of deletions\n");
985     for (ilen=0; ilen<stats->nindels; ilen++)
986     {
987         if ( stats->insertions[ilen]>0 || stats->deletions[ilen]>0 )
988             printf("ID\t%d\t%ld\t%ld\n", ilen+1, (long)stats->insertions[ilen], (long)stats->deletions[ilen]);
989     }
990
991     printf("# Indels per cycle. Use `grep ^IC | cut -f 2-` to extract this part. The columns are: cycle, number of insertions (fwd), .. (rev) , number of deletions (fwd), .. (rev)\n");
992     for (ilen=0; ilen<=stats->nbases; ilen++)
993     {
994         if ( stats->ins_cycles_1st[ilen]>0 || stats->ins_cycles_2nd[ilen]>0 || stats->del_cycles_1st[ilen]>0 || stats->del_cycles_2nd[ilen]>0 )
995             printf("IC\t%d\t%ld\t%ld\t%ld\t%ld\n", ilen+1, (long)stats->ins_cycles_1st[ilen], (long)stats->ins_cycles_2nd[ilen], (long)stats->del_cycles_1st[ilen], (long)stats->del_cycles_2nd[ilen]);
996     }
997
998     printf("# Coverage distribution. Use `grep ^COV | cut -f 2-` to extract this part.\n");
999     printf("COV\t[<%d]\t%d\t%ld\n",stats->cov_min,stats->cov_min-1, (long)stats->cov[0]);
1000     int icov;
1001     for (icov=1; icov<stats->ncov-1; icov++)
1002         printf("COV\t[%d-%d]\t%d\t%ld\n",stats->cov_min + (icov-1)*stats->cov_step, stats->cov_min + icov*stats->cov_step-1,stats->cov_min + icov*stats->cov_step-1, (long)stats->cov[icov]);
1003     printf("COV\t[%d<]\t%d\t%ld\n",stats->cov_min + (stats->ncov-2)*stats->cov_step-1,stats->cov_min + (stats->ncov-2)*stats->cov_step-1, (long)stats->cov[stats->ncov-1]);
1004
1005
1006     // Calculate average GC content, then sort by GC and depth
1007     printf("# GC-depth. Use `grep ^GCD | cut -f 2-` to extract this part. The columns are: GC%%, unique sequence percentiles, 10th, 25th, 50th, 75th and 90th depth percentile\n");
1008     uint32_t igcd;
1009     for (igcd=0; igcd<stats->igcd; igcd++)
1010     {
1011         if ( stats->fai )
1012             stats->gcd[igcd].gc = round(100. * stats->gcd[igcd].gc);
1013         else
1014             if ( stats->gcd[igcd].depth ) 
1015                 stats->gcd[igcd].gc = round(100. * stats->gcd[igcd].gc / stats->gcd[igcd].depth);
1016     }
1017     qsort(stats->gcd, stats->igcd+1, sizeof(gc_depth_t), gcd_cmp);
1018     igcd = 0;
1019     while ( igcd < stats->igcd )
1020     {
1021         // Calculate percentiles (10,25,50,75,90th) for the current GC content and print
1022         uint32_t nbins=0, itmp=igcd;
1023         float gc = stats->gcd[igcd].gc;
1024         while ( itmp<stats->igcd && fabs(stats->gcd[itmp].gc-gc)<0.1 )
1025         {
1026             nbins++;
1027             itmp++;
1028         }
1029         printf("GCD\t%.1f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n", gc, (igcd+nbins+1)*100./(stats->igcd+1),
1030                 gcd_percentile(&(stats->gcd[igcd]),nbins,10) *avg_read_length/stats->gcd_bin_size,
1031                 gcd_percentile(&(stats->gcd[igcd]),nbins,25) *avg_read_length/stats->gcd_bin_size, 
1032                 gcd_percentile(&(stats->gcd[igcd]),nbins,50) *avg_read_length/stats->gcd_bin_size, 
1033                 gcd_percentile(&(stats->gcd[igcd]),nbins,75) *avg_read_length/stats->gcd_bin_size, 
1034                 gcd_percentile(&(stats->gcd[igcd]),nbins,90) *avg_read_length/stats->gcd_bin_size 
1035               );
1036         igcd += nbins;
1037     }
1038 }
1039
1040 size_t mygetline(char **line, size_t *n, FILE *fp)
1041 {
1042     if (line == NULL || n == NULL || fp == NULL)
1043     {
1044         errno = EINVAL;
1045         return -1;
1046     }
1047     if (*n==0 || !*line)
1048     {
1049         *line = NULL;
1050         *n = 0;
1051     }
1052
1053     size_t nread=0;
1054     int c;
1055     while ((c=getc(fp))!= EOF && c!='\n')
1056     {
1057         if ( ++nread>=*n )
1058         {
1059             *n += 255;
1060             *line = realloc(*line, sizeof(char)*(*n));
1061         }
1062         (*line)[nread-1] = c;
1063     }
1064     if ( nread>=*n )
1065     {
1066         *n += 255;
1067         *line = realloc(*line, sizeof(char)*(*n));
1068     }
1069     (*line)[nread] = 0;
1070     return nread>0 ? nread : -1;
1071
1072 }
1073
1074 void init_regions(stats_t *stats, char *file)
1075 {
1076     khiter_t iter;
1077     khash_t(str) *header_hash;
1078
1079     bam_init_header_hash(stats->sam->header);
1080     header_hash = (khash_t(str)*)stats->sam->header->hash;
1081
1082     FILE *fp = fopen(file,"r");
1083     if ( !fp ) error("%s: %s\n",file,strerror(errno));
1084
1085     char *line = NULL;
1086     size_t len = 0;
1087     ssize_t nread;
1088     int warned = 0;
1089     int prev_tid=-1, prev_pos=-1;
1090     while ((nread = mygetline(&line, &len, fp)) != -1) 
1091     {
1092         if ( line[0] == '#' ) continue;
1093
1094         int i = 0;
1095         while ( i<nread && !isspace(line[i]) ) i++;
1096         if ( i>=nread ) error("Could not parse the file: %s [%s]\n", file,line);
1097         line[i] = 0;
1098
1099         iter = kh_get(str, header_hash, line);
1100         int tid = kh_val(header_hash, iter);
1101         if ( iter == kh_end(header_hash) )
1102         {
1103             if ( !warned )
1104                 fprintf(stderr,"Warning: Some sequences not present in the BAM, e.g. \"%s\". This message is printed only once.\n", line);
1105             warned = 1;
1106             continue;
1107         }
1108
1109         if ( tid >= stats->nregions )
1110         {
1111             stats->regions = realloc(stats->regions,sizeof(regions_t)*(stats->nregions+100));
1112             int j;
1113             for (j=stats->nregions; j<stats->nregions+100; j++)
1114             {
1115                 stats->regions[j].npos = stats->regions[j].mpos = stats->regions[j].cpos = 0;
1116                 stats->regions[j].pos = NULL;
1117             }
1118             stats->nregions += 100;
1119         }
1120         int npos = stats->regions[tid].npos;
1121         if ( npos >= stats->regions[tid].mpos )
1122         {
1123             stats->regions[tid].mpos += 1000;
1124             stats->regions[tid].pos = realloc(stats->regions[tid].pos,sizeof(pos_t)*stats->regions[tid].mpos);
1125         }
1126
1127         if ( (sscanf(line+i+1,"%d %d",&stats->regions[tid].pos[npos].from,&stats->regions[tid].pos[npos].to))!=2 ) error("Could not parse the region [%s]\n");
1128         if ( prev_tid==-1 || prev_tid!=tid )
1129         {
1130             prev_tid = tid;
1131             prev_pos = stats->regions[tid].pos[npos].from;
1132         }
1133         if ( prev_pos>stats->regions[tid].pos[npos].from )
1134             error("The positions are not in chromosomal order (%s:%d comes after %d)\n", line,stats->regions[tid].pos[npos].from,prev_pos);
1135         stats->regions[tid].npos++;
1136     }
1137     if (line) free(line);
1138     if ( !stats->regions ) error("Unable to map the -t sequences to the BAM sequences.\n");
1139     fclose(fp);
1140 }
1141
1142 void destroy_regions(stats_t *stats)
1143 {
1144     int i;
1145     for (i=0; i<stats->nregions; i++)
1146     {
1147         if ( !stats->regions[i].mpos ) continue;
1148         free(stats->regions[i].pos);
1149     }
1150     if ( stats->regions ) free(stats->regions);
1151 }
1152
1153 static int fetch_read(const bam1_t *bam_line, void *data)
1154 {
1155     collect_stats((bam1_t*)bam_line,(stats_t*)data);
1156     return 1;
1157 }
1158
1159 void reset_regions(stats_t *stats)
1160 {
1161     int i;
1162     for (i=0; i<stats->nregions; i++)
1163         stats->regions[i].cpos = 0;
1164 }
1165
1166 int is_in_regions(bam1_t *bam_line, stats_t *stats)
1167 {
1168     if ( !stats->regions ) return 1;
1169
1170     if ( bam_line->core.tid >= stats->nregions || bam_line->core.tid<0 ) return 0;
1171     if ( !stats->is_sorted ) error("The BAM must be sorted in order for -t to work.\n");
1172
1173     regions_t *reg = &stats->regions[bam_line->core.tid];
1174     if ( reg->cpos==reg->npos ) return 0;       // done for this chr
1175
1176     // Find a matching interval or skip this read. No splicing of reads is done, no indels or soft clips considered, 
1177     //  even small overlap is enough to include the read in the stats.
1178     int i = reg->cpos;
1179     while ( i<reg->npos && reg->pos[i].to<=bam_line->core.pos ) i++;
1180     if ( i>=reg->npos ) { reg->cpos = reg->npos; return 0; }
1181     if ( bam_line->core.pos + bam_line->core.l_qseq + 1 < reg->pos[i].from ) return 0;
1182     reg->cpos = i;
1183     stats->reg_from = reg->pos[i].from;
1184     stats->reg_to   = reg->pos[i].to;
1185
1186     return 1;
1187 }
1188
1189 void error(const char *format, ...)
1190 {
1191     if ( !format )
1192     {
1193         printf("Version: %s\n", BAMCHECK_VERSION);
1194         printf("About: The program collects statistics from BAM files. The output can be visualized using plot-bamcheck.\n");
1195         printf("Usage: bamcheck [OPTIONS] file.bam\n");
1196         printf("       bamcheck [OPTIONS] file.bam chr:from-to\n");
1197         printf("Options:\n");
1198         printf("    -c, --coverage <int>,<int>,<int>    Coverage distribution min,max,step [1,1000,1]\n");
1199         printf("    -d, --remove-dups                   Exlude from statistics reads marked as duplicates\n");
1200         printf("    -f, --required-flag <int>           Required flag, 0 for unset [0]\n");
1201         printf("    -F, --filtering-flag <int>          Filtering flag, 0 for unset [0]\n");
1202         printf("    -h, --help                          This help message\n");
1203         printf("    -i, --insert-size <int>             Maximum insert size [8000]\n");
1204         printf("    -l, --read-length <int>             Include in the statistics only reads with the given read length []\n");
1205         printf("    -m, --most-inserts <float>          Report only the main part of inserts [0.99]\n");
1206         printf("    -q, --trim-quality <int>            The BWA trimming parameter [0]\n");
1207         printf("    -r, --ref-seq <file>                Reference sequence (required for GC-depth calculation).\n");
1208         printf("    -t, --target-regions <file>         Do stats in these regions only. Tab-delimited file chr,from,to, 1-based, inclusive.\n");
1209         printf("    -s, --sam                           Input is SAM\n");
1210         printf("\n");
1211     }
1212     else
1213     {
1214         va_list ap;
1215         va_start(ap, format);
1216         vfprintf(stderr, format, ap);
1217         va_end(ap);
1218     }
1219     exit(-1);
1220 }
1221
1222 int main(int argc, char *argv[])
1223 {
1224     char *targets = NULL;
1225     char *bam_fname = NULL;
1226     samfile_t *sam = NULL;
1227     char in_mode[5];
1228
1229     stats_t *stats = calloc(1,sizeof(stats_t));
1230     stats->ngc    = 200;
1231     stats->nquals = 95;
1232     stats->nbases = 300;
1233     stats->nisize = 8000;
1234     stats->max_len   = 30;
1235     stats->max_qual  = 40;
1236     stats->isize_main_bulk = 0.99;   // There are always outliers at the far end
1237     stats->gcd_bin_size = 20000;
1238     stats->ngcd         = 3e5;     // 300k of 20k bins is enough to hold a genome 6Gbp big
1239     stats->nref_seq     = stats->gcd_bin_size;
1240     stats->rseq_pos     = -1;
1241     stats->tid = stats->gcd_pos = -1;
1242     stats->is_sorted = 1;
1243     stats->cov_min  = 1;
1244     stats->cov_max  = 1000;
1245     stats->cov_step = 1;
1246     stats->argc = argc;
1247     stats->argv = argv;
1248     stats->filter_readlen = -1;
1249     stats->nindels = stats->nbases;
1250
1251     strcpy(in_mode, "rb");
1252
1253     static struct option loptions[] = 
1254     {
1255         {"help",0,0,'h'},
1256         {"remove-dups",0,0,'d'},
1257         {"sam",0,0,'s'},
1258         {"ref-seq",0,0,'r'},
1259         {"coverage",0,0,'c'},
1260         {"read-length",0,0,'l'},
1261         {"insert-size",0,0,'i'},
1262         {"most-inserts",0,0,'m'},
1263         {"trim-quality",0,0,'q'},
1264         {"target-regions",0,0,'t'},
1265         {"required-flag",0,0,'f'},
1266         {"filtering-flag",0,0,'F'},
1267         {0,0,0,0}
1268     };
1269     int opt;
1270     while ( (opt=getopt_long(argc,argv,"?hdsr:c:l:i:t:m:q:f:F:",loptions,NULL))>0 )
1271     {
1272         switch (opt)
1273         {
1274             case 'f': stats->flag_require=strtol(optarg,0,0); break;
1275             case 'F': stats->flag_filter=strtol(optarg,0,0); break;
1276             case 'd': stats->flag_filter|=BAM_FDUP; break;
1277             case 's': strcpy(in_mode, "r"); break;
1278             case 'r': stats->fai = fai_load(optarg); 
1279                       if (stats->fai==0) 
1280                           error("Could not load faidx: %s\n", optarg); 
1281                       break;
1282             case 'c': if ( sscanf(optarg,"%d,%d,%d",&stats->cov_min,&stats->cov_max,&stats->cov_step)!= 3 ) 
1283                           error("Unable to parse -c %s\n", optarg); 
1284                       break;
1285             case 'l': stats->filter_readlen = atoi(optarg); break;
1286             case 'i': stats->nisize = atoi(optarg); break;
1287             case 'm': stats->isize_main_bulk = atof(optarg); break;
1288             case 'q': stats->trim_qual = atoi(optarg); break;
1289             case 't': targets = optarg; break;
1290             case '?': 
1291             case 'h': error(NULL);
1292             default: error("Unknown argument: %s\n", optarg);
1293         }
1294     }
1295     if ( optind<argc )
1296         bam_fname = argv[optind++];
1297
1298     if ( !bam_fname )
1299     {
1300         if ( isatty(fileno((FILE *)stdin)) )
1301             error(NULL);
1302         bam_fname = "-";
1303     }
1304
1305     // Init structures
1306     //  .. coverage bins and round buffer
1307     if ( stats->cov_step > stats->cov_max - stats->cov_min + 1 )
1308     {
1309         stats->cov_step = stats->cov_max - stats->cov_min;
1310         if ( stats->cov_step <= 0 )
1311             stats->cov_step = 1;
1312     }
1313     stats->ncov = 3 + (stats->cov_max-stats->cov_min) / stats->cov_step;
1314     stats->cov_max = stats->cov_min + ((stats->cov_max-stats->cov_min)/stats->cov_step +1)*stats->cov_step - 1;
1315     stats->cov = calloc(sizeof(uint64_t),stats->ncov);
1316     stats->cov_rbuf.size = stats->nbases*5;
1317     stats->cov_rbuf.buffer = calloc(sizeof(int32_t),stats->cov_rbuf.size);
1318     // .. bam
1319     if ((sam = samopen(bam_fname, in_mode, NULL)) == 0) 
1320         error("Failed to open: %s\n", bam_fname);
1321     stats->sam = sam;
1322     bam1_t *bam_line = bam_init1();
1323     // .. arrays
1324     stats->quals_1st      = calloc(stats->nquals*stats->nbases,sizeof(uint64_t));
1325     stats->quals_2nd      = calloc(stats->nquals*stats->nbases,sizeof(uint64_t));
1326     stats->gc_1st         = calloc(stats->ngc,sizeof(uint64_t));
1327     stats->gc_2nd         = calloc(stats->ngc,sizeof(uint64_t));
1328     stats->isize_inward   = calloc(stats->nisize,sizeof(uint64_t));
1329     stats->isize_outward  = calloc(stats->nisize,sizeof(uint64_t));
1330     stats->isize_other    = calloc(stats->nisize,sizeof(uint64_t));
1331     stats->gcd            = calloc(stats->ngcd,sizeof(gc_depth_t));
1332     stats->rseq_buf       = calloc(stats->nref_seq,sizeof(uint8_t));
1333     stats->mpc_buf        = stats->fai ? calloc(stats->nquals*stats->nbases,sizeof(uint64_t)) : NULL;
1334     stats->acgt_cycles    = calloc(4*stats->nbases,sizeof(uint64_t));
1335     stats->read_lengths   = calloc(stats->nbases,sizeof(uint64_t));
1336     stats->insertions     = calloc(stats->nbases,sizeof(uint64_t));
1337     stats->deletions      = calloc(stats->nbases,sizeof(uint64_t));
1338     stats->ins_cycles_1st = calloc(stats->nbases+1,sizeof(uint64_t));
1339     stats->ins_cycles_2nd = calloc(stats->nbases+1,sizeof(uint64_t));
1340     stats->del_cycles_1st = calloc(stats->nbases+1,sizeof(uint64_t));
1341     stats->del_cycles_2nd = calloc(stats->nbases+1,sizeof(uint64_t));
1342     if ( targets )
1343         init_regions(stats, targets);
1344
1345     // Collect statistics
1346     if ( optind<argc )
1347     {
1348         // Collect stats in selected regions only
1349         bam_index_t *bam_idx = bam_index_load(bam_fname);
1350         if (bam_idx == 0)
1351             error("Random alignment retrieval only works for indexed BAM files.\n");
1352
1353         int i;
1354         for (i=optind; i<argc; i++) 
1355         {
1356             int tid, beg, end;
1357             bam_parse_region(stats->sam->header, argv[i], &tid, &beg, &end);
1358             if ( tid < 0 ) continue;
1359             reset_regions(stats);
1360             bam_fetch(stats->sam->x.bam, bam_idx, tid, beg, end, stats, fetch_read);
1361         }
1362         bam_index_destroy(bam_idx);
1363     }
1364     else
1365     {
1366         // Stream through the entire BAM ignoring off-target regions if -t is given
1367         while (samread(sam,bam_line) >= 0) 
1368             collect_stats(bam_line,stats);
1369     }
1370     round_buffer_flush(stats,-1);
1371
1372     output_stats(stats);
1373
1374     bam_destroy1(bam_line);
1375     samclose(stats->sam);
1376     if (stats->fai) fai_destroy(stats->fai);
1377     free(stats->cov_rbuf.buffer); free(stats->cov);
1378     free(stats->quals_1st); free(stats->quals_2nd); 
1379     free(stats->gc_1st); free(stats->gc_2nd);
1380     free(stats->isize_inward); free(stats->isize_outward); free(stats->isize_other);
1381     free(stats->gcd);
1382     free(stats->rseq_buf);
1383     free(stats->mpc_buf);
1384     free(stats->acgt_cycles);
1385     free(stats->read_lengths);
1386     free(stats->insertions);
1387     free(stats->deletions);
1388     free(stats->ins_cycles_1st);
1389     free(stats->ins_cycles_2nd);
1390     free(stats->del_cycles_1st);
1391     free(stats->del_cycles_2nd);
1392     destroy_regions(stats);
1393     free(stats);
1394
1395         return 0;
1396 }
1397
1398
1399