Running Pymol From Python Script
3
3
Entering edit mode
10.7 years ago
Sheila ▴ 280

Hi all, I would like to know is it possible to run Pymol from Pythoin script ? I tried this script (to produce png file from pdb file)

#!/usr/bin/python
import __main__
__main__.pymol_argv = [ 'pymol', '-qc']
import pymol
pymol.finish_launching()
pdb_file ="my.pdb"
pdb_name ="my_pdb"
pymol.cmd.disable("all")
pymol.cmd.enable(pdb_name)
pymol.cmd.png("my_pdb.png")


And run it as "python script.py" but no png file is produced and no error !!! Did I got something wrong about Pymol?? any suggestion !!!

python pymol structure • 27k views
0
Entering edit mode

The PyMOL Wiki post "Launching From a Script" might be helpful.

6
Entering edit mode
10.6 years ago
João Rodrigues ★ 2.5k

The script below worked perfectly in my machine. I installed pymol via MacPorts, so it's a completely brand new installation without any modifications or tweaks. The script outputted one line about the ray tracing process and the image, with the protein, is sitting in the directory I ran the script in.

Do you have pymol in our PYTHONPATH? Can you go to a Python shell and import pymol? Also, which version of Pymol do you have and which OS are you using?

import __main__
__main__.pymol_argv = [ 'pymol', '-qc'] # Quiet and no GUI

import sys, time, os
import pymol

pymol.finish_launching()

##
spath = os.path.abspath(sys.argv[1])
sname = spath.split('/')[-1].split('.')[0]

pymol.cmd.disable("all")
pymol.cmd.enable(sname)
pymol.cmd.png("my_image.png")

# Get out!
pymol.cmd.quit()

2
Entering edit mode

I guess pymol provided by Linux package manager have some bug or feature missing. Removing old and installed new Pymol from http://sourceforge.net/projects/pymol/ and Now it works. Thanks @João Rodrigues and @Niek De Klein

1
Entering edit mode

If he didn't have pymol in his PYTHONPATH, wouldn't he get an error?

0
Entering edit mode

Yes, but sometimes package managers install different versions of the software in 'random' places.. it's good to know what exactly is being called.

3
Entering edit mode
10.7 years ago
Niek De Klein ★ 2.6k

In the end, don’t forget to close with pymol.cmd.quit()

pymol.cmd.quit()


to the end of your script should probably work.

2
Entering edit mode

Besides, the -q option means that the output of Pymol is silenced, thus the reason you might not see any error.

0
Entering edit mode

even after adding pymol.cmd.quit() and time.sleep(1) I didn't get png file :( also similar to http://doeidoei.wordpress.com/2009/02/11/pymol-api-simple-example/ I tried pymol.cmd.save("%s.png" %(pdb_name), pdb_name , 0, 'png') but still no png :(

0
Entering edit mode

Try Joao's suggestion, remove the q. Maybe it does give errors.

0
Entering edit mode

Yes I tried that also still no png. The script in link http://doeidoei.wordpress.com/2009/02/11/pymol-api-simple-example/ is working fine (so my Pymol and Python are working) but I am not getting any png using the command

pymol.cmd.png("my_pdb.png")


or

pymol.cmd.save("%s.png" %(pdb_name), pdb_name , 0, 'png')


:(

0
Entering edit mode

This might be a stupid question, but have you checked if the file is not being saved somewhere else? Also, remove the "disable"/"enable" part and see if it works.

0
Entering edit mode

I did both but still no PNG :( if someone have a working script which makes PNG from a PDB please post

0
Entering edit mode
9.0 years ago
tyler.weirick ▴ 120

I encountered this problem on Ubuntu 12.04 running pymol verson 1.4.1 (the version installed with apt-get) . I removed that version and reinstalled pymol 1.6.0.0 and was able to get it to work.