Changelog entry marking the package released.
[tabix.git] / tabix.h
1 /* The MIT License
2
3    Copyright (c) 2009 Genome Research Ltd (GRL), 2010 Broad Institute
4
5    Permission is hereby granted, free of charge, to any person obtaining
6    a copy of this software and associated documentation files (the
7    "Software"), to deal in the Software without restriction, including
8    without limitation the rights to use, copy, modify, merge, publish,
9    distribute, sublicense, and/or sell copies of the Software, and to
10    permit persons to whom the Software is furnished to do so, subject to
11    the following conditions:
12
13    The above copyright notice and this permission notice shall be
14    included in all copies or substantial portions of the Software.
15
16    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20    BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21    ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22    CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23    SOFTWARE.
24 */
25
26 /* Contact: Heng Li <lh3@live.co.uk> */
27
28 #ifndef __TABIDX_H
29 #define __TABIDX_H
30
31 #include <stdint.h>
32 #include "kstring.h"
33 #include "bgzf.h"
34
35 #define TI_PRESET_GENERIC 0
36 #define TI_PRESET_SAM     1
37 #define TI_PRESET_VCF     2
38
39 #define TI_FLAG_UCSC      0x10000
40
41 typedef int (*ti_fetch_f)(int l, const char *s, void *data);
42
43 struct __ti_index_t;
44 typedef struct __ti_index_t ti_index_t;
45
46 struct __ti_iter_t;
47 typedef struct __ti_iter_t *ti_iter_t;
48
49 typedef struct {
50         BGZF *fp;
51         ti_index_t *idx;
52         char *fn, *fnidx;
53 } tabix_t;
54
55 typedef struct {
56         int32_t preset;
57         int32_t sc, bc, ec; // seq col., beg col. and end col.
58         int32_t meta_char, line_skip;
59 } ti_conf_t;
60
61 typedef struct {
62         int beg, end;
63         char *ss, *se;
64 } ti_interval_t;
65
66 extern ti_conf_t ti_conf_gff, ti_conf_bed, ti_conf_psltbl, ti_conf_vcf, ti_conf_sam; // preset
67
68 #ifdef __cplusplus
69 extern "C" {
70 #endif
71
72         /*******************
73          * High-level APIs *
74          *******************/
75
76         tabix_t *ti_open(const char *fn, const char *fnidx);
77         int ti_lazy_index_load(tabix_t *t);
78         void ti_close(tabix_t *t);
79         ti_iter_t ti_query(tabix_t *t, const char *name, int beg, int end);
80         ti_iter_t ti_queryi(tabix_t *t, int tid, int beg, int end);
81         ti_iter_t ti_querys(tabix_t *t, const char *reg);
82         const char *ti_read(tabix_t *t, ti_iter_t iter, int *len);
83
84         /* Destroy the iterator */
85         void ti_iter_destroy(ti_iter_t iter);
86
87         /* Get the list of sequence names. Each "char*" pointer points to a
88          * internal member of the index, so DO NOT modify the returned
89          * pointer; otherwise the index will be corrupted. The returned
90          * pointer should be freed by a single free() call by the routine
91          * calling this function. The number of sequences is returned at *n. */
92         const char **ti_seqname(const ti_index_t *idx, int *n);
93
94         /******************
95          * Low-level APIs *
96          ******************/
97
98         /* Build the index for file <fn>. File <fn>.tbi will be generated
99          * and overwrite the file of the same name. Return -1 on failure. */
100         int ti_index_build(const char *fn, const ti_conf_t *conf);
101
102         /* Load the index from file <fn>.tbi. If <fn> is a URL and the index
103          * file is not in the working directory, <fn>.tbi will be
104          * downloaded. Return NULL on failure. */
105         ti_index_t *ti_index_load(const char *fn);
106
107         ti_index_t *ti_index_load_local(const char *fnidx);
108
109         /* Destroy the index */
110         void ti_index_destroy(ti_index_t *idx);
111
112         /* Parse a region like: chr2, chr2:100, chr2:100-200. Return -1 on failure. */
113         int ti_parse_region(const ti_index_t *idx, const char *str, int *tid, int *begin, int *end);
114
115         int ti_get_tid(const ti_index_t *idx, const char *name);
116
117         /* Get the iterator pointing to the first record at the current file
118          * position. If the file is just openned, the iterator points to the
119          * first record in the file. */
120         ti_iter_t ti_iter_first(void);
121
122         /* Get the iterator pointing to the first record in region tid:beg-end */
123         ti_iter_t ti_iter_query(const ti_index_t *idx, int tid, int beg, int end);
124
125         /* Get the data line pointed by the iterator and iterate to the next record. */
126         const char *ti_iter_read(BGZF *fp, ti_iter_t iter, int *len);
127
128         const ti_conf_t *ti_get_conf(ti_index_t *idx);
129         int ti_get_intv(const ti_conf_t *conf, int len, char *line, ti_interval_t *intv);
130
131         /*******************
132          * Deprecated APIs *
133          *******************/
134
135         /* The callback version for random access */
136         int ti_fetch(BGZF *fp, const ti_index_t *idx, int tid, int beg, int end, void *data, ti_fetch_f func);
137
138         /* Read one line. */
139         int ti_readline(BGZF *fp, kstring_t *str);
140
141 #ifdef __cplusplus
142 }
143 #endif
144
145 #endif