Question: Python- Searching codons using for/if
0
gravatar for andreammo97
14 months ago by
andreammo970 wrote:

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

if for search codon python • 1.6k views
ADD COMMENTlink modified 14 months ago by natasha.sernova3.8k • written 14 months ago by andreammo970

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

ADD REPLYlink written 14 months ago by a.zielezinski9.4k

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

ADD REPLYlink written 14 months ago by andreammo970

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 REPLYlink modified 14 months ago • written 14 months ago by Joe18k

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 REPLYlink written 14 months ago by andreammo970
> 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 REPLYlink modified 14 months ago by Joe18k • written 14 months ago by natasha.sernova3.8k

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 REPLYlink written 14 months ago by Joe18k

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

ADD REPLYlink written 14 months ago by Joe18k
0
gravatar for Alex Reynolds
14 months ago by
Alex Reynolds31k
Seattle, WA USA
Alex Reynolds31k wrote:

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 COMMENTlink modified 14 months ago • written 14 months ago by Alex Reynolds31k
0
gravatar for andreammo97
14 months ago by
andreammo970 wrote:

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 COMMENTlink written 14 months ago by andreammo970
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1868 users visited in the last hour
_