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)
45 """Set the unique label used in outputs and particle/restraint names.
48 if self._label_is_set:
49 raise ValueError(
"Label has already been set.")
52 self._label_suffix =
""
54 self._label = str(label)
55 self._label_suffix =
"_" + self._label
56 self._label_is_set =
True
63 """Set the weight to apply to all internal restraints.
67 for rs
in self.restraint_sets:
68 rs.set_weight(self.weight)
71 """Add the restraint to the model."""
72 self._label_is_set =
True
73 for rs
in self.restraint_sets:
77 """Evaluate the score of the restraint."""
78 self._label_is_set =
True
79 return self.weight * self.rs.unprotected_evaluate(
None)
82 """Get the primary restraint set."""
83 self._label_is_set =
True
87 """Get the primary restraint set. Identical to `get_restraint_set`."""
91 """Get the restraint for visualization in an RMF file."""
92 self._label_is_set =
True
96 """Get any created particles which should be sampled."""
97 self._label_is_set =
True
101 """Get outputs to write to stat files."""
105 output[
"_TotalScore"] = str(score)
107 suffix =
"_Score" + self._label_suffix
108 for rs
in self.restraint_sets:
109 out_name = rs.get_name() + suffix
110 output[out_name] = str(
111 self.weight * rs.unprotected_evaluate(
None))
114 def _create_restraint_set(self, name=None):
115 """Create ``IMP.RestraintSet``."""
119 name = self.name +
"_" + str(name)
121 rs.set_weight(self.weight)
122 self.restraint_sets.append(rs)
126 class _RestraintNuisanceMixin(object):
128 """Mix-in to add nuisance particle creation functionality to restraint.
130 This class must only be inherited if also inheriting
131 IMP.pmi.restraints.RestraintBase.
134 def __init__(self, *args, **kwargs):
135 super(_RestraintNuisanceMixin, self).
__init__(*args, **kwargs)
136 self.sampled_nuisances = {}
139 def _create_nuisance(self, init_val, min_val, max_val, max_trans, name,
141 """Create nuisance particle.
142 @param init_val Initial value of nuisance
143 @param min_val Minimum value of nuisance
144 @param max_val Maximum value of nuisance
145 @param max_trans Maximum move to apply to nuisance
146 @param name Name of particle
147 @param is_sampled Nuisance is a sampled particle
148 \see IMP.pmi.tools.SetupNuisance
150 nuis = IMP.pmi.tools.SetupNuisance(
151 self.m, init_val, min_val, max_val,
152 isoptimized=is_sampled).get_particle()
153 nuis_name = self.name +
"_" + name
154 nuis.set_name(nuis_name)
155 self.nuisances[nuis_name] = nuis
157 self.sampled_nuisances[nuis_name] = (nuis, max_trans)
161 """Get any created particles which should be sampled."""
163 for name, (nuis, max_trans)
in self.sampled_nuisances.items():
164 ps[
"Nuisances_" + name + self._label_suffix] = ([nuis], max_trans)
168 """Get outputs to write to stat files."""
169 output = super(_RestraintNuisanceMixin, self).
get_output()
170 for nuis_name, nuis
in self.nuisances.items():
171 output[nuis_name + self._label_suffix] = str(nuis.get_scale())
175 class _NuisancesBase(object):
177 """This base class is used to provide nuisance setup and interface
178 for the ISD cross-link restraints"""
180 sigma_dictionary = {}
183 def create_length(self):
184 """Create a nuisance on the length of the cross-link."""
186 self.lengthissampled =
True
187 lengthminnuis = 0.0000001
188 lengthmaxnuis = 1000.0
192 length = IMP.pmi.tools.SetupNuisance(self.m, lengthinit,
193 lengthminnuis, lengthmaxnuis,
196 self.rslen.add_restraint(
204 def create_sigma(self, resolution):
205 """Create a nuisance on the structural uncertainty."""
206 if isinstance(resolution, str):
209 sigmainit = resolution + 2.0
210 self.sigmaissampled =
True
211 sigmaminnuis = 0.0000001
212 sigmamaxnuis = 1000.0
216 sigma = IMP.pmi.tools.SetupNuisance(self.m, sigmainit, sigmaminnuis,
217 sigmamaxnuis, self.sigmaissampled
219 self.sigma_dictionary[resolution] = (
223 self.rssig.add_restraint(
232 def get_sigma(self, resolution):
233 """Get the nuisance on structural uncertainty."""
234 if resolution
not in self.sigma_dictionary:
235 self.create_sigma(resolution)
236 return self.sigma_dictionary[resolution]
238 def create_psi(self, value):
239 """Create a nuisance on the inconsistency."""
240 if isinstance(value, str):
244 self.psiissampled =
True
245 psiminnuis = 0.0000001
246 psimaxnuis = 0.4999999
250 psi = IMP.pmi.tools.SetupNuisance(self.m, psiinit,
251 psiminnuis, psimaxnuis,
252 self.psiissampled).get_particle()
253 self.psi_dictionary[value] = (
257 self.rspsi.add_restraint(
266 def get_psi(self, value):
267 """Get the nuisance on the inconsistency."""
268 if value
not in self.psi_dictionary:
269 self.create_psi(value)
270 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 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).