forked from samtools/bcftools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcfconvert.c
1531 lines (1378 loc) · 60.1 KB
/
vcfconvert.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
/* vcfconvert.c -- convert between VCF/BCF and related formats.
Copyright (C) 2013-2017 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 <strings.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 <htslib/faidx.h>
#include <htslib/vcf.h>
#include <htslib/bgzf.h>
#include <htslib/synced_bcf_reader.h>
#include <htslib/vcfutils.h>
#include <htslib/kseq.h>
#include "bcftools.h"
#include "filter.h"
#include "convert.h"
#include "tsv2vcf.h"
// Logic of the filters: include or exclude sites which match the filters?
#define FLT_INCLUDE 1
#define FLT_EXCLUDE 2
typedef struct _args_t args_t;
struct _args_t
{
faidx_t *ref;
filter_t *filter;
char *filter_str;
int filter_logic; // include or exclude sites which match the filters? One of FLT_INCLUDE/FLT_EXCLUDE
convert_t *convert;
bcf_srs_t *files;
bcf_hdr_t *header;
void (*convert_func)(struct _args_t *);
struct {
int total, skipped, hom_rr, het_ra, hom_aa, het_aa, missing;
} n;
kstring_t str;
int32_t *gts;
float *flt;
int rev_als, output_vcf_ids, hap2dip, output_chrom_first_col;
int nsamples, *samples, sample_is_file, targets_is_file, regions_is_file, output_type;
char **argv, *sample_list, *targets_list, *regions_list, *tag, *columns;
char *outfname, *infname, *ref_fname, *sex_fname;
int argc, n_threads, record_cmd_line;
};
static void destroy_data(args_t *args)
{
if ( args->ref ) fai_destroy(args->ref);
if ( args->convert) convert_destroy(args->convert);
if ( args->filter ) filter_destroy(args->filter);
free(args->samples);
if ( args->files ) bcf_sr_destroy(args->files);
}
static void open_vcf(args_t *args, const char *format_str)
{
args->files = bcf_sr_init();
if ( args->n_threads && bcf_sr_set_threads(args->files, args->n_threads)!=0 )
error("Could not initialize --threads %d\n", args->n_threads);
if ( args->regions_list )
{
if ( bcf_sr_set_regions(args->files, args->regions_list, args->regions_is_file)<0 )
error("Failed to read the regions: %s\n", args->regions_list);
}
if ( args->targets_list )
{
if ( bcf_sr_set_targets(args->files, args->targets_list, args->targets_is_file, 0)<0 )
error("Failed to read the targets: %s\n", args->targets_list);
}
if ( !bcf_sr_add_reader(args->files, args->infname) )
error("Failed to open %s: %s\n", args->infname,bcf_sr_strerror(args->files->errnum));
args->header = args->files->readers[0].header;
if ( args->filter_str )
args->filter = filter_init(args->header, args->filter_str);
int i, nsamples = 0, *samples = NULL;
if ( args->sample_list && strcmp("-",args->sample_list) )
{
for (i=0; i<args->files->nreaders; i++)
{
int ret = bcf_hdr_set_samples(args->files->readers[i].header,args->sample_list,args->sample_is_file);
if ( ret<0 ) error("Error parsing the sample list\n");
else if ( ret>0 ) error("Sample name mismatch: sample #%d not found in the header\n", ret);
}
if ( args->sample_list[0]!='^' )
{
// the sample ordering may be different if not negated
int n;
char **smpls = hts_readlist(args->sample_list, args->sample_is_file, &n);
if ( !smpls ) error("Could not parse %s\n", args->sample_list);
if ( n!=bcf_hdr_nsamples(args->files->readers[0].header) )
error("The number of samples does not match, perhaps some are present multiple times?\n");
nsamples = bcf_hdr_nsamples(args->files->readers[0].header);
samples = (int*) malloc(sizeof(int)*nsamples);
for (i=0; i<n; i++)
{
samples[i] = bcf_hdr_id2int(args->files->readers[0].header, BCF_DT_SAMPLE,smpls[i]);
free(smpls[i]);
}
free(smpls);
}
}
if ( format_str ) args->convert = convert_init(args->header, samples, nsamples, format_str);
free(samples);
}
static int tsv_setter_chrom_pos_ref_alt(tsv_t *tsv, bcf1_t *rec, void *usr)
{
args_t *args = (args_t*) usr;
char tmp, *se = tsv->ss, *ss = tsv->ss;
while ( se < tsv->se && *se!=':' ) se++;
if ( *se!=':' ) error("Could not parse CHROM in CHROM:POS_REF_ALT id: %s\n", tsv->ss);
tmp = *se; *se = 0;
rec->rid = bcf_hdr_name2id(args->header,ss);
if ( rec->rid<0 ) error("Could not determine sequence name or multiple sequences present: %s\n", tsv->ss);
*se = tmp;
// POS
rec->pos = strtol(se+1,&ss,10);
if ( ss==se+1 ) error("Could not parse POS in CHROM:POS_REF_ALT: %s\n", tsv->ss);
rec->pos--;
// REF,ALT
args->str.l = 0;
se = ++ss;
while ( se < tsv->se && *se!='_' ) se++;
if ( *se!='_' ) error("Could not parse REF in CHROM:POS_REF_ALT id: %s\n", tsv->ss);
kputsn(ss,se-ss,&args->str);
ss = ++se;
while ( se < tsv->se && *se!='_' && isspace(*tsv->se) ) se++;
if ( se < tsv->se && *se!='_' && isspace(*tsv->se) ) error("Could not parse ALT in CHROM:POS_REF_ALT id: %s\n", tsv->ss);
kputc(',',&args->str);
kputsn(ss,se-ss,&args->str);
bcf_update_alleles_str(args->header, rec, args->str.s);
// END - optional
if (*se && *se=='_') {
long end = strtol(se+1,&ss,10);
if ( ss==se+1 ) error("Could not parse END in CHROM:POS_REF_ALT_END: %s\n", tsv->ss);
bcf_update_info_int32(args->header, rec, "END", &end, 1);
}
return 0;
}
static int tsv_setter_verify_pos(tsv_t *tsv, bcf1_t *rec, void *usr)
{
char *se;
int pos = strtol(tsv->ss,&se,10);
if ( tsv->ss==se ) error("Could not parse POS: %s\n", tsv->ss);
if ( rec->pos != pos-1 ) error("POS mismatch: %s\n", tsv->ss);
return 0;
}
static int tsv_setter_verify_ref_alt(tsv_t *tsv, bcf1_t *rec, void *usr)
{
args_t *args = (args_t*) usr;
args->rev_als = 0;
char tmp = *tsv->se; *tsv->se = 0;
if ( strcmp(tsv->ss,rec->d.allele[0]) )
{
if ( strcmp(tsv->ss,rec->d.allele[1]) ) { *tsv->se = tmp; error("REF/ALT mismatch: [%s][%s]\n", tsv->ss,rec->d.allele[1]); }
args->rev_als = 1;
}
*tsv->se = tmp;
while ( *tsv->se && isspace(*tsv->se) ) tsv->se++;
tsv->ss = tsv->se;
while ( *tsv->se && !isspace(*tsv->se) ) tsv->se++;
tmp = *tsv->se; *tsv->se = 0;
if ( !args->rev_als && strcmp(tsv->ss,rec->d.allele[1]) ) { *tsv->se = tmp; error("REF/ALT mismatch: [%s][%s]\n", tsv->ss,rec->d.allele[1]); }
else if ( args->rev_als && strcmp(tsv->ss,rec->d.allele[0]) ) { *tsv->se = tmp; error("REF/ALT mismatch: [%s][%s]\n", tsv->ss,rec->d.allele[0]); }
*tsv->se = tmp;
return 0;
}
static int tsv_setter_gt_gp(tsv_t *tsv, bcf1_t *rec, void *usr)
{
args_t *args = (args_t*) usr;
int i, nsamples = bcf_hdr_nsamples(args->header);
for (i=0; i<nsamples; i++)
{
float aa,ab,bb;
aa = strtod(tsv->ss, &tsv->se);
if ( tsv->ss==tsv->se ) { fprintf(stderr,"Could not parse first value of %d-th sample\n", i+1); return -1; }
tsv->ss = tsv->se+1;
ab = strtod(tsv->ss, &tsv->se);
if ( tsv->ss==tsv->se ) { fprintf(stderr,"Could not parse second value of %d-th sample\n", i+1); return -1; }
tsv->ss = tsv->se+1;
bb = strtod(tsv->ss, &tsv->se);
if ( tsv->ss==tsv->se ) { fprintf(stderr,"Could not parse third value of %d-th sample\n", i+1); return -1; }
tsv->ss = tsv->se+1;
if ( args->rev_als ) { float tmp = bb; bb = aa; aa = tmp; }
args->flt[3*i+0] = aa;
args->flt[3*i+1] = ab;
args->flt[3*i+2] = bb;
if ( aa >= ab )
{
if ( aa >= bb ) args->gts[2*i+0] = args->gts[2*i+1] = bcf_gt_unphased(0);
else args->gts[2*i+0] = args->gts[2*i+1] = bcf_gt_unphased(1);
}
else if ( ab >= bb )
{
args->gts[2*i+0] = bcf_gt_unphased(0);
args->gts[2*i+1] = bcf_gt_unphased(1);
}
else args->gts[2*i+0] = args->gts[2*i+1] = bcf_gt_unphased(1);
}
if ( *tsv->se ) error("Could not parse: %s\n", tsv->ss);
if ( bcf_update_genotypes(args->header,rec,args->gts,nsamples*2) ) error("Could not update GT field\n");
if ( bcf_update_format_float(args->header,rec,"GP",args->flt,nsamples*3) ) error("Could not update GP field\n");
return 0;
}
static int tsv_setter_haps(tsv_t *tsv, bcf1_t *rec, void *usr)
{
args_t *args = (args_t*) usr;
int i, nsamples = bcf_hdr_nsamples(args->header);
int32_t a0, a1;
if ( args->rev_als ) { a0 = bcf_gt_phased(1); a1 = bcf_gt_phased(0); }
else { a0 = bcf_gt_phased(0); a1 = bcf_gt_phased(1); }
// up is short for "unphased"
int nup = 0;
for (i=0; i<nsamples; i++)
{
char *ss = tsv->ss + 4*i + nup;
int up = 0, all;
for (all=0; all < 2; all++){
// checking for premature ending
if ( !ss[0] || !ss[1] || !ss[2] ||
(up && (!ss[3] || !ss[4]) ) )
{
fprintf(stderr,"Wrong number of fields at %d-th sample ([%c][%c][%c]). ",i+1,ss[0],ss[1],ss[2]);
return -1;
}
switch(ss[all*2+up]){
case '0':
args->gts[2*i+all] = a0;
break;
case '1' :
args->gts[2*i+all] = a1;
break;
case '?' :
// there is no macro to express phased missing allele
args->gts[2*i+all] = bcf_gt_phased(-1);
break;
case '-' :
args->gts[2*i+all] = bcf_int32_vector_end;
break;
default :
fprintf(stderr,"Could not parse: [%c][%s]\n", ss[all*2+up],tsv->ss);
return -1;
}
if( ss[all*2+up+1]=='*' ) up = up + 1;
}
if(up && up != 2)
{
fprintf(stderr,"Missing unphased marker '*': [%c][%s]", ss[2+up], tsv->ss);
return -1;
}
// change alleles to unphased if the alleles are unphased
if ( up )
{
args->gts[2*i] = bcf_gt_unphased(bcf_gt_allele(args->gts[2*i]));
args->gts[2*i+1] = bcf_gt_unphased(bcf_gt_allele(args->gts[2*i+1]));
}
nup = nup + up;
}
if ( tsv->ss[(nsamples-1)*4+3+nup] )
{
fprintf(stderr,"nup: %d", nup);
fprintf(stderr,"Wrong number of fields (%d-th column = [%c]). ", nsamples*2,tsv->ss[(nsamples-1)*4+nup]);
return -1;
}
if ( bcf_update_genotypes(args->header,rec,args->gts,nsamples*2) ) error("Could not update GT field\n");
return 0;
}
static void gensample_to_vcf(args_t *args)
{
/*
* Inpute: IMPUTE2 output (indentation changed here for clarity):
*
* 20:62116619_C_T 20:62116619 62116619 C T 0.969 0.031 0 ...
* --- 20:62116698_C_A 62116698 C A 1 0 0 ...
*
* Second column is expected in the form of CHROM:POS_REF_ALT. We use second
* column because the first can be empty ("--") when filling sites from reference
* panel.
*
* Output: VCF with filled GT,GP
*
*/
kstring_t line = {0,0,0};
char *gen_fname = NULL, *sample_fname = NULL;
sample_fname = strchr(args->infname,',');
if ( !sample_fname )
{
args->str.l = 0;
ksprintf(&args->str,"%s.gen.gz", args->infname);
gen_fname = strdup(args->str.s);
args->str.l = 0;
ksprintf(&args->str,"%s.samples", args->infname);
sample_fname = strdup(args->str.s);
}
else
{
*sample_fname = 0;
gen_fname = strdup(args->infname);
sample_fname = strdup(sample_fname+1);
}
htsFile *gen_fh = hts_open(gen_fname, "r");
if ( !gen_fh ) error("Could not read: %s\n", gen_fname);
if ( hts_getline(gen_fh, KS_SEP_LINE, &line) <= 0 ) error("Empty file: %s\n", gen_fname);
// Find out the chromosome name, sample names, init and print the VCF header
args->str.l = 0;
char *ss, *se = line.s;
while ( *se && !isspace(*se) ) se++;
if ( !*se ) error("Could not parse %s: %s\n", gen_fname,line.s);
ss = se+1;
se = strchr(ss,':');
if ( !se ) error("Expected CHROM:POS_REF_ALT in second column of %s\n", gen_fname);
kputsn(ss, se-ss, &args->str);
tsv_t *tsv = tsv_init("-,CHROM_POS_REF_ALT,POS,REF_ALT,GT_GP");
tsv_register(tsv, "CHROM_POS_REF_ALT", tsv_setter_chrom_pos_ref_alt, args);
tsv_register(tsv, "POS", tsv_setter_verify_pos, NULL);
tsv_register(tsv, "REF_ALT", tsv_setter_verify_ref_alt, args);
tsv_register(tsv, "GT_GP", tsv_setter_gt_gp, args);
args->header = bcf_hdr_init("w");
bcf_hdr_append(args->header, "##INFO=<ID=END,Number=1,Type=Integer,Description=\"End position of the variant described in this record\">");
bcf_hdr_append(args->header, "##FORMAT=<ID=GT,Number=1,Type=String,Description=\"Genotype\">");
bcf_hdr_append(args->header, "##FORMAT=<ID=GP,Number=G,Type=Float,Description=\"Genotype Probabilities\">");
bcf_hdr_printf(args->header, "##contig=<ID=%s,length=%d>", args->str.s,0x7fffffff); // MAX_CSI_COOR
if (args->record_cmd_line) bcf_hdr_append_version(args->header, args->argc, args->argv, "bcftools_convert");
int i, nsamples;
char **samples = hts_readlist(sample_fname, 1, &nsamples);
if ( !samples ) error("Could not read %s\n", sample_fname);
for (i=2; i<nsamples; i++)
{
se = samples[i]; while ( *se && !isspace(*se) ) se++;
*se = 0;
bcf_hdr_add_sample(args->header,samples[i]);
}
for (i=0; i<nsamples; i++) free(samples[i]);
free(samples);
htsFile *out_fh = hts_open(args->outfname,hts_bcf_wmode(args->output_type));
if ( out_fh == NULL ) error("Can't write to \"%s\": %s\n", args->outfname, strerror(errno));
if ( args->n_threads ) hts_set_threads(out_fh, args->n_threads);
bcf_hdr_write(out_fh,args->header);
bcf1_t *rec = bcf_init();
nsamples -= 2;
args->gts = (int32_t *) malloc(sizeof(int32_t)*nsamples*2);
args->flt = (float *) malloc(sizeof(float)*nsamples*3);
do
{
bcf_clear(rec);
args->n.total++;
if ( !tsv_parse(tsv, rec, line.s) )
bcf_write(out_fh, args->header, rec);
else
error("Error occurred while parsing: %s\n", line.s);
}
while ( hts_getline(gen_fh, KS_SEP_LINE, &line)>0 );
if ( hts_close(out_fh) ) error("Close failed: %s\n", args->outfname);
if ( hts_close(gen_fh) ) error("Close failed: %s\n", gen_fname);
bcf_hdr_destroy(args->header);
bcf_destroy(rec);
free(sample_fname);
free(gen_fname);
free(args->str.s);
free(line.s);
free(args->gts);
free(args->flt);
tsv_destroy(tsv);
fprintf(stderr,"Number of processed rows: \t%d\n", args->n.total);
}
static void haplegendsample_to_vcf(args_t *args)
{
/*
* Convert from IMPUTE2 hap/legend/sample output files to VCF
*
* hap:
* 0 1 0 1
* legend:
* id position a0 a1
* 1:186946386_G_T 186946386 G T
* sample:
* sample population group sex
* sample1 sample1 sample1 2
* sample2 sample2 sample2 2
*
* Output: VCF with filled GT
*/
kstring_t line = {0,0,0};
char *hap_fname = NULL, *leg_fname = NULL, *sample_fname = NULL;
sample_fname = strchr(args->infname,',');
if ( !sample_fname )
{
args->str.l = 0;
ksprintf(&args->str,"%s.hap.gz", args->infname);
hap_fname = strdup(args->str.s);
args->str.l = 0;
ksprintf(&args->str,"%s.samples", args->infname);
sample_fname = strdup(args->str.s);
args->str.l = 0;
ksprintf(&args->str,"%s.legend.gz", args->infname);
leg_fname = strdup(args->str.s);
}
else
{
char *ss = sample_fname, *se = strchr(ss+1,',');
if ( !se ) error("Could not parse hap/legend/sample file names: %s\n", args->infname);
*ss = 0;
*se = 0;
hap_fname = strdup(args->infname);
leg_fname = strdup(ss+1);
sample_fname = strdup(se+1);
}
htsFile *hap_fh = hts_open(hap_fname, "r");
if ( !hap_fh ) error("Could not read: %s\n", hap_fname);
htsFile *leg_fh = hts_open(leg_fname,"r");
if ( !leg_fh ) error("Could not read: %s\n", leg_fname);
// Eat up first legend line, then determine chromosome name
if ( hts_getline(leg_fh, KS_SEP_LINE, &line) <= 0 ) error("Empty file: %s\n", leg_fname);
if ( hts_getline(leg_fh, KS_SEP_LINE, &line) <= 0 ) error("Empty file: %s\n", leg_fname);
// Find out the chromosome name, sample names, init and print the VCF header
args->str.l = 0;
char *se = strchr(line.s,':');
if ( !se ) error("Expected CHROM:POS_REF_ALT in first column of %s\n", leg_fname);
kputsn(line.s, se-line.s, &args->str);
tsv_t *leg_tsv = tsv_init("CHROM_POS_REF_ALT,POS,REF_ALT");
tsv_register(leg_tsv, "CHROM_POS_REF_ALT", tsv_setter_chrom_pos_ref_alt, args);
tsv_register(leg_tsv, "POS", tsv_setter_verify_pos, NULL);
tsv_register(leg_tsv, "REF_ALT", tsv_setter_verify_ref_alt, args);
tsv_t *hap_tsv = tsv_init("HAPS");
tsv_register(hap_tsv, "HAPS", tsv_setter_haps, args);
args->header = bcf_hdr_init("w");
bcf_hdr_append(args->header, "##INFO=<ID=END,Number=1,Type=Integer,Description=\"End position of the variant described in this record\">");
bcf_hdr_append(args->header, "##FORMAT=<ID=GT,Number=1,Type=String,Description=\"Genotype\">");
bcf_hdr_printf(args->header, "##contig=<ID=%s,length=%d>", args->str.s,0x7fffffff); // MAX_CSI_COOR
if (args->record_cmd_line) bcf_hdr_append_version(args->header, args->argc, args->argv, "bcftools_convert");
int i, nrows, nsamples;
char **samples = hts_readlist(sample_fname, 1, &nrows);
if ( !samples ) error("Could not read %s\n", sample_fname);
nsamples = nrows - 1;
// sample_fname should contain a header line, so need to ignore first row
// returned from hts_readlist (i=1, and not i=0)
for (i=1; i<nrows; i++)
{
se = samples[i]; while ( *se && !isspace(*se) ) se++;
*se = 0;
bcf_hdr_add_sample(args->header,samples[i]);
}
bcf_hdr_add_sample(args->header,NULL);
for (i=0; i<nrows; i++) free(samples[i]);
free(samples);
htsFile *out_fh = hts_open(args->outfname,hts_bcf_wmode(args->output_type));
if ( out_fh == NULL ) error("Can't write to \"%s\": %s\n", args->outfname, strerror(errno));
if ( args->n_threads ) hts_set_threads(out_fh, args->n_threads);
bcf_hdr_write(out_fh,args->header);
bcf1_t *rec = bcf_init();
args->gts = (int32_t *) malloc(sizeof(int32_t)*nsamples*2);
while (1)
{
bcf_clear(rec);
args->n.total++;
if ( tsv_parse(leg_tsv, rec, line.s) )
error("Error occurred while parsing %s: %s\n", leg_fname,line.s);
if ( hts_getline(hap_fh, KS_SEP_LINE, &line)<=0 )
error("Different number of records in %s and %s?\n", leg_fname,hap_fname);
if ( tsv_parse(hap_tsv, rec, line.s) )
error("Error occurred while parsing %s: %s\n", hap_fname,line.s);
bcf_write(out_fh, args->header, rec);
if ( hts_getline(leg_fh, KS_SEP_LINE, &line)<=0 )
{
if ( hts_getline(hap_fh, KS_SEP_LINE, &line)>0 )
error("Different number of records in %s and %s?\n", leg_fname,hap_fname);
break;
}
}
if ( hts_close(out_fh) ) error("Close failed: %s\n", args->outfname);
if ( hts_close(hap_fh) ) error("Close failed: %s\n", hap_fname);
if ( hts_close(leg_fh) ) error("Close failed: %s\n", leg_fname);
bcf_hdr_destroy(args->header);
bcf_destroy(rec);
free(sample_fname);
free(hap_fname);
free(leg_fname);
free(args->str.s);
free(line.s);
free(args->gts);
tsv_destroy(hap_tsv);
tsv_destroy(leg_tsv);
fprintf(stderr,"Number of processed rows: \t%d\n", args->n.total);
}
static void hapsample_to_vcf(args_t *args)
{
/*
* Input: SHAPEIT output
*
* 20:19995888_A_G 20:19995888 19995888 A G 0 0 0 0 ...
*
* First column is expected in the form of CHROM:POS_REF_ALT
*
* Output: VCF with filled GT
*
*/
kstring_t line = {0,0,0};
char *hap_fname = NULL, *sample_fname = NULL;
sample_fname = strchr(args->infname,',');
if ( !sample_fname )
{
args->str.l = 0;
ksprintf(&args->str,"%s.hap.gz", args->infname);
hap_fname = strdup(args->str.s);
args->str.l = 0;
ksprintf(&args->str,"%s.samples", args->infname);
sample_fname = strdup(args->str.s);
}
else
{
*sample_fname = 0;
hap_fname = strdup(args->infname);
sample_fname = strdup(sample_fname+1);
}
htsFile *hap_fh = hts_open(hap_fname, "r");
if ( !hap_fh ) error("Could not read: %s\n", hap_fname);
if ( hts_getline(hap_fh, KS_SEP_LINE, &line) <= 0 ) error("Empty file: %s\n", hap_fname);
// Find out the chromosome name, sample names, init and print the VCF header
args->str.l = 0;
char *se = strchr(line.s,':');
if ( !se ) error("Expected CHROM:POS_REF_ALT in first column of %s\n", hap_fname);
kputsn(line.s, se-line.s, &args->str);
tsv_t *tsv = tsv_init("CHROM_POS_REF_ALT,-,POS,REF_ALT,HAPS");
tsv_register(tsv, "CHROM_POS_REF_ALT", tsv_setter_chrom_pos_ref_alt, args);
tsv_register(tsv, "POS", tsv_setter_verify_pos, NULL);
tsv_register(tsv, "REF_ALT", tsv_setter_verify_ref_alt, args);
tsv_register(tsv, "HAPS", tsv_setter_haps, args);
args->header = bcf_hdr_init("w");
bcf_hdr_append(args->header, "##INFO=<ID=END,Number=1,Type=Integer,Description=\"End position of the variant described in this record\">");
bcf_hdr_append(args->header, "##FORMAT=<ID=GT,Number=1,Type=String,Description=\"Genotype\">");
bcf_hdr_printf(args->header, "##contig=<ID=%s,length=%d>", args->str.s,0x7fffffff); // MAX_CSI_COOR
if (args->record_cmd_line) bcf_hdr_append_version(args->header, args->argc, args->argv, "bcftools_convert");
int i, nsamples;
char **samples = hts_readlist(sample_fname, 1, &nsamples);
if ( !samples ) error("Could not read %s\n", sample_fname);
for (i=2; i<nsamples; i++)
{
se = samples[i]; while ( *se && !isspace(*se) ) se++;
*se = 0;
bcf_hdr_add_sample(args->header,samples[i]);
}
bcf_hdr_add_sample(args->header,NULL);
for (i=0; i<nsamples; i++) free(samples[i]);
free(samples);
htsFile *out_fh = hts_open(args->outfname,hts_bcf_wmode(args->output_type));
if ( out_fh == NULL ) error("Can't write to \"%s\": %s\n", args->outfname, strerror(errno));
if ( args->n_threads ) hts_set_threads(out_fh, args->n_threads);
bcf_hdr_write(out_fh,args->header);
bcf1_t *rec = bcf_init();
nsamples -= 2;
args->gts = (int32_t *) malloc(sizeof(int32_t)*nsamples*2);
do
{
bcf_clear(rec);
args->n.total++;
if ( !tsv_parse(tsv, rec, line.s) )
bcf_write(out_fh, args->header, rec);
else
error("Error occurred while parsing: %s\n", line.s);
}
while ( hts_getline(hap_fh, KS_SEP_LINE, &line)>0 );
if ( hts_close(out_fh) ) error("Close failed: %s\n", args->outfname);
if ( hts_close(hap_fh) ) error("Close failed: %s\n", hap_fname);
bcf_hdr_destroy(args->header);
bcf_destroy(rec);
free(sample_fname);
free(hap_fname);
free(args->str.s);
free(line.s);
free(args->gts);
tsv_destroy(tsv);
fprintf(stderr,"Number of processed rows: \t%d\n", args->n.total);
}
char *init_sample2sex(bcf_hdr_t *hdr, char *sex_fname)
{
int i, nlines;
char *sample2sex = (char*) calloc(bcf_hdr_nsamples(hdr),1);
char **lines = hts_readlist(sex_fname, 1, &nlines);
if ( !lines ) error("Could not read %s\n", sex_fname);
for (i=0; i<nlines; i++)
{
char *se = lines[i]; while ( *se && !isspace(*se) ) se++;
char tmp = *se;
*se = 0;
int id = bcf_hdr_id2int(hdr, BCF_DT_SAMPLE, lines[i]);
*se = tmp;
if ( id<0 ) continue;
while ( *se && isspace(*se) ) se++;
if ( *se=='M' ) sample2sex[id] = '1';
else if ( *se=='F' ) sample2sex[id] = '2';
else error("Could not parse %s: %s\n", sex_fname,lines[i]);
}
for (i=0; i<nlines; i++) free(lines[i]);
free(lines);
for (i=0; i<bcf_hdr_nsamples(hdr); i++)
if ( !sample2sex[i] ) error("Missing sex for sample %s in %s\n", bcf_hdr_int2id(hdr, BCF_DT_SAMPLE, i),sex_fname);
return sample2sex;
}
static void vcf_to_gensample(args_t *args)
{
kstring_t str = {0,0,0};
// insert chrom as first column if needed
if(args->output_chrom_first_col)
kputs("%CHROM ", &str);
else
kputs("%CHROM:%POS\\_%REF\\_%FIRST_ALT ", &str);
// insert rsid as second column if needed
if(args->output_vcf_ids)
kputs("%ID ", &str);
else
kputs("%CHROM:%POS\\_%REF\\_%FIRST_ALT ", &str);
kputs("%POS %REF %FIRST_ALT", &str);
if ( !args->tag || !strcmp(args->tag,"GT") ) kputs("%_GT_TO_PROB3",&str);
else if ( !strcmp(args->tag,"PL") ) kputs("%_PL_TO_PROB3",&str);
else if ( !strcmp(args->tag,"GP") ) kputs("%_GP_TO_PROB3",&str);
else error("todo: --tag %s\n", args->tag);
kputs("\n", &str);
open_vcf(args,str.s);
int ret, gen_compressed = 1, sample_compressed = 0;
char *gen_fname = NULL, *sample_fname = NULL;
str.l = 0;
kputs(args->outfname,&str);
int n_files = 0, i;
char **files = hts_readlist(str.s, 0, &n_files);
if ( n_files==1 )
{
int l = str.l;
kputs(".samples",&str);
sample_fname = strdup(str.s);
str.l = l;
kputs(".gen.gz",&str);
gen_fname = strdup(str.s);
}
else if ( n_files==2 )
{
if (strlen(files[0]) && strcmp(files[0],".")!=0) gen_fname = strdup(files[0]);
if (strlen(files[1]) && strcmp(files[1],".")!=0) sample_fname = strdup(files[1]);
}
else
{
error("Error parsing --gensample filenames: %s\n", args->outfname);
}
for (i=0; i<n_files; i++) free(files[i]);
free(files);
if ( gen_fname && (strlen(gen_fname)<3 || strcasecmp(".gz",gen_fname+strlen(gen_fname)-3)) ) gen_compressed = 0;
if ( sample_fname && strlen(sample_fname)>3 && strcasecmp(".gz",sample_fname+strlen(sample_fname)-3)==0 ) sample_compressed = 0;
if (gen_fname) fprintf(stderr, "Gen file: %s\n", gen_fname);
if (sample_fname) fprintf(stderr, "Sample file: %s\n", sample_fname);
// write samples file
if (sample_fname)
{
char *sample2sex = NULL;
if ( args->sex_fname ) sample2sex = init_sample2sex(args->header,args->sex_fname);
int i;
BGZF *sout = bgzf_open(sample_fname, sample_compressed ? "wg" : "wu");
str.l = 0;
kputs(sample2sex ? "ID_1 ID_2 missing sex\n0 0 0 0\n" : "ID_1 ID_2 missing\n0 0 0\n", &str);
ret = bgzf_write(sout, str.s, str.l);
if ( ret != str.l ) error("Error writing %s: %s\n", sample_fname, strerror(errno));
for (i=0; i<bcf_hdr_nsamples(args->header); i++)
{
str.l = 0;
if ( sample2sex )
ksprintf(&str, "%s %s 0 %c\n", args->header->samples[i],args->header->samples[i],sample2sex[i]);
else
ksprintf(&str, "%s %s 0\n", args->header->samples[i],args->header->samples[i]);
ret = bgzf_write(sout, str.s, str.l);
if ( ret != str.l ) error("Error writing %s: %s\n", sample_fname, strerror(errno));
}
if ( bgzf_close(sout)!=0 ) error("Error closing %s: %s\n", sample_fname, strerror(errno));
free(sample_fname);
free(sample2sex);
}
if (!gen_fname) {
if ( str.m ) free(str.s);
return;
}
int prev_rid = -1, prev_pos = -1;
int no_alt = 0, non_biallelic = 0, filtered = 0, ndup = 0, nok = 0;
BGZF *gout = bgzf_open(gen_fname, gen_compressed ? "wg" : "wu");
while ( bcf_sr_next_line(args->files) )
{
bcf1_t *line = bcf_sr_get_line(args->files,0);
if ( args->filter )
{
int pass = filter_test(args->filter, line, NULL);
if ( args->filter_logic & FLT_EXCLUDE ) pass = pass ? 0 : 1;
if ( !pass ) { filtered++; continue; }
}
// ALT allele is required
if ( line->n_allele<2 ) { no_alt++; continue; }
// biallelic required
if ( line->n_allele>2 ) {
if (!non_biallelic)
fprintf(stderr, "Warning: non-biallelic records are skipped. Consider splitting multi-allelic records into biallelic records using 'bcftools norm -m-'.\n");
non_biallelic++;
continue;
}
// skip duplicate lines, or otherwise shapeit complains
if ( prev_rid==line->rid && prev_pos==line->pos ) { ndup++; continue; }
prev_rid = line->rid;
prev_pos = line->pos;
str.l = 0;
convert_line(args->convert, line, &str);
if ( str.l )
{
int ret = bgzf_write(gout, str.s, str.l);
if ( ret!= str.l ) error("Error writing %s: %s\n", gen_fname,strerror(errno));
nok++;
}
}
fprintf(stderr, "%d records written, %d skipped: %d/%d/%d/%d no-ALT/non-biallelic/filtered/duplicated\n",
nok, no_alt+non_biallelic+filtered+ndup, no_alt, non_biallelic, filtered, ndup);
if ( str.m ) free(str.s);
if ( bgzf_close(gout)!=0 ) error("Error closing %s: %s\n", gen_fname,strerror(errno));
free(gen_fname);
}
static void vcf_to_haplegendsample(args_t *args)
{
kstring_t str = {0,0,0};
if ( args->hap2dip )
kputs("%_GT_TO_HAP2\n", &str);
else
kputs("%_GT_TO_HAP\n", &str);
open_vcf(args,str.s);
int ret, hap_compressed = 1, legend_compressed = 1, sample_compressed = 0;
char *hap_fname = NULL, *legend_fname = NULL, *sample_fname = NULL;
str.l = 0;
kputs(args->outfname,&str);
int n_files = 0, i;
char **files = hts_readlist(str.s, 0, &n_files);
if ( n_files==1 )
{
int l = str.l;
kputs(".samples",&str);
sample_fname = strdup(str.s);
str.l = l;
kputs(".legend.gz",&str);
legend_fname = strdup(str.s);
str.l = l;
kputs(".hap.gz",&str);
hap_fname = strdup(str.s);
}
else if ( n_files==3 )
{
if (strlen(files[0]) && strcmp(files[0],".")!=0) hap_fname = strdup(files[0]);
if (strlen(files[1]) && strcmp(files[1],".")!=0) legend_fname = strdup(files[1]);
if (strlen(files[2]) && strcmp(files[2],".")!=0) sample_fname = strdup(files[2]);
}
else
{
error("Error parsing --hapslegendsample filenames: %s\n", args->outfname);
}
for (i=0; i<n_files; i++) free(files[i]);
free(files);
if ( hap_fname && (strlen(hap_fname)<3 || strcasecmp(".gz",hap_fname+strlen(hap_fname)-3)) ) hap_compressed = 0;
if ( legend_fname && (strlen(legend_fname)<3 || strcasecmp(".gz",legend_fname+strlen(legend_fname)-3)) ) legend_compressed = 0;
if ( sample_fname && strlen(sample_fname)>3 && strcasecmp(".gz",sample_fname+strlen(sample_fname)-3)==0 ) sample_compressed = 0;
if (hap_fname) fprintf(stderr, "Hap file: %s\n", hap_fname);
if (legend_fname) fprintf(stderr, "Legend file: %s\n", legend_fname);
if (sample_fname) fprintf(stderr, "Sample file: %s\n", sample_fname);
// write samples file
if (sample_fname)
{
char *sample2sex = NULL;
if ( args->sex_fname ) sample2sex = init_sample2sex(args->header,args->sex_fname);
int i;
BGZF *sout = bgzf_open(sample_fname, sample_compressed ? "wg" : "wu");
str.l = 0;
kputs("sample population group sex\n", &str);
ret = bgzf_write(sout, str.s, str.l);
if ( ret != str.l ) error("Error writing %s: %s\n", sample_fname, strerror(errno));
for (i=0; i<bcf_hdr_nsamples(args->header); i++)
{
str.l = 0;
ksprintf(&str, "%s %s %s %c\n", args->header->samples[i], args->header->samples[i], args->header->samples[i], sample2sex ? sample2sex[i] : '2');
ret = bgzf_write(sout, str.s, str.l);
if ( ret != str.l ) error("Error writing %s: %s\n", sample_fname, strerror(errno));
}
if ( bgzf_close(sout)!=0 ) error("Error closing %s: %s\n", sample_fname, strerror(errno));
free(sample_fname);
free(sample2sex);
}
if (!hap_fname && !legend_fname) {
if ( str.m ) free(str.s);
return;
}
// open haps and legend outputs
BGZF *hout = hap_fname ? bgzf_open(hap_fname, hap_compressed ? "wg" : "wu") : NULL;
if ( hap_compressed && args->n_threads ) bgzf_thread_pool(hout, args->files->p->pool, args->files->p->qsize);
BGZF *lout = legend_fname ? bgzf_open(legend_fname, legend_compressed ? "wg" : "wu") : NULL;
if (legend_fname) {
str.l = 0;
kputs("id position a0 a1\n", &str);
ret = bgzf_write(lout, str.s, str.l);
if ( ret != str.l ) error("Error writing %s: %s\n", legend_fname, strerror(errno));
}
int no_alt = 0, non_biallelic = 0, filtered = 0, nok = 0;
while ( bcf_sr_next_line(args->files) )
{
bcf1_t *line = bcf_sr_get_line(args->files,0);
if ( args->filter )
{
int pass = filter_test(args->filter, line, NULL);
if ( args->filter_logic & FLT_EXCLUDE ) pass = pass ? 0 : 1;
if ( !pass ) { filtered++; continue; }
}
// ALT allele is required
if ( line->n_allele<2 ) { no_alt++; continue; }
// biallelic required
if ( line->n_allele>2 ) {
if (!non_biallelic)
fprintf(stderr, "Warning: non-biallelic records are skipped. Consider splitting multi-allelic records into biallelic records using 'bcftools norm -m-'.\n");
non_biallelic++;
continue;
}
str.l = 0;
convert_line(args->convert, line, &str);
if ( !str.l ) continue;
// write haps file
if (hap_fname) {
ret = bgzf_write(hout, str.s, str.l); // write hap file
if ( ret != str.l ) error("Error writing %s: %s\n", hap_fname, strerror(errno));
}
if (legend_fname) {
str.l = 0;
if ( args->output_vcf_ids && (line->d.id[0]!='.' || line->d.id[1]!=0) )
ksprintf(&str, "%s %d %s %s\n", line->d.id, line->pos+1, line->d.allele[0], line->d.allele[1]);
else
ksprintf(&str, "%s:%d_%s_%s %d %s %s\n", bcf_seqname(args->header, line), line->pos+1, line->d.allele[0], line->d.allele[1], line->pos+1, line->d.allele[0], line->d.allele[1]);
// write legend file
ret = bgzf_write(lout, str.s, str.l);
if ( ret != str.l ) error("Error writing %s: %s\n", legend_fname, strerror(errno));
}
nok++;
}
fprintf(stderr, "%d records written, %d skipped: %d/%d/%d no-ALT/non-biallelic/filtered\n", nok,no_alt+non_biallelic+filtered, no_alt, non_biallelic, filtered);
if ( str.m ) free(str.s);
if ( hout && bgzf_close(hout)!=0 ) error("Error closing %s: %s\n", hap_fname, strerror(errno));
if ( lout && bgzf_close(lout)!=0 ) error("Error closing %s: %s\n", legend_fname, strerror(errno));
if (hap_fname) free(hap_fname);
if (legend_fname) free(legend_fname);
}
static void vcf_to_hapsample(args_t *args)
{
/*
* WTCCC style haplotypes file
* see https://mathgen.stats.ox.ac.uk/genetics_software/shapeit/shapeit.html#hapsample
*
* These are essentially the haplotypes from the impute2 format with some
* legend info tacked on to the first 5 columns
*
*/
kstring_t str = {0,0,0};
// print ID instead of CHROM:POS_REF_ALT1
if ( args->output_vcf_ids )
kputs("%CHROM %ID %POS %REF %FIRST_ALT ", &str);
else
kputs("%CHROM %CHROM:%POS\\_%REF\\_%FIRST_ALT %POS %REF %FIRST_ALT ", &str);
if ( args->hap2dip )
kputs("%_GT_TO_HAP2\n", &str);
else
kputs("%_GT_TO_HAP\n", &str);
open_vcf(args,str.s);
int ret, hap_compressed = 1, sample_compressed = 0;
char *hap_fname = NULL, *sample_fname = NULL;
str.l = 0;
kputs(args->outfname,&str);
int n_files = 0, i;
char **files = hts_readlist(str.s, 0, &n_files);
if ( n_files==1 )
{
int l = str.l;
kputs(".sample",&str);
sample_fname = strdup(str.s);
str.l = l;
kputs(".hap.gz",&str);
hap_fname = strdup(str.s);
}
else if ( n_files==2 )
{
if (strlen(files[0]) && strcmp(files[0],".")!=0) hap_fname = strdup(files[0]);