Chemical Data Processing Library C++ API - Version 1.4.0
FastGaussianShapeAlignment.hpp
Go to the documentation of this file.
1 /*
2  * FastGaussianShapeAlignment.hpp
3  *
4  * This file is part of the Chemical Data Processing Toolkit
5  *
6  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; see the file COPYING. If not, write to
20  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 
29 #ifndef CDPL_SHAPE_FASTGAUSSIANSHAPEALIGNMENT_HPP
30 #define CDPL_SHAPE_FASTGAUSSIANSHAPEALIGNMENT_HPP
31 
32 #include <vector>
33 #include <cstddef>
34 #include <utility>
35 #include <unordered_map>
36 #include <memory>
37 #include <functional>
38 
39 #include <boost/functional/hash.hpp>
40 #include <boost/random/mersenne_twister.hpp>
41 
42 #include "CDPL/Shape/APIPrefix.hpp"
47 #include "CDPL/Math/Matrix.hpp"
51 
52 
53 namespace CDPL
54 {
55 
56  namespace Shape
57  {
58 
70  {
71 
72  typedef std::vector<AlignmentResult> ResultList;
73 
74  public:
78  static constexpr double DEF_OPTIMIZATION_STOP_GRADIENT = 1.0;
79 
83  static constexpr std::size_t DEF_MAX_OPTIMIZATION_ITERATIONS = 20;
84 
88  static constexpr unsigned int DEF_RESULT_SELECTION_MODE = AlignmentResultSelectionMode::BEST_PER_REFERENCE_SET;
89 
93  static constexpr double DEF_SYMMETRY_THRESHOLD = 0.15;
94 
98  static constexpr std::size_t DEF_NUM_RANDOM_STARTS = 4;
99 
103  static constexpr double DEF_MAX_RANDOM_TRANSLATION = 2.0;
104 
108  typedef std::shared_ptr<FastGaussianShapeAlignment> SharedPointer;
109 
113  typedef ResultList::const_iterator ConstResultIterator;
114 
118  typedef ResultList::iterator ResultIterator;
119 
123  typedef std::function<double(const AlignmentResult&)> ScoringFunction;
124 
128  typedef std::function<bool(const AlignmentResult&, const AlignmentResult&)> ResultCompareFunction;
129 
134 
140 
146 
148 
153 
155 
161 
167 
173 
179 
184  void setResultSelectionMode(unsigned int mode);
185 
190  unsigned int getResultSelectionMode() const;
191 
196  void performAlignment(bool perf_align);
197 
202  bool performAlignment() const;
203 
208  void optimizeOverlap(bool optimize);
209 
214  bool optimizeOverlap() const;
215 
220  void greedyOptimization(bool greedy);
221 
226  bool greedyOptimization() const;
227 
232  void setMaxNumOptimizationIterations(std::size_t max_iter);
233 
238  std::size_t getMaxNumOptimizationIterations() const;
239 
244  void setOptimizationStopGradient(double grad_norm);
245 
251 
256 
262  void addReferenceShape(const GaussianShape& shape, bool new_set = true);
263 
269  void addReferenceShapes(const GaussianShapeSet& shapes, bool new_set = true);
270 
275  std::size_t getNumReferenceShapes() const;
276 
281  void genShapeCenterStarts(bool generate);
282 
287  bool genShapeCenterStarts() const;
288 
293  void genColorCenterStarts(bool generate);
294 
299  bool genColorCenterStarts() const;
300 
305  void genNonColorCenterStarts(bool generate);
306 
312 
317  void genRandomStarts(bool generate);
318 
323  bool genRandomStarts() const;
324 
329  void genForAlignedShapeCenters(bool generate);
330 
336 
341  void genForReferenceShapeCenters(bool generate);
342 
348 
353  void genForLargerShapeCenters(bool generate);
354 
360 
365  void setSymmetryThreshold(double thresh);
366 
372 
377  void setMaxRandomTranslation(double max_trans);
378 
383  double getMaxRandomTranslation() const;
384 
389  void setNumRandomStarts(std::size_t num_starts);
390 
395  std::size_t getNumRandomStarts() const;
396 
401  void setRandomSeed(unsigned int seed);
402 
408  bool align(const GaussianShape& shape);
409 
415  bool align(const GaussianShapeSet& shapes);
416 
421  std::size_t getNumResults() const;
422 
429  const AlignmentResult& getResult(std::size_t idx) const;
430 
437  AlignmentResult& getResult(std::size_t idx);
438 
444 
450 
456 
462 
468 
474 
480 
486 
487  private:
488  struct ShapeData
489  {
490 
491  struct Element
492  {
493 
494  std::size_t color;
496  double radius;
497  double delta;
498  double weightFactor;
499  double volume;
500  };
501 
502  typedef std::vector<Element> ElementArray;
503 
504  ElementArray elements;
505  std::size_t colElemOffs;
506  std::size_t setIndex;
507  std::size_t index;
508  unsigned int symClass;
510  double selfOverlap;
511  double colSelfOverlap;
512  bool equalNonColDelta;
513  };
514 
515  typedef std::pair<std::size_t, std::size_t> ResultID;
516 
517  void alignAndProcessResults(std::size_t ref_idx, std::size_t al_idx);
518  void processResult(AlignmentResult& res, std::size_t ref_idx, std::size_t al_idx);
519 
520  void setupShapeData(const GaussianShape& shape, ShapeData& data, bool ref);
521  void setupShapeDataElement(const GaussianShape::Element& gs_elem, ShapeData::Element& sd_elem) const;
522 
523  void prepareForAlignment();
524 
525  bool generateStartTransforms(const ShapeData& ref_data);
526 
527  void generateTransformsForElementCenters(const ShapeData& data, unsigned int axes_swap_flags, bool ref_shape);
528  void generateTransforms(const Math::Vector3D& ctr_trans, unsigned int axes_swap_flags);
529 
530  template <typename QE>
531  void addStartTransform(Math::Vector3D::ConstPointer ctr_trans_data, const Math::QuaternionExpression<QE>& rot_quat);
532 
533  void transformAlignedShape();
534 
535  double calcAlignmentFunctionValue(const QuaternionTransformation& xform_quat);
536  double calcAlignmentFunctionGradient(const QuaternionTransformation& xform_quat, QuaternionTransformation& xform_grad);
537 
538  double calcOverlap(const ShapeData& ref_data, const ShapeData& ovl_data, bool color) const;
539  double calcOverlapGradient(const ShapeData& ref_data, Math::Vector3DArray& grad) const;
540 
541  bool getResultIndex(const ResultID& res_id, std::size_t& res_idx);
542 
543  typedef std::vector<ShapeData> ShapeDataArray;
544  typedef std::unordered_map<ResultID, std::size_t, boost::hash<ResultID> > ResultIndexMap;
545  typedef std::vector<QuaternionTransformation> StartTransformList;
546  typedef boost::random::mt11213b RandomEngine;
548 
549  bool perfAlignment;
550  bool optOverlap;
551  bool greedyOpt;
552  std::size_t maxNumOptIters;
553  double optStopGrad;
554  unsigned int resultSelMode;
555  ResultCompareFunction resultCmpFunc;
556  ScoringFunction scoringFunc;
557  ShapeDataArray refShapeData;
558  ShapeData algdShapeData;
559  ResultIndexMap resIndexMap;
560  ResultList results;
561  std::size_t currSetIndex;
562  std::size_t currShapeIndex;
563  bool shapeCtrStarts;
564  bool colCtrStarts;
565  bool nonColCtrStarts;
566  bool randomStarts;
567  bool genForAlgdShape;
568  bool genForRefShape;
569  bool genForLargerShape;
570  double symThreshold;
571  double maxRandomTrans;
572  std::size_t numRandomStarts;
573  std::size_t numSubTransforms;
574  RandomEngine randomEngine;
575  StartTransformList startTransforms;
576  Math::Vector3DArray startPoseCoords;
577  Math::Vector3DArray optPoseCoordsGrad;
578  BFGSMinimizer minimizer;
579  Math::Matrix4D xformMatrix;
580  QuaternionTransformation normXformQuat;
581  std::size_t currRefShapeIdx;
582  };
583  } // namespace Shape
584 } // namespace CDPL
585 
586 #endif // CDPL_SHAPE_FASTGAUSSIANSHAPEALIGNMENT_HPP
Definition of constants in namespace CDPL::Shape::AlignmentResultSelectionMode.
Definition of class CDPL::Shape::AlignmentResult.
Implementation of the BFGS optimization algorithm.
Definition of class CDPL::Shape::GaussianShapeSet.
Definition of matrix data types.
Definition of various quaternion expression types and operations.
Definition of the type CDPL::Shape::QuaternionTransformation.
Definition of the preprocessor macro CDPL_SHAPE_API.
#define CDPL_SHAPE_API
Tells the compiler/linker which classes, functions and variables are part of the library API.
Definition of class CDPL::Math::VectorArray.
Fixed-size vector of dimension N backed by a C-array (no dynamic allocation).
Definition: Vector.hpp:2047
CRTP base class of all quaternion expression types.
Definition: Expression.hpp:148
Result of a Gaussian-shape alignment between a reference and an aligned shape.
Definition: AlignmentResult.hpp:52
High-level driver for the fast alignment of Gaussian shapes against a set of reference shapes.
Definition: FastGaussianShapeAlignment.hpp:70
ResultList::const_iterator ConstResultIterator
A constant iterator over the alignment results.
Definition: FastGaussianShapeAlignment.hpp:113
void setSymmetryThreshold(double thresh)
Sets the relative threshold for treating two principal moments as equal.
bool genColorCenterStarts() const
Tells whether starting transformations at color (pharmacophore) feature centers are generated.
AlignmentResult & getResult(std::size_t idx)
Returns the alignment result at index idx.
void genForAlignedShapeCenters(bool generate)
Specifies whether element-/color-center starts shall be generated for centers of the aligned shape.
std::size_t getNumRandomStarts() const
Returns the currently configured number of random starts.
void setNumRandomStarts(std::size_t num_starts)
Sets the number of random starting transformations.
const ResultCompareFunction & getResultCompareFunction() const
Returns the currently configured result-compare function.
void setMaxRandomTranslation(double max_trans)
Sets the maximum random translation magnitude applied to random starts.
std::shared_ptr< FastGaussianShapeAlignment > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated FastGaussianShapeAlignment instan...
Definition: FastGaussianShapeAlignment.hpp:108
void setMaxNumOptimizationIterations(std::size_t max_iter)
Sets the maximum number of overlap-optimization iterations.
std::size_t getNumResults() const
Returns the number of stored alignment results.
void optimizeOverlap(bool optimize)
Specifies whether the overlap shall be optimized iteratively after the initial alignment.
ResultIterator getResultsBegin()
Returns a mutable iterator pointing to the first alignment result.
FastGaussianShapeAlignment(const GaussianShapeSet &ref_shapes)
Constructs the FastGaussianShapeAlignment instance with the shapes in ref_shapes as the reference set...
std::function< bool(const AlignmentResult &, const AlignmentResult &)> ResultCompareFunction
Type of the function used to compare two alignment results.
Definition: FastGaussianShapeAlignment.hpp:128
void clearReferenceShapes()
Removes all reference shapes and reference shape sets.
void genColorCenterStarts(bool generate)
Enables or disables the generation of starting transformations at color (pharmacophore) feature cente...
void setResultCompareFunction(const ResultCompareFunction &func)
Specifies the function used to compare two alignment results for sorting and filtering.
bool genForLargerShapeCenters() const
Tells whether element-/color-center starts are generated for centers of the shape with more elements ...
ResultIterator getResultsEnd()
Returns a mutable iterator pointing one past the last alignment result.
void genForReferenceShapeCenters(bool generate)
Specifies whether element-/color-center starts shall be generated for centers of the reference shape.
ResultIterator end()
Returns a mutable iterator pointing one past the last alignment result (range-based for support).
double getSymmetryThreshold()
Returns the currently configured symmetry threshold.
void setRandomSeed(unsigned int seed)
Sets the seed used by the random number generator that produces the random starts.
void setOptimizationStopGradient(double grad_norm)
Sets the gradient norm at which the overlap optimization is stopped.
unsigned int getResultSelectionMode() const
Returns the currently configured alignment-result selection mode.
ConstResultIterator begin() const
Returns a constant iterator pointing to the first alignment result (range-based for support).
ConstResultIterator getResultsBegin() const
Returns a constant iterator pointing to the first alignment result.
void setResultSelectionMode(unsigned int mode)
Sets the alignment-result selection mode.
bool genShapeCenterStarts() const
Tells whether a starting transformation at the shape centroid is generated.
bool greedyOptimization() const
Tells whether the overlap optimization uses a greedy strategy.
std::size_t getMaxNumOptimizationIterations() const
Returns the currently configured maximum number of overlap-optimization iterations.
void genNonColorCenterStarts(bool generate)
Enables or disables the generation of starting transformations at non-color (shape) element centers.
void genRandomStarts(bool generate)
Enables or disables the generation of random starting transformations.
bool genRandomStarts() const
Tells whether random starting transformations are generated.
void setScoringFunction(const ScoringFunction &func)
Specifies the function used to score an alignment result.
void greedyOptimization(bool greedy)
Specifies whether the overlap optimization shall use a greedy strategy that stops at the first local ...
const AlignmentResult & getResult(std::size_t idx) const
Returns the alignment result at index idx.
bool performAlignment() const
Tells whether the actual alignment is performed.
bool genForReferenceShapeCenters() const
Tells whether element-/color-center starts are generated for centers of the reference shape.
std::size_t getNumReferenceShapes() const
Returns the total number of reference shapes.
const ScoringFunction & getScoringFunction() const
Returns the currently configured scoring function.
bool align(const GaussianShape &shape)
Aligns shape against all reference shapes.
void performAlignment(bool perf_align)
Specifies whether the actual alignment shall be performed (vs. only evaluating overlaps in the initia...
ResultList::iterator ResultIterator
A mutable iterator over the alignment results.
Definition: FastGaussianShapeAlignment.hpp:118
double getOptimizationStopGradient() const
Returns the currently configured overlap-optimization stop gradient.
bool optimizeOverlap() const
Tells whether the overlap is optimized iteratively.
FastGaussianShapeAlignment(const GaussianShape &ref_shape)
Constructs the FastGaussianShapeAlignment instance with ref_shape as the single reference shape.
void genForLargerShapeCenters(bool generate)
Specifies whether element-/color-center starts shall be generated for centers of the shape with more ...
ConstResultIterator end() const
Returns a constant iterator pointing one past the last alignment result (range-based for support).
void genShapeCenterStarts(bool generate)
Enables or disables the generation of a starting transformation at the shape centroid.
bool genNonColorCenterStarts() const
Tells whether starting transformations at non-color (shape) element centers are generated.
std::function< double(const AlignmentResult &)> ScoringFunction
Type of the function used to score an alignment result.
Definition: FastGaussianShapeAlignment.hpp:123
FastGaussianShapeAlignment(const FastGaussianShapeAlignment &alignment)=delete
void addReferenceShapes(const GaussianShapeSet &shapes, bool new_set=true)
Adds the shapes in shapes to the reference shapes.
double getMaxRandomTranslation() const
Returns the currently configured maximum random translation.
FastGaussianShapeAlignment & operator=(const FastGaussianShapeAlignment &alignment)=delete
ResultIterator begin()
Returns a mutable iterator pointing to the first alignment result (range-based for support).
bool genForAlignedShapeCenters() const
Tells whether element-/color-center starts are generated for centers of the aligned shape.
FastGaussianShapeAlignment()
Constructs the FastGaussianShapeAlignment instance.
bool align(const GaussianShapeSet &shapes)
Aligns each shape in shapes against all reference shapes.
ConstResultIterator getResultsEnd() const
Returns a constant iterator pointing one past the last alignment result.
void addReferenceShape(const GaussianShape &shape, bool new_set=true)
Adds shape to the reference shapes.
Data structure storing a set of (shared) Gaussian shapes.
Definition: GaussianShapeSet.hpp:52
Data structure representing a single sphere of the Gaussian shape, characterized by a position,...
Definition: GaussianShape.hpp:58
Data type for the descripton of arbitrary shapes composed of spheres approximated by gaussian functio...
Definition: GaussianShape.hpp:51
VectorArray< Vector3D > Vector3DArray
Array storing vectors of type Math::Vector3D.
Definition: VectorArray.hpp:87
CVector< double, 3 > Vector3D
Bounded 3 element vector holding floating-point values of type double.
Definition: Vector.hpp:3218
constexpr unsigned int BEST_PER_REFERENCE_SET
Keep only the best alignment result per reference shape set.
Definition: AlignmentResultSelectionMode.hpp:63
CDPL_SHAPE_API void transform(GaussianShape &shape, const Math::Matrix4D &xform)
Applies an affine transformation to the element positions of shape.
The namespace of the Chemical Data Processing Library.
Definition: FastGaussianShapeAlignment.hpp:492
Math::Vector3D center
Definition: FastGaussianShapeAlignment.hpp:495
double radius
Definition: FastGaussianShapeAlignment.hpp:496
std::size_t color
Definition: FastGaussianShapeAlignment.hpp:494
double volume
Definition: FastGaussianShapeAlignment.hpp:499
double weightFactor
Definition: FastGaussianShapeAlignment.hpp:498
double delta
Definition: FastGaussianShapeAlignment.hpp:497