forked from samtools/bcftools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcfannotate.c
3525 lines (3348 loc) · 152 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-2022 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 <assert.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"
#include "dbuf.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 (1<<0) // -c +TAG .. replace only missing values
#define REPLACE_ALL (1<<1) // -c TAG .. replace both missing and existing values
#define REPLACE_NON_MISSING (1<<2) // -c -TAG .. replace only if tgt is not missing
#define SET_OR_APPEND (1<<3) // -c =TAG .. set new value if missing or non-existent, append otherwise
#define MATCH_VALUE (1<<4) // -c ~ID .. do not set, just match the value
#define CARRY_OVER_MISSING (1<<5) // -c .TAG .. carry over source missing values as well
#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
#define MM_APPEND_MISSING 7 // missing values will be transferred as well
typedef struct _annot_col_t
{
int icol, replace, number; // number: one of BCF_VL_* types
char *hdr_key_src, *hdr_key_dst;
// The setters return 0 on successful update of the bcf record, negative value (bcf_update_* return status) on errors,
// or 1 on (repeated partial updates) concluded with a src=NULL call
int (*setter)(struct _args_t *, bcf1_t *dst, struct _annot_col_t *, void *src); // the last is the annotation line, either src bcf1_t or annot_line_t
int (*getter)(struct _args_t *, bcf1_t *src, struct _annot_col_t *, void **ptr, int *mptr);
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;
size_t
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)
double
*mm_dbl;
void *ptr;
int mptr, done;
}
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, *tgts_hdr;
htsFile *out_fh;
int output_type, n_threads, clevel;
bcf_sr_regions_t *tgts;
regidx_t *tgt_idx; // keep everything in memory only with .tab annotation file and -c BEG,END columns
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
annot_line_t *aline_missing;
uint32_t *srt_alines; // sorted indexes (iALT<<16 || iAline)
int nalines, malines, nsrt_alines, msrt_alines;
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;
int match_id; // set iff `-c ~ID` given, -1 otherwise
int match_end; // set iff `-c ~INFO/END` is given, -1 otherwise
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, *rename_annots, *sample_names, *mark_sites;
char **rename_annots_map;
char *min_overlap_str;
float min_overlap_ann, min_overlap_vcf;
int rename_annots_nmap;
kstring_t merge_method_str;
int argc, drop_header, record_cmd_line, tgts_is_vcf, mark_sites_logic, force, single_overlaps;
int columns_is_file, has_append_mode, pair_logic;
dbuf_t *header_lines;
}
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 ( !strcmp("END",bcf_hdr_int2id(args->hdr,BCF_DT_ID,inf->key)) )
line->rlen = line->n_allele ? strlen(line->d.allele[0]) : 0;
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;
int i, ntags, needs_info = 0;
if ( args->set_ids )
{
const char **tags = convert_list_used_tags(args->set_ids,&ntags);
for (i=0; i<ntags; i++)
if ( !strncmp("INFO/",tags[i],4) ) needs_info = 1;
}
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;
if ( needs_info ) error("Error: `--remove INFO/%s` is executed first, cannot combine with `--set-id %s`\n",tag->key,args->set_ids_fmt);
}
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;
if ( needs_info ) error("Error: `--remove INFO/%s` is executed first, cannot combine with `--set-id %s`\n",tag->key,args->set_ids_fmt);
}
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) )
{
if ( needs_info ) error("Error: `--remove INFO` is executed first, cannot combine with `--set-id %s`\n",args->set_ids_fmt);
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 )
{
int id = bcf_hdr_id2int(args->hdr, BCF_DT_ID, str.s);
if ( bcf_hdr_idinfo_exists(args->hdr,BCF_HL_INFO,id) ) error("Error: did you mean INFO/%s?\n",str.s);
if ( bcf_hdr_idinfo_exists(args->hdr,BCF_HL_FMT,id) ) error("Error: did you mean FORMAT/%s?\n",str.s);
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)
{
if ( args->header_fname )
{
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 ( args->header_lines )
{
int i, n = dbuf_n(args->header_lines);
for (i=0; i<n; i++)
{
char *line = dbuf_ith(args->header_lines,i);
if ( bcf_hdr_append(args->hdr_out,line) ) error("Could not parse the header line: %s\n", line);
bcf_hdr_append(args->hdr,line); // the input file may not have the header line if run with -H (and nothing else)
}
dbuf_destroy_free(args->header_lines);
args->header_lines = NULL;
}
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 vcf_getter_info_str2str(args_t *args, bcf1_t *rec, annot_col_t *col, void **ptr, int *mptr)
{
return bcf_get_info_string(args->tgts_hdr,rec,col->hdr_key_src,ptr,mptr);
}
static int vcf_getter_id2str(args_t *args, bcf1_t *rec, annot_col_t *col, void **ptr, int *mptr)
{
char *str = *((char**)ptr);
int len = strlen(rec->d.id);
if ( len >= *mptr ) str = realloc(str, len+1);
strcpy(str, rec->d.id);
*((char**)ptr) = str;
*mptr = len+1;
return len;
}
static int vcf_getter_filter2str(args_t *args, bcf1_t *rec, annot_col_t *col, void **ptr, int *mptr)
{
kstring_t str;
str.s = *((char**)ptr);
str.m = *mptr;
str.l = 0;
int i;
if ( rec->d.n_flt )
{
for (i=0; i<rec->d.n_flt; i++)
{
if (i) kputc(';', &str);
kputs(bcf_hdr_int2id(args->tgts_hdr,BCF_DT_ID,rec->d.flt[i]), &str);
}
}
else kputc('.', &str);
*((char**)ptr) = str.s;
*mptr = str.m;
return str.l;
}
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][0]=='.' && !tab->cols[col->icol][1] ) // don't overwrite with a missing value unless asked
{
if ( (col->replace & CARRY_OVER_MISSING) && (col->replace & (REPLACE_ALL|REPLACE_NON_MISSING)) ) bcf_update_filter(args->hdr_out,line,NULL,0);
return 0;
}
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 \"%s\" is not defined in the header, was the -h option provided?\n", tab->cols[col->icol]);
if ( col->replace & SET_OR_APPEND ) return bcf_add_filter(args->hdr_out,line,args->tmpi[0]);
if ( !(col->replace & REPLACE_MISSING) )
{
bcf_update_filter(args->hdr_out,line,NULL,0);
return bcf_update_filter(args->hdr_out,line,args->tmpi,1);
}
// only update missing FILTER
if ( !(line->unpacked & BCF_UN_FLT) ) bcf_unpack(line, BCF_UN_FLT);
if ( !line->d.n_flt )
return 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, ret = 0;
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 ) // don't overwrite with a missing value unless asked
{
if ( (col->replace & CARRY_OVER_MISSING) && (col->replace & (REPLACE_ALL|REPLACE_NON_MISSING)) ) bcf_update_filter(args->hdr_out,line,NULL,0);
return 0;
}
if ( col->replace & (SET_OR_APPEND|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]);
if ( bcf_add_filter(args->hdr_out,line,bcf_hdr_id2int(args->hdr_out, BCF_DT_ID, flt)) < 0 ) ret = -1;
}
return ret;
}
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);
return bcf_update_filter(args->hdr_out,line,args->tmpi,rec->d.n_flt);
}
static int setter_pos(args_t *args, bcf1_t *line, annot_col_t *col, void *data)
{
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 "."
char *tmp;
int pos = strtol(tab->cols[col->icol], &tmp, 10);
if ( tmp==tab->cols[col->icol] )
error("Could not parse ~POS at %s:%"PRId64" .. [%s]\n",bcf_seqname(args->hdr,line),(int64_t)line->pos+1,tab->cols[col->icol]);
line->pos = pos - 1;
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");
if ( col->replace & MATCH_VALUE ) return 0;
// 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)
{
if ( col->replace & MATCH_VALUE ) return 0;
bcf1_t *rec = (bcf1_t*) data;
char *id;
if ( col->getter )
{
int nret = col->getter(args,rec,col,&col->ptr,&col->mptr);
id = (char*) col->ptr;
if ( nret<=0 || (nret==1 && *id=='.') ) return 0; // don't replace with "."
}
else
{
if ( rec->d.id && rec->d.id[0]=='.' && !rec->d.id[1] ) return 0; // don't replace with "."
id = rec->d.id;
}
if ( col->replace & SET_OR_APPEND ) return bcf_add_id(args->hdr_out,line,id);
if ( !(col->replace & REPLACE_MISSING) ) return bcf_update_id(args->hdr_out,line,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,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];
int ret = bcf_update_alleles(args->hdr_out, line, als, line->n_allele);
free(als);
return ret;
}
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 ( line->n_allele>1 && (col->replace & REPLACE_MISSING) ) return 0;
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];
int ret = bcf_update_alleles(args->hdr_out, line, als, rec->n_allele);
free(als);
return ret;
}
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 ) // don't overwrite with a missing value unless asked
{
if ( (col->replace & CARRY_OVER_MISSING) && (col->replace & (REPLACE_ALL|REPLACE_NON_MISSING)) ) bcf_float_set_missing(line->qual);
return 0;
}
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) ) // don't overwrite with a missing value unless asked
{
if ( (col->replace & CARRY_OVER_MISSING) && (col->replace & (REPLACE_ALL|REPLACE_NON_MISSING)) ) bcf_float_set_missing(line->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 ) // don't overwrite with a missing value unless asked
{
if ( (col->replace & CARRY_OVER_MISSING) && (col->replace & (REPLACE_ALL|REPLACE_NON_MISSING)) ) bcf_update_info_flag(args->hdr_out,line,col->hdr_key_dst,NULL,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] ];
}
return bcf_update_info_int32(args->hdr_out,line,col->hdr_key_dst,args->tmpi2,ndst);
}
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;
// This is a bit hacky, only to reuse existing code with minimal changes:
// -c =TAG will now behave as -l TAG:APPEND for integers
if ( col->replace & SET_OR_APPEND ) col->merge_method=MM_APPEND;
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 &&
col->merge_method!=MM_APPEND_MISSING )
error("Error: at the moment only the sum,avg,min,max,append,append-missing options are supported with --merge-logic for INFO type=Integer\n");
}
int i,ntmpi = 0;
if ( (col->replace & SET_OR_APPEND) && !col->mm_dbl_nused )
{
ntmpi = bcf_get_info_int32(args->hdr, line, col->hdr_key_dst, &args->tmpi, &args->mtmpi);
if ( ntmpi>0 && (args->tmpi[0]!=bcf_int32_missing || (col->replace & CARRY_OVER_MISSING)) )
{
col->mm_dbl_nused = col->mm_dbl_ndat = 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];
col->mm_dbl_ndat = 1;
}
ntmpi = 0;
}
if ( tab ) // has data, not flushing yet
{
char *str = tab->cols[col->icol], *end = str;
if ( str[0]=='.' && str[1]==0 && col->merge_method!=MM_APPEND_MISSING && !(col->replace & CARRY_OVER_MISSING) ) return 1;
while ( *end )
{
ntmpi++;
hts_expand(int32_t,ntmpi,args->mtmpi,args->tmpi);
if ( str[0]=='.' && (str[1]==0 || str[1]==',') )
{
if ( col->merge_method==MM_APPEND_MISSING || (col->replace & CARRY_OVER_MISSING) )
args->tmpi[ntmpi-1] = bcf_int32_missing;
else
ntmpi--;
if ( str[1]==0 ) end = str+1;
str += 2;
}
else
{
args->tmpi[ntmpi-1] = 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]);
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 || col->merge_method==MM_APPEND_MISSING )
{
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++;
return 1;
}
}
else if ( col->merge_method==MM_SUM || col->merge_method==MM_MIN || col->merge_method==MM_MAX || col->merge_method==MM_APPEND || col->merge_method==MM_APPEND_MISSING )
{
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;
}
return bcf_update_info_int32(args->hdr_out,line,col->hdr_key_dst,args->tmpi,ntmpi);
}
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;
}
return bcf_update_info_int32(args->hdr_out,line,col->hdr_key_dst,args->tmpi,ntmpi);
}
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] ];
}
return bcf_update_info_float(args->hdr_out,line,col->hdr_key_dst,args->tmpf2,ndst);
}
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;
// This is a bit hacky, only to reuse existing code with minimal changes:
// -c =TAG will now behave as -l TAG:APPEND for floats
if ( col->replace & SET_OR_APPEND ) col->merge_method=MM_APPEND;
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 &&
col->merge_method!=MM_APPEND_MISSING )
error("Error: at the moment only the sum,avg,min,max,append,append-missing options are supported with --merge-logic for INFO type=Float\n");
}
int i,ntmpf = 0;
if ( (col->replace & SET_OR_APPEND) && !col->mm_dbl_nused )
{
ntmpf = bcf_get_info_float(args->hdr, line, col->hdr_key_dst, &args->tmpf, &args->mtmpf);
if ( ntmpf>0 && (!bcf_float_is_missing(args->tmpf[0]) || (col->replace & CARRY_OVER_MISSING)) )
{
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++)
if ( bcf_float_is_missing(args->tmpf[i]) )
bcf_double_set_missing(col->mm_dbl[i]);
else
col->mm_dbl[i] = args->tmpf[i];
col->mm_dbl_ndat = 1;
}
ntmpf = 0;
}
if ( tab ) // data row, not just flushing
{
char *str = tab->cols[col->icol], *end = str;
if ( str[0]=='.' && str[1]==0 && col->merge_method!=MM_APPEND_MISSING && !(col->replace & CARRY_OVER_MISSING) ) return 1;
while ( *end )
{
ntmpf++;
hts_expand(float,ntmpf,args->mtmpf,args->tmpf);
if ( str[0]=='.' && (str[1]==0 || str[1]==',') )
{
if ( col->merge_method==MM_APPEND_MISSING || (col->replace & CARRY_OVER_MISSING) )
bcf_float_set_missing(args->tmpf[ntmpf-1]);
else
ntmpf--;
if ( str[1]==0 ) end = str+1;
str += 2;
}
else
{
args->tmpf[ntmpf-1] = 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]);
str = end+1;
}
}
if ( col->merge_method!=MM_FIRST )
{
if ( !col->mm_dbl_nused )
{