RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
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 <cmath>
19#include <limits>
20#include <utility>
21
22namespace RDKit {
23class ROMol;
24
25//! used to indicate errors from incorrect conformer access
26class RDKIT_GRAPHMOL_EXPORT ConformerException : public std::exception {
27 public:
28 //! construct with an error message
29 ConformerException(const char *msg) : _msg(msg) {}
30 //! construct with an error message
31 ConformerException(std::string msg) : _msg(std::move(msg)) {}
32 //! get the error message
33 const char *what() const noexcept override { return _msg.c_str(); }
34 ~ConformerException() noexcept override = default;
35
36 private:
37 std::string _msg;
38};
39
40//! The class for representing 2D or 3D conformation of a molecule
41/*!
42 This class contains
43 - a pointer to the owing molecule
44 - a vector of 3D points (positions of atoms)
45*/
47 public:
48 friend class ROMol;
49
50 //! Constructor
51 Conformer() { d_positions.clear(); }
52
53 //! Constructor with number of atoms specified ID specification
54 Conformer(unsigned int numAtoms) {
55 if (numAtoms) {
56 d_positions.resize(numAtoms, RDGeom::Point3D(0.0, 0.0, 0.0));
57 } else {
58 d_positions.resize(0);
59 d_positions.clear();
60 }
61 }
62
63 //! Copy Constructor: initialize from a second conformation.
64 Conformer(const Conformer &other);
66 Conformer(Conformer &&o) noexcept
67 : RDProps(std::move(o)),
68 df_is3D(std::move(o.df_is3D)),
69 d_id(std::move(o.d_id)),
70 dp_mol(std::move(o.dp_mol)),
71 d_positions(std::move(o.d_positions)){};
72 Conformer &operator=(Conformer &&o) noexcept {
73 if (this == &o) {
74 return *this;
75 }
76 RDProps::operator=(std::move(o));
77 df_is3D = std::move(o.df_is3D);
78 d_id = std::move(o.d_id);
79 dp_mol = std::move(o.dp_mol);
80 d_positions = std::move(o.d_positions);
81 return *this;
82 }
83 //! Destructor
84 ~Conformer() = default;
85
86 //! Resize the conformer so that more atoms location can be added.
87 //! Useful, for e.g., when adding hydrogens
88 void resize(unsigned int size) { d_positions.resize(size); }
89
90 //! Reserve more space for atom position
91 void reserve(unsigned int size) { d_positions.reserve(size); }
92
93 //! returns whether or not this instance belongs to a molecule
94 bool hasOwningMol() const { return dp_mol != nullptr; }
95
96 //! Get the molecule that owns this instance
98 PRECONDITION(dp_mol, "no owner");
99 return *dp_mol;
100 }
101
102 //! Get a const reference to the vector of atom positions
104
105 //! Get a reference to the atom positions
107
108 //! Get the position of the specified atom
109 const RDGeom::Point3D &getAtomPos(unsigned int atomId) const;
110 //! overload
111 template <class U>
112 const RDGeom::Point3D &getAtomPos(U atomId) const {
113 return getAtomPos(rdcast<unsigned int>(atomId));
114 }
115
116 //! Get the position of the specified atom
117 RDGeom::Point3D &getAtomPos(unsigned int atomId);
118 //! overload
119 template <class U>
121 return getAtomPos(rdcast<unsigned int>(atomId));
122 }
123
124 //! Set the position of the specified atom
125 inline void setAtomPos(unsigned int atomId, const RDGeom::Point3D &position) {
126 if (atomId == std::numeric_limits<unsigned int>::max()) {
127 throw ValueErrorException("atom index overflow");
128 }
129 if (atomId >= d_positions.size()) {
130 d_positions.resize(atomId + 1, RDGeom::Point3D(0.0, 0.0, 0.0));
131 }
132 d_positions[atomId] = position;
133 }
134 //! overload
135 template <class U>
136 void setAtomPos(U atomId, const RDGeom::Point3D &position) {
137 return setAtomPos(rdcast<unsigned int>(atomId), position);
138 }
139 //! get the ID of this conformer
140 inline unsigned int getId() const { return d_id; }
141
142 //! set the ID of this conformer
143 inline void setId(unsigned int id) { d_id = id; }
144
145 //! Get the number of atoms
146 inline unsigned int getNumAtoms() const {
147 return rdcast<unsigned int>(d_positions.size());
148 }
149 inline bool is3D() const { return df_is3D; }
150 inline void set3D(bool v) { df_is3D = v; }
151
152 protected:
153 //! Set owning molecule
154 void setOwningMol(ROMol *mol);
155
156 //! Set owning molecule
157 void setOwningMol(ROMol &mol);
158
159 private:
160 bool df_is3D{true}; // is this a 3D conformation?
161 unsigned int d_id{0}; // id is the conformation
162 ROMol *dp_mol{nullptr}; // owning molecule
163 RDGeom::POINT3D_VECT d_positions; // positions of the atoms
164 void initFromOther(const Conformer &conf);
165};
166
167typedef boost::shared_ptr<Conformer> CONFORMER_SPTR;
168
169//! Returns true if any of the z coords are non zero, false otherwise
170/*!
171 \param conf Conformer object to analyze
172*/
173inline bool hasNonZeroZCoords(const Conformer &conf) {
174 constexpr double zeroTol = 1e-3;
175 for (auto p : conf.getPositions()) {
176 if (std::abs(p.z) > zeroTol) {
177 return true;
178 }
179 }
180 return false;
181}
182
183} // namespace RDKit
184
185#endif
#define PRECONDITION(expr, mess)
Definition Invariant.h:109
used to indicate errors from incorrect conformer access
Definition Conformer.h:26
ConformerException(std::string msg)
construct with an error message
Definition Conformer.h:31
ConformerException(const char *msg)
construct with an error message
Definition Conformer.h:29
const char * what() const noexcept override
get the error message
Definition Conformer.h:33
~ConformerException() noexcept override=default
The class for representing 2D or 3D conformation of a molecule.
Definition Conformer.h:46
ROMol & getOwningMol() const
Get the molecule that owns this instance.
Definition Conformer.h:97
Conformer(unsigned int numAtoms)
Constructor with number of atoms specified ID specification.
Definition Conformer.h:54
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:140
Conformer & operator=(const Conformer &other)
RDGeom::Point3D & getAtomPos(unsigned int atomId)
Get the position of the specified atom.
void set3D(bool v)
Definition Conformer.h:150
void setAtomPos(U atomId, const RDGeom::Point3D &position)
overload
Definition Conformer.h:136
void setOwningMol(ROMol *mol)
Set owning molecule.
void resize(unsigned int size)
Definition Conformer.h:88
RDGeom::Point3D & getAtomPos(U atomId)
overload
Definition Conformer.h:120
unsigned int getNumAtoms() const
Get the number of atoms.
Definition Conformer.h:146
bool is3D() const
Definition Conformer.h:149
Conformer(Conformer &&o) noexcept
Definition Conformer.h:66
const RDGeom::POINT3D_VECT & getPositions() const
Get a const reference to the vector of atom positions.
void setId(unsigned int id)
set the ID of this conformer
Definition Conformer.h:143
void reserve(unsigned int size)
Reserve more space for atom position.
Definition Conformer.h:91
~Conformer()=default
Destructor.
const RDGeom::Point3D & getAtomPos(unsigned int atomId) const
Get the position of the specified atom.
bool hasOwningMol() const
returns whether or not this instance belongs to a molecule
Definition Conformer.h:94
void setAtomPos(unsigned int atomId, const RDGeom::Point3D &position)
Set the position of the specified atom.
Definition Conformer.h:125
Conformer & operator=(Conformer &&o) noexcept
Definition Conformer.h:72
Conformer()
Constructor.
Definition Conformer.h:51
RDGeom::POINT3D_VECT & getPositions()
Get a reference to the atom positions.
const RDGeom::Point3D & getAtomPos(U atomId) const
overload
Definition Conformer.h:112
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:233
std::vector< Point3D > POINT3D_VECT
Definition point.h:546
Std stuff.
bool hasNonZeroZCoords(const Conformer &conf)
Returns true if any of the z coords are non zero, false otherwise.
Definition Conformer.h:173
boost::shared_ptr< Conformer > CONFORMER_SPTR
Definition Conformer.h:167