0% found this document useful (0 votes)
4 views5 pages

DNA RNA Protein

The document provides a detailed explanation of the processes of transcription and translation in molecular biology, including code snippets for transcribing DNA to mRNA and translating mRNA to protein. It also includes functions to identify start and stop codons, extract codons from mRNA sequences, and handle errors for invalid DNA inputs. Examples of DNA sequences are processed to demonstrate these concepts, showing the resulting mRNA and protein sequences.

Uploaded by

Muhammad Iqbal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

DNA RNA Protein

The document provides a detailed explanation of the processes of transcription and translation in molecular biology, including code snippets for transcribing DNA to mRNA and translating mRNA to protein. It also includes functions to identify start and stop codons, extract codons from mRNA sequences, and handle errors for invalid DNA inputs. Examples of DNA sequences are processed to demonstrate these concepts, showing the resulting mRNA and protein sequences.

Uploaded by

Muhammad Iqbal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

2/21/25, 10:58 PM transcription_and_translation

DNA → RNA → Protein: Understanding


Transcription & Translation

Transcribing DNA to mRNA


In [3]: import warnings
warnings.filterwarnings("ignore")

from Bio.Seq import Seq

dna_seq = Seq("ATGCGTACGTTAG") #DNA Sequence

# Transcribe to mRNA
mRNA = dna_seq.transcribe()
print(f"mRNA Sequence: {mRNA}")

mRNA Sequence: AUGCGUACGUUAG

Translating mRNA to Protein


In [4]: protein = mRNA.translate()
print(f"Protien Sequnce: {protein}")

Protien Sequnce: MRTL

Full Pipeline: DNA → mRNA → Protein


In [5]: def is_valid_dna(sequence):
valid_bases = set("ATGC")
return all(base in valid_bases for base in sequence.upper())

In [6]: dna_input = input("Enter a DNA sequence (only A, T, G, C): ").strip().upper()

if not is_valid_dna(dna_input):
print("Error: Invalid DNA sequence. Only A, T, G, and C are allowed.")
else:
dna_seq = Seq(dna_input)

# Transcription
mRNA = dna_seq.transcribe()
print(f"mRNA Sequence: {mRNA}")

# Translation
protein = mRNA.translate(to_stop=True)
print(f"Protein Sequence: {protein}")

mRNA Sequence: AAUGC


Protein Sequence: N

file:///F:/MS CS/2nd Semester/Bioinformatics/Assignments/Assignment 1/transcription_and_translation.html 1/5


2/21/25, 10:58 PM transcription_and_translation

Finding Start and Stop Codons in DNA Sequences


In [7]: # Function to find start and stop codons, and translate sequence
def translate_dna(seq_str):
dna_seq = Seq(seq_str)
mRNA = dna_seq.transcribe()
protein_seq = mRNA.translate(to_stop=True) # Translates until the first stop cod
start_codon = seq_str[:3]
stop_codon = None

# Find the stop codon


for i in range(0, len(seq_str) - 2, 3):
codon = seq_str[i:i+3]
if codon in ["TAA", "TAG", "TGA"]:
stop_codon = codon
break

# Check if stop codon exists at the end


if not stop_codon and seq_str[-3:] in ["TAA", "TAG", "TGA"]:
stop_codon = seq_str[-3:]

return start_codon, stop_codon, str(protein_seq)

# List of DNA sequences


dna_sequences = [
"ATGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG",
"ATGTTTCCCGGGAATCTAA",
"ATGGGTTGAGTAA",
"ATGAGGGTCCCTGA"
]

for seq in dna_sequences:


start, stop, protein = translate_dna(seq)
print(f"DNA Sequence: {seq}")
print(f"Start Codon: {start}")
print(f"Stop Codon: {stop if stop else 'No stop codon found'}")
print(f"Protein Sequence: {protein}")
print("-" * 50)

file:///F:/MS CS/2nd Semester/Bioinformatics/Assignments/Assignment 1/transcription_and_translation.html 2/5


2/21/25, 10:58 PM transcription_and_translation

DNA Sequence: ATGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG


Start Codon: ATG
Stop Codon: TGA
Protein Sequence: MAIVMGR
--------------------------------------------------
DNA Sequence: ATGTTTCCCGGGAATCTAA
Start Codon: ATG
Stop Codon: TAA
Protein Sequence: MFPGNL
--------------------------------------------------
DNA Sequence: ATGGGTTGAGTAA
Start Codon: ATG
Stop Codon: TGA
Protein Sequence: MG
--------------------------------------------------
DNA Sequence: ATGAGGGTCCCTGA
Start Codon: ATG
Stop Codon: TGA
Protein Sequence: MRVP
--------------------------------------------------

Find the Start Codon


In [ ]: for seq in dna_sequences:
if seq[0:3] == "ATG":
mRNA = Seq(seq).transcribe()
protein = mRNA.translate(to_stop=True)
print(f"mRNA Sequence: {mRNA}")
print(f"Protein Sequence: {protein}")
print("-" * 50)
else:
print("No Start Codon Found")
print("-" * 50)

mRNA Sequence: AUGGCCAUUGUAAUGGGCCGCUGAAAGGGUGCCCGAUAG


Protein Sequence: MAIVMGR
--------------------------------------------------
No Start Codon Found
--------------------------------------------------
mRNA Sequence: AUGGGUUGAGUAA
Protein Sequence: MG
--------------------------------------------------
No Start Codon Found
--------------------------------------------------

Extract All Codons


In [ ]: def extract_codons(mrna_sequence):
# Check if sequence length is a multiple of 3
if len(mrna_sequence) % 3 != 0:
print("Warning: Sequence length is not a multiple of 3. Extra nucleotides will

print("Codons:")
for i in range(0, len(mrna_sequence) - 2, 3):

file:///F:/MS CS/2nd Semester/Bioinformatics/Assignments/Assignment 1/transcription_and_translation.html 3/5


2/21/25, 10:58 PM transcription_and_translation

codon = mrna_sequence[i:i+3]
print(codon)

for seq in dna_sequences:


mRNA = Seq(seq).transcribe()
extract_codons(mRNA)
print("-" * 50)

Codons:
AUG
GCC
AUU
GUA
AUG
GGC
CGC
UGA
AAG
GGU
GCC
CGA
UAG
--------------------------------------------------
Codons:
UGU
UUC
CCG
GGA
AUC
UAA
--------------------------------------------------
Warning: Sequence length is not a multiple of 3. Extra nucleotides will be ignored.
Codons:
AUG
GGU
UGA
GUA
--------------------------------------------------
Warning: Sequence length is not a multiple of 3. Extra nucleotides will be ignored.
Codons:
UGA
GGG
UCC
CUG
--------------------------------------------------

Stop translation if a stop codon is found


In [8]: def translate_mrna_until_stop(mrna_sequence):
# List of stop codons
stop_codons = ["UAA", "UAG", "UGA"]

print("Codons:")
for i in range(0, len(mrna_sequence) - 2, 3):
codon = mrna_sequence[i:i+3]

file:///F:/MS CS/2nd Semester/Bioinformatics/Assignments/Assignment 1/transcription_and_translation.html 4/5


2/21/25, 10:58 PM transcription_and_translation

print(codon)

if codon in stop_codons:
print("Stop codon encountered. Translation terminated.")
break

for seq in dna_sequences:


mRNA = Seq(seq).transcribe()
translate_mrna_until_stop(mRNA)
print("-" * 50)

Codons:
AUG
GCC
AUU
GUA
AUG
GGC
CGC
UGA
Stop codon encountered. Translation terminated.
--------------------------------------------------
Codons:
AUG
UUU
CCC
GGG
AAU
CUA
--------------------------------------------------
Codons:
AUG
GGU
UGA
Stop codon encountered. Translation terminated.
--------------------------------------------------
Codons:
AUG
AGG
GUC
CCU
--------------------------------------------------

file:///F:/MS CS/2nd Semester/Bioinformatics/Assignments/Assignment 1/transcription_and_translation.html 5/5

You might also like