| Bsoft 2.0.5 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
31 double molgroup_rotate_and_compare(Bmolgroup* molgroup, Transform t)
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
51 Transform molgroup_find_transformation(Bmolgroup* molgroup1, Bmolgroup* molgroup2)
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
66 Vector3<double> com = molgroup_center_of_mass(molgroup1);
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();
94 a.singular_value_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
139 Transform mol_find_transformation(Bmolecule* mol1, Bmolecule* mol2, int offset)
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
161 Vector3<double> com = mol_center_of_mass(mol1);
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();
187 a.singular_value_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
270 double mol_calculate_rmsd(Bmolecule* mol1, Bmolecule* mol2)
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
297 Vector3<double> comd = mol_center_of_mass(mol2) - mol_center_of_mass(mol1);
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
seq_align.h
Header file for functions to generate and analyze dot plots.
Bmolgroup::mol
Bmolecule * mol
Definition: rwmolecule.h:189
Vector3::normalize
double normalize()
Definition: Vector3.h:236
Vector3::length
double length()
Definition: Vector3.h:222
mol_calculate_rmsd
double mol_calculate_rmsd(Bmolecule *mol1, Bmolecule *mol2)
Calculates the RMSD between two molecules.
Definition: mol_compare.cpp:270
symmetry.h
Header file for general symmetry functions.
utilities.h
Header file for general utilities.
Bmolecule::res
Bresidue * res
Definition: rwmolecule.h:122
Matrix3
Definition: Matrix3.h:17
Matrix::multiply_in_place
int multiply_in_place(vector< double > &vec)
Definition: matrix.cpp:49
mol_transform.h
Library routines used for atomic coordinate transformations.
Matrix.h
Generalized matrix class.
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
Vector3::distance
double distance(const Vector3 &v)
Definition: Vector3.h:227
mol_symmetry.h
Library routines used for symmetry operations on atomic coordinates.
Matrix3.h
3x3 Matrix manipulation functions
Matrix::singular_value_decomposition
double singular_value_decomposition()
Singular value decomposition of a matrix .
Definition: matrix.cpp:177
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
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_util.h
Library routines used for atomic coordinates.
Vector3::distance2
double distance2(const Vector3 &v)
Definition: Vector3.h:228
Transform
Definition: Transform.h:24
rwmolecule.h
Header file for reading reflection files.
Bmolgroup
Definition: rwmolecule.h:170
molgroup_copy
Bmolgroup * molgroup_copy(Bmolgroup *molgroup)
Copies a molecule group.
Definition: rwmolecule.cpp:547
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
verbose
int verbose
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
matrix3_show_hp
int matrix3_show_hp(Matrix3 &mat)
Print out a matrix.
Definition: Matrix3.cpp:31
rwatomprop.h
Header file for reading atom property files.
molgroup_kill
int molgroup_kill(Bmolgroup *molgroup)
Destroys a molecule group.
Definition: rwmolecule.cpp:374
Bmolecule::next
Bmolecule * next
Definition: rwmolecule.h:108
Transform::trans
Vector3< double > trans
Definition: Transform.h:30
Bresidue::atom
Batom * atom
Definition: rwmolecule.h:54
Bmolecule
Definition: rwmolecule.h:107
Bresidue::next
Bresidue * next
Definition: rwmolecule.h:50
Transform::origin
Vector3< double > origin
Definition: Transform.h:27
Bresidue
Definition: rwmolecule.h:49
Matrix
Definition: Matrix.h:21
Batom
Definition: rwmolecule.h:24
Matrix3::normalize
void normalize()
Definition: Matrix3.h:294
matrix_linear.h
Header file for matrix functions.
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< double >
Batom::coord
Vector3< double > coord
Definition: rwmolecule.h:30
Batom::el
char el[4]
Definition: rwmolecule.h:27
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
Transform::axis
Vector3< double > axis
Definition: Transform.h:28
molgroup_coor_rotate
int molgroup_coor_rotate(Bmolgroup *molgroup, Transform t)
Rotates and translates a molecule group.
Definition: mol_transform.cpp:87
VERB_FULL
#define VERB_FULL
Definition: utilities.h:109
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
Batom::next
Batom * next
Definition: rwmolecule.h:25
mol_compare.h
Library routines used to compare sets of atomic coordinates.