Bsoft  2.1.0
Bernard's software package
mol_map_energy.cpp
Go to the documentation of this file.
1 
9 #include "rwmolecule.h"
10 #include "rwimg.h"
11 #include "utilities.h"
12 
13 // Declaration of global variables
14 extern int verbose; // Level of output to the screen
15 
23 double molgroup_map_energy(Bmolgroup* molgroup, Bimage* map, double Kmap)
24 {
25  map->change_type(Float) ;
26 
27  Bmolgroup* mg;
28  Bmolecule* mol;
29  Bresidue* res;
30  Batom* atom;
31  long i, n(0), nmg, nm;
32  long x, y, z;
33  long slicesize = map->sizeX()*map->sizeY();
34  double E(0), Emg, Em;
35  double invunit = 0.5*Kmap/map->sampling(0)[0];
37 
38  for ( Emg=0, nmg=0, mg = molgroup; mg; mg = mg->next ) {
39  for ( Em=0, nm=0, mol = mg->mol; mol; mol = mol->next ) {
40  for( res = mol->res; res; res = res->next ) {
41  for ( atom = res->atom; atom; atom = atom->next ) {
42  x = (long) (atom->coord[0]/map->sampling(0)[0] + map->image->origin()[0] + 0.5);
43  y = (long) (atom->coord[1]/map->sampling(0)[1] + map->image->origin()[1] + 0.5);
44  z = (long) (atom->coord[2]/map->sampling(0)[2] + map->image->origin()[2] + 0.5);
45  if ( x>0 && x<map->sizeX()-1 && y>0 && y<map->sizeY()-1 && z>0 && z<map->sizeZ()-1 ) {
46  i = map->index(0, x, y, z, 0);
47  F = Vector3<double>((*map)[i+1] - (*map)[i-1], (*map)[i+map->sizeX()] - (*map)[i-map->sizeX()],
48  (*map)[i+slicesize] - (*map)[i-slicesize]);
49  Em -= (*map)[i];
50  atom->F += F * invunit;
51  }
52  nm++;
53  }
54  }
55  if ( nm ) mol->fom = (Em/nm + map->average())/map->standard_deviation() + 10;
56  else mol->fom = 100;
57  Emg += Em;
58  nmg += nm;
59  }
60  if ( nmg ) mg->fom = (Emg/nmg + map->average())/map->standard_deviation() + 10;
61  else mg->fom = 100;
62  E += Emg;
63  n += nmg;
64  }
65 
66  if ( n < 1 ) {
67  if ( verbose & VERB_PROCESS )
68  cout << "Warning: Molecule group outside map boundaries!" << endl;
69  E = 1e100;
70  } else
71  E = Kmap*((E/n + map->average())/map->standard_deviation() + 10);
72 
73  return E;
74 }
75 
83 double mol_map_energy(Bmolecule* mol, Bimage* map, double Kmap)
84 {
85  map->change_type(Float) ;
86 
87  Bresidue* res;
88  Batom* atom;
89  long i, n(0);
90  long x, y, z;
91  long slicesize = map->sizeX()*map->sizeY();
92  double E(0);
93  double invunit = 0.5*Kmap/map->sampling(0)[0];
95 
96  for( res = mol->res; res; res = res->next ) {
97  for ( atom = res->atom; atom; atom = atom->next ) {
98  x = (long) (atom->coord[0]/map->sampling(0)[0] + map->image->origin()[0] + 0.5);
99  y = (long) (atom->coord[1]/map->sampling(0)[1] + map->image->origin()[1] + 0.5);
100  z = (long) (atom->coord[2]/map->sampling(0)[2] + map->image->origin()[2] + 0.5);
101  if ( x>0 && x<map->sizeX()-1 && y>0 && y<map->sizeY()-1 && z>0 && z<map->sizeZ()-1 ) {
102  i = map->index(0, x, y, z, 0);
103  F = Vector3<double>((*map)[i+1] - (*map)[i-1], (*map)[i+map->sizeX()] - (*map)[i-map->sizeX()],
104  (*map)[i+slicesize] - (*map)[i-slicesize]);
105  E -= (*map)[i];
106  atom->F += F * invunit;
107  }
108  n++;
109  }
110  }
111 
112  if ( n < 1 ) {
113  if ( verbose & VERB_PROCESS )
114  cout << "Warning: Molecule outside map boundaries!" << endl;
115  E = 1e100;
116  } else
117  E = Kmap*((E/n + map->average())/map->standard_deviation() + 10);
118 
119  return E;
120 }
121 
129 {
130  map->change_type(Float) ;
131 
132  Bmolecule* mol;
133  Bresidue* res;
134  Batom* atom;
135  long i, n(0);
136  long datasize = map->data_size();
137  long x, y, z;
138  double sumc2(0), sumd2(0), sumcd(0);
139  map->next = new Bimage(Float, TSimple, map->size(), map->images());
140  float* calc = (float *) map->next->data_pointer();
141 
142  for ( mol = molgroup->mol; mol; mol = mol->next ) {
143  for( res = mol->res; res; res = res->next ) {
144  for ( atom = res->atom; atom; atom = atom->next ) {
145  x = (long) (atom->coord[0]/map->sampling(0)[0] + map->image->origin()[0] + 0.5);
146  y = (long) (atom->coord[1]/map->sampling(0)[1] + map->image->origin()[1] + 0.5);
147  z = (long) (atom->coord[2]/map->sampling(0)[2] + map->image->origin()[2] + 0.5);
148  if ( x>=0 && x<map->sizeX() && y>=0 && y<map->sizeY() && z>=0 && z<map->sizeZ() ) {
149  i = map->index(0, x, y, z, 0);
150  calc[i] += atom->mass;
151  }
152  }
153  }
154  }
155 
156  for ( i=0; i<datasize; i++ ) {
157  if ( calc[i] ) {
158  sumc2 += calc[i]*calc[i];
159  sumd2 += (*map)[i]*(*map)[i];
160  calc[i] *= (*map)[i];
161  sumcd += calc[i];
162  n++;
163  }
164  }
165 
166  double den = sqrt(sumc2*sumd2);
167  double CC = sumcd/den;
168  double norm = n/den;
169 
170  for ( i=0; i<datasize; i++ )
171  if ( calc[i] ) calc[i] *= norm;
172 
173  if ( verbose & VERB_FULL )
174  cout << "Correlation: " << CC << " (" << n << ")" << endl << endl;
175 
176  return CC;
177 }
178 
186 {
187  map->change_type(Float) ;
188 
189  Bresidue* res;
190  Batom* atom;
191  long i, n(0);
192  long datasize = map->data_size();
193  long x, y, z;
194  double sumc2(0), sumd2(0), sumcd(0);
195  map->next = new Bimage(Float, TSimple, map->size(), map->images());
196  float* calc = (float *) map->next->data_pointer();
197 
198  for( res = mol->res; res; res = res->next ) {
199  for ( atom = res->atom; atom; atom = atom->next ) {
200  x = (long) (atom->coord[0]/map->sampling(0)[0] + map->image->origin()[0] + 0.5);
201  y = (long) (atom->coord[1]/map->sampling(0)[1] + map->image->origin()[1] + 0.5);
202  z = (long) (atom->coord[2]/map->sampling(0)[2] + map->image->origin()[2] + 0.5);
203  if ( x>=0 && x<map->sizeX() && y>=0 && y<map->sizeY() && z>=0 && z<map->sizeZ() ) {
204  i = map->index(0, x, y, z, 0);
205  calc[i] += atom->mass;
206  }
207  }
208  }
209 
210  for ( i=0; i<datasize; i++ ) {
211  if ( calc[i] ) {
212  sumc2 += calc[i]*calc[i];
213  sumd2 += (*map)[i]*(*map)[i];
214  calc[i] *= (*map)[i];
215  sumcd += calc[i];
216  n++;
217  }
218  }
219 
220  double den = sqrt(sumc2*sumd2);
221  double CC = sumcd/den;
222 
223  if ( verbose & VERB_FULL )
224  cout << "Correlation for " << mol->id << ": " << CC << " (" << n << ")" << endl << endl;
225 
226  return CC;
227 }
228 
250 double molgroup_bond_fit_map_energy(Bmolgroup* molgroup, Bimage* map, double Kmap, int steps)
251 {
252  map->change_type(Float) ;
253 
254  Bbond* bond;
255  Batom *atom1, *atom2;
256  long i, n(0);
257  long slicesize = map->sizeX()*map->sizeY();
258  long x, y, z;
259  double frac, df, frac1, s1, s2, E(0);
260  double invunit = 0.5*Kmap/map->sampling(0)[0];
261  Vector3<double> F, F1, F2, loc;
262 
263  for ( bond = molgroup->bond; bond; bond = bond->next ) {
264  atom1 = bond->atom1;
265  atom2 = bond->atom2;
266  df = 1.0L/steps;
267  s1 = s2 = 0;
268  F1 = F2 = 0;
269  for ( frac=0; frac<=1; frac+=df ) {
270  frac1 = 1 - frac;
271  loc = atom1->coord * frac1 + atom2->coord * frac;
272  x = (long) (loc[0]/map->sampling(0)[0] + map->image->origin()[0] + 0.5);
273  y = (long) (loc[1]/map->sampling(0)[1] + map->image->origin()[1] + 0.5);
274  z = (long) (loc[2]/map->sampling(0)[2] + map->image->origin()[2] + 0.5);
275  if ( x>0 && x<map->sizeX()-1 && y>0 && y<map->sizeY()-1 && z>0 && z<map->sizeZ()-1 ) {
276  i = map->index(0, x, y, z, 0);
277  F = Vector3<double>((*map)[i+1] - (*map)[i-1], (*map)[i+map->sizeX()] - (*map)[i-map->sizeX()],
278  (*map)[i+slicesize] - (*map)[i-slicesize]);
279  F1 += F * frac1;
280  F2 += F * frac;
281  if ( !isfinite((*map)[i]) ) cout << "Infinite value at " << x << " " << y << " " << z << endl;
282  E -= (*map)[i];
283  s1 += frac1;
284  s2 += frac;
285  }
286  n++;
287  }
288  atom1->F += F1 * (invunit/s1);
289  atom2->F += F2 * (invunit/s2);
290  }
291 
292  if ( n < 1 ) {
293  if ( verbose & VERB_PROCESS )
294  cout << "Warning: Molecule outside map boundaries!" << endl;
295  E = 1e100;
296  } else
297  E = Kmap * ((E/n + map->average())/map->standard_deviation() + 10);
298 
299  return E;
300 }
301 
302 
303 
verbose
int verbose
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
Bresidue::next
Bresidue * next
Definition: rwmolecule.h:50
Bbond::next
Bbond * next
Definition: rwmolecule.h:135
Bimage::next
Bimage * next
Definition: Bimage.h:179
Bbond::atom1
Batom * atom1
Definition: rwmolecule.h:136
Bresidue::atom
Batom * atom
Definition: rwmolecule.h:54
Bmolgroup::mol
Bmolecule * mol
Definition: rwmolecule.h:189
molgroup_bond_fit_map_energy
double molgroup_bond_fit_map_energy(Bmolgroup *molgroup, Bimage *map, double Kmap, int steps)
Energy calculation of a molecule fit into a map.
Definition: mol_map_energy.cpp:250
Vector3< double >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
Bbond::atom2
Batom * atom2
Definition: rwmolecule.h:137
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Batom::next
Batom * next
Definition: rwmolecule.h:25
Bimage::standard_deviation
double standard_deviation()
Definition: Bimage.h:544
Bmolecule::res
Bresidue * res
Definition: rwmolecule.h:122
Bimage::images
long images()
Definition: Bimage.h:504
molgroup_map_correlation
double molgroup_map_correlation(Bmolgroup *molgroup, Bimage *map)
Calculation of the fit of a molecule group into a map as a correlation coefficient.
Definition: mol_map_energy.cpp:128
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
TSimple
@ TSimple
Definition: Bimage.h:59
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
rwmolecule.h
Header file for reading reflection files.
molgroup_map_energy
double molgroup_map_energy(Bmolgroup *molgroup, Bimage *map, double Kmap)
Energy calculation of a molecule fit into a map.
Definition: mol_map_energy.cpp:23
Bbond
Definition: rwmolecule.h:134
Bmolecule::next
Bmolecule * next
Definition: rwmolecule.h:108
Bimage::data_size
long data_size()
Definition: Bimage.h:270
Bmolecule
Definition: rwmolecule.h:107
Bimage
General image parameter class.
Definition: Bimage.h:177
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
Bmolgroup::bond
Bbond * bond
Definition: rwmolecule.h:190
Bmolgroup::next
Bmolgroup * next
Definition: rwmolecule.h:171
utilities.h
Header file for general utilities.
Batom::mass
double mass
Definition: rwmolecule.h:32
isfinite
bool isfinite(complex< T > &value)
Batom::F
Vector3< double > F
Definition: rwmolecule.h:36
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
Bimage::index
long index(long nx, long ny) const
Definition: Bimage.h:396
rwimg.h
Header file for 2D and 3D image I/O.
Bresidue
Definition: rwmolecule.h:49
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:284
Bmolgroup
Definition: rwmolecule.h:170
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
mol_map_energy
double mol_map_energy(Bmolecule *mol, Bimage *map, double Kmap)
Energy calculation of a molecule fit into a map.
Definition: mol_map_energy.cpp:83
Bmolgroup::fom
double fom
Definition: rwmolecule.h:187
Bmolecule::fom
double fom
Definition: rwmolecule.h:121
Float
@ Float
Definition: utilities.h:161
Batom::coord
Vector3< double > coord
Definition: rwmolecule.h:30
Batom
Definition: rwmolecule.h:24
Bmolecule::id
Bstring id
Definition: rwmolecule.h:110
mol_map_correlation
double mol_map_correlation(Bmolecule *mol, Bimage *map)
Calculation of the fit of a molecule into a map as a correlation coefficient.
Definition: mol_map_energy.cpp:185