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 <RDGeneral/RDThreads.h>
14 #include "AtomTyper.h"
15 #include "Builder.h"
16 
17 namespace RDKit {
18 class ROMol;
19 namespace MMFF {
20 //! Convenience function for optimizing a molecule using MMFF
21 /*
22  \param mol the molecule to use
23  \param maxIters the maximum number of force-field iterations
24  \param mmffVariant the MMFF variant to use, should be "MMFF94" or "MMFF94S"
25  \param nonBondedThresh the threshold to be used in adding non-bonded terms
26  to the force field. Any non-bonded contact whose
27  current
28  distance is greater than \c nonBondedThresh * the minimum
29  value
30  for that contact will not be included.
31  \param confId the optional conformer id, if this isn't provided, the
32  molecule's
33  default confId will be used.
34  \param ignoreInterfragInteractions if true, nonbonded terms will not be added
35  between
36  fragments
37 
38  \return a pair with:
39  first: -1 if parameters were missing, 0 if the optimization converged, 1 if
40  more iterations are required.
41  second: the energy
42 */
43 std::pair<int, double> MMFFOptimizeMolecule(
44  ROMol &mol, int maxIters = 1000, std::string mmffVariant = "MMFF94",
45  double nonBondedThresh = 10.0, int confId = -1,
46  bool ignoreInterfragInteractions = true) {
47  int res = -1;
48  double e = -1;
49  MMFF::MMFFMolProperties mmffMolProperties(mol, mmffVariant);
50  if (mmffMolProperties.isValid()) {
52  mol, nonBondedThresh, confId, ignoreInterfragInteractions);
53  ff->initialize();
54  res = ff->minimize(maxIters);
55  e = ff->calcEnergy();
56  delete ff;
57  }
58  return std::make_pair(res, e);
59 }
60 #ifdef RDK_THREADSAFE_SSS
61 namespace detail {
62 void MMFFOptimizeMoleculeConfsHelper_(ForceFields::ForceField ff, ROMol *mol,
63  std::vector<std::pair<int, double> > *res,
64  unsigned int threadIdx,
65  unsigned int numThreads, int maxIters) {
66  unsigned int i = 0;
67  ff.positions().resize(mol->getNumAtoms());
68  for (ROMol::ConformerIterator cit = mol->beginConformers();
69  cit != mol->endConformers(); ++cit, ++i) {
70  if (i % numThreads != threadIdx) continue;
71  for (unsigned int aidx = 0; aidx < mol->getNumAtoms(); ++aidx) {
72  ff.positions()[aidx] = &(*cit)->getAtomPos(aidx);
73  }
74  ff.initialize();
75  int needsMore = ff.minimize(maxIters);
76  double e = ff.calcEnergy();
77  (*res)[i] = std::make_pair(needsMore, e);
78  }
79 }
80 } // end of detail namespace
81 #endif
82 //! Convenience function for optimizing all of a molecule's conformations using
83 // MMFF
84 /*
85  \param mol the molecule to use
86  \param res vector of (needsMore,energy) pairs
87  \param numThreads the number of simultaneous threads to use (only has an
88  effect if the RDKit is compiled with thread support).
89  If set to zero, the max supported by the system will be
90  used.
91  \param maxIters the maximum number of force-field iterations
92  \param mmffVariant the MMFF variant to use, should be "MMFF94" or "MMFF94S"
93  \param nonBondedThresh the threshold to be used in adding non-bonded terms
94  to the force field. Any non-bonded contact whose
95  current
96  distance is greater than \c nonBondedThresh * the minimum
97  value
98  for that contact will not be included.
99  \param ignoreInterfragInteractions if true, nonbonded terms will not be added
100  between
101  fragments
102 
103 */
105  std::vector<std::pair<int, double> > &res,
106  int numThreads = 1, int maxIters = 1000,
107  std::string mmffVariant = "MMFF94",
108  double nonBondedThresh = 10.0,
109  bool ignoreInterfragInteractions = true) {
110  res.resize(mol.getNumConformers());
111  numThreads = getNumThreadsToUse(numThreads);
112  MMFF::MMFFMolProperties mmffMolProperties(mol, mmffVariant);
113  if (mmffMolProperties.isValid()) {
115  mol, nonBondedThresh, -1, ignoreInterfragInteractions);
116  if (numThreads == 1) {
117  unsigned int i = 0;
118  for (ROMol::ConformerIterator cit = mol.beginConformers();
119  cit != mol.endConformers(); ++cit, ++i) {
120  for (unsigned int aidx = 0; aidx < mol.getNumAtoms(); ++aidx) {
121  ff->positions()[aidx] = &(*cit)->getAtomPos(aidx);
122  }
123  ff->initialize();
124  int needsMore = ff->minimize(maxIters);
125  double e = ff->calcEnergy();
126  res[i] = std::make_pair(needsMore, e);
127  }
128  }
129 #ifdef RDK_THREADSAFE_SSS
130  else {
131  boost::thread_group tg;
132  for (int ti = 0; ti < numThreads; ++ti) {
133  tg.add_thread(
134  new boost::thread(detail::MMFFOptimizeMoleculeConfsHelper_, *ff,
135  &mol, &res, ti, numThreads, maxIters));
136  }
137  tg.join_all();
138  }
139 #endif
140  delete ff;
141  } else {
142  for (unsigned int i = 0; i < mol.getNumConformers(); ++i) {
143  res[i] = std::make_pair(static_cast<int>(-1), static_cast<double>(-1));
144  }
145  }
146 }
147 } // end of namespace UFF
148 } // end of namespace RDKit
149 #endif
unsigned int getNumConformers() const
Definition: ROMol.h:363
int minimize(unsigned int snapshotFreq, RDKit::SnapshotVect *snapshotVect, unsigned int maxIts=200, double forceTol=1e-4, double energyTol=1e-6)
minimizes the energy of the system by following gradients
RDGeom::PointPtrVect & positions()
returns a reference to our points (a PointPtrVect)
Definition: ForceField.h:160
unsigned int getNumAtoms(bool onlyExplicit=1) const
returns our number of atoms
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:43
ROMol is a molecule class that is intended to have a fixed topology.
Definition: ROMol.h:103
void initialize()
does initialization
Includes a bunch of functionality for handling Atom and Bond queries.
Definition: Atom.h:29
unsigned int getNumThreadsToUse(int target)
Definition: RDThreads.h:38
void MMFFOptimizeMoleculeConfs(ROMol &mol, std::vector< std::pair< int, double > > &res, 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.
Definition: MMFF.h:104
ConformerIterator endConformers()
Definition: ROMol.h:543
ConformerIterator beginConformers()
Definition: ROMol.h:541
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:57
double calcEnergy(std::vector< double > *contribs=NULL) const
calculates and returns the energy (in kcal/mol) based on existing