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 <limits>
19#include <utility>
20
21namespace RDKit {
22class ROMol;
23
24//! used to indicate errors from incorrect conformer access
25class 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);
65 Conformer(Conformer &&o) noexcept
66 : RDProps(std::move(o)),
67 df_is3D(std::move(o.df_is3D)),
68 d_id(std::move(o.d_id)),
69 dp_mol(std::move(o.dp_mol)),
70 d_positions(std::move(o.d_positions)){};
71 Conformer &operator=(Conformer &&o) noexcept {
72 if (this == &o) {
73 return *this;
74 }
75 RDProps::operator=(std::move(o));
76 df_is3D = std::move(o.df_is3D);
77 d_id = std::move(o.d_id);
78 dp_mol = std::move(o.dp_mol);
79 d_positions = std::move(o.d_positions);
80 return *this;
81 }
82 //! Destructor
83 ~Conformer() = default;
84
85 //! Resize the conformer so that more atoms location can be added.
86 //! Useful, for e.g., when adding hydrogens
87 void resize(unsigned int size) { d_positions.resize(size); }
88
89 //! Reserve more space for atom position
90 void reserve(unsigned int size) { d_positions.reserve(size); }
91
92 //! returns whether or not this instance belongs to a molecule
93 bool hasOwningMol() const { return dp_mol != nullptr; }
94
95 //! Get the molecule that owns this instance
97 PRECONDITION(dp_mol, "no owner");
98 return *dp_mol;
99 }
100
101 //! Get a const reference to the vector of atom positions
103
104 //! Get a reference to the atom positions
106
107 //! Get the position of the specified atom
108 const RDGeom::Point3D &getAtomPos(unsigned int atomId) const;
109 //! overload
110 template <class U>
111 const RDGeom::Point3D &getAtomPos(U atomId) const {
112 return getAtomPos(rdcast<unsigned int>(atomId));
113 }
114
115 //! Get the position of the specified atom
116 RDGeom::Point3D &getAtomPos(unsigned int atomId);
117 //! overload
118 template <class U>
120 return getAtomPos(rdcast<unsigned int>(atomId));
121 }
122
123 //! Set the position of the specified atom
124 inline void setAtomPos(unsigned int atomId, const RDGeom::Point3D &position) {
125 if (atomId == std::numeric_limits<unsigned int>::max()) {
126 throw ValueErrorException("atom index overflow");
127 }
128 if (atomId >= d_positions.size()) {
129 d_positions.resize(atomId + 1, RDGeom::Point3D(0.0, 0.0, 0.0));
130 }
131 d_positions[atomId] = position;
132 }
133 //! overload
134 template <class U>
135 void setAtomPos(U atomId, const RDGeom::Point3D &position) {
136 return setAtomPos(rdcast<unsigned int>(atomId), position);
137 }
138 //! get the ID of this conformer
139 inline unsigned int getId() const { return d_id; }
140
141 //! set the ID of this conformer
142 inline void setId(unsigned int id) { d_id = id; }
143
144 //! Get the number of atoms
145 inline unsigned int getNumAtoms() const {
146 return rdcast<unsigned int>(d_positions.size());
147 }
148 inline bool is3D() const { return df_is3D; }
149 inline void set3D(bool v) { df_is3D = v; }
150
151 protected:
152 //! Set owning molecule
153 void setOwningMol(ROMol *mol);
154
155 //! Set owning molecule
156 void setOwningMol(ROMol &mol);
157
158 private:
159 bool df_is3D{true}; // is this a 3D conformation?
160 unsigned int d_id{0}; // id is the conformation
161 ROMol *dp_mol{nullptr}; // owning molecule
162 RDGeom::POINT3D_VECT d_positions; // positions of the atoms
163 void initFromOther(const Conformer &conf);
164};
165
166typedef boost::shared_ptr<Conformer> CONFORMER_SPTR;
167
168//! Returns true if any of the z coords are non zero, false otherwise
169/*!
170 \param conf Conformer object to analyze
171*/
172inline bool hasNonZeroZCoords(const Conformer &conf) {
173 for (auto p : conf.getPositions()) {
174 if (p.z != 0.0) {
175 return true;
176 }
177 }
178 return false;
179}
180
181} // namespace RDKit
182
183#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
ROMol & getOwningMol() const
Get the molecule that owns this instance.
Definition Conformer.h:96
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:139
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:149
void setAtomPos(U atomId, const RDGeom::Point3D &position)
overload
Definition Conformer.h:135
void setOwningMol(ROMol *mol)
Set owning molecule.
void resize(unsigned int size)
Definition Conformer.h:87
RDGeom::Point3D & getAtomPos(U atomId)
overload
Definition Conformer.h:119
unsigned int getNumAtoms() const
Get the number of atoms.
Definition Conformer.h:145
bool is3D() const
Definition Conformer.h:148
Conformer(Conformer &&o) noexcept
Definition Conformer.h:65
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:142
void reserve(unsigned int size)
Reserve more space for atom position.
Definition Conformer.h:90
~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:93
void setAtomPos(unsigned int atomId, const RDGeom::Point3D &position)
Set the position of the specified atom.
Definition Conformer.h:124
Conformer & operator=(Conformer &&o) noexcept
Definition Conformer.h:71
Conformer()
Constructor.
Definition Conformer.h:50
RDGeom::POINT3D_VECT & getPositions()
Get a reference to the atom positions.
const RDGeom::Point3D & getAtomPos(U atomId) const
overload
Definition Conformer.h:111
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:225
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:172
boost::shared_ptr< Conformer > CONFORMER_SPTR
Definition Conformer.h:166