// $Id$ // // Copyright (C) 2001-2010 Greg Landrum and Rational Discovery LLC // // @@ All Rights Reserved @@ // #include #include #include #include #include "SubstructMatch.h" #include "SubstructUtils.h" #include #include "ullmann.hpp" #include "vf2.hpp" namespace RDKit{ void MatchSubqueries(const ROMol &mol,QueryAtom::QUERYATOM_QUERY *q,bool useChirality,bool registerQuery); namespace detail { typedef std::list > ssPairType; class AtomLabelFunctor{ public: AtomLabelFunctor(const ROMol &query,const ROMol &mol, bool useChirality) : d_query(query), d_mol(mol), df_useChirality(useChirality) {}; bool operator()(unsigned int i,unsigned int j) const{ bool res=false; if(!df_useChirality){ res=atomCompat(d_query[i],d_mol[j]); } else { res=chiralAtomCompat(d_query[i],d_mol[j]); } //std::cerr<<" alf: "<getQuery()){ MatchSubqueries(mol,(*atIt)->getQuery(),useChirality,registerQuery); } } } //std::cerr<<"main matching"<first]=std::pair(iter->first,iter->second); } } return res; } // ---------------------------------------------- // // find all matches // // NOTE: this blows out the contents of matches // unsigned int SubstructMatch(const ROMol &mol,const ROMol &query, std::vector< MatchVectType > &matches, bool uniquify,bool recursionPossible, bool useChirality,bool registerQuery) { if(recursionPossible){ ROMol::ConstAtomIterator atIt; for(atIt=query.beginAtoms();atIt!=query.endAtoms();atIt++){ if((*atIt)->getQuery()){ //std::cerr<<"recurse from atom "<<(*atIt)->getIdx()<getQuery(),useChirality,registerQuery); } } } matches.clear(); matches.resize(0); detail::AtomLabelFunctor atomLabeler(query,mol,useChirality); detail::BondLabelFunctor bondLabeler(query,mol); std::list pms; #if 0 bool found=boost::ullmann_all(query.getTopology(),mol.getTopology(), atomLabeler,bondLabeler,pms); #else bool found=boost::vf2_all(query.getTopology(),mol.getTopology(), atomLabeler,bondLabeler,pms); #endif unsigned int res=0; if(found){ unsigned int nQueryAtoms=query.getNumAtoms(); matches.reserve(pms.size()); for(std::list::const_iterator iter1=pms.begin(); iter1!=pms.end();++iter1){ MatchVectType matchVect; matchVect.resize(nQueryAtoms); for(detail::ssPairType::const_iterator iter2=iter1->begin(); iter2!=iter1->end();++iter2){ matchVect[iter2->first]=std::pair(iter2->first,iter2->second); } matches.push_back(matchVect); } if(uniquify){ removeDuplicates(matches,mol.getNumAtoms()); } res = matches.size(); } return res; } // ---------------------------------------------- // // Intended for internal use // unsigned int RecursiveMatcher(const ROMol &mol,const ROMol &query, std::vector< int > &matches,bool useChirality, bool registerQuery) { ROMol::ConstAtomIterator atIt; for(atIt=query.beginAtoms();atIt!=query.endAtoms();atIt++){ if((*atIt)->getQuery()){ MatchSubqueries(mol,(*atIt)->getQuery(),useChirality,registerQuery); } } detail::AtomLabelFunctor atomLabeler(query,mol,useChirality); detail::BondLabelFunctor bondLabeler(query,mol); matches.clear(); matches.resize(0); std::list pms; #if 0 bool found=boost::ullmann_all(query.getTopology(),mol.getTopology(), atomLabeler,bondLabeler,pms); #else bool found=boost::vf2_all(query.getTopology(),mol.getTopology(), atomLabeler,bondLabeler,pms); #endif unsigned int res=0; if(found){ matches.reserve(pms.size()); for(std::list::const_iterator iter1=pms.begin(); iter1!=pms.end();++iter1){ if(!query.hasProp("_queryRootAtom")){ matches.push_back(iter1->begin()->second); } else { int rootIdx; query.getProp("_queryRootAtom",rootIdx); bool found=false; for(detail::ssPairType::const_iterator pairIter=iter1->begin(); pairIter!=iter1->end();++pairIter){ if(pairIter->first==rootIdx){ matches.push_back(pairIter->second); found=true; break; } } if(!found){ BOOST_LOG(rdErrorLog)<<"no match found for queryRootAtom"<getDescription()=="RecursiveStructure"){ RecursiveStructureQuery *rsq=(RecursiveStructureQuery *)query; ROMol const *queryMol = rsq->getQueryMol(); // in case we are reusing this query, clear its contents now. rsq->clear(); if(queryMol){ std::vector< int > matchStarts; unsigned int res = RecursiveMatcher(mol,*queryMol,matchStarts,useChirality,registerQuery); if(res){ for(std::vector::iterator i=matchStarts.begin(); i!=matchStarts.end(); i++){ rsq->insert(*i); } } } } else { //std::cout << "\tmsq1: "; } // now recurse over our children (these things can be nested) Queries::Query::CHILD_VECT_CI childIt; //std::cout << query << " " << query->endChildren()-query->beginChildren() << std::endl; for(childIt=query->beginChildren();childIt!=query->endChildren();childIt++){ MatchSubqueries(mol,childIt->get(),useChirality,registerQuery); } //std::cout << "<<- back " << (int)query << std::endl; } }