Files
rdkit/Code/GraphMol/MolDraw2D/catch_tests.cpp
Paolo Tosco 2c285c2c38 Fixes #3744 (#3748)
* fixes #3744

* added a unit test

* fix by stopping with the unnecessary copies

Co-authored-by: greg landrum <greg.landrum@gmail.com>
2021-01-20 18:09:29 +01:00

957 lines
33 KiB
C++

//
// 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 "catch.hpp"
#include <GraphMol/RDKitBase.h>
#include <GraphMol/SmilesParse/SmilesParse.h>
#include <GraphMol/MolDraw2D/MolDraw2D.h>
#include <GraphMol/MolDraw2D/MolDraw2DSVG.h>
#include <GraphMol/MolDraw2D/MolDraw2DUtils.h>
#include <GraphMol/MolDraw2D/MolDraw2DDetails.h>
#include <GraphMol/FileParsers/FileParsers.h>
#include <GraphMol/FileParsers/PNGParser.h>
#include <boost/algorithm/string/split.hpp>
#include <GraphMol/ChemReactions/Reaction.h>
#include <GraphMol/ChemReactions/ReactionParser.h>
#include <GraphMol/CIPLabeler/CIPLabeler.h>
#include <regex>
#ifdef RDK_BUILD_CAIRO_SUPPORT
#include <cairo.h>
#include "MolDraw2DCairo.h"
#endif
// a lot of the tests check <text> flags in the SVG. That doesn't
// happen with the Freetype versions
static const bool NO_FREETYPE = true;
using namespace RDKit;
TEST_CASE("prepareAndDrawMolecule", "[drawing]") {
SECTION("basics") {
auto m1 = "C1N[C@@H]2OCC12"_smiles;
REQUIRE(m1);
// we will be able to recognize that the prep worked because there
// will be an H in the output:
MolDraw2DSVG drawer(200, 200, -1, -1, NO_FREETYPE);
MolDraw2DUtils::prepareAndDrawMolecule(drawer, *m1);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
CHECK(text.find(">H</text>") != std::string::npos);
}
}
TEST_CASE("tag atoms in SVG", "[drawing][SVG]") {
SECTION("basics") {
auto m1 = "C1N[C@@H]2OCC12"_smiles;
REQUIRE(m1);
MolDraw2DSVG drawer(200, 200, -1, -1, NO_FREETYPE);
MolDraw2DUtils::prepareMolForDrawing(*m1);
drawer.drawMolecule(*m1);
std::map<std::string, std::string> actions;
actions["onclick"] = "alert";
double radius = 0.2;
drawer.tagAtoms(*m1, radius, actions);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
std::ofstream outs("testAtomTags_1.svg");
outs << text;
outs.flush();
CHECK(text.find("<circle") != std::string::npos);
CHECK(text.find("<circle") != std::string::npos);
CHECK(text.find("atom-selector") != std::string::npos);
CHECK(text.find("bond-selector") != std::string::npos);
}
}
TEST_CASE("contour data", "[drawing][conrec]") {
auto m1 = "C1N[C@@H]2OCC12"_smiles;
REQUIRE(m1);
SECTION("grid basics") {
MolDraw2DSVG drawer(250, 250, -1, -1, NO_FREETYPE);
MolDraw2DUtils::prepareMolForDrawing(*m1);
const size_t gridSz = 100;
auto *grid = new double[gridSz * gridSz];
std::vector<double> xps(gridSz);
std::vector<double> yps(gridSz);
double minX = 1000, minY = 1000, maxX = -1000, maxY = -1000;
const auto conf = m1->getConformer();
for (size_t i = 0; i < conf.getNumAtoms(); ++i) {
minX = std::min(minX, conf.getAtomPos(i).x);
minY = std::min(minY, conf.getAtomPos(i).y);
maxX = std::max(maxX, conf.getAtomPos(i).x);
maxY = std::max(maxY, conf.getAtomPos(i).y);
}
double x1 = minX - 0.5, y1 = minY - 0.5, x2 = maxX + 0.5, y2 = maxY + 0.5;
double dx = (x2 - x1) / gridSz, dy = (y2 - y1) / gridSz;
double maxV = 0.0;
for (size_t ix = 0; ix < gridSz; ++ix) {
auto px = x1 + ix * dx;
xps[ix] = px;
for (size_t iy = 0; iy < gridSz; ++iy) {
auto py = y1 + iy * dy;
if (ix == 0) {
yps[iy] = py;
}
RDGeom::Point2D loc(px, py);
double val = 0.0;
for (size_t ia = 0; ia < conf.getNumAtoms(); ++ia) {
auto dv = loc - RDGeom::Point2D(conf.getAtomPos(ia).x,
conf.getAtomPos(ia).y);
auto r = dv.length();
if (r > 0.1) {
val += 1 / r;
}
}
maxV = std::max(val, maxV);
grid[ix * gridSz + iy] = val;
}
}
std::vector<double> levels;
drawer.clearDrawing();
MolDraw2DUtils::contourAndDrawGrid(drawer, grid, xps, yps, 10, levels,
MolDraw2DUtils::ContourParams(),
m1.get());
drawer.drawOptions().clearBackground = false;
drawer.drawMolecule(*m1);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
std::ofstream outs("contourMol_1.svg");
outs << text;
outs.flush();
delete[] grid;
}
SECTION("gaussian basics") {
MolDraw2DSVG drawer(250, 250, -1, -1, NO_FREETYPE);
MolDraw2DUtils::prepareMolForDrawing(*m1);
drawer.drawOptions().padding = 0.1;
const auto conf = m1->getConformer();
std::vector<Point2D> cents(conf.getNumAtoms());
std::vector<double> weights(conf.getNumAtoms());
std::vector<double> widths(conf.getNumAtoms());
for (size_t i = 0; i < conf.getNumAtoms(); ++i) {
cents[i] = Point2D(conf.getAtomPos(i).x, conf.getAtomPos(i).y);
weights[i] = 1;
widths[i] = 0.4 * PeriodicTable::getTable()->getRcovalent(
m1->getAtomWithIdx(i)->getAtomicNum());
}
std::vector<double> levels;
drawer.clearDrawing();
MolDraw2DUtils::contourAndDrawGaussians(
drawer, cents, weights, widths, 10, levels,
MolDraw2DUtils::ContourParams(), m1.get());
drawer.drawOptions().clearBackground = false;
drawer.drawMolecule(*m1);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
std::ofstream outs("contourMol_2.svg");
outs << text;
outs.flush();
}
SECTION("gaussian fill") {
MolDraw2DSVG drawer(250, 250, -1, -1, NO_FREETYPE);
MolDraw2DUtils::prepareMolForDrawing(*m1);
drawer.drawOptions().padding = 0.1;
const auto conf = m1->getConformer();
std::vector<Point2D> cents(conf.getNumAtoms());
std::vector<double> weights(conf.getNumAtoms());
std::vector<double> widths(conf.getNumAtoms());
for (size_t i = 0; i < conf.getNumAtoms(); ++i) {
cents[i] = Point2D(conf.getAtomPos(i).x, conf.getAtomPos(i).y);
weights[i] = i % 2 ? -0.5 : 1;
widths[i] = 0.4 * PeriodicTable::getTable()->getRcovalent(
m1->getAtomWithIdx(i)->getAtomicNum());
}
std::vector<double> levels;
MolDraw2DUtils::ContourParams cps;
cps.fillGrid = true;
drawer.clearDrawing();
MolDraw2DUtils::contourAndDrawGaussians(drawer, cents, weights, widths, 10,
levels, cps, m1.get());
drawer.drawOptions().clearBackground = false;
drawer.drawMolecule(*m1);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
std::ofstream outs("contourMol_3.svg");
outs << text;
outs.flush();
}
SECTION("gaussian fill 2") {
auto m2 = "C1N[C@@H]2OCC12C=CC"_smiles;
REQUIRE(m2);
MolDraw2DSVG drawer(450, 250, -1, -1, NO_FREETYPE);
MolDraw2DUtils::prepareMolForDrawing(*m2);
drawer.drawOptions().padding = 0.1;
const auto conf = m2->getConformer();
std::vector<Point2D> cents(conf.getNumAtoms());
std::vector<double> weights(conf.getNumAtoms());
std::vector<double> widths(conf.getNumAtoms());
for (size_t i = 0; i < conf.getNumAtoms(); ++i) {
cents[i] = Point2D(conf.getAtomPos(i).x, conf.getAtomPos(i).y);
weights[i] = i % 2 ? -0.5 : 1;
widths[i] = 0.3 * PeriodicTable::getTable()->getRcovalent(
m2->getAtomWithIdx(i)->getAtomicNum());
}
std::vector<double> levels;
MolDraw2DUtils::ContourParams cps;
cps.fillGrid = true;
cps.gridResolution = 0.5;
drawer.clearDrawing();
MolDraw2DUtils::contourAndDrawGaussians(drawer, cents, weights, widths, 10,
levels, cps, m2.get());
drawer.drawOptions().clearBackground = false;
drawer.drawMolecule(*m2);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
std::ofstream outs("contourMol_4.svg");
outs << text;
outs.flush();
}
}
TEST_CASE("dative bonds", "[drawing][organometallics]") {
SECTION("basics") {
auto m1 = "N->[Pt]"_smiles;
REQUIRE(m1);
MolDraw2DSVG drawer(200, 200, -1, -1, NO_FREETYPE);
MolDraw2DUtils::prepareMolForDrawing(*m1);
drawer.drawMolecule(*m1);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
std::ofstream outs("testDativeBonds_1.svg");
outs << text;
outs.flush();
CHECK(text.find("<path class='bond-0' d='M 126.052,100 L 85.9675,100'"
" style='fill:none;fill-rule:evenodd;"
"stroke:#0000FF;") != std::string::npos);
}
SECTION("more complex") {
auto m1 = "N->1[C@@H]2CCCC[C@H]2N->[Pt]11OC(=O)C(=O)O1"_smiles;
REQUIRE(m1);
MolDraw2DSVG drawer(200, 200, -1, -1, NO_FREETYPE);
MolDraw2DUtils::prepareMolForDrawing(*m1);
drawer.drawMolecule(*m1);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
std::ofstream outs("testDativeBonds_2.svg");
outs << text;
outs.flush();
CHECK(text.find("<path class='bond-7' d='M 101.307,79.424 "
"L 95.669,87.1848' style='fill:none;"
"fill-rule:evenodd;stroke:#0000FF;") != std::string::npos);
}
SECTION("test colours") {
// the dative bonds point the wrong way, but the point is to test
// if the tip of the arrow is blue.
auto m1 = "[Cu++]->1->2.N1CCN2"_smiles;
REQUIRE(m1);
MolDraw2DSVG drawer(200, 200, -1, -1, NO_FREETYPE);
MolDraw2DUtils::prepareMolForDrawing(*m1);
drawer.drawMolecule(*m1);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
std::ofstream outs("testDativeBonds_3.svg");
outs << text;
outs.flush();
CHECK(text.find("<path class='bond-2' d='M 53.289,140.668"
" L 81.0244,149.68' style='fill:none;"
"fill-rule:evenodd;stroke:#0000FF;") != std::string::npos);
}
SECTION("dative series") {
auto m1 = "N->1[C@@H]2CCCC[C@H]2N->[Pt]11OC(=O)C(=O)O1"_smiles;
REQUIRE(m1);
{
MolDraw2DSVG drawer(150, 150, -1, -1, NO_FREETYPE);
MolDraw2DUtils::prepareMolForDrawing(*m1);
drawer.drawMolecule(*m1);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
std::ofstream outs("testDativeBonds_2a.svg");
outs << text;
outs.flush();
}
{
MolDraw2DSVG drawer(250, 250, -1, -1, NO_FREETYPE);
MolDraw2DUtils::prepareMolForDrawing(*m1);
drawer.drawMolecule(*m1);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
std::ofstream outs("testDativeBonds_2b.svg");
outs << text;
outs.flush();
}
{
MolDraw2DSVG drawer(350, 350, -1, -1, NO_FREETYPE);
MolDraw2DUtils::prepareMolForDrawing(*m1);
drawer.drawMolecule(*m1);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
std::ofstream outs("testDativeBonds_2c.svg");
outs << text;
outs.flush();
}
{
MolDraw2DSVG drawer(450, 450, -1, -1, NO_FREETYPE);
MolDraw2DUtils::prepareMolForDrawing(*m1);
drawer.drawMolecule(*m1);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
std::ofstream outs("testDativeBonds_2d.svg");
outs << text;
outs.flush();
}
}
}
TEST_CASE("zero-order bonds", "[drawing][organometallics]") {
SECTION("basics") {
auto m1 = "N-[Pt]"_smiles;
REQUIRE(m1);
m1->getBondWithIdx(0)->setBondType(Bond::ZERO);
MolDraw2DSVG drawer(200, 200, -1, -1, NO_FREETYPE);
MolDraw2DUtils::prepareMolForDrawing(*m1);
drawer.drawMolecule(*m1);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
std::ofstream outs("testZeroOrderBonds_1.svg");
outs << text;
outs.flush();
CHECK(text.find("stroke-dasharray:2,2") != std::string::npos);
}
}
TEST_CASE("copying drawing options", "[drawing]") {
auto m1 = "C1N[C@@H]2OCC12"_smiles;
REQUIRE(m1);
SECTION("foundations") {
{
MolDraw2DSVG drawer(200, 200, -1, -1, NO_FREETYPE);
MolDraw2DUtils::prepareAndDrawMolecule(drawer, *m1);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
std::ofstream outs("testFoundations_1.svg");
outs << text;
outs.flush();
CHECK(text.find("fill:#0000FF' >N</text>") != std::string::npos);
}
{
MolDraw2DSVG drawer(200, 200, -1, -1, NO_FREETYPE);
assignBWPalette(drawer.drawOptions().atomColourPalette);
MolDraw2DUtils::prepareAndDrawMolecule(drawer, *m1);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
std::ofstream outs("testFoundations_2.svg");
outs << text;
outs.flush();
CHECK(text.find("fill:#0000FF' >N</text>") == std::string::npos);
CHECK(text.find("fill:#000000' >N</text>") != std::string::npos);
}
}
SECTION("test") {
{
MolDraw2DSVG drawer(200, 200, -1, -1, NO_FREETYPE);
MolDrawOptions options = drawer.drawOptions();
assignBWPalette(options.atomColourPalette);
drawer.drawOptions() = options;
MolDraw2DUtils::prepareAndDrawMolecule(drawer, *m1);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
std::ofstream outs("testTest_1.svg");
outs << text;
outs.flush();
CHECK(text.find("fill:#0000FF' >N</text>") == std::string::npos);
CHECK(text.find("fill:#000000' >N</text>") != std::string::npos);
}
}
}
TEST_CASE("bad DrawMolecules() when molecules are not kekulized",
"[drawing][bug]") {
auto m1 = "CCN(CC)CCn1nc2c3ccccc3sc3c(CNS(C)(=O)=O)ccc1c32"_smiles;
REQUIRE(m1);
SECTION("foundations") {
MolDraw2DSVG drawer(500, 200, 250, 200, NO_FREETYPE);
drawer.drawOptions().prepareMolsBeforeDrawing = false;
RWMol dm1(*m1);
RWMol dm2(*m1);
bool kekulize = false;
MolDraw2DUtils::prepareMolForDrawing(dm1, kekulize);
kekulize = true;
MolDraw2DUtils::prepareMolForDrawing(dm2, kekulize);
MOL_PTR_VECT ms{&dm1, &dm2};
drawer.drawMolecule(dm1);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
std::ofstream outs("testKekulizationProblems_1.svg");
outs << text;
outs.flush();
// this is a very crude test - really we just need to look at the SVG - but
// it's better than nothing.
CHECK(text.find(
"<path class='bond-18' d='M 169.076,79.056 L 191.285,69.2653' "
"style='fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:"
"2px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;"
"stroke-dasharray:6,6' />") == std::string::npos);
}
}
TEST_CASE("draw atom/bond indices", "[drawing]") {
auto m1 = "C[C@H](F)N"_smiles;
REQUIRE(m1);
SECTION("foundations") {
{
MolDraw2DSVG drawer(250, 200, -1, -1, NO_FREETYPE);
drawer.drawMolecule(*m1);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
std::ofstream outs("testAtomBondIndices_1.svg");
outs << text;
outs.flush();
CHECK(text.find(">1</text>") == std::string::npos);
CHECK(text.find(">(</text>") == std::string::npos);
CHECK(text.find(">S</text>") == std::string::npos);
CHECK(text.find(">)</text>") == std::string::npos);
}
{
MolDraw2DSVG drawer(250, 200, -1, -1, NO_FREETYPE);
drawer.drawOptions().addAtomIndices = true;
drawer.drawMolecule(*m1);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
std::ofstream outs("testAtomBondIndices_2.svg");
outs << text;
outs.flush();
CHECK(text.find(">1</text>") != std::string::npos);
// it only appears once though:
CHECK(text.find(">1</text>", text.find(">1</text>") + 1) ==
std::string::npos);
CHECK(text.find("1,(S)") == std::string::npos);
}
{
MolDraw2DSVG drawer(250, 200, -1, -1, NO_FREETYPE);
drawer.drawOptions().addBondIndices = true;
drawer.drawMolecule(*m1);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
std::ofstream outs("testAtomBondIndices_3.svg");
outs << text;
outs.flush();
CHECK(text.find(">1</text>") != std::string::npos);
// it only appears once though:
CHECK(text.find(">1</text>", text.find(">1</text>") + 1) ==
std::string::npos);
}
{
MolDraw2DSVG drawer(250, 200, -1, -1, NO_FREETYPE);
drawer.drawOptions().addAtomIndices = true;
drawer.drawOptions().addBondIndices = true;
drawer.drawMolecule(*m1);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
std::ofstream outs("testAtomBondIndices_4.svg");
outs << text;
outs.flush();
CHECK(text.find(">1</text>") != std::string::npos);
// it appears twice:
CHECK(text.find(">1</text>", text.find(">1</text>") + 1) !=
std::string::npos);
}
{
MolDraw2DSVG drawer(250, 200, -1, -1, NO_FREETYPE);
m1->getAtomWithIdx(2)->setProp(common_properties::atomNote, "foo");
drawer.drawOptions().addAtomIndices = true;
drawer.drawOptions().addStereoAnnotation = true;
drawer.drawMolecule(*m1);
m1->getAtomWithIdx(2)->clearProp(common_properties::atomNote);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
std::ofstream outs("testAtomBondIndices_5.svg");
outs << text;
outs.flush();
CHECK(text.find(">1</text>") != std::string::npos);
CHECK(text.find(">,</text>") != std::string::npos);
CHECK(text.find(">(</text>") != std::string::npos);
CHECK(text.find(">S</text>") != std::string::npos);
CHECK(text.find(")</text>") != std::string::npos);
CHECK(text.find(">2</text>") != std::string::npos);
CHECK(text.find(">f</text>") != std::string::npos);
CHECK(text.find(">o</text>") != std::string::npos);
}
}
}
TEST_CASE("Github #3226: Lines in wedge bonds being drawn too closely together",
"[drawing]") {
auto m1 =
"C[C@H](C1=C(C=CC(=C1Cl)F)Cl)OC2=C(N=CC(=C2)C3=CN(N=C3)C4CCNCC4)N"_smiles;
REQUIRE(m1);
SECTION("larger SVG") {
{
MolDraw2DSVG drawer(450, 400);
drawer.drawMolecule(*m1);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
std::ofstream outs("testGithub3226_1.svg");
outs << text;
outs.flush();
std::vector<std::string> tkns;
boost::algorithm::find_all(tkns, text, "bond-0");
CHECK(tkns.size() == 6);
}
}
#ifdef RDK_BUILD_CAIRO_SUPPORT
SECTION("larger PNG") {
{
MolDraw2DCairo drawer(450, 400);
drawer.drawMolecule(*m1);
drawer.finishDrawing();
drawer.writeDrawingText("testGithub3226_1.png");
}
}
#endif
SECTION("smaller SVG") {
{
MolDraw2DSVG drawer(200, 150);
drawer.drawMolecule(*m1);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
std::ofstream outs("testGithub3226_2.svg");
outs << text;
outs.flush();
std::vector<std::string> tkns;
boost::algorithm::find_all(tkns, text, "bond-0");
CHECK(tkns.size() == 4);
}
}
#ifdef RDK_BUILD_CAIRO_SUPPORT
SECTION("smaller PNG") {
{
MolDraw2DCairo drawer(200, 150);
drawer.drawMolecule(*m1);
drawer.finishDrawing();
drawer.writeDrawingText("testGithub3226_2.png");
}
}
#endif
SECTION("middle SVG") {
{
MolDraw2DSVG drawer(250, 200);
drawer.drawMolecule(*m1);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
std::ofstream outs("testGithub3226_3.svg");
outs << text;
outs.flush();
std::vector<std::string> tkns;
boost::algorithm::find_all(tkns, text, "bond-0");
CHECK(tkns.size() == 4);
}
}
#ifdef RDK_BUILD_CAIRO_SUPPORT
SECTION("middle PNG") {
{
MolDraw2DCairo drawer(250, 200);
drawer.drawMolecule(*m1);
drawer.finishDrawing();
drawer.writeDrawingText("testGithub3226_3.png");
}
}
#endif
}
TEST_CASE("github #3258: ", "[drawing][bug]") {
auto m1 = "CCN"_smiles;
REQUIRE(m1);
SECTION("foundations") {
MolDraw2DSVG drawer(500, 200, 250, 200, NO_FREETYPE);
drawer.drawOptions().addAtomIndices = true;
drawer.drawOptions().addBondIndices = true;
RWMol dm1(*m1);
RWMol dm2(*m1);
MOL_PTR_VECT ms{&dm1, &dm2};
drawer.drawMolecules(ms);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
CHECK(text.find(">,</text>") == std::string::npos);
CHECK(!dm1.hasProp("_atomIndicesAdded"));
CHECK(!dm1.hasProp("_bondIndicesAdded"));
}
}
#ifdef RDK_BUILD_CAIRO_SUPPORT
TEST_CASE("adding png metadata", "[drawing][png]") {
SECTION("molecule") {
auto m1 = R"CTAB(
Mrv2014 08172015242D
0 0 0 0 0 999 V3000
M V30 BEGIN CTAB
M V30 COUNTS 3 2 0 0 0
M V30 BEGIN ATOM
M V30 1 C 2.31 -1.3337 0 0
M V30 2 C 3.6437 -2.1037 0 0
M V30 3 O 4.9774 -1.3337 0 0
M V30 END ATOM
M V30 BEGIN BOND
M V30 1 1 1 2
M V30 2 1 2 3
M V30 END BOND
M V30 END CTAB
M END
)CTAB"_ctab;
REQUIRE(m1);
{
MolDraw2DCairo drawer(250, 200);
drawer.drawMolecule(*m1);
drawer.finishDrawing();
auto png = drawer.getDrawingText();
drawer.writeDrawingText("testPNGMetadata_1.png");
CHECK(png.find(PNGData::smilesTag) != std::string::npos);
CHECK(png.find(PNGData::molTag) != std::string::npos);
CHECK(png.find(PNGData::pklTag) != std::string::npos);
std::unique_ptr<ROMol> newmol(PNGStringToMol(png));
REQUIRE(newmol);
CHECK(MolToCXSmiles(*m1) == MolToCXSmiles(*newmol));
}
{ // disable metadata output
MolDraw2DCairo drawer(250, 200);
drawer.drawOptions().includeMetadata = false;
drawer.drawMolecule(*m1);
drawer.finishDrawing();
auto png = drawer.getDrawingText();
CHECK(png.find(PNGData::smilesTag) == std::string::npos);
CHECK(png.find(PNGData::molTag) == std::string::npos);
CHECK(png.find(PNGData::pklTag) == std::string::npos);
}
{ // draw multiple molecules
MolDraw2DCairo drawer(250, 200);
drawer.drawMolecule(*m1);
drawer.drawMolecule(*m1);
drawer.finishDrawing();
auto png = drawer.getDrawingText();
CHECK(png.find(PNGData::smilesTag) != std::string::npos);
CHECK(png.find(PNGData::molTag) != std::string::npos);
CHECK(png.find(PNGData::pklTag) != std::string::npos);
CHECK(png.find(PNGData::smilesTag + "1") != std::string::npos);
CHECK(png.find(PNGData::molTag + "1") != std::string::npos);
CHECK(png.find(PNGData::pklTag + "1") != std::string::npos);
}
}
SECTION("reaction") {
std::unique_ptr<ChemicalReaction> rxn(RxnSmartsToChemicalReaction(
"[N:1][C:2][C:3](=[O:4])[O:5].[N:6][C:7][C:8](=[O:9])[O:10]>>[N:1]1[C:"
"2][C:3](=[O:4])[N:6][C:7][C:8]1=[O:9].[O:5][O:10]"));
REQUIRE(rxn);
{
MolDraw2DCairo drawer(600, 200);
drawer.drawReaction(*rxn);
drawer.finishDrawing();
auto png = drawer.getDrawingText();
drawer.writeDrawingText("testPNGMetadata_2.png");
CHECK(png.find(PNGData::smilesTag) == std::string::npos);
CHECK(png.find(PNGData::molTag) == std::string::npos);
CHECK(png.find(PNGData::pklTag) == std::string::npos);
CHECK(png.find(PNGData::rxnPklTag) != std::string::npos);
CHECK(png.find(PNGData::rxnSmartsTag) != std::string::npos);
std::unique_ptr<ChemicalReaction> rxn2(PNGStringToChemicalReaction(png));
REQUIRE(rxn2);
CHECK(ChemicalReactionToRxnSmarts(*rxn) ==
ChemicalReactionToRxnSmarts(*rxn2));
}
{ // disable metadata
MolDraw2DCairo drawer(600, 200);
drawer.drawOptions().includeMetadata = false;
drawer.drawReaction(*rxn);
drawer.finishDrawing();
auto png = drawer.getDrawingText();
CHECK(png.find(PNGData::smilesTag) == std::string::npos);
CHECK(png.find(PNGData::molTag) == std::string::npos);
CHECK(png.find(PNGData::pklTag) == std::string::npos);
CHECK(png.find(PNGData::rxnPklTag) == std::string::npos);
CHECK(png.find(PNGData::rxnSmartsTag) == std::string::npos);
}
}
}
#endif
TEST_CASE(
"github #3392: prepareMolForDrawing() incorrectly adds chiral Hs if no "
"ring info is present",
"[bug]") {
SECTION("foundations") {
SmilesParserParams ps;
ps.sanitize = false;
ps.removeHs = false;
std::unique_ptr<RWMol> m1(SmilesToMol("C[C@H](F)Cl", ps));
REQUIRE(m1);
m1->updatePropertyCache();
CHECK(m1->getNumAtoms() == 4);
const bool kekulize = false;
const bool addChiralHs = true;
MolDraw2DUtils::prepareMolForDrawing(*m1, kekulize, addChiralHs);
CHECK(m1->getNumAtoms() == 4);
}
}
TEST_CASE(
"github #3369: support new CIP code and StereoGroups in "
"addStereoAnnotation()",
"[chirality]") {
auto m1 =
"C[C@@H]1N[C@H](C)[C@@H]([C@H](C)[C@@H]1C)C1[C@@H](C)O[C@@H](C)[C@@H](C)[C@H]1C/C=C/C |a:5,o1:1,8,o2:14,16,&1:11,18,&2:3,6,r|"_smiles;
REQUIRE(m1);
SECTION("defaults") {
ROMol m2(*m1);
MolDraw2D_detail::addStereoAnnotation(m2);
std::string txt;
CHECK(m2.getAtomWithIdx(5)->getPropIfPresent(common_properties::atomNote,
txt));
CHECK(txt == "abs (S)");
CHECK(m2.getAtomWithIdx(3)->getPropIfPresent(common_properties::atomNote,
txt));
CHECK(txt == "and4");
}
SECTION("including CIP with relative stereo") {
ROMol m2(*m1);
bool includeRelativeCIP = true;
MolDraw2D_detail::addStereoAnnotation(m2, includeRelativeCIP);
std::string txt;
CHECK(m2.getAtomWithIdx(5)->getPropIfPresent(common_properties::atomNote,
txt));
CHECK(txt == "abs (S)");
CHECK(m2.getAtomWithIdx(3)->getPropIfPresent(common_properties::atomNote,
txt));
CHECK(txt == "and4 (R)");
}
SECTION("new CIP labels") {
ROMol m2(*m1);
REQUIRE(m2.getBondBetweenAtoms(20, 21));
m2.getBondBetweenAtoms(20, 21)->setStereo(Bond::BondStereo::STEREOTRANS);
// initially no label is assigned since we have TRANS
MolDraw2D_detail::addStereoAnnotation(m2);
CHECK(
!m2.getBondBetweenAtoms(20, 21)->hasProp(common_properties::bondNote));
CIPLabeler::assignCIPLabels(m2);
std::string txt;
CHECK(m2.getBondBetweenAtoms(20, 21)->getPropIfPresent(
common_properties::_CIPCode, txt));
CHECK(txt == "E");
MolDraw2D_detail::addStereoAnnotation(m2);
CHECK(m2.getBondBetweenAtoms(20, 21)->getPropIfPresent(
common_properties::bondNote, txt));
CHECK(txt == "(E)");
}
SECTION("works with the drawing code") {
MolDraw2DSVG drawer(300, 250);
RWMol dm1(*m1);
bool includeRelativeCIP = true;
MolDraw2D_detail::addStereoAnnotation(dm1, includeRelativeCIP);
drawer.drawMolecule(dm1);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
std::ofstream outs("testGithub3369_1.svg");
outs << text;
outs.flush();
}
}
TEST_CASE("includeRadicals", "[options]") {
SECTION("basics") {
auto m = "[O][C]"_smiles;
REQUIRE(m);
int panelHeight = -1;
int panelWidth = -1;
bool noFreeType = true;
{
MolDraw2DSVG drawer(250, 200, panelWidth, panelHeight, noFreeType);
drawer.drawMolecule(*m);
drawer.finishDrawing();
auto text = drawer.getDrawingText();
std::ofstream outs("testIncludeRadicals_1a.svg");
outs << text;
outs.flush();
CHECK(text.find("<path d='M") != std::string::npos);
}
{
MolDraw2DSVG drawer(250, 200, panelWidth, panelHeight, noFreeType);
drawer.drawOptions().includeRadicals = false;
drawer.drawMolecule(*m);
drawer.finishDrawing();
auto text = drawer.getDrawingText();
std::ofstream outs("testIncludeRadicals_1b.svg");
outs << text;
outs.flush();
CHECK(text.find("<path d='M") == std::string::npos);
}
}
}
TEST_CASE("including legend in drawing results in offset drawing later",
"[bug]") {
SECTION("basics") {
auto m = "c1ccccc1"_smiles;
REQUIRE(m);
MolDraw2DUtils::prepareMolForDrawing(*m);
auto &conf = m->getConformer();
std::vector<Point2D> polyg;
for (const auto &pt : conf.getPositions()) {
polyg.emplace_back(pt);
}
MolDraw2DSVG drawer(350, 300);
drawer.drawMolecule(*m, "molecule legend");
drawer.setFillPolys(true);
drawer.setColour(DrawColour(1.0, 0.3, 1.0));
drawer.drawPolygon(polyg);
drawer.finishDrawing();
auto text = drawer.getDrawingText();
std::ofstream outs("testLegendsAndDrawing-1.svg");
outs << text;
outs.flush();
// make sure the polygon starts at a bond
CHECK(text.find("<path class='bond-0' d='M 321.962,140") !=
std::string::npos);
CHECK(text.find("<path d='M 321.962,140") != std::string::npos);
}
}
TEST_CASE("Github #3577", "[bug]") {
SECTION("basics") {
auto m = "CCC"_smiles;
REQUIRE(m);
MolDraw2DUtils::prepareMolForDrawing(*m);
m->getAtomWithIdx(1)->setProp("atomNote", "CCC");
m->getAtomWithIdx(2)->setProp("atomNote", "ccc");
m->getBondWithIdx(0)->setProp("bondNote", "CCC");
MolDraw2DSVG drawer(350, 300);
drawer.drawMolecule(*m);
drawer.finishDrawing();
auto text = drawer.getDrawingText();
std::ofstream outs("testGithub3577-1.svg");
outs << text;
outs.flush();
}
}
TEST_CASE("Github #3744: Double bonds incorrectly drawn outside the ring",
"[drawing]") {
SECTION("SVG") {
ROMOL_SPTR m1(MolBlockToMol(R"CTAB(
RDKit 2D
6 6 0 0 0 0 0 0 0 0999 V2000
0.0684 -1.2135 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1.4949 -0.7500 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1.4949 0.7500 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.0684 1.2135 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-0.8133 0.0000 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0
-2.3133 -0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1 2 2 0
2 3 1 0
3 4 2 0
4 5 1 0
5 6 1 0
5 1 1 0
M END)CTAB"));
REQUIRE(m1);
MolDraw2DSVG drawer(400, 300);
drawer.drawMolecule(*m1);
drawer.finishDrawing();
std::string text = drawer.getDrawingText();
std::ofstream outs("testGithub3744.svg");
outs << text;
outs.flush();
std::vector<std::string> bond0;
std::vector<std::string> bond2;
std::istringstream ss(text);
std::string line;
while (std::getline(ss, line)) {
if (line.find("bond-0") != std::string::npos) {
bond0.push_back(line);
} else if (line.find("bond-2") != std::string::npos) {
bond2.push_back(line);
}
}
CHECK(bond0.size() == 2);
CHECK(bond2.size() == 2);
std::regex regex(
"^.*d='M\\s+(\\d+\\.\\d+),(\\d+\\.\\d+)\\s+L\\s+(\\d+\\.\\d+),(\\d+\\."
"\\d+)'.*$");
std::smatch bond0OuterMatch;
REQUIRE(std::regex_match(bond0[0], bond0OuterMatch, regex));
REQUIRE(bond0OuterMatch.size() == 5);
std::smatch bond0InnerMatch;
REQUIRE(std::regex_match(bond0[1], bond0InnerMatch, regex));
REQUIRE(bond0InnerMatch.size() == 5);
std::smatch bond2OuterMatch;
REQUIRE(std::regex_match(bond2[0], bond2OuterMatch, regex));
REQUIRE(bond2OuterMatch.size() == 5);
std::smatch bond2InnerMatch;
REQUIRE(std::regex_match(bond2[1], bond2InnerMatch, regex));
REQUIRE(bond2InnerMatch.size() == 5);
RDGeom::Point2D bond0InnerCtd(
RDGeom::Point2D(std::stof(bond0InnerMatch[1]),
std::stof(bond0InnerMatch[2])) +
RDGeom::Point2D(std::stof(bond0InnerMatch[3]),
std::stof(bond0InnerMatch[4])) /
2.0);
RDGeom::Point2D bond0OuterCtd(
RDGeom::Point2D(std::stof(bond0OuterMatch[1]),
std::stof(bond0OuterMatch[2])) +
RDGeom::Point2D(std::stof(bond0OuterMatch[3]),
std::stof(bond0OuterMatch[4])) /
2.0);
RDGeom::Point2D bond2InnerCtd(
RDGeom::Point2D(std::stof(bond2InnerMatch[1]),
std::stof(bond2InnerMatch[2])) +
RDGeom::Point2D(std::stof(bond2InnerMatch[3]),
std::stof(bond2InnerMatch[4])) /
2.0);
RDGeom::Point2D bond2OuterCtd(
RDGeom::Point2D(std::stof(bond2OuterMatch[1]),
std::stof(bond2OuterMatch[2])) +
RDGeom::Point2D(std::stof(bond2OuterMatch[3]),
std::stof(bond2OuterMatch[4])) /
2.0);
// we look at the two double bonds of pyrrole
// we check that the ratio between the distance of the centroids of the
// outer bonds and the distance of the centroids of the inner bonds is at
// least 1.3, otherwise the inner bonds are not actually inside the ring.
float outerBondsDistance = (bond0OuterCtd - bond2OuterCtd).length();
float innerBondsDistance = (bond0InnerCtd - bond2InnerCtd).length();
CHECK(outerBondsDistance / innerBondsDistance > 1.3f);
}
}