Back to index

python-biopython  1.60
xbb_blastbg.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Created: Sat Dec  2 16:02:17 2000
00003 # Last changed: Time-stamp: <00/12/03 13:12:39 thomas>
00004 # Thomas.Sicheritz@molbio.uu.se, http://evolution.bmc.uu.se/~thomas
00005 # File: xbb_blastbg.py
00006 
00007 import posixpath, posix
00008 import os, sys, commands
00009 sys.path.insert(0, '.')
00010 import Queue, threading
00011 import tempfile
00012 from Tkinter import *
00013 from xbb_utils import NotePad
00014 
00015 class BlastDisplayer:
00016     def __init__(self, command, text_id = None):
00017         self.command = command
00018         self.tid = text_id
00019         
00020     def RunCommand(self):
00021         self.outfile = tempfile.mktemp()
00022 
00023         # make sure outfile exists and is empty
00024         fid = open(self.outfile,'w+'); fid.close()
00025         
00026         com = '%s > %s' % (self.command, self.outfile)
00027 
00028         self.worker = BlastWorker(com)
00029         self.worker.start()
00030         self.UpdateResults()
00031         
00032     def UpdateResults(self):
00033         # open the oufile and displays new appended text
00034         fid = open(self.outfile)
00035         size = 0
00036         while 1:
00037             if self.worker.finished: break
00038             fid.seek(size)
00039             txt = fid.read()
00040             size = os.stat(self.outfile)[6]
00041             try:
00042                 self.tid.insert(END, txt)
00043                 self.tid.update()
00044             except:
00045                 # text widget is detroyed, we assume the search
00046                 # has been cancelled
00047                 break
00048 
00049         fid.close()
00050         self.Exit()            
00051         
00052 
00053     def Exit(self):
00054         if os.path.exists(self.outfile):
00055             os.remove(self.outfile)
00056 
00057         # do I need to stop the queue ?
00058         self.worker.shutdown()
00059         del self.worker
00060         
00061         
00062 class BlastWorker(threading.Thread):
00063   
00064     def __init__(self, command):
00065         self.com = command
00066         queue = Queue.Queue(0)
00067         self.queue = queue
00068         threading.Thread.__init__(self)
00069         self.finished = 0
00070         print dir(queue)
00071         print queue.queue
00072 
00073     def shutdown(self):
00074         # GRRRR How do I explicitely kill a thread ???????
00075         #self.queue.put(None)
00076         del self.queue
00077         
00078     def run(self):
00079         print 'running', self.com
00080         os.system(self.com)
00081         self.finished = 1
00082 
00083 
00084 if __name__ == '__main__':
00085     np = NotePad()
00086     com = "blastall -d nr -i test.fas -p blastx"
00087     test = BlastDisplayer(com, np.tid)
00088     test.RunCommand()