Modify the INFO field in a VCF
mainTo add new information to the INFO field of a VCF, you must first update the VCF header using add_info_to_header. The header update requires a dictionary containing the keys 'ID', 'Description', 'Type', and 'Number'. After updating the header, use a Writer initialized with the original VCF as a template to write the modified records to a new file.
from cyvcf2 import VCF, Writer
vcf = VCF(VCF_PATH)
# adjust the header to contain the new field
# the keys 'ID', 'Description', 'Type', and 'Number' are required.
vcf.add_info_to_header({'ID': 'gene', 'Description': 'overlapping gene',
'Type':'Character', 'Number': '1'})
# create a new vcf Writer using the input vcf as a template.
fname = "out.vcf"
w = Writer(fname, vcf)
for v in vcf:
# Perform user-defined logic to find new info
genes = get_gene_intersections(v)
if genes is not None:
v.INFO["gene"] = ",".join(genes)
w.write_record(v)
w.close(); vcf.close()