RDKit
Open-source cheminformatics and machine learning.
SubstanceGroup.h
Go to the documentation of this file.
1 //
2 //
3 // Copyright (C) 2018-2020 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 RWMol;
30 class Bond;
31 class Atom;
32 
33 //! used to indicate errors from incorrect sgroup access
35  : public std::runtime_error {
36  public:
37  //! construct with an error message
38  SubstanceGroupException(const char *msg) : std::runtime_error(msg){};
39  //! construct with an error message
40  SubstanceGroupException(const std::string &msg) : std::runtime_error(msg){};
41 };
42 
43 //! The class for representing SubstanceGroups
44 /*!
45  <b>Notes:</b>
46  - These are inspired by the SGroups in the MDL formats
47  - Implementation is based on 2010 MDL SD specification:
48  http://infochim.u-strasbg.fr/recherche/Download/Fragmentor/MDL_SDF.pdf
49  - See SGroups.md for further, more comprehensive notes.
50 
51 */
52 
54  public:
55  //! Bond type (see V3000 spec)
56  enum class BondType {
57  XBOND, // External/Crossing bond
58  CBOND, // Internal/Contained bond
59  };
60 
61  typedef std::array<RDGeom::Point3D, 3> Bracket;
62 
63  //! Data structure for SAP lines (see V3000 spec)
64  //! lvIdx may not be set; this signaled with value -1
65  struct AttachPoint {
66  unsigned int aIdx;
67  int lvIdx;
68  std::string id;
69  bool operator==(const AttachPoint &other) const {
70  return aIdx == other.aIdx && lvIdx == other.lvIdx && id == other.id;
71  }
72  };
73 
74  //! See specification for V3000 CSTATE
75  //! vector may or not be considered, depending on TYPE
76  struct CState {
77  unsigned int bondIdx;
79  bool operator==(const CState &other) const {
80  // note that we ignore coordinates for this
81  return bondIdx == other.bondIdx;
82  }
83  };
84 
85  //! No default constructor
86  SubstanceGroup() = delete;
87 
88  //! Main Constructor. Ownership is only set on this side of the relationship:
89  //! mol->addSubstanceGroup(sgroup) still needs to be called to get ownership
90  //! on the other side.
91  SubstanceGroup(ROMol *owning_mol, const std::string &type);
92 
93  SubstanceGroup(const SubstanceGroup &other) = default;
94  SubstanceGroup(SubstanceGroup &&other) = default;
95 
96  SubstanceGroup &operator=(const SubstanceGroup &other) = default;
98 
99  //! Destructor
101 
102  //! returns whether or not this belongs to a molecule
103  bool hasOwningMol() const { return dp_mol != nullptr; };
104 
105  //! Get the molecule that owns this instance
106  ROMol &getOwningMol() const {
107  PRECONDITION(dp_mol, "no owner");
108  return *dp_mol;
109  }
110 
111  //! get the index of this sgroup in dp_mol's sgroups vector
112  //! (do not mistake this by the ID!)00
113  unsigned int getIndexInMol() const;
114 
115  /* Atom and Bond methods */
116  void addAtomWithIdx(unsigned int idx);
117  void addParentAtomWithIdx(unsigned int idx);
118  void addBondWithIdx(unsigned int idx);
119  void addAtomWithBookmark(int mark);
121  void addBondWithBookmark(int mark);
122 
123  void addBracket(const Bracket &bracket);
124  void addCState(unsigned int bondIdx, const RDGeom::Point3D &vector);
125  void addAttachPoint(unsigned int aIdx, int lvIdx, const std::string &idStr);
126 
127  BondType getBondType(unsigned int bondIdx) const;
128 
129  const std::vector<unsigned int> &getAtoms() const { return d_atoms; }
130  const std::vector<unsigned int> &getParentAtoms() const { return d_patoms; }
131  const std::vector<unsigned int> &getBonds() const { return d_bonds; }
132 
133  const std::vector<Bracket> &getBrackets() const { return d_brackets; }
134  const std::vector<CState> &getCStates() const { return d_cstates; }
135  const std::vector<AttachPoint> &getAttachPoints() const { return d_saps; }
136 
137  //! adjusts our atom IDs to reflect that an atom has been removed from the
138  //! parent molecule
139  //! decrements all atom IDs that are higher than \c atomIdx
140  //! raises a \c SubstanceGroupException if \c atomIdx is actually part of
141  //! this substance group
142  //! \returns whether or not anything was changed
143  bool adjustToRemovedAtom(unsigned int atomIdx);
144 
145  //! \returns whether or not anything the specified atom is part of the
146  //! definition of this substance group
147  bool includesAtom(unsigned int atomIdx) const;
148 
149  //! adjusts our bond IDs to reflect that a bond has been removed from the
150  //! parent molecule
151  //! decrements all bond IDs that are higher than \c bondIdx
152  //! raises a \c SubstanceGroupException if \c bondIdx is actually part of
153  //! this substance group
154  //! \returns whether or not anything was changed
155  bool adjustToRemovedBond(unsigned int bondIdx);
156 
157  //! \returns whether or not anything the specified bond is part of the
158  //! definition of this substance group
159  bool includesBond(unsigned int bondIdx) const;
160 
161  //! Set owning molecule
162  //! This only updates atoms and bonds; parent sgroup has to be updated
163  //! independently, since parent might not exist at the time this is
164  //! called.
165  void setOwningMol(ROMol *mol);
166 
167  bool operator==(const SubstanceGroup &other) const {
168  // we ignore brackets and cstates, which involve coordinates
169  return dp_mol == other.dp_mol && d_atoms == other.d_atoms &&
170  d_patoms == other.d_patoms && d_bonds == other.d_bonds &&
171  d_saps == other.d_saps;
172  }
173 
174  private:
175  ROMol *dp_mol = nullptr; // owning molecule
176 
177  std::vector<unsigned int> d_atoms;
178  std::vector<unsigned int> d_patoms;
179  std::vector<unsigned int> d_bonds;
180 
181  std::vector<Bracket> d_brackets;
182  std::vector<CState> d_cstates;
183  std::vector<AttachPoint> d_saps;
184 };
185 
186 namespace SubstanceGroupChecks {
187 
188 const std::vector<std::string> sGroupTypes = {
189  // polymer sgroups:
190  "SRU", "MON", "COP", "CRO", "GRA", "MOD", "MER", "ANY",
191  // formulations/mixtures:
192  "COM", "MIX", "FOR",
193  // other
194  "SUP", "MUL", "DAT", "GEN"};
195 
196 const std::vector<std::string> sGroupSubtypes = {"ALT", "RAN", "BLO"};
197 const std::vector<std::string> sGroupConnectTypes = {"HH", "HT", "EU"};
198 
199 RDKIT_GRAPHMOL_EXPORT bool isValidType(const std::string &type);
200 
201 RDKIT_GRAPHMOL_EXPORT bool isValidSubType(const std::string &type);
202 
203 RDKIT_GRAPHMOL_EXPORT bool isValidConnectType(const std::string &type);
204 
206  unsigned int id);
207 
208 } // namespace SubstanceGroupChecks
209 
210 //! \name SubstanceGroups and molecules
211 //@{
212 
213 RDKIT_GRAPHMOL_EXPORT std::vector<SubstanceGroup> &getSubstanceGroups(
214  ROMol &mol);
215 RDKIT_GRAPHMOL_EXPORT const std::vector<SubstanceGroup> &getSubstanceGroups(
216  const ROMol &mol);
217 
218 //! Add a new SubstanceGroup. A copy is added, so we can be sure that no other
219 //! references to the SubstanceGroup exist.
220 /*!
221  \param sgroup - SubstanceGroup to be added to the molecule.
222 */
224  SubstanceGroup sgroup);
225 
226 //! Removes SubstanceGroups which reference a particular atom index
227 /*!
228  \param mol - molecule to be edited.
229  \param idx - atom index
230 */
232  RWMol &mol, unsigned int idx);
233 //! Removes SubstanceGroups which reference a particular bond index
234 /*!
235  \param mol - molecule to be edited.
236  \param idx - bond index
237 */
239  RWMol &mol, unsigned int idx);
240 //@}
241 
242 } // namespace RDKit
243 
244 //! allows SubstanceGroup objects to be dumped to streams
245 RDKIT_GRAPHMOL_EXPORT std::ostream &operator<<(std::ostream &target,
246  const RDKit::SubstanceGroup &sg);
247 #endif
RDKit::SubstanceGroupException::SubstanceGroupException
SubstanceGroupException(const char *msg)
construct with an error message
Definition: SubstanceGroup.h:38
RDKit::SubstanceGroup::addAtomWithIdx
void addAtomWithIdx(unsigned int idx)
RDKit::SubstanceGroupException
used to indicate errors from incorrect sgroup access
Definition: SubstanceGroup.h:35
RDKit::SubstanceGroup::addAtomWithBookmark
void addAtomWithBookmark(int mark)
RDKit::SubstanceGroup::CState
Definition: SubstanceGroup.h:76
RDKit::SubstanceGroup::addBondWithIdx
void addBondWithIdx(unsigned int idx)
point.h
RDKit::removeSubstanceGroupsReferencingAtom
RDKIT_GRAPHMOL_EXPORT void removeSubstanceGroupsReferencingAtom(RWMol &mol, unsigned int idx)
Removes SubstanceGroups which reference a particular atom index.
RDKit::SubstanceGroup::getBonds
const std::vector< unsigned int > & getBonds() const
Definition: SubstanceGroup.h:131
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:130
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:103
RDKit::SubstanceGroup
The class for representing SubstanceGroups.
Definition: SubstanceGroup.h:53
RDKit::RWMol
RWMol is a molecule class that is intended to be edited.
Definition: RWMol.h:31
RDGeom::Point3D
Definition: point.h:46
RDKit::SubstanceGroup::AttachPoint::id
std::string id
Definition: SubstanceGroup.h:68
RDKit::SubstanceGroup::getCStates
const std::vector< CState > & getCStates() const
Definition: SubstanceGroup.h:134
RDKit::SubstanceGroupChecks::sGroupSubtypes
const std::vector< std::string > sGroupSubtypes
Definition: SubstanceGroup.h:196
RDKit::SubstanceGroup::AttachPoint::operator==
bool operator==(const AttachPoint &other) const
Definition: SubstanceGroup.h:69
RDKit::SubstanceGroupChecks::isValidConnectType
RDKIT_GRAPHMOL_EXPORT bool isValidConnectType(const std::string &type)
RDKit::SubstanceGroup::BondType
BondType
Bond type (see V3000 spec)
Definition: SubstanceGroup.h:56
RDKit::SubstanceGroup::setOwningMol
void setOwningMol(ROMol *mol)
operator<<
RDKIT_GRAPHMOL_EXPORT std::ostream & operator<<(std::ostream &target, const RDKit::SubstanceGroup &sg)
allows SubstanceGroup objects to be dumped to streams
RDKit::SubstanceGroup::adjustToRemovedAtom
bool adjustToRemovedAtom(unsigned int atomIdx)
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:129
RDKit::SubstanceGroup::getBrackets
const std::vector< Bracket > & getBrackets() const
Definition: SubstanceGroup.h:133
RDKit::SubstanceGroup::AttachPoint::aIdx
unsigned int aIdx
Definition: SubstanceGroup.h:66
RDKit::SubstanceGroup::operator==
bool operator==(const SubstanceGroup &other) const
Definition: SubstanceGroup.h:167
RDKit::SubstanceGroup::SubstanceGroup
SubstanceGroup()=delete
No default constructor.
RDKit::ROMol
Definition: ROMol.h:171
RDKit::SubstanceGroup::addBondWithBookmark
void addBondWithBookmark(int mark)
RDKit::SubstanceGroup::addParentAtomWithBookmark
void addParentAtomWithBookmark(int mark)
RDKit::SubstanceGroup::adjustToRemovedBond
bool adjustToRemovedBond(unsigned int bondIdx)
RDKIT_GRAPHMOL_EXPORT
#define RDKIT_GRAPHMOL_EXPORT
Definition: export.h:307
RDKit::SubstanceGroup::operator=
SubstanceGroup & operator=(const SubstanceGroup &other)=default
RDKit::SubstanceGroup::includesAtom
bool includesAtom(unsigned int atomIdx) const
RDProps.h
RDKit::SubstanceGroup::getIndexInMol
unsigned int getIndexInMol() const
RDKit::SubstanceGroup::CState::bondIdx
unsigned int bondIdx
Definition: SubstanceGroup.h:77
RDKit::SubstanceGroup::AttachPoint::lvIdx
int lvIdx
Definition: SubstanceGroup.h:67
RDKit::RDProps
Definition: RDProps.h:14
RDKit::SubstanceGroup::~SubstanceGroup
~SubstanceGroup()
Destructor.
Definition: SubstanceGroup.h:100
RDKit::SubstanceGroup::CState::operator==
bool operator==(const CState &other) const
Definition: SubstanceGroup.h:79
RDKit::SubstanceGroup::addAttachPoint
void addAttachPoint(unsigned int aIdx, int lvIdx, const std::string &idStr)
RDKit::SubstanceGroupChecks::sGroupTypes
const std::vector< std::string > sGroupTypes
Definition: SubstanceGroup.h:188
RDKit::SubstanceGroup::addCState
void addCState(unsigned int bondIdx, const RDGeom::Point3D &vector)
RDKit::SubstanceGroupChecks::sGroupConnectTypes
const std::vector< std::string > sGroupConnectTypes
Definition: SubstanceGroup.h:197
RDKit
Std stuff.
Definition: Atom.h:30
RDKit::SubstanceGroupChecks::isSubstanceGroupIdFree
RDKIT_GRAPHMOL_EXPORT bool isSubstanceGroupIdFree(const ROMol &mol, unsigned int id)
RDKit::SubstanceGroup::getBondType
BondType getBondType(unsigned int bondIdx) const
RDKit::SubstanceGroup::SubstanceGroup
SubstanceGroup(const SubstanceGroup &other)=default
RDKit::SubstanceGroup::CState::vector
RDGeom::Point3D vector
Definition: SubstanceGroup.h:78
RDKit::SubstanceGroup::Bracket
std::array< RDGeom::Point3D, 3 > Bracket
Definition: SubstanceGroup.h:61
RDKit::SubstanceGroup::getOwningMol
ROMol & getOwningMol() const
Get the molecule that owns this instance.
Definition: SubstanceGroup.h:106
PRECONDITION
#define PRECONDITION(expr, mess)
Definition: Invariant.h:110
RDKit::SubstanceGroup::SubstanceGroup
SubstanceGroup(ROMol *owning_mol, const std::string &type)
RDKit::SubstanceGroup::addParentAtomWithIdx
void addParentAtomWithIdx(unsigned int idx)
RDKit::SubstanceGroup::addBracket
void addBracket(const Bracket &bracket)
RDKit::SubstanceGroupException::SubstanceGroupException
SubstanceGroupException(const std::string &msg)
construct with an error message
Definition: SubstanceGroup.h:40
RDKit::SubstanceGroup::AttachPoint
Definition: SubstanceGroup.h:65
RDKit::removeSubstanceGroupsReferencingBond
RDKIT_GRAPHMOL_EXPORT void removeSubstanceGroupsReferencingBond(RWMol &mol, unsigned int idx)
Removes SubstanceGroups which reference a particular bond index.
RDKit::SubstanceGroup::SubstanceGroup
SubstanceGroup(SubstanceGroup &&other)=default
RDKit::getSubstanceGroups
RDKIT_GRAPHMOL_EXPORT std::vector< SubstanceGroup > & getSubstanceGroups(ROMol &mol)
RDKit::SubstanceGroup::getAttachPoints
const std::vector< AttachPoint > & getAttachPoints() const
Definition: SubstanceGroup.h:135
RDKit::SubstanceGroup::operator=
SubstanceGroup & operator=(SubstanceGroup &&other)=default
RDKit::SubstanceGroup::includesBond
bool includesBond(unsigned int bondIdx) const
export.h