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) {
202 template <
typename BondType>
204 unsigned int branchIdx, BondType *&bond) {
206 RWMol *mp = molList[molIdx];
209 RWMol *branch = molList[branchIdx];
213 unsigned int activeAtomIdx = mp->getActiveAtom()->getIdx();
214 unsigned int nOrigAtoms = mp->getNumAtoms();
219 mp->insertMol(*branch);
222 for (ROMol::ATOM_BOOKMARK_MAP::const_iterator bmIt =
223 branch->getAtomBookmarks()->begin();
224 bmIt != branch->getAtomBookmarks()->end(); ++bmIt) {
225 if (bmIt->first < 0)
continue;
226 if (mp->hasAtomBookmark(bmIt->first)) {
227 std::stringstream err;
228 err <<
"SLN Parser error: Atom ID " << bmIt->first
229 <<
" used a second time.";
231 }
else if (mp->hasBondBookmark(bmIt->first)) {
232 std::stringstream err;
233 err <<
"SLN Parser error: Atom ID " << bmIt->first
234 <<
" appears *after* its ring closure.";
238 "bad atom bookmark list on branch");
240 mp->getAtomWithIdx((*bmIt->second.begin())->getIdx() + nOrigAtoms);
241 mp->setAtomBookmark(tgtAtom, bmIt->first);
246 for (ROMol::BOND_BOOKMARK_MAP::const_iterator bmIt =
247 branch->getBondBookmarks()->begin();
248 bmIt != branch->getBondBookmarks()->end(); ++bmIt) {
250 "bad bond bookmark list on branch");
251 for (ROMol::BOND_PTR_LIST::const_iterator bondIt = bmIt->second.begin();
252 bondIt != bmIt->second.end(); ++bondIt) {
253 Bond *tgtBond = *bondIt;
254 if (bmIt->first > 0 && mp->hasAtomBookmark(bmIt->first)) {
255 Atom *tmpAtom = mp->getActiveAtom();
259 mp->setActiveAtom(tmpAtom);
264 mp->setBondBookmark(tgtBond, bmIt->first);
271 bond->setOwningMol(mp);
272 bond->setBeginAtomIdx(activeAtomIdx);
273 bond->setEndAtomIdx(nOrigAtoms);
274 addBondToMol(mp, bond);
281 unsigned int sz = molList.size();
282 if (sz == branchIdx + 1) {
283 molList.resize(sz - 1);
289 unsigned int branchIdx) {
305 unsigned int fragIdx) {
318 template <
typename T>
320 std::string res = boost::lexical_cast<std::string>(val);
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 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