Basically need help with one evil section of code! My program opens a start window which introduces the program. A start button leads to a second window with a number of buttons. The main two are "open" and "find restriction site". Im having problem with the code for the latter. I need the string output of the file I open to be manipulated. I want to iterate through and find the keys from the dictionary, then return the values for use later. I need it to iterate 6 characters at a time, moving forward 1 character at a time...if that makes sense.
Anyway below is the error message and my code so far.
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python26\lib\lib-tk\Tkinter.py", line 1410, in __call__
return self.func(*args)
File "C:\Users\Kt\Documents\Biochemistry\2204-Computer programming\Assess 3\A2test2.py", line 200, in find_list_site
sixer = self.my_sequence[site:site+6]
TypeError: cannot concatenate 'str' and 'int' objects
from Tkinter import *
from tkMessageBox import *
from tkFileDialog import *
#An example library of restriction enzymes and their recognition sites
site_enz= {'GAATTC':'EcoRI','GGATTC':'BamHI','AAGCTT':'HindIII',
'CCCGGG':'Smal','GATATC':'EcoRV'}
class RestrictionSites (object):
"""A class to find splice sites within an RNA sequence chosen by the user"""
def __init__(self, master):
self.master = master
self.frame = Frame(master)
self.frame.pack()
self.my_sequence=''
# selecting the image to use
self.image = PhotoImage(file="Opening Picture.gif")
# create a canvas that fits the image used
self.canvas = Canvas(self.frame,
width=self.image.width(),
height=self.image.height())
# aligning the image to the top left corner of the canvas
self.canvas.create_image(0,0, anchor=NW, image=self.image)
self.canvas.pack()
# now I want to add a button underneath, cannot but canvas and
# button on the same frame so need to create a new frame for button
self.button_panel = Frame(self.frame)
self.button_panel.pack(side=TOP)
# sets the width and position of the start button
self.start_button = Button(self.button_panel,
text="Click to upload a single stranded DNA sequence 5'-3'",
width=100, command=self.startbutton)
self.start_button.pack()
#Button to close window
self.cancel_button= Button (self.button_panel,text="Close Program",
width=100, command=self.bye)
self.cancel_button.pack()
def startbutton (self):
self.master.withdraw()
return self.box2()
def box2 (self):
root=Tk()
self.frame=Frame(root)
self.frame.pack()
#add a text area 80 characters wide and 20 characters high
self.text = Text(self.frame, width=80, height= 20)
self.text.pack()
#underneath the text area add a label and left-justify its text
self.label= Label(self.frame, text="Please select a file containing a 5' to 3' DNA sequence. ")
self.label.pack(side=TOP, anchor=W)
#Cant mix packa nd grid in the same frame, so
#underneath the label add another Frame to hold the buttons
self.button_panel = Frame (self.frame)
self.button_panel.pack (side=TOP)
#Put the check button in the left part of the frame
self.open_button = Button(self.button_panel, text="Open", width=30,
command=self.openfile)
self.open_button.grid (row=0, column=0, padx=20)
#Add a button to vlaidate sequence using self.check
self.check_button = Button(self.button_panel, text="Check", width=30,
command=self.check)
self.check_button.grid (row=0, column=1, padx=20)
#Put the translate butotn in the right part of the Frame and grey it out
self.find_button= Button(self.button_panel, text = "Find Restriction Sites", width=30,
command=self.find_list_site)
self.find_button.grid (row=0, column=2, padx=20)
self.close_button= Button(self.button_panel, text = "close", width=30, command=root.destroy)
self.close_button.grid (row=0, column=3, padx=20)
def bye(self):
#remove the window from the screen when they click bye
self.master.withdraw()
#Stop the event handling loop and garbage collect
self.master.quit()
def key_pressed(self, event):
# strange syntax required by Tcl
sequence = self.text.get(1.0,END).strip()
# note that we receive the event BEFORE it has been processed
if event.keysym=="BackSpace" and len(sequence)<=1:
self.check_button.config(state=DISABLED)
self.find_button.config(state=DISABLED)
else:
self.check_button.config(state=NORMAL)
def openfile(self):
self.filename = askopenfilename(parent=root,initialdir="/",title='Please select a directory')
self.my_file=open(self.filename,'r')
self.temp=self.my_file.read().upper()
self.my_file.close()
self.my_sequence=''
for base in self.temp:
if base in 'ATGC':
self.my_sequence += base
self.text.insert(INSERT, self.my_sequence)
return self.my_sequence
def check(self):
# strange syntax required by Tcl
sequence = self.text.get(1.0,END).strip()
# code should now validate the sequence
while True:
self.file=self.openfile()
self.file=self.openfile.upper()
#Only these bases allowed
good_bases = "ATCG"
temp_sequence = ''
for base in self.file:
if base in good_bases:
temp_sequence += base
self.file = temp_sequence
#Flag for error- assume OK at the start.
bad_input = False
#Check each base.
for base in self.file:
if base in good_bases:
#Ok so far, check next base.
continue
else:
#Not an RNA base, so flag the error.
bad_input= True
#Give the user the bad news
print 'Sorry - that is not a DNA sequence, please try again.'
#break from the 'for' loop
break
#By this stage the sequence has been checked.
#If all is awell, break form the 'while' loop.
if bad_input ==False:
break
#Otherwise the 'while' loop will go back for re-input.
return self.file
# if it is OK, the translate button should be enabled
self.find_button.config(state=NORMAL)
def find_list_site( self):
"""find list of sites in file"""
#function to find six-base restriction sites in a DNA sequence
#self.opemfile is a string containing the DNA sequence
#init_pos is the position to start searching from (to start from the beginning, set it to 0)
#restriction_site (self.find_site) is a string containing the sequence sought....
#but i need to get the site from the dictionary
#the function returns a list of positions where the restriction site was found
#if none are found then the list will be empty
#self.openfile is the string that you read (and filter) from the file.
#restriction_site is the sequence of one of the sites in your dictionary.
#For example for EcoRI, restriction_site would be 'GAATTC'.
#You would need to run the function for each restriction site in your dictionary.
list_of_sites=[]
end = len(self.my_sequence)-6
#Key= the code from the DNA
#Value= enz code eg. BamHI
for site in site_enz.keys():
sixer = self.my_sequence[site:site+6]
if sixer==site:
list_of_sites.append(site)
return list_of_sites
def FindSite (enz):
"""This function checks if there is a restriction site in the DNA"""
if site_enz.has_key(enz)==False:
return '?'
return site_enz.get(site)
def translate_sequence( sequence, init_pos ):
""" This function translates an mRNA sequence and returns the
corresponding protein sequence. """
restriction_seq = ""
sequence_length = len(my_sequence)
# Start at the first base of the initiation codon.
# Stop at the end of the mRNA sequence.
# Increment by 3 bases each time.
for pointer in range(init_pos, sequence_length, 6):
# Slice out the codon
site = sequence[pointer:pointer+3]
# The last codon could be 1, 2 or 3 bases long
# If it's not a complete codon - we've run off the end of the sequence
if len(site) < 6: break
# Translate the codon and add the amino acid to the protein sequence.
restriction_seq += FindSite(site)
return restriction_seq
#Main program
root= Tk()
root.title("Restriction Site finder")
#Set this class to be an objecy
app=RestrictionSites(root)
root.mainloop()
Any questions will be answered quickly. Please help!