Python- Searching codons using for/if
3
0
Entering edit mode
5.0 years ago

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 • 8.2k views
ADD COMMENT
0
Entering edit mode

Replace codon==['ATG'] with codon=='ATG'.

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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.

ADD REPLY
0
Entering edit mode
> 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
ADD REPLY
0
Entering edit mode

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.

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode
5.0 years ago

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

ADD COMMENT
0
Entering edit mode
5.0 years ago

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! :)

ADD COMMENT

Login before adding your answer.

Traffic: 1817 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6