1 /** htslib-1.9 regidx.h as D module 2 * 3 * Changes include: 4 *$(LIST 5 * Removed if(n)defs 6 * Changed ^typedef struct {...} <name>$ to ^struct <name> {...}$ 7 * Commented out the REGITR_{START,END,PAYLOAD,OVERLAP} #define macros 8 * aliased typedef'd function pointers 9 *) 10 */ 11 module dhtslib.htslib.regidx; 12 13 import std.stdint : uint32_t; 14 15 extern (C): 16 /// @file htslib/regidx.h 17 /// Region indexing. 18 /* 19 Copyright (C) 2014 Genome Research Ltd. 20 21 Author: Petr Danecek <pd3@sanger.ac.uk> 22 23 Permission is hereby granted, free of charge, to any person obtaining a copy 24 of this software and associated documentation files (the "Software"), to deal 25 in the Software without restriction, including without limitation the rights 26 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 27 copies of the Software, and to permit persons to whom the Software is 28 furnished to do so, subject to the following conditions: 29 30 The above copyright notice and this permission notice shall be included in 31 all copies or substantial portions of the Software. 32 33 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 34 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 35 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 36 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 37 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 38 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 39 THE SOFTWARE. 40 */ 41 42 /* 43 Regions indexing with an optional payload. Inspired by samtools/bedidx.c. 44 This code is intended as future replacement of bcf_sr_regions_t. 45 46 Example of usage: 47 48 // Init the parser and print regions. In this example the payload is a 49 // pointer to a string. For the description of parse_custom and 50 // free_custom functions, see regidx_parse_f and regidx_free_f below, 51 // and for working example see test/test-regidx.c. 52 regidx_t *idx = regidx_init(in_fname,parse_custom,free_custom,sizeof(char*),NULL); 53 54 // Query overlap with chr:from-to 55 regitr_t itr; 56 if ( regidx_overlap(idx, chr,from,to, &itr) ) printf("There is an overlap!\n"); 57 58 while ( REGITR_OVERLAP(itr,from,to) ) 59 { 60 printf("[%d,%d] overlaps with [%d,%d], payload=%s\n", from,to, 61 REGITR_START(itr), REGITR_END(itr), REGITR_PAYLOAD(itr,char*)); 62 itr.i++; 63 } 64 65 regidx_destroy(regs); 66 */ 67 68 /// struct defined in regidx.c, we will leave as opaque 69 extern struct _regidx_t; // @suppress(dscanner.style.phobos_naming_convention) 70 alias regidx_t = _regidx_t; 71 /// region (start, end) 72 struct reg_t // @suppress(dscanner.style.phobos_naming_convention) 73 { 74 /// (start, end) -- unclear if 0-based/open 75 uint32_t start, end; 76 } 77 /// region iterator 78 struct regitr_t // @suppress(dscanner.style.phobos_naming_convention) 79 { 80 /// ??? 81 int i, n; 82 reg_t *reg; /// ??? 83 void *payload; /// ??? 84 } 85 86 /+ 87 #define REGITR_START(itr) (itr).reg[(itr).i].start 88 #define REGITR_END(itr) (itr).reg[(itr).i].end 89 #define REGITR_PAYLOAD(itr,type_t) ((type_t*)(itr).payload)[(itr).i] 90 #define REGITR_OVERLAP(itr,from,to) (itr.i < itr.n && REGITR_START(itr)<=to && REGITR_END(itr)>=from ) 91 +/ 92 pragma(inline, true) 93 { 94 /// Get the start or end coordinate of the region iterator's current region 95 uint32_t regitr_start(regitr_t *itr){ return itr.reg[itr.i].start; } 96 /// ditto 97 uint32_t regitr_end(regitr_t *itr) { return itr.reg[itr.i].end; } 98 99 /// Get the payload of the region iterator's current region 100 auto regitr_payload(T)(regitr_t *itr) { return cast(T*)itr.payload[itr.i]; } // looks super unsafe 101 102 /// Does the given (from, to) overlap the region? 103 auto regitr_overlap(T)(regitr_t *itr, T from, T to) 104 { 105 return itr.i < itr.n && regitr_start(itr) <= to && regitr_end(itr) >= from; 106 } 107 } 108 109 /* 110 * regidx_parse_f - Function to parse one input line, such as regidx_parse_bed 111 * or regidx_parse_tab below. The function is expected to set `chr_from` and 112 * `chr_to` to point to first and last character of chromosome name and set 113 * coordinates `reg->start` and `reg->end` (0-based, inclusive). If 114 * regidx_init() was called with non-zero payload_size, the `payload` points 115 * to a memory location of the payload_size and `usr` is data passed to 116 * regidx_init(). Any memory allocated by the function will be freed by 117 * regidx_free_f on regidx_destroy(). 118 * 119 * Return value: 0 on success, -1 to skip a record, -2 on fatal error. 120 */ 121 //typedef int (*regidx_parse_f)(const char *line, char **chr_beg, char **chr_end, reg_t *reg, void *payload, void *usr); 122 alias regidx_parse_f = 123 int function(const(char) *line, char **chr_beg, char **chr_end, reg_t *reg, void *payload, void *usr); 124 //typedef void (*regidx_free_f)(void *payload); 125 alias regidx_free_f = void function(void *payload); 126 127 /// regidx_parse_f for BED, CHROM,FROM,TO (0-based,right-open) 128 int regidx_parse_bed(const(char)*, char**, char**, reg_t*, void*, void*); // CHROM,FROM,TO (0-based,right-open) 129 /// regidx_Parse_f for "TAB", CHROM,POS (1-based, inclusive) 130 int regidx_parse_tab(const(char)*, char**, char**, reg_t*, void*, void*); // CHROM,POS (1-based, inclusive) 131 132 /** 133 * regidx_init() - creates new index 134 * @param fname: input file name or NULL if regions will be added one-by-one via regidx_insert() 135 * @param parsef: regidx_parse_bed, regidx_parse_tab or see description of regidx_parse_f. If NULL, 136 * the format will be autodected, currently either regidx_parse_tab (the default) or 137 * regidx_parse_bed (file must be named 'bed' or 'bed.gz') will be used. Note that 138 * the exact autodetection algorithm will change. 139 * @param freef: NULL or see description of regidx_parse_f 140 * @param payload_size: 0 with regidx_parse_bed, regidx_parse_tab or see regidx_parse_f 141 * @param usr: optional user data passed to regidx_parse_f 142 * 143 * Returns index on success or NULL on error. 144 */ 145 regidx_t *regidx_init(const(char) *fname, regidx_parse_f parsef, regidx_free_f freef, size_t payload_size, void *usr); 146 147 /** 148 * regidx_destroy() - free memory allocated by regidx_init 149 */ 150 void regidx_destroy(regidx_t *idx); 151 152 /** 153 * regidx_overlap() - check overlap of the location chr:from-to with regions 154 * @param start,end: 0-based start, end coordinate (inclusive) 155 * @param itr: pointer to iterator, can be NULL if not needed 156 * 157 * Returns 0 if there is no overlap or 1 if overlap is found. The overlapping 158 * regions can be iterated as shown in the example above. 159 */ 160 int regidx_overlap(regidx_t *idx, const(char) *chr, uint32_t start, uint32_t end, regitr_t *itr); 161 162 /** 163 * regidx_insert() - add a new region. 164 * 165 * After last region has been added, call regidx_insert(idx,NULL) to 166 * build the index. 167 * 168 * Returns 0 on success or -1 on error. 169 */ 170 int regidx_insert(regidx_t *idx, char *line); 171 172 /** 173 * regidx_seq_names() - return list of all sequence names 174 */ 175 char **regidx_seq_names(regidx_t *idx, int *n); 176 177 /** 178 * regidx_seq_nregs() - number of regions 179 * regidx_nregs() - total number of regions 180 */ 181 int regidx_seq_nregs(regidx_t *idx, const(char) *seq); 182 /// ditto 183 int regidx_nregs(regidx_t *idx);