Canon.h

Go to the documentation of this file.
00001 //
00002 //  Copyright (C) 2004-2006 Rational Discovery LLC
00003 //
00004 //   @@ All Rights Reserved @@
00005 //  This file is part of the RDKit.
00006 //  The contents are covered by the terms of the BSD license
00007 //  which is included in the file license.txt, found at the root
00008 //  of the RDKit source tree.
00009 //
00010 #ifndef _RD_CANON_H_
00011 #define _RD_CANON_H_
00012 
00013 namespace RDKit {
00014   class ROMol;
00015   class Atom;
00016   class Bond;
00017 };
00018 
00019 namespace Canon {
00020   const int MAX_NATOMS=5000; //!< used in the canonical traversal code
00021   const int MAX_CYCLES=99;   //!< used in the canonical traversal code
00022 
00023   //! used in traversals of the molecule
00024   typedef enum {
00025     WHITE_NODE=0,  //! not visited
00026     GREY_NODE,     //! visited, but not finished
00027     BLACK_NODE,    //! visited and finished
00028   } AtomColors; 
00029 
00030   //! used to indicate types of entries in the molecular stack:
00031   typedef enum {
00032     MOL_STACK_ATOM=0,       //!< an Atom
00033     MOL_STACK_BOND,         //!< a Bond
00034     MOL_STACK_RING,         //!< a ring closure
00035     MOL_STACK_BRANCH_OPEN,  //!< beginning of a branch
00036     MOL_STACK_BRANCH_CLOSE, //!< end of a branch
00037   } MolStackTypes;
00038 
00039   //! used to store components in the molecular stack
00040   typedef union{
00041     RDKit::Atom *atom;
00042     RDKit::Bond *bond;
00043   } MolStackUnion;
00044 
00045   //! these are the actual elements in the molecular stack
00046   class MolStackElem {
00047   public:
00048     //! construct an Atom node
00049     explicit MolStackElem(RDKit::Atom *at) {
00050       type = MOL_STACK_ATOM;
00051       obj.atom = at;
00052     };
00053     //! construct a bond node
00054     /*!
00055 
00056        \param bond  pointer to the Bond being added
00057        \param idx   index of the Atom traversed before this Bond
00058          (beginAtom in the canonical traversal order)
00059     */
00060     explicit MolStackElem(RDKit::Bond *bond,int idx) {
00061       type = MOL_STACK_BOND;
00062       obj.bond = bond;
00063       number = idx;
00064     };
00065     //! construct for a ring closure
00066     explicit MolStackElem(int idx) {
00067       type = MOL_STACK_RING;
00068       number = idx;
00069     };
00070     //! construct for a branch opening or closing
00071     explicit MolStackElem(const char *chr,int idx) {
00072       switch(chr[0]){
00073       case '(':
00074         type = MOL_STACK_BRANCH_OPEN;
00075         break;
00076       case ')':
00077         type = MOL_STACK_BRANCH_CLOSE;
00078         break;
00079       default:
00080         break;
00081       }
00082       number=idx;
00083     }
00084     MolStackTypes type; //!< stores the type of node
00085     MolStackUnion obj;  //!< holds our pointer (if appropriate)
00086     int number;         //!< stores our number (relevant for bonds and ring closures)
00087   };
00088   typedef std::vector<MolStackElem> MolStack;
00089 
00090 
00091   //! used to represent possible branches from an atom
00092   typedef std::pair< int, std::pair< int, RDKit::Bond * > > PossibleType;
00093   //! returns a PossibleType
00094   PossibleType makePossible(int rank,int atomIdx,RDKit::Bond *bond);
00095   //! compare two PossibleTypes
00096   int _possibleComp(const PossibleType &arg1,const PossibleType &arg2);
00097 
00098   //! constructs the canonical traversal order for a molecular fragment
00099   /*!
00100 
00101     \param mol       the ROMol we're working on
00102     \param atomIdx   the index of the atom to start the traversal from
00103     \param colors    the traversal status of each atom in \c mol
00104     \param ranks     the assigned rank of each atom in \c mol
00105     \param molStack  the current traversal stack (used to return the results)
00106 
00107     <b>Notes</b>
00108       - \c mol will, in general, be modified by this operation as bond directions
00109         and the like are changed to fit the canonical traversal order
00110 
00111    */
00112   void canonicalizeFragment(RDKit::ROMol &mol,int atomIdx,
00113                             std::vector<AtomColors> &colors,
00114                             std::vector<int> &ranks,
00115                             MolStack &molStack);
00116 
00117 };
00118 
00119 #endif