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