1 """@namespace IMP.pmi.restraints.basic
2 Some miscellaneous simple restraints.
5 from __future__
import print_function
17 """Keeps all structures inside a sphere."""
19 def __init__(self, hierarchies, radius=10.0, resolution=10, weight=1.0,
20 center=
None, label=
None):
21 """Setup external barrier restraint.
22 @param hierarchies Can be one of the following inputs: IMP Hierarchy,
23 PMI System/State/Molecule/TempResidue, or a list/set of them
24 @param radius Size of external barrier
25 @param resolution Select which resolutions to act upon
26 @param weight Weight of restraint
27 @param center Center of the external barrier
28 (IMP.algebra.Vector3D object)
29 @param label A unique label to be used in outputs and
30 particle/restraint names.
34 model = hiers[0].get_model()
35 particles = [h.get_particle()
for h
in hiers]
37 super(ExternalBarrier, self).
__init__(model, label=label,
43 elif type(center)
is IMP.algebra.Vector3D:
47 "%s: @param center must be an IMP.algebra.Vector3D object" % (
56 self.rs.add_restraint(r3)
60 """A simple distance restraint"""
62 def __init__(self, root_hier, tuple_selection1, tuple_selection2,
63 distancemin=0, distancemax=100, resolution=1.0, kappa=1.0,
64 label=
None, weight=1.):
65 """Setup distance restraint.
66 @param root_hier The hierarchy to select from
67 @param tuple_selection1 (resnum, resnum, molecule name, copy
69 @param tuple_selection2 (resnum, resnum, molecule name, copy
71 @param distancemin The minimum dist
72 @param distancemax The maximum dist
73 @param resolution For selecting particles
74 @param kappa The harmonic parameter
75 @param label A unique label to be used in outputs and
76 particle/restraint names
77 @param weight Weight of restraint
78 @note Pass the same resnum twice to each tuple_selection. Optionally
84 model = root_hier.get_model()
86 if len(tuple_selection1) > 3:
87 copy_num1 = tuple_selection1[3]
89 if len(tuple_selection2) > 3:
90 copy_num2 = tuple_selection2[3]
93 resolution=resolution,
94 molecule=tuple_selection1[2],
95 residue_index=tuple_selection1[0],
97 particles1 = sel1.get_selected_particles()
99 resolution=resolution,
100 molecule=tuple_selection2[2],
101 residue_index=tuple_selection2[0],
102 copy_index=copy_num2)
103 particles2 = sel2.get_selected_particles()
105 super(DistanceRestraint, self).
__init__(model, label=label,
109 print(
"Created distance restraint between "
110 "%s and %s" % (particles1[0].get_name(),
111 particles2[0].get_name()))
113 if len(particles1) > 1
or len(particles2) > 1:
114 raise ValueError(
"more than one particle selected")
116 self.rs.add_restraint(
120 self.rs.add_restraint(
127 """Restrain particles within (or outside) a cylinder.
128 The cylinder is aligned along the z-axis and with center x=y=0.
129 Optionally, one can restrain the cylindrical angle
133 def __init__(self, m, objects, resolution, radius, mintheta=None,
134 maxtheta=
None, repulsive=
False, label=
'None'):
136 @param objects PMI2 objects to restrain
137 @param resolution the resolution you want the restraint to be applied
138 @param radius the radius of the cylinder
139 @param mintheta minimum cylindrical angle in degrees
140 @param maxtheta maximum cylindrical angle in degrees
141 @param repulsive If True, restrain the particles to be outside
142 of the cylinder instead of inside
143 @param label A unique label to be used in outputs and
144 particle/restraint names
146 IMP.Restraint.__init__(self, m,
"CylinderRestraint %1%")
149 self.softness_angle = 0.5
153 self.mintheta = mintheta
154 self.maxtheta = maxtheta
155 self.repulsive = repulsive
159 self.particles = [h.get_particle()
for h
in hierarchies]
162 def get_probability(self, p):
164 r = self.math.sqrt(xyz.get_x()**2+xyz.get_y()**2)
165 argvalue = (r-self.radius) / self.softness
168 prob = (1.0 - self.plateau) / (1.0 + self.math.exp(-argvalue))
171 def get_angle_probability(self, p):
173 angle = self.math.atan2(xyz.get_y(), xyz.get_x())*180.0/self.math.pi
174 anglediff = (angle - self.maxtheta + 180 + 360) % 360 - 180
175 argvalue1 = anglediff / self.softness_angle
176 anglediff = (angle - self.mintheta + 180 + 360) % 360 - 180
177 argvalue2 = -anglediff / self.softness_angle
178 prob = ((1.0-self.plateau)
179 / (1.0 + self.math.exp(-max(argvalue1, argvalue2))))
184 for p
in self.particles:
185 s += -self.math.log(1.0-self.get_probability(p))
186 if self.mintheta
is not None and self.maxtheta
is not None:
187 s += -self.math.log(1.0-self.get_angle_probability(p))
191 return self.particles
193 def add_to_model(self):
196 def get_output(self):
199 output[
"_TotalScore"] = str(score)
200 output[
"CylinderRestraint_" + self.label] = str(score)
205 '''Distance restraint with bistable potential
206 Authors: G. Bouvier, R. Pellarin. Pasteur Institute.
211 def __init__(self, m, p1, p2, dist1, dist2, sigma1, sigma2, weight1,
214 input two particles, the two equilibrium distances, their amplitudes,
215 and their weights (populations)
217 IMP.Restraint.__init__(self, m,
"BiStableDistanceRestraint %1%")
224 self.weight1 = weight1
225 self.weight2 = weight2
227 if self.weight1+self.weight2 != 1:
228 raise ValueError(
"The sum of the weights must be one")
232 self.particle_list = [p1, p2]
234 def gaussian(self, x, mu, sig, w):
235 return (w*self.np.exp(-self.np.power(x - mu, 2.)
236 / (2 * self.np.power(sig, 2.))))
240 prob = self.gaussian(dist, self.dist1, self.sigma1, self.weight1) + \
241 self.gaussian(dist, self.dist2, self.sigma2, self.weight2)
242 return -self.math.log(prob)
245 return self.particle_list
249 """Anchor a particle to a specific coordinate."""
251 def __init__(self, root_hier, tuple_selection,
253 radius=10.0, kappa=10.0, resolution=1.0, weight=1.0,
255 """Setup distance restraint.
256 @param root_hier The hierarchy to select from
257 @param tuple_selection (resnum, resnum, molecule name,
259 @param anchor_point Point to which to restrain particle
260 (IMP.algebra.Vector3D object)
261 @param radius Maximum distance the particle can move from the
262 fixed point before it is restrained
263 @param kappa Strength of the harmonic restraint for point-particle
264 distance greater than the radius
265 @param resolution For selecting a particle
266 @param weight Weight of restraint
267 @param label A unique label to be used in outputs and
268 particle/restraint names
269 @note Pass the same resnum twice to each tuple_selection. Optionally
272 model = root_hier.get_model()
274 if len(tuple_selection) > 3:
275 copy_num1 = tuple_selection[3]
278 resolution=resolution,
279 molecule=tuple_selection[2],
280 residue_index=tuple_selection[0],
281 copy_index=copy_num1)
282 ps = sel1.get_selected_particles()
284 raise ValueError(
"More than one particle selected")
286 super(DistanceToPointRestraint, self).
__init__(model, label=label,
291 if anchor_point
is None:
293 elif isinstance(anchor_point, IMP.algebra.Vector3D):
296 raise TypeError(
"anchor_point must be an algebra.Vector3D object")
303 self.rs.add_restraint(r3)
305 print(
"\n%s: Created distance_to_point_restraint between "
306 "%s and %s" % (self.name, ps[0].get_name(), c3))
310 """Restrain particles to be above, below, or inside a planar membrane.
311 The membrane is defined to lie on the xy plane with a given z
312 coordinate and thickness, and particles are restrained (by their
313 z coordinates) with a simple sigmoid score.
316 def __init__(self, hier, objects_above=None, objects_inside=None,
317 objects_below=
None, center=0.0, thickness=30.0,
318 softness=3.0, plateau=0.0000000001, resolution=1,
319 weight=1.0, label=
None):
320 """Setup the restraint.
322 @param objects_inside list or tuples of objects in membrane
323 (e.g. ['p1', (10, 30,'p2')])
324 @param objects_above list or tuples of objects above membrane
325 @param objects_below list or tuples of objects below membrane
326 @param thickness Thickness of the membrane along the z-axis
327 @param softness Softness of the limiter in the sigmoid function
328 @param plateau Parameter to set the probability (=1- plateau))
329 at the plateau phase of the sigmoid
330 @param weight Weight of restraint
331 @param label A unique label to be used in outputs and
332 particle/restraint names.
336 model = self.hier.get_model()
338 super(MembraneRestraint, self).
__init__(
339 model, name=
"MembraneRestraint", label=label, weight=weight)
342 self.thickness = thickness
343 self.softness = softness
344 self.plateau = plateau
346 self.resolution = resolution
351 z_center.set_nuisance(self.center)
355 z_center.get_particle_index(),
363 for obj
in objects_above:
364 if isinstance(obj, tuple):
365 self.particles_above = self._select_from_tuple(obj)
367 elif isinstance(obj, str):
368 self.particles_above = self._select_from_string(obj)
369 mr.add_particles_above(self.particles_above)
373 for obj
in objects_inside:
374 if isinstance(obj, tuple):
375 self.particles_inside = self._select_from_tuple(obj)
377 elif isinstance(obj, str):
378 self.particles_inside = self._select_from_string(obj)
379 mr.add_particles_inside(self.particles_inside)
383 for obj
in objects_below:
384 if isinstance(obj, tuple):
385 self.particles_below = self._select_from_tuple(obj)
387 elif isinstance(obj, str):
388 self.particles_below = self._select_from_string(obj)
389 mr.add_particles_below(self.particles_below)
391 self.rs.add_restraint(mr)
393 def get_particles_above(self):
394 return self.particles_above
396 def get_particles_inside(self):
397 return self.particles_inside
399 def get_particles_below(self):
400 return self.particles_below
402 def _select_from_tuple(self, obj):
404 self.hier, molecule=obj[2],
405 residue_indexes=range(obj[0], obj[1]+1, 1),
406 resolution=self.resolution).get_selected_particles()
410 def _select_from_string(self, obj):
412 self.hier, molecule=obj,
413 resolution=self.resolution).get_selected_particles()
417 """Create an MRC density file to visualize the membrane."""
418 offset = 5.0 * self.thickness
425 -self.center - offset),
427 self.center + offset))
429 dheader.set_resolution(resolution)
432 for vox
in range(dmap.get_header().get_number_of_voxels()):
433 c = dmap.get_location_by_voxel(vox)
434 if self._is_membrane(c[2]) == 1:
435 dmap.set_value(c[0], c[1], c[2], 1.0)
437 dmap.set_value(c[0], c[1], c[2], 0.0)
439 IMP.em.write_map(dmap, file_out)
441 def _is_membrane(self, z):
442 if ((z-self.center) < self.thickness/2.0
and
443 (z-self.center) >= -self.thickness/2.0):
450 """Restrain residue/residues to bind to unknown location in a target"""
452 def __init__(self, hier, selection, cutoff=6., sigma=3., xi=0.01,
453 resolution=1.0, weight=1.0, label=
None):
456 @param hier Hierarchy of the system
457 @param selection Selection of residues and target;
458 syntax is (prot, r1, r2, target_prot) or
459 (prot1, r1, r2, target_prot, target_r1, target_r2)
460 @param cutoff Distance cutoff between selected segment and target
462 @param sigma Distance variance between selected fragments
463 @param xi Slope of a distance-linear scoring function that
464 funnels the score when the particles are too
466 @param resolution Resolution at which to apply restraint
467 @param weight Weight of the restraint
468 @param label Extra text to label the restraint so that it is
469 searchable in the output
472 m = self.hier.get_model()
474 super(ResidueProteinProximityRestraint, self).
__init__(
475 m, name=
"ResidueProteinProximityRestraint", label=label,
481 self.resolution = resolution
484 print(
'selection', selection, isinstance(selection, tuple))
485 if (
not isinstance(selection, tuple)
486 and not isinstance(selection, list)):
487 raise ValueError(
"Selection should be a tuple or list")
488 if len(selection) < 4:
490 "Selection should be (prot, r1, r2, target_prot) or "
491 "(prot1, r1, r2, target_prot, target_r1, target_r2)")
494 self.prot1 = selection[0]
495 self.r1 = int(selection[1])
496 self.r2 = int(selection[2])
498 self.prot2 = selection[3]
499 if len(selection) == 6:
500 self.tr1 = int(selection[4])
501 self.tr2 = int(selection[5])
503 if self.r1 == self.r2:
505 self.hier, molecule=self.prot1, residue_index=self.r1,
506 resolution=self.resolution).get_selected_particles()
509 self.hier, molecule=self.prot1,
510 residue_indexes=range(self.r1, self.r2+1, 1),
511 resolution=self.resolution).get_selected_particles()
513 if len(selection) == 4:
515 self.hier, molecule=self.prot2,
516 resolution=self.resolution).get_selected_particles()
518 elif len(selection) == 6:
520 self.hier, molecule=self.prot2,
521 residue_indexes=range(self.tr1, self.tr2+1, 1),
522 resolution=self.resolution).get_selected_particles()
524 self.included_ps = sel_resi + sel_target
531 m, self.cutoff, self.sigma, self.xi,
True,
532 'ResidueProteinProximityRestraint')
534 print(
'Selected fragment and target lengths:', len(sel_resi),
550 br.add_pairs_container(self.cpc)
552 br.add_contribution_particles(sel_resi, sel_target)
555 yi = ((cutoff**2/(2*sigma**2)
556 - math.log(1/math.sqrt(2*math.pi*sigma*sigma))+cutoff*xi/2.)
558 interpolation_factor = -(cutoff/2.)*(xi-yi)
559 max_p = (math.exp(-((distance+slack)**2)/(2*sigma**2))
560 / math.sqrt(2*math.pi*sigma*sigma))
561 max_score = -math.log(max_p)
565 br.set_interpolation_factor(interpolation_factor)
566 br.set_max_score(max_score)
568 self.rs.add_restraint(br)
570 self.restraint_sets = [self.rs] + self.restraint_sets[1:]
573 """ Get particles in the close pair container """
574 return self.cpc.get_indexes()
576 def get_output(self):
578 score = self.weight * self.rs.unprotected_evaluate(
None)
579 output[
"ResidueProteinProximityRestraint_score_" + self.label] \
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)
Various classes to hold sets of particles.
Upper bound harmonic function (non-zero when feature > mean)
virtual double unprotected_evaluate(DerivativeAccumulator *da) const
Return the unweighted score for the restraint.
def __init__
Setup the restraint.
Return all spatially-proximal pairs of particles (a,b) from the two SingletonContainers A and B...
Distance restraint between two particles.
double get_distance(XYZR a, XYZR b)
Compute the sphere distance between a and b.
Restrain residue/residues to bind to unknown location in a target.
Classes to handle different kinds of restraints.
def __init__
input two particles, the two equilibrium distances, their amplitudes, and their weights (populations)...
Class for sampling a density map from particles.
ParticleIndexPairs get_indexes(const ParticlePairsTemp &ps)
Get the indexes from a list of particle pairs.
def __init__
Setup external barrier restraint.
Store a list of ParticleIndexes.
Anchor a particle to a specific coordinate.
def create_membrane_density
Create an MRC density file to visualize the membrane.
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.
Restrain particles to be above, below, or inside a planar membrane.
Keeps all structures inside a sphere.
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...
def get_container_pairs
Get particles in the close pair container.
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.
Distance restraint with bistable potential Authors: G.
Select hierarchy particles identified by the biological name.
virtual ModelObjectsTemp do_get_inputs() const =0
Restrain particles within (or outside) a cylinder.
A restraint is a term in an IMP ScoringFunction.