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