Bsoft  2.1.0
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 
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 
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 
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 
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 
237 {
238  long i, j, n1(0), n2(0);
239  Bcomponent *c1, *c2;
240 
241  n1 = component_count(m1->comp);
242  n2 = component_count(m2->comp);
243 
244  Matrix mat(n1, n2);
245 
246  for ( i=0, c1 = m1->comp; c1; c1 = c1->next, ++i )
247  for ( j=0, c2 = m2->comp; c2; c2 = c2->next, ++j )
248  mat[i][j] = c1->loc.distance(c2->loc);
249 
250  return mat;
251 }
252 
265 {
266  long i, j, k, n(0);
267  Bcomponent *c1, *c2;
268  Bcomponent* c = NULL;
269  Bcomponent* c_list = NULL;
270  Bstring cid;
271 
272  n = component_count(model->comp);
273 
275 
276  if ( verbose ) {
277  cout << model->id << tab << n << endl;
279  }
280 
281  for ( i=0; i<mat.rows(); ++i ) mat[i][i] = 2*distance;
282 
283  for ( i=n=0, c1 = model->comp; c1; c1 = c1->next, ++i ) if ( mat[i][i] ) {
284  cid = Bstring(++n, "%d");
285  c = component_add(&c_list, cid);
286  component_copy(c1, c);
287  c->id = cid;
288  for ( j=i+1, k=1, c2 = c1->next; c2; c2 = c2->next, ++j ) {
289  if ( mat[i][j] <= distance ) {
290  c->loc += c2->loc;
291  mat[j][j] = 0;
292  k++;
293  }
294  }
295  if ( k > 1 ) c->loc *= 1.0L/k;
296  c->sel = k;
297  }
298 
300  model->comp = c_list;
301 
302  if ( verbose )
303  cout << "Number of components: " << n << endl << endl;
304 
305  return n;
306 }
307 
323 {
324  long nmod(0), i, j, k, n;
325  Bmodel *m1, *m2;
326  Bcomponent *c1, *c2;
327 
328  for ( m1 = model; m1; m1 = m1->next ) nmod++;
329 
330  for ( m1 = model; m1; m1 = m1->next )
332 
333  long nmat(factorial(nmod)/(2*factorial(nmod-2)));
334 
335  Matrix* mat = new Matrix[nmat];
336 
337  for ( i=0, m1 = model; m1->next; m1 = m1->next ) {
338  for ( m2 = m1->next; m2; m2 = m2->next, ++i ) {
339  mat[i] = model_distance_matrix(m1, m2);
340  if ( verbose & VERB_FULL ) {
341  cout << m1->id << " vs " << m2->id << endl;
342  mat[i].show_below_cutoff(distance);
343  }
344  }
345  }
346 
348 
349  for ( i=0, m1 = model; m1->next; m1 = m1->next ) {
350  for ( m2 = m1->next; m2; m2 = m2->next, ++i ) {
351  for ( j=0, c1 = m1->comp; c1; c1 = c1->next, ++j ) if ( c1->sel ) {
352 // c1->sel = 1;
353  for ( k=0, c2 = m2->comp; c2; c2 = c2->next, ++k ) if ( c2->sel ) {
354 // c2->sel = 1;
355  if ( mat[i][j][k] <= distance ) {
356  c1->loc += c2->loc;
357  c1->sel++;
358  c2->sel = 0;
359  }
360  }
361 // c1->loc /= c1->sel;
362  }
363  }
364  }
365 
366  delete[] mat;
367 
368  Bmodel* numod = model_init(model->id);
369  Bstring cid;
370  RGBA<float> color;
371  int* ns = new int[nmod+1];
372  for ( i=0; i<=nmod; i++ ) ns[i] = 0;
373 
374  numod->fmap = model->fmap;
375  numod->img_num = model->img_num;
376  numod->comment = model->comment;
377  Bcomptype* ct = model_add_type_by_id(numod, model->type->id);
378 
379  for ( n=0, m1 = model; m1->next; m1 = m1->next ) {
380  for ( c1 = m1->comp; c1; c1 = c1->next ) if ( c1->sel ) {
381  cid = Bstring(++n, "%d");
382  c2 = component_add(&numod->comp, cid);
383  component_copy(c1, c2);
384  c2->type = ct;
385  c2->id = cid;
386  c2->loc /= c2->sel;
387  c2->fom = c2->sel*1.0L/nmod;
388  ns[c2->sel]++;
389  color.spectrum(c2->sel, nmod - 0.5, 0.5);
390  c2->color = color;
391  if ( verbose & VERB_FULL )
392  cout << n << tab << c2->loc << tab << c2->sel << endl;
393  }
394  }
395 
396  if (verbose ) {
397  cout << "Number\tCount\t%" << endl;
398  for ( i=0; i<=nmod; i++ )
399  cout << i << tab << ns[i] << tab << ns[i]*100.0/n << endl;
400  cout << endl;
401  }
402 
403  delete[] ns;
404 
405  return numod;
406 }
407 
408 
409 
Bcomponent::next
Bcomponent * next
Definition: rwmodel.h:52
model_distance_matrix
Matrix model_distance_matrix(Bmodel *model, int view_flag)
Calculates the distance matrix for a model.
Definition: model_compare.cpp:189
Bcomponent
Definition: rwmodel.h:51
model_compare
double model_compare(Bmodel *model1, Bmodel *model2)
Compares two models.
Definition: model_compare.cpp:66
Bcomponent::loc
Vector3< float > loc
Definition: rwmodel.h:58
Bstring
Definition: Bstring.h:29
Bcomponent::id
Bstring id
Definition: rwmodel.h:53
Bstring::integer
long integer()
Definition: Bstring.cpp:547
component_add
Bcomponent * component_add(Bcomponent **comp, Bstring &id)
Adds a component to a linked list.
Definition: rwmodel.cpp:270
Bcomptype
Definition: rwmodel.h:25
Bmodel::next
Bmodel * next
Definition: rwmodel.h:120
Matrix3.h
3x3 Matrix manipulation functions
factorial
double factorial(int n)
Calculates the factorial of n.
Definition: math_util.cpp:66
Bmodel::fmap
Bstring fmap
Definition: rwmodel.h:125
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_init
Bmodel * model_init(Bstring &id)
Initializes a model structure.
Definition: rwmodel.cpp:147
component_list_kill
int component_list_kill(Bcomponent *comp)
Deallocates memory for a list of components.
Definition: rwmodel.cpp:894
Vector3::distance
double distance(const Vector3 &v)
Definition: Vector3.h:227
Bmodel::comp
Bcomponent * comp
Definition: rwmodel.h:132
Matrix::rows
long rows()
Definition: Matrix.h:153
Bmodel::id
Bstring id
Definition: rwmodel.h:121
model_select.h
Header file for reading and writing atomic model files.
distance
double distance(vector< T1 > &v1, vector< T2 > &v2)
Definition: Vector3.h:538
model_reset_selection
long model_reset_selection(Bmodel *model)
Resets the selection to all components.
Definition: model_select.cpp:232
RGBA::spectrum
void spectrum(double value, double cmin, double cmax)
Definition: Color.h:221
model_adjacency_matrix
Matrix model_adjacency_matrix(Bmodel *model)
Constructs the adjacency matrix for a model.
Definition: model_compare.cpp:152
Bmodel
Definition: rwmodel.h:119
Bcomponent::color
RGBA< float > color
Definition: rwmodel.h:63
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
RGBA< float >
Bmodel::img_num
int img_num
Definition: rwmodel.h:127
Bcomponent::view
View view
Definition: rwmodel.h:59
Bcomponent::fom
float fom
Definition: rwmodel.h:65
Bcomponent::type
Bcomptype * type
Definition: rwmodel.h:54
Bcomptype::id
Bstring id
Definition: rwmodel.h:27
model
Bmodel * model
Definition: bshow.cpp:34
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
Matrix::show_below_cutoff
void show_below_cutoff(double d)
Definition: Matrix.h:157
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
verbose
int verbose
model_maxnum_components
long model_maxnum_components(Bmodel *model)
Calculates the largest number of components in a model.
Definition: model_compare.cpp:43
model_util.h
Library routines used for model processing.
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:414
View::residual
double residual(const View &view)
Definition: View.h:168
Bmodel::type
Bcomptype * type
Definition: rwmodel.h:131
component_copy
int component_copy(Bcomponent *fromcomp, Bcomponent *tocomp)
Copies all elements from one component to another.
Definition: rwmodel.cpp:786
Vector3::length
double length()
Definition: Vector3.h:222
Matrix
Definition: Matrix.h:22
MAXLINK
#define MAXLINK
Definition: rwmodel.h:14
models_consensus
Bmodel * models_consensus(Bmodel *model, double distance)
Determines a consensus between models in a list.
Definition: model_compare.cpp:322
Bcomponent::link
Bcomponent * link[MAXLINK]
Definition: rwmodel.h:55
component_count
long component_count(Bcomponent *comp)
Counts the total number of components.
Definition: rwmodel.cpp:808
model_consolidate
long model_consolidate(Bmodel *model, double distance)
Consolidates close components within a model.
Definition: model_compare.cpp:264
Bmodel::comment
Bstring comment
Definition: rwmodel.h:126
Bcomponent::sel
int sel
Definition: rwmodel.h:66