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)
123 rs.set_was_used(
True)
127 class _RestraintNuisanceMixin(object):
129 """Mix-in to add nuisance particle creation functionality to restraint.
131 This class must only be inherited if also inheriting
132 IMP.pmi.restraints.RestraintBase.
135 def __init__(self, *args, **kwargs):
136 super(_RestraintNuisanceMixin, self).
__init__(*args, **kwargs)
137 self.sampled_nuisances = {}
140 def _create_nuisance(self, init_val, min_val, max_val, max_trans, name,
142 """Create nuisance particle.
143 @param init_val Initial value of nuisance
144 @param min_val Minimum value of nuisance
145 @param max_val Maximum value of nuisance
146 @param max_trans Maximum move to apply to nuisance
147 @param name Name of particle
148 @param is_sampled Nuisance is a sampled particle
149 \see IMP.pmi.tools.SetupNuisance
151 nuis = IMP.pmi.tools.SetupNuisance(
152 self.m, init_val, min_val, max_val,
153 isoptimized=is_sampled).get_particle()
154 nuis_name = self.name +
"_" + name
155 nuis.set_name(nuis_name)
156 self.nuisances[nuis_name] = nuis
158 self.sampled_nuisances[nuis_name] = (nuis, max_trans)
162 """Get any created particles which should be sampled."""
164 for name, (nuis, max_trans)
in self.sampled_nuisances.items():
165 ps[
"Nuisances_" + name + self._label_suffix] = ([nuis], max_trans)
169 """Get outputs to write to stat files."""
170 output = super(_RestraintNuisanceMixin, self).
get_output()
171 for nuis_name, nuis
in self.nuisances.items():
172 output[nuis_name + self._label_suffix] = str(nuis.get_scale())
176 class _NuisancesBase(object):
178 """This base class is used to provide nuisance setup and interface
179 for the ISD cross-link restraints"""
181 sigma_dictionary = {}
184 def create_length(self):
185 """Create a nuisance on the length of the cross-link."""
187 self.lengthissampled =
True
188 lengthminnuis = 0.0000001
189 lengthmaxnuis = 1000.0
193 length = IMP.pmi.tools.SetupNuisance(self.m, lengthinit,
194 lengthminnuis, lengthmaxnuis,
197 self.rslen.add_restraint(
205 def create_sigma(self, resolution):
206 """Create a nuisance on the structural uncertainty."""
207 if isinstance(resolution, str):
210 sigmainit = resolution + 2.0
211 self.sigmaissampled =
True
212 sigmaminnuis = 0.0000001
213 sigmamaxnuis = 1000.0
217 sigma = IMP.pmi.tools.SetupNuisance(self.m, sigmainit, sigmaminnuis,
218 sigmamaxnuis, self.sigmaissampled
220 self.sigma_dictionary[resolution] = (
224 self.rssig.add_restraint(
233 def get_sigma(self, resolution):
234 """Get the nuisance on structural uncertainty."""
235 if resolution
not in self.sigma_dictionary:
236 self.create_sigma(resolution)
237 return self.sigma_dictionary[resolution]
239 def create_psi(self, value):
240 """Create a nuisance on the inconsistency."""
241 if isinstance(value, str):
245 self.psiissampled =
True
246 psiminnuis = 0.0000001
247 psimaxnuis = 0.4999999
251 psi = IMP.pmi.tools.SetupNuisance(self.m, psiinit,
252 psiminnuis, psimaxnuis,
253 self.psiissampled).get_particle()
254 self.psi_dictionary[value] = (
258 self.rspsi.add_restraint(
267 def get_psi(self, value):
268 """Get the nuisance on the inconsistency."""
269 if value
not in self.psi_dictionary:
270 self.create_psi(value)
271 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).