Bsoft  2.1.0
Bernard's software package
mol_compare.cpp
Go to the documentation of this file.
1 
9 #include "rwmolecule.h"
10 #include "rwatomprop.h"
11 #include "mol_symmetry.h"
12 #include "mol_compare.h"
13 #include "mol_transform.h"
14 #include "mol_util.h"
15 #include "seq_align.h"
16 #include "Matrix3.h"
17 #include "matrix_linear.h"
18 #include "Matrix.h"
19 #include "symmetry.h"
20 #include "utilities.h"
21 
22 // Declaration of global variables
23 extern int verbose; // Level of output to the screen
24 
32 {
33  Bmolgroup* mol_rot;
34  double R;
35 
36  t.axis.normalize();
37  mol_rot = molgroup_copy(molgroup);
38  molgroup_coor_rotate(mol_rot, t);
39  R = molgroup_calc_brute_rmsd(molgroup, mol_rot);
40  molgroup_kill(mol_rot);
41 
42  return R;
43 }
44 
52 {
53  long i, j, n(0);
54 // double bx[4], by[4], bz[4], v[4] = {0,0,0,1};
55  vector<double> bx(4), by(4), bz(4), v(4,0);
56  Matrix a(4,4);
57 
58  v[3] = 1;
59 
60  for ( i=0; i<4; i++ ) bx[i] = by[i] = bz[i] = 0;
61 
62  Bmolecule *mol1, *mol2;
63  Bresidue *res1, *res2;
64  Batom *atom1, *atom2;
65 
67  Vector3<double> c1, c2;
68 
69  for ( n=0, mol1=molgroup1->mol, mol2=molgroup2->mol; mol1 && mol2; mol1=mol1->next, mol2=mol2->next ) {
70  for ( res1=mol1->res, res2=mol2->res; res1 && res2; res1=res1->next, res2=res2->next ) {
71  for ( atom1=res1->atom, atom2=res2->atom; atom1 && atom2; atom1=atom1->next, atom2=atom2->next ) {
72  if ( strncmp(atom1->el, atom2->el, 2) == 0 ) {
73  c1 = atom1->coord - com;
74  v[0] = c1[0];
75  v[1] = c1[1];
76  v[2] = c1[2];
77  c2 = atom2->coord - com;
78  for ( i=0; i<4; i++ ) {
79  bx[i] += v[i]*c2[0];
80  by[i] += v[i]*c2[1];
81  bz[i] += v[i]*c2[2];
82  for ( j=0; j<=i; j++ ) a[i][j] += v[i]*v[j];
83  }
84  n++;
85  }
86  }
87  }
88  }
89 
90  for ( i=0; i<3; i++ )
91  for ( j=i+1; j<4; j++ ) a[i][j] = a[j][i];
92 
93 // a.LU_decomposition();
95  a.multiply_in_place(bx);
96  a.multiply_in_place(by);
97  a.multiply_in_place(bz);
98 
99  Matrix3 mat(bx[0],bx[1],bx[2],by[0],by[1],by[2],bz[0],bz[1],bz[2]);
100 // mat = mat.transpose();
101  mat.normalize();
102  Transform t(mat);
103  t.origin = com;
104  t.trans = Vector3<double>(bx[3], by[3], bz[3]);
105 
106  if ( verbose & VERB_PROCESS ) {
107  cout << "Atoms compared: " << n << endl;
108  matrix3_show_hp(mat);
109  cout << endl;
110  }
111 
112  return t;
113 }
114 
140 {
141  long i, j, n(0);
142 // double bx[4], by[4], bz[4], v[4] = {0,0,0,1};
143  vector<double> bx(4), by(4), bz(4), v(4,0);
144  Matrix a(4,4);
145 
146  v[3] = 1;
147 
148  for ( i=0; i<4; i++ ) bx[i] = by[i] = bz[i] = 0;
149 
150  Batom *atom1, *atom2;
151  Bresidue* res1=mol1->res;
152  Bresidue* res2=mol2->res;
153  if ( offset < 0 ) for ( i=0; i<-offset; i++ ) res2=res2->next;
154  else for ( i=0; i<offset; i++ ) res1=res1->next;
155 
156  res1=mol1->res;
157  res2=mol2->res;
158  if ( offset < 0 ) for ( i=0; i<-offset; i++ ) res2=res2->next;
159  else for ( i=0; i<offset; i++ ) res1=res1->next;
160 
162  Vector3<double> c1, c2;
163 
164  for ( n=0; res1 && res2; res1=res1->next, res2=res2->next ) {
165  for ( atom1=res1->atom, atom2=res2->atom; atom1 && atom2; atom1=atom1->next, atom2=atom2->next ) {
166  if ( strncmp(atom1->el, atom2->el, 2) == 0 ) {
167  c1 = atom1->coord - com;
168  v[0] = c1[0];
169  v[1] = c1[1];
170  v[2] = c1[2];
171  c2 = atom2->coord - com;
172  for ( i=0; i<4; i++ ) {
173  bx[i] += v[i]*c2[0];
174  by[i] += v[i]*c2[1];
175  bz[i] += v[i]*c2[2];
176  for ( j=0; j<=i; j++ ) a[i][j] += v[i]*v[j];
177  }
178  n++;
179  }
180  }
181  }
182 
183  for ( i=0; i<3; i++ )
184  for ( j=i+1; j<4; j++ ) a[i][j] = a[j][i];
185 
186 // a.LU_decomposition();
188  a.multiply_in_place(bx);
189  a.multiply_in_place(by);
190  a.multiply_in_place(bz);
191 
192  Matrix3 mat(bx[0],bx[1],bx[2],by[0],by[1],by[2],bz[0],bz[1],bz[2]);
193 // Transform t = transform_from_matrix3(mat);
194  mat.normalize();
195  Transform t(mat);
196  t.origin = com;
197  t.trans = Vector3<double>(bx[3], by[3], bz[3]);
198 
199 // matrix3_show_hp(mat);
200 
201  return t;
202 }
203 
213 double molgroup_calculate_rmsd(Bmolgroup* molgroup1, Bmolgroup* molgroup2)
214 {
215  long n(0);
216  double d, ad(0), sd(0), R(0);
217  Bmolecule *mol1, *mol2;
218  Bresidue *res1, *res2;
219  Batom *atom1, *atom2;
220 
221 // if ( verbose & VERB_FULL )
222 // cout << "Atom\tDistance" << endl;
223  for ( mol1=molgroup1->mol, mol2=molgroup2->mol; mol1 && mol2; mol1=mol1->next, mol2=mol2->next ) {
224  for ( res1=mol1->res, res2=mol2->res; res1 && res2; res1=res1->next, res2=res2->next ) {
225  for ( atom1=res1->atom, atom2=res2->atom; atom1 && atom2; atom1=atom1->next, atom2=atom2->next ) {
226  if ( strncmp(atom1->el, atom2->el, 2) == 0 ) {
227  d = atom1->coord.distance2(atom2->coord);
228  sd += d;
229  d = sqrt(d);
230  ad += d;
231  n++;
232 // if ( verbose & VERB_FULL )
233 // cout << atom1->num << tab << d << endl;
234  }
235  }
236  }
237  }
238 
239  if ( n ) {
240  ad /= n;
241  sd /= n;
242  if ( sd > 0 ) R = sqrt(sd);
243  sd -= ad*ad;
244  if ( sd > 0 ) sd = sqrt(sd);
245  else sd = 0;
246  }
247 
248  Vector3<double> comd = molgroup_center_of_mass(molgroup2) - molgroup_center_of_mass(molgroup1);
249 
250  if ( verbose & VERB_PROCESS ) {
251  cout << "Number of comparisons: " << n << endl;
252  cout << "Center-of-mass difference: " << comd << " " << comd.length() << endl;
253  cout << "Distance avg & std: " << ad << " " << sd << " A" << endl;
254  cout << "RMSD: " << R << " A" << endl << endl;
255  }
256 
257  return R;
258 }
259 
271 {
272  long n(0);
273  double d, ad(0), sd(0), R(0);
274  Bresidue *res1, *res2;
275  Batom *atom1, *atom2;
276 
277  for ( res1=mol1->res, res2=mol2->res; res1 && res2; res1=res1->next, res2=res2->next ) {
278  for ( atom1=res1->atom, atom2=res2->atom; atom1 && atom2; atom1=atom1->next, atom2=atom2->next ) {
279  if ( strncmp(atom1->el, atom2->el, 2) == 0 ) {
280  d = atom1->coord.distance2(atom2->coord);
281  sd += d;
282  ad += sqrt(d);
283  n++;
284  }
285  }
286  }
287 
288  if ( n ) {
289  ad /= n;
290  sd /= n;
291  if ( sd > 0 ) R = sqrt(sd);
292  sd -= ad*ad;
293  if ( sd > 0 ) sd = sqrt(sd);
294  else sd = 0;
295  }
296 
298 
299  if ( verbose & VERB_FULL ) {
300  cout << "Center-of-mass difference: " << comd << " " << comd.length() << endl;
301  cout << "Distance avg & std: " << ad << " " << sd << " A" << endl;
302  cout << "RMSD: " << R << " A" << endl << endl;
303  }
304 
305  return R;
306 }
307 
314 double molgroup_calc_brute_rmsd(Bmolgroup* molgroup1, Bmolgroup* molgroup2)
315 {
316  Bmolecule *mol1, *mol2;
317  Bresidue *res1, *res2;
318  Batom *atom1, *atom2;
319  long n(0);
320  double d, dd, R(0);
321 
322  for ( mol1 = molgroup1->mol; mol1; mol1 = mol1->next ) {
323  for( res1 = mol1->res; res1; res1 = res1->next ) {
324  for ( atom1 = res1->atom; atom1; atom1 = atom1->next ) {
325  d = 1e30;
326  for ( mol2 = molgroup2->mol; mol2; mol2 = mol2->next ) {
327  for( res2 = mol2->res; res2; res2 = res2->next ) {
328  for ( atom2 = res2->atom; atom2; atom2 = atom2->next ) {
329  dd = atom1->coord.distance(atom2->coord);
330  if ( d > dd ) d = dd;
331  }
332  }
333  }
334  R += d*d;
335  n++;
336  }
337  }
338  }
339 
340  R = sqrt(R/n);
341 
342  return R;
343 }
344 
355 {
356  long i, j;
357  Bresidue *res1, *res2;
358  Batom *atom1, *atom2;
359 
360  Matrix mat(m1->nres, m2->nres);
361 
362  for ( i=0, res1 = m1->res; res1; res1 = res1->next, ++i ) {
363  for ( atom1 = res1->atom; atom1 && strncmp(atom1->type, "CA", 2); atom1 = atom1->next ) ;
364  if ( atom1 ) {
365  for ( j=0, res2 = m2->res; res2; res2 = res2->next, ++j ) {
366  for ( atom2 = res2->atom; atom2 && strncmp(atom2->type, "CA", 2); atom2 = atom2->next ) ;
367  if ( atom2 )
368  mat[i][j] = atom1->coord.distance(atom2->coord);
369  }
370  }
371  }
372 
373  return mat;
374 }
mol_find_transformation
Transform mol_find_transformation(Bmolecule *mol1, Bmolecule *mol2, int offset)
Determines the transformation between two identical molecules.
Definition: mol_compare.cpp:139
matrix_linear.h
Header file for matrix functions.
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
molgroup_kill
int molgroup_kill(Bmolgroup *molgroup)
Destroys a molecule group.
Definition: rwmolecule.cpp:374
Bresidue::next
Bresidue * next
Definition: rwmolecule.h:50
Bresidue::atom
Batom * atom
Definition: rwmolecule.h:54
Matrix::multiply_in_place
int multiply_in_place(vector< double > &vec)
Definition: matrix.cpp:49
symmetry.h
Header file for general symmetry functions.
Bmolgroup::mol
Bmolecule * mol
Definition: rwmolecule.h:189
Vector3< double >
Matrix3.h
3x3 Matrix manipulation functions
Vector3::distance2
double distance2(const Vector3 &v)
Definition: Vector3.h:228
Batom::el
char el[4]
Definition: rwmolecule.h:27
molgroup_find_transformation
Transform molgroup_find_transformation(Bmolgroup *molgroup1, Bmolgroup *molgroup2)
Determines the transformation between two groups of identical molecules.
Definition: mol_compare.cpp:51
Bmolecule::nres
long nres
Definition: rwmolecule.h:111
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
mol_transform.h
Library routines used for atomic coordinate transformations.
Transform::origin
Vector3< double > origin
Definition: Transform.h:27
Batom::next
Batom * next
Definition: rwmolecule.h:25
Bmolecule::res
Bresidue * res
Definition: rwmolecule.h:122
molgroup_coor_rotate
int molgroup_coor_rotate(Bmolgroup *molgroup, Transform t)
Rotates and translates a molecule group.
Definition: mol_transform.cpp:87
Vector3::distance
double distance(const Vector3 &v)
Definition: Vector3.h:227
Matrix3
Definition: Matrix3.h:18
Matrix::singular_value_decomposition
double singular_value_decomposition()
Singular value decomposition of a matrix .
Definition: matrix.cpp:177
verbose
int verbose
Vector3::normalize
double normalize()
Definition: Vector3.h:238
rwmolecule.h
Header file for reading reflection files.
rwatomprop.h
Header file for reading atom property files.
seq_align.h
Header file for functions to generate and analyze dot plots.
Bmolecule::next
Bmolecule * next
Definition: rwmolecule.h:108
Bmolecule
Definition: rwmolecule.h:107
Batom::type
char type[8]
Definition: rwmolecule.h:28
mol_calculate_rmsd
double mol_calculate_rmsd(Bmolecule *mol1, Bmolecule *mol2)
Calculates the RMSD between two molecules.
Definition: mol_compare.cpp:270
mol_compare.h
Library routines used to compare sets of atomic coordinates.
utilities.h
Header file for general utilities.
Matrix3::normalize
void normalize()
Definition: Matrix3.h:294
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
Transform::trans
Vector3< double > trans
Definition: Transform.h:30
Matrix.h
Generalized matrix class.
mol_symmetry.h
Library routines used for symmetry operations on atomic coordinates.
Bresidue
Definition: rwmolecule.h:49
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
Bmolgroup
Definition: rwmolecule.h:170
mol_distance_matrix
Matrix mol_distance_matrix(Bmolecule *m1, Bmolecule *m2)
Calculates the distance matrix between the residues in two molecules.
Definition: mol_compare.cpp:354
Transform::axis
Vector3< double > axis
Definition: Transform.h:28
Transform
Definition: Transform.h:24
molgroup_calc_brute_rmsd
double molgroup_calc_brute_rmsd(Bmolgroup *molgroup1, Bmolgroup *molgroup2)
Calculates the RMSD based on nearest atoms.
Definition: mol_compare.cpp:314
Vector3::length
double length()
Definition: Vector3.h:222
Matrix
Definition: Matrix.h:22
molgroup_rotate_and_compare
double molgroup_rotate_and_compare(Bmolgroup *molgroup, Transform t)
Copies and rotates the molecule group and compares it with the original.
Definition: mol_compare.cpp:31
molgroup_calculate_rmsd
double molgroup_calculate_rmsd(Bmolgroup *molgroup1, Bmolgroup *molgroup2)
Calculates the root-mean-square-deviation between two molecule groups.
Definition: mol_compare.cpp:213
molgroup_copy
Bmolgroup * molgroup_copy(Bmolgroup *molgroup)
Copies a molecule group.
Definition: rwmolecule.cpp:547
Batom::coord
Vector3< double > coord
Definition: rwmolecule.h:30
Batom
Definition: rwmolecule.h:24
mol_util.h
Library routines used for atomic coordinates.
matrix3_show_hp
int matrix3_show_hp(Matrix3 &mat)
Print out a matrix.
Definition: Matrix3.cpp:31