00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef __RD_ROMOL_H__
00013 #define __RD_ROMOL_H__
00014
00015
00016 #include <utility>
00017 #include <map>
00018
00019
00020 #include <boost/graph/graph_traits.hpp>
00021 #include <boost/graph/adjacency_list.hpp>
00022
00023 #include "AtomProps.h"
00024 #include "BondProps.h"
00025
00026 #include "Conformer.h"
00027
00028 namespace RDKit{
00029
00030 typedef boost::adjacency_list< boost::vecS,
00031 boost::vecS,
00032 boost::undirectedS,
00033 AtomProperty,
00034 BondProperty> MolGraph;
00035 class MolPickler;
00036 class RWMol;
00037 class Atom;
00038 class Bond;
00039 class QueryAtom;
00040 class QueryBond;
00041 class RingInfo;
00042
00043 template <class T1,class T2,class T3>
00044 class AtomIterator_;
00045 template <class T1,class T2,class T3>
00046 class AromaticAtomIterator_;
00047 template <class T1,class T2,class T3>
00048 class HeteroatomIterator_;
00049 template <class T1,class T2,class T3>
00050 class QueryAtomIterator_;
00051 class BondIterator_;
00052 class ConstBondIterator_;
00053
00054 typedef boost::shared_ptr<Atom> ATOM_SPTR;
00055 typedef boost::shared_ptr<Bond> BOND_SPTR;
00056
00057 extern const int ci_RIGHTMOST_ATOM;
00058 extern const int ci_LEADING_BOND;
00059 extern const int ci_ATOM_HOLDER;
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093 class ROMol {
00094 public:
00095 friend class MolPickler;
00096 friend class RWMol;
00097
00098
00099
00100
00101
00102 typedef boost::property_map<MolGraph,vertex_atom_t> GRAPH_MOL_ATOM_PMAP;
00103 typedef boost::property_map<MolGraph,edge_bond_t> GRAPH_MOL_BOND_PMAP;
00104 typedef boost::graph_traits<MolGraph> GRAPH_MOL_TRAITS;
00105 typedef GRAPH_MOL_TRAITS::edge_iterator EDGE_ITER;
00106 typedef GRAPH_MOL_TRAITS::out_edge_iterator OEDGE_ITER;
00107 typedef GRAPH_MOL_TRAITS::vertex_iterator VERTEX_ITER;
00108 typedef GRAPH_MOL_TRAITS::adjacency_iterator ADJ_ITER;
00109 typedef std::pair<EDGE_ITER,EDGE_ITER> BOND_ITER_PAIR;
00110 typedef std::pair<OEDGE_ITER,OEDGE_ITER> OBOND_ITER_PAIR;
00111 typedef std::pair<VERTEX_ITER,VERTEX_ITER> ATOM_ITER_PAIR;
00112 typedef std::pair<ADJ_ITER,ADJ_ITER> ADJ_ITER_PAIR;
00113
00114 typedef std::vector<ATOM_SPTR> ATOM_SPTR_VECT;
00115 typedef ATOM_SPTR_VECT::iterator ATOM_SPTR_VECT_I;
00116 typedef ATOM_SPTR_VECT::const_iterator ATOM_SPTR_VECT_CI;
00117 typedef std::vector<BOND_SPTR> BOND_SPTR_VECT;
00118 typedef BOND_SPTR_VECT::iterator BOND_SPTR_VECT_I;
00119 typedef BOND_SPTR_VECT::const_iterator BOND_SPTR_VECT_CI;
00120
00121 typedef std::vector<Atom *> ATOM_PTR_VECT;
00122 typedef ATOM_PTR_VECT::iterator ATOM_PTR_VECT_I;
00123 typedef ATOM_PTR_VECT::const_iterator ATOM_PTR_VECT_CI;
00124 typedef std::vector<Bond *> BOND_PTR_VECT;
00125 typedef BOND_PTR_VECT::iterator BOND_PTR_VECT_I;
00126 typedef BOND_PTR_VECT::const_iterator BOND_PTR_VECT_CI;
00127
00128 typedef std::list<Atom *> ATOM_PTR_LIST;
00129 typedef ATOM_PTR_LIST::iterator ATOM_PTR_LIST_I;
00130 typedef ATOM_PTR_LIST::const_iterator ATOM_PTR_LIST_CI;
00131 typedef std::list<Bond *> BOND_PTR_LIST;
00132 typedef BOND_PTR_LIST::iterator BOND_PTR_LIST_I;
00133 typedef BOND_PTR_LIST::const_iterator BOND_PTR_LIST_CI;
00134
00135
00136 typedef std::list<CONFORMER_SPTR> CONF_SPTR_LIST;
00137 typedef CONF_SPTR_LIST::iterator CONF_SPTR_LIST_I;
00138 typedef CONF_SPTR_LIST::const_iterator CONF_SPTR_LIST_CI;
00139 typedef std::pair<CONF_SPTR_LIST_I, CONF_SPTR_LIST_I> CONFS_I_PAIR;
00140
00141
00142 typedef Atom * GRAPH_NODE_TYPE;
00143 typedef Bond * GRAPH_EDGE_TYPE;
00144 typedef Atom const * GRAPH_NODE_CONST_TYPE;
00145 typedef Bond const * GRAPH_EDGE_CONST_TYPE;
00146 typedef std::map<int,ATOM_PTR_LIST> ATOM_BOOKMARK_MAP;
00147 typedef std::map<int,BOND_PTR_LIST> BOND_BOOKMARK_MAP;
00148
00149 typedef class AtomIterator_<Atom,ROMol,GRAPH_MOL_ATOM_PMAP::type> AtomIterator;
00150 typedef class AtomIterator_<const Atom,const ROMol,GRAPH_MOL_ATOM_PMAP::const_type> ConstAtomIterator;
00151 typedef class AromaticAtomIterator_<Atom,ROMol,GRAPH_MOL_ATOM_PMAP::type> AromaticAtomIterator;
00152 typedef class AromaticAtomIterator_<const Atom,const ROMol,GRAPH_MOL_ATOM_PMAP::const_type> ConstAromaticAtomIterator;
00153 typedef class HeteroatomIterator_<Atom,ROMol,GRAPH_MOL_ATOM_PMAP::type> HeteroatomIterator;
00154 typedef class HeteroatomIterator_<const Atom,const ROMol,GRAPH_MOL_ATOM_PMAP::const_type> ConstHeteroatomIterator;
00155 typedef class QueryAtomIterator_<Atom,ROMol,GRAPH_MOL_ATOM_PMAP::type> QueryAtomIterator;
00156 typedef class QueryAtomIterator_<const Atom,const ROMol,GRAPH_MOL_ATOM_PMAP::const_type> ConstQueryAtomIterator;
00157 typedef class BondIterator_ BondIterator;
00158 typedef class ConstBondIterator_ ConstBondIterator;
00159
00160 typedef CONF_SPTR_LIST_I ConformerIterator;
00161 typedef CONF_SPTR_LIST_CI ConstConformerIterator;
00162
00163
00164
00165
00166 ROMol() { initMol(); }
00167
00168
00169
00170
00171
00172
00173
00174
00175 ROMol(const ROMol &other,bool quickCopy=false) {dp_props=0;dp_ringInfo=0;initFromOther(other,quickCopy);};
00176
00177 ROMol(const std::string &binStr);
00178
00179 virtual ~ROMol() { destroy(); };
00180
00181
00182
00183
00184
00185
00186 unsigned int getNumAtoms(bool onlyHeavy=1) const;
00187
00188 GRAPH_NODE_TYPE getAtomWithIdx(unsigned int idx);
00189
00190 GRAPH_NODE_CONST_TYPE getAtomWithIdx(unsigned int idx) const;
00191
00192 unsigned int getAtomDegree(const Atom *at) const;
00193
00194 unsigned int getAtomDegree(ATOM_SPTR at) const;
00195
00196
00197
00198
00199
00200
00201 unsigned int getNumBonds(bool onlyHeavy=1) const;
00202
00203 GRAPH_EDGE_TYPE getBondWithIdx(unsigned int idx);
00204
00205 GRAPH_EDGE_CONST_TYPE getBondWithIdx(unsigned int idx) const;
00206
00207 GRAPH_EDGE_TYPE getBondBetweenAtoms(unsigned int idx1,unsigned int idx2);
00208
00209 GRAPH_EDGE_CONST_TYPE getBondBetweenAtoms(unsigned int idx1,unsigned int idx2) const;
00210
00211
00212
00213
00214
00215
00216
00217 void setAtomBookmark(ATOM_SPTR at,int mark) {d_atomBookmarks[mark].push_back(at.get());};
00218
00219 void setAtomBookmark(Atom *at,int mark) {d_atomBookmarks[mark].push_back(at);};
00220
00221 GRAPH_NODE_TYPE getAtomWithBookmark(int mark);
00222
00223 ATOM_PTR_LIST &getAllAtomsWithBookmark(int mark);
00224
00225 void clearAtomBookmark(const int mark);
00226
00227 void clearAtomBookmark(const int mark,const Atom *atom);
00228
00229 void clearAtomBookmark(const int mark,ATOM_SPTR atom) {clearAtomBookmark(mark,atom.get());};
00230
00231 void clearAllAtomBookmarks() { d_atomBookmarks.clear(); };
00232
00233 bool hasAtomBookmark(int mark) const {return d_atomBookmarks.count(mark);};
00234
00235 ATOM_BOOKMARK_MAP *getAtomBookmarks() { return &d_atomBookmarks; };
00236
00237
00238 void setBondBookmark(BOND_SPTR bond,int mark) {d_bondBookmarks[mark].push_back(bond.get());};
00239
00240 void setBondBookmark(Bond *bond,int mark) {d_bondBookmarks[mark].push_back(bond);};
00241
00242 GRAPH_EDGE_TYPE getBondWithBookmark(int mark);
00243
00244 BOND_PTR_LIST &getAllBondsWithBookmark(int mark);
00245
00246 void clearBondBookmark(int mark);
00247
00248 void clearBondBookmark(int mark,const Bond *bond);
00249
00250 void clearBondBookmark(int mark,BOND_SPTR bond) {clearBondBookmark(mark,bond.get());};
00251
00252 void clearAllBondBookmarks() { d_bondBookmarks.clear(); };
00253
00254 bool hasBondBookmark(int mark) {return d_bondBookmarks.count(mark);};
00255
00256 BOND_BOOKMARK_MAP *getBondBookmarks() { return &d_bondBookmarks; };
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266 const Conformer &getConformer(int id=-1) const;
00267
00268
00269
00270 Conformer &getConformer(int id=-1);
00271
00272
00273 void removeConformer(unsigned int id);
00274
00275
00276 void clearConformers() {d_confs.clear();}
00277
00278
00279
00280
00281
00282
00283
00284
00285 unsigned int addConformer(Conformer * conf, bool assignId=false);
00286
00287 inline unsigned int getNumConformers() const {
00288 return d_confs.size();
00289 }
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299 RingInfo *getRingInfo() const { return dp_ringInfo; };
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323 ADJ_ITER_PAIR getAtomNeighbors(Atom const *at) const;
00324
00325 ADJ_ITER_PAIR getAtomNeighbors(ATOM_SPTR at) const;
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360 OBOND_ITER_PAIR getAtomBonds(Atom const *at) const;
00361
00362 GRAPH_MOL_ATOM_PMAP::type getAtomPMap();
00363
00364 GRAPH_MOL_BOND_PMAP::type getBondPMap();
00365
00366 GRAPH_MOL_ATOM_PMAP::const_type getAtomPMap() const;
00367
00368 GRAPH_MOL_BOND_PMAP::const_type getBondPMap() const;
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386 ATOM_ITER_PAIR getVertices();
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403 BOND_ITER_PAIR getEdges();
00404
00405 ATOM_ITER_PAIR getVertices() const;
00406
00407 BOND_ITER_PAIR getEdges() const;
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422 MolGraph const *getTopology() const { return &d_graph; };
00423
00424
00425
00426
00427
00428
00429
00430
00431 AtomIterator beginAtoms();
00432
00433 ConstAtomIterator beginAtoms() const;
00434
00435 AtomIterator endAtoms();
00436
00437 ConstAtomIterator endAtoms() const;
00438
00439
00440 AromaticAtomIterator beginAromaticAtoms();
00441
00442 ConstAromaticAtomIterator beginAromaticAtoms() const;
00443
00444 AromaticAtomIterator endAromaticAtoms();
00445
00446 ConstAromaticAtomIterator endAromaticAtoms() const;
00447
00448
00449 HeteroatomIterator beginHeteros();
00450
00451 ConstHeteroatomIterator beginHeteros() const;
00452
00453 HeteroatomIterator endHeteros();
00454
00455 ConstHeteroatomIterator endHeteros() const;
00456
00457
00458 QueryAtomIterator beginQueryAtoms(QueryAtom const *query);
00459
00460 ConstQueryAtomIterator beginQueryAtoms(QueryAtom const *) const;
00461
00462 QueryAtomIterator endQueryAtoms();
00463
00464 ConstQueryAtomIterator endQueryAtoms() const;
00465
00466
00467 BondIterator beginBonds();
00468
00469 ConstBondIterator beginBonds() const;
00470
00471 BondIterator endBonds();
00472
00473 ConstBondIterator endBonds() const;
00474
00475 inline ConformerIterator beginConformers() {
00476 return d_confs.begin();
00477 }
00478
00479 inline ConformerIterator endConformers() {
00480 return d_confs.end();
00481 }
00482
00483 inline ConstConformerIterator beginConformers() const {
00484 return d_confs.begin();
00485 }
00486
00487 inline ConstConformerIterator endConformers() const {
00488 return d_confs.end();
00489 }
00490
00491
00492
00493
00494
00495
00496
00497 STR_VECT getPropList(bool includePrivate=true,
00498 bool includeComputed=true) const {
00499 const STR_VECT &tmp=dp_props->keys();
00500 STR_VECT res,computed;
00501 if(!includeComputed && hasProp("computedProps")){
00502 getProp("computedProps",computed);
00503 computed.push_back("computedProps");
00504 }
00505
00506 STR_VECT::const_iterator pos = tmp.begin();
00507 while(pos!=tmp.end()){
00508 if((includePrivate || (*pos)[0]!='_') &&
00509 std::find(computed.begin(),computed.end(),*pos)==computed.end()){
00510 res.push_back(*pos);
00511 }
00512 pos++;
00513 }
00514 return res;
00515 }
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527 template <typename T>
00528 void setProp(const char *key, T val, bool computed=false) const {
00529 std::string what(key);
00530 setProp(what,val, computed);
00531 }
00532
00533 template <typename T>
00534 void setProp(const std::string key, T val, bool computed=false) const {
00535 if (computed) {
00536 STR_VECT compLst;
00537 getProp("computedProps", compLst);
00538 if (std::find(compLst.begin(), compLst.end(), key) == compLst.end()) {
00539 compLst.push_back(key);
00540 dp_props->setVal("computedProps", compLst);
00541 }
00542 }
00543 dp_props->setVal(key, val);
00544 }
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560 template <typename T>
00561 void getProp(const char *key, T &res) const {
00562 dp_props->getVal(key, res);
00563 }
00564
00565 template <typename T>
00566 void getProp(const std::string key, T &res) const {
00567
00568 dp_props->getVal(key, res);
00569 }
00570
00571
00572 bool hasProp(const char *key) const {
00573 if (!dp_props) return false;
00574 return dp_props->hasVal(key);
00575 }
00576
00577 bool hasProp(const std::string key) const {
00578 if (!dp_props) return false;
00579 return dp_props->hasVal(key);
00580
00581 }
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591 void clearProp(const char *key) const {
00592 std::string what(key);
00593 clearProp(what);
00594 };
00595
00596 void clearProp(const std::string key) const {
00597 STR_VECT compLst;
00598 getProp("computedProps", compLst);
00599 STR_VECT_I svi = std::find(compLst.begin(), compLst.end(), key);
00600 if (svi != compLst.end()) {
00601 compLst.erase(svi);
00602 dp_props->setVal("computedProps", compLst);
00603 }
00604
00605 dp_props->clearVal(key);
00606 };
00607
00608
00609 void clearComputedProps(bool includeRings=true) const;
00610
00611
00612
00613
00614
00615 void updatePropertyCache(bool strict=true);
00616
00617
00618
00619
00620
00621
00622
00623 void debugMol(std::ostream& str) const;
00624
00625
00626
00627 private:
00628 MolGraph d_graph;
00629 ATOM_BOOKMARK_MAP d_atomBookmarks;
00630 BOND_BOOKMARK_MAP d_bondBookmarks;
00631 Dict *dp_props;
00632 RingInfo *dp_ringInfo;
00633 CONF_SPTR_LIST d_confs;
00634 ROMol &operator=(const ROMol &);
00635
00636 #ifdef WIN32
00637 protected:
00638 #endif
00639 void initMol();
00640 virtual void destroy();
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651 unsigned int addAtom(Atom *atom,bool updateLabel=true,bool takeOwnership=false);
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665 unsigned int addAtom(ATOM_SPTR,bool updateLabel=true);
00666
00667
00668
00669
00670
00671
00672
00673
00674 unsigned int addBond(Bond *bond,bool takeOwnership=false);
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684 unsigned int addBond(BOND_SPTR bsp);
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694 void initFromOther(const ROMol &other,bool quickCopy);
00695
00696 };
00697
00698 typedef std::vector<ROMol> MOL_VECT;
00699 typedef boost::shared_ptr<ROMol> ROMOL_SPTR;
00700 typedef std::vector<ROMol *> MOL_PTR_VECT;
00701 typedef std::vector<ROMOL_SPTR> MOL_SPTR_VECT;
00702
00703 typedef MOL_PTR_VECT::const_iterator MOL_PTR_VECT_CI;
00704 typedef MOL_PTR_VECT::iterator MOL_PTR_VECT_I;
00705
00706 };
00707 #endif