Bsoft  2.1.0
Bernard's software package
mol_edit.cpp
Go to the documentation of this file.
1 
9 #include "rwmolecule.h"
10 #include "rwatomprop.h"
11 #include "rwresprop.h"
12 #include "mol_transform.h"
13 #include "mol_edit.h"
14 #include "mol_util.h"
15 #include "seq_util.h"
16 #include "linked_list.h"
17 #include "Matrix3.h"
18 #include "random_numbers.h"
19 #include "utilities.h"
20 
21 // Declaration of global variables
22 extern int verbose; // Level of output to the screen
23 
30 {
31  long n(0);
32  Bmolecule* mol;
33  Bresidue* res;
34  Batom* atom;
35 
36  for ( mol = molgroup->mol; mol; mol = mol->next )
37  for( res = mol->res; res; res = res->next )
38  for ( atom = res->atom; atom; atom = atom->next )
39  strcpy(atom->type, atom->el);
40 
41  if ( verbose & VERB_PROCESS )
42  cout << "Atoms renamed : " << n << endl << endl;
43 
44  return n;
45 }
46 
53 {
54  long n(0);
55  Bmolecule* mol;
56  Bresidue* res;
57  Batom *atom, *atom2;
58 
59  for ( mol = molgroup->mol; mol; mol = mol->next )
60  for( res = mol->res; res; res = res->next )
61  for ( atom = atom2 = res->atom; atom; ) {
62  if ( atom->tnum == 0 ) {
63  if ( atom == atom2 ) {
64  atom2 = res->atom = atom->next;
65  delete atom;
66  atom = atom2;
67  } else {
68  atom2->next = atom->next;
69  delete atom;
70  atom = atom2->next;
71  }
72  n++;
73  } else {
74  atom2 = atom;
75  atom = atom2->next;
76  }
77  }
78 
79  if ( verbose & VERB_PROCESS )
80  cout << "Hydrogens removed: " << n << endl << endl;
81 
82  return n;
83 }
84 
85 bool bond_find(Bmolgroup* molgroup, Batom* atom1, Batom* atom2)
86 {
87  Bbond* bond;
88 
89  for ( bond = molgroup->bond; bond; bond = bond->next )
90  if ( bond->atom1 == atom1 && bond->atom2 == atom2 ) return 1;
91 
92  return 0;
93 }
94 
102 {
103  long n(0);
104  double d;
105  Bmolecule* mol;
106  Bresidue* res;
107  Batom* atom;
108  Latom* alist = NULL;
109  Latom* la = NULL;
110  Latom* la2 = NULL;
111 
112  if ( verbose )
113  cout << "Adding disulfide bonds:" << endl;
114 
115  for ( mol = molgroup->mol; mol; mol = mol->next ) {
116  for( res = mol->res; res; res = res->next ) if ( strstr(res->type, "CYS") ) {
117  for ( atom = res->atom; atom; atom = atom->next ) if ( strstr(atom->type, "SG" ) ) {
118  la = (Latom *) add_item((char **) &la, sizeof(Latom));
119  if ( !alist ) alist = la;
120  la->atom = atom;
121  n++;
122  }
123  }
124  }
125 
126  if ( verbose )
127  cout << "Cysteine sulfurs found: " << n << endl;
128 
129  for ( n=0, la = alist; la->next; la = la->next ) {
130  for ( la2 = la->next; la2; la2 = la2->next ) {
131  d = la->atom->coord.distance(la2->atom->coord);
132  if ( d <= distance ) {
133  if ( !bond_find(molgroup, la->atom, la2->atom) ) {
134  bond_add(&molgroup->bond, la->atom, la2->atom, d, 1);
135  if ( verbose )
136  cout << la->atom->num << tab << la2->atom->num << tab << d << endl;
137  n++;
138  }
139  }
140  }
141  }
142 
143  kill_list((char *) alist, sizeof(Latom));
144 
145  if ( verbose & VERB_PROCESS )
146  cout << "Disulfide bonds created: " << n << endl << endl;
147 
148  return n;
149 }
150 
158 Bmolgroup** molgroup_split_into_slices(Bmolgroup* molgroup, double slice_thickness, int& nslices)
159 {
160  molgroup_stats(molgroup);
161 
162  int i;
163  double min = molgroup->min[2];
164  char molname[20] = "";
165  char restype[20] = "UNK";
166 
167  Bmolecule* mol;
168  Bresidue* res;
169  Batom* atom;
170 
171  nslices = 1;
172  if ( slice_thickness < molgroup->max[2] - molgroup->min[2] ) {
173  min = slice_thickness*floor(molgroup->min[2]/slice_thickness);
174  nslices = (int) ((molgroup->max[2] - min)/slice_thickness + 1);
175  }
176 
177  Bmolgroup** slice_molgroup = new Bmolgroup*[nslices];
178  Bmolecule* slice_mol;
179  Bresidue** slice_res = new Bresidue*[nslices];
180  Batom** slice_atom = new Batom*[nslices];
181 
182  for ( i=0; i<nslices; i++ ) {
183  slice_molgroup[i] = NULL;
184  slice_res[i] = NULL;
185  slice_atom[i] = NULL;
186  }
187 
188  if ( verbose )
189  cout << "System split into " << nslices << " slices" << endl << endl;
190 
191  for ( i=0; i<nslices; i++ ) {
192  snprintf(molname, 20, "%d", i+1);
193  slice_molgroup[i] = molgroup_init();
194  slice_mol = molecule_add(&slice_molgroup[i]->mol, molname);
195  slice_res[i] = residue_add(&slice_mol->res, restype);
196  slice_molgroup[i]->comment += molgroup->comment;
197  }
198 
199  for ( mol = molgroup->mol; mol; mol = mol->next ) {
200  for( res = mol->res; res; res = res->next ) {
201  for ( atom = res->atom; atom; atom = atom->next ) {
202  i = (int) ((atom->coord[2] - min)/slice_thickness);
203  if ( i < 0 || i >= nslices ) {
204  error_show("Error in molgroup_split_into_slices", __FILE__, __LINE__);
205  cerr << "Index out of range: " << i << endl;
206  return NULL;
207  }
208  if ( slice_atom[i] )
209  slice_atom[i] = atom_add(&slice_atom[i], atom->type);
210  else
211  slice_atom[i] = atom_add(&slice_res[i]->atom, atom->type);
212  memcpy(slice_atom[i], atom, sizeof(Batom));
213  slice_atom[i]->next = NULL;
214  }
215  }
216  }
217 
218  delete[] slice_res;
219  delete[] slice_atom;
220 
221  for ( i=0; i<nslices; i++ ) {
222  if ( verbose & VERB_PROCESS )
223  cout << "Slice " << i+1 << ":" << endl;
224  molgroup_stats(slice_molgroup[i]);
225  }
226 
227  return slice_molgroup;
228 }
229 
244 int molgroup_insert(Bmolgroup* molgroup, Bmolgroup* molinsert, double distance)
245 {
246  if ( distance <= 0 ) distance = 2; // Default
247  if ( distance < 1 ) distance = 1; // Limits on cutoff distance in angstrom
248  if ( distance > 5 ) distance = 5;
249 
250  long i;
251  int delete_res, nresdel(0), nmoldel(0);
252  long ii, x, y, z, xx, yy, zz, ix, iy, iz;
254  Bmolecule* mol, *prevmol;
255  Bresidue* res, *prevres;
256  Batom* atom;
257  Vector3<double> box = molgroup->box;
258  Vector3<int> gridsize((int) (box[0]/sampling[0] + 0.001),
259  (int) (box[1]/sampling[1] + 0.001), (int) (box[2]/sampling[2] + 0.001));
260  gridsize = gridsize.max(1);
261  for ( i=0; i<3; i++ ) sampling[i] = box[i]/gridsize[i] + 0.001;
262  long gridvol = (long) gridsize.volume();
263 
264  if ( verbose )
265  cout << "Inserting a molecule group and deleting overlapping residues" << endl;
266 
267  if ( verbose & VERB_PROCESS )
268  cout << "Distance cutoff: " << distance << " A" << endl;
269 
270  Latom* latom;
271  Latom** alist = molgroup_atom_mesh_lists(molinsert, gridsize, sampling);
272 
273  // Find the atoms under the footprint to be deleted
274  for ( mol = prevmol = molgroup->mol; mol; ) {
275  for( res = prevres = mol->res; res; ) {
276  delete_res = 0;
277  for ( atom = res->atom; atom; atom = atom->next ) {
278  x = (long) ((atom->coord[0] - molgroup->min[0])/sampling[0]);
279  y = (long) ((atom->coord[1] - molgroup->min[1])/sampling[1]);
280  z = (long) ((atom->coord[2] - molgroup->min[2])/sampling[2]);
281  if ( x >=0 && x < gridsize[0] && y >= 0 && y < gridsize[1] && z >= 0 && z < gridsize[2] ) {
282  i = (z*gridsize[1] + y)*gridsize[0] + x;
283  for ( zz=z-1; zz<=z+1; zz++ ) {
284  iz = zz;
285  if ( iz < 0 ) iz += gridsize[2];
286  if ( iz >= gridsize[2]) iz -= gridsize[2];
287  for ( yy=y-1; yy<=y+1; yy++ ) {
288  iy = yy;
289  if ( iy < 0 ) iy += gridsize[1];
290  if ( iy >= gridsize[1] ) iy -= gridsize[1];
291  for ( xx=x-1; xx<=x+1; xx++ ) {
292  ix = xx;
293  if ( ix < 0 ) ix += gridsize[0];
294  if ( ix >= gridsize[0] ) ix -= gridsize[0];
295  ii = (iz*gridsize[1] + iy)*gridsize[0] + ix;
296  for ( latom = alist[ii]; latom; latom = latom->next ) {
297  if ( atom->coord.distance(latom->atom->coord) < distance )
298  delete_res = 1;
299  }
300  }
301  }
302  }
303  }
304  }
305  if ( delete_res ) {
306  if ( verbose & VERB_FULL )
307  cout << "Removing a residue" << endl;
308  if ( res == mol->res ) {
309  mol->res = prevres = res->next;
310  residue_kill(res);
311  res = mol->res;
312  } else {
313  prevres->next = res->next;
314  residue_kill(res);
315  res = prevres->next;
316  }
317  nresdel++;
318  } else {
319  prevres = res;
320  if ( res ) res = res->next;
321  }
322  }
323  if ( !mol->res ) {
324  if ( verbose & VERB_FULL )
325  cout << "Removing a molecule" << endl;
326  if ( molgroup->mol == mol ) {
327  molgroup->mol = prevmol = mol->next;
328  molecule_kill(mol);
329  mol = molgroup->mol;
330  } else {
331  prevmol->next = mol->next;
332  molecule_kill(mol);
333  mol = prevmol->next;
334  }
335  nmoldel++;
336  } else {
337  prevmol = mol;
338  if ( mol ) mol = mol->next;
339  }
340  }
341 
342  for ( i=0; i<gridvol; i++ ) kill_list((char *) alist[i], sizeof(Latom));
343  delete[] alist;
344 
345  if ( verbose & VERB_PROCESS )
346  cout << "Molecules and residues deleted: " << nmoldel << " " << nresdel << endl;
347 
348  // Add the new molecules
349  if ( molgroup->mol ) {
350  for ( mol = molgroup->mol; mol->next; mol = mol->next ) ;
351  mol->next = molinsert->mol;
352  } else molgroup->mol = molinsert->mol;
353  molinsert->mol = NULL;
354 
355  if ( verbose & VERB_PROCESS )
356  molgroup_density(molgroup) ;
357 
358  molgroup_stats(molgroup);
359 
360  return 0;
361 }
362 
369 int molgroup_randomize(Bmolgroup* molgroup, double random_max)
370 {
371  if ( random_max < 1e-30 ) return 0;
372 
373  random_seed();
374 
375  Bmolecule* mol;
376  Bresidue* res;
377  Batom* atom;
378 
379  double irm = random_max*2.0/get_rand_max();
380 
381  if ( verbose & VERB_PROCESS )
382  cout << "Randomizing coordinates to maximum: " << random_max << " angstrom" << endl << endl;
383 
384  for ( mol = molgroup->mol; mol; mol = mol->next ) {
385  for( res = mol->res; res; res = res->next ) {
386  for ( atom = res->atom; atom; atom = atom->next ) {
387  atom->coord[0] += irm*random() - random_max;
388  atom->coord[1] += irm*random() - random_max;
389  atom->coord[2] += irm*random() - random_max;
390  }
391  }
392  }
393 
394  return 0;
395 }
396 
403 int molgroup_randomize_B(Bmolgroup* molgroup, double B)
404 {
405  if ( B < 1e-30 ) return 0;
406 
407  random_seed();
408 
409  Bmolecule* mol;
410  Bresidue* res;
411  Batom* atom;
412 
413  extern double irm;
414  double stdev(sqrt(B));
415 // Vector3<double> v;
416 
417  if ( verbose & VERB_PROCESS )
418  cout << "Randomizing coordinates with Bfactor: " << B << " Ã…^2" << endl << endl;
419 
420  for ( mol = molgroup->mol; mol; mol = mol->next ) {
421  for( res = mol->res; res; res = res->next ) {
422  for ( atom = res->atom; atom; atom = atom->next ) {
423 // v = vector3_random_gaussian(0, stdev);
424  atom->coord += vector3_random_gaussian(0, stdev);
425  }
426  }
427  }
428 
429  return 0;
430 }
431 
444 int molgroup_remove_overlapping_atoms(Bmolgroup* molgroup, double mindist)
445 {
446  if ( mindist < 0.001 ) return 0;
447 
448  int nrem(0);
449  double dist;
450  Bmolecule *mol, *mol2, *mol3;
451  Bresidue *res, *res2, *res3;
452  Batom *atom, *atom2, *atom3;
453 
454  if ( verbose & VERB_DEBUG )
455  cout << "DEBUG molgroup_remove_overlapping_atoms: mindist=" << mindist << endl;
456 
457  for ( mol = molgroup->mol; mol && mol->next; mol = mol->next ) {
458  for( res = mol->res; res; res = res->next ) {
459  for ( atom = res->atom; atom; atom = atom->next ) {
460  for ( mol2 = mol3 = mol; mol2; mol2 = mol2 ? mol2->next : NULL ) {
461  for( res2 = res3 = mol2->res; res2; res2 = res2 ? res2->next : NULL ) {
462  for ( atom2 = atom3 = res2->atom; atom2; atom2 = atom2 ? atom2->next : NULL ) {
463  if ( atom != atom2 ) {
464  dist = atom->coord.distance(atom2->coord);
465  if ( dist < mindist ) {
466  if ( atom2 == res2->atom ) {
467  res2->atom = atom2->next;
468  delete atom2;
469  atom2 = res2->atom;
470  } else {
471  atom3->next = atom2->next;
472  delete atom2;
473  atom2 = atom3;
474  }
475  nrem++;
476  }
477  }
478  atom3 = atom2;
479  }
480  if ( !res2->atom ) {
481  if ( res2 == mol2->res ) {
482  mol2->res = res2->next;
483  residue_kill(res2);
484  res2 = mol2->res;
485  } else {
486  res3->next = res2->next;
487  residue_kill(res2);
488  res2 = res3;
489  }
490  }
491  res3 = res2;
492  }
493  if ( !mol2->res ) {
494  if ( mol2 == molgroup->mol ) {
495  molgroup->mol = mol2->next;
496  molecule_kill(mol2);
497  mol2 = molgroup->mol;
498  } else {
499  mol3->next = mol2->next;
500  molecule_kill(mol2);
501  mol2 = mol3;
502  }
503  }
504  mol3 = mol2;
505  }
506  }
507  }
508  }
509 
510  if ( molgroup->mol == NULL ) {
511  error_show("Error in molgroup_remove_overlapping_atoms: All molecules deleted!", __FILE__, __LINE__);
512  return -1;
513  }
514 
515  if ( verbose & VERB_PROCESS )
516  cout << "Overlapping atoms removed: " << nrem << endl << endl;
517 
518  molgroup_stats(molgroup);
519 
520  return nrem;
521 }
522 
533 int molgroup_bond_pseudo_atoms(Bmolgroup* molgroup, int atoms_per_bond, int wrap)
534 {
535  int i;
536  Vector3<double> v;
537  Bresidue* res = residue_add(&molgroup->mol->res, "PSD");
538  Batom* atom;
539  Bbond* bond;
540 
541  for ( bond=molgroup->bond; bond; bond=bond->next ) {
542  if ( wrap ) v = vector3_difference_PBC(bond->atom2->coord, bond->atom1->coord, molgroup->box);
543  else v = bond->atom2->coord - bond->atom1->coord;
544  for ( i=1; i<=atoms_per_bond; i++ ) {
545  atom = atom_add(&res->atom, "A");
546  atom->coord = bond->atom1->coord + (v * (i*1.0L/(atoms_per_bond+1)));
547  if ( wrap ) atom->coord = vector3_set_PBC(atom->coord, molgroup->box);
548  }
549  }
550 
551  return 0;
552 }
553 
554 char* grid_from_molgroup(Bmolgroup* molgroup, Vector3<double> min, double sampling, Vector3<int> size)
555 {
556  Bmolecule* mol;
557  Bresidue* res;
558  Batom* atom;
559  long x, y, z;
560  long i, vol = (long)size.volume();
561 
562  char* grid = new char[vol];
563  for ( i=0; i<vol; i++ ) grid[i] = 0;
564 
565  for ( mol = molgroup->mol; mol; mol = mol->next ) {
566  for( res = mol->res; res; res = res->next ) {
567  for ( atom = res->atom; atom; atom = atom->next ) {
568  x = (long) ((atom->coord[0] - min[0])/sampling);
569  y = (long) ((atom->coord[1] - min[1])/sampling);
570  z = (long) ((atom->coord[2] - min[2])/sampling);
571  grid[(z*size[1] + y)*size[0] + x] = 1;
572  }
573  }
574  }
575 
576  return grid;
577 }
578 
579 char* grid_from_molecule(Bmolecule* mol, Vector3<double> min, double sampling, Vector3<int> size)
580 {
581  Bresidue* res;
582  Batom* atom;
583  long x, y, z;
584  long i, vol = (long)size.volume();
585 
586  char* grid = new char[vol];
587  for ( i=0; i<vol; i++ ) grid[i] = 0;
588 
589  for( res = mol->res; res; res = res->next ) {
590  for ( atom = res->atom; atom; atom = atom->next ) {
591  x = (long) ((atom->coord[0] - min[0])/sampling);
592  y = (long) ((atom->coord[1] - min[1])/sampling);
593  z = (long) ((atom->coord[2] - min[2])/sampling);
594  grid[(z*size[1] + y)*size[0] + x] = 1;
595  }
596  }
597 
598  return grid;
599 }
600 
607 {
608  Bmolecule* mol;
609 
610  long i;
611  double sampling = 4;
612  Vector3<int> size;
613  size[0] = (int) (molgroup->box[0]/sampling + 1);
614  size[1] = (int) (molgroup->box[1]/sampling + 1);
615  size[2] = (int) (molgroup->box[2]/sampling + 1);
616  long vol = (long)size.volume();
617 
618  char* grid1 = NULL;
619  char* grid = grid_from_molecule(molgroup->mol, molgroup->min, sampling, size);
620  molgroup->mol->sel = 1;
621 
622  for ( mol = molgroup->mol->next; mol; mol = mol->next ) {
623  mol->sel = 0;
624  grid1 = grid_from_molecule(mol, molgroup->min, sampling, size);
625  for ( i=0; i<vol; i++ ) {
626  if ( grid1[i] ) {
627  if ( grid[i] ) mol->sel++;
628  else grid[i] = 1;
629  }
630  }
631  delete[] grid1;
632  if ( mol->sel ) mol->sel = 0;
633  else mol->sel = 1;
634  }
635 
636  delete[] grid;
637 
639 
640  return 0;
641 }
642 
649 {
650  long nmol(0), ndel(0);
651  Bmolecule* mol, *mol2;
652 
653  for ( mol = mol2 = molgroup->mol; mol; ) {
654  if ( mol->sel < 1 ) {
655  if ( verbose & VERB_FULL )
656  cout << "Deleting molecule " << mol->id << endl;
657  if ( mol == molgroup->mol ) {
658  molgroup->mol = mol->next;
659  molecule_kill(mol);
660  mol = mol2 = molgroup->mol;
661  } else {
662  mol2->next = mol->next;
663  molecule_kill(mol);
664  mol = mol2->next;
665  }
666  ndel++;
667  } else {
668  mol2 = mol;
669  mol = mol->next;
670  }
671  nmol++;
672  }
673 
674  if ( verbose )
675  cout << "Molecules deleted: " << ndel << " (" << ndel*100.0/nmol << " %)" << endl << endl;
676 
677  return ndel;
678 }
679 
689 long molgroup_prune_overlapping_atoms(Bmolgroup* molgroup, double mindist)
690 {
691 
692  vector<Batom*> atomarray = atom_get_array(molgroup);
693 
694  long i, j, natom(atomarray.size()), ndel(0);
695 
696  for ( i=0; i<natom-1; i++ ) atomarray[i]->sel = 1;
697 
698  for ( i=0; i<natom-1; i++ ) if ( atomarray[i]->sel ) {
699  for ( j=i+1; j<natom; j++ ) if ( atomarray[j]->sel ) {
700  if ( atomarray[i]->coord.distance(atomarray[j]->coord) < mindist ) {
701  atomarray[j]->sel = 0;
702  ndel++;
703  }
704  }
705  }
706 
707  if ( verbose )
708  cout << "Atoms marked for deletion: " << ndel << " (" << ndel*100.0/natom << ")" << endl << endl;
709 
710  ndel = molgroup_delete_deselected_atoms(molgroup);
711 
712  return natom - ndel;
713 }
714 
721 {
722  long natom(0), ndel(0);
723  Bmolecule* mol, *mol2;
724  Bresidue* res, *res2;
725  Batom* atom, *atom2;
726 
727  for ( mol = mol2 = molgroup->mol; mol; ) {
728  for( res = res2 = mol->res; res; ) {
729  for ( atom = atom2 = res->atom; atom; ) {
730  if ( atom->sel < 1 ) {
731  if ( verbose & VERB_FULL )
732  cout << "Deleting atom " << atom->num << endl;
733  if ( atom == res->atom ) {
734  res->atom = atom->next;
735  delete atom;
736  atom = atom2 = res->atom;
737  } else {
738  atom2->next = atom->next;
739  delete atom;
740  atom = atom2->next;
741  }
742  ndel++;
743  } else {
744  atom2 = atom;
745  atom = atom->next;
746  }
747  natom++;
748  }
749  if ( !res->atom ) {
750  if ( verbose & VERB_FULL )
751  cout << "Deleting residue " << res->num << endl;
752  if ( res == mol->res ) {
753  mol->res = res->next;
754  residue_kill(res);
755  res = res2 = mol->res;
756  } else {
757  res2->next = res->next;
758  residue_kill(res);
759  res = res2->next;
760  }
761  } else {
762  res2 = res;
763  res = res->next;
764  }
765  }
766  if ( !mol->res ) {
767  if ( verbose & VERB_FULL )
768  cout << "Deleting molecule " << mol->id << endl;
769  if ( mol == molgroup->mol ) {
770  molgroup->mol = mol->next;
771  molecule_kill(mol);
772  mol = mol2 = molgroup->mol;
773  } else {
774  mol2->next = mol->next;
775  molecule_kill(mol);
776  mol = mol2->next;
777  }
778  } else {
779  mol2 = mol;
780  mol = mol->next;
781  }
782  }
783 
784  if ( verbose )
785  cout << "Atoms deleted: " << ndel << " (" << ndel*100.0/natom << ")" << endl << endl;
786 
787  return ndel;
788 }
789 
802 int molgroup_untangle_molecules(Bmolgroup* molgroup, double sampling, double lambda)
803 {
804  Bmolecule *mol, *mol2;
805 
806  long iter, i, x, y, z, m, m2, nmol;
807  double d, dmax, all_shift = 1;
808  Vector3<int> size;
809  Vector3<double> com, com2, u, c, v;
810  char *grid, *grid2;
811 
812  for ( nmol=0, mol = molgroup->mol; mol; mol = mol->next ) nmol++;
813 
814  if ( verbose )
815  cout << "Untangling " << nmol << " molecules" << endl;
816 
817  Vector3<double>* F = new Vector3<double>[nmol];
818 
819  for ( iter=1; all_shift > 0 && iter < 100; iter++ ) {
820  all_shift = 0;
821  size[0] = (int) (molgroup->box[0]/sampling + 1);
822  size[1] = (int) (molgroup->box[1]/sampling + 1);
823  size[2] = (int) (molgroup->box[2]/sampling + 1);
824  for ( m=0, mol = molgroup->mol; mol->next; mol = mol->next, m++ ) {
825  com = mol_center_of_mass(mol);
826  grid = grid_from_molecule(mol, molgroup->min, sampling, size);
827  for ( m2=m+1, mol2 = mol->next; mol2; mol2 = mol2->next, m2++ ) {
828  com2 = mol_center_of_mass(mol2);
829  grid2 = grid_from_molecule(mol2, molgroup->min, sampling, size);
830  u = com2 - com;
831  u.normalize();
832  c = (com + com2)/2;
833  dmax = 0;
834  for ( i=z=0; z<size[2]; z++ ) {
835  for ( y=0; y<size[1]; y++ ) {
836  for ( x=0; x<size[0]; x++, i++ ) {
837  if ( grid[i] & grid2[i] ) {
838  v = Vector3<double>(x, y, z);
839  v = (v * sampling + molgroup->min - c);
840  d = v.scalar(u);
841 // d = fabs(v.scalar(u));
842  if ( dmax < d ) dmax = d;
843  }
844  }
845  }
846  }
847  delete[] grid2;
848  if ( dmax ) {
849  dmax *= lambda;
850  v = u * dmax;
851  F[m] -= v;
852  F[m2] += v;
853  all_shift += dmax;
854  }
855  }
856  delete[] grid;
857  }
858  for ( m=0, mol = molgroup->mol; mol; mol = mol->next, m++ ) {
859  mol_coor_shift(mol, F[m]);
860  F[m] = 0;
861  }
862  molgroup_stats(molgroup);
863  molgroup->box = molgroup->max - molgroup->min;
864  if ( verbose )
865  cout << iter << tab << all_shift << endl;
866  }
867 
868  delete[] F;
869 
870  return 0;
871 }
872 
885 int molgroup_untangle_groups(Bmolgroup* molgroup, double sampling, double lambda)
886 {
887  Bmolgroup *mg, *mg2;
888 
889  long iter, i, x, y, z, m, m2, nmg;
890  double d, dmax, all_shift = 1;
891  Vector3<int> size;
892  Vector3<double> min, max, box, com, com2, u, c, v;
893  char *grid, *grid2;
894 
895  for ( nmg=0, mg = molgroup; mg; mg = mg->next ) nmg++;
896 
897  if ( verbose ) {
898  cout << "Untangling " << nmg << " molecule groups" << endl;
899  cout << "Sampling: " << sampling << " A" << endl;
900  cout << "Damping factor: " << lambda << endl;
901  }
902 
903  Vector3<double>* F = new Vector3<double>[nmg];
904 
905  if ( verbose )
906  cout << "Iter\tShift" << endl;
907  for ( iter=1; all_shift > 0 && iter < 100; iter++ ) {
908  all_shift = 0;
909  min = molgroup->min;
910  max = molgroup->max;
911  for ( mg = molgroup; mg; mg = mg->next ) {
912  min = min.min(mg->min);
913  max = max.max(mg->max);
914  }
915  box = max - min;
916  size[0] = (int) (box[0]/sampling + 1);
917  size[1] = (int) (box[1]/sampling + 1);
918  size[2] = (int) (box[2]/sampling + 1);
919  for ( m=0, mg = molgroup; mg->next; mg = mg->next, m++ ) {
920  com = molgroup_center_of_mass(mg);
921  grid = grid_from_molgroup(mg, min, sampling, size);
922  for ( m2=m+1, mg2 = mg->next; mg2; mg2 = mg2->next, m2++ ) {
923  com2 = molgroup_center_of_mass(mg2);
924  grid2 = grid_from_molgroup(mg2, min, sampling, size);
925  u = com2 - com;
926  u.normalize();
927  c = (com + com2)/2;
928  dmax = 0;
929  for ( i=z=0; z<size[2]; z++ ) {
930  for ( y=0; y<size[1]; y++ ) {
931  for ( x=0; x<size[0]; x++, i++ ) {
932  if ( grid[i] & grid2[i] ) {
933  v = Vector3<double>(x, y, z);
934  v = (v * sampling + min - c);
935  d = v.scalar(u);
936 // d = fabs(v.scalar(u));
937  if ( dmax < d ) dmax = d;
938  }
939  }
940  }
941  }
942  delete[] grid2;
943  if ( dmax ) {
944  dmax *= lambda;
945  v = u * dmax;
946  F[m] -= v;
947  F[m2] += v;
948  all_shift += dmax;
949  }
950  }
951  delete[] grid;
952  }
953  for ( m=0, mg = molgroup; mg; mg = mg->next, m++ ) {
954  if ( F[m].length() ) molgroup_coor_shift(mg, F[m]);
955  F[m] = 0;
956  molgroup_stats(mg);
957  mg->box = mg->max - mg->min;
958  }
959  if ( verbose )
960  cout << iter << tab << all_shift << endl;
961  }
962 
963  delete[] F;
964 
965  return 0;
966 }
molgroup_init
Bmolgroup * molgroup_init()
Initializes and allocates a new molecule group.
Definition: rwmolecule.cpp:45
molgroup_stats
long molgroup_stats(Bmolgroup *molgroup, int show)
Calculates the statistics of a molecule group.
Definition: rwmolecule.cpp:1296
molgroup_untangle_molecules
int molgroup_untangle_molecules(Bmolgroup *molgroup, double sampling, double lambda)
Moves overlapping molecules away from each other.
Definition: mol_edit.cpp:802
atom_add
Batom * atom_add(Batom **atom, const char *type)
Adds an atom to a linked list.
Definition: rwmolecule.cpp:152
residue_add
Bresidue * residue_add(Bresidue **res, const char *type)
Adds a residue to a linked list.
Definition: rwmolecule.cpp:113
vector3_difference_PBC
Vector3< T1 > vector3_difference_PBC(Vector3< T1 > &v1, Vector3< T1 > &v2, Vector3< T2 > &box)
Definition: Vector3.h:697
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
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Bresidue::next
Bresidue * next
Definition: rwmolecule.h:50
residue_kill
int residue_kill(Bresidue *res)
Destroys a residue.
Definition: rwmolecule.cpp:453
kill_list
long kill_list(char *list, unsigned long size)
Frees all the items in a linked list.
Definition: linked_list.cpp:214
Bbond::next
Bbond * next
Definition: rwmolecule.h:135
add_item
char * add_item(char **list, unsigned long size)
Adds an item to a linked list.
Definition: linked_list.cpp:26
molgroup_delete_deselected_molecules
long molgroup_delete_deselected_molecules(Bmolgroup *molgroup)
Deletes deselected molecules.
Definition: mol_edit.cpp:648
vector3_random_gaussian
Vector3< double > vector3_random_gaussian(double avg, double std)
Generates a random vector within a random gaussian-distributed length.
Definition: random_numbers.cpp:502
Bbond::atom1
Batom * atom1
Definition: rwmolecule.h:136
Bresidue::atom
Batom * atom
Definition: rwmolecule.h:54
Batom::num
int num
Definition: rwmolecule.h:26
Bmolgroup::mol
Bmolecule * mol
Definition: rwmolecule.h:189
Vector3< double >
random_numbers.h
Header file for functions for creating random images.
Batom::tnum
int tnum
Definition: rwmolecule.h:29
Matrix3.h
3x3 Matrix manipulation functions
molgroup_remove_overlapping_atoms
int molgroup_remove_overlapping_atoms(Bmolgroup *molgroup, double mindist)
Removes overlapping atoms.
Definition: mol_edit.cpp:444
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:719
Batom::el
char el[4]
Definition: rwmolecule.h:27
molgroup_split_into_slices
Bmolgroup ** molgroup_split_into_slices(Bmolgroup *molgroup, double slice_thickness, int &nslices)
Splits a set of coordinates into slices with a given thickness.
Definition: mol_edit.cpp:158
mol_edit.h
Library routines used for atomic coordinates.
atom_get_array
vector< Batom * > atom_get_array(Bmolgroup *molgroup)
Generates an array of pointers to atom structures.
Definition: mol_util.cpp:1180
Bbond::atom2
Batom * atom2
Definition: rwmolecule.h:137
molgroup_bond_pseudo_atoms
int molgroup_bond_pseudo_atoms(Bmolgroup *molgroup, int atoms_per_bond, int wrap)
Places pseudo-atoms on bonds.
Definition: mol_edit.cpp:533
Vector3::scalar
double scalar(const Vector3 &v) const
Definition: Vector3.h:249
length
double length(vector< T > v)
Definition: Vector3.h:530
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
mol_transform.h
Library routines used for atomic coordinate transformations.
mol_coor_shift
int mol_coor_shift(Bmolecule *mol, Vector3< double > shift)
Translates a molecule.
Definition: mol_transform.cpp:56
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
molgroup_add_disulfides
int molgroup_add_disulfides(Bmolgroup *molgroup, double distance)
Adds disulfide bonds when sulfur atoms are close to each other.
Definition: mol_edit.cpp:101
Latom::atom
Batom * atom
Definition: mol_util.h:24
Vector3::distance
double distance(const Vector3 &v)
Definition: Vector3.h:227
molgroup_set_atom_types_to_elements
long molgroup_set_atom_types_to_elements(Bmolgroup *molgroup)
Sets atom types to element names.
Definition: mol_edit.cpp:29
grid_from_molgroup
char * grid_from_molgroup(Bmolgroup *molgroup, Vector3< double > min, double sampling, Vector3< int > size)
Definition: mol_edit.cpp:554
Vector3::normalize
double normalize()
Definition: Vector3.h:238
molgroup_delete_deselected_atoms
long molgroup_delete_deselected_atoms(Bmolgroup *molgroup)
Deletes deselected atoms.
Definition: mol_edit.cpp:720
rwmolecule.h
Header file for reading reflection files.
molgroup_prune_molecules
int molgroup_prune_molecules(Bmolgroup *molgroup)
Deletes overlapping molecules.
Definition: mol_edit.cpp:606
Bbond
Definition: rwmolecule.h:134
rwatomprop.h
Header file for reading atom property files.
molgroup_untangle_groups
int molgroup_untangle_groups(Bmolgroup *molgroup, double sampling, double lambda)
Moves overlapping molecules away from each other.
Definition: mol_edit.cpp:885
Bmolecule::next
Bmolecule * next
Definition: rwmolecule.h:108
Bmolecule
Definition: rwmolecule.h:107
Bmolgroup::box
Vector3< double > box
Definition: rwmolecule.h:184
molgroup_prune_overlapping_atoms
long molgroup_prune_overlapping_atoms(Bmolgroup *molgroup, double mindist)
Prunes overlapping atoms based on a distance criterion.
Definition: mol_edit.cpp:689
Bmolgroup::bond
Bbond * bond
Definition: rwmolecule.h:190
Batom::type
char type[8]
Definition: rwmolecule.h:28
molgroup_randomize
int molgroup_randomize(Bmolgroup *molgroup, double random_max)
Applies random displacements to coordinates.
Definition: mol_edit.cpp:369
Bmolgroup::next
Bmolgroup * next
Definition: rwmolecule.h:171
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
distance
double distance(vector< T1 > &v1, vector< T2 > &v2)
Definition: Vector3.h:538
Latom
Definition: mol_util.h:22
vector3_set_PBC
Vector3< T1 > vector3_set_PBC(Vector3< T1 > coord, Vector3< T2 > box)
Definition: Vector3.h:684
molgroup_coor_shift
int molgroup_coor_shift(Bmolgroup *molgroup, Vector3< double > shift)
Translates a molecule group.
Definition: mol_transform.cpp:28
molgroup_insert
int molgroup_insert(Bmolgroup *molgroup, Bmolgroup *molinsert, double distance)
Inserts one set of molecules into another.
Definition: mol_edit.cpp:244
bond_find
bool bond_find(Bmolgroup *molgroup, Batom *atom1, Batom *atom2)
Definition: mol_edit.cpp:85
utilities.h
Header file for general utilities.
molgroup_density
double molgroup_density(Bmolgroup *molgroup)
Calculates the number and mass density of a molecular system.
Definition: mol_util.cpp:666
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
Bmolgroup::comment
Bstring comment
Definition: rwmolecule.h:175
molgroup_randomize_B
int molgroup_randomize_B(Bmolgroup *molgroup, double B)
Applies random displacements to coordinates.
Definition: mol_edit.cpp:403
molecule_kill
int molecule_kill(Bmolecule *mol)
Destroys a molecule.
Definition: rwmolecule.cpp:417
grid_from_molecule
char * grid_from_molecule(Bmolecule *mol, Vector3< double > min, double sampling, Vector3< int > size)
Definition: mol_edit.cpp:579
random_seed
long random_seed()
Gets a random seed and sets the flag.
Definition: random_numbers.cpp:41
Bmolgroup::max
Vector3< double > max
Definition: rwmolecule.h:183
bond_add
Bbond * bond_add(Bbond **bond, Batom *atom1, Batom *atom2, double l, double k)
Adds a bond to a linked list.
Definition: rwmolecule.cpp:269
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
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
Bresidue::type
char type[6]
Definition: rwmolecule.h:53
Batom::sel
int sel
Definition: rwmolecule.h:34
verbose
int verbose
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
irm
double irm(1.0L/get_rand_max())
Bmolgroup
Definition: rwmolecule.h:170
get_rand_max
long get_rand_max()
Finds the maximum random number for a system.
Definition: random_numbers.cpp:22
molgroup_remove_hydrogens
int molgroup_remove_hydrogens(Bmolgroup *molgroup)
Removes all hydrogens from a molecular system.
Definition: mol_edit.cpp:52
Bmolecule::sel
int sel
Definition: rwmolecule.h:120
Latom::next
Latom * next
Definition: mol_util.h:23
molecule_add
Bmolecule * molecule_add(Bmolecule **mol, char *name)
Adds a molecule to a linked list.
Definition: rwmolecule.cpp:76
Batom::coord
Vector3< double > coord
Definition: rwmolecule.h:30
Batom
Definition: rwmolecule.h:24
Bmolecule::id
Bstring id
Definition: rwmolecule.h:110
mol_util.h
Library routines used for atomic coordinates.
Vector3::max
Type max()
Definition: Vector3.h:175