Skip to content

Commit

Permalink
Fix duplications of PG line in +scatter
Browse files Browse the repository at this point in the history
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 samtools#1736
  • Loading branch information
pd3 committed Jun 21, 2022
1 parent 560fda5 commit 68fa3be
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
19 changes: 11 additions & 8 deletions plugins/scatter.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ typedef struct
int record_cmd_line;
char **hts_opts;
int nhts_opts;
bcf_hdr_t *hdr;
}
args_t;

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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__);
}
}
}
Expand Down

0 comments on commit 68fa3be

Please sign in to comment.