annotate pyPRADA_1.2/tools/samtools-0.1.16/bgzf.h @ 0:acc2ca1a3ba4

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