File: TemplEnumTools.cpp

package info (click to toggle)
rdkit 201809.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 123,688 kB
  • sloc: cpp: 230,509; python: 70,501; java: 6,329; ansic: 5,427; sql: 1,899; yacc: 1,739; lex: 1,243; makefile: 445; xml: 229; fortran: 183; sh: 123; cs: 93
file content (423 lines) | stat: -rw-r--r-- 15,462 bytes parent folder | download | duplicates (2)
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
420
421
422
423
//  $Id$
//
//  Copyright (C) 2003,2004 Rational Discovery LLC
//   All Rights Reserved
//
#include "TemplEnum.h"
#include <Geometry/Transform3D.h>
#include <GraphMol/MolTransforms/MolTransforms.h>
#include <GraphMol/FileParsers/FileParsers.h>
#define FEQ(_a_, _b_) (fabs((_a_) - (_b_)) < 1e-4)

namespace TemplateEnum {
using namespace RDKit;

// ------------------------------------------------------------------
//
// transforms a sidechain so that it is oriented better for attachment
// to a molecule
//
//  Arguments:
//   mol: core molecule
//   sidechain: sidechain to attach.
//   molConnectorIdx: the index of the attachment point atom in the
//     molecule.
//   sidechainConnectorIdx: the index of the attachment point atom
//     in the sidechain.
//
// ------------------------------------------------------------------
void orientSidechain(RWMol *mol, RWMol *sidechain, int molAttachIdx,
                     int sidechainAttachIdx) {
  PRECONDITION(mol, "bad molecule");
  PRECONDITION(sidechain, "bad molecule");

  // ---------
  // start by getting our 4 atoms
  // ---------
  Conformer &molConf = mol->getConformer();
  Conformer &sidechainConf = sidechain->getConformer();
  Atom *molConnAtom, *molAttachAtom;
  Atom *chainConnAtom, *chainAttachAtom;
  molAttachAtom = mol->getAtomWithIdx(molAttachIdx);
  chainAttachAtom = sidechain->getAtomWithIdx(sidechainAttachIdx);
  PRECONDITION(molAttachAtom->getDegree() == 1,
               "attachment points must be degree 1");
  PRECONDITION(chainAttachAtom->getDegree() == 1,
               "attachment points must be degree 1");
  RWMol::ADJ_ITER nbrIdx, endNbrs;
  boost::tie(nbrIdx, endNbrs) = mol->getAtomNeighbors(molAttachAtom);
  molConnAtom = mol->getAtomWithIdx(*nbrIdx);
  boost::tie(nbrIdx, endNbrs) = sidechain->getAtomNeighbors(chainAttachAtom);
  chainConnAtom = sidechain->getAtomWithIdx(*nbrIdx);

  //-----------------------------------------
  //  Notation:
  //    Pmc: molecule connection point (the atom that will be
  //     removed from the molecule).
  //    Pma: molecule attachment point (the atom to which we'll form
  //     the bond).
  //    Psc: sidechain connection point
  //    Psa: sidechain attachment point
  //    Vm: Pmc-Pma (molecular attachment vector)
  //    Vs: Psc-Psa (sidechain attachment vector)
  //
  //-----------------------------------------
  RDGeom::Transform3D sidechainTform, templateTform, tmpTform;

  RDGeom::Point3D Vm, Um, Pmc, Pma;
  RDGeom::Point3D Vs, Us, Psc, Psa;
  Pmc = molConf.getAtomPos(molConnAtom->getIdx());
  Pma = molConf.getAtomPos(molAttachAtom->getIdx());
  std::cerr << "p=array([" << Pma.x << "," << Pma.y << "," << Pma.z << "])"
            << std::endl;
  Psc = sidechainConf.getAtomPos(chainConnAtom->getIdx());
  Psa = sidechainConf.getAtomPos(chainAttachAtom->getIdx());

  templateTform.setToIdentity();
  Vm = Pmc - Pma;
  // note the opposite direction here:
  Vs = Psa - Psc;
  Um = Vm;
  Um.normalize();
  std::cerr << "Um=array([" << Um.x << "," << Um.y << "," << Um.z << "])"
            << std::endl;
  Us = Vs;
  Us.normalize();
  std::cerr << "Us=array([" << Us.x << "," << Us.y << "," << Us.z << "])"
            << std::endl;
  // translate Psc -> Pma
  // RDGeom::Point3D headTrans = Pma-Psc;

  templateTform.setToIdentity();

  tmpTform.setToIdentity();
  tmpTform.SetTranslation(Pma);
  templateTform *= tmpTform;

  double sinT, cosT;
  cosT = Us.dotProduct(Um);
  if (cosT > 1.0) cosT = 1.0;
  if (fabs(cosT) < 1.0) {
    tmpTform.setToIdentity();
    sinT = sqrt(1.0 - cosT * cosT);
    RDGeom::Point3D rotnAxis = Us.crossProduct(Um);
    rotnAxis.normalize();
    std::cerr << "ax=array([" << rotnAxis.x << "," << rotnAxis.y << ","
              << rotnAxis.z << "])" << std::endl;
    tmpTform.SetRotation(cosT, sinT, rotnAxis);
    templateTform *= tmpTform;
  } else if (cosT == 1.0) {
    RDGeom::Point3D normal(1, 0, 0);
    if (fabs(Us.dotProduct(normal)) == 1.0) {
      normal = RDGeom::Point3D(0, 1, 0);
    }
    RDGeom::Point3D rotnAxis = Us.crossProduct(normal);
    templateTform.SetRotation(-1, 0, rotnAxis);
  }

  tmpTform.setToIdentity();
  tmpTform.SetTranslation(Psc * -1.0);
  templateTform *= tmpTform;

  // ---------
  // transform the atomic positions in the sidechain:
  // ---------
  MolTransforms::transformMolsAtoms(sidechain, templateTform);

  // that's it!
}
// ------------------------------------------------------------------
//
// attaches a sidechain fragment to a molecule.
//
//  Arguments:
//   mol: molecule to be modified
//   sidechain: sidechain to attach.  The sidechain is copied in.
//   molConnectorIdx: the index of the attachment point atom in the
//     molecule.
//   sidechainConnectorIdx: the index of the attachment point atom
//     in the sidechain.
//   bondType: type of the bond to form between the atoms
//
//  The connector atoms are *NOT* part of the final molecule, they
//  merely serve to establish where things connect.
//
// ------------------------------------------------------------------
void molAddSidechain(RWMol *mol, RWMol *sidechain, int molConnectorIdx,
                     int sidechainConnectorIdx, Bond::BondType bondType) {
  PRECONDITION(mol, "bad molecule provided");
  PRECONDITION(sidechain, "bad sidechain provided");
  int origNumAtoms = mol->getNumAtoms();
  mol->insertMol(*sidechain);

  // get pointers to the two connectors (these are the atoms
  // we'll end up removing)
  Atom *molConnAtom, *sidechainConnAtom;
  molConnAtom = mol->getAtomWithIdx(molConnectorIdx);
  sidechainConnAtom = mol->getAtomWithIdx(sidechainConnectorIdx + origNumAtoms);

  // now use those pointers to get the atoms which will remain
  // (we're going to connect these) and remove the original
  // connection points from the molecule
  Atom *tmpAtom;
  RWMol::ADJ_ITER nbrIdx, endNbrs;
  boost::tie(nbrIdx, endNbrs) = mol->getAtomNeighbors(molConnAtom);

  // we are assuming that the first neighbor is the correct one.
  // Really we should be able to assume that there is only a single
  // attachment point.
  tmpAtom = molConnAtom;
  molConnAtom = mol->getAtomWithIdx(*nbrIdx);
  mol->removeAtom(tmpAtom);
  // repeat that process for the sidechain:
  boost::tie(nbrIdx, endNbrs) = mol->getAtomNeighbors(sidechainConnAtom);
  tmpAtom = sidechainConnAtom;
  sidechainConnAtom = mol->getAtomWithIdx(*nbrIdx);
  mol->removeAtom(tmpAtom);

  // finally connect the remaining atoms:
  mol->addBond(molConnAtom, sidechainConnAtom, bondType);
}

// used as a starting point for connection bookmarks
const int CONNECT_BOOKMARK_START = 0x23424223;

// ------------------------------------------------------------------
//
// Loop through all the atoms and bookmark the attachment points
//
//  attachment points are assumed to have one of these properties:
//   - common_properties::molFileAlias (set by the mol file parser)
//   - common_properties::dummyLabel   (set by the SMILES parser)
//  frontMarker is the "recognition character" to be used to pick
//  valid labels.  e.g. if the frontMarker is 'X', a label beginning
//  with 'Y' will not be marked.
//
//  In addition to bookmarking the attachment points, we also set
//  the common_properties::maxAttachIdx property, which holds an integer with
//  the
//  maximum attachment bookmark index. This is used in the
//  enumeration to prevent us from having to scan through all the
//  molecule's bookmarks
//
//
// ------------------------------------------------------------------
void markAttachmentPoints(RWMOL_SPTR mol, char frontMarker) {
  markAttachmentPoints(mol.get(), frontMarker);
}
void markAttachmentPoints(RWMol *mol, char frontMarker) {
  PRECONDITION(mol, "bad molecule");

  RWMol::AtomIterator atomIt;
  int maxAttachIdx = 0;

  // scan through the atoms and mark those that have aliases
  //  (these might be attachment points)
  for (atomIt = mol->beginAtoms(); atomIt != mol->endAtoms(); atomIt++) {
    // start by finding possible attachment point properties:
    std::string attachLabel = "";
    if ((*atomIt)->hasProp(common_properties::molFileAlias)) {
      (*atomIt)->getProp(common_properties::molFileAlias, attachLabel);
    } else if ((*atomIt)->hasProp(common_properties::dummyLabel)) {
      (*atomIt)->getProp(common_properties::dummyLabel, attachLabel);
    }

    // if we got one and it starts with the appropriate front
    // marker, proceed:
    if (attachLabel != "" && attachLabel[0] == frontMarker) {
      // to avoid trouble later, we guarantee that the attachment
      // point has degree 1 (one bond to it).
      if ((*atomIt)->getDegree() > 1)
        throw EnumException("More than one bond to an attachment point.");

      int offset = CONNECT_BOOKMARK_START;
      if (attachLabel.length() > 1) {
        if (attachLabel[1] >= 'a' && attachLabel[1] <= 'z') {
          offset += (int)attachLabel[1] - (int)'a';
        }
      }
      mol->setAtomBookmark(*atomIt, offset);
      if (offset > maxAttachIdx) maxAttachIdx = offset;
    }
  }
  if (maxAttachIdx) {
    mol->setProp(common_properties::maxAttachIdx, maxAttachIdx);
  }
}

// ------------------------------------------------------------------
//
// loops through the sidechain molecules and calls
// _markAttachmentPoints()_ on each.
//
// ------------------------------------------------------------------
void prepareSidechains(RWMOL_SPTR_VECT *sidechains, char frontMarker) {
  PRECONDITION(sidechains, "bad sidechain list");
  RWMOL_SPTR_VECT::iterator mpvI;
  for (mpvI = sidechains->begin(); mpvI != sidechains->end(); mpvI++) {
    markAttachmentPoints(*mpvI, frontMarker);
  }
}

// ------------------------------------------------------------------
//
// Enumerates the library around a template and returns the result.
//
//
// ------------------------------------------------------------------
RWMOL_SPTR_VECT enumerateLibrary(RWMol *templateMol,
                                 VECT_RWMOL_SPTR_VECT &sidechains,
                                 bool orientSidechains) {
  PRECONDITION(templateMol, "bad molecule");
  RWMOL_SPTR_VECT res, tmp;
  res.push_back(RWMOL_SPTR(new RWMol(*templateMol)));

  // if there's no attachment point on the molecule or no
  // sidechains, return now:
  if (!templateMol->hasProp(common_properties::maxAttachIdx) ||
      sidechains.size() == 0)
    return res;

  int maxIdx;
  templateMol->getProp(common_properties::maxAttachIdx, maxIdx);

  tmp.clear();
  // loop over the sidechains and attach them
  for (unsigned int i = 0; i < sidechains.size(); i++) {
    int tgtMark = CONNECT_BOOKMARK_START + i;
    // here's another boundary condition
    if (tgtMark > maxIdx) break;

    /// loop over all atoms with the appropriate mark
    //  This means that if a mol has two attachment points with the
    //  same name (e.g. two Xa's) they'll always have the same
    //  sidechain attached to them.  This is a feature.
    RWMOL_SPTR_VECT::iterator sidechainIt;
    for (sidechainIt = sidechains[i].begin();
         sidechainIt != sidechains[i].end(); sidechainIt++) {
      // we've got our sidechain, find the atom it attaches from
      if ((*sidechainIt)->hasAtomBookmark(CONNECT_BOOKMARK_START)) {
        //
        // NOTE: If there's more than one marked atom in the sidechain,
        ///      we'll only use the first for the moment.
        //
        int sidechainAtomIdx = (*sidechainIt)
                                   ->getAtomWithBookmark(CONNECT_BOOKMARK_START)
                                   ->getIdx();

        // now add the sidechain to each molecule
        RWMOL_SPTR_VECT::iterator templMolIt;
        // loop over all the mols we've generated to this point
        for (templMolIt = res.begin(); templMolIt != res.end(); templMolIt++) {
          RWMol *templ = new RWMol(**templMolIt);
          std::string name, tmpStr;
          if (templ->hasProp(common_properties::_Name)) {
            templ->getProp(common_properties::_Name, tmpStr);
            name = name + " " + tmpStr;
          }
          while (templ->hasAtomBookmark(tgtMark)) {
            // this is the atom we'll be replacing in the template
            Atom *at = templ->getAtomWithBookmark(tgtMark);

            // copy and transform the sidechain:
            RWMol *sidechain;
            if (orientSidechains) {
              sidechain = new RWMol(*(sidechainIt->get()));
              orientSidechain(templ, sidechain, at->getIdx(), sidechainAtomIdx);
            } else {
              sidechain = sidechainIt->get();
            }
            // FIX: need to use the actual bond order here:
            molAddSidechain(templ, sidechain, at->getIdx(), sidechainAtomIdx,
                            Bond::SINGLE);
            if (sidechain->hasProp(common_properties::_Name)) {
              sidechain->getProp(common_properties::_Name, tmpStr);
              name = name + " " + tmpStr;
            }
            templ->clearAtomBookmark(tgtMark, at);
            if (orientSidechains) {
              delete sidechain;
            }
          }
          // std::cout << templ << "> " << MolToSmiles(*templ) << std::endl;
          if (name != "") templ->setProp(common_properties::_Name, name);
          tmp.push_back(RWMOL_SPTR(templ));
        }
      }
    }

    //
    // if we just made any molecules, free up the memory used by the
    // existing result set and move the molecules we just generated
    // over
    if (tmp.size()) {
#if 0      
	RWMOL_SPTR_VECT::iterator tmpMolIt;
	for(tmpMolIt=res.begin();tmpMolIt!=res.end();tmpMolIt++){
	  delete *tmpMolIt;
	}
#endif
      res = tmp;
      tmp.clear();
    }
  }
  return res;
}

// ------------------------------------------------------------------
//
//  Reads a template and library of sidechains from input files.
//   the template file should be a mol file and the sidechain files
//   SD files
//
// ------------------------------------------------------------------
RWMOL_SPTR_VECT enumFromFiles(const char *templateName,
                              std::vector<const char *> &sidechainNames) {
  PRECONDITION(templateName, "bad template file name passed in");

  // build and mark the template molecule
  RWMol *templ = MolFileToMol(templateName, false);
  if (!templ) throw EnumException("could not construct template molecule");
  markAttachmentPoints(templ, 'X');

  // now build and mark each set of sidechains:
  RWMOL_SPTR_VECT sidechains;
  VECT_RWMOL_SPTR_VECT allSidechains;
  for (std::vector<const char *>::const_iterator i = sidechainNames.begin();
       i != sidechainNames.end(); i++) {
    sidechains = SDFileToMols(*i, false);
    if (!sidechains.size()) {
      std::string err = "no sidechains read from file: ";
      err += *i;
      throw EnumException(err.c_str());
    }
    prepareSidechains(&sidechains, 'X');
    allSidechains.push_back(sidechains);
  }

  // enumerate the library:
  RWMOL_SPTR_VECT library = enumerateLibrary(templ, allSidechains);

  //--------------------------
  //
  // Clean up the molecules and sidechains we constructed along the
  // way.
  //
  //--------------------------
  delete templ;
#if 0
    VECT_RWMOL_SPTR_VECT::iterator vmpvI;
    for(vmpvI=allSidechains.begin();vmpvI!=allSidechains.end();vmpvI++){
      RWMOL_SPTR_VECT::iterator mpvI;
      for(mpvI=vmpvI->begin();mpvI!=vmpvI->end();mpvI++){
	delete *mpvI;
      }
      vmpvI->clear();
    }
#endif
  allSidechains.clear();

  return library;
}

}  // end of TemplateEnum namespace