Skip to content

Instantly share code, notes, and snippets.

@dinovski
Last active September 7, 2018 20:25
Show Gist options
  • Save dinovski/c9cb184be1fb5a48c53f54fcb2df72b9 to your computer and use it in GitHub Desktop.
Save dinovski/c9cb184be1fb5a48c53f54fcb2df72b9 to your computer and use it in GitHub Desktop.
edit genotype in gvcf
#!/usr/bin/env python
## get individual genotype from raw vcf
## output is tab-delimited chr \t start \t end \t ref \t alt \t GT \t ref,alt
usage="""
read in tab-delimited bedfile: chr,start,end,ref,alt,gt
usage: python addallgt.py <infile> <outfile>
"""
import sys
import os
#if os.path.isfile(sys.argv[2]):
#exit('output file already exists')
if len(sys.argv) < 2:
print usage
infile = open(sys.argv[1],'r')
outfile = open(sys.argv[2],'w')
linecount = 0
multalleles=0
for line in infile:
lineitems = line.split()
cleanedLine = line.replace("\n","")
ref = lineitems[3]
alt = lineitems[4]
if not (len(lineitems[3]) == 1):
continue
if lineitems[5]=="0/0":
outfile.write(cleanedLine + "\t" + ref + "," + ref + "\n")
elif lineitems[5]=="0/1":
outfile.write(cleanedLine + "\t" + ref + "," + alt + "\n")
elif lineitems[5]=="1/0":
outfile.write(cleanedLine + "\t" + ref + "," + alt + "\n")
elif lineitems[5]=="1/1":
outfile.write(cleanedLine + "\t" + alt + "," + alt + "\n")
else:
#print "%s warning, invalid item at %s " % (lineitems[5], linecount)
multalleles += 1
linecount += 1
print "%s" % (multalleles)
infile.close()
outfile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment