forked from samtools/bcftools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.c
1425 lines (1336 loc) · 53 KB
/
convert.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* convert.c -- functions for converting between VCF/BCF and related formats.
Copyright (C) 2013-2018 Genome Research Ltd.
Author: Petr Danecek <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. */
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <math.h>
#include <htslib/vcf.h>
#include <htslib/synced_bcf_reader.h>
#include <htslib/vcfutils.h>
#include "bcftools.h"
#include "convert.h"
#define T_CHROM 1
#define T_POS 2
#define T_ID 3
#define T_REF 4
#define T_ALT 5
#define T_QUAL 6
#define T_FILTER 7
#define T_INFO 8
#define T_FORMAT 9
#define T_SAMPLE 10
#define T_SEP 11
#define T_IS_TS 12
#define T_TYPE 13
#define T_MASK 14
#define T_GT 15
#define T_TGT 16
#define T_LINE 17
#define T_CHROM_POS_ID 18 // not publicly advertised
#define T_GT_TO_PROB3 19 // not publicly advertised
#define T_PL_TO_PROB3 20 // not publicly advertised
#define T_GP_TO_PROB3 21 // not publicly advertised
#define T_FIRST_ALT 22 // not publicly advertised
#define T_IUPAC_GT 23
#define T_GT_TO_HAP 24 // not publicly advertised
#define T_GT_TO_HAP2 25 // not publicly advertised
#define T_TBCSQ 26
#define T_END 27
#define T_POS0 28
#define T_END0 29
typedef struct _fmt_t
{
int type, id, is_gt_field, ready, subscript;
char *key;
bcf_fmt_t *fmt;
void *usr; // user data (optional)
void (*handler)(convert_t *, bcf1_t *, struct _fmt_t *, int, kstring_t *);
void (*destroy)(void*); // clean user data (optional)
}
fmt_t;
struct _convert_t
{
fmt_t *fmt;
int nfmt, mfmt;
int nsamples, *samples;
bcf_hdr_t *header;
int max_unpack;
char *format_str;
bcf_srs_t *readers; // required only for %MASK
int nreaders;
void *dat;
int ndat;
char *undef_info_tag;
int allow_undef_tags;
uint8_t **subset_samples;
};
typedef struct
{
kstring_t hap1,hap2;
char **str;
int n, m;
}
bcsq_t;
static void process_chrom(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str) { kputs(convert->header->id[BCF_DT_CTG][line->rid].key, str); }
static void process_pos(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str) { kputw(line->pos+1, str); }
static void process_pos0(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str) { kputw(line->pos, str); }
static void process_end(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str) { kputw(line->pos+line->rlen, str); }
static void process_end0(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str) { kputw(line->pos+line->rlen-1, str); }
static void process_id(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str) { kputs(line->d.id, str); }
static void process_ref(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str) { kputs(line->d.allele[0], str); }
static void process_alt(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str)
{
int i;
if ( line->n_allele==1 )
{
kputc('.', str);
return;
}
if ( fmt->subscript>=0 )
{
if ( line->n_allele > fmt->subscript+1 )
kputs(line->d.allele[fmt->subscript+1], str);
else
kputc('.', str);
return;
}
for (i=1; i<line->n_allele; i++)
{
if ( i>1 ) kputc(',', str);
kputs(line->d.allele[i], str);
}
}
static void process_first_alt(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str)
{
if ( line->n_allele==1 )
kputc('.', str);
else
kputs(line->d.allele[1], str);
}
static void process_qual(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str)
{
if ( bcf_float_is_missing(line->qual) ) kputc('.', str);
else kputd(line->qual, str);
}
static void process_filter(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str)
{
int i;
if ( line->d.n_flt )
{
for (i=0; i<line->d.n_flt; i++)
{
if (i) kputc(';', str);
kputs(convert->header->id[BCF_DT_ID][line->d.flt[i]].key, str);
}
}
else kputc('.', str);
}
static inline int32_t bcf_array_ivalue(void *bcf_array, int type, int idx)
{
if ( type==BCF_BT_INT8 )
{
int8_t val = ((int8_t*)bcf_array)[idx];
if ( val==bcf_int8_missing ) return bcf_int32_missing;
if ( val==bcf_int8_vector_end ) return bcf_int32_vector_end;
return val;
}
if ( type==BCF_BT_INT16 )
{
int16_t val = ((int16_t*)bcf_array)[idx];
if ( val==bcf_int16_missing ) return bcf_int32_missing;
if ( val==bcf_int16_vector_end ) return bcf_int32_vector_end;
return val;
}
return ((int32_t*)bcf_array)[idx];
}
static inline void _copy_field(char *src, uint32_t len, int idx, kstring_t *str)
{
int n = 0, ibeg = 0;
while ( src[ibeg] && ibeg<len && n < idx )
{
if ( src[ibeg]==',' ) n++;
ibeg++;
}
if ( ibeg==len ) { kputc('.', str); return; }
int iend = ibeg;
while ( src[iend] && src[iend]!=',' && iend<len ) iend++;
if ( iend>ibeg )
kputsn(src+ibeg, iend-ibeg, str);
else
kputc('.', str);
}
static void process_info(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str)
{
if ( fmt->id<0 )
{
kputc('.', str);
return;
}
int i;
for (i=0; i<line->n_info; i++)
if ( line->d.info[i].key == fmt->id ) break;
// output "." if the tag is not present
if ( i==line->n_info )
{
kputc('.', str);
return;
}
bcf_info_t *info = &line->d.info[i];
// if this is a flag, output 1
if ( info->len <=0 )
{
kputc('1', str);
return;
}
if ( info->len == 1 )
{
switch (info->type)
{
case BCF_BT_INT8: if ( info->v1.i==bcf_int8_missing ) kputc('.', str); else kputw(info->v1.i, str); break;
case BCF_BT_INT16: if ( info->v1.i==bcf_int16_missing ) kputc('.', str); else kputw(info->v1.i, str); break;
case BCF_BT_INT32: if ( info->v1.i==bcf_int32_missing ) kputc('.', str); else kputw(info->v1.i, str); break;
case BCF_BT_FLOAT: if ( bcf_float_is_missing(info->v1.f) ) kputc('.', str); else kputd(info->v1.f, str); break;
case BCF_BT_CHAR: kputc(info->v1.i, str); break;
default: fprintf(stderr,"todo: type %d\n", info->type); exit(1); break;
}
}
else if ( fmt->subscript >=0 )
{
if ( info->len <= fmt->subscript )
{
kputc('.', str);
return;
}
#define BRANCH(type_t, is_missing, is_vector_end, kprint) { \
type_t val = ((type_t *) info->vptr)[fmt->subscript]; \
if ( is_missing || is_vector_end ) kputc('.',str); \
else kprint; \
}
switch (info->type)
{
case BCF_BT_INT8: BRANCH(int8_t, val==bcf_int8_missing, val==bcf_int8_vector_end, kputw(val, str)); break;
case BCF_BT_INT16: BRANCH(int16_t, val==bcf_int16_missing, val==bcf_int16_vector_end, kputw(val, str)); break;
case BCF_BT_INT32: BRANCH(int32_t, val==bcf_int32_missing, val==bcf_int32_vector_end, kputw(val, str)); break;
case BCF_BT_FLOAT: BRANCH(float, bcf_float_is_missing(val), bcf_float_is_vector_end(val), kputd(val, str)); break;
case BCF_BT_CHAR: _copy_field((char*)info->vptr, info->vptr_len, fmt->subscript, str); break;
default: fprintf(stderr,"todo: type %d\n", info->type); exit(1); break;
}
#undef BRANCH
}
else
bcf_fmt_array(str, info->len, info->type, info->vptr);
}
static void init_format(convert_t *convert, bcf1_t *line, fmt_t *fmt)
{
fmt->id = bcf_hdr_id2int(convert->header, BCF_DT_ID, fmt->key);
if ( !bcf_hdr_idinfo_exists(convert->header,BCF_HL_FMT,fmt->id) ) fmt->id = -1;
fmt->fmt = NULL;
if ( fmt->id >= 0 )
{
int i;
for (i=0; i<(int)line->n_fmt; i++)
if ( line->d.fmt[i].id==fmt->id ) { fmt->fmt = &line->d.fmt[i]; break; }
}
else if ( !convert->allow_undef_tags )
error("Error: no such tag defined in the VCF header: FORMAT/%s\n", fmt->key);
fmt->ready = 1;
}
static void process_format(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str)
{
if ( !fmt->ready )
init_format(convert, line, fmt);
if ( fmt->fmt==NULL )
{
kputc('.', str);
return;
}
else if ( fmt->subscript >=0 )
{
if ( fmt->fmt->n <= fmt->subscript )
{
kputc('.', str);
return;
}
if ( fmt->fmt->type == BCF_BT_FLOAT )
{
float *ptr = (float*)(fmt->fmt->p + isample*fmt->fmt->size);
if ( bcf_float_is_missing(ptr[fmt->subscript]) || bcf_float_is_vector_end(ptr[fmt->subscript]) )
kputc('.', str);
else
kputd(ptr[fmt->subscript], str);
}
else if ( fmt->fmt->type != BCF_BT_CHAR )
{
int32_t ival = bcf_array_ivalue(fmt->fmt->p+isample*fmt->fmt->size,fmt->fmt->type,fmt->subscript);
if ( ival==bcf_int32_missing || ival==bcf_int32_vector_end )
kputc('.', str);
else
kputw(ival, str);
}
else if ( fmt->fmt->type == BCF_BT_CHAR )
_copy_field((char*)(fmt->fmt->p + isample*fmt->fmt->size), fmt->fmt->size, fmt->subscript, str);
else error("TODO: %s:%d .. fmt->type=%d\n", __FILE__,__LINE__, fmt->fmt->type);
}
else
bcf_fmt_array(str, fmt->fmt->n, fmt->fmt->type, fmt->fmt->p + isample*fmt->fmt->size);
}
static void process_gt(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str)
{
if ( !fmt->ready )
init_format(convert, line, fmt);
if ( fmt->fmt==NULL )
{
kputc('.', str);
return;
}
bcf_format_gt(fmt->fmt, isample, str);
}
static void process_tgt(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str)
{
if ( !fmt->ready )
init_format(convert, line, fmt);
if ( fmt->fmt==NULL )
{
kputc('.', str);
return;
}
assert( fmt->fmt->type==BCF_BT_INT8 );
int l;
int8_t *x = (int8_t*)(fmt->fmt->p + isample*fmt->fmt->size); // FIXME: does not work with n_alt >= 64
for (l = 0; l < fmt->fmt->n && x[l] != bcf_int8_vector_end; ++l)
{
if (l) kputc("/|"[x[l]&1], str);
if (x[l]>>1)
{
int ial = (x[l]>>1) - 1;
kputs(line->d.allele[ial], str);
}
else
kputc('.', str);
}
if (l == 0) kputc('.', str);
}
static void destroy_tbcsq(void *usr)
{
if ( !usr ) return;
bcsq_t *csq = (bcsq_t*) usr;
free(csq->hap1.s);
free(csq->hap2.s);
if ( csq->n )
free(csq->str[0]);
free(csq->str);
free(csq);
}
static void process_tbcsq(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str)
{
if ( !fmt->ready )
{
init_format(convert, line, fmt);
bcsq_t *csq;
if ( fmt->usr )
{
csq = (bcsq_t*) fmt->usr;
if ( csq->n )
free(csq->str[0]);
csq->n = 0;
}
else
csq = (bcsq_t*) calloc(1,sizeof(bcsq_t));
fmt->usr = csq;
int i=0, len = 0;
char *tmp = NULL;
if ( bcf_get_info_string(convert->header,line,fmt->key,&tmp,&len)<0 )
{
csq->n = 0;
return;
}
do
{
csq->n++;
hts_expand(char*, csq->n, csq->m, csq->str);
csq->str[ csq->n-1 ] = tmp + i;
while ( i<len && tmp[i]!=',' ) i++;
if ( i<len && tmp[i]==',' ) tmp[i++] = 0;
}
while ( i<len );
}
bcsq_t *csq = (bcsq_t*)fmt->usr;
if ( fmt->fmt==NULL || !csq->n ) return;
csq->hap1.l = 0;
csq->hap2.l = 0;
int mask = fmt->subscript==0 ? 3 : 1; // merge both haplotypes if subscript==0
#define BRANCH(type_t, nbits) { \
type_t *x = (type_t*)(fmt->fmt->p + isample*fmt->fmt->size); \
int i,j; \
if ( fmt->subscript<=0 || fmt->subscript==1 ) \
{ \
for (j=0; j < fmt->fmt->n; j++) \
{ \
type_t val = x[j]; \
if ( !val ) continue; \
for (i=0; i<nbits; i+=2) \
if ( val & (mask<<i) ) { kputs(csq->str[(j*32+i)/2], &csq->hap1); kputc_(',', &csq->hap1); } \
} \
} \
if ( fmt->subscript<0 || fmt->subscript==2 ) \
{ \
for (j=0; j < fmt->fmt->n; j++) \
{ \
type_t val = x[j]; \
if ( !val ) continue; \
for (i=1; i<nbits; i+=2) \
if ( val & (1<<i) ) { kputs(csq->str[(j*32+i)/2], &csq->hap2); kputc_(',', &csq->hap2); } \
} \
} \
}
switch (fmt->fmt->type)
{
case BCF_BT_INT8: BRANCH(uint8_t, 8); break;
case BCF_BT_INT16: BRANCH(uint16_t,16); break;
case BCF_BT_INT32: BRANCH(uint32_t,32); break;
default: error("Unexpected type: %d\n", fmt->fmt->type); exit(1); break;
}
#undef BRANCH
if ( !csq->hap1.l && !csq->hap2.l ) return;
if ( csq->hap1.l ) csq->hap1.s[--csq->hap1.l] = 0;
if ( csq->hap2.l ) csq->hap2.s[--csq->hap2.l] = 0;
if ( fmt->subscript<0 )
{
kputs(csq->hap1.l?csq->hap1.s:".", str);
kputc_('\t', str);
kputs(csq->hap2.l?csq->hap2.s:".", str);
}
else if ( fmt->subscript<2 )
kputs(csq->hap1.l?csq->hap1.s:".", str);
else
kputs(csq->hap2.l?csq->hap2.s:".", str);
}
static void init_format_iupac(convert_t *convert, bcf1_t *line, fmt_t *fmt)
{
init_format(convert, line, fmt);
if ( fmt->fmt==NULL ) return;
// Init mapping between alleles and IUPAC table
hts_expand(uint8_t, line->n_allele, convert->ndat, convert->dat);
int8_t *dat = (int8_t*)convert->dat;
int i;
for (i=0; i<line->n_allele; i++)
{
if ( line->d.allele[i][1] ) dat[i] = -1;
else
{
switch (line->d.allele[i][0])
{
case 'A': dat[i] = 0; break;
case 'C': dat[i] = 1; break;
case 'G': dat[i] = 2; break;
case 'T': dat[i] = 3; break;
case 'a': dat[i] = 0; break;
case 'c': dat[i] = 1; break;
case 'g': dat[i] = 2; break;
case 't': dat[i] = 3; break;
default: dat[i] = -1;
}
}
}
}
static void process_iupac_gt(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str)
{
if ( !fmt->ready )
init_format_iupac(convert, line, fmt);
if ( fmt->fmt==NULL )
{
kputc('.', str);
return;
}
assert( fmt->fmt->type==BCF_BT_INT8 );
static const char iupac[4][4] = { {'A','M','R','W'},{'M','C','S','Y'},{'R','S','G','K'},{'W','Y','K','T'} };
int8_t *dat = (int8_t*)convert->dat;
int8_t *x = (int8_t*)(fmt->fmt->p + isample*fmt->fmt->size); // FIXME: does not work with n_alt >= 64
int l = 0;
while ( l<fmt->fmt->n && x[l]!=bcf_int8_vector_end && x[l]!=bcf_int8_missing ) l++;
if ( l==2 )
{
// diploid
int ia = (x[0]>>1) - 1, ib = (x[1]>>1) - 1;
if ( ia>=0 && ia<line->n_allele && ib>=0 && ib<line->n_allele && dat[ia]>=0 && dat[ib]>=0 )
{
kputc(iupac[dat[ia]][dat[ib]], str);
return;
}
}
for (l = 0; l < fmt->fmt->n && x[l] != bcf_int8_vector_end; ++l)
{
if (l) kputc("/|"[x[l]&1], str);
if (x[l]>>1)
{
int ial = (x[l]>>1) - 1;
kputs(line->d.allele[ial], str);
}
else
kputc('.', str);
}
if (l == 0) kputc('.', str);
}
static void process_sample(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str)
{
kputs(convert->header->samples[isample], str);
}
static void process_sep(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str) { if (fmt->key) kputs(fmt->key, str); }
static void process_is_ts(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str)
{
int is_ts = 0;
if ( bcf_get_variant_types(line) & (VCF_SNP|VCF_MNP) )
is_ts = abs(bcf_acgt2int(*line->d.allele[0])-bcf_acgt2int(*line->d.allele[1])) == 2 ? 1 : 0;
kputc(is_ts ? '1' : '0', str);
}
static void process_type(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str)
{
int line_type = bcf_get_variant_types(line);
int i = 0;
if ( line_type == VCF_REF ) { kputs("REF", str); i++; }
if ( line_type & VCF_SNP ) { if (i) kputc(',',str); kputs("SNP", str); i++; }
if ( line_type & VCF_MNP ) { if (i) kputc(',',str); kputs("MNP", str); i++; }
if ( line_type & VCF_INDEL ) { if (i) kputc(',',str); kputs("INDEL", str); i++; }
if ( line_type & VCF_OTHER ) { if (i) kputc(',',str); kputs("OTHER", str); i++; }
if ( line_type & VCF_BND ) { if (i) kputc(',',str); kputs("BND", str); i++; }
}
static void process_line(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str)
{
vcf_format1(convert->header, line, str);
}
static void process_chrom_pos_id(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str)
{
if ( line->d.id[0]!='.' || line->d.id[1] )
{
// ID is present
kputs(line->d.id, str);
}
else
{
// use CHROM:POS instead of ID
kputs(convert->header->id[BCF_DT_CTG][line->rid].key, str);
kputc(':', str);
kputw(line->pos+1, str);
}
}
static void process_gt_to_prob3(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str)
{
int m,n,i;
m = convert->ndat / sizeof(int32_t);
n = bcf_get_genotypes(convert->header,line,&convert->dat,&m);
convert->ndat = m * sizeof(int32_t);
if ( n<=0 )
{
// Throw an error or silently proceed?
//
// for (i=0; i<convert->nsamples; i++) kputs(" 0.33 0.33 0.33", str);
// return;
error("Error parsing GT tag at %s:%d\n", bcf_seqname(convert->header,line),line->pos+1);
}
n /= convert->nsamples;
for (i=0; i<convert->nsamples; i++)
{
int32_t *ptr = (int32_t*)convert->dat + i*n;
int j;
for (j=0; j<n; j++)
if ( ptr[j]==bcf_int32_vector_end ) break;
if ( j==2 )
{
// diploid
if ( bcf_gt_is_missing(ptr[0]) )
kputs(" 0.33 0.33 0.33", str);
else if ( bcf_gt_allele(ptr[0])!=bcf_gt_allele(ptr[1]) )
kputs(" 0 1 0", str); // HET
else if ( bcf_gt_allele(ptr[0])==1 )
kputs(" 0 0 1", str); // ALT HOM, first ALT allele
else
kputs(" 1 0 0", str); // REF HOM or something else than first ALT
}
else if ( j==1 )
{
// haploid
if ( bcf_gt_is_missing(ptr[0]) )
kputs(" 0.5 0.0 0.5", str);
else if ( bcf_gt_allele(ptr[0])==1 )
kputs(" 0 0 1", str); // first ALT allele
else
kputs(" 1 0 0", str); // REF or something else than first ALT
}
else error("FIXME: not ready for ploidy %d\n", j);
}
}
static void process_pl_to_prob3(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str)
{
int m,n,i;
m = convert->ndat / sizeof(int32_t);
n = bcf_get_format_int32(convert->header,line,"PL",&convert->dat,&m);
convert->ndat = m * sizeof(int32_t);
if ( n<=0 )
{
// Throw an error or silently proceed?
//
// for (i=0; i<convert->nsamples; i++) kputs(" 0.33 0.33 0.33", str);
// return;
error("Error parsing PL tag at %s:%d\n", bcf_seqname(convert->header,line),line->pos+1);
}
n /= convert->nsamples;
for (i=0; i<convert->nsamples; i++)
{
int32_t *ptr = (int32_t*)convert->dat + i*n;
int j;
float sum = 0;
for (j=0; j<n; j++)
{
if ( ptr[j]==bcf_int32_vector_end ) break;
sum += pow(10,-0.1*ptr[j]);
}
if ( j==line->n_allele )
{
// haploid
kputc(' ',str);
ksprintf(str,"%f",pow(10,-0.1*ptr[0])/sum);
kputs(" 0 ", str);
ksprintf(str,"%f",pow(10,-0.1*ptr[1])/sum);
}
else
{
// diploid
kputc(' ',str);
ksprintf(str,"%f",pow(10,-0.1*ptr[0])/sum);
kputc(' ',str);
ksprintf(str,"%f",pow(10,-0.1*ptr[1])/sum);
kputc(' ',str);
ksprintf(str,"%f",pow(10,-0.1*ptr[2])/sum);
}
}
}
static void process_gp_to_prob3(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str)
{
int m,n,i;
m = convert->ndat / sizeof(float);
n = bcf_get_format_float(convert->header,line,"GP",&convert->dat,&m);
convert->ndat = m * sizeof(float);
if ( n<=0 )
{
// Throw an error or silently proceed?
//
// for (i=0; i<convert->nsamples; i++) kputs(" 0.33 0.33 0.33", str);
// return;
error("Error parsing GP tag at %s:%d\n", bcf_seqname(convert->header,line),line->pos+1);
}
n /= convert->nsamples;
for (i=0; i<convert->nsamples; i++)
{
float sum = 0, *ptr = (float*)convert->dat + i*n;
int j;
for (j=0; j<n; j++)
{
if ( ptr[j]==bcf_int32_vector_end ) break;
if ( ptr[j]==bcf_int32_missing ) { ptr[j]=0; continue; }
if ( ptr[j]<0 || ptr[j]>1 ) error("[%s:%d:%f] GP value outside range [0,1]; bcftools convert expects the VCF4.3+ spec for the GP field encoding genotype posterior probabilities", bcf_seqname(convert->header,line),line->pos+1,ptr[j]);
sum+=ptr[j];
}
if ( j==line->n_allele )
ksprintf(str," %f %f %f",ptr[0],0.,ptr[1]); // haploid
else
ksprintf(str," %f %f %f",ptr[0],ptr[1],ptr[2]); // diploid
}
}
static void process_gt_to_hap(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str)
{
// https://mathgen.stats.ox.ac.uk/impute/impute_v2.html#-known_haps_g
// File containing known haplotypes for the study cohort. The format
// is the same as the output format from IMPUTE2's -phase option:
// five header columns (as in the -g file) followed by two columns
// (haplotypes) per individual. Allowed values in the haplotype
// columns are 0, 1, and ?.
// If your study dataset is fully phased, you can replace the -g file
// with a -known_haps_g file. This will cause IMPUTE2 to perform
// haploid imputation, although it will still report diploid imputation
// probabilities in the main output file. If any genotypes are missing,
// they can be marked as '? ?' (two question marks separated by one
// space) in the input file. (The program does not allow just one
// allele from a diploid genotype to be missing.) If the reference
// panels are also phased, IMPUTE2 will perform a single, fast
// imputation step rather than its standard MCMC module this is how
// the program imputes into pre-phased GWAS haplotypes.
// The -known_haps_g file can also be used to specify study
// genotypes that are "partially" phased, in the sense that some
// genotypes are phased relative to a fixed reference point while
// others are not. We anticipate that this will be most useful when
// trying to phase resequencing data onto a scaffold of known
// haplotypes. To mark a known genotype as unphased, place an
// asterisk immediately after each allele, with no space between
// the allele (0/1) and the asterisk (*); e.g., "0* 1*" for a
// heterozygous genotype of unknown phase.
int i, gt_id = bcf_hdr_id2int(convert->header, BCF_DT_ID, "GT");
if ( !bcf_hdr_idinfo_exists(convert->header,BCF_HL_FMT,gt_id) )
error("FORMAT/GT tag not present at %s:%d\n", bcf_seqname(convert->header, line), line->pos+1);
if ( !(line->unpacked & BCF_UN_FMT) ) bcf_unpack(line, BCF_UN_FMT);
bcf_fmt_t *fmt_gt = NULL;
for (i=0; i<line->n_fmt; i++)
if ( line->d.fmt[i].id==gt_id ) { fmt_gt = &line->d.fmt[i]; break; }
if ( !fmt_gt )
error("FORMAT/GT tag not present at %s:%d\n", bcf_seqname(convert->header, line), line->pos+1);
// Alloc all memory in advance to avoid kput routines. The biggest allowed allele index is 99
if ( line->n_allele > 100 )
error("Too many alleles (%d) at %s:%d\n", line->n_allele, bcf_seqname(convert->header, line), line->pos+1);
if ( ks_resize(str, str->l+convert->nsamples*8) != 0 )
error("Could not alloc %d bytes\n", str->l + convert->nsamples*8);
if ( fmt_gt->type!=BCF_BT_INT8 ) // todo: use BRANCH_INT if the VCF is valid
error("Uh, too many alleles (%d) or redundant BCF representation at %s:%d\n", line->n_allele, bcf_seqname(convert->header, line), line->pos+1);
if ( fmt_gt->n!=1 && fmt_gt->n!=2 )
error("Uh, ploidy of %d not supported, see %s:%d\n", fmt_gt->n, bcf_seqname(convert->header, line), line->pos+1);
int8_t *ptr = ((int8_t*) fmt_gt->p) - fmt_gt->n;
for (i=0; i<convert->nsamples; i++)
{
ptr += fmt_gt->n;
if ( fmt_gt->n==1 ) // haploid genotypes
{
if ( ptr[0]==2 ) /* 0 */
{
str->s[str->l++] = '0'; str->s[str->l++] = ' '; str->s[str->l++] = '-'; str->s[str->l++] = ' ';
}
else if ( ptr[0]==bcf_int32_missing ) /* . */
{
str->s[str->l++] = '?'; str->s[str->l++] = ' '; str->s[str->l++] = '?'; str->s[str->l++] = ' ';
}
else if ( ptr[0]==4 ) /* 1 */
{
str->s[str->l++] = '1'; str->s[str->l++] = ' '; str->s[str->l++] = '-'; str->s[str->l++] = ' ';
}
else
{
kputw(bcf_gt_allele(ptr[0]),str); str->s[str->l++] = ' '; str->s[str->l++] = '-'; str->s[str->l++] = ' ';
}
}
else if ( ptr[0]==2 )
{
if ( ptr[1]==3 ) /* 0|0 */
{
str->s[str->l++] = '0'; str->s[str->l++] = ' '; str->s[str->l++] = '0'; str->s[str->l++] = ' ';
}
else if ( ptr[1]==5 ) /* 0|1 */
{
str->s[str->l++] = '0'; str->s[str->l++] = ' '; str->s[str->l++] = '1'; str->s[str->l++] = ' ';
}
else if ( ptr[1]==bcf_int8_vector_end ) /* 0 */
{
str->s[str->l++] = '0'; str->s[str->l++] = ' '; str->s[str->l++] = '-'; str->s[str->l++] = ' ';
}
else if ( ptr[1]==2 ) /* 0/0 */
{
str->s[str->l++] = '0'; str->s[str->l++] = '*'; str->s[str->l++] = ' '; str->s[str->l++] = '0'; str->s[str->l++] = '*'; str->s[str->l++] = ' ';
}
else if ( ptr[1]==4 ) /* 0/1 */
{
str->s[str->l++] = '0'; str->s[str->l++] = '*'; str->s[str->l++] = ' '; str->s[str->l++] = '1'; str->s[str->l++] = '*'; str->s[str->l++] = ' ';
}
else if ( bcf_gt_is_missing(ptr[1]) ) /* 0/. */
{
str->s[str->l++] = '?'; str->s[str->l++] = ' '; str->s[str->l++] = '?'; str->s[str->l++] = ' ';
}
else if ( bcf_gt_is_phased(ptr[1]) ) /* 0|x */
{
str->s[str->l++] = '0'; str->s[str->l++] = ' ';
kputw(bcf_gt_allele(ptr[1]),str);
str->s[str->l++] = ' ';
}
else /* 0/x */
{
str->s[str->l++] = '0'; str->s[str->l++] = '*'; str->s[str->l++] = ' ';
kputw(bcf_gt_allele(ptr[1]),str);
str->s[str->l++] = '*'; str->s[str->l++] = ' ';
}
}
else if ( ptr[0]==4 )
{
if ( ptr[1]==3 ) /* 1|0 */
{
str->s[str->l++] = '1'; str->s[str->l++] = ' '; str->s[str->l++] = '0'; str->s[str->l++] = ' ';
}
else if ( ptr[1]==5 ) /* 1|1 */
{
str->s[str->l++] = '1'; str->s[str->l++] = ' '; str->s[str->l++] = '1'; str->s[str->l++] = ' ';
}
else if ( ptr[1]==bcf_int8_vector_end ) /* 1 */
{
str->s[str->l++] = '1'; str->s[str->l++] = ' '; str->s[str->l++] = '-'; str->s[str->l++] = ' ';
}
else if ( ptr[1]==2 ) /* 1/0 */
{
str->s[str->l++] = '1'; str->s[str->l++] = '*'; str->s[str->l++] = ' '; str->s[str->l++] = '0'; str->s[str->l++] = '*'; str->s[str->l++] = ' ';
}
else if ( ptr[1]==4 ) /* 1/1 */
{
str->s[str->l++] = '1'; str->s[str->l++] = '*'; str->s[str->l++] = ' '; str->s[str->l++] = '1'; str->s[str->l++] = '*'; str->s[str->l++] = ' ';
}
else if ( bcf_gt_is_missing(ptr[1]) ) /* 1/. */
{
str->s[str->l++] = '?'; str->s[str->l++] = ' '; str->s[str->l++] = '?'; str->s[str->l++] = ' ';
}
else if ( bcf_gt_is_phased(ptr[1]) ) /* 1|x */
{
str->s[str->l++] = '1'; str->s[str->l++] = ' ';
kputw(bcf_gt_allele(ptr[1]),str);
str->s[str->l++] = ' ';
}
else /* 1/x */
{
str->s[str->l++] = '1'; str->s[str->l++] = '*'; str->s[str->l++] = ' ';
kputw(bcf_gt_allele(ptr[1]),str);
str->s[str->l++] = '*'; str->s[str->l++] = ' ';
}
}
else if ( bcf_gt_is_missing(ptr[0]) )
{
if ( ptr[1]==bcf_int8_vector_end )
{
str->s[str->l++] = '?'; str->s[str->l++] = ' '; str->s[str->l++] = '-'; str->s[str->l++] = ' ';
}
else
{
str->s[str->l++] = '?'; str->s[str->l++] = ' '; str->s[str->l++] = '?'; str->s[str->l++] = ' ';
}
}
else if ( ptr[1]==bcf_int8_vector_end )
{
/* use REF for something else than first ALT */
str->s[str->l++] = '0'; str->s[str->l++] = ' '; str->s[str->l++] = '-'; str->s[str->l++] = ' ';
}
else
{
kputw(bcf_gt_allele(ptr[0]),str);
if ( bcf_gt_is_phased(ptr[1]) ) str->s[str->l++] = '*';
str->s[str->l++] = ' ';
kputw(bcf_gt_allele(ptr[1]),str);
if ( bcf_gt_is_phased(ptr[1]) ) str->s[str->l++] = '*';
str->s[str->l++] = ' ';
}
}
str->s[--str->l] = 0; // delete the last space
}
static void process_gt_to_hap2(convert_t *convert, bcf1_t *line, fmt_t *fmt, int isample, kstring_t *str)
{
// same as process_gt_to_hap but converts haploid genotypes into diploid
int i, gt_id = bcf_hdr_id2int(convert->header, BCF_DT_ID, "GT");
if ( !bcf_hdr_idinfo_exists(convert->header,BCF_HL_FMT,gt_id) )
error("FORMAT/GT tag not present at %s:%d\n", bcf_seqname(convert->header, line), line->pos+1);
if ( !(line->unpacked & BCF_UN_FMT) ) bcf_unpack(line, BCF_UN_FMT);
bcf_fmt_t *fmt_gt = NULL;
for (i=0; i<line->n_fmt; i++)
if ( line->d.fmt[i].id==gt_id ) { fmt_gt = &line->d.fmt[i]; break; }
if ( !fmt_gt )
error("FORMAT/GT tag not present at %s:%d\n", bcf_seqname(convert->header, line), line->pos+1);
// Alloc all memory in advance to avoid kput routines. The biggest allowed allele index is 99
if ( line->n_allele > 100 )
error("Too many alleles (%d) at %s:%d\n", line->n_allele, bcf_seqname(convert->header, line), line->pos+1);
if ( ks_resize(str, str->l+convert->nsamples*8) != 0 )
error("Could not alloc %d bytes\n", str->l + convert->nsamples*8);
if ( fmt_gt->type!=BCF_BT_INT8 ) // todo: use BRANCH_INT if the VCF is valid
error("Uh, too many alleles (%d) or redundant BCF representation at %s:%d\n", line->n_allele, bcf_seqname(convert->header, line), line->pos+1);
int8_t *ptr = ((int8_t*) fmt_gt->p) - fmt_gt->n;
for (i=0; i<convert->nsamples; i++)
{
ptr += fmt_gt->n;
if ( ptr[0]==2 )
{
if ( ptr[1]==3 ) /* 0|0 */
{
str->s[str->l++] = '0'; str->s[str->l++] = ' '; str->s[str->l++] = '0'; str->s[str->l++] = ' ';
}
else if ( ptr[1]==5 ) /* 0|1 */
{
str->s[str->l++] = '0'; str->s[str->l++] = ' '; str->s[str->l++] = '1'; str->s[str->l++] = ' ';
}
else if ( ptr[1]==bcf_int8_vector_end ) /* 0 -> 0|0 */
{
str->s[str->l++] = '0'; str->s[str->l++] = ' '; str->s[str->l++] = '0'; str->s[str->l++] = ' ';
}
else if ( ptr[1]==2 ) /* 0/0 */
{
str->s[str->l++] = '0'; str->s[str->l++] = '*'; str->s[str->l++] = ' '; str->s[str->l++] = '0'; str->s[str->l++] = '*'; str->s[str->l++] = ' ';
}
else if ( ptr[1]==4 ) /* 0/1 */
{
str->s[str->l++] = '0'; str->s[str->l++] = '*'; str->s[str->l++] = ' '; str->s[str->l++] = '1'; str->s[str->l++] = '*'; str->s[str->l++] = ' ';
}
else if ( bcf_gt_is_missing(ptr[1]) ) /* 0/. */
{
str->s[str->l++] = '?'; str->s[str->l++] = ' '; str->s[str->l++] = '?'; str->s[str->l++] = ' ';
}
else if ( bcf_gt_is_phased(ptr[1]) ) /* 0|x */
{
str->s[str->l++] = '0'; str->s[str->l++] = ' ';
kputw(bcf_gt_allele(ptr[1]),str);
str->s[str->l++] = ' ';
}
else /* 0/x */
{
str->s[str->l++] = '0'; str->s[str->l++] = '*'; str->s[str->l++] = ' ';
kputw(bcf_gt_allele(ptr[1]),str);
str->s[str->l++] = '*'; str->s[str->l++] = ' ';
}
}
else if ( ptr[0]==4 )
{
if ( ptr[1]==3 ) /* 1|0 */
{
str->s[str->l++] = '1'; str->s[str->l++] = ' '; str->s[str->l++] = '0'; str->s[str->l++] = ' ';
}
else if ( ptr[1]==5 ) /* 1|1 */
{
str->s[str->l++] = '1'; str->s[str->l++] = ' '; str->s[str->l++] = '1'; str->s[str->l++] = ' ';
}
else if ( ptr[1]==bcf_int8_vector_end ) /* 1 -> 1|1 */
{
str->s[str->l++] = '1'; str->s[str->l++] = ' '; str->s[str->l++] = '1'; str->s[str->l++] = ' ';
}
else if ( ptr[1]==2 ) /* 1/0 */
{
str->s[str->l++] = '1'; str->s[str->l++] = '*'; str->s[str->l++] = ' '; str->s[str->l++] = '0'; str->s[str->l++] = '*'; str->s[str->l++] = ' ';
}
else if ( ptr[1]==4 ) /* 1/1 */
{
str->s[str->l++] = '1'; str->s[str->l++] = '*'; str->s[str->l++] = ' '; str->s[str->l++] = '1'; str->s[str->l++] = '*'; str->s[str->l++] = ' ';
}
else if ( bcf_gt_is_missing(ptr[1]) ) /* 1/. */
{
str->s[str->l++] = '?'; str->s[str->l++] = ' '; str->s[str->l++] = '?'; str->s[str->l++] = ' ';
}
else if ( bcf_gt_is_phased(ptr[1]) ) /* 1|x */
{
str->s[str->l++] = '1'; str->s[str->l++] = ' ';
kputw(bcf_gt_allele(ptr[1]),str);
str->s[str->l++] = ' ';
}
else /* 1/x */
{
str->s[str->l++] = '1'; str->s[str->l++] = '*'; str->s[str->l++] = ' ';
kputw(bcf_gt_allele(ptr[1]),str);
str->s[str->l++] = '*'; str->s[str->l++] = ' ';
}
}
else if ( bcf_gt_is_missing(ptr[0]) )