0
|
1 /* The MIT License
|
|
2
|
|
3 Copyright (c) 2008 Broad Institute / Massachusetts Institute of Technology
|
|
4
|
|
5 Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6 of this software and associated documentation files (the "Software"), to deal
|
|
7 in the Software without restriction, including without limitation the rights
|
|
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9 copies of the Software, and to permit persons to whom the Software is
|
|
10 furnished to do so, subject to the following conditions:
|
|
11
|
|
12 The above copyright notice and this permission notice shall be included in
|
|
13 all copies or substantial portions of the Software.
|
|
14
|
|
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21 THE SOFTWARE.
|
|
22 */
|
|
23
|
|
24 #ifndef __BGZF_H
|
|
25 #define __BGZF_H
|
|
26
|
|
27 #include <stdint.h>
|
|
28 #include <stdio.h>
|
|
29 #include <zlib.h>
|
|
30 #ifdef _USE_KNETFILE
|
|
31 #include "knetfile.h"
|
|
32 #endif
|
|
33
|
|
34 //typedef int8_t bool;
|
|
35
|
|
36 typedef struct {
|
|
37 int file_descriptor;
|
|
38 char open_mode; // 'r' or 'w'
|
|
39 int16_t owned_file, compress_level;
|
|
40 #ifdef _USE_KNETFILE
|
|
41 union {
|
|
42 knetFile *fpr;
|
|
43 FILE *fpw;
|
|
44 } x;
|
|
45 #else
|
|
46 FILE* file;
|
|
47 #endif
|
|
48 int uncompressed_block_size;
|
|
49 int compressed_block_size;
|
|
50 void* uncompressed_block;
|
|
51 void* compressed_block;
|
|
52 int64_t block_address;
|
|
53 int block_length;
|
|
54 int block_offset;
|
|
55 int cache_size;
|
|
56 const char* error;
|
|
57 void *cache; // a pointer to a hash table
|
|
58 } BGZF;
|
|
59
|
|
60 #ifdef __cplusplus
|
|
61 extern "C" {
|
|
62 #endif
|
|
63
|
|
64 /*
|
|
65 * Open an existing file descriptor for reading or writing.
|
|
66 * Mode must be either "r" or "w".
|
|
67 * A subsequent bgzf_close will not close the file descriptor.
|
|
68 * Returns null on error.
|
|
69 */
|
|
70 BGZF* bgzf_fdopen(int fd, const char* __restrict mode);
|
|
71
|
|
72 /*
|
|
73 * Open the specified file for reading or writing.
|
|
74 * Mode must be either "r" or "w".
|
|
75 * Returns null on error.
|
|
76 */
|
|
77 BGZF* bgzf_open(const char* path, const char* __restrict mode);
|
|
78
|
|
79 /*
|
|
80 * Close the BGZ file and free all associated resources.
|
|
81 * Does not close the underlying file descriptor if created with bgzf_fdopen.
|
|
82 * Returns zero on success, -1 on error.
|
|
83 */
|
|
84 int bgzf_close(BGZF* fp);
|
|
85
|
|
86 /*
|
|
87 * Read up to length bytes from the file storing into data.
|
|
88 * Returns the number of bytes actually read.
|
|
89 * Returns zero on end of file.
|
|
90 * Returns -1 on error.
|
|
91 */
|
|
92 int bgzf_read(BGZF* fp, void* data, int length);
|
|
93
|
|
94 /*
|
|
95 * Write length bytes from data to the file.
|
|
96 * Returns the number of bytes written.
|
|
97 * Returns -1 on error.
|
|
98 */
|
|
99 int bgzf_write(BGZF* fp, const void* data, int length);
|
|
100
|
|
101 /*
|
|
102 * Return a virtual file pointer to the current location in the file.
|
|
103 * No interpetation of the value should be made, other than a subsequent
|
|
104 * call to bgzf_seek can be used to position the file at the same point.
|
|
105 * Return value is non-negative on success.
|
|
106 * Returns -1 on error.
|
|
107 */
|
|
108 #define bgzf_tell(fp) ((fp->block_address << 16) | (fp->block_offset & 0xFFFF))
|
|
109
|
|
110 /*
|
|
111 * Set the file to read from the location specified by pos, which must
|
|
112 * be a value previously returned by bgzf_tell for this file (but not
|
|
113 * necessarily one returned by this file handle).
|
|
114 * The where argument must be SEEK_SET.
|
|
115 * Seeking on a file opened for write is not supported.
|
|
116 * Returns zero on success, -1 on error.
|
|
117 */
|
|
118 int64_t bgzf_seek(BGZF* fp, int64_t pos, int where);
|
|
119
|
|
120 /*
|
|
121 * Set the cache size. Zero to disable. By default, caching is
|
|
122 * disabled. The recommended cache size for frequent random access is
|
|
123 * about 8M bytes.
|
|
124 */
|
|
125 void bgzf_set_cache_size(BGZF *fp, int cache_size);
|
|
126
|
|
127 int bgzf_check_EOF(BGZF *fp);
|
|
128 int bgzf_read_block(BGZF* fp);
|
|
129 int bgzf_flush(BGZF* fp);
|
|
130 int bgzf_flush_try(BGZF *fp, int size);
|
|
131 int bgzf_check_bgzf(const char *fn);
|
|
132
|
|
133 #ifdef __cplusplus
|
|
134 }
|
|
135 #endif
|
|
136
|
|
137 static inline int bgzf_getc(BGZF *fp)
|
|
138 {
|
|
139 int c;
|
|
140 if (fp->block_offset >= fp->block_length) {
|
|
141 if (bgzf_read_block(fp) != 0) return -2; /* error */
|
|
142 if (fp->block_length == 0) return -1; /* end-of-file */
|
|
143 }
|
|
144 c = ((unsigned char*)fp->uncompressed_block)[fp->block_offset++];
|
|
145 if (fp->block_offset == fp->block_length) {
|
|
146 #ifdef _USE_KNETFILE
|
|
147 fp->block_address = knet_tell(fp->x.fpr);
|
|
148 #else
|
|
149 fp->block_address = ftello(fp->file);
|
|
150 #endif
|
|
151 fp->block_offset = 0;
|
|
152 fp->block_length = 0;
|
|
153 }
|
|
154 return c;
|
|
155 }
|
|
156
|
|
157 #endif
|