map.cpp 21.1 KB
Newer Older
Pilar Cossio's avatar
License    
Pilar Cossio committed
1
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
   < BioEM software for Bayesian inference of Electron Microscopy images>
3
4
   Copyright (C) 2016 Pilar Cossio, David Rohr, Fabio Baruffa, Markus Rampp, 
        Volker Lindenstruth and Gerhard Hummer.
5
   Max Planck Institute of Biophysics, Frankfurt, Germany.
Pilar Cossio's avatar
Pilar Cossio committed
6
7
   Frankfurt Institute for Advanced Studies, Goethe University Frankfurt, Germany.
   Max Planck Computing and Data Facility, Garching, Germany. 
David Rohr's avatar
David Rohr committed
8

Pilar Cossio's avatar
Pilar Cossio committed
9
   Released under the GNU Public License, v3. 
10
   See license statement for terms of distribution.
Pilar Cossio's avatar
License    
Pilar Cossio committed
11

12
13
14
   Note: This program contains subroutine "read_MRC" of the Situs 2.7.2 program. 
   Ref: Willy Wriggers. Using Situs for the Integration of Multi-Resolution Structures. 
   Biophysical Reviews, 2010, Vol. 2, pp. 21-27.
Pilar Cossio's avatar
Pilar Cossio committed
15
   with a GPL lisences version 3.
16
17


Pilar Cossio's avatar
License    
Pilar Cossio committed
18
19
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

20
21
22
23
24
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
25
26
#include <math.h>
#include <fftw3.h>
27

28
29
30
31
#ifdef WITH_OPENMP
#include <omp.h>
#endif

32
#include "map.h"
33
#include "param.h"
34
#include "bioem.h"
35

36
37
using namespace std;

38
int bioem_RefMap::readRefMaps(bioem_param& param, const char* filemap)
39
{
40
41
42
43
44
45
46
47
  numPixels = param.param_device.NumberPixels;
  refMapSize = param.param_device.NumberPixels * param.param_device.NumberPixels;
  // **************************************************************************************
  // ***********************Reading reference Particle Maps************************
  // **************************************************************************************
  int allocsize = 0;
  if (param.loadMap)
    {
48
      //************** Loading Map from Binary file *******
49
50
      FILE* fp = fopen("maps.dump", "rb");
      if (fp == NULL)
51
	{
52
53
54
55
56
57
58
59
60
61
62
63
	  cout << "Error opening dump file\n";
	  exit(1);
	}
      fread(&ntotRefMap, sizeof(ntotRefMap), 1, fp);
      maps = (myfloat_t*) mallocchk(ntotRefMap * refMapSize * sizeof(myfloat_t));
      fread(maps, sizeof(myfloat_t) * refMapSize, ntotRefMap, fp);
      fclose(fp);

      cout << "Particle Maps read from Map Dump\n";
    }
  else if(readMRC)
    {
64
      //************** Reading MRC file *******
65
66
67
68
      ntotRefMap=0;

      if(readMultMRC)
	{
69

70
71
	  //************** Reading Multiple MRC files *************
	  cout << "Opening File with MRC list names: " << filemap << "\n"; 
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
	  ifstream input(filemap);

	  if (!input.good())
	    {
	      cout << "Failed to open file contaning MRC names: " << filemap << "\n";
	      exit(0);
	    }

	  char line[512] = {0};
	  char mapname[100];
	  char tmpm[10] = {0};
	  const char* indifile;

	  while (!input.eof())
	    {
	      input.getline(line,511);
	      char tmpVals[100]  = {0};

90
	      string strline(line);
91

92
	      //	 cout << "MRC File name:" << strline << "\n";
93
94


95
96
97
98
99
	      strncpy (tmpVals,line,99);
	      sscanf (tmpVals,"%99c",mapname);

	      // Check for last line
	      strncpy (tmpm,mapname,3);
100

101
	      if(strcmp(tmpm,"XXX")!=0)
102
		{
103
		  indifile=strline.c_str();
Pilar Cossio's avatar
Pilar Cossio committed
104
          
105
106
		  //   size_t foundpos= strline.find("mrc");
		  //   size_t endpos = strline.find_last_not_of(" \t");
107
       
Pilar Cossio's avatar
Pilar Cossio committed
108

109
110
		  //Reading Multiple MRC
		  read_MRC(indifile,param);
111
		}
112
113
	      for(int i=0;i<3;i++)mapname[i] = 'X';
	      for(int i=3;i<100;i++)mapname[i] = 0;
114

115
116
117
118
119
120
	    }
	  cout << "\n+++++++++++++++++++++++++++++++++++++++++++ \n";
	  cout << "Particle Maps read from MULTIPLE MRC Files in: " << filemap << "\n" ;
	}
      else
	{
Pilar Cossio's avatar
Pilar Cossio committed
121

122
	  string strfilename(filemap);
Pilar Cossio's avatar
Pilar Cossio committed
123

124
125
	  size_t foundpos= strfilename.find("mrc");
	  size_t endpos = strfilename.find_last_not_of(" \t");
Pilar Cossio's avatar
Pilar Cossio committed
126

127
128
129
130
	  if(foundpos > endpos){
	    cout << "Warining:::: mrc extension NOT dectected in file name::" << filemap <<" \n";
	    cout << "Warining::::  Are you sure you want to read an MRC? \n";
	  }
Pilar Cossio's avatar
Pilar Cossio committed
131

132
133
134
	  read_MRC(filemap,param);
	  cout << "\n++++++++++++++++++++++++++++++++++++++++++ \n";
	  cout << "Particle Maps read from ONE MRC File: " << filemap << "\n" ;
135
	}
136
137
138
    }
  else
    {
139
      //************** Reading Text file *************
140
141
142
143
144
      int nummap = -1;
      int lasti = 0;
      int lastj = 0;
      ifstream input(filemap);
      if (!input.good())
David Rohr's avatar
David Rohr committed
145
	{
146
147
148
149
150
151
152
	  cout << "Particle Maps Failed to open file" << endl ;
	  exit(1);
	}

      char line[512] = {0};
      char tmpLine[512] = {0};
      bool first=true;
David Rohr's avatar
David Rohr committed
153

Pilar Cossio's avatar
Pilar Cossio committed
154
      int countpix=0;
Pilar Cossio's avatar
Pilar Cossio committed
155

156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
      while (!input.eof())
	{
	  input.getline(line, 511);

	  strncpy(tmpLine, line, strlen(line));
	  char *token = strtok(tmpLine, " ");

	  if(first){
	    if (strcmp(token, "PARTICLE") != 0) {
	      cout << "Missing correct Standard Map Format: PARTICLE HEADER\n"<< endl ;
	      exit(1);
	    }
	    first=false;
	  }

Pilar Cossio's avatar
Pilar Cossio committed
171

172
173
174
	  if (strcmp(token, "PARTICLE") == 0) // to count the number of maps
	    {
	      nummap++;
Pilar Cossio's avatar
Pilar Cossio committed
175
	      countpix=0;
176
	      if (allocsize == 0)
David Rohr's avatar
David Rohr committed
177
		{
178
179
		  allocsize = 64;
		  maps = (myfloat_t*) mallocchk(refMapSize * sizeof(myfloat_t) * allocsize);
180
		}
181
	      else if (nummap + 1 >= allocsize)
182
		{
183
184
		  allocsize *= 2;
		  maps = (myfloat_t*) reallocchk(maps, refMapSize * sizeof(myfloat_t) * allocsize);
David Rohr's avatar
David Rohr committed
185
		}
186
187
188
189
	      if (nummap % 128 == 0)
		{
		  cout << "..." << nummap << "\n";
		}
190
	      if(lasti+1 != param.param_device.NumberPixels && lastj+1 != param.param_device.NumberPixels && nummap > 0)
191
		{
192
193
		  cout << "PROBLEM INCONSISTENT NUMBER OF PIXELS IN MAPS AND INPUTFILE ( " << param.param_device.NumberPixels << ", i " << lasti << ", j " << lastj << ")" << "\n";
		  exit(1);
194
		}
195
196
197
198
199
	    }
	  else
	    {
	      int i, j;
	      float z;
Pilar Cossio's avatar
Pilar Cossio committed
200
	      
201
	      char tmpVals[36]  = {0};
202

203
204
205
206
207
208
	      strncpy (tmpVals, line, 8);
	      sscanf (tmpVals, "%d", &i);

	      strncpy (tmpVals, line + 8, 8);
	      sscanf (tmpVals, "%d", &j);

Pilar Cossio's avatar
Pilar Cossio committed
209
	      strncpy (tmpVals, line + 16, 16);
210
211
	      sscanf (tmpVals, "%f", &z);
	      //checking for Map limits
Pilar Cossio's avatar
Pilar Cossio committed
212
	      if(i > -1 && i  < param.param_device.NumberPixels && j > -1 && j  < param.param_device.NumberPixels)
213
		{
Pilar Cossio's avatar
Pilar Cossio committed
214
215
		  countpix++;
		  maps[nummap * refMapSize + i * numPixels + j] = (myfloat_t) z;
216
217
		  lasti = i;
		  lastj = j;
218
		  //	 cout << countpix << " " << param.param_device.NumberPixels*param.param_device.NumberPixels << "\n";
219
		}
220
	      else
221
		{
222
223
		  cout << "PROBLEM READING MAP (Map number " << nummap << ", i " << i << ", j " << j << ")" << "\n";
		  exit(1);
224
		}
225
226
	    }
	}
Pilar Cossio's avatar
Pilar Cossio committed
227
      if(lasti != param.param_device.NumberPixels-1 || lastj != param.param_device.NumberPixels-1 || countpix != param.param_device.NumberPixels*param.param_device.NumberPixels +1 )
228
229
230
	{
	  cout << "PROBLEM INCONSISTENT NUMBER OF PIXELS IN MAPS AND INPUTFILE ( " << param.param_device.NumberPixels << ", i " << lasti << ", j " << lastj << ")" << "\n";
	  exit(1);
231
	}
232
233
234
      cout << ".";
      ntotRefMap = nummap + 1;
      maps = (myfloat_t*) reallocchk(maps, refMapSize * sizeof(myfloat_t) * ntotRefMap);
235
      cout << "Particle Maps read from Standard File: " << ntotRefMap << "\n";
236
    }
237

238
  //************* If Dumping Maps *********************
Pilar Cossio's avatar
Pilar Cossio committed
239
  if (param.dumpMap)
240
241
242
243
244
245
246
247
248
249
250
    {
      FILE* fp = fopen("maps.dump", "w+b");
      if (fp == NULL)
	{
	  cout << "Error opening dump file\n";
	  exit(1);
	}
      fwrite(&ntotRefMap, sizeof(ntotRefMap), 1, fp);
      fwrite(maps, sizeof(myfloat_t) * refMapSize, ntotRefMap, fp);
      fclose(fp);
    }
Pilar Cossio's avatar
Pilar Cossio committed
251

252
  //*********** To Debug with few Maps ********************
Pilar Cossio's avatar
Pilar Cossio committed
253

254
255
256
257
  if (getenv("BIOEM_DEBUG_NMAPS"))
    {
      ntotRefMap = atoi(getenv("BIOEM_DEBUG_NMAPS"));
    }
David Rohr's avatar
David Rohr committed
258

259
260
261
262
  cout << "Total Number of particles: " << ntotRefMap;
  cout << "\n+++++++++++++++++++++++++++++++++++++++++++ \n";

  return(0);
263
264
265
266
}

int bioem_RefMap::PreCalculateMapsFFT(bioem_param& param)
{
267
  // **************************************************************************************
268
  // ********** Routine that pre-calculates Reference maps FFT for Convolution/ Comparison **********************
269
  // ************************************************************************************
270

271
  RefMapsFFT = new mycomplex_t[ntotRefMap * param.FFTMapSize];
272

273
#pragma omp parallel for
274
275
276
277
278
279
280
281
  for (int iRefMap = 0; iRefMap < ntotRefMap ; iRefMap++)
    {
      const int num = omp_get_thread_num();
      myfloat_t* localMap = param.fft_scratch_real[num];
      mycomplex_t* localout = param.fft_scratch_complex[num];

      //Assigning localMap values to padded Map
      for(int i = 0; i < param.param_device.NumberPixels; i++)
282
	{
283
284
285
286
287
	  for(int j = 0; j < param.param_device.NumberPixels; j++)
	    {
	      localMap[i * param.param_device.NumberPixels + j] = maps[iRefMap * refMapSize + i * param.param_device.NumberPixels + j];
	    }
	}
288

289
290
      //Calling FFT_Forward
      myfftw_execute_dft_r2c(param.fft_plan_r2c_forward, localMap, localout);
291

292
293
294
295
296
297
298
      //Saving the Reference CTFs (RefMap array possibly has incorrect alignment, so we copy here. Stupid but in fact does not matter.)
      mycomplex_t* RefMap = &RefMapsFFT[iRefMap * param.FFTMapSize];
               
      for(int i = 0; i < param.param_device.NumberPixels * param.param_device.NumberFFTPixels1D ; i++ )
	{
	  RefMap[i][0] = localout[i][0]; 
	  RefMap[i][1] = localout[i][1]; 
299
	}
300
    }
301

302
  return(0);
303
}
304
305
306

int bioem_RefMap::precalculate(bioem_param& param, bioem& bio)
{
307
308
309
  // **************************************************************************************
  // *******************************Precalculating Routine for Maps************************
  // **************************************************************************************
310

311
312
  sum_RefMap = (myfloat_t*) mallocchk(sizeof(myfloat_t) * ntotRefMap);
  sumsquare_RefMap = (myfloat_t*) mallocchk(sizeof(myfloat_t) * ntotRefMap);
313

314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
  //Precalculating cross-correlations of maps
#pragma omp parallel for
  for (int iRefMap = 0; iRefMap < ntotRefMap ; iRefMap++)
    {
      myfloat_t sum, sumsquare;
      bio.calcross_cor(getmap(iRefMap), sum, sumsquare);
      //Storing Crosscorrelations in Map class
      sum_RefMap[iRefMap] = sum;
      sumsquare_RefMap[iRefMap] = sumsquare;
    }

  // Precalculating Maps in Fourier space
  if (bio.FFTAlgo)
    {
      PreCalculateMapsFFT(param);
      free(maps);
      maps = NULL;
    }

  return(0);
334
}
335

336
void bioem_Probability::init(size_t maps, size_t angles, size_t cc, bioem& bio)
337
{
338
  //********** Initializing pointers *******************
339
340
341
342
  nMaps = maps;
  nAngles = angles;
  nCC = cc;
  ptr = bio.malloc_device_host(get_size(maps, angles, cc, bio.param.param_device.writeAngles, bio.param.param_device.writeCC));
Pilar Cossio's avatar
Pilar Cossio committed
343
  cout << "Allocation #Maps " << maps << " #Angles " << angles << " #cross.cor " << cc << "\n";
344
  //<< " == " << get_size(maps, angles, cc, bio.param.param_device.writeAngles, bio.param.param_device.writeCC)<< "\n";
345
  set_pointers();
346
}
347

David Rohr's avatar
David Rohr committed
348
349
void bioem_Probability::copyFrom(bioem_Probability* from, bioem& bio)
{
350

351
352
353
  bioem_Probability_map& pProbMap = getProbMap(0);
  bioem_Probability_map& pProbMapFrom = from->getProbMap(0);
  memcpy(&pProbMap, &pProbMapFrom, from->nMaps * sizeof(bioem_Probability_map));
David Rohr's avatar
David Rohr committed
354

355
356
357
  if (bio.param.param_device.writeAngles)
    {
      for (int iOrient = 0; iOrient < nAngles; iOrient ++)
David Rohr's avatar
David Rohr committed
358
	{
359
360
361
	  bioem_Probability_angle& pProbAngle = getProbAngle(0, iOrient);
	  bioem_Probability_angle& pProbAngleFrom = from->getProbAngle(0, iOrient);
	  memcpy(&pProbAngle, &pProbAngleFrom, from->nMaps * sizeof(bioem_Probability_angle));
David Rohr's avatar
David Rohr committed
362
	}
363
    }
364

365
366
367
  if (bio.param.param_device.writeCC)
    {
      for (int iCC = 0; iCC < nCC; iCC ++)
368
	{
369
370
371
	  bioem_Probability_cc& pProbCC = getProbCC(0, iCC);
	  bioem_Probability_cc& pProbCCFrom = from->getProbCC(0, iCC);
	  memcpy(&pProbCC, &pProbCCFrom, from->nMaps * sizeof(bioem_Probability_cc));
372
	}
373
    }
David Rohr's avatar
David Rohr committed
374
375
}

David Rohr's avatar
David Rohr committed
376
377
int bioem_RefMap::read_MRC(const char* filename,bioem_param& param)
{
378
379
380
381
382
383

  /* 	 subroutine "read_MRC" of the Situs 2.7.2 program. 
	 Ref: Willy Wriggers. Using Situs for the Integration of Multi-Resolution Structures. 
	 Biophysical Reviews, 2010, Vol. 2, pp. 21-27.*/


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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
  myfloat_t st,st2;
  unsigned long count;
  FILE *fin;
  float currfloat;
  int nc, nr, ns, swap, header_ok = 1;
  float xlen, ylen, zlen;
  int mode, ncstart, nrstart, nsstart, ispg, nsymbt, lskflg;
  float a_tmp, b_tmp, g_tmp;
  int  mx, my, mz,mapc, mapr, maps_local;
  float dmin, dmax, dmean;
  int n_range_viol0, n_range_viol1;

  fin = fopen(filename, "rb");
  if( fin == NULL ) {
    cout << "ERROR opening MRC: " << filename;
    exit(1);
  }
  n_range_viol0 = test_mrc(filename,0);
  n_range_viol1 = test_mrc(filename,1);

  if (n_range_viol0 < n_range_viol1) { //* guess endianism
    swap = 0;
    if (n_range_viol0 > 0) {
      printf(" Warning: %i header field range violations detected in file %s \n", n_range_viol0,filename);
    }
  } else {
    swap = 1;
    if (n_range_viol1 > 0) {
      printf("Warning: %i header field range violations detected in file %s \n", n_range_viol1,filename);
    }
  }
  printf("\n+++++++++++++++++++++++++++++++++++++++++++\n");
  printf("Reading Information from MRC: %s \n", filename);
  header_ok *= read_int(&nc,fin,swap);
  header_ok *= read_int(&nr,fin,swap);
  header_ok *= read_int(&ns,fin,swap);
  header_ok *= read_int(&mode,fin,swap);
  header_ok *= read_int(&ncstart,fin,swap);
  header_ok *= read_int(&nrstart,fin,swap);
  header_ok *= read_int(&nsstart,fin,swap);
  header_ok *= read_int(&mx,fin,swap);
  header_ok *= read_int(&my,fin,swap);
  header_ok *= read_int(&mz,fin,swap);
  header_ok *= read_float(&xlen,fin,swap);
  header_ok *= read_float(&ylen,fin,swap);
  header_ok *= read_float(&zlen,fin,swap);
  header_ok *= read_float(&a_tmp,fin,swap);
  header_ok *= read_float(&b_tmp,fin,swap);
  header_ok *= read_float(&g_tmp,fin,swap);
  header_ok *= read_int(&mapc,fin,swap);
  header_ok *= read_int(&mapr,fin,swap);
  header_ok *= read_int(&maps_local,fin,swap);
  header_ok *= read_float(&dmin,fin,swap);
  header_ok *= read_float(&dmax,fin,swap);
  header_ok *= read_float(&dmean,fin,swap);
  header_ok *= read_int(&ispg,fin,swap);
  header_ok *= read_int(&nsymbt,fin,swap);
  header_ok *= read_int(&lskflg,fin,swap);

  printf("Number Columns  = %8d \n",nc);
  printf("Number Rows     = %8d \n",nr);
  printf("Number Sections = %8d \n",ns);
  printf("MODE = %4d (only data type mode 2: 32-bit)\n",mode);
  printf("NSYMBT = %4d (# bytes symmetry operators)\n",nsymbt);

  /* printf("  NCSTART = %8d  (index of first column, counting from 0)\n",ncstart);
     printf(">  NRSTART = %8d  (index of first row, counting from 0)\n",nrstart);
     printf("  NSSTART = %8d  (index of first section, counting from 0)\n",nsstart);
     printf("       MX = %8d  (# of X intervals in unit cell)\n",mx);
     printf("       MY = %8d  (# of Y intervals in unit cell)\n",my);
     printf("       MZ = %8d  (# of Z intervals in unit cell)\n",mz);
     printf(" X length = %8.3f  (unit cell dimension)\n",xlen);
     printf(" Y length = %8.3f  (unit cell dimension)\n",ylen);
     printf(" Z length = %8.3f  (unit cell dimension)\n",zlen);
     printf("    Alpha = %8.3f  (unit cell angle)\n",a_tmp);
     printf("     Beta = %8.3f  (unit cell angle)\n",b_tmp);
     printf("    Gamma = %8.3f  (unit cell angle)\n",g_tmp);
     printf("     MAPC = %8d  (columns axis: 1=X,2=Y,3=Z)\n",mapc);
     printf("     MAPR = %8d  (rows axis: 1=X,2=Y,3=Z)\n",mapr);
     printf("     MAPS = %8d  (sections axis: 1=X,2=Y,3=Z)\n",maps_local);
     printf("     DMIN = %8.3f  (minimum density value - ignored)\n",dmin);
     printf("     DMAX = %8.3f  (maximum density value - ignored)\n",dmax);
     printf("    DMEAN = %8.3f  (mean density value - ignored)\n",dmean);
     printf("     ISPG = %8d  (space group number - ignored)\n",ispg);
     printf("   NSYMBT = %8d  (# bytes storing symmetry operators)\n",nsymbt);
     printf("   LSKFLG = %8d  (skew matrix flag: 0:none, 1:follows)\n",lskflg);*/

  if (header_ok == 0) {
    cout << "ERROR reading MRC header: " << filename;
    exit(1);
  }

  if(nr!=param.param_device.NumberPixels || nc!=param.param_device.NumberPixels )
    {
      cout << "PROBLEM INCONSISTENT NUMBER OF PIXELS IN MAPS AND INPUTFILE ( " << param.param_device.NumberPixels << ", i " << nc << ", j " << nr << ")" << "\n";
479
      if(!param.notsqure)  exit(1);
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
    }

  if (ntotRefMap == 0)
    {
      maps = (myfloat_t*) mallocchk(refMapSize * sizeof(myfloat_t) * ns);
    }
  else
    {
      maps = (myfloat_t*) reallocchk(maps, refMapSize * sizeof(myfloat_t) * (ntotRefMap + ns));
    }

  if(mode!=2)
    {
      cout << "ERROR with MRC mode " << mode << "\n";
      cout << "Currently mode 2 is the only one allowed" << "\n";
      exit(1);
    }
  else
    {
      rewind (fin);
      for (count=0; count<256; ++count) if (read_float_empty(fin)==0) {
	  cout << "ERROR Converting Data: " <<  filename;
	  exit(1);
David Rohr's avatar
David Rohr committed
503
504
	}

505
506
507
      for (count=0; count<(unsigned long)nsymbt; ++count) if (read_char_float(&currfloat,fin)==0) {
	  cout << "ERROR Converting Data: " <<  filename;
	  exit(1);
508
509
	}

510
      for ( int nmap = 0 ; nmap < ns ; nmap ++ )
David Rohr's avatar
David Rohr committed
511
	{
512
513
514
515
516
517
518
519
520
521
522
523
	  st=0.0;
	  st2=0.0;
	  for ( int j = 0 ; j < nr ; j ++ )
	    for ( int i = 0 ; i < nc ; i ++ )
	      {
		if (read_float(&currfloat,fin,swap)==0)
		  {
		    cout << "ERROR Converting Data: " <<  filename;
		    exit(1);
		  }
		else
		  {
524
525
526
527
528
529
530
		    if(!param.notsqure){
		      maps[(nmap + ntotRefMap) * refMapSize + i * numPixels + j] = (myfloat_t) currfloat;
		      st += currfloat;
		      st2 += currfloat*currfloat;
		    } else {
		      if( i > 595 && i < 675 && j > 1250 && j< 1330 && nmap >230 && nmap <310)cout << "map1: " << i << " "<< j << " " << nmap << " " << currfloat <<"\n";
		    }
531
532
		  }
	      }
533
	  if(param.notsqure)exit(1);
534
	  //Normaling maps to zero mean and unit standard deviation
535
	  if(!param.notnormmap){
536
537
538
539
540
541
542
	    st /= float(nr*nc);
	    st2 = sqrt(st2 / float(nr * nc) - st * st);
	    for ( int j = 0 ; j < nr ; j ++ ) for ( int i = 0 ; i < nc ; i ++ ){
		maps[(nmap + ntotRefMap) * refMapSize + i * numPixels + j] = maps[(nmap + ntotRefMap) * refMapSize + i * numPixels + j] / st2 - st/st2;
		//cout <<"MAP:: " << i << " " << j << " " <<  maps[(nmap + ntotRefMap) * refMapSize + i * numPixels + j]  << "\n";
	      }
	  }
543
	}
544
545
546
547
      ntotRefMap += ns ;
      //  cout << ntotRefMap << "\n";
    }
  fclose (fin);
David Rohr's avatar
David Rohr committed
548

549
  return(0);
David Rohr's avatar
David Rohr committed
550
551
552
}

int bioem_RefMap::read_float(float *currfloat, FILE *fin, int swap) {
553
554
555
556
557
558
559
560
561
562
563
564
565
  unsigned char *cptr, tmp;

  if (fread(currfloat,4,1,fin)!=1) return 0;
  if (swap == 1) {
    cptr = (unsigned char *)currfloat;
    tmp = cptr[0];
    cptr[0]=cptr[3];
    cptr[3]=tmp;
    tmp = cptr[1];
    cptr[1]=cptr[2];
    cptr[2]=tmp;
  }
  return 1;
David Rohr's avatar
David Rohr committed
566
567
568
}

int  bioem_RefMap::read_int(int *currlong, FILE *fin, int swap) {
569
570
571
572
573
574
575
576
577
578
579
580
581
  unsigned char *cptr, tmp;

  if (fread(currlong,4,1,fin)!=1) return 0;
  if (swap == 1) {
    cptr = (unsigned char *)currlong;
    tmp = cptr[0];
    cptr[0]=cptr[3];
    cptr[3]=tmp;
    tmp = cptr[1];
    cptr[1]=cptr[2];
    cptr[2]=tmp;
  }
  return 1;
David Rohr's avatar
David Rohr committed
582
583
}
int  bioem_RefMap::read_float_empty (FILE *fin) {
584
  float currfloat;
David Rohr's avatar
David Rohr committed
585

586
587
  if (fread(&currfloat,4,1,fin)!=1) return 0;
  return 1;
David Rohr's avatar
David Rohr committed
588
589
590
}

int  bioem_RefMap::read_char_float (float *currfloat, FILE *fin) {
591
  char currchar;
David Rohr's avatar
David Rohr committed
592

593
594
595
  if (fread(&currchar,1,1,fin)!=1) return 0;
  *currfloat=(float)currchar;
  return 1;
David Rohr's avatar
David Rohr committed
596
597
598
}

int  bioem_RefMap::test_mrc (const char *vol_file, int swap) {
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
  FILE *fin;
  int nc, nr, ns, mx, my, mz;
  int mode, ncstart, nrstart, nsstart;
  float xlen, ylen, zlen;
  int i, header_ok = 1, n_range_viols = 0;
  int mapc, mapr, maps_local;
  float alpha, beta, gamma;
  float dmin, dmax, dmean, dummy, xorigin, yorigin, zorigin;

  fin = fopen(vol_file, "rb");
  if( fin == NULL ) {
    cout << "ERROR opening MRC: " << vol_file;
    exit(1);
  }

  //* read header info
  header_ok *= read_int(&nc,fin,swap);
  header_ok *= read_int(&nr,fin,swap);
  header_ok *= read_int(&ns,fin,swap);
  header_ok *= read_int(&mode,fin,swap);
  header_ok *= read_int(&ncstart,fin,swap);
  header_ok *= read_int(&nrstart,fin,swap);
  header_ok *= read_int(&nsstart,fin,swap);
  header_ok *= read_int(&mx,fin,swap);
  header_ok *= read_int(&my,fin,swap);
  header_ok *= read_int(&mz,fin,swap);
  header_ok *= read_float(&xlen,fin,swap);
  header_ok *= read_float(&ylen,fin,swap);
  header_ok *= read_float(&zlen,fin,swap);
  header_ok *= read_float(&alpha,fin,swap);
  header_ok *= read_float(&beta,fin,swap);
  header_ok *= read_float(&gamma,fin,swap);
  header_ok *= read_int(&mapc,fin,swap);
  header_ok *= read_int(&mapr,fin,swap);
  header_ok *= read_int(&maps_local,fin,swap);
  header_ok *= read_float(&dmin,fin,swap);
  header_ok *= read_float(&dmax,fin,swap);
  header_ok *= read_float(&dmean,fin,swap);
  for (i=23; i<50; ++i) header_ok *= read_float(&dummy,fin,swap);
  header_ok *= read_float(&xorigin,fin,swap);
  header_ok *= read_float(&yorigin,fin,swap);
  header_ok *= read_float(&zorigin,fin,swap);
  fclose (fin);
  if (header_ok == 0) {
    cout << "ERROR reading MRC header: " << vol_file;
    exit(1);
  }

  n_range_viols += (nc>5000); n_range_viols += (nc<0);
  n_range_viols += (nr>5000); n_range_viols += (nr<0);
  n_range_viols += (ns>5000); n_range_viols += (ns<0);
  n_range_viols += (ncstart>5000); n_range_viols += (ncstart<-5000);
  n_range_viols += (nrstart>5000); n_range_viols += (nrstart<-5000);
  n_range_viols += (nsstart>5000); n_range_viols += (nsstart<-5000);
  n_range_viols += (mx>5000); n_range_viols += (mx<0);
  n_range_viols += (my>5000); n_range_viols += (my<0);
  n_range_viols += (mz>5000); n_range_viols += (mz<0);
  n_range_viols += (alpha>360.0f); n_range_viols += (alpha<-360.0f);
  n_range_viols += (beta>360.0f); n_range_viols += (beta<-360.0f);
  n_range_viols += (gamma>360.0f); n_range_viols += (gamma<-360.0f);

  return n_range_viols;
David Rohr's avatar
David Rohr committed
661
}