Chemical Data Processing Library C++ API - Version 1.4.0
GaussianShapeAlignment.hpp
Go to the documentation of this file.
1 /*
2  * GaussianShapeAlignment.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_GAUSSIANSHAPEALIGNMENT_HPP
30 #define CDPL_SHAPE_GAUSSIANSHAPEALIGNMENT_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/iterator/transform_iterator.hpp>
41 
42 #include "CDPL/Shape/APIPrefix.hpp"
48 #include "CDPL/Math/Matrix.hpp"
50 
51 
52 namespace CDPL
53 {
54 
55  namespace Shape
56  {
57 
67  {
68 
69  typedef std::vector<AlignmentResult> ResultList;
70  typedef std::vector<GaussianShapeFunction*> ShapeFunctionList;
71 
72  typedef const GaussianShape& (*GetShapeFunction)(const GaussianShapeFunction*);
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 std::size_t DEF_MAX_PRODUCT_ORDER = 1;
82  static constexpr unsigned int DEF_RESULT_SELECTION_MODE = AlignmentResultSelectionMode::BEST_PER_REFERENCE_SET;
84  static constexpr double DEF_DISTANCE_CUTOFF = 0.0;
85 
87  typedef std::shared_ptr<GaussianShapeAlignment> SharedPointer;
88 
90  typedef ResultList::const_iterator ConstResultIterator;
92  typedef ResultList::iterator ResultIterator;
94  typedef boost::transform_iterator<GetShapeFunction, ShapeFunctionList::const_iterator> ConstShapeIterator;
95 
101  typedef std::function<double(const AlignmentResult&)> ScoringFunction;
103  typedef std::function<bool(const AlignmentResult&, const AlignmentResult&)> ResultCompareFunction;
104 
109 
115 
121 
122  GaussianShapeAlignment(const GaussianShapeAlignment& alignment) = delete;
123 
128 
130 
136 
142 
148 
154 
160 
166 
172 
178 
184 
190 
196 
202 
208 
214 
220 
226 
231  void setResultSelectionMode(unsigned int mode);
232 
237  unsigned int getResultSelectionMode() const;
238 
243  void calcSelfOverlaps(bool calc);
244 
249  bool calcSelfOverlaps() const;
250 
255  void calcColorSelfOverlaps(bool calc);
256 
261  bool calcColorSelfOverlaps() const;
262 
267  void calcColorOverlaps(bool calc);
268 
273  bool calcColorOverlaps() const;
274 
279  void performAlignment(bool perf_align);
280 
285  bool performAlignment() const;
286 
291  void optimizeOverlap(bool optimize);
292 
297  bool optimizeOverlap() const;
298 
303  void greedyOptimization(bool greedy);
304 
309  bool greedyOptimization() const;
310 
315  void setMaxNumOptimizationIterations(std::size_t max_iter);
316 
321  std::size_t getMaxNumOptimizationIterations() const;
322 
327  void setOptimizationStopGradient(double grad_norm);
328 
334 
339  void setMaxOrder(std::size_t max_order);
340 
345  std::size_t getMaxOrder() const;
346 
351  void setDistanceCutoff(double cutoff);
352 
357  double getDistanceCutoff() const;
358 
363 
369  void addReferenceShape(const GaussianShape& shape, bool new_set = true);
370 
376  void addReferenceShapes(const GaussianShapeSet& shapes, bool new_set = true);
377 
382  std::size_t getNumReferenceShapes() const;
383 
390  const GaussianShape& getReferenceShape(std::size_t idx) const;
391 
397 
403 
409  bool align(const GaussianShape& shape);
410 
416  bool align(const GaussianShapeSet& shapes);
417 
422  std::size_t getNumResults() const;
423 
430  const AlignmentResult& getResult(std::size_t idx) const;
431 
438  AlignmentResult& getResult(std::size_t idx);
439 
445 
451 
457 
463 
464  private:
465  typedef GaussianShape::SharedPointer GaussianShapePtr;
466 
467  struct ShapeMetaData
468  {
469 
470  std::size_t setIndex;
471  std::size_t index;
472  unsigned int symClass;
474  double selfOverlap;
475  double colSelfOverlap;
476  GaussianShapePtr shape;
477  };
478 
479  typedef std::pair<std::size_t, std::size_t> ResultID;
480 
481  void prepareForAlignment(GaussianShapeFunction& func, ShapeMetaData& data, bool ref);
482 
483  void processResults(std::size_t ref_idx, std::size_t al_idx);
484 
485  bool getResultIndex(const ResultID& res_id, std::size_t& res_idx);
486 
487  GaussianShapeFunction* allocShapeFunction(const GaussianShape& shape);
488 
489  typedef Util::ObjectStack<GaussianShapeFunction> ShapeFunctionCache;
490  typedef std::vector<ShapeMetaData> ShapeMetaDataArray;
491  typedef std::unordered_map<ResultID, std::size_t, boost::hash<ResultID> > ResultIndexMap;
492 
493  ShapeFunctionCache shapeFuncCache;
494  bool calcSlfOverlaps;
495  bool calcColSlfOverlaps;
496  unsigned int resultSelMode;
497  ResultCompareFunction resultCmpFunc;
498  ScoringFunction scoringFunc;
499  GaussianShapeFunctionAlignment shapeFuncAlmnt;
500  ShapeFunctionList refShapeFuncs;
501  ShapeMetaDataArray refShapeMetaData;
502  GaussianShapeFunction algdShapeFunc;
503  ShapeMetaData algdShapeMetaData;
504  ResultIndexMap resIndexMap;
505  ResultList results;
506  std::size_t currSetIndex;
507  std::size_t currShapeIndex;
508  };
509  } // namespace Shape
510 } // namespace CDPL
511 
512 #endif // CDPL_SHAPE_GAUSSIANSHAPEALIGNMENT_HPP
Definition of constants in namespace CDPL::Shape::AlignmentResultSelectionMode.
Definition of class CDPL::Shape::AlignmentResult.
Definition of class CDPL::Shape::GaussianShapeFunctionAlignment.
Definition of class CDPL::Shape::GaussianShapeFunction.
Definition of class CDPL::Shape::GaussianShapeSet.
Definition of matrix data types.
Definition of class CDPL::Util::ObjectStack.
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.
Result of a Gaussian-shape alignment between a reference and an aligned shape.
Definition: AlignmentResult.hpp:52
Shape::GaussianShapeOverlapFunction implementation that uses two approximation techniques to trade so...
Definition: FastGaussianShapeOverlapFunction.hpp:58
Abstract base class for generators of starting transformations used to seed Gaussian-shape overlap op...
Definition: GaussianShapeAlignmentStartGenerator.hpp:57
High-level driver for the alignment of Gaussian shapes against a set of reference shapes.
Definition: GaussianShapeAlignment.hpp:67
PrincipalAxesAlignmentStartGenerator & getDefaultStartGenerator()
Returns the built-in principal-axes alignment-start generator.
ResultList::const_iterator ConstResultIterator
A constant iterator over the alignment results.
Definition: GaussianShapeAlignment.hpp:90
ConstShapeIterator getReferenceShapesEnd() const
Returns a constant iterator pointing one past the last reference shape.
bool calcColorOverlaps() const
Tells whether color overlaps are computed in addition to shape overlaps.
GaussianShapeFunctionAlignment::ColorFilterFunction ColorFilterFunction
Type of the function used to filter color (pharmacophore) features by type.
Definition: GaussianShapeAlignment.hpp:97
std::size_t getMaxOrder() const
Returns the currently configured maximum order of the Gaussian-product expansion.
GaussianShapeFunctionAlignment::ColorMatchFunction ColorMatchFunction
Type of the function used to decide whether two color features match.
Definition: GaussianShapeAlignment.hpp:99
GaussianShapeAlignment(const GaussianShapeSet &ref_shapes)
Constructs the GaussianShapeAlignment instance with the shapes in ref_shapes as the reference set.
const PrincipalAxesAlignmentStartGenerator & getDefaultStartGenerator() const
Returns the built-in principal-axes alignment-start generator.
void addReferenceShape(const GaussianShape &shape, bool new_set=true)
Adds shape to the reference shapes.
std::shared_ptr< GaussianShapeAlignment > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated GaussianShapeAlignment instances.
Definition: GaussianShapeAlignment.hpp:87
void clearReferenceShapes()
Removes all reference shapes and reference shape sets.
void setDistanceCutoff(double cutoff)
Sets the distance cutoff for pruning negligible overlap contributions.
boost::transform_iterator< GetShapeFunction, ShapeFunctionList::const_iterator > ConstShapeIterator
A constant iterator over the reference shapes.
Definition: GaussianShapeAlignment.hpp:94
void setScoringFunction(const ScoringFunction &func)
Specifies the function used to score an alignment result.
std::function< bool(const AlignmentResult &, const AlignmentResult &)> ResultCompareFunction
Type of the function used to compare two alignment results.
Definition: GaussianShapeAlignment.hpp:103
void calcColorSelfOverlaps(bool calc)
Specifies whether color self-overlaps shall be computed for the involved shapes.
bool performAlignment() const
Tells whether the actual alignment is performed.
const FastGaussianShapeOverlapFunction & getDefaultOverlapFunction() const
Returns the built-in default overlap function.
FastGaussianShapeOverlapFunction & getDefaultOverlapFunction()
Returns the built-in default overlap function.
ResultIterator getResultsBegin()
Returns a mutable iterator pointing to the first alignment result.
AlignmentResult & getResult(std::size_t idx)
Returns the alignment result at index idx.
const GaussianShape & getReferenceShape(std::size_t idx) const
Returns the reference shape at index idx.
GaussianShapeAlignment & operator=(const GaussianShapeAlignment &alignment)=delete
std::size_t getNumReferenceShapes() const
Returns the total number of reference shapes.
const ColorFilterFunction & getColorFilterFunction() const
Returns the currently configured color-filter function.
double getOptimizationStopGradient() const
Returns the currently configured overlap-optimization stop gradient.
const ResultCompareFunction & getResultCompareFunction() const
Returns the currently configured result-compare function.
bool greedyOptimization() const
Tells whether the overlap optimization uses a greedy strategy.
ConstShapeIterator getReferenceShapesBegin() const
Returns a constant iterator pointing to the first reference shape.
GaussianShapeAlignment(const GaussianShapeAlignment &alignment)=delete
const ColorMatchFunction & getColorMatchFunction() const
Returns the currently configured color-match function.
double getDistanceCutoff() const
Returns the currently configured distance cutoff.
void setStartGenerator(GaussianShapeAlignmentStartGenerator &gen)
Specifies the alignment-start generator used to seed the overlap optimization.
ResultIterator getResultsEnd()
Returns a mutable iterator pointing one past the last alignment result.
bool calcSelfOverlaps() const
Tells whether shape self-overlaps are computed for the involved shapes.
void optimizeOverlap(bool optimize)
Specifies whether the overlap shall be optimized iteratively after the initial alignment.
GaussianShapeOverlapFunction & getOverlapFunction() const
Returns the currently configured overlap function.
GaussianShapeAlignmentStartGenerator & getStartGenerator() const
Returns the currently configured alignment-start generator.
std::size_t getMaxNumOptimizationIterations() const
Returns the currently configured maximum number of overlap-optimization iterations.
void setResultSelectionMode(unsigned int mode)
Sets the alignment-result selection mode (see namespace Shape::AlignmentResultSelectionMode).
ConstResultIterator getResultsBegin() const
Returns a constant iterator pointing to the first alignment result.
bool align(const GaussianShapeSet &shapes)
Aligns each shape in shapes against all reference shapes.
void setOverlapFunction(GaussianShapeOverlapFunction &func)
Specifies the Gaussian-shape overlap function used during alignment.
void setColorMatchFunction(const ColorMatchFunction &func)
Specifies the function used to decide whether two color features match.
const ScoringFunction & getScoringFunction() const
Returns the currently configured scoring function.
ConstResultIterator getResultsEnd() const
Returns a constant iterator pointing one past the last alignment result.
std::function< double(const AlignmentResult &)> ScoringFunction
Type of the function used to score an alignment result.
Definition: GaussianShapeAlignment.hpp:101
void performAlignment(bool perf_align)
Specifies whether the actual alignment shall be performed (vs. only computing overlaps in the initial...
std::size_t getNumResults() const
Returns the number of stored alignment results.
void greedyOptimization(bool greedy)
Specifies whether the overlap optimization shall use a greedy strategy that stops at the first local ...
bool calcColorSelfOverlaps() const
Tells whether color self-overlaps are computed for the involved shapes.
GaussianShapeAlignment()
Constructs the GaussianShapeAlignment instance.
bool optimizeOverlap() const
Tells whether the overlap is optimized iteratively.
void setOptimizationStopGradient(double grad_norm)
Sets the gradient norm at which the overlap optimization is stopped.
void calcColorOverlaps(bool calc)
Specifies whether color overlaps shall be computed in addition to shape overlaps.
void addReferenceShapes(const GaussianShapeSet &shapes, bool new_set=true)
Adds the shapes in shapes to the reference shapes.
void calcSelfOverlaps(bool calc)
Specifies whether shape self-overlaps shall be computed for the involved shapes.
ResultList::iterator ResultIterator
A mutable iterator over the alignment results.
Definition: GaussianShapeAlignment.hpp:92
const AlignmentResult & getResult(std::size_t idx) const
Returns the alignment result at index idx.
void setMaxOrder(std::size_t max_order)
Sets the maximum order of the Gaussian-product expansion used by the overlap function.
void setMaxNumOptimizationIterations(std::size_t max_iter)
Sets the maximum number of overlap-optimization iterations.
void setResultCompareFunction(const ResultCompareFunction &func)
Specifies the function used to compare two alignment results for sorting and filtering.
void setColorFilterFunction(const ColorFilterFunction &func)
Specifies the function used to filter color features by type.
GaussianShapeAlignment(const GaussianShape &ref_shape)
Constructs the GaussianShapeAlignment instance with ref_shape as the single reference shape.
unsigned int getResultSelectionMode() const
Returns the currently configured alignment-result selection mode.
bool align(const GaussianShape &shape)
Aligns shape against all reference shapes.
Driver for the alignment of one Shape::GaussianShapeFunction (the aligned shape) against a fixed refe...
Definition: GaussianShapeFunctionAlignment.hpp:63
GaussianShapeOverlapFunction::ColorMatchFunction ColorMatchFunction
Type of the function used to decide whether two color features match.
Definition: GaussianShapeFunctionAlignment.hpp:88
GaussianShapeOverlapFunction::ColorFilterFunction ColorFilterFunction
Type of the function used to filter color (pharmacophore) features by type.
Definition: GaussianShapeFunctionAlignment.hpp:85
Function representation of a Gaussian shape, used to evaluate the shape's density,...
Definition: GaussianShapeFunction.hpp:61
Abstract base class for functions evaluating the overlap between two Gaussian shape functions.
Definition: GaussianShapeOverlapFunction.hpp:56
Data structure storing a set of (shared) Gaussian shapes.
Definition: GaussianShapeSet.hpp:52
Data type for the descripton of arbitrary shapes composed of spheres approximated by gaussian functio...
Definition: GaussianShape.hpp:51
std::shared_ptr< GaussianShape > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated GaussianShape instances.
Definition: GaussianShape.hpp:157
Generator that produces alignment starting transformations by aligning the principal axes of the alig...
Definition: PrincipalAxesAlignmentStartGenerator.hpp:58
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.