34 #ifndef __RD_SLNPARSEOPS_H__ 35 #define __RD_SLNPARSEOPS_H__ 42 #include <boost/lexical_cast.hpp> 48 void bookmarkAtomID(RWMol *mp,Atom *atom){
53 if(mp->hasAtomBookmark(label)){
54 std::stringstream err;
55 err <<
"SLN Parser error: Atom ID " << label <<
" used a second time.";
56 throw SLNParseException(err.str());
58 if(mp->hasBondBookmark(label)){
59 std::stringstream err;
60 err <<
"SLN Parser error: Atom ID " << label <<
" appears *after* its ring closure.";
61 throw SLNParseException(err.str());
63 mp->setAtomBookmark(atom,label);
68 template<
typename BondType>
69 void addBondToMol(RWMol *mp,BondType *bond){
72 mp->addBond(bond,
true);
76 bond->setIsAromatic(
true);
77 bond->getBeginAtom()->setIsAromatic(
true);
78 bond->getEndAtom()->setIsAromatic(
true);
85 template <
typename AtomType>
86 int startMol(std::vector<RWMol *> &molList,AtomType *firstAtom,
bool doingQuery){
89 mp->
addAtom(firstAtom,
true,
true);
90 bookmarkAtomID(mp,firstAtom);
95 for(
unsigned int i=0;i<firstAtom->getNumExplicitHs();++i){
99 firstAtom->setNumExplicitHs(0);
102 int sz = molList.size();
103 molList.push_back(mp);
109 template<
typename AtomType,
typename BondType>
110 void addAtomToMol(std::vector<RWMol *> &molList,
unsigned int idx,AtomType *atom,
111 BondType *bond,
bool doingQuery){
113 RWMol *mp=molList[idx];
118 Atom *a1 = mp->getActiveAtom();
119 int atomIdx1=a1->
getIdx();
120 int atomIdx2=mp->addAtom(atom,
true,
true);
121 bookmarkAtomID(mp,atom);
122 bond->setOwningMol(mp);
123 bond->setBeginAtomIdx(atomIdx1);
124 bond->setEndAtomIdx(atomIdx2);
125 addBondToMol(mp,bond);
130 for(
unsigned int i=0;i<atom->getNumExplicitHs();++i){
131 int hIdx=mp->addAtom(
new Atom(1),
false,
true);
134 atom->setNumExplicitHs(0);
138 template<
typename AtomType>
139 void addAtomToMol(std::vector<RWMol *> &molList,
unsigned int idx,AtomType *atom,
bool doingQuery){
148 template <
typename BondType>
150 unsigned int ringIdx,BondType *bond,
151 bool postponeAllowed=
true){
153 RWMol *mp=molList[molIdx];
157 if(!mp->hasAtomBookmark(ringIdx)){
160 bond->setOwningMol(mp);
161 bond->setEndAtomIdx(mp->getActiveAtom()->getIdx());
162 mp->setBondBookmark(bond,ringIdx);
165 std::stringstream err;
166 err <<
"SLN Parser error: Ring closure " << ringIdx <<
" does not have a corresponding opener.";
170 Atom *opener=mp->getAtomWithBookmark(ringIdx);
173 Atom *closer=mp->getActiveAtom();
175 bond->setBeginAtom(opener);
176 bond->setEndAtom(closer);
177 addBondToMol(mp,bond);
180 void closeRingBond(std::vector<RWMol *> &molList,
unsigned int molIdx,
unsigned int ringIdx){
186 template <
typename BondType>
188 unsigned int branchIdx,BondType *&bond){
190 RWMol *mp=molList[molIdx];
193 RWMol *branch=molList[branchIdx];
197 unsigned int activeAtomIdx=mp->getActiveAtom()->getIdx();
198 unsigned int nOrigAtoms=mp->getNumAtoms();
203 mp->insertMol(*branch);
206 for(ROMol::ATOM_BOOKMARK_MAP::const_iterator bmIt=branch->getAtomBookmarks()->begin();
207 bmIt != branch->getAtomBookmarks()->end();++bmIt){
208 if(bmIt->first<0)
continue;
209 if(mp->hasAtomBookmark(bmIt->first)){
210 std::stringstream err;
211 err <<
"SLN Parser error: Atom ID " << bmIt->first <<
" used a second time.";
213 }
else if(mp->hasBondBookmark(bmIt->first)){
214 std::stringstream err;
215 err <<
"SLN Parser error: Atom ID " << bmIt->first <<
" appears *after* its ring closure.";
219 CHECK_INVARIANT(bmIt->second.size()==1,
"bad atom bookmark list on branch");
220 Atom *tgtAtom=mp->getAtomWithIdx((*bmIt->second.begin())->getIdx()+nOrigAtoms);
221 mp->setAtomBookmark(tgtAtom,bmIt->first);
226 for(ROMol::BOND_BOOKMARK_MAP::const_iterator bmIt=branch->getBondBookmarks()->begin();
227 bmIt != branch->getBondBookmarks()->end();++bmIt){
228 CHECK_INVARIANT(bmIt->second.size()>=1,
"bad bond bookmark list on branch");
229 for(ROMol::BOND_PTR_LIST::const_iterator bondIt=bmIt->second.begin();
230 bondIt!=bmIt->second.end();++bondIt){
231 Bond *tgtBond=*bondIt;
232 if(bmIt->first>0 && mp->hasAtomBookmark(bmIt->first)){
233 Atom *tmpAtom=mp->getActiveAtom();
234 mp->setActiveAtom(mp->getAtomWithIdx(tgtBond->
getEndAtomIdx()+nOrigAtoms));
236 mp->setActiveAtom(tmpAtom);
241 mp->setBondBookmark(tgtBond,bmIt->first);
248 bond->setOwningMol(mp);
249 bond->setBeginAtomIdx(activeAtomIdx);
250 bond->setEndAtomIdx(nOrigAtoms);
251 addBondToMol(mp,bond);
258 unsigned int sz = molList.size();
259 if ( sz==branchIdx+1) {
260 molList.resize( sz-1 );
265 int addBranchToMol(std::vector<RWMol *> &molList,
unsigned int molIdx,
unsigned int branchIdx){
272 int addFragToMol(std::vector<RWMol *> &molList,
unsigned int molIdx,
unsigned int fragIdx){
278 template <
typename T>
280 std::string res=boost::lexical_cast<std::string>(val);
287 RWMol::BOND_BOOKMARK_MAP *marks = mol->getBondBookmarks();
288 RWMol::BOND_BOOKMARK_MAP::iterator markI=marks->begin();
289 while(markI != marks->end()){
290 RWMol::BOND_PTR_LIST &bonds=markI->second;
291 for(RWMol::BOND_PTR_LIST::iterator bondIt=bonds.begin();
292 bondIt!=bonds.end();++bondIt){
std::string convertToString(T val)
convenience function to convert the argument to a string
int addBranchToMol(std::vector< RWMol * > &molList, unsigned int molIdx, unsigned int branchIdx, BondType *&bond)
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 between them ...
RWMol is a molecule class that is intended to be edited.
unsigned int addAtom(bool updateLabel=true)
adds an empty Atom to our collection
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
#define CHECK_INVARIANT(expr, mess)
pulls in the RDKit Query functionality
unsigned int getIdx() const
returns our index within the ROMol
pulls in the core RDKit functionality
int startMol(std::vector< RWMol * > &molList, AtomType *firstAtom, bool doingQuery)
initialize a molecule
void addAtomToMol(std::vector< RWMol * > &molList, unsigned int idx, AtomType *atom, BondType *bond, bool doingQuery)
adds an atom to a molecule
Includes a bunch of functionality for handling Atom and Bond queries.
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)
The class for representing atoms.
unsigned int addBond(unsigned int beginAtomIdx, unsigned int endAtomIdx, Bond::BondType order=Bond::UNSPECIFIED)
adds a Bond between the indicated Atoms