IMP logo
IMP Reference Guide  2.5.0
The Integrative Modeling Platform
dof.py
1 """@namespace IMP.pmi.dof
2  Handling of degrees of freedom.
3 """
4 
5 from __future__ import print_function
6 
7 class dof(object):
8  """Degrees of Freedom.
9  Tasks:
10  define rigid bodies and super rigid bodies
11  define symmetries (see representation.py)
12  create nuisances and their parameters
13  generate lists of MC or MD movers
14  """
15  def __init__(self,root,
16  maxtrans_rb=None,maxrot_rb=None,
17  maxtrans_srb=None,maxrot_srb=None):
18  self.rigid_bodies = []
19  self.fixed_rigid_bodies = []
20  self.floppy_bodies = []
21  self.super_rigid_bodies = []
22 
23  self.maxtrans_rb = 2.0
24  self.maxrot_rb = 0.04
25  self.maxtrans_srb = 2.0
26  self.maxrot_srb = 0.2
27  self.rigidbodiesarefixed = False
28  self.maxtrans_fb = 3.0
29  self.mdl = mdl
30 
31  def set_rigid_body_from_hierarchies(self, hiers, particles=None):
32  '''
33  This method allows the construction of a rigid body given a list
34  of hierarchies and or a list of particles.
35 
36  hiers: list of hierarchies
37  particles: (optional, default=None) list of particles to add to the rigid body
38  '''
39 
40  if particles is None:
41  rigid_parts = set()
42  else:
43  rigid_parts = set(particles)
44 
45  name = ""
46  print("set_rigid_body_from_hierarchies> setting up a new rigid body")
47  for hier in hiers:
48  ps = IMP.atom.get_leaves(hier)
49  for p in ps:
51  rb = IMP.core.RigidMember(p).get_rigid_body()
52  print("set_rigid_body_from_hierarchies> WARNING particle %s already belongs to rigid body %s" % (p.get_name(), rb.get_name()))
53  else:
54  rigid_parts.add(p)
55  name += hier.get_name() + "-"
56  print("set_rigid_body_from_hierarchies> adding %s to the rigid body" % hier.get_name())
57  rb = IMP.atom.create_rigid_body(list(rigid_parts))
58  rb.set_coordinates_are_optimized(True)
59  rb.set_name(name + "rigid_body")
60  self.rigid_bodies.append(rb)
61  return rb
62 
63  def set_rigid_bodies(self, subunits):
64  '''
65  This method allows the construction of a rigid body given a list
66  of tuples, that identify the residue ranges and the subunit names (the names used
67  to create the component by using create_component.
68 
69  subunits: [(name_1,(first_residue_1,last_residue_1)),(name_2,(first_residue_2,last_residue_2)),.....]
70  or
71  [name_1,name_2,(name_3,(first_residue_3,last_residue_3)),.....]
72 
73  example: ["prot1","prot2",("prot3",(1,10))]
74 
75  sometimes, we know about structure of an interaction
76  and here we make such PPIs rigid
77  '''
78 
79  rigid_parts = set()
80  for s in subunits:
81  if type(s) == type(tuple()) and len(s) == 2:
82  sel = IMP.atom.Selection(
83  self.prot,
84  molecule=s[0],
85  residue_indexes=list(range(s[1][0],
86  s[1][1] + 1)))
87  if len(sel.get_selected_particles()) == 0:
88  print("set_rigid_bodies: selected particle does not exists")
89  for p in sel.get_selected_particles():
90  # if not p in self.floppy_bodies:
92  rb = IMP.core.RigidMember(p).get_rigid_body()
93  print("set_rigid_body_from_hierarchies> WARNING particle %s already belongs to rigid body %s" % (p.get_name(), rb.get_name()))
94  else:
95  rigid_parts.add(p)
96 
97  elif type(s) == type(str()):
98  sel = IMP.atom.Selection(self.prot, molecule=s)
99  if len(sel.get_selected_particles()) == 0:
100  print("set_rigid_bodies: selected particle does not exists")
101  for p in sel.get_selected_particles():
102  # if not p in self.floppy_bodies:
104  rb = IMP.core.RigidMember(p).get_rigid_body()
105  print("set_rigid_body_from_hierarchies> WARNING particle %s already belongs to rigid body %s" % (p.get_name(), rb.get_name()))
106  else:
107  rigid_parts.add(p)
108 
109  rb = IMP.atom.create_rigid_body(list(rigid_parts))
110  rb.set_coordinates_are_optimized(True)
111  rb.set_name(''.join(str(subunits)) + "_rigid_body")
112  self.rigid_bodies.append(rb)
113  return rb
114 
115  def set_super_rigid_body_from_hierarchies(
116  self,
117  hiers,
118  axis=None,
119  min_size=0):
120  # axis is the rotation axis for 2D rotation
121  super_rigid_xyzs = set()
122  super_rigid_rbs = set()
123  name = ""
124  print("set_super_rigid_body_from_hierarchies> setting up a new SUPER rigid body")
125 
126  for hier in hiers:
127  ps = IMP.atom.get_leaves(hier)
128  for p in ps:
130  rb = IMP.core.RigidMember(p).get_rigid_body()
131  super_rigid_rbs.add(rb)
132  else:
133  super_rigid_xyzs.add(p)
134  print("set_rigid_body_from_hierarchies> adding %s to the rigid body" % hier.get_name())
135  if len(super_rigid_rbs) < min_size:
136  return
137  if axis is None:
138  self.super_rigid_bodies.append((super_rigid_xyzs, super_rigid_rbs))
139  else:
140  # these will be 2D rotation SRB
141  self.super_rigid_bodies.append(
142  (super_rigid_xyzs, super_rigid_rbs, axis))
143 
144  def fix_rigid_bodies(self, rigid_bodies):
145  self.fixed_rigid_bodies += rigid_bodies
146 
148  self,
149  hiers,
150  min_length=None,
151  max_length=None,
152  axis=None):
153  '''
154  this function takes a linear list of hierarchies (they are supposed
155  to be sequence-contiguous) and
156  produces a chain of super rigid bodies with given length range, specified
157  by min_length and max_length
158  '''
159  try:
160  hiers = IMP.pmi.tools.flatten_list(hiers)
161  except:
162  pass
163  for hs in IMP.pmi.tools.sublist_iterator(hiers, min_length, max_length):
164  self.set_super_rigid_body_from_hierarchies(hs, axis, min_length)
165 
166  def set_super_rigid_bodies(self, subunits, coords=None):
167  super_rigid_xyzs = set()
168  super_rigid_rbs = set()
169 
170  for s in subunits:
171  if type(s) == type(tuple()) and len(s) == 3:
172  sel = IMP.atom.Selection(
173  self.prot,
174  molecule=s[2],
175  residue_indexes=list(range(s[0],
176  s[1] + 1)))
177  if len(sel.get_selected_particles()) == 0:
178  print("set_rigid_bodies: selected particle does not exists")
179  for p in sel.get_selected_particles():
181  rb = IMP.core.RigidMember(p).get_rigid_body()
182  super_rigid_rbs.add(rb)
183  else:
184  super_rigid_xyzs.add(p)
185  elif type(s) == type(str()):
186  sel = IMP.atom.Selection(self.prot, molecule=s)
187  if len(sel.get_selected_particles()) == 0:
188  print("set_rigid_bodies: selected particle does not exists")
189  for p in sel.get_selected_particles():
190  # if not p in self.floppy_bodies:
192  rb = IMP.core.RigidMember(p).get_rigid_body()
193  super_rigid_rbs.add(rb)
194  else:
195  super_rigid_xyzs.add(p)
196  self.super_rigid_bodies.append((super_rigid_xyzs, super_rigid_rbs))
197 
198  def remove_floppy_bodies(self, hierarchies):
199  '''
200  give a list of hierarchies, get the leaves and remove the corresponding particles
201  from the floppy bodies list. We need this function because sometimes
202  we want to constraint the floppy bodies in a rigid body. For instance
203  when you want to associate a bead with a density particle.
204  '''
205  for h in hierarchies:
206  ps = IMP.atom.get_leaves(h)
207  for p in ps:
208  if p in self.floppy_bodies:
209  print("remove_floppy_bodies: removing %s from floppy body list" % p.get_name())
210  self.floppy_bodies.remove(p)
211 
212  def set_floppy_bodies(self):
213  for p in self.floppy_bodies:
214  name = p.get_name()
215  p.set_name(name + "_floppy_body")
217  print("I'm trying to make this particle flexible although it was assigned to a rigid body", p.get_name())
218  rb = IMP.core.RigidMember(p).get_rigid_body()
219  try:
220  rb.set_is_rigid_member(p.get_index(), False)
221  except:
222  # some IMP versions still work with that
223  rb.set_is_rigid_member(p.get_particle_index(), False)
224  p.set_name(p.get_name() + "_rigid_body_member")
225 
226  def set_floppy_bodies_from_hierarchies(self, hiers):
227  for hier in hiers:
228  ps = IMP.atom.get_leaves(hier)
229  for p in ps:
230  IMP.core.XYZ(p).set_coordinates_are_optimized(True)
231  self.floppy_bodies.append(p)
232 
233  def get_particles_to_sample(self):
234  # get the list of samplable particles with their type
235  # and the mover displacement. Everything wrapped in a dictionary,
236  # to be used by samplers modules
237  ps = {}
238 
239  # remove symmetric particles: they are not sampled
240  rbtmp = []
241  fbtmp = []
242  srbtmp = []
243  if not self.rigidbodiesarefixed:
244  for rb in self.rigid_bodies:
246  if IMP.pmi.Symmetric(rb).get_symmetric() != 1:
247  rbtmp.append(rb)
248  else:
249  if rb not in self.fixed_rigid_bodies:
250  rbtmp.append(rb)
251 
252  for fb in self.floppy_bodies:
254  if IMP.pmi.Symmetric(fb).get_symmetric() != 1:
255  fbtmp.append(fb)
256  else:
257  fbtmp.append(fb)
258 
259  for srb in self.super_rigid_bodies:
260  # going to prune the fixed rigid bodies out
261  # of the super rigid body list
262  rigid_bodies = list(srb[1])
263  filtered_rigid_bodies = []
264  for rb in rigid_bodies:
265  if rb not in self.fixed_rigid_bodies:
266  filtered_rigid_bodies.append(rb)
267  srbtmp.append((srb[0], filtered_rigid_bodies))
268 
269  self.rigid_bodies = rbtmp
270  self.floppy_bodies = fbtmp
271  self.super_rigid_bodies = srbtmp
272 
273  ps["Rigid_Bodies_SimplifiedModel"] = (
274  self.rigid_bodies,
275  self.maxtrans_rb,
276  self.maxrot_rb)
277  ps["Floppy_Bodies_SimplifiedModel"] = (
278  self.floppy_bodies,
279  self.maxtrans_fb)
280  ps["SR_Bodies_SimplifiedModel"] = (
281  self.super_rigid_bodies,
282  self.maxtrans_srb,
283  self.maxrot_srb)
284  return ps
def remove_floppy_bodies
give a list of hierarchies, get the leaves and remove the corresponding particles from the floppy bod...
Definition: dof.py:198
Add symmetric attribute to a particle.
Definition: Symmetric.h:23
static bool get_is_setup(const IMP::ParticleAdaptor &p)
Definition: rigid_bodies.h:469
def set_rigid_bodies
This method allows the construction of a rigid body given a list of tuples, that identify the residue...
Definition: dof.py:63
def set_rigid_body_from_hierarchies
This method allows the construction of a rigid body given a list of hierarchies and or a list of part...
Definition: dof.py:31
static bool get_is_setup(Model *m, ParticleIndex pi)
Definition: Symmetric.h:29
A decorator for a particle with x,y,z coordinates.
Definition: XYZ.h:30
def set_chain_of_super_rigid_bodies
this function takes a linear list of hierarchies (they are supposed to be sequence-contiguous) and pr...
Definition: dof.py:147
IMP::core::RigidBody create_rigid_body(Hierarchy h)
Hierarchies get_leaves(const Selection &h)
Select hierarchy particles identified by the biological name.
Definition: Selection.h:65
def sublist_iterator
Yield all sublists of length >= lmin and <= lmax.
Definition: tools.py:1055