Skip to content

Commit

Permalink
Allow repeated calls of bcf_sr_set_regions
Browse files Browse the repository at this point in the history
and make repeated bcf_sr_seek()+next_line() calls consistent.

Resolves samtools#1623 and samtools/bcftools#1918
  • Loading branch information
pd3 committed May 25, 2023
1 parent 878cff4 commit 3e2190c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
8 changes: 5 additions & 3 deletions htslib/synced_bcf_reader.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// @file htslib/synced_bcf_reader.h
/// Stream through multiple VCF files.
/*
Copyright (C) 2012-2017, 2019-2021 Genome Research Ltd.
Copyright (C) 2012-2017, 2019-2023 Genome Research Ltd.
Author: Petr Danecek <[email protected]>
Expand Down Expand Up @@ -306,8 +306,10 @@ int bcf_sr_set_samples(bcf_srs_t *readers, const char *samples, int is_file);
* Targets (but not regions) can be prefixed with "^" to request logical complement,
* for example "^X,Y,MT" indicates that sequences X, Y and MT should be skipped.
*
* API note: bcf_sr_set_regions/bcf_sr_set_targets MUST be called before the
* first call to bcf_sr_add_reader().
* API notes:
* - bcf_sr_set_targets MUST be called before the first call to bcf_sr_add_reader()
* - bcf_sr_set_regions AFTER readers where initialized will reposition the readers
* and discard all previous regions.
*/
HTSLIB_EXPORT
int bcf_sr_set_targets(bcf_srs_t *readers, const char *targets, int is_file, int alleles);
Expand Down
14 changes: 10 additions & 4 deletions synced_bcf_reader.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* synced_bcf_reader.c -- stream through multiple VCF files.
Copyright (C) 2012-2021 Genome Research Ltd.
Copyright (C) 2012-2023 Genome Research Ltd.
Author: Petr Danecek <[email protected]>
Expand Down Expand Up @@ -76,6 +76,7 @@ static bcf_sr_regions_t *_regions_init_string(const char *str);
static int _regions_match_alleles(bcf_sr_regions_t *reg, int als_idx, bcf1_t *rec);
static void _regions_sort_and_merge(bcf_sr_regions_t *reg);
static int _bcf_sr_regions_overlap(bcf_sr_regions_t *reg, const char *seq, hts_pos_t start, hts_pos_t end, int missed_reg_handler);
static void bcf_sr_seek_start(bcf_srs_t *readers);

char *bcf_sr_strerror(int errnum)
{
Expand Down Expand Up @@ -187,8 +188,10 @@ int bcf_sr_set_regions(bcf_srs_t *readers, const char *regions, int is_file)
{
if ( readers->nreaders || readers->regions )
{
hts_log_error("Must call bcf_sr_set_regions() before bcf_sr_add_reader()");
return -1;
if ( readers->regions ) bcf_sr_regions_destroy(readers->regions);
readers->regions = bcf_sr_regions_init(regions,is_file,0,1,-2);
bcf_sr_seek_start(readers);
return 0;
}

readers->regions = bcf_sr_regions_init(regions,is_file,0,1,-2);
Expand Down Expand Up @@ -676,7 +679,6 @@ static int _reader_fill_buffer(bcf_srs_t *files, bcf_sr_t *reader)
hts_log_error("This should never happen, just to keep clang compiler happy: %d",BCF_SR_AUX(files)->targets_overlap);
exit(1);
}

if ( beg <= files->regions->prev_end || end < files->regions->start || beg > files->regions->end ) continue;
}

Expand Down Expand Up @@ -843,7 +845,11 @@ static void bcf_sr_seek_start(bcf_srs_t *readers)
for (i=0; i<reg->nseqs; i++)
reg->regs[i].creg = -1;
reg->iseq = 0;
reg->start = -1;
reg->end = -1;
reg->prev_seq = -1;
reg->prev_start = -1;
reg->prev_end = -1;
}


Expand Down

0 comments on commit 3e2190c

Please sign in to comment.