annotate ezBAMQC/src/htslib/bgzip.c @ 9:6610eedd9fae

Uploaded
author cshl-bsr
date Wed, 30 Mar 2016 12:11:46 -0400
parents dfa3745e5fd8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
1 /* bgzip.c -- Block compression/decompression utility.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
2
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
3 Copyright (C) 2008, 2009 Broad Institute / Massachusetts Institute of Technology
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
4 Copyright (C) 2010, 2013, 2014 Genome Research Ltd.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
5
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
6 Permission is hereby granted, free of charge, to any person obtaining a copy
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
7 of this software and associated documentation files (the "Software"), to deal
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
8 in the Software without restriction, including without limitation the rights
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
10 copies of the Software, and to permit persons to whom the Software is
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
11 furnished to do so, subject to the following conditions:
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
12
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
13 The above copyright notices and this permission notice shall be included in
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
14 all copies or substantial portions of the Software.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
15
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
22 THE SOFTWARE.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
23 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
24
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
25 #include <stdlib.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
26 #include <string.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
27 #include <stdio.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
28 #include <fcntl.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
29 #include <unistd.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
30 #include <errno.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
31 #include <stdarg.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
32 #include <getopt.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
33 #include <sys/select.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
34 #include <sys/stat.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
35 #include "htslib/bgzf.h"
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
36 #include "htslib/hts.h"
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
37
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
38 static const int WINDOW_SIZE = 64 * 1024;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
39
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
40 static void error(const char *format, ...)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
41 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
42 va_list ap;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
43 va_start(ap, format);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
44 vfprintf(stderr, format, ap);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
45 va_end(ap);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
46 exit(EXIT_FAILURE);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
47 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
48
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
49 static int write_open(const char *fn, int is_forced)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
50 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
51 int fd = -1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
52 char c;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
53 if (!is_forced) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
54 if ((fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0666)) < 0 && errno == EEXIST) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
55 fprintf(stderr, "[bgzip] %s already exists; do you wish to overwrite (y or n)? ", fn);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
56 if ( scanf("%c", &c) != 1 ) c = 'n';
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
57 if (c != 'Y' && c != 'y') {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
58 fprintf(stderr, "[bgzip] not overwritten\n");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
59 exit(EXIT_FAILURE);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
60 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
61 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
62 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
63 if (fd < 0) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
64 if ((fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
65 fprintf(stderr, "[bgzip] %s: Fail to write\n", fn);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
66 exit(EXIT_FAILURE);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
67 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
68 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
69 return fd;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
70 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
71
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
72 static int bgzip_main_usage(void)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
73 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
74 fprintf(stderr, "\n");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
75 fprintf(stderr, "Version: %s\n", hts_version());
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
76 fprintf(stderr, "Usage: bgzip [OPTIONS] [FILE] ...\n");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
77 fprintf(stderr, "Options:\n");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
78 fprintf(stderr, " -b, --offset INT decompress at virtual file pointer (0-based uncompressed offset)\n");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
79 fprintf(stderr, " -c, --stdout write on standard output, keep original files unchanged\n");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
80 fprintf(stderr, " -d, --decompress decompress\n");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
81 fprintf(stderr, " -f, --force overwrite files without asking\n");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
82 fprintf(stderr, " -h, --help give this help\n");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
83 fprintf(stderr, " -i, --index compress and create BGZF index\n");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
84 fprintf(stderr, " -I, --index-name FILE name of BGZF index file [file.gz.gzi]\n");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
85 fprintf(stderr, " -r, --reindex (re)index compressed file\n");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
86 fprintf(stderr, " -s, --size INT decompress INT bytes (uncompressed size)\n");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
87 fprintf(stderr, "\n");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
88 return 1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
89 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
90
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
91 int main(int argc, char **argv)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
92 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
93 int c, compress, pstdout, is_forced, index = 0, reindex = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
94 BGZF *fp;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
95 void *buffer;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
96 long start, end, size;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
97 char *index_fname = NULL;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
98
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
99 static struct option loptions[] =
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
100 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
101 {"help",0,0,'h'},
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
102 {"offset",1,0,'b'},
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
103 {"stdout",0,0,'c'},
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
104 {"decompress",0,0,'d'},
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
105 {"force",0,0,'f'},
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
106 {"index",0,0,'i'},
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
107 {"index-name",1,0,'I'},
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
108 {"reindex",0,0,'r'},
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
109 {"size",1,0,'s'},
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
110 {0,0,0,0}
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
111 };
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
112
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
113 compress = 1; pstdout = 0; start = 0; size = -1; end = -1; is_forced = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
114 while((c = getopt_long(argc, argv, "cdh?fb:s:iI:r",loptions,NULL)) >= 0){
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
115 switch(c){
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
116 case 'd': compress = 0; break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
117 case 'c': pstdout = 1; break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
118 case 'b': start = atol(optarg); compress = 0; pstdout = 1; break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
119 case 's': size = atol(optarg); pstdout = 1; break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
120 case 'f': is_forced = 1; break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
121 case 'i': index = 1; break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
122 case 'I': index_fname = optarg; break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
123 case 'r': reindex = 1; compress = 0; break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
124 case 'h':
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
125 case '?': return bgzip_main_usage();
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
126 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
127 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
128 if (size >= 0) end = start + size;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
129 if (end >= 0 && end < start) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
130 fprintf(stderr, "[bgzip] Illegal region: [%ld, %ld]\n", start, end);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
131 return 1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
132 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
133 if (compress == 1) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
134 struct stat sbuf;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
135 int f_src = fileno(stdin);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
136 int f_dst = fileno(stdout);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
137
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
138 if ( argc>optind )
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
139 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
140 if ( stat(argv[optind],&sbuf)<0 )
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
141 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
142 fprintf(stderr, "[bgzip] %s: %s\n", strerror(errno), argv[optind]);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
143 return 1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
144 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
145
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
146 if ((f_src = open(argv[optind], O_RDONLY)) < 0) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
147 fprintf(stderr, "[bgzip] %s: %s\n", strerror(errno), argv[optind]);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
148 return 1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
149 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
150
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
151 if (pstdout)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
152 f_dst = fileno(stdout);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
153 else
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
154 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
155 char *name = malloc(strlen(argv[optind]) + 5);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
156 strcpy(name, argv[optind]);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
157 strcat(name, ".gz");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
158 f_dst = write_open(name, is_forced);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
159 free(name);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
160 if (f_dst < 0) return 1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
161 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
162 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
163 else if (!pstdout && isatty(fileno((FILE *)stdout)) )
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
164 return bgzip_main_usage();
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
165 else if ( index && !index_fname )
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
166 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
167 fprintf(stderr, "[bgzip] Index file name expected when writing to stdout\n");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
168 return 1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
169 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
170
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
171 fp = bgzf_fdopen(f_dst, "w");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
172 if ( index ) bgzf_index_build_init(fp);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
173 buffer = malloc(WINDOW_SIZE);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
174 while ((c = read(f_src, buffer, WINDOW_SIZE)) > 0)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
175 if (bgzf_write(fp, buffer, c) < 0) error("Could not write %d bytes: Error %d\n", c, fp->errcode);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
176 // f_dst will be closed here
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
177 if ( index )
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
178 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
179 if ( index_fname ) bgzf_index_dump(fp, index_fname, NULL);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
180 else bgzf_index_dump(fp, argv[optind], ".gz.gzi");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
181 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
182 if (bgzf_close(fp) < 0) error("Close failed: Error %d", fp->errcode);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
183 if (argc > optind && !pstdout) unlink(argv[optind]);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
184 free(buffer);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
185 close(f_src);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
186 return 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
187 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
188 else if ( reindex )
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
189 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
190 if ( argc>optind )
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
191 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
192 fp = bgzf_open(argv[optind], "r");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
193 if ( !fp ) error("[bgzip] Could not open file: %s\n", argv[optind]);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
194 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
195 else
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
196 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
197 if ( !index_fname ) error("[bgzip] Index file name expected when reading from stdin\n");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
198 fp = bgzf_fdopen(fileno(stdin), "r");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
199 if ( !fp ) error("[bgzip] Could not read from stdin: %s\n", strerror(errno));
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
200 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
201
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
202 buffer = malloc(BGZF_BLOCK_SIZE);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
203 bgzf_index_build_init(fp);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
204 int ret;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
205 while ( (ret=bgzf_read(fp, buffer, BGZF_BLOCK_SIZE))>0 ) ;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
206 free(buffer);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
207 if ( ret<0 ) error("Is the file gzipped or bgzipped? The latter is required for indexing.\n");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
208
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
209 if ( index_fname )
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
210 bgzf_index_dump(fp, index_fname, NULL);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
211 else
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
212 bgzf_index_dump(fp, argv[optind], ".gzi");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
213
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
214 if ( bgzf_close(fp)<0 ) error("Close failed: Error %d\n",fp->errcode);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
215 return 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
216 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
217 else
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
218 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
219 struct stat sbuf;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
220 int f_dst;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
221
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
222 if ( argc>optind )
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
223 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
224 if ( stat(argv[optind],&sbuf)<0 )
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
225 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
226 fprintf(stderr, "[bgzip] %s: %s\n", strerror(errno), argv[optind]);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
227 return 1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
228 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
229 char *name;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
230 int len = strlen(argv[optind]);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
231 if ( strcmp(argv[optind]+len-3,".gz") )
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
232 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
233 fprintf(stderr, "[bgzip] %s: unknown suffix -- ignored\n", argv[optind]);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
234 return 1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
235 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
236 fp = bgzf_open(argv[optind], "r");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
237 if (fp == NULL) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
238 fprintf(stderr, "[bgzip] Could not open file: %s\n", argv[optind]);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
239 return 1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
240 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
241
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
242 if (pstdout) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
243 f_dst = fileno(stdout);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
244 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
245 else {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
246 name = strdup(argv[optind]);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
247 name[strlen(name) - 3] = '\0';
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
248 f_dst = write_open(name, is_forced);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
249 free(name);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
250 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
251 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
252 else if (!pstdout && isatty(fileno((FILE *)stdin)) )
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
253 return bgzip_main_usage();
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
254 else
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
255 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
256 f_dst = fileno(stdout);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
257 fp = bgzf_fdopen(fileno(stdin), "r");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
258 if (fp == NULL) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
259 fprintf(stderr, "[bgzip] Could not read from stdin: %s\n", strerror(errno));
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
260 return 1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
261 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
262 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
263 buffer = malloc(WINDOW_SIZE);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
264 if ( start>0 )
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
265 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
266 if ( bgzf_index_load(fp, argv[optind], ".gzi") < 0 ) error("Could not load index: %s.gzi\n", argv[optind]);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
267 if ( bgzf_useek(fp, start, SEEK_SET) < 0 ) error("Could not seek to %d-th (uncompressd) byte\n", start);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
268 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
269 while (1) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
270 if (end < 0) c = bgzf_read(fp, buffer, WINDOW_SIZE);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
271 else c = bgzf_read(fp, buffer, (end - start > WINDOW_SIZE)? WINDOW_SIZE:(end - start));
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
272 if (c == 0) break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
273 if (c < 0) error("Could not read %d bytes: Error %d\n", (end - start > WINDOW_SIZE)? WINDOW_SIZE:(end - start), fp->errcode);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
274 start += c;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
275 if ( write(f_dst, buffer, c) != c ) error("Could not write %d bytes\n", c);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
276 if (end >= 0 && start >= end) break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
277 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
278 free(buffer);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
279 if (bgzf_close(fp) < 0) error("Close failed: Error %d\n",fp->errcode);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
280 if (!pstdout) unlink(argv[optind]);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
281 return 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
282 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
283 return 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
284 }