Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minilib: molzip exposure #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ option(RDK_BUILD_FUZZ_TARGETS "build the fuzz targets" OFF)
option(RDK_BUILD_MINIMAL_LIB_RXN "build support for reactions into MinimalLib" ON )
option(RDK_BUILD_MINIMAL_LIB_SUBSTRUCTLIBRARY "build support for SubstructLibrary into MinimalLib" ON )
option(RDK_BUILD_MINIMAL_LIB_MCS "build support for MCS into MinimalLib" OFF )
option(RDK_BUILD_MINIMAL_LIB_ZIP "build support for fragment linkage into MinimalLib" OFF )

set(RDK_BOOST_VERSION "1.70.0")

Expand Down
52 changes: 52 additions & 0 deletions Code/GraphMol/ChemTransforms/MolFragmenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <boost/functional/hash.hpp>
#include <boost/range/adaptor/reversed.hpp>
#include <boost/tokenizer.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <RDGeneral/BoostEndInclude.h>

#include <algorithm>
Expand Down Expand Up @@ -1172,5 +1174,55 @@ std::unique_ptr<ROMol> molzip(const std::map<std::string, ROMOL_SPTR> &row,
return molzip(mols, params);
}

void parseMolzipParametersJSON(const char* json, MolzipParams* params) {
if (!params || !json || !strlen(json))
return;

std::istringstream ss;
ss.str(json);
boost::property_tree::ptree pt;
boost::property_tree::read_json(ss, pt);

auto& p = *params;
std::string label = pt.get<std::string>("Label", "");
if (label == "AtomMapNumber")
p.label = MolzipLabel::AtomMapNumber;
else if (label == "Isotope")
p.label = MolzipLabel::Isotope;
else if (label == "FragmentOnBonds")
p.label = MolzipLabel::FragmentOnBonds;
else if (label == "AtomType")
p.label = MolzipLabel::AtomType;
else if (label == "AtomProperty")
p.label = MolzipLabel::AtomProperty;
else if (label != "")
throw ValueErrorException("Unknown label");

std::string atomSymbolsRow = pt.get<std::string>("AtomSymbols", "");

if (atomSymbolsRow != "") {
std::string::iterator it = atomSymbolsRow.begin();
std::vector<std::string> atomSymbols;
std::string atomSymbol = "";

for (; it != atomSymbolsRow.end(); ++it) {
if (*it == ',') {
atomSymbols.push_back(atomSymbol);
atomSymbol = "";
}
else
atomSymbol += *it;
if (next(it) == atomSymbolsRow.end())
atomSymbols.push_back(atomSymbol);
}

p.atomSymbols = atomSymbols;
}

p.atomProperty = pt.get<std::string>("AtomProperty", p.atomProperty);
p.enforceValenceRules = pt.get<bool>("EnforceValenceRules", p.enforceValenceRules);
p.generateCoordinates = pt.get<bool>("GenerateCoordinates", p.generateCoordinates);
}

} // end of namespace RDKit

3 changes: 3 additions & 0 deletions Code/GraphMol/ChemTransforms/MolFragmenter.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,8 @@ RDKIT_CHEMTRANSFORMS_EXPORT std::unique_ptr<ROMol> molzip(
*/
RDKIT_CHEMTRANSFORMS_EXPORT std::unique_ptr<ROMol> molzip(const std::map<std::string, ROMOL_SPTR> &row,
const MolzipParams &params=MolzipParams());

RDKIT_CHEMTRANSFORMS_EXPORT void parseMolzipParametersJSON(const char* json,
MolzipParams* params);
} // namespace RDKit
#endif
6 changes: 5 additions & 1 deletion Code/MinimalLib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ if(RDK_BUILD_MINIMAL_LIB)
if(RDK_BUILD_MINIMAL_LIB_MMPA)
add_definitions(-DRDK_BUILD_MINIMAL_LIB_MMPA)
set(MINIMAL_LIB_LIBRARIES "${MINIMAL_LIB_LIBRARIES};MMPA_static")
endif()
endif()
if(DRDK_BUILD_MINIMAL_LIB_ZIP)
add_definitions(-DRDK_BUILD_MINIMAL_LIB_ZIP)
set(MINIMAL_LIB_LIBRARIES "${MINIMAL_LIB_LIBRARIES};ChemTransforms_static")
endif()
if(RDK_BUILD_FREETYPE_SUPPORT)
if( ${CMAKE_SYSTEM_NAME} MATCHES "Emscripten")
set(USE_FLAGS "-s USE_FREETYPE=1")
Expand Down
10 changes: 10 additions & 0 deletions Code/MinimalLib/jswrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ std::string get_mcs_as_smarts_no_details(const JSMolList &mols) {
}
#endif

#ifdef RDK_BUILD_MINIMAL_LIB_ZIP
JSMol * get_molzip_no_details(const JSMol &a, const JSMol &b) {
return get_molzip(a, b, std::string());
}
#endif

emscripten::val binary_string_to_uint8array(const std::string &pkl) {
emscripten::val view(emscripten::typed_memory_view(
pkl.size(), reinterpret_cast<const unsigned char *>(pkl.c_str())));
Expand Down Expand Up @@ -672,4 +678,8 @@ EMSCRIPTEN_BINDINGS(RDKit_minimal) {
function("get_mcs_as_smarts", &get_mcs_as_smarts);
function("get_mcs_as_smarts", &get_mcs_as_smarts_no_details);
#endif
#ifdef RDK_BUILD_MINIMAL_LIB_ZIP
function("link", &get_molzip, allow_raw_pointers());
function("link", &get_molzip_no_details, allow_raw_pointers());
#endif
}
17 changes: 17 additions & 0 deletions Code/MinimalLib/minilib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
#ifdef RDK_BUILD_MINIMAL_LIB_MCS
#include <GraphMol/FMCS/FMCS.h>
#endif
#ifdef RDK_BUILD_MINIMAL_LIB_ZIP
#include <GraphMol/ChemTransforms/MolFragmenter.h>
#endif

#include <GraphMol/Descriptors/Property.h>
#include <GraphMol/Descriptors/MolDescriptors.h>
#include <GraphMol/MolInterchange/MolInterchange.h>
Expand Down Expand Up @@ -1007,3 +1011,16 @@ JSLog *set_log_capture(const std::string &log_name) {
void enable_logging() { RDKit::MinimalLib::LogHandle::enableLogging(); }

void disable_logging() { RDKit::MinimalLib::LogHandle::disableLogging(); }

#ifdef RDK_BUILD_MINIMAL_LIB_ZIP
JSMol * get_molzip(const JSMol &a, const JSMol &b, const std::string &details_json) {

MolzipParams p = MolzipParams();
if (!details_json.empty())
parseMolzipParametersJSON(details_json.c_str(), &p);

std::unique_ptr<RDKit::ROMol> out = molzip(*(a.d_mol.get()), *(b.d_mol.get()), p);

return new JSMol(new RDKit::RWMol(*(out).get()));
}
#endif
4 changes: 4 additions & 0 deletions Code/MinimalLib/minilib.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,7 @@ std::string get_mcs_as_json(const JSMolList &mols, const std::string &details_js
std::string get_mcs_as_smarts(const JSMolList &mols, const std::string &details_json);
JSMol *get_mcs_as_mol(const JSMolList &mols, const std::string &details_json);
#endif

#ifdef RDK_BUILD_MINIMAL_LIB_ZIP
JSMol * get_molzip(const JSMol &a, const JSMol &b, const std::string &details_json);
#endif
24 changes: 24 additions & 0 deletions Code/MinimalLib/tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,30 @@ function test_get_mmpa_frags() {
}
}

function test_link_frags() {
{
var mol1 = RDKitModule.get_mol("F/C=C/[*:1]");
var mol2 = RDKitModule.get_mol("[*:1]F");
var expectedLinkage = "F/C=C/F";
var mol = RDKitModule.link(mol1, mol2);
assert(mol.get_smiles === expectedLinkage);
mol1.delete();
mol2.delete();
mol.delete();
}
{
var mol1 = RDKitModule.get_mol("[C@H]([Xe])(F)([V])");
var mol2 = RDKitModule.get_mol("[Xe]N.[V]I");
var expectedLinkage = "N[C@@H](F)I";
var details = JSON.stringify({ Label: 'AtomType', AtomSymbols: 'Xe,V' });
var mol = RDKitModule.link(mol1, mol2, details);
assert(mol.get_smiles === expectedLinkage);
mol1.delete();
mol2.delete();
mol.delete();
}
}

function test_hs_in_place() {
{
var mol = RDKitModule.get_mol("CC");
Expand Down