from collections import Counter
# Não faz verificação final
def contacodon_sem_verificacao(seq):
stops = ["TAA", "TAG", "TGA"]
count = Counter()
for i in range(seq.find('ATG'), len(seq) + 1, 3):
codon = seq[i: i + 3]
count.update([codon])
if codon in stops:
break
return dict(count)
# Faz verificação final
def contacodon_com_verificacao(seq):
stops = ["TAA", "TAG", "TGA"]
count = Counter()
for i in range(seq.find('ATG'), len(seq) + 1, 3):
codon = seq[i: i + 3]
count.update([codon])
if codon in stops:
break
if len(codon) != 3:
# Retorna um dicionário vazio, uma outra opção seria lançar um erro.
return {}
return dict(count)
# Repare que não tem códon de finalização e o último grupo tem apenas 2 caracteres.
seq = "GCGATCGAGATGAGCATCGCATCGCGGACTACCGCGCGCGCGCGCGGGAG"
print("Sem verificação:", contacodon_sem_verificacao(seq))
print("Com verificação", contacodon_com_verificacao(seq))
ZnJvbSBjb2xsZWN0aW9ucyBpbXBvcnQgQ291bnRlcgoKCiMgTsOjbyBmYXogdmVyaWZpY2HDp8OjbyBmaW5hbApkZWYgY29udGFjb2Rvbl9zZW1fdmVyaWZpY2FjYW8oc2VxKToKICAgIHN0b3BzID0gWyJUQUEiLCAiVEFHIiwgIlRHQSJdCiAgICBjb3VudCA9IENvdW50ZXIoKQogICAgZm9yIGkgaW4gcmFuZ2Uoc2VxLmZpbmQoJ0FURycpLCBsZW4oc2VxKSArIDEsIDMpOgogICAgICAgIGNvZG9uID0gc2VxW2k6IGkgKyAzXQogICAgICAgIGNvdW50LnVwZGF0ZShbY29kb25dKQogICAgICAgIGlmIGNvZG9uIGluIHN0b3BzOgogICAgICAgICAgICBicmVhawogICAgcmV0dXJuIGRpY3QoY291bnQpCgoKIyBGYXogdmVyaWZpY2HDp8OjbyBmaW5hbApkZWYgY29udGFjb2Rvbl9jb21fdmVyaWZpY2FjYW8oc2VxKToKICAgIHN0b3BzID0gWyJUQUEiLCAiVEFHIiwgIlRHQSJdCiAgICBjb3VudCA9IENvdW50ZXIoKQogICAgZm9yIGkgaW4gcmFuZ2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