00001 /** 00002 * \file rigid_fitting.h 00003 * \brief preforms rigid fitting between a set of particles and a density map 00004 * 00005 * Copyright 2007-2010 IMP Inventors. All rights reserved. 00006 */ 00007 #ifndef IMPEM_RIGID_FITTING_H 00008 #define IMPEM_RIGID_FITTING_H 00009 00010 #include <IMP/core/MonteCarlo.h> 00011 #include <IMP/core/ConjugateGradients.h> 00012 #include <IMP/RestraintSet.h> 00013 #include <IMP/algebra/Transformation3D.h> 00014 #include <IMP/VersionInfo.h> 00015 #include <IMP/Particle.h> 00016 #include <IMP/Model.h> 00017 #include "DensityMap.h" 00018 #include "FitRestraint.h" 00019 #include "em_config.h" 00020 #include <IMP/OptimizerState.h> 00021 #include <IMP/core/rigid_bodies.h> 00022 #include <IMP/ScoreState.h> 00023 #include <algorithm> 00024 IMPEM_BEGIN_NAMESPACE 00025 00026 00027 class IMPEMEXPORT FittingSolutions { 00028 typedef std::pair<algebra::Transformation3D,Float> FittingSolution; 00029 struct sort_by_cc 00030 { 00031 bool operator()(const FittingSolution &s1, const FittingSolution & s2) const 00032 { 00033 return s1.second < s2.second; 00034 } 00035 }; 00036 public: 00037 //! Get the number of solutions in the set 00038 inline int get_number_of_solutions() const {return fs_.size();} 00039 //! Get the score of the i'th solution 00040 /** 00041 \return the i'th transformation, or throw an exception 00042 if the index is out of range 00043 */ 00044 inline algebra::Transformation3D get_transformation(int i) const { 00045 IMP_USAGE_CHECK(i<fs_.size(),"The index requested ("<< 00046 i<<") in get_transformation is our of range ("<< 00047 fs_.size()<<")"<<std::endl); 00048 return fs_[i].first; 00049 } 00050 //! Get the score of the i'th solution 00051 /** 00052 \return the i'th score, or throw an exception 00053 if the index is out of range 00054 */ 00055 inline Float get_score(int i) const { 00056 IMP_USAGE_CHECK(i<fs_.size(),"The index requested ("<< 00057 i<<") in get_transformation is our of range ("<< 00058 fs_.size()<<")"<<std::endl); 00059 return fs_[i].second; 00060 } 00061 //! Add a solution to the fitting solution set 00062 void add_solution(const algebra::Transformation3D &t,Float score); 00063 //! Sort solutions by cross-correlation scores 00064 void sort(); 00065 protected: 00066 std::vector<FittingSolution> fs_; 00067 }; 00068 00069 //! Local rigid fitting of a rigid body around a center point 00070 /** 00071 \brief Fit a set of particles to a density map around an anchor point. 00072 The fitting is assessed using the cross-correaltion score. 00073 The optimization is a standard MC/CG procedure. 00074 The function returns a list of solutions sortedo the cross-correlation 00075 score. 00076 \note The transformations are the rigid-body tranformation 00077 (with respect to an internal coordinate system). To get the actual 00078 delta transformation from the original placement of the rigid body, 00079 use the operator/ with a reference trasnforamtion outside of this 00080 function. 00081 \note The returned cross-correlation score is 1-cc, as we usually want to 00082 minimize a scroing function. Thus a score of 1 means no-correlation 00083 and a score of 0. is perfect correlation. 00084 \note The input rigid body should be also IMP::atom::Hierarchy 00085 \param[in] rb The rigid body to fit 00086 \param[in] radius_key The raidus key of the particles in the rigid body 00087 \param[in] weight_key The weight key of the particles in the rigid body 00088 \param[in] dmap The density map to fit to 00089 \param[in] anchor_centroid The point to fit the particles around 00090 \param[in] display_log If provided, then intermediate states 00091 in during the optimization procedure are printed 00092 \param[in] number_of_optimization_runs number of Monte Carlo optimizations 00093 \param[in] number_of_mc_steps number of steps in a Monte Carlo optimization 00094 \param[in] number_of_cg_steps number of Conjugate Gradients steps in 00095 a Monte Carlo step 00096 \param[in] max_translation maximum translation step in a MC optimization step 00097 \param[in] max_rotation maximum rotation step in a single MC optimization step 00098 \return the refined fitting solutions 00099 */ 00100 IMPEMEXPORT FittingSolutions local_rigid_fitting_around_point( 00101 core::RigidBody &rb, const FloatKey &radius_key, 00102 const FloatKey &weight_key, 00103 DensityMap *dmap, const algebra::VectorD<3> &anchor_centroid, 00104 OptimizerState *display_log, 00105 Int number_of_optimization_runs = 5, Int number_of_mc_steps = 10, 00106 Int number_of_cg_steps=100, 00107 Float max_translation=2., Float max_rotation=5.); 00108 00109 //! Local rigid fitting of a rigid body 00110 /** 00111 \brief Fit a set of particles to a density map around their centroid. 00112 The fitting is assessed using the cross-correaltion score. 00113 The optimization is a standard MC/CG procedure. 00114 The function returns a list of solutions sortedo the cross-correlation 00115 score. 00116 \note The transformations are the rigid-body tranformation 00117 (with respect to an internal coordinate system). To get the actual 00118 delta transformation from the original placement of the rigid body, 00119 use the operator/ with a reference trasnforamtion outside of this 00120 function. 00121 \note The returned cross-correlation score is 1-cc, as we usually want to 00122 minimize a scroing function. Thus a score of 1 means no-correlation 00123 and a score of 0. is perfect correlation. 00124 \note The input rigid body should be also IMP::atom::Hierarchy 00125 \param[in] rb The rigid body to fit 00126 \param[in] radius_key The raidus key of the particles in the rigid body 00127 \param[in] weight_key The weight key of the particles in the rigid body 00128 \param[in] dmap The density map to fit to 00129 \param[in] display_log If provided, then intermediate states 00130 in during the optimization procedure are printed 00131 \param[in] number_of_optimization_runs number of Monte Carlo optimizations 00132 \param[in] number_of_mc_steps number of steps in a Monte Carlo optimization 00133 \param[in] number_of_cg_steps number of Conjugate Gradients steps in 00134 a Monte Carlo step 00135 \param[in] max_translation maximum translation step in a MC optimization step 00136 \param[in] max_rotation maximum rotation step in a single MC optimization step 00137 \return the refined fitting solutions 00138 */ 00139 00140 inline FittingSolutions local_rigid_fitting( 00141 core::RigidBody &rb, const FloatKey &radius_key, 00142 const FloatKey &weight_key, 00143 DensityMap *dmap, 00144 OptimizerState *display_log=NULL, 00145 Int number_of_optimization_runs = 5, Int number_of_mc_steps = 10, 00146 Int number_of_cg_steps=100, 00147 Float max_translation=2., Float max_rotation=5.) { 00148 algebra::Vector3D rb_cen= 00149 IMP::core::get_centroid(core::XYZsTemp(core::get_leaves(atom::Hierarchy(rb)))); 00150 return local_rigid_fitting_around_point( 00151 rb, radius_key, weight_key,dmap, rb_cen,display_log, 00152 number_of_optimization_runs, number_of_mc_steps, 00153 number_of_cg_steps, max_translation, max_rotation); 00154 } 00155 00156 00157 //! Local rigid fitting of a rigid body around a set of center points 00158 /** 00159 \brief Fit a set of particles to a density map around each of the input points. 00160 The function apply local_rigid_fitting_around_point around each center. 00161 \note The input rigid body should be also IMP::atom::Hierarchy 00162 \param[in] rb The rigid body to fit 00163 \param[in] rad_key The raidus key of the particles in the rigid body 00164 \param[in] wei_key The weight key of the particles in the rigid body 00165 \param[in] dmap The density map to fit to 00166 \param[in] anchor_centroids The points to fit the particles around 00167 \param[in] display_log If provided, then intermediate states 00168 in during the optimization procedure are printed 00169 \param[in] number_of_optimization_runs number of Monte Carlo optimizations 00170 \param[in] number_of_mc_steps number of steps in a Monte Carlo optimization 00171 \param[in] number_of_cg_steps number of Conjugate Gradients steps in 00172 a Monte Carlo step 00173 \param[in] max_translation maximum translation step in a MC optimization step 00174 \param[in] max_rotation maximum rotation step in a single MC optimization step 00175 \return the refined fitting solutions 00176 */ 00177 IMPEMEXPORT FittingSolutions local_rigid_fitting_around_points( 00178 core::RigidBody &rb, 00179 const FloatKey &rad_key, const FloatKey &wei_key, 00180 DensityMap *dmap, const std::vector<algebra::VectorD<3> > &anchor_centroids, 00181 OptimizerState *display_log, 00182 Int number_of_optimization_runs = 5, Int number_of_mc_steps = 10, 00183 Int number_of_cg_steps=100, 00184 Float max_translation=2., Float max_rotation=5.); 00185 00186 00187 //! Local grid search rigid fitting 00188 /** 00189 \brief Fit a set of particles to a density map around their centroid. 00190 The fitting is assessed using the cross-correaltion score. 00191 The optimization is a grid search 00192 \note The transformations are not clustered. 00193 \note The returned cross-correlation score is 1-cc, as we usually want to 00194 minimize a scroing function. Thus a score of 1 means no-correlation 00195 and a score of 0. is perfect correlation. 00196 \param[in] ps The particles to be fitted (treated rigid) 00197 \param[in] rad_key The raidus key of the particles in the rigid body 00198 \param[in] wei_key The weight key of the particles in the rigid body 00199 \param[in] dmap The density map to fit to 00200 \param[in] max_voxels_translation Sample translations within 00201 -max_voxel_translation to max_voxel_translation 00202 \param[in] translation_step The translation sampling step 00203 \param[in] max_angle_in_radians Sample rotations with +- max_angle_in_radians 00204 around the current rotation 00205 \param[in] number_of_rotations The number of rotations to sample 00206 \return the refiend fitting solutions 00207 */ 00208 IMPEMEXPORT FittingSolutions local_rigid_fitting_grid_search( 00209 Particles &ps, const FloatKey &rad_key, 00210 const FloatKey &wei_key, 00211 DensityMap *dmap, 00212 Int max_voxels_translation=2, 00213 Int translation_step=1, 00214 Float max_angle_in_radians = 0.174, 00215 Int number_of_rotations = 100); 00216 00217 00218 //! Compute fitting scores for a given set of rigid transformations 00219 /** 00220 \brief Score how well a set of particles fit to the map in various 00221 rigid transformations. 00222 \param[in] ps The particles to be fitted (treated rigid) 00223 \param[in] em_map The density map to fit to 00224 \param[in] rad_key The raidus key of the particles in the rigid body 00225 \param[in] wei_key The weight key of the particles in the rigid body 00226 \param[in] fast_version If fast_version is used the sampled density map of the 00227 input particles (ps) is not resampled for each 00228 transformation but instead the corresponding grid 00229 is rotated. This option significantly improves the 00230 running times but the returned scores are less accurate 00231 \param[in] transformations A set of rigid transformations 00232 \return The scored fitting solutions 00233 \note the function assumes the density map holds its density 00234 */ 00235 IMPEMEXPORT FittingSolutions compute_fitting_scores(Particles &ps, 00236 DensityMap *em_map, 00237 const FloatKey &rad_key, const FloatKey &wei_key, 00238 const std::vector<IMP::algebra::Transformation3D>& transformations, 00239 bool fast_version=false); 00240 //! Compute fitting scores for a given set of rigid transformations 00241 /** 00242 \brief Score how well a set of particles fit a map 00243 \param[in] ps The particles to be fitted 00244 \param[in] em_map The density map to fit to 00245 \param[in] rad_key The raidus key of the particles in the rigid body 00246 \param[in] wei_key The weight key of the particles in the rigid body 00247 \note the function assumes the density map holds its density 00248 */ 00249 IMPEMEXPORT Float compute_fitting_score(Particles &ps, 00250 DensityMap *em_map, 00251 FloatKey rad_key=core::XYZR::get_default_radius_key(), 00252 FloatKey wei_key=atom::Mass::get_mass_key()); 00253 IMPEM_END_NAMESPACE 00254 #endif /* IMPEM_RIGID_FITTING_H */