Skip to main content

fast vcf parsing with cython + htslib

Project description

cyvcf2

Note: cyvcf2 versions < 0.20.0 require htslib < 1.10. cyvcf2 versions >= 0.20.0 require htslib >= 1.10

The latest documentation for cyvcf2 can be found here:

Docs

If you use cyvcf2, please cite the paper

Fast python (2 and 3) parsing of VCF and BCF including region-queries.

Build Status

cyvcf2 is a cython wrapper around htslib built for fast parsing of Variant Call Format (VCF) files.

Attributes like variant.gt_ref_depths return a numpy array directly so they are immediately ready for downstream use. note that the array is backed by the underlying C data, so, once variant goes out of scope. The array will contain nonsense. To persist a copy, use: cpy = np.array(variant.gt_ref_depths) instead of just arr = variant.gt_ref_depths.

Example

The example below shows much of the use of cyvcf2.

from cyvcf2 import VCF

for variant in VCF('some.vcf.gz'): # or VCF('some.bcf')
	variant.REF, variant.ALT # e.g. REF='A', ALT=['C', 'T']


	variant.CHROM, variant.start, variant.end, variant.ID, \
				variant.FILTER, variant.QUAL

	# numpy arrays of specific things we pull from the sample fields.
	# gt_types is array of 0,1,2,3==HOM_REF, HET, UNKNOWN, HOM_ALT
	variant.gt_types, variant.gt_ref_depths, variant.gt_alt_depths # numpy arrays
	variant.gt_phases, variant.gt_quals, variant.gt_bases # numpy array


	## INFO Field.
	## extract from the info field by it's name:
	variant.INFO.get('DP') # int
	variant.INFO.get('FS') # float
	variant.INFO.get('AC') # float

	# convert back to a string.
	str(variant)


	## sample info...

	# Get a numpy array of the depth per sample:
    dp = variant.format('DP')
    # or of any other format field:
    sb = variant.format('SB')
    assert sb.shape == (n_samples, 4) # 4-values per

# to do a region-query:

vcf = VCF('some.vcf.gz')
for v in vcf('11:435345-556565'):
    if v.INFO["AF"] > 0.1: continue
    print(str(v))

Installation

pip (assuming you have htslib < 1.10 installed)

pip install cyvcf2

github (building htslib and cyvcf2 from source)

git clone --recursive https://github.com/brentp/cyvcf2
cd cyvcf2/htslib
autoheader
autoconf
./configure --enable-libcurl
make

cd ..
pip install -r requirements.txt
CYTHONIZE=1 pip install -e .

On OSX, using brew, you may have to set the following as indicated by the brew install:

For compilers to find openssl you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl/include"

For pkg-config to find openssl you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/openssl/lib/pkgconfig"

Testing

Tests can be run with:

python setup.py test

CLI

Run with cyvcf2 path_to_vcf

$ cyvcf2 --help
Usage: cyvcf2 [OPTIONS] <vcf_file> or -

  fast vcf parsing with cython + htslib

Options:
  -c, --chrom TEXT                Specify what chromosome to include.
  -s, --start INTEGER             Specify the start of region.
  -e, --end INTEGER               Specify the end of the region.
  --include TEXT                  Specify what info field to include.
  --exclude TEXT                  Specify what info field to exclude.
  --loglevel [DEBUG|INFO|WARNING|ERROR|CRITICAL]
                                  Set the level of log output.  [default:
                                  INFO]
  --silent                        Skip printing of vcf.
  --help                          Show this message and exit.

See Also

Pysam also has a cython wrapper to htslib and one block of code here is taken directly from that library. But, the optimizations that we want for gemini are very specific so we have chosen to create a separate project.

Performance

For the performance comparison in the paper, we used thousand genomes chromosome 22 With the full comparison runner here.

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

cyvcf2-0.30.11.tar.gz (1.2 MB view details)

Uploaded Source

Built Distributions

cyvcf2-0.30.11-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (9.9 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

cyvcf2-0.30.11-cp39-cp39-macosx_10_9_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

cyvcf2-0.30.11-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (10.0 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

cyvcf2-0.30.11-cp38-cp38-macosx_10_9_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

cyvcf2-0.30.11-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (9.7 MB view details)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

cyvcf2-0.30.11-cp37-cp37m-macosx_10_9_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.7m macOS 10.9+ x86-64

cyvcf2-0.30.11-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (9.7 MB view details)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64

cyvcf2-0.30.11-cp36-cp36m-macosx_10_9_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.6m macOS 10.9+ x86-64

File details

Details for the file cyvcf2-0.30.11.tar.gz.

File metadata

  • Download URL: cyvcf2-0.30.11.tar.gz
  • Upload date:
  • Size: 1.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for cyvcf2-0.30.11.tar.gz
Algorithm Hash digest
SHA256 bf544327b9c9be2491db0af887a98008cdae4d63ee72409960b9d21e572303a7
MD5 970e06082531c444cbcab81c259158b7
BLAKE2b-256 44b4fd8f8ad14bbff3ac504931212fc44601f15625b12fb0021e917a0a7e6362

See more details on using hashes here.

File details

Details for the file cyvcf2-0.30.11-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cyvcf2-0.30.11-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b9ef629ae71eb9449b8cbb7b0d4646204e110cc43316def70010e96595741236
MD5 2f9695af07ba1a39b5ed2bda180a617d
BLAKE2b-256 3860d7a1a1178bb002a42f948220e82600d525f4ba7d774384f28441c1efaf8f

See more details on using hashes here.

File details

Details for the file cyvcf2-0.30.11-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: cyvcf2-0.30.11-cp39-cp39-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 3.0 MB
  • Tags: CPython 3.9, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for cyvcf2-0.30.11-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 49a782ec213e5f46abcc2b0df56d750ed12e9b9ca5d88a1bd1032e861d965630
MD5 9b4f94c3fc5638fcb4642e6a1bb30dcf
BLAKE2b-256 15944da4e18df25c8628389cf8a2af544163b0925260ff79756f3d070e80feab

See more details on using hashes here.

File details

Details for the file cyvcf2-0.30.11-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cyvcf2-0.30.11-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f6677c6dbfce4fca75b26dbc5cdf170d515163dfe53a42bd2541ddbb7dd20c55
MD5 9279c378120251f42e38c8d993423692
BLAKE2b-256 de7bb736314860a4d1c1175f970ab356c623c63da8650465f47c420588de540a

See more details on using hashes here.

File details

Details for the file cyvcf2-0.30.11-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: cyvcf2-0.30.11-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 3.0 MB
  • Tags: CPython 3.8, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for cyvcf2-0.30.11-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 101761c4badac15d5580cf76a469fc4ad3e705382c51f4dce983a1801734661a
MD5 f3638d7b578d2b1277299c822bffc43c
BLAKE2b-256 52a4a26060bc39e215e9c2f7ad9df03755f7d45fbd379b317d09cd3ce61156a1

See more details on using hashes here.

File details

Details for the file cyvcf2-0.30.11-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cyvcf2-0.30.11-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 49539ecf161dfe93ec4bb71797e11adca86133171059e3965dfd83ee6ecc7c62
MD5 4ddcad763ec67a45a5353587232aa818
BLAKE2b-256 8819bfd9b64bcf67cb36ad2331f2d024d0a3c6f618dc3dbbac50c06cbc6cb95a

See more details on using hashes here.

File details

Details for the file cyvcf2-0.30.11-cp37-cp37m-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: cyvcf2-0.30.11-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 3.0 MB
  • Tags: CPython 3.7m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for cyvcf2-0.30.11-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 2d42ca4cd9596aa69d59336cd58fad37d80d88703130197946b257191726b0de
MD5 8f2cb6f3698c355b162463ba81e3c23f
BLAKE2b-256 9aa0f6fbc8efd60becda21a8b6cb0c1279ec71e6c0646d31d2dff33e1e5e2c14

See more details on using hashes here.

File details

Details for the file cyvcf2-0.30.11-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cyvcf2-0.30.11-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b2166e2c95a51ce366a38dbb84306a8316acc717ee29a57e9e7c500b89aef8da
MD5 e2ec57cb65187ce0c44894eafa3ebefa
BLAKE2b-256 468cb96a1fa60677236356ef70c4030db0dcf5a85f8a19c7b514d93811e7f9a2

See more details on using hashes here.

File details

Details for the file cyvcf2-0.30.11-cp36-cp36m-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: cyvcf2-0.30.11-cp36-cp36m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 3.0 MB
  • Tags: CPython 3.6m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for cyvcf2-0.30.11-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 96c39f6ebdbfcb841b0cdad14665f1ec488ddfbd96cb30d4c026098657b579bb
MD5 23936ac095d5b04fe4a7018ee0c26520
BLAKE2b-256 aa53fb1df0e0e87598e2512ac99438a99beeff7573adbcf7ad9e0966f313432f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page