Thank you for the reply it was very helpful.
1) I believe you are right that the frame is not correctly maintained, I believe the problem is not all of the start/stop are correct. What I mean by this is that sequence_temp%3 is not always true. Do you have any idea why this would be?
2) and 3) I wanted to get the counts right before I worried about formatting and calculating the percentages.
I took your advice and re wrote the code in an attempt to just get the correct counts and I am getting an error where on of the codons only contains two letters. Specifically the error message is
---> 32 codon_count[codon] += 1
33 else:
34 Sequencetemp = str(sequencefull[start:end:1])
KeyError: 'AA'
My new code:
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.Alphabet import generic_dna
codon_count = {"GCC" : 0,"GCT" : 0,"GCA" : 0,"GCG" : 0,"CGT" : 0 ,"CGC" : 0,"CGA" : 0,"CGG" : 0,"AGA" : 0,"AGG" : 0,
"AAT" : 0,"AAC" : 0,"GAT" : 0,"GAC" : 0, "TGT" : 0,"TGC" : 0,"CAA" : 0,"CAG" : 0,"GAA" : 0,
"GAG" : 0,"GGT" : 0,"GGC" : 0,"GGA" : 0,"GGG" : 0,"CAT" : 0,"CAC" : 0, "ATT" : 0,"ATC" : 0,
"ATA" : 0,"ATG" : 0,"TTA" : 0,"TTG" : 0,"CTT" : 0,"CTC" : 0,"CTA" : 0,"CTG" : 0,"AAA" : 0,
"AAG" : 0,"TTT" : 0,"TTC" : 0,"CCT" : 0,"CCC" : 0,"CCA" : 0,"CCG" : 0, "TCT" : 0,"TCC" : 0,
"TCA" : 0,"TCG" : 0,"AGT" : 0,"AGC" : 0,"ACT" : 0,"ACC" : 0,"ACA" : 0,"ACG" : 0,"TGG" : 0,
"TAT" : 0,"TAC" : 0,"GTT" : 0,"GTC" : 0,"GTA" : 0,"GTG" : 0,"TAA" : 0,"TGA" : 0,"TAG" : 0,}
for seq_record in SeqIO.parse("NC_000913.gbk", "genbank"):
sequencefull=seq_record.seq
for seq_feature in seq_record.features :
if seq_feature.type=="CDS":
location=seq_feature.location
start=seq_feature.location.start
end = seq_feature.location.end
if seq_feature.strand == -1:
Sequencetemp = str(sequencefull[start:end:-1])
for n in range(0, len(Sequencetemp), 3):
codon = Sequencetemp[n:n+3]
codon_count[codon] += 1
elif seq_feature.strand == 1:
Sequencetemp = str(sequencefull[start:end:1])
for n in range(0, len(Sequencetemp), 3):
codon = Sequencetemp[n:n+3]
codon_count[codon] += 1
else:
Sequencetemp = str(sequencefull[start:end:1])
for n in range(0, len(Sequencetemp), 3):
codon = Sequencetemp[n:n+3]
codon_count[codon] += 1
for key, value in codon_count.items() :
print (key, value)