Debian Codegraph @

bcftools/tsv2vcf.c

raw symbols SCIP
1 /* tsv2vcf.c -- convert from whitespace-separated fields to VCF
2
3 Copyright (C) 2014-2021 Genome Research Ltd.
4
5 Author: Petr Danecek <[email protected]>
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 <ctype.h>
27 #include <strings.h>
28 #include "tsv2vcf.h"
29
30 tsv_t *tsv_init(const char *str)
31 {
32 tsv_t *tsv = (tsv_t *) calloc(1,sizeof(tsv_t));
33 kstring_t tmp = {0,0,0};
34 const char *ss = str, *se = ss;
35 tsv->ncols = 0;
36 while ( *ss )
37 {
38 if ( *se && *se!=',' ) { se++; continue; }
39 tsv->ncols++;
40 tsv->cols = (tsv_col_t*) realloc(tsv->cols,sizeof(tsv_col_t)*tsv->ncols);
41 tsv->cols[tsv->ncols-1].name = NULL;
42 tsv->cols[tsv->ncols-1].setter = NULL;
43 tmp.l = 0;
44 kputsn(ss, se-ss, &tmp);
45 if ( strcasecmp("-",tmp.s) )
46 tsv->cols[tsv->ncols-1].name = strdup(tmp.s);
47 if ( !*se ) break;
48 ss = ++se;
49 }
50 free(tmp.s);
51 return tsv;
52 }
53
54 void tsv_destroy(tsv_t *tsv)
55 {
56 int i;
57 for (i=0; i<tsv->ncols; i++) free(tsv->cols[i].name);
58 free(tsv->cols);
59 free(tsv);
60 }
61
62 int tsv_register(tsv_t *tsv, const char *id, tsv_setter_t setter, void *usr)
63 {
64 int i;
65 for (i=0; i<tsv->ncols; i++)
66 {
67 if ( !tsv->cols[i].name || strcasecmp(tsv->cols[i].name,id) ) continue;
68 tsv->cols[i].setter = setter;
69 tsv->cols[i].usr = usr;
70 return 0;
71 }
72 return -1;
73 }
74
75 int tsv_parse(tsv_t *tsv, bcf1_t *rec, char *str)
76 {
77 int status = 0;
78 tsv->icol = 0;
79 tsv->ss = tsv->se = str;
80 while ( *tsv->ss && tsv->icol < tsv->ncols )
81 {
82 while ( *tsv->se && !isspace(*tsv->se) ) tsv->se++;
83 if ( tsv->cols[tsv->icol].setter )
84 {
85 int ret = tsv->cols[tsv->icol].setter(tsv,rec,tsv->cols[tsv->icol].usr);
86 if ( ret<0 ) return -1;
87 status++;
88 }
89 while ( *tsv->se && isspace(*tsv->se) ) tsv->se++;
90 tsv->ss = tsv->se;
91 tsv->icol++;
92 }
93 return status ? 0 : -1;
94 }
95
96 int tsv_setter_chrom(tsv_t *tsv, bcf1_t *rec, void *usr)
97 {
98 char tmp = *tsv->se;
99 *tsv->se = 0;
100 rec->rid = bcf_hdr_name2id((bcf_hdr_t*)usr, tsv->ss);
101 *tsv->se = tmp;
102 return rec->rid==-1 ? -1 : 0;
103 }
104
105 int tsv_setter_pos(tsv_t *tsv, bcf1_t *rec, void *usr)
106 {
107 char *endptr;
108 rec->pos = strtol(tsv->ss, &endptr, 10) - 1;
109 if ( tsv->ss==endptr ) return -1;
110 return 0;
111 }
112
113 int tsv_setter_id(tsv_t *tsv, bcf1_t *rec, void *usr)
114 {
115 char tmp = *tsv->se;
116 *tsv->se = 0;
117 bcf_update_id((bcf_hdr_t*)usr, rec, tsv->ss);
118 *tsv->se = tmp;
119 return 0;
120 }
121
122 int tsv_setter_ref_alt(tsv_t *tsv, bcf1_t *rec, void *usr)
123 {
124 bcf_hdr_t *hdr = (bcf_hdr_t*)usr;
125 char *sb = tsv->ss;
126 while ( *sb && !isspace(*sb) ) sb++;
127 if ( !*sb ) return -1;
128 char tmp = *sb;
129 *sb = ',';
130 bcf_update_alleles_str(hdr, rec, tsv->ss);
131 *sb = tmp;
132 return 0;
133 }
134
135