RDKit
Open-source cheminformatics and machine learning.
MMFF.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2015 Greg Landrum
3 //
4 // @@ All Rights Reserved @@
5 // This file is part of the RDKit.
6 // The contents are covered by the terms of the BSD license
7 // which is included in the file license.txt, found at the root
8 // of the RDKit source tree.
9 //
10 #ifndef RD_MMFFCONVENIENCE_H
11 #define RD_MMFFCONVENIENCE_H
12 #include <ForceField/ForceField.h>
13 #include "Builder.h"
14 
15 #ifdef RDK_THREADSAFE_SSS
16 #include <boost/thread.hpp>
17 #endif
18 
19 namespace RDKit {
20  class ROMol;
21  namespace MMFF {
22  //! Convenience function for optimizing a molecule using MMFF
23  /*
24  \param mol the molecule to use
25  \param maxIters the maximum number of force-field iterations
26  \param mmffVariant the MMFF variant to use, should be "MMFF94" or "MMFF94S"
27  \param nonBondedThresh the threshold to be used in adding non-bonded terms
28  to the force field. Any non-bonded contact whose current
29  distance is greater than \c nonBondedThresh * the minimum value
30  for that contact will not be included.
31  \param confId the optional conformer id, if this isn't provided, the molecule's
32  default confId will be used.
33  \param ignoreInterfragInteractions if true, nonbonded terms will not be added between
34  fragments
35 
36  \return a pair with:
37  first: -1 if parameters were missing, 0 if the optimization converged, 1 if more iterations are required.
38  second: the energy
39  */
40  std::pair<int,double> MMFFOptimizeMolecule(ROMol &mol, int maxIters=1000,
41  std::string mmffVariant="MMFF94",
42  double nonBondedThresh=10.0, int confId=-1,
43  bool ignoreInterfragInteractions=true ){
44  int res = -1;
45  double e = -1;
46  MMFF::MMFFMolProperties mmffMolProperties(mol, mmffVariant);
47  if (mmffMolProperties.isValid()) {
48  ForceFields::ForceField *ff=MMFF::constructForceField(mol,nonBondedThresh, confId,
49  ignoreInterfragInteractions);
50  ff->initialize();
51  res=ff->minimize(maxIters);
52  e=ff->calcEnergy();
53  delete ff;
54  }
55  return std::make_pair(res,e);
56  }
57 #ifdef RDK_THREADSAFE_SSS
58  namespace detail {
59  void MMFFOptimizeMoleculeConfsHelper_(ForceFields::ForceField ff,
60  ROMol *mol,
61  std::vector< std::pair<int, double> > *res,
62  unsigned int threadIdx,
63  unsigned int numThreads,
64  int maxIters){
65  unsigned int i=0;
66  ff.positions().resize(mol->getNumAtoms());
67  for(ROMol::ConformerIterator cit=mol->beginConformers();
68  cit!=mol->endConformers();++cit,++i){
69  if(i%numThreads != threadIdx) continue;
70  for(unsigned int aidx=0;aidx<mol->getNumAtoms();++aidx){
71  ff.positions()[aidx]=&(*cit)->getAtomPos(aidx);
72  }
73  ff.initialize();
74  int needsMore=ff.minimize(maxIters);
75  double e=ff.calcEnergy();
76  (*res)[i] = std::make_pair(needsMore,e);
77  }
78  }
79  } //end of detail namespace
80 #endif
81  //! Convenience function for optimizing all of a molecule's conformations using MMFF
82  /*
83  \param mol the molecule to use
84  \param res vector of (needsMore,energy) pairs
85  \param numThreads the number of simultaneous threads to use (only has an
86  effect if the RDKit is compiled with thread support).
87  \param maxIters the maximum number of force-field iterations
88  \param mmffVariant the MMFF variant to use, should be "MMFF94" or "MMFF94S"
89  \param nonBondedThresh the threshold to be used in adding non-bonded terms
90  to the force field. Any non-bonded contact whose current
91  distance is greater than \c nonBondedThresh * the minimum value
92  for that contact will not be included.
93  \param ignoreInterfragInteractions if true, nonbonded terms will not be added between
94  fragments
95 
96  */
98  std::vector< std::pair<int, double> > &res,
99  unsigned int numThreads=1,
100  int maxIters=1000,
101  std::string mmffVariant="MMFF94",
102  double nonBondedThresh=10.0,
103  bool ignoreInterfragInteractions=true ){
104  res.resize(mol.getNumConformers());
105 #ifndef RDK_THREADSAFE_SSS
106  numThreads=1;
107 #endif
108  if(numThreads<=1){
109  unsigned int i=0;
110  for(ROMol::ConformerIterator cit=mol.beginConformers();
111  cit!=mol.endConformers();++cit,++i){
112  res[i] = MMFFOptimizeMolecule(mol,maxIters,mmffVariant,
113  nonBondedThresh,(*cit)->getId(),
114  ignoreInterfragInteractions);
115  }
116  }
117 #ifdef RDK_THREADSAFE_SSS
118  else {
119  MMFF::MMFFMolProperties mmffMolProperties(mol, mmffVariant);
120  if(mmffMolProperties.isValid()) {
121  ForceFields::ForceField *ff=MMFF::constructForceField(mol,nonBondedThresh, -1,
122  ignoreInterfragInteractions);
123  boost::thread_group tg;
124  for(unsigned int ti=0;ti<numThreads;++ti){
125  tg.add_thread(new boost::thread(detail::MMFFOptimizeMoleculeConfsHelper_,
126  *ff,
127  &mol,&res,ti,numThreads,maxIters));
128 
129  }
130  tg.join_all();
131  delete ff;
132  } else {
133  for(unsigned int i=0;i<mol.getNumConformers();++i){
134  res[i] = std::make_pair(static_cast<int>(-1),static_cast<double>(-1));
135  }
136  }
137  }
138 #endif
139  }
140  } // end of namespace UFF
141 } // end of namespace RDKit
142 #endif
std::pair< int, double > MMFFOptimizeMolecule(ROMol &mol, int maxIters=1000, std::string mmffVariant="MMFF94", double nonBondedThresh=10.0, int confId=-1, bool ignoreInterfragInteractions=true)
Convenience function for optimizing a molecule using MMFF.
Definition: MMFF.h:40
double calcEnergy() const
calculates and returns the energy (in kcal/mol) based on existing positions in the forcefield ...
unsigned int getNumConformers() const
Definition: ROMol.h:315
RDGeom::PointPtrVect & positions()
returns a reference to our points (a PointPtrVect)
Definition: ForceField.h:137
unsigned int getNumAtoms(bool onlyExplicit=1) const
returns our number of atoms
int minimize(unsigned int maxIts=200, double forceTol=1e-4, double energyTol=1e-6)
minimizes the energy of the system by following gradients
ROMol is a molecule class that is intended to have a fixed topology.
Definition: ROMol.h:105
void MMFFOptimizeMoleculeConfs(ROMol &mol, std::vector< std::pair< int, double > > &res, unsigned int numThreads=1, int maxIters=1000, std::string mmffVariant="MMFF94", double nonBondedThresh=10.0, bool ignoreInterfragInteractions=true)
Convenience function for optimizing all of a molecule&#39;s conformations using MMFF. ...
Definition: MMFF.h:97
void initialize()
does initialization
Definition: types.h:23
Includes a bunch of functionality for handling Atom and Bond queries.
Definition: Atom.h:28
ConformerIterator endConformers()
Definition: ROMol.h:498
ConformerIterator beginConformers()
Definition: ROMol.h:494
ForceFields::ForceField * constructForceField(ROMol &mol, double nonBondedThresh=100.0, int confId=-1, bool ignoreInterfragInteractions=true)
Builds and returns a MMFF force field for a molecule.
A class to store forcefields and handle minimization.
Definition: ForceField.h:56