bgen_file

bgen_file(filepath)

BGEN file handler.

bgen_file.close()

Close file stream.

bgen_file.contain_samples

Check if it contains samples.

bgen_file.create_metafile(filepath[, verbose])

Create metafile file.

bgen_file.filepath

File path.

bgen_file.nsamples

Number of samples.

bgen_file.nvariants

Number of variants.

bgen_file.read_genotype(offset[, precision])

Read genotype.

bgen_file.read_probability(offset[, precision])

Read genotype probability.

bgen_file.read_samples()

Read samples.

class cbgen.bgen_file(filepath)[source]

BGEN file handler.

>>> import cbgen
>>>
>>> bgen = cbgen.bgen_file(cbgen.example.get("haplotypes.bgen"))
>>> print(bgen.nvariants)
4
>>> print(bgen.nsamples)
4
>>> print(bgen.contain_samples)
True
>>> print(bgen.read_samples())
[b'sample_0' b'sample_1' b'sample_2' b'sample_3']
>>> mf = cbgen.bgen_metafile(cbgen.example.get("haplotypes.bgen.metafile"))
>>> part = mf.read_partition(0)
>>> gt = bgen.read_genotype(part.variants.offset[0])
>>> print(gt.probability)
[[1. 0. 1. 0.]
 [0. 1. 1. 0.]
 [1. 0. 0. 1.]
 [0. 1. 0. 1.]]
>>> mf.close()
>>> bgen.close()

Use with-statement context to guarantee file closing at the end.

>>> with cbgen.bgen_file(cbgen.example.get("haplotypes.bgen")) as bgen:
...     print(bgen.nvariants)
4
Parameters

filepath (Union[str, Path]) – BGEN file path.

close()[source]

Close file stream.

property contain_samples: bool

Check if it contains samples.

Return type

bool

Returns

True if it does contain samples; False otherwise.

create_metafile(filepath, verbose=False)[source]

Create metafile file.

Parameters
  • filepath (Union[str, Path]) – File path.

  • verboseTrue to show progress; False otherwise (default).

property filepath: pathlib.Path

File path.

Return type

Path

Returns

File path.

property nsamples: int

Number of samples.

Return type

int

Returns

Number of samples.

property nvariants: int

Number of variants.

Return type

int

Returns

Number of variants.

read_genotype(offset, precision=64)[source]

Read genotype.

Parameters
  • offset (int) – Variant offset.

  • precision (int) – Probability precision in bits: 64 (default) or 32.

Return type

Genotype

Returns

Genotype.

Raises

RuntimeError – If invalid offset of or a file stream reading error occurs.

read_probability(offset, precision=64)[source]

Read genotype probability.

Parameters
  • offset (int) – Variant offset.

  • precision (int) – Probability precision in bits: 64 (default) or 32.

Return type

Any

Returns

Probabilities.

Raises

RuntimeError – If invalid offset of or a file stream reading error occurs.

read_samples()[source]

Read samples.

Return type

Any

Returns

Samples.

Raises

RuntimeError – If samples are not stored or a file stream reading error occurs.