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