From 68fa3be7307dca02301cabbc638e155508263607 Mon Sep 17 00:00:00 2001 From: Petr Danecek Date: Tue, 21 Jun 2022 11:46:04 +0100 Subject: [PATCH] Fix duplications of PG line in +scatter The same header was used repeatedly adding more and more header lines. The problem becomes apparent only for longer running jobs because bcf_hdr_append_version() does not add duplicate header lines and the time stamp would be the same for short jobs. Resolves #1736 --- NEWS | 4 ++++ plugins/scatter.c | 19 +++++++++++-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/NEWS b/NEWS index 0e6dfcb51..b28a0f1bc 100644 --- a/NEWS +++ b/NEWS @@ -28,6 +28,10 @@ Changes affecting specific commands: - Suppress the output of MQSBZ and FS annotations in absence of alternate allele +* bcftools +scatter + + - Fix erroneous addition of duplicate PG lines + ## Release 1.15.1 (7th April 2022) diff --git a/plugins/scatter.c b/plugins/scatter.c index 7122b44fe..af358fc4f 100644 --- a/plugins/scatter.c +++ b/plugins/scatter.c @@ -59,6 +59,7 @@ typedef struct int record_cmd_line; char **hts_opts; int nhts_opts; + bcf_hdr_t *hdr; } args_t; @@ -193,9 +194,12 @@ static void open_set(subset_t *set, args_t *args) if ( hts_opt_apply(set->fh, opts) ) error("Could not apply the HTS options\n"); hts_opt_free(opts); } - bcf_hdr_t *hdr = bcf_sr_get_header(args->sr, 0); - if ( bcf_hdr_write(set->fh, hdr)!=0 ) error("[%s] Error: cannot write the header to %s\n", __func__, args->str.s); - if (args->record_cmd_line) bcf_hdr_append_version(hdr, args->argc, args->argv, "bcftools_plugin"); + if ( !args->hdr ) + { + args->hdr = bcf_sr_get_header(args->sr, 0); + if ( args->record_cmd_line ) bcf_hdr_append_version(args->hdr, args->argc, args->argv, "bcftools_plugin"); + } + if ( bcf_hdr_write(set->fh, args->hdr)!=0 ) error("[%s] Error: cannot write the header to %s\n", __func__, args->str.s); } static void init_data(args_t *args) @@ -270,7 +274,6 @@ static void destroy_data(args_t *args) static void process(args_t *args) { - bcf_hdr_t *hdr = bcf_sr_get_header(args->sr, 0); bcf1_t *rec = bcf_sr_get_line(args->sr, 0); subset_t *set; @@ -283,7 +286,7 @@ static void process(args_t *args) open_set(set, args); args->rec_cnt = 0; } - if ( bcf_write(set->fh, hdr, rec)!=0 ) error("[%s] Error: failed to write the record\n", __func__); + if ( bcf_write(set->fh, args->hdr, rec)!=0 ) error("[%s] Error: failed to write the record\n", __func__); args->rec_cnt++; if (args->rec_cnt == args->nsites) { args->rec_cnt = 0; @@ -293,15 +296,15 @@ static void process(args_t *args) args->chunk_cnt++; } } else { - if ( regidx_overlap(args->reg_idx, bcf_hdr_id2name(hdr, rec->rid), rec->pos, rec->pos, args->reg_itr) ) { + if ( regidx_overlap(args->reg_idx, bcf_hdr_id2name(args->hdr, rec->rid), rec->pos, rec->pos, args->reg_itr) ) { while (regitr_overlap(args->reg_itr)) { int idx = regitr_payload(args->reg_itr, int); set = &args->sets[idx]; - if ( bcf_write(set->fh, hdr, rec)!=0 ) error("[%s] Error: failed to write the record\n", __func__); + if ( bcf_write(set->fh, args->hdr, rec)!=0 ) error("[%s] Error: failed to write the record\n", __func__); } } else if (args->extra) { set = &args->sets[args->nsets-1]; - if ( bcf_write(set->fh, hdr, rec)!=0 ) error("[%s] Error: failed to write the record\n", __func__); + if ( bcf_write(set->fh, args->hdr, rec)!=0 ) error("[%s] Error: failed to write the record\n", __func__); } } }