Debian Codegraph @

bgzf.h

raw symbols SCIP
1 /* The MIT License
2
3 Copyright (c) 2008 Broad Institute / Massachusetts Institute of Technology
4 2011, 2012 Attractive Chaos <[email protected]>
5 Copyright (C) 2009, 2013, 2014 Genome Research Ltd
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE.
24 */
25
26 /* The BGZF library was originally written by Bob Handsaker from the Broad
27 * Institute. It was later improved by the SAMtools developers. */
28
29 #ifndef HTSLIB_BGZF_H
30 #define HTSLIB_BGZF_H
31
32 #include "plink_common.h"
33 #include <sys/types.h>
34
35 #define BGZF_BLOCK_SIZE 0xff00 // make sure compressBound(BGZF_BLOCK_SIZE) < BGZF_MAX_BLOCK_SIZE
36 #define BGZF_MAX_BLOCK_SIZE 0x10000
37
38 #define BGZF_ERR_ZLIB 1
39 #define BGZF_ERR_HEADER 2
40 #define BGZF_ERR_IO 4
41 #define BGZF_ERR_MISUSE 8
42
43 struct hFILE;
44 struct bgzf_mtaux_t;
45 typedef struct __bgzidx_t bgzidx_t;
46
47 struct BGZF {
48 int errcode:16, is_write:2, is_be:2, compress_level:9, is_compressed:2, is_gzip:1;
49 int cache_size;
50 int block_length, block_offset;
51 int64_t block_address, uncompressed_address;
52 void *uncompressed_block, *compressed_block;
53 void *cache; // a pointer to a hash table
54 struct hFILE *fp; // actual file handle
55 struct bgzf_mtaux_t *mt; // only used for multi-threading
56 bgzidx_t *idx; // BGZF index
57 int idx_build_otf; // build index on the fly, set by bgzf_index_build_init()
58 z_stream *gz_stream;// for gzip-compressed files
59 };
60 #ifndef HTS_BGZF_TYPEDEF
61 typedef struct BGZF BGZF;
62 #define HTS_BGZF_TYPEDEF
63 #endif
64
65 #ifndef KSTRING_T
66 #define KSTRING_T kstring_t
67 typedef struct __kstring_t {
68 size_t l, m;
69 char *s;
70 } kstring_t;
71 #endif
72
73 // #ifdef __cplusplus
74 // extern "C" {
75 // #endif
76
77 /******************
78 * Basic routines *
79 ******************/
80
81 /**
82 * Open an existing file descriptor for reading or writing.
83 *
84 * @param fd file descriptor
85 * @param mode mode matching /[rwag][u0-9]+/: 'r' for reading, 'w' for
86 * writing, 'a' for appending, 'g' for gzip rather than BGZF
87 * compression (with 'w' only), and digit specifies the zlib
88 * compression level.
89 * Note that there is a distinction between 'u' and '0': the
90 * first yields plain uncompressed output whereas the latter
91 * outputs uncompressed data wrapped in the zlib format.
92 * @return BGZF file handler; 0 on error
93 */
94 BGZF* bgzf_dopen(int fd, const char *mode);
95
96 #define bgzf_fdopen(fd, mode) bgzf_dopen((fd), (mode)) // for backward compatibility
97
98 /**
99 * Open the specified file for reading or writing.
100 */
101 BGZF* bgzf_open(const char* path, const char *mode);
102
103 /**
104 * Open an existing hFILE stream for reading or writing.
105 */
106 BGZF* bgzf_hopen(struct hFILE *fp, const char *mode);
107
108 /**
109 * Close the BGZF and free all associated resources.
110 *
111 * @param fp BGZF file handler
112 * @return 0 on success and -1 on error
113 */
114 int bgzf_close(BGZF *fp);
115
116 /**
117 * Read up to _length_ bytes from the file storing into _data_.
118 *
119 * @param fp BGZF file handler
120 * @param data data array to read into
121 * @param length size of data to read
122 * @return number of bytes actually read; 0 on end-of-file and -1 on error
123 */
124 ssize_t bgzf_read(BGZF *fp, void *data, size_t length);
125
126 /**
127 * Write _length_ bytes from _data_ to the file. If no I/O errors occur,
128 * the complete _length_ bytes will be written (or queued for writing).
129 *
130 * @param fp BGZF file handler
131 * @param data data array to write
132 * @param length size of data to write
133 * @return number of bytes written (i.e., _length_); negative on error
134 */
135 ssize_t bgzf_write(BGZF *fp, const void *data, size_t length);
136
137 /**
138 * Read up to _length_ bytes directly from the underlying stream without
139 * decompressing. Bypasses BGZF blocking, so must be used with care in
140 * specialised circumstances only.
141 *
142 * @param fp BGZF file handler
143 * @param data data array to read into
144 * @param length number of raw bytes to read
145 * @return number of bytes actually read; 0 on end-of-file and -1 on error
146 */
147 ssize_t bgzf_raw_read(BGZF *fp, void *data, size_t length);
148
149 /**
150 * Write _length_ bytes directly to the underlying stream without
151 * compressing. Bypasses BGZF blocking, so must be used with care
152 * in specialised circumstances only.
153 *
154 * @param fp BGZF file handler
155 * @param data data array to write
156 * @param length number of raw bytes to write
157 * @return number of bytes actually written; -1 on error
158 */
159 ssize_t bgzf_raw_write(BGZF *fp, const void *data, size_t length);
160
161 /**
162 * Write the data in the buffer to the file.
163 */
164 int bgzf_flush(BGZF *fp);
165
166 /**
167 * Return a virtual file pointer to the current location in the file.
168 * No interpetation of the value should be made, other than a subsequent
169 * call to bgzf_seek can be used to position the file at the same point.
170 * Return value is non-negative on success.
171 */
172 #define bgzf_tell(fp) (((fp)->block_address << 16) | ((fp)->block_offset & 0xFFFF))
173
174 /**
175 * Set the file to read from the location specified by _pos_.
176 *
177 * @param fp BGZF file handler
178 * @param pos virtual file offset returned by bgzf_tell()
179 * @param whence must be SEEK_SET
180 * @return 0 on success and -1 on error
181 */
182 int64_t bgzf_seek(BGZF *fp, int64_t pos, int whence);
183
184 /**
185 * Check if the BGZF end-of-file (EOF) marker is present
186 *
187 * @param fp BGZF file handler opened for reading
188 * @return 1 if the EOF marker is present and correct;
189 * 2 if it can't be checked, e.g., because fp isn't seekable;
190 * 0 if the EOF marker is absent;
191 * -1 (with errno set) on error
192 */
193 int bgzf_check_EOF(BGZF *fp);
194
195 /**
196 * Check if a file is in the BGZF format
197 *
198 * @param fn file name
199 * @return 1 if _fn_ is BGZF; 0 if not or on I/O error
200 */
201 int bgzf_is_bgzf(const char *fn);
202
203 /*********************
204 * Advanced routines *
205 *********************/
206
207 /**
208 * Set the cache size. Only effective when compiled with -DBGZF_CACHE.
209 *
210 * @param fp BGZF file handler
211 * @param size size of cache in bytes; 0 to disable caching (default)
212 */
213 void bgzf_set_cache_size(BGZF *fp, int size);
214
215 /**
216 * Flush the file if the remaining buffer size is smaller than _size_
217 * @return 0 if flushing succeeded or was not needed; negative on error
218 */
219 int bgzf_flush_try(BGZF *fp, ssize_t size);
220
221 /**
222 * Read one byte from a BGZF file. It is faster than bgzf_read()
223 * @param fp BGZF file handler
224 * @return byte read; -1 on end-of-file or error
225 */
226 int bgzf_getc(BGZF *fp);
227
228 /**
229 * Read one line from a BGZF file. It is faster than bgzf_getc()
230 *
231 * @param fp BGZF file handler
232 * @param delim delimitor
233 * @param str string to write to; must be initialized
234 * @return length of the string; 0 on end-of-file; negative on error
235 */
236 int bgzf_getline(BGZF *fp, int delim, kstring_t *str);
237
238 /**
239 * Read the next BGZF block.
240 */
241 int bgzf_read_block(BGZF *fp);
242
243 /**
244 * Enable multi-threading (only effective on writing and when the
245 * library was compiled with -DBGZF_MT)
246 *
247 * @param fp BGZF file handler; must be opened for writing
248 * @param n_threads #threads used for writing
249 * @param n_sub_blks #blocks processed by each thread; a value 64-256 is recommended
250 */
251 int bgzf_mt(BGZF *fp, int n_threads, int n_sub_blks);
252
253
254 /*******************
255 * bgzidx routines *
256 *******************/
257
258 /**
259 * Position BGZF at the uncompressed offset
260 *
261 * @param fp BGZF file handler; must be opened for reading
262 * @param uoffset file offset in the uncompressed data
263 * @param where SEEK_SET supported atm
264 *
265 * Returns 0 on success and -1 on error.
266 */
267 int bgzf_useek(BGZF *fp, long uoffset, int where);
268
269 /**
270 * Position in uncompressed BGZF
271 *
272 * @param fp BGZF file handler; must be opened for reading
273 *
274 * Returns the current offset on success and -1 on error.
275 */
276 long bgzf_utell(BGZF *fp);
277
278 /**
279 * Tell BGZF to build index while compressing.
280 *
281 * @param fp BGZF file handler; can be opened for reading or writing.
282 *
283 * Returns 0 on success and -1 on error.
284 */
285 int bgzf_index_build_init(BGZF *fp);
286
287 /**
288 * Load BGZF index
289 *
290 * @param fp BGZF file handler
291 * @param bname base name
292 * @param suffix suffix to add to bname (can be NULL)
293 *
294 * Returns 0 on success and -1 on error.
295 */
296 int bgzf_index_load(BGZF *fp, const char *bname, const char *suffix);
297
298 /**
299 * Save BGZF index
300 *
301 * @param fp BGZF file handler
302 * @param bname base name
303 * @param suffix suffix to add to bname (can be NULL)
304 *
305 * Returns 0 on success and -1 on error.
306 */
307 int bgzf_index_dump(BGZF *fp, const char *bname, const char *suffix);
308
309 // #ifdef __cplusplus
310 // }
311 // #endif
312
313 #endif