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
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() = 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
92 PRECONDITION(dp_mol, "no owner");
93 return *dp_mol;
94 }
95
96 //! Get a const reference to the vector of atom positions
98
99 //! Get a reference to the atom positions
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>
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
160typedef 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*/
166inline 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
#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
Conformer & operator=(const Conformer &other)=default
ROMol & getOwningMol() const
Get the molecule that owns this instance.
Definition Conformer.h:91
Conformer(unsigned int numAtoms)
Constructor with number of atoms specified ID specification.
Definition Conformer.h:54
void setOwningMol(ROMol &mol)
Set owning molecule.
unsigned int getId() const
get the ID of this conformer
Definition Conformer.h:134
RDGeom::Point3D & getAtomPos(unsigned int atomId)
Get the position of the specified atom.
void set3D(bool v)
Definition Conformer.h:144
void setAtomPos(U atomId, const RDGeom::Point3D &position)
overload
Definition Conformer.h:130
void setOwningMol(ROMol *mol)
Set owning molecule.
Conformer()=default
Constructor.
void resize(unsigned int size)
Definition Conformer.h:82
RDGeom::Point3D & getAtomPos(U atomId)
overload
Definition Conformer.h:114
unsigned int getNumAtoms() const
Get the number of atoms.
Definition Conformer.h:140
bool is3D() const
Definition Conformer.h:143
Conformer(Conformer &&o) noexcept
Definition Conformer.h:60
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:137
void reserve(unsigned int size)
Reserve more space for atom position.
Definition Conformer.h:85
~Conformer()=default
Destructor.
Conformer(const Conformer &other)=default
Copy Constructor: initialize from a second conformation.
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:88
void setAtomPos(unsigned int atomId, const RDGeom::Point3D &position)
Set the position of the specified atom.
Definition Conformer.h:119
Conformer & operator=(Conformer &&o) noexcept
Definition Conformer.h:66
RDGeom::POINT3D_VECT & getPositions()
Get a reference to the atom positions.
const RDGeom::Point3D & getAtomPos(U atomId) const
overload
Definition Conformer.h:106
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:166
boost::shared_ptr< Conformer > CONFORMER_SPTR
Definition Conformer.h:160