upstream-dependencies/bcftools/plugins/dosage.c
1/* plugins/dosage.c -- prints genotype dosage.
2
3 Copyright (C) 2014-2018 Genome Research Ltd.
4
5 Author: Petr Danecek <pd3@sanger.ac.uk>
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in
15all copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23DEALINGS IN THE SOFTWARE. */
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <htslib/vcf.h>
28#include <math.h>
29#include <getopt.h>
30#include <inttypes.h>
31#include "bcftools.h"
32
33
34/*
35 This short description is used to generate the output of `bcftools plugin -l`.
36*/
37const char *about(void)
38{
39 return "Prints genotype dosage determined from tags requested by the user.\n";
40}
41
42const char *usage(void)
43{
44 return
45 "\n"
46 "About: Print genotype dosage\n"
47 "Usage: bcftools +dosage [General Options] -- [Plugin Options]\n"
48 "Options:\n"
49 " run \"bcftools plugin\" for a list of common options\n"
50 "\n"
51 "Plugin options:\n"
52 " -t, --tags <list> VCF tags to determine the dosage from [PL,GL,GT]\n"
53 "\n"
54 "Example:\n"
55 " bcftools +dosage in.vcf -- -t GT\n"
56 "\n";
57}
58
59bcf_hdr_t *in_hdr = NULL;
60int pl_type = 0, gl_type = 0;
61uint8_t *buf = NULL;
62int nbuf = 0; // NB: number of elements, not bytes
63char **tags = NULL;
64int ntags = 0;
65float *vals = NULL, *dsg = NULL;
66int mvals, mdsg;
67
68typedef int (*dosage_f) (bcf1_t *);
69dosage_f *handlers = NULL;
70int nhandlers = 0;
71
72
73int calc_dosage_PL(bcf1_t *rec)
74{
75 int i, j, nret = bcf_get_format_values(in_hdr,rec,"PL",(void**)&buf,&nbuf,pl_type);
76 if ( nret<0 ) return -1;
77
78 nret /= rec->n_sample;
79 if ( nret != rec->n_allele*(rec->n_allele+1)/2 ) return -1; // not diploid
80 hts_expand(float, nret, mvals, vals);
81 hts_expand(float, rec->n_allele, mdsg, dsg);
82 #define BRANCH(type_t,is_missing,is_vector_end) \
83 { \
84 type_t *ptr = (type_t*) buf; \
85 for (i=0; i<rec->n_sample; i++) \
86 { \
87 float sum = 0; \
88 for (j=0; j<nret; j++) \
89 { \
90 if ( is_missing || is_vector_end ) break; \
91 vals[j] = pow(10,-0.1*ptr[j]); \
92 sum += vals[j]; \
93 } \
94 if ( j<nret ) \
95 for (j=0; j<rec->n_allele; j++) dsg[j] = -1; \
96 else \
97 { \
98 if ( sum ) for (j=0; j<nret; j++) vals[j] /= sum; \
99 vals[0] = 0; \
100 memset(dsg, 0, sizeof(float)*rec->n_allele); \
101 int k, l = 0; \
102 for (j=0; j<rec->n_allele; j++) \
103 { \
104 for (k=0; k<=j; k++) \
105 { \
106 dsg[j] += vals[l]; \
107 dsg[k] += vals[l]; \
108 l++; \
109 } \
110 } \
111 } \
112 for (j=1; j<rec->n_allele; j++) \
113 printf("%c%f",j==1?'\t':',',dsg[j]); \
114 ptr += nret; \
115 } \
116 }
117 switch (pl_type)
118 {
119 case BCF_HT_INT: BRANCH(int32_t,ptr[j]==bcf_int32_missing,ptr[j]==bcf_int32_vector_end); break;
120 case BCF_HT_REAL: BRANCH(float,bcf_float_is_missing(ptr[j]),bcf_float_is_vector_end(ptr[j])); break;
121 }
122 #undef BRANCH
123 return 0;
124}
125
126int calc_dosage_GL(bcf1_t *rec)
127{
128 int i, j, nret = bcf_get_format_values(in_hdr,rec,"GL",(void**)&buf,&nbuf,gl_type);
129 if ( nret<0 ) return -1;
130
131 nret /= rec->n_sample;
132 if ( nret != rec->n_allele*(rec->n_allele+1)/2 ) return -1; // not diploid
133 hts_expand(float, nret, mvals, vals);
134 hts_expand(float, rec->n_allele, mdsg, dsg);
135 #define BRANCH(type_t,is_missing,is_vector_end) \
136 { \
137 type_t *ptr = (type_t*) buf; \
138 for (i=0; i<rec->n_sample; i++) \
139 { \
140 float sum = 0; \
141 for (j=0; j<nret; j++) \
142 { \
143 if ( is_missing || is_vector_end ) break; \
144 vals[j] = pow(10,ptr[j]); \
145 sum += vals[j]; \
146 } \
147 if ( j<nret ) \
148 for (j=0; j<rec->n_allele; j++) dsg[j] = -1; \
149 else \
150 { \
151 if ( sum ) for (j=0; j<nret; j++) vals[j] /= sum; \
152 vals[0] = 0; \
153 memset(dsg, 0, sizeof(float)*rec->n_allele); \
154 int k, l = 0; \
155 for (j=0; j<rec->n_allele; j++) \
156 { \
157 for (k=0; k<=j; k++) \
158 { \
159 dsg[j] += vals[l]; \
160 dsg[k] += vals[l]; \
161 l++; \
162 } \
163 } \
164 } \
165 for (j=1; j<rec->n_allele; j++) \
166 printf("%c%f",j==1?'\t':',',dsg[j]); \
167 ptr += nret; \
168 } \
169 }
170 switch (gl_type)
171 {
172 case BCF_HT_INT: BRANCH(int32_t,ptr[j]==bcf_int32_missing,ptr[j]==bcf_int32_vector_end); break;
173 case BCF_HT_REAL: BRANCH(float,bcf_float_is_missing(ptr[j]),bcf_float_is_vector_end(ptr[j])); break;
174 }
175 #undef BRANCH
176 return 0;
177}
178
179int calc_dosage_GT(bcf1_t *rec)
180{
181 int i, j, nret = bcf_get_genotypes(in_hdr,rec,(void**)&buf,&nbuf);
182 if ( nret<0 ) return -1;
183
184 nret /= rec->n_sample;
185 int32_t *ptr = (int32_t*) buf;
186 hts_expand(float, rec->n_allele, mdsg, dsg);
187 for (i=0; i<rec->n_sample; i++)
188 {
189 memset(dsg, 0, sizeof(float)*rec->n_allele);
190 for (j=0; j<nret; j++)
191 {
192 if ( ptr[j]==bcf_int32_vector_end || bcf_gt_is_missing(ptr[j]) ) break;
193 int idx = bcf_gt_allele(ptr[j]);
194 if ( idx > rec->n_allele ) error("The allele index is out of range at %s:%"PRId64"\n", bcf_seqname(in_hdr,rec),(int64_t) rec->pos+1);
195 dsg[idx] += 1;
196 }
197 if ( !j )
198 for (j=0; j<rec->n_allele; j++) dsg[j] = -1;
199 for (j=1; j<rec->n_allele; j++)
200 printf("%c%.1f",j==1?'\t':',',dsg[j]);
201 ptr += nret;
202 }
203 return 0;
204}
205
206
207char **split_list(char *str, int *nitems)
208{
209 int n = 0, done = 0;
210 char *ss = strdup(str), **out = NULL;
211 while ( !done && *ss )
212 {
213 char *se = ss;
214 while ( *se && *se!=',' ) se++;
215 if ( !*se ) done = 1;
216 *se = 0;
217 n++;
218 out = (char**) realloc(out,sizeof(char*)*n);
219 out[n-1] = ss;
220 ss = se+1;
221 }
222 *nitems = n;
223 return out;
224}
225
226int init(int argc, char **argv, bcf_hdr_t *in, bcf_hdr_t *out)
227{
228 int i, id, c;
229 char *tags_str = "PL,GL,GT";
230
231 static struct option loptions[] =
232 {
233 {"tags",1,0,'t'},
234 {0,0,0,0}
235 };
236 while ((c = getopt_long(argc, argv, "t:?h",loptions,NULL)) >= 0)
237 {
238 switch (c)
239 {
240 case 't': tags_str = optarg; break;
241 case 'h':
242 case '?':
243 default: fprintf(stderr,"%s", usage()); exit(1); break;
244 }
245 }
246 tags = split_list(tags_str, &ntags);
247
248 in_hdr = in;
249 for (i=0; i<ntags; i++)
250 {
251 if ( !strcmp("PL",tags[i]) )
252 {
253 id = bcf_hdr_id2int(in_hdr,BCF_DT_ID,"PL");
254 if ( bcf_hdr_idinfo_exists(in_hdr,BCF_HL_FMT,id) )
255 {
256 pl_type = bcf_hdr_id2type(in_hdr,BCF_HL_FMT,id);
257 if ( pl_type!=BCF_HT_INT && pl_type!=BCF_HT_REAL )
258 {
259 fprintf(stderr,"Expected numeric type of FORMAT/PL\n");
260 return -1;
261 }
262 handlers = (dosage_f*) realloc(handlers,(nhandlers+1)*sizeof(*handlers));
263 handlers[nhandlers++] = calc_dosage_PL;
264 }
265 }
266 else if ( !strcmp("GL",tags[i]) )
267 {
268 id = bcf_hdr_id2int(in_hdr,BCF_DT_ID,"GL");
269 if ( bcf_hdr_idinfo_exists(in_hdr,BCF_HL_FMT,id) )
270 {
271 gl_type = bcf_hdr_id2type(in_hdr,BCF_HL_FMT,id);
272 if ( gl_type!=BCF_HT_INT && gl_type!=BCF_HT_REAL )
273 {
274 fprintf(stderr,"Expected numeric type of FORMAT/GL\n");
275 return -1;
276 }
277 handlers = (dosage_f*) realloc(handlers,(nhandlers+1)*sizeof(*handlers));
278 handlers[nhandlers++] = calc_dosage_GL;
279 }
280 }
281 else if ( !strcmp("GT",tags[i]) )
282 {
283 handlers = (dosage_f*) realloc(handlers,(nhandlers+1)*sizeof(*handlers));
284 handlers[nhandlers++] = calc_dosage_GT;
285 }
286 else
287 {
288 fprintf(stderr,"No handler for tag \"%s\"\n", tags[i]);
289 return -1;
290 }
291 }
292 free(tags[0]);
293 free(tags);
294
295 printf("#[1]CHROM\t[2]POS\t[3]REF\t[4]ALT");
296 for (i=0; i<bcf_hdr_nsamples(in_hdr); i++) printf("\t[%d]%s", i+5,in_hdr->samples[i]);
297 printf("\n");
298
299 return 1;
300}
301
302
303bcf1_t *process(bcf1_t *rec)
304{
305 int i,j, ret;
306
307 printf("%s\t%"PRId64"\t%s", bcf_seqname(in_hdr,rec),(int64_t) rec->pos+1,rec->d.allele[0]);
308 if ( rec->n_allele == 1 ) printf("\t.");
309 else for (i=1; i<rec->n_allele; i++) printf("%c%s", i==1?'\t':',', rec->d.allele[i]);
310 if ( rec->n_allele==1 )
311 {
312 for (j=0; j<rec->n_sample; j++) printf("\t0.0");
313 }
314 else
315 {
316 for (i=0; i<nhandlers; i++)
317 {
318 ret = handlers[i](rec);
319 if ( !ret ) break; // successfully printed
320 }
321 if ( i==nhandlers )
322 {
323 // none of the annotations present
324 for (i=0; i<rec->n_sample; i++) printf("\t-1.0");
325 }
326 }
327 printf("\n");
328
329 return NULL;
330}
331
332
333void destroy(void)
334{
335 free(vals);
336 free(dsg);
337 free(handlers);
338 free(buf);
339}
340
341