Bioplib
Protein Structure C Library
 All Data Structures Files Functions Variables Typedefs Macros Pages
GetCrystPDB.c
Go to the documentation of this file.
1 /************************************************************************/
2 /**
3 
4  \file GetCrystPDB.c
5 
6  \version V1.1
7  \date 07.07.14
8  \brief
9 
10  \copyright (c) UCL / Dr. Andrew C. R. Martin 1993-2014
11  \author Dr. Andrew C. R. Martin
12  \par
13  Institute of Structural & Molecular Biology,
14  University College London,
15  Gower Street,
16  London.
17  WC1E 6BT.
18  \par
19  andrew@bioinf.org.uk
20  andrew.martin@ucl.ac.uk
21 
22 **************************************************************************
23 
24  This code is NOT IN THE PUBLIC DOMAIN, but it may be copied
25  according to the conditions laid out in the accompanying file
26  COPYING.DOC.
27 
28  The code may be modified as required, but any modifications must be
29  documented so that the person responsible can be identified.
30 
31  The code may not be sold commercially or included as part of a
32  commercial product except as described in the file COPYING.DOC.
33 
34 **************************************************************************
35 
36  Description:
37  ============
38 
39 
40 **************************************************************************
41 
42  Usage:
43  ======
44 
45 **************************************************************************
46 
47  Revision History:
48  =================
49 - V1.0R 12.10.05 Original
50 - V1.1 07.07.14 Use bl prefix for functions By: CTP
51 
52 *************************************************************************/
53 /* Doxygen
54  -------
55  #GROUP Handling PDB Data
56  #SUBGROUP File IO
57  #FUNCTION blGetCrystPDB()
58  Read the crystal parameters (unit cell, spacegroup, origin and scale
59  matrices) out of a PDB file.
60 
61  #FUNCTION blGetCrystWholePDB()
62  Read the crystal parameters (unit cell, spacegroup, origin and scale
63  matrices) out of a PDB file.
64 */
65 /************************************************************************/
66 /* Includes
67 */
68 #include <stdio.h>
69 #include <math.h>
70 #include "SysDefs.h"
71 #include "MathType.h"
72 #include "pdb.h"
73 #include "fsscanf.h"
74 
75 /************************************************************************/
76 /* Defines and macros
77 */
78 #define MAXBUFF 160
79 
80 /************************************************************************/
81 /* Globals
82 */
83 
84 /************************************************************************/
85 /* Prototypes
86 */
87 
88 /************************************************************************/
89 /*>int blGetCrystPDB(FILE *fp, VEC3F *UnitCell, VEC3F *CellAngles,
90  char *spacegroup,
91  REAL OrigMatrix[3][4], REAL ScaleMatrix[3][4])
92  ----------------------------------------------------------------
93 *//**
94 
95  \param[in] *fp Input file pointer
96  \param[out] *UnitCell The unit cell dimensions
97  \param[out] *CellAngles The unit cell angles
98  \param[out] *spacegroup The spacegroup
99  \param[out] OrigMatrix The origin matrix
100  \param[out] ScaleMatrix The scale matrix
101  \return Flags for elements read.
102 
103 
104  Flags for elements read:
105  0: Nothing at all
106  XTAL_DATA_CRYST: Unit cell
107  XTAL_DATA_ORIGX: Origin matrix
108  XTAL_DATA_SCALE: Scale matrix
109  (These are ORed together)
110 
111  Read the crystal parameters (unit cell, spacegroup, origin and scale
112  matrices) out of a PDB file.
113 
114  Stops searching as soon as an ATOM or HETATM record is hit and leaves
115  the file in a state ready for ReadPDB() to do it's stuff (i.e. with
116  the current file pointer at the first ATOM or HETATM record).
117 
118 - 12.10.95 Original By: ACRM
119 - 17.10.95 Correctly returns retval if no ATOM/HETATM records not found
120 - 07.07.14 Use bl prefix for functions By: CTP
121 */
122 int blGetCrystPDB(FILE *fp, VEC3F *UnitCell, VEC3F *CellAngles,
123  char *spacegroup,
124  REAL OrigMatrix[3][4], REAL ScaleMatrix[3][4])
125 {
126  int ch,
127  i, j,
128  record,
129  retval = 0x0000;
130  char buffer[MAXBUFF];
131  BOOL FirstChar = TRUE,
132  WholeLine = FALSE;
133 
134  /* Initialise matrices and cell dimensions */
135  UnitCell->x = UnitCell->y = UnitCell->z = (REAL)1.0;
136  CellAngles->x = CellAngles->y = CellAngles->z = (REAL)90.0*PI/180.0;
137  for(i=0; i<3; i++)
138  {
139  for(j=0; j<4; j++)
140  {
141  OrigMatrix[i][j] = (REAL)0.0;
142  ScaleMatrix[i][j] = (REAL)0.0;
143  }
144  OrigMatrix[i][i] = (REAL)1.0;
145  }
146  strcpy(spacegroup,"P");
147 
148  while(((ch=fgetc(fp)) != EOF) && !feof(fp))
149  {
150  if(ch == '\n')
151  {
152  FirstChar = TRUE;
153  }
154  else if(FirstChar)
155  {
156  WholeLine = FALSE;
157 
158  if(ch=='A' || ch=='C' || ch=='H' || ch=='O' || ch=='S')
159  {
160  buffer[0] = (char)ch;
161  for(i=1; i<6; i++)
162  {
163  ch = fgetc(fp);
164  if(ch==EOF || feof(fp))
165  return(FALSE);
166 
167  buffer[i] = (char)ch;
168  }
169  buffer[6] = '\0';
170 
171  /* See if it was an ATOM or HETATM record */
172  if(!strncmp(buffer,"ATOM ",6) || !strncmp(buffer,"HETATM",6))
173  {
174  /* If so, shove the characters back into the input stream
175  and return
176  */
177  for(i=5; i>=0; i--)
178  ungetc(buffer[i], fp);
179  return(retval);
180  }
181 
182  /* See if it was a CRYST record */
183  if(!strncmp(buffer,"CRYST1",6))
184  {
185  /* If so, shove the characters back into the input stream,
186  extract the crystal parameters and set the return value
187  */
188  for(i=5; i>=0; i--)
189  ungetc(buffer[i], fp);
190  fgets(buffer, MAXBUFF, fp);
191  WholeLine = TRUE;
192  fsscanf(buffer,
193  "%6x%9.3lf%9.3lf%9.3lf%7.2lf%7.2lf%7.2lf%1x%14s",
194  &(UnitCell->x),
195  &(UnitCell->y),
196  &(UnitCell->z),
197  &(CellAngles->x),
198  &(CellAngles->y),
199  &(CellAngles->z),
200  spacegroup);
201  CellAngles->x *= PI/180.0;
202  CellAngles->y *= PI/180.0;
203  CellAngles->z *= PI/180.0;
204 
205  retval |= XTAL_DATA_CRYST;
206  }
207 
208  /* See if it was a ORIGX record */
209  if(!strncmp(buffer,"ORIGX",5))
210  {
211  /* If so, shove the characters back into the input stream,
212  extract the crystal parameters and set the return value
213  */
214  for(i=5; i>=0; i--)
215  ungetc(buffer[i], fp);
216  fgets(buffer, MAXBUFF, fp);
217  fsscanf(buffer,"%5x%1d",&record);
218  WholeLine = TRUE;
219  record--;
220  fsscanf(buffer,
221  "%10x%10.6lf%10.6lf%10.6lf%15.5lf",
222  &(OrigMatrix[record][0]),
223  &(OrigMatrix[record][1]),
224  &(OrigMatrix[record][2]),
225  &(OrigMatrix[record][3]));
226 
227  retval |= XTAL_DATA_ORIGX;
228  }
229 
230  /* See if it was a SCALE record */
231  if(!strncmp(buffer,"SCALE",5))
232  {
233  /* If so, shove the characters back into the input stream,
234  extract the crystal parameters and set the return value
235  */
236  for(i=5; i>=0; i--)
237  ungetc(buffer[i], fp);
238  fgets(buffer, MAXBUFF, fp);
239  fsscanf(buffer,"%5x%1d",&record);
240  WholeLine = TRUE;
241  record--;
242  fsscanf(buffer,
243  "%10x%10.6lf%10.6lf%10.6lf%15.5lf",
244  &(ScaleMatrix[record][0]),
245  &(ScaleMatrix[record][1]),
246  &(ScaleMatrix[record][2]),
247  &(ScaleMatrix[record][3]));
248 
249  retval |= XTAL_DATA_SCALE;
250  }
251 
252  }
253 
254  FirstChar = WholeLine?TRUE:FALSE;
255  }
256  }
257  return(retval);
258 }
259 
260 
261 /************************************************************************/
262 /*>int blGetCrystWholePDB(WHOLEPDB *wpdb, VEC3F *UnitCell,
263  VEC3F *CellAngles, char *spacegroup,
264  REAL OrigMatrix[3][4], REAL ScaleMatrix[3][4])
265  ---------------------------------------------------------------------
266 *//**
267 
268  \param[in] *wpdb Whole PDB pointer
269  \param[out] *UnitCell The unit cell dimensions
270  \param[out] *CellAngles The unit cell angles
271  \param[out] *spacegroup The spacegroup
272  \param[out] OrigMatrix The origin matrix
273  \param[out] ScaleMatrix The scale matrix
274  \return Flags for elements read.
275 
276 
277  Flags for elements read:
278  0: Nothing at all
279  XTAL_DATA_CRYST: Unit cell
280  XTAL_DATA_ORIGX: Origin matrix
281  XTAL_DATA_SCALE: Scale matrix
282  (These are ORed together)
283 
284  Read the crystal parameters (unit cell, spacegroup, origin and scale
285  matrices) out of a PDB file.
286 
287  Stops searching as soon as an ATOM or HETATM record is hit and leaves
288  the file in a state ready for ReadPDB() to do it's stuff (i.e. with
289  the current file pointer at the first ATOM or HETATM record).
290 
291 - 16.03.15 Original based on blGetCrystPDB() By: ACRM
292 */
293 int blGetCrystWholePDB(WHOLEPDB *wpdb, VEC3F *UnitCell, VEC3F *CellAngles,
294  char *spacegroup,
295  REAL OrigMatrix[3][4], REAL ScaleMatrix[3][4])
296 {
297  int i, j,
298  record,
299  retval = 0x0000;
300  STRINGLIST *s = NULL;
301 
302  /* Initialise matrices and cell dimensions */
303  UnitCell->x = UnitCell->y = UnitCell->z = (REAL)1.0;
304  CellAngles->x = CellAngles->y = CellAngles->z = (REAL)90.0*PI/180.0;
305  for(i=0; i<3; i++)
306  {
307  for(j=0; j<4; j++)
308  {
309  OrigMatrix[i][j] = (REAL)0.0;
310  ScaleMatrix[i][j] = (REAL)0.0;
311  }
312  OrigMatrix[i][i] = (REAL)1.0;
313  }
314  strcpy(spacegroup,"P");
315 
316  /* Run through the headers */
317  for(s=wpdb->header; s!=NULL; NEXT(s))
318  {
319  char *buffer;
320 
321  buffer = s->string;
322 
323  /* Check the relevant record styles */
324  if(!strncmp(buffer,"CRYST1",6))
325  {
326  /* If it was a CRYST record, extract the crystal parameters and
327  set the return value
328  */
329  fsscanf(buffer,
330  "%6x%9.3lf%9.3lf%9.3lf%7.2lf%7.2lf%7.2lf%1x%14s",
331  &(UnitCell->x),
332  &(UnitCell->y),
333  &(UnitCell->z),
334  &(CellAngles->x),
335  &(CellAngles->y),
336  &(CellAngles->z),
337  spacegroup);
338  CellAngles->x *= PI/180.0;
339  CellAngles->y *= PI/180.0;
340  CellAngles->z *= PI/180.0;
341 
342  retval |= XTAL_DATA_CRYST;
343  }
344  else if(!strncmp(buffer,"ORIGX",5))
345  {
346  /* If it was an ORIGX record, extract the crystal parameters and
347  set the return value
348  */
349  fsscanf(buffer,"%5x%1d",&record);
350  record--;
351  fsscanf(buffer,
352  "%10x%10.6lf%10.6lf%10.6lf%15.5lf",
353  &(OrigMatrix[record][0]),
354  &(OrigMatrix[record][1]),
355  &(OrigMatrix[record][2]),
356  &(OrigMatrix[record][3]));
357 
358  retval |= XTAL_DATA_ORIGX;
359  }
360  else if(!strncmp(buffer,"SCALE",5))
361  {
362  /* If it was a SCALE record, extract the crystal parameters and
363  set the return value
364  */
365  fsscanf(buffer,"%5x%1d",&record);
366  record--;
367  fsscanf(buffer,
368  "%10x%10.6lf%10.6lf%10.6lf%15.5lf",
369  &(ScaleMatrix[record][0]),
370  &(ScaleMatrix[record][1]),
371  &(ScaleMatrix[record][2]),
372  &(ScaleMatrix[record][3]));
373 
374  retval |= XTAL_DATA_SCALE;
375  }
376  }
377 
378  return(retval);
379 }
380 
381 
Include file for PDB routines.
#define XTAL_DATA_SCALE
Definition: pdb.h:578
REAL x
Definition: MathType.h:70
short BOOL
Definition: SysDefs.h:64
#define NULL
Definition: array2.c:99
int blGetCrystWholePDB(WHOLEPDB *wpdb, VEC3F *UnitCell, VEC3F *CellAngles, char *spacegroup, REAL OrigMatrix[3][4], REAL ScaleMatrix[3][4])
Definition: GetCrystPDB.c:293
#define MAXBUFF
Definition: GetCrystPDB.c:78
#define PI
Definition: macros.h:215
STRINGLIST * header
Definition: pdb.h:375
#define FALSE
Definition: macros.h:223
Definition: pdb.h:372
#define NEXT(x)
Definition: macros.h:249
int blGetCrystPDB(FILE *fp, VEC3F *UnitCell, VEC3F *CellAngles, char *spacegroup, REAL OrigMatrix[3][4], REAL ScaleMatrix[3][4])
Definition: GetCrystPDB.c:122
#define XTAL_DATA_ORIGX
Definition: pdb.h:577
int fsscanf(char *buffer, char *format,...)
Definition: fsscanf.c:177
Definition: MathType.h:69
double REAL
Definition: MathType.h:67
Include file for fsscanf()
#define TRUE
Definition: macros.h:219
System-type variable type definitions.
#define XTAL_DATA_CRYST
Definition: pdb.h:576
Type definitions for maths.
REAL z
Definition: MathType.h:70
char * string
Definition: general.h:85
REAL y
Definition: MathType.h:70