IMP logo
IMP Reference Guide  develop.d97d4ead1f,2024/11/21
The Integrative Modeling Platform
AVNetworkRestraint.py
1 """Restraints for handling distances between accessible volumes.
2 """
3 
4 import pathlib
5 
6 import typing
7 
8 import IMP
9 import IMP.core
10 import IMP.algebra
11 import IMP.atom
12 import IMP.container
13 import IMP.bayesianem
14 import IMP.isd
15 import IMP.pmi.tools
16 import IMP.pmi.restraints
17 
18 import IMP.bff
19 import IMP.bff.tools
20 
21 
22 class AVMeanDistanceRestraint(IMP.Restraint):
23 
24  def __init__(
25  self,
26  m: IMP.Model,
27  av1: IMP.bff.AV, av2: IMP.bff.AV,
29  sigma: float,
30  weight: float = 1.0
31  ):
32  IMP.Restraint.__init__(self, m, "BiStableDistanceRestraint %1%")
33  self.dist = dist
34  self.av1 = av1
35  self.av2 = av2
36 
37  self.sigma = sigma
38  self.weight = weight
39  self.d1 = IMP.core.XYZ(av1)
40  self.d2 = IMP.core.XYZ(av2)
41  forster_radius = dist.forster_radius
42  distance_range = (1, 2.5 * forster_radius)
43  self.dc = IMP.bff.tools.FRETDistanceConverter(
44  forster_radius=forster_radius,
45  sigma=sigma,
46  distance_range=distance_range
47  )
48  self.particle_list = [av1.get_particle(), av2.get_particle()]
49 
50  def unprotected_evaluate(self, da):
51  d_exp = self.dist
52  av1 = self.av1
53  av2 = self.av2
54  r: IMP.algebra.Vector3D = \
55  IMP.core.XYZ(av1).get_coordinates() - \
56  IMP.core.XYZ(av2).get_coordinates()
57  d_mp = r.get_magnitude()
58  d_mod = self.dc(d_mp, d_exp.distance_type)
59  score = d_exp.score_model(d_mod)
60  return score
61 
62  def do_get_inputs(self):
63  return self.particle_list
64 
65 
66 class AVNetworkRestraintWrapper(IMP.pmi.restraints.RestraintBase):
67 
68  @staticmethod
69  def add_used_dyes_to_rb(used_avs: typing.List[IMP.bff.AV]):
70  for dk in used_avs:
71  dye = used_avs[dk]
72  p_dye = dye.get_particle()
73  # The coordinates of an AV are the mean AV the density map. Thus, the position of the
74  # AV changes when the AV is resampled.
75  dye.resample()
76 
77  # dye_xyz = IMP.core.XYZ(p_dye)
78  # print("Adding dye to RB:")
79  # print("-- Position key:", dk)
80  # print("-- Label parameter:", dye)
81  # print("-- Mean dye position:", dye_xyz)
82 
83  p_att = dye.get_source()
85  rbm = IMP.core.RigidBodyMember(p_att)
86  rb: IMP.core.RigidBody = rbm.get_rigid_body()
87  rb.add_member(p_dye)
88 
89  def add_xyz_mass_to_avs(self):
90  """
91  IMP_NEW(Particle,p2,(m,"p2"));
92  IMP::core::XYZR d2=IMP::core::XYZR::setup_particle(
93  m,p2->get_index(),IMP::algebra::Sphere3D(
94  IMP::algebra::Vector3D(1.0,4.0,6.0),1.0));
95  atom::Mass mm2 = atom::Mass::setup_particle(p2, 30.0);
96  rbps.push_back(d2);
97  """
98  for ak in self.used_avs:
99  av: IMP.bff.AV = self.used_avs[ak]
100  r_mean = max(av.get_radii())
101  # add radius
103  av_d.set_radius(r_mean * 1.0)
104  # add Mass
105  av_m = IMP.atom.Mass.setup_particle(av, 0.1)
106  av_m.set_mass(r_mean * 2.0)
107 
108  """Restraint for Accessible Volume (AV) decorated particles
109 
110  The AVs of the decorated particles are recomputed when the
111  score is evaluated. Computing an AV (searching for the points
112  that are accessible is computationally costly (expensive
113  restraint).
114  """
115  def __init__(
116  self,
117  hier: IMP.atom.Hierarchy,
118  fps_json_fn: str,
119  score_set: str = "",
120  weight: float = 1.0,
121  mean_position_restraint: bool = False,
122  sigma_DA: float = 6.0,
123  label: str = "AVNetworkRestraint",
124  occupy_volume: bool = True
125  ):
126  """
127 
128  :param hier:
129  :param fps_json_fn:
130  :param score_set:
131  :param weight:
132  :param mean_position_restraint:
133  :param sigma_DA:
134  :param label:
135  :param occupy_volume:
136  """
137  # some parameters
138  m = hier.get_model()
139  self.mdl: IMP.Model = m
140  self.hier: IMP.atom.Hierarchy = hier
141  super(AVNetworkRestraintWrapper, self).__init__(m, label=label, weight=weight)
142 
143  self.model_ps = []
144  self.model_ps += [k.get_particle() for k in IMP.atom.get_leaves(hier)]
145 
146  name = self.name
147  self.mean_position_restraint = mean_position_restraint
148  if pathlib.Path(fps_json_fn).is_file():
149  self.av_network_restraint = IMP.bff.AVNetworkRestraint(
150  hier,
151  fps_json_fn,
152  name,
153  score_set
154  )
155  else:
156  raise FileNotFoundError("{}".format(fps_json_fn))
157  self.rs = IMP.RestraintSet(m, 'AVNetworkRestraint')
158  self.used_avs = dict([(v.get_name(), v) for v in self.av_network_restraint.get_used_avs()])
159  if not self.mean_position_restraint:
160  self.rs.add_restraint(self.av_network_restraint)
161  else:
162  self.used_distances = self.av_network_restraint.get_used_distances()
163  self.add_used_dyes_to_rb(self.used_avs)
164  for dk in self.used_distances:
165  d_exp = self.used_distances[dk]
166  av1 = self.used_avs[d_exp.position_1]
167  av2 = self.used_avs[d_exp.position_2]
168  r = AVMeanDistanceRestraint(m, av1, av2, d_exp, sigma=sigma_DA)
169  self.rs.add_restraint(r)
170  if occupy_volume:
171  self.add_xyz_mass_to_avs()
172  self.set_weight(weight)
173 
174  def evaluate(self):
175  """Evaluate the score of the restraint."""
176  return self.rs.unprotected_evaluate(None) * self.weight
177 
178  def add_to_model(self, add_to_rmf=True):
179  IMP.pmi.tools.add_restraint_to_model(self.mdl, self.rs,
180  add_to_rmf=add_to_rmf)
A decorator for a particle with accessible volume (AV).
Definition: AV.h:94
Restraints for handling electron microscopy maps.
A member of a rigid body, it has internal (local) coordinates.
Definition: rigid_bodies.h:634
static bool get_is_setup(const IMP::ParticleAdaptor &p)
Definition: rigid_bodies.h:635
Various classes to hold sets of particles.
static XYZR setup_particle(Model *m, ParticleIndex pi)
Definition: XYZR.h:48
Miscellaneous utilities.
Definition: tools.py:1
Container for experimental distance measurement.
Definition: AV.h:54
A restraint that uses an annotated volumetric network to score particle distances.
Object used to hold a set of restraints.
Definition: RestraintSet.h:41
Class for storing model, its restraints, constraints, and particles.
Definition: Model.h:86
Classes to handle different kinds of restraints.
The standard decorator for manipulating molecular structures.
static Mass setup_particle(Model *m, ParticleIndex pi, Float mass)
Definition: Mass.h:48
def add_restraint_to_model
Add a PMI restraint to the model.
Definition: tools.py:84
A decorator for a particle with x,y,z coordinates.
Definition: XYZ.h:30
Utility functions.
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...
A decorator for a rigid body.
Definition: rigid_bodies.h:82
Functionality for loading, creating, manipulating and scoring atomic structures.
Hierarchies get_leaves(const Selection &h)
Bayesian Fluorescence Framework.
Inferential scoring building on methods developed as part of the Inferential Structure Determination ...
A restraint is a term in an IMP ScoringFunction.
Definition: Restraint.h:56