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