1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
//
// Copyright (c) 2023, Greg Landrum and other RDKit contributors
//
// @@ All Rights Reserved @@
// This file is part of the RDKit.
// The contents are covered by the terms of the BSD license
// which is included in the file license.txt, found at the root
// of the RDKit source tree.
//
//
#include <DataStructs/base64.h>
#include <GraphMol/MolBundle.h>
#include <GraphMol/MolPickler.h>
#include <GraphMol/RDKitBase.h>
#include <GraphMol/SmilesParse/SmartsWrite.h>
#include <GraphMol/SmilesParse/SmilesParse.h>
#include <GraphMol/SmilesParse/SmilesWrite.h>
#include <GraphMol/TautomerQuery/TautomerQuery.h>
#include <RDGeneral/StreamOps.h>
#include "XQMol.h"
#include <RDGeneral/BoostStartInclude.h>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <RDGeneral/BoostEndInclude.h>
#include <cstdint>
#include <sstream>
#include <variant>
namespace bpt = boost::property_tree;
namespace RDKit {
namespace GeneralizedSubstruct {
namespace detail {
constexpr std::uint16_t recognition = 0xbe73;
constexpr std::uint16_t version = 1000;
} // namespace detail
std::string ExtendedQueryMol::toBinary() const {
std::stringstream ss(std::ios_base::binary | std::ios_base::out);
streamWrite(ss, detail::recognition);
streamWrite(ss, detail::version);
std::string pkl;
if (std::holds_alternative<RWMol_T>(xqmol)) {
streamWrite(ss, ExtendedQueryMolTypes::XQM_MOL);
MolPickler::pickleMol(*std::get<RWMol_T>(xqmol), pkl);
#ifdef RDK_USE_BOOST_SERIALIZATION
} else if (std::holds_alternative<MolBundle_T>(xqmol)) {
streamWrite(ss, ExtendedQueryMolTypes::XQM_MOLBUNDLE);
pkl = std::get<MolBundle_T>(xqmol)->serialize();
} else if (std::holds_alternative<TautomerQuery_T>(xqmol)) {
streamWrite(ss, ExtendedQueryMolTypes::XQM_TAUTOMERQUERY);
pkl = std::get<TautomerQuery_T>(xqmol)->serialize();
} else if (std::holds_alternative<TautomerBundle_T>(xqmol)) {
streamWrite(ss, ExtendedQueryMolTypes::XQM_TAUTOMERBUNDLE);
const auto &itm = std::get<TautomerBundle_T>(xqmol);
std::uint16_t nTauts = itm->size();
streamWrite(ss, nTauts);
for (const auto &taut : *itm) {
streamWrite(ss, taut->serialize());
}
#endif
} else {
UNDER_CONSTRUCTION("unrecognized type in ExtendedQueryMol");
}
if (!pkl.empty()) {
streamWrite(ss, pkl);
}
return ss.str();
}
namespace {
ExtendedQueryMol::TautomerBundle_T readTautomerQueries(std::stringstream &ss) {
ExtendedQueryMol::TautomerBundle_T res{
new std::vector<ExtendedQueryMol::TautomerQuery_T>()};
std::uint16_t nTauts = 0;
streamRead(ss, nTauts);
res->reserve(nTauts);
for (auto i = 0u; i < nTauts; ++i) {
std::string pkl;
streamRead(ss, pkl, 0);
res->emplace_back(std::make_unique<TautomerQuery>(pkl));
}
return res;
}
} // namespace
void ExtendedQueryMol::initFromBinary(const std::string &pickle) {
std::stringstream ss(std::ios_base::binary | std::ios_base::in |
std::ios_base::out);
ss.write(pickle.c_str(), pickle.length());
std::uint16_t recog;
streamRead(ss, recog);
if (recog != detail::recognition) {
throw ValueErrorException("bad pickle format: bad endian ID");
}
std::uint16_t vers;
streamRead(ss, vers);
if (vers > detail::version) {
throw ValueErrorException(
"attempt to depickle from more recent pickle version");
}
unsigned char readType;
streamRead(ss, readType);
std::string pkl;
switch (readType) {
case ExtendedQueryMolTypes::XQM_MOL:
streamRead(ss, pkl, 0);
xqmol = std::make_unique<RWMol>(pkl);
break;
#ifdef RDK_USE_BOOST_SERIALIZATION
case ExtendedQueryMolTypes::XQM_MOLBUNDLE:
streamRead(ss, pkl, 0);
xqmol = std::make_unique<MolBundle>(pkl);
break;
case ExtendedQueryMolTypes::XQM_TAUTOMERQUERY:
streamRead(ss, pkl, 0);
xqmol = std::make_unique<TautomerQuery>(pkl);
break;
case ExtendedQueryMolTypes::XQM_TAUTOMERBUNDLE:
xqmol = readTautomerQueries(ss);
break;
#endif
default:
UNDER_CONSTRUCTION("unrecognized type in ExtendedQueryMol");
}
}
namespace {
bool has_query_feature(const ROMol &mol) {
for (const auto atom : mol.atoms()) {
if (atom->hasQuery()) {
return true;
}
}
for (const auto bond : mol.bonds()) {
if (bond->hasQuery()) {
return true;
}
}
return false;
}
void add_mol_to_elem(bpt::ptree &elem, const ROMol &mol) {
std::string pkl;
MolPickler::pickleMol(mol, pkl);
std::unique_ptr<char[]> b64(Base64Encode(pkl.c_str(), pkl.length()));
elem.put("pkl", b64.get());
if (has_query_feature(mol)) {
elem.put("smarts", MolToCXSmarts(mol));
} else {
elem.put("smiles", MolToCXSmiles(mol));
}
}
void to_pt(bpt::ptree &pt, const ROMol &mol) {
bpt::ptree elem;
add_mol_to_elem(elem, mol);
pt.add_child("mol", elem);
}
void to_pt(bpt::ptree &pt, const MolBundle &bndl) {
{
bpt::ptree children;
for (const auto &mol : bndl.getMols()) {
bpt::ptree elem;
RWMol mcp(*mol);
mcp.clearComputedProps();
add_mol_to_elem(elem, mcp);
children.push_back({"", elem});
}
pt.add_child("mols", children);
}
}
void to_pt(bpt::ptree &pt, const TautomerQuery &tq) {
{
bpt::ptree children;
for (const auto &taut : tq.getTautomers()) {
bpt::ptree elem;
add_mol_to_elem(elem, *taut);
children.push_back({"", elem});
}
pt.add_child("tautomers", children);
}
{
bpt::ptree elem;
add_mol_to_elem(elem, tq.getTemplateMolecule());
pt.add_child("template", elem);
}
{
bpt::ptree children;
for (const auto idx : tq.getModifiedAtoms()) {
bpt::ptree elem;
elem.put_value(idx);
children.push_back({"", elem});
}
pt.add_child("modifiedAtoms", children);
}
{
bpt::ptree children;
for (const auto idx : tq.getModifiedBonds()) {
bpt::ptree elem;
elem.put_value(idx);
children.push_back({"", elem});
}
pt.add_child("modifiedBonds", children);
}
}
void to_pt(bpt::ptree &pt,
const ExtendedQueryMol::TautomerBundle_T &tautQueries) {
{
bpt::ptree children;
for (const auto &tq : *tautQueries) {
bpt::ptree elem;
to_pt(elem, *tq);
children.push_back({"", elem});
}
pt.add_child("tautomerQueries", children);
}
}
RWMol *pt_to_mol(bpt::ptree &pt) {
auto b64pkl = pt.get<std::string>("pkl", "");
if (!b64pkl.empty()) {
unsigned int len;
std::unique_ptr<char[]> cpkl(Base64Decode(b64pkl.c_str(), &len));
std::string pkl(cpkl.get(), len);
return new RWMol(pkl);
}
auto smi = pt.get<std::string>("smiles", "");
if (!smi.empty()) {
return SmilesToMol(smi);
} else {
auto sma = pt.get<std::string>("smarts");
return SmartsToMol(sma);
}
}
template <typename T>
T from_pt(bpt::ptree &) {}
template <>
ExtendedQueryMol::RWMol_T from_pt(bpt::ptree &pt) {
return ExtendedQueryMol::RWMol_T(pt_to_mol(pt.get_child("mol")));
}
template <>
ExtendedQueryMol::TautomerQuery_T from_pt(bpt::ptree &pt) {
std::vector<ROMOL_SPTR> tautomers;
for (auto &child : pt.get_child("tautomers")) {
tautomers.push_back(ROMOL_SPTR(pt_to_mol(child.second)));
}
ROMol *templ = pt_to_mol(pt.get_child("template"));
templ->updatePropertyCache(false);
std::vector<size_t> modifiedAtoms;
for (auto &child : pt.get_child("modifiedAtoms")) {
modifiedAtoms.push_back(child.second.get<size_t>(""));
}
std::vector<size_t> modifiedBonds;
for (auto &child : pt.get_child("modifiedBonds")) {
modifiedBonds.push_back(child.second.get<size_t>(""));
}
auto res = ExtendedQueryMol::TautomerQuery_T(
new TautomerQuery(tautomers, templ, modifiedAtoms, modifiedBonds));
return res;
}
template <>
ExtendedQueryMol::MolBundle_T from_pt(bpt::ptree &pt) {
auto res = ExtendedQueryMol::MolBundle_T(new MolBundle);
for (auto &child : pt.get_child("mols")) {
res->addMol(ROMOL_SPTR(pt_to_mol(child.second)));
}
return res;
}
template <>
ExtendedQueryMol::TautomerBundle_T from_pt(bpt::ptree &pt) {
ExtendedQueryMol::TautomerBundle_T res{
new std::vector<std::unique_ptr<TautomerQuery>>()};
for (auto &child : pt.get_child("tautomerQueries")) {
res->emplace_back(from_pt<ExtendedQueryMol::TautomerQuery_T>(child.second));
}
return res;
}
} // namespace
void ExtendedQueryMol::initFromJSON(const std::string &json) {
std::istringstream ss;
ss.str(json);
try {
bpt::ptree pt;
bpt::read_json(ss, pt);
auto xqmType = pt.get<unsigned char>("xqm_type");
switch (xqmType) {
case ExtendedQueryMol::ExtendedQueryMolTypes::XQM_MOL:
xqmol = from_pt<ExtendedQueryMol::RWMol_T>(pt);
break;
case ExtendedQueryMol::ExtendedQueryMolTypes::XQM_MOLBUNDLE:
xqmol = from_pt<ExtendedQueryMol::MolBundle_T>(pt);
break;
case ExtendedQueryMol::ExtendedQueryMolTypes::XQM_TAUTOMERQUERY:
xqmol = from_pt<ExtendedQueryMol::TautomerQuery_T>(pt);
break;
case ExtendedQueryMol::ExtendedQueryMolTypes::XQM_TAUTOMERBUNDLE:
xqmol = from_pt<ExtendedQueryMol::TautomerBundle_T>(pt);
break;
default:
UNDER_CONSTRUCTION("unrecognized type in JSON");
}
} catch (const bpt::ptree_error &) {
throw ValueErrorException("problems parsing JSON");
}
}
std::string ExtendedQueryMol::toJSON() const {
bpt::ptree pt;
if (std::holds_alternative<RWMol_T>(xqmol)) {
pt.put("xqm_type", (int)ExtendedQueryMolTypes::XQM_MOL);
to_pt(pt, *std::get<RWMol_T>(xqmol));
#ifdef RDK_USE_BOOST_SERIALIZATION
} else if (std::holds_alternative<MolBundle_T>(xqmol)) {
pt.put("xqm_type", (int)ExtendedQueryMolTypes::XQM_MOLBUNDLE);
to_pt(pt, *std::get<MolBundle_T>(xqmol));
} else if (std::holds_alternative<TautomerQuery_T>(xqmol)) {
pt.put("xqm_type", (int)ExtendedQueryMolTypes::XQM_TAUTOMERQUERY);
to_pt(pt, *std::get<TautomerQuery_T>(xqmol));
} else if (std::holds_alternative<TautomerBundle_T>(xqmol)) {
pt.put("xqm_type", (int)ExtendedQueryMolTypes::XQM_TAUTOMERBUNDLE);
const auto &itm = std::get<TautomerBundle_T>(xqmol);
pt.put("num_entries", itm->size());
to_pt(pt, itm);
#endif
} else {
UNDER_CONSTRUCTION("unrecognized type in ExtendedQueryMol");
}
std::stringstream ss;
bpt::json_parser::write_json(ss, pt);
return ss.str();
}
} // namespace GeneralizedSubstruct
} // namespace RDKit
|