genetic_code = {
# this was adapted from
# https://gitlab.com/RebelCoder/dna-toolset/-/blob/90e8d9249c6dcc82a850aa854da4baabef98ee6f/structures.py
"GCT": "A", "GCC": "A", "GCA": "A", "GCG": "A",
"TGT": "C", "TGC": "C",
"GAT": "D", "GAC": "D",
"GAA": "E", "GAG": "E",
"TTT": "F", "TTC": "F",
"GGT": "G", "GGC": "G", "GGA": "G", "GGG": "G",
"CAT": "H", "CAC": "H",
"ATA": "I", "ATT": "I", "ATC": "I",
"AAA": "K", "AAG": "K",
"TTA": "L", "TTG": "L", "CTT": "L", "CTC": "L", "CTA": "L", "CTG": "L",
"ATG": "M",
"AAT": "N", "AAC": "N",
"CCT": "P", "CCC": "P", "CCA": "P", "CCG": "P",
"CAA": "Q", "CAG": "Q",
"CGT": "R", "CGC": "R", "CGA": "R", "CGG": "R", "AGA": "R", "AGG": "R",
"TCT": "S", "TCC": "S", "TCA": "S", "TCG": "S", "AGT": "S", "AGC": "S",
"ACT": "T", "ACC": "T", "ACA": "T", "ACG": "T",
"GTT": "V", "GTC": "V", "GTA": "V", "GTG": "V",
"TGG": "W",
"TAT": "Y", "TAC": "Y",
"TAA": "*", "TAG": "*", "TGA": "*"
}
'TTTTATGATGT'.index('ATG')
4
list(range(4, 25, 3))
[4, 7, 10, 13, 16, 19, 22]
test_sequence = 'TTTTATGACCT'
print(test_sequence[10:10+3])
T
genetic_code['AGA']
'R'
'AAA' not in 'ACGAAAT'
False
def translate(sequence, genetic_code):
protein = []
if 'ATG' not in sequence:
return protein
first_codon_position = sequence.index('ATG')
sequence_length = len(sequence)
for codon_start in range(first_codon_position, sequence_length, 3):
codon = sequence[codon_start:codon_start+3]
if len(codon) == 3:
try:
amino_acid = genetic_code[codon]
except KeyError:
raise KeyError(codon + " is not present in the genetic code.")
if amino_acid == '*':
return protein
protein.append(amino_acid)
return protein
translate("AGGTACGTGGAACGTACGTGACCGATGGACCACACUCATTGAGTGTGTACACACACGTGTGTGTGACACAACAAC", genetic_code)
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-22-02b19746c2c2> in translate(sequence, genetic_code) 14 try: ---> 15 amino_acid = genetic_code[codon] 16 except KeyError: KeyError: 'ACU' During handling of the above exception, another exception occurred: KeyError Traceback (most recent call last) <ipython-input-23-a63d615a2bdf> in <module> ----> 1 translate("AGGTACGTGGAACGTACGTGACCGATGGACCACACUCATTGAGTGTGTACACACACGTGTGTGTGACACAACAAC", genetic_code) <ipython-input-22-02b19746c2c2> in translate(sequence, genetic_code) 15 amino_acid = genetic_code[codon] 16 except KeyError: ---> 17 raise KeyError(codon + " is not present in the genetic code.") 18 if amino_acid == '*': 19 return protein KeyError: 'ACU is not present in the genetic code.'