| Bsoft 2.0.5 Bernard's software package

model_compare.cpp
Go to the documentation of this file.
1
9 #include "model_select.h"
10 #include "model_util.h"
11 #include "Matrix3.h"
12 #include "utilities.h"
13
14 // Declaration of global variables
15 extern int verbose; // Level of output to the screen
16
26 long model_component_number_difference(Bmodel* model1, Bmodel* model2)
27 {
28 long n1, n2;
29 Bcomponent* comp1, *comp2;
30
31 for ( n1 = 0, comp1 = model1->comp; comp1; comp1 = comp1->next ) n1++;
32
33 for ( n2 = 0, comp2 = model2->comp; comp2; comp2 = comp2->next ) n2++;
34
35 return n1 - n2;
36 }
37
43 long model_maxnum_components(Bmodel* model)
44 {
45 long ncomp, n;
46 Bmodel* mp;
47 Bcomponent* comp;
48
49 for ( ncomp = 0, mp = model; mp; mp = mp->next ) {
50 for ( n=0, comp = mp->comp; comp; comp = comp->next ) n++;
51 if ( ncomp < n ) ncomp = n;
52 }
53
54 return ncomp;
55 }
56
66 double model_compare(Bmodel* model1, Bmodel* model2)
67 {
68 Bcomponent* comp1;
69 Bcomponent* comp2;
70 Bcomponent* compsel;
71
72 long n(0), n1(0), n2(0);
73 double d, dmin, R(0);
74
75 // Vector3<float> com1 = model_center_of_mass(model1);
76 // Vector3<float> com2 = model_center_of_mass(model2);
77
78 // for ( comp1 = model1->comp; comp1; comp1 = comp1->next ) comp1->loc -= com1;
79 // for ( comp2 = model2->comp; comp2; comp2 = comp2->next ) comp2->loc -= com2;
80
81 if ( verbose ) {
82 cout << "Comparing " << model1->id << " with " << model2->id << ":" << endl;
83 cout << "Comp1\tComp2\tDmin" << endl;
84 }
85
86 for ( comp1 = model1->comp; comp1; comp1 = comp1->next ) if ( comp1->sel ) {
87 dmin = 1e30;
88 compsel = NULL;
89 for ( comp2 = model2->comp; comp2; comp2 = comp2->next ) if ( comp2->sel ) {
90 d = comp1->loc.distance(comp2->loc);
91 if ( dmin > d ) {
92 dmin = d;
93 compsel = comp2;
94 }
95 }
96 if ( compsel ) {
97 comp1->sel = compsel->id.integer();
98 comp1->fom = dmin;
99 }
100 n1++;
101 }
102
103 for ( comp2 = model2->comp; comp2; comp2 = comp2->next ) if ( comp2->sel ) {
104 dmin = 1e30;
105 compsel = NULL;
106 for ( comp1 = model1->comp; comp1; comp1 = comp1->next ) if ( comp1->sel ) {
107 d = comp1->loc.distance(comp2->loc);
108 if ( dmin > d ) {
109 dmin = d;
110 compsel = comp1;
111 }
112 }
113 if ( compsel ) {
114 comp2->sel = compsel->id.integer();
115 comp2->fom = dmin;
116 }
117 n2++;
118 }
119
120 for ( comp1 = model1->comp; comp1; comp1 = comp1->next ) if ( comp1->sel ) {
121 for ( comp2 = model2->comp; comp2 && comp1->sel != comp2->id.integer(); comp2 = comp2->next ) ;
122 if ( comp2 && comp2->sel == comp1->id.integer() ) {
123 R += comp1->fom*comp1->fom;
124 n++;
125 if ( verbose )
126 cout << comp1->id << tab << comp2->id << tab << comp1->fom << endl;
127 }
128 }
129
130 R = sqrt(R/n);
131
132 if ( verbose ) {
133 cout << "Components compared: " << n << endl;
134 cout << "Model 1 components: " << n1 << " (" << n*100.0/n1 << "%)" << endl;
135 cout << "Model 2 components: " << n2 << " (" << n*100.0/n2 << "%)" << endl;
136 cout << "RMSD: " << R << endl << endl;
137 }
138
139 return R;
140 }
141
152 Matrix model_adjacency_matrix(Bmodel* model)
153 {
154 int i, j, k, ncomp;
155 Bcomponent* comp;
156
157 for ( ncomp = 0, comp = model->comp; comp; comp = comp->next, ncomp++ )
158 comp->sel = ncomp;
159
160 Matrix mat(ncomp,ncomp);
161
162 for ( comp = model->comp; comp; comp = comp->next ) {
163 i = comp->sel;
164 for ( k=0; k<MAXLINK && comp->link[k]; k++ ) {
165 j = comp->link[k]->sel;
166 mat[i][j] = 1;
167 }
168 }
169
170 for ( comp = model->comp; comp; comp = comp->next )
171 comp->sel = 1;
172
173 return mat;
174 }
175
189 Matrix model_distance_matrix(Bmodel* model, int view_flag)
190 {
191 long n, i, j;
192 double d, dmax;
193 Bcomponent* comp;
194 Bcomponent* comp2;
195
196 for ( n=0, dmax = 0, comp = model->comp; comp; comp = comp->next ) if ( comp->sel ) {
197 d = comp->loc.length();
198 if ( dmax < d ) dmax = d;
199 n++;
200 }
201
202 if ( verbose )
203 cout << "Calculating a " << n << " x " << n << " distance matrix:" << endl << endl;
204
205 Matrix dmat(n, n);
206
207 for ( i=0, comp = model->comp; comp->next; comp = comp->next ) if ( comp->sel ) {
208 for ( j=i+1, comp2 = comp->next; comp2; comp2 = comp2->next ) if ( comp2->sel ) {
209 if ( view_flag )
210 dmat[i][j] = dmat[j][i] =
211 comp->loc.distance(comp2->loc)/dmax + comp->view.residual(comp2->view);
212 else
213 dmat[i][j] = dmat[j][i] = comp->loc.distance(comp2->loc);
214 j++;
215 }
216 i++;
217 }
218
219 if ( verbose & VERB_FULL )
220 cout << dmat << endl;
221
222 return dmat;
223 }
224
238 Matrix model_distance_matrix(Bmodel* m1, Bmodel* m2)
239 {
240 long i, j, n1(0), n2(0);
241 Bcomponent *c1, *c2;
242
243 n1 = component_count(m1->comp);
244 n2 = component_count(m2->comp);
245
246 Matrix mat(n1, n2);
247
248 for ( i=0, c1 = m1->comp; c1; c1 = c1->next, ++i )
249 for ( j=0, c2 = m2->comp; c2; c2 = c2->next, ++j )
250 mat[i][j] = c1->loc.distance(c2->loc);
251
252 return mat;
253 }
254
266 long model_consolidate(Bmodel* model, double distance)
267 {
268 long i, j, k, n(0);
269 Bcomponent *c1, *c2;
270 Bcomponent* c = NULL;
271 Bcomponent* c_list = NULL;
272 Bstring cid;
273
274 n = component_count(model->comp);
275
276 Matrix mat = model_distance_matrix(model, 0);
277
278 if ( verbose ) {
279 cout << model->id << tab << n << endl;
280 mat.show_below_cutoff(distance);
281 }
282
283 for ( i=0; i<mat.rows(); ++i ) mat[i][i] = 2*distance;
284
285 for ( i=n=0, c1 = model->comp; c1; c1 = c1->next, ++i ) if ( mat[i][i] ) {
286 cid = Bstring(++n, "%d");
287 c = component_add(&c_list, cid);
288 component_copy(c1, c);
289 c->id = cid;
290 for ( j=i+1, k=1, c2 = c1->next; c2; c2 = c2->next, ++j ) {
291 if ( mat[i][j] <= distance ) {
292 c->loc += c2->loc;
293 mat[j][j] = 0;
294 k++;
295 }
296 }
297 if ( k > 1 ) c->loc *= 1.0L/k;
298 c->sel = k;
299 }
300
301 component_list_kill(model->comp);
302 model->comp = c_list;
303
304 if ( verbose )
305 cout << "Number of components: " << n << endl << endl;
306
307 return n;
308 }
309
324 Bmodel* models_consensus(Bmodel* model, double distance)
325 {
326 long nmod(0), i, j, k, n;
327 Bmodel *m1, *m2;
328 Bcomponent *c1, *c2;
329
330 for ( m1 = model; m1; m1 = m1->next ) nmod++;
331
332 for ( m1 = model; m1; m1 = m1->next )
333 model_consolidate(m1, distance);
334
335 long nmat(factorial(nmod)/(2*factorial(nmod-2)));
336
337 Matrix* mat = new Matrix[nmat];
338
339 for ( i=0, m1 = model; m1->next; m1 = m1->next ) {
340 for ( m2 = m1->next; m2; m2 = m2->next, ++i ) {
341 mat[i] = model_distance_matrix(m1, m2);
342 if ( verbose & VERB_FULL ) {
343 cout << m1->id << " vs " << m2->id << endl;
344 mat[i].show_below_cutoff(distance);
345 }
346 }
347 }
348
349 models_process(model, model_reset_selection);
350
351 for ( i=0, m1 = model; m1->next; m1 = m1->next ) {
352 for ( m2 = m1->next; m2; m2 = m2->next, ++i ) {
353 for ( j=0, c1 = m1->comp; c1; c1 = c1->next, ++j ) if ( c1->sel ) {
354 // c1->sel = 1;
355 for ( k=0, c2 = m2->comp; c2; c2 = c2->next, ++k ) if ( c2->sel ) {
356 // c2->sel = 1;
357 if ( mat[i][j][k] <= distance ) {
358 c1->loc += c2->loc;
359 c1->sel++;
360 c2->sel = 0;
361 }
362 }
363 // c1->loc /= c1->sel;
364 }
365 }
366 }
367
368 delete[] mat;
369
370 Bmodel* numod = model_init(model->id);
371 Bstring cid;
372 RGBA<float> color;
373 int* ns = new int[nmod+1];
374 for ( i=0; i<=nmod; i++ ) ns[i] = 0;
375
376 numod->fmap = model->fmap;
377 numod->img_num = model->img_num;
378 numod->comment = model->comment;
379 Bcomptype* ct = model_add_type_by_id(numod, model->type->id);
380
381 for ( n=0, m1 = model; m1->next; m1 = m1->next ) {
382 for ( c1 = m1->comp; c1; c1 = c1->next ) if ( c1->sel ) {
383 cid = Bstring(++n, "%d");
384 c2 = component_add(&numod->comp, cid);
385 component_copy(c1, c2);
386 c2->type = ct;
387 c2->id = cid;
388 c2->loc /= c2->sel;
389 c2->fom = c2->sel*1.0L/nmod;
390 ns[c2->sel]++;
391 color.spectrum(c2->sel, nmod - 0.5, 0.5);
392 c2->color = color;
393 if ( verbose & VERB_FULL )
394 cout << n << tab << c2->loc << tab << c2->sel << endl;
395 }
396 }
397
398 if (verbose ) {
399 cout << "Number\tCount\t%" << endl;
400 for ( i=0; i<=nmod; i++ )
401 cout << i << tab << ns[i] << tab << ns[i]*100.0/n << endl;
402 cout << endl;
403 }
404
405 delete[] ns;
406
407 return numod;
408 }
409
410
411
distance
double distance(vector< T1 > &v1, vector< T2 > &v2)
Definition: Vector3.h:536
Bmodel::img_num
int img_num
Definition: rwmodel.h:127
MAXLINK
#define MAXLINK
Definition: rwmodel.h:14
Bcomponent::id
Bstring id
Definition: rwmodel.h:53
Bcomponent::type
Bcomptype * type
Definition: rwmodel.h:54
Matrix::show_below_cutoff
void show_below_cutoff(double d)
Definition: Matrix.h:157
Bmodel::id
Bstring id
Definition: rwmodel.h:121
Matrix::rows
long rows()
Definition: Matrix.h:153
Vector3::length
double length()
Definition: Vector3.h:222
utilities.h
Header file for general utilities.
model_util.h
Library routines used for model processing.
RGBA< float >
model_component_number_difference
long model_component_number_difference(Bmodel *model1, Bmodel *model2)
Compares the numbers of components in two models.
Definition: model_compare.cpp:26
Bmodel::comment
Bstring comment
Definition: rwmodel.h:126
model_adjacency_matrix
Matrix model_adjacency_matrix(Bmodel *model)
Constructs the adjacency matrix for a model.
Definition: model_compare.cpp:152
component_add
Bcomponent * component_add(Bcomponent **comp, Bstring &id)
Adds a component to a linked list.
Definition: rwmodel.cpp:267
model_distance_matrix
Matrix model_distance_matrix(Bmodel *model, int view_flag)
Calculates the distance matrix for a model.
Definition: model_compare.cpp:189
component_list_kill
int component_list_kill(Bcomponent *comp)
Deallocates memory for a list of components.
Definition: rwmodel.cpp:891
Vector3::distance
double distance(const Vector3 &v)
Definition: Vector3.h:227
Matrix3.h
3x3 Matrix manipulation functions
models_consensus
Bmodel * models_consensus(Bmodel *model, double distance)
Determines a consensus between models in a list.
Definition: model_compare.cpp:324
factorial
double factorial(int n)
Calculates the factorial of n.
Definition: math_util.cpp:66
verbose
int verbose
model_init
Bmodel * model_init(Bstring &id)
Initializes a model structure.
Definition: rwmodel.cpp:144
model_maxnum_components
long model_maxnum_components(Bmodel *model)
Calculates the largest number of components in a model.
Definition: model_compare.cpp:43
Bcomponent::view
View view
Definition: rwmodel.h:59
Bcomponent::fom
float fom
Definition: rwmodel.h:65
Bmodel::type
Bcomptype * type
Definition: rwmodel.h:131
RGBA::spectrum
void spectrum(double value, double cmin, double cmax)
Definition: Color.h:221
Bmodel::next
Bmodel * next
Definition: rwmodel.h:120
Bcomponent::sel
int sel
Definition: rwmodel.h:66
model_add_type_by_id
Bcomptype * model_add_type_by_id(Bmodel *model, Bstring &id)
Add a type to a model if the identifier does not exist already.
Definition: rwmodel.cpp:411
model
Bmodel * model
Definition: bshow.cpp:32
model_consolidate
long model_consolidate(Bmodel *model, double distance)
Consolidates close components within a model.
Definition: model_compare.cpp:266
Matrix
Definition: Matrix.h:21
Bstring
Definition: Bstring.h:29
View::residual
double residual(const View &view)
Definition: View.h:165
model_reset_selection
long model_reset_selection(Bmodel *model)
Resets the selection to all components.
Definition: model_select.cpp:232
Bcomponent::link
Bcomponent * link[MAXLINK]
Definition: rwmodel.h:55
Bmodel::fmap
Bstring fmap
Definition: rwmodel.h:125
Bcomponent::next
Bcomponent * next
Definition: rwmodel.h:52
Bcomptype::id
Bstring id
Definition: rwmodel.h:27
model_compare
double model_compare(Bmodel *model1, Bmodel *model2)
Compares two models.
Definition: model_compare.cpp:66
component_copy
int component_copy(Bcomponent *fromcomp, Bcomponent *tocomp)
Copies all elements from one component to another.
Definition: rwmodel.cpp:783
Bstring::integer
long integer()
Definition: Bstring.cpp:539
Bmodel
Definition: rwmodel.h:119
Bcomptype
Definition: rwmodel.h:25
component_count
long component_count(Bcomponent *comp)
Counts the total number of components.
Definition: rwmodel.cpp:805
Bmodel::comp
Bcomponent * comp
Definition: rwmodel.h:132
models_process
long models_process(Bmodel *model, long(modfunc)(Bmodel *))
Process a list of models using the specified function.
Definition: model_util.cpp:36
model_select.h
Header file for reading and writing atomic model files.
VERB_FULL
#define VERB_FULL
Definition: utilities.h:109
Bcomponent::loc
Vector3< float > loc
Definition: rwmodel.h:58
Bcomponent::color
RGBA< float > color
Definition: rwmodel.h:63
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:21
Bcomponent
Definition: rwmodel.h:51