Bsoft  2.1.0
Bernard's software package
model_extract_build.cpp
Go to the documentation of this file.
1 
9 #include "Bimage.h"
10 #include "model_extract_build.h"
11 #include "model_util.h"
12 #include "file_util.h"
13 #include "linked_list.h"
14 #include "random_numbers.h"
15 #include "utilities.h"
16 
17 // Declaration of global variables
18 extern int verbose; // Level of output to the screen
19 
20 /*
21  The first axis is along the link.
22  The second axis is the average of the component normals, usually directed
23  away from the molecule group center.
24  The third axes is generated as the cross product of the first two. i.e.,
25  perpendicular to both.
26  A new second axis is generated as the cross product between the other two,
27  ensuring that all the axes are orthogonal.
28 */
30 {
31  Matrix3 mat;
32  Vector3<double> u, v, w;
33 
34  u = link->comp[1]->loc - link->comp[0]->loc;
35  w = Vector3<double>(link->comp[0]->view[0], link->comp[0]->view[1], link->comp[0]->view[2]);
36  w += Vector3<double>(link->comp[1]->view[0], link->comp[1]->view[1], link->comp[1]->view[2]);
37  v = w.cross(u);
38  w = u.cross(v);
39  u.normalize();
40  v.normalize();
41  w.normalize();
42 // for ( int i=0; i<3; i++ ) {
43 // mat[i] = u[i];
44 // mat[i+3] = v[i];
45 // mat[i+6] = w[i];
46 // }
47  mat[0] = u;
48  mat[1] = v;
49  mat[2] = w;
50  mat = mat.transpose();
51 // cout << mat << endl;
52 
53  return mat;
54 }
55 
57  Bimage* pmask, Bimage* pfsmask, View view,
58  double hires, double lores, double shift_limit,
59  int shift_flag, fft_plan planf, fft_plan planb)
60 {
61  long n, imax(0);
62  Vector3<long> size(ptemp->size());
63  Vector3<double> origin(ptemp->image->origin());
64  Vector3<double> scale(ptemp->sampling(0));
65  Vector3<double> loc = comp->loc/scale + p->image->origin();
66 
67  Matrix3 mat = view.matrix();
68  Bimage* pcomp = p->extract(0, loc, size, origin, mat);
69  Bimage* pone = NULL;
70  Bimage* pmaskrot = NULL;
71 
72  double cc, ccmax(-1);
73  Vector3<double> shift, best_shift;
74 
75  if ( pmask ) {
76  pcomp->rescale_to_avg_std(0, 1, pmask);
77  pcomp->multiply(pmask);
78  }
79 
80  if ( pfsmask ) pmaskrot = pfsmask->extract_wrap(0, size, mat);
81 
82  for ( n=0; n<ptemp->images(); n++ ) {
83  pone = ptemp->extract(n);
84  pone->origin(origin);
85  shift = pcomp->find_shift(pone, pmaskrot, hires, lores, shift_limit, 0, 1, planf, planb, cc);
86  if ( ccmax < cc ) {
87  ccmax = cc;
88  imax = n;
89  if ( shift_flag == 1 ) shift[0] = shift[1] = 0; // Shift only along the normal
90  else if ( shift_flag == 2 ) shift[2] = 0; // Shift only lateral
91  best_shift = mat * shift; // Shift in voxels
92  }
93  delete pone;
94  }
95 
96  delete pcomp;
97  delete pmaskrot;
98 
99  comp->sel = imax + 1;
100  comp->fom = ccmax;
101 
102  return best_shift;
103 }
104 
106  Bimage* pmask, Bimage* pfsmask, View view,
107  double hires, double lores, double shift_limit,
108  int shift_flag, fft_plan planf, fft_plan planb)
109 {
110  long n, imax(0);
111  Vector3<long> size(ptemp->size());
112  Vector3<double> origin(ptemp->image->origin());
113  Vector3<double> scale(ptemp->sampling(0));
114  Vector3<double> loc = comp->loc/scale + p->image->origin();
115 
116  Matrix3 mat = view.matrix();
117  mat = mat.transpose();
118 
119  Bimage* pcomp = p->extract(0, loc, size, origin);
120  Bimage* pone = NULL;
121  Bimage* pmaskrot = NULL;
122 
123  double cc, ccmax(-1);
124  Vector3<double> shift, best_shift;
125 
126  if ( pmask ) {
127  pmaskrot = pmask->rotate(size, mat);
128  pcomp->rescale_to_avg_std(0, 1, pmaskrot);
129  pcomp->multiply(pmaskrot);
130  delete pmaskrot;
131  }
132 
133  for ( n=0; n<ptemp->images(); n++ ) {
134  pone = ptemp->extract(n, origin, size, origin, mat);
135  shift = pcomp->find_shift(pone, pfsmask, hires, lores, shift_limit, 0, 1, planf, planb, cc);
136  if ( ccmax < cc ) {
137  ccmax = cc;
138  imax = n;
139  if ( shift_flag == 1 ) shift[0] = shift[1] = 0; // Shift only along the normal
140  else if ( shift_flag == 2 ) shift[2] = 0; // Shift only lateral
141  best_shift = shift; // Shift in voxels
142  }
143  delete pone;
144  }
145 
146  delete pcomp;
147 
148  comp->sel = imax + 1;
149  comp->fom = ccmax;
150 
151  return best_shift;
152 }
153 
154 int component_refine_random(Bcomponent* comp, Bimage* p, Bimage* ptemp, Bimage* pmask,
155  Bimage* pfsmask, int max_iter, double hires, double lores, double max_shift,
156  double max_view_angle, double max_rot_angle, int shift_flag, fft_plan planf, fft_plan planb)
157 {
158  if ( verbose & VERB_FULL )
159  cout << "Refining component " << comp->id << " by random view variations" << endl;
160 
161  int imax(0), iter, itm;
162  double shift_limit(max_shift/ptemp->sampling(0)[0]);
163  double cc, ccmax(-1), a, s, irm(1.0/get_rand_max());
164  View view(comp->view);
165  View comp_view(comp->view);
166  Vector3<double> scale(ptemp->sampling(0));
167  Vector3<double> r, comp_shift;
168  Vector3<double> shift;
169 
170  for ( iter=itm=0; iter<=max_iter; iter++ ) {
171  if ( iter > 0 ) {
172  r = vector3_random_gaussian(0, max_view_angle);
173  a = max_rot_angle*(random()*irm*2 - 1);
174  view[0] += r[0];
175  view[1] += r[1];
176  view[2] += r[2];
177  view[3] += a;
178  view.normalize();
179  }
180  shift = component_find_shift(comp, p, ptemp, pmask, pfsmask, view, hires, lores,
181  shift_limit, shift_flag, planf, planb);
182  cc = comp->fom;
183  if ( ccmax < cc ) {
184  ccmax = cc;
185  imax = comp->sel - 1;
186  comp_shift = shift; // Shift in voxels
187  comp_view = view;
188  itm = iter;
189  }
190  }
191 
192  comp_shift *= scale; // Shift in angstroms
193  s = comp_shift.length();
194  if ( s > max_shift ) {
195  comp_shift *= max_shift/s;
196  s = comp_shift.length();
197  }
198  comp->loc += comp_shift;
199  comp->vec = comp_shift;
200  comp->view = comp_view;
201  comp->sel = imax + 1;
202  comp->fom = ccmax;
203 
204  return itm;
205 }
206 
207 int component_refine(Bcomponent* comp, Bimage* p, Bimage* ptemp, Bimage* pmask,
208  Bimage* pfsmask, double viewmax, double rotmax,
209  double viewstep, double rotstep,
210  double hires, double lores, double accuracy, double max_shift,
211  int shift_flag, fft_plan planf, fft_plan planb)
212 {
213  if ( verbose & VERB_FULL )
214  cout << "Refining component " << comp->id << " by contracting grid search" << endl;
215 
216  if ( viewmax < 0 ) viewmax = 0;
217  if ( rotmax < 0 ) rotmax = 0;
218  if ( viewstep < 0 ) viewstep = 0;
219  if ( rotstep < 0 ) rotstep = 0;
220 
221  int imax(0), itm;
222  double shift_limit(max_shift/ptemp->sampling(0)[0]);
223  double cc, ccp(-2), ccmax(-1), s;
224  Vector3<double> scale(ptemp->sampling(0));
225  Vector3<double> comp_shift;
226  Vector3<double> shift;
227 
228  View comp_view(comp->view);
229  View* views;
230  View* v;
231 
232  double tol(1e-6);
233  if ( viewstep && accuracy > viewstep ) accuracy = viewstep;
234  if ( rotstep && accuracy > rotstep ) accuracy = rotstep;
235 
236  int notdone(1);
237  for ( itm=0; notdone; itm++ ) {
238  views = views_within_limits(comp_view, viewstep, viewstep, rotstep, viewmax, rotmax);
239  for ( v = views; v; v = v->next ) {
240  shift = component_find_shift2(comp, p, ptemp, pmask, pfsmask, *v, hires, lores,
241  shift_limit, shift_flag, planf, planb);
242  cc = comp->fom;
243  if ( ccmax < cc ) {
244  ccmax = cc;
245  imax = comp->sel - 1;
246  comp_shift = shift; // Shift in voxels
247  comp_view = *v;
248  }
249  }
250  kill_list((char *) views, sizeof(View));
251  if ( fabs(ccp - ccmax) < tol ) {
252  viewstep /= 2; // Contract around best point
253  rotstep /= 2;
254  }
255  ccp = ccmax;
256  if ( viewstep < tol && rotstep < tol ) notdone = 0;
257  if ( viewstep <= accuracy && rotstep <= accuracy ) notdone = 0;
258  }
259 
260  comp_shift *= scale; // Shift in angstroms
261  s = comp_shift.length();
262  if ( s > max_shift ) {
263  comp_shift *= max_shift/s;
264  s = comp_shift.length();
265  }
266  comp->loc += comp_shift;
267  comp->vec = comp_shift;
268  comp->view = comp_view;
269  comp->sel = imax + 1;
270  comp->fom = ccmax;
271 
272  return itm;
273 }
274 
301  Bimage* pmask, Bimage* pfsmask, int max_iter, double viewstep, double rotstep,
302  double hires, double lores, double accuracy, double max_shift,
303  double max_view_angle, double max_rot_angle, int shift_flag)
304 {
305  if ( max_iter < 0 ) max_iter = 0;
306  if ( max_shift <= 0 ) max_shift = ptemp->sizeX()*ptemp->sampling(0)[0]/10.0;
307 // if ( max_view_angle <= 0 ) max_view_angle = M_PI;
308 // if ( max_rot_angle <= 0 ) max_rot_angle = M_PI;
309 
310 // double alpha_step = M_PI/60.0; // 3 degree starting step size
311 // if ( alpha_step > max_view_angle ) alpha_step = max_view_angle;
312 
313 // double alpha_step = hires/ptemp->real_size()[0]; // resolution-linked starting step size
314 // if ( alpha_step > max_rot_angle ) alpha_step = max_rot_angle;
315 
316  long i, nname;
317  Bstring base("C"), id, tempname(ptemp->file_name());
318  Bstring* name;
319  Bcomptype* ct;
320  Bcomptype* ct2;
321 
322  if ( !ct_names ) {
323  for ( ct = model->type; ct && !ct->sel; ct = ct->next ) ;
324  if ( ct ) base = ct->id;
325  if ( ptemp->images() < 2 ) string_add(&ct_names, base);
326  else for ( i=1; i<=ptemp->images(); i++ ) {
327  id = base + Bstring(i, "%03d");
328  string_add(&ct_names, id);
329  }
330  }
331 
332  for ( nname=0, name = ct_names; name; name = name->next ) nname++;
333 
334  if ( nname != ptemp->images() ) {
335  cerr << "Error: The number of component type names must equal" << endl;
336  cerr << " the number of template sub-images (" << ptemp->images() << ")" << endl << endl;
337  return -1;
338  }
339 
340  // If a type is selected, it will be replaced
341  for ( ct = model->type; ct && !ct->sel; ct = ct->next ) ;
342  if ( ct ) {
343  for ( ct2 = model->type; ct2; ct2 = ct2->next ) if ( ct2->sel ) ct2->sel = -1;
344  if ( ptemp->images() == 1 ) {
345  ct->id = *ct_names;
346  ct->sel = 1;
347  } else {
348  for ( i=0, name = ct_names; name; name = name->next, i++ )
349  ct = model_add_type_by_id_and_filename(model, *name, tempname, i);
350  for ( ct = ct2 = model->type; ct; ) {
351  if ( ct->sel < 0 ) {
352  if ( ct == model->type ) {
353  model->type = ct2 = ct->next;
354  comp_type_kill(ct);
355  ct = ct2;
356  } else {
357  ct2->next = ct->next;
358  comp_type_kill(ct);
359  ct = ct2->next;
360  }
361  } else {
362  ct2 = ct;
363  ct = ct->next;
364  }
365  }
366  }
367  }
368 
369  random_seed();
370 
371  Bimage* p = read_img(model->fmap, 1, model->img_num);
372  if ( p == NULL ) {
373  cerr << "Error: No model map file read! (" << model->fmap << ")" << endl;
374  bexit(-1);
375  }
376 
377  p->change_type(Float) ;
378  ptemp->change_type(Float) ;
379 
380  if ( pmask ) {
381  ptemp->rescale_to_avg_std(0, 1, pmask);
382  ptemp->multiply(pmask);
383  }
384 
385  if ( hires > lores ) swap(hires, lores);
386 
387  double radius = ptemp->sizeX()/2;
388 
389  View ref_view;
390  View* views = views_within_limits(ref_view, viewstep, viewstep, rotstep, max_view_angle, max_rot_angle);
391  long nv = count_list((char *) views);
392  kill_list((char *) views, sizeof(View));
393 
394  if ( verbose ) {
395  cout << "Refining component positions and views:" << endl;
396  cout << "Component types: ";
397  for ( name = ct_names; name; name = name->next ) cout << *name << " ";
398  cout << endl;
399  cout << "Reference map: " << ptemp->file_name() << endl;
400  if ( pmask ) cout << "Mask: " << pmask->file_name() << endl;
401  if ( pfsmask ) cout << "Fourier space mask: " << pfsmask->file_name() << endl;
402  cout << "Iterations: " << max_iter << endl;
403  cout << "Resolution limits: " << hires << " " << lores << " A" << endl;
404  cout << "Maximum shift: " << max_shift << " A" << endl;
405  cout << "Maximum view rotation: " << max_view_angle*180.0/M_PI << " degrees" << endl;
406  cout << "Maximum angle rotation: " << max_rot_angle*180.0/M_PI << " degrees" << endl;
407  cout << "Step sizes: " << viewstep*180.0/M_PI << " "
408  << rotstep*180.0/M_PI << " degrees" << endl;
409  cout << "Accuracy: " << accuracy*180.0/M_PI << " degrees" << endl;
410  cout << "Number of views: " << nv << endl;
411  cout << "Density radius: " << radius << endl << endl;
412  }
413 
414  long n, ncomp;
415  double s, a, d, d1, d2, ccavg(0), d1avg(0), d2avg(0);
416  Vector3<double> map_origin(p->image->origin());
417  Vector3<double> scale(ptemp->sampling(0));
418  Vector3<double> loc, comp_shift, shift_avg;
419 
420  if ( verbose & VERB_DEBUG )
421  cout << "DEBUG model_refine_components: map_origin=" << map_origin << " scale=" << scale << endl;
422 
423  Bcomponent* comp;
424  Blink* link;
425  int* num = new int[ptemp->images()];
426  for ( n=0; n<ptemp->images(); n++ ) num[n] = 0;
427 
428  for ( ncomp=0, comp = model->comp; comp; comp = comp->next )
429  if ( comp->sel ) ncomp++;
430 
431  Bcomponent** comparr = new Bcomponent*[ncomp];
432 
433  for ( i=0, comp = model->comp; comp; comp = comp->next ) if ( comp->sel ) {
434  comparr[i++] = comp;
435  loc = comp->loc/scale + map_origin;
436  comp->density = p->density(0, loc, radius);
437  }
438 
439  fft_plan planf = ptemp->fft_setup(FFTW_FORWARD, 1);
440  fft_plan planb = ptemp->fft_setup(FFTW_BACKWARD, 1);
441 
442  if ( verbose )
443  cout << "Comp\tType\tdx\tdy\tdz\tCC" << endl;
444 #ifdef HAVE_GCD
445  dispatch_queue_t myq = dispatch_queue_create(NULL, NULL);
446  dispatch_apply(ncomp, dispatch_get_global_queue(0, 0), ^(size_t i){
447  if ( max_iter )
448  component_refine_random(comparr[i], p, ptemp, pmask, pfsmask, max_iter, hires, lores,
449  max_shift, max_view_angle, max_rot_angle, shift_flag, planf, planb);
450  else
451  component_refine(comparr[i], p, ptemp, pmask, pfsmask, max_view_angle,
452  max_rot_angle, viewstep, rotstep, hires, lores, accuracy,
453  max_shift, shift_flag, planf, planb);
454  if ( verbose )
455  dispatch_sync(myq, ^{
456  cout << comparr[i]->id << tab << comparr[i]->sel << tab
457  << setprecision(2) << comparr[i]->vec
458  << tab << setprecision(4) << comparr[i]->fom << endl;
459  });
460  });
461 #else
462 #pragma omp parallel for
463  for ( i=0; i<ncomp; i++ ) {
464  if ( max_iter )
465  component_refine_random(comparr[i], p, ptemp, pmask, pfsmask, max_iter, hires, lores,
466  max_shift, max_view_angle, max_rot_angle, shift_flag, planf, planb);
467  else
468  component_refine(comparr[i], p, ptemp, pmask, pfsmask, max_view_angle,
469  max_rot_angle, viewstep, rotstep, hires, lores, accuracy,
470  max_shift, shift_flag, planf, planb);
471  #pragma omp critical
472  if ( verbose )
473  cout << comparr[i]->id << tab << comparr[i]->sel << tab
474  << setprecision(2) << comparr[i]->vec
475  << tab << setprecision(4) << comparr[i]->fom << endl;
476  }
477 #endif
478 
479  if ( verbose )
480  cout << "Comp\tType\tdx\tdy\tdz\tShift\tCC\tDens1\tDens2\tdDens" << endl;
481  for ( i=0; i<ncomp; i++ ) {
482  comp = comparr[i];
483  s = comp->vec.length();
484  num[comp->sel-1]++;
485  loc = comp->loc/scale + map_origin;
486  shift_avg += comp->vec;
487  ccavg += comp->fom;
488  d1 = comp->density;
489 // d2 = img_density_at_coordinates(p, 0, loc, radius);
490  d2 = p->density(0, loc, radius);
491  d1avg += d1;
492  d2avg += d2;
493  comp->density = d2;
494  for ( n=1, name = ct_names; n<comp->sel && n<ptemp->images(); n++, name = name->next ) ;
495  if ( name ) comp->type = model_add_type_by_id(model, *name);
496  if ( verbose )
497  cout << comp->id << tab << comp->sel << tab
498  << setprecision(2) << comp->vec[0] << tab << comp->vec[1] << tab << comp->vec[2]
499  << tab << s << tab
500  << setprecision(4) << comp->fom << tab << d1 << tab << d2 << tab << d2 - d1 << endl;
501  }
502 
505 
506  shift_avg /= ncomp;
507  s = shift_avg.length();
508  ccavg /= ncomp;
509  d1avg /= ncomp;
510  d2avg /= ncomp;
511 
512  if ( verbose ) {
513  cout << "Average\t\t" << shift_avg[0] << tab << shift_avg[1] << tab <<
514  shift_avg[2] << tab << s << tab << ccavg << tab <<
515  d1avg << tab << d2avg << tab << d2avg - d1avg << endl;
516  cout << "Component type distribution:\n#\tType\tCount" << endl;
517  for ( n=0, name = ct_names; n<ptemp->images(); n++, name = name->next )
518  cout << n+1 << tab << *name << tab << num[n] << endl;
519  cout << endl;
520  if ( model->link ) {
521  for ( n=0, a=s=0, link = model->link; link; link = link->next, n++ ) {
522  d = link->comp[0]->loc.distance(link->comp[1]->loc);
523  a += d;
524  s += d*d;
525  }
526  if ( n ) {
527  a /= n;
528  s = s/n - a*a;
529  if ( s > 0 ) s = sqrt(s);
530  else s = 0;
531  }
532  cout << "Average link length: " << a << " A (" << s << ")" << endl << endl;
533  }
534  }
535 
536  delete[] num;
537  delete p;
538 
539  return 0;
540 }
541 
560  Bimage* pfsmask, double hires, double lores, double max_shift, int shift_flag, double bias)
561 {
562  if ( max_shift <= 0 ) max_shift = 1e37;
563 
564  random_seed();
565 
566  Bimage* p = read_img(model->fmap, 1, model->img_num);
567  if ( p == NULL ) {
568  cerr << "Error: No model map file read! (" << model->fmap << ")" << endl;
569  bexit(-1);
570  }
571 
572  p->change_type(Float) ;
573  ptemp->change_type(Float) ;
574 
575  if ( pmask ) ptemp->multiply(pmask);
576 
577  if ( hires > lores ) swap(hires, lores);
578 
579  double radius = ptemp->sizeX()/2;
580 
581  if ( verbose ) {
582  cout << "Refining link positions:" << endl;
583  cout << "Resolution limits: " << hires << " " << lores << " A" << endl;
584  cout << "Maximum shift: " << max_shift << " A" << endl;
585  cout << "Density radius: " << radius << endl;
586  cout << "Bias: " << bias << endl << endl;
587  }
588 
589  long n, nlink, imax(0);
590  Vector3<long> size(ptemp->size());
591  Vector3<double> origin(ptemp->image->origin());
592  Vector3<double> map_origin(p->image->origin());
593  Vector3<double> scale(ptemp->sampling(0));
594  Vector3<double> link_shift, shift_avg, link_center;
595  Vector3<double> shift;
596  Bimage* plink = NULL;
597  Bimage* pone = NULL;
598  Bimage* pmaskrot = NULL;
599 
600  Blink* link = NULL;
601  Matrix3 mat;
602  double ccmax, s, d1, d2, ccavg(0), d1avg(0), d2avg(0);
603  int* num = new int[ptemp->images()];
604  double* cc = new double[ptemp->images()];
605  for ( n=0; n<ptemp->images(); n++ ) cc[n] = num[n] = 0;
606 
607  fft_plan planf = ptemp->fft_setup(FFTW_FORWARD, 1);
608  fft_plan planb = ptemp->fft_setup(FFTW_BACKWARD, 1);
609 
610  if ( verbose ) {
611  cout << "Bond\tdx\tdy\tdz\tShift\tDens1\tDens2\tdDens";
612  for ( n=0; n<ptemp->images(); n++ ) cout << "\tCC" << n+1;
613  cout << endl;
614  }
615  for ( nlink=0, link = model->link; link; link = link->next, nlink++ ) if ( link->sel ) {
616  link_center = (link->comp[0]->loc + link->comp[1]->loc)*0.5;
617  d1 = p->density(0, link_center/scale + map_origin, radius);
618  mat = get_link_matrix(link);
619  plink = p->extract(0, map_origin + link_center/p->sampling(0)[0], size, origin, mat);
620  if ( pmask ) plink->multiply(pmask);
621  if ( pfsmask ) pmaskrot = pfsmask->extract_wrap(0, size, mat);
622  ccmax = -1;
623  for ( n=0; n<ptemp->images(); n++ ) {
624  pone = ptemp->extract(n);
625  pone->origin(origin);
626  shift = plink->find_shift(pone, pmaskrot, hires, lores, ptemp->sizeX()/10.0, 0, 1, planf, planb, cc[n]);
627  if ( n==0 ) cc[n] *= bias;
628  if ( ccmax < cc[n] ) {
629  ccmax = cc[n];
630  imax = n;
631  if ( shift_flag == 1 ) shift[0] = shift[1] = 0; // Shift only along the normal
632  else if ( shift_flag == 2 ) shift[2] = 0; // Shift only lateral
633  link_shift = scale * (mat * shift);
634  }
635  delete pone;
636  }
637  delete plink;
638  delete pmaskrot;
639  num[imax]++;
640  s = link_shift.length();
641  if ( s > max_shift ) {
642  link_shift *= max_shift/s;
643  s = max_shift;
644  }
645  link->comp[0]->loc += link_shift;
646  link->comp[1]->loc += link_shift;
647  link_center = (link->comp[0]->loc + link->comp[1]->loc)*0.5;
648  link->sel = imax;
649  link->fom = ccmax;
650  shift_avg += link_shift;
651  ccavg += ccmax;// d2 = img_density_at_coordinates(p, 0, link_center/scale + map_origin, radius);
652  d2 = p->density(0, link_center/scale + map_origin, radius);
653  d1avg += d1;
654  d2avg += d2;
655  if ( verbose ) {
656  cout << nlink+1 << tab << link_shift[0] << tab << link_shift[1] << tab <<
657  link_shift[2] << tab << s << tab << d1 << tab << d2 << tab << d2 - d1;
658  for ( n=0; n<ptemp->images(); n++ ) cout << tab << cc[n];
659  cout << endl;
660  }
661  }
662 
665 
666  shift_avg /= nlink;
667  s = shift_avg.length();
668  ccavg /= nlink;
669  d1avg /= nlink;
670  d2avg /= nlink;
671 
672  delete[] cc;
673  delete[] num;
674  delete p;
675 
676  if ( verbose ) {
677  cout << "Average\t" << shift_avg[0] << tab << shift_avg[1] << tab <<
678  shift_avg[2] << tab << s << tab << d1avg << tab << d2avg << tab <<
679  d2avg - d1avg << tab << ccavg << endl << endl;
680  cout << "Distribution:";
681  for ( n=0; n<ptemp->images(); n++ ) cout << tab << num[n];
682  cout << endl << endl;
683  }
684 
685  return 0;
686 }
687 
704 {
705  if ( origin[0] == 0 && origin[1] == 0 && origin[2] == 0 )
706  origin = size/2;
707 
708  if ( npt < 1 ) npt = 1;
709 
710  long i, j, n, navg, nc(0);
711  Bmodel* mp;
712  Bcomponent* comp;
713  Bcomptype* ct = NULL;
714  Bimage* p = NULL;
715  Bstring* comptypelist = NULL;
716  Bstring* comptype = NULL;
717  Bstring* temptype = NULL;
718 
719  // Compile a list of component types
720  for ( navg=0, mp = model; mp; mp = mp->next ) if ( mp->sel ) {
721  for ( ct = mp->type; ct; ct = ct->next ) if ( ct->sel ) {
722  for ( temptype = comptypelist; temptype && *temptype != ct->id; temptype = temptype->next ) ;
723  if ( !temptype ) {
724  comptype = string_add(&comptype, ct->id);
725  if ( !comptypelist ) comptypelist = comptype;
726  navg++;
727  }
728  }
729  }
730 
731  // Associate a consistent image number to each component type
732  for ( mp = model; mp; mp = mp->next ) if ( mp->sel ) {
733  for ( ct = mp->type; ct; ct = ct->next ) if ( ct->sel ) {
734  for ( n=0, temptype = comptypelist; temptype && *temptype != ct->id; temptype = temptype->next ) n++;
735  ct->num = n;
736  }
737  }
738 
739  string_kill(comptypelist);
740 
741  if ( verbose ) {
742  cout << "Averaging component densities:" << endl;
743  cout << "Number of selected types: " << navg << endl;
744  cout << "Number of averages per type: " << npt << endl;
745  cout << "Size: " << size << endl;
746  cout << "Origin: " << origin << endl;
747  }
748 
749  navg *= npt;
750 
751  int* ncomp = new int[navg];
752  for ( n=0; n<navg; n++ ) ncomp[n] = 0;
753 
754  Bimage* pcomp = new Bimage(Float, TSimple, size, navg);
755  pcomp->origin(origin);
756 
757  long imgsize = pcomp->sizeX()*pcomp->sizeY()*pcomp->sizeZ();
758  double radius = origin[0];
759  Vector3<double> imgori, loc;
760  Matrix3 mat(1);
761  Bimage* pone = NULL;
762  pcomp->next = new Bimage(Float, TSimple, pcomp->size(), pcomp->images());
763  float* fom = (float *) pcomp->next->data_pointer();
764 
765  for ( mp = model; mp; mp = mp->next ) if ( mp->sel ) {
766  p = read_img(mp->fmap, 1, mp->img_num);
767  if ( p == NULL ) {
768  cerr << "Error: No model map file read! (" << mp->fmap << ")" << endl;
769  bexit(-1);
770  }
771  pcomp->sampling(p->sampling(0));
772  p->change_type(Float) ;
773  imgori = p->image->origin();
774  if ( verbose & VERB_PROCESS ) {
775  cout << "Model: " << mp->id << endl;
776  cout << "Map: " << mp->fmap << " (" << mp->img_num << ")" << endl;
777  cout << "Comp\tAvg#\tvx\tvy\tvz\tva" << endl;
778  }
779  for ( comp = mp->comp; comp; comp = comp->next ) if ( comp->sel ) {
780  ct = comp->type;
781  n = ct->num * npt + nc%npt;
782  ncomp[n]++;
783 // if ( verbose & VERB_PROCESS )
784  cout << comp->id << tab << n+1 << tab << comp->view << endl;
785  mat = comp->view.matrix();
786  loc = imgori + comp->loc/p->sampling(0);
787  comp->density = p->density(0, loc, radius);
788  pone = p->extract(0, loc, size, origin, comp->view);
789  for ( i=n*imgsize, j=0; j<imgsize; i++, j++ ) {
790  pcomp->add(i, (*pone)[j]);
791  fom[i] += (*pone)[j]*(*pone)[j];
792  }
793  delete pone;
794  nc++;
795  }
796  delete p;
797  }
798 
799  for ( i=n=0; n<navg; n++ ) if ( ncomp[n] ) {
800  pcomp->multiply(n, 1.0L/ncomp[n]);
801  for ( j=0; j<imgsize; i++, j++ ) {
802  fom[i] = fom[i]/ncomp[n] - (*pcomp)[i]*(*pcomp)[i];
803  if ( fom[i] > 0 ) fom[i] = sqrt(fom[i]);
804  else fom[i] = 0;
805  }
806  }
807 
808  if ( verbose ) {
809  cout << "Component types:\n#\tType\tCount" << endl;
810  for ( n=0, ct = model->type; ct; ct = ct->next ) if ( ct->sel ) {
811  for ( i=0; i<npt; i++, n++ )
812  cout << n+1 << tab << ct->id << tab << ncomp[n] << endl;
813  }
814  cout << endl;
815  }
816 
817  delete[] ncomp;
818 
819  return pcomp;
820 }
821 
837 {
838  Bimage* p = read_img(model->fmap, 1, model->img_num);
839  if ( p == NULL ) {
840  cerr << "Error: No model map file read! (" << model->fmap << ")" << endl;
841  bexit(-1);
842  }
843 
844  p->change_type(Float) ;
845 
846  long ncomp(0);
847 
848  if ( origin[0] == 0 && origin[1] == 0 && origin[2] == 0 )
849  origin = Vector3<double>(size/2);
850 
851  Bcomponent** carr = component_get_array(model, ncomp);
852 
853  if ( verbose ) {
854  cout << "Extracting component densities:" << endl;
855  cout << "Number of selected components: " << ncomp << endl;
856  cout << "Size: " << size << endl;
857  cout << "Origin: " << origin << endl;
858  }
859 
860  double radius = origin[0];
861 
862  Bimage* pcomp = new Bimage(Float, p->compound_type(), size, ncomp);
863  pcomp->origin(origin);
864  pcomp->sampling(p->sampling(0));
865 
866  Vector3<double> imgori(p->image->origin());
867 
868  if ( verbose & VERB_PROCESS ) {
869  cout << "Model: " << model->id << endl;
870  cout << "Map: " << model->fmap << " (" << model->img_num << ")" << endl;
871  cout << "Map origin: " << p->image->origin() << endl;
872  cout << "Comp\tx\ty\tz\tvx\tvy\tvz\tva" << endl;
873  }
874 
875 #ifdef HAVE_GCD
876  dispatch_apply(ncomp, dispatch_get_global_queue(0, 0), ^(size_t i){
877  Bcomponent* comp = carr[i];
878  Vector3<double> loc(comp->loc/p->sampling(0) + imgori);
879  comp->density = p->density(0, loc, radius);
880  Bimage* pone = p->extract(0, loc, size, origin, comp->view);
881  pcomp->replace(i, pone);
882  delete pone;
883  if ( verbose & VERB_PROCESS )
884  cout << comp->id << tab << loc << tab << comp->view << endl;
885  });
886 #else
887 #pragma omp parallel for
888  for ( long i=0; i<ncomp; i++ ) {
889  Bcomponent* comp = carr[i];
890  Vector3<double> loc(comp->loc/p->sampling(0) + imgori);
891  comp->density = p->density(0, loc, radius);
892  Bimage* pone = p->extract(0, loc, size, origin, comp->view);
893  pcomp->replace(i, pone);
894  delete pone;
895  if ( verbose & VERB_PROCESS )
896  cout << comp->id << tab << loc << tab << comp->view << endl;
897  }
898 #endif
899 
900  delete p;
901  delete[] carr;
902 
903  return pcomp;
904 }
905 
918 {
919  if ( origin[0] == 0 && origin[1] == 0 && origin[2] == 0 )
920  origin = Vector3<double>(size[0]/2, size[1]/2, size[2]/2);
921 
922  long i, j, n, navg;
923  Bmodel* mp;
924  Blink* link;
925  Matrix3 mat;
926  Bimage* p = NULL;
927 
928  for ( navg=0, link = model->link; link; link = link->next )
929  if ( link->sel > navg ) navg = link->sel;
930 
931  if ( verbose ) {
932  cout << "Averaging link density" << endl << endl;
933  cout << "Number of averages: " << navg << endl;
934  cout << "Size: " << size << endl;
935  cout << "Origin: " << origin << endl;
936  }
937 
938  int* nlink = new int[navg+1];
939  for ( n=0; n<=navg; n++ ) nlink[n] = 0;
940 
941  Bimage* plink = new Bimage(Float, TSimple, size, navg);
942  plink->origin(origin);
943 
944  long imgsize = plink->sizeX()*plink->sizeY()*plink->sizeZ();
945  Vector3<double> imgori, loc;
946  Bimage* pone = NULL;
947  plink->next = new Bimage(Float, TSimple, plink->size(), plink->images());
948  float* fom = (float *) plink->next->data_pointer();
949 
950  for ( mp = model; mp; mp = mp->next ) if ( mp->sel ) {
951  p = read_img(mp->fmap, 1, mp->img_num);
952  if ( p == NULL ) {
953  cerr << "Error: No model map file read! (" << mp->fmap << ")" << endl;
954  bexit(-1);
955  }
956  plink->sampling(p->sampling(0));
957  p->change_type(Float) ;
958  imgori = p->image->origin();
959  if ( verbose & VERB_PROCESS )
960  cout << "Comp1\tComp2\tAvg#" << endl;
961  for ( link = mp->link; link; link = link->next ) if ( link->sel ) {
962  n = (long) link->sel;
963  nlink[n]++;
964  if ( n ) {
965  if ( verbose & VERB_PROCESS )
966  cout << link->comp[0]->id << tab << link->comp[1]->id << tab << n << endl;
967  mat = get_link_matrix(link);
968  loc = imgori + (link->comp[1]->loc + link->comp[0]->loc)/(2*p->sampling(0)[0]);
969 // pone = img_extract_density(p, size, origin, loc, mat);
970  pone = p->extract(0, loc, size, origin, mat);
971  for ( i=(n-1)*imgsize, j=0; j<imgsize; i++, j++ ) {
972  plink->add(i, (*pone)[j]);
973  fom[i] += (*pone)[j]*(*pone)[j];
974  }
975  delete pone;
976  }
977  }
978  delete p;
979  }
980 
981  for ( i=n=0; n<navg; n++ ) if ( nlink[n] )
982  plink->multiply(n, 1.0L/nlink[n]);
983  for ( j=0; j<imgsize; i++, j++ ) {
984  fom[i] = fom[i]/nlink[n] - (*plink)[i]*(*plink)[i];
985  if ( fom[i] > 0 ) fom[i] = sqrt(fom[i]);
986  else fom[i] = 0;
987  }
988 
989  delete[] nlink;
990 
991  return plink;
992 }
993 
1009  Vector3<double> origin, int flags)
1010 {
1011  long i, ntype(1), nmap(1), nb;
1012  Bcomptype* ct = NULL;
1013  Bcomponent* comp;
1014  Bimage* p = NULL;
1015  Vector3<double> sampling;
1016 
1017  // Set up for the number of component types and their image numbers
1018  for ( ntype = 0, ct = model->type; ct; ct = ct->next )
1019  if ( ct->sel ) ntype++;
1020  for ( ct = model->type; ct; ct = ct->next )
1021  if ( ct->sel ) break;
1022  if ( flags & 2 ) nmap = ntype;
1023 
1024  if ( !ct ) {
1025  cerr << "Error: No component types defined for model " << model->id << endl;
1026  bexit(-1);
1027  }
1028 
1029  if ( file_type(ct->fmod) == Image ) {
1030  p = read_img(ct->fmod, 0, ct->num);
1031  if ( !p ) {
1032  cerr << "Error: No component map file read! (" << ct->fmod << ")" << endl;
1033  bexit(-1);
1034  }
1035  sampling = p->sampling(0);
1036  delete p;
1037  p = NULL;
1038  }
1039 
1040  if ( origin[0] == 0 && origin[1] == 0 && origin[2] == 0 )
1041  origin = size/2;
1042 
1043  Bimage* pmap = new Bimage(Float, TSimple, size, nmap);
1044  pmap->origin(origin);
1045  pmap->sampling(sampling);
1046 
1047  if ( verbose ) {
1048  cout << "Building new maps from component densities:" << endl;
1049  cout << "Component types selected: " << ntype << endl;
1050  cout << "Number of maps to build: " << pmap->images() << endl;
1051  cout << "Size: " << pmap->size() << endl;
1052  cout << "Origin: " << origin << endl;
1053  }
1054 
1055  float* fom = NULL;
1056  if ( flags & 1 ) {
1057  pmap->next = new Bimage(Float, TSimple, pmap->size(), pmap->images());
1058  fom = (float *) pmap->next->data_pointer();
1059  }
1060 
1061  long x, y, z, n, ncomp, nlink;
1062  double radius, linklength, weight;
1063  Vector3<double> v, loc, ori_comp, ori_map(pmap->image->origin());
1064  Matrix3 mat;
1065 
1066  for ( n = 0, ct = model->type; ct; ct = ct->next )
1067  if ( ct->sel && file_type(ct->fmod) == Image ) {
1068  p = read_img(ct->fmod, 1, ct->num);
1069  if ( !p ) {
1070  cerr << "Error: No component map file read! (" << ct->fmod << ")" << endl;
1071  bexit(-1);
1072  }
1073  p->change_type(Float) ;
1074  if ( verbose & VERB_PROCESS )
1075  cout << "Comp\tType\tFile\tImage" << endl;
1076  for ( ncomp=nb=0, comp = model->comp; comp; comp = comp->next, ncomp++ )
1077  if ( comp->sel && comp->type == ct ) {
1078  if ( verbose & VERB_PROCESS )
1079  cout << comp->id << tab << n+1 << tab << ct->fmod << tab << ct->num << endl;
1080  ori_comp = ori_map + comp->loc/p->sampling(0)[0];
1081  mat = comp->view.matrix();
1082  for ( i=0, nlink=0, linklength=0; comp->link[i]; i++ ) {
1083  linklength += comp->loc.distance(comp->link[i]->loc);
1084  nlink++;
1085  }
1086  if ( nlink ) linklength /= 2*nlink;
1087  for ( z = 1; z < p->sizeZ()-1; z++ ) {
1088  v[2] = z - p->image->origin()[2];
1089  for ( y = 1; y < p->sizeY()-1; y++ ) {
1090  v[1] = y - p->image->origin()[1];
1091  for ( x = 1; x < p->sizeX()-1; x++ ) {
1092  v[0] = x - p->image->origin()[0];
1093  loc = (mat * v) + ori_comp;
1094  if ( loc[0]>=0 && loc[0]<=pmap->sizeX()-2 && loc[1]>=0 && loc[1]<=pmap->sizeY()-2 && loc[2]>=0 && loc[2]<=pmap->sizeZ()-2 ) {
1095  i = p->index(0, x, y, z, 0);
1096  weight = 1;
1097  if ( nlink ) {
1098  radius = v.length() - linklength;
1099  if ( radius > 0 ) weight = 1 - 0.002*radius*radius;
1100  }
1101  if ( weight > 0 ) {
1102  if ( flags & 1 )
1103  pmap->add(loc[0], loc[1], loc[2], n, weight*(*p)[i]);
1104  else
1105  pmap->set_max(loc[0], loc[1], loc[2], n, weight*(*p)[i]);
1106  if ( fom ) fom[pmap->index(loc, n)] += weight;
1107  }
1108  }
1109  }
1110  }
1111  }
1112  nb++;
1113  }
1114  delete p;
1115  if ( flags & 2 ) n++;
1116  if ( verbose )
1117  cout << "Built " << nb << " components of type " << ct->id << " into map" << endl;
1118  }
1119 
1120  if ( flags & 1 ) for ( i=0; i<pmap->data_size(); i++ )
1121  if ( fom[i] ) pmap->set(i, (*pmap)[i]/fom[i]);
1122 
1123  return pmap;
1124 }
1125 
1140  Vector3<long> size, Vector3<double> origin, int link_select, int flags)
1141 {
1142  Bimage* p = read_img(linkmap, 1, -1);
1143  if ( p == NULL ) {
1144  cerr << "Error: No link map file read! (" << model->type->fmod << ")" << endl;
1145  bexit(-1);
1146  }
1147 
1148  p->change_type(Float) ;
1149 
1150  if ( origin[0] == 0 && origin[1] == 0 && origin[2] == 0 )
1151  origin = Vector3<double>(size[0]/2, size[1]/2, size[2]/2);
1152 
1153  long i;
1154 
1155  Bimage* pmap = new Bimage(Float, p->compound_type(), size[0], size[1], size[2], p->images());
1156  pmap->origin(origin);
1157  pmap->sampling(p->sampling(0));
1158 
1159  if ( verbose ) {
1160  cout << "Building a new map from a link density" << endl << endl;
1161  cout << "Size: " << pmap->size() << endl;
1162  cout << "Origin: " << origin << endl;
1163  }
1164 
1165  pmap->next = new Bimage(Float, TSimple, pmap->size(), pmap->images());
1166  float* fom = (float *) pmap->next->data_pointer();
1167 
1168  long x, y, z, n, xmin, xmax, nlink;
1169  double linklength, fx, weight;
1170  Vector3<double> v, loc, ori_link;
1171  Matrix3 mat;
1172  Blink* link;
1173 
1174  if ( verbose & VERB_PROCESS )
1175  cout << "Comp1\tComp2\tMap" << endl;
1176  for ( nlink=0, link = model->link; link; link = link->next, nlink++ ) if ( link->sel ) {
1177  n = (long) link->sel - 1;
1178  if ( n >= p->images() ) n = p->images() - 1;
1179  if ( link_select < 1 || link_select == n+1 ) {
1180  if ( verbose & VERB_PROCESS )
1181  cout << link->comp[0]->id << tab << link->comp[1]->id << tab << n+1 << endl;
1182  linklength = link->comp[1]->loc.distance(link->comp[0]->loc);
1183  ori_link = origin + (link->comp[1]->loc + link->comp[0]->loc)/(2*p->sampling(0)[0]);
1184  xmin = (long) (p->image->origin()[0] - linklength/(2*p->sampling(0)[0]) + 0.5);
1185  xmax = (long) (p->image->origin()[0] + linklength/(2*p->sampling(0)[0]) + 0.5);
1186  mat = get_link_matrix(link);
1187  for ( z = 1; z < p->sizeZ()-1; z++ ) {
1188  v[2] = z - p->image->origin()[2];
1189  for ( y = 1; y < p->sizeY()-1; y++ ) {
1190  v[1] = y - p->image->origin()[1];
1191  for ( x = 1; x < p->sizeX()-1; x++ ) {
1192  v[0] = x - p->image->origin()[0];
1193  loc = (mat * v) + ori_link;
1194  if ( loc[0]>=0 && loc[0]<=pmap->sizeX()-2 && loc[1]>=0 && loc[1]<=pmap->sizeY()-2 && loc[2]>=0 && loc[2]<=pmap->sizeZ()-2 ) {
1195  i = p->index(0, x, y, z, n);
1196  weight = 1;
1197  fx = 0;
1198  if ( x < xmin ) fx = xmin - x;
1199  else if ( x > xmax ) fx = x - xmax;
1200  if ( fx ) weight = 1 - 0.002*fx*fx;
1201  if ( weight > 0 )
1202  pmap->add(loc[0], loc[1], loc[2], 0, weight*(*p)[i]);
1203  }
1204  }
1205  }
1206  }
1207  }
1208  }
1209 
1210  if ( flags & 1 ) for ( i=0; i<pmap->data_size(); i++ )
1211  if ( fom[i] ) pmap->set(i, (*pmap)[i]/fom[i]);
1212 
1213  delete p;
1214 
1215  return pmap;
1216 }
1217 
Bimage::fft_setup
fft_plan fft_setup(fft_direction dir, int opt=0)
Sets up a plan for fast Fourier transforms.
Definition: Bimage_fft.cpp:29
comp_type_kill
int comp_type_kill(Bcomptype *type)
Deallocates memory for one component type.
Definition: rwmodel.cpp:915
View::next
View * next
Definition: View.h:28
Bcomponent::next
Bcomponent * next
Definition: rwmodel.h:52
link_select
Tcl_Obj * link_select(Bmodel *model, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bmodel.cpp:1491
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
Bcomponent
Definition: rwmodel.h:51
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:768
Bcomptype::fmod
Bstring fmod
Definition: rwmodel.h:29
kill_list
long kill_list(char *list, unsigned long size)
Frees all the items in a linked list.
Definition: linked_list.cpp:214
Bcomponent::loc
Vector3< float > loc
Definition: rwmodel.h:58
file_util.h
Header file for file checking functions.
Bcomptype::sel
int sel
Definition: rwmodel.h:34
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1118
Bstring
Definition: Bstring.h:29
Bimage::next
Bimage * next
Definition: Bimage.h:179
model_extract_build.h
Functions to extract subvolumes and build new maps.
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
Bcomponent::id
Bstring id
Definition: rwmodel.h:53
Matrix3::transpose
Matrix3 transpose() const
Definition: Matrix3.h:268
base
string base(string &filename)
Definition: string_util.cpp:28
verbose
int verbose
Bimage::extract_wrap
Bimage * extract_wrap(long nn, Vector3< double > loc, Vector3< long > size, Vector3< double > origin, Matrix3 mat)
Extracts a region of one sub-image into new image structure with wrapping.
Definition: Bimage_extract.cpp:151
Bcomptype
Definition: rwmodel.h:25
Vector3< double >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
random_numbers.h
Header file for functions for creating random images.
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
Bmodel::next
Bmodel * next
Definition: rwmodel.h:120
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
View
Definition: View.h:26
component_find_shift2
Vector3< double > component_find_shift2(Bcomponent *comp, Bimage *p, Bimage *ptemp, Bimage *pmask, Bimage *pfsmask, View view, double hires, double lores, double shift_limit, int shift_flag, fft_plan planf, fft_plan planb)
Definition: model_extract_build.cpp:105
Bimage::find_shift
int find_shift(Bimage *pref, Bimage *pmask, double hires, double lores, double radius, double sigma, int refine_flag)
Calculates a cross-correlation map to find the shift for the pair of images.
Definition: Bimage_correlate.cpp:929
Bmodel::link
Blink * link
Definition: rwmodel.h:133
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
count_list
long count_list(char *list)
Counts the number of items in a linked list.
Definition: linked_list.cpp:235
component_get_array
Bcomponent ** component_get_array(Bmodel *model, long &ncomp)
Generates an array of pointers to component structures.
Definition: model_util.cpp:903
get_link_matrix
Matrix3 get_link_matrix(Blink *link)
Definition: model_extract_build.cpp:29
Bmodel::fmap
Bstring fmap
Definition: rwmodel.h:125
planf
fft_plan planf
Definition: bico.cpp:24
Vector3::distance
double distance(const Vector3 &v)
Definition: Vector3.h:227
Bimage::density
double density(long nn, Vector3< double > coord, double radius, double &sigma)
Calculates the density in a sphere around a coordinate in an image.
Definition: Bimage.cpp:3012
Bimage::images
long images()
Definition: Bimage.h:504
Bmodel::comp
Bcomponent * comp
Definition: rwmodel.h:132
Matrix3
Definition: Matrix3.h:18
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:77
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
Bcomponent::vec
Vector3< float > vec
Definition: rwmodel.h:60
Bimage::multiply
void multiply(long j, Complex< double > cv)
Definition: Bimage.h:325
TSimple
@ TSimple
Definition: Bimage.h:59
Bimage::add
void add(long j, double v)
Definition: Bimage.h:321
component_refine_random
int component_refine_random(Bcomponent *comp, Bimage *p, Bimage *ptemp, Bimage *pmask, Bimage *pfsmask, int max_iter, double hires, double lores, double max_shift, double max_view_angle, double max_rot_angle, int shift_flag, fft_plan planf, fft_plan planb)
Definition: model_extract_build.cpp:154
model_average_component_density
Bimage * model_average_component_density(Bmodel *model, Vector3< long > size, Vector3< double > origin, int npt)
Averages the density associated with each component type in a model.
Definition: model_extract_build.cpp:703
Vector3::normalize
double normalize()
Definition: Vector3.h:238
model_add_type_by_id_and_filename
Bcomptype * model_add_type_by_id_and_filename(Bmodel *model, Bstring &id, Bstring &filename, int num)
Add a type to a model if the type with the given id and file name does not exist already.
Definition: rwmodel.cpp:468
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
fft_destroy_plan
int fft_destroy_plan(fft_plan plan)
Deallocates a plan for fast Fourier transforms.
Definition: fft.cpp:87
Bmodel::id
Bstring id
Definition: rwmodel.h:121
Bimage::rotate
int rotate()
Rotates an image using parameters defined in the image in place.
Definition: Bimage_transform.cpp:255
Bimage::data_size
long data_size()
Definition: Bimage.h:270
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:364
Bimage::set_max
void set_max(double xx, double yy, double zz, long nn, double v)
Sets a value at a given location to neigboring data elements if it is larger.
Definition: Bimage.cpp:1303
Bmodel::sel
int sel
Definition: rwmodel.h:129
Bimage::replace
int replace(Bimage *img)
Replaces the data with that from the given image.
Definition: Bimage.cpp:2830
model_build_from_component_density
Bimage * model_build_from_component_density(Bmodel *model, Vector3< long > size, Vector3< double > origin, int flags)
Builds a new map from a densities of components in a model.
Definition: model_extract_build.cpp:1008
Bimage.h
Header file for image class.
Bmodel
Definition: rwmodel.h:119
views_within_limits
View * views_within_limits(View theview, double theta_step, double phi_step, double alpha_step, double view_angle_limit, double alpha_angle_limit)
Generates a list of views within an angular distance from the input view.
Definition: View.cpp:207
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
component_find_shift
Vector3< double > component_find_shift(Bcomponent *comp, Bimage *p, Bimage *ptemp, Bimage *pmask, Bimage *pfsmask, View view, double hires, double lores, double shift_limit, int shift_flag, fft_plan planf, fft_plan planb)
Definition: model_extract_build.cpp:56
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
Image
@ Image
Definition: file_util.h:22
Bmodel::img_num
int img_num
Definition: rwmodel.h:127
Bcomponent::view
View view
Definition: rwmodel.h:59
Bcomptype::num
int num
Definition: rwmodel.h:30
Bcomponent::fom
float fom
Definition: rwmodel.h:65
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
random_seed
long random_seed()
Gets a random seed and sets the flag.
Definition: random_numbers.cpp:41
Bcomponent::type
Bcomptype * type
Definition: rwmodel.h:54
Bcomptype::id
Bstring id
Definition: rwmodel.h:27
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
model
Bmodel * model
Definition: bshow.cpp:34
Bimage::index
long index(long nx, long ny) const
Definition: Bimage.h:396
Bstring::next
Bstring * next
Definition: Bstring.h:31
Bimage::extract
Bimage * extract(long nn)
Extracts one sub-image into new image structure.
Definition: Bimage_extract.cpp:21
fft_plan
fftwf_plan fft_plan
Definition: fft.h:20
component_refine
int component_refine(Bcomponent *comp, Bimage *p, Bimage *ptemp, Bimage *pmask, Bimage *pfsmask, double viewmax, double rotmax, double viewstep, double rotstep, double hires, double lores, double accuracy, double max_shift, int shift_flag, fft_plan planf, fft_plan planb)
Definition: model_extract_build.cpp:207
linked_list.h
Header file for generalized linked list functions.
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:284
model_build_from_link_density
Bimage * model_build_from_link_density(Bmodel *model, Bstring &linkmap, Vector3< long > size, Vector3< double > origin, int link_select, int flags)
Builds a new map from a density of a link in a model.
Definition: model_extract_build.cpp:1139
model_extract_component_densities
Bimage * model_extract_component_densities(Bmodel *model, Vector3< long > size, Vector3< double > origin)
Extracts all densities associated with components in a model.
Definition: model_extract_build.cpp:836
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
irm
double irm(1.0L/get_rand_max())
planb
fft_plan planb
Definition: bico.cpp:24
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
Bmodel::type
Bcomptype * type
Definition: rwmodel.h:131
Bcomptype::next
Bcomptype * next
Definition: rwmodel.h:26
View::matrix
Matrix3 matrix()
Definition: View.h:153
get_rand_max
long get_rand_max()
Finds the maximum random number for a system.
Definition: random_numbers.cpp:22
Vector3::length
double length()
Definition: Vector3.h:222
model_refine_components
int model_refine_components(Bmodel *model, Bstring *ct_names, Bimage *ptemp, Bimage *pmask, Bimage *pfsmask, int max_iter, double viewstep, double rotstep, double hires, double lores, double accuracy, double max_shift, double max_view_angle, double max_rot_angle, int shift_flag)
Refines component views and positions by cross-correlation.
Definition: model_extract_build.cpp:300
Float
@ Float
Definition: utilities.h:161
Bcomponent::link
Bcomponent * link[MAXLINK]
Definition: rwmodel.h:55
Bcomponent::density
float density
Definition: rwmodel.h:64
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:713
model_refine_link_positions
int model_refine_link_positions(Bmodel *model, Bimage *ptemp, Bimage *pmask, Bimage *pfsmask, double hires, double lores, double max_shift, int shift_flag, double bias)
Refines link positions by cross-correlation.
Definition: model_extract_build.cpp:559
Vector3::cross
Vector3 cross(const Vector3 &v)
Definition: Vector3.h:252
model_average_link_density
Bimage * model_average_link_density(Bmodel *model, Vector3< long > size, Vector3< double > origin)
Extracts a density associated with each link in a model.
Definition: model_extract_build.cpp:917
Bcomponent::sel
int sel
Definition: rwmodel.h:66
file_type
FileType file_type(const char *filename)
Checks the file type using the extension and contents.
Definition: file_util.cpp:178
Bimage::rescale_to_avg_std
int rescale_to_avg_std(double nuavg, double nustd)
Rescales the image data to a given average and standard deviation.
Definition: Bimage_rescale.cpp:163