Gamgee
You miserable little maggot. I'll stove your head in!
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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  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 <stdint.h>
33 #include <stdio.h>
34 #include <zlib.h>
35 #include <sys/types.h>
36 
37 #define BGZF_BLOCK_SIZE 0xff00 // make sure compressBound(BGZF_BLOCK_SIZE) < BGZF_MAX_BLOCK_SIZE
38 #define BGZF_MAX_BLOCK_SIZE 0x10000
39 
40 #define BGZF_ERR_ZLIB 1
41 #define BGZF_ERR_HEADER 2
42 #define BGZF_ERR_IO 4
43 #define BGZF_ERR_MISUSE 8
44 
45 struct hFILE;
46 struct bgzf_mtaux_t;
47 typedef struct __bgzidx_t bgzidx_t;
48 
49 struct BGZF {
55  void *cache; // a pointer to a hash table
56  struct hFILE *fp; // actual file handle
57  struct bgzf_mtaux_t *mt; // only used for multi-threading
58  bgzidx_t *idx; // BGZF index
59  int idx_build_otf; // build index on the fly, set by bgzf_index_build_init()
60  z_stream *gz_stream;// for gzip-compressed files
61 };
62 #ifndef HTS_BGZF_TYPEDEF
63 typedef struct BGZF BGZF;
64 #define HTS_BGZF_TYPEDEF
65 #endif
66 
67 #ifndef KSTRING_T
68 #define KSTRING_T kstring_t
69 typedef struct __kstring_t {
70  size_t l, m;
71  char *s;
72 } kstring_t;
73 #endif
74 
75 #ifdef __cplusplus
76 extern "C" {
77 #endif
78 
79  /******************
80  * Basic routines *
81  ******************/
82 
96  BGZF* bgzf_dopen(int fd, const char *mode);
97 
98  #define bgzf_fdopen(fd, mode) bgzf_dopen((fd), (mode)) // for backward compatibility
99 
103  BGZF* bgzf_open(const char* path, const char *mode);
104 
108  BGZF* bgzf_hopen(struct hFILE *fp, const char *mode);
109 
116  int bgzf_close(BGZF *fp);
117 
126  ssize_t bgzf_read(BGZF *fp, void *data, size_t length);
127 
137  ssize_t bgzf_write(BGZF *fp, const void *data, size_t length);
138 
149  ssize_t bgzf_raw_read(BGZF *fp, void *data, size_t length);
150 
161  ssize_t bgzf_raw_write(BGZF *fp, const void *data, size_t length);
162 
166  int bgzf_flush(BGZF *fp);
167 
174  #define bgzf_tell(fp) (((fp)->block_address << 16) | ((fp)->block_offset & 0xFFFF))
175 
184  int64_t bgzf_seek(BGZF *fp, int64_t pos, int whence);
185 
195  int bgzf_check_EOF(BGZF *fp);
196 
203  int bgzf_is_bgzf(const char *fn);
204 
205  /*********************
206  * Advanced routines *
207  *********************/
208 
215  void bgzf_set_cache_size(BGZF *fp, int size);
216 
221  int bgzf_flush_try(BGZF *fp, ssize_t size);
222 
228  int bgzf_getc(BGZF *fp);
229 
238  int bgzf_getline(BGZF *fp, int delim, kstring_t *str);
239 
243  int bgzf_read_block(BGZF *fp);
244 
253  int bgzf_mt(BGZF *fp, int n_threads, int n_sub_blks);
254 
255 
256  /*******************
257  * bgzidx routines *
258  *******************/
259 
269  int bgzf_useek(BGZF *fp, long uoffset, int where);
270 
278  long bgzf_utell(BGZF *fp);
279 
287  int bgzf_index_build_init(BGZF *fp);
288 
298  int bgzf_index_load(BGZF *fp, const char *bname, const char *suffix);
299 
309  int bgzf_index_dump(BGZF *fp, const char *bname, const char *suffix);
310 
311 #ifdef __cplusplus
312 }
313 #endif
314 
315 #endif
void * cache
Definition: bgzf.h:55
struct bgzf_mtaux_t * mt
Definition: bgzf.h:57
Definition: hfile.h:42
int64_t bgzf_seek(BGZF *fp, int64_t pos, int whence)
Definition: bgzf.c:846
int bgzf_index_dump(BGZF *fp, const char *bname, const char *suffix)
Definition: bgzf.c:967
ssize_t bgzf_raw_write(BGZF *fp, const void *data, size_t length)
Definition: bgzf.c:790
int bgzf_flush_try(BGZF *fp, ssize_t size)
Definition: bgzf.c:761
int cache_size
Definition: bgzf.h:51
void bgzf_set_cache_size(BGZF *fp, int size)
Definition: bgzf.c:828
int is_compressed
Definition: bgzf.h:50
bgzidx_t * idx
Definition: bgzf.h:58
size_t l
Definition: bgzf.h:70
z_stream * gz_stream
Definition: bgzf.h:60
enum @17 mode
int is_gzip
Definition: bgzf.h:50
int compress_level
Definition: bgzf.h:50
int bgzf_getc(BGZF *fp)
Definition: bgzf.c:879
int is_be
Definition: bgzf.h:50
struct __kstring_t kstring_t
int block_length
Definition: bgzf.h:52
BGZF * bgzf_dopen(int fd, const char *mode)
Definition: bgzf.c:190
int bgzf_useek(BGZF *fp, long uoffset, int where)
Definition: bgzf.c:1068
int is_write
Definition: bgzf.h:50
int bgzf_read_block(BGZF *fp)
Definition: bgzf.c:418
BGZF * bgzf_open(const char *path, const char *mode)
Definition: bgzf.c:168
Definition: bgzf.h:69
int bgzf_is_bgzf(const char *fn)
Definition: bgzf.c:867
int block_offset
Definition: bgzf.h:52
BGZF * bgzf_hopen(struct hFILE *fp, const char *mode)
Definition: bgzf.c:212
int errcode
Definition: bgzf.h:50
int bgzf_mt(BGZF *fp, int n_threads, int n_sub_blks)
Definition: bgzf.c:628
size_t m
Definition: bgzf.h:70
#define str(x)
Definition: sam.c:66
ssize_t bgzf_write(BGZF *fp, const void *data, size_t length)
Definition: bgzf.c:767
int bgzf_index_load(BGZF *fp, const char *bname, const char *suffix)
Definition: bgzf.c:1017
int idx_build_otf
Definition: bgzf.h:59
int bgzf_flush(BGZF *fp)
Definition: bgzf.c:735
void * compressed_block
Definition: bgzf.h:54
void * uncompressed_block
Definition: bgzf.h:54
int bgzf_check_EOF(BGZF *fp)
Definition: bgzf.c:833
char * s
Definition: bgzf.h:71
Definition: bgzf.c:79
int64_t uncompressed_address
Definition: bgzf.h:53
ssize_t bgzf_read(BGZF *fp, void *data, size_t length)
Definition: bgzf.c:546
int bgzf_getline(BGZF *fp, int delim, kstring_t *str)
Definition: bgzf.c:900
int bgzf_close(BGZF *fp)
Definition: bgzf.c:795
long bgzf_utell(BGZF *fp)
Definition: bgzf.c:1119
int bgzf_index_build_init(BGZF *fp)
Definition: bgzf.c:943
struct hFILE * fp
Definition: bgzf.h:56
Definition: bgzf.h:49
ssize_t bgzf_raw_read(BGZF *fp, void *data, size_t length)
Definition: bgzf.c:575
int64_t block_address
Definition: bgzf.h:53
Definition: bgzf.c:588