IMP logo
IMP Reference Guide  2.6.2
The Integrative Modeling Platform
basic.py
1 """@namespace IMP.pmi.restraints.basic
2 Some miscellaneous simple restraints.
3 """
4 
5 from __future__ import print_function
6 import IMP
7 import IMP.core
8 import IMP.algebra
9 import IMP.atom
10 import IMP.container
11 import IMP.pmi.tools
12 
13 class ExternalBarrier(object):
14 
15  def __init__(self,
16  representation=None,
17  radius=10.0,
18  hierarchies=None,
19  resolution=10,
20  weight=1.0,
21  center=None):
22  """Setup external barrier to keep all your structures inside sphere
23  @param representation DEPRECATED
24  @param center - Center of the external barrier restraint (IMP.algebra.Vector3D object)
25  @param radius Size of external barrier
26  @param hierarchies Can be one of the following inputs:
27  IMP Hierarchy, PMI System/State/Molecule/TempResidue, or a list/set of them
28  @param resolution Select which resolutions to act upon
29  """
30  self.radius = radius
31  self.label = "None"
32  self.weight = weight
33 
34  if representation:
35  self.m = representation.prot.get_model()
36  particles = IMP.pmi.tools.select(
37  representation,
38  resolution=resolution,
39  hierarchies=hierarchies)
40  elif hierarchies:
41  hiers = IMP.pmi.tools.input_adaptor(hierarchies,resolution,flatten=True)
42  self.m = hiers[0].get_model()
43  particles = [h.get_particle() for h in hiers]
44  else:
45  raise Exception("ExternalBarrier: must pass representation or hierarchies")
46 
47  self.rs = IMP.RestraintSet(self.m, 'barrier')
48 
49  if center is None:
50  c3 = IMP.algebra.Vector3D(0, 0, 0)
51  elif type(center) is IMP.algebra.Vector3D:
52  c3 = center
53  else:
54  raise Exception("ExternalBarrier: @param center must be an algebra::Vector3D object")
55 
56  ub3 = IMP.core.HarmonicUpperBound(radius, 10.0)
59 
60  lsc.add(particles)
62  self.rs.add_restraint(r3)
63  self.set_weight(self.weight)
64 
65  def set_label(self, label):
66  self.label = label
67 
68  def add_to_model(self):
70 
71  def get_restraint(self):
72  return self.rs
73 
74  def get_output(self):
75  self.m.update()
76  output = {}
77  score = self.evaluate()
78  output["_TotalScore"] = str(score)
79  output["ExternalBarrier_" + self.label] = str(score)
80  return output
81 
82  def set_weight(self, weight):
83  self.weight = weight
84  self.rs.set_weight(weight)
85 
86  def evaluate(self):
87  return self.weight * self.rs.unprotected_evaluate(None)
88 
89 
90 class DistanceRestraint(object):
91  """A simple distance restraint"""
92  def __init__(self,
93  representation=None,
94  tuple_selection1=None,
95  tuple_selection2=None,
96  distancemin=0,
97  distancemax=100,
98  resolution=1.0,
99  kappa=1.0,
100  root_hier = None):
101  """Setup distance restraint.
102  @param representation DEPRECATED
103  @param tuple_selection1 (resnum,resnum,molecule name, copy number (=0))
104  @param tuple_selection2 (resnum,resnum,molecule name, copy number (=0))
105  @param distancemin The minimum dist
106  @param distancemax The maximum dist
107  @param resolution For selecting particles
108  @param kappa The harmonic parameter
109  @param root_hier The hierarchy to select from (use this instead of representation)
110  \note Pass the same resnum twice to each tuple_selection. Optionally add a copy number (PMI2 only)
111  """
112  self.weight=1.0
113  self.label="None"
114  if tuple_selection1 is None or tuple_selection2 is None:
115  raise Exception("You must pass tuple_selection1/2")
116  ts1 = IMP.core.HarmonicUpperBound(distancemax, kappa)
117  ts2 = IMP.core.HarmonicLowerBound(distancemin, kappa)
118 
119  if representation and not root_hier:
120  self.m = representation.prot.get_model()
121  particles1 = IMP.pmi.tools.select(representation,
122  resolution=resolution,
123  name=tuple_selection1[2],
124  residue=tuple_selection1[0])
125  particles2 = IMP.pmi.tools.select(representation,
126  resolution=resolution,
127  name=tuple_selection2[2],
128  residue=tuple_selection2[0])
129  elif root_hier and not representation:
130  self.m = root_hier.get_model()
131  copy_num1 = 0
132  if len(tuple_selection1)>3:
133  copy_num1 = tuple_selection1[3]
134  copy_num2 = 0
135  if len(tuple_selection2)>3:
136  copy_num2 = tuple_selection2[3]
137 
138  sel1 = IMP.atom.Selection(root_hier,
139  resolution=resolution,
140  molecule=tuple_selection1[2],
141  residue_index=tuple_selection1[0],
142  copy_index=copy_num1)
143  particles1 = sel1.get_selected_particles()
144  sel2 = IMP.atom.Selection(root_hier,
145  resolution=resolution,
146  molecule=tuple_selection2[2],
147  residue_index=tuple_selection2[0],
148  copy_index=copy_num2)
149  particles2 = sel2.get_selected_particles()
150  else:
151  raise Exception("Pass representation or root_hier, not both")
152 
153  self.rs = IMP.RestraintSet(self.m, 'distance')
154 
155  print("Created distance restraint between "
156  "%s and %s" % (particles1[0].get_name(),particles2[0].get_name()))
157 
158  if len(particles1) > 1 or len(particles2) > 1:
159  raise ValueError("more than one particle selected")
160 
161  self.rs.add_restraint(
162  IMP.core.DistanceRestraint(self.m, ts1,
163  particles1[0],
164  particles2[0]))
165  self.rs.add_restraint(
166  IMP.core.DistanceRestraint(self.m, ts2,
167  particles1[0],
168  particles2[0]))
169 
170  def set_weight(self,weight):
171  self.weight = weight
172  self.rs.set_weight(weight)
173 
174  def set_label(self, label):
175  self.label = label
176 
177  def add_to_model(self):
178  IMP.pmi.tools.add_restraint_to_model(self.m, self.rs)
179 
180  def get_restraint(self):
181  return self.rs
182 
183  def get_restraint_for_rmf(self):
184  return self.rs
185 
186  def get_output(self):
187  self.m.update()
188  output = {}
189  score = self.weight * self.rs.unprotected_evaluate(None)
190  output["_TotalScore"] = str(score)
191  output["DistanceRestraint_" + self.label] = str(score)
192  return output
193 
194  def evaluate(self):
195  return self.weight * self.rs.unprotected_evaluate(None)
196 
197 
198 
199 
201  '''
202  a python restraint with bistable potential
203  Authors: G. Bouvier, R. Pellarin. Pasteur Institute.
204  '''
205  import numpy as np
206  import math
207 
208  def __init__(self,m,p1,p2,dist1,dist2,sigma1,sigma2,weight1,weight2):
209  '''
210  input twp particles, the two equilibrium distances, their amplitudes, and their weights (populations)
211  '''
212  IMP.Restraint.__init__(self, m, "BiStableDistanceRestraint %1%")
213  self.dist1=dist1
214  self.dist2=dist2
215 
216  self.sigma1=sigma1
217  self.sigma2=sigma2
218 
219  self.weight1=weight1
220  self.weight2=weight2
221 
222  if self.weight1+self.weight2 != 1:
223  raise ValueError("The sum of the weights must be one")
224 
225  self.d1=IMP.core.XYZ(p1)
226  self.d2=IMP.core.XYZ(p2)
227  self.particle_list=[p1,p2]
228 
229  def gaussian(self,x, mu, sig, w):
230  return w*self.np.exp(-self.np.power(x - mu, 2.) / (2 * self.np.power(sig, 2.)))
231 
232  def unprotected_evaluate(self,da):
233  dist=IMP.core.get_distance(self.d1,self.d2)
234  prob=self.gaussian(dist,self.dist1,self.sigma1,self.weight1)+\
235  self.gaussian(dist,self.dist2,self.sigma2,self.weight2)
236  return -self.math.log(prob)
237 
238  def do_get_inputs(self):
239  return self.particle_list
Applies a SingletonScore to each Singleton in a list.
def __init__
Setup distance restraint.
Definition: basic.py:103
A simple distance restraint.
Definition: basic.py:90
Lower bound harmonic function (non-zero when feature < mean)
Various classes to hold sets of particles.
Upper bound harmonic function (non-zero when feature > mean)
Miscellaneous utilities.
Definition: tools.py:1
Distance restraint between two particles.
double get_distance(XYZR a, XYZR b)
Compute the sphere distance between a and b.
Definition: XYZR.h:87
Object used to hold a set of restraints.
Definition: RestraintSet.h:36
def input_adaptor
Adapt things for PMI (degrees of freedom, restraints, ...) Returns list of list of hierarchies...
Definition: tools.py:1455
def __init__
input twp particles, the two equilibrium distances, their amplitudes, and their weights (populations)...
Definition: basic.py:208
Store a list of ParticleIndexes.
def add_restraint_to_model
Add a PMI restraint to the model.
Definition: tools.py:37
Apply a function to the distance to a fixed point.
A decorator for a particle with x,y,z coordinates.
Definition: XYZ.h:30
Basic functionality that is expected to be used by a wide variety of IMP users.
General purpose algebraic and geometric methods that are expected to be used by a wide variety of IMP...
The general base class for IMP exceptions.
Definition: exception.h:49
VectorD< 3 > Vector3D
Definition: VectorD.h:395
def select
this function uses representation=SimplifiedModel it returns the corresponding selected particles rep...
Definition: tools.py:702
Functionality for loading, creating, manipulating and scoring atomic structures.
a python restraint with bistable potential Authors: G.
Definition: basic.py:200
Select hierarchy particles identified by the biological name.
Definition: Selection.h:66
virtual ModelObjectsTemp do_get_inputs() const =0
A restraint is a term in an IMP ScoringFunction.
Definition: Restraint.h:52