Bsoft  2.1.0
Bernard's software package
mol_util.cpp
Go to the documentation of this file.
1 
9 #include "rwmolecule.h"
10 #include "rwatomprop.h"
11 #include "rwresprop.h"
12 #include "mol_util.h"
13 #include "seq_util.h"
14 #include "linked_list.h"
15 #include "Matrix.h"
16 #include "utilities.h"
17 
18 #include <map>
19 
20 // Declaration of global variables
21 extern int verbose; // Level of output to the screen
22 
29 {
30  int nat = 0;
31  double avgB = 0, sdB = 0, atomB = 0, dist, maxdist = 0;
32  char atomtype[10];
33  Vector3<double> ca;
34  Bmolecule* mol;
35  Bresidue* res;
36  Batom* atom;
37 
38  if ( verbose & VERB_LABEL )
39  cout << "Residue\tNatoms\tAvgB\tStDevB" << endl;
40 
41  for ( mol = molgroup->mol; mol; mol = mol->next ) {
42  for( res = mol->res; res; res = res->next ) {
43  nat = 0;
44  avgB = sdB = 0;
45  for ( atom = res->atom; atom; atom = atom->next ) {
46  nat++;
47  avgB += atom->b;
48  sdB += atom->b*atom->b;
49  }
50  avgB /= nat;
51  sdB = sqrt(sdB/nat - avgB*avgB);
52  cout << res->num << tab << nat << tab << avgB << tab << sdB << endl;
53  }
54  }
55 
56  if ( verbose & VERB_PROCESS )
57  cout << "Residue\tMaxDist\tBfactor\tAtom" << endl;
58 
59  for ( mol = molgroup->mol; mol; mol = mol->next ) {
60  for( res = mol->res; res; res = res->next ) {
61  for ( atom = res->atom; atom; atom = atom->next ) {
62  if ( strstr(atom->type, "CA" ) ) {
63  ca = atom->coord;
64  strncpy(atomtype, atom->type, 8);
65  atomB = atom->b;
66  } else if ( strncmp(atom->type, "H", 1 ) &&
67  strncmp(atom->type, " H", 2 ) &&
68  !strstr(atom->type, "N " ) &&
69  !strstr(atom->type, "C " ) &&
70  !strstr(atom->type, "O " ) ) {
71  dist = (atom->coord - ca).length();
72  if ( maxdist < dist ) {
73  maxdist = dist;
74  strncpy(atomtype, atom->type, 8);
75  atomB = atom->b;
76  }
77  }
78  }
79  cout << res->num << tab << maxdist << tab << atomB << tab << atomtype << endl;
80  maxdist = 0;
81  }
82  }
83 
84  return 0;
85 }
86 
93 {
94  Bmolecule* mol;
95 
96  if ( verbose & VERB_LABEL )
97  cout << "Sequence:" << endl;
98 
99  for ( mol = molgroup->mol; mol; mol = mol->next ) {
100  cout << "Molecule: " << mol->id << endl << mol->seq << endl;
101  if ( verbose & VERB_PROCESS )
102  cout << endl << "Length = " << mol->nres << endl << endl;
103  }
104 
105  return 0;
106 }
107 
114 int molgroup_set_radius(Bmolgroup* molgroup, double radius)
115 {
116  if ( radius < 0.1 ) return 0;
117 
118  long natom;
119  double dist, sum, sumsq, avg, std;
120  Bmolecule* mol;
121  Bresidue* res;
122  Batom* atom;
123 
124  if ( verbose & VERB_LABEL )
125  cout << "Shifting to radius: " << radius << endl << endl;
126 
127  for ( sum=sumsq=natom=0, mol = molgroup->mol; mol; mol = mol->next ) {
128  for( res = mol->res; res; res = res->next ) {
129  for ( atom = res->atom; atom; atom = atom->next, natom++ ) {
130  dist = atom->coord.length();
131  sum += dist;
132  sumsq += dist*dist;
133  atom->coord *= radius/dist;
134  }
135  }
136  }
137 
138  avg = sum/natom;
139  std = sumsq/natom - avg*avg;
140  if ( std > 0 ) std = sqrt(std);
141  else std = 0;
142 
143  if ( verbose & VERB_PROCESS )
144  cout << "Radius avg and std: " << avg << " " << std << endl << endl;
145 
146  molgroup_stats(molgroup);
147 
148  return 0;
149 }
150 
161 int molgroup_rename(Bmolgroup* molgroup, char first_name)
162 {
163  if ( first_name == 0 ) return 0;
164 
165  char letter = first_name;
166  Bmolecule* mol;
167 
168  for ( mol=molgroup->mol; mol; mol=mol->next ) {
169  mol->id[0] = letter++;
170  if ( letter > 'Z' ) letter = 'A';
171  }
172 
173  return 0;
174 }
175 
182 int molgroup_residue_renumber(Bmolgroup* molgroup, int first)
183 {
184  if ( first < 1 ) return 0;
185 
186  int resnum;
187  Bmolecule* mol;
188  Bresidue* res;
189 
190  if ( verbose & VERB_LABEL )
191  cout << "Renumbering residues from: " << first << endl;
192 
193  for ( mol = molgroup->mol; mol; mol = mol->next ) {
194  resnum = first;
195  for( res = mol->res; res; res = res->next ) {
196  res->num = resnum;
197  res->insert[0] = ' ';
198  resnum++;
199  }
200  }
201 
202  return 0;
203 }
204 
211 int molgroup_atom_renumber(Bmolgroup* molgroup, int first)
212 {
213  if ( first < 1 ) return 0;
214 
215  int atomnum = first;
216  Bmolecule* mol;
217  Bresidue* res;
218  Batom* atom;
219 
220  if ( verbose & VERB_LABEL )
221  cout << "Renumbering atoms from: " << first << endl;
222 
223  for ( mol = molgroup->mol; mol; mol = mol->next ) {
224  for( res = mol->res; res; res = res->next ) {
225  for ( atom = res->atom; atom; atom = atom->next ) {
226  atom->num = atomnum;
227  atomnum++;
228  }
229  }
230  }
231 
232  return 0;
233 }
234 
243 int molgroup_coor_reset_occupancy(Bmolgroup* molgroup, int range_first,
244  int range_last, double occupancy)
245 {
246  Bmolecule* mol;
247  Bresidue* res;
248  Batom* atom;
249 
250  if ( verbose & VERB_LABEL )
251  cout << "Reseting occupancy to range: " << range_first << " " <<range_last << endl;
252 
253  for ( mol = molgroup->mol; mol; mol = mol->next ) {
254  for( res = mol->res; res; res = res->next ) {
255  if ( res->num >= range_first && res->num <= range_last )
256  for ( atom = res->atom; atom; atom = atom->next ) {
257  atom->q = occupancy;
258  }
259  }
260  }
261 
262  return 0;
263 }
264 
271 {
272  int n;
273  double summass(0), totmass = 0;
274  Bmolecule* mol;
275  Bresidue* res;
276  Batom* atom;
277 
278  if ( verbose & VERB_PROCESS )
279  cout << "Molecule\tMW(Da)\tAtoms" << endl;
280  for ( mol = molgroup->mol; mol; mol = mol->next ) {
281  summass = 0;
282  n = 0;
283  for( res = mol->res; res; res = res->next ) {
284  for ( atom = res->atom; atom; atom = atom->next ) {
285  n++;
286  summass += atom->mass;
287  }
288  }
289  if ( verbose & VERB_PROCESS )
290  cout << mol->id << tab << summass << tab << n << endl;
291  totmass += summass;
292  }
293 
294  if ( verbose ) {
295  cout << "Total molecular weight: " << totmass << " Da" << endl;
296  cout << "Total volume: " << totmass/RHO << " A3" << endl << endl;
297  }
298 
299  return totmass;
300 }
301 
308 {
309  double summass(0);
310  Bmolecule* mol;
311  Bresidue* res;
312  Batom* atom;
313  Vector3<double> acoord, com, vec;
314 
315  for ( mol = molgroup->mol; mol; mol = mol->next ) {
316  for( res = mol->res; res; res = res->next ) {
317  for ( atom = res->atom; atom; atom = atom->next ) {
318  acoord = atom->coord * atom->mass;
319  com += acoord;
320  vec += acoord*acoord;
321  summass += atom->mass;
322  }
323  }
324  }
325 
326  if ( summass ) {
327  com /= summass;
328  vec = vec/summass - com*com;
329  vec = vec.square_root();
330  vec.normalize();
331  } else cout << "Error: No atom masses found!" << endl << endl;
332 
333  if ( verbose & VERB_FULL ) {
334  cout << "Total molecular weight: " << summass << " Da" << endl;
335  cout << "Center of mass: " << com << " A" << endl;
336  cout << "Major axis: " << vec << endl;
337  }
338 
339  return com;
340 }
341 
348 {
349  double summass(0);
350  Bmolecule* mol;
351  Bresidue* res;
352  Batom* atom;
353  Vector3<double> acoord, com, vec;
354 
355  for ( mol = molgroup->mol; mol; mol = mol->next ) if ( mol->sel ) {
356  for( res = mol->res; res; res = res->next ) {
357  for ( atom = res->atom; atom; atom = atom->next ) if ( atom->sel ) {
358  acoord = atom->coord * atom->mass;
359  com += acoord;
360  vec += acoord*acoord;
361  summass += atom->mass;
362  }
363  }
364  }
365 
366  if ( summass ) {
367  com /= summass;
368  vec = vec/summass - com*com;
369  vec = vec.square_root();
370  vec.normalize();
371  } else cout << "Error: No atom masses found!" << endl << endl;
372 
373  if ( verbose & VERB_FULL ) {
374  cout << "Total molecular weight: " << summass << " Da" << endl;
375  cout << "Center of mass: " << com << " A" << endl;
376  cout << "Major axis: " << vec << endl;
377  }
378 
379  return com;
380 }
381 
388 {
390 
391  cout << "Center of mass: " << com << " A" << endl;
392 
393  return com;
394 }
395 
402 {
403  double summass(0);
404  Bresidue* res;
405  Batom* atom;
406  Vector3<double> acoord, com, vec;
407 
408  for( res = mol->res; res; res = res->next ) {
409  for ( atom = res->atom; atom; atom = atom->next ) {
410  acoord = atom->coord * atom->mass;
411  com += acoord;
412  vec += acoord*acoord;
413  summass += atom->mass;
414  }
415  }
416 
417  if ( summass ) {
418  com /= summass;
419  vec = vec/summass - com*com;
420  vec = vec.square_root();
421  vec.normalize();
422  } else cerr << "Error: No atom masses found!" << endl << endl;
423 
424  if ( verbose & VERB_FULL ) {
425  cout << "Molecule: " << mol->id << endl;
426  cout << "Molecular weight: " << summass << " Da" << endl;
427  cout << "Center of mass: " << com << " A" << endl;
428  cout << "Major axis: " << vec << endl << endl;
429  }
430 
431  return com;
432 }
433 
440 {
442 
443  cout << "Molecule: " << mol->id << endl;
444  cout << "Center of mass: " << com << " A" << endl;
445 
446  return com;
447 }
448 
456 {
457  double summass(0);
458  Bmolecule* mol;
459  Bresidue* res;
460  Batom* atom;
461  Vector3<double> eigenval, vec, vec2, vecx;
462 
463  for ( mol = molgroup->mol; mol; mol = mol->next ) {
464  mol_principal_axes(mol, eigenvec);
465  for( res = mol->res; res; res = res->next ) {
466  for ( atom = res->atom; atom; atom = atom->next ) {
467  vec += atom->coord * atom->mass; // Sums
468  vec2 += (atom->coord*atom->coord) * atom->mass; // Square sums
469  vecx[0] += atom->mass*atom->coord[0]*atom->coord[1]; // Cross-term sums
470  vecx[1] += atom->mass*atom->coord[0]*atom->coord[2];
471  vecx[2] += atom->mass*atom->coord[1]*atom->coord[2];
472  summass += atom->mass;
473  }
474  }
475  }
476 
477  if ( verbose & VERB_PROCESS ) {
478  cout << "Molecule group:" << endl;
479  cout << "Molecular weight: " << summass << " Da" << endl;
480  }
481 
482  if ( summass < 1 ) {
483  cout << "Error: No atom masses found!" << endl << endl;
484  return vec;
485  }
486 
487  vec /= summass;
488  vec2 /= summass;
489  vecx /= summass;
490 
491  return principal_axes(vec, vec2, vecx, eigenvec);
492 }
493 
501 {
502  double summass(0);
503  Bresidue* res;
504  Batom* atom;
505  Vector3<double> eigenval, vec, vec2, vecx;
506 
507  for( res = mol->res; res; res = res->next ) {
508  for ( atom = res->atom; atom; atom = atom->next ) {
509  vec += atom->coord * atom->mass; // Sums
510  vec2 += (atom->coord*atom->coord) * atom->mass; // Square sums
511  vecx[0] += atom->mass*atom->coord[0]*atom->coord[1]; // Cross-term sums
512  vecx[1] += atom->mass*atom->coord[0]*atom->coord[2];
513  vecx[2] += atom->mass*atom->coord[1]*atom->coord[2];
514  summass += atom->mass;
515  }
516  }
517 
518  if ( verbose & VERB_PROCESS ) {
519  cout << "Molecule: " << mol->id << endl;
520  cout << "Molecular weight: " << summass << " Da" << endl;
521  }
522 
523  if ( summass < 1 ) {
524  cout << "Error: No atom masses found!" << endl << endl;
525  bexit(-1);
526  }
527 
528  vec /= summass;
529  vec2 /= summass;
530  vecx /= summass;
531 
532  return principal_axes(vec, vec2, vecx, eigenvec);
533 }
534 
545 double molgroup_sphericity(Bmolgroup* molgroup, double da)
546 {
547  if ( da > 1 ) da *= M_PI/180.0;
548 
549  long i, j, k;
550  double t, p, dp, pm, st, dist, r, dm(0), avg(0), std(0), sph(0);
551  Bmolecule* mol;
552  Bresidue* res;
553  Batom* atom;
554  Vector3<double> c;
555 
557 
558  if ( verbose )
559  cout << "Calculating sphericity with a step size of "
560  << da*180.0/M_PI << " degrees" << endl << endl;
561 
562  vector<long> n, na;
563 
564  for ( i = 0, t = 0; t <= M_PI; t += da ) {
565  st = sin(t);
566  if ( st > 1e-3 ) {
567  dp = da/sin(t);
568  pm = TWOPI - dp/2;
569  } else {
570  dp = 10;
571  pm = TWOPI;
572  }
573  for ( j=0, p = 0; p<pm; p += dp, i++, j++ )
574  if ( verbose & VERB_FULL )
575  cout << i << tab << t << tab << p << endl;
576  na.push_back(i);
577  n.push_back(j);
578  if ( verbose & VERB_FULL )
579  cout << t << tab << n.back() << tab << na.back() << endl;
580  }
581 
582  vector<long> d(i, 0);
583 
584  for ( mol = molgroup->mol; mol; mol = mol->next )
585  for( res = mol->res; res; res = res->next )
586  for ( atom = res->atom; atom; atom = atom->next ) {
587  c = atom->coord - com;
588  dist = c.normalize();
589  if ( dm < dist ) dm = dist;
590  t = acos(c[2]);
591  j = long(t/da + 0.5);
592  if ( j >= n.size() ) j = n.size() - 1;
593  i = j;
594  if ( i < 0 || i >= n.size() )
595  cerr << "i = " << i << endl;
596  k = 0;
597  if ( j ) {
598  i = na[j-1];
599  r = sqrt(c[0]*c[0]+c[1]*c[1]);
600  if ( r > da/2 ) {
601  dp = da/r;
602  p = atan2(c[1],c[0]);
603 // if ( p < -dp ) p += TWOPI;
604 // if ( p >= TWOPI - dp ) p -= TWOPI;
605  if ( p < 0 ) p += TWOPI;
606  if ( p >= TWOPI ) p -= TWOPI;
607  k = long(p/dp);
608  if ( k >= n[j] ) k -= n[j];
609 // if ( k < 0 )
610 // cerr << "k = " << k << endl;
611  i += k;
612  }
613  }
614  if ( i >= d.size() ) {
615  cerr << "Error: array out of bounds!" << endl;
616  cerr << i << tab << j << tab << k << endl;
617  }
618  if ( d[i] < dist ) d[i] = dist;
619  }
620 
621  for ( i = 0; i < d.size(); ++i ) {
622  for ( j = 0; j < na.size() && na[j] <= i; ++j ) ;
623  if ( j ) k = i - na[j-1];
624  else k = 0;
625  t = da*j;
626  st = sin(t);
627  if ( st > 1e-3 )
628  p = (da/st)*k;
629  else
630  p = 0;
631  if ( verbose & VERB_PROCESS )
632  cout << i << tab << j << tab << t << tab << p << tab << d[i] << endl;
633  avg += d[i];
634  std += d[i] * d[i];
635  }
636 
637  avg /= d.size();
638  std /= d.size();
639  std = sqrt(std - avg*avg);
640  sph = 1/(1+std);
641 
642  if ( verbose ) {
643  cout << "Number of solid angle bins: " << d.size() << endl;
644  cout << "Maximal distance: " << dm << endl;
645  cout << "Average bin maximal distance: " << avg << endl;
646  cout << "Standard deviation: " << std << endl;
647  cout << "Sphericity: " << sph << endl << endl;
648  }
649 
650  return sph;
651 }
652 
666 double molgroup_density(Bmolgroup* molgroup)
667 {
668  long nmol, nres, natom;
669  double mass = 0;
670  Bmolecule* mol;
671  Bresidue* res;
672  Batom* atom;
673 
674  for ( nmol=nres=natom=0, mol = molgroup->mol; mol; mol = mol->next, nmol++ )
675  for( res = mol->res; res; res = res->next, nres++ )
676  for ( atom = res->atom; atom; atom = atom->next, natom++ ) mass += atom->mass;
677 
678  double volume = molgroup->box.volume();
679 
680  double density = mass/volume;
681 
682  if ( verbose & VERB_PROCESS ) {
683  cout << "Number of molecules and atoms: " << nmol << " " << nres << " " << natom << endl;
684  cout << "Volume: " << molgroup->box << " = " << volume << " A3" << endl;
685  cout << "Density: " << density << " Da/A3" << density*1.66058 << " g/ml" << endl;
686  cout << "Density: " << nmol*1.0/volume << " molecules/A3 "
687  << nres*1.0/volume << " residues/A3 " << natom*1.0/volume << " atoms/A3" << endl << endl;
688  }
689 
690  return density;
691 }
692 
704 double molgroup_volume(Bmolgroup* molgroup, Bstring& paramfile, int wrap)
705 {
706  Batomtype* atompar = get_atom_properties(paramfile);
707  Bresidue_type* respar = get_residue_properties(paramfile);
708 
709 // write_atom_properties("at.star", atompar);
710 
711  if ( molgroup->box.volume() < 1 )
712  molgroup->box = molgroup->max - molgroup->min;
713 
714  long i, x, y, z, ix, iy, iz, rad;
715  double volume = 0, VdWvol = 0, mass = 0, resmass, sampling = 1, edge = 3, dist, dist2;
716  double voxel_volume = sampling*sampling*sampling;
717  Vector3<int> gridsize, lo, hi;
718  Vector3<double> c, d;
719  if ( wrap ) edge = 0;
720  gridsize[0] = (int) ((molgroup->max[0] - molgroup->min[0] + 2*edge)/sampling + 1);
721  gridsize[1] = (int) ((molgroup->max[1] - molgroup->min[1] + 2*edge)/sampling + 1);
722  gridsize[2] = (int) ((molgroup->max[2] - molgroup->min[2] + 2*edge)/sampling + 1);
723 // Vector3<int> h = gridsize * 0.5;
724  long size = (long) gridsize.volume();
725 
726  char* grid = new char[size];
727  for ( i=0; i<size; i++ ) grid[i] = 0;
728 
729  Bmolecule* mol;
730  Bresidue* res;
731  Batom* atom;
732  Batomtype* at;
733  Bresidue_type* rt;
734 
735  cout << "Calculating the Van der Waals volume:" << endl;
736  cout << " in volume: " << gridsize << " = " << size << endl;
737  if ( wrap ) cout << "with wrapping." << endl;
738 
739  for ( mol = molgroup->mol; mol; mol = mol->next ) {
740  for( res = mol->res; res; res = res->next ) {
741  for ( atom = res->atom, resmass=0; atom; atom = atom->next ) {
742  resmass += atom->mass;
743  for ( at = atompar; at && strncmp(atom->el, at->el, 2)!=0; at = at->next ) ;
744  dist = 2/sampling; // Atom types not found tend to be larger
745  if ( at ) {
746  if ( at->vdw < 0.8 )
747  cout << "VdW distance too short! atom=" << atom->el << " dist=" << at->vdw << endl;
748  else
749  dist = at->vdw/sampling;
750  }
751  dist2 = dist*dist;
752  rad = (long) (dist + 1);
753  c = atom->coord - molgroup->min;
754  c += edge;
755  c /= sampling;
756  lo = Vector3<int>((int)c[0], (int)c[1], (int)c[2]) - rad;
757  hi = Vector3<int>((int)c[0], (int)c[1], (int)c[2]) + rad;
758  if ( !wrap ) {
759  lo = lo.max(0);
760  hi = hi.min(gridsize - 1);
761  }
762  for ( z=lo[2]; z<=hi[2]; z++ ) {
763  iz = z;
764  if ( iz < 0 ) iz += gridsize[2];
765  if ( iz >= gridsize[2] ) iz -= gridsize[2];
766  d[2] = c[2] - z;
767  for ( y=lo[1]; y<=hi[1]; y++ ) {
768  iy = y;
769  if ( iy < 0 ) iy += gridsize[1];
770  if ( iy >= gridsize[1] ) iy -= gridsize[1];
771  d[1] = c[1] - iy;
772  for ( x=lo[0]; x<=hi[0]; x++ ) {
773  ix = x;
774  if ( ix < 0 ) ix += gridsize[0];
775  if ( ix >= gridsize[0] ) ix -= gridsize[0];
776  d[0] = c[0] - ix;
777  if ( d.length2() <= dist2 )
778  grid[(iz*gridsize[1] + iy)*gridsize[0] + ix] = 1;
779  }
780  }
781  }
782  }
783  mass += resmass;
784  for ( rt = respar; rt && strncmp(rt->cod, res->type, 3)!=0; rt = rt->next ) ;
785  if ( rt ) volume += rt->vol;
786  else volume += resmass/RHO;
787  }
788  }
789 
790  cout << "Mass: " << mass << " Da" << endl;
791  cout << "Volume from residue volumes: " << volume << " A3 (" <<
792  volume*100.0/molgroup->box.volume() << " %)" << endl;
793  cout << "Density: " << mass/volume << " Da/A3 "
794  << 1.66058*mass/volume << " g/ml" << endl << endl;
795 
796  for ( i=0, VdWvol=0; i<size; i++ ) VdWvol += grid[i];
797  VdWvol *= voxel_volume;
798 
799  cout << "Van der Waals volume: " << VdWvol << " A3 ("
800  << VdWvol*100.0/(voxel_volume*size) << " %)" << endl;
801  cout << "Packing density: " << VdWvol/volume << endl << endl;
802 
803  delete[] grid;
804 
805  kill_list((char *) atompar, sizeof(Batomtype));
806  kill_list((char *) respar, sizeof(Bresidue_type));
807 
808  return volume;
809 }
810 
817 int molgroup_composition(Bmolgroup* molgroup, Bstring& paramfile)
818 {
819  Batomtype* atompar = get_atom_properties(paramfile);
820 
821  long i, t, nel(0), nres(0), el[1024], an(0);
822  double mass[1024], mw(0), extinc(0);
823  Bmolecule* mol;
824  Bresidue* res;
825  Batom* atom;
826  Batomtype* at;
827 
828  map<char, long> num;
829 
830  for ( t=0, at = atompar; at && t < 1022; at = at->next, t++ )
831  mass[t] = el[t] = 0;
832 
833  for ( mol = molgroup->mol; mol; mol = mol->next ) {
834  if ( mol->seq.length() ) {
835  for ( i=0; i<mol->seq.length(); i++, nres++ ) {
836  if ( num.find(mol->seq[i]) == num.end() )
837  num[mol->seq[i]] = 1;
838  else
839  num[mol->seq[i]]++;
840  }
841  }
842  for( res = mol->res; res; res = res->next )
843  for ( atom = res->atom; atom; atom = atom->next ) {
844  el[atom->tnum]++;
845  mass[atom->tnum] += atom->mass;
846  mw += atom->mass;
847  nel++;
848  }
849  }
850 
851  for ( t=0, at = atompar; at && t < 1022; at = at->next, t++ )
852  an += el[t]*at->z;
853 
854  // From Mach, Middaugh and Lewis (1992) Analytical Biochemistry 200:74-80
855  // From Pace et al. (1995) Protein Science 4:2411-2423.
856  // Cystines: 125 /M/cm
857  extinc += num['W'] * 5500;
858  extinc += num['Y'] * 1490;
859  // From different source
860  extinc += num['F'] * 200;
861 
862  if ( nel ) {
863  cout << "Elemental composition:" << endl;
864  cout << "Element\tNumber\tMass" << endl;
865  for ( t=0, at = atompar; at && t < 1022; at = at->next, t++ )
866  if ( el[t] ) cout << at->el << tab << el[t] << tab << mass[t] << endl;
867  cout << endl;
868  cout << endl;
869  cout << "Total molecular weight: " << mw << " Da" << endl;
870  cout << "Total atomic number: " << an << endl << endl;
871  }
872 
873  if ( nres ) {
874  cout << "Residue composition:" << endl;
875  cout << "Residue\tNumber" << endl;
876  for ( auto it=num.begin(); it!=num.end(); ++it )
877  cout << it->first << tab << it->second << endl;
878  cout << endl;
879  cout << "Extinction coefficient (280 nm): " << extinc << " /M/cm" << endl << endl;
880  }
881 
882  kill_list((char *) atompar, sizeof(Batomtype));
883 
884  return 0;
885 }
886 
893 {
894  JSvalue comp(JSobject);
895 
896  Bmolecule* mol;
897  Bresidue* res;
898  Batom* atom;
899 
900  for ( mol = molgroup->mol; mol; mol = mol->next )
901  for( res = mol->res; res; res = res->next )
902  for ( atom = res->atom; atom; atom = atom->next )
903  if ( comp.exists(atom->el) )
904  comp[atom->el] += 1;
905  else
906  comp[atom->el] = 0;
907 
908 // cout << comp << endl;
909 
910  return comp;
911 }
912 
920 {
921  long i, j, n(0);
922  Bmolecule* mol;
923  Bresidue_type* rt_list = get_residue_properties(paramfile);
924  Bresidue_type* rt = NULL;
925  vector<double> el(5,0), eltot(5,0);
926 
927  if ( verbose & VERB_LABEL )
928  cout << "Calculating the elemental composition:" << endl << endl;
929 
930  if ( verbose )
931  cout << "Molecule\tH\tC\tN\tO\tS\tTotal" << endl;
932  for ( mol = molgroup->mol; mol; mol = mol->next ) {
933 // cout << mol->seq << endl;
934  for ( i=0; i<mol->seq.length(); i++ ) {
935  for ( rt = rt_list; rt && rt->c != mol->seq[i]; rt = rt->next ) ;
936  if ( rt )
937  for ( j=0; j<5; ++j )
938  el[j] += rt->comp[j];
939  }
940  if ( verbose )
941  cout << mol->id;
942  for ( j=n=0; j<5; ++j ) {
943  n += el[j];
944  eltot[j] += el[j];
945  if ( verbose )
946  cout << tab << el[j];
947  el[j] = 0;
948  }
949  if ( verbose )
950  cout << tab << n << endl;
951  }
952  if ( verbose )
953  cout << "Total";
954  for ( j=n=0; j<5; ++j ) {
955  n += eltot[j];
956  if ( verbose )
957  cout << tab << eltot[j];
958  el[j] = 0;
959  }
960  if ( verbose )
961  cout << tab << n << endl;
962 
963  kill_list((char *) rt_list, sizeof(Bresidue_type));
964 
965  if ( verbose ) {
966  cout << "%";
967  for ( j=0; j<5; ++j )
968  cout << tab << eltot[j]*100.0/n;
969  cout << endl;
970  }
971 
972  JSvalue comp(JSobject);
973 
974  comp["H"] = eltot[0];
975  comp["C"] = eltot[1];
976  comp["N"] = eltot[2];
977  comp["O"] = eltot[3];
978  comp["S"] = eltot[4];
979 
980  if ( verbose & VERB_PROCESS )
981  cout << comp << endl;
982 
983  return comp;
984 }
985 
992 {
993  JSvalue comp(JSobject);
994 
995  comp["H"] = 0.498;
996  comp["C"] = 0.320;
997  comp["N"] = 0.085;
998  comp["O"] = 0.095;
999  comp["S"] = 0.002;
1000 
1001  double mc = comp["H"].real() +
1002  12*comp["C"].real() +
1003  14*comp["N"].real() +
1004  16*comp["O"].real() +
1005  32*comp["S"].real();
1006 
1007  double sc = mass/mc;
1008 // cout << mass << tab << mc << tab << sc << endl;
1009 
1010  for ( auto it = comp.object_begin(); it != comp.object_end(); ++it )
1011  it->second *= sc;
1012 
1013  if ( verbose & VERB_PROCESS )
1014  cout << comp << endl;
1015 
1016  return comp;
1017 }
1018 
1030 int molgroup_radial_density(Bmolgroup* molgroup, double interval, double cutoff, int wrap)
1031 {
1032  int i, ii, j, x, y, z, xx, yy, zz, ix, iy, iz;
1033  int xlo, ylo, zlo, xhi, yhi, zhi;
1034  Vector3<double> box = molgroup->box;
1035  double dist;
1036  Vector3<double> d, sampling(cutoff, cutoff, cutoff);
1037  Vector3<int> size((int) (box[0]/sampling[0] + 0.001),
1038  (int) (box[1]/sampling[1] + 0.001), (int) (box[2]/sampling[2] + 0.001));
1039  size = size.max(1);
1040  for ( i=0; i<3; i++ ) sampling[i] = box[i]/size[i] + 0.001;
1041 
1042  Latom** alist = molgroup_atom_mesh_lists(molgroup, size, sampling);
1043  Latom *latom, *latom2;
1044 
1045  double mult = 1.0/interval;
1046  int n = (int) (mult*cutoff+1);
1047  int* rdf = new int[n];
1048  for ( i=0; i<n; i++ ) rdf[i] = 0;
1049 
1050  for ( i=z=0; z<size[2]; z++ ) {
1051  zlo = z - 1;
1052  zhi = z + 1;
1053  if ( !wrap ) {
1054  if ( zlo < 0 ) zlo = 0;
1055  if ( zhi >= size[2] ) zhi = size[2] - 1;
1056  }
1057  for ( y=0; y<size[1]; y++ ) {
1058  ylo = y - 1;
1059  yhi = y + 1;
1060  if ( !wrap ) {
1061  if ( ylo < 0 ) ylo = 0;
1062  if ( yhi >= size[1] ) yhi = size[1] - 1;
1063  }
1064  for ( x=0; x<size[0]; x++, i++ ) {
1065  xlo = x - 1;
1066  xhi = x + 1;
1067  if ( !wrap ) {
1068  if ( xlo < 0 ) xlo = 0;
1069  if ( xhi >= size[0] ) xhi = size[0] - 1;
1070  }
1071  for ( latom = alist[i]; latom; latom = latom->next ) {
1072  for ( zz=zlo; zz<=zhi; zz++ ) {
1073  iz = zz;
1074  if ( wrap ) {
1075  if ( iz < 0 ) iz += size[2];
1076  if ( iz >= size[2]) iz -= size[2];
1077  }
1078  for ( yy=ylo; yy<=yhi; yy++ ) {
1079  iy = yy;
1080  if ( wrap ) {
1081  if ( iy < 0 ) iy += size[1];
1082  if ( iy >= size[1] ) iy -= size[1];
1083  }
1084  for ( xx=xlo; xx<=xhi; xx++ ) {
1085  ix = xx;
1086  if ( wrap ) {
1087  if ( ix < 0 ) ix += size[0];
1088  if ( ix >= size[0] ) ix -= size[0];
1089  }
1090  ii = (iz*size[1] + iy)*size[0] + ix;
1091  for ( latom2 = alist[ii]; latom2; latom2 = latom2->next )
1092  if ( latom2->atom != latom->atom ) {
1093  d = vector3_difference_PBC(latom2->atom->coord, latom->atom->coord, box);
1094  dist = d.length();
1095  if ( dist <= cutoff ) {
1096  j = (int) (mult*dist);
1097  if ( j < n ) rdf[j]++;
1098  }
1099  }
1100  }
1101  }
1102  }
1103  }
1104  }
1105  }
1106  }
1107 
1108  for ( i=0; i<size.volume(); i++ ) kill_list((char *) alist[i], sizeof(Latom));
1109  delete[] alist;
1110 
1111  cout << "Calculating the radial distribution function within " << cutoff << " A" << endl;
1112 
1113  cout << "r(A)\tRDF" << endl;
1114  for ( i=0; i<n; i++ ) cout << i*interval << tab << rdf[i] << endl;
1115  cout << endl;
1116 
1117  delete[] rdf;
1118 
1119  return 0;
1120 }
1121 
1133 {
1134  long i, x, y, z;
1135 
1136  Vector3<double> origin = molgroup->min;
1137 
1138  long gridvol = (long) size.volume();
1139  Bmolecule* mol;
1140  Bresidue* res;
1141  Batom* atom;
1142  Latom** alist = new Latom*[gridvol];
1143  Latom** alistp = new Latom*[gridvol];
1144 
1145  for ( i=0; i<gridvol; i++ ) alist[i] = alistp[i] = NULL;
1146 
1147  for ( mol = molgroup->mol; mol; mol = mol->next ) {
1148  for ( res = mol->res; res; res = res->next ) {
1149  for ( atom = res->atom; atom; atom = atom->next ) {
1150  x = (long) ((atom->coord[0] - origin[0])/sampling[0]);
1151  y = (long) ((atom->coord[1] - origin[1])/sampling[1]);
1152  z = (long) ((atom->coord[2] - origin[2])/sampling[2]);
1153  if ( x < 0 ) x = 0;
1154  if ( y < 0 ) y = 0;
1155  if ( z < 0 ) z = 0;
1156  if ( x >= size[0] ) x = size[0] - 1;
1157  if ( y >= size[1] ) y = size[1] - 1;
1158  if ( z >= size[2] ) z = size[2] - 1;
1159  i = (z*size[1] + y)*size[0] + x;
1160  alistp[i] = (Latom *) add_item((char **) &alistp[i], sizeof(Latom));
1161  alistp[i]->atom = atom;
1162  if ( !alist[i] ) alist[i] = alistp[i];
1163  }
1164  }
1165  }
1166 
1167  delete[] alistp;
1168 
1169  if ( verbose & VERB_DEBUG )
1170  cout << "DEBUG molgroup_atom_mesh_lists: Done!" << endl;
1171 
1172  return alist;
1173 }
1174 
1180 vector<Batom*> atom_get_array(Bmolgroup* molgroup)
1181 {
1182  long n;
1183  Bmolecule* mol;
1184  Bresidue* res;
1185  Batom* atom;
1186  vector<Batom*> atomarray;
1187 
1188  for ( n=0, mol = molgroup->mol; mol; mol = mol->next ) if ( mol->sel )
1189  for( res = mol->res; res; res = res->next )
1190  for ( atom = res->atom; atom; atom = atom->next, n++ )
1191  atomarray.push_back(atom);
1192 
1193 
1194  return atomarray;
1195 }
1196 
Vector3::square_root
Vector3 square_root()
Definition: Vector3.h:229
Batomtype::vdw
float vdw
Definition: rwatomprop.h:27
JSvalue::object_begin
map< string, JSvalue >::iterator object_begin()
Definition: json.h:230
molgroup_principal_axes
Vector3< double > molgroup_principal_axes(Bmolgroup *molgroup, Vector3< double > *eigenvec)
Calculates the principal axes of a molecule group.
Definition: mol_util.cpp:455
molgroup_stats
long molgroup_stats(Bmolgroup *molgroup, int show)
Calculates the statistics of a molecule group.
Definition: rwmolecule.cpp:1296
RHO
#define RHO
Definition: utilities.h:95
vector3_difference_PBC
Vector3< T1 > vector3_difference_PBC(Vector3< T1 > &v1, Vector3< T1 > &v2, Vector3< T2 > &box)
Definition: Vector3.h:697
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
molgroup_print_sequence
int molgroup_print_sequence(Bmolgroup *molgroup)
Prints the sequence of a molecule as a single letter code string.
Definition: mol_util.cpp:92
kill_list
long kill_list(char *list, unsigned long size)
Frees all the items in a linked list.
Definition: linked_list.cpp:214
molgroup_selected_center_of_mass
Vector3< double > molgroup_selected_center_of_mass(Bmolgroup *molgroup)
Calculates the center of mass of a molecule group.
Definition: mol_util.cpp:347
Bresidue_type::c
char c
Definition: rwresprop.h:24
Bstring
Definition: Bstring.h:29
add_item
char * add_item(char **list, unsigned long size)
Adds an item to a linked list.
Definition: linked_list.cpp:26
molgroup_sphericity
double molgroup_sphericity(Bmolgroup *molgroup, double da)
Calculates how close the coordinate set is to a spherical shape.
Definition: mol_util.cpp:545
Bresidue::atom
Batom * atom
Definition: rwmolecule.h:54
Bresidue_type::next
Bresidue_type * next
Definition: rwresprop.h:23
Batom::num
int num
Definition: rwmolecule.h:26
JSvalue::real
double & real()
Definition: json.h:149
Bmolgroup::mol
Bmolecule * mol
Definition: rwmolecule.h:189
Vector3< double >
molgroup_set_radius
int molgroup_set_radius(Bmolgroup *molgroup, double radius)
Shifts all coordinates to a given radius.
Definition: mol_util.cpp:114
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
Batom::tnum
int tnum
Definition: rwmolecule.h:29
molgroup_rename
int molgroup_rename(Bmolgroup *molgroup, char first_name)
Renames molecules.
Definition: mol_util.cpp:161
Batom::el
char el[4]
Definition: rwmolecule.h:27
Batomtype
Definition: rwatomprop.h:19
molgroup_center_of_mass
Vector3< double > molgroup_center_of_mass(Bmolgroup *molgroup)
Calculates the center of mass of a molecule group.
Definition: mol_util.cpp:307
mol_center_of_mass
Vector3< double > mol_center_of_mass(Bmolecule *mol)
Calculates the center of mass of a molecule.
Definition: mol_util.cpp:401
principal_axes
Vector3< double > principal_axes(Vector3< double > avg, Vector3< double > avg2, Vector3< double > avgx, Vector3< double > *eigenvec)
Calculates the principal axes of 3D coordinates.
Definition: matrix.cpp:706
Batom::q
double q
Definition: rwmolecule.h:31
Bmolecule::nres
long nres
Definition: rwmolecule.h:111
get_atom_properties
Batomtype * get_atom_properties(Bstring &filename)
Reading atomic properties from parameter files.
Definition: rwatomprop.cpp:47
molgroup_elements
JSvalue molgroup_elements(Bmolgroup *molgroup)
Calculates the elemental composition.
Definition: mol_util.cpp:892
length
double length(vector< T > v)
Definition: Vector3.h:530
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
get_residue_properties
Bresidue_type * get_residue_properties(Bstring &filename)
Reading residue properties from parameter files.
Definition: rwresprop.cpp:99
Batom::next
Batom * next
Definition: rwmolecule.h:25
Bresidue::num
int num
Definition: rwmolecule.h:51
rwresprop.h
Header file for reading residue property files.
Bmolecule::res
Bresidue * res
Definition: rwmolecule.h:122
Bmolecule::seq
Bstring seq
Definition: rwmolecule.h:114
Latom::atom
Batom * atom
Definition: mol_util.h:24
molgroup_Bfactors
int molgroup_Bfactors(Bmolgroup *molgroup)
Calculates the average Bfactors for residues in molecules.
Definition: mol_util.cpp:28
Vector3::normalize
double normalize()
Definition: Vector3.h:238
protein_composition_default
JSvalue protein_composition_default(double mass)
Default protein composition adjusted by mass.
Definition: mol_util.cpp:991
rwmolecule.h
Header file for reading reflection files.
JSvalue::exists
bool exists(string tag)
Definition: json.h:221
rwatomprop.h
Header file for reading atom property files.
Bmolecule::next
Bmolecule * next
Definition: rwmolecule.h:108
Bmolecule
Definition: rwmolecule.h:107
Bmolgroup::box
Vector3< double > box
Definition: rwmolecule.h:184
volume
T volume(vector< T > v)
Definition: Vector3.h:547
Batom::type
char type[8]
Definition: rwmolecule.h:28
JSvalue
Definition: json.h:53
Bresidue_type
Definition: rwresprop.h:22
molgroup_calculate_weight
double molgroup_calculate_weight(Bmolgroup *molgroup)
Calculates the weight of each molecule.
Definition: mol_util.cpp:270
Latom
Definition: mol_util.h:22
Batomtype::z
int z
Definition: rwatomprop.h:21
Batom::b
double b
Definition: rwmolecule.h:31
molgroup_residue_renumber
int molgroup_residue_renumber(Bmolgroup *molgroup, int first)
Renumbers residues.
Definition: mol_util.cpp:182
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
JSvalue::object_end
map< string, JSvalue >::iterator object_end()
Definition: json.h:231
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
Bmolgroup::max
Vector3< double > max
Definition: rwmolecule.h:183
atom_get_array
vector< Batom * > atom_get_array(Bmolgroup *molgroup)
Generates an array of pointers to atom structures.
Definition: mol_util.cpp:1180
molgroup_composition
int molgroup_composition(Bmolgroup *molgroup, Bstring &paramfile)
Calculates the elemental composition of a molecular system.
Definition: mol_util.cpp:817
molgroup_radial_density
int molgroup_radial_density(Bmolgroup *molgroup, double interval, double cutoff, int wrap)
Calculates a radial distribution function.
Definition: mol_util.cpp:1030
seq_util.h
Header file for sequence utilities.
Vector3::volume
double volume()
Definition: Vector3.h:237
Bmolgroup::min
Vector3< double > min
Definition: rwmolecule.h:182
molgroup_density
double molgroup_density(Bmolgroup *molgroup)
Calculates the number and mass density of a molecular system.
Definition: mol_util.cpp:666
Bresidue::type
char type[6]
Definition: rwmolecule.h:53
Batom::sel
int sel
Definition: rwmolecule.h:34
JSobject
@ JSobject
Definition: json.h:26
molgroup_atom_mesh_lists
Latom ** molgroup_atom_mesh_lists(Bmolgroup *molgroup, Vector3< int > size, Vector3< double > sampling)
Generates lists of atoms based on a grid.
Definition: mol_util.cpp:1132
Matrix.h
Generalized matrix class.
Bresidue::insert
char insert[2]
Definition: rwmolecule.h:52
mol_principal_axes
Vector3< double > mol_principal_axes(Bmolecule *mol, Vector3< double > *eigenvec)
Calculates the principal axes of a molecule.
Definition: mol_util.cpp:500
Bresidue
Definition: rwmolecule.h:49
linked_list.h
Header file for generalized linked list functions.
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
Vector3::min
Type min()
Definition: Vector3.h:169
Bmolgroup
Definition: rwmolecule.h:170
molgroup_show_center_of_mass
Vector3< double > molgroup_show_center_of_mass(Bmolgroup *molgroup)
Calculates the center of mass of a molecule group.
Definition: mol_util.cpp:387
molgroup_volume
double molgroup_volume(Bmolgroup *molgroup, Bstring &paramfile, int wrap)
Calculates the volume of a molecular system.
Definition: mol_util.cpp:704
Vector3::length
double length()
Definition: Vector3.h:222
Bresidue_type::comp
float comp[5]
Definition: rwresprop.h:32
Bmolecule::sel
int sel
Definition: rwmolecule.h:120
mol_show_center_of_mass
Vector3< double > mol_show_center_of_mass(Bmolecule *mol)
Calculates the center of mass of a molecule.
Definition: mol_util.cpp:439
molgroup_coor_reset_occupancy
int molgroup_coor_reset_occupancy(Bmolgroup *molgroup, int range_first, int range_last, double occupancy)
Sets occupancies to new values..
Definition: mol_util.cpp:243
Bresidue_type::cod
char cod[3]
Definition: rwresprop.h:25
verbose
int verbose
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:110
Latom::next
Latom * next
Definition: mol_util.h:23
Vector3::length2
double length2()
Definition: Vector3.h:221
Batom::coord
Vector3< double > coord
Definition: rwmolecule.h:30
Bresidue_type::vol
float vol
Definition: rwresprop.h:27
molgroup_atom_renumber
int molgroup_atom_renumber(Bmolgroup *molgroup, int first)
Renumbers all atoms, disregarding molecule distinction.
Definition: mol_util.cpp:211
Batom
Definition: rwmolecule.h:24
Bmolecule::id
Bstring id
Definition: rwmolecule.h:110
TWOPI
#define TWOPI
Definition: utilities.h:72
mol_util.h
Library routines used for atomic coordinates.
Vector3::max
Type max()
Definition: Vector3.h:175