When I try to run this code, it never finishes and I think it's stuck somewhere but I'm not too sure since I am new to python.
import re
codon = []
rcodon = []
dataset = "ggtcagaaaaagccctctccatgtctactcacgatacatccctgaaaaccactgaggaagtggcttttcagatcatcttgctttgccagtttggggttgggacttttgccaatgtatttctctttgtctataatttctctccaatctcgactggttctaaacagaggcccagacaagtgattttaagacacatggctgtggccaatgccttaactctcttcctcactatatttccaaacaacatgatga"
startcodon=0
n=0
print ("DNA sequence: ", dataset)
def find_codon(codon, string, start):
i = start + 3
while i < len(string):
i = string.find(codon, i) # find the next substring
if (i - start) % 3 == 0: # check that it's a multiple of 3 after start
return i
return None
while(n < 1):
startcodon=dataset.find("atg", startcodon)
#locate stop codons
taacodon=find_codon("taa", dataset, startcodon)
tagcodon=find_codon("tag", dataset, startcodon)
tgacodon=find_codon("tga", dataset, startcodon)
stopcodon = min(taacodon, tagcodon, tgacodon)
codon.append(dataset[startcodon:stopcodon+3])
if(startcodon > len(dataset) or startcodon < 0):
n = 2;
startcodon=stopcodon
#reverse the string and swap the letters
n=0;
while(n < len(codon)):
rcodon.append (codon[n][len(codon[n])::-1])
#replace a with u
rcodon[n] = re.sub('a', "u", rcodon[n])
#replace t with a
rcodon[n] = re.sub('t', "a", rcodon[n])
#replace c with x
rcodon[n] = re.sub('c', "x", rcodon[n])
#replace g with c
rcodon[n] = re.sub('g', "c", rcodon[n])
#replace x with g
rcodon[n] = re.sub('x', "g", rcodon[n])
print("DNA sequence: ", codon[n] ,'\n', "RNA sequence:", rcodon[n])
n=n+1
answer = 0
print("Total Sequences: ", len(codon)-3)
while (int(answer) >=0):
#str = "Please enter an integer from 0 to " + str(len(dataset)) + " or -1 to quit: "
answer = int(input("Please enter a sequence you would like to see or -1 to quit: "))
if(int(answer) >= 0):
print("DNA sequence: ", codon[int(answer)] ,'\n', "RNA sequence:", rcodon[int(answer)])
Any advice would be helpful.
This is a project about transcribing DNA WITHOUT biopython The goal: create a program that can locate the 'atg' in a DNA sequence and then find the stop sequence (tga, taa, or tag) while counting in threes from the initial atg.
edit: I want the program to give me the sequences between atg and a stop codon like my original code. My original code, however, did not consider moving by 3's from the atg to find the correct stop sequence.
my original code:
import re
codon = []
rcodon = []
dataset = "ggtcagaaaaagccctctccatgtctactcacgatacatccctgaaaaccactgaggaagtggcttttcagatcatcttgctttgccagtttggggttgggacttttgccaatgtatttctctttgtctataatttctctccaatctcgactggttctaaacagaggcccagacaagtgattttaagacacatggctgtggccaatgccttaactctcttcctcactatatttccaaacaacatgatga"
startcodon=0
n=0
while(n < 1):
startcodon=dataset.find("atg", startcodon, len(dataset)-startcodon)
#locate stop codons
taacodon=dataset.find("taa", startcodon+3, len(dataset)-startcodon)
tagcodon=dataset.find("tag", startcodon+3, len(dataset)-startcodon)
tgacodon=dataset.find("tga", startcodon+3, len(dataset)-startcodon)
if(taacodon<tagcodon):
if(taacodon<tgacodon):
stopcodon=taacodon
#print("taacodon", startcodon)
else:
stopcodon=tgacodon
#print("tGacodon", startcodon)
elif(tgacodon>tagcodon):
stopcodon=tagcodon
#print("taGcodon", startcodon)
else:
stopcodon=tgacodon
#print("tGacodon", startcodon)
#to add sequences to an array
codon.append(dataset[startcodon:stopcodon+3])
if(startcodon > len(dataset) or startcodon < 0):
n = 2;
startcodon=stopcodon
#reverse the string and swap the letters
n=0;
while(n < len(codon)):
rcodon.append (codon[n][len(codon[n])::-1])
#replace a with u
rcodon[n] = re.sub('a', "u", rcodon[n])
#replace t with a
rcodon[n] = re.sub('t', "a", rcodon[n])
#replace c with x
rcodon[n] = re.sub('c', "x", rcodon[n])
#replace g with c
rcodon[n] = re.sub('g', "c", rcodon[n])
#replace x with g
rcodon[n] = re.sub('x', "g", rcodon[n])
print("DNA sequence: ", codon[n] ,'\n', "RNA sequence:", rcodon[n])
n=n+1
answer = 0
print("Total Sequences: ", len(codon)-3)
while (int(answer) >= 0):
#str = "Please enter an integer from 0 to " + str(len(dataset)) + " or -1 to quit: "
answer = int(input("Please enter an sequence you would like to see or -1 to quit: "))
if(int(answer) >= 0):
print("DNA sequence: ", codon[int(answer)] ,'\n', "RNA sequence:", rcodon[int(answer)])