File: EditableMol.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 (122 lines) | stat: -rw-r--r-- 3,902 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
// $Id: EditableMol.cpp 2000 2012-03-23 04:47:22Z glandrum $
//
//  Copyright (C) 2007 Greg Landrum
//
//   @@ 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.
//
// there's a compiler bug in some versions of g++ that causes this file to not compile unless
// we skip the docstrings:
//#define BOOST_PYTHON_NO_PY_SIGNATURES

#define NO_IMPORT_ARRAY
#include <boost/python.hpp>
#include <string>

#include "rdchem.h"
// ours
#include <GraphMol/RDKitBase.h>

namespace python = boost::python;

namespace RDKit {

namespace {
  class EditableMol : boost::noncopyable {
  public:
    EditableMol(const ROMol &m) {
      dp_mol= new RWMol(m);
    };
    ~EditableMol() {
      PRECONDITION(dp_mol,"no molecule");
      delete dp_mol;
    };

    void RemoveAtom(unsigned int idx){
      PRECONDITION(dp_mol,"no molecule");
      dp_mol->removeAtom(idx);
    };
    void RemoveBond(unsigned int idx1,unsigned int idx2){
      PRECONDITION(dp_mol,"no molecule");
      dp_mol->removeBond(idx1,idx2);
    };
    int AddBond(unsigned int begAtomIdx,
                 unsigned int endAtomIdx,
                 Bond::BondType order=Bond::UNSPECIFIED)
    {
      PRECONDITION(dp_mol,"no molecule");
      return dp_mol->addBond(begAtomIdx,endAtomIdx,order);
    };
    int AddAtom(Atom *atom){
      PRECONDITION(dp_mol,"no molecule");
      PRECONDITION(atom,"bad atom");
      return dp_mol->addAtom(atom,true,false);
    };
    void ReplaceAtom(unsigned int idx,Atom *atom){
      PRECONDITION(dp_mol,"no molecule");
      PRECONDITION(atom,"bad atom");
      dp_mol->replaceAtom(idx,atom);
    };
    ROMol *GetMol() const{
      PRECONDITION(dp_mol,"no molecule");
      ROMol *res=new ROMol(*dp_mol);
      return res;
    };
  private:
    RWMol *dp_mol;
  };
}
  
struct EditableMol_wrapper {
  static void wrap(){
    std::string molClassDoc = "The EditableMol class.\n\n\
   This class can be used to add/remove bonds and atoms to\n\
   a molecule.\n\
   In order to use it, you need to first construct an EditableMol\n\
   from a standard Mol:\n\
   >>> m = Chem.MolFromSmiles('CCC')\n\
   >>> em = Chem.EditableMol(m)\n\
   >>> em.AddAtom(Chem.Atom(8))\n\
   >>> em.AddBond(0,3,Chem.BondType.SINGLE)\n\
   >>> m2 = em.GetMol()\n\
   >>> Chem.SanitizeMol(m2)\n\
   >>> Chem.MolToSmiles(m2)\n\
   'CCCO'\n\
\n\
   *Note*: It is very, very easy to shoot yourself in the foot with\n\
           this class by constructing an unreasonable molecule.\n\
";
    python::class_<EditableMol,boost::noncopyable >("EditableMol",
        "an editable molecule class",
        python::init<const ROMol &>("Construct from a Mol"))
      .def("RemoveAtom",&EditableMol::RemoveAtom,
      "Remove the specified atom from the molecule")
      .def("RemoveBond",&EditableMol::RemoveBond,
      "Remove the specified bond from the molecule")
      
      .def("AddBond",&EditableMol::AddBond,
                     (python::arg("mol"),python::arg("beginAtomIdx"),python::arg("endAtomIdx"),
                      python::arg("order")=Bond::UNSPECIFIED),
      "add a bond, returns the index of the newly added bond")
      
      .def("AddAtom",&EditableMol::AddAtom,
                     (python::arg("mol"),python::arg("atom")),
      "add an atom, returns the index of the newly added atom")
      .def("ReplaceAtom",&EditableMol::ReplaceAtom,
                     (python::arg("mol"),python::arg("index"),python::arg("newAtom")),
      "replaces the specified atom with the provided one")
      .def("GetMol",&EditableMol::GetMol,
           "Returns a Mol (a normal molecule)",
           python::return_value_policy<python::manage_new_object>())
      ;

  };
};

}// end of namespace
void wrap_EditableMol() {
  RDKit::EditableMol_wrapper::wrap();
}