IMP  2.4.0
The Integrative Modeling Platform
saxs.py
1 """@namespace IMP.pmi.restraints.saxs
2 Restraints for handling small angle x-ray (SAXS) data.
3 """
4 
5 from __future__ import print_function
6 import IMP
7 import IMP.core
8 import IMP.base
9 import IMP.algebra
10 import IMP.atom
11 import IMP.container
12 
13 
14 class SAXSISDRestraint(object):
15 
16  import IMP.saxs
17  import IMP.isd
18  import IMP.isd2
19  import IMP.pmi.tools
20 
21  def __init__(self, representation, profile, resolution=0, weight=1,
22  ff_type=IMP.saxs.HEAVY_ATOMS):
23 
24  self.m = representation.prot.get_model()
25  self.label = "None"
26  self.rs = IMP.RestraintSet(self.m, 'saxs')
27 
28  self.taumaxtrans = 0.05
29  self.prof = IMP.saxs.Profile(profile)
30 
31  self.atoms = IMP.pmi.tools.select(
32  representation,
33  resolution=resolution)
34 
35  # gamma nuisance
36  self.gamma = IMP.pmi.tools.SetupNuisance(
37  self.m, 1., 0., None, False).get_particle()
38 
39  # sigma nuisance
40  self.sigma = IMP.pmi.tools.SetupNuisance(self.m, 10.0, 0., None, False
41  ).get_particle()
42 
43  # tau nuisance, optimized
44  self.tau = IMP.pmi.tools.SetupNuisance(self.m, 1., 0., None, False,
45  ).get_particle()
46 
47  #c1 and c2, optimized
48  self.c1 = IMP.pmi.tools.SetupNuisance(self.m, 1.0, 0.95, 1.05,
49  True).get_particle()
50  self.c2 = IMP.pmi.tools.SetupNuisance(self.m, 0.0, -2., 4.,
51  True).get_particle()
52 
53  #weight, optimized
54  self.w = IMP.pmi.tools.SetupWeight(self.m).get_particle()
55  IMP.isd.Weight(self.w).set_weights_are_optimized(True)
56 
57  # take identity covariance matrix for the start
58  self.cov = [[1 if i == j else 0 for j in range(self.prof.size())]
59  for i in range(self.prof.size())]
60 
61  print("create saxs restraint")
62  self.saxs = IMP.isd2.SAXSRestraint(self.prof, self.sigma, self.tau,
63  self.gamma, self.w, self.c1, self.c2)
64  self.saxs.add_scatterer(self.atoms, self.cov, ff_type)
65 
66  self.rs.add_restraint(self.saxs)
67  self.rs.set_weight(weight)
68 
69  # self.saxs_stuff={'nuis':(sigma,gamma),'cov':cov,
70  # 'exp':prof,'th':tmp}
71 
72  self.rs2 = IMP.RestraintSet(self.m, 'jeffreys')
73  # jeffreys restraints for nuisances
74  j1 = IMP.isd.JeffreysRestraint(self.m, self.sigma)
75  self.rs2.add_restraint(j1)
76  j2 = IMP.isd.JeffreysRestraint(self.m, self.tau)
77  self.rs2.add_restraint(j2)
78  j3 = IMP.isd.JeffreysRestraint(self.m, self.gamma)
79  self.rs2.add_restraint(j3)
80 
81  def optimize_sigma(self):
82  """set sigma to the value that maximizes its conditional likelihood"""
83  from math import sqrt
84  self.m.update()
85  sigma2hat = self.saxs.get_sigmasq_scale_parameter() \
86  / (self.saxs.get_sigmasq_shape_parameter() + 1)
87  IMP.isd.Scale(self.sigma).set_scale(sqrt(sigma2hat))
88 
89  def optimize_gamma(self):
90  """set gamma to the value that maximizes its conditional likelihood"""
91  from math import exp
92  self.m.update()
93  gammahat = exp(self.saxs.get_loggamma_variance_parameter()
94  * self.saxs.get_loggamma_jOg_parameter())
95  IMP.isd.Scale(self.gamma).set_scale(gammahat)
96 
97  def logspace(self, a, b, num=100):
98  """mimick numpy's logspace function"""
99  for i in range(num):
100  val = a + float(b - a) / float(num - 1) * i
101  yield 10 ** val
102 
103  def optimize_tau(self, ltaumin=-2, ltaumax=3, npoints=100):
104  from math import log
105  import IMP.atom
106  values = []
107  self.m.update()
108  IMP.atom.write_pdb(self.atoms, 'tauvals.pdb')
109  fl = open('tauvals.txt', 'w')
110  for tauval in self.logspace(ltaumin, ltaumax, npoints):
111  IMP.isd.Scale(self.tau).set_scale(tauval)
112  try:
113  values.append((self.m.evaluate(False), tauval))
114  except:
115  pass
116  fl.write('%G %G\n' % (values[-1][1], values[-1][0]))
117  values.sort()
118  ltcenter = log(values[0][1]) / log(10)
119  spacing = (ltaumax - ltaumin) / float(npoints)
120  values = []
121  for tauval in self.logspace(
122  ltcenter - 2 * spacing, ltcenter + 2 * spacing,
123  npoints):
124  IMP.isd.Scale(self.tau).set_scale(tauval)
125  values.append((self.m.evaluate(False), tauval))
126  fl.write('%G %G\n' % (values[-1][1], values[-1][0]))
127  values.sort()
128  IMP.isd.Scale(self.tau).set_scale(values[0][1])
129 
130  def draw_sigma(self):
131  """draw 1/sigma2 from gamma distribution"""
132  self.m.update()
133  self.saxs.draw_sigma()
134 
135  def draw_gamma(self):
136  """draw gamma from lognormal distribution"""
137  self.m.update()
138  self.saxs.draw_gamma()
139 
140  def update_covariance_matrix(self):
141  c1 = IMP.isd.Nuisance(self.c1).get_nuisance()
142  c2 = IMP.isd.Nuisance(self.c2).get_nuisance()
143  #tau = IMP.isd.Nuisance(self.tau).get_nuisance()
144  tau = 1.0
145  self.cov = IMP.isd2.compute_relative_covariance(self.atoms, c1, c2,
146  tau, self.prof)
147  # for i in xrange(len(self.cov)):
148  # for j in xrange(len(self.cov)):
149  # self.cov[i][j] = self.cov[i][j]/tau**2
150  self.saxs.set_cov(0, self.cov)
151 
152  def write_covariance_matrix(self, fname):
153  fl = open(fname, 'w')
154  for line in self.cov:
155  for i in line:
156  fl.write('%G ' % i)
157  fl.write('\n')
158 
159  def get_gamma_value(self):
160  return self.gamma.get_scale()
161 
162  def set_label(self, label):
163  self.label = label
164 
165  def add_to_model(self):
166  self.m.add_restraint(self.rs)
167  self.m.add_restraint(self.rs2)
168 
169  def set_taumaxtrans(self, taumaxtrans):
170  self.taumaxtrans = taumaxtrans
171 
172  def get_particles_to_sample(self):
173  ps = {}
174  # ps["Nuisances_SAXSISDRestraint_Tau_" +
175  # self.label] = ([self.tau], self.taumaxtrans)
176  return ps
177 
178  def get_output(self):
179  self.m.update()
180  output = {}
181  score = self.rs.unprotected_evaluate(None)
182  score2 = self.rs2.unprotected_evaluate(None)
183  output["_TotalScore"] = str(score + score2)
184 
185  output["SAXSISDRestraint_Likelihood_" + self.label] = str(score)
186  output["SAXSISDRestraint_Prior_" + self.label] = str(score2)
187  output["SAXSISDRestraint_Sigma_" +
188  self.label] = str(self.sigma.get_scale())
189  output["SAXSISDRestraint_Tau_" +
190  self.label] = str(self.tau.get_scale())
191  output["SAXSISDRestraint_Gamma_" +
192  self.label] = str(self.gamma.get_scale())
193  return output
Add weights for a set of states to a particle.
Definition: Weight.h:24
void write_pdb(const Selection &mhd, base::TextOutput out, unsigned int model=1)
Various classes to hold sets of particles.
Miscellaneous utilities.
Definition: tools.py:1
Object used to hold a set of restraints.
Low level functionality (logging, error handling, profiling, command line flags etc) that is used by ...
Add scale parameter to particle.
Definition: Scale.h:24
log
Definition: log.py:1
Add nuisance parameter to particle.
Definition: Nuisance.h:25
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...
def select
this function uses representation=SimplifiedModel it returns the corresponding selected particles rep...
Definition: tools.py:633
Functionality for loading, creating, manipulating and scoring atomic structures.
Support for small angle X-ray scattering (SAXS) data.
Inferential scoring building on methods developed as part of the Inferential Structure Determination ...