Skip to content

Commit

Permalink
Update MinimalLib for Function Exposure: runReactants (#7210)
Browse files Browse the repository at this point in the history
  • Loading branch information
syedzayyan committed May 9, 2024
1 parent 6bc3aa4 commit c2369f9
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Code/MinimalLib/jswrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,13 @@ emscripten::val get_mmpa_frags_helper(const JSMol &self, unsigned int minCuts,
return obj;
}
#endif

} // namespace

using namespace emscripten;
EMSCRIPTEN_BINDINGS(RDKit_minimal) {
register_vector<std::string>("StringList");
register_vector<JSMolList *>("JSMolListList");

class_<JSMol>("Mol")
.function("is_valid", &JSMol::is_valid)
Expand Down Expand Up @@ -582,6 +584,9 @@ EMSCRIPTEN_BINDINGS(RDKit_minimal) {
#ifdef RDK_BUILD_MINIMAL_LIB_RXN
class_<JSReaction>("Reaction")
#ifdef __EMSCRIPTEN__
.function("run_reactants", select_overload<std::vector<JSMolList *>(
const JSMolList &, unsigned int) const>(
&JSReaction::run_reactants))
.function("draw_to_canvas_with_offset", &draw_rxn_to_canvas_with_offset)
.function("draw_to_canvas", &draw_rxn_to_canvas)
.function("draw_to_canvas_with_highlights",
Expand Down
22 changes: 21 additions & 1 deletion Code/MinimalLib/minilib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -669,8 +669,28 @@ std::string JSReaction::get_svg_with_highlights(
int h = d_defaultHeight;
return MinimalLib::rxn_to_svg(*d_rxn, w, h, details);
}

bool JSReaction::is_valid() const { return true; }

std::vector<JSMolList *> JSReaction::run_reactants(
const JSMolList &reactants, unsigned int maxProducts) const {
d_rxn->initReactantMatchers();
RDKit::MOL_SPTR_VECT reactant_vec;

for (const auto &reactant : reactants.mols()) {
if (!reactant) {
throw ValueErrorException("Reactant must not be null");
}
reactant_vec.push_back(reactant);
}

std::vector<RDKit::MOL_SPTR_VECT> prods;
prods = d_rxn->runReactants(reactant_vec, maxProducts);
std::vector<JSMolList *> newResults;
for (auto &mol_array : prods) {
newResults.push_back(new JSMolList(mol_array));
}
return newResults;
}
#endif

JSMol *JSMolList::next() {
Expand Down
3 changes: 3 additions & 0 deletions Code/MinimalLib/minilib.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ class JSReaction {
"instead")]] bool
is_valid() const;

std::vector<JSMolList *> run_reactants(const JSMolList &reactants,
unsigned int maxProducts) const;
static constexpr int maxProducts = 1000;
std::string get_svg(int width, int height) const;
std::string get_svg() const {
return get_svg(d_defaultWidth, d_defaultHeight);
Expand Down
46 changes: 45 additions & 1 deletion Code/MinimalLib/tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,49 @@ function test_flexicanvas() {
assert(svg.search("height='19px'")>0);
}

function test_run_reaction() {
let rxn1;
let molList;
try {
rxn1 = RDKitModule.get_rxn('[#6:1][O:2]>>[#6:1]=[O:2]');
molList = molListFromSmiArray(['CC(C)O',]);
let products;
try {
products = rxn1.run_reactants(molList, 10000);
for (let i = 0; i < products.size(); i++) {
let element;
try {
element = products.get(i);
let mol;
try {
mol = element.next();
assert(mol && mol.get_smiles() === "CC(C)=O");
} finally {
if (mol) {
mol.delete();
}
}
} finally {
if (element) {
element.delete();
}
}
}
} finally {
if (products) {
products.delete();
}
}
} finally {
if (rxn1) {
rxn1.delete();
}
if (molList) {
molList.delete();
}
}
}

function test_rxn_drawing() {
{
var rxn = RDKitModule.get_rxn("[CH3:1][OH:2]>>[CH2:1]=[OH0:2]");
Expand Down Expand Up @@ -2947,6 +2990,7 @@ initRDKitModule().then(function(instance) {
test_flexicanvas();
if (RDKitModule.get_rxn) {
test_rxn_drawing();
test_run_reaction();
}
test_legacy_stereochem();
test_allow_non_tetrahedral_chirality();
Expand Down Expand Up @@ -2980,4 +3024,4 @@ initRDKitModule().then(function(instance) {
waitAllTestsFinished().then(() =>
console.log("Tests finished successfully")
);
});
});

0 comments on commit c2369f9

Please sign in to comment.