[Chimera-users] Non-gui chimera in a shell?

Thomas Goddard goddard at cgl.ucsf.edu
Mon May 8 21:27:10 PDT 2006


Hi Jim,

  Importing Chimera from your Python script to use its functionality is
something we have put very little effort into supporting.  I have done
this in an nuclear magnetic resonance assignment program called Sparky
that is based on Python to run Chimera as a structure viewer.  Below is
the code that starts Chimera from the Sparky Python interpretter.

  There are many problems.

  You need to set an environment variable to tell the operating system
where the Chimera shared libraries are before you start Python.  See
example below.

  You need to use the Python included with Chimera because Chimera
uses a modified Python.  Ick.  The modification allows Chimera objects
like Molecules to be given new attributes.  There is also some other
issue on the Mac where a stock Python can't find the init_chimera()
method in shared library _chimera.so.

  If you start the Chimera graphical user interface then you will need
to run the Tk event-loop to get Chimera to update the graphics.  But
then Chimera will not return to your script that started it.  So my
example below does not start the event-loop.

  If you start Chimera in nogui mode, it quits as soon as it processes
any arguments which would probably prevent your script from calling into
Chimera after that as Chimera has cleaned up.

  When I used the script below and then opened a PDB file on my Mac OS
10.4.6 system the screen went dim and a message told me to hold the
power button down to do a hard reboot!  Probably a graphics driver
issue.

  As you can see we really haven't worked much on this type of Chimera use.

	Tom

----

Example starting Chimera on a Mac from Python:

$ export CHIMERA="/Users/goddard/Desktop/Chimera 2199.app/Contents/Resources"
$ export DYLD_LIBRARY_PATH=$CHIMERA/lib
$ "/Users/goddard/Desktop/Chimera 2199.app/Contents/Resources/bin/python2.4"
Python 2.4.2 (#1, Jan 30 2006, 11:15:23) 
[GCC 4.0.1 (Apple Computer, Inc. build 5247)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import start_chimera
>>> start_chimera.start_chimera()
>>>
>>> # Now you could "import chimera" and do some stuff.
>>>

---

The following is the start_sparky.py file used in the above example:

# -----------------------------------------------------------------------------
#
start_count = 0
def start_chimera():

  setup_chimera()

  import chimeraInit
  try:
    chimeraInit.init(argv = [], nogui = False, eventloop = False,
                     exitonquit = False)
  except SystemExit, value:
    # Chimera raises SystemExit sometimes when it can't start.
    # Have to catch it here or Sparky will exit.
    import traceback
    traceback.print_exc()
    raise EnvironmentError, 'Chimera failed to start'
  except:
    global start_count
    if start_count == 1:
      warning = '''
Chimera can only be started once per Sparky session.
This is a limitation in all versions of Chimera released so far
(through March 2004).

'''
      import sys
      sys.stdout.write(warning)
    raise

  start_count += 1

# -----------------------------------------------------------------------------
# Do initial Chimera setup that only needs to be done once even if Chimera
# is started and quit several times.
#
chimera_setup = False
def setup_chimera():

  global chimera_setup
  if chimera_setup:
    return
  chimera_setup = True
  
  import os
  if not os.environ.has_key('CHIMERA'):
    raise EnvironmentError, 'CHIMERA environment variable not set.'
  
  chimera_path = os.environ['CHIMERA']
  import os.path
  if not os.path.exists(chimera_path):
    raise EnvironmentError, 'CHIMERA environment variable gives non-existent directory ' + chimera_path

  chimera_python = os.path.join(chimera_path, 'share')
  chimera_lib = os.path.join(chimera_path, 'lib')
  import sys
  if sys.platform == 'win32':
    chimera_site_packages = os.path.join(chimera_path, 'bin', 'Lib',
                                         'site-packages')
  else:
    chimera_site_packages = os.path.join(chimera_path, 'lib', 'python2.4',
                                         'site-packages')
  
  sys.path.insert(0, chimera_site_packages)
  sys.path.insert(0, chimera_python)
  sys.path.insert(0, chimera_lib)




More information about the Chimera-users mailing list