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
|
//
// Copyright (C) 2016 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 <GraphMol/RDKitBase.h>
#include <GraphMol/MolTransforms/MolTransforms.h>
#include <Geometry/point.h>
#include "PMI.h"
#include <Eigen/Dense>
namespace RDKit {
namespace Descriptors {
namespace {
bool getMoments(const ROMol& mol, int confId, bool useAtomicMasses, double& pm1,
double& pm2, double& pm3, bool force) {
PRECONDITION(mol.getNumConformers() >= 1, "molecule has no conformers");
const char* pn1 = useAtomicMasses ? "_PMI1_mass" : "_PMI1";
const char* pn2 = useAtomicMasses ? "_PMI2_mass" : "_PMI2";
const char* pn3 = useAtomicMasses ? "_PMI3_mass" : "_PMI3";
if (!force && mol.hasProp(pn1) && mol.hasProp(pn2) && mol.hasProp(pn3)) {
mol.getProp(pn1, pm1);
mol.getProp(pn2, pm2);
mol.getProp(pn3, pm3);
return true;
}
const Conformer& conf = mol.getConformer(confId);
Eigen::Matrix3d axes;
Eigen::Vector3d moments;
bool res;
bool ignoreHs = false;
if (useAtomicMasses) {
std::vector<double> weights;
weights.resize(mol.getNumAtoms());
for (ROMol::ConstAtomIterator cai = mol.beginAtoms(); cai != mol.endAtoms();
++cai) {
weights[(*cai)->getIdx()] = (*cai)->getMass();
}
res = MolTransforms::computePrincipalAxesAndMoments(
conf, axes, moments, ignoreHs, false, &weights);
} else {
res = MolTransforms::computePrincipalAxesAndMoments(conf, axes, moments,
ignoreHs);
}
if (res) {
pm1 = moments(0);
pm2 = moments(1);
pm3 = moments(2);
mol.setProp(pn1, pm1, true);
mol.setProp(pn2, pm2, true);
mol.setProp(pn3, pm3, true);
}
return res;
}
bool getMomentsFromGyration(const ROMol& mol, int confId, bool useAtomicMasses,
double& pm1, double& pm2, double& pm3, bool force) {
PRECONDITION(mol.getNumConformers() >= 1, "molecule has no conformers");
const char* pn1 = useAtomicMasses ? "_PMI1_mass_cov" : "_PMI1_cov";
const char* pn2 = useAtomicMasses ? "_PMI2_mass_cov" : "_PMI2_cov";
const char* pn3 = useAtomicMasses ? "_PMI3_mass_cov" : "_PMI3_cov";
if (!force && mol.hasProp(pn1) && mol.hasProp(pn2) && mol.hasProp(pn3)) {
mol.getProp(pn1, pm1);
mol.getProp(pn2, pm2);
mol.getProp(pn3, pm3);
return true;
}
const Conformer& conf = mol.getConformer(confId);
Eigen::Matrix3d axes;
Eigen::Vector3d moments;
bool res;
bool ignoreHs = false;
if (useAtomicMasses) {
std::vector<double> weights;
weights.resize(mol.getNumAtoms());
for (ROMol::ConstAtomIterator cai = mol.beginAtoms(); cai != mol.endAtoms();
++cai) {
weights[(*cai)->getIdx()] = (*cai)->getMass();
}
res = MolTransforms::computePrincipalAxesAndMomentsFromGyrationMatrix(
conf, axes, moments, ignoreHs, false, &weights);
} else {
res = MolTransforms::computePrincipalAxesAndMomentsFromGyrationMatrix(
conf, axes, moments, ignoreHs);
}
if (res) {
pm1 = moments(0);
pm2 = moments(1);
pm3 = moments(2);
mol.setProp(pn1, pm1, true);
mol.setProp(pn2, pm2, true);
mol.setProp(pn3, pm3, true);
}
return res;
}
} // end of anonymous namespace
double NPR1(const ROMol& mol, int confId, bool useAtomicMasses, bool force) {
PRECONDITION(mol.getNumConformers() >= 1, "molecule has no conformers");
double pm1, pm2, pm3;
if (!getMoments(mol, confId, useAtomicMasses, pm1, pm2, pm3, force)) {
// the eigenvector calculation failed
return 0.0; // FIX: throw an exception here?
}
if (pm3 < 1e-8) return 0.0;
return pm1 / pm3;
}
double NPR2(const ROMol& mol, int confId, bool useAtomicMasses, bool force) {
PRECONDITION(mol.getNumConformers() >= 1, "molecule has no conformers");
double pm1, pm2, pm3;
if (!getMoments(mol, confId, useAtomicMasses, pm1, pm2, pm3, force)) {
// the eigenvector calculation failed
return 0.0; // FIX: throw an exception here?
}
if (pm3 < 1e-8) return 0.0;
return pm2 / pm3;
}
double PMI1(const ROMol& mol, int confId, bool useAtomicMasses, bool force) {
PRECONDITION(mol.getNumConformers() >= 1, "molecule has no conformers");
double pm1, pm2, pm3;
if (!getMoments(mol, confId, useAtomicMasses, pm1, pm2, pm3, force)) {
// the eigenvector calculation failed
return 0.0; // FIX: throw an exception here?
}
return pm1;
}
double PMI2(const ROMol& mol, int confId, bool useAtomicMasses, bool force) {
PRECONDITION(mol.getNumConformers() >= 1, "molecule has no conformers");
double pm1, pm2, pm3;
if (!getMoments(mol, confId, useAtomicMasses, pm1, pm2, pm3, force)) {
// the eigenvector calculation failed
return 0.0; // FIX: throw an exception here?
}
return pm2;
}
double PMI3(const ROMol& mol, int confId, bool useAtomicMasses, bool force) {
PRECONDITION(mol.getNumConformers() >= 1, "molecule has no conformers");
double pm1, pm2, pm3;
if (!getMoments(mol, confId, useAtomicMasses, pm1, pm2, pm3, force)) {
// the eigenvector calculation failed
return 0.0; // FIX: throw an exception here?
}
return pm3;
}
double radiusOfGyration(const ROMol& mol, int confId, bool useAtomicMasses,
bool force) {
PRECONDITION(mol.getNumConformers() >= 1, "molecule has no conformers");
double pm1, pm2, pm3;
if (!getMomentsFromGyration(mol, confId, useAtomicMasses, pm1, pm2, pm3,
force)) {
// the eigenvector calculation failed
return 0.0; // FIX: throw an exception here?
}
return sqrt(pm1 + pm2 + pm3);
}
double inertialShapeFactor(const ROMol& mol, int confId, bool useAtomicMasses,
bool force) {
PRECONDITION(mol.getNumConformers() >= 1, "molecule has no conformers");
double pm1, pm2, pm3;
if (!getMoments(mol, confId, useAtomicMasses, pm1, pm2, pm3, force)) {
// the eigenvector calculation failed
return 0.0; // FIX: throw an exception here?
}
if (pm1 < 1e-4 || pm3 < 1e-4) {
// planar or no coordinates
return 0.0;
} else {
return pm2 / (pm1 * pm3);
}
}
double eccentricity(const ROMol& mol, int confId, bool useAtomicMasses,
bool force) {
PRECONDITION(mol.getNumConformers() >= 1, "molecule has no conformers");
double pm1, pm2, pm3;
if (!getMoments(mol, confId, useAtomicMasses, pm1, pm2, pm3, force)) {
// the eigenvector calculation failed
return 0.0; // FIX: throw an exception here?
}
if (pm3 < 1e-4) {
// no coordinates
return 0.0;
} else {
return sqrt(pm3 * pm3 - pm1 * pm1) / pm3;
}
}
double asphericity(const ROMol& mol, int confId, bool useAtomicMasses,
bool force) {
PRECONDITION(mol.getNumConformers() >= 1, "molecule has no conformers");
double pm1, pm2, pm3;
if (!getMomentsFromGyration(mol, confId, useAtomicMasses, pm1, pm2, pm3,
force)) {
// the eigenvector calculation failed
return 0.0; // FIX: throw an exception here?
}
if (pm3 < 1e-4) {
// no coordinates
return 0.0;
} else {
double denom = pm1 + pm2 + pm3;
return 0.5 * (pow(pm1 - pm2, 2) + pow(pm1 - pm3, 2) + pow(pm2 - pm3, 2)) /
(denom * denom);
}
}
double spherocityIndex(const ROMol& mol, int confId, bool force) {
PRECONDITION(mol.getNumConformers() >= 1, "molecule has no conformers");
bool useAtomicMasses = false;
double pm1, pm2, pm3;
if (!getMomentsFromGyration(mol, confId, useAtomicMasses, pm1, pm2, pm3,
force)) {
// the eigenvector calculation failed
return 0.0; // FIX: throw an exception here?
}
if (pm3 < 1e-4) {
// no coordinates
return 0.0;
} else {
return 3. * pm1 / (pm1 + pm2 + pm3);
}
}
} // namespace Descriptors
} // namespace RDKit
|