This is a read-only mirror of pymolwiki.org

Difference between revisions of "Get Coordinates I"

From PyMOL Wiki
Jump to navigation Jump to search
(alternatives)
 
Line 1: Line 1:
 +
There are several ways to extract or load atomic coordinates in PyMOL using the python API.
 +
 +
== Extract coordinates using [[Get_Model|get_model]] ==
 +
 +
This is the fastest method to extract coordinates. It considers the object rotation matrix.
 +
 
<source lang="python">
 
<source lang="python">
# This script gets a copy of the coordinates in Python,
+
xyz = cmd.get_model('sele', 1).get_coord_list()
# rotates the object about the Z axis, and then
+
</source>
# updates the coordinates in the original object.
+
 
 +
== Extract coordinates using [[iterate_state]] ==
 +
 
 +
This is much slower than the first method. It does '''not''' consider the object rotation matrix.
  
from pymol import cmd
+
<source lang="python">
 +
xyz = []
 +
cmd.iterate_state(1, 'sele', 'xyz.append([x,y,z])', space=locals(), atomic=0)
 +
</source>
  
model = cmd.get_model("pept")
+
== Load coordinates using [[Alter_State|alter_state]] ==
for a in model.atom:
+
 
  a.coord=[ -a.coord[1], a.coord[0], a.coord[2]]
+
This is the most convenient way to load coordinates and works equivalent to '''iterate_state'''.
  
cmd.load_model(model,"tmp")
+
<source lang="python">
cmd.update("pept","tmp")
+
xyz = [...] # some Nx3 list with coordinates
cmd.delete("tmp")
+
xyz_iter = iter(xyz)
 +
cmd.alter_state(1, 'sele', '(x,y,z) = xyz_iter.next()', space=locals())
 
</source>
 
</source>
  
The same result can be achieved also like this:
+
== Load coordinates using [[update]] ==
 +
 
 +
This example gets a copy of the coordinates in Python, rotates the object about the Z axis, and then updates the coordinates in the original object.
  
 
<source lang="python">
 
<source lang="python">
alter_state 1, pept, (x,y,z) = (-y,x,z)
+
model = cmd.get_model('pept')
 +
for a in model.atom:
 +
    a.coord = [ -a.coord[1], a.coord[0], a.coord[2]]
 +
 
 +
cmd.load_model(model, "_tmp")
 +
cmd.update("pept", "_tmp")
 +
cmd.delete("_tmp")
 
</source>
 
</source>
  
or like this:
+
== Load coordinates using load_coords ==
 +
 
 +
This method is experimental and buggy, but it's the fastest method to load coordinates. There is a count mistake in the state argument (must use state+1). The xyz argument '''must''' be a list of lists (no tuples). It only works for complete objects, not atom selections.
  
 
<source lang="python">
 
<source lang="python">
rotate z, 90, pept, camera=0, origin=[0,0,0]
+
import pymol.experimenting
 +
pymol.experimenting.loadable = cmd.loadable
 +
pymol.experimenting.load_coords(xyz, object, state+1)
 
</source>
 
</source>
  
 
[[Category:Script_Library|Get Coordinates I]]
 
[[Category:Script_Library|Get Coordinates I]]
 
[[Category:ObjSel_Scripts]]
 
[[Category:ObjSel_Scripts]]

Revision as of 12:29, 2 April 2012

There are several ways to extract or load atomic coordinates in PyMOL using the python API.

Extract coordinates using get_model

This is the fastest method to extract coordinates. It considers the object rotation matrix.

xyz = cmd.get_model('sele', 1).get_coord_list()

Extract coordinates using iterate_state

This is much slower than the first method. It does not consider the object rotation matrix.

xyz = []
cmd.iterate_state(1, 'sele', 'xyz.append([x,y,z])', space=locals(), atomic=0)

Load coordinates using alter_state

This is the most convenient way to load coordinates and works equivalent to iterate_state.

xyz = [...] # some Nx3 list with coordinates
xyz_iter = iter(xyz)
cmd.alter_state(1, 'sele', '(x,y,z) = xyz_iter.next()', space=locals())

Load coordinates using update

This example gets a copy of the coordinates in Python, rotates the object about the Z axis, and then updates the coordinates in the original object.

model = cmd.get_model('pept')
for a in model.atom:
    a.coord = [ -a.coord[1], a.coord[0], a.coord[2]]

cmd.load_model(model, "_tmp")
cmd.update("pept", "_tmp")
cmd.delete("_tmp")

Load coordinates using load_coords

This method is experimental and buggy, but it's the fastest method to load coordinates. There is a count mistake in the state argument (must use state+1). The xyz argument must be a list of lists (no tuples). It only works for complete objects, not atom selections.

import pymol.experimenting
pymol.experimenting.loadable = cmd.loadable
pymol.experimenting.load_coords(xyz, object, state+1)