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