Source code

Download source archive ↓

upstream-dependencies/samtools/bam_index.c

1/*  bam_index.c -- index and idxstats subcommands.
2
3    Copyright (C) 2008-2011, 2013-2016, 2018, 2019, 2023-2024  Genome Research Ltd.
4    Portions copyright (C) 2010 Broad Institute.
5    Portions copyright (C) 2013 Peter Cock, The James Hutton Institute.
6
7    Author: Heng Li <lh3@sanger.ac.uk>
8
9Permission is hereby granted, free of charge, to any person obtaining a copy
10of this software and associated documentation files (the "Software"), to deal
11in the Software without restriction, including without limitation the rights
12to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13copies of the Software, and to permit persons to whom the Software is
14furnished to do so, subject to the following conditions:
15
16The above copyright notices and this permission notice shall be included in
17all copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.  */
26
27#include <config.h>
28
29#include <htslib/hts.h>
30#include <htslib/sam.h>
31#include <htslib/hfile.h>
32#include <htslib/khash.h>
33#include <stdlib.h>
34#include <stdio.h>
35#include <inttypes.h>
36#include <unistd.h>
37#include <getopt.h>
38
39#include "samtools.h"
40#include "sam_opts.h"
41
42#define BAM_LIDX_SHIFT    14
43
44static void index_usage(FILE *fp)
45{
46    fprintf(fp,
47"Usage: samtools index -M [-bc] [-m INT] <in1.bam> <in2.bam>...\n"
48"   or: samtools index [-bc] [-m INT] <in.bam> [out.index]\n"
49"Options:\n"
50"  -b, --bai            Generate BAI-format index for BAM files [default]\n"
51"  -c, --csi            Generate CSI-format index for BAM files\n"
52"  -m, --min-shift INT  Set minimum interval size for CSI indices to 2^INT [%d]\n"
53"  -M                   Interpret all filename arguments as files to be indexed\n"
54"  -o, --output FILE    Write index to FILE [alternative to <out.index> in args]\n"
55"  -@, --threads INT    Sets the number of threads [none]\n", BAM_LIDX_SHIFT);
56}
57
58// Returns 1 if the file does not exist or can be positively
59// identified as an index file.
60static int nonexistent_or_index(const char *fn)
61{
62    int ret1, ret2;
63    htsFormat fmt;
64    hFILE *fp = hopen(fn, "r");
65    if (fp == NULL) return 1;
66
67    ret1 = hts_detect_format2(fp, fn, &fmt);
68    ret2 = hclose(fp);
69    if (ret1 < 0 || ret2 < 0) return 0;
70
71    return fmt.category == index_file;
72}
73
74int bam_index(int argc, char *argv[])
75{
76    int csi = 0;
77    int min_shift = BAM_LIDX_SHIFT;
78    int multiple = 0;
79    int n_threads = 0;
80    int n_files, c, i, ret;
81    const char *fn_idx = NULL;
82
83    static const struct option lopts[] = {
84        SAM_OPT_GLOBAL_OPTIONS('-', '-', '-', '-', '-', '@'),
85        {"output",    required_argument, NULL, 'o'},
86        {"bai",       no_argument,       NULL, 'b'},
87        {"csi",       no_argument,       NULL, 'c'},
88        {"min-shift", required_argument, NULL, 'm'},
89        { NULL, 0, NULL, 0 }
90    };
91
92    while ((c = getopt_long(argc, argv, "bcm:Mo:@:", lopts, NULL)) >= 0)
93        switch (c) {
94        case 'b': csi = 0; break;
95        case 'c': csi = 1; break;
96        case 'm': csi = 1; min_shift = atoi(optarg); break;
97        case 'M': multiple = 1; break;
98        case 'o': fn_idx = optarg; break;
99        case '@': n_threads = atoi(optarg); break;
100        default:
101            index_usage(stderr);
102            return 1;
103        }
104
105    n_files = argc - optind;
106
107    if (n_files == 0) {
108        index_usage(stdout);
109        return 0;
110    }
111
112    // Handle legacy synopsis
113    if (n_files == 2 && !fn_idx && nonexistent_or_index(argv[optind+1])) {
114        n_files = 1;
115        fn_idx = argv[optind+1];
116    }
117
118    if (n_files > 1 && !multiple) {
119        print_error("index", "use -M to enable indexing more than one alignment file");
120        return EXIT_FAILURE;
121    }
122
123    if (fn_idx && n_files > 1) {
124        // TODO In future we may allow %* placeholders or similar
125        print_error("index", "can't use -o with multiple input alignment files");
126        return EXIT_FAILURE;
127    }
128
129    for (i = optind; i < optind + n_files; i++) {
130        ret = sam_index_build3(argv[i], fn_idx, csi? min_shift : 0, n_threads);
131        if (ret < 0) {
132            if (ret == -2)
133                print_error_errno("index", "failed to open \"%s\"", argv[i]);
134            else if (ret == -3)
135                print_error("index", "\"%s\" is in a format that cannot be usefully indexed", argv[i]);
136            else if (ret == -4 && fn_idx)
137                print_error("index", "failed to create or write index \"%s\"", fn_idx);
138            else if (ret == -4)
139                print_error("index", "failed to create or write index");
140            else
141                print_error_errno("index", "failed to create index for \"%s\"", argv[i]);
142            return EXIT_FAILURE;
143        }
144    }
145
146    return EXIT_SUCCESS;
147}
148
149/*
150 * Cram indices do not contain mapped/unmapped record counts, so we have to
151 * decode each record and count.  However we can speed this up as much as
152 * possible by using the required fields parameter.
153 *
154 * This prints the stats to stdout in the same manner than the BAM function
155 * does.
156 *
157 * Returns 0 on success,
158 *        -1 on failure.
159 */
160int slow_idxstats(samFile *fp, sam_hdr_t *header) {
161    int ret, last_tid = -2;
162    bam1_t *b = bam_init1();
163
164    if (hts_set_opt(fp, CRAM_OPT_REQUIRED_FIELDS, SAM_RNAME | SAM_FLAG))
165        return -1;
166
167    uint64_t (*count0)[2] = calloc(sam_hdr_nref(header)+1, sizeof(*count0));
168    uint64_t (*counts)[2] = count0+1;
169    if (!count0)
170        return -1;
171
172    while ((ret = sam_read1(fp, header, b)) >= 0) {
173        if (b->core.tid >= sam_hdr_nref(header) || b->core.tid < -1) {
174            free(count0);
175            return -1;
176        }
177
178        if (b->core.tid != last_tid) {
179            if (last_tid >= -1) {
180                if (counts[b->core.tid][0] + counts[b->core.tid][1]) {
181                    print_error("idxstats", "file is not position sorted");
182                    free(count0);
183                    return -1;
184                }
185            }
186            last_tid = b->core.tid;
187        }
188
189        counts[b->core.tid][(b->core.flag & BAM_FUNMAP) ? 1 : 0]++;
190    }
191
192    if (ret == -1) {
193        int i;
194        for (i = 0; i < sam_hdr_nref(header); i++) {
195            printf("%s\t%"PRId64"\t%"PRIu64"\t%"PRIu64"\n",
196                   sam_hdr_tid2name(header, i),
197                   (int64_t) sam_hdr_tid2len(header, i),
198                   counts[i][0], counts[i][1]);
199        }
200        printf("*\t0\t%"PRIu64"\t%"PRIu64"\n", counts[-1][0], counts[-1][1]);
201    }
202
203    free(count0);
204
205    bam_destroy1(b);
206
207    return (ret == -1) ? 0 : -1;
208}
209
210static void usage_exit(FILE *fp, int exit_status)
211{
212    fprintf(fp, "Usage: samtools idxstats [options] <in.bam>\n"
213                "  -X           Include customized index file\n");
214    sam_global_opt_help(fp, "-.---@-.");
215    exit(exit_status);
216}
217
218int bam_idxstats(int argc, char *argv[])
219{
220    hts_idx_t* idx;
221    sam_hdr_t* header;
222    samFile* fp;
223    int c, has_index_file = 0, file_names = 1;
224    char *index_name = NULL;
225
226    sam_global_args ga = SAM_GLOBAL_ARGS_INIT;
227    static const struct option lopts[] = {
228        SAM_OPT_GLOBAL_OPTIONS('-', 0, '-', '-', '-', '@'),
229        {NULL, 0, NULL, 0}
230    };
231
232    while ((c = getopt_long(argc, argv, "@:X", lopts, NULL)) >= 0) {
233        switch (c) {
234            case 'X': has_index_file=1; break;
235            default:  if (parse_sam_global_opt(c, optarg, lopts, &ga) == 0) break;
236                /* else fall-through */
237            case '?':
238                usage_exit(stderr, EXIT_FAILURE);
239        }
240    }
241
242    if (has_index_file) {
243        file_names = 2;
244        index_name = argv[optind + 1];
245    }
246
247    if (argc != optind + file_names) {
248        if (argc == optind) usage_exit(stdout, EXIT_SUCCESS);
249        else usage_exit(stderr, EXIT_FAILURE);
250    }
251
252    fp = sam_open_format(argv[optind], "r", &ga.in);
253    if (fp == NULL) {
254        print_error_errno("idxstats", "failed to open \"%s\"", argv[optind]);
255        return 1;
256    }
257    header = sam_hdr_read(fp);
258    if (header == NULL) {
259        print_error("idxstats", "failed to read header for \"%s\"", argv[optind]);
260        return 1;
261    }
262
263    if (hts_get_format(fp)->format != bam) {
264    slow_method:
265        if (ga.nthreads)
266            hts_set_threads(fp, ga.nthreads);
267
268        if (slow_idxstats(fp, header) < 0) {
269            print_error("idxstats", "failed to process \"%s\"", argv[optind]);
270            return 1;
271        }
272    } else {
273        idx = sam_index_load2(fp, argv[optind], index_name);
274        if (idx == NULL) {
275            print_error("idxstats", "fail to load index for \"%s\", "
276                        "reverting to slow method", argv[optind]);
277            goto slow_method;
278        }
279
280        int i;
281        for (i = 0; i < sam_hdr_nref(header); ++i) {
282            // Print out contig name and length
283            printf("%s\t%"PRId64, sam_hdr_tid2name(header, i), (int64_t) sam_hdr_tid2len(header, i));
284            // Now fetch info about it from the meta bin
285            uint64_t u, v;
286            hts_idx_get_stat(idx, i, &u, &v);
287            printf("\t%" PRIu64 "\t%" PRIu64 "\n", u, v);
288        }
289        // Dump information about unmapped reads
290        printf("*\t0\t0\t%" PRIu64 "\n", hts_idx_get_n_no_coor(idx));
291        hts_idx_destroy(idx);
292    }
293
294    sam_hdr_destroy(header);
295    sam_close(fp);
296    return 0;
297}