34 #ifndef __RD_SLNPARSEOPS_H__ 35 #define __RD_SLNPARSEOPS_H__ 43 #include <boost/lexical_cast.hpp> 50 void bookmarkAtomID(RWMol *mp, Atom *atom) {
55 if (mp->hasAtomBookmark(label)) {
56 std::stringstream err;
57 err <<
"SLN Parser error: Atom ID " << label <<
" used a second time.";
58 throw SLNParseException(err.str());
60 if (mp->hasBondBookmark(label)) {
61 std::stringstream err;
62 err <<
"SLN Parser error: Atom ID " << label
63 <<
" appears *after* its ring closure.";
64 throw SLNParseException(err.str());
66 mp->setAtomBookmark(atom, label);
71 template <
typename BondType>
72 void addBondToMol(RWMol *mp, BondType *bond) {
75 mp->addBond(bond,
true);
79 bond->setIsAromatic(
true);
80 bond->getBeginAtom()->setIsAromatic(
true);
81 bond->getEndAtom()->setIsAromatic(
true);
88 template <
typename AtomType>
89 int startMol(std::vector<RWMol *> &molList, AtomType *firstAtom,
93 mp->
addAtom(firstAtom,
true,
true);
94 bookmarkAtomID(mp, firstAtom);
100 for (
unsigned int i = 0; i < firstAtom->getNumExplicitHs(); ++i) {
104 firstAtom->setNumExplicitHs(0);
107 int sz = molList.size();
108 molList.push_back(mp);
114 template <
typename AtomType,
typename BondType>
116 AtomType *atom, BondType *bond,
bool doingQuery) {
118 RWMol *mp = molList[idx];
123 Atom *a1 = mp->getActiveAtom();
124 int atomIdx1 = a1->
getIdx();
125 int atomIdx2 = mp->addAtom(atom,
true,
true);
126 bookmarkAtomID(mp, atom);
127 bond->setOwningMol(mp);
128 bond->setBeginAtomIdx(atomIdx1);
129 bond->setEndAtomIdx(atomIdx2);
130 addBondToMol(mp, bond);
136 for (
unsigned int i = 0; i < atom->getNumExplicitHs(); ++i) {
137 int hIdx = mp->addAtom(
new Atom(1),
false,
true);
140 atom->setNumExplicitHs(0);
144 template <
typename AtomType>
146 AtomType *atom,
bool doingQuery) {
155 template <
typename BondType>
157 unsigned int ringIdx, BondType *bond,
158 bool postponeAllowed =
true) {
160 RWMol *mp = molList[molIdx];
164 if (!mp->hasAtomBookmark(ringIdx)) {
165 if (postponeAllowed) {
167 bond->setOwningMol(mp);
168 bond->setEndAtomIdx(mp->getActiveAtom()->getIdx());
169 mp->setBondBookmark(bond, ringIdx);
172 std::stringstream err;
173 err <<
"SLN Parser error: Ring closure " << ringIdx
174 <<
" does not have a corresponding opener.";
178 Atom *opener = mp->getAtomWithBookmark(ringIdx);
181 Atom *closer = mp->getActiveAtom();
183 bond->setBeginAtom(opener);
184 bond->setEndAtom(closer);
185 addBondToMol(mp, bond);
189 unsigned int ringIdx) {
195 template <
typename BondType>
197 unsigned int branchIdx, BondType *&bond) {
199 RWMol *mp = molList[molIdx];
202 RWMol *branch = molList[branchIdx];
206 unsigned int activeAtomIdx = mp->getActiveAtom()->getIdx();
207 unsigned int nOrigAtoms = mp->getNumAtoms();
212 mp->insertMol(*branch);
215 for (ROMol::ATOM_BOOKMARK_MAP::const_iterator bmIt =
216 branch->getAtomBookmarks()->begin();
217 bmIt != branch->getAtomBookmarks()->end(); ++bmIt) {
218 if (bmIt->first < 0)
continue;
219 if (mp->hasAtomBookmark(bmIt->first)) {
220 std::stringstream err;
221 err <<
"SLN Parser error: Atom ID " << bmIt->first
222 <<
" used a second time.";
224 }
else if (mp->hasBondBookmark(bmIt->first)) {
225 std::stringstream err;
226 err <<
"SLN Parser error: Atom ID " << bmIt->first
227 <<
" appears *after* its ring closure.";
231 "bad atom bookmark list on branch");
233 mp->getAtomWithIdx((*bmIt->second.begin())->getIdx() + nOrigAtoms);
234 mp->setAtomBookmark(tgtAtom, bmIt->first);
239 for (ROMol::BOND_BOOKMARK_MAP::const_iterator bmIt =
240 branch->getBondBookmarks()->begin();
241 bmIt != branch->getBondBookmarks()->end(); ++bmIt) {
243 "bad bond bookmark list on branch");
244 for (ROMol::BOND_PTR_LIST::const_iterator bondIt = bmIt->second.begin();
245 bondIt != bmIt->second.end(); ++bondIt) {
246 Bond *tgtBond = *bondIt;
247 if (bmIt->first > 0 && mp->hasAtomBookmark(bmIt->first)) {
248 Atom *tmpAtom = mp->getActiveAtom();
252 mp->setActiveAtom(tmpAtom);
257 mp->setBondBookmark(tgtBond, bmIt->first);
264 bond->setOwningMol(mp);
265 bond->setBeginAtomIdx(activeAtomIdx);
266 bond->setEndAtomIdx(nOrigAtoms);
267 addBondToMol(mp, bond);
274 unsigned int sz = molList.size();
275 if (sz == branchIdx + 1) {
276 molList.resize(sz - 1);
282 unsigned int branchIdx) {
291 unsigned int fragIdx) {
297 template <
typename T>
299 std::string res = boost::lexical_cast<std::string>(val);
306 RWMol::BOND_BOOKMARK_MAP *marks = mol->getBondBookmarks();
307 RWMol::BOND_BOOKMARK_MAP::iterator markI = marks->begin();
308 while (markI != marks->end()) {
309 RWMol::BOND_PTR_LIST &bonds = markI->second;
310 for (RWMol::BOND_PTR_LIST::iterator bondIt = bonds.begin();
311 bondIt != bonds.end(); ++bondIt) {
std::string convertToString(T val)
convenience function to convert the argument to a string
void addAtomToMol(std::vector< RWMol *> &molList, unsigned int idx, AtomType *atom, BondType *bond, bool doingQuery)
adds an atom to a molecule
RWMol is a molecule class that is intended to be edited.
unsigned int addAtom(bool updateLabel=true)
adds an empty Atom to our collection
#define CHECK_INVARIANT(expr, mess)
pulls in the RDKit Query functionality
pulls in the core RDKit functionality
int addBranchToMol(std::vector< RWMol *> &molList, unsigned int molIdx, unsigned int branchIdx, BondType *&bond)
int startMol(std::vector< RWMol *> &molList, AtomType *firstAtom, bool doingQuery)
initialize a molecule
unsigned int getIdx() const
returns our index within the ROMol
unsigned int getEndAtomIdx() const
returns the index of our end Atom
class for representing a bond
void setOwningMol(ROMol *other)
sets our owning molecule
void setOwningMol(ROMol *other)
sets our owning molecule
void CleanupAfterParseError(RWMol *mol)
void setEndAtomIdx(unsigned int what)
sets the index of our end Atom
const std::string _AtomID
#define PRECONDITION(expr, mess)
int addFragToMol(std::vector< RWMol *> &molList, unsigned int molIdx, unsigned int fragIdx)
adds the atoms and bonds from a fragment to the molecule, sets no bond
The class for representing atoms.
void closeRingBond(std::vector< RWMol *> &molList, unsigned int molIdx, unsigned int ringIdx, BondType *bond, bool postponeAllowed=true)
closes an indexed ring in a molecule using the bond provided
unsigned int addBond(unsigned int beginAtomIdx, unsigned int endAtomIdx, Bond::BondType order=Bond::UNSPECIFIED)
adds a Bond between the indicated Atoms