On Fri, Mar 26, 2010 at 5:14 PM, Werner Schroedinger <werner.schroedinger@googlemail.com> wrote:
For making this procedure faster and more automaticm I wonder if somehow
can I tell pymol to pay atention to some external file, or for the
ligand being visualized now, to its copy on the hard disk, and
continuosly monitor it, so when it detects a change (for instance,
beacuse the quantum program did something) reload the molecule and
display it.

There may already be a built-in function to do this in PyMOL, but I'm not aware of it.  It's easy to script in Python, however.  The code below definitely works on Mac.  Depending on how fast your calculation is, you may want to increase or decrease the interval, but 2s is a reasonable place to start.


from pymol import cmd
import threading
import os.path
import time

class monitor_file (object) :
  def __init__ (self, file_name, object_name, interval=2, verbose=True) :
    self.file_name = file_name
    self.object_name = object_name
    self.interval = interval
    self.abort = False
    self.last_mtime = 0
    if os.path.isfile(file_name) :
      self.last_mtime = os.path.getmtime(file_name)

  def reload_file (self) :
    if self.verbose :
      print "Reloading %s as '%s'" % (self.file_name, self.object_name)
    cmd.load(self.file_name, self.object_name, state=1)

  def start (self) :
    if self.verbose :
      print "starting thread"
    t = threading.Thread(target=self.run)

  def run (self) :
    while not self.abort :
      if os.path.isfile(self.file_name) :
        if self.verbose :
          print "checking file mtime. . ."
        current_mtime = os.path.getmtime(self.file_name)
        if current_mtime > self.last_mtime :
          self.last_mtime = current_mtime
    return True

  def stop (self) :
    self.abort = True

# Usage
m = monitor_file("/var/tmp/caffeine.pdb", "caffeine")