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

Install pytest, then tests can be run with:

pytest

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.16.tar.gz (1.2 MB view details)

Uploaded Source

Built Distributions

cyvcf2-0.30.16-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (6.7 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

cyvcf2-0.30.16-cp310-cp310-macosx_11_0_arm64.whl (1.4 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

cyvcf2-0.30.16-cp310-cp310-macosx_10_9_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

cyvcf2-0.30.16-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (6.8 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

cyvcf2-0.30.16-cp39-cp39-macosx_11_0_arm64.whl (1.4 MB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

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

Uploaded CPython 3.9macOS 10.9+ x86-64

cyvcf2-0.30.16-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (6.8 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

cyvcf2-0.30.16-cp38-cp38-macosx_11_0_arm64.whl (1.4 MB view details)

Uploaded CPython 3.8macOS 11.0+ ARM64

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

Uploaded CPython 3.8macOS 10.9+ x86-64

cyvcf2-0.30.16-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (6.6 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.17+ x86-64

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

Uploaded CPython 3.7mmacOS 10.9+ x86-64

cyvcf2-0.30.16-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (6.6 MB view details)

Uploaded CPython 3.6mmanylinux: glibc 2.17+ x86-64

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

Uploaded CPython 3.6mmacOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: cyvcf2-0.30.16.tar.gz
  • Upload date:
  • Size: 1.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for cyvcf2-0.30.16.tar.gz
Algorithm Hash digest
SHA256 8d48f0d09d8d400c05ac44cbbe173545eda00f6ee484be2aab966922715d8073
MD5 8a7b07f6068d23d5b1c6f499a80ef4f5
BLAKE2b-256 fbc5ccc4711aeb71cbe7d0d169fe569a03b3481b1e02b45b803a97eddb47ca9d

See more details on using hashes here.

File details

Details for the file cyvcf2-0.30.16-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cyvcf2-0.30.16-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 33911e301268b346baddd0668547788b8458e2ef4dc163e822cbf181ab6a4784
MD5 7601f51c01179745bfb51c7461aeb354
BLAKE2b-256 85413e1f8f473d5792f07cf9daab48f395cae6361b7f27e6dbd4acc925da4147

See more details on using hashes here.

File details

Details for the file cyvcf2-0.30.16-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for cyvcf2-0.30.16-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5144d56fde7452a19fd042033f17d4b65b1dc9ebc0e0971d0dfa3bc2f74516da
MD5 07f8fdc6831c537e1719cf333ee47cd2
BLAKE2b-256 04c6cf0420506346da3698edc9da17383fd9241c5dc6109e8cf574b5aa74bc9f

See more details on using hashes here.

File details

Details for the file cyvcf2-0.30.16-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for cyvcf2-0.30.16-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 a091d1c4f10767ac12366b4e16c1ec07951b1714a6454dce4240c3f1000605bb
MD5 c05a6deac26a45757ecef06d5ba4f346
BLAKE2b-256 0bc17115cf73251ce65ca0c2b08ff8578d0de4afd8c1de6f8268290ec7c38e15

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cyvcf2-0.30.16-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ddf1520aafa49d0cc57f233cba110403e1c71b4bc2426515d17a1213ffe1bbc3
MD5 2cf2b17bc2a21077591b71dab8be0ba2
BLAKE2b-256 ebfd95cd3f576bdea9c4adbd204edbd3e44ccf44581694f99fdd6e3a3c54d440

See more details on using hashes here.

File details

Details for the file cyvcf2-0.30.16-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for cyvcf2-0.30.16-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8f30b3169d6f4bde70040d10a0277e07c4d21ed06dfaaf09b047ab561189b45c
MD5 cf6e4fd94da7117dd9f7b1ee64fc3c8c
BLAKE2b-256 429136851c51808ab862e18fd72641af816890206f540d57930c05a4c3e42c15

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cyvcf2-0.30.16-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 a08f82b0181b31c83f505717251ac8637a98cd556fdb57c3667de1aa2180d427
MD5 4913a494d6287e8f84f323c83045c712
BLAKE2b-256 7218c95916002f9e67401b5b96f75d30f7c789aa6655597a3c15d529b644ed3d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cyvcf2-0.30.16-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0e6760580c251f3a4aaed5bc22d025641d2e50922e3c50f9b46fd5c5df122f37
MD5 201723cfca1883082efa99eef6c4aafa
BLAKE2b-256 224256cffb5049a7b425b9ecccf884b47bb6e04df9247c545a53b6739a9904dd

See more details on using hashes here.

File details

Details for the file cyvcf2-0.30.16-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for cyvcf2-0.30.16-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2f0a65664c0579cd212cd9dba872eb672ce2e23c1214f5dd5fa9e6176f7b270b
MD5 288adda1420a886411bbd77cf94cf1a3
BLAKE2b-256 399b4eb08058c363ed032bb140d11112d92afe42e2d474599efa094f97bb1910

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cyvcf2-0.30.16-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 b28c0029c1760b79ed3d0f3757e9454a7a7fb877a294ee32cc7dad3a4e66f4a5
MD5 90a0b516c301e128345c1a2fe4614782
BLAKE2b-256 35ee12277d74feaf4f8286922da6ba3b3305f9d8ea8c611bdbabb2a684c15a58

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cyvcf2-0.30.16-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 06c88221263be536292127bb68a8068e36201176f0ee5dd48796ee954f546c49
MD5 d7de480b6cfc3317ebcc57718416e31b
BLAKE2b-256 1d6fe52e4613c0688b8aa0f55b2f497ec9d6d8063ea3a30fc07be6592ab2eafb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cyvcf2-0.30.16-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e24f6d93e4debc98548435a986136ffed8d28757e94f58277cf146568a9e431e
MD5 c7122c12266eaa8d314d845d43862539
BLAKE2b-256 a931cee08239f85296dd1851a725f96557f5c3542cd3407b430fdd4a30d1493e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cyvcf2-0.30.16-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 02e0a5e7cc4350648a18b65b48a5ef8dda3a2b3ca31e503d95277091a6434e66
MD5 89c53e6d9e4e6647bfa5db9dcd647e16
BLAKE2b-256 0544f3941937d61ad6a8c2f9db1ffca7a7901ffd06360970196e323443c5d76e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cyvcf2-0.30.16-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 2c62a75536a11e52ad3a042ec1954391fa9240b784ca2d1f999bb939db61ae87
MD5 eb186c73f65d4f5dd0290b9a4632729d
BLAKE2b-256 578a1e37df734993277baa187c4d8c300b3960253bc0c09d1ce485f3dee2d017

See more details on using hashes here.

Supported by

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