File: rdDistGeom.cpp

package info (click to toggle)
rdkit 201203-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 37,840 kB
  • sloc: cpp: 93,902; python: 51,897; java: 5,192; ansic: 3,497; xml: 2,499; sql: 1,641; yacc: 1,518; lex: 1,076; makefile: 325; fortran: 183; sh: 153; cs: 51
file content (231 lines) | stat: -rw-r--r-- 10,529 bytes parent folder | download
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
// $Id: rdDistGeom.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 <boost/python.hpp>
#define PY_ARRAY_UNIQUE_SYMBOL rdDistGeom_array_API
#include "numpy/arrayobject.h"
#include <DistGeom/BoundsMatrix.h>

#include <GraphMol/GraphMol.h>
#include <RDBoost/Wrap.h>

#include <GraphMol/DistGeomHelpers/BoundsMatrixBuilder.h>
#include <GraphMol/DistGeomHelpers/Embedder.h>

namespace python = boost::python;

namespace RDKit {
  int EmbedMolecule(ROMol &mol, unsigned int maxAttempts,
                    int seed, bool clearConfs,
		    bool useRandomCoords,double boxSizeMult,
                    bool randNegEig, unsigned int numZeroFail,
                    python::dict &coordMap,double forceTol,
                    bool ignoreSmoothingFailures){
    std::map<int,RDGeom::Point3D> pMap;
    python::list ks = coordMap.keys();
    unsigned int nKeys=python::extract<unsigned int>(ks.attr("__len__")());
    for(unsigned int i=0;i<nKeys;++i){
      unsigned int id = python::extract<unsigned int>(ks[i]);
      pMap[id]= python::extract<RDGeom::Point3D>(coordMap[id]);
    }
    std::map<int,RDGeom::Point3D> *pMapPtr=0;
    if(nKeys){
      pMapPtr=&pMap;
    }

    int res = DGeomHelpers::EmbedMolecule(mol, maxAttempts, 
                                          seed, clearConfs,
					  useRandomCoords,boxSizeMult,
					  randNegEig,
                                          numZeroFail,
                                          pMapPtr,forceTol,
                                          ignoreSmoothingFailures);
    return res;
  }

  INT_VECT EmbedMultipleConfs(ROMol &mol, unsigned int numConfs,
			      unsigned int maxAttempts,
                              int seed, bool clearConfs,
			      bool useRandomCoords,double boxSizeMult,
                              bool randNegEig, unsigned int numZeroFail,
			      double pruneRmsThresh,python::dict &coordMap,
                              double forceTol,
                              bool ignoreSmoothingFailures) {

    std::map<int,RDGeom::Point3D> pMap;
    python::list ks = coordMap.keys();
    unsigned int nKeys=python::extract<unsigned int>(ks.attr("__len__")());
    for(unsigned int i=0;i<nKeys;++i){
      unsigned int id = python::extract<unsigned int>(ks[i]);
      pMap[id]= python::extract<RDGeom::Point3D>(coordMap[id]);
    }
    std::map<int,RDGeom::Point3D> *pMapPtr=0;
    if(nKeys){
      pMapPtr=&pMap;
    }

    INT_VECT res = DGeomHelpers::EmbedMultipleConfs(mol, numConfs, maxAttempts,
                                                    seed, clearConfs,
						    useRandomCoords,boxSizeMult, 
                                                    randNegEig, numZeroFail,
                                                    pruneRmsThresh,pMapPtr,forceTol,
                                                    ignoreSmoothingFailures);

    return res;
  } 

  PyObject *getMolBoundsMatrix(ROMol &mol, bool set15bounds=true,
                               bool scaleVDW=false) {
    unsigned int nats = mol.getNumAtoms();
    npy_intp dims[2];
    dims[0] = nats;
    dims[1] = nats;

    DistGeom::BoundsMatPtr mat(new DistGeom::BoundsMatrix(nats));
    DGeomHelpers::initBoundsMat(mat);
    DGeomHelpers::setTopolBounds(mol,mat, set15bounds, scaleVDW);
    PyArrayObject *res = (PyArrayObject *)PyArray_SimpleNew(2,dims,NPY_DOUBLE);
    memcpy(static_cast<void *>(res->data),
	   static_cast<void *>(mat->getData()),
	   nats*nats*sizeof(double));
	   
    return PyArray_Return(res);
  }
}

BOOST_PYTHON_MODULE(rdDistGeom) {
  python::scope().attr("__doc__") =
    "Module containing functions to compute atomic coordinates in 3D using distance geometry"
    ;

  import_array();

  //RegisterListConverter<RDKit::Atom*>();

  std::string docString = "Use distance geometry to obtain intial \n\
 coordinates for a molecule\n\n\
 \n\
 ARGUMENTS:\n\n\
    - mol : the molecule of interest\n\
    - maxAttempts : the maximum number of attempts to try embedding \n\
    - randomSeed : provide a seed for the random number generator \n\
                   so that the same coordinates can be obtained \n\
                   for a molecule on multiple runs. The default \n\
                   (-1) uses a random seed \n\
    - clearConfs : clear all existing conformations on the molecule\n\
    - useRandomCoords : Start the embedding from random coordinates instead of\n\
                        using eigenvalues of the distance matrix.\n\
    - boxSizeMult    Determines the size of the box that is used for\n\
                     random coordinates. If this is a positive number, the \n\
                     side length will equal the largest element of the distance\n\
                     matrix times boxSizeMult. If this is a negative number,\n\
                     the side length will equal -boxSizeMult (i.e. independent\n\
                     of the elements of the distance matrix).\n\
    - randNegEig : If the embedding yields a negative eigenvalue, \n\
                   pick coordinates that correspond \n\
                   to this component at random \n\
    - numZeroFail : fail embedding is we have this more zero eigenvalues \n\
    - coordMap : a dictionary mapping atom IDs->coordinates. Use this to \n\
                 require some atoms to have fixed coordinates in the resulting \n\
                 conformation.\n\
    - forceTol : tolerance to be used during the force-field minimization with \n\
                 the distance geometry force field.\n\
    - ignoreSmoothingFailures : try to embed the molecule even if triangle smoothing\n\
                 of the bounds matrix fails.\n\
\n\
 RETURNS:\n\n\
    ID of the new conformation added to the molecule \n\
\n";
  python::def("EmbedMolecule", RDKit::EmbedMolecule,
              (python::arg("mol"), python::arg("maxAttempts")=0,
               python::arg("randomSeed")=-1, python::arg("clearConfs")=true,
               python::arg("useRandomCoords")=false,
	       python::arg("boxSizeMult")=2.0,
               python::arg("randNegEig")=true, python::arg("numZeroFail")=1,
               python::arg("coordMap")=python::dict(),
               python::arg("forceTol")=1e-3,
               python::arg("ignoreSmoothingFailures")=false),
              docString.c_str());

  docString = "Use distance geometry to obtain multiple sets of \n\
 coordinates for a molecule\n\
 \n\
 ARGUMENTS:\n\n\
  - mol : the molecule of interest\n\
  - numConfs : the number of conformers to generate \n\
  - maxAttempts : the maximum number of attempts to try embedding \n\
  - randomSeed : provide a seed for the random number generator \n\
                 so that the same coordinates can be obtained \n\
                 for a molecule on multiple runs. The default \n\
                 (-1) uses a random seed \n\
  - clearConfs : clear all existing conformations on the molecule\n\
  - useRandomCoords : Start the embedding from random coordinates instead of\n\
                      using eigenvalues of the distance matrix.\n\
  - boxSizeMult    Determines the size of the box that is used for\n\
                   random coordinates. If this is a positive number, the \n\
                   side length will equal the largest element of the distance\n\
                   matrix times boxSizeMult. If this is a negative number,\n\
                   the side length will equal -boxSizeMult (i.e. independent\n\
                   of the elements of the distance matrix).\n\
  - randNegEig : If the embedding yields a negative eigenvalue, \n\
                 pick coordinates that correspond \n\
                 to this component at random \n\
  - numZeroFail : fail embedding is we have this more zero eigenvalues \n\
  - pruneRmsThresh : Retain only the conformations out of 'numConfs' \n\
                    after embedding that are at least \n\
                    this far apart from each other. \n\
          RMSD is computed on the heavy atoms. \n\
          Pruning is greedy; i.e. the first embedded conformation\n\
          is retained and from then on only those that are at\n\
          least pruneRmsThresh away from all retained conformations\n\
          are kept. The pruning is done after embedding and \n\
          bounds violation minimization. No pruning by default.\n\
    - coordMap : a dictionary mapping atom IDs->coordinates. Use this to \n\
                 require some atoms to have fixed coordinates in the resulting \n\
                 conformation.\n\
    - forceTol : tolerance to be used during the force-field minimization with \n\
                 the distance geometry force field.\n\
    - ignoreSmoothingFailures : try to embed the molecule even if triangle smoothing\n\
                 of the bounds matrix fails.\n\
 RETURNS:\n\n\
    List of new conformation IDs \n\
\n";
  python::def("EmbedMultipleConfs", RDKit::EmbedMultipleConfs,
              (python::arg("mol"), python::arg("numConfs")=10, 
               python::arg("maxAttempts")=0,
               python::arg("randomSeed")=-1, python::arg("clearConfs")=true,
               python::arg("useRandomCoords")=false,
	       python::arg("boxSizeMult")=2.0,
               python::arg("randNegEig")=true, python::arg("numZeroFail")=1,
	       python::arg("pruneRmsThresh")=-1.0,
               python::arg("coordMap")=python::dict(),
               python::arg("forceTol")=1e-3,
               python::arg("ignoreSmoothingFailures")=false),
              docString.c_str());

  docString = "Returns the distance bounds matrix for a molecule\n\
 \n\
 ARGUMENTS:\n\n\
    - mol : the molecule of interest\n\
    - set15bounds : set bounds for 1-5 atom distances based on \n\
                    topology (otherwise stop at 1-4s)\n\
    - scaleVDW : scale down the sum of VDW radii when setting the \n\
                 lower bounds for atoms less than 5 bonds apart \n\
 RETURNS:\n\n\
    the bounds matrix as a Numeric array with lower bounds in \n\
    the lower triangle and upper bounds in the upper triangle\n\
\n";
  python::def("GetMoleculeBoundsMatrix", RDKit::getMolBoundsMatrix,
              (python::arg("mol"), python::arg("set15bounds")=true,
               python::arg("scaleVDW")=false),
              docString.c_str());


}