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::ReplicaExchange0).
15 class RestraintBase(object):
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, cls=restraint_set_class)
47 def set_label(self, label):
48 """Set the unique label used in outputs and particle/restraint names.
51 if self._label_is_set:
52 raise ValueError(
"Label has already been set.")
55 self._label_suffix =
""
57 self._label = str(label)
58 self._label_suffix =
"_" + self._label
59 self._label_is_set =
True
65 def set_weight(self, weight):
66 """Set the weight to apply to all internal restraints.
70 for rs
in self.restraint_sets:
71 rs.set_weight(self.weight)
73 def add_to_model(self):
74 """Add the restraint to the model."""
75 self._label_is_set =
True
76 for rs
in self.restraint_sets:
78 add_to_rmf=self._include_in_rmf)
81 """Evaluate the score of the restraint."""
82 self._label_is_set =
True
83 return self.weight * self.rs.unprotected_evaluate(
None)
85 def get_restraint_set(self):
86 """Get the primary restraint set."""
87 self._label_is_set =
True
90 def get_restraint(self):
91 """Get the primary restraint set. Identical to `get_restraint_set`."""
92 return self.get_restraint_set()
94 def get_restraint_for_rmf(self):
95 """Get the restraint for visualization in an RMF file."""
96 self._label_is_set =
True
99 def get_particles_to_sample(self):
100 """Get any created particles which should be sampled."""
101 self._label_is_set =
True
104 def get_output(self):
105 """Get outputs to write to stat files."""
107 score = self.evaluate()
108 output[
"_TotalScore"] = str(score)
110 suffix =
"_Score" + self._label_suffix
111 for rs
in self.restraint_sets:
112 out_name = rs.get_name() + suffix
113 output[out_name] = str(
114 self.weight * rs.unprotected_evaluate(
None))
117 def _create_restraint_set(self, name=None, cls=IMP.RestraintSet):
118 """Create ``IMP.RestraintSet``."""
122 name = self.name +
"_" + str(name)
123 rs = cls(self.model, name)
124 rs.set_weight(self.weight)
125 self.restraint_sets.append(rs)
126 rs.set_was_used(
True)
130 class _RestraintNuisanceMixin(object):
132 """Mix-in to add nuisance particle creation functionality to restraint.
134 This class must only be inherited if also inheriting
135 IMP.pmi.restraints.RestraintBase.
138 def __init__(self, *args, **kwargs):
139 super(_RestraintNuisanceMixin, self).__init__(*args, **kwargs)
140 self.sampled_nuisances = {}
143 def _create_nuisance(self, init_val, min_val, max_val, max_trans, name,
145 """Create nuisance particle.
146 @param init_val Initial value of nuisance
147 @param min_val Minimum value of nuisance
148 @param max_val Maximum value of nuisance
149 @param max_trans Maximum move to apply to nuisance
150 @param name Name of particle
151 @param is_sampled Nuisance is a sampled particle
152 \see IMP.pmi.tools.SetupNuisance
154 nuis = IMP.pmi.tools.SetupNuisance(
155 self.model, init_val, min_val, max_val,
156 isoptimized=is_sampled).get_particle()
157 nuis_name = self.name +
"_" + name
158 nuis.set_name(nuis_name)
159 self.nuisances[nuis_name] = nuis
161 self.sampled_nuisances[nuis_name] = (nuis, max_trans)
164 def get_particles_to_sample(self):
165 """Get any created particles which should be sampled."""
166 ps = super(_RestraintNuisanceMixin, self).get_particles_to_sample()
167 for name, (nuis, max_trans)
in self.sampled_nuisances.items():
168 ps[
"Nuisances_" + name + self._label_suffix] = ([nuis], max_trans)
171 def get_output(self):
172 """Get outputs to write to stat files."""
173 output = super(_RestraintNuisanceMixin, self).get_output()
174 for nuis_name, nuis
in self.nuisances.items():
175 output[nuis_name + self._label_suffix] = str(nuis.get_scale())
179 class _NuisancesBase(object):
181 """This base class is used to provide nuisance setup and interface
182 for the ISD cross-link restraints"""
184 sigma_dictionary = {}
187 def create_length(self):
188 """Create a nuisance on the length of the cross-link."""
190 self.lengthissampled =
True
191 lengthminnuis = 0.0000001
192 lengthmaxnuis = 1000.0
196 length = IMP.pmi.tools.SetupNuisance(self.m, lengthinit,
197 lengthminnuis, lengthmaxnuis,
200 self.rslen.add_restraint(
208 def create_sigma(self, resolution):
209 """Create a nuisance on the structural uncertainty."""
210 if isinstance(resolution, str):
213 sigmainit = resolution + 2.0
214 self.sigmaissampled =
True
215 sigmaminnuis = 0.0000001
216 sigmamaxnuis = 1000.0
220 sigma = IMP.pmi.tools.SetupNuisance(self.m, sigmainit, sigmaminnuis,
221 sigmamaxnuis, self.sigmaissampled
223 self.sigma_dictionary[resolution] = (
227 self.rssig.add_restraint(
236 def get_sigma(self, resolution):
237 """Get the nuisance on structural uncertainty."""
238 if resolution
not in self.sigma_dictionary:
239 self.create_sigma(resolution)
240 return self.sigma_dictionary[resolution]
242 def create_psi(self, value):
243 """Create a nuisance on the inconsistency."""
244 if isinstance(value, str):
248 self.psiissampled =
True
249 psiminnuis = 0.0000001
250 psimaxnuis = 0.4999999
254 psi = IMP.pmi.tools.SetupNuisance(self.m, psiinit,
255 psiminnuis, psimaxnuis,
256 self.psiissampled).get_particle()
257 self.psi_dictionary[value] = (
261 self.rspsi.add_restraint(
270 def get_psi(self, value):
271 """Get the nuisance on the inconsistency."""
272 if value
not in self.psi_dictionary:
273 self.create_psi(value)
274 return self.psi_dictionary[value]
Object used to hold a set of restraints.
Python classes to represent, score, sample and analyze models.