annotate ezBAMQC/src/htslib/cram/cram_io.h @ 0:dfa3745e5fd8

Uploaded
author youngkim
date Thu, 24 Mar 2016 17:12:52 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
1 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
2 Copyright (c) 2012-2014 Genome Research Ltd.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
3 Author: James Bonfield <jkb@sanger.ac.uk>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
4
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
5 Redistribution and use in source and binary forms, with or without
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
6 modification, are permitted provided that the following conditions are met:
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
7
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
8 1. Redistributions of source code must retain the above copyright notice,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
9 this list of conditions and the following disclaimer.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
10
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
11 2. Redistributions in binary form must reproduce the above copyright notice,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
12 this list of conditions and the following disclaimer in the documentation
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
13 and/or other materials provided with the distribution.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
14
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
15 3. Neither the names Genome Research Ltd and Wellcome Trust Sanger
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
16 Institute nor the names of its contributors may be used to endorse or promote
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
17 products derived from this software without specific prior written permission.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
18
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
19 THIS SOFTWARE IS PROVIDED BY GENOME RESEARCH LTD AND CONTRIBUTORS "AS IS" AND
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
22 DISCLAIMED. IN NO EVENT SHALL GENOME RESEARCH LTD OR CONTRIBUTORS BE LIABLE
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
25 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
26 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
27 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
29 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
30
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
31 /*! \file
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
32 * Include cram.h instead.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
33 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
34 * This is an internal part of the CRAM system and is automatically included
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
35 * when you #include cram.h.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
36 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
37 * Implements the low level CRAM I/O primitives.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
38 * This includes basic data types such as byte, int, ITF-8,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
39 * maps, bitwise I/O, etc.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
40 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
41
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
42 #ifndef _CRAM_IO_H_
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
43 #define _CRAM_IO_H_
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
44
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
45 #ifdef __cplusplus
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
46 extern "C" {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
47 #endif
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
48
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
49 #define ITF8_MACROS
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
50
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
51 #include <stdint.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
52 #include <cram/misc.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
53
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
54 /**@{ ----------------------------------------------------------------------
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
55 * ITF8 encoding and decoding.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
56 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
57 * Also see the itf8_get and itf8_put macros.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
58 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
59
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
60 /*! INTERNAL: Converts two characters into an integer for use in switch{} */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
61 #define CRAM_KEY(a,b) (((a)<<8)|((b)))
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
62
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
63 /*! Reads an integer in ITF-8 encoding from 'fd' and stores it in
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
64 * *val.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
65 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
66 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
67 * Returns the number of bytes read on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
68 * -1 on failure
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
69 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
70 int itf8_decode(cram_fd *fd, int32_t *val);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
71
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
72 #ifndef ITF8_MACROS
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
73 /*! Reads an integer in ITF-8 encoding from 'cp' and stores it in
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
74 * *val.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
75 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
76 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
77 * Returns the number of bytes read on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
78 * -1 on failure
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
79 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
80 int itf8_get(char *cp, int32_t *val_p);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
81
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
82 /*! Stores a value to memory in ITF-8 format.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
83 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
84 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
85 * Returns the number of bytes required to store the number.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
86 * This is a maximum of 5 bytes.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
87 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
88 int itf8_put(char *cp, int32_t val);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
89
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
90 #else
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
91
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
92 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
93 * Macro implementations of the above
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
94 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
95 #define itf8_get(c,v) (((uc)(c)[0]<0x80)?(*(v)=(uc)(c)[0],1):(((uc)(c)[0]<0xc0)?(*(v)=(((uc)(c)[0]<<8)|(uc)(c)[1])&0x3fff,2):(((uc)(c)[0]<0xe0)?(*(v)=(((uc)(c)[0]<<16)|((uc)(c)[1]<<8)|(uc)(c)[2])&0x1fffff,3):(((uc)(c)[0]<0xf0)?(*(v)=(((uc)(c)[0]<<24)|((uc)(c)[1]<<16)|((uc)(c)[2]<<8)|(uc)(c)[3])&0x0fffffff,4):(*(v)=(((uc)(c)[0]&0x0f)<<28)|((uc)(c)[1]<<20)|((uc)(c)[2]<<12)|((uc)(c)[3]<<4)|((uc)(c)[4]&0x0f),5)))))
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
96
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
97 #define itf8_put(c,v) ((!((v)&~0x7f))?((c)[0]=(v),1):(!((v)&~0x3fff))?((c)[0]=((v)>>8)|0x80,(c)[1]=(v)&0xff,2):(!((v)&~0x1fffff))?((c)[0]=((v)>>16)|0xc0,(c)[1]=((v)>>8)&0xff,(c)[2]=(v)&0xff,3):(!((v)&~0xfffffff))?((c)[0]=((v)>>24)|0xe0,(c)[1]=((v)>>16)&0xff,(c)[2]=((v)>>8)&0xff,(c)[3]=(v)&0xff,4):((c)[0]=0xf0|(((v)>>28)&0xff),(c)[1]=((v)>>20)&0xff,(c)[2]=((v)>>12)&0xff,(c)[3]=((v)>>4)&0xff,(c)[4]=(v)&0xf,5))
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
98
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
99 #define itf8_size(v) ((!((v)&~0x7f))?1:(!((v)&~0x3fff))?2:(!((v)&~0x1fffff))?3:(!((v)&~0xfffffff))?4:5)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
100
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
101 #endif
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
102
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
103 int ltf8_get(char *cp, int64_t *val_p);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
104 int ltf8_put(char *cp, int64_t val);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
105
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
106 /*! Pushes a value in ITF8 format onto the end of a block.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
107 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
108 * This shouldn't be used for high-volume data as it is not the fastest
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
109 * method.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
110 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
111 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
112 * Returns the number of bytes written
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
113 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
114 int itf8_put_blk(cram_block *blk, int val);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
115
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
116 /**@}*/
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
117 /**@{ ----------------------------------------------------------------------
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
118 * CRAM blocks - the dynamically growable data block. We have code to
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
119 * create, update, (un)compress and read/write.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
120 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
121 * These are derived from the deflate_interlaced.c blocks, but with the
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
122 * CRAM extension of content types and IDs.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
123 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
124
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
125 /*! Allocates a new cram_block structure with a specified content_type and
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
126 * id.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
127 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
128 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
129 * Returns block pointer on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
130 * NULL on failure
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
131 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
132 cram_block *cram_new_block(enum cram_content_type content_type,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
133 int content_id);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
134
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
135 /*! Reads a block from a cram file.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
136 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
137 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
138 * Returns cram_block pointer on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
139 * NULL on failure
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
140 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
141 cram_block *cram_read_block(cram_fd *fd);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
142
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
143 /*! Writes a CRAM block.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
144 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
145 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
146 * Returns 0 on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
147 * -1 on failure
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
148 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
149 int cram_write_block(cram_fd *fd, cram_block *b);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
150
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
151 /*! Frees a CRAM block, deallocating internal data too.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
152 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
153 void cram_free_block(cram_block *b);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
154
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
155 /*! Uncompress a memory block using Zlib.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
156 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
157 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
158 * Returns 0 on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
159 * -1 on failure
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
160 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
161 char *zlib_mem_inflate(char *cdata, size_t csize, size_t *size);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
162
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
163 /*! Uncompresses a CRAM block, if compressed.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
164 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
165 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
166 * Returns 0 on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
167 * -1 on failure
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
168 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
169 int cram_uncompress_block(cram_block *b);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
170
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
171 /*! Compresses a block.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
172 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
173 * Compresses a block using one of two different zlib strategies. If we only
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
174 * want one choice set strat2 to be -1.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
175 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
176 * The logic here is that sometimes Z_RLE does a better job than Z_FILTERED
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
177 * or Z_DEFAULT_STRATEGY on quality data. If so, we'd rather use it as it is
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
178 * significantly faster.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
179 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
180 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
181 * Returns 0 on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
182 * -1 on failure
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
183 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
184 int cram_compress_block(cram_fd *fd, cram_block *b, cram_metrics *metrics,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
185 int method, int level);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
186
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
187 cram_metrics *cram_new_metrics(void);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
188 char *cram_block_method2str(enum cram_block_method m);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
189 char *cram_content_type2str(enum cram_content_type t);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
190
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
191 /* --- Accessor macros for manipulating blocks on a byte by byte basis --- */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
192
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
193 /* Block size and data pointer. */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
194 #define BLOCK_SIZE(b) ((b)->byte)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
195 #define BLOCK_DATA(b) ((b)->data)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
196
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
197 /* Returns the address one past the end of the block */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
198 #define BLOCK_END(b) (&(b)->data[(b)->byte])
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
199
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
200 /* Request block to be at least 'l' bytes long */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
201 #define BLOCK_RESIZE(b,l) \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
202 do { \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
203 while((b)->alloc <= (l)) { \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
204 (b)->alloc = (b)->alloc ? (b)->alloc*1.5 : 1024; \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
205 (b)->data = realloc((b)->data, (b)->alloc); \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
206 } \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
207 } while(0)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
208
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
209 /* Ensure the block can hold at least another 'l' bytes */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
210 #define BLOCK_GROW(b,l) BLOCK_RESIZE((b), BLOCK_SIZE((b)) + (l))
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
211
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
212 /* Append string 's' of length 'l' */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
213 #define BLOCK_APPEND(b,s,l) \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
214 do { \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
215 BLOCK_GROW((b),(l)); \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
216 memcpy(BLOCK_END((b)), (s), (l)); \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
217 BLOCK_SIZE((b)) += (l); \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
218 } while (0)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
219
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
220 /* Append as single character 'c' */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
221 #define BLOCK_APPEND_CHAR(b,c) \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
222 do { \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
223 BLOCK_GROW((b),1); \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
224 (b)->data[(b)->byte++] = (c); \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
225 } while (0)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
226
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
227 /* Append a single unsigned integer */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
228 #define BLOCK_APPEND_UINT(b,i) \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
229 do { \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
230 unsigned char *cp; \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
231 BLOCK_GROW((b),11); \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
232 cp = &(b)->data[(b)->byte]; \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
233 (b)->byte += append_uint32(cp, (i)) - cp; \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
234 } while (0)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
235
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
236 static inline unsigned char *append_uint32(unsigned char *cp, uint32_t i) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
237 uint32_t j;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
238
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
239 if (i == 0) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
240 *cp++ = '0';
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
241 return cp;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
242 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
243
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
244 if (i < 100) goto b1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
245 if (i < 10000) goto b3;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
246 if (i < 1000000) goto b5;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
247 if (i < 100000000) goto b7;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
248
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
249 if ((j = i / 1000000000)) {*cp++ = j + '0'; i -= j*1000000000; goto x8;}
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
250 if ((j = i / 100000000)) {*cp++ = j + '0'; i -= j*100000000; goto x7;}
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
251 b7:if ((j = i / 10000000)) {*cp++ = j + '0'; i -= j*10000000; goto x6;}
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
252 if ((j = i / 1000000)) {*cp++ = j + '0', i -= j*1000000; goto x5;}
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
253 b5:if ((j = i / 100000)) {*cp++ = j + '0', i -= j*100000; goto x4;}
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
254 if ((j = i / 10000)) {*cp++ = j + '0', i -= j*10000; goto x3;}
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
255 b3:if ((j = i / 1000)) {*cp++ = j + '0', i -= j*1000; goto x2;}
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
256 if ((j = i / 100)) {*cp++ = j + '0', i -= j*100; goto x1;}
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
257 b1:if ((j = i / 10)) {*cp++ = j + '0', i -= j*10; goto x0;}
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
258 if (i) *cp++ = i + '0';
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
259 return cp;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
260
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
261 x8: *cp++ = i / 100000000 + '0', i %= 100000000;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
262 x7: *cp++ = i / 10000000 + '0', i %= 10000000;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
263 x6: *cp++ = i / 1000000 + '0', i %= 1000000;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
264 x5: *cp++ = i / 100000 + '0', i %= 100000;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
265 x4: *cp++ = i / 10000 + '0', i %= 10000;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
266 x3: *cp++ = i / 1000 + '0', i %= 1000;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
267 x2: *cp++ = i / 100 + '0', i %= 100;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
268 x1: *cp++ = i / 10 + '0', i %= 10;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
269 x0: *cp++ = i + '0';
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
270
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
271 return cp;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
272 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
273
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
274 static inline unsigned char *append_sub32(unsigned char *cp, uint32_t i) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
275 *cp++ = i / 100000000 + '0', i %= 100000000;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
276 *cp++ = i / 10000000 + '0', i %= 10000000;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
277 *cp++ = i / 1000000 + '0', i %= 1000000;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
278 *cp++ = i / 100000 + '0', i %= 100000;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
279 *cp++ = i / 10000 + '0', i %= 10000;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
280 *cp++ = i / 1000 + '0', i %= 1000;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
281 *cp++ = i / 100 + '0', i %= 100;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
282 *cp++ = i / 10 + '0', i %= 10;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
283 *cp++ = i + '0';
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
284
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
285 return cp;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
286 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
287
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
288 static inline unsigned char *append_uint64(unsigned char *cp, uint64_t i) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
289 uint64_t j;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
290
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
291 if (i <= 0xffffffff)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
292 return append_uint32(cp, i);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
293
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
294 if ((j = i/1000000000) > 1000000000) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
295 cp = append_uint32(cp, j/1000000000);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
296 j %= 1000000000;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
297 cp = append_sub32(cp, j);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
298 } else {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
299 cp = append_uint32(cp, i / 1000000000);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
300 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
301 cp = append_sub32(cp, i % 1000000000);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
302
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
303 return cp;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
304 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
305
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
306 #define BLOCK_UPLEN(b) \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
307 (b)->comp_size = (b)->uncomp_size = BLOCK_SIZE((b))
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
308
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
309 /**@}*/
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
310 /**@{ ----------------------------------------------------------------------
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
311 * Reference sequence handling
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
312 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
313
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
314 /*! Loads a reference set from fn and stores in the cram_fd.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
315 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
316 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
317 * Returns 0 on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
318 * -1 on failure
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
319 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
320 int cram_load_reference(cram_fd *fd, char *fn);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
321
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
322 /*! Generates a lookup table in refs based on the SQ headers in SAM_hdr.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
323 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
324 * Indexes references by the order they appear in a BAM file. This may not
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
325 * necessarily be the same order they appear in the fasta reference file.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
326 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
327 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
328 * Returns 0 on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
329 * -1 on failure
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
330 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
331 int refs2id(refs_t *r, SAM_hdr *bfd);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
332
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
333 void refs_free(refs_t *r);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
334
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
335 /*! Returns a portion of a reference sequence from start to end inclusive.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
336 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
337 * The returned pointer is owned by the cram_file fd and should not be freed
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
338 * by the caller. It is valid only until the next cram_get_ref is called
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
339 * with the same fd parameter (so is thread-safe if given multiple files).
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
340 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
341 * To return the entire reference sequence, specify start as 1 and end
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
342 * as 0.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
343 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
344 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
345 * Returns reference on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
346 * NULL on failure
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
347 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
348 char *cram_get_ref(cram_fd *fd, int id, int start, int end);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
349 void cram_ref_incr(refs_t *r, int id);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
350 void cram_ref_decr(refs_t *r, int id);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
351 /**@}*/
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
352 /**@{ ----------------------------------------------------------------------
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
353 * Containers
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
354 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
355
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
356 /*! Creates a new container, specifying the maximum number of slices
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
357 * and records permitted.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
358 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
359 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
360 * Returns cram_container ptr on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
361 * NULL on failure
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
362 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
363 cram_container *cram_new_container(int nrec, int nslice);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
364 void cram_free_container(cram_container *c);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
365
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
366 /*! Reads a container header.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
367 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
368 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
369 * Returns cram_container on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
370 * NULL on failure or no container left (fd->err == 0).
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
371 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
372 cram_container *cram_read_container(cram_fd *fd);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
373
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
374 /*! Writes a container structure.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
375 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
376 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
377 * Returns 0 on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
378 * -1 on failure
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
379 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
380 int cram_write_container(cram_fd *fd, cram_container *h);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
381
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
382 /*! Flushes a container to disk.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
383 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
384 * Flushes a completely or partially full container to disk, writing
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
385 * container structure, header and blocks. This also calls the encoder
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
386 * functions.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
387 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
388 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
389 * Returns 0 on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
390 * -1 on failure
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
391 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
392 int cram_flush_container(cram_fd *fd, cram_container *c);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
393 int cram_flush_container_mt(cram_fd *fd, cram_container *c);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
394
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
395
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
396 /**@}*/
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
397 /**@{ ----------------------------------------------------------------------
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
398 * Compression headers; the first part of the container
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
399 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
400
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
401 /*! Creates a new blank container compression header
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
402 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
403 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
404 * Returns header ptr on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
405 * NULL on failure
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
406 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
407 cram_block_compression_hdr *cram_new_compression_header(void);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
408
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
409 /*! Frees a cram_block_compression_hdr */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
410 void cram_free_compression_header(cram_block_compression_hdr *hdr);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
411
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
412
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
413 /**@}*/
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
414 /**@{ ----------------------------------------------------------------------
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
415 * Slices and slice headers
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
416 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
417
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
418 /*! Frees a slice header */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
419 void cram_free_slice_header(cram_block_slice_hdr *hdr);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
420
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
421 /*! Frees a slice */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
422 void cram_free_slice(cram_slice *s);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
423
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
424 /*! Creates a new empty slice in memory, for subsequent writing to
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
425 * disk.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
426 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
427 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
428 * Returns cram_slice ptr on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
429 * NULL on failure
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
430 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
431 cram_slice *cram_new_slice(enum cram_content_type type, int nrecs);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
432
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
433 /*! Loads an entire slice.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
434 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
435 * FIXME: In 1.0 the native unit of slices within CRAM is broken
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
436 * as slices contain references to objects in other slices.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
437 * To work around this while keeping the slice oriented outer loop
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
438 * we read all slices and stitch them together into a fake large
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
439 * slice instead.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
440 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
441 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
442 * Returns cram_slice ptr on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
443 * NULL on failure
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
444 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
445 cram_slice *cram_read_slice(cram_fd *fd);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
446
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
447
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
448
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
449 /**@}*/
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
450 /**@{ ----------------------------------------------------------------------
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
451 * CRAM file definition (header)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
452 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
453
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
454 /*! Reads a CRAM file definition structure.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
455 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
456 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
457 * Returns file_def ptr on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
458 * NULL on failure
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
459 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
460 cram_file_def *cram_read_file_def(cram_fd *fd);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
461
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
462 /*! Writes a cram_file_def structure to cram_fd.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
463 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
464 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
465 * Returns 0 on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
466 * -1 on failure
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
467 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
468 int cram_write_file_def(cram_fd *fd, cram_file_def *def);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
469
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
470 /*! Frees a cram_file_def structure. */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
471 void cram_free_file_def(cram_file_def *def);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
472
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
473
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
474 /**@}*/
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
475 /**@{ ----------------------------------------------------------------------
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
476 * SAM header I/O
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
477 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
478
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
479 /*! Reads the SAM header from the first CRAM data block.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
480 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
481 * Also performs minimal parsing to extract read-group
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
482 * and sample information.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
483 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
484 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
485 * Returns SAM hdr ptr on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
486 * NULL on failure
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
487 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
488 SAM_hdr *cram_read_SAM_hdr(cram_fd *fd);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
489
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
490 /*! Writes a CRAM SAM header.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
491 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
492 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
493 * Returns 0 on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
494 * -1 on failure
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
495 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
496 int cram_write_SAM_hdr(cram_fd *fd, SAM_hdr *hdr);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
497
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
498
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
499 /**@}*/
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
500 /**@{ ----------------------------------------------------------------------
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
501 * The top-level cram opening, closing and option handling
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
502 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
503
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
504 /*! Opens a CRAM file for read (mode "rb") or write ("wb").
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
505 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
506 * The filename may be "-" to indicate stdin or stdout.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
507 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
508 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
509 * Returns file handle on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
510 * NULL on failure.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
511 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
512 cram_fd *cram_open(const char *filename, const char *mode);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
513
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
514 /*! Opens an existing stream for reading or writing.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
515 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
516 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
517 * Returns file handle on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
518 * NULL on failure.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
519 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
520 cram_fd *cram_dopen(struct hFILE *fp, const char *filename, const char *mode);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
521
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
522 /*! Closes a CRAM file.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
523 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
524 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
525 * Returns 0 on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
526 * -1 on failure
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
527 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
528 int cram_close(cram_fd *fd);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
529
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
530 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
531 * Seek within a CRAM file.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
532 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
533 * Returns 0 on success
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
534 * -1 on failure
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
535 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
536 int cram_seek(cram_fd *fd, off_t offset, int whence);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
537
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
538 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
539 * Flushes a CRAM file.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
540 * Useful for when writing to stdout without wishing to close the stream.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
541 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
542 * Returns 0 on success
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
543 * -1 on failure
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
544 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
545 int cram_flush(cram_fd *fd);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
546
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
547 /*! Checks for end of file on a cram_fd stream.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
548 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
549 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
550 * Returns 0 if not at end of file
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
551 * 1 if we hit an expected EOF (end of range or EOF block)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
552 * 2 for other EOF (end of stream without EOF block)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
553 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
554 int cram_eof(cram_fd *fd);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
555
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
556 /*! Sets options on the cram_fd.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
557 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
558 * See CRAM_OPT_* definitions in cram_structs.h.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
559 * Use this immediately after opening.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
560 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
561 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
562 * Returns 0 on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
563 * -1 on failure
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
564 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
565 int cram_set_option(cram_fd *fd, enum cram_option opt, ...);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
566
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
567 /*! Sets options on the cram_fd.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
568 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
569 * See CRAM_OPT_* definitions in cram_structs.h.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
570 * Use this immediately after opening.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
571 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
572 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
573 * Returns 0 on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
574 * -1 on failure
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
575 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
576 int cram_set_voption(cram_fd *fd, enum cram_option opt, va_list args);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
577
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
578 /*!
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
579 * Attaches a header to a cram_fd.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
580 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
581 * This should be used when creating a new cram_fd for writing where
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
582 * we have an SAM_hdr already constructed (eg from a file we've read
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
583 * in).
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
584 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
585 * @return
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
586 * Returns 0 on success;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
587 * -1 on failure
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
588 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
589 int cram_set_header(cram_fd *fd, SAM_hdr *hdr);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
590
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
591
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
592 #ifdef __cplusplus
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
593 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
594 #endif
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
595
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
596 #endif /* _CRAM_IO_H_ */