1 //
2 //  Copyright (C) 2017 Greg Landrum
3 //
4 //   @@ All Rights Reserved @@
5 //  This file is part of the RDKit.
6 //  The contents are covered by the terms of the BSD license
7 //  which is included in the file license.txt, found at the root
8 //  of the RDKit source tree.
9 //
10 
11 #define NO_IMPORT_ARRAY
12 #include <RDBoost/python.h>
13 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
14 #include <numpy/arrayobject.h>
15 #include <boost/python/list.hpp>
16 #include <RDGeneral/Exceptions.h>
17 #include <GraphMol/RDKitBase.h>
18 #include <CoordGen/CoordGen.h>
19 
20 namespace python = boost::python;
21 
22 namespace RDKit {
23 
24 namespace {
SetCoordMap(CoordGen::CoordGenParams * self,python::dict & coordMap)25 void SetCoordMap(CoordGen::CoordGenParams *self, python::dict &coordMap) {
26   self->coordMap.clear();
27   python::list ks = coordMap.keys();
28   for (unsigned int i = 0;
29        i < python::extract<unsigned int>(ks.attr("__len__")()); i++) {
30     unsigned int id = python::extract<unsigned int>(ks[i]);
31     self->coordMap[id] = python::extract<RDGeom::Point2D>(coordMap[id]);
32   }
33 }
addCoordsHelper(ROMol & mol,python::object & params)34 void addCoordsHelper(ROMol &mol, python::object &params) {
35   CoordGen::CoordGenParams *ps = nullptr;
36   if (params != python::object()) {
37     ps = python::extract<CoordGen::CoordGenParams *>(params);
38   }
39   CoordGen::addCoords(mol, ps);
40 }
SetTemplateMol(CoordGen::CoordGenParams * self,const ROMol * templ)41 void SetTemplateMol(CoordGen::CoordGenParams *self, const ROMol *templ) {
42   self->templateMol = templ;
43 }
SetDefaultTemplateFileDir(const std::string & dir)44 void SetDefaultTemplateFileDir(const std::string &dir) {
45   CoordGen::defaultParams.templateFileDir = dir;
46 }
47 }  // end of anonymous namespace
48 
49 struct coordgen_wrapper {
wrapRDKit::coordgen_wrapper50   static void wrap() {
51     std::string docString = "";
52 
53     python::class_<CoordGen::CoordGenParams>(
54         "CoordGenParams", "Parameters controlling coordinate generation")
55         .def(
56             "SetCoordMap", SetCoordMap,
57             "expects a dictionary of Point2D objects with template coordinates")
58         .def("SetTemplateMol", SetTemplateMol,
59              python::with_custodian_and_ward<1, 2>(),
60              "sets a molecule to be used as the template")
61         .def_readwrite("coordgenScaling",
62                        &CoordGen::CoordGenParams::coordgenScaling,
63                        "scaling factor for a single bond")
64         .def_readwrite("dbg_useConstrained",
65                        &CoordGen::CoordGenParams::dbg_useConstrained,
66                        "for debugging use")
67         .def_readwrite("dbg_useFixed", &CoordGen::CoordGenParams::dbg_useFixed,
68                        "for debugging use")
69         .def_readwrite("templateFileDir",
70                        &CoordGen::CoordGenParams::templateFileDir,
71                        "directory containing the templates.mae file")
72         .def_readwrite("minimizeOnly", &CoordGen::CoordGenParams::minimizeOnly,
73                        "uses coordgen's force field to cleanup the 2D "
74                        "coordinates of the active conformation")
75         .def_readonly("sketcherBestPrecision",
76                       &CoordGen::CoordGenParams::sketcherBestPrecision,
77                       "highest quality (and slowest) precision setting")
78         .def_readonly("sketcherStandardPrecision",
79                       &CoordGen::CoordGenParams::sketcherStandardPrecision,
80                       "standard quality precision setting, the default for the "
81                       "coordgen project")
82         .def_readonly("sketcherQuickPrecision",
83                       &CoordGen::CoordGenParams::sketcherQuickPrecision,
84                       "faster precision setting")
85         .def_readonly(
86             "sketcherCoarsePrecision",
87             &CoordGen::CoordGenParams::sketcherCoarsePrecision,
88             "\"coarse\" (fastest) precision setting, produces good-quality "
89             "coordinates"
90             " most of the time, this is the default setting for the RDKit")
91         .def_readwrite("minimizerPrecision",
92                        &CoordGen::CoordGenParams::minimizerPrecision,
93                        "controls sketcher precision")
94         .def_readwrite(
95             "treatNonterminalBondsToMetalAsZOBs",
96             &CoordGen::CoordGenParams::treatNonterminalBondsToMetalAsZeroOrder);
97     python::def("SetDefaultTemplateFileDir", SetDefaultTemplateFileDir);
98     docString =
99         "Add 2D coordinates.\n"
100         "ARGUMENTS:\n"
101         "   - mol: molecule to modify\n"
102         "   - params: (optional) parameters controlling the coordinate "
103         "generation\n"
104         "\n";
105     python::def("AddCoords", addCoordsHelper,
106                 (python::arg("mol"), python::arg("params") = python::object()),
107                 docString.c_str());
108   }
109 };
110 
111 }  // end of namespace RDKit
BOOST_PYTHON_MODULE(rdCoordGen)112 BOOST_PYTHON_MODULE(rdCoordGen) {
113   python::scope().attr("__doc__") =
114       "Module containing interface to the CoordGen library.";
115   RDKit::coordgen_wrapper::wrap();
116 }
117