Skip to content

Instantly share code, notes, and snippets.

@leelasd
Forked from jaimergp/chimera_cli.py
Created January 2, 2016 02:07
Show Gist options
  • Save leelasd/119b7dddc196cca320a3 to your computer and use it in GitHub Desktop.
Save leelasd/119b7dddc196cca320a3 to your computer and use it in GitHub Desktop.
Use UCSF Chimera IDLE directly in terminal, no GUI
"""
Experimental wrapper to use Chimera's Python directly from console.
You can `import chimera` with no errors!
Usage
=====
1. Alias your /path/to/chimera/bin/python binary to something more user-friendly, like `chimerapy`.
2. Download these two files and put them together
3. Run `chimerapy -i chimera_cli.py`. (Tip: Realias `chimerapy` to this command).
4. Profit!
Notes
=====
Most of this is taken from Greg Couch, at chimera-users:
<http://www.cgl.ucsf.edu/pipermail/chimera-users/2015-January/010647.html>
"""
import os
import sys
def chimera_env():
"""
Prepare environment and path
"""
os.environ['CHIMERA'] = CHIMERA = guess_chimera_path()
os.environ['CHIMERAPATH'] = CHIMERA
sys.path.insert(0, os.path.join(CHIMERA, 'share'))
sys.path.insert(0, os.path.join(CHIMERA, 'bin'))
def chimera_init():
"""
Trigger Chimera initializers
"""
import chimeraInit
basepath = os.path.dirname(os.path.abspath(sys.argv[0]))
# This is the magic!
# If a script needs to be executed, Chimera does not launch
# the in-house command interface! So, just passing a `pass`
# statement does the trick.
chimera_pass = os.path.join(basepath, 'pass.py')
chimeraInit.init(['@jaimergp', '--script', chimera_pass], nogui=True,
eventloop=False, exitonquit=False)
def guess_chimera_path():
# WINDOWS
if sys.platform.startswith('win'):
for programfiles in ('PROGRAMFILES', 'PROGRAMFILES(X86)', 'PROGRAMW6432'):
paths = [os.path.join(os.environ[programfiles], d)
for d in os.listdir(os.environ[programfiles])
if d.startswith('Chimera')]
if paths:
break
else:
paths = [raw_input('Chimera could not be found. Please, type its location manually.\n'
'It should be something like C:/Program Files/Chimera 1.10.1\n')]
paths.sort()
return paths[-1] # get latest version available, if more than one exists
else:
return subprocess.check_output(['chimera', '--root']).decode('utf-8')
if "__main__" == __name__:
chimera_env()
chimera_init()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment