Reputation: 1
I have this question where we need to write a code that takes a protein fasta file and the protein sequence identifier, and counts all the possible RNA combinations for the sequence in the fasta file, with a condition that the total of combinations should be less than 5000.
I started with making an RNA codons dictionary, then I made a function that puts the elements of the fasta file (amino acids) into a list, then I tried to do combinations from that list, but I get an error and I tried but didn't know where the problem is, if anyone can check the code and tell me what's wrong I would be grateful.
import itertools
from Bio import SeqIO
RNA_codon_table = {
'A': ('GCU', 'GCC', 'GCA', 'GCG'),
'C': ('UGU', 'UGC'),
'D': ('GAU', 'GAC'),
'E': ('GAA', 'GAG'),
'F': ('UUU', 'UUC'),
'G': ('GGU', 'GGC', 'GGA', 'GGG'),
'H': ('CAU', 'CAC'),
'I': ('AUU', 'AUC', 'AUA'),
'K': ('AAA', 'AAG'),
'L': ('UUA', 'UUG', 'CUU', 'CUC', 'CUA', 'CUG'),
'M': ('AUG',),
'N': ('AAU', 'AAC'),
'P': ('CCU', 'CCC', 'CCA', 'CCG'),
'Q': ('CAA', 'CAG'),
'R': ('CGU', 'CGC', 'CGA', 'CGG', 'AGA', 'AGG'),
'S': ('UCU', 'UCC', 'UCA', 'UCG', 'AGU', 'AGC'),
'T': ('ACU', 'ACC', 'ACA', 'ACG'),
'V': ('GUU', 'GUC', 'GUA', 'GUG'),
'W': ('UGG',),
'Y': ('UAU', 'UAC'),}
def protein_fasta (protein_file):
protein_sequence = []
protein = SeqIO.parse(protein_file, format = 'fasta')
for Seqrecord in protein:
protein_sequence.append(Seqrecord.seq)
print (protein_sequence)
for seq in protein_sequence:
codons = [ list(RNA_codon_table[key]) for key in protein_sequence ]
print(list(itertools.product(codons)))
I'm sorry I don't know how to attach a fasta file, but this is the sequence inside:
>seq_compl complete sequence
IEEATHMTPCYELHGLRWVQIQDYAINVMQCL
This is the error I get:
KeyError Traceback (most recent call last)
<ipython-input-65-3dd46947c505> in <module>
----> 1 all_combinations ('short_protein.fasta')
<ipython-input-64-45a50fffc1d9> in all_combinations(protein_file)
5 protein_sequence.append(Seqrecord.seq)
6
----> 7 codons = [ list(RNA_codon_table[key]) for key in protein_sequence
]
8 print(list(itertools.product(codons)))
<ipython-input-64-45a50fffc1d9> in <listcomp>(.0)
5 protein_sequence.append(Seqrecord.seq)
6
----> 7 codons = [ list(RNA_codon_table[key]) for key in protein_sequence
]
8 print(list(itertools.product(codons)))
KeyError: Seq('IEEATHMTPCYELHGLRWVQIQDYAINVMQCL')
Upvotes: 0
Views: 644
Reputation: 42143
Your protein string will produce billions of combinations:
from itertools import product,islice
def protGen(proteins):
for codons in product(*(RNA_codon_table[P] for P in proteins)):
yield "".join(codons)
counting combinations:
proteins = "IEEATHMTPCYELHGLRWVQIQDYAINVMQCL"
count = 1
for P in proteins: count *= len(RNA_codon_table[P])
print(count) # 37,572,373,905,408 combinations
output:
for protSeq in islice(protGen(proteins),500): # first 500
print(protSeq)
AUUGAAGAAGCUACUCAUAUGACUCCUUGUUAUGAAUUACAUGGUUUACGUUGGGUUCAAAUUCAAGAUUAUGCUAUUAAUGUUAUGCAAUGUUUA
AUUGAAGAAGCUACUCAUAUGACUCCUUGUUAUGAAUUACAUGGUUUACGUUGGGUUCAAAUUCAAGAUUAUGCUAUUAAUGUUAUGCAAUGUUUG
AUUGAAGAAGCUACUCAUAUGACUCCUUGUUAUGAAUUACAUGGUUUACGUUGGGUUCAAAUUCAAGAUUAUGCUAUUAAUGUUAUGCAAUGUCUU
AUUGAAGAAGCUACUCAUAUGACUCCUUGUUAUGAAUUACAUGGUUUACGUUGGGUUCAAAUUCAAGAUUAUGCUAUUAAUGUUAUGCAAUGUCUC
AUUGAAGAAGCUACUCAUAUGACUCCUUGUUAUGAAUUACAUGGUUUACGUUGGGUUCAAAUUCAAGAUUAUGCUAUUAAUGUUAUGCAAUGUCUA
AUUGAAGAAGCUACUCAUAUGACUCCUUGUUAUGAAUUACAUGGUUUACGUUGGGUUCAAAUUCAAGAUUAUGCUAUUAAUGUUAUGCAAUGUCUG
AUUGAAGAAGCUACUCAUAUGACUCCUUGUUAUGAAUUACAUGGUUUACGUUGGGUUCAAAUUCAAGAUUAUGCUAUUAAUGUUAUGCAAUGCUUA
AUUGAAGAAGCUACUCAUAUGACUCCUUGUUAUGAAUUACAUGGUUUACGUUGGGUUCAAAUUCAAGAUUAUGCUAUUAAUGUUAUGCAAUGCUUG
AUUGAAGAAGCUACUCAUAUGACUCCUUGUUAUGAAUUACAUGGUUUACGUUGGGUUCAAAUUCAAGAUUAUGCUAUUAAUGUUAUGCAAUGCCUU
AUUGAAGAAGCUACUCAUAUGACUCCUUGUUAUGAAUUACAUGGUUUACGUUGGGUUCAAAUUCAAGAUUAUGCUAUUAAUGUUAUGCAAUGCCUC
AUUGAAGAAGCUACUCAUAUGACUCCUUGUUAUGAAUUACAUGGUUUACGUUGGGUUCAAAUUCAAGAUUAUGCUAUUAAUGUUAUGCAAUGCCUA
AUUGAAGAAGCUACUCAUAUGACUCCUUGUUAUGAAUUACAUGGUUUACGUUGGGUUCAAAUUCAAGAUUAUGCUAUUAAUGUUAUGCAAUGCCUG
AUUGAAGAAGCUACUCAUAUGACUCCUUGUUAUGAAUUACAUGGUUUACGUUGGGUUCAAAUUCAAGAUUAUGCUAUUAAUGUUAUGCAGUGUUUA
AUUGAAGAAGCUACUCAUAUGACUCCUUGUUAUGAAUUACAUGGUUUACGUUGGGUUCAAAUUCAAGAUUAUGCUAUUAAUGUUAUGCAGUGUUUG
AUUGAAGAAGCUACUCAUAUGACUCCUUGUUAUGAAUUACAUGGUUUACGUUGGGUUCAAAUUCAAGAUUAUGCUAUUAAUGUUAUGCAGUGUCUU
AUUGAAGAAGCUACUCAUAUGACUCCUUGUUAUGAAUUACAUGGUUUACGUUGGGUUCAAAUUCAAGAUUAUGCUAUUAAUGUUAUGCAGUGUCUC
AUUGAAGAAGCUACUCAUAUGACUCCUUGUUAUGAAUUACAUGGUUUACGUUGGGUUCAAAUUCAAGAUUAUGCUAUUAAUGUUAUGCAGUGUCUA
Upvotes: 0
Reputation: 1411
Based on your example, the protein_sequence variable is currently only declared in local scope to the protein_fasta function.
You will need to assign the result of this function to a variable before you can iterate over it.
For example, switch your print to a return:
def protein_fasta (protein_file):
protein_sequence = []
protein = SeqIO.parse(protein_file, format = 'fasta')
for Seqrecord in protein:
protein_sequence.append(Seqrecord.seq)
return protein_sequence
And make sure to call and assign the result of the function:
protein_sequence = protein_fasta(protein_file)
Now you have something you can iterate over.
I can see an additional problem with your for loop. You aren't doing anything with seq. Presumably protein_sequence should be swapped for seq in this instance. I've also taken out the list wrapping RNA_codon_table, as I think it's not needed in this case:
for seq in protein_sequence:
codons = [ RNA_codon_table[key] for key in seq ]
print(list(itertools.product(*codons)))
Upvotes: 1