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