<p>I'm afraid none of your code makes much sense. You have written a function that expects an array, and then you are passing it a single letter</p>
<pre class="lang-py prettyprint-override"><code>for gene in genelist:
    for codon in gene:
        mRNA= transcription(codon)
</code></pre>
<p>Then, the function also doesn't make sense. What does <code>next(codon)</code> mean? How would your script know what a codon is or how to move to the next one? And your <code>if codon == 'ATG':</code> is within the <code>if codon != 'ATG':</code> block, so it will never be executed by definition.</p>
<p>I think what you are looking for is something like this, using the same basic approach as your script:</p>
<pre class="lang-py prettyprint-override"><code>#!/bin/python3
from Bio.Seq import Seq
from Bio.Seq import transcribe 

genelist=[]

gene_1= 'A','C','G','G','A','C','T','A','T','T','C'
gene_2= 'G','G','C','C','A','T','G','A','G','T','A','A','C','G','C','A','T','A','G','G','G','C','C','C'
gene_3='G','G','G','C','C','C','A','T','G','A','C','G','T','A','C','T','A','G','G','G','G','C','C','C','A','T','G','C','A','T','T','C','A','T','A','G'

genelist.append(gene_1)
genelist.append(gene_2)
genelist.append(gene_3)

def transcription(ORF):
    mRNA= ''
    foundStart = False
    foundEnd = False
    for i in range(0, len(ORF), 3):
        codon= &quot;&quot;.join(ORF[i:i+3])
        if codon == 'ATG' and not foundStart:
            foundStart = True
        if foundStart and not foundEnd:
            cc=transcribe(codon)
            mRNA = mRNA + transcribe(codon)
        if codon == 'TAG':
            foundEnd = True
       
    return(mRNA)

mRNAs=[]
for gene in genelist:
    mRNA = transcription(gene)
    mRNAs.append(mRNA)
print(mRNAs)
</code></pre>
<p>Which returns:</p>
<pre><code>$ foo.py 
['', '', 'AUGACGUACUAG']
</code></pre>
<p>Of course, it only returns one mRNA since you only have one gene with an ORF in the 1st frame and your script was only looking at the forward strand and the first frame. In reality, ORFs can be in any of the 6 possible frames. You are also assuming that all ORFs will end with a TAG which is perhaps the case in the species you are working in? I don't remember if there is any genetic code that has TAG as the <em>only</em> stop.</p>
<p>If you do actually need to find ORFs in any frame, then I hope the script above should be enough to get you started.</p>

CC BY-SA 4.0
2020-08-11T13:42:34.850Z
@EdwinOrtegaArzola you're welcome, but please read the last paragraph. There are some serious issues with this approach and you shouldn't be using it for any real work. I strongly urge you to use an already existing tool for this instead of writing your own.
terdon
CC BY-SA 4.0
2020-08-11T13:43:24.157Z
This is precisely the structure and functions I was looking for!! I forgot about the True and False statements and they are extremely useful here! so Thank you!
I agree on the ORFs issue, so, I will work later on the different reading frames, as for the stop codons I was really only using TAG as an example, I am actually including other stop condons in the code. Once, again thank you!
Edwin Ortega Arzola
CC BY-SA 4.0
2020-08-01T17:36:56.963Z
2020-08-01T17:36:56.963Z
terdon
<p>Just in case this <em>isn't</em> a homework problem, there are already plenty of programs available to do this. One program I use is <code>getorf</code> from the EMBOSS toolkit:</p>
<p><a href="https://emboss.bioinformatics.nl/cgi-bin/emboss/getorf" rel="nofollow noreferrer">https://emboss.bioinformatics.nl/cgi-bin/emboss/getorf</a></p>

CC BY-SA 4.0
2020-08-11T13:51:10.240Z
Hello, this is not a homework problem but thank you for the link, I can use it to compare results!
Edwin Ortega Arzola
CC BY-SA 4.0
2020-08-02T01:13:56.207Z
2020-08-02T01:13:56.207Z
gringer
<p>I am trying to find a way to read and transcribe a list of DNA sequences (list of lists) only when the for loop finds a start codon (triplet of the list items) and until it finds a stop codon, over and over, but the if function will only transcribe the first codon and won't go through the whole sequence.
I'm trying to do this with a mix of normal python and biopython functions cause I think it's simpler.
<br></p>
<p>This is an example:</p>
<p>Start codon: ATG</p>
<p>Stop codon: TAG</p>
<p>gene_1= 'ACGGACTATTC'</p>
<p>gene_2= 'GGCC<strong>ATG</strong>AGTAACGCA<strong>TAG</strong>GGCCC</p>
<p>gene_3=GGGCCC<strong>ATG</strong>ACGTAC<strong>TAG</strong>GGGCCC<strong>ATG</strong>CATTCA<strong>TAG</strong></p>
<p>So, the first gene does not contain any transcript, whereas the second contains 1 and the third contains 2. I'm trying to get rid of everything outside these reading frames and append these transcripts into a list that should look something like this.</p>
<p>mRNAs = ['AGUAACGCA', 'ACGUAC', 'CAUUCA']</p>
<p>This is what I have so far:</p>
<pre><code>genelist=[]

gene_1= 'A','C','G','G','A','C','T','A','T','T','C'
gene_2= 'G','G','C','C','A','T','G','A','G','T','A','A','C','G','C','A','T','A','G','G','G','C','C','C'
gene_3='G','G','G','C','C','C','A','T','G','A','C','G','T','A','C','T','A','G','G','G','G','C','C','C','A','T','G','C','A','T','T','C','A','T','A','G'

genelist.append(gene_1)
genelist.append(gene_2)
genelist.append(gene_3)

def transcription(ORF):
    mRNA= ''
    for i in range(0, len(ORF), 3):
        codon= ORF[i:i+3]
        if codon != 'ATG':
            next(codon)
            if codon == 'ATG':
                mRNA=codon.transcribe()
                if codon == 'TAG':
                    break
    return(mRNA)

mRNAs=[]
for gene in genelist:
    for codon in gene:
        mRNA= transcription(codon)
        mRNAs.append(mRNA)
print(mRNAs)
</code></pre>
<p>But it is not really giving anything back, I wonder if the code it's too redundant and I really don't need to define a function here, do you know any better way to do this?
Thaaanks!!</p>

CC BY-SA 4.0
2020-07-31T13:12:43.063Z
There are a lot of issues with this code, some very basic Python mistakes such as forgetting quotes around ATG/TAG in your if statements,  and looping through your genes per base, instead of per codon as your code seems to suggest you wish to. Try adding print statements to your code to see at which positions you are not getting expected results, and edit your code accordingly.
Pallie
CC BY-SA 4.0
2020-07-31T13:17:09.127Z
Is this a homework? If yes, you should mention it's a homework. If not, are you sure you want to do gene prediction like this?
Kamil S Jaron
CC BY-SA 4.0
2020-07-31T13:35:47.217Z
I agree, I forgot the quotes in the start-stop codons, however, the question goes more about the methodology used here, until now, I just managed to transcribe the start codon, but it wouldn't keep extracting the other codons.

@KamilSJaron This is not a homework, it's just an example of what I want I'm trying to do here which is extract fragments within the start/stop frame
Edwin Ortega Arzola
CC BY-SA 4.0
2020-07-31T15:31:44.190Z
I bet my shoes this problem must have a reliable solution. What about https://www.ncbi.nlm.nih.gov/orffinder/ for example?
Kamil S Jaron
CC BY-SA 4.0
2020-08-01T17:15:17.100Z
Debugging 101: add `print` statements to see what you are actually processing. You are calling `mRNA= transcription(codon)`. What is the value of `codon`? Isn't it a single base? Isn't your function expecting an array instead? have you even tried running this? Don't you get an error message?
terdon
<python><phylogenetics><homework>
Transcribing DNA exons and append transcripts into a list
# Transcribing DNA exons and append transcripts into a list

(Asked by: username_0 on 2020-07-31)

I am trying to find a way to read and transcribe a list of DNA sequences (list of lists) only when the for loop finds a start codon (triplet of the list items) and until it finds a stop codon, over and over, but the if function will only transcribe the first codon and won't go through the whole sequence.
I'm trying to do this with a mix of normal python and biopython functions cause I think it's simpler.

This is an example:
Start codon: ATG
Stop codon: TAG
gene_1= 'ACGGACTATTC'
gene_2= 'GGCCATGAGTAACGCATAGGGCCC
gene_3=GGGCCCATGACGTACTAGGGGCCCATGCATTCATAG
So, the first gene does not contain any transcript, whereas the second contains 1 and the third contains 2. I'm trying to get rid of everything outside these reading frames and append these transcripts into a list that should look something like this.
mRNAs = ['AGUAACGCA', 'ACGUAC', 'CAUUCA']
This is what I have so far:
genelist=[]

gene_1= 'A','C','G','G','A','C','T','A','T','T','C'
gene_2= 'G','G','C','C','A','T','G','A','G','T','A','A','C','G','C','A','T','A','G','G','G','C','C','C'
gene_3='G','G','G','C','C','C','A','T','G','A','C','G','T','A','C','T','A','G','G','G','G','C','C','C','A','T','G','C','A','T','T','C','A','T','A','G'

genelist.append(gene_1)
genelist.append(gene_2)
genelist.append(gene_3)

def transcription(ORF):
    mRNA= ''
    for i in range(0, len(ORF), 3):
        codon= ORF[i:i+3]
        if codon != 'ATG':
            next(codon)
            if codon == 'ATG':
                mRNA=codon.transcribe()
                if codon == 'TAG':
                    break
    return(mRNA)

mRNAs=[]
for gene in genelist:
    for codon in gene:
        mRNA= transcription(codon)
        mRNAs.append(mRNA)
print(mRNAs)

But it is not really giving anything back, I wonder if the code it's too redundant and I really don't need to define a function here, do you know any better way to do this?
Thaaanks!!

**Tags:** python, phylogenetics, homework
> **Comments:**
> **username_1**: There are a lot of issues with this code, some very basic Python mistakes such as forgetting quotes around ATG/TAG in your if statements,  and looping through your genes per base, instead of per codon as your code seems to suggest you wish to. Try adding print statements to your code to see at which positions you are not getting expected results, and edit your code accordingly.  
> **username_2**: Is this a homework? If yes, you should mention it's a homework. If not, are you sure you want to do gene prediction like this?  
> **username_0**: I agree, I forgot the quotes in the start-stop codons, however, the question goes more about the methodology used here, until now, I just managed to transcribe the start codon, but it wouldn't keep extracting the other codons.  @KamilSJaron This is not a homework, it's just an example of what I want I'm trying to do here which is extract fragments within the start/stop frame  
> **username_2**: I bet my shoes this problem must have a reliable solution. What about https://www.ncbi.nlm.nih.gov/orffinder/ for example?  
> **username_3**: Debugging 101: add `print` statements to see what you are actually processing. You are calling `mRNA= transcription(codon)`. What is the value of `codon`? Isn't it a single base? Isn't your function expecting an array instead? have you even tried running this? Don't you get an error message?  

---

## 2 answers

### Answer by username_3 on 2020-08-01. Score: 1 | Accepted answer

I'm afraid none of your code makes much sense. You have written a function that expects an array, and then you are passing it a single letter
for gene in genelist:
    for codon in gene:
        mRNA= transcription(codon)

Then, the function also doesn't make sense. What does next(codon) mean? How would your script know what a codon is or how to move to the next one? And your if codon == 'ATG': is within the if codon != 'ATG': block, so it will never be executed by definition.
I think what you are looking for is something like this, using the same basic approach as your script:
#!/bin/python3
from Bio.Seq import Seq
from Bio.Seq import transcribe 

genelist=[]

gene_1= 'A','C','G','G','A','C','T','A','T','T','C'
gene_2= 'G','G','C','C','A','T','G','A','G','T','A','A','C','G','C','A','T','A','G','G','G','C','C','C'
gene_3='G','G','G','C','C','C','A','T','G','A','C','G','T','A','C','T','A','G','G','G','G','C','C','C','A','T','G','C','A','T','T','C','A','T','A','G'

genelist.append(gene_1)
genelist.append(gene_2)
genelist.append(gene_3)

def transcription(ORF):
    mRNA= ''
    foundStart = False
    foundEnd = False
    for i in range(0, len(ORF), 3):
        codon= "".join(ORF[i:i+3])
        if codon == 'ATG' and not foundStart:
            foundStart = True
        if foundStart and not foundEnd:
            cc=transcribe(codon)
            mRNA = mRNA + transcribe(codon)
        if codon == 'TAG':
            foundEnd = True
       
    return(mRNA)

mRNAs=[]
for gene in genelist:
    mRNA = transcription(gene)
    mRNAs.append(mRNA)
print(mRNAs)

Which returns:
$ foo.py 
['', '', 'AUGACGUACUAG']

Of course, it only returns one mRNA since you only have one gene with an ORF in the 1st frame and your script was only looking at the forward strand and the first frame. In reality, ORFs can be in any of the 6 possible frames. You are also assuming that all ORFs will end with a TAG which is perhaps the case in the species you are working in? I don't remember if there is any genetic code that has TAG as the only stop.
If you do actually need to find ORFs in any frame, then I hope the script above should be enough to get you started.

> **Comments:**
> **username_3**: @EdwinOrtegaArzola you're welcome, but please read the last paragraph. There are some serious issues with this approach and you shouldn't be using it for any real work. I strongly urge you to use an already existing tool for this instead of writing your own.  
> **username_0**: This is precisely the structure and functions I was looking for!! I forgot about the True and False statements and they are extremely useful here! so Thank you! I agree on the ORFs issue, so, I will work later on the different reading frames, as for the stop codons I was really only using TAG as an example, I am actually including other stop condons in the code. Once, again thank you!  

---

### Answer by username_4 on 2020-08-02. Score: 0

Just in case this *isn't* a homework problem, there are already plenty of programs available to do this. One program I use is `getorf` from the EMBOSS toolkit:

<https://emboss.bioinformatics.nl/cgi-bin/emboss/getorf>

> **Comments:**
> **username_0**: Hello, this is not a homework problem but thank you for the link, I can use it to compare results!  

---

