File: chempy_model02.py

package info (click to toggle)
pymol 2.4.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 43,312 kB
  • sloc: cpp: 480,106; python: 79,860; ansic: 28,343; javascript: 6,792; sh: 47; makefile: 30; csh: 8
file content (69 lines) | stat: -rw-r--r-- 2,120 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from pymol import cmd
from random import random
import time

# this shows how you can efficiently update the coordinates
# of an existing model for real-time viewing

# for asychronous execution,  you want to run this using the "spawn"
# command from inside PyMOL or with the "-l" option from the unix shell

# WARNING: if you do not run this using "spawn" or the "-l" option,
# PyMOL will freeze, will not refresh the workspace, and might be difficult
# to recover.

# To run this script so that you can stop it, run with the global option:
#
# spawn chempy_model02.py, global
#
# Then you have access to the "cont" variable, and can stop the loop by
# simply setting it:
#
# cont = False
#

cont = False
# first we need a model
if os.path.exists("$PYMOL_PATH/test/dat/pept.pdb"):
    cmd.load("$PYMOL_PATH/test/dat/pept.pdb","demo")
else:
    from inspect import getsourcefile
    current_file_dir = os.path.dirname(os.path.abspath(getsourcefile(lambda:0)))
    cmd.load(os.path.join(current_file_dir, "../../test/dat/pept.pdb"), "demo")

# let's dress it up a little bit 

cmd.show("sticks","demo")

cmd.show("spheres","resi 10")

cmd.color("yellow","resi 5 and element C")

# turn off some of the chatter about reloading the object...

cmd.feedback("disable","executive","actions")

# now loop, updating the coordinates and reloading the model into
# state 1 of the "demo" object

m = cmd.get_model()
while cont:
   time.sleep(0.05)
   try:
      cmd.set("suspend_updates","1") # only necessary if multithreading...
      for a in m.atom:
         a.coord[0]+=(random()-0.5)*0.1
         a.coord[1]+=(random()-0.5)*0.1
         a.coord[2]+=(random()-0.5)*0.1
      cmd.load_model(m,"demo",1)
      cmd.set("suspend_updates","0") # only necessary if multithreading...
   except:
      cmd.set("suspend_updates","0") # only necessary if multithreading...
      traceback.print_exc()
   cmd.refresh()

# Summary: this is portable, safe, but inefficient.  For real-time visualization
# of coordinate changes, there is a way to do this by passing in an opaque
# C data structure...

# Cheers, warren@delanoscientific.com