NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hts.h
Go to the documentation of this file.
1 #ifndef HTS_H
2 #define HTS_H
3 
4 #include <stddef.h>
5 #include <stdint.h>
6 
7 #ifndef HTS_BGZF_TYPEDEF
8 typedef struct BGZF BGZF;
9 #define HTS_BGZF_TYPEDEF
10 #endif
11 struct cram_fd;
12 struct hFILE;
13 
14 #ifndef KSTRING_T
15 #define KSTRING_T kstring_t
16 typedef struct __kstring_t {
17  size_t l, m;
18  char *s;
19 } kstring_t;
20 #endif
21 
22 #ifndef kroundup32
23 #define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
24 #endif
25 
33 #define hts_expand(type_t, n, m, ptr) if ((n) > (m)) { \
34  (m) = (n); kroundup32(m); \
35  (ptr) = (type_t*)realloc((ptr), (m) * sizeof(type_t)); \
36  }
37 #define hts_expand0(type_t, n, m, ptr) if ((n) > (m)) { \
38  int t = (m); (m) = (n); kroundup32(m); \
39  (ptr) = (type_t*)realloc((ptr), (m) * sizeof(type_t)); \
40  memset(((type_t*)ptr)+t,0,sizeof(type_t)*((m)-t)); \
41  }
42 
43 /************
44  * File I/O *
45  ************/
46 
47 typedef struct {
48  uint32_t is_bin:1, is_write:1, is_be:1, is_cram:1, is_compressed:2, is_kstream:1, dummy:25;
51  char *fn, *fn_aux;
52  union {
54  struct cram_fd *cram;
55  struct hFILE *hfile;
56  void *voidp;
57  } fp;
58 } htsFile;
59 
60 /**********************
61  * Exported functions *
62  **********************/
63 
64 extern int hts_verbose;
65 
67 extern const unsigned char seq_nt16_table[256];
68 
70 extern const char seq_nt16_str[];
71 
72 #ifdef __cplusplus
73 extern "C" {
74 #endif
75 
81 const char *hts_version(void);
82 
106 htsFile *hts_open(const char *fn, const char *mode);
107 
113 int hts_close(htsFile *fp);
114 
115 int hts_getline(htsFile *fp, int delimiter, kstring_t *str);
116 char **hts_readlines(const char *fn, int *_n);
125 char **hts_readlist(const char *fn, int is_file, int *_n);
126 
134 int hts_set_threads(htsFile *fp, int n);
135 
143 int hts_set_fai_filename(htsFile *fp, const char *fn_aux);
144 
145 #ifdef __cplusplus
146 }
147 #endif
148 
149 /************
150  * Indexing *
151  ************/
152 
163 #define HTS_IDX_NOCOOR (-2)
164 #define HTS_IDX_START (-3)
165 #define HTS_IDX_REST (-4)
166 #define HTS_IDX_NONE (-5)
167 
168 #define HTS_FMT_CSI 0
169 #define HTS_FMT_BAI 1
170 #define HTS_FMT_TBI 2
171 #define HTS_FMT_CRAI 3
172 
173 struct __hts_idx_t;
174 typedef struct __hts_idx_t hts_idx_t;
175 
176 typedef struct {
178 } hts_pair64_t;
179 
180 typedef int hts_readrec_func(BGZF *fp, void *data, void *r, int *tid, int *beg, int *end);
181 
182 typedef struct {
183  uint32_t read_rest:1, finished:1, dummy:29;
184  int tid, beg, end, n_off, i;
188  struct {
189  int n, m;
190  int *a;
191  } bins;
192 } hts_itr_t;
193 
194 #ifdef __cplusplus
195 extern "C" {
196 #endif
197 
198  #define hts_bin_first(l) (((1<<(((l)<<1) + (l))) - 1) / 7)
199  #define hts_bin_parent(l) (((l) - 1) >> 3)
200 
201  hts_idx_t *hts_idx_init(int n, int fmt, uint64_t offset0, int min_shift, int n_lvls);
202  void hts_idx_destroy(hts_idx_t *idx);
203  int hts_idx_push(hts_idx_t *idx, int tid, int beg, int end, uint64_t offset, int is_mapped);
204  void hts_idx_finish(hts_idx_t *idx, uint64_t final_offset);
205 
206  void hts_idx_save(const hts_idx_t *idx, const char *fn, int fmt);
207  hts_idx_t *hts_idx_load(const char *fn, int fmt);
208 
209  uint8_t *hts_idx_get_meta(hts_idx_t *idx, int *l_meta);
210  void hts_idx_set_meta(hts_idx_t *idx, int l_meta, uint8_t *meta, int is_copy);
211 
212  int hts_idx_get_stat(const hts_idx_t* idx, int tid, uint64_t* mapped, uint64_t* unmapped);
214 
215  const char *hts_parse_reg(const char *s, int *beg, int *end);
216  hts_itr_t *hts_itr_query(const hts_idx_t *idx, int tid, int beg, int end, hts_readrec_func *readrec);
217  void hts_itr_destroy(hts_itr_t *iter);
218 
219  typedef int (*hts_name2id_f)(void*, const char*);
220  typedef const char *(*hts_id2name_f)(void*, int);
221  typedef hts_itr_t *hts_itr_query_func(const hts_idx_t *idx, int tid, int beg, int end, hts_readrec_func *readrec);
222 
223  hts_itr_t *hts_itr_querys(const hts_idx_t *idx, const char *reg, hts_name2id_f getid, void *hdr, hts_itr_query_func *itr_query, hts_readrec_func *readrec);
224  int hts_itr_next(BGZF *fp, hts_itr_t *iter, void *r, void *data);
225  const char **hts_idx_seqnames(const hts_idx_t *idx, int *n, hts_id2name_f getid, void *hdr); // free only the array, not the values
226 
236  #define FT_UNKN 0
237  #define FT_GZ 1
238  #define FT_VCF 2
239  #define FT_VCF_GZ (FT_GZ|FT_VCF)
240  #define FT_BCF (1<<2)
241  #define FT_BCF_GZ (FT_GZ|FT_BCF)
242  #define FT_STDIN (1<<3)
243  int hts_file_type(const char *fname);
244 
245 
246 #ifdef __cplusplus
247 }
248 #endif
249 
250 static inline int hts_reg2bin(int64_t beg, int64_t end, int min_shift, int n_lvls)
251 {
252  int l, s = min_shift, t = ((1<<((n_lvls<<1) + n_lvls)) - 1) / 7;
253  for (--end, l = n_lvls; l > 0; --l, s += 3, t -= 1<<((l<<1)+l))
254  if (beg>>s == end>>s) return t + (beg>>s);
255  return 0;
256 }
257 
258 static inline int hts_bin_bot(int bin, int n_lvls)
259 {
260  int l, b;
261  for (l = 0, b = bin; b; ++l, b = hts_bin_parent(b)); // compute the level of bin
262  return (bin - hts_bin_first(l)) << (n_lvls - l) * 3;
263 }
264 
265 /**************
266  * Endianness *
267  **************/
268 
269 static inline int ed_is_big(void)
270 {
271  long one= 1;
272  return !(*((char *)(&one)));
273 }
274 static inline uint16_t ed_swap_2(uint16_t v)
275 {
276  return (uint16_t)(((v & 0x00FF00FFU) << 8) | ((v & 0xFF00FF00U) >> 8));
277 }
278 static inline void *ed_swap_2p(void *x)
279 {
280  *(uint16_t*)x = ed_swap_2(*(uint16_t*)x);
281  return x;
282 }
283 static inline uint32_t ed_swap_4(uint32_t v)
284 {
285  v = ((v & 0x0000FFFFU) << 16) | (v >> 16);
286  return ((v & 0x00FF00FFU) << 8) | ((v & 0xFF00FF00U) >> 8);
287 }
288 static inline void *ed_swap_4p(void *x)
289 {
290  *(uint32_t*)x = ed_swap_4(*(uint32_t*)x);
291  return x;
292 }
293 static inline uint64_t ed_swap_8(uint64_t v)
294 {
295  v = ((v & 0x00000000FFFFFFFFLLU) << 32) | (v >> 32);
296  v = ((v & 0x0000FFFF0000FFFFLLU) << 16) | ((v & 0xFFFF0000FFFF0000LLU) >> 16);
297  return ((v & 0x00FF00FF00FF00FFLLU) << 8) | ((v & 0xFF00FF00FF00FF00LLU) >> 8);
298 }
299 static inline void *ed_swap_8p(void *x)
300 {
301  *(uint64_t*)x = ed_swap_8(*(uint64_t*)x);
302  return x;
303 }
304 
305 #endif