Skip to content

Biostumblematic

A biophysicist teaches himself how to code

More work on gpdbcleaner. Today has been one of those sessions where I feel like I’m learning by leaps and bounds.

I changed the GUI to use the “grid” layout manager. This just seems cleaner than the “pack” manager. I also added message widgets which return the filenames being worked on (the PDB file to open and the save file name).

Along the way I learned about StringVar(). This allowed me to dynamically update the filenames as they were altered by the user, and I can finally pass the filenames between functions! I got rid of the clunky global file list I was using before.

I’ve also modified the “Open” button to perform two functions. The first is the same as before, to set the filename. The second reads in a listing of all record names in the PDB file. This is in preparation for an expansion in which I’ll let the user choose which record names to include in the “Clean” file.

Here is a screenshot of what the program looks like these days:

gpdbcleaner interface

gpdbcleaner interface

And here is the source code:

#!/usr/bin/python

# https://biostumblematic.wordpress.com

import sys, re, tkFileDialog, string
from Tkinter import *

recordnames = []

class Gpdbcleaner:
    def __init__(self, root):
        '''Set up the TKinter window'''
        frame = Frame(root)
        frame.pack(fill=BOTH)

        self.openbutton = Button(frame, text='Select PDB file', command=self.openfile)
        self.openbutton.grid(row=0, sticky=W)

        self.openfilename = StringVar()
        self.openfilename.set('')

        self.openfile = Label(frame, width=50, relief=SUNKEN, anchor=W, textvariable=self.openfilename)
        self.openfile.grid(row=0, column=1, sticky=W)

        self.savefilename = StringVar()
        self.savefilename.set('')

        self.savebutton = Button(frame, text='Save As', command=self.asksaveasfilename)
        self.savebutton.grid(row=1, sticky=W)

        self.savefile = Label(frame, width=50, relief=SUNKEN, anchor=W, textvariable=self.savefilename)
        self.savefile.grid(row=1, column=1, sticky=W)

        self.runbutton = Button(frame, text='Run', fg='green', command=self.run)
        self.runbutton.grid(row=2, sticky=W)

        self.quitbutton = Button(frame, text='Quit', fg='red', command=frame.quit)
        self.quitbutton.grid(row=2, column=1, sticky=W)

    def openfile(self):
        self.askopenfilename()
        self.getrecordnames()

    def askopenfilename(self):
        '''Get the name of the PDB file to be cleaned up'''
        dirtyfile = tkFileDialog.askopenfilename()
        self.openfilename.set(dirtyfile)

    def asksaveasfilename(self):
        '''Get the save as file name'''
        cleanfile = tkFileDialog.asksaveasfilename()
        self.savefilename.set(cleanfile)

    def getrecordnames(self):
        '''Create a list of all the record names in the PDB file'''
        filetoopen = self.openfilename.get()
        inputfile = open(filetoopen, 'r')
        lines = inputfile.readlines()
        for line in lines:
            recordmatch = re.match('^\S*', line)
            record = recordmatch.group()
	    if record in recordnames:
	        pass
	    else:
	        recordnames.append(record)
        inputfile.close()

    def run(self):
        '''Perform the cleanup'''
        atomrecords=[]
        filetoopen = self.openfilename.get()
        inputfile = open(filetoopen, 'r')
        lines = inputfile.readlines()
        for line in lines:
            match = re.search('^ATOM', line)
            if match:
                atomrecords.append(line)
            else:
                pass
        atomrecords.append('END')
        inputfile.close()

        # Write out the records
        filetowrite = self.savefilename.get()
        outputfile = open(filetowrite, 'w')
        outputfile.writelines(atomrecords)
        outputfile.close()

root = Tk()
root.title('PDB cleaner')

gpdbcleaner = Gpdbcleaner(root)

root.mainloop()
Advertisements

Tags: , , ,

%d bloggers like this: