IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
The Integrative Modeling Platform
isd/GaussianEMRestraint.h
Go to the documentation of this file.
1/**
2 * \file IMP/isd/GaussianEMRestraint.h
3 * \brief Restrain two sets of Gaussians (model and GMM derived from EM map)
4 *
5 * Copyright 2007-2026 IMP Inventors. All rights reserved.
6 *
7 */
8
9#ifndef IMPISD_GAUSSIAN_EM_RESTRAINT_H
10#define IMPISD_GAUSSIAN_EM_RESTRAINT_H
11
12#include <IMP/isd/isd_config.h>
13#include <IMP/file.h>
14#include <IMP/PairContainer.h>
17#include <IMP/core/XYZ.h>
18#include <IMP/core/Gaussian.h>
22#include <IMP/atom/Mass.h>
23#include <cmath>
24#include <Eigen/Dense>
25#include <boost/unordered_map.hpp>
26#include <cereal/access.hpp>
27#include <cereal/types/vector.hpp>
28
29IMPISD_BEGIN_NAMESPACE
30
31//! Restraint between two Gaussian Mixture Models, "model" and "density"
32/** This restrains two sets of GMMs with a density overlap function.
33 The function is correlation of the two GMMs \f$f_M\f$ and \f$f_D\f$:
34 \f[
35 \frac{2\int{f_M(x)f_D(x)dx}}{\int{f_M^2(x)+f_D^2(x)dx}}
36 \f]
37 Where the integral is the "overlap function" given by:
38 \f[
39 ov(f_M,f_D) = \sum_{i=1}^{N_M} \sum_{j=1}^{N_D} \frac{1}{(2 \pi)^{3/2}|\Sigma_{Mi}+\Sigma_{Dj}|^{1/2}}\exp\left [-\frac{1}{2}(\boldsymbol\mu_{Mi} - \boldsymbol\mu_{Dj})^\top (\Sigma_{Mi}+\Sigma_{Dj})^{-1} (\boldsymbol\mu_{Mi} - \boldsymbol \mu_{Dj})\right ]
40 \f]
41 \note Source: Greenberg, Pellarin, Sali. In preparation.
42 */
43class IMPISDEXPORT GaussianEMRestraint : public Restraint
44{
45
46 public:
47 //! Setup the GaussianEMRestraint
48 /**
49 \param[in] mdl the Model object to operate on
50 \param[in] model_ps particles for the model GMM
51 \param[in] density_ps particles for the density GMM
52 \param[in] global_sigma Particle to modulate the uncertainty
53 (currently unused)
54 \param[in] model_cutoff_dist Cutoff for the model-model interactions
55 \param[in] density_cutoff_dist Cutoff for model-density interactions
56 \param[in] slope Gentle term to move all particles to the density
57 \param[in] update_model (DEPRECATED) update model each cycle
58 \param[in] backbone_slope Limit the slope only to backbone particles
59 \param[in] local Only consider density particles that are within the
60 specified cutoff of the model particles (experimental)
61 \param[in] name Name of this restraint
62 \note the model and density particles must be set up as Gaussian
63 */
65 ParticleIndexes model_ps, ParticleIndexes density_ps,
66 ParticleIndex global_sigma,
67 Float model_cutoff_dist, Float density_cutoff_dist,
68 Float slope,
69 bool update_model=true, bool backbone_slope=false,
70 bool local=false,
71 std::string name="GaussianEMRestraint%1%");
72 GaussianEMRestraint() { is_custom_reset_ = true; }
73
74 //! Returns exp(score)
75 double get_probability() const {
76 return exp(-unprotected_evaluate(NULL));
77 }
78
79 //! Get cross correlation between the model and the map
80 /** This CCC is that calculated from the last scoring function
81 evaluation; calling this function before the score is calculated
82 results in undefined behavior.
83 */
85 return cross_correlation_;
86 }
87
88 //! Set the filename corresponding to the density GMM particles
89 /** If the density GMM particles were read from a file, this method
90 can be used to tell the restraint so that it can track this
91 information back to the original EM density file, which is useful
92 for deposition. */
93 void set_density_filename(std::string density_fn) {
94 density_fn_ = get_absolute_path(density_fn);
95 }
96
97 //! Pre-calculate the density-density and model-model scores
98 /** This is automatically called by the constructor.
99 You only need to call it manually if you change Gaussian variances.
100 */
102
103 //! Set restraint slope
104 void set_slope(Float s){slope_=s;}
105
106 //! Get restraint slope
107 Float get_slope() const {return slope_;}
108
109 //! Get the particles for the model GMM
110 ParticleIndexes get_model_particle_indexes() const { return model_ps_; }
111
112 //! Get the particles for the density GMM
113 ParticleIndexes get_density_particle_indexes() const { return density_ps_; }
114
115 //! Get the model particles that the slope term acts on
116 /** This is all model particles, unless backbone_slope was set
117 in the constructor. */
118 ParticleIndexes get_slope_particle_indexes() const { return slope_ps_; }
119
120 //! Get the cutoff for model-model interactions
121 double get_model_cutoff_distance() const { return model_cutoff_dist_; }
122
123 //! Get the cutoff for model-density interactions
124 double get_density_cutoff_distance() const { return density_cutoff_dist_; }
125
126 //! Return true iff only density particles near the model are considered
127 bool get_is_local() const { return local_; }
128
129 //! Get the precalculated density-density overlap score
130 /** \see compute_initial_scores() */
131 double get_density_density_score() const { return dd_score_; }
132
133 //! Get the precalculated model-model self-overlap score
134 /** This is the sum of the overlaps of each model Gaussian with itself.
135 \see compute_initial_scores() */
136 double get_model_self_score() const { return self_mm_score_; }
137
138 virtual double
140 virtual IMP::ModelObjectsTemp do_get_inputs() const override;
141 void show(std::ostream &out) const { out << "GEM restraint"; }
142
143 //! \return Information for writing to RMF files
145
146 //! \return Information for writing to RMF files
148
150
151 private:
152 double model_cutoff_dist_, density_cutoff_dist_;
153 ParticleIndexes model_ps_;
154 ParticleIndexes density_ps_;
155 Float slope_;
156 bool update_model_,local_;
157 int msize_,dsize_;
158 Float normalization_;
159 Float dd_score_;
160 Float self_mm_score_;
163 ParticleIndexes slope_ps_; //experiment
164 std::string density_fn_;
165
166 //variables needed to tabulate the exponential
167 Floats exp_grid_;
168 double invdx_;
169 double argmax_;
170
171 mutable double cross_correlation_;
172
173 //! Create containers of model and density particles
174 void create_containers();
175
176 friend class cereal::access;
177
178 template<class Archive> void serialize(Archive &ar) {
179 ar(cereal::base_class<Restraint>(this), model_cutoff_dist_,
180 density_cutoff_dist_, model_ps_, density_ps_, slope_, update_model_,
181 local_, msize_,dsize_, normalization_, dd_score_, self_mm_score_,
182 slope_ps_, density_fn_, exp_grid_,
183 invdx_, argmax_, cross_correlation_);
184 // recreate md_container and mm_container on input
185 if (std::is_base_of<cereal::detail::InputArchiveBase, Archive>::value) {
186 create_containers();
187 }
188 }
189
190 IMP_OBJECT_SERIALIZE_DECL(GaussianEMRestraint);
191};
192
193IMPISD_END_NAMESPACE
194
195#endif /* IMPISD_GAUSSIAN_EM_RESTRAINT_H */
Return all pairs from a SingletonContainer.
Return all pairs from a SingletonContainer.
Gaussian shape.
Store a list of ParticleIndexes.
A decorator for particles with mass.
A container for Pairs.
Simple XYZ decorator.
Class for adding derivatives from restraints to the model.
Report key:value information on restraints.
Restraint(Model *m, std::string name)
Create a restraint and register it with the model.
GaussianEMRestraint(Model *mdl, ParticleIndexes model_ps, ParticleIndexes density_ps, ParticleIndex global_sigma, Float model_cutoff_dist, Float density_cutoff_dist, Float slope, bool update_model=true, bool backbone_slope=false, bool local=false, std::string name="GaussianEMRestraint%1%")
Setup the GaussianEMRestraint.
Float get_slope() const
Get restraint slope.
double get_probability() const
Returns exp(score).
double get_model_self_score() const
Get the precalculated model-model self-overlap score.
virtual IMP::ModelObjectsTemp do_get_inputs() const override
void compute_initial_scores()
Pre-calculate the density-density and model-model scores.
double get_density_cutoff_distance() const
Get the cutoff for model-density interactions.
double get_density_density_score() const
Get the precalculated density-density overlap score.
virtual double unprotected_evaluate(IMP::DerivativeAccumulator *accum) const override
Return the unweighted score for the restraint.
ParticleIndexes get_model_particle_indexes() const
Get the particles for the model GMM.
double get_model_cutoff_distance() const
Get the cutoff for model-model interactions.
void set_density_filename(std::string density_fn)
Set the filename corresponding to the density GMM particles.
ParticleIndexes get_slope_particle_indexes() const
Get the model particles that the slope term acts on.
RestraintInfo * get_dynamic_info() const override
ParticleIndexes get_density_particle_indexes() const
Get the particles for the density GMM.
RestraintInfo * get_static_info() const override
void set_slope(Float s)
Set restraint slope.
bool get_is_local() const
Return true iff only density particles near the model are considered.
double get_cross_correlation_coefficient() const
Get cross correlation between the model and the map.
Macros to define containers of objects.
Decorator to hold Gaussian3D.
Handling of file input/output.
Index< ParticleIndexTag > ParticleIndex
Definition base_types.h:196
double Float
Basic floating-point value (could be float, double...).
Definition types.h:19
std::string get_absolute_path(std::string file)
Convert a possibly relative path to an absolute path.
Vector< ParticleIndex, std::allocator< ParticleIndex > > ParticleIndexes
A list of particles, each identified by its ParticleIndex.
Definition base_types.h:199
IMP::Vector< IMP::WeakPointer< ModelObject > > ModelObjectsTemp
Definition base_types.h:122
IMP::Vector< Float > Floats
Standard way to pass a bunch of Float values.
Definition types.h:46
#define IMP_OBJECT_SERIALIZE_DECL(Name)
Declare methods needed for serialization of Object pointers.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
A smart pointer to a ref-counted Object that is a class member.
Definition Pointer.h:144