Get Coordinates I: Difference between revisions

From PyMOLWiki
Jump to navigation Jump to search
No edit summary
 
(updated)
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
There are several ways to extract or load atomic coordinates in PyMOL using the python API.
== Extract coordinates using [[get_coords]] ==
''New in PyMOL 1.7.4''. This is the fastest method to extract coordinates from a selection. It considers the object rotation matrix.
<source lang="python">
xyz = cmd.get_coords('sele', 1)
</source>
== Extract coordinates using [[get_coordset]] ==
''New in PyMOL 1.7.4''. Operates on the object-state level, not on selections. Does '''not''' consider the object rotation matrix. Retrieves coordinates in original order (e.g. PDB file atom order), not in sorted atom order. Faster than [[get_coords]].
<source lang="python">
xyz = cmd.get_coordset(objectname, 1)
</source>
== Extract coordinates using [[Get_Model|get_model]] ==
Before 1.7.4, this was the fastest method to extract coordinates. It considers the object rotation matrix.
<source lang="python">
xyz = cmd.get_model('sele', 1).get_coord_list()
</source>
== Extract coordinates using [[iterate_state]] ==
This is much slower than the first method. It does '''not''' consider the object rotation matrix.
<source lang="python">
xyz = []
cmd.iterate_state(1, 'sele', 'xyz.append([x,y,z])', space=locals(), atomic=0)
</source>
== Load coordinates using [[Alter_State|alter_state]] ==
This is the most convenient way to load coordinates and works equivalent to '''iterate_state'''.
<source lang="python">
<source lang="python">
from pymol import cmd
xyz = [...] # some Nx3 list with coordinates
xyz_iter = iter(xyz)
cmd.alter_state(1, 'sele', '(x,y,z) = xyz_iter.next()', space=locals())
</source>
 
== Load coordinates using [[update]] ==


model = cmd.get_model("pept")
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">
model = cmd.get_model('pept')
for a in model.atom:
for a in model.atom:
  a.coord=[ -a.coord[1], a.coord[0], a.coord[2]]
    a.coord = [ -a.coord[1], a.coord[0], a.coord[2]]


cmd.load_model(model,"tmp")
cmd.load_model(model, "_tmp")
cmd.update("pept","tmp")
cmd.update("pept", "_tmp")
cmd.delete("tmp")
cmd.delete("_tmp")
</source>
 
== Load coordinates using [[load_coords]] ==
 
''Changed in PyMOL 1.7.3''. Update selection coordinates from a Nx3 float array.
 
<source lang="python">
cmd.load_coords(xyz, selection)
</source>
 
== Load coordinates using [[load_coordset]] ==
 
''New in PyMOL 1.7.4''. Update object state coordinates from a Nx3 float array. Can also append a state. Order of coordinates equivalent to [[get_coordset]].
 
<source lang="python">
cmd.load_coordset(xyz, objectname, 1)
</source>
 
== Load coordinates using [[get_coordset]] ==
 
''New in PyMOL 1.7.4''. The [[get_coordset]] function can also return a memory view instead of a copy. This allows modifying the coordinates in place.
 
<source lang="python">
xyz = cmd.get_coordset(objectname, 1, copy=0)
xyz[:] = newxyz
</source>
</source>


[[Category:Scripting_Script_Library|Get Coordinates I]]
[[Category:Script_Library|Get Coordinates I]]
[[Category:ObjSel_Scripts]]

Latest revision as of 20:40, 28 December 2015

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

Extract coordinates using get_coords

New in PyMOL 1.7.4. This is the fastest method to extract coordinates from a selection. It considers the object rotation matrix.

xyz = cmd.get_coords('sele', 1)

Extract coordinates using get_coordset

New in PyMOL 1.7.4. Operates on the object-state level, not on selections. Does not consider the object rotation matrix. Retrieves coordinates in original order (e.g. PDB file atom order), not in sorted atom order. Faster than get_coords.

xyz = cmd.get_coordset(objectname, 1)

Extract coordinates using get_model

Before 1.7.4, this was 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

Changed in PyMOL 1.7.3. Update selection coordinates from a Nx3 float array.

cmd.load_coords(xyz, selection)

Load coordinates using load_coordset

New in PyMOL 1.7.4. Update object state coordinates from a Nx3 float array. Can also append a state. Order of coordinates equivalent to get_coordset.

cmd.load_coordset(xyz, objectname, 1)

Load coordinates using get_coordset

New in PyMOL 1.7.4. The get_coordset function can also return a memory view instead of a copy. This allows modifying the coordinates in place.

xyz = cmd.get_coordset(objectname, 1, copy=0)
xyz[:] = newxyz