IMP logo
IMP Reference Guide  2.11.0
The Integrative Modeling Platform
pmi/restraints/__init__.py
1 """@namespace IMP.pmi.restraints
2  Classes to handle different kinds of restraints.
3 
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).
8 """
9 
10 import IMP
11 import IMP.pmi
12 import IMP.pmi.tools
13 
14 
15 class RestraintBase(object):
16 
17  """Base class for PMI restraints, which wrap `IMP.Restraint`(s)."""
18 
19  def __init__(self, m, name=None, label=None, weight=1.):
20  """Constructor.
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.
28  """
29  self.model = m
30  self.restraint_sets = []
31  self._label_is_set = False
32  self.weight = weight
33  self._label = None
34  self._label_suffix = ""
35  self.set_label(label)
36 
37  if not name:
38  self.name = self.__class__.__name__
39  else:
40  self.name = str(name)
41 
42  self.rs = self._create_restraint_set(name=None)
43 
44  def set_label(self, label):
45  """Set the unique label used in outputs and particle/restraint names.
46  @param label Label
47  """
48  if self._label_is_set:
49  raise ValueError("Label has already been set.")
50  if not label:
51  self._label = ""
52  self._label_suffix = ""
53  else:
54  self._label = str(label)
55  self._label_suffix = "_" + self._label
56  self._label_is_set = True
57 
58  @property
59  def label(self):
60  return self._label
61 
62  def set_weight(self, weight):
63  """Set the weight to apply to all internal restraints.
64  @param weight Weight
65  """
66  self.weight = weight
67  for rs in self.restraint_sets:
68  rs.set_weight(self.weight)
69 
70  def add_to_model(self):
71  """Add the restraint to the model."""
72  self._label_is_set = True
73  for rs in self.restraint_sets:
75 
76  def evaluate(self):
77  """Evaluate the score of the restraint."""
78  self._label_is_set = True
79  return self.weight * self.rs.unprotected_evaluate(None)
80 
81  def get_restraint_set(self):
82  """Get the primary restraint set."""
83  self._label_is_set = True
84  return self.rs
85 
86  def get_restraint(self):
87  """Get the primary restraint set. Identical to `get_restraint_set`."""
88  return self.get_restraint_set()
89 
91  """Get the restraint for visualization in an RMF file."""
92  self._label_is_set = True
93  return self.rs
94 
96  """Get any created particles which should be sampled."""
97  self._label_is_set = True
98  return {}
99 
100  def get_output(self):
101  """Get outputs to write to stat files."""
102  output = {}
103  score = self.evaluate()
104  output["_TotalScore"] = str(score)
105 
106  suffix = "_Score" + self._label_suffix
107  for rs in self.restraint_sets:
108  out_name = rs.get_name() + suffix
109  output[out_name] = str(
110  self.weight * rs.unprotected_evaluate(None))
111  return output
112 
113  def _create_restraint_set(self, name=None):
114  """Create ``IMP.RestraintSet``."""
115  if not name:
116  name = self.name
117  else:
118  name = self.name + "_" + str(name)
119  rs = IMP.RestraintSet(self.model, name)
120  rs.set_weight(self.weight)
121  self.restraint_sets.append(rs)
122  rs.set_was_used(True)
123  return rs
124 
125 
126 class _RestraintNuisanceMixin(object):
127 
128  """Mix-in to add nuisance particle creation functionality to restraint.
129 
130  This class must only be inherited if also inheriting
131  IMP.pmi.restraints.RestraintBase.
132  """
133 
134  def __init__(self, *args, **kwargs):
135  super(_RestraintNuisanceMixin, self).__init__(*args, **kwargs)
136  self.sampled_nuisances = {}
137  self.nuisances = {}
138 
139  def _create_nuisance(self, init_val, min_val, max_val, max_trans, name,
140  is_sampled=False):
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
149  """
150  nuis = IMP.pmi.tools.SetupNuisance(
151  self.model, 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
156  if is_sampled:
157  self.sampled_nuisances[nuis_name] = (nuis, max_trans)
158  return nuis
159 
160  def get_particles_to_sample(self):
161  """Get any created particles which should be sampled."""
162  ps = super(_RestraintNuisanceMixin, self).get_particles_to_sample()
163  for name, (nuis, max_trans) in self.sampled_nuisances.items():
164  ps["Nuisances_" + name + self._label_suffix] = ([nuis], max_trans)
165  return ps
166 
167  def get_output(self):
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())
172  return output
173 
174 
175 class _NuisancesBase(object):
176 
177  """This base class is used to provide nuisance setup and interface
178  for the ISD cross-link restraints"""
179 
180  sigma_dictionary = {}
181  psi_dictionary = {}
182 
183  def create_length(self):
184  """Create a nuisance on the length of the cross-link."""
185  lengthinit = 10.0
186  self.lengthissampled = True
187  lengthminnuis = 0.0000001
188  lengthmaxnuis = 1000.0
189  lengthmin = 6.0
190  lengthmax = 30.0
191  lengthtrans = 0.2
192  length = IMP.pmi.tools.SetupNuisance(self.m, lengthinit,
193  lengthminnuis, lengthmaxnuis,
194  self.lengthissampled
195  ).get_particle()
196  self.rslen.add_restraint(
198  self.m,
199  length,
200  1000000000.0,
201  lengthmax,
202  lengthmin))
203 
204  def create_sigma(self, resolution):
205  """Create a nuisance on the structural uncertainty."""
206  if isinstance(resolution, str):
207  sigmainit = 2.0
208  else:
209  sigmainit = resolution + 2.0
210  self.sigmaissampled = True
211  sigmaminnuis = 0.0000001
212  sigmamaxnuis = 1000.0
213  sigmamin = 0.01
214  sigmamax = 100.0
215  sigmatrans = 0.5
216  sigma = IMP.pmi.tools.SetupNuisance(self.m, sigmainit, sigmaminnuis,
217  sigmamaxnuis, self.sigmaissampled
218  ).get_particle()
219  self.sigma_dictionary[resolution] = (
220  sigma,
221  sigmatrans,
222  self.sigmaissampled)
223  self.rssig.add_restraint(
225  self.m,
226  sigma,
227  1000000000.0,
228  sigmamax,
229  sigmamin))
230  # self.rssig.add_restraint(IMP.isd.JeffreysRestraint(self.sigma))
231 
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]
237 
238  def create_psi(self, value):
239  """Create a nuisance on the inconsistency."""
240  if isinstance(value, str):
241  psiinit = 0.5
242  else:
243  psiinit = value
244  self.psiissampled = True
245  psiminnuis = 0.0000001
246  psimaxnuis = 0.4999999
247  psimin = 0.01
248  psimax = 0.49
249  psitrans = 0.1
250  psi = IMP.pmi.tools.SetupNuisance(self.m, psiinit,
251  psiminnuis, psimaxnuis,
252  self.psiissampled).get_particle()
253  self.psi_dictionary[value] = (
254  psi,
255  psitrans,
256  self.psiissampled)
257  self.rspsi.add_restraint(
259  self.m,
260  psi,
261  1000000000.0,
262  psimax,
263  psimin))
264  self.rspsi.add_restraint(IMP.isd.JeffreysRestraint(self.m, psi))
265 
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.
Miscellaneous utilities.
Definition: tools.py:1
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.
Uniform distribution with harmonic boundaries.
Definition: UniformPrior.h:20
Object used to hold a set of restraints.
Definition: RestraintSet.h:36
def add_restraint_to_model
Add a PMI restraint to the model.
Definition: tools.py:86
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).