Skip to content

Instantly share code, notes, and snippets.

@dinovski
Last active September 7, 2018 20:03
Show Gist options
  • Save dinovski/684f012fbb21a552d9d568a4c1d04637 to your computer and use it in GitHub Desktop.
Save dinovski/684f012fbb21a552d9d568a4c1d04637 to your computer and use it in GitHub Desktop.
change IUPAC nucleotide code to nucleotide
#!/usr/bin/env python
usage="""
iupac_change.py <infile> <outfile>
change IUPAC nucleotide codes for tab delimited file with CHR, ID, REF, ALT columns
"""
import sys
import os
if len(sys.argv) < 3:
print usage
vcf_file = sys.argv[1]
f = open(vcf_file, "r")
outfile = open(sys.argv[2], "w")
for line in f.readlines():
delim = line.split('\t')
alt = delim[4]
ref = delim[3]
if alt == "A" or alt == "T" or alt =="C" or alt == "G":
alt = alt
elif alt == "K" and ref == "G":
alt = "T"
elif alt =="K" and ref == "T":
alt = "G"
elif alt == "M" and ref == "A":
alt = "C"
elif alt == "M" and ref == "C":
alt = "A"
elif alt == "R" and ref == "A":
alt = "G"
elif alt == "R" and ref == "G":
alt = "A"
elif alt == "S" and ref == "G":
alt = "C"
elif alt == "S" and ref == "C":
alt = "G"
elif alt == "W" and ref == "A":
alt = "T"
elif alt == "W" and ref == "T":
alt = "A"
elif alt == "Y" and ref == "C":
alt = "T"
elif alt == "Y" and ref == "T":
alt = "C"
delim[4] = alt
#convert delim list to string
line = "\t".join(delim)
outfile.write(line)
f.close()
outfile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment