annotate PsiCLASS-1.0.2/samtools-0.1.19/bgzip.c @ 0:903fc43d6227 draft default tip

Uploaded
author lsong10
date Fri, 26 Mar 2021 16:52:45 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1 /* The MIT License
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3 Copyright (c) 2008 Broad Institute / Massachusetts Institute of Technology
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5 Permission is hereby granted, free of charge, to any person obtaining a copy
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6 of this software and associated documentation files (the "Software"), to deal
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7 in the Software without restriction, including without limitation the rights
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9 copies of the Software, and to permit persons to whom the Software is
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10 furnished to do so, subject to the following conditions:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12 The above copyright notice and this permission notice shall be included in
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13 all copies or substantial portions of the Software.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 THE SOFTWARE.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 #include <stdlib.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25 #include <string.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26 #include <stdio.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27 #include <fcntl.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28 #include <unistd.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 #include <errno.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30 #include <sys/select.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31 #include <sys/stat.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32 #include "bgzf.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34 static const int WINDOW_SIZE = 64 * 1024;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36 static int bgzip_main_usage()
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38 fprintf(stderr, "\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39 fprintf(stderr, "Usage: bgzip [options] [file] ...\n\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40 fprintf(stderr, "Options: -c write on standard output, keep original files unchanged\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41 fprintf(stderr, " -d decompress\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42 fprintf(stderr, " -f overwrite files without asking\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 fprintf(stderr, " -b INT decompress at virtual file pointer INT\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 fprintf(stderr, " -s INT decompress INT bytes in the uncompressed file\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45 fprintf(stderr, " -h give this help\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 fprintf(stderr, "\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50 static int write_open(const char *fn, int is_forced)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52 int fd = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 char c;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54 if (!is_forced) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 if ((fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0666)) < 0 && errno == EEXIST) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56 fprintf(stderr, "[bgzip] %s already exists; do you wish to overwrite (y or n)? ", fn);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57 scanf("%c", &c);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58 if (c != 'Y' && c != 'y') {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59 fprintf(stderr, "[bgzip] not overwritten\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60 exit(1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64 if (fd < 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65 if ((fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66 fprintf(stderr, "[bgzip] %s: Fail to write\n", fn);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67 exit(1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70 return fd;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73 static void fail(BGZF* fp)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75 fprintf(stderr, "Error: %s\n", fp->error);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 exit(1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79 int main(int argc, char **argv)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81 int c, compress, pstdout, is_forced;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82 BGZF *fp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83 void *buffer;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84 long start, end, size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86 compress = 1; pstdout = 0; start = 0; size = -1; end = -1; is_forced = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 while((c = getopt(argc, argv, "cdhfb:s:")) >= 0){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88 switch(c){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89 case 'h': return bgzip_main_usage();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90 case 'd': compress = 0; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91 case 'c': pstdout = 1; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92 case 'b': start = atol(optarg); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93 case 's': size = atol(optarg); break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94 case 'f': is_forced = 1; break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
97 if (size >= 0) end = start + size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
98 if (end >= 0 && end < start) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
99 fprintf(stderr, "[bgzip] Illegal region: [%ld, %ld]\n", start, end);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
100 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
101 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
102 if (compress == 1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
103 struct stat sbuf;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
104 int f_src = fileno(stdin);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
105 int f_dst = fileno(stdout);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
106
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
107 if ( argc>optind )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
108 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
109 if ( stat(argv[optind],&sbuf)<0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
110 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
111 fprintf(stderr, "[bgzip] %s: %s\n", strerror(errno), argv[optind]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
112 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
113 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
114
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
115 if ((f_src = open(argv[optind], O_RDONLY)) < 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
116 fprintf(stderr, "[bgzip] %s: %s\n", strerror(errno), argv[optind]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
117 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
118 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
119
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
120 if (pstdout)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
121 f_dst = fileno(stdout);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
122 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
123 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
124 char *name = malloc(strlen(argv[optind]) + 5);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
125 strcpy(name, argv[optind]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
126 strcat(name, ".gz");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
127 f_dst = write_open(name, is_forced);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
128 if (f_dst < 0) return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
129 free(name);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
130 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
131 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
132 else if (!pstdout && isatty(fileno((FILE *)stdout)) )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
133 return bgzip_main_usage();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
134
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
135 fp = bgzf_fdopen(f_dst, "w");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
136 buffer = malloc(WINDOW_SIZE);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
137 while ((c = read(f_src, buffer, WINDOW_SIZE)) > 0)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
138 if (bgzf_write(fp, buffer, c) < 0) fail(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
139 // f_dst will be closed here
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
140 if (bgzf_close(fp) < 0) fail(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
141 if (argc > optind && !pstdout) unlink(argv[optind]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
142 free(buffer);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
143 close(f_src);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
144 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
145 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
146 struct stat sbuf;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
147 int f_dst;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
148
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
149 if ( argc>optind )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
150 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
151 if ( stat(argv[optind],&sbuf)<0 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
152 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
153 fprintf(stderr, "[bgzip] %s: %s\n", strerror(errno), argv[optind]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
154 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
155 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
156 char *name;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
157 int len = strlen(argv[optind]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
158 if ( strcmp(argv[optind]+len-3,".gz") )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
159 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
160 fprintf(stderr, "[bgzip] %s: unknown suffix -- ignored\n", argv[optind]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
161 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
162 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
163 fp = bgzf_open(argv[optind], "r");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
164 if (fp == NULL) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
165 fprintf(stderr, "[bgzip] Could not open file: %s\n", argv[optind]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
166 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
167 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
168
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
169 if (pstdout) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
170 f_dst = fileno(stdout);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
171 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
172 else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
173 name = strdup(argv[optind]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
174 name[strlen(name) - 3] = '\0';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
175 f_dst = write_open(name, is_forced);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
176 free(name);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
177 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
178 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
179 else if (!pstdout && isatty(fileno((FILE *)stdin)) )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
180 return bgzip_main_usage();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
181 else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
182 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
183 f_dst = fileno(stdout);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
184 fp = bgzf_fdopen(fileno(stdin), "r");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
185 if (fp == NULL) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
186 fprintf(stderr, "[bgzip] Could not read from stdin: %s\n", strerror(errno));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
187 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
188 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
189 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
190 buffer = malloc(WINDOW_SIZE);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
191 if (bgzf_seek(fp, start, SEEK_SET) < 0) fail(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
192 while (1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
193 if (end < 0) c = bgzf_read(fp, buffer, WINDOW_SIZE);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
194 else c = bgzf_read(fp, buffer, (end - start > WINDOW_SIZE)? WINDOW_SIZE:(end - start));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
195 if (c == 0) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
196 if (c < 0) fail(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
197 start += c;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
198 write(f_dst, buffer, c);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
199 if (end >= 0 && start >= end) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
200 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
201 free(buffer);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
202 if (bgzf_close(fp) < 0) fail(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
203 if (!pstdout) unlink(argv[optind]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
204 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
205 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
206 }