1 /// @file htslib/regidx.h
2 /// Region indexing.
3 /*
4     Copyright (C) 2014 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     Regions indexing with an optional payload. Inspired by samtools/bedidx.c.
29     This code is intended as future replacement of bcf_sr_regions_t.
30 
31     Example of usage:
32 
33         // Init the parser and print regions. In this example the payload is a
34         // pointer to a string. For the description of parse_custom and
35         // free_custom functions, see regidx_parse_f and regidx_free_f below,
36         // and for working example see test/test-regidx.c.
37         regidx_t *idx = regidx_init(in_fname,parse_custom,free_custom,sizeof(char*),NULL);
38 
39         // Query overlap with chr:from-to
40         regitr_t itr;
41         if ( regidx_overlap(idx, chr,from,to, &itr) ) printf("There is an overlap!\n");
42 
43         while ( REGITR_OVERLAP(itr,from,to) )
44         {
45             printf("[%d,%d] overlaps with [%d,%d], payload=%s\n", from,to,
46                 REGITR_START(itr), REGITR_END(itr), REGITR_PAYLOAD(itr,char*));
47             itr.i++;
48         }
49 
50         regidx_destroy(regs);
51 */
52 
53 #ifndef HTSLIB_REGIDX_H
54 #define HTSLIB_REGIDX_H
55 
56 #include <stdio.h>
57 #include <inttypes.h>
58 
59 #ifdef __cplusplus
60 extern "C" {
61 #endif
62 
63 typedef struct _regidx_t regidx_t;
64 typedef struct
65 {
66     uint32_t start, end;
67 }
68 reg_t;
69 typedef struct
70 {
71     int i, n;
72     reg_t *reg;
73     void *payload;
74 }
75 regitr_t;
76 
77 #define REGITR_START(itr) (itr).reg[(itr).i].start
78 #define REGITR_END(itr)   (itr).reg[(itr).i].end
79 #define REGITR_PAYLOAD(itr,type_t) ((type_t*)(itr).payload)[(itr).i]
80 #define REGITR_OVERLAP(itr,from,to) (itr.i < itr.n && REGITR_START(itr)<=to && REGITR_END(itr)>=from )
81 
82 /*
83  *  regidx_parse_f - Function to parse one input line, such as regidx_parse_bed
84  *  or regidx_parse_tab below. The function is expected to set `chr_from` and
85  *  `chr_to` to point to first and last character of chromosome name and set
86  *  coordinates `reg->start` and `reg->end` (0-based, inclusive). If
87  *  regidx_init() was called with non-zero payload_size, the `payload` points
88  *  to a memory location of the payload_size and `usr` is data passed to
89  *  regidx_init(). Any memory allocated by the function will be freed by
90  *  regidx_free_f on regidx_destroy().
91  *
92  *  Return value: 0 on success, -1 to skip a record, -2 on fatal error.
93  */
94 typedef int  (*regidx_parse_f)(const char *line, char **chr_beg, char **chr_end, reg_t *reg, void *payload, void *usr);
95 typedef void (*regidx_free_f)(void *payload);
96 
97 int regidx_parse_bed(const char*,char**,char**,reg_t*,void*,void*);   // CHROM,FROM,TO (0-based,right-open)
98 int regidx_parse_tab(const char*,char**,char**,reg_t*,void*,void*);   // CHROM,POS (1-based, inclusive)
99 
100 /*
101  *  regidx_init() - creates new index
102  *  @param fname:  input file name or NULL if regions will be added one-by-one via regidx_insert()
103  *  @param parsef: regidx_parse_bed, regidx_parse_tab or see description of regidx_parse_f. If NULL,
104  *                 the format will be autodected, currently either regidx_parse_tab (the default) or
105  *                 regidx_parse_bed (file must be named 'bed' or 'bed.gz') will be used. Note that
106  *                 the exact autodetection algorithm will change.
107  *  @param freef:  NULL or see description of regidx_parse_f
108  *  @param payload_size: 0 with regidx_parse_bed, regidx_parse_tab or see regidx_parse_f
109  *  @param usr:    optional user data passed to regidx_parse_f
110  *
111  *  Returns index on success or NULL on error.
112  */
113 regidx_t *regidx_init(const char *fname, regidx_parse_f parsef, regidx_free_f freef, size_t payload_size, void *usr);
114 
115 /*
116  *  regidx_destroy() - free memory allocated by regidx_init
117  */
118 void regidx_destroy(regidx_t *idx);
119 
120 /*
121  *  regidx_overlap() - check overlap of the location chr:from-to with regions
122  *  @param start,end:   0-based start, end coordinate (inclusive)
123  *  @param itr:         pointer to iterator, can be NULL if not needed
124  *
125  *  Returns 0 if there is no overlap or 1 if overlap is found. The overlapping
126  *  regions can be iterated as shown in the example above.
127  */
128 int regidx_overlap(regidx_t *idx, const char *chr, uint32_t start, uint32_t end, regitr_t *itr);
129 
130 /*
131  *  regidx_insert() - add a new region.
132  *
133  *  After last region has been added, call regidx_insert(idx,NULL) to
134  *  build the index.
135  *
136  *  Returns 0 on success or -1 on error.
137  */
138 int regidx_insert(regidx_t *idx, char *line);
139 
140 /*
141  *  regidx_seq_names() - return list of all sequence names
142  */
143 char **regidx_seq_names(regidx_t *idx, int *n);
144 
145 /*
146  *  regidx_seq_nregs() - number of regions
147  *  regidx_nregs()  - total number of regions
148  */
149 int regidx_seq_nregs(regidx_t *idx, const char *seq);
150 int regidx_nregs(regidx_t *idx);
151 
152 #ifdef __cplusplus
153 }
154 #endif
155 
156 #endif
157