0

I am new to python so forgive me if my vocabulary isn't correct. I have a homework assignment where I need to enter a DNA sequence that is a multiple of three, has a start codon, and a stop codon. So far it's going fairly well but I am stuck on a couple of issues. I added "or" operators in the stop codon section since there are three acceptable inputs, however, I still receive the print message "sequence does not include a valid start codon". The other thing I want to be able to do is to have the program go back to the original input if the sequence does not meet the criteria. Currently it outputs the mRNA and protein anyways. See pictures and code below.

Thanks

Output

from Bio.Seq import Seq
DNA_text = raw_input("Please enter a DNA sequanece that is a valid ORF\n")
DNA = Seq(DNA_text)
print("You have entered the following DNA sequence: " + DNA)
while DNA[0:3] != "ATG":
    print ("Sequence does not include a valid start codon")
    break
while DNA[-3:len(DNA)] != "TAA"or"TAG"or"TGA":
    print ("Sequence does not include a valid stop codon")
    break
while len(DNA) %3 != 0:
    print ("Sequence is not a valid reading frame")
    break
mRNA = DNA.transcribe()
protein = mRNA.translate()
print("mRNA: " + mRNA)
print("protein: "+ protein)
Dante
  • 21
  • 3

2 Answers2

1

You're using conditionals so using if is better than while. Anyway, your problem is that in the second condition check, the condition is interpreted as DNA[-3:len(DNA)] != "TAA")or"TAG"or"TGA". So, you can do if DNA[-3:len(DNA)] not in ("TAA", "TAG", "TGA"):. For your second question, I'll assume that you want to take input again if the input is not valid. This is easily achievable with an infinite loop with a break condition. Here's an example.

valid = False
while !valid:
    DNA_text = raw_input("Please enter a DNA sequanece that is a valid ORF\n")
    DNA = Seq(DNA_text)
    print("You have entered the following DNA sequence: " + DNA)
    if DNA[0:3] != "ATG":
        print ("Sequence does not include a valid start codon")
    
    elif DNA[-3:len(DNA)] not in ("TAA","TAG","TGA"):
        print ("Sequence does not include a valid stop codon")

    elif len(DNA) %3 != 0:
        print ("Sequence is not a valid reading frame")

    else:
        valid = True

#rest of your code here
Abdur Rakib
  • 336
  • 1
  • 12
0

First things first you need a condition for every or operator used.
while DNA[-3:len(DNA)] != "TAA"or"TAG"or"TGA":
Should be
while DNA[-3:len(DNA)] != "TAA"or DNA[-3:len(DNA)] != "TAG"or DNA[-3:len(DNA)] !="TGA":
A cleaner way of doing this would be
while DNA[-3:len(DNA)] not in ("TAA","TAG","TGA"):

Also you are not using while loops properly. It seems like you are trying to use them like if statements which would simply be:
if DNA[-3:len(DNA)] not in ("TAA","TAG","TGA"):

If you would like to return to the beginning of the input you're going to want to wrap all this code in a while loop with a flag that tells your program whether or not to re-run the code. For example:
flagToReturnToBeginning= true
while(flagToReturnToBeginning):
And inside of the while loop you will set flagToReturnToBeginning to false when the necessary condition is met and you no longer want to continue.

qTzz
  • 126
  • 6
  • https://imgur.com/7VrUEVE That's what I changed. It works but it's not going back to the top when there is an error. I must not be doing it right. – Dante Jan 14 '22 at 19:43
  • You're breaking out of the while loop whenever one of those conditions is met. Not sure what you're trying to do but if you want to continue the while loop you shouldn't have any breaks. – qTzz Jan 14 '22 at 23:40