RDKit: SubstanceGroup.h Source File

| RDKit Open-source cheminformatics and machine learning.

Loading...
Searching...
No Matches
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 <utility>
21#include <unordered_map>
22
23#include <Geometry/point.h>
24#include <RDGeneral/types.h>
25#include <RDGeneral/RDProps.h>
26#include <boost/smart_ptr.hpp>
27
28namespace RDKit {
29class ROMol;
30class RWMol;
31class Bond;
32class Atom;
33
34//! used to indicate errors from incorrect sgroup access
35 class RDKIT_GRAPHMOL_EXPORT SubstanceGroupException
36 : public std::runtime_error {
37 public:
38 //! construct with an error message
39 SubstanceGroupException(const char *msg) : std::runtime_error(msg) {}
40 //! construct with an error message
41 SubstanceGroupException(const std::string &msg) : std::runtime_error(msg) {}
42};
43
44//! The class for representing SubstanceGroups
45/*!
46 <b>Notes:</b>
47 - These are inspired by the SGroups in the MDL formats
48 - Implementation is based on 2010 MDL SD specification:
49 http://infochim.u-strasbg.fr/recherche/Download/Fragmentor/MDL_SDF.pdf
50 - See SGroups.md for further, more comprehensive notes.
51
52*/
53
54 class RDKIT_GRAPHMOL_EXPORT SubstanceGroup : public RDProps {
55 public:
56 //! Bond type (see V3000 spec)
57 enum class BondType {
58 XBOND, // External/Crossing bond
59 CBOND, // Internal/Contained bond
60 };
61
62 typedef std::array<RDGeom::Point3D, 3> Bracket;
63
64 //! Data structure for SAP lines (see V3000 spec)
65 //! lvIdx may not be set; this signaled with value -1
66 struct AttachPoint {
67 unsigned int aIdx;
68 int lvIdx;
69 std::string id;
70 bool operator==(const AttachPoint &other) const {
71 return aIdx == other.aIdx && lvIdx == other.lvIdx && id == other.id;
72 }
73 };
74
75 //! See specification for V3000 CSTATE
76 //! vector may or not be considered, depending on TYPE
77 struct CState {
78 unsigned int bondIdx;
79 RDGeom::Point3D vector;
80 bool operator==(const CState &other) const {
81 // note that we ignore coordinates for this
82 return bondIdx == other.bondIdx;
83 }
84 };
85
86//! No default constructor
87#ifndef SWIG
88 // Unfortunately, SWIG generated wrapper code uses temporary variables that
89 // require a default ctor not be deleted.
90 SubstanceGroup() = delete;
91#endif // !SWIG
92
93 //! Main Constructor. Ownership is only set on this side of the relationship:
94 //! mol->addSubstanceGroup(sgroup) still needs to be called to get ownership
95 //! on the other side.
96 SubstanceGroup(ROMol *owning_mol, const std::string &type);
97
98 SubstanceGroup(const SubstanceGroup &other) = default;
99 SubstanceGroup &operator=(const SubstanceGroup &other) = default;
100
101 SubstanceGroup(SubstanceGroup &&other) noexcept : RDProps(std::move(other)) {
102 dp_mol = std::exchange(other.dp_mol, nullptr);
103 d_atoms = std::move(other.d_atoms);
104 d_patoms = std::move(other.d_patoms);
105 d_bonds = std::move(other.d_bonds);
106 d_brackets = std::move(other.d_brackets);
107 d_cstates = std::move(other.d_cstates);
108 d_saps = std::move(other.d_saps);
109 }
110
111 SubstanceGroup &operator=(SubstanceGroup &&other) noexcept {
112 if (this == &other) {
113 return *this;
114 }
115 RDProps::operator=(std::move(other));
116 dp_mol = std::exchange(other.dp_mol, nullptr);
117 d_atoms = std::move(other.d_atoms);
118 d_patoms = std::move(other.d_patoms);
119 d_bonds = std::move(other.d_bonds);
120 d_brackets = std::move(other.d_brackets);
121 d_cstates = std::move(other.d_cstates);
122 d_saps = std::move(other.d_saps);
123 return *this;
124 }
125
126 //! Destructor
127 ~SubstanceGroup() = default;
128
129 //! returns whether or not this belongs to a molecule
130 bool hasOwningMol() const { return dp_mol != nullptr; }
131
132 //! Get the molecule that owns this instance
133 ROMol &getOwningMol() const {
134 PRECONDITION(dp_mol, "no owner");
135 return *dp_mol;
136 }
137
138 //! returns whether or not this group is valid; invalid groups must be
139 //! ignored.
140 bool getIsValid() const { return d_isValid; }
141
142 //! set whether or not this group is valid; invalid groups must be ignored.
143 void setIsValid(bool isValid) { d_isValid = isValid; }
144
145 //! get the index of this sgroup in dp_mol's sgroups vector
146 //! (do not mistake this by the ID!)
147 unsigned int getIndexInMol() const;
148
149 /* Atom and Bond methods */
150 void addAtomWithIdx(unsigned int idx);
151 void addParentAtomWithIdx(unsigned int idx);
152 void addBondWithIdx(unsigned int idx);
153 void addAtomWithBookmark(int mark);
154 void addParentAtomWithBookmark(int mark);
155 void addBondWithBookmark(int mark);
156
157 // These methods should be handled with care, since they can leave
158 // Attachment points and CStates in an invalid state!
159 void removeAtomWithIdx(unsigned int idx);
160 void removeParentAtomWithIdx(unsigned int idx);
161 void removeBondWithIdx(unsigned int idx);
162
163 void addBracket(const Bracket &bracket);
164 void addCState(unsigned int bondIdx, const RDGeom::Point3D &vector);
165 void addAttachPoint(unsigned int aIdx, int lvIdx, const std::string &idStr);
166
167 BondType getBondType(unsigned int bondIdx) const;
168
169 const std::vector<unsigned int> &getAtoms() const { return d_atoms; }
170 const std::vector<unsigned int> &getParentAtoms() const { return d_patoms; }
171 const std::vector<unsigned int> &getBonds() const { return d_bonds; }
172
173 void setAtoms(std::vector<unsigned int> atoms);
174 void setParentAtoms(std::vector<unsigned int> patoms);
175 void setBonds(std::vector<unsigned int> bonds);
176
177 const std::vector<Bracket> &getBrackets() const { return d_brackets; }
178 const std::vector<CState> &getCStates() const { return d_cstates; }
179 const std::vector<AttachPoint> &getAttachPoints() const { return d_saps; }
180
181 std::vector<Bracket> &getBrackets() { return d_brackets; }
182 std::vector<CState> &getCStates() { return d_cstates; }
183 std::vector<AttachPoint> &getAttachPoints() { return d_saps; }
184
185 void clearBrackets() { d_brackets.clear(); }
186 void clearCStates() { d_cstates.clear(); }
187 void clearAttachPoints() { d_saps.clear(); }
188
189 //! adjusts our atom IDs to reflect that an atom has been removed from the
190 //! parent molecule
191 //! decrements all atom IDs that are higher than \c atomIdx
192 //! raises a \c SubstanceGroupException if \c atomIdx is actually part of
193 //! this substance group
194 //! \returns whether or not anything was changed
195 bool adjustToRemovedAtom(unsigned int atomIdx);
196
197 //! \returns whether or not anything the specified atom is part of the
198 //! definition of this substance group
199 bool includesAtom(unsigned int atomIdx) const;
200
201 //! adjusts our bond IDs to reflect that a bond has been removed from the
202 //! parent molecule
203 //! decrements all bond IDs that are higher than \c bondIdx
204 //! raises a \c SubstanceGroupException if \c bondIdx is actually part of
205 //! this substance group
206 //! \returns whether or not anything was changed
207 bool adjustToRemovedBond(unsigned int bondIdx);
208
209 //! \returns whether or not anything the specified bond is part of the
210 //! definition of this substance group
211 bool includesBond(unsigned int bondIdx) const;
212
213 //! Set owning molecule
214 //! This only updates atoms and bonds; parent sgroup has to be updated
215 //! independently, since parent might not exist at the time this is
216 //! called.
217 void setOwningMol(ROMol *mol);
218
219 bool operator==(const SubstanceGroup &other) const {
220 // we ignore brackets and cstates, which involve coordinates
221 return dp_mol == other.dp_mol && d_atoms == other.d_atoms &&
222 d_patoms == other.d_patoms && d_bonds == other.d_bonds &&
223 d_saps == other.d_saps;
224 }
225
226 private:
227 ROMol *dp_mol = nullptr; // owning molecule
228
229 bool d_isValid = true;
230
231 std::vector<unsigned int> d_atoms;
232 std::vector<unsigned int> d_patoms;
233 std::vector<unsigned int> d_bonds;
234
235 std::vector<Bracket> d_brackets;
236 std::vector<CState> d_cstates;
237 std::vector<AttachPoint> d_saps;
238}; // namespace RDKit
239
240 namespace SubstanceGroupChecks {
241
242 const std::vector<std::string> sGroupTypes = {
243 // polymer sgroups:
244 "SRU", "MON", "COP", "CRO", "GRA", "MOD", "MER", "ANY",
245 // formulations/mixtures:
246 "COM", "MIX", "FOR",
247 // other
248 "SUP", "MUL", "DAT", "GEN"};
249
250 const std::vector<std::string> sGroupSubtypes = {"ALT", "RAN", "BLO"};
251 const std::vector<std::string> sGroupConnectTypes = {"HH", "HT", "EU"};
252 const std::vector<std::string> sGroupClasses = {
253 "AA", "dAA", "DNA", "RNA", "SUGAR", "BASE",
254 "PHOSPHATE", "LINKER", "CHEM", "LGRP", "MODAA", "MODdAA",
255 "MODDNA", "MODRNA", "XLINKAA", "XLINKdAA", "XLINKDNA", "XLINKRNA",
256};
257
258 RDKIT_GRAPHMOL_EXPORT bool isValidType(const std::string &type);
259
260 RDKIT_GRAPHMOL_EXPORT bool isValidSubType(const std::string &type);
261
262 RDKIT_GRAPHMOL_EXPORT bool isValidConnectType(const std::string &type);
263
264 RDKIT_GRAPHMOL_EXPORT bool isValidClass(const std::string &sgroupClass);
265
266 RDKIT_GRAPHMOL_EXPORT bool isSubstanceGroupIdFree(const ROMol &mol,
267 unsigned int id);
268
269} // namespace SubstanceGroupChecks
270
271//! \name SubstanceGroups and molecules
272//! @{
273
274 RDKIT_GRAPHMOL_EXPORT std::vector<SubstanceGroup> &getSubstanceGroups(
275 ROMol &mol);
276 RDKIT_GRAPHMOL_EXPORT const std::vector<SubstanceGroup> &getSubstanceGroups(
277 const ROMol &mol);
278
279//! Add a new SubstanceGroup. A copy is added, so we can be sure that no other
280//! references to the SubstanceGroup exist.
281/*!
282 \param sgroup - SubstanceGroup to be added to the molecule.
283*/
284 RDKIT_GRAPHMOL_EXPORT unsigned int addSubstanceGroup(ROMol &mol,
285 SubstanceGroup sgroup);
286
287//! Removes SubstanceGroups which reference a particular atom index
288/*!
289 \param mol - molecule to be edited.
290 \param idx - atom index
291*/
292 RDKIT_GRAPHMOL_EXPORT void removeSubstanceGroupsReferencingAtom(
293 RWMol &mol, unsigned int idx);
294//! Removes SubstanceGroups which reference a particular bond index
295/*!
296 \param mol - molecule to be edited.
297 \param idx - bond index
298*/
299 RDKIT_GRAPHMOL_EXPORT void removeSubstanceGroupsReferencingBond(
300 RWMol &mol, unsigned int idx);
301//! @}
302
303} // namespace RDKit
304
305//! allows SubstanceGroup objects to be dumped to streams
306 RDKIT_GRAPHMOL_EXPORT std::ostream &operator<<(std::ostream &target,
307 const RDKit::SubstanceGroup &sg);
308#endif
PRECONDITION
#define PRECONDITION(expr, mess)
Definition Invariant.h:108
RDProps.h
operator<<
RDKIT_GRAPHMOL_EXPORT std::ostream & operator<<(std::ostream &target, const RDKit::SubstanceGroup &sg)
allows SubstanceGroup objects to be dumped to streams
RDGeom::Point3D
Definition point.h:53
RDKit::Atom
The class for representing atoms.
Definition Atom.h:74
RDKit::Bond
class for representing a bond
Definition Bond.h:46
RDKit::RDProps::operator=
RDProps & operator=(const RDProps &rhs)
Definition RDProps.h:24
RDKit::RDProps::RDProps
RDProps()
Definition RDProps.h:22
RDKit::ROMol
Definition ROMol.h:199
RDKit::RWMol
RWMol is a molecule class that is intended to be edited.
Definition RWMol.h:32
RDKit::SubstanceGroupException::SubstanceGroupException
SubstanceGroupException(const std::string &msg)
construct with an error message
Definition SubstanceGroup.h:41
RDKit::SubstanceGroupException::SubstanceGroupException
SubstanceGroupException(const char *msg)
construct with an error message
Definition SubstanceGroup.h:39
RDKit::SubstanceGroup
The class for representing SubstanceGroups.
Definition SubstanceGroup.h:54
RDKit::SubstanceGroup::getBonds
const std::vector< unsigned int > & getBonds() const
Definition SubstanceGroup.h:171
RDKit::SubstanceGroup::addBondWithIdx
void addBondWithIdx(unsigned int idx)
RDKit::SubstanceGroup::setOwningMol
void setOwningMol(ROMol *mol)
RDKit::SubstanceGroup::operator=
SubstanceGroup & operator=(const SubstanceGroup &other)=default
RDKit::SubstanceGroup::setParentAtoms
void setParentAtoms(std::vector< unsigned int > patoms)
RDKit::SubstanceGroup::addAttachPoint
void addAttachPoint(unsigned int aIdx, int lvIdx, const std::string &idStr)
RDKit::SubstanceGroup::clearAttachPoints
void clearAttachPoints()
Definition SubstanceGroup.h:187
RDKit::SubstanceGroup::setBonds
void setBonds(std::vector< unsigned int > bonds)
RDKit::SubstanceGroup::~SubstanceGroup
~SubstanceGroup()=default
Destructor.
RDKit::SubstanceGroup::setIsValid
void setIsValid(bool isValid)
set whether or not this group is valid; invalid groups must be ignored.
Definition SubstanceGroup.h:143
RDKit::SubstanceGroup::getAtoms
const std::vector< unsigned int > & getAtoms() const
Definition SubstanceGroup.h:169
RDKit::SubstanceGroup::addParentAtomWithBookmark
void addParentAtomWithBookmark(int mark)
RDKit::SubstanceGroup::setAtoms
void setAtoms(std::vector< unsigned int > atoms)
RDKit::SubstanceGroup::adjustToRemovedBond
bool adjustToRemovedBond(unsigned int bondIdx)
RDKit::SubstanceGroup::addCState
void addCState(unsigned int bondIdx, const RDGeom::Point3D &vector)
RDKit::SubstanceGroup::getOwningMol
ROMol & getOwningMol() const
Get the molecule that owns this instance.
Definition SubstanceGroup.h:133
RDKit::SubstanceGroup::getBrackets
const std::vector< Bracket > & getBrackets() const
Definition SubstanceGroup.h:177
RDKit::SubstanceGroup::SubstanceGroup
SubstanceGroup()=delete
No default constructor.
RDKit::SubstanceGroup::adjustToRemovedAtom
bool adjustToRemovedAtom(unsigned int atomIdx)
RDKit::SubstanceGroup::operator==
bool operator==(const SubstanceGroup &other) const
Definition SubstanceGroup.h:219
RDKit::SubstanceGroup::BondType
BondType
Bond type (see V3000 spec)
Definition SubstanceGroup.h:57
RDKit::SubstanceGroup::BondType::XBOND
@ XBOND
Definition SubstanceGroup.h:58
RDKit::SubstanceGroup::BondType::CBOND
@ CBOND
Definition SubstanceGroup.h:59
RDKit::SubstanceGroup::SubstanceGroup
SubstanceGroup(const SubstanceGroup &other)=default
RDKit::SubstanceGroup::SubstanceGroup
SubstanceGroup(SubstanceGroup &&other) noexcept
Definition SubstanceGroup.h:101
RDKit::SubstanceGroup::clearBrackets
void clearBrackets()
Definition SubstanceGroup.h:185
RDKit::SubstanceGroup::addBondWithBookmark
void addBondWithBookmark(int mark)
RDKit::SubstanceGroup::getParentAtoms
const std::vector< unsigned int > & getParentAtoms() const
Definition SubstanceGroup.h:170
RDKit::SubstanceGroup::clearCStates
void clearCStates()
Definition SubstanceGroup.h:186
RDKit::SubstanceGroup::addAtomWithBookmark
void addAtomWithBookmark(int mark)
RDKit::SubstanceGroup::includesAtom
bool includesAtom(unsigned int atomIdx) const
RDKit::SubstanceGroup::getBrackets
std::vector< Bracket > & getBrackets()
Definition SubstanceGroup.h:181
RDKit::SubstanceGroup::removeParentAtomWithIdx
void removeParentAtomWithIdx(unsigned int idx)
RDKit::SubstanceGroup::SubstanceGroup
SubstanceGroup(ROMol *owning_mol, const std::string &type)
RDKit::SubstanceGroup::addParentAtomWithIdx
void addParentAtomWithIdx(unsigned int idx)
RDKit::SubstanceGroup::addAtomWithIdx
void addAtomWithIdx(unsigned int idx)
RDKit::SubstanceGroup::getIsValid
bool getIsValid() const
Definition SubstanceGroup.h:140
RDKit::SubstanceGroup::getCStates
const std::vector< CState > & getCStates() const
Definition SubstanceGroup.h:178
RDKit::SubstanceGroup::Bracket
std::array< RDGeom::Point3D, 3 > Bracket
Definition SubstanceGroup.h:62
RDKit::SubstanceGroup::addBracket
void addBracket(const Bracket &bracket)
RDKit::SubstanceGroup::getAttachPoints
const std::vector< AttachPoint > & getAttachPoints() const
Definition SubstanceGroup.h:179
RDKit::SubstanceGroup::getCStates
std::vector< CState > & getCStates()
Definition SubstanceGroup.h:182
RDKit::SubstanceGroup::hasOwningMol
bool hasOwningMol() const
returns whether or not this belongs to a molecule
Definition SubstanceGroup.h:130
RDKit::SubstanceGroup::includesBond
bool includesBond(unsigned int bondIdx) const
RDKit::SubstanceGroup::removeAtomWithIdx
void removeAtomWithIdx(unsigned int idx)
RDKit::SubstanceGroup::getAttachPoints
std::vector< AttachPoint > & getAttachPoints()
Definition SubstanceGroup.h:183
RDKit::SubstanceGroup::removeBondWithIdx
void removeBondWithIdx(unsigned int idx)
RDKit::SubstanceGroup::getBondType
BondType getBondType(unsigned int bondIdx) const
RDKit::SubstanceGroup::operator=
SubstanceGroup & operator=(SubstanceGroup &&other) noexcept
Definition SubstanceGroup.h:111
RDKit::SubstanceGroup::getIndexInMol
unsigned int getIndexInMol() const
export.h
RDKIT_GRAPHMOL_EXPORT
#define RDKIT_GRAPHMOL_EXPORT
Definition export.h:249
RDKit::SubstanceGroupChecks
Definition SubstanceGroup.h:240
RDKit::SubstanceGroupChecks::sGroupClasses
const std::vector< std::string > sGroupClasses
Definition SubstanceGroup.h:252
RDKit::SubstanceGroupChecks::isValidClass
RDKIT_GRAPHMOL_EXPORT bool isValidClass(const std::string &sgroupClass)
RDKit::SubstanceGroupChecks::isValidType
RDKIT_GRAPHMOL_EXPORT bool isValidType(const std::string &type)
RDKit::SubstanceGroupChecks::isValidSubType
RDKIT_GRAPHMOL_EXPORT bool isValidSubType(const std::string &type)
RDKit::SubstanceGroupChecks::sGroupConnectTypes
const std::vector< std::string > sGroupConnectTypes
Definition SubstanceGroup.h:251
RDKit::SubstanceGroupChecks::isSubstanceGroupIdFree
RDKIT_GRAPHMOL_EXPORT bool isSubstanceGroupIdFree(const ROMol &mol, unsigned int id)
RDKit::SubstanceGroupChecks::isValidConnectType
RDKIT_GRAPHMOL_EXPORT bool isValidConnectType(const std::string &type)
RDKit::SubstanceGroupChecks::sGroupSubtypes
const std::vector< std::string > sGroupSubtypes
Definition SubstanceGroup.h:250
RDKit::SubstanceGroupChecks::sGroupTypes
const std::vector< std::string > sGroupTypes
Definition SubstanceGroup.h:242
RDKit
Std stuff.
Definition Abbreviations.h:19
RDKit::getSubstanceGroups
RDKIT_GRAPHMOL_EXPORT std::vector< SubstanceGroup > & getSubstanceGroups(ROMol &mol)
RDKit::addSubstanceGroup
RDKIT_GRAPHMOL_EXPORT unsigned int addSubstanceGroup(ROMol &mol, SubstanceGroup sgroup)
RDKit::removeSubstanceGroupsReferencingBond
RDKIT_GRAPHMOL_EXPORT void removeSubstanceGroupsReferencingBond(RWMol &mol, unsigned int idx)
Removes SubstanceGroups which reference a particular bond index.
RDKit::removeSubstanceGroupsReferencingAtom
RDKIT_GRAPHMOL_EXPORT void removeSubstanceGroupsReferencingAtom(RWMol &mol, unsigned int idx)
Removes SubstanceGroups which reference a particular atom index.
point.h
RDKit::SubstanceGroup::AttachPoint
Definition SubstanceGroup.h:66
RDKit::SubstanceGroup::AttachPoint::id
std::string id
Definition SubstanceGroup.h:69
RDKit::SubstanceGroup::AttachPoint::aIdx
unsigned int aIdx
Definition SubstanceGroup.h:67
RDKit::SubstanceGroup::AttachPoint::lvIdx
int lvIdx
Definition SubstanceGroup.h:68
RDKit::SubstanceGroup::AttachPoint::operator==
bool operator==(const AttachPoint &other) const
Definition SubstanceGroup.h:70
RDKit::SubstanceGroup::CState
Definition SubstanceGroup.h:77
RDKit::SubstanceGroup::CState::vector
RDGeom::Point3D vector
Definition SubstanceGroup.h:79
RDKit::SubstanceGroup::CState::operator==
bool operator==(const CState &other) const
Definition SubstanceGroup.h:80
RDKit::SubstanceGroup::CState::bondIdx
unsigned int bondIdx
Definition SubstanceGroup.h:78
types.h