Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't error when making an iterator on a tid not in the index #1545

Merged
merged 1 commit into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions hts.c
Original file line number Diff line number Diff line change
Expand Up @@ -3077,16 +3077,14 @@ hts_itr_t *hts_itr_query(const hts_idx_t *idx, int tid, hts_pos_t beg, hts_pos_t
free(iter);
iter = NULL;
}
} else if (tid >= idx->n || (bidx = idx->bidx[tid]) == NULL) {
iter->finished = 1;
} else {
if (beg < 0) beg = 0;
if (end < beg) {
free(iter);
return NULL;
}
if (tid >= idx->n || (bidx = idx->bidx[tid]) == NULL) {
free(iter);
return NULL;
}

k = kh_get(bin, bidx, META_BIN(idx));
if (k != kh_end(bidx))
Expand Down
4 changes: 4 additions & 0 deletions test/modhdr.expected.vcf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
##fileformat=VCFv4.3
##FILTER=<ID=PASS,Description="All filters passed">
##contig=<ID=chr22,length=51304566>
#CHROM POS ID REF ALT QUAL FILTER INFO
Binary file added test/modhdr.vcf.gz
Binary file not shown.
Binary file added test/modhdr.vcf.gz.csi
Binary file not shown.
5 changes: 5 additions & 0 deletions test/test.pl
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,11 @@ sub test_vcf_various
cmd => "$$opts{bin}/htsfile -c $$opts{path}/formatmissing.vcf");
test_cmd($opts, %args, out => "vcf_meta_meta.vcf",
cmd => "$$opts{bin}/htsfile -c $$opts{path}/vcf_meta_meta.vcf");

# VCF file with contig IDX=1, simulating an edited BCF file
# See htslib issue 1534
test_cmd($opts, %args, out => "modhdr.expected.vcf",
cmd => "$$opts{path}/test_view $$opts{path}/modhdr.vcf.gz chr22:1-2");
}

sub write_multiblock_bgzf {
Expand Down
3 changes: 2 additions & 1 deletion test/test_view.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ int vcf_loop(int argc, char **argv, int optind, struct opts *opts, htsFile *in,
hts_itr_t *iter;
if ((iter = bcf_itr_querys(idx, h, argv[i])) == 0) {
fprintf(stderr, "[E::%s] fail to parse region '%s'\n", __func__, argv[i]);
continue;
exit_code = 1;
break;
}
while ((r = bcf_itr_next(in, iter, b)) >= 0) {
if (!opts->benchmark && bcf_write1(out, h, b) < 0) {
Expand Down