File: AlignMolecules.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 (140 lines) | stat: -rw-r--r-- 5,772 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
// $Id: AlignMolecules.cpp 1528 2010-09-26 17:04:37Z glandrum $
//
//  Copyright (C) 2001-2008 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 "AlignMolecules.h"
#include <Geometry/Transform3D.h>
#include <Numerics/Vector.h>
#include <GraphMol/Substruct/SubstructMatch.h>
#include <GraphMol/Conformer.h>
#include <GraphMol/ROMol.h>
#include <Numerics/Alignment/AlignPoints.h>
#include <GraphMol/MolTransforms/MolTransforms.h>

namespace RDKit {
  namespace MolAlign {
    double getAlignmentTransform(const ROMol &prbMol, const ROMol &refMol,
                                         RDGeom::Transform3D &trans,
                                         int prbCid, int refCid, 
                                         const MatchVectType *atomMap, 
                                         const RDNumeric::DoubleVector *weights, bool reflect,
                                         unsigned int maxIterations) {
      RDGeom::Point3DConstPtrVect refPoints, prbPoints;
      const Conformer &prbCnf = prbMol.getConformer(prbCid);
      const Conformer &refCnf = refMol.getConformer(refCid);
      if (atomMap == 0) {
        // we have to figure out the mapping between the two molecule
        MatchVectType match;
        if (SubstructMatch(refMol, prbMol, match)) {
          MatchVectType::const_iterator mi;
          for (mi = match.begin(); mi != match.end(); mi++) {
            prbPoints.push_back(&prbCnf.getAtomPos(mi->first));
            refPoints.push_back(&refCnf.getAtomPos(mi->second));
          }
        } else {
          throw MolAlignException("No sub-structure match found between the probe and query mol");
        } 
      } else {
        MatchVectType::const_iterator mi;
        for (mi = atomMap->begin(); mi != atomMap->end(); mi++) {
          prbPoints.push_back(&prbCnf.getAtomPos(mi->first));
          refPoints.push_back(&refCnf.getAtomPos(mi->second));
        }
      }
      double ssr = RDNumeric::Alignments::AlignPoints(refPoints, prbPoints, 
                                                      trans, weights, reflect, maxIterations);
      ssr /= (prbPoints.size());
      return sqrt(ssr);
    }
    
    double alignMol(ROMol &prbMol, const ROMol &refMol, int prbCid, int refCid,
                            const MatchVectType *atomMap, const RDNumeric::DoubleVector *weights, 
                            bool reflect, unsigned int maxIterations) {
      RDGeom::Transform3D trans;
      double res = getAlignmentTransform(prbMol, refMol, trans, prbCid, refCid, 
                                                 atomMap, weights, reflect, maxIterations);
      // now transform the relevant conformation on prbMol
      Conformer &conf = prbMol.getConformer(prbCid);
      MolTransforms::transformConformer(conf, trans);
      return res;
    }

    void _fillAtomPositions(RDGeom::Point3DConstPtrVect &pts, const Conformer &conf,
                            const std::vector<unsigned int> *atomIds=0) {
      unsigned int na = conf.getNumAtoms();
      pts.clear();
      if (atomIds == 0) {
        unsigned int ai;
        pts.reserve(na);
        for (ai = 0; ai < na; ++ai) {
          pts.push_back(&conf.getAtomPos(ai));
        }
      } else {
        pts.reserve(atomIds->size());
        std::vector<unsigned int>::const_iterator cai;
        for (cai = atomIds->begin(); cai != atomIds->end(); cai++) {
          pts.push_back(&conf.getAtomPos(*cai));
        }
      }
    }

    void alignMolConformers(ROMol &mol, const std::vector<unsigned int> *atomIds,
                            const std::vector<unsigned int> *confIds,
                            const RDNumeric::DoubleVector *weights, 
                            bool reflect, unsigned int maxIters) {
      if (mol.getNumConformers() == 0) {
        // nothing to be done ;
        return ;
      }

      RDGeom::Point3DConstPtrVect refPoints, prbPoints;
      int cid = -1;
      if ((confIds != 0) && (confIds->size() > 0)) {
        cid = confIds->front();
      }
      const Conformer &refCnf = mol.getConformer(cid);
      _fillAtomPositions(refPoints, refCnf, atomIds);
      
      // now loop throught the remaininf conformations and transform them
      RDGeom::Transform3D trans;
      if (confIds == 0) {
        unsigned int i=0;
        ROMol::ConformerIterator cnfi;
        //Conformer *conf; 
        for (cnfi = mol.beginConformers(); cnfi != mol.endConformers(); cnfi++) {
          //conf = (*cnfi);
          i += 1;
          if (i == 1) {
            continue;
          }
          _fillAtomPositions(prbPoints, *(*cnfi), atomIds);
          RDNumeric::Alignments::AlignPoints(refPoints, prbPoints, 
                                                          trans, weights, reflect, 
                                                          maxIters);
          MolTransforms::transformConformer(*(*cnfi), trans);
        }
      } else {
        std::vector<unsigned int>::const_iterator cai;
        unsigned int i=0;
        for (cai = confIds->begin(); cai != confIds->end(); cai++) {
          i += 1;
          if (i == 1) {
            continue;
          }
          Conformer &conf = mol.getConformer(*cai);
          _fillAtomPositions(prbPoints, conf, atomIds);
          RDNumeric::Alignments::AlignPoints(refPoints, prbPoints, 
                                                          trans, weights, reflect, 
                                                          maxIters);
          MolTransforms::transformConformer(conf, trans);
        }
      }
    }   
  }
}