Most of my education in bioinformatics used the R coding language. In my free time I have done some coding exercises to familiarize myself with python as well. Below is one of the scripts I have written as a coding exercise. It takes a users RNA string as input and translates it into the corresponding amino acid string. Iām well aware many translation tools already exist but I thought it would be a good exercise to practice writing loops.
#RNA to protein translation script
# Ask user to input RNA string
rna_string = input("Please input RNA sequence: ")
# remove newlines and whitespaces + determine length of string
rna_string_stripped = rna_string.rstrip()
length_string = len(rna_string_stripped)
# generate position of the first nucleotide in each codon.
numberlist = list(range(0,length_string,3))
# create empty lists that will later be used to hold codons and amino strings.
amino_string = []
codon_list = []
# break up RNA string into codons and add codons to codon list.
for number in numberlist:
codon_list.append(rna_string_stripped[number] + rna_string_stripped[number+1] + rna_string_stripped[number+2])
# create a codon dictionary which can be used to look up the corresponding amino acid.
codondict ={
'UUU': 'F',
'UUC': 'F',
'UUA': 'L',
'UUG': 'L',
'CUU': 'L',
'CUC': 'L',
'CUG': 'L',
'CUA': 'L',
'AUU': 'I',
'AUC': 'I',
'AUA': 'I',
'AUG': 'M',
'GUU': 'V',
'GUC': 'V',
'GUA': 'V',
'GUG': 'V',
'UCU': 'S',
'UCC': 'S',
'UCA': 'S',
'UCG': 'S',
'CCU': 'P',
'CCC': 'P',
'CCA': 'P',
'CCG': 'P',
'ACU': 'T',
'ACC': 'T',
'ACA': 'T',
'ACG': 'T',
'GCU': 'A',
'GCC': 'A',
'GCA': 'A',
'GCG': 'A',
'UAU': 'Y',
'UAC': 'Y',
'UAA': 'Stop',
'UAG': 'Stop',
'CAU': 'H',
'CAC': 'H',
'CAA': 'Q',
'CAG': 'Q',
'AAU': 'N',
'AAC': 'N',
'AAA': 'K',
'AAG': 'K',
'GAU': 'D',
'GAC': 'D',
'GAA': 'E',
'GAG': 'E',
'UGU': 'C',
'UGC': 'C',
'UGA': 'Stop',
'UGG': 'W',
'CGU': 'R',
'CGC': 'R',
'CGA': 'R',
'CGG': 'R',
'AGU': 'S',
'AGC': 'S',
'AGA': 'R',
'AGG': 'R',
'GGU': 'G',
'GGC': 'G',
'GGA': 'G',
'GGG': 'G',
}
# Use codon dictionary to translate codons to amino acids.
for codon in codon_list:
amino_string.append(codondict[codon])
#join amino acids into long long string
output = ''.join(amino_string)
print(output)