forked from samtools/bcftools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcfannotate.c
2799 lines (2637 loc) · 118 KB
/
vcfannotate.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
/* vcfannotate.c -- Annotate and edit VCF/BCF files.
Copyright (C) 2013-2019 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 <dirent.h>
#include <math.h>
#include <inttypes.h>
#include <htslib/vcf.h>
#include <htslib/synced_bcf_reader.h>
#include <htslib/kseq.h>
#include <htslib/khash_str2int.h>
#include "bcftools.h"
#include "vcmp.h"
#include "filter.h"
#include "convert.h"
#include "smpl_ilist.h"
#include "regidx.h"
struct _args_t;
typedef struct _rm_tag_t
{
char *key;
int hdr_id;
void (*handler)(struct _args_t *, bcf1_t *, struct _rm_tag_t *);
}
rm_tag_t;
typedef struct
{
char **cols;
int ncols, mcols;
char **als;
int nals, mals;
kstring_t line;
int rid, start, end;
}
annot_line_t;
#define REPLACE_MISSING 0 // replace only missing values
#define REPLACE_ALL 1 // replace both missing and existing values
#define REPLACE_NON_MISSING 2 // replace only if tgt is not missing
#define SET_OR_APPEND 3 // set new value if missing or non-existent, append otherwise
#define MM_FIRST 0 // if multiple annotation lines overlap a VCF record, use the first, discarding the rest
#define MM_APPEND 1 // append, possibly multiple times
#define MM_UNIQUE 2 // append, only unique values
#define MM_SUM 3
#define MM_AVG 4
#define MM_MIN 5
#define MM_MAX 6
typedef struct _annot_col_t
{
int icol, replace, number; // number: one of BCF_VL_* types
char *hdr_key_src, *hdr_key_dst;
int (*setter)(struct _args_t *, bcf1_t *, struct _annot_col_t *, void*);
int merge_method; // one of the MM_* defines
khash_t(str2int) *mm_str_hash; // lookup table to ensure uniqueness of added string values
kstring_t mm_kstr;
double
mm_dbl_nalloc, // the allocated size --merge-logic values array
mm_dbl_nused, // the number of used elements in the mm_dbl array
mm_dbl_ndat, // the number of merged rows (for calculating the average)
*mm_dbl;
}
annot_col_t;
// Logic of the filters: include or exclude sites which match the filters?
#define FLT_INCLUDE 1
#define FLT_EXCLUDE 2
#define MARK_LISTED 1
#define MARK_UNLISTED 2
typedef struct _args_t
{
bcf_srs_t *files;
bcf_hdr_t *hdr, *hdr_out;
htsFile *out_fh;
int output_type, n_threads;
bcf_sr_regions_t *tgts;
regidx_t *tgt_idx;
regitr_t *tgt_itr;
int tgt_is_bed;
filter_t *filter;
char *filter_str;
int filter_logic; // include or exclude sites which match the filters? One of FLT_INCLUDE/FLT_EXCLUDE
int keep_sites;
rm_tag_t *rm; // tags scheduled for removal
int nrm;
int flt_keep_pass; // when all filters removed, reset to PASS
vcmp_t *vcmp; // for matching annotation and VCF lines by allele
annot_line_t *alines; // buffered annotation lines
int nalines, malines;
int ref_idx, alt_idx, chr_idx, beg_idx, end_idx; // -1 if not present
annot_col_t *cols; // column indexes and setters
int ncols;
char *set_ids_fmt;
convert_t *set_ids;
int set_ids_replace;
int nsmpl_annot;
int *sample_map, nsample_map, sample_is_file; // map[idst] -> isrc
uint8_t *src_smpl_pld, *dst_smpl_pld; // for Number=G format fields
int mtmpi, mtmpf, mtmps;
int mtmpi2, mtmpf2, mtmps2;
int mtmpi3, mtmpf3, mtmps3;
int32_t *tmpi, *tmpi2, *tmpi3;
float *tmpf, *tmpf2, *tmpf3;
char *tmps, *tmps2, **tmpp, **tmpp2;
kstring_t tmpks;
char **argv, *output_fname, *targets_fname, *regions_list, *header_fname;
char *remove_annots, *columns, *rename_chrs, *sample_names, *mark_sites;
char *merge_method_str;
int argc, drop_header, record_cmd_line, tgts_is_vcf, mark_sites_logic, force, single_overlaps;
}
args_t;
char *msprintf(const char *fmt, ...);
int parse_with_payload(const char *line, char **chr_beg, char **chr_end, uint32_t *beg, uint32_t *end, void *payload, void *usr)
{
args_t *args = (args_t*) usr;
int ret = args->tgt_is_bed ? regidx_parse_bed(line, chr_beg, chr_end, beg, end, NULL, NULL) : regidx_parse_tab(line, chr_beg, chr_end, beg, end, NULL, NULL);
if ( ret<0 ) return ret;
*((char **)payload) = strdup(line);
return 0;
}
void free_payload(void *payload)
{
char *str = *((char**)payload);
free(str);
}
void remove_id(args_t *args, bcf1_t *line, rm_tag_t *tag)
{
bcf_update_id(args->hdr,line,NULL);
}
void remove_filter(args_t *args, bcf1_t *line, rm_tag_t *tag)
{
if ( tag->key && tag->hdr_id<0 )
{
error("Error: Cannot proceed, not even with the --force option, bad things could happen.\n"
" Note that \"bcftools annotate -x FILTER\" can be used to remove ALL filters.\n"
" Even better, use \"bcftools view -h\" and \"bcftools reheader\" to fix the header!\n"
);
}
if ( !tag->key ) bcf_update_filter(args->hdr, line, NULL, args->flt_keep_pass);
else bcf_remove_filter(args->hdr, line, tag->hdr_id, args->flt_keep_pass);
}
void remove_qual(args_t *args, bcf1_t *line, rm_tag_t *tag)
{
bcf_float_set_missing(line->qual);
}
void remove_info(args_t *args, bcf1_t *line, rm_tag_t *tag)
{
// remove all INFO fields
if ( !(line->unpacked & BCF_UN_INFO) ) bcf_unpack(line, BCF_UN_INFO);
int i;
for (i=0; i<line->n_info; i++)
{
bcf_info_t *inf = &line->d.info[i];
if ( inf->vptr_free )
{
free(inf->vptr - inf->vptr_off);
inf->vptr_free = 0;
}
line->d.shared_dirty |= BCF1_DIRTY_INF;
inf->vptr = NULL;
inf->vptr_off = inf->vptr_len = 0;
}
}
void remove_info_tag(args_t *args, bcf1_t *line, rm_tag_t *tag)
{
bcf_update_info(args->hdr, line, tag->key, NULL, 0, BCF_HT_INT); // the type does not matter with n=0
}
void remove_format_tag(args_t *args, bcf1_t *line, rm_tag_t *tag)
{
bcf_update_format(args->hdr, line, tag->key, NULL, 0, BCF_HT_INT); // the type does not matter with n=0
}
void remove_format(args_t *args, bcf1_t *line, rm_tag_t *tag)
{
// remove all FORMAT fields except GT
if ( !(line->unpacked & BCF_UN_FMT) ) bcf_unpack(line, BCF_UN_FMT);
int i;
for (i=0; i<line->n_fmt; i++)
{
bcf_fmt_t *fmt = &line->d.fmt[i];
const char *key = bcf_hdr_int2id(args->hdr,BCF_DT_ID,fmt->id);
if ( key[0]=='G' && key[1]=='T' && !key[2] ) continue;
if ( fmt->p_free )
{
free(fmt->p - fmt->p_off);
fmt->p_free = 0;
}
line->d.indiv_dirty = 1;
fmt->p = NULL;
}
}
#include "htslib/khash.h"
KHASH_MAP_INIT_STR(vdict, bcf_idinfo_t)
typedef khash_t(vdict) vdict_t;
static void remove_hdr_lines(bcf_hdr_t *hdr, int type)
{
int i = 0, nrm = 0;
while ( i<hdr->nhrec )
{
if ( hdr->hrec[i]->type!=type ) { i++; continue; }
bcf_hrec_t *hrec = hdr->hrec[i];
if ( type==BCF_HL_FMT || type==BCF_HL_INFO || type==BCF_HL_FMT || type== BCF_HL_CTG )
{
// everything except FORMAT/GT
int id = bcf_hrec_find_key(hrec, "ID");
if ( id>=0 )
{
if ( type==BCF_HL_FMT && !strcmp(hrec->vals[id],"GT") ) { i++; continue; }
vdict_t *d = type==BCF_HL_CTG ? (vdict_t*)hdr->dict[BCF_DT_CTG] : (vdict_t*)hdr->dict[BCF_DT_ID];
khint_t k = kh_get(vdict, d, hdr->hrec[i]->vals[id]);
kh_val(d, k).hrec[type==BCF_HL_CTG?0:type] = NULL;
kh_val(d, k).info[type] |= 0xf;
}
}
nrm++;
hdr->nhrec--;
if ( i < hdr->nhrec )
memmove(&hdr->hrec[i],&hdr->hrec[i+1],(hdr->nhrec-i)*sizeof(bcf_hrec_t*));
bcf_hrec_destroy(hrec);
}
if ( nrm ) {
if (bcf_hdr_sync(hdr) < 0)
error_errno("[%s] Failed to update header", __func__);
}
}
static void init_remove_annots(args_t *args)
{
int keep_info = 0, keep_fmt = 0, keep_flt = 0;
void *keep = khash_str2int_init();
kstring_t str = {0,0,0};
char *ss = args->remove_annots;
while ( *ss )
{
args->nrm++;
args->rm = (rm_tag_t*) realloc(args->rm,sizeof(rm_tag_t)*args->nrm);
rm_tag_t *tag = &args->rm[args->nrm-1];
tag->key = NULL;
int type = BCF_HL_GEN;
if ( !strncasecmp("INFO/",ss,5) ) { type = BCF_HL_INFO; ss += 5; }
else if ( !strncasecmp("INF/",ss,4) ) { type = BCF_HL_INFO; ss += 4; }
else if ( !strncasecmp("FORMAT/",ss,7) ) { type = BCF_HL_FMT; ss += 7; }
else if ( !strncasecmp("FMT/",ss,4) ) { type = BCF_HL_FMT; ss += 4; }
else if ( !strncasecmp("FILTER/",ss,7) ) { type = BCF_HL_FLT; ss += 7; }
else if ( !strncasecmp("^INFO/",ss,6) ) { type = BCF_HL_INFO; ss += 6; keep_info = 1; }
else if ( !strncasecmp("^INF/",ss,5) ) { type = BCF_HL_INFO; ss += 5; keep_info = 1; }
else if ( !strncasecmp("^FORMAT/",ss,8) ) { type = BCF_HL_FMT; ss += 8; keep_fmt = 1; }
else if ( !strncasecmp("^FMT/",ss,5) ) { type = BCF_HL_FMT; ss += 5; keep_fmt = 1; }
else if ( !strncasecmp("^FILTER/",ss,8) ) { type = BCF_HL_FLT; ss += 8; keep_flt = 1; }
char *se = ss;
while ( *se && *se!=',' ) se++;
str.l = 0;
kputsn(ss, se-ss, &str);
if ( type==BCF_HL_FLT )
{
if ( !keep_flt )
{
args->flt_keep_pass = 1;
tag->handler = remove_filter;
tag->key = strdup(str.s);
tag->hdr_id = bcf_hdr_id2int(args->hdr, BCF_DT_ID, tag->key);
if ( !bcf_hdr_idinfo_exists(args->hdr,BCF_HL_FLT,tag->hdr_id) )
{
if ( args->keep_sites )
error("Error: The filter \"%s\" is not defined in the header, cannot use the -k option\n", str.s);
else
fprintf(stderr,"Warning: The filter \"%s\" is not defined in the header\n", str.s);
}
else if ( !args->keep_sites ) bcf_hdr_remove(args->hdr_out,BCF_HL_FLT,tag->key);
}
else
{
int value, ret = khash_str2int_get(keep, str.s, &value);
if ( ret==-1 ) khash_str2int_set(keep, strdup(str.s),1<<BCF_HL_FLT);
else khash_str2int_set(keep, str.s, value | 1<<BCF_HL_FLT);
args->nrm--;
}
}
else if ( type!=BCF_HL_GEN )
{
int id = bcf_hdr_id2int(args->hdr,BCF_DT_ID,str.s);
if ( !bcf_hdr_idinfo_exists(args->hdr,type,id) )
{
if ( args->keep_sites )
error("Error: The tag \"%s\" is not defined in the header, cannot use the -k option\n", str.s);
else
fprintf(stderr,"Warning: The tag \"%s\" not defined in the header\n", str.s);
tag->key = strdup(str.s);
if ( type==BCF_HL_INFO ) tag->handler = remove_info_tag;
else if ( type==BCF_HL_FMT ) tag->handler = remove_format_tag;
}
else if ( (type==BCF_HL_FMT && keep_fmt) || (type==BCF_HL_INFO && keep_info) )
{
int value, ret = khash_str2int_get(keep, str.s, &value);
if ( ret==-1 ) khash_str2int_set(keep, strdup(str.s),1<<type);
else khash_str2int_set(keep, str.s, value | 1<<type);
args->nrm--;
}
else
{
tag->key = strdup(str.s);
if ( type==BCF_HL_INFO ) tag->handler = remove_info_tag;
else if ( type==BCF_HL_FMT ) tag->handler = remove_format_tag;
if ( !args->keep_sites ) bcf_hdr_remove(args->hdr_out,type,tag->key);
}
}
else if ( !strcasecmp("ID",str.s) ) tag->handler = remove_id;
else if ( !strcasecmp("FILTER",str.s) )
{
tag->handler = remove_filter;
if ( !args->keep_sites ) remove_hdr_lines(args->hdr_out,BCF_HL_FLT);
}
else if ( !strcasecmp("QUAL",str.s) ) tag->handler = remove_qual;
else if ( !strcasecmp("INFO",str.s) )
{
tag->handler = remove_info;
if ( !args->keep_sites ) remove_hdr_lines(args->hdr_out,BCF_HL_INFO);
}
else if ( !strcasecmp("FMT",str.s) || !strcasecmp("FORMAT",str.s) )
{
tag->handler = remove_format;
if ( !args->keep_sites ) remove_hdr_lines(args->hdr_out,BCF_HL_FMT);
}
else if ( str.l )
{
if ( !args->keep_sites )
{
if ( str.s[0]=='#' && str.s[1]=='#' )
bcf_hdr_remove(args->hdr_out,BCF_HL_GEN,str.s+2);
else
bcf_hdr_remove(args->hdr_out,BCF_HL_STR,str.s);
}
args->nrm--;
}
ss = *se ? se+1 : se;
}
free(str.s);
if ( keep_flt || keep_info || keep_fmt )
{
int j;
for (j=0; j<args->hdr->nhrec; j++)
{
bcf_hrec_t *hrec = args->hdr->hrec[j];
if ( hrec->type!=BCF_HL_FLT && hrec->type!=BCF_HL_INFO && hrec->type!=BCF_HL_FMT ) continue;
if ( !keep_flt && hrec->type==BCF_HL_FLT ) continue;
if ( !keep_info && hrec->type==BCF_HL_INFO ) continue;
if ( !keep_fmt && hrec->type==BCF_HL_FMT ) continue;
int k = bcf_hrec_find_key(hrec,"ID");
assert( k>=0 ); // this should always be true for valid VCFs
int value, ret = khash_str2int_get(keep,hrec->vals[k],&value);
if ( ret==0 && value>>hrec->type ) // keep
{
if ( hrec->type==BCF_HL_FLT && !strcmp("PASS",hrec->vals[k]) ) args->flt_keep_pass = 1;
continue;
}
args->nrm++;
args->rm = (rm_tag_t*) realloc(args->rm,sizeof(rm_tag_t)*args->nrm);
rm_tag_t *tag = &args->rm[args->nrm-1];
if ( hrec->type==BCF_HL_INFO ) tag->handler = remove_info_tag;
else if ( hrec->type==BCF_HL_FMT ) tag->handler = remove_format_tag;
else
{
tag->handler = remove_filter;
tag->hdr_id = bcf_hdr_id2int(args->hdr, BCF_DT_ID, hrec->vals[k]);
}
tag->key = strdup(hrec->vals[k]);
if ( !args->keep_sites ) bcf_hdr_remove(args->hdr_out,hrec->type,tag->key);
}
}
khash_str2int_destroy_free(keep);
if ( !args->nrm ) error("No matching tag in -x %s\n", args->remove_annots);
if (bcf_hdr_sync(args->hdr_out) < 0)
error_errno("[%s] Failed to update header", __func__);
}
static void init_header_lines(args_t *args)
{
htsFile *file = hts_open(args->header_fname, "rb");
if ( !file ) error("Error reading %s\n", args->header_fname);
kstring_t str = {0,0,0};
while ( hts_getline(file, KS_SEP_LINE, &str) > 0 )
{
if ( bcf_hdr_append(args->hdr_out,str.s) ) error("Could not parse %s: %s\n", args->header_fname, str.s);
bcf_hdr_append(args->hdr,str.s); // the input file may not have the header line if run with -h (and nothing else)
}
if ( hts_close(file)!=0 ) error("[%s] Error: close failed .. %s\n", __func__,args->header_fname);
free(str.s);
if (bcf_hdr_sync(args->hdr_out) < 0)
error_errno("[%s] Failed to update output header", __func__);
if (bcf_hdr_sync(args->hdr) < 0)
error_errno("[%s] Failed to update input header", __func__);
}
static int setter_filter(args_t *args, bcf1_t *line, annot_col_t *col, void *data)
{
if ( !data ) error("Error: the --merge-logic option cannot be used with FILTER (yet?)\n");
// note: so far this works only with one filter, not a list of filters
annot_line_t *tab = (annot_line_t*) data;
if ( tab->cols[col->icol] && tab->cols[col->icol][0]=='.' && !tab->cols[col->icol][1] ) return 0; // don't replace with "."
hts_expand(int,1,args->mtmpi,args->tmpi);
args->tmpi[0] = bcf_hdr_id2int(args->hdr_out, BCF_DT_ID, tab->cols[col->icol]);
if ( args->tmpi[0]<0 ) error("The FILTER is not defined in the header: %s\n", tab->cols[col->icol]);
if ( col->replace==SET_OR_APPEND ) { bcf_add_filter(args->hdr_out,line,args->tmpi[0]); return 0; }
if ( col->replace!=REPLACE_MISSING )
{
bcf_update_filter(args->hdr_out,line,NULL,0);
bcf_update_filter(args->hdr_out,line,args->tmpi,1);
return 0;
}
// only update missing FILTER
if ( !(line->unpacked & BCF_UN_FLT) ) bcf_unpack(line, BCF_UN_FLT);
if ( !line->d.n_flt )
bcf_update_filter(args->hdr_out,line,args->tmpi,1);
return 0;
}
static int vcf_setter_filter(args_t *args, bcf1_t *line, annot_col_t *col, void *data)
{
int i;
bcf1_t *rec = (bcf1_t*) data;
if ( !(rec->unpacked & BCF_UN_FLT) ) bcf_unpack(rec, BCF_UN_FLT);
if ( !(line->unpacked & BCF_UN_FLT) ) bcf_unpack(line, BCF_UN_FLT);
if ( !rec->d.n_flt ) return 0; // don't overwrite with a missing value
if ( col->replace==SET_OR_APPEND || col->replace==REPLACE_MISSING )
{
if ( col->replace==REPLACE_MISSING && line->d.n_flt ) return 0; // only update missing FILTER
for (i=0; i<rec->d.n_flt; i++)
{
const char *flt = bcf_hdr_int2id(args->files->readers[1].header, BCF_DT_ID, rec->d.flt[i]);
bcf_add_filter(args->hdr_out,line,bcf_hdr_id2int(args->hdr_out, BCF_DT_ID, flt));
}
return 0;
}
hts_expand(int,rec->d.n_flt,args->mtmpi,args->tmpi);
for (i=0; i<rec->d.n_flt; i++)
{
const char *flt = bcf_hdr_int2id(args->files->readers[1].header, BCF_DT_ID, rec->d.flt[i]);
args->tmpi[i] = bcf_hdr_id2int(args->hdr_out, BCF_DT_ID, flt);
}
bcf_update_filter(args->hdr_out,line,NULL,0);
bcf_update_filter(args->hdr_out,line,args->tmpi,rec->d.n_flt);
return 0;
}
static int setter_id(args_t *args, bcf1_t *line, annot_col_t *col, void *data)
{
if ( !data ) error("Error: the --merge-logic option cannot be used with ID (yet?)\n");
// possible cases:
// IN ANNOT OUT ACHIEVED_BY
// x y x -c +ID
// x y y -c ID
// x y x,y -c =ID
// x . x -c +ID, ID
// x . . -x ID
// . y y -c +ID, -c ID
//
annot_line_t *tab = (annot_line_t*) data;
if ( tab->cols[col->icol] && tab->cols[col->icol][0]=='.' && !tab->cols[col->icol][1] ) return 0; // don't replace with "."
if ( col->replace==SET_OR_APPEND ) return bcf_add_id(args->hdr_out,line,tab->cols[col->icol]);
if ( col->replace!=REPLACE_MISSING ) return bcf_update_id(args->hdr_out,line,tab->cols[col->icol]);
// running with +ID, only update missing ids
if ( !line->d.id || (line->d.id[0]=='.' && !line->d.id[1]) )
return bcf_update_id(args->hdr_out,line,tab->cols[col->icol]);
return 0;
}
static int vcf_setter_id(args_t *args, bcf1_t *line, annot_col_t *col, void *data)
{
bcf1_t *rec = (bcf1_t*) data;
if ( rec->d.id && rec->d.id[0]=='.' && !rec->d.id[1] ) return 0; // don't replace with "."
if ( col->replace==SET_OR_APPEND ) return bcf_add_id(args->hdr_out,line,rec->d.id);
if ( col->replace!=REPLACE_MISSING ) return bcf_update_id(args->hdr_out,line,rec->d.id);
// running with +ID, only update missing ids
if ( !line->d.id || (line->d.id[0]=='.' && !line->d.id[1]) )
return bcf_update_id(args->hdr_out,line,rec->d.id);
return 0;
}
static int vcf_setter_ref(args_t *args, bcf1_t *line, annot_col_t *col, void *data)
{
bcf1_t *rec = (bcf1_t*) data;
if ( !strcmp(rec->d.allele[0],line->d.allele[0]) ) return 0; // no update necessary
const char **als = (const char**) malloc(sizeof(char*)*line->n_allele);
als[0] = rec->d.allele[0];
int i;
for (i=1; i<line->n_allele; i++) als[i] = line->d.allele[i];
bcf_update_alleles(args->hdr_out, line, als, line->n_allele);
free(als);
return 0;
}
static int vcf_setter_alt(args_t *args, bcf1_t *line, annot_col_t *col, void *data)
{
bcf1_t *rec = (bcf1_t*) data;
int i;
if ( rec->n_allele==line->n_allele )
{
for (i=1; i<rec->n_allele; i++) if ( strcmp(rec->d.allele[i],line->d.allele[i]) ) break;
if ( i==rec->n_allele ) return 0; // no update necessary
}
const char **als = (const char**) malloc(sizeof(char*)*rec->n_allele);
als[0] = line->d.allele[0];
for (i=1; i<rec->n_allele; i++) als[i] = rec->d.allele[i];
bcf_update_alleles(args->hdr_out, line, als, rec->n_allele);
free(als);
return 0;
}
static int setter_qual(args_t *args, bcf1_t *line, annot_col_t *col, void *data)
{
if ( !data ) error("Error: the --merge-logic option cannot be used with QUAL (yet?)\n");
annot_line_t *tab = (annot_line_t*) data;
char *str = tab->cols[col->icol];
if ( str[0]=='.' && str[1]==0 ) return 0; // empty
if ( col->replace==REPLACE_MISSING && !bcf_float_is_missing(line->qual) ) return 0;
line->qual = strtod(str, &str);
if ( str == tab->cols[col->icol] )
error("Could not parse %s at %s:%"PRId64" .. [%s]\n", col->hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1,tab->cols[col->icol]);
return 0;
}
static int vcf_setter_qual(args_t *args, bcf1_t *line, annot_col_t *col, void *data)
{
bcf1_t *rec = (bcf1_t*) data;
if ( bcf_float_is_missing(rec->qual) ) return 0;
if ( col->replace==REPLACE_MISSING && !bcf_float_is_missing(line->qual) ) return 0;
line->qual = rec->qual;
return 0;
}
static int setter_info_flag(args_t *args, bcf1_t *line, annot_col_t *col, void *data)
{
if ( !data ) error("Error: the --merge-logic option cannot be used with INFO type=Flag (yet?)\n");
annot_line_t *tab = (annot_line_t*) data;
char *str = tab->cols[col->icol];
if ( str[0]=='.' && str[1]==0 ) return 0;
if ( str[0]=='1' && str[1]==0 ) return bcf_update_info_flag(args->hdr_out,line,col->hdr_key_dst,NULL,1);
if ( str[0]=='0' && str[1]==0 ) return bcf_update_info_flag(args->hdr_out,line,col->hdr_key_dst,NULL,0);
error("Could not parse %s at %s:%"PRId64" .. [%s]\n", col->hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1,tab->cols[col->icol]);
return -1;
}
static int vcf_setter_info_flag(args_t *args, bcf1_t *line, annot_col_t *col, void *data)
{
bcf1_t *rec = (bcf1_t*) data;
int flag = bcf_get_info_flag(args->files->readers[1].header,rec,col->hdr_key_src,NULL,NULL);
bcf_update_info_flag(args->hdr_out,line,col->hdr_key_dst,NULL,flag);
return 0;
}
static int setter_ARinfo_int32(args_t *args, bcf1_t *line, annot_col_t *col, int nals, char **als, int ntmpi)
{
if ( col->number==BCF_VL_A && ntmpi!=nals-1 && (ntmpi!=1 || args->tmpi[0]!=bcf_int32_missing || args->tmpi[1]!=bcf_int32_vector_end) )
error("Incorrect number of values (%d) for the %s tag at %s:%"PRId64"\n", ntmpi,col->hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1);
else if ( col->number==BCF_VL_R && ntmpi!=nals && (ntmpi!=1 || args->tmpi[0]!=bcf_int32_missing || args->tmpi[1]!=bcf_int32_vector_end) )
error("Incorrect number of values (%d) for the %s tag at %s:%"PRId64"\n", ntmpi,col->hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1);
int ndst = col->number==BCF_VL_A ? line->n_allele - 1 : line->n_allele;
int *map = vcmp_map_ARvalues(args->vcmp,ndst,nals,als,line->n_allele,line->d.allele);
if ( !map ) error("REF alleles not compatible at %s:%"PRId64"\n", bcf_seqname(args->hdr,line),(int64_t) line->pos+1);
// fill in any missing values in the target VCF (or all, if not present)
int ntmpi2 = bcf_get_info_float(args->hdr, line, col->hdr_key_dst, &args->tmpi2, &args->mtmpi2);
if ( ntmpi2 < ndst ) hts_expand(int32_t,ndst,args->mtmpi2,args->tmpi2);
int i;
for (i=0; i<ndst; i++)
{
if ( map[i]<0 )
{
if ( ntmpi2 < ndst ) args->tmpi2[i] = bcf_int32_missing;
continue;
}
if ( ntmpi2==ndst && col->replace==REPLACE_MISSING
&& args->tmpi2[i]!=bcf_int32_missing
&& args->tmpi2[i]!=bcf_int32_vector_end ) continue;
args->tmpi2[i] = args->tmpi[ map[i] ];
}
bcf_update_info_int32(args->hdr_out,line,col->hdr_key_dst,args->tmpi2,ndst);
return 0;
}
static int setter_info_int(args_t *args, bcf1_t *line, annot_col_t *col, void *data)
{
annot_line_t *tab = (annot_line_t*) data;
if ( !tab )
{
if ( col->merge_method!=MM_SUM && col->merge_method!=MM_AVG && col->merge_method!=MM_MIN && col->merge_method!=MM_MAX && col->merge_method!=MM_APPEND )
error("Error: at the moment only the sum,avg,min,max,append options are supported with --merge-logic for INFO type=Integer\n");
}
int i,ntmpi = 0;
if ( tab )
{
char *str = tab->cols[col->icol], *end = str;
if ( str[0]=='.' && str[1]==0 ) return 0;
while ( *end )
{
int val = strtol(str, &end, 10);
if ( end==str )
error("Could not parse %s at %s:%"PRId64" .. [%s]\n", col->hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1,tab->cols[col->icol]);
ntmpi++;
hts_expand(int32_t,ntmpi,args->mtmpi,args->tmpi);
args->tmpi[ntmpi-1] = val;
str = end+1;
}
if ( col->merge_method!=MM_FIRST )
{
if ( !col->mm_dbl_nused )
{
col->mm_dbl_nused = ntmpi;
hts_expand(double,col->mm_dbl_nused,col->mm_dbl_nalloc,col->mm_dbl);
for (i=0; i<ntmpi; i++)
col->mm_dbl[i] = args->tmpi[i];
}
else
{
if ( col->merge_method==MM_APPEND )
{
int nori = col->mm_dbl_nused;
col->mm_dbl_nused += ntmpi;
hts_expand(double,col->mm_dbl_nused,col->mm_dbl_nalloc,col->mm_dbl);
for (i=0; i<ntmpi; i++)
col->mm_dbl[i+nori] = args->tmpi[i];
}
else
{
if ( ntmpi!=col->mm_dbl_nused ) error("Error: cannot merge fields of unequal length\n");
if ( col->merge_method==MM_SUM || col->merge_method==MM_AVG )
for (i=0; i<ntmpi; i++) col->mm_dbl[i] += args->tmpi[i];
else if ( col->merge_method==MM_MIN )
for (i=0; i<ntmpi; i++) { if ( col->mm_dbl[i] > args->tmpi[i] ) col->mm_dbl[i] = args->tmpi[i]; }
else if ( col->merge_method==MM_MAX )
for (i=0; i<ntmpi; i++) { if ( col->mm_dbl[i] < args->tmpi[i] ) col->mm_dbl[i] = args->tmpi[i]; }
}
}
col->mm_dbl_ndat++;
}
}
else if ( col->merge_method==MM_SUM || col->merge_method==MM_MIN || col->merge_method==MM_MAX || col->merge_method==MM_APPEND )
{
ntmpi = col->mm_dbl_nused;
hts_expand(int32_t,ntmpi,args->mtmpi,args->tmpi);
for (i=0; i<ntmpi; i++) args->tmpi[i] = col->mm_dbl[i];
col->mm_dbl_nused = col->mm_dbl_ndat = 0;
}
else if ( col->merge_method==MM_AVG )
{
ntmpi = col->mm_dbl_nused;
hts_expand(int32_t,ntmpi,args->mtmpi,args->tmpi);
for (i=0; i<ntmpi; i++) args->tmpi[i] = col->mm_dbl[i]/col->mm_dbl_ndat;
col->mm_dbl_nused = col->mm_dbl_ndat = 0;
}
if ( col->number==BCF_VL_A || col->number==BCF_VL_R )
return setter_ARinfo_int32(args,line,col,tab->nals,tab->als,ntmpi);
if ( col->replace==REPLACE_MISSING )
{
int ret = bcf_get_info_int32(args->hdr, line, col->hdr_key_dst, &args->tmpi2, &args->mtmpi2);
if ( ret>0 && args->tmpi2[0]!=bcf_int32_missing ) return 0;
}
bcf_update_info_int32(args->hdr_out,line,col->hdr_key_dst,args->tmpi,ntmpi);
return 0;
}
static int vcf_setter_info_int(args_t *args, bcf1_t *line, annot_col_t *col, void *data)
{
bcf1_t *rec = (bcf1_t*) data;
int ntmpi = bcf_get_info_int32(args->files->readers[1].header,rec,col->hdr_key_src,&args->tmpi,&args->mtmpi);
if ( ntmpi < 0 ) return 0; // nothing to add
if ( col->number==BCF_VL_A || col->number==BCF_VL_R )
return setter_ARinfo_int32(args,line,col,rec->n_allele,rec->d.allele,ntmpi);
if ( col->replace==REPLACE_MISSING )
{
int ret = bcf_get_info_int32(args->hdr, line, col->hdr_key_dst, &args->tmpi2, &args->mtmpi2);
if ( ret>0 && args->tmpi2[0]!=bcf_int32_missing ) return 0;
}
bcf_update_info_int32(args->hdr_out,line,col->hdr_key_dst,args->tmpi,ntmpi);
return 0;
}
static int setter_ARinfo_real(args_t *args, bcf1_t *line, annot_col_t *col, int nals, char **als, int ntmpf)
{
if ( col->number==BCF_VL_A && ntmpf!=nals-1 && (ntmpf!=1 || !bcf_float_is_missing(args->tmpf[0]) || !bcf_float_is_vector_end(args->tmpf[0])) )
error("Incorrect number of values (%d) for the %s tag at %s:%"PRId64"\n", ntmpf,col->hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1);
else if ( col->number==BCF_VL_R && ntmpf!=nals && (ntmpf!=1 || !bcf_float_is_missing(args->tmpf[0]) || !bcf_float_is_vector_end(args->tmpf[0])) )
error("Incorrect number of values (%d) for the %s tag at %s:%"PRId64"\n", ntmpf,col->hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1);
int ndst = col->number==BCF_VL_A ? line->n_allele - 1 : line->n_allele;
int *map = vcmp_map_ARvalues(args->vcmp,ndst,nals,als,line->n_allele,line->d.allele);
if ( !map ) error("REF alleles not compatible at %s:%"PRId64"\n", bcf_seqname(args->hdr,line),(int64_t) line->pos+1);
// fill in any missing values in the target VCF (or all, if not present)
int ntmpf2 = bcf_get_info_float(args->hdr, line, col->hdr_key_dst, &args->tmpf2, &args->mtmpf2);
if ( ntmpf2 < ndst ) hts_expand(float,ndst,args->mtmpf2,args->tmpf2);
int i;
for (i=0; i<ndst; i++)
{
if ( map[i]<0 )
{
if ( ntmpf2 < ndst ) bcf_float_set_missing(args->tmpf2[i]);
continue;
}
if ( ntmpf2==ndst && col->replace==REPLACE_MISSING
&& !bcf_float_is_missing(args->tmpf2[i])
&& !bcf_float_is_vector_end(args->tmpf2[i]) ) continue;
args->tmpf2[i] = args->tmpf[ map[i] ];
}
bcf_update_info_float(args->hdr_out,line,col->hdr_key_dst,args->tmpf2,ndst);
return 0;
}
static int setter_info_real(args_t *args, bcf1_t *line, annot_col_t *col, void *data)
{
annot_line_t *tab = (annot_line_t*) data;
if ( !tab )
{
if ( col->merge_method!=MM_SUM && col->merge_method!=MM_AVG && col->merge_method!=MM_MIN && col->merge_method!=MM_MAX && col->merge_method!=MM_APPEND )
error("Error: at the moment only the sum,avg,min,max,append options are supported with --merge-logic for INFO type=Float\n");
}
int i,ntmpf = 0;
if ( tab )
{
char *str = tab->cols[col->icol], *end = str;
if ( str[0]=='.' && str[1]==0 ) return 0;
while ( *end )
{
double val = strtod(str, &end);
if ( end==str )
error("Could not parse %s at %s:%"PRId64" .. [%s]\n", col->hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1,tab->cols[col->icol]);
ntmpf++;
hts_expand(float,ntmpf,args->mtmpf,args->tmpf);
args->tmpf[ntmpf-1] = val;
str = end+1;
}
if ( col->merge_method!=MM_FIRST )
{
if ( !col->mm_dbl_nused )
{
col->mm_dbl_nused = ntmpf;
hts_expand(double,col->mm_dbl_nused,col->mm_dbl_nalloc,col->mm_dbl);
for (i=0; i<ntmpf; i++)
col->mm_dbl[i] = args->tmpf[i];
}
else
{
if ( col->merge_method==MM_APPEND )
{
int nori = col->mm_dbl_nused;
col->mm_dbl_nused += ntmpf;
hts_expand(double,col->mm_dbl_nused,col->mm_dbl_nalloc,col->mm_dbl);
for (i=0; i<ntmpf; i++)
col->mm_dbl[i+nori] = args->tmpf[i];
}
else
{
if ( ntmpf!=col->mm_dbl_nused ) error("Error: cannot merge fields of unequal length\n");
if ( col->merge_method==MM_SUM || col->merge_method==MM_AVG )
for (i=0; i<ntmpf; i++) col->mm_dbl[i] += args->tmpf[i];
else if ( col->merge_method==MM_MIN )
for (i=0; i<ntmpf; i++) { if ( col->mm_dbl[i] > args->tmpf[i] ) col->mm_dbl[i] = args->tmpf[i]; }
else if ( col->merge_method==MM_MAX )
for (i=0; i<ntmpf; i++) { if ( col->mm_dbl[i] < args->tmpf[i] ) col->mm_dbl[i] = args->tmpf[i]; }
}
}
col->mm_dbl_ndat++;
}
}
else if ( col->merge_method==MM_SUM || col->merge_method==MM_MIN || col->merge_method==MM_MAX || col->merge_method==MM_APPEND )
{
ntmpf = col->mm_dbl_nused;
hts_expand(int32_t,ntmpf,args->mtmpf,args->tmpf);
for (i=0; i<ntmpf; i++) args->tmpf[i] = col->mm_dbl[i];
col->mm_dbl_nused = col->mm_dbl_ndat = 0;
}
else if ( col->merge_method==MM_AVG )
{
ntmpf = col->mm_dbl_nused;
hts_expand(int32_t,ntmpf,args->mtmpf,args->tmpf);
for (i=0; i<ntmpf; i++) args->tmpf[i] = col->mm_dbl[i]/col->mm_dbl_ndat;
col->mm_dbl_nused = col->mm_dbl_ndat = 0;
}
if ( col->number==BCF_VL_A || col->number==BCF_VL_R )
return setter_ARinfo_real(args,line,col,tab->nals,tab->als,ntmpf);
if ( col->replace==REPLACE_MISSING )
{
int ret = bcf_get_info_float(args->hdr, line, col->hdr_key_dst, &args->tmpf2, &args->mtmpf2);
if ( ret>0 && !bcf_float_is_missing(args->tmpf2[0]) ) return 0;
}
bcf_update_info_float(args->hdr_out,line,col->hdr_key_dst,args->tmpf,ntmpf);
return 0;
}
static int vcf_setter_info_real(args_t *args, bcf1_t *line, annot_col_t *col, void *data)
{
bcf1_t *rec = (bcf1_t*) data;
int ntmpf = bcf_get_info_float(args->files->readers[1].header,rec,col->hdr_key_src,&args->tmpf,&args->mtmpf);
if ( ntmpf < 0 ) return 0; // nothing to add
if ( col->number==BCF_VL_A || col->number==BCF_VL_R )
return setter_ARinfo_real(args,line,col,rec->n_allele,rec->d.allele,ntmpf);
if ( col->replace==REPLACE_MISSING )
{
int ret = bcf_get_info_float(args->hdr, line, col->hdr_key_dst, &args->tmpf2, &args->mtmpf2);
if ( ret>0 && !bcf_float_is_missing(args->tmpf2[0]) ) return 0;
}
bcf_update_info_float(args->hdr_out,line,col->hdr_key_dst,args->tmpf,ntmpf);
return 0;
}
int copy_string_field(char *src, int isrc, int src_len, kstring_t *dst, int idst); // see vcfmerge.c
static int setter_ARinfo_string(args_t *args, bcf1_t *line, annot_col_t *col, int nals, char **als)
{
assert( col->merge_method==MM_FIRST );
int nsrc = 1, lsrc = 0;
while ( args->tmps[lsrc] )
{
if ( args->tmps[lsrc]==',' ) nsrc++;
lsrc++;
}
if ( col->number==BCF_VL_A && nsrc!=nals-1 && (nsrc!=1 || args->tmps[0]!='.' || args->tmps[1]!=0 ) )
error("Incorrect number of values (%d) for the %s tag at %s:%"PRId64"\n", nsrc,col->hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1);
else if ( col->number==BCF_VL_R && nsrc!=nals && (nsrc!=1 || args->tmps[0]!='.' || args->tmps[1]!=0 ) )
error("Incorrect number of values (%d) for the %s tag at %s:%"PRId64"\n", nsrc,col->hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1);
int ndst = col->number==BCF_VL_A ? line->n_allele - 1 : line->n_allele;
int *map = vcmp_map_ARvalues(args->vcmp,ndst,nals,als,line->n_allele,line->d.allele);
if ( !map ) error("REF alleles not compatible at %s:%"PRId64"\n", bcf_seqname(args->hdr,line),(int64_t) line->pos+1);
// fill in any missing values in the target VCF (or all, if not present)
int i, empty = 0, nstr, mstr = args->tmpks.m;
nstr = bcf_get_info_string(args->hdr, line, col->hdr_key_dst, &args->tmpks.s, &mstr);
args->tmpks.m = mstr;
if ( nstr<0 || (nstr==1 && args->tmpks.s[0]=='.' && args->tmpks.s[1]==0) )
{
empty = 0;
args->tmpks.l = 0;
kputc('.',&args->tmpks);
for (i=1; i<ndst; i++) kputs(",.",&args->tmpks);
}
else args->tmpks.l = nstr;
for (i=0; i<ndst; i++)
{
if ( map[i]<0 )
{
if ( empty ) copy_string_field(".",0,1,&args->tmpks,i);
continue;
}
if ( col->replace==REPLACE_MISSING )
{
// Do not replace filled values. The field must be looked up again because
// of realloc in copy_string_field
int n = 0;
char *str = args->tmpks.s;
while ( *str && n<i )
{
if ( *str==',' ) n++;
str++;
}
if ( str[0]!='.' || (str[1]!=',' && str[1]!=0) ) continue; // value already set
}
int ret = copy_string_field(args->tmps,map[i],lsrc,&args->tmpks,i);
assert( ret==0 );
}
bcf_update_info_string(args->hdr_out,line,col->hdr_key_dst,args->tmpks.s);
return 0;
}
void khash_str2int_clear_free(void *_hash)
{
khash_t(str2int) *hash = (khash_t(str2int)*)_hash;
khint_t k;
if (hash == 0) return;
for (k = 0; k < kh_end(hash); ++k)
if (kh_exist(hash, k)) free((char*)kh_key(hash, k));
kh_clear(str2int, hash);
}
static int setter_info_str(args_t *args, bcf1_t *line, annot_col_t *col, void *data)
{
if ( col->replace==REPLACE_MISSING && col->number!=BCF_VL_A && col->number!=BCF_VL_R )
{
int ret = bcf_get_info_string(args->hdr, line, col->hdr_key_dst, &args->tmps2, &args->mtmps2);
if ( ret>0 && (args->tmps2[0]!='.' || args->tmps2[1]!=0) ) return 0;
}
annot_line_t *tab = (annot_line_t*) data;
int len = 0;
if ( tab )
{
len = strlen(tab->cols[col->icol]);
if ( !len ) return 0;
if ( len==1 && tab->cols[col->icol][0]=='.' ) return 0;
}
if ( col->merge_method!=MM_FIRST )
{
if ( col->number==BCF_VL_A || col->number==BCF_VL_R )
error("Error: the --merge-logic option cannot be used with INFO tags Type=String,Number={A,R,G}\n");
if ( data )
{
assert( col->merge_method==MM_APPEND || col->merge_method==MM_UNIQUE );
if ( col->merge_method==MM_UNIQUE )
{
if ( !col->mm_str_hash ) col->mm_str_hash = (khash_t(str2int)*)khash_str2int_init();
if ( khash_str2int_has_key(col->mm_str_hash, tab->cols[col->icol]) ) return 0;
khash_str2int_inc(col->mm_str_hash, strdup(tab->cols[col->icol]));
}
if ( col->mm_kstr.l ) kputc(',',&col->mm_kstr);
kputs(tab->cols[col->icol], &col->mm_kstr);
return 0;
}
if ( col->mm_kstr.l )
{
hts_expand(char,col->mm_kstr.l+1,args->mtmps,args->tmps);
memcpy(args->tmps,col->mm_kstr.s,col->mm_kstr.l+1);
}
else
return 0;
if ( !data ) // flush the line
{
if ( col->merge_method==MM_UNIQUE )
khash_str2int_clear_free(col->mm_str_hash);
col->mm_kstr.l = 0;
}
}
else
{
assert(tab);
hts_expand(char,len+1,args->mtmps,args->tmps);
memcpy(args->tmps,tab->cols[col->icol],len+1);
if ( col->number==BCF_VL_A || col->number==BCF_VL_R )
return setter_ARinfo_string(args,line,col,tab->nals,tab->als);