Skip to content

Commit

Permalink
Issue 6411 ROMol hasquery (rdkit#6739)
Browse files Browse the repository at this point in the history
* added ROMol::hasQuery

* python bindings for Mol.HasQuery

* at least I checked that my Python tests were running...

* hasQuery use C++11 range iterators
  • Loading branch information
richardjgowers authored Sep 25, 2023
1 parent 2761441 commit 4db63b8
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
14 changes: 14 additions & 0 deletions Code/GraphMol/ROMol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,20 @@ ROMol::ConstHeteroatomIterator ROMol::endHeteros() const {
return ConstHeteroatomIterator(this, getNumAtoms());
}

bool ROMol::hasQuery() const {
for (auto atom : atoms()) {
if (atom->hasQuery()) {
return true;
}
}
for (auto bond : bonds()) {
if (bond->hasQuery()) {
return true;
}
}
return false;
}

ROMol::QueryAtomIterator ROMol::beginQueryAtoms(QueryAtom const *what) {
return QueryAtomIterator(this, what);
}
Expand Down
3 changes: 3 additions & 0 deletions Code/GraphMol/ROMol.h
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,9 @@ class RDKIT_GRAPHMOL_EXPORT ROMol : public RDProps {
//! \overload
ConstHeteroatomIterator endHeteros() const;

//! if the Mol has any Query atoms or bonds
bool hasQuery() const;

//! get an AtomIterator pointing at our first Atom that matches \c query
QueryAtomIterator beginQueryAtoms(QueryAtom const *query);
//! \overload
Expand Down
3 changes: 3 additions & 0 deletions Code/GraphMol/Wrap/Mol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,9 @@ struct mol_wrapper {
"instead.\n\n"
" NOTE: bond indices start at 0\n")

.def("HasQuery", &ROMol::hasQuery,
"Returns if any atom or bond in molecule has a query")

// substructures
.def("HasSubstructMatch",
(bool (*)(const ROMol &m, const ROMol &query, bool, bool,
Expand Down
12 changes: 11 additions & 1 deletion Code/GraphMol/Wrap/rough_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7357,7 +7357,17 @@ def testHasQueryHs(self):
(True, False))]:
pat = Chem.MolFromSmarts(sma)
self.assertEqual(Chem.HasQueryHs(pat), hasQHs)


def testMolHasQuery(self):
m1 = Chem.MolFromSmiles("CCO")
self.assertFalse(m1.HasQuery())

m2 = Chem.MolFromSmarts("[#6][#6][#8]")
self.assertTrue(m2.HasQuery())

m3 = Chem.MolFromSmiles("CC~O")
self.assertTrue(m3.HasQuery())

def testMrvHandling(self):
fn1 = os.path.join(RDConfig.RDBaseDir,'Code','GraphMol','MarvinParse','test_data','aspirin.mrv')
mol = Chem.MolFromMrvFile(fn1)
Expand Down
19 changes: 19 additions & 0 deletions Code/GraphMol/catch_graphmol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3418,4 +3418,23 @@ TEST_CASE(
REQUIRE(sgs.size() == 1);
CHECK(&sgs[0].getOwningMol() == &mol);
}
}

TEST_CASE(
"ROMol hasQuery") {
SECTION("check false Mol.hasQuery") {
std::unique_ptr<ROMol> mol{SmilesToMol("CCO")};

REQUIRE(!mol->hasQuery());
}
SECTION("check true Mol.hasQuery because Atom") {
std::unique_ptr<ROMol> mol{SmartsToMol("[#6][#6][#8]")};

REQUIRE(mol->hasQuery());
}
SECTION("check true Mol.hashQuery because Bond") {
std::unique_ptr<ROMol> mol{SmilesToMol("CC~O")};

REQUIRE(mol->hasQuery());
}
}

0 comments on commit 4db63b8

Please sign in to comment.