00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef BALL_STRUCTURE_SMARTSMATCHER_H
00011 #define BALL_STRUCTURE_SMARTSMATCHER_H
00012
00013 #ifndef BALL_STRUCTURE_SMARTSPARSER_H
00014 # include <BALL/STRUCTURE/smartsParser.h>
00015 #endif
00016
00017 #include <vector>
00018 #include <set>
00019 #include <map>
00020
00021 namespace BALL
00022 {
00023
00024 class Molecule;
00025
00047 class BALL_EXPORT SmartsMatcher
00048 {
00049 public:
00050
00054 typedef std::vector<std::set<const Atom*> > Match;
00056
00057
00061
00062 SmartsMatcher();
00063
00065 virtual ~SmartsMatcher();
00067
00068
00072
00073 void match(Match& matches, Molecule& mol, const String& smarts)
00074 throw(Exception::ParseError);
00075
00077 void match(Match& matches, Molecule& mol, const String& smarts, const std::set<const Atom*>& start_atoms)
00078 throw(Exception::ParseError);
00079
00081 void match(std::vector<Match>& matches, Molecule& mol, const std::vector<String>& smarts)
00082 throw(Exception::ParseError);
00083
00085 void match(std::vector<Match>& matches, Molecule& mol, const std::vector<String>& smarts, const std::set<const Atom*>& start_atoms)
00086 throw(Exception::ParseError);
00087
00089 void setSSSR(const std::vector<std::vector<Atom*> >& sssr);
00090
00092 void unsetSSSR();
00094
00095
00096 private:
00097
00099 SmartsMatcher(const SmartsMatcher& matcher);
00100
00104
00105 SmartsMatcher& operator = (const SmartsMatcher& matcher);
00107
00108
00109 protected:
00110
00114 typedef SmartsParser::SPNode SPNode;
00115 typedef SmartsParser::SPEdge SPEdge;
00116 typedef SmartsParser::SPAtom SPAtom;
00117 typedef SmartsParser::SPBond SPBond;
00119
00121 class RecStructCore_
00122 {
00123 public:
00124
00126 RecStructCore_();
00127
00129 RecStructCore_(const RecStructCore_&);
00130
00132 virtual ~RecStructCore_();
00133
00135 RecStructCore_& operator = (const RecStructCore_&);
00136
00138 void clear();
00139
00141 std::vector<std::set<const Atom*> > matched_atoms;
00142
00144 std::vector<std::map<const SPNode*, const Atom*> > mapped_atoms;
00145
00147 std::vector<std::set<const Atom*> > visited_atoms;
00148
00150 std::vector<std::set<const Bond*> > visited_bonds;
00151
00153 std::vector<std::set<const SPEdge*> > visited_edges;
00154
00156 std::vector<std::pair<const SPNode*, const Atom*> > first_matches;
00157 };
00158
00160 class RecStructPool_
00161 {
00162 public:
00163
00165 RecStructPool_();
00166
00168 virtual ~RecStructPool_();
00169
00171 RecStructCore_* getNextFree();
00172
00174 Position getLastPosition();
00175
00177 void destroy(Position pos);
00178
00179 private:
00180
00182 void resize_();
00183
00185 RecStructPool_(const RecStructPool_&);
00186
00188 RecStructPool_& operator = (const RecStructPool_&);
00189
00191 std::vector<RecStructCore_*> rec_struct_pool_;
00192
00194 std::vector<Position> free_list_;
00195
00197 Position last_position_;
00198 };
00199
00200
00202 class RecStruct_
00203 {
00204 private:
00206 RecStructCore_* rec_struct_core_;
00207
00208 public:
00209
00211 RecStruct_();
00212
00214 RecStruct_(const RecStruct_& rec_struct);
00215
00217 virtual ~RecStruct_();
00218
00220 RecStruct_& operator = (const RecStruct_&);
00221
00223 std::vector<std::set<const Atom*> >& matched_atoms;
00224
00226 std::vector<std::map<const SPNode*, const Atom*> >& mapped_atoms;
00227
00229 std::vector<std::set<const Atom*> >& visited_atoms;
00230
00232 std::vector<std::set<const Bond*> >& visited_bonds;
00233
00235 std::vector<std::set<const SPEdge*> >& visited_edges;
00236
00238 std::vector<std::pair<const SPNode*, const Atom*> >& first_matches;
00239
00241 void add(const RecStruct_& rec_struct);
00242
00244 void add(const RecStruct_& rec_struct, Size i);
00245
00247 void clear();
00248
00250 void dump(const String& name, Size depth_ = 0);
00251
00252 private:
00253
00255 Position pos_;
00256 };
00257
00259 static RecStructPool_* pool_;
00260
00262 bool evaluateRingEdges_(const std::set<const Atom*>& matching, const std::map<const SPNode*, const Atom*>& mapping, const String& smarts);
00263
00265 void evaluate_( RecStruct_& rs, SPNode* start_node, const Atom* start_atom);
00266
00268 bool evaluate_node_(RecStruct_& rs, SPNode* start_node, const Atom* start_atom);
00269
00271 bool evaluate_edge_(RecStruct_& rs, SPEdge* start_node, const Atom* start_atom, const Bond* start_bond);
00272
00274 std::map<SPNode*, std::vector<std::set<const Atom*> > > rec_matches_;
00275
00277 bool has_user_sssr_;
00278
00280 std::vector<std::vector<Atom*> > sssr_;
00281
00282
00283 Size depth_;
00284 };
00285
00286 }
00287
00288 #endif // BALL_STRUCTURE_SMARTSMATCHER_H
00289