Debian Codegraph @

bcftools/tsv2vcf.c.pysam.c

raw symbols SCIP
1 #include "bcftools.pysam.h"
2
3 /* tsv2vcf.c -- convert from whitespace-separated fields to VCF
4
5 Copyright (C) 2014-2021 Genome Research Ltd.
6
7 Author: Petr Danecek <[email protected]>
8
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 */
27
28 #include <ctype.h>
29 #include <strings.h>
30 #include "tsv2vcf.h"
31
32 tsv_t *tsv_init(const char *str)
33 {
34 tsv_t *tsv = (tsv_t *) calloc(1,sizeof(tsv_t));
35 kstring_t tmp = {0,0,0};
36 const char *ss = str, *se = ss;
37 tsv->ncols = 0;
38 while ( *ss )
39 {
40 if ( *se && *se!=',' ) { se++; continue; }
41 tsv->ncols++;
42 tsv->cols = (tsv_col_t*) realloc(tsv->cols,sizeof(tsv_col_t)*tsv->ncols);
43 tsv->cols[tsv->ncols-1].name = NULL;
44 tsv->cols[tsv->ncols-1].setter = NULL;
45 tmp.l = 0;
46 kputsn(ss, se-ss, &tmp);
47 if ( strcasecmp("-",tmp.s) )
48 tsv->cols[tsv->ncols-1].name = strdup(tmp.s);
49 if ( !*se ) break;
50 ss = ++se;
51 }
52 free(tmp.s);
53 return tsv;
54 }
55
56 void tsv_destroy(tsv_t *tsv)
57 {
58 int i;
59 for (i=0; i<tsv->ncols; i++) free(tsv->cols[i].name);
60 free(tsv->cols);
61 free(tsv);
62 }
63
64 int tsv_register(tsv_t *tsv, const char *id, tsv_setter_t setter, void *usr)
65 {
66 int i;
67 for (i=0; i<tsv->ncols; i++)
68 {
69 if ( !tsv->cols[i].name || strcasecmp(tsv->cols[i].name,id) ) continue;
70 tsv->cols[i].setter = setter;
71 tsv->cols[i].usr = usr;
72 return 0;
73 }
74 return -1;
75 }
76
77 int tsv_parse(tsv_t *tsv, bcf1_t *rec, char *str)
78 {
79 int status = 0;
80 tsv->icol = 0;
81 tsv->ss = tsv->se = str;
82 while ( *tsv->ss && tsv->icol < tsv->ncols )
83 {
84 while ( *tsv->se && !isspace(*tsv->se) ) tsv->se++;
85 if ( tsv->cols[tsv->icol].setter )
86 {
87 int ret = tsv->cols[tsv->icol].setter(tsv,rec,tsv->cols[tsv->icol].usr);
88 if ( ret<0 ) return -1;
89 status++;
90 }
91 while ( *tsv->se && isspace(*tsv->se) ) tsv->se++;
92 tsv->ss = tsv->se;
93 tsv->icol++;
94 }
95 return status ? 0 : -1;
96 }
97
98 int tsv_setter_chrom(tsv_t *tsv, bcf1_t *rec, void *usr)
99 {
100 char tmp = *tsv->se;
101 *tsv->se = 0;
102 rec->rid = bcf_hdr_name2id((bcf_hdr_t*)usr, tsv->ss);
103 *tsv->se = tmp;
104 return rec->rid==-1 ? -1 : 0;
105 }
106
107 int tsv_setter_pos(tsv_t *tsv, bcf1_t *rec, void *usr)
108 {
109 char *endptr;
110 rec->pos = strtol(tsv->ss, &endptr, 10) - 1;
111 if ( tsv->ss==endptr ) return -1;
112 return 0;
113 }
114
115 int tsv_setter_id(tsv_t *tsv, bcf1_t *rec, void *usr)
116 {
117 char tmp = *tsv->se;
118 *tsv->se = 0;
119 bcf_update_id((bcf_hdr_t*)usr, rec, tsv->ss);
120 *tsv->se = tmp;
121 return 0;
122 }
123
124 int tsv_setter_ref_alt(tsv_t *tsv, bcf1_t *rec, void *usr)
125 {
126 bcf_hdr_t *hdr = (bcf_hdr_t*)usr;
127 char *sb = tsv->ss;
128 while ( *sb && !isspace(*sb) ) sb++;
129 if ( !*sb ) return -1;
130 char tmp = *sb;
131 *sb = ',';
132 bcf_update_alleles_str(hdr, rec, tsv->ss);
133 *sb = tmp;
134 return 0;
135 }
136
137