BALL  1.4.1
smartsMatcher.h
Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 
00005 #ifndef BALL_STRUCTURE_SMARTSMATCHER_H
00006 #define BALL_STRUCTURE_SMARTSMATCHER_H
00007 
00008 #ifndef BALL_STRUCTURE_SMARTSPARSER_H
00009 # include <BALL/STRUCTURE/smartsParser.h>
00010 #endif
00011 
00012 #include <vector>
00013 #include <set>
00014 #include <map>
00015 
00016 namespace BALL 
00017 {
00018   // forward declaration
00019   class Molecule;
00020 
00042   class BALL_EXPORT SmartsMatcher
00043   {
00044     public: 
00045 
00049       typedef std::vector<std::set<const Atom*> > Match;
00051 
00052 
00056 
00057       SmartsMatcher();
00058 
00060       virtual ~SmartsMatcher();
00062 
00063 
00067 
00068       void match(Match& matches, Molecule& mol, const String& smarts)
00069         throw(Exception::ParseError);
00070 
00072       void match(Match& matches, Molecule& mol, const String& smarts, const std::set<const Atom*>& start_atoms)
00073         throw(Exception::ParseError);
00074 
00076       void match(std::vector<Match>& matches, Molecule& mol, const std::vector<String>& smarts)
00077         throw(Exception::ParseError);
00078 
00080       void match(std::vector<Match>& matches, Molecule& mol, const std::vector<String>& smarts, const std::set<const Atom*>& start_atoms)
00081         throw(Exception::ParseError);
00082 
00084       void setSSSR(const std::vector<std::vector<Atom*> >& sssr);
00085 
00087       void unsetSSSR();
00089 
00090 
00091     private:
00092       
00094       SmartsMatcher(const SmartsMatcher& matcher);
00095 
00099 
00100       SmartsMatcher& operator = (const SmartsMatcher& matcher);
00102 
00103 
00104     protected:
00105 
00109       typedef SmartsParser::SPNode SPNode;
00110       typedef SmartsParser::SPEdge SPEdge;
00111       typedef SmartsParser::SPAtom SPAtom;
00112       typedef SmartsParser::SPBond SPBond;
00114 
00116       class RecStructCore_
00117       {
00118         public:
00119           
00121           RecStructCore_();
00122 
00124           RecStructCore_(const RecStructCore_&);
00125 
00127           virtual ~RecStructCore_();
00128 
00130           RecStructCore_& operator = (const RecStructCore_&);
00131 
00133           void clear();
00134       
00136           std::vector<std::set<const Atom*> > matched_atoms;
00137 
00139           std::vector<std::map<const SPNode*, const Atom*> > mapped_atoms;
00140 
00142           std::vector<std::set<const Atom*> > visited_atoms;
00143 
00145           std::vector<std::set<const Bond*> > visited_bonds;
00146 
00148           std::vector<std::set<const SPEdge*> > visited_edges;
00149 
00151           std::vector<std::pair<const SPNode*, const Atom*> > first_matches;
00152       };
00153 
00155       class RecStructPool_
00156       {
00157         public:
00158           
00160           RecStructPool_();
00161 
00163           virtual ~RecStructPool_();
00164 
00166           RecStructCore_* getNextFree();
00167 
00169           Position getLastPosition();
00170 
00172           void destroy(Position pos);
00173 
00174         private:
00175 
00177           void resize_();
00178 
00180           RecStructPool_(const RecStructPool_&);
00181 
00183           RecStructPool_& operator = (const RecStructPool_&);
00184   
00186           std::vector<RecStructCore_*> rec_struct_pool_;
00187 
00189           std::vector<Position> free_list_;
00190 
00192           Position last_position_;
00193       };
00194 
00195 
00197       class RecStruct_
00198       {
00199         private:
00201           RecStructCore_* rec_struct_core_;
00202       
00203         public:
00204           
00206           RecStruct_();
00207 
00209           RecStruct_(const RecStruct_& rec_struct);
00210 
00212           virtual ~RecStruct_();
00213 
00215           RecStruct_& operator = (const RecStruct_&); 
00216 
00218           std::vector<std::set<const Atom*> >& matched_atoms;
00219 
00221           std::vector<std::map<const SPNode*, const Atom*> >& mapped_atoms;
00222 
00224           std::vector<std::set<const Atom*> >& visited_atoms;
00225 
00227           std::vector<std::set<const Bond*> >& visited_bonds;
00228 
00230           std::vector<std::set<const SPEdge*> >& visited_edges;
00231 
00233           std::vector<std::pair<const SPNode*, const Atom*> >& first_matches;
00234 
00236           void add(const RecStruct_& rec_struct);
00237 
00239           void add(const RecStruct_& rec_struct, Size i);
00240 
00242           void clear();
00243 
00245           void dump(const String& name, Size depth_ = 0);
00246 
00247         private:
00248         
00250           Position pos_;
00251       };
00252 
00254       static RecStructPool_* pool_;
00255 
00257       bool evaluateRingEdges_(const std::set<const Atom*>& matching, const std::map<const SPNode*, const Atom*>& mapping, const String& smarts);
00258       
00260       void evaluate_( RecStruct_& rs, SPNode* start_node, const Atom* start_atom);
00261   
00263       bool evaluate_node_(RecStruct_& rs, SPNode* start_node, const Atom* start_atom);
00264   
00266       bool evaluate_edge_(RecStruct_& rs, SPEdge* start_node, const Atom* start_atom, const Bond* start_bond);
00267 
00269       std::map<SPNode*, std::vector<std::set<const Atom*> > > rec_matches_;
00270 
00272       bool has_user_sssr_;
00273 
00275       std::vector<std::vector<Atom*> > sssr_;
00276 
00277       // debug output depth
00278       Size depth_;
00279   };
00280   
00281 } // namespace BALL
00282 
00283 #endif // BALL_STRUCTURE_SMARTSMATCHER_H
00284 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines