Bsoft  2.1.0
Bernard's software package
molecule_to_map.cpp
Go to the documentation of this file.
1 
9 #include "molecule_to_map.h"
10 #include "Complex.h"
11 #include "UnitCell.h"
12 #include "linked_list.h"
13 #include "utilities.h"
14 #include <fstream>
15 
16 // Declaration of global variables
17 extern int verbose; // Level of output to the screen
18 
19 // Constants
20 #define MAXSID 1024 // Maximum grid sidelength
21 #define MAXSCAT 100 // Maximum number of atomic scattering data points
22 #define MAXRAD 100 // Maximum radial points at 0.1 A/point for calculating atomic potential curves
23 
24 // Internal function prototypes
25 int mol_to_image(Bmolgroup* molgroup, Bimage* p, Batomtype* atompar,
26  double resolution, double Bfactor, int wrap, int gextype);
27 int mol_to_structure_factors(Bmolgroup* molgroup, Bimage* p,
28  Batomtype* atompar, double resolution, int wrap, double Bfactor);
29 
54  Vector3<long> size, Vector3<double> sampling, double resolution, double Bfactor,
55  int wrap, int gextype, int spacegroup, UnitCell unit_cell)
56 {
57  Batomtype* atompar = NULL;
58  Bstring paramfile;
59  if ( gextype) atompar = get_atom_properties(paramfile);
60 
61  if ( sampling.volume() < 1e-6 )
62  sampling[0] = sampling[1] = sampling[2] = 1;
63  else
64  sampling = sampling.max(0.01);
65 
66  if ( resolution < sampling[0] ) resolution = sampling[0];
67  if ( resolution < 0.01 ) resolution = 2*sampling[0];
68 
69  if ( size[0] < 1 ) size[0] = (long) ((molgroup->max[0] + 3*resolution)/sampling[0] + origin[0]);
70  if ( size[1] < 1 ) size[1] = (long) ((molgroup->max[1] + 3*resolution)/sampling[1] + origin[1]);
71  if ( size[2] < 1 ) size[2] = (long) ((molgroup->max[2] + 3*resolution)/sampling[2] + origin[2]);
72  size = size.min(MAXSID);
73  size = size.max(1);
74 
75  if ( verbose & VERB_DEBUG )
76  cout << "DEBUG img_sf_from_molecule: size = " << size << endl;
77 
78  Bimage* p = new Bimage(Float, TSimple, size, 1);
79  p->unit_cell(unit_cell);
80  p->origin(origin);
81  p->sampling(sampling);
82  p->space_group(spacegroup);
83 
84  int side = (long) floor((3.0/2.0)*resolution/p->sampling(0)[0]+1);
85  if ( side > MAXSID ) side = MAXSID;
86 
87  if ( verbose & VERB_PROCESS ) {
88  if ( gextype ) cout << "Calculating atomic potential:" << endl;
89  else cout << "Calculating density by simple gaussian expansion:" << endl;
90  cout << "Dimensions: " << p->size() << " voxels" << endl;
91  cout << "Origin: " << p->image->origin() << endl;
92  cout << "Sampling: " << p->sampling(0) << " A/voxel" << endl;
93  if ( !gextype )
94  cout << "Resolution: " << resolution << " A" << endl;
95  cout << "Global B-factor: " << Bfactor << " A2" << endl;
96  cout << "Wrap: " << wrap << endl;
97  } else if ( verbose & VERB_LABEL )
98  cout << "Calculating density" << endl << endl;
99 
100  mol_to_image(molgroup, p, atompar, resolution, Bfactor, wrap, gextype);
101 
102  if ( gextype) kill_list((char *) atompar, sizeof(Batomtype));
103 
104  p->statistics();
105 
106  return p;
107 }
108 
109 /*
110 @brief Calculates a 3D density map from a set of atomic coordinates.
111 @param *molgroup set of molecules with atomic coordinates.
112 @param *p image structure for the new map.
113 @param *atompar atom type parameters.
114 @param resolution resolution (angstrom).
115 @param Bfactor global B-factor to use, if 0, use individual atom B-factors
116 @param wrap wrapping flag.
117 @param gextype type of gaussian used: 0 = single, 1 = atomic potential
118 @return int 0.
119 
120  A 3D map is calculated from atomic coordinates by placing a gaussian
121  sphere at each set of atomic coordinates. The resolution is set as
122  twice the sigma coefficient of the gaussian function. The amplitude
123  of the gaussian function is set so that the total density calculated
124  equals the atomic mass. The resultant map therefore has the density
125  units of Dalton/voxel.
126  The content and statistics of the new image is not checked.
127 
128 **/
129 int mol_to_image(Bmolgroup* molgroup, Bimage* p, Batomtype* atompar,
130  double resolution, double Bfactor, int wrap, int gextype)
131 {
132  long h, i, j, irad, nsig, t;
133  long x, y, z, ix, iy, iz;
134  Vector3<int> lo, hi;
135  Vector3<double> cgrid;
136  double sigma, sigma2, avgsig, dx, dy, dz, dist2;
137  double sigma2_global = resolution*resolution/4 + Bfactor/(8*M_PI*M_PI);
138  double interval = p->sizeX()*p->sampling(0)[0]*0.5/MAXRAD; // Sampling for radial potential curve calculations
139  if ( interval > 0.1 ) interval = 0.1;
140  double sam2 = p->sampling(0)[0]*p->sampling(0)[0];
141  double voxel_size = p->sampling(0).volume();
142  double thisamp, thisdens, thisatom, summass(0), totmass(0);
143  long side = (long) floor((3.0/2.0)*resolution/p->sampling(0)[0]+1);
144  if ( side > MAXSID ) side = MAXSID;
145  if ( gextype ) {
146  side = (long) (MAXRAD*interval/p->sampling(0)[0] - 1);
147  if ( side*p->sampling(0)[0] > 5 ) side = (long) (5/p->sampling(0)[0] + 1);
148  }
149  if ( side > p->sizeX() ) side = p->sizeX();
150  double radius2 = side*side*sam2;
151 
152  if ( verbose & VERB_PROCESS )
153  cout << "Cube edge length: " << 2*side+1 << " voxels" << endl;
154 
155  Bmolecule* mol;
156  Bresidue* res;
157  Batom* atom;
158 
159  double* apot = NULL;
160  if ( atompar ) apot = get_potential_curves(atompar, interval);
161 
162  nsig = 0;
163  avgsig = 0;
164  for ( h=0, mol = molgroup->mol; mol; mol = mol->next ) if ( mol->sel ) {
165  h++;
166  for( res = mol->res; res; res = res->next ) {
167  for ( atom = res->atom; atom; atom = atom->next ) {
168  cgrid[0] = atom->coord[0]/p->sampling(0)[0] + p->image->origin()[0];
169  cgrid[1] = atom->coord[1]/p->sampling(0)[1] + p->image->origin()[1];
170  cgrid[2] = atom->coord[2]/p->sampling(0)[2] + p->image->origin()[2];
171  lo[0] = (long) (cgrid[0] + 0.5 - side);
172  hi[0] = (long) (cgrid[0] + 0.5 + side);
173  lo[1] = (long) (cgrid[1] + 0.5 - side);
174  hi[1] = (long) (cgrid[1] + 0.5 + side);
175  lo[2] = (long) (cgrid[2] + 0.5 - side);
176  hi[2] = (long) (cgrid[2] + 0.5 + side);
177  if ( !wrap ) {
178  if ( lo[0] < 0 ) lo[0] = 0;
179  if ( lo[1] < 0 ) lo[1] = 0;
180  if ( lo[2] < 0 ) lo[2] = 0;
181  if ( hi[0] > (long)p->sizeX() - 1 ) hi[0] = p->sizeX() - 1;
182  if ( hi[1] > (long)p->sizeY() - 1 ) hi[1] = p->sizeY() - 1;
183  if ( hi[2] > (long)p->sizeZ() - 1 ) hi[2] = p->sizeZ() - 1;
184  }
185  if ( Bfactor > 0 ) sigma2 = sigma2_global;
186  else sigma2 = sigma2_global + atom->b/(8*M_PI*M_PI); // B-factors
187  if ( sigma2 < 0.01 ) sigma2 = 0.01;
188  sigma = sqrt(sigma2);
189  nsig++;
190  avgsig += sigma;
191  if ( verbose & VERB_DEBUG )
192  cout << atom->coord << " " << atom->type << " " << atom->mass << endl;
193  thisatom = 0;
194  thisamp = atom->mass*voxel_size/(sqrt(TWOPI*sigma2)*TWOPI*sigma2);
195  sigma2 = -1.0/(2.0*sigma2);
196  ix = (long) (cgrid[0] + 0.5);
197  iy = (long) (cgrid[1] + 0.5);
198  iz = (long) (cgrid[2] + 0.5);
199  i = p->index(0, ix, iy, iz, 0);
200  for ( z=lo[2]; z<=hi[2]; z++ ) {
201  dz = (cgrid[2] - z)*(cgrid[2] - z)*sam2;
202  for ( y=lo[1]; y<=hi[1]; y++ ) {
203  dy = (cgrid[1] - y)*(cgrid[1] - y)*sam2;
204  for ( x=lo[0]; x<=hi[0]; x++ ) {
205  dx = (cgrid[0] - x)*(cgrid[0] - x)*sam2;
206  dist2 = dx + dy + dz;
207  if ( dist2 <= radius2 ) {
208  ix = x;
209  iy = y;
210  iz = z;
211  if ( wrap ) {
212  while ( ix < 0 ) ix += p->sizeX(); /* This wraps the density */
213  while ( iy < 0 ) iy += p->sizeY();
214  while ( iz < 0 ) iz += p->sizeZ();
215  while ( ix >= p->sizeX() ) ix -= p->sizeX();
216  while ( iy >= p->sizeY() ) iy -= p->sizeY();
217  while ( iz >= p->sizeZ() ) iz -= p->sizeZ();
218  }
219  j = p->index(ix, iy, iz);
220  thisdens = 0;
221  if ( gextype ) {
222  t = atom->tnum;
223  if ( j == i ) {
224  thisdens = apot[t*MAXRAD];
225  } else {
226  irad = (long) (sqrt(dist2)/interval + 0.5);
227  if ( irad < MAXRAD ) thisdens = apot[t*MAXRAD+irad];
228  }
229  } else {
230  thisdens = thisamp*exp(dist2*sigma2);
231  }
232  p->add(j, thisdens);
233  thisatom += thisdens;
234  }
235  }
236  }
237  }
238  totmass += thisatom;
239  summass += atom->mass;
240  }
241  }
242  if ( verbose & VERB_TIME )
243  cout << "Molecules done: " << h+1 << "\r" << flush;
244  }
245 
246  double scale = POTPREFAC;
247  if ( p->sizeX() > 1 ) scale *= p->sampling(0)[0];
248  if ( p->sizeY() > 1 ) scale *= p->sampling(0)[1];
249  if ( p->sizeZ() > 1 ) scale *= p->sampling(0)[2];
250 
251  if ( gextype ) {
252  p->rescale(scale, 0);
253  totmass *= scale;
254  }
255 
256  if ( verbose & VERB_PROCESS ) {
257  cout << "Average sigma: " << avgsig/nsig << " A" << endl;
258  cout << "Total mass (sum): " << summass << " Da" << endl;
259  cout << "Total mass (density): " << totmass << " Da" << endl;
260  cout << " Ratio: " << totmass/summass << endl;
261  cout << "Total volume: " << totmass/RHO << " A3" << endl << endl;
262  }
263 
264  if ( apot ) delete[] apot;
265 
266  return 0;
267 }
268 
277 int compare_mol_map(Bmolgroup* molgroup, Bimage* pcalc, Bimage* p)
278 {
279  Bmolecule* mol;
280  Bresidue* res;
281  Batom* atom;
282 
283  long j, natom(0);
284  long x, y, z;
285  double min = 1e30, max = -1e30, Qavg(0), Qsd(0);
286  double scale = (pcalc->maximum() - pcalc->minimum())/(p->maximum() - p->minimum());
287 
288  for ( mol = molgroup->mol; mol; mol = mol->next ) {
289  for( res = mol->res; res; res = res->next ) {
290  for ( atom = res->atom; atom; atom = atom->next ) {
291  x = (long) floor(atom->coord[0]/pcalc->sampling(0)[0] + pcalc->image->origin()[0] + 0.5);
292  y = (long) floor(atom->coord[1]/pcalc->sampling(0)[1] + pcalc->image->origin()[1] + 0.5);
293  z = (long) floor(atom->coord[2]/pcalc->sampling(0)[2] + pcalc->image->origin()[2] + 0.5);
294  while ( x < 0 ) x += pcalc->sizeX();
295  while ( x >= pcalc->sizeX() ) x -= pcalc->sizeX();
296  while ( y < 0 ) y += pcalc->sizeY();
297  while ( y >= pcalc->sizeY() ) y -= pcalc->sizeY();
298  while ( z < 0 ) z += pcalc->sizeZ();
299  while ( z >= pcalc->sizeZ() ) z -= pcalc->sizeZ();
300  j = pcalc->index(x, y, z);
301  if ( (*pcalc)[j] ) atom->q = scale*(*p)[j]/(*pcalc)[j];
302  if ( atom->q > 1 ) atom->q = 1;
303  if ( min > atom->q ) min = atom->q;
304  if ( max < atom->q ) max = atom->q;
305  Qavg += atom->q;
306  Qsd += atom->q*atom->q;
307  atom->b = atom->q;
308  natom++;
309  }
310  }
311  }
312  Qavg /= natom;
313  Qsd = sqrt(Qsd/natom - Qavg*Qavg);
314 
315  if ( verbose & VERB_PROCESS ) {
316  cout << "Comparison of molecule " << mol->id << " with map " << p->file_name() << endl;
317  cout << "Origin (original): " << p->image->origin() << endl;
318  cout << "Origin (calculated): " << pcalc->image->origin() << endl;
319  cout << "Scale: " << scale << endl;
320  cout << "Min & max: " << min << " " << max << endl;
321  cout << "Average and standard deviation: " << Qavg << " " << Qsd << endl << endl;
322  } else {
323  cout << "Comparison of molecule " << mol->id << " with map " << p->file_name() << endl;
324  cout << "Average and standard deviation: " << Qavg << " " << Qsd << endl << endl;
325  }
326 
327  return 0;
328 }
329 
330 
331 
358  Vector3<double> origin, Vector3<long> size, Vector3<double> sampling, double resolution,
359  int spacegroup, UnitCell unit_cell, int wrap, double Bfactor, Bstring& paramfile)
360 {
361  Batomtype* atompar = get_atom_properties(paramfile);
362 
363  if ( sampling.volume() < 1e-6 )
364  sampling[0] = sampling[1] = sampling[2] = 1;
365  else
366  sampling = sampling.max(0.01);
367 
368  if ( resolution < sampling[0] ) resolution = sampling[0];
369  if ( resolution < 0.01 ) resolution = 2*sampling[0];
370 
371  if ( size[0] < 1 ) size[0] = (long) ((molgroup->max[0] - molgroup->min[0])/sampling[0]);
372  if ( size[1] < 1 ) size[1] = (long) ((molgroup->max[1] - molgroup->min[1])/sampling[1]);
373  if ( size[2] < 1 ) size[2] = (long) ((molgroup->max[2] - molgroup->min[2])/sampling[2]);
374  size = size.min(MAXSID);
375  size = size.max(1);
376 
377  if ( verbose & VERB_DEBUG )
378  cout << "DEBUG img_sf_from_molecule: size = " << size << endl;
379 
380  Bimage* p = new Bimage(Float, TComplex, size, 1);
381  p->unit_cell(unit_cell);
382  p->origin(origin);
383  p->sampling(sampling);
385  p->space_group(spacegroup);
386 
387  double smax = 2.0/p->real_size().length();
388  if ( resolution > 0.01 ) smax = 1.0/resolution;
389 
390  if ( ( verbose & VERB_LABEL ) || ( verbose & VERB_PROCESS ) )
391  cout << "Calculating structure factors from atomic coordinates" << endl;
392 
393  if ( verbose & VERB_PROCESS ) {
394  cout << "Size: " << p->size() << " voxels" << endl;
395  cout << "Origin: " << p->image->origin() << endl;
396  cout << "Sampling: " << p->sampling(0) << " A/voxel" << endl;
397  cout << "Resolution: " << resolution << " A" << endl;
398  cout << "Reciprocal resolution: " << smax << " /A" << endl;
399  }
400 
401  mol_to_structure_factors(molgroup, p, atompar, resolution, wrap, Bfactor);
402 
403  atom_type_kill(atompar);
404 
405  p->statistics();
406 
407  return p;
408 }
409 
410 double one_sf(Bmolgroup* molgroup, Bimage* p, long i, double s, double scale, double* scat)
411 {
412  if ( s <= 0 ) return 0;
413 
414  long x, y, z;
415  long h, k, l, t, si;
416  double fraction, fraction1, f, phi;
417  Bmolecule* mol;
418  Bresidue* res;
419  Batom* atom;
420  Complex<float> temp;
421 
422  p->coordinates(i, x, y, z);
423  if ( x < (p->sizeX() + 1)/2 ) h = x; else h = (long)x - p->sizeX();
424  if ( y < (p->sizeY() + 1)/2 ) k = y; else k = (long)y - p->sizeY();
425  if ( z < (p->sizeZ() + 1)/2 ) l = z; else l = (long)z - p->sizeZ();
426 
427  si = (long) s;
428  fraction = s - si;
429  fraction1 = 1 - fraction;
430 
431  for ( mol = molgroup->mol; mol; mol = mol->next ) {
432  for( res = mol->res; res; res = res->next ) {
433  for ( atom = res->atom; atom; atom = atom->next )
434  if ( atom->sel ) {
435  t = atom->tnum*MAXSCAT + si;
436  f = atom->q*(fraction1*scat[t] + fraction*scat[t+1]);
437  phi = MIN2PI*(h*atom->vel[0] +
438  k*atom->vel[1] + l*atom->vel[2]);
439  temp = Complex<float>(cos(phi), sin(phi));
440  p->set(i, p->complex(i) + f*temp);
441  }
442  }
443  }
444 
445  p->set(i, p->complex(i) * scale);
446 
447  if ( verbose & VERB_DEBUG )
448  cout << "DEBUG mol_to_structure_factors: " << x << " " << y << " " << z << " " <<
449  (p->complex(i)).real() << " " << (p->complex(i)).imag() << endl;
450 
451  return 0;
452 }
453 
454 /*
455 @brief Calculates a 3D set of structure factors from a set of atomic coordinates.
456 @param *molgroup set of molecules with atomic coordinates.
457 @param *p image structure for the new structure factors.
458 @param *atompar atom type parameters.
459 @param resolution resolution (angstrom).
460 @param wrap 0=cut atoms outside box, 1=wrap coordinates within unit cell.
461 @param Bfactor constant for decay curve.
462 @return int 0.
463 
464  All structure factors within a given resolution are calculated from
465  all the selected atomic coordinates. The coordinates are fractionalized
466  to fit into the given size box. If the size of the box is not given,
467  it defaults to:
468  x_size = (max(x_coor) - min(x_coor)) / x_sampling
469  The atomic scattering profiles are read from the STAR database as the
470  amplitudes and B-factors of reciprocal space gaussians. For each profile,
471  a lookup table is calculated to speed up further calculations.
472  The content and statistics of the new image is not checked.
473 
474 **/
476  Batomtype* atompar, double resolution, int wrap, double Bfactor)
477 {
478  long nscat;
479  long i, j, h, k, l, x, y, z, yi, zi, n, t, nmax;
480  long nmol(0), natom(0);
481  double s, smax, sx, sy, sz;
482  Bmolecule* mol;
483  Bresidue* res;
484  Batom* atom;
485  Batomtype* at;
486 
487  // Set the resolution limit
488  smax = 1;
489  if ( resolution ) smax = 1.0/resolution;
490  resolution = 1.0/smax;
491 
492  double recip_interval = 1.1*smax/MAXSCAT;
493  double inv_recip_int = 1/recip_interval;
494  Vector3<double> ori = p->sampling(0)*p->image->origin();
495  Vector3<double> fcoor;
496 
497  // Convert to fractional coordinates
498  UnitCell unit_cell = p->unit_cell();
499  unit_cell.a(p->real_size()[0]);
500  unit_cell.b(p->real_size()[1]);
501  unit_cell.c(p->real_size()[2]);
502  p->unit_cell(unit_cell);
503  Matrix3 frac_mat = unit_cell.skew_matrix();
504 
505  double* scat = get_scattering_curves(atompar, Bfactor, recip_interval, nscat);
506 
507  for ( nmol = 0, mol = molgroup->mol; mol; mol = mol->next ) if ( mol->sel ) {
508  nmol++;
509  for( res = mol->res; res; res = res->next ) {
510  for ( atom = res->atom; atom; atom = atom->next, natom++ ) {
511  atom->tnum = -1;
512  for ( t=0, at = atompar; at && atom->tnum < 0; at = at->next, t++ )
513  if ( strncmp(atom->el, at->el, 2) == 0 )
514  atom->tnum = t;
515  if ( atom->tnum < 0 ) atom->sel = 0;
516  if ( verbose & VERB_DEBUG )
517  cout << "DEBUG mol_to_structure_factors: Atom = " << atom->el << endl;
518  fcoor = atom->coord + ori;
519  atom->vel = frac_mat * fcoor;
520  if ( wrap == 1 ) {
521  while ( atom->vel[0] < 0 ) atom->vel[0] += 1;
522  while ( atom->vel[1] < 0 ) atom->vel[1] += 1;
523  while ( atom->vel[2] < 0 ) atom->vel[2] += 1;
524  while ( atom->vel[0] > 1 ) atom->vel[0] -= 1;
525  while ( atom->vel[1] > 1 ) atom->vel[1] -= 1;
526  while ( atom->vel[2] > 1 ) atom->vel[2] -= 1;
527  } else if ( wrap > 1 ) {
528  if ( atom->vel[0] < 0 ) atom->sel = 0;
529  if ( atom->vel[1] < 0 ) atom->sel = 0;
530  if ( atom->vel[0] > 1 ) atom->sel = 0;
531  if ( atom->vel[1] > 1 ) atom->sel = 0;
532  while ( atom->vel[2] < 0 ) atom->vel[2] += 1;
533  while ( atom->vel[2] > 1 ) atom->vel[2] -= 1;
534  } else {
535  if ( atom->vel[0] < 0 ) atom->sel = 0;
536  if ( atom->vel[1] < 0 ) atom->sel = 0;
537  if ( atom->vel[2] < 0 ) atom->sel = 0;
538  if ( atom->vel[0] > 1 ) atom->sel = 0;
539  if ( atom->vel[1] > 1 ) atom->sel = 0;
540  if ( atom->vel[2] > 1 ) atom->sel = 0;
541  }
542  if ( verbose & VERB_DEBUG )
543  cout << "DEBUG mol_to_structure_factors: "
544  << atom->el << " " << atom->vel << endl;
545  }
546  }
547  }
548 
549  if ( verbose & VERB_PROCESS ) {
550  cout << "Molecules and atoms: " << nmol << " " << natom << endl;
551  cout << "Unit cell: " <<
552  unit_cell.a() << " " << unit_cell.b() << " " << unit_cell.c() << " " <<
553  unit_cell.alpha()*180/M_PI << " " << unit_cell.beta()*180/M_PI << " " << unit_cell.gamma()*180/M_PI << endl;
554  cout << "Fractionalization matrix: " << endl << frac_mat << endl;
555  }
556 
557  // Calculate the structure factors
558  // Default space group = 1
559  // Estimate the number of structure factors from the spherical volume or circular area
560  n = 0;
561  nmax = (long) ((4.0*M_PI/3.0)*smax*smax*smax/(frac_mat[0][0]*frac_mat[1][1]*frac_mat[2][2]));
562  if ( p->sizeZ() < 2 )
563  nmax = (long) (M_PI*smax*smax/(frac_mat[0][0]*frac_mat[1][1]));
564  // Factor required to calculate the estimated completion from the number of
565  // structure factors already calculated
566 // pfac = 100.0/(nmax/2 + M_PI*smax*smax/(2*frac_mat[1][1]*frac_mat[2][2]));
567 // if ( p->sizeZ() < 2 )
568 // pfac = 100.0/(nmax/2 + smax/frac_mat[1][1]);
569  if ( verbose & VERB_PROCESS )
570  cout << "Reflections estimated: " << nmax << endl;
571 
572  // Scaled by the potential prefactor K divided by the volume
573  double scale = POTPREFAC/p->size().volume();
574  if ( p->sizeX() > 1 ) scale /= p->sampling(0)[0];
575  if ( p->sizeY() > 1 ) scale /= p->sampling(0)[1];
576  if ( p->sizeZ() > 1 ) scale /= p->sampling(0)[2];
577 
578  if ( verbose & VERB_DEBUG )
579  cout << "DEBUG mol_to_structure_factors: scale = " << scale << endl;
580 
581  long imgsize = (long) p->size().volume();
582  long hx = p->sizeX()/2, hy = p->sizeY()/2, hz = p->sizeZ()/2;
583  long hx1 = (p->sizeX()+1)/2, hy1 = (p->sizeY()+1)/2, hz1 = (p->sizeZ()+1)/2;
584  Complex<float> temp;
585 
586  p->next = new Bimage(Float, TSimple, p->size(), p->images());
587  float* fom = (float *) p->next->data_pointer();
588 
589  // Set up FOM block with s values
590  for ( l=-hz; l<hz1; l++ ) {
591  z = ( l < 0 ) ? l + p->sizeZ() : l;
592  sz = l*frac_mat[2][2];
593  sz *= sz;
594  for ( k=-hy; k<hy1; k++ ) {
595  y = ( k < 0 ) ? k + p->sizeY() : k;
596  sy = k*frac_mat[1][1];
597  sy *= sy;
598  for ( h=-hx; h<=0; h++ ) {
599  x = ( h < 0 ) ? h + p->sizeX() : h;
600  sx = h*frac_mat[0][0];
601  sx *= sx;
602  s = sqrt(sx + sy + sz);
603  if ( s < smax ) {
604  i = p->index(x, y, z);
605  s *= inv_recip_int;
606  fom[i] = s;
607  n++;
608  }
609  }
610  }
611  }
612 
613  fom[0] = 1e-10; // Make F0 non-zero
614 
615 #ifdef HAVE_GCD
616  dispatch_apply(imgsize, dispatch_get_global_queue(0, 0), ^(size_t i){
617  if ( fom[i] ) one_sf(molgroup, p, i, fom[i], scale, scat);
618  });
619 #else
620 #pragma omp parallel for
621  for ( i=0; i<imgsize; i++ ) if ( fom[i] )
622  one_sf(molgroup, p, i, fom[i], scale, scat);
623 #endif
624 
625 // cout << "first half done" << endl;
626 
627  // Fill in the second half from the first
628  for ( l=-hz; l<hz1; l++ ) {
629  z = ( l < 0 ) ? l + p->sizeZ() : l;
630  zi = ( z == 0 ) ? 0 : p->sizeZ() - z;
631  sz = l*frac_mat[2][2];
632  sz *= sz;
633  for ( k=-hy; k<hy1; k++ ) {
634  y = ( k < 0 ) ? k + p->sizeY() : k;
635  yi = ( y == 0 ) ? 0 : p->sizeY() - y;
636  sy = k*frac_mat[1][1];
637  sy *= sy;
638  for ( h=1; h<hx1; h++ ) {
639  sx = h*frac_mat[0][0];
640  sx *= sx;
641  s = sqrt(sx + sy + sz);
642  if ( s < smax ) {
643  i = p->index(p->sizeX() - h, yi, zi);
644  j = p->index(h, y, z);
645  p->set(j, (p->complex(i)).conj());
646  n++;
647  }
648  }
649  }
650  }
651 
652  if ( verbose & VERB_PROCESS )
653  cout << "Reflections calculated: " << n << endl << endl;
654 
655  delete[] scat;
656 
657  return 0;
658 }
659 
660 double* get_potential_curves(Batomtype* atompar, double interval)
661 {
662  long i, j, n, t;
663  double interval2 = -4*M_PI*M_PI*interval*interval, prefac, expfac;
664  Batomtype* at;
665 
666  // Get the atomic properties
667  for ( n=0, at = atompar; at; at = at->next, n++ ) ;
668 
669  // Calculate the atomic scattering profiles
670  if ( verbose & VERB_DEBUG )
671  cout << "DEBUG get_potential_curves: n=" << n << " interval=" << interval << endl;
672 
673  double* apot = new double[MAXRAD*n];
674  for ( i=0; i<MAXRAD*n; i++ ) apot[i] = 0;
675 
676  for ( t=0, at = atompar; at; at = at->next, t++ ) {
677  apot[t*MAXRAD] = at->sfc;
678  for ( j=0; j<5; j++ ) {
679  prefac = M_PI*sqrt(M_PI)*at->sfa[j]/(at->sfb[j]*sqrt(at->sfb[j]));
680  expfac = interval2/at->sfb[j];
681  for ( i=0; i<MAXRAD; i++ )
682  apot[t*MAXRAD+i] += prefac*exp(expfac*i*i);
683  }
684  }
685 
686  if ( verbose & VERB_FULL ) {
687  cout << "Scattering coefficients:" << endl;
688  for ( at = atompar; at; at = at->next )
689  cout << tab << at->el << "(" << at->z << ")";
690  cout << endl;
691  for ( j=0; j<5; j++ ) {
692  cout << "a" << j+1;
693  for ( at = atompar; at; at = at->next )
694  cout << tab << at->sfa[j];
695  cout << endl;
696  }
697  for ( j=0; j<5; j++ ) {
698  cout << "b" << j+1;
699  for ( at = atompar; at; at = at->next )
700  cout << tab << at->sfb[j];
701  cout << endl;
702  }
703  cout << "c";
704  for ( at = atompar; at; at = at->next )
705  cout << tab << at->sfc;
706  cout << endl << "Atomic radial potential curves:" << endl << "s";
707  for ( at = atompar; at; at = at->next )
708  cout << tab << at->el << "(" << at->z << ")";
709  cout << endl;
710  for ( i=0; i<MAXRAD; i++ ) {
711  cout << i*interval;
712  for ( t=0, at = atompar; at; at = at->next, t++ )
713  cout << tab << apot[t*MAXRAD+i];
714  cout << endl;
715  }
716  cout << endl;
717  }
718 
719  return apot;
720 }
721 
722 double* get_scattering_curves(Batomtype* atompar, double Bfactor,
723  double recip_interval, long& nscat)
724 {
725  long i, j, n, t;
726  double s2, fact = -Bfactor/4;
727  Batomtype* at;
728 
729  if ( recip_interval < 1e-10 ) {
730  cerr << "Error: Reciprocal space interval not defined!" << endl;
731  return NULL;
732  }
733 
734  double recip_int2 = recip_interval*recip_interval/4;
735 
736  // Get the atomic properties
737  for ( n=0, at = atompar; at; at = at->next, n++ ) ;
738 
739  // Calculate the atomic scattering profiles
740  if ( verbose & VERB_DEBUG )
741  cout << "DEBUG get_scattering_curves: nscat = " << n << endl;
742 
743  double* scat = new double[MAXSCAT*n];
744  for ( i=0; i<MAXSCAT*n; i++ ) scat[i] = 0;
745 
746  for ( i=0; i<MAXSCAT; i++ ) {
747  s2 = i*i*recip_int2;
748  for ( t=0, at = atompar; at; at = at->next, t++ ) {
749  scat[t*MAXSCAT+i] = at->sfc;
750  for ( j=0; j<5; j++ )
751  scat[t*MAXSCAT+i] += at->sfa[j]*exp(-at->sfb[j]*s2);
752  scat[t*MAXSCAT+i] *= exp(fact*s2);
753  }
754  }
755 
756  if ( verbose & VERB_FULL ) {
757  cout << "Scattering coefficients:" << endl;
758  for ( at = atompar; at; at = at->next )
759  cout << tab << at->el << "(" << at->z << ")";
760  cout << endl;
761  for ( j=0; j<5; j++ ) {
762  cout << "a" << j+1;
763  for ( at = atompar; at; at = at->next )
764  cout << tab << at->sfa[j];
765  cout << endl;
766  }
767  for ( j=0; j<5; j++ ) {
768  cout << "b" << j+1;
769  for ( at = atompar; at; at = at->next )
770  cout << tab << at->sfb[j];
771  cout << endl;
772  }
773  cout << "c";
774  for ( at = atompar; at; at = at->next )
775  cout << tab << at->sfc;
776  cout << endl << "Scattering curves:" << endl << "s";
777  for ( at = atompar; at; at = at->next )
778  cout << tab << at->el << "(" << at->z << ")";
779  cout << endl;
780  for ( i=0; i<MAXSCAT; i++ ) {
781  s2 = i*i*recip_int2;
782  cout << sqrt(s2);
783  for ( t=0, at = atompar; at; at = at->next, t++ )
784  cout << tab << scat[t*MAXSCAT+i];
785  cout << endl;
786  }
787  cout << endl;
788  }
789 
790  nscat = n;
791 
792  return scat;
793 }
794 
805 int write_scattering_curves(Bstring& paramfile, Bstring& outfile, Bstring& selection)
806 {
807 
808  long i, j, nscat;
809  double s2;
810  double recip_interval = 1.0/MAXSCAT;
811  double recip_int2 = recip_interval*recip_interval/4;
812 
813  Batomtype* at;
814  Batomtype* atompar = get_atom_properties(paramfile);
815 
816  double* scat = get_scattering_curves(atompar, 0, recip_interval, nscat);
817 
818  char elsel[1024], *aptr;
819  if ( selection.length() ) strcpy(elsel, selection.c_str());
820  else for ( at = atompar, aptr = elsel; at; aptr += strlen(at->el), at = at->next )
821  strcpy(aptr, at->el);
822 
823  ofstream fd(outfile.c_str());
824  if ( fd.fail() ) return -1;
825 
826  if ( verbose ) {
827  cout << "Writing scattering curves to: " << outfile << endl;
828  cout << "Elemental selection: " << elsel << endl << endl;
829  }
830 
831  fd << "Scattering coefficients:" << endl;
832  for ( at = atompar; at; at = at->next )
833  if ( strstr(elsel, at->el) )
834  fd << tab << at->el << "(" << at->z << ")";
835  fd << endl;
836  for ( j=0; j<5; j++ ) {
837  fd << "a" << j+1;
838  for ( at = atompar; at; at = at->next )
839  if ( strstr(elsel, at->el) )
840  fd << tab << at->sfa[j];
841  fd << endl;
842  }
843  for ( j=0; j<5; j++ ) {
844  fd << "b" << j+1;
845  for ( at = atompar; at; at = at->next )
846  if ( strstr(elsel, at->el) )
847  fd << tab << at->sfb[j];
848  fd << endl;
849  }
850  fd << "c";
851  for ( at = atompar; at; at = at->next )
852  if ( strstr(elsel, at->el) )
853  fd << tab << at->sfc;
854  fd << endl;
855 
856  fd << "Scattering curves:" << endl << "s";
857  for ( at = atompar; at; at = at->next )
858  if ( strstr(elsel, at->el) )
859  fd << tab << at->el << "(" << at->z << ")";
860  fd << endl;
861  for ( i=0; i<MAXSCAT; i++ ) {
862  s2 = i*i*recip_int2;
863  fd << sqrt(s2);
864  for ( j=0, at = atompar; at; at = at->next, j++ )
865  if ( strstr(elsel, at->el) )
866  fd << tab << scat[j*MAXSCAT+i];
867  fd << endl;
868  }
869  fd << endl;
870 
871  fd.close();
872 
873  delete[] scat;
874  kill_list((char *) atompar, sizeof(Batomtype));
875 
876  return 0;
877 }
878 
Bimage::coordinates
void coordinates(long i, long &nx, long &ny, long &nz)
Definition: Bimage.h:429
img_from_molecule
Bimage * img_from_molecule(Bmolgroup *molgroup, Vector3< double > origin, Vector3< long > size, Vector3< double > sampling, double resolution, double Bfactor, int wrap, int gextype, int spacegroup, UnitCell unit_cell)
Calculates a 3D density map from a set of atomic coordinates.
Definition: molecule_to_map.cpp:53
Bimage::rescale
int rescale(double scale, double shift)
Rescales the image data with a given multiplier and offset.
Definition: Bimage_rescale.cpp:27
Bimage::real_size
Vector3< double > real_size()
Definition: Bimage.h:500
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
img_sf_from_molecule
Bimage * img_sf_from_molecule(Bmolgroup *molgroup, Vector3< double > origin, Vector3< long > size, Vector3< double > sampling, double resolution, int spacegroup, UnitCell unit_cell, int wrap, double Bfactor, Bstring &paramfile)
Calculates a 3D set of structure factors from a set of atomic coordinates.
Definition: molecule_to_map.cpp:357
RHO
#define RHO
Definition: utilities.h:95
MAXRAD
#define MAXRAD
Definition: molecule_to_map.cpp:22
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Bresidue::next
Bresidue * next
Definition: rwmolecule.h:50
Batomtype::el
char el[4]
Definition: rwatomprop.h:22
kill_list
long kill_list(char *list, unsigned long size)
Frees all the items in a linked list.
Definition: linked_list.cpp:214
Complex
Definition: Complex.h:23
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1118
Bstring
Definition: Bstring.h:29
UnitCell::c
void c(double d)
Definition: UnitCell.h:38
Bimage::next
Bimage * next
Definition: Bimage.h:179
Bresidue::atom
Batom * atom
Definition: rwmolecule.h:54
Bmolgroup::mol
Bmolecule * mol
Definition: rwmolecule.h:189
Vector3< double >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
POTPREFAC
#define POTPREFAC
Definition: molecule_to_map.h:14
Bimage::unit_cell
void unit_cell(UnitCell uc)
Definition: Bimage.h:608
Batom::tnum
int tnum
Definition: rwmolecule.h:29
Batom::el
char el[4]
Definition: rwmolecule.h:27
Complex.h
Class for complex numbers.
Batomtype
Definition: rwatomprop.h:19
TComplex
@ TComplex
Definition: Bimage.h:60
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
UnitCell.h
Header file for unit cell functions.
UnitCell::b
void b(double d)
Definition: UnitCell.h:37
Batom::q
double q
Definition: rwmolecule.h:31
get_atom_properties
Batomtype * get_atom_properties(Bstring &filename)
Reading atomic properties from parameter files.
Definition: rwatomprop.cpp:47
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
Batom::next
Batom * next
Definition: rwmolecule.h:25
mol_to_image
int mol_to_image(Bmolgroup *molgroup, Bimage *p, Batomtype *atompar, double resolution, double Bfactor, int wrap, int gextype)
Definition: molecule_to_map.cpp:129
Bmolecule::res
Bresidue * res
Definition: rwmolecule.h:122
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Standard
@ Standard
Definition: Bimage.h:42
get_potential_curves
double * get_potential_curves(Batomtype *atompar, double interval)
Definition: molecule_to_map.cpp:660
Batomtype::sfa
float sfa[5]
Definition: rwatomprop.h:29
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
TSimple
@ TSimple
Definition: Bimage.h:59
Bimage::add
void add(long j, double v)
Definition: Bimage.h:321
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
Bimage::fourier_type
FourierType fourier_type()
Definition: Bimage.h:380
Bmolecule::next
Bmolecule * next
Definition: rwmolecule.h:108
Bmolecule
Definition: rwmolecule.h:107
Bimage
General image parameter class.
Definition: Bimage.h:177
UnitCell::alpha
void alpha(double d)
Definition: UnitCell.h:39
MIN2PI
#define MIN2PI
Definition: utilities.h:75
Batom::type
char type[8]
Definition: rwmolecule.h:28
Batom::vel
Vector3< double > vel
Definition: rwmolecule.h:35
UnitCell::a
void a(double d)
Definition: UnitCell.h:36
Bimage::complex
Complex< double > complex(long j)
Returns a complex value at the given index.
Definition: Bimage.cpp:1027
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
Batomtype::z
int z
Definition: rwatomprop.h:21
one_sf
double one_sf(Bmolgroup *molgroup, Bimage *p, long i, double s, double scale, double *scat)
Definition: molecule_to_map.cpp:410
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
Batom::b
double b
Definition: rwmolecule.h:31
UnitCell::skew_matrix
Matrix3 skew_matrix()
Calculates the skew matrix from the unit cell parameters.
Definition: UnitCell.cpp:26
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
MAXSCAT
#define MAXSCAT
Definition: molecule_to_map.cpp:21
Batomtype::next
Batomtype * next
Definition: rwatomprop.h:20
Batom::mass
double mass
Definition: rwmolecule.h:32
Bstring::length
long length() const
Definition: Bstring.cpp:263
atom_type_kill
int atom_type_kill(Batomtype *atype)
Destroys an atom type.
Definition: rwatomprop.cpp:177
Batomtype::sfb
float sfb[5]
Definition: rwatomprop.h:30
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
Bmolgroup::max
Vector3< double > max
Definition: rwmolecule.h:183
MAXSID
#define MAXSID
Definition: molecule_to_map.cpp:20
verbose
int verbose
Bimage::index
long index(long nx, long ny) const
Definition: Bimage.h:396
Vector3::volume
double volume()
Definition: Vector3.h:237
Bmolgroup::min
Vector3< double > min
Definition: rwmolecule.h:182
mol_to_structure_factors
int mol_to_structure_factors(Bmolgroup *molgroup, Bimage *p, Batomtype *atompar, double resolution, int wrap, double Bfactor)
Definition: molecule_to_map.cpp:475
Batomtype::sfc
float sfc
Definition: rwatomprop.h:31
Batom::sel
int sel
Definition: rwmolecule.h:34
Bimage::space_group
long space_group()
Definition: Bimage.h:598
Bresidue
Definition: rwmolecule.h:49
linked_list.h
Header file for generalized linked list functions.
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:284
Vector3::min
Type min()
Definition: Vector3.h:169
Bimage::minimum
double minimum()
Definition: Bimage.h:541
Bmolgroup
Definition: rwmolecule.h:170
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
compare_mol_map
int compare_mol_map(Bmolgroup *molgroup, Bimage *pcalc, Bimage *p)
Compares reference and calculated maps and calculates an occupancy for every atom in the molecule set...
Definition: molecule_to_map.cpp:277
Vector3::length
double length()
Definition: Vector3.h:222
Bmolecule::sel
int sel
Definition: rwmolecule.h:120
Float
@ Float
Definition: utilities.h:161
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:110
molecule_to_map.h
Header file for functions to calculate a 3D map from atomic coordinates.
Batom::coord
Vector3< double > coord
Definition: rwmolecule.h:30
UnitCell::gamma
void gamma(double d)
Definition: UnitCell.h:41
get_scattering_curves
double * get_scattering_curves(Batomtype *atompar, double Bfactor, double recip_interval, long &nscat)
Definition: molecule_to_map.cpp:722
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
write_scattering_curves
int write_scattering_curves(Bstring &paramfile, Bstring &outfile, Bstring &selection)
Calculates and writes atomic scattering profiles to a file.
Definition: molecule_to_map.cpp:805
Batom
Definition: rwmolecule.h:24
Bmolecule::id
Bstring id
Definition: rwmolecule.h:110
TWOPI
#define TWOPI
Definition: utilities.h:72
Vector3::max
Type max()
Definition: Vector3.h:175