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 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
|
//
//
// Copyright (C) 2019 Greg Landrum
//
// @@ 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 <string>
#include <iostream>
#include "minilib.h"
#include <RDGeneral/versions.h>
#include <GraphMol/RDKitBase.h>
#include <GraphMol/MolPickler.h>
#include <GraphMol/SmilesParse/SmilesParse.h>
#include <GraphMol/SmilesParse/SmilesWrite.h>
#include <GraphMol/FileParsers/FileParsers.h>
#include <GraphMol/MolDraw2D/MolDraw2D.h>
#include <GraphMol/MolDraw2D/MolDraw2DSVG.h>
#include <GraphMol/MolDraw2D/MolDraw2DUtils.h>
#include <GraphMol/Substruct/SubstructMatch.h>
#include <GraphMol/Descriptors/Property.h>
#include <GraphMol/Descriptors/MolDescriptors.h>
#include <GraphMol/Fingerprints/MorganFingerprints.h>
#include <GraphMol/Depictor/RDDepictor.h>
#include <GraphMol/CIPLabeler/CIPLabeler.h>
#include <GraphMol/Abbreviations/Abbreviations.h>
#include <DataStructs/BitOps.h>
#include <INCHI-API/inchi.h>
#include <rapidjson/document.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>
namespace rj = rapidjson;
using namespace RDKit;
std::string process_details(const std::string &details, unsigned int &width,
unsigned int &height, int &offsetx, int &offsety,
std::string &legend, std::vector<int> &atomIds,
std::vector<int> &bondIds) {
rj::Document doc;
doc.Parse(details.c_str());
if (!doc.IsObject()) return "Invalid JSON";
if (doc.HasMember("atoms")) {
if (!doc["atoms"].IsArray()) {
return "JSON doesn't contain 'atoms' field, or it is not an array";
}
for (const auto &molval : doc["atoms"].GetArray()) {
if (!molval.IsInt()) return ("Atom IDs should be integers");
atomIds.push_back(molval.GetInt());
}
}
if (doc.HasMember("bonds")) {
if (!doc["bonds"].IsArray()) {
return "JSON contain 'bonds' field, but it is not an array";
}
for (const auto &molval : doc["bonds"].GetArray()) {
if (!molval.IsInt()) return ("Bond IDs should be integers");
bondIds.push_back(molval.GetInt());
}
}
if (doc.HasMember("width")) {
if (!doc["width"].IsUint()) {
return "JSON contains 'width' field, but it is not an unsigned int";
}
width = doc["width"].GetUint();
}
if (doc.HasMember("height")) {
if (!doc["height"].IsUint()) {
return "JSON contains 'height' field, but it is not an unsigned int";
}
height = doc["height"].GetUint();
}
if (doc.HasMember("offsetx")) {
if (!doc["offsetx"].IsInt()) {
return "JSON contains 'offsetx' field, but it is not an int";
}
offsetx = doc["offsetx"].GetInt();
}
if (doc.HasMember("offsety")) {
if (!doc["offsety"].IsInt()) {
return "JSON contains 'offsety' field, but it is not an int";
}
offsety = doc["offsety"].GetInt();
}
if (doc.HasMember("legend")) {
if (!doc["legend"].IsString()) {
return "JSON contains 'legend' field, but it is not a string";
}
legend = doc["legend"].GetString();
}
return "";
}
namespace {
RWMol *mol_from_input(const std::string &input) {
RWMol *res = nullptr;
if (input.find("M END") != std::string::npos) {
bool sanitize = false;
res = MolBlockToMol(input, sanitize);
} else {
SmilesParserParams ps;
ps.sanitize = false;
res = SmilesToMol(input, ps);
}
if (res) {
try {
MolOps::sanitizeMol(*res);
MolOps::assignStereochemistry(*res, true, true, true);
} catch (...) {
delete res;
res = nullptr;
}
}
return res;
}
RWMol *qmol_from_input(const std::string &input) {
RWMol *res = nullptr;
if (input.find("M END") != std::string::npos) {
bool sanitize = false;
res = MolBlockToMol(input, sanitize);
} else {
res = SmartsToMol(input);
}
return res;
}
std::string svg_(const ROMol &m, unsigned int w, unsigned int h,
const std::string &details = "") {
std::vector<int> atomIds;
std::vector<int> bondIds;
std::string legend = "";
int offsetx = 0, offsety = 0;
if (!details.empty()) {
auto problems = process_details(details, w, h, offsetx, offsety, legend,
atomIds, bondIds);
if (!problems.empty()) {
return problems;
}
}
MolDraw2DSVG drawer(w, h);
if (!details.empty()) {
MolDraw2DUtils::updateDrawerParamsFromJSON(drawer, details);
}
drawer.setOffset(offsetx, offsety);
MolDraw2DUtils::prepareAndDrawMolecule(drawer, m, legend, &atomIds, &bondIds);
drawer.finishDrawing();
return drawer.getDrawingText();
}
} // namespace
std::string JSMol::get_smiles() const {
if (!d_mol) return "";
return MolToSmiles(*d_mol);
}
std::string JSMol::get_cxsmiles() const {
if (!d_mol) return "";
return MolToCXSmiles(*d_mol);
}
std::string JSMol::get_svg(unsigned int w, unsigned int h) const {
if (!d_mol) return "";
return svg_(*d_mol, w, h);
}
std::string JSMol::get_svg_with_highlights(const std::string &details) const {
if (!d_mol) return "";
unsigned int w = d_defaultWidth;
unsigned int h = d_defaultHeight;
return svg_(*d_mol, w, h, details);
}
std::string JSMol::get_inchi() const {
if (!d_mol) return "";
ExtraInchiReturnValues rv;
return MolToInchi(*d_mol, rv);
}
std::string JSMol::get_molblock() const {
if (!d_mol) return "";
return MolToMolBlock(*d_mol);
}
std::string JSMol::get_v3Kmolblock() const {
if (!d_mol) return "";
return MolToV3KMolBlock(*d_mol);
}
namespace {
void get_sss_json(const ROMol *d_mol, const ROMol *q_mol,
const MatchVectType &match, rj::Value &obj,
rj::Document &doc) {
rj::Value rjAtoms(rj::kArrayType);
for (const auto &pr : match) {
rjAtoms.PushBack(pr.second, doc.GetAllocator());
}
obj.AddMember("atoms", rjAtoms, doc.GetAllocator());
rj::Value rjBonds(rj::kArrayType);
for (const auto qbond : q_mol->bonds()) {
unsigned int idx1 = match[qbond->getBeginAtomIdx()].second;
unsigned int idx2 = match[qbond->getEndAtomIdx()].second;
const auto bond = d_mol->getBondBetweenAtoms(idx1, idx2);
if (bond != nullptr) {
rjBonds.PushBack(bond->getIdx(), doc.GetAllocator());
}
}
obj.AddMember("bonds", rjBonds, doc.GetAllocator());
}
} // namespace
std::string JSMol::get_substruct_match(const JSMol &q) const {
std::string res = "{}";
if (!d_mol || !q.d_mol) return res;
MatchVectType match;
if (SubstructMatch(*d_mol, *(q.d_mol), match)) {
rj::Document doc;
doc.SetObject();
get_sss_json(d_mol.get(), q.d_mol.get(), match, doc, doc);
rj::StringBuffer buffer;
rj::Writer<rj::StringBuffer> writer(buffer);
doc.Accept(writer);
res = buffer.GetString();
}
return res;
}
std::string JSMol::get_substruct_matches(const JSMol &q) const {
std::string res = "{}";
if (!d_mol || !q.d_mol) return res;
auto matches = SubstructMatch(*d_mol, (*q.d_mol));
if (!matches.empty()) {
rj::Document doc;
doc.SetArray();
for (const auto &match : matches) {
rj::Value rjMatch(rj::kObjectType);
get_sss_json(d_mol.get(), q.d_mol.get(), match, rjMatch, doc);
doc.PushBack(rjMatch, doc.GetAllocator());
}
rj::StringBuffer buffer;
rj::Writer<rj::StringBuffer> writer(buffer);
doc.Accept(writer);
res = buffer.GetString();
}
return res;
}
std::string JSMol::get_descriptors() const {
if (!d_mol) return "{}";
rj::Document doc;
doc.SetObject();
Descriptors::Properties props;
std::vector<std::string> dns = props.getPropertyNames();
std::vector<double> dvs = props.computeProperties(*d_mol);
for (size_t i = 0; i < dns.size(); ++i) {
rj::Value v(dvs[i]);
const auto srt = rj::StringRef(dns[i].c_str());
doc.AddMember(srt, v, doc.GetAllocator());
}
if (std::find(dns.begin(), dns.end(), std::string("amw")) == dns.end()) {
rj::Value v(Descriptors::calcAMW(*d_mol));
doc.AddMember("amw", v, doc.GetAllocator());
}
rj::StringBuffer buffer;
rj::Writer<rj::StringBuffer> writer(buffer);
writer.SetMaxDecimalPlaces(5);
doc.Accept(writer);
return buffer.GetString();
}
std::string JSMol::get_morgan_fp(unsigned int radius,
unsigned int fplen) const {
if (!d_mol) return "";
auto fp = MorganFingerprints::getFingerprintAsBitVect(*d_mol, radius, fplen);
std::string res = BitVectToText(*fp);
delete fp;
return res;
}
std::string JSMol::get_stereo_tags() const {
if (!d_mol) return "{}";
rj::Document doc;
doc.SetObject();
bool cleanIt = true;
bool force = true;
bool flagPossibleStereocenters = true;
MolOps::assignStereochemistry(*d_mol, cleanIt, force,
flagPossibleStereocenters);
CIPLabeler::assignCIPLabels(*d_mol);
rj::Value rjAtoms(rj::kArrayType);
for (const auto atom : d_mol->atoms()) {
std::string cip;
if (!atom->getPropIfPresent(common_properties::_CIPCode, cip)) {
if (atom->hasProp(common_properties::_ChiralityPossible)) {
cip = "?";
}
}
if (!cip.empty()) {
cip = "(" + cip + ")";
rj::Value entry(rj::kArrayType);
entry.PushBack(atom->getIdx(), doc.GetAllocator());
rj::Value v;
v.SetString(cip.c_str(), cip.size(), doc.GetAllocator());
entry.PushBack(v, doc.GetAllocator());
rjAtoms.PushBack(entry, doc.GetAllocator());
}
}
doc.AddMember("CIP_atoms", rjAtoms, doc.GetAllocator());
rj::Value rjBonds(rj::kArrayType);
for (const auto bond : d_mol->bonds()) {
std::string cip;
if (bond->getPropIfPresent(common_properties::_CIPCode, cip)) {
cip = "(" + cip + ")";
rj::Value entry(rj::kArrayType);
entry.PushBack(bond->getBeginAtomIdx(), doc.GetAllocator());
entry.PushBack(bond->getEndAtomIdx(), doc.GetAllocator());
rj::Value v;
v.SetString(cip.c_str(), cip.size(), doc.GetAllocator());
entry.PushBack(v, doc.GetAllocator());
rjBonds.PushBack(entry, doc.GetAllocator());
}
}
doc.AddMember("CIP_bonds", rjBonds, doc.GetAllocator());
rj::StringBuffer buffer;
rj::Writer<rj::StringBuffer> writer(buffer);
doc.Accept(writer);
return buffer.GetString();
}
std::string JSMol::get_aromatic_form() const {
if (!d_mol) return "";
RWMol molCopy(*d_mol);
MolOps::setAromaticity(molCopy);
bool includeStereo = true;
int confId = -1;
bool kekulize = false;
return MolToMolBlock(molCopy, includeStereo, confId, kekulize);
}
std::string JSMol::get_kekule_form() const {
if (!d_mol) return "";
RWMol molCopy(*d_mol);
MolOps::Kekulize(molCopy);
bool includeStereo = true;
int confId = -1;
bool kekulize = true;
return MolToMolBlock(molCopy, includeStereo, confId, kekulize);
}
std::string JSMol::get_new_coords(bool useCoordGen) const {
if (!d_mol) return "";
RWMol molCopy(*d_mol);
#ifdef RDK_BUILD_COORDGEN_SUPPORT
bool oprefer = RDDepict::preferCoordGen;
RDDepict::preferCoordGen = useCoordGen;
#endif
RDDepict::compute2DCoords(molCopy);
#ifdef RDK_BUILD_COORDGEN_SUPPORT
RDDepict::preferCoordGen = oprefer;
#endif
return MolToMolBlock(molCopy);
}
std::string JSMol::remove_hs() const {
if (!d_mol) return "";
RWMol molCopy(*d_mol);
MolOps::removeAllHs(molCopy);
bool includeStereo = true;
int confId = -1;
bool kekulize = true;
return MolToMolBlock(molCopy, includeStereo, confId, kekulize);
}
std::string JSMol::add_hs() const {
if (!d_mol) return "";
RWMol molCopy(*d_mol);
MolOps::addHs(molCopy);
// RDDepict::generateDepictionMatching2DStructure(molCopy, *d_mol);
bool includeStereo = true;
int confId = -1;
bool kekulize = true;
return MolToMolBlock(molCopy, includeStereo, confId, kekulize);
}
std::string JSMol::condense_abbreviations(double maxCoverage, bool useLinkers) {
if (!d_mol) return "";
if (!useLinkers) {
Abbreviations::condenseMolAbbreviations(
*d_mol, Abbreviations::Utils::getDefaultAbbreviations(), maxCoverage);
} else {
Abbreviations::condenseMolAbbreviations(
*d_mol, Abbreviations::Utils::getDefaultLinkers(), maxCoverage);
}
return "";
}
std::string JSMol::condense_abbreviations_from_defs(
const std::string &definitions, double maxCoverage, bool areLinkers) {
static std::string lastDefs = "";
static std::vector<Abbreviations::AbbreviationDefinition> abbrevs;
if (definitions != lastDefs) {
// yes, we are making the assumption that the "areLinkers" argument remains
// the same if the definitions are the same
bool removeExtraDummies = areLinkers;
bool allowConnectionToDummies = areLinkers;
lastDefs = definitions;
try {
abbrevs = Abbreviations::Utils::parseAbbreviations(
definitions, removeExtraDummies, allowConnectionToDummies);
} catch (...) {
return "cannot parse abbreviations";
}
}
Abbreviations::condenseMolAbbreviations(*d_mol, abbrevs, maxCoverage);
}
std::string JSMol::generate_aligned_coords(const JSMol &templateMol,bool useCoordGen){
if (!d_mol || !templateMol.d_mol || !templateMol.d_mol->getNumConformers()) return "";
#ifdef RDK_BUILD_COORDGEN_SUPPORT
bool oprefer = RDDepict::preferCoordGen;
RDDepict::preferCoordGen = useCoordGen;
#endif
RDKit::ROMol *refPattern = nullptr;
bool acceptFailure = true;
int confId = -1;
RDDepict::generateDepictionMatching2DStructure(*d_mol, *templateMol.d_mol, confId,
refPattern, acceptFailure);
#ifdef RDK_BUILD_COORDGEN_SUPPORT
RDDepict::preferCoordGen = oprefer;
#endif
return "";
};
std::string get_inchikey_for_inchi(const std::string &input) {
return InchiToInchiKey(input);
}
JSMol *get_mol(const std::string &input) {
RWMol *mol = mol_from_input(input);
return new JSMol(mol);
}
JSMol *get_qmol(const std::string &input) {
RWMol *mol = qmol_from_input(input);
return new JSMol(mol);
}
std::string version() { return std::string(rdkitVersion); }
void prefer_coordgen(bool useCoordGen) {
#ifdef RDK_BUILD_COORDGEN_SUPPORT
RDDepict::preferCoordGen = useCoordGen;
#endif
}
|