Package: cyvcf2
cyvcf2Note: cyvcf2 versions < 0.20.0 require htslib < 1.10. cyvcf2 versions >= 0.20.0 require htslib >= 1.10The latest documentation for cyvcf2 can be found here:If you use cyvcf2, please cite thepaperFast python(2 and 3)parsing of VCF and BCF including region-queries.cyvcf2 is a cython wrapper aroundhtslibbuilt for fast parsing ofVariant Call Format(VCF) files.Attributes likevariant.gt_ref_depthswork for diploid samples and return a numpy array directly so they are immediately ready for downstream use.notethat the array is backed by the underlying C data, so, oncevariantgoes out of scope. The array will contain nonsense.
To persist a copy, use:cpy = np.array(variant.gt_ref_depths)instead of justarr = variant.gt_ref_depths.ExampleThe example below shows much of the use of cyvcf2.fromcyvcf2importVCFforvariantinVCF('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_ALTvariant.gt_types,variant.gt_ref_depths,variant.gt_alt_depths# numpy arraysvariant.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')# intvariant.INFO.get('FS')# floatvariant.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')assertsb.shape==(n_samples,4)# 4-values per# to do a region-query:vcf=VCF('some.vcf.gz')forvinvcf('11:435345-556565'):ifv.INFO["AF"]>0.1:continueprint(str(v))Installationpip with bundled htslibpip install cyvcf2pip with system htslibAssuming you have already built and installed htslib version 1.12 or higher.CYVCF2_HTSLIB_MODE=EXTERNAL pip install --no-binary cyvcf2 cyvcf2windows (experimental, only test on MSYS2)Assuming you have already built and installed htslib.SETUPTOOLS_USE_DISTUTILS=stdlib pip install cyvcf2github (building htslib and cyvcf2 from source)git clone --recursive https://github.com/brentp/cyvcf2
pip install -r requirements.txt
# sometimes it can be required to remove old files:
# python setup.py clean_ext
CYVCF2_HTSLIB_MODE=BUILTIN CYTHONIZE=1 python setup.py install
# or to use a system htslib.so
CYVCF2_HTSLIB_MODE=EXTERNAL python setup.py installOnOSX, 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"TestingInstallpytest, then tests can be run with:pytestCLIRun withcyvcf2 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 AlsoPysam alsohas a cython wrapper to htsliband 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.PerformanceFor the performance comparison in the paper, we usedthousand genomes chromosome 22With the full comparison runnerhere.