Skip to content

Commit

Permalink
read_ploidy and read_missing in C level
Browse files Browse the repository at this point in the history
  • Loading branch information
horta committed Aug 11, 2020
1 parent b40fc36 commit eceda00
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 11 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ install:
- isort --check-only .
- python3 setup.py test
- python3 -m pip install cibuildwheel
- git clean -xdfq
script:
- python3 -m cibuildwheel --output-dir wheelhouse
after_success:
Expand Down
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ include README.md
include bgen_reader/athr.dll
include bgen_reader/bgen.dll
include bgen_reader/interface.h
include bgen_reader/genotype.c
include bgen_reader/genotype.h
include bgen_reader/partition.c
include bgen_reader/partition.h
include bgen_reader/samples.c
Expand Down
2 changes: 1 addition & 1 deletion bgen_reader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

raise RuntimeError(str(e) + _ffi_err)

__version__ = "4.1.0"
__version__ = "4.0.5"

__all__ = [
"__version__",
Expand Down
16 changes: 9 additions & 7 deletions bgen_reader/_bgen_file.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from math import floor, sqrt
from pathlib import Path

from numpy import asarray, float64, full, nan, zeros
from numpy import float64, full, nan, uint8, zeros
from pandas import Series

from ._ffi import ffi, lib
Expand Down Expand Up @@ -71,13 +71,15 @@ def read_genotype(self, offset: int):
lib.bgen_genotype_read(genotype, ffi.cast("double *", probs.ctypes.data))

phased = lib.bgen_genotype_phased(genotype)
ploidy = asarray(
[lib.bgen_genotype_ploidy(genotype, i) for i in range(nsamples)], int
)
missing = asarray(
[lib.bgen_genotype_missing(genotype, i) for i in range(nsamples)], bool
)

ploidy = full(nsamples, 0, dtype=uint8)
lib.read_ploidy(genotype, ffi.cast("uint8_t *", ploidy.ctypes.data), nsamples)

missing = full(nsamples, 0, dtype=bool)
lib.read_missing(genotype, ffi.cast("bool *", missing.ctypes.data), nsamples)

lib.bgen_genotype_close(genotype)

return {"probs": probs, "phased": phased, "ploidy": ploidy, "missing": missing}

def close(self):
Expand Down
18 changes: 18 additions & 0 deletions bgen_reader/genotype.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>

static void read_ploidy(struct bgen_genotype const* genotype, uint8_t* ploidy,
uint32_t nsamples)
{
for (uint32_t i = 0; i < nsamples; ++i)
ploidy[i] = bgen_genotype_ploidy(genotype, i);
}

static void read_missing(struct bgen_genotype const* genotype, bool* missing,
uint32_t nsamples)
{
for (uint32_t i = 0; i < nsamples; ++i)
missing[i] = bgen_genotype_missing(genotype, i);
}
4 changes: 4 additions & 0 deletions bgen_reader/genotype.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
static void read_ploidy(struct bgen_genotype const* genotype, uint8_t* ploidy,
uint32_t nsamples);
static void read_missing(struct bgen_genotype const* genotype, bool* missing,
uint32_t nsamples);
7 changes: 7 additions & 0 deletions build_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
with open(join(folder, "bgen_reader", "interface.h"), "r") as f:
ffibuilder.cdef(f.read())

with open(join(folder, "bgen_reader", "genotype.h"), "r") as f:
ffibuilder.cdef(f.read())

with open(join(folder, "bgen_reader", "genotype.c"), "r") as f:
genotype_c = f.read()

with open(join(folder, "bgen_reader", "partition.h"), "r") as f:
ffibuilder.cdef(f.read())

Expand All @@ -38,6 +44,7 @@
"bgen_reader._ffi",
fr"""
#include "bgen/bgen.h"
{genotype_c}
{partition_c}
{samples_c}
""",
Expand Down
1 change: 1 addition & 0 deletions ci/linux-deps
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
yum install -y libffi libffi-devel zlib-devel
yum remove -y cmake

python -m pip install pip --upgrade
python -m pip install cmake numpy pandas

curl https://raw.githubusercontent.com/horta/zstd.install/master/install --output install-zstd
Expand Down
1 change: 1 addition & 0 deletions ci/test
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/bash

ldd $(find / -type f -name "_ffi.abi3.so")
(cd ~/ && python -c "import bgen_reader; import sys; sys.exit(bgen_reader.test())")
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["setuptools >= 49.3.0", "wheel >= 0.34.2"]
requires = ["setuptools>=49.3.0", "wheel>=0.34.2", "cffi>=1.14.1"]
build-backend = "setuptools.build_meta"

[tool.isort]
Expand Down
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ zip_safe = False
include_package_data = True
packages = find:
setup_requires =
cffi>=1.14.0
cffi>=1.14.1
pytest-runner>=5.2
install_requires =
appdirs>=1.4.3
cachetools>=3.1.1
cffi>=1.14.0
cffi>=1.14.1
dask[bag,array,dataframe,delayed]>=2.12.0
numpy>=1.17.4
pandas>=0.24.0
Expand Down

0 comments on commit eceda00

Please sign in to comment.