Package for loading data from bgen files
Project description
Another bgen parser
This is a package for reading and writing bgen files.
This package uses cython to wrap c++ code for parsing bgen files. It's fairly quick, it can parse genotypes from 500,000 individuals at ~300 variants per second within a single python process (~450 million probabilities per second with a 3GHz CPU). Decompressing the genotype probabilities can be the slow step, zlib decompression takes 80% of the total time, using zstd compressed genotypes is ~2X faster.
This has been optimized for UKBiobank bgen files (i.e. bgen version 1.2 with zlib compressed 8-bit genotype probabilities, but the other bgen versions and zstd compression have also been tested using example bgen files).
Install
pip install bgen
Usage
from bgen import BgenReader, BgenWriter
bfile = BgenReader(BGEN_PATH)
rsids = bfile.rsids()
# select a variant by indexing
var = bfile[1000]
# pull out genotype probabilities
probs = var.probabilities # returns 2D numpy array
dosage = var.minor_allele_dosage # returns 1D numpy array for biallelic variant
# iterate through every variant in the file
with BgenReader(BGEN_PATH, delay_parsing=True) as bfile:
for var in bfile:
dosage = var.minor_allele_dosage
# get all variants in a genomic region
variants = bfile.fetch('21', 10000, 5000000)
# or for writing bgen files
import numpy as np
from bgen import BgenWriter
geno = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]).astype(np.float64)
with BgenWriter(BGEN_PATH, n_samples=3) as bfile:
bfile.add_variant(varid='var1', rsid='rs1', chrom='chr1', pos=1,
alleles=['A', 'G'], genotypes=geno)
API documentation
class BgenReader(path, sample_path='', delay_parsing=False)
# opens a bgen file. If a bgenix index exists for the file, the index file
# will be opened automatically for quicker access of specific variants.
Arguments:
path: path to bgen file
sample_path: optional path to sample file. Samples will be given integer IDs
if sample file is not given and sample IDs not found in the bgen file
delay_parsing: True/False option to allow for not loading all variants into
memory when the BgenFile is opened. This can save time when iterating
across variants in the file
Attributes:
samples: list of sample IDs
header: BgenHeader with info about the bgen version and compression.
Methods:
slicing: BgenVars can be accessed by slicing the BgenFile e.g. bfile[1000]
iteration: variants in a BgenFile can be looped over e.g. for x in bfile: print(x)
fetch(chrom, start=None, stop=None): get all variants within a genomic region
drop_variants(list[int]): drops variants by index from being used in analyses
with_rsid(rsid): returns BgenVar with given rsid
at_position(pos): returns BgenVar at a given position
varids(): returns list of varids for variants in the bgen file.
rsids(): returns list of rsids for variants in the bgen file.
chroms(): returns list of chromosomes for variants in the bgen file.
positions(): returns list of positions for variants in the bgen file.
class BgenVar(handle, offset, layout, compression, n_samples):
# Note: this isn't called directly, but instead returned from BgenFile methods
Attributes:
varid: ID for variant
rsid: reference SNP ID for variant
chrom: chromosome variant is on
pos: nucleotide position variant is at
alleles: list of alleles for variant
is_phased: True/False for whether variant has phased genotype data
ploidy: list of ploidy for each sample. Samples are ordered as per BgenFile.samples
minor_allele: the least common allele (for biallelic variants)
minor_allele_dosage: 1D numpy array of minor allele dosages for each sample
alt_dosage: 1D numpy array of alt allele dosages for each sample
probabilitiies: 2D numpy array of genotype probabilities, one sample per row
BgenVars can be pickled e.g. pickle.dumps(var)
class BgenWriter(path, n_samples, samples=[], compression='zstd' layout=2, metadata=None)
# opens a bgen file to write variants to. Automatically makes a bgenix index file
Arguments:
path: path to write data to
n_samples: number of samples that you have data for
samples: list of sample IDs (same length as n_samples)
compression: compression type: None, 'zstd', or 'zlib' (default='zstd')
layout: bgen layout format (default=2)
metadata: any additional metadata you want o include in the file (as str)
Methods:
add_variant_direct(variant)
Arguments:
variant: BgenVar, to be directly copied from one begn file to
another. This can be done when the new bgen file is for the same
set of samples as the one being read from. This is much faster
due to not having to decode and re-encode the genotype data.
add_variant(varid, rsid, chrom, pos, alleles, genotypes, ploidy=2,
phased=False, bit_depth=8)
Arguments:
varid: variant ID e.g. 'var1'
rsid: reference SNP ID e.g. 'rs1'
chrom: chromosome the variant is on e.g 'chr1'
pos: nucleotide position of the variant e.g. 100
alleles: list of allele strings e.g. ['A', 'C']
genotypes: numpy array of genotype probabilities, ordered as per the
bgen samples e.g. np.array([[0, 0, 1], [0.5, 0.5, 0]])
ploidy: ploidy state, either as integer to indicate constant ploidy
(e.g. 2), or numpy array of ploidy values per sample, e.g. np.array([1, 2, 2])
phased: whether the genotypes are for phased data or not (default=False)
bit_depth: how many bits to store each genotype as (1-32, default=8)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for bgen-1.6.2-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7e89a3075a625ef14f2f337e7af17dc70aa9e4e8bf72c3cade02c498d311d9db |
|
MD5 | bfccdf65370b6c0553fe22864ba46940 |
|
BLAKE2b-256 | 60b245a531760e29bb5d0407615b6a8775c3a36d1b61749b9c76c091cc2ed09b |
Hashes for bgen-1.6.2-cp312-cp312-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 94b41f265498a9aa01904c4d72474db1868dc877ef00576454fe69ec036d47ca |
|
MD5 | f2a83a209468ca940e28610b026802a6 |
|
BLAKE2b-256 | d519d211cad0b6eb93017eaf0983a18f6cda16782e6c312e41dfa3eb17a8a8f5 |
Hashes for bgen-1.6.2-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 525bc37666c69f70e0b3c6aa52a0bb3597b5c5324df3e496987e8902f7b7fec5 |
|
MD5 | 34923fbc0c6cfc75cb1f8b3486aa4599 |
|
BLAKE2b-256 | 0d0bd889d9e3a0423031d8f2e0fdda06bd14e0ffb4055414a232f49c441885ae |
Hashes for bgen-1.6.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b00a83a0b8d2e928c9bb2a0ad5b40abe19819cb1a86e09b916d8ae18e9bbc6ec |
|
MD5 | b3448165f1c57f01ce5e49e082a52c2d |
|
BLAKE2b-256 | 64e295923ce3a68f8cce9b75be5439e81098242cbd665162bd5458c17e881e54 |
Hashes for bgen-1.6.2-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea4203a04a4ccbf34d1ffbb08191be2162ca671e2dd97415b9d7c235ef4766cb |
|
MD5 | 5d6b4860cefddff9239883a22d29e9fc |
|
BLAKE2b-256 | 78a1a3103cdee51ab536b7af4c5e6370da7a727d38010aad4ec5c713a8d472fd |
Hashes for bgen-1.6.2-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 077e385b63c0d84ec0256419f4591cb4cb9370632fa6c66de4ddf88884380eb7 |
|
MD5 | 43feef8148df5d4611f67f5b6e5069f9 |
|
BLAKE2b-256 | 99cf177bc89fa533bde17b799228ecffc279a62c3d49bb0c68a1e30a4acc04d3 |
Hashes for bgen-1.6.2-cp311-cp311-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae2c8e35593af256dd9940d4a44189d8c160fe0577f5188a5086248b8dac1776 |
|
MD5 | 014dfc2dd9215dd815ea660d2c9409ff |
|
BLAKE2b-256 | e1d65713475a9a00746fa580e9fd47d14a62a300c3d67dfdee07b7f00cdeebbc |
Hashes for bgen-1.6.2-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6dd0410272a7cec9d902da2d3a8439bc7d5fe0c3ababfe4a3fd41fa463690b27 |
|
MD5 | 5489cf9708bc215b0d30439fdf3c4ac8 |
|
BLAKE2b-256 | b03cf99b571b4d299703961a5856d2b2a24d3d5d96e7d9207912b99218ea8cdd |
Hashes for bgen-1.6.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ebd8c41368d2c921d139dfae9a44bfcd0d373e1d56a9a0b0ffaf659d449738e9 |
|
MD5 | 876cf74df2b9de0565214c5869e53858 |
|
BLAKE2b-256 | d6794dfbad222f41ee958e10ae8869afbf9401deaf631cf776862d7b3e4f6de1 |
Hashes for bgen-1.6.2-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 073ec5446216500c8d9d846672c97b25b1ccba41e719a0c13af360d3f55f899c |
|
MD5 | 3130e5d374e62416803ea5ad1ac65ae2 |
|
BLAKE2b-256 | 89a51af6a88e7ff1fccae2a6f2780f0fa26dabf3a517d0d58dd9112027325061 |
Hashes for bgen-1.6.2-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2b13ad3b168e54ee11ee70e97a719583dafaa1e0bbdb9e2c105cdbad4a5456a6 |
|
MD5 | c95c6e12a638418f5cbb013867cb3726 |
|
BLAKE2b-256 | 7bdc75c92158b188d392783dbba46ad8d42b1435aea7eeb7f8c87032adbb7283 |
Hashes for bgen-1.6.2-cp310-cp310-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a84cdf99435e770932013eecc74ddf0aaa92139170313641fefa19517afb657d |
|
MD5 | 2bd61be8744200b1ebd2e5800fc50c40 |
|
BLAKE2b-256 | 3fa55650d4d0a36f97839bab41749bf452678764f64fe42d67876805ebf3ac0b |
Hashes for bgen-1.6.2-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e298a9d4aaaf35b155cac5a5754d693def4452e522b76f77136c5e92ecd2e6f7 |
|
MD5 | af9ce612d5b9dd643e61516b1b7e60e2 |
|
BLAKE2b-256 | 7154d11a0871ce52dd7762b79427ca71ff54746642e3213d232962500a00d5f4 |
Hashes for bgen-1.6.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff79d4a0e131650aee1878c7e4e0e094e88b6a5828ac1f5dd25d10e7b78eafd0 |
|
MD5 | c75fee339776a0139e6aa84112050e4b |
|
BLAKE2b-256 | 8467b513eee8a1a01eea561384f5fb94f971f543413ffc6001ef05752b3685b2 |
Hashes for bgen-1.6.2-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1daf5bb597569a079c6732bffec8c40a20dd9d6d9af88ae52801c8a540fb8a4b |
|
MD5 | bd3c70bdc0fda856449b22981f82cabd |
|
BLAKE2b-256 | cc34fda89ec4d7d0721a42886e07646fb5a0eb30648b17e4b02f46d3dc9a692a |
Hashes for bgen-1.6.2-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd4bf90457d076fdda5134e76cba47c0a30d306561479650417f521615b77edf |
|
MD5 | ff03196a3dfae8ff30a1bf4693413c93 |
|
BLAKE2b-256 | 814a32f6427dcc36c1df417529e02ca06b51b35809f431a389f588fabbabecfa |
Hashes for bgen-1.6.2-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 02e0b2101b79d4dda8a565a2b4d243e15408636879e3da9ab673dcfc8a08041b |
|
MD5 | c4b41f813a74ff5eea62cc8fd031f1e4 |
|
BLAKE2b-256 | f5b746c9edbe98e93f6e18890d97d3ed7e6c386ec0b8a1b342643abceb0ba88a |
Hashes for bgen-1.6.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c6d958c5222ad53fceed67462ec4591e3c5b53cd3fb77016d72cbb122be42e46 |
|
MD5 | ae05f69b4705e83094755facf5e3dce1 |
|
BLAKE2b-256 | d1351461789c833c739c2a477feb8eda2e889119212d340095053a6b5dd40b18 |
Hashes for bgen-1.6.2-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6843f411f4c10021b4eb7781789e5e734c61a131064004721ed72022d530db74 |
|
MD5 | 53013843d97ab37ad30e5af179586abe |
|
BLAKE2b-256 | 242317bdab662b8ac1220767113e69208c49061614e81099fbe5a2f59d3212e0 |
Hashes for bgen-1.6.2-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cfbe1f2fc3336b525559b08153b58719e81833dc7e5ca7500002bb6909f0a793 |
|
MD5 | 8addd993ceef31802b3fceab28aadd64 |
|
BLAKE2b-256 | 1f33af08d20f0dd6ad62cccc6a44a52b9aaaeb57636c098d1c6149bd033a1cd0 |
Hashes for bgen-1.6.2-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a355c42c73b27cce6b9a7d28f422a6b24a8b390bebfce3192b606e5fc5af942d |
|
MD5 | e5076cd10c6ce265dcde117c2be43e0f |
|
BLAKE2b-256 | 087134a498231ee0d0b5342a69df405c3ac14384cb59edf780aa8800084371a7 |
Hashes for bgen-1.6.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 497fd952378c58499623556a83c3cc32d572caba572bde6399f58697d7076936 |
|
MD5 | 770e724a5e0ea116a825cebda817f1de |
|
BLAKE2b-256 | 0de2d99ed810bbc76a4d14236217ec00f7aca7c4f54e0639fccd9b99a98586a5 |
Hashes for bgen-1.6.2-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48a28da5100aa85d3ef9476ecb56fbaf0d30c7bed275123d4aa720b36881cb31 |
|
MD5 | df9bb48eafe830686aa4921884337950 |
|
BLAKE2b-256 | bf7775ab4dc0e1a07fe16095163673255aa65aff572064820b37b8c8e6390b7b |