Package: vcfio
🔭 Overviewvcfio is anefficientandeasy-to-usepackage forVCFreading, writing and manipluation.It is designed for robust variant processing, and requiresminimumcomputing resources.⚡ Fast - iterate >4500 variants per second🪶 Lightweight - automatically parses onlycrucial informationabout variants (CHROM, POS, ID, REF, ALT, QUAL, FILTER), all the other information (INFO and SAMPLES) is parsed on demand🏁 Efficient - No advanced parsing of lines and casting to hugh memory objects🔌 Dependency free - We do have optional dependencies which enhance the package🎯 Why ?The existing python VCF solutions wereextremely inefficient- parsing every variant line in advance and casting
every bit of data into their own objects or a list of strings, which is very memory-consuming.This affected our runtime by a huge factor.We wrote vcfio to overcome those issues, making it a lightweight and dependency-free package.⚙️ ️Installation# Basic installationpipinstallvcfio# Include optional dependenciespipinstallvcfio[bio]⭐ Features:heavy_check_mark:Read and writeplain and compressed vcf (no specification needed) -file.vcf,file.vcf.gz,file.vcf.bgz.:heavy_check_mark:Automatically inferthe type of values in the file. For example'1,2,3'will yield[1, 2, 3].:heavy_check_mark:Parsevalueson demandfor maximum efficiency.:heavy_check_mark:Fetchvariant ranges within a chromosome.❓ How to UseVcfReaderHere are some examples of what you can do with VcfReader
(We recommend you explore all the available methods)Click to view usage!importvcfiowithvcfio.VcfReader('/path/to/file.vcf')asreader:# Iterate variantsforvariantinreader:# type: vcfio.Variantprint(variant.to_vcf_line())# Iterate the variant's samplesforsample_name,sampleinvariant.samples.items():# type: AnyStr, EasyDict# Try to find "AD" in sample, if not found - find in variant's info, if the value is a dot - return Nonead=variant.get_value('AD',sample_name,empty_value='.')zygosity=variant.get_zygosity(sample_name)# Fetch variants within the range chr3:1-1000forvariantinreader.fetch('chr3',start=1,end=1000):# type: vcfio.Variantprint(variant)VcfWriterHere is an example of what you can do with VcfWriterClick to view usage!importvcfio# This will open an existing vcf, introduce a new value to each variant's INFO and write to another vcf.gzwithvcfio.VcfReader('/path/to/output_file.vcf.gz')asreader,\vcfio.VcfWriter('/path/to/output.vcf',headers=reader.headers)aswriter:forvariantinreader:# type: vcfio.Variantvariant.info['new_info_field']='new_info_value'writer.write_variant(variant)VariantHere are some examples of what you can do with Variant
(We recommend you to explore all the available methods)Click to view usage!importvcfiovariant_line="chr1	726	.	G	C,T	500	.	DP=200;MQ=250.00	GT:AD:AF:DP:GQ	0/1:10,160,30:0.8,0.15:200:420"# This will parse the raw variant line into a Variant instancevariant=vcfio.Variant.from_variant_line(variant_line,sample_names=['proband'])print(variant.quality,# --> 726variant.chromosome,# --> "chr1"variant.alt,# --> ["C", "T"]variant.get_zygosity('proband'),# --> "HET"variant.get_value('MQM','proband'),# --> 250variant.samples['proband'].get('GT')# --> "0/1")EasyDictA Dict-inherited class with a smartgetmethod. It's main (and only) feature is to automatically infer the type of the value it returns.
A vcf file rarely specifies the type of values within the variant's data so this object makes a bioinformaticaion's life easier and exempts him from casting-duty.
It is used invcfio.Variant's attributes.d=EasyDict({'simple_int':1,'not_simple_int':'2','this_is_not_a_list':['abc'],'list_of_numbers':['1',2,'3.1'],'this_is_a_real_list':['a','b','c'],'dot_is_not_a_value':'.','this_is_a_list':'1,2,3','this_is_a_list_but_i_like_strings':'1,2,3'})d.get('simple_int')# --> 1d.get('not_simple_int',infer_type=True)# --> 2d.get('this_is_not_a_list',infer_type=True)# --> 'abc'd.get('list_of_numbers',infer_type=True)# --> [1, 2, 3.1]d.get('this_is_a_real_list',infer_type=True)# --> ['a', 'b', 'c']d.get('dot_is_not_a_value',empty_value='.',infer_type=True)# --> Noned.get('this_is_a_list',empty_value='.',infer_type=True)# --> [1, 2, 3]d.get('this_is_a_list_but_i_like_strings',empty_value='.')# --> '1,2,3'CreditsThis package uses the following open source packages:PyVCFContributors