Bioplib
Protein Structure C Library
 All Data Structures Files Functions Variables Typedefs Macros Pages
FitCaCbPDB.c
Go to the documentation of this file.
1 /************************************************************************/
2 /**
3 
4  \file FitCaCbPDB.c
5 
6  \version V1.4
7  \date 07.07.14
8  \brief Fit two PDB linked lists. Also a weighted fit and support
9  routines
10 
11  \copyright (c) UCL / Dr. Andrew C. R. Martin 1993-6
12  \author Dr. Andrew C. R. Martin
13  \par
14  Institute of Structural & Molecular Biology,
15  University College London,
16  Gower Street,
17  London.
18  WC1E 6BT.
19  \par
20  andrew@bioinf.org.uk
21  andrew.martin@ucl.ac.uk
22 
23 **************************************************************************
24 
25  This code is NOT IN THE PUBLIC DOMAIN, but it may be copied
26  according to the conditions laid out in the accompanying file
27  COPYING.DOC.
28 
29  The code may be modified as required, but any modifications must be
30  documented so that the person responsible can be identified.
31 
32  The code may not be sold commercially or included as part of a
33  commercial product except as described in the file COPYING.DOC.
34 
35 **************************************************************************
36 
37  Description:
38  ============
39 
40 
41 **************************************************************************
42 
43  Usage:
44  ======
45 
46 **************************************************************************
47 
48  Revision History:
49  =================
50 - V1.0 01.03.94 Original release
51 - V1.1 11.03.94 Fixed bug in calls to matfit(). Had not been changed
52  to reflect modification in MatMult3_33().
53 - V1.2 14.03.94 Fixed FitPDB(); wasn't filling in the output matrix
54 - V1.3 14.03.96 Added FitCaPDB()
55  Changed FitPDB() and FitCaCbPDB() to use
56  ApplyMatrixPDB() rather than RotatePDB() since the PDB
57  linked lists are already at the origin
58 - V1.4 07.07.14 Use bl prefix for functions. By: CTP
59 
60 *************************************************************************/
61 /* Doxygen
62  -------
63  #GROUP Handling PDB Data
64  #SUBGROUP Fitting
65  #FUNCTION blFitCaCbPDB()
66  Does a weighted fitting of 2 PDB linked lists. The CA and CB are given
67  a weight of 1.0 while the other atoms are given a weight of 1.0/natom
68  in the residue.
69  Thus for N,CA,C,CB backbone only, this will be N and C with weights
70  of 0.25
71 */
72 /************************************************************************/
73 /* Includes
74 */
75 #include <math.h>
76 #include <stdlib.h>
77 #include <string.h>
78 
79 #include "MathType.h"
80 #include "SysDefs.h"
81 #include "macros.h"
82 #include "fit.h"
83 #include "pdb.h"
84 
85 /************************************************************************/
86 /* Defines and macros
87 */
88 
89 /************************************************************************/
90 /* Globals
91 */
92 
93 /************************************************************************/
94 /* Prototypes
95 */
96 
97 /************************************************************************/
98 /*>BOOL blFitCaCbPDB(PDB *ref_pdb, PDB *fit_pdb, REAL rm[3][3])
99  ------------------------------------------------------------
100 *//**
101 
102  \param[in] *ref_pdb Reference PDB linked list
103  \param[in,out] *fit_pdb PDB linked list to be fitted
104  \param[out] rm Rotation matrix
105  \return Success
106 
107  Does a weigthed fitting of 2 PDB linked lists. The CA and CB are given
108  a weight of 1.0 while the other atoms are given a weight of 1.0/natom
109  in the residue.
110  Thus for N,CA,C,CB backbone only, this will be N and C with weights
111  of 0.25
112 
113 - 21.06.93 Original By: ACRM
114 - 22.06.93 Added i increment (!). Corrected return of rm
115 - 11.03.94 Changed call to matfit(). Corrected to normal matrix.
116 - 15.03.94 Defines the rotation matrix locally and only copies for
117  output if rm is not NULL
118 - 14.03.96 Changed to use ApplyMatrixPDB() rather than RotatePDB() since
119  we are already at the origin
120 - 07.07.14 Use bl prefix for functions By: CTP
121 */
122 BOOL blFitCaCbPDB(PDB *ref_pdb, PDB *fit_pdb, REAL rm[3][3])
123 {
124  REAL RotMat[3][3],
125  *weights = NULL;
126  COOR *ref_coor = NULL,
127  *fit_coor = NULL;
128  VEC3F ref_CofG;
129  int NCoor = 0,
130  natom = 0,
131  resnum = 0,
132  i = 0,
133  j = 0;
134  char insert = ' ';
135  BOOL RetVal = TRUE;
136  PDB *p = NULL,
137  *q = NULL,
138  *start = NULL;
139 
140  /* Get the CofG of the reference structure */
141  blGetCofGPDB(ref_pdb, &ref_CofG);
142 
143  /* Move them both to the origin */
144  blOriginPDB(ref_pdb);
145  blOriginPDB(fit_pdb);
146 
147  /* Create coordinate arrays */
148  NCoor = blGetPDBCoor(ref_pdb, &ref_coor);
149  if((NCoor == 0) || (blGetPDBCoor(fit_pdb, &fit_coor) != NCoor))
150  {
151  /* Free and return if arrays don't match */
152  if(ref_coor) free(ref_coor);
153  if(fit_coor) free(fit_coor);
154  return(FALSE);
155  }
156 
157  /* Can't fit with fewer than 3 coordinates */
158  if(NCoor < 3)
159  {
160  if(ref_coor) free(ref_coor);
161  if(fit_coor) free(fit_coor);
162  return(FALSE);
163  }
164 
165  /* Allocate memory for weigths array */
166  if((weights = (REAL *)malloc(NCoor * sizeof(REAL))) == NULL)
167  {
168  if(ref_coor) free(ref_coor);
169  if(fit_coor) free(fit_coor);
170  return(FALSE);
171  }
172 
173  /* Create the weight array */
174  resnum = ref_pdb->resnum;
175  insert = ref_pdb->insert[0];
176  start = ref_pdb;
177  natom = 0;
178  i = 0;
179 
180  for(p=ref_pdb; p!=NULL; NEXT(p))
181  {
182  if(p->resnum != resnum || p->insert[0] != insert)
183  {
184  /* Start of next residue */
185  for(q=start; q!=p; NEXT(q))
186  {
187  weights[i] = (REAL)(1.0/(REAL)natom);
188  if(!strncmp(q->atnam,"CA ",4)) weights[i] = (REAL)1.0;
189  if(!strncmp(q->atnam,"CB ",4)) weights[i] = (REAL)1.0;
190  i++;
191  }
192  natom = 0;
193  start = p;
194  }
195  natom++;
196  }
197 
198  /* End of structure */
199  for(q=start; q!=NULL; NEXT(q))
200  {
201  weights[i] = (REAL)(1.0/(REAL)natom);
202  if(!strncmp(q->atnam,"CA ",4)) weights[i] = (REAL)1.0;
203  if(!strncmp(q->atnam,"CB ",4)) weights[i] = (REAL)1.0;
204  i++;
205  }
206 
207  /* Everything OK, go ahead with the fitting */
208  RetVal = blMatfit(ref_coor,fit_coor,RotMat,NCoor,weights,FALSE);
209 
210  /* Now we can rotate the rotation list */
211  if(RetVal)
212  blApplyMatrixPDB(fit_pdb, RotMat);
213 
214  /* Translate both structures back to reference CofG */
215  blTranslatePDB(fit_pdb, ref_CofG);
216  blTranslatePDB(ref_pdb, ref_CofG);
217 
218  /* Free the coordinate and weights arrays */
219  free(ref_coor);
220  free(fit_coor);
221  free(weights);
222 
223  /* Fill in the rotation matrix for output, if required */
224  if(rm!=NULL)
225  {
226  for(i=0; i<3; i++)
227  for(j=0; j<3; j++)
228  rm[i][j] = RotMat[i][j];
229  }
230 
231  return(RetVal);
232 }
233 
Include file for PDB routines.
int resnum
Definition: pdb.h:310
short BOOL
Definition: SysDefs.h:64
#define NULL
Definition: array2.c:99
BOOL blFitCaCbPDB(PDB *ref_pdb, PDB *fit_pdb, REAL rm[3][3])
Definition: FitCaCbPDB.c:122
Definition: pdb.h:298
#define FALSE
Definition: macros.h:223
#define NEXT(x)
Definition: macros.h:249
Useful macros.
Definition: MathType.h:69
int blGetPDBCoor(PDB *pdb, COOR **coor)
Definition: GetPDBCoor.c:104
double REAL
Definition: MathType.h:67
#define TRUE
Definition: macros.h:219
BOOL blMatfit(COOR *x1, COOR *x2, REAL rm[3][3], int n, REAL *wt1, BOOL column)
Definition: fit.c:128
Include file for least squares fitting.
void blGetCofGPDB(PDB *pdb, VEC3F *cg)
Definition: GetCGPDB.c:98
System-type variable type definitions.
void blOriginPDB(PDB *pdb)
Definition: OriginPDB.c:96
Type definitions for maths.
void blTranslatePDB(PDB *pdb, VEC3F tvect)
Definition: TranslatePDB.c:95
void blApplyMatrixPDB(PDB *pdb, REAL matrix[3][3])
Definition: ApMatPDB.c:92
char insert[8]
Definition: pdb.h:320