PyMOL Schrödinger

Advanced Scripting Workshop

Part 7: Write PyMOL Extensions

Every script which adds functionality to PyMOL by binding new commands, hotkeys or plugin menu entries can be considered a PyMOL extension.

When writing an extension, you should focus on first exposing functionality as commands. New commands are registered with cmd.extend. If applicable use arguments and default values like selection=all, state=0 and quiet=1. Write a docstring with DESCRIPTION, ARGUMENTS and EXAMPLES, following the style of core PyMOL commands.

PyMOL's Python API is exposed with the pymol.cmd object. Every Python script should start with:

from pymol import cmd
Example:
from pymol import cmd

@cmd.extend
def residue_stats(selection='all', quiet=1):
    '''
DESCRIPTION

    Count how often each residue is contained in the selection.

USAGE

    residue_stats [ selection ]

EXAMPLE

    fetch 1ubq, async=0
    residue_stats 1ubq
    '''
    from collections import defaultdict
    rescounts = defaultdict(int)

    cmd.iterate('name CA and (' + selection + ')',
        'rescounts[resn] += 1',
        space={'rescounts': rescounts})

    if not int(quiet):
        for resn, count in rescounts.items():
            print(resn + ': ' + str(count))

    return rescounts

Write a "formula" command

Write a formula command that computes the chemical formula of a selection. You'll probably need:

Typing "help formula" should show the following:

USAGE

    formula [ selection [, quiet ]]

EXAMPLE

    PyMOL>formula organic
    FORMUL   P3 C21 H26 O17 N7 2-

Write a center-of-mass command

Write center-of-mass script with iterate_state.

Auto-completion

Argument auto-completion is defined with cmd.auto_arg. Read the PyMOLWiki page and add auto-completion to the formula command.

Get pairwise distances after RMS superposition

Get pairwise distances after alignment and/or superposition. You'll need:


© 2017 Schrödinger, Inc.