IMP logo
IMP Reference Guide  develop.63b38c487d,2024/12/21
The Integrative Modeling Platform
GaussianAnchorEMRestraint.h
Go to the documentation of this file.
1 /**
2  * \file IMP/isd/GaussianAnchorEMRestraint.h
3  * \brief Restraint between two sets of anchor points "model" and "EM density"
4  *
5  * Copyright 2007-2022 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPISD_GAUSSIAN_ANCHOR_EMRESTRAINT_H
10 #define IMPISD_GAUSSIAN_ANCHOR_EMRESTRAINT_H
11 
12 #include <IMP/isd/isd_config.h>
13 #include <IMP/isd/ISDRestraint.h>
14 #include <IMP/PairContainer.h>
15 #include <IMP/isd/Scale.h>
16 #include <IMP/core/XYZ.h>
18 #include <map>
19 #include <cmath>
20 
21 IMPISD_BEGIN_NAMESPACE
22 
23 //! Restraint between two sets of anchor points "model" and "EM density"
24 /** \note This class is obsolete - use GaussianEMRestraint instead. It exists
25  solely to support older applications of IMP that used this method.
26  */
27 class IMPISDEXPORT GaussianAnchorEMRestraint : public ISDRestraint
28 {
29 public:
30  //! Constructor
31  /**
32  \param[in] model_ps particles for the model GMM
33  \param[in] model_sigs sigmas for the model GMM
34  (currently spherically symmetric)
35  \param[in] model_weights weights for the model GMM
36  \param[in] density_ps particles for the density GMM
37  \param[in] density_sigs sigmas for the density GMM
38  (currently spherically symmetric)
39  \param[in] density_weights weights for the density GMM
40  \param[in] global_sigma Particle to modulate the uncertainty
41  \param[in] cutoff_dist Only consider model-density interactions
42  up to this distance
43  \param[in] rigid Set to true if the model is rigid (faster)
44  \param[in] tabexp Use pre-calculated tabulated exponential
45  \param[in] name User-visible name of the restraint, if desired
46  */
48  Floats model_weights, ParticlesTemp density_ps,
49  Floats density_sigs, Floats density_weights,
50  Particle *global_sigma, Float cutoff_dist,
51  bool rigid, bool tabexp,
52  std::string name="GaussianAnchorEMRestraint%1%"):
53  ISDRestraint(model_ps[0]->get_model(),name),
54  model_ps_(model_ps),
55  model_sigs_(model_sigs),
56  model_weights_(model_weights),
57  density_ps_(density_ps),
58  density_sigs_(density_sigs),
59  density_weights_(density_weights),
60  global_sigma_(global_sigma),
61  cutoff_dist_(cutoff_dist),
62  rigid_(rigid),
63  tabexp_(tabexp){
64  msize_=model_ps.size();
65  dsize_=density_ps.size();
66  IMP_USAGE_CHECK(model_sigs.size()==msize_ &&
67  model_weights.size()==msize_,
68  "All model inputs must be same size");
69  IMP_USAGE_CHECK(density_ps.size()==dsize_ &&
70  density_sigs.size()==dsize_ &&
71  density_weights.size()==dsize_,
72  "All input vectors must be same size");
73 
74  if (tabexp_){
75  unsigned exparg_grid_size=1000001;
76  argmax_=10.0;
77  invdx_=double(exparg_grid_size)/argmax_;
78  for(unsigned k=0;k<exparg_grid_size;++k){
79  double argvalue=double(k)/invdx_;
80  exp_grid_.push_back(std::exp(-argvalue));
81  }
82  }
83 
84 
85  // calculating prefactors and initial model-model score
86  init_mm_score_=0.0;
87  for (size_t m1=0;m1<msize_;m1++){
88  init_mm_score_+=calc_prefactor(model_sigs_[m1],model_weights_[m1],
89  model_sigs[m1],model_weights_[m1]);
90  for (size_t m2=m1;m2<msize_;m2++){
91  Float dist=core::get_distance(core::XYZ(model_ps_[m1]),
92  core::XYZ(model_ps_[m2]));
93  ParticlePair pp1(model_ps_[m1],model_ps_[m2]);
94  ParticlePair pp2(model_ps_[m2],model_ps_[m1]);
95  Float pref=calc_prefactor(model_sigs_[m1],model_weights_[m1],
96  model_sigs[m2],model_weights_[m2]);
97  Float prod=calc_prod(model_sigs_[m1],model_sigs_[m2]);
98  mm_prefactors_[pp1]=pref;
99  mm_prefactors_[pp2]=pref;
100  mm_prods_[pp1]=prod;
101  mm_prods_[pp2]=prod;
102  if (m2>m1){
103  init_mm_score_+=2.0*pref*calc_score(dist,prod);
104  }
105  }
106 
107  //model-density prefactors
108  for (size_t d1=0;d1<dsize_;d1++){
109  ParticlePair ppd1(model_ps_[m1],density_ps_[d1]);
110  md_prefactors_[ppd1]=
111  calc_prefactor(model_sigs_[m1],model_weights_[m1],
112  density_sigs[d1],density_weights_[d1]);
113  md_prods_[ppd1]=calc_prod(model_sigs_[m1],density_sigs[d1]);
114  }
115  }
116 
117  // calculating density-density score (doesn't change)
118  for (size_t d1=0;d1<dsize_;d1++){
119  core::XYZ d_d1(density_ps_[d1]);
120  dd_score_+=calc_prefactor(density_sigs_[d1],density_weights_[d1],
121  density_sigs_[d1],density_weights_[d1]);
122  for (size_t d2=d1+1;d2<dsize_;d2++){
123  Float dist=core::get_distance(core::XYZ(density_ps_[d1]),
124  core::XYZ(density_ps_[d2]));
125  Float pref=calc_prefactor(density_sigs_[d1],density_weights_[d1],
126  density_sigs_[d2],density_weights_[d2]);
127  Float prod=calc_prod(density_sigs_[d1],density_sigs_[d2]);
128  dd_score_+=2.0*pref*calc_score(dist,prod);
129  }
130  }
131  //KLUGE!
132  if (rigid_){
133  init_mm_score_=0.0;
134  dd_score_=0.0;
135  }
136 
137  //Set up md container
138  md_container_ = new container::CloseBipartitePairContainer(
139  model_ps,density_ps,cutoff_dist_);
140  }
141  ParticlesTemp get_density_particles() const {
142  ParticlesTemp ret = density_ps_;
143  return ret;
144  }
145  double get_probability() const override {
146  return exp(-unprotected_evaluate(NULL));
147  }
148 
149  virtual double
150  unprotected_evaluate(IMP::DerivativeAccumulator *accum) const override;
151  virtual IMP::ModelObjectsTemp do_get_inputs() const override;
153  protected:
154  Particles model_ps_;
155  Floats model_sigs_,model_weights_;
156  Particles density_ps_;
157  Floats density_sigs_,density_weights_;
158  Particle * global_sigma_;
159  Float cutoff_dist_;
160  bool rigid_;
161  bool tabexp_;
162  Float dd_score_,init_mm_score_;
163  std::map<ParticlePair,Float> mm_prefactors_,
164  md_prefactors_,mm_prods_,md_prods_;
165  size_t msize_,dsize_;
166  //variables needed to tabulate the exponential
167  Floats exp_grid_;
168  double invdx_;
169  double argmax_;
171  //actual scores defined here (prefactor*score)
172  inline Float calc_prefactor(Float s1, Float w1, Float s2, Float w2) const{
173  return w1*w2*sqrt(8*algebra::PI)*s1*s1*s2*s2/
174  pow(s1*s1+s2*s2,1.5);
175  }
176  inline Float calc_prod(Float s1, Float s2) const{
177  return 1.0/(2.0*(s1*s1+s2*s2));
178  }
179  inline Float calc_score (Float dist, Float prod) const{
180  double argvalue=dist*dist*prod;
181  double expn;
182  if (tabexp_){
183  double minarg=std::min(argvalue,argmax_);
184  unsigned k = static_cast<unsigned>( std::floor(minarg*invdx_) );
185  expn=exp_grid_[k];
186  }
187  else{
188  expn=std::exp(-argvalue);
189  }
190  return expn;
191  }
192 };
193 
194 IMPISD_END_NAMESPACE
195 
196 #endif /* IMPISD_GAUSSIAN_ANCHOR_EMRESTRAINT_H */
A base class for ISD Restraints.
static const double PI
the constant pi
A class to store a fixed array of same-typed values.
Definition: Array.h:40
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
GaussianAnchorEMRestraint(ParticlesTemp model_ps, Floats model_sigs, Floats model_weights, ParticlesTemp density_ps, Floats density_sigs, Floats density_weights, Particle *global_sigma, Float cutoff_dist, bool rigid, bool tabexp, std::string name="GaussianAnchorEMRestraint%1%")
Constructor.
A decorator for scale parameters particles.
Return all spatially-proximal pairs of particles (a,b) from the two SingletonContainers A and B...
Simple XYZ decorator.
virtual ModelObjectsTemp do_get_inputs() const override
A container for Pairs.
Return all pairs from a SingletonContainer.
Restraint between two sets of anchor points "model" and "EM density".
A decorator for a particle with x,y,z coordinates.
Definition: XYZ.h:30
virtual double unprotected_evaluate(DerivativeAccumulator *accum) const override
Return the unweighted score for the restraint.
double Float
Basic floating-point value (could be float, double...)
Definition: types.h:19
Class to handle individual particles of a Model object.
Definition: Particle.h:43
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:168
double get_distance(const Surface &s, const XYZR &d)
Get distance from sphere to surface.
Definition: Surface.h:153
Class for adding derivatives from restraints to the model.