Difference between revisions of "Pml"

From PyMOLWiki
Jump to navigation Jump to search
(→‎Basic usage: clarify about relative paths)
 
(4 intermediate revisions by the same user not shown)
Line 2: Line 2:
 
== PyMOL Script ==
 
== PyMOL Script ==
  
PyMOL scripts are plain text files containing PyMOL commands, one per line, appearing as they would be entered at the PyMOL command prompt.  The standard extension for a PyMOL script is `.pml`.
+
PyMOL scripts are plain text files containing PyMOL commands, one per line, appearing as they would be entered at the PyMOL command prompt.  The standard extension for a PyMOL script is <tt>.pml</tt>.
  
  
 
=== Basic usage ===
 
=== Basic usage ===
  
Save your commands in a plain text file (using e.g. Notepad, TextEdit, or a text editor of your choice) with a `.pml` extension, e.g. `script.pml`.
+
Save your commands in a plain text file (using e.g. Notepad, TextEdit, or a text editor of your choice) with a <tt>.pml</tt> extension, e.g. <tt>script.pml</tt>.
 +
 
 +
Run the script by either '''File > Run script...''' in the GUI menu bar, or via the command
  
Run the script by either "File > Run script..." in the GUI menu bar, or via the command
 
 
  @/path/to/my/script.pml
 
  @/path/to/my/script.pml
  
 +
Relative paths are fine, so if PyMOL has been launched in the same directory as your script, you can simplify this to <code>@script.pml</code>
  
 
=== Command syntax ===
 
=== Command syntax ===
Line 29: Line 31:
 
  fetch 1nmr, async=0
 
  fetch 1nmr, async=0
  
will use the asynchronous version of fetch, waiting to execute any following commands until the `fetch` command finishes.
+
will use the asynchronous version of <code>fetch</code>, waiting to execute any following commands until the <code>fetch</code> command finishes.
  
  
 
==== Python code ====
 
==== Python code ====
  
PyMOL scripts can also contain *Python* code.  Single-line Python statements can be written normally and will be handled as expected;
+
''PyMOL'' scripts can also contain ''Python'' code.  Single-line Python statements can be written normally and will be handled as expected;
  
# imports
+
<source lang="python">
import this
+
# imports
from glob import glob
+
import this
 +
from glob import glob
  
# variable assignments
+
# variable assignments
prefix = "structure_"
+
prefix = "structure_"
  
# complex single-line statements like list comprehensions
+
# complex single-line statements like list comprehensions
[cmd.load(x) for x in glob(f'{prefix}*.pdb')]
+
[cmd.load(x) for x in glob(f'{prefix}*.pdb')]
 
# any other arbitrary Python code
 
print(2 + 2)
 
  
Python code that spans more than one line can be enclosed in a `python` code block, like this:
+
# any other arbitrary Python code
 +
print(2 + 2)
 +
</source>
  
python
+
Python code that spans more than one line can be enclosed in a <code>python/python end</code> code block, like this:
def hello(name="World"):
 
    print("Hello,", name)
 
python end
 
  
+
<source lang="python">
 +
python
 +
def hello(name="World"):
 +
    print("Hello,", name)
 +
python end
 +
</source>
  
 
=== Handy commands to include in your scripts ===
 
=== Handy commands to include in your scripts ===

Latest revision as of 15:46, 25 March 2020

PyMOL Script

PyMOL scripts are plain text files containing PyMOL commands, one per line, appearing as they would be entered at the PyMOL command prompt. The standard extension for a PyMOL script is .pml.


Basic usage

Save your commands in a plain text file (using e.g. Notepad, TextEdit, or a text editor of your choice) with a .pml extension, e.g. script.pml.

Run the script by either File > Run script... in the GUI menu bar, or via the command

@/path/to/my/script.pml

Relative paths are fine, so if PyMOL has been launched in the same directory as your script, you can simplify this to @script.pml

Command syntax

PyMOL commands are typically in the form:

command arg1[, arg2[, ...]]

For example:

color green, chain A

which will, as you might expect, color all the atoms of chain A green. There is no comma between the command and the first argument, and in general, no quotation marks are necessary around the arguments. (Exceptions to this rule include situations where you are passing a text string as an argument which contains a comma; in this case the entire string should be quoted.)

For commands that take keyword arguments, the syntax is similar to passing keyword arguments to a Python function:

fetch 1nmr, async=0

will use the asynchronous version of fetch, waiting to execute any following commands until the fetch command finishes.


Python code

PyMOL scripts can also contain Python code. Single-line Python statements can be written normally and will be handled as expected;

# imports
import this
from glob import glob

# variable assignments
prefix = "structure_"

# complex single-line statements like list comprehensions
[cmd.load(x) for x in glob(f'{prefix}*.pdb')]

# any other arbitrary Python code
print(2 + 2)

Python code that spans more than one line can be enclosed in a python/python end code block, like this:

python
def hello(name="World"):
    print("Hello,", name)
python end

Handy commands to include in your scripts

  • reinitialize - issue this command at the start of your script to allow it to be run multiple times in the same PyMOL session.
  • log_open log.pml - create a log file to record actions taken in the GUI. Examine this log file to learn new commands, although keep in mind that these are typically formatted as Python function calls.
  • deselect - place this at the very end of the script to ensure no atoms are selected after the script completes, for a cleaner GUI appearance.