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