Example Scripts: Difference between revisions

From PyMOLWiki
Jump to navigation Jump to search
 
Line 42: Line 42:
<source lang="python">
<source lang="python">
create dimer,(chain A,B)  # Create separate dimer
create dimer,(chain A,B)  # Create separate dimer
</source>
instead use:
<source lang="python">
create dimer,(chain A,B);  # Create separate dimer
</source>
</source>



Latest revision as of 11:10, 25 October 2023

Multiple Object Manipulation Scripts

Dis/Enable Objects

If someone has many (possibly hundreds) of objects -- say distance objects -- one can turn them all on or off by using Python scripts within PyMol. The following script will extend the commands "sd" and "hd" for "show distance" and "hide distance," respectively. The first script,

from pymol import cmd 
num_dist = 100 
        
def show_dist(): 
    """ show all of my distance objects """ 
    for i in range(num_dist): 
        cmd.enable('_dist%s'%i) 
        
def hide_dist(): 
    """ hide all of my distance objects """ 
    for i in range(num_dist): 
        cmd.disable('_dist%s'%i) 
        
cmd.extend('sd',show_dist) 
cmd.extend('hd',hide_dist)

works on 100 objects. We can extend the idea with more elegant scripting to work w/o forcing us to keep track of the number of objects:

def show_dist(): 
    dists = [name for name in cmd.get_names() if cmd.get_type(name) == 'object:distance'] 
    for name in dists: cmd.enable(name)

Now, just running "sd" would show all the distance objects.


Comments in Scripts

The hash-mark, "#" is the Python comment. However, when scripting in Python for PyMol note that the hash character (#) works to include comments on separate lines, but not at the end of a line that contains commands. So you can do

# Create separate dimer
create dimer,(chain A,B)

but not

create dimer,(chain A,B)  # Create separate dimer

instead use:

create dimer,(chain A,B);  # Create separate dimer

Sources

Taken from the PyMol Users list. Python source by Michael Lerner.