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:
76  static constexpr double DEF_OPTIMIZATION_STOP_GRADIENT = 1.0;
78  static constexpr std::size_t DEF_MAX_OPTIMIZATION_ITERATIONS = 20;
80  static constexpr unsigned int DEF_RESULT_SELECTION_MODE = AlignmentResultSelectionMode::BEST_PER_REFERENCE_SET;
82  static constexpr double DEF_SYMMETRY_THRESHOLD = 0.15;
84  static constexpr std::size_t DEF_NUM_RANDOM_STARTS = 4;
86  static constexpr double DEF_MAX_RANDOM_TRANSLATION = 2.0;
87 
89  typedef std::shared_ptr<FastGaussianShapeAlignment> SharedPointer;
90 
92  typedef ResultList::const_iterator ConstResultIterator;
94  typedef ResultList::iterator ResultIterator;
95 
97  typedef std::function<double(const AlignmentResult&)> ScoringFunction;
99  typedef std::function<bool(const AlignmentResult&, const AlignmentResult&)> ResultCompareFunction;
100 
105 
111 
117 
119 
124 
126 
132 
138 
144 
150 
155  void setResultSelectionMode(unsigned int mode);
156 
161  unsigned int getResultSelectionMode() const;
162 
167  void performAlignment(bool perf_align);
168 
173  bool performAlignment() const;
174 
179  void optimizeOverlap(bool optimize);
180 
185  bool optimizeOverlap() const;
186 
191  void greedyOptimization(bool greedy);
192 
197  bool greedyOptimization() const;
198 
203  void setMaxNumOptimizationIterations(std::size_t max_iter);
204 
209  std::size_t getMaxNumOptimizationIterations() const;
210 
215  void setOptimizationStopGradient(double grad_norm);
216 
222 
227 
233  void addReferenceShape(const GaussianShape& shape, bool new_set = true);
234 
240  void addReferenceShapes(const GaussianShapeSet& shapes, bool new_set = true);
241 
246  std::size_t getNumReferenceShapes() const;
247 
252  void genShapeCenterStarts(bool generate);
253 
258  bool genShapeCenterStarts() const;
259 
264  void genColorCenterStarts(bool generate);
265 
270  bool genColorCenterStarts() const;
271 
276  void genNonColorCenterStarts(bool generate);
277 
283 
288  void genRandomStarts(bool generate);
289 
294  bool genRandomStarts() const;
295 
300  void genForAlignedShapeCenters(bool generate);
301 
307 
312  void genForReferenceShapeCenters(bool generate);
313 
319 
324  void genForLargerShapeCenters(bool generate);
325 
331 
336  void setSymmetryThreshold(double thresh);
337 
343 
348  void setMaxRandomTranslation(double max_trans);
349 
354  double getMaxRandomTranslation() const;
355 
360  void setNumRandomStarts(std::size_t num_starts);
361 
366  std::size_t getNumRandomStarts() const;
367 
372  void setRandomSeed(unsigned int seed);
373 
379  bool align(const GaussianShape& shape);
380 
386  bool align(const GaussianShapeSet& shapes);
387 
392  std::size_t getNumResults() const;
393 
400  const AlignmentResult& getResult(std::size_t idx) const;
401 
408  AlignmentResult& getResult(std::size_t idx);
409 
415 
421 
427 
433 
439 
445 
451 
457 
458  private:
459  struct ShapeData
460  {
461 
462  struct Element
463  {
464 
465  std::size_t color;
467  double radius;
468  double delta;
469  double weightFactor;
470  double volume;
471  };
472 
473  typedef std::vector<Element> ElementArray;
474 
475  ElementArray elements;
476  std::size_t colElemOffs;
477  std::size_t setIndex;
478  std::size_t index;
479  unsigned int symClass;
481  double selfOverlap;
482  double colSelfOverlap;
483  bool equalNonColDelta;
484  };
485 
486  typedef std::pair<std::size_t, std::size_t> ResultID;
487 
488  void alignAndProcessResults(std::size_t ref_idx, std::size_t al_idx);
489  void processResult(AlignmentResult& res, std::size_t ref_idx, std::size_t al_idx);
490 
491  void setupShapeData(const GaussianShape& shape, ShapeData& data, bool ref);
492  void setupShapeDataElement(const GaussianShape::Element& gs_elem, ShapeData::Element& sd_elem) const;
493 
494  void prepareForAlignment();
495 
496  bool generateStartTransforms(const ShapeData& ref_data);
497 
498  void generateTransformsForElementCenters(const ShapeData& data, unsigned int axes_swap_flags, bool ref_shape);
499  void generateTransforms(const Math::Vector3D& ctr_trans, unsigned int axes_swap_flags);
500 
501  template <typename QE>
502  void addStartTransform(Math::Vector3D::ConstPointer ctr_trans_data, const Math::QuaternionExpression<QE>& rot_quat);
503 
504  void transformAlignedShape();
505 
506  double calcAlignmentFunctionValue(const QuaternionTransformation& xform_quat);
507  double calcAlignmentFunctionGradient(const QuaternionTransformation& xform_quat, QuaternionTransformation& xform_grad);
508 
509  double calcOverlap(const ShapeData& ref_data, const ShapeData& ovl_data, bool color) const;
510  double calcOverlapGradient(const ShapeData& ref_data, Math::Vector3DArray& grad) const;
511 
512  bool getResultIndex(const ResultID& res_id, std::size_t& res_idx);
513 
514  typedef std::vector<ShapeData> ShapeDataArray;
515  typedef std::unordered_map<ResultID, std::size_t, boost::hash<ResultID> > ResultIndexMap;
516  typedef std::vector<QuaternionTransformation> StartTransformList;
517  typedef boost::random::mt11213b RandomEngine;
519 
520  bool perfAlignment;
521  bool optOverlap;
522  bool greedyOpt;
523  std::size_t maxNumOptIters;
524  double optStopGrad;
525  unsigned int resultSelMode;
526  ResultCompareFunction resultCmpFunc;
527  ScoringFunction scoringFunc;
528  ShapeDataArray refShapeData;
529  ShapeData algdShapeData;
530  ResultIndexMap resIndexMap;
531  ResultList results;
532  std::size_t currSetIndex;
533  std::size_t currShapeIndex;
534  bool shapeCtrStarts;
535  bool colCtrStarts;
536  bool nonColCtrStarts;
537  bool randomStarts;
538  bool genForAlgdShape;
539  bool genForRefShape;
540  bool genForLargerShape;
541  double symThreshold;
542  double maxRandomTrans;
543  std::size_t numRandomStarts;
544  std::size_t numSubTransforms;
545  RandomEngine randomEngine;
546  StartTransformList startTransforms;
547  Math::Vector3DArray startPoseCoords;
548  Math::Vector3DArray optPoseCoordsGrad;
549  BFGSMinimizer minimizer;
550  Math::Matrix4D xformMatrix;
551  QuaternionTransformation normXformQuat;
552  std::size_t currRefShapeIdx;
553  };
554  } // namespace Shape
555 } // namespace CDPL
556 
557 #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:1876
CRTP base class for all quaternion expression types.
Definition: Expression.hpp:142
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:92
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:89
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:99
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 (see namespace Shape::AlignmentResultSelectionMode).
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:94
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:97
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:85
CVector< double, 3 > Vector3D
Bounded 3 element vector holding floating point values of type double.
Definition: Vector.hpp:2937
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:463
Math::Vector3D center
Definition: FastGaussianShapeAlignment.hpp:466
double radius
Definition: FastGaussianShapeAlignment.hpp:467
std::size_t color
Definition: FastGaussianShapeAlignment.hpp:465
double volume
Definition: FastGaussianShapeAlignment.hpp:470
double weightFactor
Definition: FastGaussianShapeAlignment.hpp:469
double delta
Definition: FastGaussianShapeAlignment.hpp:468