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 list of BgenVars with given rsid
at_position(pos): returns list of BgenVars 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
probabilities: 2D numpy array of genotype probabilities, one sample per row
These are most likely for biallelic diploid variants. In that scenario
unphased probabilities have three columns, for homozygous first allele
(AA), heterozygous (Aa), homozygous second allele (aa).
In contrast, phased probabilities (for a biallelic diploid variant) would
have four columns, first two for haplotype 1 (hap1-allele1, hap1-allele2),
last two for haplotype 2 (hap2-allele1, hap2-allele2).
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.7.1-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 29ccdbc76170e553d8573d524725e6179417c3d83982b431551eb8684d99078e |
|
MD5 | ac1515f233357c54cc39edc40a0cf555 |
|
BLAKE2b-256 | 152bf0e49fd002c7e806f2ebe9b976a045722ce56d0669b8b8dfdafdcb5d69ef |
Hashes for bgen-1.7.1-cp312-cp312-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 89fb79414fdc367ef1fba5cd90527b0de01d1bd62d90d05d78f8d95854896cdd |
|
MD5 | ebe17f4ccfedf2bb75d9197ceb3740b7 |
|
BLAKE2b-256 | 663d95b1326c13f51ab3cf58761d50e06593f57d2893f14646b4adfc74003949 |
Hashes for bgen-1.7.1-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 801325aeecafe7a7a37c5eecc378cdac4b6f0f27856bcf0f8b63b7afc4bcbd1a |
|
MD5 | 5dfaa3359838e8bd830d14e1dedcc350 |
|
BLAKE2b-256 | b14255849462c5f5eb1ea74038903ef1e3162a347f61539732da1c3f5a70fe81 |
Hashes for bgen-1.7.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 06b88592006ef3a33c935ee9b2fc7c9ec3e7b69834f3e89fb7b13d18fe1f37b9 |
|
MD5 | 6129f6c2bad1d4477ba1cf14883cc2ce |
|
BLAKE2b-256 | e48828a6ec4ca55fde076c4be0508e89d1e7a8a3001c648ebff770947be28deb |
Hashes for bgen-1.7.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4750884289ddcde17e6d6fdeaa88c02858999fb6012f101e77b9031ec92aa17e |
|
MD5 | 3e4a962b28455283f4c5f2ccacf1fdc3 |
|
BLAKE2b-256 | 8d2285f8d1d9f1836315a53d5745aec67689a1a397c66d22f1dde4721df4b021 |
Hashes for bgen-1.7.1-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 67683a525c302619980df0f2a20c492519a7fb94b4637cbeaf18478eb3540ce2 |
|
MD5 | 42ebb6472bd95dfa6cef3091b781d0f2 |
|
BLAKE2b-256 | d187ce80b733e0c05730f78d5e3c9065a4d260cb0eb8e7ac26b26478486a81ab |
Hashes for bgen-1.7.1-cp311-cp311-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a6cb996efd3219ff7d8b3ab304622311adce25dcfa2695e4ab24786065be80ef |
|
MD5 | 5d26ab5703feb87e242ff9fb0d4833a8 |
|
BLAKE2b-256 | b40ce1f1084c845624fd271fa01734a2a1349942a3dc10986f5b7f092e6095cc |
Hashes for bgen-1.7.1-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ed94af6992e397d83537d6de3607166845dc29d2f2ee451d68718fa58babf7b |
|
MD5 | 4bb28b0a09cf9b7f0bb4ca628f75f079 |
|
BLAKE2b-256 | 210e4c94d764368b80666667f0f68ba96fe8824b46d1976e2ba2607cc6aebeea |
Hashes for bgen-1.7.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 74ec45a7d76faa0a1c741e5145fbe9e8200939a67cef513c42d2797c3161e6ce |
|
MD5 | 13e9df95fda0c85970e041a01f750a6b |
|
BLAKE2b-256 | 4b2b45815a8127e6ed6b99d69690d819fafbcdaf208d56834a56a302c46acef7 |
Hashes for bgen-1.7.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c2bfb06d9ce5e4e97c33f8e91d0dcdf5cb5c957f4760c6e3c5f2df6cda4fe4be |
|
MD5 | c6800ffe49f666c8bac2945ce3e285ed |
|
BLAKE2b-256 | a4f93ee756a023ff37a6fd5924b9567eff86747ef574c26f41060d6377af4651 |
Hashes for bgen-1.7.1-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f3671dc616b4cf625f5656f216ebbc87df3b420046f2cc5564086ec82f71f84a |
|
MD5 | 1d917e7b8c452b921a3b87170c1b0766 |
|
BLAKE2b-256 | 7cef448f6601444ecd255194469a1738b8bba95426b991dcd4e0a322a86f4163 |
Hashes for bgen-1.7.1-cp310-cp310-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b6c76471dca6c2a6e33fcd3ddc22ba9b1c7e689487f8224d979585e23394b70 |
|
MD5 | 41acb740b09ae2fabff2c45fc52d05ad |
|
BLAKE2b-256 | 3e6f15df3a68390c86262153630e336918c565919db6ce2fb8922fd0e3b3b54d |
Hashes for bgen-1.7.1-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6719bbba9fbf7b559a2c045a60ef81f611d9f8607e7f7d3a5d7c61bd22248574 |
|
MD5 | 63563523b0b91fd968c13f78733dc5df |
|
BLAKE2b-256 | f40fa901aacb8bfa7ab132509cabd42bfc2bb1df16071e44488fd3c52dddc171 |
Hashes for bgen-1.7.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c6944550721c1addf7dc12442d241f38b8f9d7fe91dde1819c79755cec2e39f |
|
MD5 | 5db9a8d41f24b754a6c892c25ec337e7 |
|
BLAKE2b-256 | 9cf892bb484c0af02432570c5d32daef3ae441414586e57a450d6a8e8be56193 |
Hashes for bgen-1.7.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6996376699053bfc8aa254bba347ffdd172449f78f9d4b98eebedcebe6a4c9ae |
|
MD5 | 16365e447a1ccb514a79d854a41cc700 |
|
BLAKE2b-256 | 732d066e6ef069d084adb6c2f0d49e110eb47d89e146a6e9ce5c5ff992c917d8 |
Hashes for bgen-1.7.1-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 83517807875306c9ba369c720d0c72fde68b93f79c06414cb17d7c70815473e9 |
|
MD5 | 0dc9ad7f364fde32215fa00c1162ff6d |
|
BLAKE2b-256 | 609f19e6887552caea1c9f678b4c9089234c7c41361a59bd70307f7c52f1a15f |
Hashes for bgen-1.7.1-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 84aa3a1798a7451879a7122abd7452273c1fb9a2e22be2f28207dd679a39337a |
|
MD5 | 3a8e635b5fd02a25cedf49482e54ee29 |
|
BLAKE2b-256 | 89cc5cfda57c97f5367fe9dd65597fb73d901cd4e98d612b677bf06f57465dba |
Hashes for bgen-1.7.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 93679523138f7466be4b0cdf710ad0ada18a17f379d86129ec68e90cf689e8c2 |
|
MD5 | 5828a5fc741bb061c8f4e4b3ec958b71 |
|
BLAKE2b-256 | d82faf264ab4db853fb23ced662b3ec1b34149440241408fb3da21b1ec367b28 |
Hashes for bgen-1.7.1-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a4f27ef567c08370cba55f7a7877b6a223bc507a1672cb2102793f092766efc |
|
MD5 | 71c31edb8475e97b86960ae25f951b73 |
|
BLAKE2b-256 | e7746e5f9f93375d6576bc5dd419d3599487c1b9802df0d345223083adf350fb |
Hashes for bgen-1.7.1-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 74fb2a635f1246b219ef6f966dd013f9e52e7136867a1cea5a25801bd5c4a732 |
|
MD5 | 7eedf0e872595385259fc2475bcf0766 |
|
BLAKE2b-256 | 6a1f5086f7f54458b5d077c401f7d1745df85c8576344e3ff12e55597046b51d |
Hashes for bgen-1.7.1-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae3436d7866875637984045e03321eea90459c567db480ed76cf8f78b4d4ebc3 |
|
MD5 | 55f4b5bfafa8ecec6a9e605ce17250d3 |
|
BLAKE2b-256 | 14b7a3616ad524f938417ef3f1751db6273224f388cba2177274afac950fb087 |
Hashes for bgen-1.7.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d6d66ef8bc9f17f5e1bba97b65d2404829ced1ea5f1740dcf983d2a28d18a975 |
|
MD5 | 44a52148367ee98da8a238a0f475cd61 |
|
BLAKE2b-256 | ce3abe90e7c4cb84b2db6042213af8ffceebadd6c7d57cd9474cdd5e64c4dc31 |
Hashes for bgen-1.7.1-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dc45cab45687ac1f220f6dc6bc2385d067236b12ab34a1646136cbe1b13af95f |
|
MD5 | 67e5438a19e425e02eae4e1bf7fe470f |
|
BLAKE2b-256 | 368c20ecb4425f0fb24d832d2f98007f02942d82f99017d5cb18d47822d68d35 |