1 """@namespace IMP.pmi.restraints.basic
2 Some miscellaneous simple restraints.
5 from __future__
import print_function
17 """Restraint to keep all structures inside sphere."""
27 """Setup external barrier restraint.
28 @param representation DEPRECATED
29 @param radius Size of external barrier
30 @param hierarchies Can be one of the following inputs: IMP Hierarchy,
31 PMI System/State/Molecule/TempResidue, or a list/set of them
32 @param resolution Select which resolutions to act upon
33 @param weight Weight of restraint
34 @param center Center of the external barrier restraint
35 (IMP.algebra.Vector3D object)
36 @param label A unique label to be used in outputs and
37 particle/restraint names.
40 model = representation.prot.get_model()
43 resolution=resolution,
44 hierarchies=hierarchies)
46 hiers = IMP.pmi.tools.input_adaptor(hierarchies, resolution,
48 model = hiers[0].get_model()
49 particles = [h.get_particle()
for h
in hiers]
51 raise Exception(
"%s: must pass representation or hierarchies" % (
54 super(ExternalBarrier, self).
__init__(model, label=label,
60 elif type(center)
is IMP.algebra.Vector3D:
64 "%s: @param center must be an IMP.algebra.Vector3D object" % (
73 self.rs.add_restraint(r3)
78 """A simple distance restraint"""
82 tuple_selection1=
None,
83 tuple_selection2=
None,
91 """Setup distance restraint.
92 @param representation DEPRECATED
93 @param tuple_selection1 (resnum, resnum, molecule name, copy
95 @param tuple_selection2 (resnum, resnum, molecule name, copy
97 @param distancemin The minimum dist
98 @param distancemax The maximum dist
99 @param resolution For selecting particles
100 @param kappa The harmonic parameter
101 @param root_hier The hierarchy to select from (use this instead of
103 @param label A unique label to be used in outputs and
104 particle/restraint names
105 @param weight Weight of restraint
106 @note Pass the same resnum twice to each tuple_selection. Optionally
107 add a copy number (PMI2 only)
109 if tuple_selection1
is None or tuple_selection2
is None:
110 raise Exception(
"You must pass tuple_selection1/2")
114 if representation
and not root_hier:
115 model = representation.prot.get_model()
117 resolution=resolution,
118 name=tuple_selection1[2],
119 residue=tuple_selection1[0])
121 resolution=resolution,
122 name=tuple_selection2[2],
123 residue=tuple_selection2[0])
124 elif root_hier
and not representation:
125 model = root_hier.get_model()
127 if len(tuple_selection1) > 3:
128 copy_num1 = tuple_selection1[3]
130 if len(tuple_selection2) > 3:
131 copy_num2 = tuple_selection2[3]
134 resolution=resolution,
135 molecule=tuple_selection1[2],
136 residue_index=tuple_selection1[0],
137 copy_index=copy_num1)
138 particles1 = sel1.get_selected_particles()
140 resolution=resolution,
141 molecule=tuple_selection2[2],
142 residue_index=tuple_selection2[0],
143 copy_index=copy_num2)
144 particles2 = sel2.get_selected_particles()
146 raise Exception(
"Pass representation or root_hier, not both")
148 super(DistanceRestraint, self).
__init__(model, label=label,
152 print(
"Created distance restraint between "
153 "%s and %s" % (particles1[0].get_name(),
154 particles2[0].get_name()))
156 if len(particles1) > 1
or len(particles2) > 1:
157 raise ValueError(
"more than one particle selected")
159 self.rs.add_restraint(
163 self.rs.add_restraint(
170 "If you use this class please let the PMI developers know.")
173 def __init__(self, m, objects, resolution, angular_tolerance,label='None'):
174 IMP.Restraint.__init__(self, m,
"TorqueRestraint %1%")
175 self.softness_angle = 0.5
179 hierarchies = IMP.pmi.tools.input_adaptor(objects,
182 self.particles = [h.get_particle()
for h
in hierarchies]
185 self.at=angular_tolerance
187 def get_angle_probability(self,xyz,angle_center):
188 maxtheta=angle_center+self.at
189 mintheta=angle_center-self.at
190 angle=self.math.atan2(xyz.get_y(),xyz.get_x() )*180.0/self.math.pi
191 anglediff = (angle - maxtheta + 180 + 360) % 360 - 180
192 argvalue1=anglediff / self.softness_angle
193 anglediff = (angle - mintheta + 180 + 360) % 360 - 180
194 argvalue2=-anglediff / self.softness_angle
195 prob = (1.0-self.plateau) / (1.0 + self.math.exp(-max(argvalue1,argvalue2)))
198 def unprotected_evaluate(self, da):
201 angle_center=self.math.atan2(center[1],center[0])*180.0/self.math.pi
203 s+=-self.math.log(1.0-self.get_angle_probability(xyz,angle_center))
206 def do_get_inputs(self):
207 return self.particles
214 score = self.weight * self.unprotected_evaluate(
None)
215 output[
"_TotalScore"] = str(score)
216 output[
"TorqueRestraint_" + self.label] = str(score)
224 PMI2 python restraint. Restrains particles within a
225 Cylinder aligned along the z-axis and
227 Optionally, one can restrain the cylindrical angle
230 def __init__(self, m, objects, resolution, radius,mintheta=None,
231 maxtheta=
None,repulsive=
False,label=
'None'):
233 @param objects PMI2 objects
234 @param resolution the resolution you want the restraint to be applied
235 @param radius the radius of the cylinder
236 @param mintheta minimum cylindrical angle in degrees
237 @param maxtheta maximum cylindrical angle in degrees
239 IMP.Restraint.__init__(self, m,
"CylinderRestraint %1%")
242 self.softness_angle = 0.5
246 self.mintheta=mintheta
247 self.maxtheta=maxtheta
248 self.repulsive=repulsive
249 hierarchies = IMP.pmi.tools.input_adaptor(objects,
252 self.particles = [h.get_particle()
for h
in hierarchies]
255 def get_probability(self,p):
257 r=self.math.sqrt(xyz.get_x()**2+xyz.get_y()**2)
258 argvalue=(r-self.radius) / self.softness
259 if self.repulsive: argvalue=-argvalue
260 prob = (1.0 - self.plateau) / (1.0 + self.math.exp(-argvalue))
263 def get_angle_probability(self,p):
265 angle=self.math.atan2(xyz.get_y(),xyz.get_x() )*180.0/self.math.pi
266 anglediff = (angle - self.maxtheta + 180 + 360) % 360 - 180
267 argvalue1=anglediff / self.softness_angle
268 anglediff = (angle - self.mintheta + 180 + 360) % 360 - 180
269 argvalue2=-anglediff / self.softness_angle
270 prob = (1.0-self.plateau) / (1.0 + self.math.exp(-max(argvalue1,argvalue2)))
273 def unprotected_evaluate(self, da):
275 for p
in self.particles:
276 s+=-self.math.log(1.0-self.get_probability(p))
277 if self.mintheta
is not None and self.maxtheta
is not None:
278 s+=-self.math.log(1.0-self.get_angle_probability(p))
282 return self.particles
284 def add_to_model(self):
287 def get_output(self):
289 score = self.weight * self.unprotected_evaluate(
None)
290 output[
"_TotalScore"] = str(score)
291 output[
"CylinderRestraint_" + self.label] = str(score)
298 a python restraint with bistable potential
299 Authors: G. Bouvier, R. Pellarin. Pasteur Institute.
304 def __init__(self,m,p1,p2,dist1,dist2,sigma1,sigma2,weight1,weight2):
306 input twp particles, the two equilibrium distances, their amplitudes, and their weights (populations)
308 IMP.Restraint.__init__(self, m,
"BiStableDistanceRestraint %1%")
318 if self.weight1+self.weight2 != 1:
319 raise ValueError(
"The sum of the weights must be one")
323 self.particle_list=[p1,p2]
325 def gaussian(self,x, mu, sig, w):
326 return w*self.np.exp(-self.np.power(x - mu, 2.) / (2 * self.np.power(sig, 2.)))
328 def unprotected_evaluate(self,da):
330 prob=self.gaussian(dist,self.dist1,self.sigma1,self.weight1)+\
331 self.gaussian(dist,self.dist2,self.sigma2,self.weight2)
332 return -self.math.log(prob)
335 return self.particle_list
340 """Restraint for anchoring a particle to a specific coordinate."""
344 tuple_selection=
None,
352 """Setup distance restraint.
353 @param representation DEPRECATED
354 @param tuple_selection (resnum, resnum, molecule name,
356 @param anchor_point Point to which to restrain particle
357 (IMP.algebra.Vector3D object)
358 @param radius Size of the tolerance length
359 @param kappa The harmonic parameter
360 @param resolution For selecting a particle
361 @param weight Weight of restraint
362 @param root_hier The hierarchy to select from (use this instead of
364 @param label A unique label to be used in outputs and
365 particle/restraint names
366 @note Pass the same resnum twice to each tuple_selection. Optionally
367 add a copy number (PMI2 only)
369 if tuple_selection
is None:
370 raise ValueError(
"You must pass a tuple_selection")
372 if representation
and not root_hier:
373 model = representation.prot.get_model()
375 resolution=resolution,
376 name=tuple_selection[2],
377 residue=tuple_selection[0])
378 elif root_hier
and not representation:
379 model = root_hier.get_model()
381 if len(tuple_selection) > 3:
382 copy_num1 = tuple_selection[3]
385 resolution=resolution,
386 molecule=tuple_selection[2],
387 residue_index=tuple_selection[0],
388 copy_index=copy_num1)
389 ps = sel1.get_selected_particles()
391 raise ValueError(
"Pass representation or root_hier, not both")
393 raise ValueError(
"More than one particle selected")
395 super(DistanceToPointRestraint, self).
__init__(model, label=label,
400 if anchor_point
is None:
402 elif isinstance(anchor_point, IMP.algebra.Vector3D):
405 raise TypeError(
"anchor_point must be an algebra.Vector3D object")
412 self.rs.add_restraint(r3)
414 print(
"\n%s: Created distance_to_point_restraint between "
415 "%s and %s" % (self.name, ps[0].get_name(), c3))
426 plateau=0.0000000001,
430 """ Setup Membrane restraint
432 Simple sigmoid score calculated for particles above,
434 @param objects_inside list or tuples of objects in membrane (e.g. ['p1', (10, 30,'p2')])
435 @param objects_above list or tuples of objects above membrane
436 @param objects_below list or tuples of objects below membrane
437 @param thickness Thickness of the membrane along the z-axis
438 @param softness Softness of the limiter in the sigmoid function
439 @param plateau Parameter to set the probability (=1- plateau)) at the plateau
441 @param weight Weight of restraint
442 @param label A unique label to be used in outputs and
443 particle/restraint names.
444 input a list of particles, the slope and theta of the sigmoid potential
445 theta is the cutoff distance for a protein-protein contact
449 model = self.hier.get_model()
451 rname =
"MembraneRestraint"
452 super(MembraneRestraint, self).
__init__(
453 model, name=
"MembraneRestraint", label=label, weight=weight)
456 self.thickness = thickness
457 self.softness = softness
458 self.plateau = plateau
460 self.resolution = resolution
465 z_center.set_nuisance(self.center)
469 z_center.get_particle_index(),
477 for obj
in objects_above:
478 if isinstance(obj, tuple):
479 self.particles_above = self._select_from_tuple(obj)
481 elif isinstance(obj, str):
482 self.particles_above = self._select_from_string(obj)
483 mr.add_particles_above(self.particles_above)
487 for obj
in objects_inside:
488 if isinstance(obj, tuple):
489 self.particles_inside = self._select_from_tuple(obj)
491 elif isinstance(obj, str):
492 self.particles_inside = self._select_from_string(obj)
493 mr.add_particles_inside(self.particles_inside)
498 for obj
in objects_below:
499 if isinstance(obj, tuple):
500 self.particles_below = self._select_from_tuple(obj)
502 elif isinstance(obj, str):
503 self.particles_below = self._select_from_string(obj)
504 mr.add_particles_below(self.particles_below)
507 self.rs.add_restraint(mr)
509 def get_particles_above(self):
510 return self.particles_above
512 def get_particles_inside(self):
513 return self.particles_inside
515 def get_particles_below(self):
516 return self.particles_below
518 def _select_from_tuple(self, obj):
521 residue_indexes = range(obj[0], obj[1]+1, 1),
522 resolution = self.resolution).get_selected_particles()
526 def _select_from_string(self, obj):
529 resolution = self.resolution).get_selected_particles()
532 def create_membrane_density(self, file_out='membrane_localization.mrc'):
535 Just for visualization purposes.
536 Writes density of membrane localization
539 offset = 5.0*self.thickness
548 dheader.set_resolution(resolution)
551 for vox
in range(dmap.get_header().get_number_of_voxels()):
552 c = dmap.get_location_by_voxel(vox)
553 if self._is_membrane(c[2])==1:
554 dmap.set_value(c[0], c[1], c[2], 1.0)
556 dmap.set_value(c[0], c[1], c[2], 0.0)
558 IMP.em.write_map(dmap, file_out)
560 def _is_membrane(self, z):
561 if (z-self.center) < self.thickness/2.0
and (z-self.center) >= -self.thickness/2.0 :
Applies a SingletonScore to each Singleton in a list.
static Nuisance setup_particle(Model *m, ParticleIndex pi)
def __init__
Setup distance restraint.
A simple distance restraint.
Lower bound harmonic function (non-zero when feature < mean)
def add_to_model
Add the restraint to the model.
Various classes to hold sets of particles.
Upper bound harmonic function (non-zero when feature > mean)
algebra::Vector3D get_centroid(const XYZs &ps)
Get the centroid.
Distance restraint between two particles.
double get_distance(XYZR a, XYZR b)
Compute the sphere distance between a and b.
Classes to handle different kinds of restraints.
def __init__
input twp particles, the two equilibrium distances, their amplitudes, and their weights (populations)...
Class for sampling a density map from particles.
def __init__
Setup external barrier restraint.
Store a list of ParticleIndexes.
Restraint for anchoring a particle to a specific coordinate.
DensityHeader create_density_header(const algebra::BoundingBoxD< 3 > &bb, float spacing)
Create a header from a bounding box in 3D.
Apply a function to the distance to a fixed point.
A decorator for a particle with x,y,z coordinates.
Restraint to keep all structures inside sphere.
def deprecated_object
Python decorator to mark a class as deprecated.
def __init__
Setup distance restraint.
Basic functionality that is expected to be used by a wide variety of IMP users.
General purpose algebraic and geometric methods that are expected to be used by a wide variety of IMP...
The general base class for IMP exceptions.
Class to handle individual particles of a Model object.
Functionality for loading, creating, manipulating and scoring atomic structures.
a python restraint with bistable potential Authors: G.
Select hierarchy particles identified by the biological name.
def get_output
Get outputs to write to stat files.
virtual ModelObjectsTemp do_get_inputs() const =0
Base class for PMI restraints, which wrap IMP.Restraint(s).
A restraint is a term in an IMP ScoringFunction.