3 bam_cat -- efficiently concatenates bam files
\r
5 bam_cat can be used to concatenate BAM files. Under special
\r
6 circumstances, it can be used as an alternative to 'samtools merge' to
\r
7 concatenate multiple sorted files into a single sorted file. For this
\r
8 to work each file must be sorted, and the sorted files must be given
\r
9 as command line arguments in order such that the final read in file i
\r
10 is less than or equal to the first read in file i+1.
\r
12 This code is derived from the bam_reheader function in samtools 0.1.8
\r
13 and modified to perform concatenation by Chris Saunders on behalf of
\r
21 Original SAMtools work copyright (c) 2008-2009 Genome Research Ltd.
\r
22 Modified SAMtools work copyright (c) 2010 Illumina, Inc.
\r
24 Permission is hereby granted, free of charge, to any person obtaining a copy
\r
25 of this software and associated documentation files (the "Software"), to deal
\r
26 in the Software without restriction, including without limitation the rights
\r
27 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
28 copies of the Software, and to permit persons to whom the Software is
\r
29 furnished to do so, subject to the following conditions:
\r
31 The above copyright notice and this permission notice shall be included in
\r
32 all copies or substantial portions of the Software.
\r
34 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
35 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
36 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
37 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
38 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
39 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
49 CFLAGS+=-g -Wall -O2 -D_FILE_OFFSET_BITS=64 -D_USE_KNETFILE -I$(SAMTOOLS_DIR)
\r
50 LDFLAGS+=-L$(SAMTOOLS_DIR)
\r
62 #include "knetfile.h"
\r
66 #define BUF_SIZE 0x10000
\r
71 #define BGZF_EMPTY_BLOCK_SIZE 28
\r
74 int bam_cat(int nfn, char * const *fn, const bam_header_t *h, const char* outbam)
\r
79 uint8_t ebuf[BGZF_EMPTY_BLOCK_SIZE];
\r
80 const int es=BGZF_EMPTY_BLOCK_SIZE;
\r
83 fp = strcmp(outbam, "-")? bgzf_open(outbam, "w") : bgzf_fdopen(fileno(stdout), "w");
\r
85 fprintf(stderr, "[%s] ERROR: fail to open output file '%s'.\n", __func__, outbam);
\r
88 if (h) bam_header_write(fp, h);
\r
90 buf = (uint8_t*) malloc(BUF_SIZE);
\r
91 for(i = 0; i < nfn; ++i){
\r
96 in = strcmp(fn[i], "-")? bam_open(fn[i], "r") : bam_dopen(fileno(stdin), "r");
\r
98 fprintf(stderr, "[%s] ERROR: fail to open file '%s'.\n", __func__, fn[i]);
\r
101 if (in->is_write) return -1;
\r
103 old = bam_header_read(in);
\r
104 if (h == 0 && i == 0) bam_header_write(fp, old);
\r
106 if (in->block_offset < in->block_length) {
\r
107 bgzf_write(fp, in->uncompressed_block + in->block_offset, in->block_length - in->block_offset);
\r
112 #ifdef _USE_KNETFILE
\r
114 while ((len = knet_read(in->fp, buf, BUF_SIZE)) > 0) {
\r
117 while (!feof(in->file) && (len = fread(buf, 1, BUF_SIZE, in->file)) > 0) {
\r
122 fprintf(stderr, "[%s] ERROR: truncated file?: '%s'.\n", __func__, fn[i]);
\r
125 fwrite(ebuf, 1, len, fp_file);
\r
126 memcpy(ebuf,ebuf+len,diff);
\r
127 memcpy(ebuf+diff,buf,len);
\r
129 if(j!=0) fwrite(ebuf, 1, es, fp_file);
\r
131 memcpy(ebuf,buf+len,es);
\r
132 fwrite(buf, 1, len, fp_file);
\r
137 /* check final gzip block */
\r
139 const uint8_t gzip1=ebuf[0];
\r
140 const uint8_t gzip2=ebuf[1];
\r
141 const uint32_t isize=*((uint32_t*)(ebuf+es-4));
\r
142 if(((gzip1!=GZIPID1) || (gzip2!=GZIPID2)) || (isize!=0)) {
\r
143 fprintf(stderr, "[%s] WARNING: Unexpected block structure in file '%s'.", __func__, fn[i]);
\r
144 fprintf(stderr, " Possible output corruption.\n");
\r
145 fwrite(ebuf, 1, es, fp_file);
\r
148 bam_header_destroy(old);
\r
158 int main_cat(int argc, char *argv[])
\r
160 bam_header_t *h = 0;
\r
163 while ((c = getopt(argc, argv, "h:o:")) >= 0) {
\r
166 tamFile fph = sam_open(optarg);
\r
168 fprintf(stderr, "[%s] ERROR: fail to read the header from '%s'.\n", __func__, argv[1]);
\r
171 h = sam_header_read(fph);
\r
175 case 'o': outfn = strdup(optarg); break;
\r
178 if (argc - optind < 2) {
\r
179 fprintf(stderr, "Usage: samtools cat [-h header.sam] [-o out.bam] <in1.bam> <in2.bam> [...]\n");
\r
182 ret = bam_cat(argc - optind, argv + optind, h, outfn? outfn : "-");
\r