File: rdMolInterchange.cpp

package info (click to toggle)
rdkit 202503.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 220,160 kB
  • sloc: cpp: 399,240; python: 77,453; ansic: 25,517; java: 8,173; javascript: 4,005; sql: 2,389; yacc: 1,565; lex: 1,263; cs: 1,081; makefile: 580; xml: 229; fortran: 183; sh: 105
file content (133 lines) | stat: -rw-r--r-- 4,755 bytes parent folder | download | duplicates (2)
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
//
//  Copyright (C) 2018 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 <RDBoost/python.h>
#include <GraphMol/GraphMol.h>
#include <RDBoost/Wrap.h>

#include <GraphMol/MolInterchange/MolInterchange.h>

namespace python = boost::python;

namespace {
python::tuple JSONToMols(const std::string &jsonBlock,
                         python::object pyparams) {
  RDKit::MolInterchange::JSONParseParameters params;
  if (pyparams) {
    params =
        python::extract<RDKit::MolInterchange::JSONParseParameters>(pyparams);
  } else {
    params = RDKit::MolInterchange::defaultJSONParseParameters;
  }
  auto mols = RDKit::MolInterchange::JSONDataToMols(jsonBlock, params);
  python::list result;
  for (auto &mol : mols) {
    result.append(mol);
  }
  return python::tuple(result);
}

std::string MolsToJSON(const python::object &mols,
                       const python::object pyparams) {
  auto pymols = pythonObjectToVect<const RDKit::ROMol *>(mols);
  if (!pymols) {
    return "";
  }
  RDKit::MolInterchange::JSONWriteParameters params =
      RDKit::MolInterchange::defaultJSONWriteParameters;
  if (pyparams) {
    params =
        python::extract<RDKit::MolInterchange::JSONWriteParameters>(pyparams);
  }
  return RDKit::MolInterchange::MolsToJSONData(*pymols, params);
}
std::string MolToJSON(const RDKit::ROMol &mol, const python::object pyparams) {
  RDKit::MolInterchange::JSONWriteParameters params =
      RDKit::MolInterchange::defaultJSONWriteParameters;
  if (pyparams) {
    params =
        python::extract<RDKit::MolInterchange::JSONWriteParameters>(pyparams);
  }
  return RDKit::MolInterchange::MolToJSONData(mol, params);
}

}  // namespace

BOOST_PYTHON_MODULE(rdMolInterchange) {
  python::scope().attr("__doc__") =
      "Module containing functions for interchange of molecules.\n"
      "Note that this should be considered beta and that the format\n"
      "  and API will very likely change in future releases.";

  python::class_<RDKit::MolInterchange::JSONParseParameters,
                 boost::noncopyable>("JSONParseParameters",
                                     "Parameters controlling the JSON parser")
      .def_readwrite(
          "setAromaticBonds",
          &RDKit::MolInterchange::JSONParseParameters::setAromaticBonds,
          "set bond types to aromatic for bonds flagged aromatic")
      .def_readwrite(
          "strictValenceCheck",
          &RDKit::MolInterchange::JSONParseParameters::strictValenceCheck,
          "be strict when checking atom valences")
      .def_readwrite(
          "parseConformers",
          &RDKit::MolInterchange::JSONParseParameters::parseConformers,
          "parse conformers in the JSON")
      .def_readwrite(
          "parseProperties",
          &RDKit::MolInterchange::JSONParseParameters::parseProperties,
          "parse molecular properties in the JSON")
      .def_readwrite("useHCounts",
                     &RDKit::MolInterchange::JSONParseParameters::useHCounts,
                     "use atomic H counts from the JSON. You may want to set "
                     "this to False when parsing queries.");

  python::class_<RDKit::MolInterchange::JSONWriteParameters,
                 boost::noncopyable>("JSONWriteParameters",
                                     "Parameters controlling the JSON writer")
      .def_readwrite(
          "useRDKitExtensions",
          &RDKit::MolInterchange::JSONWriteParameters::useRDKitExtensions,
          "use RDKit extensions to the commonchem format");

  std::string docString;
  docString =
      "Convert a single molecule to JSON\n\
\n\
    ARGUMENTS:\n\
      - mol: the molecule to work with\n\
    RETURNS:\n\
      a string\n";
  python::def("MolToJSON", MolToJSON,
              (python::arg("mol"), python::arg("params") = python::object()),
              docString.c_str());
  docString =
      "Convert a set of molecules to JSON\n\
\n\
    ARGUMENTS:\n\
      - mols: the molecules to work with\n\
    RETURNS:\n\
      a string\n";
  python::def("MolsToJSON", MolsToJSON,
              (python::arg("mols"), python::arg("params") = python::object()),
              docString.c_str());
  docString =
      "Convert JSON to a tuple of molecules\n\
\n\
    ARGUMENTS:\n\
      - jsonBlock: the molecule to work with\n\
      - params: (optional) JSONParseParameters controlling the JSON parsing\n\
    RETURNS:\n\
      a tuple of Mols\n";
  python::def(
      "JSONToMols", JSONToMols,
      (python::arg("jsonBlock"), python::arg("params") = python::object()),
      docString.c_str());
}