cython+htslib快速vcf解析
cyvcf2的Python项目详细描述
cyvcf2
如果您使用cyvcf2,请引用paper
快速python(2和3)分析vcf和bcf,包括区域查询。
cyvcf2是围绕htslib构建的cython包装器,用于快速解析Variant Call Format(vcf)文件。
像variant.gt_ref_depths
这样的属性直接返回一个numpy数组,以便它们可以立即供下游使用。
注意数组由底层c数据支持,因此,一旦variant
超出范围。数组将包含无意义的内容。
要保存副本,请使用:cpy = np.array(variant.gt_ref_depths)
,而不仅仅是arr = variant.gt_ref_depths
。
示例
下面的例子展示了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))
安装
点
pip install cyvcf2
github(从源代码构建htslib和cyvcf2)
git clone --recursive https://github.com/brentp/cyvcf2
cd cyvcf2/htslib
autoheader
autoconf
./configure --enable-libcurl
make
cd ..
pip install -e .
在osx上,使用brew时,您可能需要按照brew安装说明设置以下各项:
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"
测试
可以使用以下命令运行测试:
python setup.py test
cli
使用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.
另请参见
pysam也是has a cython wrapper to htslib,这里的一段代码直接从那个库中获取。但是,我们希望双子座的优化是非常具体的,所以我们选择创建一个单独的项目。
性能
在本文的性能比较中,我们使用thousand genomes chromosome 22 使用完全比较运行器here。