> From: Kristian Rother 

> In this and other recent questions inside and outside this 
> forum i have 
> discovered that many of them could be solved quite easy if 
> there was an 
> easy way to access the atomic coordinates directly from the PyMOL API.
> 
> Is this achievable somehow?

The actual C-langauge arrays aren't exposed, but there are at least three 
different ways you can modify coordinates from within Python:

##### 
(1) You can get a python object which contains the molecular information, 
modify the coordinates in that object, load the modified molecule into PyMOL, 
update the modified coordinates to the original model, and then delete the 
modified object.

(in a python script)

from pymol import cmd

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")

######
(2) Another approach is the "alter_state" function, which can perform the same 
transformation in a single PyMOL command statement:

alter_state 1,pept,(x,y)=(-y,x)

Likewise sub-selections can be transformed as well:

alter_state 1,(pept and name ca),(x,y,z)=(x+5,y,z)

#######
(3) A third approach is to use alter_state with the global "stored" object:

(in Python script)

from pymol import cmd
from pymol import stored

stored.xyz = []
cmd.iterate_state(1,"pept","stored.xyz.append([x,y,z])")

# at this point, stored.xyz is a native Python array holding
# the coordinates, which you can modify as required

stored.xyz = map(lambda v:[-v[1],v[0],v[2]],stored.xyz)

# and now you can update the internal coordinate sets

cmd.alter_state(1,"pept","(x,y,z)=stored.xyz.pop(0)")

#####

Approaches 2 gives the best performance, approach 3 gives more flexibility, and 
approach 1 gives you a reusable and fully modifiable Python object.

Cheers,
Warren


Reply via email to