Bsoft  2.1.0
Bernard's software package
bfind.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "mg_processing.h"
11 #include "mg_particle_select.h"
12 #include "rwmg.h"
13 #include "symmetry.h"
14 #include "ps_views.h"
15 #include "linked_list.h"
16 #include "file_util.h"
17 #include "options.h"
18 #include "utilities.h"
19 #include "timer.h"
20 
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 
24 // Declaration of global variables
25 extern int verbose; // Level of output to the screen
26 
27 // Function prototypes
28 vector<Bimage*> setup_images(Bstring& map_file, Bstring& template_file,
29  Bstring& rs_mask, Bstring& fs_mask,
30  int read_img_num, Vector3<long>& bin, Vector3<double> sam,
31  int set_origin, Vector3<double>& origin, Vector3<double>& part_ori);
32 
33 // Usage assistance
34 const char* use[] = {
35 " ",
36 "Usage: bfind [options] input.img/input.star output.img",
37 "------------------------------------------------------",
38 "Searches orientation space for the best fit of a 3D map to a template.",
39 "Modes: Global: Search an asymmetric unit with a fixed angular step size.",
40 " Directional: Do a search around a view within an angular distance (-limit option).",
41 " Refine: Refine iteratively around a view to some accuracy (-accuracy option).",
42 " Symmetry: Search all symmetry-related views using a given view.",
43 "For modes other than global, the view is taken from the command line (-View option),",
44 " or from the parameter file, or as a last resort from the image header.",
45 "If a parameter file is used as input, the input image is selected based on ",
46 " the reconstruction ID and the particle image number.",
47 "Only one image in a multi-image file will be used (see -image option).",
48 "The output file contains the transformed map only if the input is an image (i.e., not a parameter file).",
49 "Specify the output of the transformed map or template with the",
50 " -particle and -newtemplate options.",
51 " ",
52 "Actions:",
53 "-global 2 Global search before other modes with binning.",
54 "-mode refine Local modes: directional, refine or symmetric.",
55 "-setsequential Set particle selection numbers sequentially.",
56 "-subset 25,5 View subset selection from a start offset and with a given size.",
57 "-rescale -0.1,5.2 Rescale output particle to average and standard deviation.",
58 " ",
59 "Parameters:",
60 "-verbose 7 Verbosity of output.",
61 "-datatype u Force writing of a new data type.",
62 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; default from input file; a single value can be given).",
63 "-origin 110,50,44 Template image origin (default taken from template file).",
64 "-partorigin 50,80,47 Particle image origin (default taken from particle file).",
65 "-angles 8.8,2.5,5.2 Step size for alpha, theta and phi, one value sets all (default 45 degrees).",
66 "-side 15 Generate side view projections within the given angle from the equator.",
67 "-resolution 15.6,200 Resolution limits for cross-correlation (default 0).",
68 "-shiftlimit 5.2 Radial limit to search for shift in cross-correlation map (default 1/4 of map).",
69 "-recid A435 The 3D reconstruction ID (should correspond to that in the parameter file).",
70 "-image 2 Image number in a multi-image file or particle number (default 1=first image).",
71 "-select 5 Particle selection number in parameter file (default not used).",
72 "-bin 3 Binning of input, template and mask by given kernel size,",
73 " output image remains same size as input (only for local modes).",
74 " ",
75 "Parameters for the global and symmetric modes:",
76 "-symmetry D6 Symmetry: Point group identifier.",
77 " ",
78 "Paramweters for the directional and refinement modes:",
79 "-View 0.3,-0.5,0.8,33 View to initiate search: vector {xyz} and angle.",
80 " ",
81 "Parameters for the directional mode:",
82 "-limit 17.5 Radial limit around given view (default 20 degrees).",
83 " ",
84 "Parameters for the refinement mode:",
85 "-accuracy 0.3 Smallest angle step size (default 0.5 degrees).",
86 " ",
87 "Input:",
88 "-Template image.map Template to search for.",
89 "-mask rs_mask.mrc Mask in real space to apply before cross-correlation.",
90 "-Mask fs_mask.mrc Mask in reciprocal space to apply during cross-correlation.",
91 " ",
92 "Output:",
93 "-Postscript plot.ps Output postscript file with a plot of global view vectors.",
94 "-output file.star Parameter output file with best orientation.",
95 "-ppx Write temporary particle parameter files to directory \"ppx\".",
96 "-json file.json JSON single particle output file with best orientation.",
97 "-particle newpart.pif New transformed particle file.",
98 "-newtemplate newtemp.pif New transformed template file.",
99 " ",
100 NULL
101 };
102 
103 int main(int argc, char **argv)
104 {
105  // Initialize variables
106  Vector3<long> global_bin; // Flag and binning for global search
107  Vector3<long> bin(1,1,1); // No binning for local modes
108  Bstring mode; // Mode: directional, refine or symmetric
109  double nuavg(0), nustd(0); // Rescaling to average and stdev
110  int view_start(0); // Subset start
111  int view_subset(0); // Subset size
112  DataType nudatatype(Unknown_Type); // Conversion to new type
113  Bstring rec_id; // Reconstruction ID
114  int img_num(1); // Image number in file
115  int sel_num(-1); // Selection number in parameter file
116  int set_seq_sel(0); // Flag to set selection number sequentially
117  Vector3<double> sam; // Units for the three axes (A/pixel)
118  Vector3<double> temp_ori; // Template origin
119  Vector3<double> part_ori; // Particle origin
120  int set_origin(0); // Flag to set the template origin
121  double alpha_step(M_PI_4); // Angular step size for alpha
122  double theta_step(M_PI_4); // Angular step size for theta
123  double phi_step(M_PI_4); // Angular step size for phi
124  double side_ang(-1); // Side view variation angle
125  double angle_limit(M_PI/9.0); // Angular limit for directional search
126  Bsymmetry* sym = NULL; // Default: asymmetric or C1 point group
127  double hires(0), lores(0); // Limiting resolution for cross-correlation
128  double search_radius(-1); // Use default search radius
129  int currview_set(0); // Flag to indicate current view is set
130  View currview; // View to initiate search from and use as current view
131  View bestview; // Eventually the best view
132  View ref_view; // Reference view
133  double accuracy(0); // Accuracy for refinement: default at Nyquest
134  int flags(0); // Flags for processing options
135  Bstring template_file; // Reference template file name
136  Bstring rs_mask; // Real space mask file name
137  Bstring fs_mask; // Reciprocal space mask file name
138  Bstring ps_file; // Postscript output file name
139  Bstring param_file; // Parameter file name
140  Bstring jsfile; // JSON file for one particle
141  Bstring newpart_file; // Transformed particle output file name
142  Bstring newtemp_file; // Transformed template output file name
143 
144  int i, optind;
145  Boption* option = get_option_list(use, argc, argv, optind);
146  Boption* curropt;
147  for ( curropt = option; curropt; curropt = curropt->next ) {
148  if ( curropt->tag == "global" ) {
149  if ( ( global_bin[0] = curropt->value.integer() ) < 1 )
150  cerr << "-global: A bin size must be specified!" << endl;
151  else {
152  if ( global_bin[0] < 1 ) global_bin[0] = 1;
153  global_bin[2] = global_bin[1] = global_bin[0];
154  }
155  }
156  if ( curropt->tag == "mode" ) {
157  if ( curropt->value.length() > 0 ) {
158  if ( curropt->value[0] == 'd' || curropt->value[0] == 'D' ) mode = "directional";
159  if ( curropt->value[0] == 'r' || curropt->value[0] == 'R' ) mode = "refine";
160  if ( curropt->value[0] == 's' || curropt->value[0] == 'S' ) mode = "symmetric";
161  } else
162  cerr << "-mode: A mode must be specified" << endl;
163  }
164  if ( curropt->tag == "setsequential" ) set_seq_sel = 1;
165  if ( curropt->tag == "subset" )
166  if ( curropt->values(view_start, view_subset) < 2 )
167  cerr << "-subset: A view subset start and size must be specified" << endl;
168  if ( curropt->tag == "rescale" )
169  if ( curropt->values(nuavg, nustd) < 2 )
170  cerr << "-rescale: Both average and standard deviation must be specified!" << endl;
171  if ( curropt->tag == "image" )
172  if ( ( img_num = curropt->value.integer() ) < 1 )
173  cerr << "-image: An image number must be specified" << endl;
174  if ( curropt->tag == "select" )
175  if ( ( sel_num = curropt->value.integer() ) < 0 )
176  cerr << "-select: A selection number must be specified" << endl;
177  if ( curropt->tag == "datatype" )
178  nudatatype = curropt->datatype();
179  if ( curropt->tag == "sampling" )
180  sam = curropt->scale();
181  if ( curropt->tag == "origin" ) {
182  if ( curropt->value[0] == 'c' ) {
183  set_origin |= 2;
184  } else {
185  temp_ori = curropt->origin();
186  set_origin |= 1;
187  }
188  }
189  if ( curropt->tag == "partorigin" ) {
190  if ( curropt->value[0] == 'c' ) {
191  set_origin |= 8;
192  } else {
193  part_ori = curropt->origin();
194  set_origin |= 4;
195  }
196  }
197  if ( curropt->tag == "angles" ) {
198  if ( ( i = curropt->values(alpha_step, theta_step, phi_step) ) < 1 )
199  cerr << "-angles: An angle step size must be specified" << endl;
200  else {
201  if ( i < 2 ) phi_step = theta_step = alpha_step;
202  else if ( i < 3 ) phi_step = theta_step;
203  alpha_step *= M_PI/180.0;
204  theta_step *= M_PI/180.0;
205  phi_step *= M_PI/180.0;
206  }
207  }
208  if ( curropt->tag == "side" ) {
209  if ( ( side_ang = curropt->value.real() ) < 0.001 )
210  cerr << "-side: One angle must be specified!" << endl;
211  else
212  side_ang *= M_PI/180.0;
213  }
214  if ( curropt->tag == "limit" ) {
215  if ( ( angle_limit = curropt->value.real() ) < 0.001 )
216  cerr << "-limit: An angular limit must be specified!" << endl;
217  else
218  angle_limit *= M_PI/180.0;
219  }
220  if ( curropt->tag == "View" ) {
221  currview = curropt->view();
222  currview_set = 1;
223  }
224  if ( curropt->tag == "resolution" )
225  if ( curropt->values(hires, lores) < 1 )
226  cerr << "-resolution: A resolution must be specified!" << endl;
227  if ( curropt->tag == "shiftlimit" )
228  if ( ( search_radius = curropt->value.real() ) < 0.001 )
229  cerr << "-shiftlimit: A shift search radius must be specified!" << endl;
230  if ( curropt->tag == "symmetry" )
231  sym = curropt->symmetry();
232  if ( curropt->tag == "bin" ) {
233  if ( ( bin[0] = curropt->value.integer() ) < 1 )
234  cerr << "-bin: A bin size must be specified!" << endl;
235  else {
236  if ( bin[0] < 1 ) bin[0] = 1;
237  bin[2] = bin[1] = bin[0];
238  }
239  }
240  if ( curropt->tag == "accuracy" ) {
241  if ( ( accuracy = curropt->value.real() ) < 0.001 )
242  cerr << "-accuracy: An accuracy in degrees must be specified!" << endl;
243  else
244  accuracy *= M_PI/180.0;
245  }
246  if ( curropt->tag == "ppx" ) flags |= WRITE_PPX | CHECK_PPX;
247  if ( curropt->tag == "recid" )
248  rec_id = curropt->value;
249  if ( curropt->tag == "Template" )
250  template_file = curropt->filename();
251  if ( curropt->tag == "mask" )
252  rs_mask = curropt->filename();
253  if ( curropt->tag == "Mask" )
254  fs_mask = curropt->filename();
255  if ( curropt->tag == "Postscript" )
256  ps_file = curropt->filename();
257  if ( curropt->tag == "output" )
258  param_file = curropt->filename();
259  if ( curropt->tag == "json" )
260  jsfile = curropt->filename();
261  if ( curropt->tag == "particle" )
262  newpart_file = curropt->filename();
263  if ( curropt->tag == "newtemplate" )
264  newtemp_file = curropt->filename();
265  }
266  option_kill(option);
267 
268  double ti = timer_start();
269 
270  int read_img_num = img_num - 1;
271  int cnt(1), found(0);
272  Bimage* p = NULL;
273  Bimage* ptemp = NULL;
274  Bstring filename;
275  Vector3<double> scale(1,1,1);
276  Vector3<long> size;
277  Bproject* project = NULL;
278  Breconstruction* rec = NULL;
279  Bparticle* part = NULL;
280 
281  // Sets up the input image filename
282  // If the input file is a parameter file, find the reconstruction or particle to process
283  if ( optind < argc ) {
284  filename = argv[optind++];
285  if ( file_type(filename) == Micrograph ) {
286  project = read_project(filename);
287  project->select = 1;
288  part_set_FOM(project, 0, 0);
289  if ( set_seq_sel ) part_set_sequential(project);
290 // for ( rec = project->rec; rec; rec = rec->next )
291 // for ( part = rec->part; part; part = part->next ) part->sel = cnt++;
292  rec = project->rec;
293  if ( sel_num > 0 ) { // Finds the first particle with this selection number
294  for ( found=0, rec = project->rec; rec && !found; ) {
295  for ( part = rec->part; part && !found; ) {
296  if ( part->sel == sel_num ) found = 1;
297  else part = part->next;
298  }
299  if ( !found ) rec = rec->next;
300  }
301  } else { // Finds the specified particle and image number
302  if ( rec_id.length() )
303  for ( rec = project->rec; rec && rec_id != rec->id; rec = rec->next ) ;
304  if ( rec ) {
305  for ( part = rec->part; part && part->id != img_num; part = part->next ) ;
306  } else {
307  cerr << "Error: No reconstruction with ID = " << rec_id << endl;
308  bexit(-1);
309  }
310  }
311  if ( rec ) {
312  filename = rec->frec;
313  if ( rec->fpart.length() ) filename = rec->fpart;
314  if ( part ) {
315  img_num = part->id;
316  read_img_num = part->id - 1;
317  if ( !currview_set ) {
318  currview = part->view;
319  currview_set = 1;
320  }
321 // cout << "part->ori=" << part->ori << endl;
322  if ( part_ori.length() < 1 && part->ori.length() ) part_ori = part->ori;
323  if ( part->fpart.length() ) {
324  filename = part->fpart;
325  read_img_num = 0;
326  }
327  if ( verbose ) {
328  cout << "Processing particle " << part->id << ":" << endl;
329  cout << "Origin: " << part->ori << endl;
330  cout << "View: " << part->view << endl;
331 // cout << "Current view: " << currview << endl;
332  }
333  } else {
334  cerr << "Error: Particle not defined!" << endl;
335  bexit(-1);
336  }
337  if ( !currview_set ) {
338  currview = rec->view;
339  currview_set = 1;
340  }
341  } else {
342  cerr << "Error: Reconstruction not defined!" << endl;
343  bexit(-1);
344  }
345  }
346  if ( filename.length() < 1 ) {
347  cerr << "Error: No image file name found!" << endl;
348  bexit (-1);
349  }
350  }
351 
352  FOMType fom_tag[NFOM] = {FOM};
353  if ( part && ( flags & CHECK_PPX ) ) {
354  // Directory for individual particle parameter files
355  if ( flags & WRITE_PPX ) {
356  mkdir("ppx", O_CREAT );
357  chmod("ppx", 0755);
358  }
359  if ( ppx_check(part, fom_tag) ) {
360  cerr << filename << " already done! Aborting" << endl;
361  bexit(0);
362  }
363  }
364 
365 // cout << "part_ori=" << part_ori << endl;
366 
367  View* view = NULL;
368  Vector3<double> origin, currshift, bestshift;
369  double cc, best_cc(-1e37);
370 
371  Matrix3 mat = ref_view.matrix();
372  if ( !sym ) sym = init_point_group_symmetry("C1");
373  for ( i=0; i<sym->n; i++ )
374  sym->op[i].axis = mat * sym->op[i].axis;
375 
376  bestview = currview;
377 
378  if ( global_bin[0] > 0 || !currview_set ) {
379 
380  if ( side_ang < 0 )
381  view = asymmetric_unit_views(sym, theta_step, phi_step, alpha_step, 1);
382  else
383  view = side_views(sym, side_ang, theta_step, phi_step, alpha_step);
384 
385  if ( verbose & VERB_FULL )
386  show_views(view);
387 
388  if ( view_subset > 0 ) if ( view_list_subset(&view, view_start, view_subset) < 1 ) {
389  cerr << "Error: At least one view must be selected!" << endl;
390  bexit(-1);
391  }
392 
393  if ( ps_file.length() && view ) ps_views(ps_file, sym->label, view, 0);
394 
395  if ( verbose )
396  cout << "Number of global views: " << count_list((char *) view) << endl << endl;
397 
398  origin = temp_ori;
399 
400  vector<Bimage*> imgvec = setup_images(filename, template_file, rs_mask, fs_mask,
401  read_img_num, global_bin, sam, set_origin, origin, part_ori);
402 
403  p = imgvec[0];
404 
405  best_cc = p->search_views(imgvec[1], view, hires, lores,
406  search_radius/global_bin[0], imgvec[2], bestview, bestshift);
407 
408  kill_list((char *) view, sizeof(View));
409  view = NULL;
410 
411  if ( global_bin[0] != bin[0] )
412  bestshift *= Vector3<double>(global_bin[0]/bin[0], global_bin[1]/bin[1], global_bin[2]/bin[2]);
413 
414  p->meta_data_update();
415 
416  if ( jsfile.length() ) p->meta_data().write(jsfile.c_str());
417 
418  for ( auto it = imgvec.begin(); it != imgvec.end(); ++it ) delete *it;
419  }
420 
421  if ( mode.length() ) {
422  if ( bin[0] > 1 ) search_radius /= bin[0];
423  origin = temp_ori;
424 
425  vector<Bimage*> imgvec = setup_images(filename, template_file, rs_mask, fs_mask,
426  read_img_num, bin, sam, set_origin, origin, part_ori);
427 
428  p = imgvec[0];
429 
430  if ( mode == "directional" ) {
431  view = views_within_limits(bestview, theta_step, phi_step, alpha_step, angle_limit, M_PI);
432  if ( verbose ) {
433  cout << "Central view: " << bestview << endl;
434  cout << "Number of directional views: " << count_list((char *) view) << endl << endl;
435  }
436  } else if ( mode == "symmetric" ) {
437  view = symmetry_get_all_views(sym, bestview);
438  if ( verbose )
439  cout << "Number of symmetry views: " << count_list((char *) view) << endl << endl;
440  }
441 
442  if ( mode == "directional" || mode == "symmetric" ) {
443  best_cc = p->search_views(imgvec[1], view, hires, lores,
444  search_radius, imgvec[2], bestview, bestshift);
445  kill_list((char *) view, sizeof(View));
446  } else if ( mode == "refine" ) {
447  currview = bestview;
448  best_cc = -1;
449  cnt = 0;
450  if ( accuracy < 2.0/p->sizeX() ) accuracy = 2.0/p->sizeX();
451  while ( alpha_step >= accuracy ) {
452  if ( hires > alpha_step * p->real_size()[0] )
453  hires = alpha_step * p->real_size()[0];
454  view = views_for_refinement(bestview, alpha_step);
455  cc = p->search_views(imgvec[1], view, hires, lores,
456  search_radius, imgvec[2], bestview, currshift);
457  if ( currview.residual(bestview) < 1e-6 ||
458  fabs(cc - best_cc) < 1e-6 ||
459  cnt > 3 ) {
460  alpha_step /= 2; // Contract around best point
461  cnt = 0;
462  }
463  if ( best_cc < cc ) {
464  best_cc = cc;
465  bestview = currview;
466  bestshift = currshift;
467  }
468  kill_list((char *) view, sizeof(View));
469  cnt++;
470  }
471  }
472 
473  p->meta_data_update();
474 
475  if ( jsfile.length() ) p->meta_data().write(jsfile.c_str());
476 
477  for ( auto it = imgvec.begin(); it != imgvec.end(); ++it ) delete *it;
478  }
479 
480  if ( bin[0] ) bestshift *= Vector3<double>(bin[0], bin[1], bin[2]);
481 
482  if ( newpart_file.length() < 1 && optind < argc ) newpart_file = argv[optind];
483 
484  currshift = -bestshift;
485 
486  if ( newtemp_file.length() ) { // Output transformed template
487  mat = bestview.matrix();
488  ptemp = read_img(template_file, 1, 0);
489  if ( set_origin & 1 ) ptemp->origin(temp_ori);
490  else if ( set_origin & 2) ptemp->origin(ptemp->size()/2);
491  mat = mat.transpose();
492  ptemp->calculate_background();
493  ptemp->transform(scale, ptemp->image->origin(), currshift, mat, FILL_BACKGROUND, 0);
494  ptemp->view(bestview);
495  if ( nudatatype == Unknown_Type ) nudatatype = p->data_type(); // Preserve the old type
496  ptemp->change_type(nudatatype);
497  write_img(newtemp_file, ptemp, 0);
498  delete ptemp;
499  }
500 
501  Bimage* ps = NULL;
502 
503  if ( newpart_file.length() ) { // Output transformed image
504 // cout << "origin=" << origin << " shift=" << bestshift << " view=" << bestview << endl;
505  mat = bestview.matrix();
506  p = read_img(filename, 1, read_img_num);
507  ptemp = read_img(template_file, 0, 0);
508  if ( p->size().distance(ptemp->size()) > 0.5 || p->real_size().distance(ptemp->real_size()) > 0.1 ) {
509  ps = p->scale_to_same_size(ptemp);
510  delete p;
511  p = ps;
512  }
513  delete ptemp;
514  origin += currshift; // Origin for rotation
516 // cout << "origin=" << origin << " shift=" << bestshift << " view=" << bestview << endl;
517  p->transform(scale, origin, bestshift, mat, FILL_BACKGROUND, 0);
518  p->image->view(0,0,1,0);
519  if ( nudatatype == Unknown_Type ) nudatatype = p->data_type(); // Preserve the old type
520  p->change_type(nudatatype);
521  if ( nustd > 0 ) p->rescale_to_avg_std(nuavg, nustd);
522  write_img(newpart_file, p, 0);
523  delete p;
524  }
525 
526  p = read_img(filename, 0, 0);
527  if ( !project ) project = project_init();
528  if ( !rec ) {
529  if ( rec_id.length() < 1 ) {
530  rec_id = p->file_name();
531  rec_id = rec_id.pre_rev('.');
532  }
533  rec = reconstruction_add(&project->rec, rec_id);
534  part = particle_add(&rec->part, img_num);
535  }
536  if ( sam[0] > 0 ) rec->voxel_size = sam;
537  else if ( p ) rec->voxel_size = p->sampling(0);
538  delete p;
539  rec->symmetry = sym->label;
540  if ( part ) {
541  if ( rec->fpart.length() < 1 ) part->fpart = filename;
542  if ( newpart_file.length() ) {
543  part->fpart = newpart_file;
544  } else {
545  part->view = bestview;
546  part->ori = origin + currshift;
547  }
548  part->fom[0] = best_cc;
549  if ( flags & WRITE_PPX ) {
550  Bstring log_name = ppx_filename(rec->id, part->id);
551  write_particle(log_name, part, 0, 0, fom_tag);
552  }
553 // for ( part = rec->part; part; part = part->next )
554 // if ( part->id == img_num ) part->fom[0] = best_cc;
555 // else part->fom[0] = 0;
556  } else {
557  rec->frec = filename;
558  if ( newpart_file.length() ) {
559  rec->frec = newpart_file;
560  } else {
561  rec->view = bestview;
562  rec->origin = origin + currshift;
563  }
564  for ( rec = project->rec; rec; rec = rec->next )
565  if ( rec->id == rec_id ) rec->fom = best_cc;
566  else rec->fom = 0;
567  }
568 
569  if ( param_file.length() ) {
570  write_project(param_file, project, 0, 0);
571  }
572 
574  kill_symmetry(sym);
575 
576  if ( verbose & VERB_TIME )
577  timer_report(ti);
578 
579  bexit(0);
580 }
581 
582 
583 vector<Bimage*> setup_images(Bstring& map_file, Bstring& template_file,
584  Bstring& rs_mask, Bstring& fs_mask,
585  int read_img_num, Vector3<long>& bin, Vector3<double> sam,
586  int set_origin, Vector3<double>& origin, Vector3<double>& part_ori)
587 {
588  int err(0);
589 
590  Bimage* p = NULL;
591  Bimage* ptemp = NULL;
592  Bimage* prs_mask = NULL;
593  Bimage* pfs_mask = NULL;
594 
595  if ( map_file.length() < 1 ) {
596  cerr << "Error: No image file name specified!" << endl;
597  err--;
598  } else {
599  p = read_img(map_file, 1, read_img_num);
600  if ( p ) {
601  if ( sam.volume() > 0 ) p->sampling(sam);
602  if ( set_origin & 4 ) p->origin(part_ori);
603  else if ( set_origin & 8 ) p->origin(p->size()/2);
604  part_ori = p->image->origin();
605  } else {
606  cerr << "Error: " << map_file << " not read!" << endl;
607  err--;
608  }
609  }
610 
611  if ( template_file.length() < 1 ) {
612  cerr << "Error: No input template file specified!" << endl;
613  err--;
614  } else {
615  ptemp = read_img(template_file, 1, 0);
616  if ( ptemp ) {
617  if ( sam.volume() > 0 ) ptemp->sampling(sam);
618  if ( set_origin & 1 ) ptemp->origin(origin);
619  else if ( set_origin & 2 ) ptemp->origin(ptemp->size()/2);
620  origin = ptemp->image->origin();
621  } else {
622  cerr << "Error: " << template_file << " not read!" << endl;
623  err--;
624  }
625  }
626 
627  if ( rs_mask.length() ) {
628  prs_mask = read_img(rs_mask, 1, 0);
629  if ( !prs_mask ) {
630  cerr << "Error: " << rs_mask << " not read!" << endl;
631  err--;
632  }
633  ptemp->multiply(prs_mask);
634  delete prs_mask;
635  }
636 
637  if ( fs_mask.length() ) {
638  pfs_mask = read_img(fs_mask, 1, 0);
639  if ( !pfs_mask ) {
640  cerr << "Error: " << fs_mask << " not read!" << endl;
641  err--;
642  }
643  }
644 
645  if ( err ) {
646  cerr << "Too many errors! (" << -err << ")" << endl;
647  bexit(-1);
648  }
649 
650  Bimage* ps = NULL;
651 
652  if ( p->size().distance(ptemp->size()) > 0.5 && p->real_size().distance(ptemp->real_size()) > 0.1 ) {
653  ps = p->scale_to_same_size(ptemp);
654  delete p;
655  p = ps;
656  }
657 
658  if ( bin[0] > 1 ) {
659  if ( p ) p->bin(bin);
660  if ( ptemp ) ptemp->bin(bin);
661  if ( pfs_mask ) pfs_mask->bin(bin);
662  } else {
663  bin = Vector3<long>(1,1,1);
664  }
665 
666  if ( verbose ) {
667  if ( p )
668  cout << "Map file: " << p->file_name() << " (" << read_img_num << ")" << endl;
669  if ( ptemp )
670  cout << "Template file: " << ptemp->file_name() << endl;
671  if ( pfs_mask )
672  cout << "Mask file: " << pfs_mask->file_name() << endl;
673  cout << "Size: " << p->size() << endl;
674  cout << endl;
675  }
676 
677  vector<Bimage*> imgvec;
678  imgvec.push_back(p);
679  imgvec.push_back(ptemp);
680  imgvec.push_back(pfs_mask);
681 
682  return imgvec;
683 }
684 
Boption
A class for a command line input option.
Definition: options.h:26
CHECK_PPX
#define CHECK_PPX
Definition: mg_processing.h:27
project_kill
int project_kill(Bproject *project)
Frees a whole project.
Definition: mg_processing.cpp:429
view_list_subset
int view_list_subset(View **view_list, int start, int size)
Replaces a view list with a subset of it.
Definition: View.cpp:474
views_for_refinement
View * views_for_refinement(View theview, double alpha_step)
Generates a 3x3 grid of views around an input view.
Definition: View.cpp:285
mg_particle_select.h
Select particles.
Breconstruction::part
Bparticle * part
Definition: mg_processing.h:307
Breconstruction::id
Bstring id
Definition: mg_processing.h:285
Bimage::scale_to_same_size
Bimage * scale_to_same_size(Bimage *pref)
Scales and image to the given reference.
Definition: Bimage_transform.cpp:735
WRITE_PPX
#define WRITE_PPX
Definition: mg_processing.h:26
Bimage::real_size
Vector3< double > real_size()
Definition: Bimage.h:500
Breconstruction::fpart
Bstring fpart
Definition: mg_processing.h:291
Bsymmetry::n
int n
Definition: symmetry.h:42
verbose
int verbose
Boption::scale
Vector3< double > scale()
Definition: options.h:190
Bproject::rec
Breconstruction * rec
Definition: mg_processing.h:347
symmetry_get_all_views
View * symmetry_get_all_views(Bsymmetry *sym, View asu_view)
Get all symmetry-related views of one given view.
Definition: symmetry.cpp:442
Bproject::select
int select
Definition: mg_processing.h:342
M_PI_4
#define M_PI_4
Definition: utilities.h:69
kill_list
long kill_list(char *list, unsigned long size)
Frees all the items in a linked list.
Definition: linked_list.cpp:214
Bparticle::view
View view
Definition: mg_processing.h:102
NFOM
#define NFOM
Definition: mg_processing.h:16
file_util.h
Header file for file checking functions.
Bstring
Definition: Bstring.h:29
Breconstruction::voxel_size
Vector3< double > voxel_size
Definition: mg_processing.h:295
Bparticle
Definition: mg_processing.h:90
Matrix3::transpose
Matrix3 transpose() const
Definition: Matrix3.h:268
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::datatype
DataType datatype()
Definition: options.h:93
symmetry.h
Header file for general symmetry functions.
FOMType
FOMType
Definition: mg_processing.h:38
Breconstruction::fom
double fom
Definition: mg_processing.h:305
Boption::next
Boption * next
Definition: options.h:28
Vector3< long >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
main
int main(int argc, char **argv)
Definition: bfind.cpp:103
get_option_list
Boption * get_option_list(const char *use[], int argc, char *argv[], int &optind)
Parses command line arguments based on a template.
Definition: options.cpp:40
project_init
Bproject * project_init()
Initializes a micrograph processing project parameter structure.
Definition: mg_processing.cpp:30
Bparticle::fom
double fom[NFOM]
Definition: mg_processing.h:103
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
Bstring::real
double real()
Definition: Bstring.cpp:553
options.h
Header file for option handlers.
Bimage::calculate_background
int calculate_background(long nn, int flag)
Calculates the background for one sub-image.
Definition: Bimage_background.cpp:27
Bimage::transform
int transform(Vector3< double > scale, Vector3< double > origin, Vector3< double > translate, Matrix3 mat, int fill_type=0, double fill=0)
Transforms an image by translation, rotation, scaling and skewing, in place.
Definition: Bimage_transform.cpp:37
asymmetric_unit_views
View * asymmetric_unit_views(Bsymmetry *sym, double theta_step, double phi_step, int flag)
Initializes a well-distributed set of views in an asymmetric unit.
Definition: symmetry.cpp:564
part_set_sequential
long part_set_sequential(Bproject *project)
Selects one selection number and sets all others to zero.
Definition: mg_particle_select.cpp:352
View
Definition: View.h:26
Bsub_image::view
void view(double x, double y, double z, double a)
Definition: Bimage.h:157
ppx_filename
Bstring ppx_filename(Bstring &id, int part_id)
Definition: rwmg.cpp:415
Boption::origin
Vector3< double > origin()
Definition: options.h:180
Boption::view
View view()
Definition: options.h:247
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
Bsymmetry
Definition: symmetry.h:38
count_list
long count_list(char *list)
Counts the number of items in a linked list.
Definition: linked_list.cpp:235
Bsymop::axis
Vector3< double > axis
Definition: symmetry.h:25
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Boption::symmetry
Bsymmetry * symmetry()
Definition: options.h:240
Vector3::distance
double distance(const Vector3 &v)
Definition: Vector3.h:227
Breconstruction::origin
Vector3< double > origin
Definition: mg_processing.h:296
project
Bproject * project
Definition: bshow.cpp:33
Boption::tag
Bstring tag
Definition: options.h:29
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
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
rwmg.h
Header file for reading and writing micrograph parameters.
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
Bimage::multiply
void multiply(long j, Complex< double > cv)
Definition: Bimage.h:325
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
Bparticle::fpart
Bstring fpart
Definition: mg_processing.h:92
part_set_FOM
long part_set_FOM(Bproject *project, int fom_index, double fom)
Sets the FOM for all particles.
Definition: mg_particle_select.cpp:382
Micrograph
@ Micrograph
Definition: file_util.h:23
Boption::value
Bstring value
Definition: options.h:30
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:421
Bimage::meta_data
JSvalue & meta_data()
Definition: Bimage.h:234
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
Bproject
Definition: mg_processing.h:336
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
Bparticle::sel
long sel
Definition: mg_processing.h:104
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
ps_views
int ps_views(Bstring &filename, Bstring &symmetry_string, View *view, int flags)
Generates postscript plot of views on the unit sphere.
Definition: ps_views.cpp:34
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
side_views
View * side_views(Bsymmetry *sym, double side_ang, double theta_step, double phi_step)
Initializes a set of views around the z-axis for helical projection.
Definition: symmetry.cpp:763
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
read_project
Bproject * read_project(const char *filename, int flags=0)
Reading micrograph parameters from files.
Definition: rwmg.cpp:141
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
Breconstruction::symmetry
Bstring symmetry
Definition: mg_processing.h:289
Breconstruction
Definition: mg_processing.h:283
Bstring::length
long length() const
Definition: Bstring.cpp:263
init_point_group_symmetry
Bsymmetry * init_point_group_symmetry(Bstring symmetry_string)
Sets up a symmetry structure from a string identifier.
Definition: symmetry.cpp:39
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:76
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
Vector3::volume
double volume()
Definition: Vector3.h:237
particle_add
Bparticle * particle_add(Bparticle **part, int pid)
Adds a particle parameter structure to a linked list.
Definition: mg_processing.cpp:221
rwimg.h
Header file for 2D and 3D image I/O.
write_project
int write_project(const char *filename, Bproject *project)
Writing micrograph parameters to a file.
Definition: rwmg.cpp:321
Bimage::meta_data_update
void meta_data_update()
Update metadata from the sub-image information.
Definition: Bimage.cpp:678
Bparticle::ori
Vector3< double > ori
Definition: mg_processing.h:101
mg_processing.h
Header file for micrograph processing.
ppx_check
int ppx_check(Bparticle *part, FOMType fom_tag[NFOM])
Definition: rwmg.cpp:441
Bparticle::next
Bparticle * next
Definition: mg_processing.h:91
kill_symmetry
int kill_symmetry(Bsymmetry *sym)
Deallocates a symmetry structure.
Definition: symmetry.cpp:207
linked_list.h
Header file for generalized linked list functions.
Boption::filename
Bstring filename()
Definition: options.h:85
use
const char * use[]
Definition: bfind.cpp:34
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
setup_images
vector< Bimage * > setup_images(Bstring &map_file, Bstring &template_file, Bstring &rs_mask, Bstring &fs_mask, int read_img_num, Vector3< long > &bin, Vector3< double > sam, int set_origin, Vector3< double > &origin, Vector3< double > &part_ori)
Definition: bfind.cpp:583
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
Breconstruction::frec
Bstring frec
Definition: mg_processing.h:290
Bsymmetry::op
Bsymop * op
Definition: symmetry.h:43
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:178
Bimage::search_views
double search_views(Bimage *ptemp, View *view, double hires, double lores, double search_radius, Bimage *pmask, View &currview, Vector3< double > &currshift)
Searches a 2D/3D density map for a template.
Definition: Bimage_search.cpp:34
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
View::residual
double residual(const View &view)
Definition: View.h:168
View::matrix
Matrix3 matrix()
Definition: View.h:153
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bsymmetry::label
Bstring label
Definition: symmetry.h:41
write_particle
int write_particle(Bstring &filename, Bparticle *part, int euler_flag, int omega_flag, FOMType fom_tag[NFOM])
Definition: rwmg.cpp:406
Vector3::length
double length()
Definition: Vector3.h:222
show_views
int show_views(View *v)
Displays a linked list of views.
Definition: View.cpp:28
reconstruction_add
Breconstruction * reconstruction_add(Breconstruction **rec, Bstring &rec_id)
Adds a reconstruction parameter structure to a linked list.
Definition: mg_processing.cpp:398
JSvalue::write
void write(string filename)
Definition: json.h:373
ps_views.h
Header file for postscript tools dealing with views.
Bparticle::id
int id
Definition: mg_processing.h:93
Bimage::view
void view(double vx, double vy, double vz, double va)
Definition: Bimage.h:596
FOM
@ FOM
Definition: mg_processing.h:40
file_type
FileType file_type(const char *filename)
Checks the file type using the extension and contents.
Definition: file_util.cpp:178
Breconstruction::view
View view
Definition: mg_processing.h:298
Breconstruction::next
Breconstruction * next
Definition: mg_processing.h:284
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