OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
the_hammer.py

Dynamic recalculation of clash score for a moving object. The real-valued clash score is then color-mapped onto the objects.

1 from PyQt4 import QtCore
2 import math
3 # remove all objects from scene, just in case
4 scene.RemoveAll()
5 
6 class Anim(QtCore.QTimer):
7  def __init__(self, a, b):
8  QtCore.QTimer.__init__(self)
9  self.a=a
10  self.b=b
11  self.angle=0.0
12  self.dir=0.01
13  self.edi=self.a.view.handle.EditXCS(mol.UNBUFFERED_EDIT)
14  QtCore.QObject.connect(self, QtCore.SIGNAL("timeout()"), self.OnTimer)
15 
16 
17  def OnTimer(self):
18  self.angle+=self.dir
19  if self.angle>math.pi/2:
20  self.dir=-self.dir
21  if self.angle<0:
22  self.dir=-self.dir
23  rot=geom.AxisRotation(geom.Vec3(0.0, 0.0, -1.0), self.angle)
24  self.edi.SetTransform(geom.Mat4(rot))
25  self.edi.UpdateICS()
26  for a in self.b.view.atoms:
27  score=mol.alg.ClashScore(a.handle, self.a.view)
28  a.SetFloatProp('clash', score)
29  self.a.UpdatePositions()
30  self.b.ReapplyColorOps()
31 
32 
33 def Hammer(off=geom.Vec3()):
34  ent=mol.CreateEntity()
35  edi=ent.EditXCS(mol.BUFFERED_EDIT)
36  chain=edi.InsertChain("A")
37  res=edi.AppendResidue(chain, "QUAD")
38  a=edi.InsertAtom(res, "A", off+geom.Vec3(0.0, 0.0, 0.0), "H")
39  b=edi.InsertAtom(res, "B", off+geom.Vec3(0.0, 4.0, 0.0), "H")
40  c=edi.InsertAtom(res, "C", off+geom.Vec3(2.0, 4.0, 0.0), "H")
41  d=edi.InsertAtom(res, "D", off+geom.Vec3(2.0, 5.0, 0.0), "H")
42  e=edi.InsertAtom(res, "E", off+geom.Vec3(-2.0, 5.0, 0.0), "H")
43  f=edi.InsertAtom(res, "F", off+geom.Vec3(-2.0, 4.0, 0.0), "H")
44  edi.Connect(a, b)
45  edi.Connect(b, c)
46  edi.Connect(c, d)
47  edi.Connect(d, e)
48  edi.Connect(e, f)
49  edi.Connect(f, b)
50  return ent
51 
52 def TheWall():
53  ent=mol.CreateEntity()
54  edi=ent.EditXCS(mol.BUFFERED_EDIT)
55  chain=edi.InsertChain("A")
56  res=edi.AppendResidue(chain, "QUAD")
57  index=0
58  for i in range(-10, 10):
59  for j in range(-10, 10):
60  index+=1
61  atom=edi.InsertAtom(res, "A%d" % index, geom.Vec3(4.0, -2.0, 0.0)+
62  geom.Vec3(i, 0, j)*0.25)
63  atom.radius=0.25
64 
65  return ent
66 
67 a=Hammer()
68 b=TheWall()
69 
70 a_go=gfx.Entity("a", gfx.CUSTOM, a)
71 b_go=gfx.Entity("b", gfx.CUSTOM, b)
72 for a in a.atoms:
73  a.SetFloatProp('clash', 0.0)
74 
75 scene.Add(a_go)
76 scene.Add(b_go)
77 
78 anim=Anim(a_go, b_go)
79 anim.start(20)
80 grad=gfx.Gradient()
81 grad.SetColorAt(0.0, gfx.Color(0.0, 1.0, 0.0))
82 grad.SetColorAt(0.5, gfx.Color(1.0, 0.0, 1.0))
83 grad.SetColorAt(1.0, gfx.Color(1.0, 0.0, 0.0))
84 b_go.ColorBy('clash', gfx.Color(0,1,0), gfx.Color(1,0,0), 0.0, 10.0, mol.Prop.Level.ATOM)
85 scene.CenterOn(b_go)
86 print 'Demo 7: Illustrating a clash score'