Skip to content

Instantly share code, notes, and snippets.

@madscientist01
Created December 29, 2012 17:00
Show Gist options
  • Save madscientist01/4408032 to your computer and use it in GitHub Desktop.
Save madscientist01/4408032 to your computer and use it in GitHub Desktop.
Convert your 'lover letter' in DNA sequence!
#!/usr/bin/python
import sys,os,re
def translate(datafile):
#
# Regex strips all of space and non alphabetic from the sentence
#
regex = re.compile('[\s|\W|0-9_]')
#
# Dictionary for the Amido acid - Codon relationship
#
codon = {
'A':'GCT', # Alanine
'B':'GTG', # Let's use one of 'V' codon as 'B'.
'C':'TGT', # Cysteine
'D':'GAT', # Aspartate
'E':'GAA', # Glutamate
'F':'TTT', # Phenylanaline
'G':'GGT', # Glycine
'H':'CAT', # Histidine
'I':'ATT', # Isoleucine
'J':'AGA', # In this time, let's steal one of Argninine Codon.
'K':'AAA', # Lysine
'L':'TTA', # Leucine
'M':'ATG', # Methionine
'N':'AAT', # Asparagine
'O':'TAG', # Pyrrolysine..Do you know new 22th amino acid is out there? It is called 'Pyrrolysine'
'P':'CCT', # Proline
'Q':'CAA', # Glutamine
'R':'CGT', # Arginine
'S':'TCT', # Serine
'T':'ACT', # Threonine
'U':'TGA', # Selenocysteine..This is 21th amino acid
'V':'GTT', # Valine
'W':'TGG', # Tryptopin
'X':'AGT', # 'X' amino acid would be so weird..Anyway, steal again from serine.
'Y':'TAT', # Tyrosine
'Z':'GGG', # 'Z'...Let's steal one of Glycine Codon 'GGG'
'*':'TAA' # This is the 'Stop' Codon.
# There are other two stop codon (TAG and TGA),
# but they are already used for two rare amino acids.
}
try:
f=open(datafile)
filecontent=f.readlines()
oneline=""
for line in filecontent:
oneline = oneline+regex.sub('',line)
aminoacid='M'+oneline.upper()+'*'
#
# Add 'Start Codon' and 'Stop Codon'. Yes..I would like to make a 'functional' protein..LOL
#
nucleotide =""
for ch in aminoacid:
nucleotide = nucleotide+codon[ch];
print ">"+oneline
print nucleotide
except IOError as e:
print 'f**k'
if __name__ =='__main__':
for arg in sys.argv[1:]:
if os.path.isfile(arg):
translate(arg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment