IMP logo
IMP Reference Guide  2.18.0
The Integrative Modeling Platform
rigid_fitting.h
Go to the documentation of this file.
1 /**
2  * \file IMP/em/rigid_fitting.h
3  * \brief preforms rigid fitting between a set of particles and a density map
4  *
5  * Copyright 2007-2022 IMP Inventors. All rights reserved.
6  */
7 
8 #ifndef IMPEM_RIGID_FITTING_H
9 #define IMPEM_RIGID_FITTING_H
10 
11 #include <IMP/core/MonteCarlo.h>
13 #include <IMP/RestraintSet.h>
15 #include <IMP/Particle.h>
16 #include <IMP/Model.h>
17 #include "DensityMap.h"
18 #include "FitRestraint.h"
19 #include <IMP/em/em_config.h>
20 #include <IMP/OptimizerState.h>
21 #include <IMP/core/rigid_bodies.h>
22 #include <IMP/ScoreState.h>
23 #include <algorithm>
24 #include <boost/serialization/access.hpp>
25 #include <boost/serialization/vector.hpp>
26 
27 namespace boost {
28  namespace serialization {
29  template<class Archive> void serialize(Archive &ar,
30  std::pair<IMP::algebra::Transformation3D, IMP::Float> &g,
31  const unsigned int) {
32  ar & g.first & g.second;
33  }
34  }
35 }
36 
37 IMPEM_BEGIN_NAMESPACE
38 
39 //! A simple list of fitting solutions.
40 /**
41  \see local_rigid_fitting_around_point
42  \see local_rigid_fitting_around_points
43  \see local_rigid_fitting_grid_search
44  \see compute_fitting_scores
45  */
46 class IMPEMEXPORT FittingSolutions {
47  typedef std::pair<algebra::Transformation3D, Float> FittingSolution;
48  struct sort_by_cc {
49  bool operator()(const FittingSolution &s1,
50  const FittingSolution &s2) const {
51  return s1.second < s2.second;
52  }
53  };
54 
55  public:
56  FittingSolutions() {}
57  //! Get the number of solutions in the set
58  inline int get_number_of_solutions() const { return fs_.size(); }
59  //! Get the score of the i'th solution
60  /**
61  \return the i'th transformation, or throw an exception
62  if the index is out of range
63  */
64  inline algebra::Transformation3D get_transformation(unsigned int i) const {
65  IMP_USAGE_CHECK(i < fs_.size(),
66  "The index requested ("
67  << i << ") in get_transformation is out of range ("
68  << fs_.size() << ")" << std::endl);
69  return fs_[i].first;
70  }
71  //! Get the score of the i'th solution
72  /**
73  \return the i'th score, or throw an exception
74  if the index is out of range
75  */
76  inline Float get_score(unsigned int i) const {
77  IMP_USAGE_CHECK(i < fs_.size(),
78  "The index requested ("
79  << i << ") in get_transformation is out of range ("
80  << fs_.size() << ")" << std::endl);
81  return fs_[i].second;
82  }
83  void set_score(unsigned int i, Float score) {
84  IMP_USAGE_CHECK(i < fs_.size(),
85  "The index requested ("
86  << i << ") in get_transformation is out of range ("
87  << fs_.size() << ")" << std::endl);
88  fs_[i].second = score;
89  }
90  //! Add a solution to the fitting solution set
91  void add_solution(const algebra::Transformation3D &t, Float score);
92  //! Sort solutions by cross-correlation scores
93  void sort(bool reverse = false);
94  //! Multiply each transformation (T) by t,
95  //!such that the new transformation are T*t
97  for (unsigned int i = 0; i < fs_.size(); i++)
98  fs_[i].first = fs_[i].first * t;
99  }
100  inline algebra::Transformation3Ds get_transformations() const {
102  for (unsigned int i = 0; i < fs_.size(); i++)
103  all_ts.push_back(fs_[i].first);
104  return all_ts;
105  }
106  void show(std::ostream &out = std::cout) const {
107  for (std::vector<FittingSolution>::const_iterator it = fs_.begin();
108  it != fs_.end(); it++) {
109  out << "(" << it->first << " , " << it->second << ")" << std::endl;
110  }
111  }
112 
113  protected:
114  std::vector<FittingSolution> fs_;
115 
116 private:
117  friend class boost::serialization::access;
118 
119  template<class Archive> void serialize(Archive &ar, const unsigned int) {
120  ar & fs_;
121  }
122 };
124 
125 //! Local rigid fitting of a rigid body around a center point
126 /**
127 \brief Fit a set of particles to a density map around an anchor point.
128  The fitting is assessed using the cross-correlation score.
129  The optimization is a standard MC/CG procedure.
130  The function returns a list of solutions sorted by the cross-correlation
131  score.
132 \note The returned cross-correlation score is 1-cc, as we usually want to
133  minimize a scoring function. Thus a score of 1 means no-correlation
134  and a score of 0. is perfect correlation.
135 \note The input rigid body should also be IMP::atom::Hierarchy
136 \param[in] p The rigid body to fit
137 \param[in] refiner Refiner to yield rigid body members
138 \param[in] weight_key The weight key of the particles in the rigid body
139 \param[in] dmap The density map to fit to
140 \param[in] anchor_centroid The point to fit the particles around
141 \param[in] display_log If provided, then intermediate states
142  in during the optimization procedure are printed
143 \param[in] number_of_optimization_runs number of Monte Carlo optimizations
144 \param[in] number_of_mc_steps number of steps in a Monte Carlo optimization
145 \param[in] number_of_cg_steps number of Conjugate Gradients steps in
146  a Monte Carlo step
147 \param[in] max_translation maximum translation step in a MC optimization step
148 \param[in] max_rotation maximum rotation step in a single MC optimization step
149 \param[in] fast if true the density map of the rigid body is not resampled
150  but transformed at each iteration of the optimization
151 \return the refined fitting solutions
152 */
154  Particle *p, Refiner *refiner, const FloatKey &weight_key,
155  DensityMap *dmap, const algebra::Vector3D &anchor_centroid,
156  OptimizerStates display_log, Int number_of_optimization_runs = 5,
157  Int number_of_mc_steps = 10, Int number_of_cg_steps = 100,
158  Float max_translation = 2., Float max_rotation = .3, bool fast = false);
159 
160 //! Local rigid fitting of a rigid body
161 /** Fit a set of particles to a density map around their centroid.
162  The fitting is assessed using the cross-correlation score.
163  The optimization is a standard MC/CG procedure.
164  The function returns a list of solutions sorted by the cross-correlation
165  score.
166 \note The returned cross-correlation score is 1-cc, as we usually want to
167  minimize a scoring function. Thus a score of 1 means no correlation
168  and a score of 0 is perfect correlation.
169 \note The input rigid body should also be IMP::atom::Hierarchy
170 \param[in] p The root of the hierarchy to fit
171 \param[in] refiner The refiner to get the leaves of the particle
172 \param[in] weight_key The weight key of the particles in the rigid body
173 \param[in] dmap The density map to fit to
174 \param[in] display_log If provided, then intermediate states
175  during the optimization procedure are printed
176 \param[in] number_of_optimization_runs number of Monte Carlo optimizations
177 \param[in] number_of_mc_steps number of steps in a Monte Carlo optimization
178 \param[in] number_of_cg_steps number of Conjugate Gradients steps in
179  a Monte Carlo step
180 \param[in] max_translation maximum translation step in a MC optimization step
181 \param[in] max_rotation maximum rotation step in radians in a single
182  MC optimization step
183 \param[in] fast if true the density map of the rigid body is not resampled
184  but transformed at each iteration of the optimization
185 \return the refined fitting solutions
186 */
187 
189  Particle *p, Refiner *refiner, const FloatKey &weight_key,
190  DensityMap *dmap, OptimizerStates display_log,
191  Int number_of_optimization_runs = 5, Int number_of_mc_steps = 10,
192  Int number_of_cg_steps = 100, Float max_translation = 2.,
193  Float max_rotation = .3, bool fast = true) {
194  IMP_LOG_VERBOSE("Start: local_rigid_fitting\n");
195  algebra::Vector3D rb_cen =
197  IMP_LOG_VERBOSE("centroid is:" << rb_cen << "\n");
199  p, refiner, weight_key, dmap, rb_cen, display_log,
200  number_of_optimization_runs, number_of_mc_steps, number_of_cg_steps,
201  max_translation, max_rotation, fast);
202 }
203 
204 //! Local rigid fitting of a rigid body around a set of center points
205 /**
206 \brief Fit a set of particles to a density map around each of the input points.
207  The function apply local_rigid_fitting_around_point around each center.
208 \note The input rigid body should also be IMP::atom::Hierarchy
209 \param[in] p The rigid body to fit
210 \param[in] refiner Refiner to yield rigid body members
211 \param[in] wei_key The weight key of the particles in the rigid body
212 \param[in] dmap The density map to fit to
213 \param[in] anchor_centroids The points to fit the particles around
214 \param[in] display_log If provided, then intermediate states
215  in during the optimization procedure are printed
216 \param[in] number_of_optimization_runs number of Monte Carlo optimizations
217 \param[in] number_of_mc_steps number of steps in a Monte Carlo optimization
218 \param[in] number_of_cg_steps number of Conjugate Gradients steps in
219  a Monte Carlo step
220 \param[in] max_translation maximum translation step in a MC optimization step
221 \param[in] max_rotation maximum rotation step in a single MC optimization step
222 \return the refined fitting solutions
223 */
224 IMPEMEXPORT FittingSolutions local_rigid_fitting_around_points(
225  Particle *p, Refiner *refiner, const FloatKey &wei_key,
226  DensityMap *dmap, const algebra::Vector3Ds &anchor_centroids,
227  OptimizerStates display_log, Int number_of_optimization_runs = 5,
228  Int number_of_mc_steps = 10, Int number_of_cg_steps = 100,
229  Float max_translation = 2., Float max_rotation = .3);
230 
231 //! Local grid search rigid fitting
232 /**
233 \brief Fit a set of particles to a density map around their centroid.
234  The fitting is assessed using the cross-correlation score.
235  The optimization is a grid search
236 \note The transformations are not clustered.
237 \note The returned cross-correlation score is 1-cc, as we usually want to
238  minimize a scoring function. Thus a score of 1 means no-correlation
239  and a score of 0. is perfect correlation.
240 \param[in] ps The particles to be fitted (treated rigid)
241 \param[in] wei_key The weight key of the particles in the rigid body
242 \param[in] dmap The density map to fit to
243 \param[in] max_voxels_translation Sample translations within
244  -max_voxel_translation to max_voxel_translation
245 \param[in] translation_step The translation sampling step
246 \param[in] max_angle_in_radians Sample rotations with +- max_angle_in_radians
247  around the current rotation
248 \param[in] number_of_rotations The number of rotations to sample
249 \return the refined fitting solutions
250 */
251 IMPEMEXPORT FittingSolutions local_rigid_fitting_grid_search(
252  const ParticlesTemp &ps, const FloatKey &wei_key, DensityMap *dmap,
253  Int max_voxels_translation = 2, Int translation_step = 1,
254  Float max_angle_in_radians = 0.174, Int number_of_rotations = 100);
255 
256 //! Compute fitting scores for a given set of rigid transformations
257 /**
258 \brief Score how well a set of particles fit to the map in various
259  rigid transformations.
260 \param[in] ps The particles to be fitted (treated rigid)
261 \param[in] em_map The density map to fit to
262 \param[in] wei_key The weight key of the particles in the rigid body
263 \param[in] fast_version If fast_version is used the sampled density map of the
264  input particles (ps) is not resampled for each
265  transformation but instead the corresponding grid
266  is rotated. This option significantly improves the
267  running times but the returned scores are less accurate
268 \param[in] transformations A set of rigid transformations
269 \param[in] fast_version if true the density map of each transformation
270  is interpolated
271 \param[in] local_score if true a local cross correlation score is used
272 \return The scored fitting solutions
273 \note the function assumes the density map holds its density
274  */
275 IMPEMEXPORT FittingSolutions
276  compute_fitting_scores(const ParticlesTemp &ps, DensityMap *em_map,
277  const algebra::Transformation3Ds &transformations,
278  bool fast_version = false, bool local_score = false,
279  const FloatKey &wei_key =
280  atom::Mass::get_mass_key());
281 
282 //! Compute fitting scores for a given set of rigid transformations
283 /**
284 \brief Score how well a rigid body fits to the map
285 \param[in] em_map The density map to fit to
286 \param[in] rb The rigid body
287 \param[in] refiner The rigid body refiner
288 \param[in] transformations Transformations of the rigid body
289 \return The scored fitting solutions
290 \note the function assumes the density map holds its density
291  */
293  DensityMap *em_map, core::RigidBody rb, Refiner *refiner,
294  const algebra::Transformation3Ds &transformations) {
295  return compute_fitting_scores(refiner->get_refined(rb), em_map,
296  transformations, true);
297 }
298 
299 //! Compute fitting scores for a given set of rigid transformations
300 /**
301 \brief Scores how well a set of particles fit a map
302 \param[in] ps The particles to be fitted
303 \param[in] em_map The density map to fit to
304 \param[in] wei_key The weight key of the particles in the rigid body
305 \note the function assumes the density map holds its density
306  */
307 IMPEMEXPORT Float
308  compute_fitting_score(const ParticlesTemp &ps, DensityMap *em_map,
309  FloatKey wei_key = atom::Mass::get_mass_key());
310 
311 IMPEM_END_NAMESPACE
312 #endif /* IMPEM_RIGID_FITTING_H */
Simple 3D transformation class.
Simple conjugate gradients optimizer.
Calculate score based on fit to EM map.
Used to hold a set of related restraints.
algebra::Transformation3D get_transformation(unsigned int i) const
Get the score of the i'th solution.
Definition: rigid_fitting.h:64
Simple Monte Carlo optimizer.
Storage of a model, its restraints, constraints and particles.
A simple list of fitting solutions.
Definition: rigid_fitting.h:46
#define IMP_LOG_VERBOSE(expr)
Definition: log_macros.h:83
algebra::Vector3D get_centroid(const XYZs &ps)
Get the centroid.
Class for handling density maps.
Class for handling density maps.
Definition: DensityMap.h:93
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
Definition: value_macros.h:23
functionality for defining rigid bodies
Float compute_fitting_score(const ParticlesTemp &ps, DensityMap *em_map, FloatKey wei_key=atom::Mass::get_mass_key())
Compute fitting scores for a given set of rigid transformations.
FittingSolutions compute_fitting_scores(DensityMap *em_map, core::RigidBody rb, Refiner *refiner, const algebra::Transformation3Ds &transformations)
Compute fitting scores for a given set of rigid transformations.
FittingSolutions local_rigid_fitting_around_point(Particle *p, Refiner *refiner, const FloatKey &weight_key, DensityMap *dmap, const algebra::Vector3D &anchor_centroid, OptimizerStates display_log, Int number_of_optimization_runs=5, Int number_of_mc_steps=10, Int number_of_cg_steps=100, Float max_translation=2., Float max_rotation=.3, bool fast=false)
Local rigid fitting of a rigid body around a center point.
Shared score state.
virtual const ParticlesTemp get_refined(Particle *a) const =0
Refine the passed particle into a set of particles.
Simple 3D transformation class.
Classes to handle individual model particles. (Note that implementation of inline functions is in int...
std::ostream & show(Hierarchy h, std::ostream &out=std::cout)
Print the hierarchy using a given decorator to display each node.
FittingSolutions local_rigid_fitting(Particle *p, Refiner *refiner, const FloatKey &weight_key, DensityMap *dmap, OptimizerStates display_log, Int number_of_optimization_runs=5, Int number_of_mc_steps=10, Int number_of_cg_steps=100, Float max_translation=2., Float max_rotation=.3, bool fast=true)
Local rigid fitting of a rigid body.
VectorD< 3 > Vector3D
Definition: VectorD.h:421
double Float
Basic floating-point value (could be float, double...)
Definition: types.h:19
Abstract class to implement hierarchical methods.
Definition: Refiner.h:34
void multiply(const algebra::Transformation3D &t)
Definition: rigid_fitting.h:96
Class to handle individual particles of a Model object.
Definition: Particle.h:41
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:168
int Int
Basic integer value.
Definition: types.h:34
Shared optimizer state.
FittingSolutions local_rigid_fitting_around_points(Particle *p, Refiner *refiner, const FloatKey &wei_key, DensityMap *dmap, const algebra::Vector3Ds &anchor_centroids, OptimizerStates display_log, Int number_of_optimization_runs=5, Int number_of_mc_steps=10, Int number_of_cg_steps=100, Float max_translation=2., Float max_rotation=.3)
Local rigid fitting of a rigid body around a set of center points.
FittingSolutions local_rigid_fitting_grid_search(const ParticlesTemp &ps, const FloatKey &wei_key, DensityMap *dmap, Int max_voxels_translation=2, Int translation_step=1, Float max_angle_in_radians=0.174, Int number_of_rotations=100)
Local grid search rigid fitting.
A decorator for a rigid body.
Definition: rigid_bodies.h:82
int get_number_of_solutions() const
Get the number of solutions in the set.
Definition: rigid_fitting.h:58
Float get_score(unsigned int i) const
Get the score of the i'th solution.
Definition: rigid_fitting.h:76