RDKit
Open-source cheminformatics and machine learning.
SubstanceGroup.h
Go to the documentation of this file.
1 //
2 //
3 // Copyright (C) 2002-2018 Greg Landrum and T5 Informatics GmbH
4 //
5 // @@ All Rights Reserved @@
6 // This file is part of the RDKit.
7 // The contents are covered by the terms of the BSD license
8 // which is included in the file license.txt, found at the root
9 // of the RDKit source tree.
10 //
11 /*! \file SubstanceGroup.h
12 
13  \brief Defines the SubstanceGroup class
14 
15 */
16 #include <RDGeneral/export.h>
17 #ifndef _RD_SGROUP_H
18 #define _RD_SGROUP_H
19 
20 #include <unordered_map>
21 
22 #include <Geometry/point.h>
23 #include <RDGeneral/types.h>
24 #include <RDGeneral/RDProps.h>
25 #include <boost/smart_ptr.hpp>
26 
27 namespace RDKit {
28 class ROMol;
29 class Bond;
30 class Atom;
31 
32 //! used to indicate errors from incorrect sgroup access
34  : public std::runtime_error {
35  public:
36  //! construct with an error message
37  SubstanceGroupException(const char *msg) : std::runtime_error(msg){};
38  //! construct with an error message
39  SubstanceGroupException(const std::string &msg) : std::runtime_error(msg){};
40 };
41 
42 //! The class for representing SubstanceGroups
43 /*!
44  <b>Notes:</b>
45  - These are inspired by the SGroups in the MDL formats
46  - Implementation is based on 2010 MDL SD specification:
47  http://infochim.u-strasbg.fr/recherche/Download/Fragmentor/MDL_SDF.pdf
48  - See SGroups.md for further, more comprehensive notes.
49 
50 */
51 
53  public:
54  //! Bond type (see V3000 spec)
55  enum class BondType {
56  XBOND, // External/Crossing bond
57  CBOND, // Internal/Contained bond
58  };
59 
60  typedef std::array<RDGeom::Point3D, 3> Bracket;
61 
62  //! Data structure for SAP lines (see V3000 spec)
63  //! lvIdx may not be set; this signaled with value -1
64  struct AttachPoint {
65  unsigned int aIdx;
66  int lvIdx;
67  std::string id;
68  bool operator==(const AttachPoint &other) const {
69  return aIdx == other.aIdx && lvIdx == other.lvIdx && id == other.id;
70  }
71  };
72 
73  //! See specification for V3000 CSTATE
74  //! vector may or not be considered, depending on TYPE
75  struct CState {
76  unsigned int bondIdx;
78  bool operator==(const CState &other) const {
79  // note that we ignore coordinates for this
80  return bondIdx == other.bondIdx;
81  }
82  };
83 
84  //! No default constructor
85  SubstanceGroup() = delete;
86 
87  //! Main Constructor. Ownership is only set on this side of the relationship:
88  //! mol->addSubstanceGroup(sgroup) still needs to be called to get ownership
89  //! on the other side.
90  SubstanceGroup(ROMol *owning_mol, const std::string &type);
91 
92  SubstanceGroup(const SubstanceGroup &other) = default;
93  SubstanceGroup(SubstanceGroup &&other) = default;
94 
95  SubstanceGroup &operator=(const SubstanceGroup &other) = default;
96  SubstanceGroup &operator=(SubstanceGroup &&other) = default;
97 
98  //! Destructor
100 
101  //! returns whether or not this belongs to a molecule
102  bool hasOwningMol() const { return dp_mol != nullptr; };
103 
104  //! Get the molecule that owns this instance
105  ROMol &getOwningMol() const {
106  PRECONDITION(dp_mol, "no owner");
107  return *dp_mol;
108  }
109 
110  //! get the index of this sgroup in dp_mol's sgroups vector
111  //! (do not mistake this by the ID!)
112  unsigned int getIndexInMol() const;
113 
114  /* Atom and Bond methods */
115  void addAtomWithIdx(unsigned int idx);
116  void addParentAtomWithIdx(unsigned int idx);
117  void addBondWithIdx(unsigned int idx);
118  void addAtomWithBookmark(int mark);
119  void addParentAtomWithBookmark(int mark);
120  void addBondWithBookmark(int mark);
121 
122  void addBracket(const Bracket &bracket);
123  void addCState(unsigned int bondIdx, const RDGeom::Point3D &vector);
124  void addAttachPoint(unsigned int aIdx, int lvIdx, const std::string &idStr);
125 
126  BondType getBondType(unsigned int bondIdx) const;
127 
128  const std::vector<unsigned int> &getAtoms() const { return d_atoms; }
129  const std::vector<unsigned int> &getParentAtoms() const { return d_patoms; }
130  const std::vector<unsigned int> &getBonds() const { return d_bonds; }
131 
132  const std::vector<Bracket> &getBrackets() const { return d_brackets; }
133  const std::vector<CState> &getCStates() const { return d_cstates; }
134  const std::vector<AttachPoint> &getAttachPoints() const { return d_saps; }
135 
136  //! Set owning molecule
137  //! This only updates atoms and bonds; parent sgroup has to be updated
138  //! independently, since parent might not exist at the time this is called.
139  void setOwningMol(ROMol *mol);
140 
141  bool operator==(const SubstanceGroup &other) const {
142  // we ignore brackets and cstates, which involve coordinates
143  return dp_mol == other.dp_mol && d_atoms == other.d_atoms &&
144  d_patoms == other.d_patoms && d_bonds == other.d_bonds &&
145  d_saps == other.d_saps;
146  }
147 
148  private:
149  ROMol *dp_mol = nullptr; // owning molecule
150 
151  std::vector<unsigned int> d_atoms;
152  std::vector<unsigned int> d_patoms;
153  std::vector<unsigned int> d_bonds;
154 
155  std::vector<Bracket> d_brackets;
156  std::vector<CState> d_cstates;
157  std::vector<AttachPoint> d_saps;
158 };
159 
160 namespace SubstanceGroupChecks {
161 
162 const std::vector<std::string> sGroupTypes = {
163  // polymer sgroups:
164  "SRU", "MON", "COP", "CRO", "GRA", "MOD", "MER", "ANY",
165  // formulations/mixtures:
166  "COM", "MIX", "FOR",
167  // other
168  "SUP", "MUL", "DAT", "GEN"};
169 
170 const std::vector<std::string> sGroupSubtypes = {"ALT", "RAN", "BLO"};
171 const std::vector<std::string> sGroupConnectTypes = {"HH", "HT", "EU"};
172 
173 RDKIT_GRAPHMOL_EXPORT bool isValidType(const std::string &type);
174 
175 RDKIT_GRAPHMOL_EXPORT bool isValidSubType(const std::string &type);
176 
177 RDKIT_GRAPHMOL_EXPORT bool isValidConnectType(const std::string &type);
178 
180  unsigned int id);
181 
182 } // namespace SubstanceGroupChecks
183 
184 //! \name SubstanceGroups and molecules
185 //@{
186 
187 RDKIT_GRAPHMOL_EXPORT std::vector<SubstanceGroup> &getSubstanceGroups(
188  ROMol &mol);
189 RDKIT_GRAPHMOL_EXPORT const std::vector<SubstanceGroup> &getSubstanceGroups(
190  const ROMol &mol);
191 
192 //! Add a new SubstanceGroup. A copy is added, so we can be sure that no other
193 //! references to the SubstanceGroup exist.
194 /*!
195  \param sgroup - SubstanceGroup to be added to the molecule.
196 */
197 RDKIT_GRAPHMOL_EXPORT unsigned int addSubstanceGroup(ROMol &mol,
198  SubstanceGroup sgroup);
199 //@}
200 
201 } // namespace RDKit
202 
203 //! allows SubstanceGroup objects to be dumped to streams
204 RDKIT_GRAPHMOL_EXPORT std::ostream &operator<<(std::ostream &target,
205  const RDKit::SubstanceGroup &sg);
206 #endif
RDKit::SubstanceGroupException::SubstanceGroupException
SubstanceGroupException(const char *msg)
construct with an error message
Definition: SubstanceGroup.h:37
RDKit::SubstanceGroupException
used to indicate errors from incorrect sgroup access
Definition: SubstanceGroup.h:33
RDKit::SubstanceGroup::CState
Definition: SubstanceGroup.h:75
point.h
RDKit::SubstanceGroup::getBonds
const std::vector< unsigned int > & getBonds() const
Definition: SubstanceGroup.h:130
types.h
RDKit::SubstanceGroupChecks::isValidSubType
RDKIT_GRAPHMOL_EXPORT bool isValidSubType(const std::string &type)
RDKit::SubstanceGroup::getParentAtoms
const std::vector< unsigned int > & getParentAtoms() const
Definition: SubstanceGroup.h:129
RDKit::addSubstanceGroup
RDKIT_GRAPHMOL_EXPORT unsigned int addSubstanceGroup(ROMol &mol, SubstanceGroup sgroup)
RDKit::SubstanceGroup::hasOwningMol
bool hasOwningMol() const
returns whether or not this belongs to a molecule
Definition: SubstanceGroup.h:102
RDKit::SubstanceGroup
The class for representing SubstanceGroups.
Definition: SubstanceGroup.h:52
RDGeom::Point3D
Definition: point.h:46
RDKit::SubstanceGroup::AttachPoint::id
std::string id
Definition: SubstanceGroup.h:67
RDKit::SubstanceGroup::getCStates
const std::vector< CState > & getCStates() const
Definition: SubstanceGroup.h:133
RDKit::SubstanceGroupChecks::sGroupSubtypes
const std::vector< std::string > sGroupSubtypes
Definition: SubstanceGroup.h:170
RDKit::SubstanceGroup::AttachPoint::operator==
bool operator==(const AttachPoint &other) const
Definition: SubstanceGroup.h:68
RDKit::SubstanceGroupChecks::isValidConnectType
RDKIT_GRAPHMOL_EXPORT bool isValidConnectType(const std::string &type)
RDKit::SubstanceGroup::BondType
BondType
Bond type (see V3000 spec)
Definition: SubstanceGroup.h:55
operator<<
RDKIT_GRAPHMOL_EXPORT std::ostream & operator<<(std::ostream &target, const RDKit::SubstanceGroup &sg)
allows SubstanceGroup objects to be dumped to streams
RDKit::SubstanceGroupChecks::isValidType
RDKIT_GRAPHMOL_EXPORT bool isValidType(const std::string &type)
RDKit::SubstanceGroup::getAtoms
const std::vector< unsigned int > & getAtoms() const
Definition: SubstanceGroup.h:128
RDKit::SubstanceGroup::getBrackets
const std::vector< Bracket > & getBrackets() const
Definition: SubstanceGroup.h:132
RDKit::SubstanceGroup::AttachPoint::aIdx
unsigned int aIdx
Definition: SubstanceGroup.h:65
RDKit::SubstanceGroup::operator==
bool operator==(const SubstanceGroup &other) const
Definition: SubstanceGroup.h:141
RDKit::ROMol
Definition: ROMol.h:171
RDKIT_GRAPHMOL_EXPORT
#define RDKIT_GRAPHMOL_EXPORT
Definition: export.h:307
RDProps.h
RDKit::SubstanceGroup::CState::bondIdx
unsigned int bondIdx
Definition: SubstanceGroup.h:76
RDKit::SubstanceGroup::AttachPoint::lvIdx
int lvIdx
Definition: SubstanceGroup.h:66
RDKit::RDProps
Definition: RDProps.h:10
RDKit::SubstanceGroup::~SubstanceGroup
~SubstanceGroup()
Destructor.
Definition: SubstanceGroup.h:99
RDKit::SubstanceGroup::CState::operator==
bool operator==(const CState &other) const
Definition: SubstanceGroup.h:78
RDKit::SubstanceGroupChecks::sGroupTypes
const std::vector< std::string > sGroupTypes
Definition: SubstanceGroup.h:162
RDKit::SubstanceGroupChecks::sGroupConnectTypes
const std::vector< std::string > sGroupConnectTypes
Definition: SubstanceGroup.h:171
RDKit
Std stuff.
Definition: Atom.h:30
RDKit::SubstanceGroupChecks::isSubstanceGroupIdFree
RDKIT_GRAPHMOL_EXPORT bool isSubstanceGroupIdFree(const ROMol &mol, unsigned int id)
RDKit::SubstanceGroup::CState::vector
RDGeom::Point3D vector
Definition: SubstanceGroup.h:77
RDKit::SubstanceGroup::Bracket
std::array< RDGeom::Point3D, 3 > Bracket
Definition: SubstanceGroup.h:60
RDKit::SubstanceGroup::getOwningMol
ROMol & getOwningMol() const
Get the molecule that owns this instance.
Definition: SubstanceGroup.h:105
PRECONDITION
#define PRECONDITION(expr, mess)
Definition: Invariant.h:109
RDKit::SubstanceGroupException::SubstanceGroupException
SubstanceGroupException(const std::string &msg)
construct with an error message
Definition: SubstanceGroup.h:39
RDKit::SubstanceGroup::AttachPoint
Definition: SubstanceGroup.h:64
RDKit::getSubstanceGroups
RDKIT_GRAPHMOL_EXPORT std::vector< SubstanceGroup > & getSubstanceGroups(ROMol &mol)
RDKit::SubstanceGroup::getAttachPoints
const std::vector< AttachPoint > & getAttachPoints() const
Definition: SubstanceGroup.h:134
export.h