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:
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 std::size_t DEF_MAX_PRODUCT_ORDER = 1;
89 
93  static constexpr unsigned int DEF_RESULT_SELECTION_MODE = AlignmentResultSelectionMode::BEST_PER_REFERENCE_SET;
94 
98  static constexpr double DEF_DISTANCE_CUTOFF = 0.0;
99 
103  typedef std::shared_ptr<GaussianShapeAlignment> SharedPointer;
104 
108  typedef ResultList::const_iterator ConstResultIterator;
109 
113  typedef ResultList::iterator ResultIterator;
114 
118  typedef boost::transform_iterator<GetShapeFunction, ShapeFunctionList::const_iterator> ConstShapeIterator;
119 
124 
129 
133  typedef std::function<double(const AlignmentResult&)> ScoringFunction;
134 
138  typedef std::function<bool(const AlignmentResult&, const AlignmentResult&)> ResultCompareFunction;
139 
144 
150 
156 
157  GaussianShapeAlignment(const GaussianShapeAlignment& alignment) = delete;
158 
163 
165 
171 
177 
183 
189 
195 
201 
207 
213 
219 
225 
231 
237 
243 
249 
255 
261 
266  void setResultSelectionMode(unsigned int mode);
267 
272  unsigned int getResultSelectionMode() const;
273 
278  void calcSelfOverlaps(bool calc);
279 
284  bool calcSelfOverlaps() const;
285 
290  void calcColorSelfOverlaps(bool calc);
291 
296  bool calcColorSelfOverlaps() const;
297 
302  void calcColorOverlaps(bool calc);
303 
308  bool calcColorOverlaps() const;
309 
314  void performAlignment(bool perf_align);
315 
320  bool performAlignment() const;
321 
326  void optimizeOverlap(bool optimize);
327 
332  bool optimizeOverlap() const;
333 
338  void greedyOptimization(bool greedy);
339 
344  bool greedyOptimization() const;
345 
350  void setMaxNumOptimizationIterations(std::size_t max_iter);
351 
356  std::size_t getMaxNumOptimizationIterations() const;
357 
362  void setOptimizationStopGradient(double grad_norm);
363 
369 
374  void setMaxOrder(std::size_t max_order);
375 
380  std::size_t getMaxOrder() const;
381 
386  void setDistanceCutoff(double cutoff);
387 
392  double getDistanceCutoff() const;
393 
398 
404  void addReferenceShape(const GaussianShape& shape, bool new_set = true);
405 
411  void addReferenceShapes(const GaussianShapeSet& shapes, bool new_set = true);
412 
417  std::size_t getNumReferenceShapes() const;
418 
425  const GaussianShape& getReferenceShape(std::size_t idx) const;
426 
432 
438 
444  bool align(const GaussianShape& shape);
445 
451  bool align(const GaussianShapeSet& shapes);
452 
457  std::size_t getNumResults() const;
458 
465  const AlignmentResult& getResult(std::size_t idx) const;
466 
473  AlignmentResult& getResult(std::size_t idx);
474 
480 
486 
492 
498 
499  private:
500  typedef GaussianShape::SharedPointer GaussianShapePtr;
501 
502  struct ShapeMetaData
503  {
504 
505  std::size_t setIndex;
506  std::size_t index;
507  unsigned int symClass;
509  double selfOverlap;
510  double colSelfOverlap;
511  GaussianShapePtr shape;
512  };
513 
514  typedef std::pair<std::size_t, std::size_t> ResultID;
515 
516  void prepareForAlignment(GaussianShapeFunction& func, ShapeMetaData& data, bool ref);
517 
518  void processResults(std::size_t ref_idx, std::size_t al_idx);
519 
520  bool getResultIndex(const ResultID& res_id, std::size_t& res_idx);
521 
522  GaussianShapeFunction* allocShapeFunction(const GaussianShape& shape);
523 
524  typedef Util::ObjectStack<GaussianShapeFunction> ShapeFunctionCache;
525  typedef std::vector<ShapeMetaData> ShapeMetaDataArray;
526  typedef std::unordered_map<ResultID, std::size_t, boost::hash<ResultID> > ResultIndexMap;
527 
528  ShapeFunctionCache shapeFuncCache;
529  bool calcSlfOverlaps;
530  bool calcColSlfOverlaps;
531  unsigned int resultSelMode;
532  ResultCompareFunction resultCmpFunc;
533  ScoringFunction scoringFunc;
534  GaussianShapeFunctionAlignment shapeFuncAlmnt;
535  ShapeFunctionList refShapeFuncs;
536  ShapeMetaDataArray refShapeMetaData;
537  GaussianShapeFunction algdShapeFunc;
538  ShapeMetaData algdShapeMetaData;
539  ResultIndexMap resIndexMap;
540  ResultList results;
541  std::size_t currSetIndex;
542  std::size_t currShapeIndex;
543  };
544  } // namespace Shape
545 } // namespace CDPL
546 
547 #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:108
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:123
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:128
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:103
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:118
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:138
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.
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:133
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:113
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:100
GaussianShapeOverlapFunction::ColorFilterFunction ColorFilterFunction
Type of the function used to filter color (pharmacophore) features by type.
Definition: GaussianShapeFunctionAlignment.hpp:95
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.