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
|
//
// Copyright (C) 2015-2017 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.
//
#define PY_ARRAY_UNIQUE_SYMBOL rdmoldraw2d_array_API
#include <RDBoost/python.h>
#include <GraphMol/ROMol.h>
#include <RDBoost/Wrap.h>
#include <GraphMol/MolDraw2D/MolDraw2D.h>
#include <GraphMol/MolDraw2D/MolDraw2DUtils.h>
#include <GraphMol/MolDraw2D/MolDraw2DSVG.h>
#include <Geometry/point.h>
#include <boost/python/suite/indexing/map_indexing_suite.hpp>
#ifdef RDK_BUILD_CAIRO_SUPPORT
#include <cairo.h>
#include <GraphMol/MolDraw2D/MolDraw2DCairo.h>
#endif
namespace python = boost::python;
namespace RDKit {
namespace {
void pyDictToColourMap(python::object pyo, ColourPalette &res) {
python::dict tDict = python::extract<python::dict>(pyo);
for (unsigned int i = 0;
i < python::extract<unsigned int>(tDict.keys().attr("__len__")()); ++i) {
python::tuple tpl = python::extract<python::tuple>(tDict.values()[i]);
float r = python::extract<float>(tpl[0]);
float g = python::extract<float>(tpl[1]);
float b = python::extract<float>(tpl[2]);
DrawColour clr(r, g, b);
res[python::extract<int>(tDict.keys()[i])] = clr;
}
}
ColourPalette *pyDictToColourMap(python::object pyo) {
ColourPalette *res = nullptr;
if (pyo) {
res = new ColourPalette;
pyDictToColourMap(pyo, *res);
}
return res;
}
void pyDictToDoubleMap(python::object pyo, std::map<int, double> &res) {
python::dict tDict = python::extract<python::dict>(pyo);
for (unsigned int i = 0;
i < python::extract<unsigned int>(tDict.keys().attr("__len__")()); ++i) {
double r = python::extract<double>(tDict.values()[i]);
res[python::extract<int>(tDict.keys()[i])] = r;
}
}
std::map<int, double> *pyDictToDoubleMap(python::object pyo) {
std::map<int, double> *res = nullptr;
if (pyo) {
res = new std::map<int, double>;
pyDictToDoubleMap(pyo, *res);
}
return res;
}
DrawColour pyTupleToDrawColour(const python::tuple tpl) {
float r = python::extract<float>(tpl[0]);
if (r > 1 || r < 0) {
throw ValueErrorException("RGB color value needs to be between 0 and 1.");
}
float g = python::extract<float>(tpl[1]);
if (g > 1 || g < 0) {
throw ValueErrorException("RGB color value needs to be between 0 and 1.");
}
float b = python::extract<float>(tpl[2]);
if (b > 1 || b < 0) {
throw ValueErrorException("RGB color value needs to be between 0 and 1.");
}
DrawColour clr(r, g, b);
return clr;
}
void pyListToColourVec(python::object pyo, std::vector<DrawColour> &res) {
res.clear();
python::list tList = python::extract<python::list>(pyo);
for (unsigned int i = 0;
i < python::extract<unsigned int>(tList.attr("__len__")()); ++i) {
python::tuple tpl = python::extract<python::tuple>(tList[i]);
res.push_back(pyTupleToDrawColour(tpl));
}
}
} // namespace
void drawMoleculeHelper1(MolDraw2D &self, const ROMol &mol,
python::object highlight_atoms,
python::object highlight_atom_map,
python::object highlight_atom_radii, int confId,
std::string legend) {
std::unique_ptr<std::vector<int>> highlightAtoms =
pythonObjectToVect(highlight_atoms, static_cast<int>(mol.getNumAtoms()));
ColourPalette *ham = pyDictToColourMap(highlight_atom_map);
std::map<int, double> *har = pyDictToDoubleMap(highlight_atom_radii);
self.drawMolecule(mol, legend, highlightAtoms.get(), ham, har, confId);
delete ham;
delete har;
}
void drawMoleculeHelper2(MolDraw2D &self, const ROMol &mol,
python::object highlight_atoms,
python::object highlight_bonds,
python::object highlight_atom_map,
python::object highlight_bond_map,
python::object highlight_atom_radii, int confId,
std::string legend) {
std::unique_ptr<std::vector<int>> highlightAtoms =
pythonObjectToVect(highlight_atoms, static_cast<int>(mol.getNumAtoms()));
std::unique_ptr<std::vector<int>> highlightBonds =
pythonObjectToVect(highlight_bonds, static_cast<int>(mol.getNumBonds()));
// FIX: support these
ColourPalette *ham = pyDictToColourMap(highlight_atom_map);
ColourPalette *hbm = pyDictToColourMap(highlight_bond_map);
std::map<int, double> *har = pyDictToDoubleMap(highlight_atom_radii);
self.drawMolecule(mol, legend, highlightAtoms.get(), highlightBonds.get(),
ham, hbm, har, confId);
delete ham;
delete hbm;
delete har;
}
void drawMoleculesHelper2(MolDraw2D &self, python::object pmols,
python::object highlight_atoms,
python::object highlight_bonds,
python::object highlight_atom_map,
python::object highlight_bond_map,
python::object highlight_atom_radii,
python::object pconfIds, python::object plegends) {
std::unique_ptr<std::vector<ROMol *>> mols =
pythonObjectToVect<ROMol *>(pmols);
if (mols == nullptr || !mols->size()) {
return;
}
unsigned int nThere = mols->size();
std::unique_ptr<std::vector<std::vector<int>>> highlightAtoms;
if (highlight_atoms) {
if (python::extract<unsigned int>(highlight_atoms.attr("__len__")()) !=
nThere) {
throw ValueErrorException(
"If highlightAtoms is provided it must be the same length as the "
"molecule list.");
}
highlightAtoms.reset(new std::vector<std::vector<int>>(nThere));
for (unsigned int i = 0; i < nThere; ++i) {
pythonObjectToVect(highlight_atoms[i], (*highlightAtoms)[i]);
}
}
std::unique_ptr<std::vector<std::vector<int>>> highlightBonds;
if (highlight_bonds) {
if (python::extract<unsigned int>(highlight_bonds.attr("__len__")()) !=
nThere) {
throw ValueErrorException(
"If highlightBonds is provided it must be the same length as the "
"molecule list.");
}
highlightBonds.reset(new std::vector<std::vector<int>>(nThere));
for (unsigned int i = 0; i < nThere; ++i) {
pythonObjectToVect(highlight_bonds[i], (*highlightBonds)[i]);
}
}
std::unique_ptr<std::vector<ColourPalette>> highlightAtomMap;
if (highlight_atom_map) {
if (python::extract<unsigned int>(highlight_atom_map.attr("__len__")()) !=
nThere) {
throw ValueErrorException(
"If highlightAtomMap is provided it must be the same length as the "
"molecule list.");
}
highlightAtomMap.reset(new std::vector<ColourPalette>(nThere));
for (unsigned int i = 0; i < nThere; ++i) {
pyDictToColourMap(highlight_atom_map[i], (*highlightAtomMap)[i]);
}
}
std::unique_ptr<std::vector<ColourPalette>> highlightBondMap;
if (highlight_bond_map) {
if (python::extract<unsigned int>(highlight_bond_map.attr("__len__")()) !=
nThere) {
throw ValueErrorException(
"If highlightBondMap is provided it must be the same length as the "
"molecule list.");
}
highlightBondMap.reset(new std::vector<ColourPalette>(nThere));
for (unsigned int i = 0; i < nThere; ++i) {
pyDictToColourMap(highlight_bond_map[i], (*highlightBondMap)[i]);
}
}
std::unique_ptr<std::vector<std::map<int, double>>> highlightRadii;
if (highlight_atom_radii) {
if (python::extract<unsigned int>(highlight_atom_radii.attr("__len__")()) !=
nThere) {
throw ValueErrorException(
"If highlightAtomRadii is provided it must be the same length as the "
"molecule list.");
}
highlightRadii.reset(new std::vector<std::map<int, double>>(nThere));
for (unsigned int i = 0; i < nThere; ++i) {
pyDictToDoubleMap(highlight_atom_radii[i], (*highlightRadii)[i]);
}
}
// std::unique_ptr<std::vector<int> > highlightAtoms =
// pythonObjectToVect(highlight_atoms,
// static_cast<int>(mol.getNumAtoms()));
// std::unique_ptr<std::vector<int> > highlightBonds =
// pythonObjectToVect(highlight_bonds,
// static_cast<int>(mol.getNumBonds()));
// FIX: support these
// std::map<int, DrawColour> *ham = pyDictToColourMap(highlight_atom_map);
// std::map<int, DrawColour> *hbm = pyDictToColourMap(highlight_bond_map);
// std::map<int, double> *har = pyDictToDoubleMap(highlight_atom_radii);
//
std::unique_ptr<std::vector<int>> confIds = pythonObjectToVect<int>(pconfIds);
std::unique_ptr<std::vector<std::string>> legends =
pythonObjectToVect<std::string>(plegends);
self.drawMolecules(*mols, legends.get(), highlightAtoms.get(),
highlightBonds.get(), highlightAtomMap.get(),
highlightBondMap.get(), highlightRadii.get(),
confIds.get());
}
void drawReactionHelper(MolDraw2D &self, const ChemicalReaction &rxn,
bool highlightByReactant,
python::object phighlightColorsReactants,
python::object pconfIds) {
std::unique_ptr<std::vector<DrawColour>> highlightColorsReactants;
if (phighlightColorsReactants) {
highlightColorsReactants.reset(new std::vector<DrawColour>);
pyListToColourVec(phighlightColorsReactants, *highlightColorsReactants);
}
std::unique_ptr<std::vector<int>> confIds = pythonObjectToVect<int>(pconfIds);
self.drawReaction(rxn, highlightByReactant, highlightColorsReactants.get(),
confIds.get());
}
#ifdef RDK_BUILD_CAIRO_SUPPORT
python::object getCairoDrawingText(const RDKit::MolDraw2DCairo &self) {
std::string res = self.getDrawingText();
python::object retval = python::object(
python::handle<>(PyBytes_FromStringAndSize(res.c_str(), res.length())));
return retval;
}
#endif
ROMol *prepMolForDrawing(const ROMol *m, bool kekulize = true,
bool addChiralHs = true, bool wedgeBonds = true,
bool forceCoords = false) {
auto *res = new RWMol(*m);
MolDraw2DUtils::prepareMolForDrawing(*res, kekulize, addChiralHs, wedgeBonds,
forceCoords);
return static_cast<ROMol *>(res);
}
python::tuple colourToPyTuple(const DrawColour &clr) {
python::list res;
res.append(clr.get<0>());
res.append(clr.get<1>());
res.append(clr.get<2>());
return python::tuple(res);
}
python::object getBgColour(const RDKit::MolDrawOptions &self) {
return colourToPyTuple(self.backgroundColour);
}
python::object getHighlightColour(const RDKit::MolDrawOptions &self) {
return colourToPyTuple(self.highlightColour);
}
void setBgColour(RDKit::MolDrawOptions &self, python::tuple tpl) {
self.backgroundColour = pyTupleToDrawColour(tpl);
}
void setHighlightColour(RDKit::MolDrawOptions &self, python::tuple tpl) {
self.highlightColour = pyTupleToDrawColour(tpl);
}
void useDefaultAtomPalette(RDKit::MolDrawOptions &self) {
assignDefaultPalette(self.atomColourPalette);
}
void useBWAtomPalette(RDKit::MolDrawOptions &self) {
assignBWPalette(self.atomColourPalette);
}
void updateAtomPalette(RDKit::MolDrawOptions &self, python::object cmap) {
pyDictToColourMap(cmap, self.atomColourPalette);
}
void setAtomPalette(RDKit::MolDrawOptions &self, python::object cmap) {
self.atomColourPalette.clear();
updateAtomPalette(self, cmap);
}
void addMoleculeMetadata(const RDKit::MolDraw2DSVG &self, const RDKit::ROMol &m,
int confId) {
self.addMoleculeMetadata(m, confId);
}
} // namespace RDKit
BOOST_PYTHON_MODULE(rdMolDraw2D) {
python::scope().attr("__doc__") =
"Module containing a C++ implementation of 2D molecule drawing";
std::string docString;
python::class_<std::map<int, std::string>>("IntStringMap")
.def(python::map_indexing_suite<std::map<int, std::string>, true>());
docString = "Drawing options";
python::class_<RDKit::MolDrawOptions, boost::noncopyable>("MolDrawOptions",
docString.c_str())
.def_readwrite("dummiesAreAttachments",
&RDKit::MolDrawOptions::dummiesAreAttachments)
.def_readwrite("circleAtoms", &RDKit::MolDrawOptions::circleAtoms)
//.def_readwrite("highlightColour",
//&RDKit::MolDrawOptions::highlightColour,
// "the highlight colour")
.def("getBackgroundColour", &RDKit::getBgColour,
"method returning the background colour")
.def("getHighlightColour", &RDKit::getHighlightColour,
"method returning the highlight colour")
.def("setBackgroundColour", &RDKit::setBgColour,
"method for setting the background colour")
.def("setHighlightColour", &RDKit::setHighlightColour,
"method for setting the highlight colour")
.def("useDefaultAtomPalette", &RDKit::useDefaultAtomPalette,
"use the default colour palette for atoms and bonds")
.def("useBWAtomPalette", &RDKit::useBWAtomPalette,
"use the black & white palette for atoms and bonds")
.def("updateAtomPalette", &RDKit::updateAtomPalette,
"updates the palette for atoms and bonds from a dictionary mapping "
"ints to 3-tuples")
.def(
"setAtomPalette", &RDKit::setAtomPalette,
"sets the palette for atoms and bonds from a dictionary mapping ints "
"to 3-tuples")
.def_readwrite("atomLabels", &RDKit::MolDrawOptions::atomLabels,
"maps indices to atom labels")
.def_readwrite("atomLabelDeuteriumTritium",
&RDKit::MolDrawOptions::atomLabelDeuteriumTritium,
"labels deuterium as D and tritium as T")
.def_readwrite("continuousHighlight",
&RDKit::MolDrawOptions::continuousHighlight)
.def_readwrite("fillHighlights", &RDKit::MolDrawOptions::fillHighlights)
.def_readwrite("flagCloseContactsDist",
&RDKit::MolDrawOptions::flagCloseContactsDist)
.def_readwrite("atomRegions", &RDKit::MolDrawOptions::atomRegions,
"regions to outline")
.def_readwrite("includeAtomTags", &RDKit::MolDrawOptions::includeAtomTags,
"include atom tags in output")
.def_readwrite("clearBackground", &RDKit::MolDrawOptions::clearBackground,
"clear the background before drawing a molecule")
.def_readwrite("legendFontSize", &RDKit::MolDrawOptions::legendFontSize,
"font size in pixels of the legend (if drawn)")
.def_readwrite(
"multipleBondOffset", &RDKit::MolDrawOptions::multipleBondOffset,
"offset (in Angstroms) for the extra lines in a multiple bond")
.def_readwrite("padding", &RDKit::MolDrawOptions::padding,
"fraction of empty space to leave around molecule")
.def_readwrite("additionalAtomLabelPadding",
&RDKit::MolDrawOptions::additionalAtomLabelPadding,
"additional padding to leave around atom labels. "
"Expressed as a fraction of the font size.");
docString = "Drawer abstract base class";
python::class_<RDKit::MolDraw2D, boost::noncopyable>(
"MolDraw2D", docString.c_str(), python::no_init)
.def("SetFontSize", &RDKit::MolDraw2D::setFontSize,
"change the default font size")
.def("FontSize", &RDKit::MolDraw2D::fontSize, "get the default font size")
.def(
"DrawMolecule", RDKit::drawMoleculeHelper1,
(python::arg("self"), python::arg("mol"),
python::arg("highlightAtoms") = python::object(),
python::arg("highlightAtomColors") = python::object(),
python::arg("highlightAtomRadii") = python::object(),
python::arg("confId") = -1, python::arg("legend") = std::string("")),
"renders a molecule\n")
.def(
"DrawMolecule", RDKit::drawMoleculeHelper2,
(python::arg("self"), python::arg("mol"),
python::arg("highlightAtoms"), python::arg("highlightBonds"),
python::arg("highlightAtomColors") = python::object(),
python::arg("highlightBondColors") = python::object(),
python::arg("highlightAtomRadii") = python::object(),
python::arg("confId") = -1, python::arg("legend") = std::string("")),
"renders a molecule\n")
.def("DrawMolecules", RDKit::drawMoleculesHelper2,
(python::arg("self"), python::arg("mols"),
python::arg("highlightAtoms") = python::object(),
python::arg("highlightBonds") = python::object(),
python::arg("highlightAtomColors") = python::object(),
python::arg("highlightBondColors") = python::object(),
python::arg("highlightAtomRadii") = python::object(),
python::arg("confIds") = python::object(),
python::arg("legends") = python::object()),
"renders multiple molecules\n")
.def("DrawReaction", RDKit::drawReactionHelper,
(python::arg("self"), python::arg("rxn"),
python::arg("highlightByReactant") = false,
python::arg("highlightColorsReactants") = python::object(),
python::arg("confIds") = python::object()),
"renders a reaction\n")
.def("Width", &RDKit::MolDraw2D::width,
"get the width of the drawing canvas")
.def("Height", &RDKit::MolDraw2D::height,
"get the height of the drawing canvas")
.def("SetOffset", &RDKit::MolDraw2D::setOffset,
"set the offset (in drawing coordinates) for the drawing")
.def("Offset", &RDKit::MolDraw2D::offset,
"returns the offset (in drawing coordinates) for the drawing")
.def("SetScale", &RDKit::MolDraw2D::setScale,
"uses the values provided to set the drawing scaling")
.def("DrawString", &RDKit::MolDraw2D::drawString,
(python::arg("self"), python::arg("string"), python::arg("pos")),
"add text to the canvas")
.def("GetDrawCoords",
(RDGeom::Point2D(RDKit::MolDraw2D::*)(const RDGeom::Point2D &)
const) &
RDKit::MolDraw2D::getDrawCoords,
(python::arg("self"), python::arg("point")),
"get the coordinates in drawing space for a particular point in "
"molecule space")
.def("GetDrawCoords",
(RDGeom::Point2D(RDKit::MolDraw2D::*)(int) const) &
RDKit::MolDraw2D::getDrawCoords,
(python::arg("self"), python::arg("atomIndex")),
"get the coordinates in drawing space for a particular atom")
.def("drawOptions",
(RDKit::MolDrawOptions & (RDKit::MolDraw2D::*)()) &
RDKit::MolDraw2D::drawOptions,
python::return_internal_reference<
1, python::with_custodian_and_ward_postcall<0, 1>>(),
"Returns a modifiable version of the current drawing options");
docString = "SVG molecule drawer";
python::class_<RDKit::MolDraw2DSVG, python::bases<RDKit::MolDraw2D>,
boost::noncopyable>("MolDraw2DSVG", docString.c_str(),
python::init<int, int>())
.def(python::init<int, int, int, int>())
.def("FinishDrawing", &RDKit::MolDraw2DSVG::finishDrawing,
"add the last bits of SVG to finish the drawing")
.def("AddMoleculeMetadata", RDKit::addMoleculeMetadata,
(python::arg("mol"), python::arg("confId") = -1),
"add RDKit-specific information to the bottom of the drawing")
.def("GetDrawingText", &RDKit::MolDraw2DSVG::getDrawingText,
"return the SVG");
#ifdef RDK_BUILD_CAIRO_SUPPORT
docString = "Cairo molecule drawer";
python::class_<RDKit::MolDraw2DCairo, python::bases<RDKit::MolDraw2D>,
boost::noncopyable>("MolDraw2DCairo", docString.c_str(),
python::init<int, int>())
.def(python::init<int, int, int, int>())
.def("FinishDrawing", &RDKit::MolDraw2DCairo::finishDrawing,
"add the last bits to finish the drawing")
.def("GetDrawingText", &RDKit::getCairoDrawingText,
"return the PNG data as a string")
.def("WriteDrawingText", &RDKit::MolDraw2DCairo::writeDrawingText,
"write the PNG data to the named file");
#endif
docString =
"Does some cleanup operations on the molecule to prepare it to draw "
"nicely.\n"
"The operations include: kekulization, addition of chiral Hs (so that we "
"can draw\n"
"wedges to them), wedging of bonds at chiral centers, and generation of "
"a 2D\n"
"conformation if the molecule does not already have a conformation\n"
"\nReturns a modified copy of the molecule.\n";
python::def(
"PrepareMolForDrawing", &RDKit::prepMolForDrawing,
(python::arg("mol"), python::arg("kekulize") = true,
python::arg("addChiralHs") = true, python::arg("wedgeBonds") = true,
python::arg("forceCoords") = false),
docString.c_str(),
python::return_value_policy<python::manage_new_object>());
}
|