Skip to content

Instantly share code, notes, and snippets.

@jodyphelan
Created February 12, 2021 09:23
Show Gist options
  • Save jodyphelan/db0e24a17a4b744e76acb688a9c0b99b to your computer and use it in GitHub Desktop.
Save jodyphelan/db0e24a17a4b744e76acb688a9c0b99b to your computer and use it in GitHub Desktop.
import sys
import argparse
import re
import subprocess as sp
def main(args):
IN = sys.stdin if args.vcf=="-" else sp.Popen("bcftools view %(vcf)s" % vars(args),shell=True,stdout=sp.PIPE).stdout
for l in IN:
try:
l = l.decode()
except:
pass
row = l.strip().split()
if l[0]=="#":
sys.stdout.write(l)
continue
gt_index = row[8].split(":").index("GT")
for i in range(9,len(row)):
tmp = row[i].split(":")
gt = "%s/%s" % (tmp[gt_index],tmp[gt_index])
tmp[gt_index] = gt
row[i] = ":".join(tmp)
sys.stdout.write("\t".join(row)+"\n")
parser = argparse.ArgumentParser(description='Changed haploid to diploid calls',formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('vcf',help='VCF file (use \'-\' if piping from stdin)')
parser.set_defaults(func=main)
args = parser.parse_args()
args.func(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment