Package for loading data from bgen files
Project description
Another bgen reader
This is a package for reading 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). Using zstd compressed genotypes would be even faster, maybe 1.5X 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 BgenFile
bfile = BgenFile(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 BgenFile(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)
API documentation
class BgenFile(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(pos): returns BgenVar with given position
at_position(rsid): returns BgenVar with given rsid
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
probabilitiies: 2D numpy array of genotype probabilities, one sample per row
BgenVars can be pickled e.g. pickle.dumps(var)
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.2.12-cp38-cp38-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5a58dd3bc98716d86eb3785ca4e6819ec09594bd40dc10c54eac13fb5ea6309 |
|
MD5 | 8dd51f53eb745cf0c0b0b3d5d5e585d9 |
|
BLAKE2b-256 | 6278c61a93feca006475e698e2888e97f3b11a9a4889ebd5e38ae8db818cb81d |
Hashes for bgen-1.2.12-cp38-cp38-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b24aeedb61d65bdd1f5d3ec6b94d720b2fec835ead9242c6a5272b5adb97095a |
|
MD5 | f2a52e74fa7044fee85d2766b4d9cc05 |
|
BLAKE2b-256 | d44cee09a9bf43e7cbcafeb02a67c73c65689f36baedba64a197a8e26f5de866 |
Hashes for bgen-1.2.12-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3e45378b8b5c4d3d2694c69db24755d687923568551e76267cdd50dcd3b2d699 |
|
MD5 | 16d7c85d13326e567afdf26b7560c1c6 |
|
BLAKE2b-256 | f01bc07e8b39fcfb3d4aac7c29d3a0d0ff9ceb3cf62ca865b928d43811fcb048 |
Hashes for bgen-1.2.12-cp37-cp37m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 233d9ea2c3758f9756319195c0318f5c701ac1fb30b8010cfc2e675bbca306bd |
|
MD5 | 72447303630aa96ea3629014e6e71f7a |
|
BLAKE2b-256 | 07113f1c4542a49bad92b1c0574136656ec9b3be1352184e9d0870dd8d4ebd3e |
Hashes for bgen-1.2.12-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 489a9afa9ad96fbd8aab5eb6f65991b140e2fe2f13289404669bd94051396ed8 |
|
MD5 | 9182eefcef329fb14ffd1b4d83e9dcb6 |
|
BLAKE2b-256 | 0936c771f1474a72464fad6f7c0b1b9fdd60b48ed75470e70d749785847c692f |
Hashes for bgen-1.2.12-cp36-cp36m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c9c4c50540eb4db1cb0cccaa3e0c7eb8a4e0fbd8ab301ac08379df1d670dd15 |
|
MD5 | 3a947ccf226050f9a990f8af764d910e |
|
BLAKE2b-256 | 815ad6d8420740dc944b914ff4ffa4d8f12031eeac7cc33044ea238822df0475 |