Source code

Download source archive ↓

upstream-dependencies/samtools/coverage.c

1/* coverage.c -- samtools coverage subcommand
2
3    Copyright (C) 2018,2019 Florian Breitwieser
4    Portions copyright (C) 2019-2021, 2023-2024 Genome Research Ltd.
5
6    Author: Florian P Breitwieser <florian.bw@gmail.com>
7
8Permission is hereby granted, free of charge, to any person obtaining a copy
9of this software and associated documentation files (the "Software"), to deal
10in the Software without restriction, including without limitation the rights
11to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12copies of the Software, and to permit persons to whom the Software is
13furnished to do so, subject to the following conditions:
14
15The above copyright notice and this permission notice shall be included in
16all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24DEALINGS IN THE SOFTWARE.  */
25
26/* This program calculates coverage from multiple BAMs
27 * simultaneously, to achieve random access and to use the BED interface.
28 * To compile this program separately, you may:
29 *
30 *   gcc -g -O2 -Wall -o bamcov -D_MAIN_BAMCOV coverage.c -lhts -lz
31 */
32
33// C headers
34#include <config.h>
35
36#include <ctype.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <stdarg.h>  // variadic functions
40#include <limits.h>  // INT_MAX
41#include <math.h>    // round
42#include <stdbool.h>
43#include <string.h>
44#include <unistd.h>
45#include <assert.h>
46
47#ifdef _WIN32
48#include <windows.h>
49#else
50#include <sys/ioctl.h>
51#endif
52
53#include "htslib/sam.h"
54#include "htslib/hts.h"
55#include "samtools.h"
56#include "sam_opts.h"
57
58typedef struct {  // auxiliary data structure to hold stats on coverage
59    unsigned long long n_covered_bases;
60    unsigned long long summed_coverage;
61    unsigned long long summed_baseQ;
62    unsigned long long summed_mapQ;
63    unsigned long long quality_bases;
64    unsigned int n_reads;
65    unsigned int n_selected_reads;
66    bool covered;
67    hts_pos_t beg;
68    hts_pos_t end;
69    int64_t bin_width;
70} stats_aux_t;
71
72typedef struct {  // auxiliary data structure to hold a BAM file
73    samFile *fp;     // file handle
74    sam_hdr_t *hdr;  // file header
75    hts_itr_t *iter; // iterator to a region - NULL for us by default
76    int min_mapQ;    // mapQ filter
77    int min_len;     // length filter
78    int fail_flags;
79    int required_flags;
80    stats_aux_t *stats;
81} bam_aux_t;
82
83#if __STDC_VERSION__ >= 199901L
84#define VERTICAL_LINE "\u2502" // BOX DRAWINGS LIGHT VERTICAL
85
86// UTF8 specifies block characters in eights going from \u2581 (lower one eight block) to \u2588 (full block)
87//   https://en.wikipedia.org/wiki/Block_Elements
88// LOWER ONE EIGHTH BLOCK … FULL BLOCK
89static const char *const BLOCK_CHARS8[8] = {"\u2581", "\u2582", "\u2583", "\u2584", "\u2585", "\u2586", "\u2587", "\u2588"};
90// In some terminals / with some fonts not all UTF8 block characters are supported (e.g. Putty). Use only half and full block for those
91static const char *const BLOCK_CHARS2[2] = {".", ":"};
92
93#else
94
95// Fall back to explicit UTF-8 encodings of the same characters
96#define VERTICAL_LINE "\xE2\x94\x82"
97
98static const char *const BLOCK_CHARS8[8] = {
99    "\xE2\x96\x81", "\xE2\x96\x82", "\xE2\x96\x83", "\xE2\x96\x84",
100    "\xE2\x96\x85", "\xE2\x96\x86", "\xE2\x96\x87", "\xE2\x96\x88" };
101
102static const char *const BLOCK_CHARS2[2] = {".", ":"};
103
104#endif
105
106// in bam_plcmd.c
107int read_file_list(const char *file_list, int *n, char **argv[]);
108
109static int usage(void) {
110    fprintf(stdout, "Usage: samtools coverage [options] in1.bam [in2.bam [...]]\n\n"
111            "Input options:\n"
112            "  -b, --bam-list FILE     list of input BAM filenames, one per line\n"
113            "  -l, --min-read-len INT  ignore reads shorter than INT bp [0]\n"
114            "  -q, --min-MQ INT        mapping quality threshold [0]\n"
115            "  -Q, --min-BQ INT        base quality threshold [0]\n"
116            "  --rf <int|str>          required flags: skip reads with mask bits unset []\n"
117            "  --ff <int|str>          filter flags: skip reads with mask bits set \n"
118            "                                      [UNMAP,SECONDARY,QCFAIL,DUP]\n"
119            "  -d, --depth INT         maximum allowed coverage depth [1000000].\n"
120            "                          If 0, depth is set to the maximum integer value,\n"
121            "                          effectively removing any depth limit.\n"
122            "Output options:\n"
123            "  -m, --histogram         show histogram instead of tabular output\n"
124            "  -D, --plot-depth        plot depth instead of tabular output\n"
125            "  -A, --ascii             show only ASCII characters in histogram\n"
126            "  -o, --output FILE       write output to FILE [stdout]\n"
127            "  -H, --no-header         don't print a header in tabular mode\n"
128            "  -w, --n-bins INT        number of bins in histogram [terminal width - 40]\n"
129            "  -r, --region REG        show specified region. Format: chr:start-end. \n"
130            "  -h, --help              help (this page)\n");
131
132    fprintf(stdout, "\nGeneric options:\n");
133    sam_global_opt_help(stdout, "-.--.--.");
134
135    fprintf(stdout,
136            "\nSee manpage for additional details.\n"
137            "  rname       Reference name / chromosome\n"
138            "  startpos    Start position\n"
139            "  endpos      End position (or sequence length)\n"
140            "  numreads    Number reads aligned to the region (after filtering)\n"
141            "  covbases    Number of covered bases with depth >= 1\n"
142            "  coverage    Percentage of covered bases [0..100]\n"
143            "  meandepth   Mean depth of coverage\n"
144            "  meanbaseq   Mean baseQ in covered region\n"
145            "  meanmapq    Mean mapQ of selected reads\n"
146           );
147
148    return EXIT_SUCCESS;
149}
150
151static char* center_text(char *text, char *buf, int width) {
152    int len = strlen(text);
153    assert(len <= width);
154    int padding = (width - len) / 2;
155    int padding_ex = (width - len) % 2;
156    if (padding >= 1)
157        sprintf(buf, " %*s%*s", len+padding, text, padding-1+padding_ex, " ");
158    else
159        sprintf(buf, "%s", text);
160
161    return buf;
162}
163
164static char* readable_bps(double base_pairs, char *buf) {
165    const char* units[] = {"", "K", "M", "G", "T"};
166    int i = 0;
167    while (base_pairs >= 1000 && i < (sizeof(units)/sizeof(units[0]) - 1)) {
168        base_pairs /= 1000;
169        i++;
170    }
171    sprintf(buf, "%.*f%s", i, base_pairs, units[i]);
172    return buf;
173}
174
175// read one alignment from one BAM file
176static int read_bam(void *data, bam1_t *b) {
177    bam_aux_t *aux = (bam_aux_t*)data; // data in fact is a pointer to an auxiliary structure
178    int nref = sam_hdr_nref(aux->hdr);
179    int ret;
180    while (1) {
181        if((ret = aux->iter? sam_itr_next(aux->fp, aux->iter, b) : sam_read1(aux->fp, aux->hdr, b)) < 0) break;
182        if (b->core.tid >= 0 && b->core.tid < nref)
183            aux->stats[b->core.tid].n_reads++;
184
185        if ( aux->fail_flags && (b->core.flag & aux->fail_flags) ) continue;
186        if ( aux->required_flags && !(b->core.flag & aux->required_flags) ) continue;
187        if ( b->core.qual < aux->min_mapQ ) continue;
188        if ( aux->min_len && bam_cigar2qlen(b->core.n_cigar, bam_get_cigar(b)) < aux->min_len ) continue;
189        if (b->core.tid >= 0 && b->core.tid < nref) {
190            aux->stats[b->core.tid].n_selected_reads++;
191            aux->stats[b->core.tid].summed_mapQ += b->core.qual;
192        }
193        break;
194    }
195    return ret;
196}
197
198void print_tabular_line(FILE *file_out, const sam_hdr_t *h, const stats_aux_t *stats, int tid) {
199    fputs(sam_hdr_tid2name(h, tid), file_out);
200    double region_len = (double) stats[tid].end - stats[tid].beg;
201    fprintf(file_out, "\t%"PRId64"\t%"PRId64"\t%u\t%llu\t%g\t%g\t%.3g\t%.3g\n",
202            stats[tid].beg+1,
203            stats[tid].end,
204            stats[tid].n_selected_reads,
205            stats[tid].n_covered_bases,
206            100.0 * stats[tid].n_covered_bases / region_len,
207            stats[tid].summed_coverage / region_len,
208            stats[tid].quality_bases > 0? stats[tid].summed_baseQ/(double) stats[tid].quality_bases : 0,
209            stats[tid].n_selected_reads > 0? stats[tid].summed_mapQ/(double) stats[tid].n_selected_reads : 0
210           );
211}
212
213void print_hist(FILE *file_out, const sam_hdr_t *h, const stats_aux_t *stats, int tid, const uint32_t *hist,
214        const int hist_size, const bool full_utf, const bool plot_coverage) {
215    int i, col;
216    bool show_percentiles = false;
217    const int n_rows = 10;
218    const char * const * BLOCK_CHARS = full_utf? BLOCK_CHARS8 : BLOCK_CHARS2;
219    const int blockchar_len = full_utf? 8 : 2;
220    double region_len = stats[tid].end - stats[tid].beg;
221
222    // Calculate histogram that contains percent covered
223    double hist_data[hist_size];
224    double max_val = 0.0;
225    for (i = 0; i < hist_size; ++i) {
226        hist_data[i] = (plot_coverage?1:100) * hist[i] / (double) stats[tid].bin_width;
227        if (hist_data[i] > max_val) max_val = hist_data[i];
228    }
229
230    char buf[30];
231    fprintf(file_out, "%s (%sbp)\n", sam_hdr_tid2name(h, tid), readable_bps(sam_hdr_tid2len(h, tid), buf));
232
233    double row_bin_size = max_val / (double) n_rows;
234    for (i = n_rows-1; i >= 0; --i) {
235        double current_bin = row_bin_size * i;
236        if (plot_coverage) {
237            fprintf(file_out, ">%8.1f ",i*row_bin_size);
238        } else if (show_percentiles) {
239            fprintf(file_out, ">%3i%% ", i*10);
240        } else {
241            fprintf(file_out, ">%7.2f%% ", current_bin);
242        }
243        fprintf(file_out, full_utf ? VERTICAL_LINE : "|");
244        for (col = 0; col < hist_size; ++col) {
245            // get the difference in eights, or halfs when full UTF8 is not supported
246            int cur_val_diff = round(blockchar_len * (hist_data[col] - current_bin) / row_bin_size) - 1;
247            if (cur_val_diff < 0) {
248                fputc(' ', file_out);
249            } else {
250                if (cur_val_diff >= blockchar_len)
251                    cur_val_diff = blockchar_len - 1;
252
253                fprintf(file_out, "%s", BLOCK_CHARS[cur_val_diff]);
254            }
255        }
256        fprintf(file_out, full_utf ? VERTICAL_LINE : "|");
257        fputc(' ', file_out);
258        switch (i) {
259            case 9: fprintf(file_out, "Number of reads: %u", stats[tid].n_selected_reads); break;
260            case 8: if (stats[tid].n_reads - stats[tid].n_selected_reads > 0) fprintf(file_out, "    (%i filtered)", stats[tid].n_reads - stats[tid].n_selected_reads); break;
261            case 7: fprintf(file_out, "Covered bases:   %sbp", readable_bps(stats[tid].n_covered_bases, buf)); break;
262            case 6: fprintf(file_out, "Percent covered: %.4g%%",
263                            100.0 * stats[tid].n_covered_bases / region_len); break;
264            case 5: fprintf(file_out, "Mean coverage:   %.3gx",
265                            stats[tid].summed_coverage / region_len); break;
266            case 4: fprintf(file_out, "Mean baseQ:      %.3g",
267                            stats[tid].quality_bases > 0? stats[tid].summed_baseQ/(double) stats[tid].quality_bases : 0); break;
268            case 3: fprintf(file_out, "Mean mapQ:       %.3g",
269                            stats[tid].summed_mapQ/(double) stats[tid].n_selected_reads); break;
270            case 1: fprintf(file_out, "Histo bin width: %sbp",
271                            readable_bps(stats[tid].bin_width, buf)); break;
272            case 0: if (plot_coverage) {
273                        fprintf(file_out, "Histo max cov:   %.5g",  max_val);
274                    } else {
275                        fprintf(file_out, "Histo max bin:   %.5g%%", max_val);
276                    }
277                    break;
278
279        };
280        fputc('\n', file_out);
281    }
282
283    // print x axis. Could be made pretty for widths that are not divisible
284    // by 10 by variable spacing of the labels, instead of placing a label every 10 characters
285    char buf2[50];
286    fprintf(file_out, "     %s", center_text(readable_bps(stats[tid].beg + 1, buf), buf2, 10));
287    int rest;
288    for (rest = 10; rest < 10*(hist_size/10); rest += 10) {
289        fprintf(file_out, "%s", center_text(readable_bps(stats[tid].beg + stats[tid].bin_width*rest, buf), buf2, 10));
290    }
291    int last_padding = hist_size%10;
292    fprintf(file_out, "%*s%s", last_padding, " ", center_text(readable_bps(stats[tid].end, buf), buf2, 10));
293    fprintf(file_out, "\n");
294}
295
296int main_coverage(int argc, char *argv[]) {
297    int status = EXIT_SUCCESS;
298
299    int ret, tid = -1, old_tid = -1, pos, i, j;
300
301    int max_depth = 1000000;
302    int opt_min_baseQ = 0;
303    int opt_min_mapQ = 0;
304    int opt_min_len = 0;
305    int opt_n_bins = 50;
306    bool opt_full_width = true;
307    char *opt_output_file = NULL;
308    bam_aux_t **data = NULL;
309    bam_mplp_t mplp = NULL;
310    const bam_pileup1_t **plp = NULL;
311    uint32_t *hist = NULL;
312    stats_aux_t *stats = NULL;
313    char *opt_reg = 0; // specified region
314    char *opt_file_list = NULL;
315    int n_bam_files = 0;
316    char **fn = NULL;
317    int fail_flags = (BAM_FUNMAP | BAM_FSECONDARY | BAM_FQCFAIL | BAM_FDUP); // Default fail flags
318    int required_flags = 0;
319    int print_value_warning = 0;
320
321    int *n_plp = NULL;
322    sam_hdr_t *h = NULL; // BAM header of the 1st input
323
324    bool opt_print_header = true;
325    bool opt_print_tabular = true;
326    bool opt_print_histogram = false;
327    bool opt_plot_coverage = false;
328    bool opt_full_utf = true;
329
330    FILE *file_out = stdout;
331
332    sam_global_args ga = SAM_GLOBAL_ARGS_INIT;
333    static const struct option lopts[] = {
334        SAM_OPT_GLOBAL_OPTIONS('-', 0, '-', '-', 0, '-'),
335        {"rf", required_argument, NULL, 1}, // require flag
336        {"ff", required_argument, NULL, 2}, // filter flag
337        {"incl-flags", required_argument, NULL, 1}, // require flag
338        {"excl-flags", required_argument, NULL, 2}, // filter flag
339        {"bam-list", required_argument, NULL, 'b'},
340        {"min-read-len", required_argument, NULL, 'l'},
341        {"min-MQ", required_argument, NULL, 'q'},
342        {"min-mq", required_argument, NULL, 'q'},
343        {"min-BQ", required_argument, NULL, 'Q'},
344        {"min-bq", required_argument, NULL, 'Q'},
345        {"histogram", no_argument, NULL, 'm'},
346        {"ascii", no_argument, NULL, 'A'},
347        {"plot-depth", no_argument, NULL, 'D'},
348        {"output", required_argument, NULL, 'o'},
349        {"no-header", no_argument, NULL, 'H'},
350        {"n-bins", required_argument, NULL, 'w'},
351        {"region", required_argument, NULL, 'r'},
352        {"help", no_argument, NULL, 'h'},
353        {"depth", required_argument, NULL, 'd'},
354        { NULL, 0, NULL, 0 }
355    };
356
357    // parse the command line
358    int c;
359    opterr = 0;
360    while ((c = getopt_long(argc, argv, "Ao:l:q:Q:hHw:r:b:md:D", lopts, NULL)) != -1) {
361        switch (c) {
362            case 1:
363                if ((required_flags = bam_str2flag(optarg)) < 0) {
364                    fprintf(stderr,"Could not parse --rf %s\n", optarg); return EXIT_FAILURE;
365                }; break;
366            case 2:
367                if ((fail_flags = bam_str2flag(optarg)) < 0) {
368                    fprintf(stderr,"Could not parse --ff %s\n", optarg); return EXIT_FAILURE;
369                }; break;
370            case 'o': opt_output_file = optarg; opt_full_width = false; break;
371            case 'l': opt_min_len = atoi(optarg); break;
372            case 'q': opt_min_mapQ = atoi(optarg); break;
373            case 'Q': opt_min_baseQ = atoi(optarg); break;
374            case 'd': max_depth = atoi(optarg); break; // maximum coverage depth
375            case 'w': opt_n_bins = atoi(optarg); opt_full_width = false;
376                      opt_print_histogram = true; opt_print_tabular = false;
377                      break;
378            case 'r': opt_reg = optarg; break;   // parsing a region requires a BAM header (strdup unnecessary)
379            case 'b': opt_file_list = optarg; break;
380            case 'm': opt_print_histogram = true; opt_print_tabular = false; break;
381            case 'A': opt_full_utf = false;
382                      opt_print_histogram = true; opt_print_tabular = false;
383                      break;
384            case 'D': opt_print_histogram = true;
385                     opt_print_tabular = false;
386                     opt_plot_coverage = true;
387                     break;
388            case 'H': opt_print_header = false; break;
389            case 'h': return usage();
390            default:  if (parse_sam_global_opt(c, optarg, lopts, &ga) == 0) break;
391                          /* else fall-through */
392            case '?':
393                if (optopt != '?') {  // '-?' appeared on command line
394                    if (optopt) { // Bad short option
395                        print_error("coverage", "invalid option -- '%c'", optopt);
396                    } else { // Bad long option
397                        // Do our best.  There is no good solution to finding
398                        // out what the bad option was.
399                        // See, e.g. https://stackoverflow.com/questions/2723888/where-does-getopt-long-store-an-unrecognized-option
400                        if (optind > 0 && strncmp(argv[optind - 1], "--", 2) == 0) {
401                            print_error("coverage", "unrecognised option '%s'",
402                                        argv[optind - 1]);
403                        }
404                    }
405                }
406                return usage();
407        }
408    }
409    if (optind == argc && !opt_file_list)
410        return usage();
411
412    // output file provided by user
413    if (opt_output_file != NULL && strcmp(opt_output_file,"-")!=0) {
414        file_out = fopen( opt_output_file, "w" );
415        if (file_out == NULL) {
416            print_error_errno("coverage", "Cannot open \"%s\" for writing.", opt_output_file);
417            return EXIT_FAILURE;
418        }
419    }
420
421    if (opt_n_bins <= 0 || opt_full_width) {
422        // get number of columns of terminal
423        const char* env_columns = getenv("COLUMNS");
424        int columns = 0;
425        if (env_columns == NULL) {
426#ifdef _WIN32
427            CONSOLE_SCREEN_BUFFER_INFO csbi;
428            if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
429                columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
430            }
431#elif defined TIOCGWINSZ
432            struct winsize w;
433            if (ioctl(2, TIOCGWINSZ, &w) == 0)
434                columns = w.ws_col;
435#endif
436        } else {
437            columns = atoi(env_columns); // atoi(NULL) returns 0
438        }
439
440        if (columns > 60) {
441            opt_n_bins = columns - 40;
442        } else {
443            opt_n_bins = 40;
444        }
445    }
446
447    // setvbuf(file_out, NULL, _IONBF, 0); //turn off buffering
448
449    // Open all BAM files
450    if (opt_file_list) {
451        // Read file names from opt_file_list into argv, and record the number of files in n_bam_files
452        if (read_file_list(opt_file_list, &n_bam_files, &fn)) {
453            print_error_errno("coverage", "Cannot open file list \"%s\".", opt_file_list);
454            return EXIT_FAILURE;
455        }
456        argv = fn;
457        optind = 0;
458    } else {
459        n_bam_files = argc - optind; // the number of BAMs on the command line
460    }
461
462    data = (bam_aux_t **)calloc(n_bam_files, sizeof(bam_aux_t*)); // data[i] for the i-th BAM file
463    if (!data) {
464        print_error_errno("coverage", "Failed to allocate memory");
465        status = EXIT_FAILURE;
466        goto coverage_end;
467    }
468
469    for (i = 0; i < n_bam_files; ++i) {
470        int rf;
471        data[i] = (bam_aux_t *) calloc(1, sizeof(bam_aux_t));
472        if (!data[i]) {
473            print_error_errno("coverage", "Failed to allocate memory");
474            status = EXIT_FAILURE;
475            goto coverage_end;
476        }
477        data[i]->fp = sam_open_format(argv[optind+i], "r", &ga.in); // open BAM
478
479        if (data[i]->fp == NULL) {
480            print_error_errno("coverage", "Could not open \"%s\"", argv[optind+i]);
481            status = EXIT_FAILURE;
482            goto coverage_end;
483        }
484        rf = SAM_FLAG | SAM_RNAME | SAM_POS | SAM_MAPQ | SAM_CIGAR | SAM_SEQ;
485        if (opt_min_baseQ) rf |= SAM_QUAL;
486
487        // Set CRAM options on file handle - returns 0 on success
488        if (hts_set_opt(data[i]->fp, CRAM_OPT_REQUIRED_FIELDS, rf)) {
489            print_error("coverage", "Failed to set CRAM_OPT_REQUIRED_FIELDS value");
490            status = EXIT_FAILURE;
491            goto coverage_end;
492        }
493        if (hts_set_opt(data[i]->fp, CRAM_OPT_DECODE_MD, 0)) {
494            print_error("coverage", "Failed to set CRAM_OPT_DECODE_MD value");
495            status = EXIT_FAILURE;
496            goto coverage_end;
497        }
498        data[i]->min_mapQ = opt_min_mapQ;            // set the mapQ filter
499        data[i]->min_len  = opt_min_len;             // set the qlen filter
500        data[i]->hdr = sam_hdr_read(data[i]->fp);    // read the BAM header
501        data[i]->fail_flags = fail_flags;
502        data[i]->required_flags = required_flags;
503        if (data[i]->hdr == NULL) {
504            print_error_errno("coverage", "Could not read header for \"%s\"", argv[optind+i]);
505            status = EXIT_FAILURE;
506            goto coverage_end;
507        }
508
509        // Lookup region if specified
510        if (opt_reg) { // if a region is specified
511            hts_idx_t *idx = sam_index_load(data[i]->fp, argv[optind+i]);  // load the index
512            if (idx == NULL) {
513                print_error_errno("coverage", "Failed to load index for \"%s\"", argv[optind+i]);
514                status = EXIT_FAILURE;
515                goto coverage_end;
516            }
517            data[i]->iter = sam_itr_querys(idx, data[i]->hdr, opt_reg); // set the iterator
518            hts_idx_destroy(idx); // the index is not needed any more; free the memory
519            if (data[i]->iter == NULL) {
520                print_error("coverage", "Failed to parse region \"%s\". Check the region format or region name presence in the file \"%s\"", opt_reg, argv[optind+i]);
521                status = EXIT_FAILURE;
522                goto coverage_end;
523            }
524        }
525    }
526
527    if (opt_print_tabular && opt_print_header)
528        fputs("#rname\tstartpos\tendpos\tnumreads\tcovbases\tcoverage\tmeandepth\tmeanbaseq\tmeanmapq\n", file_out);
529
530    h = data[0]->hdr; // easy access to the header of the 1st BAM
531    int n_targets = sam_hdr_nref(h);
532    stats = calloc(n_targets, sizeof(stats_aux_t));
533    if (!stats) {
534        print_error_errno("coverage", "Failed to allocate memory");
535        status = EXIT_FAILURE;
536        goto coverage_end;
537    }
538
539    int64_t n_bins = opt_n_bins;
540    if (opt_reg) {
541        stats_aux_t *s = stats + data[0]->iter->tid;
542        s->beg = data[0]->iter->beg; // and to the parsed region coordinates
543        s->end = data[0]->iter->end;
544        if (s->end == HTS_POS_MAX) {
545            s->end = sam_hdr_tid2len(h, data[0]->iter->tid);
546        }
547        if (opt_n_bins > s->end - s->beg) {
548            n_bins = s->end - s->beg;
549        }
550        s->bin_width = (s->end-s->beg) / (n_bins > 0 ? n_bins : 1);
551    }
552
553    for (i=0; i<n_bam_files; i++)
554        data[i]->stats = stats;
555
556    int64_t current_bin = 0;
557
558    // the core multi-pileup loop
559    mplp = bam_mplp_init(n_bam_files, read_bam, (void**)data); // initialization
560    if (max_depth > 0)
561        bam_mplp_set_maxcnt(mplp, max_depth);  // set maximum coverage depth
562    else if (!max_depth)
563        bam_mplp_set_maxcnt(mplp, INT_MAX);
564
565
566    // Extra info for histogram and coverage counting
567    hist = (uint32_t*) calloc(opt_n_bins, sizeof(uint32_t));
568    n_plp = (int*) calloc(n_bam_files, sizeof(int*)); // n_plp[i] is the number of covering reads from the i-th BAM
569    plp = (const bam_pileup1_t**) calloc(n_bam_files, sizeof(bam_pileup1_t*)); // plp[i] points to the array of covering reads (internal in mplp)
570    if (!hist || !n_plp || !plp) {
571        print_error_errno("coverage", "Failed to allocate memory");
572        status = EXIT_FAILURE;
573        goto coverage_end;
574    }
575
576    while ((ret=bam_mplp_auto(mplp, &tid, &pos, n_plp, plp)) > 0) { // come to the next covered position
577
578        if (tid != old_tid) { // Next target sequence
579            if (old_tid >= 0) {
580                if (opt_print_histogram) {
581                    print_hist(file_out, h, stats, old_tid, hist, n_bins, opt_full_utf, opt_plot_coverage);
582                    fputc('\n', file_out);
583                } else if (opt_print_tabular) {
584                    print_tabular_line(file_out, h, stats, old_tid);
585                }
586
587                if (opt_print_histogram)
588                    memset(hist, 0, n_bins*sizeof(uint32_t));
589            }
590
591            stats[tid].covered = true;
592            if (!opt_reg)
593                stats[tid].end = sam_hdr_tid2len(h, tid);
594
595            if (opt_print_histogram) {
596                n_bins = opt_n_bins > stats[tid].end-stats[tid].beg? stats[tid].end-stats[tid].beg : opt_n_bins;
597                stats[tid].bin_width = (stats[tid].end-stats[tid].beg) / n_bins;
598            }
599
600            old_tid = tid;
601        }
602        if (pos < stats[tid].beg || pos >= stats[tid].end) continue; // out of range; skip
603        if (tid >= n_targets) continue;     // diff number of @SQ lines per file?
604
605        if (opt_print_histogram) {
606            current_bin = (pos - stats[tid].beg) / stats[tid].bin_width;
607        }
608
609        bool count_base = false;
610        for (i = 0; i < n_bam_files; ++i) { // base level filters have to go here
611            int depth_at_pos = n_plp[i];
612            for (j = 0; j < n_plp[i]; ++j) {
613                const bam_pileup1_t *p = plp[i] + j; // DON'T modify plp[][] unless you really know
614
615                if (p->is_del || p->is_refskip) {
616                    --depth_at_pos; // having dels or refskips at tid:pos
617                } else if (p->qpos < p->b->core.l_qseq) {
618                    if (bam_get_qual(p->b)[p->qpos] < opt_min_baseQ) {
619                        --depth_at_pos; // low base quality
620                    } else {
621                        stats[tid].summed_baseQ += bam_get_qual(p->b)[p->qpos];
622                        stats[tid].quality_bases++;
623                    }
624                } else {
625                    print_value_warning = 1; // no quality at position
626                }
627            }
628
629            if (depth_at_pos > 0) {
630                count_base = true;
631                stats[tid].summed_coverage += depth_at_pos;
632            }
633
634            if(current_bin < n_bins && opt_plot_coverage) {
635                hist[current_bin] += depth_at_pos;
636            }
637        }
638        if (count_base) {
639            stats[tid].n_covered_bases++;
640            if (opt_print_histogram && current_bin < n_bins && !opt_plot_coverage)
641                ++(hist[current_bin]); // Histogram based on breadth of coverage
642        }
643    }
644
645    if (tid == -1 && opt_reg && *opt_reg != '*')
646        // Region specified but no data covering it.
647        tid = data[0]->iter->tid;
648
649    if (tid < n_targets && tid >=0) {
650        if (opt_print_histogram) {
651            print_hist(file_out, h, stats, tid, hist, n_bins, opt_full_utf, opt_plot_coverage);
652        } else if (opt_print_tabular) {
653            print_tabular_line(file_out, h, stats, tid);
654        }
655    }
656
657
658    if (!opt_reg && opt_print_tabular) {
659        for (i = 0; i < n_targets; ++i) {
660            if (!stats[i].covered) {
661                stats[i].end = sam_hdr_tid2len(h, i);
662                print_tabular_line(file_out, h, stats, i);
663            }
664        }
665    }
666
667    if (print_value_warning) {
668        print_error("coverage", "Warning:  Missing quality values in alignments.  Mean base quality calculated only on available values.");
669    }
670
671    if (ret < 0) status = EXIT_FAILURE;
672
673coverage_end:
674    if (n_plp) free(n_plp);
675    if (plp) free(plp);
676    if (mplp) bam_mplp_destroy(mplp);
677
678    if (hist) free(hist);
679    if (stats) free(stats);
680
681    // Close files and free data structures
682    if (!(file_out == stdout || fclose(file_out) == 0)) {
683        if (status == EXIT_SUCCESS) {
684            print_error_errno("coverage", "error on closing \"%s\"",
685                    (opt_output_file && strcmp(opt_output_file, "-") != 0?
686                     opt_output_file : "stdout"));
687            status = EXIT_FAILURE;
688        }
689    }
690
691    if (data) {
692        for (i = 0; i < n_bam_files && data[i]; ++i) {
693            sam_hdr_destroy(data[i]->hdr);
694            if (data[i]->fp) sam_close(data[i]->fp);
695            hts_itr_destroy(data[i]->iter);
696            free(data[i]);
697        }
698        free(data);
699    }
700
701    if (opt_file_list && fn) {
702        for (i = 0; i < n_bam_files; ++i)
703            free(fn[i]);
704        free(fn);
705    }
706    sam_global_args_free(&ga);
707
708    return status;
709}
710
711#ifdef _MAIN_BAMCOV
712int main(int argc, char *argv[]) {
713    return main_coverage(argc, argv);
714}
715#endif