parsmart.h

Go to the documentation of this file.
00001 /**********************************************************************
00002 parsmart.h - Daylight SMARTS parser.
00003  
00004 Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
00005 Some portions Copyright (C) 2001-2005 by Geoffrey R. Hutchison
00006  
00007 This file is part of the Open Babel project.
00008 For more information, see <http://openbabel.sourceforge.net/>
00009  
00010 This program is free software; you can redistribute it and/or modify
00011 it under the terms of the GNU General Public License as published by
00012 the Free Software Foundation version 2 of the License.
00013  
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 ***********************************************************************/
00019 
00020 #ifndef OB_PARSMART_H
00021 #define OB_PARSMART_H
00022 
00023 #include <string>
00024 #include <vector>
00025 
00026 #include <openbabel/mol.h>
00027 
00028 /*==========================*/
00029 /*  SMARTS Data Structures  */
00030 /*==========================*/
00031 
00032 namespace OpenBabel
00033 {
00034 
00035   // mark this so that SWIG will not attempt to wrap for scripting languages
00036 
00037 #ifndef SWIG
00038 
00041   typedef union _AtomExpr {
00042     int type;
00043     struct
00044     {
00045       int type;
00046       int prop;
00047       int value;
00048     }
00049       leaf;
00050     struct
00051     {
00052       int type;
00053       void *recur;
00054     }
00055       recur;
00056     struct
00057     {
00058       int type;
00059       union _AtomExpr *arg;
00060     }
00061       mon;
00062     struct
00063     {
00064       int type;
00065       union _AtomExpr *lft;
00066       union _AtomExpr *rgt;
00067     }
00068       bin;
00069   } AtomExpr;
00070 
00071 #define BE_LEAF      0x01
00072 #define BE_ANDHI     0x02
00073 #define BE_ANDLO     0x03
00074 #define BE_NOT       0x04
00075 #define BE_OR        0x05
00076 
00077 #define BL_CONST     0x01
00078 #define BL_TYPE      0x02
00079 
00080 #define BT_SINGLE     0x01
00081 #define BT_DOUBLE     0x02
00082 #define BT_TRIPLE     0x03
00083 #define BT_AROM       0x04
00084 #define BT_UP         0x05
00085 #define BT_DOWN       0x06
00086 #define BT_UPUNSPEC   0x07
00087 #define BT_DOWNUNSPEC 0x08
00088 #define BT_RING       0x09
00089 
00092   typedef union _BondExpr {
00093     int type;
00094     struct
00095     {
00096       int type;
00097       int prop;
00098       int value;
00099     }
00100       leaf;
00101     struct
00102     {
00103       int type;
00104       union _BondExpr *arg;
00105     }
00106       mon;
00107     struct
00108     {
00109       int type;
00110       union _BondExpr *lft;
00111       union _BondExpr *rgt;
00112     }
00113       bin;
00114   } BondExpr;
00115 
00118   typedef struct
00119   {
00120     BondExpr *expr;
00121     int src,dst;
00122     int visit;
00123     bool grow;
00124   }
00125   BondSpec;
00126 
00129   typedef struct
00130   {
00131     AtomExpr *expr;
00132     int visit;
00133     int part;
00134     int chiral_flag;
00135     int vb;
00136   }
00137   AtomSpec;
00138 
00141   typedef struct
00142   {
00143     int aalloc,acount;
00144     int balloc,bcount;
00145     bool ischiral;
00146     AtomSpec *atom;
00147     BondSpec *bond;
00148     int parts;
00149   }
00150   Pattern;
00151 #else
00152   // for SWIG, just forward declare that we have some Pattern struct
00153   // (but this is private and not wrapped for scripting languages)
00154   struct Pattern;
00155 #endif
00156 
00157   // class introduction in parsmart.cpp
00159   class OBAPI OBSmartsPattern
00160   {
00161   protected:
00162     std::vector<bool>                     _growbond; 
00163     std::vector<std::vector<int> >      _mlist;    
00164     Pattern                        *_pat;      
00165     std::string                                       _str;      
00166 
00167   public:
00168     OBSmartsPattern() : _pat(NULL) { }
00169     virtual ~OBSmartsPattern();
00170 
00171   OBSmartsPattern(const OBSmartsPattern& cp): _pat(NULL)
00172       {
00173         *this = cp;
00174       }
00175     OBSmartsPattern& operator=(const OBSmartsPattern& cp)
00176       {
00177         if (_pat)
00178           delete [] _pat;
00179         _pat = NULL;
00180         std::string s = cp._str;
00181         Init(s);
00182         return (*this);
00183       }
00184     
00186 
00187 
00188 
00189     bool         Init(const char* pattern);
00192     bool         Init(const std::string& pattern);
00194 
00196 
00197 
00198     const std::string &GetSMARTS() const    {      return _str;    }
00200     std::string  &GetSMARTS()               {      return _str;    }
00201 
00203     bool         Empty() const     {      return(_pat == NULL);    }
00205     bool         IsValid() const   {      return(_pat != NULL);    }
00206 
00208     unsigned int NumAtoms()   const
00209     {
00210       return _pat ? _pat->acount : 0;
00211     }
00213     unsigned int NumBonds()   const
00214     {
00215       return _pat ? _pat->bcount : 0;
00216     }
00217 
00223     void         GetBond(int& src,int& dst,int& ord,int idx);
00225     int          GetAtomicNum(int idx);
00227     int          GetCharge(int idx);
00228 
00230     int          GetVectorBinding(int idx) const
00231     {
00232       return(_pat->atom[idx].vb);
00233     }
00235 
00237 
00238 
00239 
00240 
00241 
00242     bool Match(OBMol &mol, bool single=false);
00243 
00244     bool RestrictedMatch(OBMol &mol, std::vector<std::pair<int,int> > &pairs, bool single=false);
00245 
00246     bool RestrictedMatch(OBMol &mol, OBBitVec &bv, bool single=false);
00249     unsigned int NumMatches() const
00250     {
00251       return (unsigned int)_mlist.size();
00252     }
00253 
00256     std::vector<std::vector<int> > &GetMapList()
00257       {
00258         return(_mlist);
00259       }
00261     std::vector<std::vector<int> >::iterator BeginMList()
00262       {
00263         return(_mlist.begin());
00264       }
00266     std::vector<std::vector<int> >::iterator EndMList()
00267       {
00268         return(_mlist.end());
00269       }
00270 
00272 
00282     std::vector<std::vector<int> > &GetUMapList();
00284 
00286     void         WriteMapList(std::ostream&);
00287   };
00288 
00292   class OBAPI OBSSMatch
00293   {
00294   protected:
00295     bool        *_uatoms;
00296     OBMol       *_mol;
00297     Pattern     *_pat;
00298     std::vector<int>  _map;
00299 
00300   public:
00301     OBSSMatch(OBMol&,Pattern*);
00302     ~OBSSMatch();
00303     void Match(std::vector<std::vector<int> > &v, int bidx=-1);
00304   };
00305 
00306   OBAPI void SmartsLexReplace(std::string &,
00307                               std::vector<std::pair<std::string,std::string> > &);
00308 
00309 } // end namespace OpenBabel
00310 
00311 #endif // OB_PARSMART_H
00312