summaryrefslogtreecommitdiff
path: root/mmdb2/mmdb_cryst.h
blob: 4ca4150713b0bdbf883a273d11bb9eaea5311b9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//  $Id: mmdb_cryst.h $
//  =================================================================
//
//   CCP4 Coordinate Library: support of coordinate-related
//   functionality in protein crystallography applications.
//
//   Copyright (C) Eugene Krissinel 2000-2013.
//
//    This library is free software: you can redistribute it and/or
//    modify it under the terms of the GNU Lesser General Public
//    License version 3, modified in accordance with the provisions
//    of the license to address the requirements of UK law.
//
//    You should have received a copy of the modified GNU Lesser
//    General Public License along with this library. If not, copies
//    may be downloaded from http://www.ccp4.ac.uk/ccp4license.php
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU Lesser General Public License for more details.
//
//  =================================================================
//
//    12.09.13   <--  Date of Last Modification.
//                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//  -----------------------------------------------------------------
//
//  **** Module  :  MMDB_Cryst <interface>
//       ~~~~~~~~~
//  **** Project :  MacroMolecular Data Base (MMDB)
//       ~~~~~~~~~
//  **** Classes :  mmdb::CrystContainer ( container for cryst. data )
//       ~~~~~~~~~  mmdb::NCSMatrix  ( non-cryst. symm. matrix class )
//                  mmdb::TVect      ( translation vector class      )
//                  mmdb::Cryst      ( MMDB cryst. section class     )
//
//  (C) E. Krissinel 2000-2013
//
//  =================================================================
//

#ifndef __MMDB_Cryst__
#define __MMDB_Cryst__

#include "mmdb_io_stream.h"
#include "mmdb_symop.h"
#include "mmdb_defs.h"
#include "mmdb_utils.h"

namespace mmdb  {

  //  ====================  CrystContainer  ======================

  DefineClass(CrystContainer);
  DefineStreamFunctions(CrystContainer);

  class CrystContainer : public ClassContainer  {

    public :

      CrystContainer () : ClassContainer() {}
      CrystContainer ( io::RPStream Object )
                        : ClassContainer ( Object ) {}
      ~CrystContainer() {}

      PContainerClass MakeContainerClass ( int ClassID );

      ERROR_CODE AddMTRIXLine ( cpstr S );

  };


  //  ==================  NCSMatrix  ========================

  enum NCSM_SET  {
    NCSMSET_Matrix1 = 0x00000001,
    NCSMSET_Matrix2 = 0x00000002,
    NCSMSET_Matrix3 = 0x00000004,
    NCSMSET_All     = 0x00000007
  };

  DefineClass(NCSMatrix);
  DefineStreamFunctions(NCSMatrix);

  class NCSMatrix : public ContainerClass  {

    friend class Cryst;

    public :

      int   serNum;   // serial number
      mat33 m;        // non-crystallographic symmetry matrix
      vect3 v;        // translational part of ncs matrix
      int   iGiven;   // iGiven flag (see PDB format)

      NCSMatrix ();
      NCSMatrix ( cpstr S );
      NCSMatrix ( io::RPStream Object );
      ~NCSMatrix();

      bool       PDBASCIIDump1   ( io::RFile f );
      ERROR_CODE ConvertPDBASCII ( cpstr S );
      void       MakeCIF         ( mmcif::PData CIF, int N );
      ERROR_CODE GetCIF          ( mmcif::PData CIF, int & n );
      CLASS_ID   GetClassID      () { return ClassID_NCSMatrix; }

      void  SetNCSMatrix    ( int serialNum,
                              mat33 & ncs_m, vect3 & ncs_v,
                              int i_Given );

      void  Copy  ( PContainerClass NCSMatrix );

      void  write ( io::RFile f );
      void  read  ( io::RFile f );

    protected :
      word  WhatIsSet;  //    mask       field
                        //   0x0001    MTRIX1 was converted
                        //   0x0002    MTRIX2 was converted
                        //   0x0004    MTRIX3 was converted

      void  Init();

  };


  //  ==================  TVect  ========================

  DefineClass(TVect);
  DefineStreamFunctions(TVect);

  class TVect : public ContainerClass  {

    public :

      int   serNum;   // serial number
      vect3 t;        // translation vector
      pstr  comment;  // comment

      TVect ();
      TVect ( cpstr S );
      TVect ( io::RPStream Object );
      ~TVect();

      void       PDBASCIIDump    ( pstr S, int N );
      ERROR_CODE ConvertPDBASCII ( cpstr S );
      void       MakeCIF         ( mmcif::PData CIF, int N );
      ERROR_CODE GetCIF          ( mmcif::PData CIF, int & n );
      CLASS_ID   GetClassID      () { return ClassID_TVect; }

      void  Copy  ( PContainerClass TVect );

      void  write ( io::RFile f );
      void  read  ( io::RFile f );

    protected :

      void  Init();

  };


  //  =================  Cryst  =======================

  DefineClass(Cryst);
  DefineStreamFunctions(Cryst);

  // constants for the CellCheck field
  enum CELL_CHECK  {
    CCHK_Ok           = 0x00000000,
    CCHK_NoCell       = 0x00000001,
    CCHK_Error        = 0x00000002,
    CCHK_Disagreement = 0x00000004,
    CCHK_NoOrthCode   = 0x00000008,
    CCHK_Translations = 0x00000010,
    CCHK_Unchecked    = 0x00001000
  };

  // constants for the WhatIsSet field
  enum CELL_SET  {
    CSET_CellParams1  = 0x00000001,
    CSET_CellParams2  = 0x00000002,
    CSET_CellParams   = 0x00000003,
    CSET_SpaceGroup   = 0x00000004,
    CSET_ZValue       = 0x00000008,
    CSET_CrystCard    = 0x0000000F,
    CSET_OrigMatrix1  = 0x00000010,
    CSET_OrigMatrix2  = 0x00000020,
    CSET_OrigMatrix3  = 0x00000040,
    CSET_OrigMatrix   = 0x00000070,
    CSET_ScaleMatrix1 = 0x00000080,
    CSET_ScaleMatrix2 = 0x00000100,
    CSET_ScaleMatrix3 = 0x00000200,
    CSET_ScaleMatrix  = 0x00000380,
    CSET_Transforms   = 0x00000400,
    CSET_DummyCell    = 0x00001000
  };

  extern cpstr OrthCode[6];

  class Cryst : public io::Stream  {

    friend class Channel;

    public :
      realtype  a,b,c;            // cell parameters
      realtype  alpha,beta,gamma; // cell parameters
      mat44     RO,RF;            // orthogonal-fractional recalculation
                                  //   matrices
      mat44     ROU,RFU;          // ort-frac recalc matrices for
                                  //   anisotr. t-fac
      mat633    RR;               // standard orthogonalizations
      realtype  Vol;              // cell volume
      int       NCode;            // code of orthogonalization matrix
      SymGroup  spaceGroup;       // group of space symmetry as read
                                  //    from file
      SymGroup  spaceGroupFix;    // actually used space group
      int       Z;                // Z-value

      mat33     o;                // orthogonal transformation matrix
      vect3     t;                // translation orthogonal vector
      mat33     s;                // scale matrix
      vect3     u;                // translation part of the scale matrix

      word      CellCheck;        // 0x0000 - Ok
                                  // 0x0001 - no cell stored
                                  // 0x0002 - some error in cell volume
                                  // 0x0004 - disagreement between
                                  //           cell and PDB
                                  // 0x0008 - no orth code derived
                                  // 0x0010 - translations also specified
                                  // 0x1000 - the check was not done
      word   WhatIsSet;           // indicator of the fields set
      bool   ignoreScalei;        // flag to ignore SCALEi cards
      bool   processSG;           // flag to process space group at file
                                  // read
      bool   fixSpaceGroup;       // flag to fix space group at file read

      Cryst ();
      Cryst ( io::RPStream Object );
      ~Cryst();

      void  FreeMemory();
      void  Reset     ();

      //   ConvertPDBString(..) interprets an ASCII PDB line and fills
      // the corresponding data fields. It returns zero if the line was
      // successfully converted, otherwise returns a non-negative value
      // of Error_XXXX.
      //   PDBString must be not shorter than 81 characters.
      ERROR_CODE ConvertPDBString ( pstr PDBString );

      //   RWBROOKReadPrintout() may be invoked after reading PDB file
      // for simulating the old RWBROOK messages and warnings
      void  RWBROOKReadPrintout();

      void  SetCell ( realtype cell_a,
                      realtype cell_b,
                      realtype cell_c,
                      realtype cell_alpha,
                      realtype cell_beta,
                      realtype cell_gamma,
                      int      OrthCode );
      void  PutCell ( realtype cell_a,
                      realtype cell_b,
                      realtype cell_c,
                      realtype cell_alpha,
                      realtype cell_beta,
                      realtype cell_gamma,
                      int      OrthCode );

      void  GetCell ( realtype & cell_a,
                      realtype & cell_b,
                      realtype & cell_c,
                      realtype & cell_alpha,
                      realtype & cell_beta,
                      realtype & cell_gamma,
                      realtype & vol );

      void  GetRCell ( realtype & cell_as,
                       realtype & cell_bs,
                       realtype & cell_cs,
                       realtype & cell_alphas,
                       realtype & cell_betas,
                       realtype & cell_gammas,
                       realtype & vols );

      void  SetSyminfoLib ( cpstr syminfoLib );
      pstr  GetSyminfoLib ();

      int   SetSpaceGroup ( cpstr spGroup );
      pstr  GetSpaceGroup ();
      pstr  GetSpaceGroupFix();

      //   CalcCoordTransforms() should be called once after all data
      // relevant to the crystallographic information, are read and
      // converted. Field CellCheck will then have bits set if there
      // are errors, e.g. bit CCHK_NoCell means that the coordinate
      // transformations cannot be performed.
      void  CalcCoordTransforms();

      // A PDB ASCII dump
      void  PDBASCIIDump ( io::RFile f );

      ERROR_CODE GetCIF  ( mmcif::PData CIF );
      void  MakeCIF ( mmcif::PData CIF );

      bool areMatrices();  // returns True if the orthogonal-to-
                              // fractional and fractional-to-orthogonal
                              // matrices are defined

      //   Frac2Orth(..) and Orth2Frac(..) transform between fractional
      // and orthogonal coordinates, if areMatrices() returns True.
      // If the transformation matrices were not set, the functions just
      // copy the coordinates.  Returns True if the transformation was
      // done; False return means that transformation matrices were not
      // calculated
      bool Frac2Orth (
                realtype x,    realtype y,    realtype z,
                realtype & xx, realtype & yy, realtype & zz );
      bool Orth2Frac (
                realtype x,    realtype y,    realtype z,
                realtype & xx, realtype & yy, realtype & zz );

      //   Below, F and T are transformation matrices in fractional and
      // orthogonal coordinates, respectively.
      bool Frac2Orth ( mat44 & F, mat44 & T );
      bool Orth2Frac ( mat44 & T, mat44 & F );


      //   Cryst2Orth(..) and Orth2Cryst(..) transform between fractional
      // and orthogonal anisotropic temperature factors, if areMatrices()
      // returns True. If the transformation matrices were not set, the
      // functions leave the factors unchanged.
      //   Vector U is composed as follows:
      //      U[0]=u11   U[1]=u22   U[2]=u33
      //      U[3]=u12   U[4]=u13   U[5]=u23
      // Returns True if the transformation was done; False retuen
      // means that transformation matrices were not calculated
      bool  Cryst2Orth ( rvector U );
      bool  Orth2Cryst ( rvector U );

      void  CalcOrthMatrices();  // calculates RR, AC, cella's and Vol

      bool  isNCSMatrix     ();
      bool  isScaleMatrix   ();
      bool  isCellParameters();

      int   GetNumberOfSymOps();
      pstr  GetSymOp ( int Nop );

      int   GetNumberOfNCSMatrices();
      int   GetNumberOfNCSMates   ();  // Returns the number of
                                       // NCS mates not given in
                                       // the file (iGiven==0)

      bool  GetNCSMatrix ( int NCSMatrixNo, mat33 & ncs_m,
                           vect3 & ncs_v );
      bool  GetNCSMatrix ( int NCSMatrixNo, mat44 & ncs_m,
                           int & iGiven ); // no=0..N-1
      int   AddNCSMatrix ( mat33 & ncs_m, vect3 & ncs_v, int iGiven );

      //  GetTMatrix(..) calculates and returns the coordinate
      //  transformation matrix, which converts orthogonal coordinates
      //  according to the symmetry operation number Nop and places
      //  them into unit cell shifted by cellshift_a a's, cellshift_b
      //  b's and cellshift_c c's.
      //
      //  Return 0 means everything's fine,
      //         1 there's no symmetry operation Nop defined
      //         2 fractionalizing/orthogonalizing matrices were not
      //           calculated
      //         3 cell parameters were not set up.
      int   GetTMatrix ( mat44 & TMatrix, int Nop,
                         int cellshift_a, int cellshift_b,
                         int cellshift_c, PSymOps symOpers=NULL );

      //  GetUCTMatrix(..) calculates and returns the coordinate
      //  transformation matrix, which converts orthogonal coordinates
      //  according to the symmetry operation Nop. Translation part
      //  of the matrix is  being chosen such that point (x,y,z) has
      //  least distance to the center of primary (333) unit cell,
      //  and then it is shifted by cellshift_a a's, cellshift_b b's
      //  and cellshift_c c's.
      //
      //  Return 0 means everything's fine,
      //         1 there's no symmetry operation Nop defined
      //         2 fractionalizing/orthogonalizing matrices were not
      //           calculated
      //         3 cell parameters were not set up.
      //
      int   GetUCTMatrix ( mat44 & TMatrix, int Nop,
                           realtype x, realtype y, realtype z,
                           int cellshift_a, int cellshift_b,
                           int cellshift_c, PSymOps symOpers=NULL );

      //  GetFractMatrix(..) calculates and returns the coordinate
      //  transformation matrix, which converts fractional coordinates
      //  according to the symmetry operation number Nop and places them
      //  into unit cell shifted by cellshift_a a's, cellshift_b b's and
      //  cellshift_c c's.
      //
      //  Return 0 means everything's fine,
      //         1 there's no symmetry operation Nop defined
      //         2 fractionalizing/orthogonalizing matrices were not
      //           calculated
      //         3 cell parameters were not set up.
      int   GetFractMatrix ( mat44 & TMatrix, int Nop,
                             int cellshift_a, int cellshift_b,
                             int cellshift_c, PSymOps symOpers=NULL );

      //  GetSymOpMatrix(..) returns the transformation matrix for
      //  Nop-th symmetry operator in the space group
      //
      //  Return 0 means everything's fine,
      //         1 there's no symmetry operation Nop defined
      //         2 fractionalizing/orthogonalizing matrices were not
      //           calculated
      //         3 cell parameters were not set up.
      //
      int   GetSymOpMatrix ( mat44 & TMatrix, int Nop );

      void  Copy  ( PCryst Cryst );

      void  write ( io::RFile f );    // writes header to PDB binary file
      void  read  ( io::RFile f );    // reads header from PDB binary file

    protected :

      CrystContainer ncsMatrix;      // non-cryst. symm. matrices
      CrystContainer tVect;          // translation vectors

      realtype  as,bs,cs;             // calculated 'cell parameters'
      realtype  alphas,betas,gammas;  // calculated 'cell parameters'
      realtype  AC[6];
      realtype  VolChk,VolErr;

      pstr      syminfo_lib;          // path to syminfo.lib
      SymOps    symOps;               // symmetry operations

      void  Init ( bool fullInit );
      int   FixSpaceGroup();

  };

  extern cpstr getOrthCodeName ( int NCode );

}  // namespace mmdb

/*
extern void  TestCryst();  //  reads from 'in.cryst', writes into
                           //  'out.cryst' and 'abin.cryst'
*/


#endif