Bsoft  2.1.0
Bernard's software package
rwCCP4.cpp
Go to the documentation of this file.
1 
9 #include "rwCCP4.h"
10 #include "UnitCell.h"
11 #include "file_util.h"
12 #include "utilities.h"
13 
14 #ifndef NOSYMOP
15 #include "rwsymop.h"
16 #endif
17 
18 // Declaration of global variables
19 extern int verbose; // Level of output to the screen
20 
34 int set_CCP4_machine_stamp(char* machine_stamp)
35 {
36  switch ( systype(0) ) {
37  case BigIEEE:
38  machine_stamp[0] = machine_stamp[1] = 17;
39  break;
40  case LittleIEEE:
41  machine_stamp[0] = 68;
42  machine_stamp[1] = 65;
43  break;
44  case LittleVAX:
45  machine_stamp[0] = 34;
46  machine_stamp[1] = 65;
47  break;
48  default:
49  break;
50  }
51 
52  return 0;
53 }
54 
81 int readCCP4(Bimage* p, int readdata)
82 {
83  ifstream* fimg = new ifstream(p->file_name());
84  if ( fimg->fail() ) return -1;
85 
86  if ( verbose & VERB_DEBUG )
87  cout << "DEBUG readCCP4: File opened" << endl;
88 
89  CCP4head* header = new CCP4head;
90 
91  fimg->read((char *)header, CCP4SIZE);
92  if ( fimg->fail() ) return -2;
93 
94  // Determine byte order and swap bytes if from little-endian machine
95  unsigned char* b = (unsigned char *) header;
96  long i, sb(0);
97  if ( abs(header->mode) > SWAPTRIG || abs(header->mapc) > SWAPTRIG ) {
98  if ( verbose & VERB_PROCESS )
99  cerr << "Warning: Swapping header byte order for 4-byte types" << endl;
100  sb = 1;
101  long extent = CCP4SIZE - 800; // exclude labels from swapping
102  for ( i=0; i<extent; i+=4 )
103  if ( i < 208 || i > 212 ) swapbytes(b+i, 4);
104  }
105 
106  if ( header->nx < 1 || header->ny < 1 || header->nz < 1 || header->mode < 0 ) {
107  cerr << "Error readCCP4: The file " << p->file_name() << " is not a valid CCP4 image!" << endl;
108  cerr << "Size: " << header->nx << tab << header->ny <<
109  tab << header->nz << endl;
110  fimg->close();
111  return -3;
112  }
113 
114  // Convert VAX floating point types if necessary
115  // Machine stamp for vax is: 34 65 00 00
116  if ( ( header->machst[0] == 34 ) || ( header->amin > header->amax ) ) {
117  if ( verbose & VERB_PROCESS )
118  cerr << "Warning: Converting VAX floating point" << endl;
119  for ( i=40; i<64; i+=4 ) // Unit cell parameters
120  vax2ieee(b+i, sb);
121  for ( i=76; i<88; i+=4 ) // Min, max, mean
122  vax2ieee(b+i, sb);
123  for ( i=100; i<204; i+=4 ) // Skew matrix and extra
124  vax2ieee(b+i, sb);
125  vax2ieee(b+216, sb); // RMS
126  }
127 
128  // Map the parameters
129  if ( strncmp(header->map, "STK ", 4) == 0 ) {
130  p->images(header->nz);
131  p->size(header->nx, header->ny, 1);
132  } else {
133  p->images(1);
134  p->size(header->nx, header->ny, header->nz);
135  }
136  p->channels(1);
137  switch ( header->mode ) {
138  case 0: p->data_type(SCharacter); break;
139  case 1: p->data_type(Short); break;
140  case 2: p->data_type(Float); break;
141  case 3: p->data_type(Short); p->compound_type(TComplex); p->channels(2); break;
142  case 4: p->data_type(Float); p->compound_type(TComplex); p->channels(2); break;
143  default: p->data_type(SCharacter); break;
144  }
145  p->data_offset(CCP4SIZE + header->nsymbt);
146  if ( header->mode > 2 ) {
147  fimg->seekg(0, ios::end);
148  if ( (double) fimg->tellg() > p->data_offset() + 0.8*p->alloc_size() )
149  p->sizeX(2*(p->sizeX() - 1));
150  if ( header->mx%2 == 1 ) p->sizeX(p->sizeX() + 1); // Quick fix for odd x-size maps
151  fimg->clear();
152  fimg->seekg(0, ios::beg);
153  }
154  p->minimum(header->amin);
155  p->maximum(header->amax);
156  p->average(header->amean);
157  p->standard_deviation(header->arms);
158 
159  if ( header->mx && header->my && header->mz )
160  p->sampling(header->a/header->mx, header->b/header->my, header->c/header->mz);
161  p->space_group(header->ispg);
162  UnitCell uc(header->a, header->b, header->c, header->alpha, header->beta, header->gamma);
163  uc.degrees_to_radians();
164  p->unit_cell(uc);
165  p->label(header->labels);
166 
167  // Allocating the single sub-image and setting its origin
168  p->origin(-header->nxStart, -header->nyStart, -header->nzStart);
169 
170  delete header;
171 
172  if ( verbose & VERB_DEBUG )
173  cout << "DEBUG rwCCP4: Header setup done" << endl;
174 
175  long readsize = p->alloc_size();
176  unsigned char* data = NULL;
177 
178  if ( readdata ) {
179  p->data_alloc();
180  if ( p->compound_type() == TSimple ) {
181  fread_large(p->data_pointer(), readsize, p->data_offset(), fimg);
182  if ( sb ) swapbytes(readsize, p->data_pointer(), p->data_type_size());
183  } else {
184  readsize = p->channels()*(p->sizeX()/2+1)*p->sizeY()*p->sizeZ()*p->data_type_size();
185  data = new unsigned char[readsize];
186  fread_large(data, readsize, p->data_offset(), fimg);
187  if ( sb ) swapbytes(readsize, data, p->data_type_size());
188 // img_unpack_transform(p, data, CentHerm);
189  p->unpack_transform(data, CentHerm);
190  delete[] data;
191  }
192  }
193 
194  fimg->close();
195  delete fimg;
196 
197  if ( verbose & VERB_DEBUG )
198  cout << "DEBUG rwCCP4: Data read" << endl;
199 
200  return 0;
201 }
202 
210 {
211  if ( verbose & VERB_DEBUG )
212  cout << "DEBUG rwCCP4: Writing CCP4 file" << endl;
213 
214  p->color_to_simple();
215 
216 // if ( p->compound_type() == TComplex ) p->change_type(Float);
217 
218  switch ( p->data_type() ) {
219  case Bit: case UCharacter: case SCharacter:
220  if ( p->compound_type() == TComplex ) p->change_type(Short);
221  else p->change_type(SCharacter); break;
222  case UShort: case Short:
223  p->change_type(Short); break;
224  default:
225  p->change_type(Float); break;
226  }
227 
228  if ( verbose & VERB_DEBUG )
229  cout << "DEBUG rwCCP4: data type = " << p->data_type() << endl;
230 
231  CCP4head* header = new CCP4head;
232  memset(header, 0, sizeof(CCP4head));
233 
234  UnitCell uc = p->unit_cell();
235 
236  // Map the parameters
238  header->nx = p->sizeX();
239  header->ny = p->sizeY();
240  if ( p->sizeZ() > 1 || p->images() == 1 ) {
241  strncpy(header->map, "MAP ", 4);
242  header->nz = p->sizeZ();
243  } else {
244  strncpy(header->map, "STK ", 4);
245  header->nz = p->images();
246  }
247  if ( p->compound_type() == TComplex ) header->nx = p->sizeX()/2 + 1; // If a transform, physical storage is nx/2 + 1
248  switch ( p->data_type() ) {
249  case SCharacter: header->mode = 0; break;
250  case Short: header->mode = 1; if ( p->compound_type() == TComplex ) header->mode = 3; break;
251  case Float: header->mode = 2; if ( p->compound_type() == TComplex ) header->mode = 4; break;
252  default: header->mode = 0; break;
253  }
254  if ( p->image->origin()[0] >= 0 )
255  header->nxStart = (int) -(p->image->origin()[0] + 0.5);
256  else
257  header->nxStart = (int) -(p->image->origin()[0] - 0.5);
258  if ( p->image->origin()[1] >= 0 )
259  header->nyStart = (int) -(p->image->origin()[1] + 0.5);
260  else
261  header->nyStart = (int) -(p->image->origin()[1] - 0.5);
262  if ( p->image->origin()[2] >= 0 )
263  header->nzStart = (int) -(p->image->origin()[2] + 0.5);
264  else
265  header->nzStart = (int) -(p->image->origin()[2] - 0.5);
266  header->mx = (int) (uc.a()/p->sampling(0)[0] + 0.5);
267  header->my = (int) (uc.b()/p->sampling(0)[1] + 0.5);
268  header->mz = (int) (uc.c()/p->sampling(0)[2] + 0.5);
269  header->mapc = 1;
270  header->mapr = 2;
271  header->maps = 3;
272  header->amin = p->minimum();
273  header->amax = p->maximum();
274  header->amean = p->average();
275  header->arms = p->standard_deviation();
276 // cout << "Unitcell %g %g %g\n", uc.a(), uc.b(), uc.c());
277  header->a = uc.a();
278  header->b = uc.b();
279  header->c = uc.c();
280 
281  // This is a band-aid to overcome the limitations of the image format
282  if ( fabs(header->a - p->sampling(0)[0]*header->mx) > 0.001 || fabs(header->b - p->sampling(0)[1]*header->my) > 0.001 ||
283  fabs(header->c - p->sampling(0)[2]*header->mz) > 0.001 ) {
284  header->a = p->sampling(0)[0]*header->mx;
285  header->b = p->sampling(0)[1]*header->my;
286  header->c = p->sampling(0)[2]*header->mz;
287  if ( verbose & VERB_FULL )
288  cerr << "Warning: Resetting the unit cell to: " << header->a << "," << header->b << "," << header->c << endl;
289  }
290 
291  long i,j,k;
292  Matrix3 frac_mat = uc.skew_matrix();
293  header->alpha = uc.alpha()*180/M_PI;
294  header->beta = uc.beta()*180/M_PI;
295  header->gamma = uc.gamma()*180/M_PI;
296  for ( i=k=0; i<3; i++ ) for ( j=0; j<3; j++, k++ ) header->skwmat[k] = frac_mat[i][j];
297 // for ( i=0; i<3; i++ ) header->skwtrn[i] = frac_mat[i+9];
298  header->ispg = p->space_group();
299 
300  if ( verbose & VERB_DEBUG )
301  cout << "DEBUG rwCCP4: Writing labels" << endl;
302 
303  header->nlabl = 10;
304  strncpy(header->labels, p->label().c_str(), 799);
305  header->labels[799] = 0;
306 
307  if ( verbose & VERB_DEBUG ) {
308  cout << "DEBUG rwCCP4: nlabl = " << header->nlabl << endl;
309  for ( i=0; i<header->nlabl; i++ )
310  if ( header->labels[i*80] ) cout << &header->labels[i*80] << endl;
311  }
312 
313  int nsym(0);
314  Bstring temp;
315  char* symop = NULL;
316 
317 #ifndef NOSYMOP
318  if ( p->space_group() > 0 ) symop = read_symop(temp, p->space_group(), nsym);
319 #endif
320 
321  header->nsymbt = nsym*80;
322 
323  if ( verbose & VERB_DEBUG )
324  cout << "DEBUG rwCCP4: nsymbt = " << header->nsymbt << endl;
325 
326  p->data_offset(CCP4SIZE + header->nsymbt);
327 
328  long datatypesize = p->channels()*p->data_type_size();
329  long datasize = (long)header->nx*header->ny*header->nz*datatypesize;
330 
331  if ( verbose & VERB_DEBUG )
332  cout << "DEBUG rwCCP4: offset=" << p->data_offset() << " typesize=" << datatypesize << " datasize=" << datasize << endl;
333 
334  unsigned char* data = NULL;
335 
336  ofstream fimg(p->file_name());
337  if ( fimg.fail() ) return -1;
338 
339  fimg.write((char *)header, CCP4SIZE);
340 
341  if ( header->nsymbt ) fimg.write((char *)symop, header->nsymbt);
342 
343  if ( p->data_pointer() ) {
344  if ( p->compound_type() < TComplex ) {
345  fimg.write((char *)p->data_pointer(), datasize);
346  } else {
347  data = new unsigned char[datasize];
348  p->pack_transform(data, CentHerm);
349  fimg.write((char *)data, datasize);
350  delete[] data;
351  }
352  }
353 
354  fimg.close();
355 
356  if ( symop ) delete[] symop;
357  delete header;
358 
359  if ( verbose & VERB_DEBUG )
360  cout << "DEBUG rwCCP4: Done writing" << endl;
361 
362  return 0;
363 }
364 
CCP4head::gamma
float gamma
Definition: rwCCP4.h:31
CCP4head::mz
int mz
Definition: rwCCP4.h:25
rwsymop.h
Header file for reading and writing symmetry operators.
CCP4head::nzStart
int nzStart
Definition: rwCCP4.h:22
CentHerm
@ CentHerm
Definition: Bimage.h:45
vax2ieee
void vax2ieee(unsigned char *v, int sb)
Converts VAX floating point format to IEEE floating point format.
Definition: utilities.cpp:512
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
CCP4head::mapc
int mapc
Definition: rwCCP4.h:32
CCP4head::amean
float amean
Definition: rwCCP4.h:37
Bit
@ Bit
Definition: utilities.h:152
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
CCP4head
Definition: rwCCP4.h:15
UCharacter
@ UCharacter
Definition: utilities.h:153
file_util.h
Header file for file checking functions.
fread_large
int fread_large(unsigned char *aptr, size_t pagesize, size_t offset, ifstream *fimg)
Reads blocks of memeory no larger than 1Gb.
Definition: file_util.cpp:347
Bstring
Definition: Bstring.h:29
UnitCell::c
void c(double d)
Definition: UnitCell.h:38
set_CCP4_machine_stamp
int set_CCP4_machine_stamp(char *machine_stamp)
Setting a CCP4 style machine stamp.
Definition: rwCCP4.cpp:34
systype
SysType systype(int show)
Finds the system type - mostly just for byte order.
Definition: utilities.cpp:151
CCP4head::ispg
int ispg
Definition: rwCCP4.h:38
CCP4head::labels
char labels[800]
Definition: rwCCP4.h:48
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
Bimage::unit_cell
void unit_cell(UnitCell uc)
Definition: Bimage.h:608
TComplex
@ TComplex
Definition: Bimage.h:60
CCP4head::alpha
float alpha
Definition: rwCCP4.h:29
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
UnitCell.h
Header file for unit cell functions.
sb
int sb(0)
UnitCell::b
void b(double d)
Definition: UnitCell.h:37
Bimage::data_type_size
long data_type_size() const
Returns the size of the datatype in bytes.
Definition: Bimage.cpp:1607
CCP4head::amax
float amax
Definition: rwCCP4.h:36
CCP4head::c
float c
Definition: rwCCP4.h:28
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Bimage::maximum
double maximum()
Definition: Bimage.h:542
Bimage::standard_deviation
double standard_deviation()
Definition: Bimage.h:544
CCP4head::a
float a
Definition: rwCCP4.h:26
CCP4head::ny
int ny
Definition: rwCCP4.h:17
Bimage::images
long images()
Definition: Bimage.h:504
Matrix3
Definition: Matrix3.h:18
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
Short
@ Short
Definition: utilities.h:156
TSimple
@ TSimple
Definition: Bimage.h:59
verbose
int verbose
Bimage::pack_transform
int pack_transform(unsigned char *data, FourierType tf)
Definition: Bimage.cpp:2441
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
CCP4head::mx
int mx
Definition: rwCCP4.h:23
CCP4head::arms
float arms
Definition: rwCCP4.h:46
Bimage::alloc_size
long alloc_size() const
Definition: Bimage.h:267
CCP4head::skwmat
float skwmat[9]
Definition: rwCCP4.h:41
CCP4head::nz
int nz
Definition: rwCCP4.h:18
rwCCP4.h
Header file for reading and writing CCP4 files.
swapbytes
void swapbytes(unsigned char *v, size_t n)
Swaps bytes.
Definition: utilities.cpp:468
CCP4head::mapr
int mapr
Definition: rwCCP4.h:33
Bimage::data_offset
long data_offset()
Definition: Bimage.h:288
UShort
@ UShort
Definition: utilities.h:155
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:364
CCP4head::amin
float amin
Definition: rwCCP4.h:35
Bimage::average
double average(long cc, double xf, double yf, double zf, long nn, double iscale)
Averages in 3D when scaling is less than 1.
Definition: Bimage.cpp:1371
UnitCell::alpha
void alpha(double d)
Definition: UnitCell.h:39
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:271
Bimage::color_to_simple
void color_to_simple()
A color image is converted to a simple image.
Definition: Bimage_color.cpp:94
UnitCell::a
void a(double d)
Definition: UnitCell.h:36
LittleIEEE
@ LittleIEEE
Definition: utilities.h:135
BigIEEE
@ BigIEEE
Definition: utilities.h:133
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
UnitCell::skew_matrix
Matrix3 skew_matrix()
Calculates the skew matrix from the unit cell parameters.
Definition: UnitCell.cpp:26
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
CCP4head::mode
int mode
Definition: rwCCP4.h:19
CCP4SIZE
#define CCP4SIZE
Definition: rwCCP4.h:13
writeCCP4
int writeCCP4(Bimage *p)
Writing a CCP4 map image file format.
Definition: rwCCP4.cpp:209
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
CCP4head::nxStart
int nxStart
Definition: rwCCP4.h:20
SWAPTRIG
#define SWAPTRIG
Definition: rwimg.h:16
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
CCP4head::nlabl
int nlabl
Definition: rwCCP4.h:47
CCP4head::beta
float beta
Definition: rwCCP4.h:30
LittleVAX
@ LittleVAX
Definition: utilities.h:137
CCP4head::maps
int maps
Definition: rwCCP4.h:34
Bimage::space_group
long space_group()
Definition: Bimage.h:598
CCP4head::nsymbt
int nsymbt
Definition: rwCCP4.h:39
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
SCharacter
@ SCharacter
Definition: utilities.h:154
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:284
CCP4head::map
char map[4]
Definition: rwCCP4.h:44
CCP4head::my
int my
Definition: rwCCP4.h:24
Bimage::minimum
double minimum()
Definition: Bimage.h:541
CCP4head::machst
char machst[4]
Definition: rwCCP4.h:45
UnitCell::beta
void beta(double d)
Definition: UnitCell.h:40
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
UnitCell
Definition: UnitCell.h:22
read_symop
char * read_symop(Bstring &filename, int spacegroup, int &nsym)
Reading crystallographic symmetry operators.
Definition: rwsymop.cpp:59
CCP4head::nyStart
int nyStart
Definition: rwCCP4.h:21
CCP4head::b
float b
Definition: rwCCP4.h:27
Float
@ Float
Definition: utilities.h:161
Bimage::channels
long channels()
Definition: Bimage.h:384
CCP4head::nx
int nx
Definition: rwCCP4.h:16
Bimage::unpack_transform
int unpack_transform(unsigned char *data, FourierType tf)
Definition: Bimage.cpp:2356
readCCP4
int readCCP4(Bimage *p, int readdata)
Reading a CCP4 map image file format.
Definition: rwCCP4.cpp:81
UnitCell::degrees_to_radians
void degrees_to_radians()
Definition: UnitCell.h:54
UnitCell::gamma
void gamma(double d)
Definition: UnitCell.h:41
Bimage::label
void label(string s)
Definition: Bimage.h:250