Skip to content

Commit 44dc679

Browse files
committed
add VCF.load_index to allow explicitly loading an index
1 parent 36edb2a commit 44dc679

File tree

5 files changed

+17
-2
lines changed

5 files changed

+17
-2
lines changed

src/hts/private/hts_concat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ void hts_idx_finish(hts_idx_t *idx, uint64_t final_offset);
235235

236236
void hts_idx_save(const hts_idx_t *idx, const char *fn, int fmt);
237237
hts_idx_t *hts_idx_load(const char *fn, int fmt);
238+
hts_idx_t *hts_idx_load2(const char *fn, const char *fnidx);
238239

239240

240241
uint8_t *hts_idx_get_meta(hts_idx_t *idx, int *l_meta);

src/hts/private/hts_concat.nim

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ proc hts_idx_save*(idx: ptr hts_idx_t; fn: cstring; fmt: cint) {.cdecl,
294294
importc: "hts_idx_save", dynlib: libname.}
295295
proc hts_idx_load*(fn: cstring; fmt: cint): ptr hts_idx_t {.cdecl,
296296
importc: "hts_idx_load", dynlib: libname.}
297+
proc hts_idx_load2*(fn: cstring; fnidx: cstring): ptr hts_idx_t {.cdecl,
298+
importc: "hts_idx_load2", dynlib: libname.}
297299
proc hts_idx_get_meta*(idx: ptr hts_idx_t; l_meta: ptr cint): ptr uint8 {.cdecl,
298300
importc: "hts_idx_get_meta", dynlib: libname.}
299301
proc hts_idx_set_meta*(idx: ptr hts_idx_t; l_meta: uint32; meta: ptr uint8; is_copy: cint): cint {.
@@ -637,7 +639,7 @@ const
637639
##
638640

639641
type
640-
INNER_C_UNION_hts_concat_544* {.bycopy.} = object {.union.}
642+
INNER_C_UNION_hts_concat_545* {.bycopy.} = object {.union.}
641643
i*: int32 ## integer value
642644
f*: cfloat ## float value
643645

@@ -667,7 +669,7 @@ type
667669
key*: cint ## key: numeric tag id, the corresponding string is bcf_hdr_t::id[BCF_DT_ID][$key].key
668670
`type`*: cint
669671
len*: cint ## type: one of BCF_BT_* types; len: vector length, 1 for scalars
670-
v1*: INNER_C_UNION_hts_concat_544 ## only set if $len==1; for easier access
672+
v1*: INNER_C_UNION_hts_concat_545 ## only set if $len==1; for easier access
671673
vptr*: ptr uint8 ## pointer to data array in bcf1_t->shared.s, excluding the size+type and tag id bytes
672674
vptr_len*: uint32 ## length of the vptr block or, when set, of the vptr_mod block, excluding offset
673675
vptr_off* {.bitsize: 31.}: uint32 ## vptr offset, i.e., the size of the INFO key plus size+type bytes

src/hts/vcf.nim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,14 @@ iterator items*(v:VCF): Variant =
527527
stderr.write_line "last read variant:", variant.tostring()
528528
quit(2)
529529

530+
proc load_index*(v: VCF, path: string) =
531+
## load the index at the given path (remote or local).
532+
v.bidx = hts_idx_load2(v.fname, path)
533+
if v.bidx == nil:
534+
v.tidx = tbx_index_load2(v.fname, path)
535+
if v.bidx == nil and v.tidx == nil:
536+
raise newException(OSError, "unable to load index at:" & path)
537+
530538
iterator vquery(v:VCF, region:string): Variant =
531539
## internal iterator for VCF regions called from query()
532540
if v.tidx == nil:

tests/other-for-test.bcf.csi

103 Bytes
Binary file not shown.

tests/vcftest.nim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ suite "vcf suite":
228228
check variant.info.set("myflag", val) == Status.OK
229229
check "myflag" notin variant.tostring()
230230

231+
test "load index":
232+
var vcf:VCF
233+
check open(vcf, "tests/test.bcf")
234+
vcf.load_index("tests/other-for-test.bcf.csi")
231235

232236
test "remove info from header":
233237
var vcf:VCF

0 commit comments

Comments
 (0)