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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
// $Id: Embedder.cpp 1952 2012-02-05 07:27:40Z glandrum $
//
// Copyright (C) 2004-2012 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.
//
#include "Embedder.h"
#include <DistGeom/BoundsMatrix.h>
#include <DistGeom/DistGeomUtils.h>
#include <DistGeom/TriangleSmooth.h>
#include "BoundsMatrixBuilder.h"
#include <ForceField/ForceField.h>
#include <GraphMol/ROMol.h>
#include <GraphMol/Atom.h>
#include <GraphMol/AtomIterators.h>
#include <GraphMol/Conformer.h>
#include <RDGeneral/types.h>
#include <RDGeneral/RDLog.h>
#include <Geometry/Transform3D.h>
#include <Numerics/Alignment/AlignPoints.h>
#include <DistGeom/ChiralSet.h>
#include <GraphMol/MolOps.h>
#include <boost/dynamic_bitset.hpp>
#define ERROR_TOL 0.00001
namespace RDKit {
namespace DGeomHelpers {
typedef std::pair<int,int> INT_PAIR;
typedef std::vector<INT_PAIR> INT_PAIR_VECT;
bool _embedPoints(RDGeom::PointPtrVect &positions,
const DistGeom::BoundsMatPtr mmat,
bool useRandomCoords,double boxSizeMult,
bool randNegEig,
unsigned int numZeroFail, double optimizerForceTol,
double basinThresh, int seed, unsigned int maxIterations,
const DistGeom::VECT_CHIRALSET &chiralCenters){
unsigned int nat = positions.size();
if(maxIterations==0){
maxIterations=10*nat;
}
RDNumeric::DoubleSymmMatrix distMat(nat, 0.0);
// The basin threshold just gets us into trouble when we're using
// random coordinates since it ends up ignoring 1-4 (and higher)
// interactions. This causes us to get folded-up (and self-penetrating)
// conformations for large flexible molecules
if(useRandomCoords) basinThresh=1e8;
bool gotCoords = false;
unsigned int iter = 0;
double largestDistance=-1.0;
while ((gotCoords == false) && (iter < maxIterations)) {
++iter;
if(!useRandomCoords){
if (seed > 0) {
largestDistance=DistGeom::pickRandomDistMat(*mmat, distMat, iter*seed);
} else {
largestDistance=DistGeom::pickRandomDistMat(*mmat, distMat);
}
gotCoords = DistGeom::computeInitialCoords(distMat, positions,
randNegEig, numZeroFail);
} else {
double boxSize;
if(boxSizeMult>0){
boxSize=5.*boxSizeMult;
} else {
boxSize=-1*boxSizeMult;
}
if (seed > 0) {
RDKit::rng_type &generator = RDKit::getRandomGenerator();
generator.seed(seed*iter);
}
gotCoords = DistGeom::computeRandomCoords(positions,boxSize);
}
}
if (gotCoords) {
ForceFields::ForceField *field = DistGeom::constructForceField(*mmat, positions,
chiralCenters,
1.0, 0.1,
0,basinThresh);
unsigned int nPasses=0;
field->initialize();
//std::cerr<<"FIELD E: "<<field->calcEnergy()<<std::endl;
if(field->calcEnergy() > ERROR_TOL){
int needMore = 1;
while(needMore){
needMore = field->minimize(200,optimizerForceTol);
//needMore = field->minimize(200,1e-6);
++nPasses;
}
}
//std::cerr<<" "<<field->calcEnergy()<<" after npasses: "<<nPasses<<std::endl;
// now redo the minimization if we have a chiral center, this
// time removing the chiral constraints and
// increasing the weight on the fourth dimension
if (chiralCenters.size()>0 || useRandomCoords) {
ForceFields::ForceField *field2 = DistGeom::constructForceField(*mmat, positions,
chiralCenters,
0.1, 1.0, 0,
basinThresh);
field2->initialize();
//std::cerr<<"FIELD2 E: "<<field2->calcEnergy()<<std::endl;
if(field2->calcEnergy() > ERROR_TOL){
int needMore = 1;
int nPasses2=0;
while(needMore){
needMore = field2->minimize(200,optimizerForceTol);
++nPasses2;
}
//std::cerr<<" "<<field2->calcEnergy()<<" after npasses: "<<nPasses2<<std::endl;
}
delete field2;
}
delete field;
}
return gotCoords;
}
void _findChiralSets(const ROMol &mol, DistGeom::VECT_CHIRALSET &chiralCenters) {
ROMol::ConstAtomIterator ati;
INT_PAIR_VECT nbrs;
ROMol::OEDGE_ITER beg,end;
Atom *oatom;
for (ati = mol.beginAtoms(); ati != mol.endAtoms(); ati++) {
if ((*ati)->getAtomicNum() != 1) { //skip hydrogens
if ((*ati)->hasProp("_CIPCode")) {
// make a chiral set from the neighbors
nbrs.clear();
nbrs.reserve(4);
// find the neighbors of this atom and enter them into the
// nbr list along with their CIPRanks
boost::tie(beg,end) = mol.getAtomBonds(*ati);
while (beg != end) {
oatom = mol[*beg]->getOtherAtom(*ati);
int rank;
oatom->getProp("_CIPRank", rank);
INT_PAIR rAid(rank, oatom->getIdx());
nbrs.push_back(rAid);
++beg;
}
// if we have less than 4 heavy atoms as neighbors,
// we need to include the chiral center into the mix
// we should at least have 3 though
bool includeSelf = false;
CHECK_INVARIANT(nbrs.size() >= 3, "Cannot be a chiral center");
std::sort(nbrs.begin(), nbrs.end());
if (nbrs.size() < 4) {
int rank;
(*ati)->getProp("_CIPRank", rank);
INT_PAIR rAid(rank, (*ati)->getIdx());
nbrs.insert(nbrs.begin(), rAid);
includeSelf = true;
}
// now create a chiral set and set the upper and lower bound on the volume
std::string cipCode;
(*ati)->getProp("_CIPCode", cipCode);
if (cipCode == "S") {
// postive chiral volume
DistGeom::ChiralSet *cset = new DistGeom::ChiralSet(nbrs[0].second,
nbrs[1].second,
nbrs[2].second,
nbrs[3].second,
5.0, 100.0);
DistGeom::ChiralSetPtr cptr(cset);
chiralCenters.push_back(cptr);
} else {
DistGeom::ChiralSet *cset = new DistGeom::ChiralSet(nbrs[0].second,
nbrs[1].second,
nbrs[2].second,
nbrs[3].second,
-100.0, -5.0);
DistGeom::ChiralSetPtr cptr(cset);
chiralCenters.push_back(cptr);
}
} // if block -chirality check
} // if block - heavy atom check
} // for loop over atoms
} // end of _findChiralSets
void _fillAtomPositions(RDGeom::Point3DConstPtrVect &pts, const Conformer &conf) {
unsigned int na = conf.getNumAtoms();
pts.clear();
unsigned int ai;
pts.reserve(na);
for (ai = 0; ai < na; ++ai) {
pts.push_back(&conf.getAtomPos(ai));
}
}
bool _isConfFarFromRest(const ROMol &mol, const Conformer &conf,
double threshold) {
// NOTE: it is tempting to use some triangle inequality to prune
// conformations here but some basic testing has shown very
// little advantage and given that the time for pruning fades in
// comparison to embedding - we will use a simple for loop below
// over all conformation until we find a match
ROMol::ConstConformerIterator confi;
RDGeom::Point3DConstPtrVect refPoints, prbPoints;
_fillAtomPositions(refPoints, conf);
bool res = true;
unsigned int na = conf.getNumAtoms();
double ssrThres = na*threshold*threshold;
RDGeom::Transform3D trans;
double ssr;
for (confi = mol.beginConformers(); confi != mol.endConformers(); confi++) {
_fillAtomPositions(prbPoints, *(*confi));
ssr = RDNumeric::Alignments::AlignPoints(refPoints, prbPoints, trans);
if (ssr < ssrThres) {
res = false;
break;
}
}
return res;
}
int EmbedMolecule(ROMol &mol, unsigned int maxIterations, int seed,
bool clearConfs,
bool useRandomCoords,double boxSizeMult,
bool randNegEig, unsigned int numZeroFail,
const std::map<int,RDGeom::Point3D> *coordMap,
double optimizerForceTol,
bool ignoreSmoothingFailures,
double basinThresh){
INT_VECT confIds;
confIds=EmbedMultipleConfs(mol,1,maxIterations,seed,clearConfs,
useRandomCoords,boxSizeMult,randNegEig,
numZeroFail,-1.0,coordMap,optimizerForceTol,
ignoreSmoothingFailures,basinThresh);
int res;
if(confIds.size()){
res=confIds[0];
} else {
res=-1;
}
return res;
}
void adjustBoundsMatFromCoordMap(DistGeom::BoundsMatPtr mmat,unsigned int nAtoms,
const std::map<int,RDGeom::Point3D> *coordMap){
for(std::map<int,RDGeom::Point3D>::const_iterator iIt=coordMap->begin();
iIt!=coordMap->end();++iIt){
int iIdx=iIt->first;
const RDGeom::Point3D &iPoint=iIt->second;
std::map<int,RDGeom::Point3D>::const_iterator jIt=iIt;
while(++jIt != coordMap->end()){
int jIdx=jIt->first;
const RDGeom::Point3D &jPoint=jIt->second;
double dist=(iPoint-jPoint).length();
mmat->setUpperBound(iIdx,jIdx,dist);
mmat->setLowerBound(iIdx,jIdx,dist);
}
}
}
INT_VECT EmbedMultipleConfs(ROMol &mol, unsigned int numConfs,
unsigned int maxIterations,
int seed, bool clearConfs,
bool useRandomCoords,double boxSizeMult,
bool randNegEig, unsigned int numZeroFail,
double pruneRmsThresh,
const std::map<int,RDGeom::Point3D> *coordMap,
double optimizerForceTol,
bool ignoreSmoothingFailures,
double basinThresh){
INT_VECT fragMapping;
std::vector<ROMOL_SPTR> molFrags=MolOps::getMolFrags(mol,true,&fragMapping);
if(molFrags.size()>1 && coordMap){
BOOST_LOG(rdWarningLog)<<"Constrained conformer generation (via the coordMap argument) does not work with molecules that have multiple fragments."<<std::endl;
coordMap=0;
}
std::vector< Conformer * > confs;
confs.reserve(numConfs);
for(unsigned int i=0;i<numConfs;++i){
confs.push_back(new Conformer(mol.getNumAtoms()));
}
boost::dynamic_bitset<> confsOk(numConfs);
confsOk.set();
if (clearConfs) {
mol.clearConformers();
}
INT_VECT res;
for(unsigned int fragIdx=0;fragIdx<molFrags.size();++fragIdx){
ROMOL_SPTR piece=molFrags[fragIdx];
unsigned int nAtoms = piece->getNumAtoms();
DistGeom::BoundsMatrix *mat = new DistGeom::BoundsMatrix(nAtoms);
DistGeom::BoundsMatPtr mmat(mat);
initBoundsMat(mmat);
setTopolBounds(*piece, mmat, true, false);
if(coordMap){
adjustBoundsMatFromCoordMap(mmat,nAtoms,coordMap);
}
if (!DistGeom::triangleSmoothBounds(mmat)) {
// ok this bound matrix failed to triangle smooth - re-compute the bounds matrix
// without 15 bounds and with VDW scaling
initBoundsMat(mmat);
setTopolBounds(*piece, mmat, false, true);
if(coordMap){
adjustBoundsMatFromCoordMap(mmat,nAtoms,coordMap);
}
// try triangle smoothing again
if (!DistGeom::triangleSmoothBounds(mmat)) {
// ok, we're not going to be able to smooth this,
if(ignoreSmoothingFailures){
// proceed anyway with the more relaxed bounds matrix
initBoundsMat(mmat);
setTopolBounds(*piece, mmat, false, true);
if(coordMap){
adjustBoundsMatFromCoordMap(mmat,nAtoms,coordMap);
}
} else {
BOOST_LOG(rdWarningLog)<<"Could not triangle bounds smooth molecule."<<std::endl;
return res;
}
}
}
#if 0
for(unsigned int li=0;li<piece->getNumAtoms();++li){
for(unsigned int lj=li+1;lj<piece->getNumAtoms();++lj){
std::cerr<<" ("<<li<<","<<lj<<"): "<<mat->getLowerBound(li,lj)<<" -> "<<mat->getUpperBound(li,lj)<<std::endl;
}
}
#endif
// find all the chiral centers in the molecule
DistGeom::VECT_CHIRALSET chiralCenters;
MolOps::assignStereochemistry(*piece);
_findChiralSets(*piece, chiralCenters);
// if we have any chiral centers or are using random coordinates, we will
// first embed the molecule in four dimensions, otherwise we will use 3D
bool fourD = false;
if (useRandomCoords || chiralCenters.size() > 0) {
fourD = true;
}
RDGeom::PointPtrVect positions;
for (unsigned int i = 0; i < nAtoms; ++i) {
if(fourD){
positions.push_back(new RDGeom::PointND(4));
} else {
positions.push_back(new RDGeom::Point3D());
}
}
for (unsigned int ci=0; ci<numConfs; ci++) {
if(!confsOk[ci]){
// if one of the fragments here has already failed, there's no
// sense in embedding this one
continue;
}
bool gotCoords = _embedPoints(positions, mmat,
useRandomCoords,boxSizeMult,
randNegEig, numZeroFail,
optimizerForceTol,
basinThresh, (ci+1)*seed,
maxIterations, chiralCenters);
if (gotCoords) {
Conformer *conf = confs[ci];
unsigned int fragAtomIdx=0;
for (unsigned int i = 0; i < mol.getNumAtoms();++i){
if(fragMapping[i]==static_cast<int>(fragIdx) ){
conf->setAtomPos(i, RDGeom::Point3D((*positions[fragAtomIdx])[0],
(*positions[fragAtomIdx])[1],
(*positions[fragAtomIdx])[2]));
++fragAtomIdx;
}
}
} else {
confsOk[ci]=0;
}
}
for (unsigned int i = 0; i < nAtoms; ++i) {
delete positions[i];
}
}
for(unsigned int ci=0;ci<confs.size();++ci){
Conformer *conf = confs[ci];
if(confsOk[ci]){
// check if we are pruning away conformations and
// a closeby conformation has already been chosen :
if (pruneRmsThresh > 0.0 &&
!_isConfFarFromRest(mol, *conf, pruneRmsThresh)) {
delete conf;
} else {
int confId = (int)mol.addConformer(conf, true);
res.push_back(confId);
}
} else {
delete conf;
}
}
return res;
}
}
}
|