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.0-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a67a8b164609e49efd284d1d2545334d3dbdb412d229ab4a963dbce3ba70d48c |
|
MD5 | 06553891acc447cc011baea36b59109e |
|
BLAKE2b-256 | 3f9fcf3855a99c3fe7e5c4cc8dcf7df3e57a6e47e71ab8cd11198350f5748389 |
Hashes for bgen-1.7.0-cp312-cp312-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d15d0d540eac7e7cd18716774afe9753276c50862f4b80945e7a33b751373b8e |
|
MD5 | 38b59efefe125482db8476a721698901 |
|
BLAKE2b-256 | 2b0a50c05d8f8d830f6e44fc05a619908d8029a3704936ceb2eab80fd9ae4dbd |
Hashes for bgen-1.7.0-cp312-cp312-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | de204c504813e4194204c7f17d1052f057f6430a7ee1427bec8d7128ef6dddf6 |
|
MD5 | 0b56ad3bf29da4bca24371d832d4bb7e |
|
BLAKE2b-256 | 3dd344db1cab1362fd5d631345aa1d7a4669324cd632e16fbce494f0a6d9e74a |
Hashes for bgen-1.7.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 37f936eac34e5bedc9f11ce6339164f52357a62854f4e1bcd2263b9bbb97f836 |
|
MD5 | a0ba94bbfa0852060e2cfc984a15dc3b |
|
BLAKE2b-256 | 21fa3debe86f26881ff469b3010ab05b15d4fe8584749c9632847d349c6257fb |
Hashes for bgen-1.7.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | caa05976ed8aeed138f70d1bb32ef0ee6ecb630795bbb40980fe2e6697efac90 |
|
MD5 | 52b2ee406d9a7154c03e23c7cfc29675 |
|
BLAKE2b-256 | ad67a6e7895e4d4f6169055194c3ad1d4a5ec5b169a166fda92badd65b35ad85 |
Hashes for bgen-1.7.0-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 94570ef62b4faab97471a067f46c6b9f737e58557c75d66d8b2ea8f0f1adfd3b |
|
MD5 | 002adc6707008199aab43d187bc8a27a |
|
BLAKE2b-256 | a8f0cba38be0f5496e2137d35165c5d41c6d9b0ae504d933b052ab72549ffc73 |
Hashes for bgen-1.7.0-cp311-cp311-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d97cf735e808bc6239dab12924f656f1dcbe59fb630f8990dadc45fd93113fe |
|
MD5 | 5f29ee91a905d96b536a1ed1569d6128 |
|
BLAKE2b-256 | 1c0517d11e04e2580cf4a5a23fb546a0ec5d2cb2069eb2eb487ca684cb379a24 |
Hashes for bgen-1.7.0-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3d75b1dbea8e382fd7b08facc00928c749cdda37d4788d95fd71cdec7844201b |
|
MD5 | 7a7ace64b26edd59e758baf78c104f07 |
|
BLAKE2b-256 | 84d942c477a22b49c46c7a17a05fa81c3a57c9736db828879f3bad3c5e1ffb19 |
Hashes for bgen-1.7.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 37fcd94367e43d13d0142df92937c51c7244f8c526c388c0c362b9ea0e2af11a |
|
MD5 | 81aa70f527a6af07245523981204f8d0 |
|
BLAKE2b-256 | 122fbe1561f642677f77e4e4859c223941ef56142aeeb1a16a2e5fd92e704f92 |
Hashes for bgen-1.7.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ede4175cdb2aed9d85cfdec64ae84d979cf38f3dc82c0ee83b40ddb72936e459 |
|
MD5 | 136b6ccc5d843cc2751c004851aa4de4 |
|
BLAKE2b-256 | a885e91f85456cc0ede98cc5da73bfd94bb6e36966cd57f4fb5a67c76666b3a8 |
Hashes for bgen-1.7.0-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b03a769f1aea5ebabcf0567a7f5998c0910a457ca1b16efe1f3df97cc2f281e0 |
|
MD5 | 514f14b50cab40c7229386a0e21107ee |
|
BLAKE2b-256 | bd2307db3b8873e855e6ec67b16870646ab590e37cb10608bc6001501370ebb7 |
Hashes for bgen-1.7.0-cp310-cp310-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a1bb7af4ba48ccdc6e71573269818816c5544576ae7760c23cdd870a4908094 |
|
MD5 | 7c70dcec035f19843f6261ff2493bc3e |
|
BLAKE2b-256 | 46c09b86917919cbf99ed99dd81c69bc506728a4e27411b119569ca96c51ccfc |
Hashes for bgen-1.7.0-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e520c8e8bb8b6778233f68488a06d6527183c21fe8abbc9ab1599764f4f9a0b1 |
|
MD5 | c7d8caa161ae1d5ff8b29dfc7f534709 |
|
BLAKE2b-256 | c1484fab92523aad0dbd9838529151909d23d7ef18d3fb98996af0bd8d03c4dd |
Hashes for bgen-1.7.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9fb2b3c9b52f81c6fc79bc860dc3a19b3e742011c63c47cbcaf82ff53d77250d |
|
MD5 | cb1b2b3eabcd099a75b701cde0f0d40a |
|
BLAKE2b-256 | 88f4ed2065430b980141ad2a151708b9ea708d52d1e97875cb1ae08f7bfe13cd |
Hashes for bgen-1.7.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd7018dc9a8574a88b8819d6c1ff49f8a8afa91f94fc072e1fb0bbcc350666b0 |
|
MD5 | 511ca5bfc184aa1bce002893d7f44c66 |
|
BLAKE2b-256 | 43479070b3ed76202e2b6689265163305ee91ea90da96606c01d34ac011b5430 |
Hashes for bgen-1.7.0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 782d79bd0fd59dc03e5291761150cff50b3ec030b2de4e1270433d857089b111 |
|
MD5 | b9c93e3f82e1257a52e91408ff47c400 |
|
BLAKE2b-256 | b15cd1f454f007914fedf6f1c54323a198581d30cb5c6a1f37b1a2524f849446 |
Hashes for bgen-1.7.0-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 969ec5deb34d9189c66df6bb7f93fe9faaa4210dad287197f56ba9f27506b56f |
|
MD5 | 8d56ab62df29da6627d503ac50ff6a6c |
|
BLAKE2b-256 | b68f3f2f733d4fc553e74e6936647d18799e8e2a2a08f307b1b7b15e55cf4a9c |
Hashes for bgen-1.7.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0980aed3b913dfaf9aec6c9467cf8895883818b536733625c6ae348820780192 |
|
MD5 | 5871c3a74301781531da7439b9300088 |
|
BLAKE2b-256 | d797690fea4e9f3d137ec798e451bc9c2a7d8d6f4ca649e9003a009eba821806 |
Hashes for bgen-1.7.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dc75023563926eee34436e531bd7a86ad85d9a068d371f096a6d2935b5a055ed |
|
MD5 | 995d8e033da9152bfc5231266735010b |
|
BLAKE2b-256 | c54235bd6ded2d100205ac35cee71eca971314336d97aca1c061dd6974b41680 |
Hashes for bgen-1.7.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 56d525b1ab764b0f1b8b3f9f6ebc86af37da8aa2f82bc6d5e4038e45789c5f2e |
|
MD5 | 34cd9559bc50e066b4d51f98d5e86fa3 |
|
BLAKE2b-256 | a429d33ce76f64d292abba4850530fc17a645787a857ff231dc014ed0d6b89f9 |
Hashes for bgen-1.7.0-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf80b0939d088e9829a1929220074a7acc6710dee6e9b5caff498d0f929f39eb |
|
MD5 | 6375140a1020b94371bf464dbcf79dec |
|
BLAKE2b-256 | 4fd479f5904632a8583c0e1e6843a5e3c945c4ca79ca41b415a68a72e406a374 |
Hashes for bgen-1.7.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b14432271b97b2bf4c26a5ce755f2d7bb6c66e214ff85a492a87bf51dd3f04f8 |
|
MD5 | fb245ec1c91edf89bd1ff22d2b962731 |
|
BLAKE2b-256 | 56de72ebe9648e913ef59fe00d13c689c54eef2adf70aa09c1957b45384010df |
Hashes for bgen-1.7.0-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 93d8713bfa6c5f3d744076b6868d246e00e95f3ac268733b7de85a60b37c4860 |
|
MD5 | be5869543a6f9fef9be22db57ab00c35 |
|
BLAKE2b-256 | e97dc78d47eb5952872613f6c16c6985ab3f99f747d22d537e44957f5b4ecb86 |