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 crosslink 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::ReplicaExchange0).
17 """Base class for PMI restraints, which wrap `IMP.Restraint`(s)."""
19 def __init__(self, m, name=None, label=None, weight=1.):
21 @param m The model object
22 @param name The name of the primary restraint set that is wrapped.
23 This is used for outputs and particle/restraint names
24 and should be set by the child class.
25 @param label A unique label to be used in outputs and
26 particle/restraint names.
27 @param weight The weight to apply to all internal restraints.
30 self.restraint_sets = []
31 self._label_is_set =
False
34 self._label_suffix =
""
38 self.name = self.__class__.__name__
42 self.rs = self._create_restraint_set(name=
None)
55 """Set the unique label used in outputs and particle/restraint names.
58 if self._label_is_set:
59 raise ValueError(
"Label has already been set.")
62 self._label_suffix =
""
64 self._label = str(label)
65 self._label_suffix =
"_" + self._label
66 self._label_is_set =
True
73 """Set the weight to apply to all internal restraints.
77 for rs
in self.restraint_sets:
78 rs.set_weight(self.weight)
81 """Add the restraint to the model."""
82 self._label_is_set =
True
83 for rs
in self.restraint_sets:
87 """Evaluate the score of the restraint."""
88 self._label_is_set =
True
89 return self.weight * self.rs.unprotected_evaluate(
None)
92 """Get the primary restraint set."""
93 self._label_is_set =
True
97 """Get the primary restraint set. Identical to `get_restraint_set`."""
101 """Get the restraint for visualization in an RMF file."""
102 self._label_is_set =
True
106 """Get any created particles which should be sampled."""
107 self._label_is_set =
True
111 """Get outputs to write to stat files."""
114 output[
"_TotalScore"] = str(score)
116 suffix =
"_Score" + self._label_suffix
117 for rs
in self.restraint_sets:
118 out_name = rs.get_name() + suffix
119 output[out_name] = str(
120 self.weight * rs.unprotected_evaluate(
None))
123 def _create_restraint_set(self, name=None):
124 """Create ``IMP.RestraintSet``."""
128 name = self.name +
"_" + str(name)
130 rs.set_weight(self.weight)
131 self.restraint_sets.append(rs)
132 rs.set_was_used(
True)
136 class _RestraintNuisanceMixin(object):
138 """Mix-in to add nuisance particle creation functionality to restraint.
140 This class must only be inherited if also inheriting
141 IMP.pmi.restraints.RestraintBase.
144 def __init__(self, *args, **kwargs):
145 super(_RestraintNuisanceMixin, self).
__init__(*args, **kwargs)
146 self.sampled_nuisances = {}
149 def _create_nuisance(self, init_val, min_val, max_val, max_trans, name,
151 """Create nuisance particle.
152 @param init_val Initial value of nuisance
153 @param min_val Minimum value of nuisance
154 @param max_val Maximum value of nuisance
155 @param max_trans Maximum move to apply to nuisance
156 @param name Name of particle
157 @param is_sampled Nuisance is a sampled particle
158 \see IMP.pmi.tools.SetupNuisance
160 nuis = IMP.pmi.tools.SetupNuisance(
161 self.model, init_val, min_val, max_val,
162 isoptimized=is_sampled).get_particle()
163 nuis_name = self.name +
"_" + name
164 nuis.set_name(nuis_name)
165 self.nuisances[nuis_name] = nuis
167 self.sampled_nuisances[nuis_name] = (nuis, max_trans)
171 """Get any created particles which should be sampled."""
173 for name, (nuis, max_trans)
in self.sampled_nuisances.items():
174 ps[
"Nuisances_" + name + self._label_suffix] = ([nuis], max_trans)
178 """Get outputs to write to stat files."""
179 output = super(_RestraintNuisanceMixin, self).
get_output()
180 for nuis_name, nuis
in self.nuisances.items():
181 output[nuis_name + self._label_suffix] = str(nuis.get_scale())
185 class _NuisancesBase(object):
187 """This base class is used to provide nuisance setup and interface
188 for the ISD cross-link restraints"""
190 sigma_dictionary = {}
193 def create_length(self):
194 """Create a nuisance on the length of the cross-link."""
196 self.lengthissampled =
True
197 lengthminnuis = 0.0000001
198 lengthmaxnuis = 1000.0
202 length = IMP.pmi.tools.SetupNuisance(self.m, lengthinit,
203 lengthminnuis, lengthmaxnuis,
206 self.rslen.add_restraint(
214 def create_sigma(self, resolution):
215 """Create a nuisance on the structural uncertainty."""
216 if isinstance(resolution, str):
219 sigmainit = resolution + 2.0
220 self.sigmaissampled =
True
221 sigmaminnuis = 0.0000001
222 sigmamaxnuis = 1000.0
226 sigma = IMP.pmi.tools.SetupNuisance(self.m, sigmainit, sigmaminnuis,
227 sigmamaxnuis, self.sigmaissampled
229 self.sigma_dictionary[resolution] = (
233 self.rssig.add_restraint(
242 def get_sigma(self, resolution):
243 """Get the nuisance on structural uncertainty."""
244 if resolution
not in self.sigma_dictionary:
245 self.create_sigma(resolution)
246 return self.sigma_dictionary[resolution]
248 def create_psi(self, value):
249 """Create a nuisance on the inconsistency."""
250 if isinstance(value, str):
254 self.psiissampled =
True
255 psiminnuis = 0.0000001
256 psimaxnuis = 0.4999999
260 psi = IMP.pmi.tools.SetupNuisance(self.m, psiinit,
261 psiminnuis, psimaxnuis,
262 self.psiissampled).get_particle()
263 self.psi_dictionary[value] = (
267 self.rspsi.add_restraint(
276 def get_psi(self, value):
277 """Get the nuisance on the inconsistency."""
278 if value
not in self.psi_dictionary:
279 self.create_psi(value)
280 return self.psi_dictionary[value]
def evaluate
Evaluate the score of the restraint.
def add_to_model
Add the restraint to the model.
def set_weight
Set the weight to apply to all internal restraints.
def get_particles_to_sample
Get any created particles which should be sampled.
def get_restraint_for_rmf
Get the restraint for visualization in an RMF file.
Object used to hold a set of restraints.
def deprecated_method
Python decorator to mark a method as deprecated.
def get_restraint_set
Get the primary restraint set.
def set_label
Set the unique label used in outputs and particle/restraint names.
def get_restraint
Get the primary restraint set.
Python classes to represent, score, sample and analyze models.
def get_output
Get outputs to write to stat files.
Base class for PMI restraints, which wrap IMP.Restraint(s).