void testHasPropMatch() { QueryAtom qA; qA.setQuery(makeHasPropQuery<Atom>("foo")); Atom a1(6); TEST_ASSERT(!qA.Match(&a1)); a1.setProp<int>("foo", 1); TEST_ASSERT(qA.Match(&a1)); }
void testRecursiveSerialNumbers(){ std::cout << " ----------------- Testing serial numbers on recursive queries" << std::endl; MatchVectType matchV; std::vector< MatchVectType > matches; int n; RWMol *m,*q1,*q2; Atom *a6 = new Atom(6); Atom *a8 = new Atom(8); m = new RWMol(); m->addAtom(a6); m->addAtom(a6); m->addAtom(a8); m->addAtom(a6); m->addAtom(a6); m->addBond(1,0,Bond::SINGLE); m->addBond(1,2,Bond::SINGLE); m->addBond(1,3,Bond::SINGLE); m->addBond(2,4,Bond::SINGLE); { // this will be the recursive query q1 = new RWMol(); q1->addAtom(new QueryAtom(6),true); q1->addAtom(new QueryAtom(8),true); q1->addBond(0,1,Bond::UNSPECIFIED); // here's the main query q2 = new RWMol(); QueryAtom *qA = new QueryAtom(6); RecursiveStructureQuery *rsq = new RecursiveStructureQuery(new RWMol(*q1),1); qA->expandQuery(rsq,Queries::COMPOSITE_AND); //std::cout << "post expand: " << qA->getQuery() << std::endl; q2->addAtom(qA,true,true); //std::cout << "mol: " << q2->getAtomWithIdx(0)->getQuery() << std::endl; q2->addAtom(new QueryAtom(8),true,true); q2->addBond(0,1,Bond::UNSPECIFIED); qA = new QueryAtom(6); rsq = new RecursiveStructureQuery(new RWMol(*q1),1); qA->expandQuery(rsq,Queries::COMPOSITE_AND); q2->addAtom(qA,true,true); q2->addBond(1,2,Bond::UNSPECIFIED); bool found = SubstructMatch(*m,*q2,matchV); CHECK_INVARIANT(found,""); CHECK_INVARIANT(matchV.size()==3,""); n = SubstructMatch(*m,*q2,matches,true); TEST_ASSERT(n==1); TEST_ASSERT(matches.size()==1); TEST_ASSERT(matches[0].size()==3); delete q1; delete q2; } delete m; std::cout << "Done\n" << std::endl; }
void testHasPropWithValueMatch() { { QueryAtom qA; qA.setQuery(makePropQuery<Atom, int>("foo", 2)); Atom a1(6); TEST_ASSERT(!qA.Match(&a1)); a1.setProp<int>("foo", 1); TEST_ASSERT(!qA.Match(&a1)); a1.setProp<int>("foo", 2); TEST_ASSERT(qA.Match(&a1)); a1.clearProp("foo"); a1.setProp<double>("foo", 2); TEST_ASSERT(!qA.Match(&a1)); } { QueryAtom qA; qA.setQuery(makePropQuery<Atom, std::string>("foo", "bar")); Atom a1(6); TEST_ASSERT(!qA.Match(&a1)); a1.setProp<std::string>("foo", "bar"); TEST_ASSERT(qA.Match(&a1)); } { QueryBond qA; qA.setQuery(makePropQuery<Bond, int>("foo", 2)); Bond a1; TEST_ASSERT(!qA.Match(&a1)); a1.setProp<int>("foo", 1); TEST_ASSERT(!qA.Match(&a1)); a1.setProp<int>("foo", 2); TEST_ASSERT(qA.Match(&a1)); a1.clearProp("foo"); a1.setProp<double>("foo", 2); TEST_ASSERT(!qA.Match(&a1)); } { QueryBond qA; qA.setQuery(makePropQuery<Bond, std::string>("foo", "bar")); Bond a1; TEST_ASSERT(!qA.Match(&a1)); a1.setProp<std::string>("foo", "bar"); TEST_ASSERT(qA.Match(&a1)); } }
void test5QueryRoot(){ std::cout << " ----------------- Test 5 QueryRoot" << std::endl; MatchVectType matchV; std::vector< MatchVectType > matches; int n; RWMol *m,*q1,*q2; Atom *a6 = new Atom(6); Atom *a8 = new Atom(8); // CC(OC)C m = new RWMol(); m->addAtom(a6); m->addAtom(a6); m->addAtom(a8); m->addAtom(a6); m->addAtom(a6); m->addBond(0,1,Bond::SINGLE); m->addBond(1,2,Bond::SINGLE); m->addBond(1,4,Bond::SINGLE); m->addBond(2,3,Bond::SINGLE); // this will be the recursive query q1 = new RWMol(); q1->addAtom(new QueryAtom(8),true); q1->addAtom(new QueryAtom(6),true); q1->addBond(0,1,Bond::UNSPECIFIED); q1->setProp("_queryRootAtom",1); // here's the main query q2 = new RWMol(); QueryAtom *qA = new QueryAtom(); RecursiveStructureQuery *rsq = new RecursiveStructureQuery(q1); qA->setQuery(rsq); q2->addAtom(qA,true,true); q2->addAtom(new QueryAtom(6),true,true); q2->addBond(0,1,Bond::UNSPECIFIED); bool found = SubstructMatch(*m,*q2,matchV); CHECK_INVARIANT(found,""); CHECK_INVARIANT(matchV.size()==2,""); n = SubstructMatch(*m,*q2,matches,true); CHECK_INVARIANT(n==2,""); CHECK_INVARIANT(matches[0].size()==2,""); std::cout << "Done\n" << std::endl; }
void test4() { string smi = "C1COCCNCOCNSCC1"; unsigned int heteros1[] = {2, 7}; Mol *m = SmilesToMol(smi); QueryAtom *q = new QueryAtom(); q->setQuery(makeAtomNumQuery(8)); { unsigned int nSeen = 0; for (Mol::QueryAtomIterator queryIt = m->beginQueryAtoms(q); queryIt != m->endQueryAtoms(); queryIt++) { CHECK_INVARIANT((*queryIt)->getIdx() == heteros1[nSeen], "bad query"); nSeen++; } } { Mol::QueryAtomIterator queryIt = m->beginQueryAtoms(q); queryIt++; queryIt--; CHECK_INVARIANT((*queryIt)->getIdx() == heteros1[0], "bad query"); CHECK_INVARIANT((*++queryIt)->getIdx() == heteros1[1], "bad query"); CHECK_INVARIANT((*queryIt)->getIdx() == heteros1[1], "bad query"); } { Mol::QueryAtomIterator queryIt = m->beginQueryAtoms(q); queryIt++; queryIt--; Mol::QueryAtomIterator queryIt2 = queryIt; CHECK_INVARIANT((*queryIt2)->getIdx() == heteros1[0], "bad query"); CHECK_INVARIANT((*++queryIt2)->getIdx() == heteros1[1], "bad query"); CHECK_INVARIANT((*queryIt2)->getIdx() == heteros1[1], "bad query"); } smi = "CC(C)CC(C)CC(C)CC(C)C"; unsigned int heteros2[] = {1, 4, 7, 10}; m = SmilesToMol(smi); // m->debugMol(cout); q->setQuery(makeAtomImplicitValenceQuery(1)); { unsigned int nSeen = 0; for (Mol::QueryAtomIterator queryIt = m->beginQueryAtoms(q); queryIt != m->endQueryAtoms(); ++queryIt) { CHECK_INVARIANT((*queryIt)->getIdx() == heteros2[nSeen], "bad query"); nSeen++; } } BOOST_LOG(rdInfoLog) << "test4 done" << endl; };
void test8() { { string smi = "CC1CC2CC1C2"; Mol *m = SmilesToMol(smi); QueryAtom *q = new QueryAtom(); q->setQuery(makeAtomExplicitDegreeQuery(3)); q->expandQuery(makeAtomRingBondCountQuery(2)); unsigned int nSeen = 0; for (Mol::QueryAtomIterator queryIt = m->beginQueryAtoms(q); queryIt != m->endQueryAtoms(); ++queryIt) { TEST_ASSERT((*queryIt)->getIdx() == 1); nSeen++; } TEST_ASSERT(nSeen == 1); delete m; delete q; } BOOST_LOG(rdInfoLog) << "test8 done" << endl; };
void testHasPropWithDoubleValueMatch() { { QueryAtom qA; qA.setQuery(makePropQuery<Atom, double>("foo", 2)); Atom a1(6); TEST_ASSERT(!qA.Match(&a1)); a1.setProp<double>("foo", 1); TEST_ASSERT(!qA.Match(&a1)); a1.setProp<double>("foo", 2); TEST_ASSERT(qA.Match(&a1)); } { QueryBond qA; qA.setQuery(makePropQuery<Bond, double>("foo", 2)); Bond a1; TEST_ASSERT(!qA.Match(&a1)); a1.setProp<double>("foo", 1); TEST_ASSERT(!qA.Match(&a1)); a1.setProp<double>("foo", 2); TEST_ASSERT(qA.Match(&a1)); } }
void testExtraAtomQueries() { BOOST_LOG(rdErrorLog) << "---------------------- Test extra atom queries" << std::endl; { // radicals QueryAtom qA; qA.setQuery(makeAtomNumRadicalElectronsQuery(1)); Atom a1(6); TEST_ASSERT(!qA.Match(&a1)); a1.setNumRadicalElectrons(1); TEST_ASSERT(qA.Match(&a1)); a1.setNumRadicalElectrons(2); TEST_ASSERT(!qA.Match(&a1)); qA.getQuery()->setNegation(true); TEST_ASSERT(qA.Match(&a1)); a1.setNumRadicalElectrons(0); TEST_ASSERT(qA.Match(&a1)); a1.setNumRadicalElectrons(1); TEST_ASSERT(!qA.Match(&a1)); } { // chiral tags QueryAtom qA; qA.setQuery(makeAtomHasChiralTagQuery()); Atom a1(6); TEST_ASSERT(!qA.Match(&a1)); a1.setChiralTag(Atom::CHI_TETRAHEDRAL_CW); TEST_ASSERT(qA.Match(&a1)); a1.setChiralTag(Atom::CHI_TETRAHEDRAL_CCW); TEST_ASSERT(qA.Match(&a1)); a1.setChiralTag(Atom::CHI_OTHER); TEST_ASSERT(qA.Match(&a1)); qA.getQuery()->setNegation(true); a1.setChiralTag(Atom::CHI_UNSPECIFIED); TEST_ASSERT(qA.Match(&a1)); a1.setChiralTag(Atom::CHI_TETRAHEDRAL_CW); TEST_ASSERT(!qA.Match(&a1)); a1.setChiralTag(Atom::CHI_TETRAHEDRAL_CCW); TEST_ASSERT(!qA.Match(&a1)); a1.setChiralTag(Atom::CHI_OTHER); TEST_ASSERT(!qA.Match(&a1)); } { // missing chiral tags QueryAtom qA; qA.setQuery(makeAtomMissingChiralTagQuery()); Atom a1(6); TEST_ASSERT(!qA.Match(&a1)); a1.setChiralTag(Atom::CHI_TETRAHEDRAL_CW); TEST_ASSERT(!qA.Match(&a1)); a1.setChiralTag(Atom::CHI_TETRAHEDRAL_CCW); TEST_ASSERT(!qA.Match(&a1)); a1.setChiralTag(Atom::CHI_OTHER); TEST_ASSERT(!qA.Match(&a1)); a1.setChiralTag(Atom::CHI_UNSPECIFIED); a1.setProp(common_properties::_ChiralityPossible, 1); TEST_ASSERT(qA.Match(&a1)); a1.setChiralTag(Atom::CHI_TETRAHEDRAL_CW); TEST_ASSERT(!qA.Match(&a1)); a1.setChiralTag(Atom::CHI_TETRAHEDRAL_CCW); TEST_ASSERT(!qA.Match(&a1)); a1.setChiralTag(Atom::CHI_OTHER); TEST_ASSERT(!qA.Match(&a1)); qA.getQuery()->setNegation(true); a1.clearProp(common_properties::_ChiralityPossible); a1.setChiralTag(Atom::CHI_UNSPECIFIED); TEST_ASSERT(qA.Match(&a1)); a1.setChiralTag(Atom::CHI_TETRAHEDRAL_CW); TEST_ASSERT(qA.Match(&a1)); a1.setChiralTag(Atom::CHI_TETRAHEDRAL_CCW); TEST_ASSERT(qA.Match(&a1)); a1.setChiralTag(Atom::CHI_OTHER); TEST_ASSERT(qA.Match(&a1)); a1.setChiralTag(Atom::CHI_UNSPECIFIED); a1.setProp(common_properties::_ChiralityPossible, 1); TEST_ASSERT(!qA.Match(&a1)); } BOOST_LOG(rdErrorLog) << "Done!" << std::endl; }
void testQualifiedQueries() { BOOST_LOG(rdErrorLog) << "---------------------- Test queries using qualifiers instead of ==" << std::endl; RWMol *m = SmilesToMol("CNO"); { QueryAtom qA; qA.setQuery(makeAtomNumQuery<ATOM_GREATER_QUERY>(7, "test")); TEST_ASSERT(qA.Match(m->getAtomWithIdx(0))); TEST_ASSERT(!qA.Match(m->getAtomWithIdx(1))); TEST_ASSERT(!qA.Match(m->getAtomWithIdx(2))); } { QueryAtom qA; qA.setQuery(makeAtomNumQuery<ATOM_GREATEREQUAL_QUERY>(7, "test")); TEST_ASSERT(qA.Match(m->getAtomWithIdx(0))); TEST_ASSERT(qA.Match(m->getAtomWithIdx(1))); TEST_ASSERT(!qA.Match(m->getAtomWithIdx(2))); } { QueryAtom qA; qA.setQuery(makeAtomNumQuery<ATOM_LESS_QUERY>(7, "test")); TEST_ASSERT(!qA.Match(m->getAtomWithIdx(0))); TEST_ASSERT(!qA.Match(m->getAtomWithIdx(1))); TEST_ASSERT(qA.Match(m->getAtomWithIdx(2))); } { QueryAtom qA; qA.setQuery(makeAtomNumQuery<ATOM_LESSEQUAL_QUERY>(7, "test")); TEST_ASSERT(!qA.Match(m->getAtomWithIdx(0))); TEST_ASSERT(qA.Match(m->getAtomWithIdx(1))); TEST_ASSERT(qA.Match(m->getAtomWithIdx(2))); } delete m; BOOST_LOG(rdErrorLog) << "Done!" << std::endl; }
void test1() { BOOST_LOG(rdErrorLog) << "---------------------- Test1" << std::endl; Mol qM; Mol m; Atom *a = new Atom(6); // we copy in addAtom, so this is safe m.addAtom(a); m.addAtom(a); delete a; m.addBond(0, 1, Bond::SINGLE); a = new Atom(8); m.addAtom(a); delete a; m.addBond(1, 2, Bond::DOUBLE); MolOps::sanitizeMol(m); QueryAtom *qA = new QueryAtom(6); CHECK_INVARIANT(qA->Match(m.getAtomWithIdx(0)), ""); CHECK_INVARIANT(qA->Match(m.getAtomWithIdx(1)), ""); CHECK_INVARIANT(!qA->Match(m.getAtomWithIdx(2)), ""); qA->expandQuery(makeAtomImplicitValenceQuery(3)); CHECK_INVARIANT(qA->Match(m.getAtomWithIdx(0)), ""); CHECK_INVARIANT(!qA->Match(m.getAtomWithIdx(1)), ""); CHECK_INVARIANT(!qA->Match(m.getAtomWithIdx(2)), ""); qM.addAtom(qA); delete qA; qA = new QueryAtom(6); qA->expandQuery(makeAtomNumQuery(8), Queries::COMPOSITE_OR); qM.addAtom(qA); delete qA; qM.addAtom(new QueryAtom(8), true, true); // Atom::ATOM_SPTR qA(new QueryAtom(6)); QueryBond *qB; qB = new QueryBond(Bond::UNSPECIFIED); qB->setOwningMol(qM); qB->setBeginAtomIdx(0); qB->setEndAtomIdx(1); CHECK_INVARIANT(qB->Match(m.getBondWithIdx(0)), ""); CHECK_INVARIANT(qB->Match(m.getBondWithIdx(1)), ""); qM.addBond(qB, true); qB = new QueryBond(Bond::DOUBLE); qB->setOwningMol(qM); qB->setBeginAtomIdx(1); qB->setEndAtomIdx(2); qM.addBond(qB, true); CHECK_INVARIANT(qM.getAtomWithIdx(0)->Match(m.getAtomWithIdx(0)), ""); CHECK_INVARIANT(!qM.getAtomWithIdx(0)->Match(m.getAtomWithIdx(1)), ""); CHECK_INVARIANT(!qM.getAtomWithIdx(0)->Match(m.getAtomWithIdx(2)), ""); CHECK_INVARIANT(qM.getAtomWithIdx(1)->Match(m.getAtomWithIdx(0)), ""); CHECK_INVARIANT(qM.getAtomWithIdx(1)->Match(m.getAtomWithIdx(1)), ""); CHECK_INVARIANT(qM.getAtomWithIdx(1)->Match(m.getAtomWithIdx(2)), ""); CHECK_INVARIANT(!qM.getAtomWithIdx(2)->Match(m.getAtomWithIdx(0)), ""); CHECK_INVARIANT(!qM.getAtomWithIdx(2)->Match(m.getAtomWithIdx(1)), ""); CHECK_INVARIANT(qM.getAtomWithIdx(2)->Match(m.getAtomWithIdx(2)), ""); CHECK_INVARIANT(qM.getBondWithIdx(0)->Match(m.getBondWithIdx(0)), ""); CHECK_INVARIANT(qM.getBondWithIdx(0)->Match(m.getBondWithIdx(1)), ""); CHECK_INVARIANT(!qM.getBondWithIdx(1)->Match(m.getBondWithIdx(0)), ""); CHECK_INVARIANT(qM.getBondWithIdx(1)->Match(m.getBondWithIdx(1)), ""); BOOST_LOG(rdErrorLog) << "Done!" << std::endl; }
QueryAtom *HasPropQueryAtom(const std::string &propname, bool negate) { QueryAtom *res = new QueryAtom(); res->setQuery(makeHasPropQuery<Atom>(propname)); if (negate) res->getQuery()->setNegation(true); return res; }