Update debian changelog
[pysam.git] / samtools / bam_cat.c.pysam.c
1 #include "pysam.h"
2
3 /*\r
4 \r
5 bam_cat -- efficiently concatenates bam files\r
6 \r
7 bam_cat can be used to concatenate BAM files. Under special\r
8 circumstances, it can be used as an alternative to 'samtools merge' to\r
9 concatenate multiple sorted files into a single sorted file. For this\r
10 to work each file must be sorted, and the sorted files must be given\r
11 as command line arguments in order such that the final read in file i\r
12 is less than or equal to the first read in file i+1.\r
13 \r
14 This code is derived from the bam_reheader function in samtools 0.1.8\r
15 and modified to perform concatenation by Chris Saunders on behalf of\r
16 Illumina.\r
17 \r
18 \r
19 ########## License:\r
20 \r
21 The MIT License\r
22 \r
23 Original SAMtools work copyright (c) 2008-2009 Genome Research Ltd.\r
24 Modified SAMtools work copyright (c) 2010 Illumina, Inc.\r
25 \r
26 Permission is hereby granted, free of charge, to any person obtaining a copy\r
27 of this software and associated documentation files (the "Software"), to deal\r
28 in the Software without restriction, including without limitation the rights\r
29 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
30 copies of the Software, and to permit persons to whom the Software is\r
31 furnished to do so, subject to the following conditions:\r
32 \r
33 The above copyright notice and this permission notice shall be included in\r
34 all copies or substantial portions of the Software.\r
35 \r
36 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
37 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
38 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
39 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
40 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
41 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
42 THE SOFTWARE.\r
43 \r
44 */\r
45 \r
46 \r
47 /*\r
48 makefile:\r
49 """\r
50 CC=gcc\r
51 CFLAGS+=-g -Wall -O2 -D_FILE_OFFSET_BITS=64 -D_USE_KNETFILE -I$(SAMTOOLS_DIR)\r
52 LDFLAGS+=-L$(SAMTOOLS_DIR)\r
53 LDLIBS+=-lbam -lz\r
54 \r
55 all:bam_cat\r
56 """\r
57 */\r
58 \r
59 \r
60 #include <stdio.h>\r
61 #include <stdlib.h>\r
62 #include <unistd.h>\r
63 \r
64 #include "bgzf.h"\r
65 #include "bam.h"\r
66 \r
67 #define BUF_SIZE 0x10000\r
68 \r
69 #define GZIPID1 31\r
70 #define GZIPID2 139\r
71 \r
72 #define BGZF_EMPTY_BLOCK_SIZE 28\r
73 \r
74 \r
75 int bam_cat(int nfn, char * const *fn, const bam_header_t *h, const char* outbam)\r
76 {\r
77     BGZF *fp;\r
78     FILE* fp_file;\r
79     uint8_t *buf;\r
80     uint8_t ebuf[BGZF_EMPTY_BLOCK_SIZE];\r
81     const int es=BGZF_EMPTY_BLOCK_SIZE;\r
82     int i;\r
83     \r
84     fp = strcmp(outbam, "-")? bgzf_open(outbam, "w") : bgzf_fdopen(fileno(stdout), "w");\r
85     if (fp == 0) {\r
86         fprintf(pysamerr, "[%s] ERROR: fail to open output file '%s'.\n", __func__, outbam);\r
87         return 1;\r
88     }\r
89     if (h) bam_header_write(fp, h);\r
90     \r
91     buf = (uint8_t*) malloc(BUF_SIZE);\r
92     for(i = 0; i < nfn; ++i){\r
93         BGZF *in;\r
94         bam_header_t *old;\r
95         int len,j;\r
96         \r
97         in = strcmp(fn[i], "-")? bam_open(fn[i], "r") : bam_dopen(fileno(stdin), "r");\r
98         if (in == 0) {\r
99             fprintf(pysamerr, "[%s] ERROR: fail to open file '%s'.\n", __func__, fn[i]);\r
100             return -1;\r
101         }\r
102         if (in->open_mode != 'r') return -1;\r
103         \r
104         old = bam_header_read(in);\r
105                 if (h == 0 && i == 0) bam_header_write(fp, old);\r
106         \r
107         if (in->block_offset < in->block_length) {\r
108             bgzf_write(fp, in->uncompressed_block + in->block_offset, in->block_length - in->block_offset);\r
109             bgzf_flush(fp);\r
110         }\r
111         \r
112         j=0;\r
113 #ifdef _USE_KNETFILE\r
114         fp_file=fp->x.fpw;\r
115         while ((len = knet_read(in->x.fpr, buf, BUF_SIZE)) > 0) {\r
116 #else  \r
117         fp_file=fp->file;\r
118         while (!feof(in->file) && (len = fread(buf, 1, BUF_SIZE, in->file)) > 0) {\r
119 #endif\r
120             if(len<es){\r
121                 int diff=es-len;\r
122                 if(j==0) {\r
123                     fprintf(pysamerr, "[%s] ERROR: truncated file?: '%s'.\n", __func__, fn[i]);\r
124                     return -1;\r
125                 }\r
126                 fwrite(ebuf, 1, len, fp_file);\r
127                 memcpy(ebuf,ebuf+len,diff);\r
128                 memcpy(ebuf+diff,buf,len);\r
129             } else {\r
130                 if(j!=0) fwrite(ebuf, 1, es, fp_file);\r
131                 len-= es;\r
132                 memcpy(ebuf,buf+len,es);\r
133                 fwrite(buf, 1, len, fp_file);\r
134             }\r
135             j=1;\r
136         }\r
137 \r
138         /* check final gzip block */\r
139         {\r
140             const uint8_t gzip1=ebuf[0];\r
141             const uint8_t gzip2=ebuf[1];\r
142             const uint32_t isize=*((uint32_t*)(ebuf+es-4));\r
143             if(((gzip1!=GZIPID1) || (gzip2!=GZIPID2)) || (isize!=0)) {\r
144                 fprintf(pysamerr, "[%s] WARNING: Unexpected block structure in file '%s'.", __func__, fn[i]);\r
145                 fprintf(pysamerr, " Possible output corruption.\n");\r
146                 fwrite(ebuf, 1, es, fp_file);\r
147             }\r
148         }\r
149         bam_header_destroy(old);\r
150         bgzf_close(in);\r
151     }\r
152     free(buf);\r
153     bgzf_close(fp);\r
154     return 0;\r
155 }\r
156 \r
157 \r
158 \r
159 int main_cat(int argc, char *argv[])\r
160 {\r
161     bam_header_t *h = 0;\r
162         char *outfn = 0;\r
163         int c, ret;\r
164         while ((c = getopt(argc, argv, "h:o:")) >= 0) {\r
165                 switch (c) {\r
166                         case 'h': {\r
167                         tamFile fph = sam_open(optarg);\r
168                         if (fph == 0) {\r
169                         fprintf(pysamerr, "[%s] ERROR: fail to read the header from '%s'.\n", __func__, argv[1]);\r
170                             return 1;\r
171                         }\r
172                     h = sam_header_read(fph);\r
173                 sam_close(fph);\r
174                                 break;\r
175                         }\r
176                         case 'o': outfn = strdup(optarg); break;\r
177                 }\r
178         }\r
179         if (argc - optind < 2) {\r
180         fprintf(pysamerr, "Usage: samtools cat [-h header.sam] [-o out.bam] <in1.bam> <in2.bam> [...]\n");\r
181         return 1;\r
182     }\r
183     ret = bam_cat(argc - optind, argv + optind, h, outfn? outfn : "-");\r
184         free(outfn);\r
185         return ret;\r
186 }\r