35 #ifndef __RD_SLNPARSEOPS_H__ 36 #define __RD_SLNPARSEOPS_H__ 44 #include <boost/lexical_cast.hpp> 51 void bookmarkAtomID(RWMol *mp, Atom *atom) {
56 if (mp->hasAtomBookmark(label)) {
57 std::stringstream err;
58 err <<
"SLN Parser error: Atom ID " << label <<
" used a second time.";
59 throw SLNParseException(err.str());
61 if (mp->hasBondBookmark(label)) {
62 std::stringstream err;
63 err <<
"SLN Parser error: Atom ID " << label
64 <<
" appears *after* its ring closure.";
65 throw SLNParseException(err.str());
67 mp->setAtomBookmark(atom, label);
72 template <
typename BondType>
73 void addBondToMol(RWMol *mp, BondType *bond) {
76 mp->addBond(bond,
true);
80 bond->setIsAromatic(
true);
81 bond->getBeginAtom()->setIsAromatic(
true);
82 bond->getEndAtom()->setIsAromatic(
true);
89 template <
typename AtomType>
90 int startMol(std::vector<RWMol *> &molList, AtomType *firstAtom,
94 mp->
addAtom(firstAtom,
true,
true);
95 bookmarkAtomID(mp, firstAtom);
101 for (
unsigned int i = 0; i < firstAtom->getNumExplicitHs(); ++i) {
105 firstAtom->setNumExplicitHs(0);
108 int sz = molList.size();
109 molList.push_back(mp);
115 template <
typename AtomType,
typename BondType>
117 AtomType *atom, BondType *bond,
bool doingQuery) {
119 RWMol *mp = molList[idx];
124 Atom *a1 = mp->getActiveAtom();
125 int atomIdx1 = a1->
getIdx();
126 int atomIdx2 = mp->addAtom(atom,
true,
true);
127 bookmarkAtomID(mp, atom);
128 bond->setOwningMol(mp);
129 bond->setBeginAtomIdx(atomIdx1);
130 bond->setEndAtomIdx(atomIdx2);
131 addBondToMol(mp, bond);
137 for (
unsigned int i = 0; i < atom->getNumExplicitHs(); ++i) {
138 int hIdx = mp->addAtom(
new Atom(1),
false,
true);
141 atom->setNumExplicitHs(0);
145 template <
typename AtomType>
147 AtomType *atom,
bool doingQuery) {
156 template <
typename BondType>
158 unsigned int ringIdx, BondType *bond,
159 bool postponeAllowed =
true) {
161 RWMol *mp = molList[molIdx];
165 if (!mp->hasAtomBookmark(ringIdx)) {
166 if (postponeAllowed) {
168 bond->setOwningMol(mp);
169 bond->setEndAtomIdx(mp->getActiveAtom()->getIdx());
170 mp->setBondBookmark(bond, ringIdx);
173 std::stringstream err;
174 err <<
"SLN Parser error: Ring closure " << ringIdx
175 <<
" does not have a corresponding opener.";
179 Atom *opener = mp->getAtomWithBookmark(ringIdx);
182 Atom *closer = mp->getActiveAtom();
184 bond->setBeginAtom(opener);
185 bond->setEndAtom(closer);
186 addBondToMol(mp, bond);
190 unsigned int ringIdx) {
196 template <
typename BondType>
198 unsigned int branchIdx, BondType *&bond) {
200 RWMol *mp = molList[molIdx];
203 RWMol *branch = molList[branchIdx];
207 unsigned int activeAtomIdx = mp->getActiveAtom()->getIdx();
208 unsigned int nOrigAtoms = mp->getNumAtoms();
213 mp->insertMol(*branch);
216 for (ROMol::ATOM_BOOKMARK_MAP::const_iterator bmIt =
217 branch->getAtomBookmarks()->begin();
218 bmIt != branch->getAtomBookmarks()->end(); ++bmIt) {
219 if (bmIt->first < 0)
continue;
220 if (mp->hasAtomBookmark(bmIt->first)) {
221 std::stringstream err;
222 err <<
"SLN Parser error: Atom ID " << bmIt->first
223 <<
" used a second time.";
225 }
else if (mp->hasBondBookmark(bmIt->first)) {
226 std::stringstream err;
227 err <<
"SLN Parser error: Atom ID " << bmIt->first
228 <<
" appears *after* its ring closure.";
232 "bad atom bookmark list on branch");
234 mp->getAtomWithIdx((*bmIt->second.begin())->getIdx() + nOrigAtoms);
235 mp->setAtomBookmark(tgtAtom, bmIt->first);
240 for (ROMol::BOND_BOOKMARK_MAP::const_iterator bmIt =
241 branch->getBondBookmarks()->begin();
242 bmIt != branch->getBondBookmarks()->end(); ++bmIt) {
244 "bad bond bookmark list on branch");
245 for (ROMol::BOND_PTR_LIST::const_iterator bondIt = bmIt->second.begin();
246 bondIt != bmIt->second.end(); ++bondIt) {
247 Bond *tgtBond = *bondIt;
248 if (bmIt->first > 0 && mp->hasAtomBookmark(bmIt->first)) {
249 Atom *tmpAtom = mp->getActiveAtom();
253 mp->setActiveAtom(tmpAtom);
258 mp->setBondBookmark(tgtBond, bmIt->first);
265 bond->setOwningMol(mp);
266 bond->setBeginAtomIdx(activeAtomIdx);
267 bond->setEndAtomIdx(nOrigAtoms);
268 addBondToMol(mp, bond);
275 unsigned int sz = molList.size();
276 if (sz == branchIdx + 1) {
277 molList.resize(sz - 1);
283 unsigned int branchIdx) {
292 unsigned int fragIdx) {
298 template <
typename T>
300 std::string res = boost::lexical_cast<std::string>(val);
307 RWMol::BOND_BOOKMARK_MAP *marks = mol->getBondBookmarks();
308 RWMol::BOND_BOOKMARK_MAP::iterator markI = marks->begin();
309 while (markI != marks->end()) {
310 RWMol::BOND_PTR_LIST &bonds = markI->second;
311 for (RWMol::BOND_PTR_LIST::iterator bondIt = bonds.begin();
312 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
#define PRECONDITION(expr, mess)
RDKIT_RDGENERAL_EXPORT const std::string _AtomID
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