1 """@namespace IMP.pmi.restraints
2 Classes to handle different kinds of restraints.
4 PMI restraints generally wrap IMP restraints. Typical features in PMI restraints are:
5 - Easy setup: for example, you can usually create one with a PMI [Molecule](@ref IMP::pmi::topology::Molecule) or a slice from one.
6 - Fast setup from data files. For example you can set up the [CrossLinkingMassSpectrometryRestraint](@ref IMP::pmi::restraints::crosslinking::CrossLinkingMassSpectrometryRestraint) by reading in a cross-link file into a [database](@ref IMP::pmi::io::crosslink::CrossLinkDataBase).
7 - Useful output: reporting functions which are put into log files when running [ReplicaExchange](@ref IMP::pmi::macros::ReplicaExchange).
16 _include_in_rmf =
False
18 """Base class for PMI restraints, which wrap `IMP.Restraint`(s)."""
20 def __init__(self, m, name=None, label=None, weight=1.,
23 @param m The model object
24 @param name The name of the primary restraint set that is wrapped.
25 This is used for outputs and particle/restraint names
26 and should be set by the child class.
27 @param label A unique label to be used in outputs and
28 particle/restraint names.
29 @param weight The weight to apply to all internal restraints.
30 @param restraint_set_class The class to use for the restraint set
33 self.restraint_sets = []
34 self._label_is_set =
False
37 self._label_suffix =
""
41 self.name = self.__class__.__name__
45 self.rs = self._create_restraint_set(name=
None,
46 cls=restraint_set_class)
48 def set_label(self, label):
49 """Set the unique label used in outputs and particle/restraint names.
52 if self._label_is_set:
53 raise ValueError(
"Label has already been set, or restraint has "
54 "already been added to model.")
57 self._label_suffix =
""
59 self._label = str(label)
60 self._label_suffix =
"_" + self._label
61 self._label_is_set =
True
67 def set_weight(self, weight):
68 """Set the weight to apply to all internal restraints.
72 for rs
in self.restraint_sets:
73 rs.set_weight(self.weight)
75 def add_to_model(self):
76 """Add the restraint to the model."""
77 self._label_is_set =
True
78 for rs
in self.restraint_sets:
80 self.model, rs, add_to_rmf=self._include_in_rmf)
83 """Evaluate the score of the restraint."""
84 self._label_is_set =
True
85 return self.weight * self.rs.unprotected_evaluate(
None)
87 def get_restraint_set(self):
88 """Get the primary restraint set."""
89 self._label_is_set =
True
92 def get_restraint(self):
93 """Get the primary restraint set. Identical to `get_restraint_set`."""
94 return self.get_restraint_set()
96 def get_restraint_for_rmf(self):
97 """Get the restraint for visualization in an RMF file."""
98 self._label_is_set =
True
101 def get_particles_to_sample(self):
102 """Get any created particles which should be sampled."""
103 self._label_is_set =
True
106 def get_output(self):
107 """Get outputs to write to stat files."""
109 score = self.evaluate()
110 output[
"_TotalScore"] = str(score)
112 suffix =
"_Score" + self._label_suffix
113 for rs
in self.restraint_sets:
114 out_name = rs.get_name() + suffix
115 output[out_name] = str(
116 self.weight * rs.unprotected_evaluate(
None))
119 def _create_restraint_set(self, name=None, cls=IMP.RestraintSet):
120 """Create ``IMP.RestraintSet``."""
124 name = self.name +
"_" + str(name)
125 rs = cls(self.model, name)
126 rs.set_weight(self.weight)
127 self.restraint_sets.append(rs)
128 rs.set_was_used(
True)
132 class _RestraintNuisanceMixin:
134 """Mix-in to add nuisance particle creation functionality to restraint.
136 This class must only be inherited if also inheriting
137 IMP.pmi.restraints.RestraintBase.
140 def __init__(self, *args, **kwargs):
141 super().__init__(*args, **kwargs)
142 self.sampled_nuisances = {}
145 def _create_nuisance(self, init_val, min_val, max_val, max_trans, name,
147 """Create nuisance particle.
148 @param init_val Initial value of nuisance
149 @param min_val Minimum value of nuisance
150 @param max_val Maximum value of nuisance
151 @param max_trans Maximum move to apply to nuisance
152 @param name Name of particle
153 @param is_sampled Nuisance is a sampled particle
154 @see IMP.pmi.tools.SetupNuisance
156 nuis = IMP.pmi.tools.SetupNuisance(
157 self.model, init_val, min_val, max_val,
158 isoptimized=is_sampled).get_particle()
159 nuis_name = self.name +
"_" + name
160 nuis.set_name(nuis_name)
161 self.nuisances[nuis_name] = nuis
163 self.sampled_nuisances[nuis_name] = (nuis, max_trans)
166 def get_particles_to_sample(self):
167 """Get any created particles which should be sampled."""
168 ps = super().get_particles_to_sample()
169 for name, (nuis, max_trans)
in self.sampled_nuisances.items():
170 ps[
"Nuisances_" + name + self._label_suffix] = ([nuis], max_trans)
173 def get_output(self):
174 """Get outputs to write to stat files."""
175 output = super().get_output()
176 for nuis_name, nuis
in self.nuisances.items():
177 output[nuis_name + self._label_suffix] = str(nuis.get_scale())
181 class _NuisancesBase:
183 """This base class is used to provide nuisance setup and interface
184 for the ISD cross-link restraints"""
186 sigma_dictionary = {}
189 def create_length(self):
190 """Create a nuisance on the length of the cross-link."""
192 self.lengthissampled =
True
193 lengthminnuis = 0.0000001
194 lengthmaxnuis = 1000.0
197 length = IMP.pmi.tools.SetupNuisance(self.m, lengthinit,
198 lengthminnuis, lengthmaxnuis,
201 self.rslen.add_restraint(
209 def create_sigma(self, resolution):
210 """Create a nuisance on the structural uncertainty."""
211 if isinstance(resolution, str):
214 sigmainit = resolution + 2.0
215 self.sigmaissampled =
True
216 sigmaminnuis = 0.0000001
217 sigmamaxnuis = 1000.0
221 sigma = IMP.pmi.tools.SetupNuisance(self.m, sigmainit, sigmaminnuis,
222 sigmamaxnuis, self.sigmaissampled
224 self.sigma_dictionary[resolution] = (
228 self.rssig.add_restraint(
237 def get_sigma(self, resolution):
238 """Get the nuisance on structural uncertainty."""
239 if resolution
not in self.sigma_dictionary:
240 self.create_sigma(resolution)
241 return self.sigma_dictionary[resolution]
243 def create_psi(self, value):
244 """Create a nuisance on the inconsistency."""
245 if isinstance(value, str):
249 self.psiissampled =
True
250 psiminnuis = 0.0000001
251 psimaxnuis = 0.4999999
255 psi = IMP.pmi.tools.SetupNuisance(self.m, psiinit,
256 psiminnuis, psimaxnuis,
257 self.psiissampled).get_particle()
258 self.psi_dictionary[value] = (
262 self.rspsi.add_restraint(
271 def get_psi(self, value):
272 """Get the nuisance on the inconsistency."""
273 if value
not in self.psi_dictionary:
274 self.create_psi(value)
275 return self.psi_dictionary[value]
Object used to hold a set of restraints.
Python classes to represent, score, sample and analyze models.