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 #ifndef SWIG
87  // Unfortunately, SWIG generated wrapper code uses temporary variables that
88  // require a default ctor not be deleted.
89  SubstanceGroup() = delete;
90 #endif // !SWIG
91 
92  //! Main Constructor. Ownership is only set on this side of the relationship:
93  //! mol->addSubstanceGroup(sgroup) still needs to be called to get ownership
94  //! on the other side.
95  SubstanceGroup(ROMol *owning_mol, const std::string &type);
96 
97  SubstanceGroup(const SubstanceGroup &other) = default;
98  SubstanceGroup(SubstanceGroup &&other) = default;
99 
100  SubstanceGroup &operator=(const SubstanceGroup &other) = default;
102 
103  //! Destructor
104  ~SubstanceGroup() = default;
105 
106  //! returns whether or not this belongs to a molecule
107  bool hasOwningMol() const { return dp_mol != nullptr; }
108 
109  //! Get the molecule that owns this instance
110  ROMol &getOwningMol() const {
111  PRECONDITION(dp_mol, "no owner");
112  return *dp_mol;
113  }
114 
115  //! returns whether or not this group is valid; invalid groups must be
116  //! ignored.
117  bool getIsValid() const { return d_isValid; }
118 
119  //! set whether or not this group is valid; invalid groups must be ignored.
120  void setIsValid(bool isValid) { d_isValid = isValid; }
121 
122  //! get the index of this sgroup in dp_mol's sgroups vector
123  //! (do not mistake this by the ID!)
124  unsigned int getIndexInMol() const;
125 
126  /* Atom and Bond methods */
127  void addAtomWithIdx(unsigned int idx);
128  void addParentAtomWithIdx(unsigned int idx);
129  void addBondWithIdx(unsigned int idx);
130  void addAtomWithBookmark(int mark);
132  void addBondWithBookmark(int mark);
133 
134  void addBracket(const Bracket &bracket);
135  void addCState(unsigned int bondIdx, const RDGeom::Point3D &vector);
136  void addAttachPoint(unsigned int aIdx, int lvIdx, const std::string &idStr);
137 
138  BondType getBondType(unsigned int bondIdx) const;
139 
140  const std::vector<unsigned int> &getAtoms() const { return d_atoms; }
141  const std::vector<unsigned int> &getParentAtoms() const { return d_patoms; }
142  const std::vector<unsigned int> &getBonds() const { return d_bonds; }
143 
144  void setAtoms(std::vector<unsigned int> atoms) { d_atoms = std::move(atoms); }
145  void setParentAtoms(std::vector<unsigned int> patoms) {
146  d_patoms = std::move(patoms);
147  }
148  void setBonds(std::vector<unsigned int> bonds) { d_bonds = std::move(bonds); }
149 
150  const std::vector<Bracket> &getBrackets() const { return d_brackets; }
151  const std::vector<CState> &getCStates() const { return d_cstates; }
152  const std::vector<AttachPoint> &getAttachPoints() const { return d_saps; }
153 
154  std::vector<Bracket> &getBrackets() { return d_brackets; }
155  std::vector<CState> &getCStates() { return d_cstates; }
156  std::vector<AttachPoint> &getAttachPoints() { return d_saps; }
157 
158  void clearBrackets() { d_brackets.clear(); }
159  void clearCStates() { d_cstates.clear(); }
160  void clearAttachPoints() { d_saps.clear(); }
161 
162  //! adjusts our atom IDs to reflect that an atom has been removed from the
163  //! parent molecule
164  //! decrements all atom IDs that are higher than \c atomIdx
165  //! raises a \c SubstanceGroupException if \c atomIdx is actually part of
166  //! this substance group
167  //! \returns whether or not anything was changed
168  bool adjustToRemovedAtom(unsigned int atomIdx);
169 
170  //! \returns whether or not anything the specified atom is part of the
171  //! definition of this substance group
172  bool includesAtom(unsigned int atomIdx) const;
173 
174  //! adjusts our bond IDs to reflect that a bond has been removed from the
175  //! parent molecule
176  //! decrements all bond IDs that are higher than \c bondIdx
177  //! raises a \c SubstanceGroupException if \c bondIdx is actually part of
178  //! this substance group
179  //! \returns whether or not anything was changed
180  bool adjustToRemovedBond(unsigned int bondIdx);
181 
182  //! \returns whether or not anything the specified bond is part of the
183  //! definition of this substance group
184  bool includesBond(unsigned int bondIdx) const;
185 
186  //! Set owning molecule
187  //! This only updates atoms and bonds; parent sgroup has to be updated
188  //! independently, since parent might not exist at the time this is
189  //! called.
190  void setOwningMol(ROMol *mol);
191 
192  bool operator==(const SubstanceGroup &other) const {
193  // we ignore brackets and cstates, which involve coordinates
194  return dp_mol == other.dp_mol && d_atoms == other.d_atoms &&
195  d_patoms == other.d_patoms && d_bonds == other.d_bonds &&
196  d_saps == other.d_saps;
197  }
198 
199  private:
200  ROMol *dp_mol = nullptr; // owning molecule
201 
202  bool d_isValid = true;
203 
204  std::vector<unsigned int> d_atoms;
205  std::vector<unsigned int> d_patoms;
206  std::vector<unsigned int> d_bonds;
207 
208  std::vector<Bracket> d_brackets;
209  std::vector<CState> d_cstates;
210  std::vector<AttachPoint> d_saps;
211 };
212 
213 namespace SubstanceGroupChecks {
214 
215 const std::vector<std::string> sGroupTypes = {
216  // polymer sgroups:
217  "SRU", "MON", "COP", "CRO", "GRA", "MOD", "MER", "ANY",
218  // formulations/mixtures:
219  "COM", "MIX", "FOR",
220  // other
221  "SUP", "MUL", "DAT", "GEN"};
222 
223 const std::vector<std::string> sGroupSubtypes = {"ALT", "RAN", "BLO"};
224 const std::vector<std::string> sGroupConnectTypes = {"HH", "HT", "EU"};
225 
226 RDKIT_GRAPHMOL_EXPORT bool isValidType(const std::string &type);
227 
228 RDKIT_GRAPHMOL_EXPORT bool isValidSubType(const std::string &type);
229 
230 RDKIT_GRAPHMOL_EXPORT bool isValidConnectType(const std::string &type);
231 
233  unsigned int id);
234 
235 } // namespace SubstanceGroupChecks
236 
237 //! \name SubstanceGroups and molecules
238 //@{
239 
240 RDKIT_GRAPHMOL_EXPORT std::vector<SubstanceGroup> &getSubstanceGroups(
241  ROMol &mol);
242 RDKIT_GRAPHMOL_EXPORT const std::vector<SubstanceGroup> &getSubstanceGroups(
243  const ROMol &mol);
244 
245 //! Add a new SubstanceGroup. A copy is added, so we can be sure that no other
246 //! references to the SubstanceGroup exist.
247 /*!
248  \param sgroup - SubstanceGroup to be added to the molecule.
249 */
251  SubstanceGroup sgroup);
252 
253 //! Removes SubstanceGroups which reference a particular atom index
254 /*!
255  \param mol - molecule to be edited.
256  \param idx - atom index
257 */
259  RWMol &mol, unsigned int idx);
260 //! Removes SubstanceGroups which reference a particular bond index
261 /*!
262  \param mol - molecule to be edited.
263  \param idx - bond index
264 */
266  RWMol &mol, unsigned int idx);
267 //@}
268 
269 } // namespace RDKit
270 
271 //! allows SubstanceGroup objects to be dumped to streams
272 RDKIT_GRAPHMOL_EXPORT std::ostream &operator<<(std::ostream &target,
273  const RDKit::SubstanceGroup &sg);
274 #endif
#define PRECONDITION(expr, mess)
Definition: Invariant.h:109
RDKIT_GRAPHMOL_EXPORT std::ostream & operator<<(std::ostream &target, const RDKit::SubstanceGroup &sg)
allows SubstanceGroup objects to be dumped to streams
RWMol is a molecule class that is intended to be edited.
Definition: RWMol.h:32
used to indicate errors from incorrect sgroup access
SubstanceGroupException(const std::string &msg)
construct with an error message
SubstanceGroupException(const char *msg)
construct with an error message
The class for representing SubstanceGroups.
const std::vector< Bracket > & getBrackets() const
void addBondWithIdx(unsigned int idx)
void setOwningMol(ROMol *mol)
void setParentAtoms(std::vector< unsigned int > patoms)
std::vector< AttachPoint > & getAttachPoints()
void addAttachPoint(unsigned int aIdx, int lvIdx, const std::string &idStr)
void setBonds(std::vector< unsigned int > bonds)
~SubstanceGroup()=default
Destructor.
void setIsValid(bool isValid)
set whether or not this group is valid; invalid groups must be ignored.
const std::vector< unsigned int > & getAtoms() const
std::vector< Bracket > & getBrackets()
void addParentAtomWithBookmark(int mark)
const std::vector< unsigned int > & getParentAtoms() const
SubstanceGroup(SubstanceGroup &&other)=default
void setAtoms(std::vector< unsigned int > atoms)
const std::vector< unsigned int > & getBonds() const
bool adjustToRemovedBond(unsigned int bondIdx)
void addCState(unsigned int bondIdx, const RDGeom::Point3D &vector)
const std::vector< CState > & getCStates() const
SubstanceGroup()=delete
No default constructor.
bool adjustToRemovedAtom(unsigned int atomIdx)
bool operator==(const SubstanceGroup &other) const
BondType
Bond type (see V3000 spec)
SubstanceGroup(const SubstanceGroup &other)=default
const std::vector< AttachPoint > & getAttachPoints() const
ROMol & getOwningMol() const
Get the molecule that owns this instance.
std::vector< CState > & getCStates()
SubstanceGroup & operator=(const SubstanceGroup &other)=default
void addBondWithBookmark(int mark)
void addAtomWithBookmark(int mark)
bool includesAtom(unsigned int atomIdx) const
SubstanceGroup(ROMol *owning_mol, const std::string &type)
void addParentAtomWithIdx(unsigned int idx)
void addAtomWithIdx(unsigned int idx)
SubstanceGroup & operator=(SubstanceGroup &&other)=default
std::array< RDGeom::Point3D, 3 > Bracket
void addBracket(const Bracket &bracket)
bool hasOwningMol() const
returns whether or not this belongs to a molecule
bool includesBond(unsigned int bondIdx) const
BondType getBondType(unsigned int bondIdx) const
unsigned int getIndexInMol() const
#define RDKIT_GRAPHMOL_EXPORT
Definition: export.h:209
RDKIT_GRAPHMOL_EXPORT bool isValidType(const std::string &type)
RDKIT_GRAPHMOL_EXPORT bool isValidSubType(const std::string &type)
const std::vector< std::string > sGroupConnectTypes
RDKIT_GRAPHMOL_EXPORT bool isSubstanceGroupIdFree(const ROMol &mol, unsigned int id)
RDKIT_GRAPHMOL_EXPORT bool isValidConnectType(const std::string &type)
const std::vector< std::string > sGroupSubtypes
const std::vector< std::string > sGroupTypes
Std stuff.
Definition: Abbreviations.h:18
RDKIT_GRAPHMOL_EXPORT unsigned int addSubstanceGroup(ROMol &mol, SubstanceGroup sgroup)
RDKIT_GRAPHMOL_EXPORT void removeSubstanceGroupsReferencingBond(RWMol &mol, unsigned int idx)
Removes SubstanceGroups which reference a particular bond index.
RDKIT_GRAPHMOL_EXPORT std::vector< SubstanceGroup > & getSubstanceGroups(ROMol &mol)
RDKIT_GRAPHMOL_EXPORT void removeSubstanceGroupsReferencingAtom(RWMol &mol, unsigned int idx)
Removes SubstanceGroups which reference a particular atom index.
bool operator==(const AttachPoint &other) const
bool operator==(const CState &other) const