This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Python- Searching codons using for/if

Hi everyone!

Can someone help me with this little problem? I am trying to look for the position of the first nucleotide of a metionine codon (or a start codon, whatever you like). For that reason, I am using for and if in this way (here is my code):

s_otro_dna = str(otro_dna)
donde_M = []
for i in range(0,len(s_otro_dna),3):
    codon = s_otro_dna[i:i+3]
    if (codon==['ATG']):
        donde_M.append(i)

Note: The type of the variable where I saved the sequence is string

This code doesn't give me any error message. However, when I try to look what is inside the list "donde_M" (where it is supposed to be the positions), Python "says" that is empty:

donde_M
Out[37]: []

What I am doing wrong? Thank you for your help!! :)

python

I tried and the output that python gives me is the same

The approach seems like it works to me:

>>> dna = "ATGCGATCGATCGATCGATCGCGCGCGCAGCTA"
>>> for i in range(0, len(dna), 3):
...     codon = dna[i:i+3]
...     if codon == 'ATG':
...         print(str(i))

0

As alex points out, you don't need the [ ] or the ( ) for that matter, in your if

Than you for your answer, but it also doesn't work. I've been investigating and the problem is within the if condition, it is something wrong.

> dna = "ATGCGATCGATCGATCGATCGCGCGCGCAGCTA" for i in range(0, len(dna),
> 3):
>     codon = dna[i:i+3]
> #    if codon == 'ATG':
> #        print(str(i))
>     print(codon, i)
> # why do you need str(i)?
>     print(codon, str(i)) # - nothing in this case

Because i is an int and if you don't cast it to a str first, print will complain.

If you're printing i only, its not strictly necessary, but I usually print these things within other strings, so its just a habit I'm in.

Then something is wrong elsewhere in your code, because that works absolutely fine for me.

3 answers

Use re to find the start index positions of all instances of your string-of-interest (here, your start codon, or whatever).

$ python
>>> import re
>>> [m.start() for m in re.finditer('ATG', 'ATGCGATCGATCGATCGATCGCGCGCGCAGCTA')]
[0]

Replace with ATGCGATCGATCGATCGATCGCGCGCGCAGCTA with s_otro_dna or whatever you want to search through.

Via: https://stackoverflow.com/questions/4664850/how-to-find-all-occurrences-of-a-substring

I think I have found the problem! Ii is not in the if condition, it is in the way I have expressed the range. Instead of indicating the end of range with len(s_otro_dna) I introduce the length (in this case, 923) it works. Here is the code:

for i in range(0,923, 3):
    codon = s_otro_dna[i:i+3]
    if codon == 'ATG':
        print(str(i))


231
348
606
792
864

Thank you for your help! :)

dna = "ATGCGATCGATCGATCGATCGCGCGCGCAGCTA"

for i in range(0, len(dna), 3): codon = dna[i:i+3]

if codon == 'ATG':

print(str(i))

print(codon, i)

why do you need str(i)?

print(codon, str(i)) # - nothing in this case

Log in to answer this question.