00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 #ifndef __MMDB_MATH_Graph__
00051 #define __MMDB_MATH_Graph__
00052
00053 #include <time.h>
00054
00055 #include "mmdb_atom.h"
00056
00057 namespace mmdb {
00058
00059 namespace math {
00060
00061
00062
00063 DefineClass(Vertex);
00064
00065 enum GRAPH_FLAG {
00066 CHIRAL_RIGHT = 0x10000000,
00067 CHIRAL_LEFT = 0x20000000,
00068 ATOM_LEAVING = 0x40000000,
00069 HYDROGEN_BOND = 0x0F000000,
00070 SYMREL_MASK = 0x00FF0000,
00071 CHIRAL_MASK = 0xCFFFFFFF,
00072 TYPE_MASK = 0x00FFFFFF
00073 };
00074
00075 class Vertex : public io::Stream {
00076
00077 friend class Graph;
00078 friend class GraphMatch;
00079
00080 public:
00081
00082 Vertex ();
00083 Vertex ( io::RPStream Object );
00084 Vertex ( int vtype, cpstr vname );
00085 Vertex ( int vtype );
00086 Vertex ( cpstr chem_elem );
00087 Vertex ( cpstr chem_elem, cpstr name );
00088 ~Vertex();
00089
00090 void SetVertex ( cpstr chem_elem );
00091 void SetVertex ( int vtype, cpstr vname );
00092 void SetVertex ( int vtype );
00093 void SetType ( int vtype );
00094 void SetTypeExt ( int typeExt );
00095
00096 void RemoveChirality();
00097 void LeaveChirality ( int eltype );
00098
00099 void SetName ( cpstr vname );
00100 void SetProperty ( int vprop );
00101 void SetID ( int vid );
00102 void AddBond ();
00103 void CopyNBonds ( PVertex V );
00104 inline void SetUserID ( int vid ) { user_id = vid; }
00105 inline int GetProperty () { return property; }
00106 inline int GetID () { return id; }
00107 inline int GetUserID () { return user_id; }
00108 inline cpstr GetName () { return name; }
00109 inline int GetType () { return type; }
00110 inline int GetTypeExt () { return type_ext; }
00111 int GetNBonds ();
00112
00113 void SaveType ();
00114 void RestoreType ();
00115 void CopyType ( PVertex V );
00116
00117 virtual void Print ( int PKey );
00118
00119 virtual void Copy ( PVertex V );
00120
00121 void read ( io::RFile f );
00122 void write ( io::RFile f );
00123
00124 void mem_read ( cpstr S, int & l );
00125 void mem_write ( pstr S, int & l );
00126
00127 protected:
00128 pstr name;
00129 int type;
00130
00131 int type_ext;
00132 int property;
00133 int id;
00134 int user_id;
00135
00136 void InitVertex();
00137
00138 };
00139
00140 DefineStreamFunctions(Vertex);
00141
00142
00143
00144
00145 enum GRAPH_BOND {
00146 BOND_SINGLE = 1,
00147 BOND_DOUBLE = 2,
00148 BOND_AROMATIC = 3,
00149 BOND_TRIPLE = 4
00150 };
00151
00152 DefineClass(Edge);
00153
00154 class Edge : public io::Stream {
00155
00156 friend class Graph;
00157 friend class CGMatch;
00158
00159 public:
00160
00161 Edge ();
00162 Edge ( io::RPStream Object );
00163 Edge ( int vx1, int vx2, int btype );
00164
00165
00166
00167
00168 ~Edge();
00169
00170 void SetEdge ( int vx1, int vx2, cpstr btype );
00171 void SetEdge ( int vx1, int vx2, int btype );
00172
00173 void SetType ( int btype );
00174 void SetProperty ( int eprop );
00175 void SaveType ();
00176 void RestoreType ();
00177
00178 inline int GetVertex1 () { return v1; }
00179 inline int GetVertex2 () { return v2; }
00180 inline int GetType () { return type; }
00181 inline int GetProperty () { return property; }
00182
00183 virtual void Print ( int PKey );
00184
00185 virtual void Copy ( PEdge G );
00186
00187 void read ( io::RFile f );
00188 void write ( io::RFile f );
00189
00190 void mem_read ( cpstr S, int & l );
00191 void mem_write ( pstr S, int & l );
00192
00193 protected:
00194 int v1,v2;
00195 int type;
00196 int property;
00197
00198 void InitEdge();
00199
00200 };
00201
00202 DefineStreamFunctions(Edge);
00203
00204
00205
00206
00207 enum GRAPH_RC {
00208 MKGRAPH_Ok = 0,
00209 MKGRAPH_NoAtoms = -1,
00210 MKGRAPH_ChangedAltLoc = 1,
00211 MKGRAPH_MaxOccupancy = 2
00212 };
00213
00214 DefineClass(Graph);
00215
00216 class Graph : public io::Stream {
00217
00218 friend class GraphMatch;
00219 friend class CSBase0;
00220
00221 public :
00222
00223 Graph ();
00224 Graph ( PResidue R, cpstr altLoc=NULL );
00225 Graph ( io::RPStream Object );
00226 ~Graph();
00227
00228 void Reset ();
00229 void SetName ( cpstr gname );
00230 inline pstr GetName() { return name; }
00231
00232
00233
00234
00235
00236
00237 void AddVertex ( PVertex V );
00238 void AddEdge ( PEdge G );
00239 void SetVertices ( PPVertex V, int vlen );
00240 void SetEdges ( PPEdge G, int glen );
00241
00242 void RemoveChirality();
00243 void LeaveChirality ( int eltype );
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271 int MakeGraph ( PResidue R, cpstr altLoc=NULL );
00272
00273 int MakeGraph ( PPAtom atom, int nAtoms );
00274
00275 void HideType ( int bond_vx_type );
00276 void ExcludeType ( int type );
00277
00278 void MakeSymmetryRelief ( bool noCO2 );
00279 void IdentifyRings ();
00280 int IdentifyConnectedComponents();
00281
00282 int Build ( bool bondOrder );
00283
00284 void MakeVertexIDs ();
00285 int GetVertexID ( int vertexNo );
00286 int GetVertexNo ( cpstr vname );
00287
00288 int GetNBondedVertices ( int vertexNo );
00289 int GetBondedVertexID ( int vertexNo, int bond_vx_type,
00290 int bondNo );
00291
00292 PVertex GetVertex ( int vertexNo );
00293 inline int GetNofVertices() { return nVertices; }
00294
00295 PEdge GetEdge ( int edgeNo );
00296 inline int GetNofEdges() { return nEdges; }
00297
00298 void GetVertices ( PPVertex & V, int & nV );
00299 void GetEdges ( PPEdge & E, int & nE );
00300
00301 virtual void Print();
00302 void Print1();
00303
00304 virtual void Copy ( PGraph G );
00305
00306 void read ( io::RFile f );
00307 void write ( io::RFile f );
00308
00309 void mem_read ( cpstr S, int & l );
00310 void mem_write ( pstr S, int & l );
00311
00312 protected :
00313 pstr name;
00314 int nVertices,nEdges, nAllVertices,nAllEdges;
00315 PPVertex vertex;
00316 PPEdge edge;
00317 imatrix graph;
00318
00319 void InitGraph ();
00320 void FreeMemory();
00321
00322 void markConnected ( int vno, int cno );
00323
00324 private :
00325 int nVAlloc,nEAlloc,nGAlloc;
00326
00327 };
00328
00329 DefineStreamFunctions(Graph);
00330
00331
00332
00333
00334 DefineClass(GMatch);
00335 DefineStreamFunctions(GMatch);
00336
00337 class GMatch : public io::Stream {
00338
00339 friend class GraphMatch;
00340
00341 public :
00342
00343 GMatch ();
00344 GMatch ( io::RPStream Object );
00345 GMatch ( ivector FV1, ivector FV2, int nv, int n, int m );
00346 ~GMatch();
00347
00348
00349 void SetMatch ( ivector FV1, ivector FV2, int nv, int n, int m );
00350
00351 bool isMatch ( ivector FV1, ivector FV2, int nv );
00352 bool isCombination ( ivector FV1, ivector FV2, int nv );
00353
00354
00355 void GetMatch ( ivector & FV1, ivector & FV2, int & nv,
00356 realtype & p1, realtype & p2 );
00357
00358 void read ( io::RFile f );
00359 void write ( io::RFile f );
00360
00361 void mem_read ( cpstr S, int & l );
00362 void mem_write ( pstr S, int & l );
00363
00364 protected :
00365 int n1,n2,mlength;
00366 ivector F1,F2;
00367
00368 void InitGMatch();
00369
00370 private :
00371 int nAlloc;
00372
00373 };
00374
00375
00376
00377
00378 #define _UseRecursion
00379
00380 enum GRAPH_MATCH_FLAG {
00381 GMF_UniqueMatch = 0x00000001,
00382 GMF_NoCombinations = 0x00000002
00383 };
00384
00385 enum VERTEX_EXT_TYPE {
00386 EXTTYPE_Ignore = 0,
00387 EXTTYPE_Equal = 1,
00388 EXTTYPE_AND = 2,
00389 EXTTYPE_OR = 3,
00390 EXTTYPE_XOR = 4,
00391 EXTTYPE_NotEqual = 5,
00392 EXTTYPE_NotAND = 6,
00393 EXTTYPE_NotOR = 7
00394 };
00395
00396 DefineClass(GraphMatch);
00397
00398 class GraphMatch : public io::Stream {
00399
00400 public :
00401
00402 GraphMatch ();
00403 GraphMatch ( io::RPStream Object );
00404 ~GraphMatch();
00405
00406 void SetFlag ( word flag );
00407 void RemoveFlag ( word flag );
00408 void SetMaxNofMatches ( int maxNofMatches, bool stopOnMaxN );
00409 void SetTimeLimit ( int maxTimeToRun=0 );
00410 inline bool GetStopSignal() { return Stop; }
00411
00412 void Reset();
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422 void MatchGraphs ( PGraph Gh1, PGraph Gh2, int minMatch,
00423 bool vertexType=true,
00424 VERTEX_EXT_TYPE vertexExt=EXTTYPE_Ignore );
00425 void PrintMatches ();
00426 inline int GetNofMatches () { return nMatches; }
00427 inline int GetMaxMatchSize() { return maxMatch; }
00428
00429
00430
00431
00432 void GetMatch ( int MatchNo, ivector & FV1, ivector & FV2,
00433 int & nv, realtype & p1, realtype & p2 );
00434
00435 void read ( io::RFile f );
00436 void write ( io::RFile f );
00437
00438 void mem_read ( cpstr S, int & l );
00439 void mem_write ( pstr S, int & l );
00440
00441 protected :
00442 PGraph G1,G2;
00443 PPVertex V1;
00444 PPVertex V2;
00445 imatrix c1,c2;
00446 bool swap;
00447 #ifndef _UseRecursion
00448 ivector jj;
00449 #endif
00450 int n,m;
00451
00452 imatrix3 P;
00453 imatrix iF1;
00454 ivector F1,F2,ix;
00455
00456 int nMatches,maxNMatches;
00457 PPGMatch Match;
00458 bool wasFullMatch,Stop,stopOnMaxNMathches;
00459 word flags;
00460 int maxMatch,timeLimit;
00461
00462 void InitGraphMatch();
00463 void FreeMemory ();
00464 void FreeRecHeap ();
00465 void GetMemory ();
00466 void GetRecHeap ();
00467 int Initialize ( bool vertexType, int vertexExt );
00468 #ifdef _UseRecursion
00469 void Backtrack ( int i );
00470 #else
00471 void Ullman ();
00472 #endif
00473 void Backtrack1 ( int i, int k0 );
00474 void CollectMatch ( int nm );
00475
00476 private :
00477 int nAlloc,mAlloc,nMAlloc;
00478 time_t startTime;
00479
00480 };
00481
00482 DefineStreamFunctions(GraphMatch);
00483
00484 extern void SetGraphAllocPortion ( int alloc_portion );
00485
00486
00487
00488
00489
00490 }
00491
00492 }
00493
00494 #endif