knetfile.c
[samtools.git] / bam_cat.c
1 /*\r
2 \r
3 bam_cat -- efficiently concatenates bam files\r
4 \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
11 \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
14 Illumina.\r
15 \r
16 \r
17 ########## License:\r
18 \r
19 The MIT License\r
20 \r
21 Original SAMtools work copyright (c) 2008-2009 Genome Research Ltd.\r
22 Modified SAMtools work copyright (c) 2010 Illumina, Inc.\r
23 \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
30 \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
33 \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
40 THE SOFTWARE.\r
41 \r
42 */\r
43 \r
44 \r
45 /*\r
46 makefile:\r
47 """\r
48 CC=gcc\r
49 CFLAGS+=-g -Wall -O2 -D_FILE_OFFSET_BITS=64 -D_USE_KNETFILE -I$(SAMTOOLS_DIR)\r
50 LDFLAGS+=-L$(SAMTOOLS_DIR)\r
51 LDLIBS+=-lbam -lz\r
52 \r
53 all:bam_cat\r
54 """\r
55 */\r
56 \r
57 \r
58 #include <stdio.h>\r
59 #include <stdlib.h>\r
60 #include <unistd.h>\r
61 \r
62 #include "bgzf.h"\r
63 #include "bam.h"\r
64 \r
65 #define BUF_SIZE 0x10000\r
66 \r
67 #define GZIPID1 31\r
68 #define GZIPID2 139\r
69 \r
70 #define BGZF_EMPTY_BLOCK_SIZE 28\r
71 \r
72 \r
73 int bam_cat(int nfn, char * const *fn, const bam_header_t *h, const char* outbam)\r
74 {\r
75     BGZF *fp;\r
76     FILE* fp_file;\r
77     uint8_t *buf;\r
78     uint8_t ebuf[BGZF_EMPTY_BLOCK_SIZE];\r
79     const int es=BGZF_EMPTY_BLOCK_SIZE;\r
80     int i;\r
81     \r
82     fp = strcmp(outbam, "-")? bgzf_open(outbam, "w") : bgzf_fdopen(fileno(stdout), "w");\r
83     if (fp == 0) {\r
84         fprintf(stderr, "[%s] ERROR: fail to open output file '%s'.\n", __func__, outbam);\r
85         return 1;\r
86     }\r
87     if (h) bam_header_write(fp, h);\r
88     \r
89     if ((buf = (uint8_t*) malloc(BUF_SIZE)) == NULL) {\r
90         fprintf(stderr, "[%s] failed to allocate input buffer.\n", __func__);\r
91         return -1;\r
92     }\r
93     for(i = 0; i < nfn; ++i){\r
94         BGZF *in;\r
95         bam_header_t *old;\r
96         int len,j;\r
97         \r
98         in = strcmp(fn[i], "-")? bam_open(fn[i], "r") : bam_dopen(fileno(stdin), "r");\r
99         if (in == 0) {\r
100             fprintf(stderr, "[%s] ERROR: fail to open file '%s'.\n", __func__, fn[i]);\r
101             return -1;\r
102         }\r
103         if (in->open_mode != 'r') return -1;\r
104         \r
105         old = bam_header_read(in);\r
106                 if (h == 0 && i == 0) bam_header_write(fp, old);\r
107         \r
108         if (in->block_offset < in->block_length) {\r
109             bgzf_write(fp, in->uncompressed_block + in->block_offset, in->block_length - in->block_offset);\r
110             bgzf_flush(fp);\r
111         }\r
112         \r
113         j=0;\r
114 #ifdef _USE_KNETFILE\r
115         fp_file=fp->x.fpw;\r
116         while ((len = knet_read(in->x.fpr, buf, BUF_SIZE)) > 0) {\r
117 #else  \r
118         fp_file=fp->file;\r
119         while (!feof(in->file) && (len = fread(buf, 1, BUF_SIZE, in->file)) > 0) {\r
120 #endif\r
121             if(len<es){\r
122                 int diff=es-len;\r
123                 if(j==0) {\r
124                     fprintf(stderr, "[%s] ERROR: truncated file?: '%s'.\n", __func__, fn[i]);\r
125                     return -1;\r
126                 }\r
127                 if (fwrite(ebuf, 1, len, fp_file) < len) {\r
128                     fprintf(stderr, "[%s] failed to write to output file.\n", __func__);\r
129                     return -1;\r
130                 }\r
131                 memcpy(ebuf,ebuf+len,diff);\r
132                 memcpy(ebuf+diff,buf,len);\r
133             }\r
134             else {\r
135                 if(j!=0) {\r
136                     if (fwrite(ebuf, 1, es, fp_file) < es) {\r
137                         fprintf(stderr, "[%s] failed to write to output file.\n", __func__);\r
138                         return -1;\r
139                     }\r
140                 }\r
141                 len-= es;\r
142                 memcpy(ebuf,buf+len,es);\r
143                 if (fwrite(buf, 1, len, fp_file) < len) {\r
144                     fprintf(stderr, "[%s] failed to write to output file.\n", __func__);\r
145                     return -1;\r
146                 }\r
147             }\r
148             j=1;\r
149         }\r
150 \r
151         /* check final gzip block */\r
152         {\r
153             const uint8_t gzip1=ebuf[0];\r
154             const uint8_t gzip2=ebuf[1];\r
155             const uint32_t isize=*((uint32_t*)(ebuf+es-4));\r
156             if(((gzip1!=GZIPID1) || (gzip2!=GZIPID2)) || (isize!=0)) {\r
157                 fprintf(stderr, "[%s] WARNING: Unexpected block structure in file '%s'.", __func__, fn[i]);\r
158                 fprintf(stderr, " Possible output corruption.\n");\r
159                 if (fwrite(ebuf, 1, es, fp_file) < es) {\r
160                     fprintf(stderr, " Also, failed to write final block.\n");\r
161                 }\r
162             }\r
163         }\r
164         bam_header_destroy(old);\r
165         bgzf_close(in);\r
166     }\r
167     free(buf);\r
168     bgzf_close(fp);\r
169     return 0;\r
170 }\r
171 \r
172 \r
173 \r
174 int main_cat(int argc, char *argv[])\r
175 {\r
176     bam_header_t *h = 0;\r
177         char *outfn = 0;\r
178         int c, ret;\r
179         while ((c = getopt(argc, argv, "h:o:")) >= 0) {\r
180                 switch (c) {\r
181                         case 'h': {\r
182                         tamFile fph = sam_open(optarg);\r
183                         if (fph == 0) {\r
184                         fprintf(stderr, "[%s] ERROR: fail to read the header from '%s'.\n", __func__, argv[1]);\r
185                             return 1;\r
186                         }\r
187                     h = sam_header_read(fph);\r
188                 sam_close(fph);\r
189                                 break;\r
190                         }\r
191                         case 'o': outfn = strdup(optarg); break;\r
192                 }\r
193         }\r
194         if (argc - optind < 2) {\r
195         fprintf(stderr, "Usage: samtools cat [-h header.sam] [-o out.bam] <in1.bam> <in2.bam> [...]\n");\r
196         return 1;\r
197     }\r
198     ret = bam_cat(argc - optind, argv + optind, h, outfn? outfn : "-");\r
199         free(outfn);\r
200         return ret;\r
201 }\r