1 // $Id: ssm_superpose.h $
2 //  =================================================================
3 //
4 //   CCP4 SSM Library: Protein structure superposition based on
5 //   SSM algorithm: E. Krissinel & K. Henrick (2004) Acta Cryst. D60,
6 //   2256-2268.
7 //
8 //   Copyright (C) Eugene Krissinel 2002-2013.
9 //
10 //   This library is free software: you can redistribute it and/or
11 //   modify it under the terms of the GNU Lesser General Public
12 //   License version 3, modified in accordance with the provisions
13 //   of the license to address the requirements of UK law.
14 //
15 //   You should have received a copy of the modified GNU Lesser
16 //   General Public License along with this library. If not, copies
17 //   may be downloaded from http://www.ccp4.ac.uk/ccp4license.php
18 //
19 //   This program is distributed in the hope that it will be useful,
20 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
21 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 //   GNU Lesser General Public License for more details.
23 //
24 // =================================================================
25 //
26 //    16.09.13   <--  Date of Last Modification.
27 //                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28 // -----------------------------------------------------------------
29 //
30 //  **** Module  :  ssm_spose  <interface>
31 //       ~~~~~~~~~
32 //  **** Functions : SuperposeCalphas ( superposing protein structures )
33 //       ~~~~~~~~~~~
34 //
35 //  E. Krissinel 2002-2013
36 //
37 // =================================================================
38 //
39 
40 #ifndef  __SSM_Superpose__
41 #define  __SSM_Superpose__
42 
43 #include "ssm_graph.h"
44 
45 //  =================================================================
46 
47 namespace ssm  {
48 
49   DefineStructure(SpAtom);
50 
51   struct SpAtom  {
52     mmdb::ChainID  chID;
53     int            c,sse,c0;
54     mmdb::realtype dist,dist0;
55     int            unmap1,unmap2;
56     bool           excluded;
57     bool  CompatibleSSE ( RSpAtom a );
58   };
59 
60 
61   DefineStructure(SectionDist);
62 
63   struct SectionDist  {
64     mmdb::realtype dist,rmsd,cosine;
65     int            core_pos1,core_pos2,core_e1,core_e2;
66     int            na,pos1,pos2,e1,e2;
67     int            sse1,sse2;
68     void Copy ( RSectionDist D );
69   };
70 
71 
72   DefineStructure(SSEDesc);
73 
74   struct SSEDesc  {
75     mmdb::realtype x1,y1,z1,x2,y2,z2;      // transformed start/end coordinates
76     mmdb::realtype xs1,ys1,zs1,xs2,ys2,zs2;   // original start/end coordinates
77     mmdb::realtype score,Qscore,Rscore,Xscore; // overlaping scores
78     int            pos,len,pend, type,classID;
79     int            m,match;
80     void  Transform ( mmdb::mat44 & T );
81     void  CalcScore ( RSSEDesc D );
82     mmdb::realtype Cosine ( RSSEDesc D );
83     void  Copy ( RSSEDesc D );
84   };
85 
86 
87   DefineStructure(SortDistData);
88 
89   struct SortDistData  {
90     mmdb::realtype dist;
91     int            index,unmap1,unmap2;
92   };
93 
94   DefineClass(SortDist);
95 
96   class SortDist : public mmdb::QuickSort  {
97     public :
SortDist()98       SortDist() : QuickSort() {}
99       int  Compare ( int i, int j );
100       void Swap    ( int i, int j );
101       void Sort    ( PSortDistData sdata, int len );
102     protected :
103       PSortDistData sd;
104   };
105 
106 
107   DefineStructure(SuperposeData);
108 
109   struct SuperposeData  {
110     PGraph         G;          //!< SSE graph
111     mmdb::PManager M;          //!< the structure
112     PSpAtom        a;          //!< atom superposition vector
113     mmdb::PPAtom   Calpha;     //!< selected C-alphas
114     PSSEDesc       SSED;       //!< SSE description vector
115     mmdb::pstr     selstring;  //!< C-alpha selection string
116     int            selHnd;     //!< C-alpha selection handle
117     int            selHndIncl; //!< selection handle of inculded C-alphas
118     int            nres;       //!< number of residues (C-alphas)
119     int            nSSEs;      //!< number of SSEs
120     void  Init   ();
121     void  Dispose();
122     void  DeselectCalphas();
123     void  SelectCalphas  ();
124   };
125 
126 
127   DefineClass(Superpose);
128 
129   class Superpose  {
130 
131     public :
132       Superpose();
133       ~Superpose();
134 
135       void SetAllowMC         ( bool allowMisconnections );
136       void SetIterationLimits ( int iter_max, int iter_min,
137                                 int max_hollow );
138       void SetCaSelections    ( mmdb::cpstr selection1,
139                                 mmdb::cpstr selection2 );
140 
141       int  SuperposeSSGraphs  ( PGraph G1, mmdb::ivector F1,
142                                 PGraph G2, mmdb::ivector F2,
143                                 int matchlen );
144 
145       //  driver #1
146       int  SuperposeCalphas  (
147               PGraph         G1,   //!< SSE graph of 1st structure
148               PGraph         G2,   //!< SSE graph of 2nd structure
149               mmdb::ivector  F1,   //!< matched vertices of G1 [1..mlen]
150               mmdb::ivector  F2,   //!< matched vertices of G2 [1..mlen]
151               int            mlen, //!< length of match (F1,F2)
152               mmdb::PManager M1,   //!< 1st structure
153               mmdb::PManager M2,   //!< 2nd structure
154               int  selHndIncl1=0,  //!< sel handle to include atoms from M1
155               int  selHndIncl2=0   //!< sel handle to include atoms from M2
156                              );
157 
158       //  driver #2
159       int  SuperposeCalphas  (
160               PSuperposeData SD1, //!< superposition data of 1st structure
161               PSuperposeData SD2, //!< superposition data of 2nd structure
162               mmdb::ivector  F1,  //!< matched vertices of SD1.G [1..mlen]
163               mmdb::ivector  F2,  //!< matched vertices of SD2.G [1..mlen]
164               int            mlen //!< length of match (F1,F2)
165                              );
166 
167       void  GetTMatrix ( mmdb::mat44 & TMat ); //!< to be applied to 1st struct.
168       mmdb::mat44 *  GetTMatrix();    //!< to be applied to 1st structure
169       mmdb::realtype GetRMSD   ();
170       int            GetNAlign ();
171       void    GetSuperposition ( mmdb::ivector  & Ca1  ,
172                                  mmdb::rvector  & dist1, int & nCa1,
173                                  mmdb::ivector  & Ca2  , int & nCa2,
174                                  mmdb::mat44    & TMat ,
175                                  mmdb::realtype & rmsdAchieved,
176                                  int & nAligned,   int & nGaps,
177                                  mmdb::realtype & seqIdentity,
178                                  int & nMisD, mmdb::realtype & nCombs );
179 
180       void GetCalphas1 ( mmdb::PPAtom & Calpha, int & numRes );
181       void GetCalphas2 ( mmdb::PPAtom & Calpha, int & numRes );
182 
183       void GetSSEDesc1 ( RPSSEDesc sseDesc, int & numSSEs );
184       void GetSSEDesc2 ( RPSSEDesc sseDesc, int & numSSEs );
185       PSSEDesc GetSSEDesc1();
186       PSSEDesc GetSSEDesc2();
187 
188       void GetSuperposedSSEs ( mmdb::ivector v1, mmdb::ivector v2,
189                                int & nSupSSEs );
190 
GetCalphaQ()191       mmdb::realtype GetCalphaQ   ()  { return Q_achieved; }
192       mmdb::realtype MatchQuality ( int Nalign, mmdb::realtype Rmsd );
193 
194     protected :
195       mmdb::mat44     TMatrix,TMx;
196       PSpAtom         a1,a2;
197       mmdb::realtype  Rmsd0;       //!< optimization parameter
198       mmdb::realtype  minContact;  //!< minimal Calpha-pair contact parameter
199       mmdb::realtype  maxContact;  //!< maximal Calpha-pair contact parameter
200       mmdb::realtype  maxRMSD;     //!< maximal RMSD allowed
201       mmdb::realtype  minQStep;    //!< minimal quality improvement that counts
202       mmdb::realtype  minCosine;   //!< min cosine between co-directional SSEs
203       mmdb::realtype  SSEweight;   //!< additional weight for SSE atoms
204       int             sseGray;     //!< gray zone on the ends of SSEs allowed for
205                                    //!< matching to non-SSE atoms
206       int             selInclHnd1; //!< selection handle for included Calpha1
207       int             selInclHnd2; //!< selection handle for included Calpha2
208       int             driverID;    //!< ID of the used Superpose driver
209       mmdb::pstr      selString1;  //!< optional sel-n string for 1st structure
210       mmdb::pstr      selString2;  //!< optional sel-n string for 2nd structure
211 
212 
213       mmdb::realtype  rmsd_achieved,Q_achieved,ncombs,seqIdent;
214       int             shortSect1,shortSect2;
215       int             iterMax,iterMin;
216       int             maxHollowIt;  //!< maximal allowed number of consequtive
217                                     /// iterations without quality improvement
218 
219       int            nres1,nres2,nalgn,ngaps,nmd,nmisdr;
220       bool           allowMC;       //!< allowing for misconnection
221 
222       mmdb::rmatrix  A,U,V, AD;
223       mmdb::rvector  W,RV1;
224 
225       mmdb::ivector  copyF1,copyF2;   //!< copy pointers to input F1,F2
226       int            copyFlen;        //!< length of FF1,FF2
227       mmdb::rvector  cax0,cay0,caz0;  //!< working arrays
228       PSortDistData  sdata;
229 
230       mmdb::PManager MMDB1,MMDB2; //!< copies of 1st and 2nd structure MMDBs
231 
232       mmdb::PPAtom   Calpha1,Calpha2;
233       PSSEDesc       SSED1,SSED2;
234       mmdb::ivector  FH1,FS1,FH2,FS2;
235       int            nSSEs1,nSSEs2;
236       int            nFH1,nFS1,nFH2,nFS2;
237       PPSectionDist  SDist;
238       int            SDistAlloc;
239 
240       SortDist       sortDist;
241 
242 
243       void  InitSuperpose       ();
244       void  FreeMemory          ();
245       void  SelectCalphas       ( mmdb::PManager MMDB, PGraph G,
246                                   mmdb::PPAtom & Calpha, PSpAtom & a,
247                                   int & nres, int & selHnd,
248                                   int selInclHnd, mmdb::cpstr selString );
249       void  MapSSEs             ( mmdb::PPAtom Calpha, PSpAtom a,
250                                   int nres, PGraph G, RPSSEDesc SSED,
251                                   int & nSSEs );
252       void  IdentifyUnmatchedSSEs ( mmdb::ivector & FH, int & nFH,
253                                     mmdb::ivector & FS, int & nFS,
254                                     mmdb::ivector F, int mlen,
255                                     PGraph G );
256       void  GetSSESpseCenters   ( RSSEDesc Q1, RSSEDesc Q2,
257                                   RSSEDesc T1, RSSEDesc T2,
258                                   mmdb::realtype & qc1,
259                                   mmdb::realtype & qc2,
260                                   mmdb::realtype & tc1,
261                                   mmdb::realtype & tc2 );
262       int   FirstGuess          ( mmdb::ivector F1, mmdb::ivector F2,
263                                   int mlen );
264       void  ChooseFirstRotation ( int rotSSE1, int rotSSE2 );
265       void  CalcDistance        ( int SSE1, int SSSE2, RSectionDist D );
266       void  AlignSSEs           ( RSectionDist D, int unmap );
267       bool  isMC                ( int pos1, int pos2 );
268       void  CorrespondSSEs      ( mmdb::ivector F1, int nF1,
269                                   mmdb::ivector F2, int nF2,
270                                   mmdb::realtype rmsd_est );
271       void  CorrespondContacts  ( mmdb::PManager M1,
272                                   mmdb::realtype rmsd_est );
273       void  ExpandContact       ( mmdb::RContact c, int & ip, int & im,
274                                   mmdb::realtype maxDist2 );
275       void  RecoverGaps         ( mmdb::PPAtom Ca1, PSpAtom at1, int nat1,
276                                   mmdb::PPAtom Ca2, PSpAtom at2, int nat2,
277                                   mmdb::realtype thresh );
278       void  CleanShortSections  ( PSpAtom at1, int nat1, PSpAtom at2 );
279 
280       int   CalculateTMatrix    ();
281       void     CalcNGaps        ( PSpAtom a, int nres, int & Ng, int & Nm );
282       mmdb::realtype CalcNCombs ( PGraph G, PSSEDesc SSED, int nSSEs,
283                                   PSpAtom  a, int nres );
284       mmdb::realtype MatchQuality2 ( int Nalign, mmdb::realtype dist2 );
285       void     CalcQScore       ( RSSEDesc SSE1 );
286       int   OptimizeNalign      ();
287       void  UnmapExcluded       ( PSpAtom a1, PSpAtom a2, int nres1 );
288 
289       void  _superpose ( PGraph G1, PGraph G2, int & rc );
290 
291   };
292 
293 }  // namespace ssm
294 
295 #endif
296