Bsoft  2.1.0
Bernard's software package
brefine.cpp
Go to the documentation of this file.
1 
9 #include "mg_processing.h"
10 #include "mg_refine.h"
11 #include "rwmg.h"
12 #include "mg_particle_select.h"
13 #include "rwFSC_XML.h"
14 #include "utilities.h"
15 #include "options.h"
16 #include "timer.h"
17 
18 // Declaration of global variables
19 extern int verbose; // Level of output to the screen
20 
21 // Usage assistance
22 const char* use[] = {
23 " ",
24 "Usage: brefine [options] input.star [input.star]",
25 "------------------------------------------------",
26 "Refines the orientations of particle images with respect to a reference map.",
27 "Defocus refinement on non-CTF corrected images is turned on by the -defocus option.",
28 " ",
29 "Actions:",
30 "-all Reset selection to all particles before other selections (default not).",
31 "-symmetry C5 Search for the best symmetry-related orientations.",
32 "-magnification 0.01 Turns on magnification refinement: Maximum adjustment (default 0).",
33 "-defocus 0.002 Turns on defocus refinement: Standard deviation for Monte Carlo,",
34 " Step size for grid search (default 0 um).",
35 " ",
36 "Actions for the type of refinement:",
37 "-monte 80 Monte Carlo with a maximum number of iterations.",
38 "-grid 2.5,0.3 Grid search with angular step size and accuracy (both in degrees).",
39 " ",
40 "Parameters:",
41 "-verbose 7 Verbosity of output.",
42 "-select 14 Selection number of particles to process (default all selected).",
43 "-fomtype dpr Resolution measurement type: frc (default), dpr.",
44 "-resolution 20,250 High and low resolution limits (angstrom).",
45 "-kernel 11,3 Interpolation kernel width and power (default 8,2)",
46 "-edge 125 Edge mask radius using previous particle origins (default not used).",
47 //"-threads 4 Set the number of threads (default maximum allowed).",
48 "-nothreads Do not use threads (default parallel processing).",
49 " ",
50 "Parameters only for Monte Carlo refinement:",
51 "-shift 0.4 Random origin shift standard deviation (default 0.1 pixels).",
52 "-view 0.02 Random view shift standard deviation (default 0.01).",
53 "-angle 0.7 Random rotation angle maximum adjustment (default 0.5 degrees).",
54 " ",
55 "Parameters only for grid search:",
56 "-step 1.5,0.3 Grid search shift step size and accuracy (default 0.4,0.1 pixels).",
57 " ",
58 "Input:",
59 "-reference file.ext Input 3D reference map file.",
60 "-mask mask.map Real space 2D mask to be applied to particles.",
61 "-FSC file.xml FSC curve used to weigh FOM contributions.",
62 " ",
63 "Output:",
64 "-output file.star Output parameter file.",
65 "-ppx Write temporary particle parameter files to directory \"ppx\".",
66 " ",
67 NULL
68 };
69 
70 
71 int main(int argc, char** argv)
72 {
73  // Initializing variables
74  int reset(0); // Keep selection as read from file
75  int part_select(-1); // Process all selected particles
76  int max_iter(0); // Maximum Monte Carlo refining iterations
77  double alpha_step(0); // Angular step size for grid search
78  double accuracy(0.5); // Angular accuracy for grid search
79  double shift_step(0.4); // Angular step size for grid search
80  double shift_accuracy(0.1); // Angular accuracy for grid search
81  double edge_radius(0); // Edge mask radius
82  double def_std(0); // Random defocus standard deviation
83  double shift_std(0.1); // Random origin shift standard deviation
84  double view_std(0.01); // Random view shift standard deviation
85  double max_angle(M_PI/360); // Random rotation angle maximum adjustment
86  double max_mag(0); // Random magnification maximum adjustment
87  int fom_type(0); // Default type is FRC
88  double hi_res(0), lo_res(0); // Must be set > 0 to limit resolution
89  int kernel_width(8); // Interpolation kernel width
90  int kernel_power(2); // Interpolation kernel power
91  int nthreads(1); // Number of threads to run
92  int nothreads(0); // Flag to turn off threads
93  Bstring symmetry_string; // No symmetry specified
94  int flags(0); // Flags for processing options
95  Bstring reffile; // Reference map file
96  Bstring maskfile; // Mask to be applied to particles
97  Bstring fscfile; // FSC curve
98  Bstring outfile; // Output parameter file
99 
100  int optind;
101  Boption* option = get_option_list(use, argc, argv, optind);
102  Boption* curropt;
103  for ( curropt = option; curropt; curropt = curropt->next ) {
104  if ( curropt->tag == "all" ) reset = 1;
105  if ( curropt->tag == "select" )
106  if ( ( part_select = curropt->value.integer() ) < 0 )
107  cerr << "-select: A selection number must be specified!" << endl;
108  if ( curropt->tag == "symmetry" )
109  symmetry_string = curropt->symmetry_string();
110  if ( curropt->tag == "monte" )
111  if ( ( max_iter = curropt->value.integer() ) < 1 )
112  cerr << "-monte: A number of iterations must be specified!" << endl;
113  if ( curropt->tag == "grid" ) {
114  if ( curropt->values(alpha_step, accuracy) < 1 )
115  cerr << "-grid: An angular step size must be specified!" << endl;
116  else {
117  alpha_step *= M_PI/180.0;
118  accuracy *= M_PI/180.0;
119  }
120  }
121  if ( curropt->tag == "fomtype" ) {
122  if ( curropt->value[0] == 'f' ) fom_type = 0;
123  if ( curropt->value[0] == 'd' ) fom_type = 1;
124  }
125  if ( curropt->tag == "resolution" ) {
126  if ( curropt->values(hi_res, lo_res) < 1 )
127  cerr << "-resolution: At least one resolution limit must be specified!" << endl;
128  else
129  if ( lo_res > 0 && lo_res < hi_res ) swap(hi_res, lo_res);
130  }
131  if ( curropt->tag == "kernel" )
132  if ( curropt->values(kernel_width, kernel_power) < 1 )
133  cerr << "-kernel: A kernel width must be specified!" << endl;
134  if ( curropt->tag == "edge" )
135  if ( ( edge_radius = curropt->value.real() ) < 0 )
136  cerr << "-edge: An edge radius must be specified!" << endl;
137  if ( curropt->tag == "defocus" ) {
138  if ( ( def_std = curropt->value.real() ) < 0.00001 )
139  cerr << "-defocus: An defocus standard deviation must be specified!" << endl;
140  else
141  if ( def_std < 10 ) def_std *= 1e4; // Assume um
142  }
143  if ( curropt->tag == "magnification" )
144  if ( ( max_mag = curropt->value.real() ) < 0 )
145  cerr << "-magnification: A maximum magnification adjustment must be specified!" << endl;
146  if ( curropt->tag == "shift" )
147  if ( ( shift_std = curropt->value.real() ) < 0.001 )
148  cerr << "-shift: An origin shift standard deviation must be specified!" << endl;
149  if ( curropt->tag == "view" )
150  if ( ( view_std = curropt->value.real() ) < 0.001 )
151  cerr << "-view: A view shift standard deviation must be specified!" << endl;
152  if ( curropt->tag == "angle" ) {
153  if ( ( max_angle = curropt->value.real() ) < 0.1 )
154  cerr << "-angle: A maximum rotation angle adjustment must be specified!" << endl;
155  else
156  max_angle *= M_PI/180.0;
157  }
158  if ( curropt->tag == "step" )
159  if ( curropt->values(shift_step, shift_accuracy) < 1 )
160  cerr << "-step: A shift step size must be specified!" << endl;
161  if ( curropt->tag == "threads" ) {
162  if ( curropt->value[0] == 'm' ) nthreads = 1000;
163  else if ( ( nthreads = curropt->value.integer() ) < 1 )
164  cerr << "-threads: A number of threads must be specified!" << endl;
165  }
166  if ( curropt->tag == "nothreads" ) nothreads = 1;
167  if ( curropt->tag == "ppx" ) flags |= WRITE_PPX | CHECK_PPX;
168  if ( curropt->tag == "reference" )
169  reffile = curropt->filename();
170  if ( curropt->tag == "mask" )
171  maskfile = curropt->filename();
172  if ( curropt->tag == "FSC" )
173  fscfile = curropt->filename();
174  if ( curropt->tag == "output" )
175  outfile = curropt->filename();
176  }
177  option_kill(option);
178 
179  double ti = timer_start();
180 
181 #ifdef HAVE_GCD
182  if ( !nothreads ) {
183  fftwf_init_threads();
184  fftwf_plan_with_nthreads(system_processors());
185  if ( verbose )
186  cout << "Number of threads: " << system_processors() << endl;
187  }
188 #endif
189 
190  // Read all the parameter files
191  Bstring* file_list = NULL;
192  while ( optind < argc ) string_add(&file_list, argv[optind++]);
193  if ( !file_list ) {
194  cerr << "Error: No parameter files specified!" << endl;
195  bexit(-1);
196  }
197 
198  Bproject* project = read_project(file_list);
199  string_kill(file_list);
200 
201  if ( project == NULL ) {
202  cerr << "Error: No input file read!" << endl;
203  bexit(-1);
204  }
205 
206  project->fom_tag[0] = FOM;
207  project->fom_tag[1] = FOM_CV;
208 
209  if ( reset ) part_reset_selection(project, 3);
210 
211  vector<double> weight;
212  int i, j;
213  Bplot* plot = NULL;
214  if ( fscfile.length() ) {
215  plot = xml_read_fsc(fscfile);
216  if ( !plot ) {
217  cerr << "Error: The FSC file " << fscfile << " was not read!" << endl;
218  bexit(-1);
219  }
220  weight.resize(plot->rows());
221  for ( i=0, j=plot->rows(); i<plot->rows(); i++, j++ )
222  weight[i] = sqrt((*plot)[j]);
223  delete plot;
224  }
225 
226  if ( outfile.length() ) {
227  if ( reffile.length() && ( max_iter || alpha_step > 0 ) ) {
228  if ( nothreads ) {
229  if ( mg_refine_orientations(project, reffile, maskfile,
230  symmetry_string, part_select, max_iter,
231  alpha_step, accuracy, shift_step,
232  shift_accuracy, fom_type, weight,
233  hi_res, lo_res, kernel_width, kernel_power,
234  edge_radius, def_std, shift_std,
235  view_std, max_angle, max_mag, flags) < 0 )
236  bexit(-1);
237  } else {
238  if ( project_refine_orientations(project, reffile, maskfile,
239  symmetry_string, part_select, max_iter,
240  alpha_step, accuracy, shift_step,
241  shift_accuracy, fom_type, weight,
242  hi_res, lo_res, kernel_width, kernel_power,
243  edge_radius, def_std, shift_std,
244  view_std, max_angle, max_mag, flags) < 0 )
245  bexit(-1);
246  }
247  }
248 
249  write_project(outfile, project, 0, 0);
250  }
251 
253 
254 #ifdef HAVE_GCD
255  if ( !nothreads ) fftwf_cleanup_threads();
256 #endif
257 
258  if ( verbose & VERB_TIME )
259  timer_report(ti);
260 
261  bexit(0);
262 }
263 
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
mg_particle_select.h
Select particles.
WRITE_PPX
#define WRITE_PPX
Definition: mg_processing.h:26
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:768
main
int main(int argc, char **argv)
Definition: brefine.cpp:71
system_processors
size_t system_processors()
Returns the number of processors.
Definition: utilities.cpp:193
Bstring
Definition: Bstring.h:29
Bplot::rows
void rows(long n)
Definition: Bplot.h:212
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::next
Boption * next
Definition: options.h:28
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
verbose
int verbose
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
Bplot
Definition: Bplot.h:146
Bstring::real
double real()
Definition: Bstring.cpp:553
options.h
Header file for option handlers.
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
rwmg.h
Header file for reading and writing micrograph parameters.
part_reset_selection
long part_reset_selection(Bproject *project, int flag=0)
Resets selection to all particles.
Definition: mg_particle_select.cpp:133
xml_read_fsc
Bplot * xml_read_fsc(Bstring &filename, Bstring &xsdfile)
Function prototypes.
Boption::value
Bstring value
Definition: options.h:30
timer.h
Utilities for timing functions.
mg_refine_orientations
long mg_refine_orientations(Bproject *project, Bstring &reffile, Bstring &maskfile, Bstring &sym_string, int part_select, int max_iter, double alpha_step, double accuracy, double shift_step, double shift_accuracy, int fom_type, vector< double > weight, double hi_res, double lo_res, int kernel_width, int kernel_power, double edge_radius, double def_std, double shift_std, double view_std, double max_angle, double max_mag, int flags)
Refine the orientation and origin with respect to a reference map.
Definition: mg_refine.cpp:77
Bproject
Definition: mg_processing.h:336
part_select
Tcl_Obj * part_select(Bparticle *part, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bmg.cpp:563
Boption::symmetry_string
Bstring symmetry_string()
Definition: options.h:233
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
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
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
Bstring::length
long length() const
Definition: Bstring.cpp:263
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:76
write_project
int write_project(const char *filename, Bproject *project)
Writing micrograph parameters to a file.
Definition: rwmg.cpp:321
mg_processing.h
Header file for micrograph processing.
mg_refine.h
Reciprocal space refinement of orientation parameters of particle images.
Boption::filename
Bstring filename()
Definition: options.h:85
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
project_refine_orientations
long project_refine_orientations(Bproject *project, Bstring &reffile, Bstring &maskfile, Bstring &sym_string, int part_select, int max_iter, double alpha_step, double accuracy, double shift_step, double shift_accuracy, int fom_type, vector< double > weight, double hi_res, double lo_res, int kernel_width, int kernel_power, double edge_radius, double def_std, double shift_std, double view_std, double max_angle, double max_mag, int flags)
Definition: mg_refine.cpp:251
use
const char * use[]
Definition: brefine.cpp:22
FOM_CV
@ FOM_CV
Definition: mg_processing.h:42
rwFSC_XML.h
Library routines to read and write FSC curves.
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:713
FOM
@ FOM
Definition: mg_processing.h:40
Bproject::fom_tag
FOMType fom_tag[NFOM]
Definition: mg_processing.h:345