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
|
// $Id$
//
// Copyright (C) 2005-2008 Greg Landrum and Rational Discovery LLC
//
// @@ 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 rdmoltransforms_array_API
#include <RDBoost/python.h>
#include <RDBoost/import_array.h>
#include "numpy/arrayobject.h"
#include <GraphMol/ROMol.h>
#include <RDBoost/Wrap.h>
#include <GraphMol/Conformer.h>
#include <GraphMol/MolTransforms/MolTransforms.h>
#include <Geometry/Transform3D.h>
#include <Geometry/point.h>
namespace python = boost::python;
namespace RDKit {
PyObject *computeCanonTrans(const Conformer &conf,
const RDGeom::Point3D *center = nullptr,
bool normalizeCovar = false, bool ignoreHs = true) {
RDGeom::Transform3D *trans;
trans = MolTransforms::computeCanonicalTransform(conf, center, normalizeCovar,
ignoreHs);
npy_intp dims[2];
dims[0] = 4;
dims[1] = 4;
PyArrayObject *res = (PyArrayObject *)PyArray_SimpleNew(2, dims, NPY_DOUBLE);
double *resData = reinterpret_cast<double *>(PyArray_DATA(res));
const double *tdata = trans->getData();
memcpy(static_cast<void *>(resData), static_cast<const void *>(tdata),
4 * 4 * sizeof(double));
delete trans;
return PyArray_Return(res);
}
#ifdef RDK_HAS_EIGEN3
PyObject *computePrincAxesMomentsHelper(bool func(const Conformer &,
Eigen::Matrix3d &, Eigen::Vector3d &,
bool, bool, const std::vector<double> *),
const Conformer &conf,
bool ignoreHs, const python::object &weights) {
Eigen::Matrix3d axes;
Eigen::Vector3d moments;
std::vector<double> *weightsVecPtr = NULL;
std::vector<double> weightsVec;
size_t i;
if (weights != python::object()) {
size_t numElements = python::extract<int>(weights.attr("__len__")());
if (numElements != conf.getNumAtoms())
throw ValueErrorException("The Python container must have length equal to conf.GetNumAtoms()");
weightsVec.resize(numElements);
for (i = 0; i < numElements; ++i)
weightsVec[i] = python::extract<double>(weights[i]);
weightsVecPtr = &weightsVec;
}
PyObject *res = PyTuple_New(2);
bool success = func(conf, axes, moments, ignoreHs, true, weightsVecPtr);
if (success) {
npy_intp dims[2];
dims[0] = 3;
dims[1] = 3;
PyArrayObject *axesNpy = (PyArrayObject *)PyArray_SimpleNew(2, dims, NPY_DOUBLE);
double *axesNpyData = reinterpret_cast<double *>(PyArray_DATA(axesNpy));
i = 0;
for (size_t y = 0; y < 3; ++y) {
for (size_t x = 0; x < 3; ++x)
axesNpyData[i++] = axes(y, x);
}
PyArrayObject *momentsNpy = (PyArrayObject *)PyArray_SimpleNew(1, dims, NPY_DOUBLE);
double *momentsNpyData = reinterpret_cast<double *>(PyArray_DATA(momentsNpy));
for (size_t y = 0; y < 3; ++y)
momentsNpyData[y] = moments(y);
res = PyTuple_New(2);
PyTuple_SetItem(res, 0, (PyObject *)axesNpy);
PyTuple_SetItem(res, 1, (PyObject *)momentsNpy);
}
else {
PyTuple_SetItem(res, 0, Py_None);
PyTuple_SetItem(res, 1, Py_None);
}
return res;
}
PyObject *computePrincAxesMoments(const Conformer &conf,
bool ignoreHs, const python::object &weights) {
return computePrincAxesMomentsHelper(
MolTransforms::computePrincipalAxesAndMoments, conf, ignoreHs, weights);
}
PyObject *computePrincAxesMomentsFromGyrationMatrix(const Conformer &conf,
bool ignoreHs, const python::object &weights) {
return computePrincAxesMomentsHelper(
MolTransforms::computePrincipalAxesAndMomentsFromGyrationMatrix, conf, ignoreHs, weights);
}
#endif
void transConformer(Conformer &conf, python::object trans) {
PyObject *transObj = trans.ptr();
if (!PyArray_Check(transObj)) {
throw_value_error("Expecting a numeric array for transformation");
}
PyArrayObject *transMat = reinterpret_cast<PyArrayObject *>(transObj);
unsigned int nrows = PyArray_DIM(transMat, 0);
unsigned int dSize = nrows * nrows;
double *inData = reinterpret_cast<double *>(PyArray_DATA(transMat));
RDGeom::Transform3D transform;
double *tData = transform.getData();
memcpy(static_cast<void *>(tData), static_cast<void *>(inData),
dSize * sizeof(double));
MolTransforms::transformConformer(conf, transform);
}
}
BOOST_PYTHON_MODULE(rdMolTransforms) {
python::scope().attr("__doc__") =
"Module containing functions to perform 3D operations like rotate and "
"translate conformations";
rdkit_import_array();
std::string docString =
"Compute the centroid of the conformation - hydrogens are ignored and no attention\n\
if paid to the difference in sizes of the heavy atoms\n";
python::def("ComputeCentroid", MolTransforms::computeCentroid,
(python::arg("conf"), python::arg("ignoreHs") = true),
docString.c_str());
docString =
"Compute the transformation required aligna conformer so that\n\
the the principal axes align up with the x,y, z axes\n\
The conformer itself is left unchanged\n\
ARGUMENTS:\n\
- conf : the conformer of interest\n\
- center : optional center point to compute the principal axes around (defaults to the centroid)\n\
- normalizeCovar : optionally normalize the covariance matrix by the number of atoms\n";
python::def(
"ComputeCanonicalTransform", RDKit::computeCanonTrans,
(python::arg("conf"), python::arg("center") = (RDGeom::Point3D *)nullptr,
python::arg("normalizeCovar") = false, python::arg("ignoreHs") = true),
docString.c_str());
#ifdef RDK_HAS_EIGEN3
docString =
"Compute principal axes and moments of inertia for a conformer\n\
These values are calculated from the inertia tensor:\n\
Iij = - sum_{s=1..N}(w_s * r_{si} * r_{sj}) i != j\n\
Iii = sum_{s=1..N} sum_{j!=i} (w_s * r_{sj} * r_{sj})\n\
where the coordinates are relative to the center of mass.\n\
\n\
ARGUMENTS:\n\
- conf : the conformer of interest\n\
- ignoreHs : if True, ignore hydrogen atoms\n\
- weights : if present, used to weight the atomic coordinates\n\n\
Returns a (principal axes, principal moments) tuple\n";
python::def(
"ComputePrincipalAxesAndMoments", RDKit::computePrincAxesMoments,
(python::arg("conf"), python::arg("ignoreHs") = true,
python::arg("weights") = python::object()),
docString.c_str());
docString =
"Compute principal axes and moments from the gyration matrix of a conformer\n\
These values are calculated from the gyration matrix/tensor:\n\
Iij = sum_{s=1..N}(w_s * r_{si} * r_{sj}) i != j\n\
Iii = sum_{s=1..N} sum_{t!=s}(w_s * r_{si} * r_{ti})\n\
where the coordinates are relative to the center of mass.\n\
\n\
ARGUMENTS:\n\
- conf : the conformer of interest\n\
- ignoreHs : if True, ignore hydrogen atoms\n\
- weights : if present, used to weight the atomic coordinates\n\n\
Returns a (principal axes, principal moments) tuple\n";
python::def(
"ComputePrincipalAxesAndMomentsFromGyrationMatrix", RDKit::computePrincAxesMomentsFromGyrationMatrix,
(python::arg("conf"), python::arg("ignoreHs") = true,
python::arg("weights") = python::object()),
docString.c_str());
#endif
python::def("TransformConformer", RDKit::transConformer,
"Transform the coordinates of a conformer");
docString =
"Canonicalize the orientation of a conformer so that its principal axes\n\
around the specified center point coincide with the x, y, z axes\n\
\n\
ARGUMENTS:\n\
- conf : conformer of interest \n\
- center : optionally center point about which the principal axes are computed \n\
if not specified the centroid of the conformer will be used\n\
- normalizeCovar : Optionally normalize the covariance matrix by the number of atoms\n";
python::def(
"CanonicalizeConformer", MolTransforms::canonicalizeConformer,
(python::arg("conf"), python::arg("center") = (RDGeom::Point3D *)nullptr,
python::arg("normalizeCovar") = false, python::arg("ignoreHs") = true),
docString.c_str());
python::def("CanonicalizeMol", MolTransforms::canonicalizeMol,
(python::arg("mol"), python::arg("normalizeCovar") = false,
python::arg("ignoreHs") = true),
"Loop over the conformers in a molecule and canonicalize their "
"orientation");
python::def(
"GetBondLength", &MolTransforms::getBondLength,
(python::arg("conf"), python::arg("iAtomId"), python::arg("jAtomId")),
"Returns the bond length in angstrom between atoms i, j\n");
python::def("SetBondLength", &MolTransforms::setBondLength,
(python::arg("conf"), python::arg("iAtomId"),
python::arg("jAtomId"), python::arg("value")),
"Sets the bond length in angstrom between atoms i, j; "
"all atoms bonded to atom j are moved\n");
python::def("GetAngleRad", &MolTransforms::getAngleRad,
(python::arg("conf"), python::arg("iAtomId"),
python::arg("jAtomId"), python::arg("kAtomId")),
"Returns the angle in radians between atoms i, j, k\n");
python::def("GetAngleDeg", &MolTransforms::getAngleDeg,
(python::arg("conf"), python::arg("iAtomId"),
python::arg("jAtomId"), python::arg("kAtomId")),
"Returns the angle in degrees between atoms i, j, k\n");
python::def(
"SetAngleRad", &MolTransforms::setAngleRad,
(python::arg("conf"), python::arg("iAtomId"), python::arg("jAtomId"),
python::arg("kAtomId"), python::arg("value")),
"Sets the angle in radians between atoms i, j, k; "
"all atoms bonded to atom k are moved\n");
python::def(
"SetAngleDeg", &MolTransforms::setAngleDeg,
(python::arg("conf"), python::arg("iAtomId"), python::arg("jAtomId"),
python::arg("kAtomId"), python::arg("value")),
"Sets the angle in degrees between atoms i, j, k; "
"all atoms bonded to atom k are moved\n");
python::def(
"GetDihedralRad", &MolTransforms::getDihedralRad,
(python::arg("conf"), python::arg("iAtomId"), python::arg("jAtomId"),
python::arg("kAtomId"), python::arg("lAtomId")),
"Returns the dihedral angle in radians between atoms i, j, k, l\n");
python::def(
"GetDihedralDeg", &MolTransforms::getDihedralDeg,
(python::arg("conf"), python::arg("iAtomId"), python::arg("jAtomId"),
python::arg("kAtomId"), python::arg("lAtomId")),
"Returns the dihedral angle in degrees between atoms i, j, k, l\n");
python::def(
"SetDihedralRad", &MolTransforms::setDihedralRad,
(python::arg("conf"), python::arg("iAtomId"), python::arg("jAtomId"),
python::arg("kAtomId"), python::arg("lAtomId"), python::arg("value")),
"Sets the dihedral angle in radians between atoms i, j, k, l; "
"all atoms bonded to atom l are moved\n");
python::def("SetDihedralDeg", &MolTransforms::setDihedralDeg,
"Sets the dihedral angle in degrees between atoms i, j, k, l; "
"all atoms bonded to atom l are moved\n");
}
|