Package: bgen
Another bgen parserThis is a package for reading and writingbgen 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).Installpip install bgenUsagefrombgenimportBgenReader,BgenWriterbfile=BgenReader(BGEN_PATH)rsids=bfile.rsids()# select a variant by indexingvar=bfile[1000]# pull out genotype probabilitiesprobs=var.probabilities# returns 2D numpy arraydosage=var.minor_allele_dosage# returns 1D numpy array for biallelic variant# iterate through every variant in the filewithBgenReader(BGEN_PATH,delay_parsing=True)asbfile:forvarinbfile:dosage=var.minor_allele_dosage# get all variants in a genomic regionvariants=bfile.fetch('21',10000,5000000)# or for writing bgen filesimportnumpyasnpfrombgenimportBgenWritergeno=np.array([[1,0,0],[0,1,0],[0,0,1]]).astype(np.float64)withBgenWriter(BGEN_PATH,n_samples=3)asbfile:bfile.add_variant(varid='var1',rsid='rs1',chrom='chr1',pos=1,alleles=['A','G'],genotypes=geno)API documentationclassBgenReader(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:pathtobgenfilesample_path:optionalpathtosamplefile.SampleswillbegivenintegerIDsifsamplefileisnotgivenandsampleIDsnotfoundinthebgenfiledelay_parsing:True/FalseoptiontoallowfornotloadingallvariantsintomemorywhentheBgenFileisopened.ThiscansavetimewheniteratingacrossvariantsinthefileAttributes:samples:listofsampleIDsheader:BgenHeaderwithinfoaboutthebgenversionandcompression.Methods:slicing:BgenVarscanbeaccessedbyslicingtheBgenFilee.g.bfile[1000]iteration:variantsinaBgenFilecanbeloopedovere.g.forxinbfile:print(x)fetch(chrom,start=None,stop=None):getallvariantswithinagenomicregiondrop_variants(list[int]):dropsvariantsbyindexfrombeingusedinanalyseswith_rsid(rsid):returnsBgenVarwithgivenrsidat_position(pos):returnsBgenVaratagivenpositionvarids():returnslistofvaridsforvariantsinthebgenfile.rsids():returnslistofrsidsforvariantsinthebgenfile.chroms():returnslistofchromosomesforvariantsinthebgenfile.positions():returnslistofpositionsforvariantsinthebgenfile.classBgenVar(handle,offset,layout,compression,n_samples):# Note: this isn't called directly, but instead returned from BgenFile methodsAttributes:varid:IDforvariantrsid:referenceSNPIDforvariantchrom:chromosomevariantisonpos:nucleotidepositionvariantisatalleles:listofallelesforvariantis_phased:True/Falseforwhethervarianthasphasedgenotypedataploidy:listofploidyforeachsample.SamplesareorderedasperBgenFile.samplesminor_allele:theleastcommonallele(forbiallelicvariants)minor_allele_dosage:1Dnumpyarrayofminoralleledosagesforeachsamplealt_dosage:1Dnumpyarrayofaltalleledosagesforeachsampleprobabilitiies:2Dnumpyarrayofgenotypeprobabilities,onesampleperrowBgenVarscanbepicklede.g.pickle.dumps(var)classBgenWriter(path,n_samples,samples=[],compression='zstd'layout=2,metadata=None)# opens a bgen file to write variants to. Automatically makes a bgenix index fileArguments:path:pathtowritedataton_samples:numberofsamplesthatyouhavedataforsamples:listofsampleIDs(samelengthasn_samples)compression:compressiontype:None,'zstd',or'zlib'(default='zstd')layout:bgenlayoutformat(default=2)metadata:anyadditionalmetadatayouwantoincludeinthefile(asstr)Methods:add_variant_direct(variant)Arguments:variant:BgenVar,tobedirectlycopiedfromonebegnfiletoanother.Thiscanbedonewhenthenewbgenfileisforthesamesetofsamplesastheonebeingreadfrom.Thisismuchfasterduetonothavingtodecodeandre-encodethegenotypedata.add_variant(varid,rsid,chrom,pos,alleles,genotypes,ploidy=2,phased=False,bit_depth=8)Arguments:varid:variantIDe.g.'var1'rsid:referenceSNPIDe.g.'rs1'chrom:chromosomethevariantisone.g'chr1'pos:nucleotidepositionofthevariante.g.100alleles:listofallelestringse.g.['A','C']genotypes:numpyarrayofgenotypeprobabilities,orderedasperthebgensamplese.g.np.array([[0,0,1],[0.5,0.5,0]])ploidy:ploidystate,eitherasintegertoindicateconstantploidy(e.g.2),ornumpyarrayofploidyvaluespersample,e.g.np.array([1,2,2])phased:whetherthegenotypesareforphaseddataornot(default=False)bit_depth:howmanybitstostoreeachgenotypeas(1-32,default=8)