1 /// @file htslib/tbx.h
2 /// Tabix API functions.
3 /*
4     Copyright (C) 2009, 2012-2015, 2019 Genome Research Ltd.
5     Copyright (C) 2010, 2012 Broad Institute.
6 
7     Author: Heng Li <lh3@sanger.ac.uk>
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.  */
26 module htslib.tbx;
27 
28 import htslib.hts;
29 import htslib.bgzf : BGZF;
30 
31 @system:
32 nothrow:
33 @nogc:
34 
35 extern (C):
36 
37 enum TBX_MAX_SHIFT = 31;
38 
39 enum TBX_GENERIC = 0;
40 enum TBX_SAM = 1;
41 enum TBX_VCF = 2;
42 enum TBX_UCSC = 0x10000;
43 
44 struct tbx_conf_t
45 {
46     int preset;
47     int sc;
48     int bc;
49     int ec; // seq col., beg col. and end col.
50     int meta_char;
51     int line_skip;
52 }
53 
54 struct tbx_t
55 {
56     tbx_conf_t conf;
57     hts_idx_t* idx;
58     void* dict;
59 }
60 
61 extern __gshared const tbx_conf_t tbx_conf_gff;
62 extern __gshared const tbx_conf_t tbx_conf_bed;
63 extern __gshared const tbx_conf_t tbx_conf_psltbl;
64 extern __gshared const tbx_conf_t tbx_conf_sam;
65 extern __gshared const tbx_conf_t tbx_conf_vcf;
66 
67 alias tbx_itr_destroy = hts_itr_destroy;
68 
69 pragma(inline, true)
70 auto tbx_itr_queryi(const tbx_t *tbx, int tid, hts_pos_t beg, hts_pos_t end)
71     { return hts_itr_query(tbx.idx, tid, beg, end, &tbx_readrec); }
72 
73 pragma(inline, true)
74 auto tbx_itr_querys(const tbx_t *tbx, const char *s)
75 {
76     return hts_itr_querys(tbx.idx, s,
77         cast(hts_name2id_f)(&tbx_name2id),
78         cast(void*)tbx,
79         &hts_itr_query,
80         &tbx_readrec);
81 }
82 
83 pragma(inline, true)
84 auto tbx_itr_next(htsFile *htsfp, tbx_t *tbx, hts_itr_t *itr, void *r)
85     { return hts_itr_next(hts_get_bgzfp(htsfp), itr, r, tbx); }
86 
87 pragma(inline, true)
88 auto tbx_bgzf_itr_next(BGZF *bgzfp, tbx_t *tbx, hts_itr_t *itr, void *r)
89     { return hts_itr_next(bgzfp, itr, r, tbx); }
90 
91 // contig name to integer id
92 int tbx_name2id(tbx_t* tbx, const(char)* ss);
93 
94 /* Internal helper function used by tbx_itr_next() defined in hts.c -- do not use directly*/
95 BGZF* hts_get_bgzfp(htsFile* fp);
96 
97 int tbx_readrec(
98     BGZF* fp,
99     void* tbxv,
100     void* sv,
101     int* tid,
102     hts_pos_t* beg,
103     hts_pos_t* end);
104 
105 /// Build an index of the lines in a BGZF-compressed file
106 /** The index struct returned by a successful call should be freed
107     via tbx_destroy() when it is no longer needed.
108 */
109 tbx_t* tbx_index(BGZF* fp, int min_shift, const(tbx_conf_t)* conf);
110 /*
111  * All tbx_index_build* methods return: 0 (success), -1 (general failure) or -2 (compression not BGZF)
112  */
113 int tbx_index_build(const(char)* fn, int min_shift, const(tbx_conf_t)* conf);
114 
115 int tbx_index_build2(
116     const(char)* fn,
117     const(char)* fnidx,
118     int min_shift,
119     const(tbx_conf_t)* conf);
120 
121 int tbx_index_build3(
122     const(char)* fn,
123     const(char)* fnidx,
124     int min_shift,
125     int n_threads,
126     const(tbx_conf_t)* conf);
127 
128 /// Load or stream a .tbi or .csi index
129 /** @param fn     Name of the data file corresponding to the index
130 
131     Equivalent to tbx_index_load3(fn, NULL, HTS_IDX_SAVE_REMOTE);
132 */
133 tbx_t* tbx_index_load(const(char)* fn);
134 
135 /// Load or stream a .tbi or .csi index
136 /** @param fn     Name of the data file corresponding to the index
137     @param fnidx  Name of the indexed file
138     @return The index, or NULL if an error occurred
139 
140     If @p fnidx is NULL, the index name will be derived from @p fn.
141 
142     Equivalent to tbx_index_load3(fn, fnidx, HTS_IDX_SAVE_REMOTE);
143 */
144 tbx_t* tbx_index_load2(const(char)* fn, const(char)* fnidx);
145 
146 /// Load or stream a .tbi or .csi index
147 /** @param fn     Name of the data file corresponding to the index
148     @param fnidx  Name of the indexed file
149     @param flags  Flags to alter behaviour (see description)
150     @return The index, or NULL if an error occurred
151 
152     If @p fnidx is NULL, the index name will be derived from @p fn.
153 
154     The @p flags parameter can be set to a combination of the following
155     values:
156 
157         HTS_IDX_SAVE_REMOTE   Save a local copy of any remote indexes
158         HTS_IDX_SILENT_FAIL   Fail silently if the index is not present
159 
160     The index struct returned by a successful call should be freed
161     via tbx_destroy() when it is no longer needed.
162 */
163 tbx_t* tbx_index_load3(const(char)* fn, const(char)* fnidx, int flags);
164 
165 /// return C-style array of sequence names (NB: free the array but not the values)
166 const(char*)* tbx_seqnames(tbx_t* tbx, int* n); // free the array but not the values
167 
168 /// destroy/dealloc tabix data
169 void tbx_destroy(tbx_t* tbx);
170