Script Tutorial: Difference between revisions

From PyMOLWiki
Jump to navigation Jump to search
(New page: = Introduction = One of the more powerful features of PyMOL is that it supports Python scripting. That gives you the power of using all the Python libraries. You can even use the Python ...)
 
(remove duplicated content)
 
(17 intermediate revisions by 2 users not shown)
Line 1: Line 1:
= Introduction =
This page has been splitted into:
One of the more powerful features of PyMOL is that it supports Python scripting.  That gives you the power of using all the Python libraries.  You can even use the Python API to write programs in other languages and then send the results back into PyMOL (this is what [[cealign]] does).  The PyMOLWiki has a rather extensive [[script_library]] full of useful scripts (feel free to add your own).
 
= General Scripts =
Scripting follows a simple recipe, in PyMOL. 
 
To write them:
#Write the function, let's call it '''doSimpleThing''', in a Python file, let's call the file '''pyProgram.py'''.
#Add the following command to the end of the '''pyProgram.py''' file <source lang="python">cmd.extend(doSimpleThing,doSimpleThing)</source>
 
To use them:
# simply import the script into PyMOL: <source lang="python">run /home/userName/path/toscript/pyProgram.py</source>
 
That's it.  Your script can, through Python, import any modules you need and also edit modify objects in PyMOL.
 
== Getting PyMOL Data into your Script ==
To get PyMOL data into your script you will need to somehow get access to the PyMOL objects and pull out the data.  For example, if you want the atomic coordinates of a selection of alpha carbon atoms your Python function may do something like this (all PyMOL functions are referenced in the See Also section, below):
<source lang="python">
# Import PyMOL's stored module.  This will allow us with a
# way to pull out the PyMOL data and modify it in our script.
# See below.
from pymol import stored
 
def functionName( userSelection ):
    # this array will be used to hold the coordinates.  It
    # has access to PyMOL objects and, we have access to it.
    stored.alphaCabons = []
 
    # let's just get the alpha carbons, so make the
    # selection just for them
    userSelection = userSelection + " and n. CA"
 
    cmd.iterate_state(1, selector.process(userSelection), "stored.alphaCarbons.append([x,y,z])")
 
 
 
</source>
 
 
== Example ==
 
= Advanced Scripts =
 
== Other Languages ==
=== Recipe ===
 
 
== Example ==
See the source code for [[cealign]].
 


* [[Simple Scripting]]
* [[Advanced Scripting]]


== See Also ==
== See Also ==
[[stored]], [[iterate_state]]


[[Category:Development|Script_Tutorial]]
* [[:Category:Scripting]]
* [[:Category:Development]]

Latest revision as of 10:29, 29 May 2011