1 /// @file htslib/regidx.h 2 /// Region indexing. 3 /* 4 Copyright (C) 2014-2019 Genome Research Ltd. 5 6 Author: Petr Danecek <pd3@sanger.ac.uk> 7 8 Permission is hereby granted, free of charge, to any person obtaining a copy 9 of this software and associated documentation files (the "Software"), to deal 10 in the Software without restriction, including without limitation the rights 11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 copies of the Software, and to permit persons to whom the Software is 13 furnished to do so, subject to the following conditions: 14 15 The above copyright notice and this permission notice shall be included in 16 all copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 THE SOFTWARE. 25 */ 26 27 /* 28 Region indexing with an optional payload. 29 30 Example of usage: 31 32 // Init the parser and print regions. In this example the payload is a 33 // pointer to a string. For the description of parse_custom and 34 // free_custom functions, see regidx_parse_f and regidx_free_f below, 35 // and for working example see test/test-regidx.c. 36 regidx_t *idx = regidx_init(in_fname,parse_custom,free_custom,sizeof(char*),NULL); 37 38 // Query overlap with chr:beg-end (beg,end are 1-based coordinates) 39 regitr_t *itr = regitr_init(idx); 40 if ( regidx_overlap(idx, chr,beg-1,end-1, itr) ) printf("There is an overlap!\n"); 41 42 while ( regitr_overlap(itr) ) 43 { 44 printf("[%"PRIhts_pos",%"PRIhts_pos"] overlaps with [%"PRIhts_pos",%"PRIhts_pos"], payload=%s\n", 45 beg, end, itr->beg+1, itr->end+1, regitr_payload(itr,char*)); 46 } 47 48 regidx_destroy(idx); 49 regitr_destroy(itr); 50 51 52 Another example, loop over all regions: 53 54 regidx_t *idx = regidx_init(in_fname,NULL,NULL,0,NULL); 55 regitr_t *itr = regitr_init(idx); 56 57 while ( regitr_loop(itr) ) 58 printf("chr=%s beg=%d end=%d\n", itr->seq, itr->beg+1, itr->end+1); 59 60 regidx_destroy(idx); 61 regitr_destroy(itr); 62 */ 63 64 #ifndef HTSLIB_REGIDX_H 65 #define HTSLIB_REGIDX_H 66 67 #include "hts.h" 68 69 #ifdef __cplusplus 70 extern "C" { 71 #endif 72 73 // maximum regidx position (0-based). Used to represent the end point of 74 // regions which do not explicitly set one. regidx_push() also limits 75 // positions passed to it to be no bigger than this. 76 77 // Limit is set to ensure some internal values used by regidx keep within 32 78 // bits and to stop the index from getting too big. 79 80 #define REGIDX_MAX (1ULL << 35) 81 82 typedef struct regidx_t regidx_t; 83 typedef struct regitr_t 84 { 85 hts_pos_t beg,end; 86 void *payload; 87 char *seq; 88 void *itr; 89 } 90 regitr_t; 91 92 #define regitr_payload(itr,type_t) (*((type_t*)(itr)->payload)) 93 94 // Old API for backwards compatibility 95 #define REGITR_START(itr) (itr).beg 96 #define REGITR_END(itr) (itr).end 97 #define REGITR_PAYLOAD(itr,type_t) ((type_t*)(itr).payload) 98 #define REGITR_OVERLAP(itr,from,to) regidx_overlap((itr)); 99 100 /* 101 * regidx_parse_f - Function to parse one input line, such as regidx_parse_bed 102 * or regidx_parse_tab below. The function is expected to set `chr_from` and 103 * `chr_to` to point to first and last character of chromosome name and set 104 * coordinates `beg` and `end` (0-based, inclusive). If regidx_init() was 105 * called with non-zero payload_size, the `payload` points to a memory 106 * location of the payload_size and `usr` is the data passed to regidx_init(). 107 * Any memory allocated by the function will be freed by regidx_free_f called 108 * by regidx_destroy(). 109 * 110 * Return value: 0 on success, -1 to skip a record, -2 on fatal error. 111 */ 112 typedef int (*regidx_parse_f)(const char *line, char **chr_beg, char **chr_end, hts_pos_t *beg, hts_pos_t *end, void *payload, void *usr); 113 typedef void (*regidx_free_f)(void *payload); 114 115 /* 116 * A note about the parsers: 117 * - leading spaces are ignored 118 * - lines starting with "#" are ignored 119 */ 120 HTSLIB_EXPORT 121 int regidx_parse_bed(const char*,char**,char**,hts_pos_t*,hts_pos_t*,void*,void*); // CHROM or whitespace-sepatated CHROM,FROM,TO (0-based,right-open) 122 HTSLIB_EXPORT 123 int regidx_parse_tab(const char*,char**,char**,hts_pos_t*,hts_pos_t*,void*,void*); // CHROM or whitespace-separated CHROM,POS (1-based, inclusive) 124 HTSLIB_EXPORT 125 int regidx_parse_reg(const char*,char**,char**,hts_pos_t*,hts_pos_t*,void*,void*); // CHROM, CHROM:POS, CHROM:FROM-TO, CHROM:FROM- (1-based, inclusive) 126 HTSLIB_EXPORT 127 int regidx_parse_vcf(const char*,char**,char**,hts_pos_t*,hts_pos_t*,void*,void*); 128 129 /* 130 * regidx_init() - creates new index 131 * regidx_init_string() - creates new index, from a string rather than from a file 132 * 133 * @param fname: input file name or NULL if regions will be added one-by-one via regidx_insert() 134 * @param parsef: regidx_parse_bed, regidx_parse_tab or see description of regidx_parse_f. If NULL, 135 * the format will be autodected, currently either regidx_parse_tab (the default) or 136 * regidx_parse_bed (file must be named 'bed' or 'bed.gz') will be used. Note that 137 * the exact autodetection algorithm will change. 138 * @param freef: NULL or see description of regidx_parse_f 139 * @param payload_size: 0 with regidx_parse_bed, regidx_parse_tab or see regidx_parse_f 140 * @param usr: optional user data passed to regidx_parse_f 141 * 142 * Returns index on success or NULL on error. 143 * 144 * The regidx_t index struct returned by a successful call should be freed 145 * via regidx_destroy() when it is no longer needed. 146 */ 147 HTSLIB_EXPORT 148 regidx_t *regidx_init(const char *fname, regidx_parse_f parsef, regidx_free_f freef, size_t payload_size, void *usr); 149 HTSLIB_EXPORT 150 regidx_t *regidx_init_string(const char *string, regidx_parse_f parsef, regidx_free_f freef, size_t payload_size, void *usr); 151 152 /* 153 * regidx_destroy() - free memory allocated by regidx_init 154 */ 155 HTSLIB_EXPORT 156 void regidx_destroy(regidx_t *idx); 157 158 /* 159 * regidx_overlap() - check overlap of the location chr:from-to with regions 160 * @param beg,end: 0-based start, end coordinate (inclusive) 161 * @param itr: pointer to iterator, can be NULL if regidx_loop not needed 162 * 163 * Returns 0 if there is no overlap or 1 if overlap is found. The overlapping 164 * regions can be iterated as shown in the example above. 165 */ 166 HTSLIB_EXPORT 167 int regidx_overlap(regidx_t *idx, const char *chr, hts_pos_t beg, hts_pos_t end, regitr_t *itr); 168 169 /* 170 * regidx_insert() - add a new region. 171 * regidx_insert_list() - add new regions from a list 172 * regidx_push() - low level insertion of a new region 173 * 174 * Returns 0 on success or -1 on error. 175 */ 176 HTSLIB_EXPORT 177 int regidx_insert(regidx_t *idx, char *line); 178 HTSLIB_EXPORT 179 int regidx_insert_list(regidx_t *idx, char *line, char delim); 180 HTSLIB_EXPORT 181 int regidx_push(regidx_t *idx, char *chr_beg, char *chr_end, hts_pos_t beg, hts_pos_t end, void *payload); 182 183 /* 184 * regidx_seq_names() - return list of all sequence names 185 */ 186 HTSLIB_EXPORT 187 char **regidx_seq_names(regidx_t *idx, int *n); 188 189 /* 190 * regidx_seq_nregs() - number of regions 191 * regidx_nregs() - total number of regions 192 */ 193 HTSLIB_EXPORT 194 int regidx_seq_nregs(regidx_t *idx, const char *seq); 195 196 HTSLIB_EXPORT 197 int regidx_nregs(regidx_t *idx); 198 199 /* 200 * regitr_init() - initialize an iterator. The idx parameter is required only 201 * with regitr_loop. If only regitr_overlap is called, NULL 202 * can be given. 203 * 204 * The regitr_t struct returned by a successful regitr_init() 205 * call should be freed via regitr_destroy() when it is no 206 * longer needed. 207 * 208 * regitr_reset() - initialize an iterator for a repeated regitr_loop cycle. 209 * Not required with regitr_overlap. 210 */ 211 HTSLIB_EXPORT 212 regitr_t *regitr_init(regidx_t *idx); 213 HTSLIB_EXPORT 214 void regitr_destroy(regitr_t *itr); 215 HTSLIB_EXPORT 216 void regitr_reset(regidx_t *idx, regitr_t *itr); 217 218 /* 219 * regitr_overlap() - next overlapping region 220 * Returns 0 when done or 1 when itr is set to next region 221 */ 222 HTSLIB_EXPORT 223 int regitr_overlap(regitr_t *itr); 224 225 /* 226 * regitr_loop() - loop over all regions 227 * Returns 0 when done or 1 when itr is set to next region 228 */ 229 HTSLIB_EXPORT 230 int regitr_loop(regitr_t *itr); 231 232 /* 233 * regitr_copy() - create a copy of an iterator for a repeated iteration with regitr_loop 234 */ 235 HTSLIB_EXPORT 236 void regitr_copy(regitr_t *dst, regitr_t *src); 237 238 #ifdef __cplusplus 239 } 240 #endif 241 242 #endif 243