comparison ezBAMQC/src/htslib/test/test-regidx.c @ 0:dfa3745e5fd8

Uploaded
author youngkim
date Thu, 24 Mar 2016 17:12:52 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:dfa3745e5fd8
1 /* test/test-regidx.c -- Regions index test harness.
2
3 Copyright (C) 2014 Genome Research Ltd.
4
5 Author: Petr Danecek <pd3@sanger.ac.uk>
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE.
24 */
25
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <ctype.h>
30 #include <string.h>
31 #include <htslib/regidx.h>
32
33 void error(const char *format, ...)
34 {
35 va_list ap;
36 va_start(ap, format);
37 vfprintf(stderr, format, ap);
38 va_end(ap);
39 exit(-1);
40 }
41
42 int custom_parse(const char *line, char **chr_beg, char **chr_end, reg_t *reg, void *payload, void *usr)
43 {
44 // Use the standard parser for CHROM,FROM,TO
45 int i, ret = regidx_parse_tab(line,chr_beg,chr_end,reg,NULL,NULL);
46 if ( ret!=0 ) return ret;
47
48 // Skip the fields that were parsed above
49 char *ss = (char*) line;
50 while ( *ss && isspace(*ss) ) ss++;
51 for (i=0; i<3; i++)
52 {
53 while ( *ss && !isspace(*ss) ) ss++;
54 if ( !*ss ) return -2; // wrong number of fields
55 while ( *ss && isspace(*ss) ) ss++;
56 }
57 if ( !*ss ) return -2;
58
59 // Parse the payload
60 char *se = ss;
61 while ( *se && !isspace(*se) ) se++;
62 char **dat = (char**) payload;
63 *dat = (char*) malloc(se-ss+1);
64 memcpy(*dat,ss,se-ss+1);
65 (*dat)[se-ss] = 0;
66 return 0;
67 }
68 void custom_free(void *payload)
69 {
70 char **dat = (char**)payload;
71 free(*dat);
72 }
73
74 int main(int argc, char **argv)
75 {
76 // Init index with no file name, we will insert the regions manually
77 regidx_t *idx = regidx_init(NULL,custom_parse,custom_free,sizeof(char*),NULL);
78 if ( !idx ) error("init failed\n");
79
80 // Insert regions
81 char *line;
82 line = "1 10000000 10000000 1:10000000-10000000"; if ( regidx_insert(idx,line)!=0 ) error("insert failed: %s\n", line);
83 line = "1 20000000 20000001 1:20000000-20000001"; if ( regidx_insert(idx,line)!=0 ) error("insert failed: %s\n", line);
84 line = "1 20000002 20000002 1:20000002-20000002"; if ( regidx_insert(idx,line)!=0 ) error("insert failed: %s\n", line);
85 line = "1 30000000 30000000 1:30000000-30000000"; if ( regidx_insert(idx,line)!=0 ) error("insert failed: %s\n", line);
86
87 // Finish initialization
88 regidx_insert(idx,NULL);
89
90 // Test
91 regitr_t itr;
92 int from, to;
93
94 from = to = 10000000;
95 if ( !regidx_overlap(idx,"1",from-1,to-1,&itr) ) error("query failed: 1:%d-%d\n",from,to);
96 if ( strcmp("1:10000000-10000000",REGITR_PAYLOAD(itr,char*)) ) error("query failed: 1:%d-%d vs %s\n", from,to,REGITR_PAYLOAD(itr,char*));
97 if ( !regidx_overlap(idx,"1",from-2,to-1,&itr) ) error("query failed: 1:%d-%d\n",from-1,to);
98 if ( !regidx_overlap(idx,"1",from-2,to+3,&itr) ) error("query failed: 1:%d-%d\n",from-1,to+2);
99 if ( regidx_overlap(idx,"1",from-2,to-2,&itr) ) error("query failed: 1:%d-%d\n",from-1,to-1);
100
101 from = to = 20000000;
102 if ( !regidx_overlap(idx,"1",from-1,to-1,&itr) ) error("query failed: 1:%d-%d\n",from,to);
103
104 from = to = 20000002;
105 if ( !regidx_overlap(idx,"1",from-1,to-1,&itr) ) error("query failed: 1:%d-%d\n",from,to);
106
107 from = to = 30000000;
108 if ( !regidx_overlap(idx,"1",from-1,to-1,&itr) ) error("query failed: 1:%d-%d\n",from,to);
109
110 // Clean up
111 regidx_destroy(idx);
112
113 return 0;
114 }
115
116