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