RDKit
Open-source cheminformatics and machine learning.
Conformer.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2021 Greg Landrum and other RDKit contributors
3 //
4 // @@ All Rights Reserved @@
5 // This file is part of the RDKit.
6 // The contents are covered by the terms of the BSD license
7 // which is included in the file license.txt, found at the root
8 // of the RDKit source tree.
9 //
10 #include <RDGeneral/export.h>
11 #ifndef _RD_CONFORMER_H
12 #define _RD_CONFORMER_H
13 
14 #include <Geometry/point.h>
15 #include <RDGeneral/types.h>
16 #include <boost/smart_ptr.hpp>
17 #include <RDGeneral/RDProps.h>
18 #include <limits>
19 #include <utility>
20 
21 namespace RDKit {
22 class ROMol;
23 
24 //! used to indicate errors from incorrect conformer access
25 class RDKIT_GRAPHMOL_EXPORT ConformerException : public std::exception {
26  public:
27  //! construct with an error message
28  ConformerException(const char *msg) : _msg(msg) {}
29  //! construct with an error message
30  ConformerException(std::string msg) : _msg(std::move(msg)) {}
31  //! get the error message
32  const char *what() const noexcept override { return _msg.c_str(); }
33  ~ConformerException() noexcept override = default;
34 
35  private:
36  std::string _msg;
37 };
38 
39 //! The class for representing 2D or 3D conformation of a molecule
40 /*!
41  This class contains
42  - a pointer to the owing molecule
43  - a vector of 3D points (positions of atoms)
44 */
46  public:
47  friend class ROMol;
48 
49  //! Constructor
50  Conformer() { d_positions.clear(); }
51 
52  //! Constructor with number of atoms specified ID specification
53  Conformer(unsigned int numAtoms) {
54  if (numAtoms) {
55  d_positions.resize(numAtoms, RDGeom::Point3D(0.0, 0.0, 0.0));
56  } else {
57  d_positions.resize(0);
58  d_positions.clear();
59  }
60  }
61 
62  //! Copy Constructor: initialize from a second conformation.
63  Conformer(const Conformer &other);
64  Conformer &operator=(const Conformer &other);
65 
66  //! Destructor
67  ~Conformer() = default;
68 
69  //! Resize the conformer so that more atoms location can be added.
70  //! Useful, for e.g., when adding hydrogens
71  void resize(unsigned int size) { d_positions.resize(size); }
72 
73  //! Reserve more space for atom position
74  void reserve(unsigned int size) { d_positions.reserve(size); }
75 
76  //! returns whether or not this instance belongs to a molecule
77  bool hasOwningMol() const { return dp_mol != nullptr; }
78 
79  //! Get the molecule that owns this instance
80  ROMol &getOwningMol() const {
81  PRECONDITION(dp_mol, "no owner");
82  return *dp_mol;
83  }
84 
85  //! Get a const reference to the vector of atom positions
87 
88  //! Get a reference to the atom positions
90 
91  //! Get the position of the specified atom
92  const RDGeom::Point3D &getAtomPos(unsigned int atomId) const;
93  //! overload
94  template <class U>
95  const RDGeom::Point3D &getAtomPos(U atomId) const {
96  return getAtomPos(rdcast<unsigned int>(atomId));
97  }
98 
99  //! Get the position of the specified atom
100  RDGeom::Point3D &getAtomPos(unsigned int atomId);
101  //! overload
102  template <class U>
104  return getAtomPos(rdcast<unsigned int>(atomId));
105  }
106 
107  //! Set the position of the specified atom
108  inline void setAtomPos(unsigned int atomId, const RDGeom::Point3D &position) {
109  if (atomId == std::numeric_limits<unsigned int>::max()) {
110  throw ValueErrorException("atom index overflow");
111  }
112  if (atomId >= d_positions.size()) {
113  d_positions.resize(atomId + 1, RDGeom::Point3D(0.0, 0.0, 0.0));
114  }
115  d_positions[atomId] = position;
116  }
117  //! overload
118  template <class U>
119  void setAtomPos(U atomId, const RDGeom::Point3D &position) {
120  return setAtomPos(rdcast<unsigned int>(atomId), position);
121  }
122  //! get the ID of this conformer
123  inline unsigned int getId() const { return d_id; }
124 
125  //! set the ID of this conformer
126  inline void setId(unsigned int id) { d_id = id; }
127 
128  //! Get the number of atoms
129  inline unsigned int getNumAtoms() const {
130  return rdcast<unsigned int>(d_positions.size());
131  }
132  inline bool is3D() const { return df_is3D; }
133  inline void set3D(bool v) { df_is3D = v; }
134 
135  protected:
136  //! Set owning molecule
137  void setOwningMol(ROMol *mol);
138 
139  //! Set owning molecule
140  void setOwningMol(ROMol &mol);
141 
142  private:
143  bool df_is3D{true}; // is this a 3D conformation?
144  unsigned int d_id{0}; // id is the conformation
145  ROMol *dp_mol{nullptr}; // owning molecule
146  RDGeom::POINT3D_VECT d_positions; // positions of the atoms
147  void initFromOther(const Conformer &conf);
148 };
149 
150 typedef boost::shared_ptr<Conformer> CONFORMER_SPTR;
151 
152 //! Returns true if any of the z coords are non zero, false otherwise
153 /*!
154  \param conf Conformer object to analyze
155 */
156 inline bool hasNonZeroZCoords(const Conformer &conf) {
157  for (auto p : conf.getPositions()) {
158  if (p.z != 0.0) return true;
159  }
160  return false;
161 }
162 
163 } // namespace RDKit
164 
165 #endif
#define PRECONDITION(expr, mess)
Definition: Invariant.h:109
used to indicate errors from incorrect conformer access
Definition: Conformer.h:25
ConformerException(std::string msg)
construct with an error message
Definition: Conformer.h:30
ConformerException(const char *msg)
construct with an error message
Definition: Conformer.h:28
const char * what() const noexcept override
get the error message
Definition: Conformer.h:32
~ConformerException() noexcept override=default
The class for representing 2D or 3D conformation of a molecule.
Definition: Conformer.h:45
const RDGeom::Point3D & getAtomPos(U atomId) const
overload
Definition: Conformer.h:95
Conformer(unsigned int numAtoms)
Constructor with number of atoms specified ID specification.
Definition: Conformer.h:53
Conformer(const Conformer &other)
Copy Constructor: initialize from a second conformation.
void setOwningMol(ROMol &mol)
Set owning molecule.
unsigned int getId() const
get the ID of this conformer
Definition: Conformer.h:123
RDGeom::Point3D & getAtomPos(U atomId)
overload
Definition: Conformer.h:103
ROMol & getOwningMol() const
Get the molecule that owns this instance.
Definition: Conformer.h:80
const RDGeom::Point3D & getAtomPos(unsigned int atomId) const
Get the position of the specified atom.
void set3D(bool v)
Definition: Conformer.h:133
void setAtomPos(U atomId, const RDGeom::Point3D &position)
overload
Definition: Conformer.h:119
Conformer & operator=(const Conformer &other)
void setOwningMol(ROMol *mol)
Set owning molecule.
RDGeom::Point3D & getAtomPos(unsigned int atomId)
Get the position of the specified atom.
void resize(unsigned int size)
Definition: Conformer.h:71
unsigned int getNumAtoms() const
Get the number of atoms.
Definition: Conformer.h:129
bool is3D() const
Definition: Conformer.h:132
const RDGeom::POINT3D_VECT & getPositions() const
Get a const reference to the vector of atom positions.
RDGeom::POINT3D_VECT & getPositions()
Get a reference to the atom positions.
void setId(unsigned int id)
set the ID of this conformer
Definition: Conformer.h:126
void reserve(unsigned int size)
Reserve more space for atom position.
Definition: Conformer.h:74
~Conformer()=default
Destructor.
bool hasOwningMol() const
returns whether or not this instance belongs to a molecule
Definition: Conformer.h:77
void setAtomPos(unsigned int atomId, const RDGeom::Point3D &position)
Set the position of the specified atom.
Definition: Conformer.h:108
Conformer()
Constructor.
Definition: Conformer.h:50
Class to allow us to throw a ValueError from C++ and have it make it back to Python.
Definition: Exceptions.h:40
#define RDKIT_GRAPHMOL_EXPORT
Definition: export.h:209
std::vector< Point3D > POINT3D_VECT
Definition: point.h:538
Std stuff.
Definition: Abbreviations.h:18
bool hasNonZeroZCoords(const Conformer &conf)
Returns true if any of the z coords are non zero, false otherwise.
Definition: Conformer.h:156
boost::shared_ptr< Conformer > CONFORMER_SPTR
Definition: Conformer.h:150