Skip to content
Open

Bgen #227

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ To run PrediXcan Associations and MulTiXcan, you also need:
* [h5py-cache (>=1.0.0)](https://pypi.python.org/pypi/h5py-cache/1.0) *Now folded into h5py

To run prediction of biological mechanisms on individual-level data, you will also need:
* [bgen_reader (>=3.0.3)](https://pypi.org/project/bgen-reader/)
* [bgen (>=1.9.10)](https://pypi.org/project/bgen/)
* [cyvcf2 (>=0.8.0)](https://brentp.github.io/cyvcf2)

[R](https://www.r-project.org/) with [ggplot](http://ggplot2.org/) and [dplyr](https://cran.r-project.org/web/packages/dplyr/index.html)
Expand Down
6 changes: 3 additions & 3 deletions software/ToHDF5.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from metax import Logging, Utilities

import numpy as np
import h5py_cache
import h5py
import pandas

def run(args):
Expand All @@ -18,7 +18,7 @@ def run(args):
data = pandas.read_table(args.input)

logging.info("Opening output")
f = h5py_cache.File(args.output, 'w', chunk_cache_mem_size=int(50 * (1024 ** 2)))
f = h5py.File(args.output, 'w', rdcc_nbytes=int(50 * (1024 ** 2)))

n_genes = data.shape[1]-2
n_samples = data.shape[0]
Expand Down Expand Up @@ -58,4 +58,4 @@ def run(args):

args = parser.parse_args()
Logging.configureLogging(args.verbosity)
run(args)
run(args)
8 changes: 3 additions & 5 deletions software/conda_env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ channels:
dependencies:
- python=3.9
- pandas=2.2
- scipy=1.12
- numpy=1.26
- pyliftover=0.4
- statsmodels=0.14
- h5py=3.12
- pyarrow=19.0
- pip=25.0
- pip:
- cyvcf2==0.30.28
- bgen-reader==4.0.8

- bgen==1.9.10
- scipy==1.12
- numpy==1.26
6 changes: 3 additions & 3 deletions software/metax/MainScreen.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def __init__(self,args):
def run(self):
try:
MetaXcan.run(self.args)
except Exceptions.ReportableException, e:
except Exceptions.ReportableException as e:
logging.error(e.msg)
except Exception as e:
logging.info("Exception when running task: %s", str(e))
Expand All @@ -368,12 +368,12 @@ def run(self):
work = self.works[i]
work.run()
del self.works[i]
except Exceptions.ReportableException, e:
except Exceptions.ReportableException as e:
logging.error(e.msg)
except Exception as e:
logging.info("Exception when running task: %s", str(e))
finally:
pass

work = MetaxcanWorkWrapper(beta_args)
return work
return work
2 changes: 1 addition & 1 deletion software/metax/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.7.5"
__version__ = "0.8.2"

def exitIf(doExit, Exception, msg):
if doExit:
Expand Down
23 changes: 10 additions & 13 deletions software/metax/genotype/BGENGenotype.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import bgen_reader
import numpy
import pandas
import logging
from bgen import BgenReader

from ..misc import Genomics

def bgen_file_geno_lines(file, variant_mapping = None, force_colon = False, use_rsid=False, whitelist=None, skip_palindromic=False, liftover_conversion=None):
logging.log(9, "Processing bgen %s", file)
bgen = bgen_reader.read_bgen(file)
variants = bgen["variants"]
bfile = BgenReader(file)
dict_mapping = variant_mapping is not None and type(variant_mapping) == dict
for variant in variants.itertuples():
for variant in bfile:
if use_rsid:
varid = variant.rsid
else:
varid = variant.id
varid = variant.varid

if force_colon:
varid = varid.replace("_", ":")

alleles = variant.allele_ids.split(",")
alleles = variant.alleles
if len(alleles) > 2:
logging.info("variant %s is multiallelic, skipping", varid)
continue
Expand Down Expand Up @@ -51,11 +50,10 @@ def bgen_file_geno_lines(file, variant_mapping = None, force_colon = False, use_
# the alleles in the genotype might be swapped respect the variant in the mapping
# You should verify if you must match it

v = bgen["genotype"][variant.Index].compute()
if v["phased"]:
d = numpy.apply_along_axis(lambda x: x[1] + x[3], 1, numpy.array(v["probs"], dtype=float))
if variant.is_phased:
d = numpy.apply_along_axis(lambda x: x[1] + x[3], 1, variant.probabilities)
else:
d = numpy.apply_along_axis(lambda x: x[1] + x[2] * 2, 1, numpy.array(v["probs"], dtype=float))
d = numpy.apply_along_axis(lambda x: x[1] + x[2] * 2, 1, variant.probabilities)

#e = bgen_reader.allele_expectation(bgen, variant.Index)
#d2 = bgen_reader.compute_dosage(e, alt=1)
Expand All @@ -70,7 +68,6 @@ def bgen_files_geno_lines(files, variant_mapping = None, force_colon = False, us

def get_samples(path):
logging.info("Opening bgen to get samples")
bgen = bgen_reader.read_bgen(path, verbose=False)
samples = bgen["samples"].values
samples = pandas.DataFrame({"FID":samples, "IID":samples})[["FID", "IID"]]
bfile = BgenReader(path)
samples = pandas.DataFrame({"FID":bfile.samples, "IID":bfile.samples})[["FID", "IID"]]
return samples
15 changes: 12 additions & 3 deletions software/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,23 @@ def read(fname):
'MulTiXcan.py',
'SMulTiXcan.py'],
description=["TBD"],
install_requires=['scipy>=1.2.2', 'numpy>=1.14.2', 'pandas>=0.22.0', 'patsy>=0.5.0',
'statsmodels>=0.10.0', 'h5py>=2.7.1', 'h5py-cache>=1.0', 'bgen_reader>=3.0.3', 'cyvcf2>=0.8.0'],
install_requires=[
'numpy>=1.23,<2',
'scipy>=1.10,<2',
'pandas>=2.0,<3',
'patsy>=0.5.6',
'statsmodels>=0.14,<0.15',
'h5py>=3.10,<4',
'pyliftover>=0.4',
'bgen==1.9.10',
'cyvcf2>=0.30',
],
extras_require={"test": ["sqlalchemy"]},
long_description=read('Readme.md'),
keywords=['TBD'],
test_suite='tests',
package_data={'tests/files':['*']},
python_requires='>=3.5',
python_requires='>=3.9',
classifiers=[
"Development Status :: 4 - Beta",
"Topic :: Utilities",
Expand Down