This is a test version of Biostars. For the public version, visit https://www.biostars.org.
What is the best way to load bgen files into python?

I am new to the BGEN format, (I mainly work with PLINK formatted files or VCFs). I have been trying to load a series of BGEN files to python using bgen-reader; however, there has been some issues that the developers have not been able to solve. Is there an alternative to loading BGEN files into python? maybe change the BGEN to another format and then import them to python?

bgen python

1 answer

bgen-reader is available via PyPi or Conda. It offers a NumPy-inspired API. Here is a sample:

>>> # Download a sample file
>>> from bgen_reader import example_filepath
>>> bgen_file = example_filepath("example.bgen")

>>> # Read from the file
>>> from bgen_reader import open_bgen
>>> bgen = open_bgen(bgen_file, verbose=False)
>>> probs0 = bgen.read(0)   # Read 1st variant
>>> print(probs0.shape)     # Shape of the NumPy array
(500, 1, 3)
>>> probs_all = bgen.read() # Read all variants
>>> print(probs_all.shape)  # Shape of the NumPy array
(500, 199, 3)

I'm one of the authors and happy to help users.

  • Carl
  • Carl Kadie, Ph.D.
  • FaST-LMM & PySnpTools Team (also Bgen-Reader!)
  • (Microsoft Research, retired)

Log in to answer this question.