| 1 | /* |
| 2 | Copyright (C) 2014-2016, 2018 Genome Research Ltd. |
| 3 | |
| 4 | Author: Petr Danecek <[email protected]> |
| 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 | /* Avoid conflicts with HTSlib's regidx_* functions. */ |
| 69 | #define regidx_destroy bcftools_regidx_destroy |
| 70 | #define regidx_init bcftools_regidx_init |
| 71 | #define regidx_init_string bcftools_regidx_init_string |
| 72 | #define regidx_insert bcftools_regidx_insert |
| 73 | #define regidx_insert_list bcftools_regidx_insert_list |
| 74 | #define regidx_nregs bcftools_regidx_nregs |
| 75 | #define regidx_overlap bcftools_regidx_overlap |
| 76 | #define regidx_parse_bed bcftools_regidx_parse_bed |
| 77 | #define regidx_parse_reg bcftools_regidx_parse_reg |
| 78 | #define regidx_parse_tab bcftools_regidx_parse_tab |
| 79 | #define regidx_parse_vcf bcftools_regidx_parse_vcf |
| 80 | #define regidx_push bcftools_regidx_push |
| 81 | #define regidx_seq_names bcftools_regidx_seq_names |
| 82 | #define regidx_seq_nregs bcftools_regidx_seq_nregs |
| 83 | #define regitr_copy bcftools_regitr_copy |
| 84 | #define regitr_destroy bcftools_regitr_destroy |
| 85 | #define regitr_init bcftools_regitr_init |
| 86 | #define regitr_loop bcftools_regitr_loop |
| 87 | #define regitr_overlap bcftools_regitr_overlap |
| 88 | #define regitr_reset bcftools_regitr_reset |
| 89 | |
| 90 | #ifdef __cplusplus |
| 91 | extern "C" { |
| 92 | #endif |
| 93 | |
| 94 | #define REGIDX_MAX 2147483646 // maximum regidx coordinate (0-based) |
| 95 | |
| 96 | typedef struct _regidx_t regidx_t; |
| 97 | typedef struct |
| 98 | { |
| 99 | uint32_t beg,end; |
| 100 | void *payload; |
| 101 | char *seq; |
| 102 | void *itr; |
| 103 | } |
| 104 | regitr_t; |
| 105 | |
| 106 | #define regitr_payload(itr,type_t) (*((type_t*)(itr)->payload)) |
| 107 | |
| 108 | /* |
| 109 | * regidx_parse_f - Function to parse one input line, such as regidx_parse_bed |
| 110 | * or regidx_parse_tab below. The function is expected to set `chr_from` and |
| 111 | * `chr_to` to point to first and last character of chromosome name and set |
| 112 | * coordinates `beg` and `end` (0-based, inclusive). If regidx_init() was |
| 113 | * called with non-zero payload_size, the `payload` points to a memory |
| 114 | * location of the payload_size and `usr` is the data passed to regidx_init(). |
| 115 | * Any memory allocated by the function will be freed by regidx_free_f called |
| 116 | * by regidx_destroy(). |
| 117 | * |
| 118 | * Return value: 0 on success, -1 to skip a record, -2 on fatal error. |
| 119 | */ |
| 120 | typedef int (*regidx_parse_f)(const char *line, char **chr_beg, char **chr_end, uint32_t *beg, uint32_t *end, void *payload, void *usr); |
| 121 | typedef void (*regidx_free_f)(void *payload); |
| 122 | |
| 123 | /* |
| 124 | * A note about the parsers: |
| 125 | * - leading spaces are ignored |
| 126 | * - lines starting with "#" are ignored |
| 127 | */ |
| 128 | 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) |
| 129 | int regidx_parse_tab(const char*,char**,char**,uint32_t*,uint32_t*,void*,void*); // CHROM or whitespace-separated CHROM,POS (1-based, inclusive) |
| 130 | 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) |
| 131 | int regidx_parse_vcf(const char*,char**,char**,uint32_t*,uint32_t*,void*,void*); |
| 132 | |
| 133 | /* |
| 134 | * regidx_init() - creates new index |
| 135 | * regidx_init_string() - creates new index, from a string rather than from a file |
| 136 | * |
| 137 | * @param fname: input file name or NULL if regions will be added one-by-one via regidx_insert() |
| 138 | * @param parsef: regidx_parse_bed, regidx_parse_tab or see description of regidx_parse_f. If NULL, |
| 139 | * the format will be autodetected, currently either regidx_parse_tab (the default) or |
| 140 | * regidx_parse_bed (file must be named 'bed' or 'bed.gz') will be used. Note that |
| 141 | * the exact autodetection algorithm will change. |
| 142 | * @param freef: NULL or see description of regidx_parse_f |
| 143 | * @param payload_size: 0 with regidx_parse_bed, regidx_parse_tab or see regidx_parse_f |
| 144 | * @param usr: optional user data passed to regidx_parse_f |
| 145 | * |
| 146 | * Returns index on success or NULL on error. |
| 147 | */ |
| 148 | regidx_t *regidx_init(const char *fname, regidx_parse_f parsef, regidx_free_f freef, size_t payload_size, void *usr); |
| 149 | regidx_t *regidx_init_string(const char *string, regidx_parse_f parsef, regidx_free_f freef, size_t payload_size, void *usr); |
| 150 | |
| 151 | /* |
| 152 | * regidx_destroy() - free memory allocated by regidx_init |
| 153 | */ |
| 154 | void regidx_destroy(regidx_t *idx); |
| 155 | |
| 156 | /* |
| 157 | * regidx_overlap() - check overlap of the location chr:from-to with regions |
| 158 | * @param beg,end: 0-based start, end coordinate (inclusive) |
| 159 | * @param itr: pointer to iterator, can be NULL if regidx_loop not needed |
| 160 | * |
| 161 | * Returns 0 if there is no overlap or 1 if overlap is found. The overlapping |
| 162 | * regions can be iterated as shown in the example above. |
| 163 | */ |
| 164 | int regidx_overlap(regidx_t *idx, const char *chr, uint32_t beg, uint32_t end, regitr_t *itr); |
| 165 | |
| 166 | /* |
| 167 | * regidx_insert() - add a new region. |
| 168 | * regidx_insert_list() - add new regions from a list |
| 169 | * regidx_push() - low level insertion of a new region |
| 170 | * |
| 171 | * Returns 0 on success or -1 on error. |
| 172 | */ |
| 173 | int regidx_insert(regidx_t *idx, char *line); |
| 174 | int regidx_insert_list(regidx_t *idx, char *line, char delim); |
| 175 | int regidx_push(regidx_t *idx, char *chr_beg, char *chr_end, uint32_t beg, uint32_t end, void *payload); |
| 176 | |
| 177 | /* |
| 178 | * regidx_seq_names() - return list of all sequence names |
| 179 | */ |
| 180 | char **regidx_seq_names(regidx_t *idx, int *n); |
| 181 | |
| 182 | /* |
| 183 | * regidx_seq_nregs() - number of regions |
| 184 | * regidx_nregs() - total number of regions |
| 185 | */ |
| 186 | int regidx_seq_nregs(regidx_t *idx, const char *seq); |
| 187 | int regidx_nregs(regidx_t *idx); |
| 188 | |
| 189 | /* |
| 190 | * regitr_init() - initialize an iterator. The idx parameter is required only |
| 191 | * with regitr_loop. If only regitr_overlap is called, NULL |
| 192 | * can be given. |
| 193 | * |
| 194 | * regitr_reset() - initialize an iterator for a repeated regitr_loop cycle. |
| 195 | * Not required with regitr_overlap. |
| 196 | */ |
| 197 | regitr_t *regitr_init(regidx_t *idx); |
| 198 | void regitr_destroy(regitr_t *itr); |
| 199 | void regitr_reset(regidx_t *idx, regitr_t *itr); |
| 200 | |
| 201 | /* |
| 202 | * regitr_overlap() - next overlapping region |
| 203 | * Returns 0 when done or 1 when itr is set to next region |
| 204 | */ |
| 205 | int regitr_overlap(regitr_t *itr); |
| 206 | |
| 207 | /* |
| 208 | * regitr_loop() - loop over all regions |
| 209 | * Returns 0 when done or 1 when itr is set to next region |
| 210 | */ |
| 211 | int regitr_loop(regitr_t *itr); |
| 212 | |
| 213 | /* |
| 214 | * regitr_copy() - create a copy of an iterator for a repeated iteration with regitr_loop |
| 215 | */ |
| 216 | void regitr_copy(regitr_t *dst, regitr_t *src); |
| 217 | |
| 218 | #ifdef __cplusplus |
| 219 | } |
| 220 | #endif |
| 221 | |
| 222 | #endif |