This is a read-only mirror of pymolwiki.org

Running Scripts

From PyMOL Wiki
Revision as of 03:49, 28 March 2014 by Bell (talk | contribs) (5 revisions)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This page is a short description for beginners on how to run scripts in PyMOL, e.g. from the Script Library. For help on writing scripts, look here: Simple_Scripting, Advanced_Scripting.

Saving the source code

First, you have to save the source code. Copy the text in the box to a text editor and save it in text format under an arbitrary file name, let's say we save it as script.txt in the PyMOL working directory.

Python or Pymol?

Then, you have to find out wheter the script you want to run is a python script or a PyMOL script. Look at the source code: if the lines look just as you type them in the command line in PyMOL, it is a PyMOL script. Run it with @, in our example, type

@script.txt

in the PyMOL command line. You can find examples in the script library: Split_Movement (loads a structure from the internet and moves parts in different directions), Show_charged (Selects charged residues and highlights them). Any PyMOL log file would also be an example for a pymol script.

If, in contrast, you find words as "import" in one of the first lines, or "def" or "cmd" in the text body, you have a python script. Run it with run. In the example, type

run script.txt

in the PyMOL command line.

Most python scripts in the script library don't start action immediately but define a new command instead. You first have to run the script and then you can use the command. Many script pages provide hints for usage. If not, look at the bottom of the script for a line like this:

cmd.extend("new_command",new_command)

The text in quotation marks is the new command. Enter it in the PyMOL command line.

You can find many examples for python scripts in the script library, e.g.: Color_Objects (colors all the objects differently) Resicolor (Colors residues according to their property)

Python Modules

A python module is a python script that runs in it's own namespace, by using the import syntax instead of run. The file must be located in any directory of the sys.path variable. This is the recommended way for scripts from the Pymol-script-repo.

import color_obj   # skip the .py extension!

Example: Color_Objects

PyMOL>run color_obj.py
PyMOL>color_obj 
 
Colouring objects using PyMOL defined colours
 
   obj_0001 red
   obj_0002 green
   obj_0003 blue
   obj_0004 yellow
   obj_0005 violet
   obj_0006 cyan
   obj_0007 salmon
   obj_0008 lime
   obj_0009 pink
   obj_0010 slate
   obj_0011 magenta
   obj_0012 orange
   obj_0013 marine
   obj_0014 olive
   ...