Loading...
Searching...
No Matches
parsmart.h
Go to the documentation of this file.
/**********************************************************************
parsmart.h - Daylight SMARTS parser.
Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
Some portions Copyright (C) 2001-2005 by Geoffrey R. Hutchison
This file is part of the Open Babel project.
For more information, see <http://openbabel.org/>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
***********************************************************************/
#ifndef OB_PARSMART_H
#define OB_PARSMART_H
#include <string>
#include <vector>
#include <openbabel/babelconfig.h>
/*==========================*/
/* SMARTS Data Structures */
/*==========================*/
namespace OpenBabel
{
class OBMol;
class OBAtom;
class OBBond;
class OBBitVec;
// mark this so that SWIG will not attempt to wrap for scripting languages
#ifndef SWIG
typedef union _AtomExpr {
int type;
struct
{
int type;
int value;
}
leaf;
struct
{
int type;
void *recur;
}
recur;
struct
{
int type;
union _AtomExpr *arg;
}
mon;
struct
{
int type;
union _AtomExpr *lft;
union _AtomExpr *rgt;
}
bin;
} AtomExpr;
typedef union _BondExpr {
int type;
struct
{
int type;
union _BondExpr *arg;
}
mon;
struct
{
int type;
union _BondExpr *lft;
union _BondExpr *rgt;
}
bin;
} BondExpr;
typedef struct
{
BondExpr *expr;
int src,dst;
int visit;
bool grow;
}
BondSpec;
typedef struct
{
AtomExpr *expr;
int visit;
int part;
int chiral_flag;
int vb;
std::vector<int> nbrs;
}
AtomSpec;
typedef struct
{
int aalloc,acount;
int balloc,bcount;
bool ischiral;
AtomSpec *atom;
BondSpec *bond;
int parts;
bool hasExplicitH;
}
Pattern;
typedef struct
{
BondExpr *closord[100];
int closure[100];
int closindex;
} ParseState;
#else
// for SWIG, just forward declare that we have some Pattern struct
// (but this is private and not wrapped for scripting languages)
struct Pattern;
#endif
class OBSmartsPrivate;
// class introduction in parsmart.cpp
class OBAPI OBSmartsPattern
{
protected:
OBSmartsPrivate *_d;
std::vector<bool> _growbond;
std::vector<std::vector<int> > _mlist;
Pattern *_pat;
std::string _str;
char *_buffer;
char *LexPtr;
char *MainPtr;
Pattern *ParseSMARTSPattern( void );
Pattern *ParseSMARTSPart( Pattern*, int );
Pattern *SMARTSError( Pattern *pat );
Pattern *ParseSMARTSError( Pattern *pat, BondExpr *expr );
AtomExpr *ParseSimpleAtomPrimitive( void );
AtomExpr *ParseComplexAtomPrimitive( void );
AtomExpr *ParseAtomExpr( int level );
BondExpr *ParseBondPrimitive( void );
BondExpr *ParseBondExpr( int level );
Pattern *ParseSMARTSString( char *ptr );
Pattern *ParseSMARTSRecord( char *ptr );
int GetVectorBinding();
Pattern *SMARTSParser( Pattern *pat, ParseState *stat,
int prev, int part );
public:
OBSmartsPattern() : _pat(nullptr), _buffer(nullptr), LexPtr(nullptr), MainPtr(nullptr) { }
virtual ~OBSmartsPattern();
OBSmartsPattern(const OBSmartsPattern& cp): _pat(nullptr), _buffer(nullptr), LexPtr(nullptr), MainPtr(nullptr)
{
*this = cp;
}
OBSmartsPattern& operator=(const OBSmartsPattern& cp)
{
if (this == &cp)
return *this;
if (_pat)
delete[] _pat;
if (_buffer)
delete[] _buffer;
_buffer = nullptr;
_pat = nullptr;
std::string s = cp._str;
Init(s);
return (*this);
}
bool Init(const char* pattern);
bool Init(const std::string& pattern);
const std::string &GetSMARTS() const { return _str; }
#ifndef SWIG
std::string &GetSMARTS() { return _str; }
#endif
bool Empty() const { return(_pat == nullptr); }
bool IsValid() const { return(_pat != nullptr); }
unsigned int NumAtoms() const
{
return _pat ? _pat->acount : 0;
}
unsigned int NumBonds() const
{
return _pat ? _pat->bcount : 0;
}
void GetBond(int& src,int& dst,int& ord,int idx);
int GetAtomicNum(int idx);
int GetCharge(int idx);
int GetVectorBinding(int idx) const
{
return(_pat->atom[idx].vb);
}
// number and kind of matches to return
enum MatchType {All, Single, AllUnique};
bool Match(OBMol &mol, bool single=false);
bool Match(OBMol &mol, std::vector<std::vector<int> > & mlist, MatchType mtype = All) const;
bool HasMatch(OBMol &mol) const;
bool RestrictedMatch(OBMol &mol, std::vector<std::pair<int,int> > &pairs, bool single=false);
bool RestrictedMatch(OBMol &mol, OBBitVec &bv, bool single=false);
unsigned int NumMatches() const
{
return static_cast<unsigned int>(_mlist.size());
}
std::vector<std::vector<int> > &GetMapList()
{
return(_mlist);
}
std::vector<std::vector<int> >::iterator BeginMList()
{
return(_mlist.begin());
}
std::vector<std::vector<int> >::iterator EndMList()
{
return(_mlist.end());
}
std::vector<std::vector<int> > &GetUMapList();
void WriteMapList(std::ostream&);
};
class OBAPI OBSmartsMatcher
{
protected:
//recursive smarts cache
std::vector<std::pair<const Pattern*,std::vector<bool> > > RSCACHE;
// list of fragment patterns (e.g., (*).(*)
std::vector<const Pattern*> Fragments;
/*
bool EvalAtomExpr(AtomExpr *expr,OBAtom *atom);
bool EvalBondExpr(BondExpr *expr,OBBond *bond);
int GetVectorBinding();
int CreateAtom(Pattern*,AtomExpr*,int,int vb=0);
*/
bool EvalAtomExpr(AtomExpr *expr,OBAtom *atom);
bool EvalBondExpr(BondExpr *expr,OBBond *bond);
void SetupAtomMatchTable(std::vector<std::vector<bool> > &ttab,
const Pattern *pat, OBMol &mol);
void FastSingleMatch(OBMol &mol,const Pattern *pat,
std::vector<std::vector<int> > &mlist);
friend class OBSSMatch;
public:
OBSmartsMatcher() {}
virtual ~OBSmartsMatcher() {}
bool match(OBMol &mol, const Pattern *pat,std::vector<std::vector<int> > &mlist,bool single=false);
};
class OBAPI OBSSMatch
{
protected:
bool *_uatoms;
OBMol *_mol;
const Pattern *_pat;
std::vector<int> _map;
public:
OBSSMatch(OBMol&,const Pattern*);
~OBSSMatch();
void Match(std::vector<std::vector<int> > &v, int bidx=-1);
};
OBAPI void SmartsLexReplace(std::string &,
std::vector<std::pair<std::string,std::string> > &);
} // end namespace OpenBabel
#endif // OB_PARSMART_H
OpenBabel::OBAtom
Atom class.
Definition atom.h:72
OpenBabel::OBBitVec
A speed-optimized vector of bits.
Definition bitvec.h:58
OpenBabel::OBBond
Bond class.
Definition bond.h:59
OpenBabel::OBMol
Molecule Class.
Definition mol.h:121
OpenBabel::OBSSMatch::_map
std::vector< int > _map
Definition parsmart.h:376
OpenBabel::OBSSMatch::_mol
OBMol * _mol
Definition parsmart.h:374
OpenBabel::OBSSMatch::Match
void Match(std::vector< std::vector< int > > &v, int bidx=-1)
Definition parsmart.cpp:2324
OpenBabel::OBSSMatch::_pat
const Pattern * _pat
Definition parsmart.h:375
OpenBabel::OBSSMatch::OBSSMatch
OBSSMatch(OBMol &, const Pattern *)
Definition parsmart.cpp:2303
OpenBabel::OBSSMatch::_uatoms
bool * _uatoms
Definition parsmart.h:373
OpenBabel::OBSmartsMatcher::RSCACHE
std::vector< std::pair< const Pattern *, std::vector< bool > > > RSCACHE
Definition parsmart.h:342
OpenBabel::OBSmartsMatcher::OBSSMatch
friend class OBSSMatch
Definition parsmart.h:358
OpenBabel::OBSmartsMatcher::SetupAtomMatchTable
void SetupAtomMatchTable(std::vector< std::vector< bool > > &ttab, const Pattern *pat, OBMol &mol)
Definition parsmart.cpp:1885
OpenBabel::OBSmartsMatcher::~OBSmartsMatcher
virtual ~OBSmartsMatcher()
Definition parsmart.h:361
OpenBabel::OBSmartsMatcher::Fragments
std::vector< const Pattern * > Fragments
Definition parsmart.h:344
OpenBabel::OBSmartsMatcher::EvalBondExpr
bool EvalBondExpr(BondExpr *expr, OBBond *bond)
Definition parsmart.cpp:2202
OpenBabel::OBSmartsMatcher::FastSingleMatch
void FastSingleMatch(OBMol &mol, const Pattern *pat, std::vector< std::vector< int > > &mlist)
Definition parsmart.cpp:1902
OpenBabel::OBSmartsMatcher::OBSmartsMatcher
OBSmartsMatcher()
Definition parsmart.h:360
OpenBabel::OBSmartsMatcher::EvalAtomExpr
bool EvalAtomExpr(AtomExpr *expr, OBAtom *atom)
Definition parsmart.cpp:2106
OpenBabel::OBSmartsPattern
SMARTS (SMiles ARbitrary Target Specification) substructure searching.
Definition parsmart.h:155
OpenBabel::OBSmartsPattern::GetVectorBinding
int GetVectorBinding(int idx) const
Definition parsmart.h:254
OpenBabel::OBSmartsPattern::ParseAtomExpr
AtomExpr * ParseAtomExpr(int level)
Definition parsmart.cpp:1158
OpenBabel::OBSmartsPattern::LexPtr
char * LexPtr
Definition parsmart.h:164
OpenBabel::OBSmartsPattern::MainPtr
char * MainPtr
Definition parsmart.h:165
OpenBabel::OBSmartsPattern::NumAtoms
unsigned int NumAtoms() const
Definition parsmart.h:232
OpenBabel::OBSmartsPattern::_d
OBSmartsPrivate * _d
Internal data storage for future expansion.
Definition parsmart.h:157
OpenBabel::OBSmartsPattern::ParseComplexAtomPrimitive
AtomExpr * ParseComplexAtomPrimitive(void)
Definition parsmart.cpp:640
OpenBabel::OBSmartsPattern::SMARTSError
Pattern * SMARTSError(Pattern *pat)
Definition parsmart.cpp:568
OpenBabel::OBSmartsPattern::_str
std::string _str
The string of the SMARTS expression.
Definition parsmart.h:161
OpenBabel::OBSmartsPattern::ParseSimpleAtomPrimitive
AtomExpr * ParseSimpleAtomPrimitive(void)
Definition parsmart.cpp:579
OpenBabel::OBSmartsPattern::operator=
OBSmartsPattern & operator=(const OBSmartsPattern &cp)
Definition parsmart.h:191
OpenBabel::OBSmartsPattern::ParseSMARTSPart
Pattern * ParseSMARTSPart(Pattern *, int)
Definition parsmart.cpp:1600
OpenBabel::OBSmartsPattern::NumMatches
unsigned int NumMatches() const
Definition parsmart.h:293
OpenBabel::OBSmartsPattern::GetSMARTS
const std::string & GetSMARTS() const
Definition parsmart.h:221
OpenBabel::OBSmartsPattern::EndMList
std::vector< std::vector< int > >::iterator EndMList()
Definition parsmart.h:310
OpenBabel::OBSmartsPattern::ParseBondExpr
BondExpr * ParseBondExpr(int level)
Definition parsmart.cpp:1258
OpenBabel::OBSmartsPattern::NumBonds
unsigned int NumBonds() const
Definition parsmart.h:237
OpenBabel::OBSmartsPattern::MatchType
MatchType
Definition parsmart.h:261
OpenBabel::OBSmartsPattern::All
@ All
Definition parsmart.h:261
OpenBabel::OBSmartsPattern::Single
@ Single
Definition parsmart.h:261
OpenBabel::OBSmartsPattern::AllUnique
@ AllUnique
Definition parsmart.h:261
OpenBabel::OBSmartsPattern::_growbond
std::vector< bool > _growbond
Definition parsmart.h:158
OpenBabel::OBSmartsPattern::BeginMList
std::vector< std::vector< int > >::iterator BeginMList()
Definition parsmart.h:305
OpenBabel::OBSmartsPattern::GetMapList
std::vector< std::vector< int > > & GetMapList()
Definition parsmart.h:300
OpenBabel::OBSmartsPattern::OBSmartsPattern
OBSmartsPattern()
Definition parsmart.h:183
OpenBabel::OBSmartsPattern::ParseSMARTSError
Pattern * ParseSMARTSError(Pattern *pat, BondExpr *expr)
Definition parsmart.cpp:1350
OpenBabel::OBSmartsPattern::_mlist
std::vector< std::vector< int > > _mlist
The list of matches.
Definition parsmart.h:159
OpenBabel::OBSmartsPattern::Init
bool Init(const char *pattern)
Definition parsmart.cpp:1710
OpenBabel::OBSmartsPattern::Empty
bool Empty() const
Definition parsmart.h:227
OpenBabel::OBSmartsPattern::GetVectorBinding
int GetVectorBinding()
Definition parsmart.cpp:1335
OpenBabel::OBSmartsPattern::IsValid
bool IsValid() const
Definition parsmart.h:229
OpenBabel::OBSmartsPattern::SMARTSParser
Pattern * SMARTSParser(Pattern *pat, ParseState *stat, int prev, int part)
Definition parsmart.cpp:1357
OpenBabel::OBSmartsPattern::ParseSMARTSRecord
Pattern * ParseSMARTSRecord(char *ptr)
Definition parsmart.cpp:1688
OpenBabel::OBSmartsPattern::ParseSMARTSString
Pattern * ParseSMARTSString(char *ptr)
Definition parsmart.cpp:1674
OpenBabel::OBSmartsPattern::GetSMARTS
std::string & GetSMARTS()
Definition parsmart.h:224
OpenBabel::OBSmartsPattern::_pat
Pattern * _pat
The parsed SMARTS pattern.
Definition parsmart.h:160
OpenBabel::OBSmartsPattern::ParseBondPrimitive
BondExpr * ParseBondPrimitive(void)
Definition parsmart.cpp:1235
OpenBabel::OBSmartsPattern::OBSmartsPattern
OBSmartsPattern(const OBSmartsPattern &cp)
Definition parsmart.h:186
OpenBabel::OBSmartsPattern::ParseSMARTSPattern
Pattern * ParseSMARTSPattern(void)
Definition parsmart.cpp:1640
OpenBabel::OBSmartsPattern::_buffer
char * _buffer
Definition parsmart.h:163
OpenBabel
Global namespace for all Open Babel code.
Definition alias.h:23
OpenBabel::ttab
OB_EXTERN OBTypeTable ttab
Definition data.h:225
OpenBabel::AtomExpr
union OpenBabel::_AtomExpr AtomExpr
OpenBabel::BondExpr
union OpenBabel::_BondExpr BondExpr
OpenBabel::SmartsLexReplace
void SmartsLexReplace(std::string &, std::vector< std::pair< std::string, std::string > > &)
Definition parsmart.cpp:2501
OpenBabel::AtomSpec
An internal (SMARTS parser) atom specification.
Definition parsmart.h:107
OpenBabel::AtomSpec::expr
AtomExpr * expr
Definition parsmart.h:108
OpenBabel::AtomSpec::part
int part
Definition parsmart.h:110
OpenBabel::AtomSpec::nbrs
std::vector< int > nbrs
Definition parsmart.h:113
OpenBabel::AtomSpec::vb
int vb
Definition parsmart.h:112
OpenBabel::AtomSpec::visit
int visit
Definition parsmart.h:109
OpenBabel::AtomSpec::chiral_flag
int chiral_flag
Definition parsmart.h:111
OpenBabel::BondSpec
An internal (SMARTS parser) bond specification.
Definition parsmart.h:96
OpenBabel::BondSpec::dst
int dst
Definition parsmart.h:98
OpenBabel::BondSpec::grow
bool grow
Definition parsmart.h:100
OpenBabel::BondSpec::expr
BondExpr * expr
Definition parsmart.h:97
OpenBabel::BondSpec::visit
int visit
Definition parsmart.h:99
OpenBabel::BondSpec::src
int src
Definition parsmart.h:98
OpenBabel::ParseState
A SMARTS parser internal state.
Definition parsmart.h:134
OpenBabel::ParseState::closindex
int closindex
Definition parsmart.h:137
OpenBabel::ParseState::closord
BondExpr * closord[100]
Definition parsmart.h:135
OpenBabel::ParseState::closure
int closure[100]
Definition parsmart.h:136
OpenBabel::Pattern
A SMARTS parser internal pattern.
Definition parsmart.h:120
OpenBabel::Pattern::bcount
int bcount
Definition parsmart.h:122
OpenBabel::Pattern::bond
BondSpec * bond
Definition parsmart.h:125
OpenBabel::Pattern::ischiral
bool ischiral
Definition parsmart.h:123
OpenBabel::Pattern::parts
int parts
Definition parsmart.h:126
OpenBabel::Pattern::balloc
int balloc
Definition parsmart.h:122
OpenBabel::Pattern::aalloc
int aalloc
Definition parsmart.h:121
OpenBabel::Pattern::acount
int acount
Definition parsmart.h:121
OpenBabel::Pattern::hasExplicitH
bool hasExplicitH
Definition parsmart.h:127
OpenBabel::Pattern::atom
AtomSpec * atom
Definition parsmart.h:124
OpenBabel::_AtomExpr
An internal (SMARTS parser) atomic expression.
Definition parsmart.h:45
OpenBabel::_AtomExpr::leaf
struct OpenBabel::_AtomExpr::@143060066250266246312322250251142067140267233336 leaf
OpenBabel::_AtomExpr::mon
struct OpenBabel::_AtomExpr::@351052335320342171320327157151136325161252226035 mon
OpenBabel::_AtomExpr::rgt
union _AtomExpr * rgt
Definition parsmart.h:69
OpenBabel::_AtomExpr::bin
struct OpenBabel::_AtomExpr::@047222304013363336305144163114324266110300257353 bin
OpenBabel::_AtomExpr::value
int value
Definition parsmart.h:50
OpenBabel::_AtomExpr::type
int type
Definition parsmart.h:46
OpenBabel::_AtomExpr::lft
union _AtomExpr * lft
Definition parsmart.h:68
OpenBabel::_AtomExpr::recur
void * recur
Definition parsmart.h:56
OpenBabel::_AtomExpr::arg
union _AtomExpr * arg
Definition parsmart.h:62
OpenBabel::_BondExpr
An internal (SMARTS parser) bond expression.
Definition parsmart.h:76
OpenBabel::_BondExpr::bin
struct OpenBabel::_BondExpr::@127077355217324124136206224140324073323142334330 bin
OpenBabel::_BondExpr::mon
struct OpenBabel::_BondExpr::@205212017026137361141022056345165166206244314374 mon
OpenBabel::_BondExpr::rgt
union _BondExpr * rgt
Definition parsmart.h:88
OpenBabel::_BondExpr::type
int type
Definition parsmart.h:77
OpenBabel::_BondExpr::lft
union _BondExpr * lft
Definition parsmart.h:87
OpenBabel::_BondExpr::arg
union _BondExpr * arg
Definition parsmart.h:81