zmoores1 zmoores1 - 11 months ago 55
Python Question

DNA To Protein Sequence

I am trying to create a program which translates a DNA sequence entered by the user to 3 alternative protein sequence using the following dictionary (codons are keys, amino acids are values):

{'TGA': '*', 'GCG': 'A', 'CGA': 'R', 'ATA': 'I', 'AGA': 'R', 'TAA': '*', 'TTT': 'F', 'GAG': 'E', 'CTT': 'L', 'CGT': 'R', 'CTC': 'L', 'CTG': 'L', 'TGT': 'C', 'CCA': 'P', 'AAT': 'N', 'GTC': 'V', 'GAC': 'D', 'GAT': 'D', 'TAT': 'Y', 'AAA': 'K', 'GTA': 'V', 'TAG': '*', 'CGC': 'R', 'GCA': 'A', 'TCG': 'S', 'GCT': 'A', 'GCC': 'A', 'TGG': 'W', 'TTC': 'F', 'CCC': 'P', 'TTG': 'L', 'CGG': 'R', 'GGC': 'G', 'AGG': 'R', 'TCC': 'S', 'CCT': 'P', 'GGT': 'G', 'GGG': 'G', 'TCA': 'S', 'AGC': 'S', 'CAG': 'Q', 'CAC': 'H', 'ATC': 'I', 'GAA': 'E', 'GTG': 'V', 'CCG': 'P', 'CAT': 'H', 'AAG': 'K', 'ATG': 'M', 'AAC': 'N', 'TAC': 'Y', 'TGC': 'C', 'CTA': 'L', 'TCT': 'S', 'ATT': 'I', 'ACG': 'T', 'AGT': 'S', 'GTT': 'V', 'TTA': 'L', 'CAA': 'Q', 'GGA': 'G', 'ACC': 'T', 'ACA': 'T', 'ACT': 'T'}

I want to just use mapping and not biopython.
So, when the program is run it should look like this:

Please enter a DNA sequence: GCTgttaagactatgaaaagaataagcaacaccatcaat


Frame 2 is LLRL*KE*ATPS


I have created this dictionary from a file, but I'm unsure how to get started from here. Any help would be greatly appreciated. Thanks!

Answer Source

Declare the dictionary:

prot_match = {'TGA': '*', 'GCG': 'A', 'CGA': 'R', 'ATA': 'I', 'AGA': 'R', 'TAA': '*', 'TTT': 'F', 'GAG': 'E', 'CTT': 'L', 'CGT': 'R', 'CTC': 'L', 'CTG': 'L', 'TGT': 'C', 'CCA': 'P', 'AAT': 'N', 'GTC': 'V', 'GAC': 'D', 'GAT': 'D', 'TAT': 'Y', 'AAA': 'K', 'GTA': 'V', 'TAG': '*', 'CGC': 'R', 'GCA': 'A', 'TCG': 'S', 'GCT': 'A', 'GCC': 'A', 'TGG': 'W', 'TTC': 'F', 'CCC': 'P', 'TTG': 'L', 'CGG': 'R', 'GGC': 'G', 'AGG': 'R', 'TCC': 'S', 'CCT': 'P', 'GGT': 'G', 'GGG': 'G', 'TCA': 'S', 'AGC': 'S', 'CAG': 'Q', 'CAC': 'H', 'ATC': 'I', 'GAA': 'E', 'GTG': 'V', 'CCG': 'P', 'CAT': 'H', 'AAG': 'K', 'ATG': 'M', 'AAC': 'N', 'TAC': 'Y', 'TGC': 'C', 'CTA': 'L', 'TCT': 'S', 'ATT': 'I', 'ACG': 'T', 'AGT': 'S', 'GTT': 'V', 'TTA': 'L', 'CAA': 'Q', 'GGA': 'G', 'ACC': 'T', 'ACA': 'T', 'ACT': 'T'}

Slice the string to chunks of 3 characters:

def chunks (arr, size = 1, frame = 1):
    return [arr[i: i+size] for i in range(frame - 1, len(arr), size)]
dna = input('Enter DNA: ').upper()

Then, map for the prot_match value of the current sequence (use get for safety in case you dont have that sequence for sure):

frames = 3
for frame in range(frames):
    chunked_dna = chunks(dna, 3, frame + 1)
    prot_seq = map(lambda seq: prot_match.get(seq, '0'), chunked_dna )

Finally, use join to get the protein sequence as a string from the map:

    prot_seq = ''.join(prot_seq)
    print('Protein sequence number', frame + 1, ':', prot_seq)

Example (these snippets together in a file):

Enter DNA: GCTgttaagactatgaaaagaataagcaacaccatcaat
Protein sequence number 1 : AVKTMKRISNTIN
Protein sequence number 2 : LLRL*KE*ATPS0
Protein sequence number 3 : C*DYEKNKQHHQ0