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