RDKit: Conformer.h Source File

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