NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bgzf.h
Go to the documentation of this file.
1 /* The MIT License
2 
3  Copyright (c) 2008 Broad Institute / Massachusetts Institute of Technology
4  2011, 2012 Attractive Chaos <attractor@live.co.uk>
5 
6  Permission is hereby granted, free of charge, to any person obtaining a copy
7  of this software and associated documentation files (the "Software"), to deal
8  in the Software without restriction, including without limitation the rights
9  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  copies of the Software, and to permit persons to whom the Software is
11  furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  THE SOFTWARE.
23 */
24 
25 /* The BGZF library was originally written by Bob Handsaker from the Broad
26  * Institute. It was later improved by the SAMtools developers. */
27 
28 #ifndef __BGZF_H
29 #define __BGZF_H
30 
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <zlib.h>
34 #include <sys/types.h>
35 
36 #define BGZF_BLOCK_SIZE 0xff00 // make sure compressBound(BGZF_BLOCK_SIZE) < BGZF_MAX_BLOCK_SIZE
37 #define BGZF_MAX_BLOCK_SIZE 0x10000
38 
39 #define BGZF_ERR_ZLIB 1
40 #define BGZF_ERR_HEADER 2
41 #define BGZF_ERR_IO 4
42 #define BGZF_ERR_MISUSE 8
43 
44 struct hFILE;
45 struct bgzf_mtaux_t;
46 typedef struct __bgzidx_t bgzidx_t;
47 
48 struct BGZF {
54  void *cache; // a pointer to a hash table
55  struct hFILE *fp; // actual file handle
56  struct bgzf_mtaux_t *mt; // only used for multi-threading
57  bgzidx_t *idx; // BGZF index
58  int idx_build_otf; // build index on the fly, set by bgzf_index_build_init()
59  z_stream *gz_stream;// for gzip-compressed files
60 };
61 #ifndef HTS_BGZF_TYPEDEF
62 typedef struct BGZF BGZF;
63 #define HTS_BGZF_TYPEDEF
64 #endif
65 
66 #ifndef KSTRING_T
67 #define KSTRING_T kstring_t
68 typedef struct __kstring_t {
69  size_t l, m;
70  char *s;
71 } kstring_t;
72 #endif
73 
74 #ifdef __cplusplus
75 extern "C" {
76 #endif
77 
78  /******************
79  * Basic routines *
80  ******************/
81 
94  BGZF* bgzf_dopen(int fd, const char *mode);
95 
96  #define bgzf_fdopen(fd, mode) bgzf_dopen((fd), (mode)) // for backward compatibility
97 
101  BGZF* bgzf_open(const char* path, const char *mode);
102 
106  BGZF* bgzf_hopen(struct hFILE *fp, const char *mode);
107 
114  int bgzf_close(BGZF *fp);
115 
124  ssize_t bgzf_read(BGZF *fp, void *data, size_t length);
125 
135  ssize_t bgzf_write(BGZF *fp, const void *data, size_t length);
136 
147  ssize_t bgzf_raw_read(BGZF *fp, void *data, size_t length);
148 
159  ssize_t bgzf_raw_write(BGZF *fp, const void *data, size_t length);
160 
164  int bgzf_flush(BGZF *fp);
165 
172  #define bgzf_tell(fp) (((fp)->block_address << 16) | ((fp)->block_offset & 0xFFFF))
173 
182  int64_t bgzf_seek(BGZF *fp, int64_t pos, int whence);
183 
193  int bgzf_check_EOF(BGZF *fp);
194 
201  int bgzf_is_bgzf(const char *fn);
202 
203  /*********************
204  * Advanced routines *
205  *********************/
206 
213  void bgzf_set_cache_size(BGZF *fp, int size);
214 
219  int bgzf_flush_try(BGZF *fp, ssize_t size);
220 
226  int bgzf_getc(BGZF *fp);
227 
236  int bgzf_getline(BGZF *fp, int delim, kstring_t *str);
237 
241  int bgzf_read_block(BGZF *fp);
242 
251  int bgzf_mt(BGZF *fp, int n_threads, int n_sub_blks);
252 
253 
254  /*******************
255  * bgzidx routines *
256  *******************/
257 
267  int bgzf_useek(BGZF *fp, long uoffset, int where);
268 
276  long bgzf_utell(BGZF *fp);
277 
285  int bgzf_index_build_init(BGZF *fp);
286 
296  int bgzf_index_load(BGZF *fp, const char *bname, const char *suffix);
297 
307  int bgzf_index_dump(BGZF *fp, const char *bname, const char *suffix);
308 
309 #ifdef __cplusplus
310 }
311 #endif
312 
313 #endif