Files
rdkit/Code/GraphMol/Descriptors/AUTOCORR3D.cpp
Greg Landrum 71932ee4f9 Add a collection of new 3D descriptors (#1467)
* 3D Descriptors Dragons

* stripped down, not yet working

* get this building on a non C++-11 compiler

* move the python test to the python directory

* move the python test to the python directory

* add the python test

* now at least those tests runn

* warning comment

* some basic refactoring and cleanup

* get python wrapper "working" (completely untested)

* fix Name

* fixing AutoCorr & RDF

* AutoCorr test added
* RDF reviewed based on AutoCorr comments

* fix Morse code

* Morse reviewed

* Correct Morse & start Getaway

* correct MORSE test
* start Getaway clean up

* simplification of Whim

* better

* fix Getaway

* fix RCON

* merge repaired

* adding Dragon 2D autocorrelations descriptors

* fix the 3D autocorrelation descriptors based on the modification in
Dragon.
* Adding the 2D autocorrelation descriptors (no need of Eigen
dependency for this one)
* Adding 2D test case
* IState … no idea for the moment
* there is an error in 2D computation (memory error ???)

* fix the IState for molecules with Hs

* need to use getTotalNumHs(true) not getTotalNumHs()
* also need to remove Hs in both dv and d!

* fixing Rcov values

I fix the Rcov values

* fix Getaway

* remove push_back
* remove call to sum
* improve tests

* fix getaway

* adding precision parameter to GETAWAY
* adding rouding (1e-3)

* fix WHIM

* use void in declarations of function
* update MolDescriptors link

* remove print option in WHIM

* fix python wrapper to 3D descriptors.

- all modifications reduce computation time by a factor of 3!

* final fix for Getaway

* all output are fixed except the 2 first values due to clustering
approach.
* cluster cannot be fixed du tu float precision issue between Java &
c++

* best fix of ITH and ISH

* use the same algorithm as in Dragon 6 but there is still a deviation

* remove std::move

* std:move only works on c++ 11

* fixing issue based on Greg Comments

* auto2D still not working on my env

* update 3d test.py

* auto 2D not working after the first loop test

* tighten up the tests

* change name

* update, but still does not pass

* make this run (though it does not work)

* re-enable test3D

* some cleanup

* add GETAWAY test data. Note that the tests fail

* fix the ATS and ATSC autocorrelation 2D

Broto Moreau and Geary autocorrelation are not correct again a specificity of Dragon to compute them. The result are not consistant with Padel because we use the relative weigth not in Padel.

* one minor change to get things to compile

* fix the M & G matrix computation

fix inversion in the computation of the equations for both M & G matrixes

* update autocorr2d tests

* 192 examples

* fix issue in cluster 0.01 0.009 case

this is not correct all the cases

* update GETAWAY expected values to reflect the fact that we cannot reproduce the literature values exactly
fix a leak in GETAWAY

* fix the negative values in gamma

this is strickly the implementation that we find in the book molecular descriptors for chemoinformatics (except the case where an atom is already in the axis in this case it should be added in the symetric list which is not the case in this implementation)

* Update WHIM.cpp

adding the axis atoms to the symetrical list

* update WHIM tests

* add AUTOCORR2D to MolDescriptors and the python wrappers

* start adding tests

* test the python versions of the new descriptors

* update list of descriptors
2017-06-27 18:57:10 +02:00

194 lines
6.7 KiB
C++

//
// Copyright (c) 2016, Guillaume GODIN.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Institue of Cancer Research.
// nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written
// permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Guillaume GODIN access the AutoCorrelation 3D descriptors names in Dragon TDB
#include <GraphMol/RDKitBase.h>
#include "AUTOCORR3D.h"
#include "MolData3Ddescriptors.h"
#include <math.h>
#include <Eigen/Dense>
#include <Eigen/SVD>
#include <iostream>
#include <Eigen/Core>
#include <Eigen/QR>
using namespace Eigen;
namespace RDKit {
namespace Descriptors {
namespace {
MolData3Ddescriptors moldata3D;
VectorXd getEigenVect(std::vector<double> v) {
double* varray_ptr = &v[0];
Map<VectorXd> V(varray_ptr, v.size());
return V;
}
double* GetGeodesicMatrix(double* dist, int lag, int numAtoms) {
int sizeArray = numAtoms * numAtoms;
double* Geodesic = new double[sizeArray];
for (int i = 0; i < sizeArray; i++) {
if (dist[i] == lag)
Geodesic[i] = 1.0;
else
Geodesic[i] = 0.0;
}
return Geodesic;
}
// matrix prod that mimic the v2 version
// the code is in respect to Dragon 6 descriptors.
// replace the number of "Bicount" vertex per lag by a (numAtoms * (numAtoms -
// 1))!
// provided by Kobe team!
void get3DautocorrelationDesc(double* dist3D, double* topologicaldistance,
int numAtoms, const ROMol& mol,
std::vector<double>& res) {
Map<MatrixXd> dm(dist3D, numAtoms, numAtoms);
Map<MatrixXd> di(topologicaldistance, numAtoms, numAtoms);
std::vector<double> wp = moldata3D.GetRelativePol(mol);
VectorXd Wp = getEigenVect(wp);
std::vector<double> wm = moldata3D.GetRelativeMW(mol);
VectorXd Wm = getEigenVect(wm);
std::vector<double> wi = moldata3D.GetRelativeIonPol(mol);
VectorXd Wi = getEigenVect(wi);
std::vector<double> wv = moldata3D.GetRelativeVdW(mol);
VectorXd Wv = getEigenVect(wv);
std::vector<double> we = moldata3D.GetRelativeENeg(mol);
VectorXd We = getEigenVect(we);
std::vector<double> wu = moldata3D.GetUn(numAtoms);
VectorXd Wu = getEigenVect(wu);
std::vector<double> ws = moldata3D.GetIState(mol);
VectorXd Ws = getEigenVect(ws);
std::vector<double> wr = moldata3D.GetRelativeRcov(mol);
VectorXd Wr = getEigenVect(wr);
MatrixXd Bi;
MatrixXd tmp;
double TDBmat[8][10];
double dtmp;
for (int i = 0; i < 10; i++) {
double* Bimat = GetGeodesicMatrix(topologicaldistance, i + 1, numAtoms);
Map<MatrixXd> Bi(Bimat, numAtoms, numAtoms);
MatrixXd RBi = Bi.cwiseProduct(dm);
// double Bicount = (double)Bi.sum();
tmp = Wu.transpose() * RBi * Wu;
dtmp = (double)tmp(0);
if (std::isnan(dtmp)) dtmp = 0.0;
TDBmat[0][i] = dtmp;
tmp = Wm.transpose() * RBi * Wm;
dtmp = (double)tmp(0);
if (std::isnan(dtmp)) dtmp = 0.0;
TDBmat[1][i] = dtmp;
tmp = Wv.transpose() * RBi * Wv;
dtmp = (double)tmp(0);
if (std::isnan(dtmp)) dtmp = 0.0;
TDBmat[2][i] = dtmp;
tmp = We.transpose() * RBi * We;
dtmp = (double)tmp(0);
if (std::isnan(dtmp)) dtmp = 0.0;
TDBmat[3][i] = dtmp;
tmp = Wp.transpose() * RBi * Wp;
dtmp = (double)tmp(0);
if (std::isnan(dtmp)) dtmp = 0.0;
TDBmat[4][i] = dtmp;
tmp = Wi.transpose() * RBi * Wi;
dtmp = (double)tmp(0);
if (std::isnan(dtmp)) dtmp = 0.0;
TDBmat[5][i] = dtmp;
tmp = Ws.transpose() * RBi * Ws;
dtmp = (double)tmp(0);
if (std::isnan(dtmp)) dtmp = 0.0;
TDBmat[6][i] = dtmp;
tmp = Wr.transpose() * RBi * Wr;
dtmp = (double)tmp(0);
if (std::isnan(dtmp)) dtmp = 0.0;
TDBmat[7][i] = dtmp;
delete[] Bimat;
}
// update the Output vector!
for (unsigned int j = 0; j < 8; ++j) {
for (unsigned int i = 0; i < 10; ++i) {
res[j * 10 + i] =
round(1000 * TDBmat[j][i] / (numAtoms * (numAtoms - 1))) / 1000;
}
}
}
void Get3Dauto(double* dist3D, double* topologicaldistance, int numAtoms,
const ROMol& mol, std::vector<double>& res) {
// AUTOCORRNAMES={"TDB01u","TDB02u","TDB03u","TDB04u","TDB05u","TDB06u","TDB07u","TDB08u","TDB09u","TDB10u","TDB01m","TDB02m","TDB03m","TDB04m","TDB05m","TDB06m","TDB07m","TDB08m","TDB09m","TDB10m","TDB01v","TDB02v","TDB03v","TDB04v","TDB05v","TDB06v","TDB07v","TDB08v","TDB09v","TDB10v","TDB01e","TDB02e","TDB03e","TDB04e","TDB05e","TDB06e","TDB07e","TDB08e","TDB09e","TDB10e","TDB01p","TDB02p","TDB03p","TDB04p","TDB05p","TDB06p","TDB07p","TDB08p","TDB09p","TDB10p","TDB01i","TDB02i","TDB03i","TDB04i","TDB05i","TDB06i","TDB07i","TDB08i","TDB09i","TDB10i","TDB01s","TDB02s","TDB03s","TDB04s","TDB05s","TDB06s","TDB07s","TDB08s","TDB09s","TDB10s","TDB01r","TDB02r","TDB03r","TDB04r","TDB05r","TDB06r","TDB07r","TDB08r","TDB09r","TDB10r"};
get3DautocorrelationDesc(dist3D, topologicaldistance, numAtoms, mol, res);
}
} // end of anonymous namespace
void AUTOCORR3D(const ROMol& mol, std::vector<double>& res, int confId) {
PRECONDITION(mol.getNumConformers() >= 1, "molecule has no conformers")
int numAtoms = mol.getNumAtoms();
const Conformer& conf = mol.getConformer(confId);
double* topologicaldistance =
MolOps::getDistanceMat(mol, false); // topological matrix
double* dist3D =
MolOps::get3DDistanceMat(mol, confId, false, true); // 3D distance matrix
res.clear();
res.resize(80);
Get3Dauto(dist3D, topologicaldistance, numAtoms, mol, res);
}
} // end of Descriptors namespace
} // end of RDKit namespace