2727sample identifiers from its header block.
2828
2929For BGEN the ``.bgen.bgi`` sidecar is a SQLite database that
30- :func:`vcztools.bgen.write_bgen_index` writes to a filesystem path.
31- We materialise it to a tempfile at session-init time, read the bytes
32- back, and hold them in the server process's memory alongside the
33- ``.sample`` text.
30+ :func:`vcztools.write_bgi` writes to a filesystem path. We materialise
31+ it to a tempfile at session-init time, read the bytes back, and hold
32+ them in the server process's memory alongside the ``.sample`` text.
3433"""
3534
3635import dataclasses
36+ import io
3737import pathlib
3838import tempfile
3939from collections .abc import Callable
4040
4141import vcztools
42- from vcztools import bgen as vcztools_bgen
43- from vcztools import plink as vcztools_plink
4442
4543
4644@dataclasses .dataclass (frozen = True )
@@ -86,9 +84,13 @@ def _plink_static_suffixes(opts) -> tuple[str, ...]:
8684def _build_plink_static (reader , opts ) -> dict [str , bytes ]:
8785 out : dict [str , bytes ] = {}
8886 if not opts .no_bim :
89- out [".bim" ] = vcztools_plink .generate_bim (reader ).encode ("utf-8" )
87+ buf = io .StringIO ()
88+ vcztools .write_bim (reader , buf )
89+ out [".bim" ] = buf .getvalue ().encode ("utf-8" )
9090 if not opts .no_fam :
91- out [".fam" ] = vcztools_plink .generate_fam (reader ).encode ("utf-8" )
91+ buf = io .StringIO ()
92+ vcztools .write_fam (reader , buf )
93+ out [".fam" ] = buf .getvalue ().encode ("utf-8" )
9294 return out
9395
9496
@@ -104,18 +106,20 @@ def _bgen_static_suffixes(opts) -> tuple[str, ...]:
104106def _build_bgen_static (reader , opts ) -> dict [str , bytes ]:
105107 out : dict [str , bytes ] = {}
106108 if not opts .no_sample_file :
107- out [".sample" ] = vcztools_bgen .generate_sample (reader ).encode ("utf-8" )
109+ buf = io .StringIO ()
110+ vcztools .write_sample (reader , buf )
111+ out [".sample" ] = buf .getvalue ().encode ("utf-8" )
108112 if not opts .no_bgi :
109- # ``write_bgen_index `` takes a filesystem path. Materialise the
110- # .bgi into a TemporaryDirectory, read the bytes back, then let
111- # the context manager clean up. The encoder used to harvest
112- # ``variant_offsets`` is I/O-free in ``__init__`` and is
113- # discarded once the offsets are read.
113+ # ``write_bgi `` requires a filesystem path (sqlite3.connect needs
114+ # a real path). Materialise the .bgi into a TemporaryDirectory,
115+ # read the bytes back, then let the context manager clean up.
116+ # The encoder used to harvest ``variant_offsets`` is I/O-free in
117+ # ``__init__`` and is discarded once the offsets are read.
114118 with vcztools .BgenEncoder (reader ) as encoder :
115119 variant_offsets = encoder .variant_offsets
116120 with tempfile .TemporaryDirectory (prefix = "biofuse-bgen-" ) as tmp_dir :
117121 bgi_path = pathlib .Path (tmp_dir ) / "index.bgen.bgi"
118- vcztools_bgen . write_bgen_index (reader , str (bgi_path ), variant_offsets )
122+ vcztools . write_bgi (reader , str (bgi_path ), variant_offsets )
119123 out [".bgen.bgi" ] = bgi_path .read_bytes ()
120124 return out
121125
0 commit comments