Skip to content

Instantly share code, notes, and snippets.

@stephenturner
Created June 23, 2022 11:33
Show Gist options
  • Save stephenturner/9f80b8e07bd160b237b51fcbdd9c06a1 to your computer and use it in GitHub Desktop.
Save stephenturner/9f80b8e07bd160b237b51fcbdd9c06a1 to your computer and use it in GitHub Desktop.
Adds AD=2 to VCF on a stream
#!/usr/bin/env python
# Adds allelic depth (AD)=2 to a VCF.
# Usage: bcftools view my.vcf.gz | addad.py | bcftools sort -Oz -o my.ad.vcf.gz && tabix my.ad.vcf.gz
import pysam
vcf_in=pysam.VariantFile("-","r")
vcf_in.header.formats.add("AD",".","Integer","Allelic depth, hard-coded as 2")
print(vcf_in.header, end='')
for variant in vcf_in:
for sample in variant.samples:
variant.samples[sample]['AD']=2
print(variant, end='')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment