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

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