Bsoft  2.1.0
Bernard's software package
borient.cpp
Go to the documentation of this file.
1 
9 #include "mg_orient.h"
10 #include "mg_processing.h"
11 #include "mg_img_proc.h"
12 #include "mg_reconstruct.h"
13 #include "rwmg.h"
14 #include "mg_particle_select.h"
15 #include "utilities.h"
16 #include "options.h"
17 #include "timer.h"
18 
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 
22 // Declaration of global variables
23 extern int verbose; // Level of output to the screen
24 
25 // Usage assistance
26 const char* use[] = {
27 " ",
28 "Usage: borient [options] input.star [input.star]",
29 "------------------------------------------------",
30 "Determines the orientations and origins of single particles with respect to a reference.",
31 "Options -resolution and -bands are mutually exclusive.",
32 " ",
33 "Actions:",
34 "-mode scc Mode: 0,pps: polar power spectrum only (fast, default),",
35 " 1,scc: polar power spectrum with selected cross correlation.",
36 " 2,ccc: polar power spectrum with all cross correlation (slow).",
37 " 3,ori: origin determination by cross correlation.",
38 "-CTF Apply CTF to projections (default not).",
39 "-all Reset selection to all particles before other selections (default not).",
40 "-side 15 Generate side view projections within the given angle from the equator.",
41 "-bin 2 Binning by given kernel size (default 1).",
42 "-prepare 45,12 Prepare a set of particle images as 2D references (number and first image).",
43 " ",
44 "Parameters:",
45 "-verbose 1 Verbosity of output.",
46 "-select 14 Selection number of particles to process (default all selected).",
47 "-symmetry C5 Point group symmetry.",
48 "-angles 2.5,5.2 Step size for theta and phi, one value sets both (default 45 degrees).",
49 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; a single value can be given.)",
50 "-resolution 20,360 High and low resolution limits (angstrom).",
51 "-polarresolution 23.5 Resolution limit for in-plane angular alignment (angstrom).",
52 "-bands 250,-1,150,1,30,0 Bands for analysis in reciprocal space (pairs of numbers: angstrom,flag).",
53 " Band with 0: Not used - end flag.",
54 " Band with 1: Use for orientation determination.",
55 " Band with -1: Use for cross-validation.",
56 "-annuli 2,95 Real space annular limits (default 0,inf pixels).",
57 "-shiftlimit 3.5 Limit on origin shift relative to nominal center (default 10% of box edge size).",
58 "-rotationlimit 16.7 Limit on in-plane rotation change from input orientation (default not applied).",
59 "-edge 125 Edge mask radius using previous particle origins (default not used).",
60 "-fom 0.3 Set FOM threshold for selection (default 0).",
61 #ifdef HAVE_GCD
62 "-nothreads Do not use threads (default parallel processing).",
63 #endif
64 " ",
65 "Input:",
66 "-reference file.ext Input 3D reference map or projection file.",
67 "-mask mask.mrc Real space 2D mask to be applied to projections and particles.",
68 " ",
69 "Output:",
70 "-output file.star Output parameter file.",
71 "-log Write particle log files to directory \"log\".",
72 "-ppx Write temporary particle parameter files to directory \"ppx\".",
73 "-TwoD file.ext File name for 2D real space reconstruction.",
74 "-oriented Output oriented images with file names ending with \"_proj.spi\".",
75 " (must be used with the -TwoD option).",
76 " ",
77 NULL
78 };
79 
80 
81 int main(int argc, char** argv)
82 {
83  // Initializing variables
84  int reset(0); // Keep selection as read from file
85  long part_select(-1); // Process all selected particles
86  Bstring symmetry_string("C1"); // Point group string
87  double theta_step(M_PI_4); // Angular step size for theta
88  double phi_step(M_PI_4); // Angular step size for phi
89  double side_ang(-1); // Side view variation angle
90  int bin(0); // Data compression by binning
91  long fref(0), nref(0); // First and number of reference images to prepare
92  Vector3<double> sam; // Units for the three axes (A/pixel)
93  double res_lo(10000); // Resolution limits for orientation finding
94  double res_hi(0); // Must be set > 0 to determine orientations
95  double res_polar(0); // Must be set > 0 to limit in-plane angular resolution
96  vector<double> band; // Reciprocal space bands
97  long ann_min(0); // Minimum annulus
98  long ann_max(1000); // Maximum annulus, reset to maximum radius in image
99  double shift_limit(-1); // Maximum shift from nominal box origin
100  double angle_limit(0); // Maximum in-plane rotation from input orientation
101  double edge_radius(0); // Edge mask radius
102  double FOM_cut(0); // Threshold to accept orientations
103  int mode(0); // Only polar power spectrum
104  int transform_output(0); // Flag to output transformed images
105  long nthreads(1); // Number of threads to run
106 #ifdef HAVE_GCD
107  int nothreads(0); // Flag to turn off threads
108 #endif
109  int flags(FULL_ASU); // Flags for processing options
110  Bstring outfile; // Output STAR format file
111  Bstring ref_filename; // Input 3D map or projection file name
112  Bstring mask_filename; // Mask to be applied to projections and particles
113  Bstring twoD_recons_name; // Output 2D reconstruction file
114 
115  int i, optind;
116  Boption* option = get_option_list(use, argc, argv, optind);
117  Boption* curropt;
118  for ( curropt = option; curropt; curropt = curropt->next ) {
119  if ( curropt->tag == "all" )
120  reset = 1;
121  if ( curropt->tag == "select" )
122  if ( ( part_select = curropt->value.integer() ) < 0 )
123  cerr << "-select: A selection number must be specified!" << endl;
124  if ( curropt->tag == "symmetry" )
125  symmetry_string = curropt->symmetry_string();
126  if ( curropt->tag == "angles" ) {
127  if ( ( i = curropt->values(theta_step, phi_step) ) < 1 )
128  cerr << "-angles: An angle step size must be specified" << endl;
129  else {
130  theta_step *= M_PI/180.0;
131  if ( i < 2 ) phi_step = theta_step;
132  else phi_step *= M_PI/180.0;
133  }
134  }
135  if ( curropt->tag == "side" ) {
136  if ( ( side_ang = curropt->value.real() ) < 0.001 )
137  cerr << "-side: One angle must be specified!" << endl;
138  else
139  side_ang *= M_PI/180.0;
140  }
141  if ( curropt->tag == "bin" ) {
142  if ( ( bin = curropt->value.integer() ) < 1 )
143  cerr << "-bin: An integer must be specified!" << endl;
144  else {
145  if ( bin < 2 ) bin = 0;
146  if ( bin > 4 ) bin = 4;
147  }
148  }
149  if ( curropt->tag == "prepare" )
150  if ( curropt->values(nref, fref) < 1 )
151  cerr << "-prepare: A number of particle images must be specified!" << endl;
152  if ( curropt->tag == "sampling" )
153  sam = curropt->scale();
154  if ( curropt->tag == "resolution" ) {
155  if ( curropt->values(res_hi, res_lo) < 1 )
156  cerr << "-resolution: At least one resolution limit must be specified!" << endl;
157  else
158  if ( res_lo < res_hi ) swap(res_hi, res_lo);
159  }
160  if ( curropt->tag == "polarresolution" )
161  if ( ( res_polar = curropt->value.real() ) < 0.1 )
162  cerr << "-polarresolution: A resolution limit must be specified!" << endl;
163  if ( curropt->tag == "bands" ) {
164  band = curropt->value.split_into_doubles(",");
165  if ( band.size() < 2 )
166  cerr << "-bands: At least two pairs of numbers must be specified!" << endl;
167  band[band.size()] = 0;
168  }
169  if ( curropt->tag == "annuli" )
170  if ( curropt->values(ann_min, ann_max) < 1 )
171  cerr << "-annuli: At least a minimum annulus must be specified!" << endl;
172  if ( curropt->tag == "shiftlimit" )
173  if ( ( shift_limit = curropt->value.real() ) < 0.1 )
174  cerr << "-shiftlimit: A maximum shift in pixels must be specified!" << endl;
175  if ( curropt->tag == "rotationlimit" ) {
176  if ( ( angle_limit = curropt->value.real() ) < 0.1 )
177  cerr << "-rotationlimit: A maximum rotation angle must be specified!" << endl;
178  else
179  angle_limit *= M_PI/180.0;
180  }
181  if ( curropt->tag == "edge" )
182  if ( ( edge_radius = curropt->value.real() ) < 0 )
183  cerr << "-edge: An edge radius must be specified!" << endl;
184  if ( curropt->tag == "CTF" ) flags |= APPLY_CTF;
185  if ( curropt->tag == "multiple" ) flags |= MULTI_FILE;
186  if ( curropt->tag == "fom" )
187  if ( ( FOM_cut = curropt->value.real() ) < 0.0001 )
188  cerr << "-fom: A FOM threshold must be specified!" << endl;
189  if ( curropt->tag == "threads" ) {
190  if ( curropt->value[0] == 'm' ) nthreads = 1000;
191  else if ( ( nthreads = curropt->value.integer() ) < 1 )
192  cerr << "-threads: A number of threads must be specified!" << endl;
193  }
194 #ifdef HAVE_GCD
195  if ( curropt->tag == "nothreads" ) nothreads = 1;
196 #endif
197  if ( curropt->tag == "mode" ) {
198  if ( isdigit(curropt->value[0]) ) {
199  mode = curropt->value.integer();
200  } else {
201  if ( curropt->value.contains("pps") ) mode = 0;
202  else if ( curropt->value.contains("scc") ) mode = 1;
203  else if ( curropt->value.contains("ccc") ) mode = 2;
204  else if ( curropt->value.contains("ori") ) mode = 3;
205  }
206  if ( mode < 0 || mode > 3 ) mode = 0;
207  flags |= mode;
208  }
209  if ( curropt->tag == "oriented" ) transform_output = 1;
210  if ( curropt->tag == "log" ) flags |= PART_LOG; //part_log = 1;
211  if ( curropt->tag == "ppx" ) flags |= WRITE_PPX | CHECK_PPX;
212  if ( curropt->tag == "output" )
213  outfile = curropt->filename();
214  if ( curropt->tag == "reference" )
215  ref_filename = curropt->filename();
216  if ( curropt->tag == "mask" )
217  mask_filename = curropt->filename();
218  if ( curropt->tag == "TwoD" )
219  twoD_recons_name = curropt->filename();
220  }
221  option_kill(option);
222 
223  double ti = timer_start();
224 
225 #ifdef HAVE_GCD
226  if ( !nothreads ) {
227  fftwf_init_threads();
228 // fftwf_plan_with_nthreads(system_processors());
229  if ( verbose )
230  cout << "Number of threads: " << system_processors() << endl;
231  }
232 #endif
233 
234  Bsymmetry* sym = init_point_group_symmetry(symmetry_string);
235 
236  if ( res_hi <= 0 ) res_hi = 0.1;
237 
238  // Read all the parameter files
239  Bstring* file_list = NULL;
240  while ( optind < argc ) string_add(&file_list, argv[optind++]);
241  if ( !file_list ) {
242  cerr << "Warning: No parameter files specified!" << endl;
243  bexit(-1);
244  }
245 
246  Bproject* project = read_project(file_list);
247  string_kill(file_list);
248 
249  if ( project == NULL ) {
250  cerr << "Error: No input file read!" << endl;
251  bexit(-1);
252  }
253 
254  Bimage* proj = NULL;
255  if ( ref_filename.length() )
256  proj = img_prepare_projections(ref_filename, mask_filename,
257  bin, sym, theta_step, phi_step, side_ang);
258  else if ( nref )
259  proj = project_prepare_2D_references(project, fref, nref, bin, flags & APPLY_CTF);
260 
261  ref_filename = 0;
262 
263 // if ( !proj ) {
264 // cerr << "Warning: No projections generated!" << endl;
265 // bexit(-1);
266 // }
267 
268  project->fom_tag[0] = FOM;
269  project->fom_tag[1] = FOM_CV;
270 
272  project_set_views_from_images(project); // This must be done before operations on particles
273 
274  if ( reset ) part_reset_selection(project, 3);
275 
276  if ( sam[0] > 0 && sam[1] > 0 && sam[2] > 0 ) {
277  if ( sam[0] < 0.1 ) sam = Vector3<double>(1,1,1);
279  }
280 
281  if ( proj && outfile.length() ) {
282  if ( mode == 3 ) {
283  if ( project_determine_origins(project, proj, bin, sym,
284  part_select, res_lo, res_hi, shift_limit, flags) < 0 ) {
285  cerr << "Error: Alignment failed!" << endl;
286  bexit(-1);
287  }
288  } else {
289  if ( project_determine_orientations2(project, proj, mask_filename,
290  bin, sym, part_select, band, res_lo, res_hi, res_polar,
291  ann_min, ann_max, shift_limit, angle_limit, edge_radius, flags) < 0 ) {
292  cerr << "Error: Alignment failed!" << endl;
293  bexit(-1);
294  }
295  }
296  }
297 
298  delete proj;
299  mask_filename = 0;
300 
301  if ( FOM_cut > 0 ) part_deselect(project, 0, FOM_cut);
302 
303  Bimage* prec = NULL;
304  if ( twoD_recons_name.length() ) {
305  if ( transform_output )
306  prec = project_reconstruct_2D(project, transform_output);
307  else
309  if ( prec ) {
310  write_img(twoD_recons_name, prec, 0);
311  delete prec;
312  }
313  for ( Bparticle* part = project->class_avg; part; part = part->next )
314  part->fpart = twoD_recons_name;
315  twoD_recons_name = 0;
316  } else if ( verbose > VERB_RESULT ) {
318  }
319 
320  time_t t = time(NULL);
321  project->comment += "# Finished at: ";
322  project->comment += asctime(localtime(&t));
323  project->comment += "\n";
324 
325  // Write an output STAR format file if a name is given
326  if ( outfile.length() )
327  write_project(outfile, project, 0, 0);
328 
330  kill_symmetry(sym);
331  outfile = 0;
332  symmetry_string = 0;
333 
334 #ifdef HAVE_GCD
335  if ( !nothreads ) fftwf_cleanup_threads();
336 #endif
337 
338  if ( verbose & VERB_TIME )
339  timer_report(ti);
340 
341  bexit(0);
342 }
343 
Boption
A class for a command line input option.
Definition: options.h:26
CHECK_PPX
#define CHECK_PPX
Definition: mg_processing.h:27
mg_reconstruct.h
Functions for reconstruction.
project_kill
int project_kill(Bproject *project)
Frees a whole project.
Definition: mg_processing.cpp:429
project_reconstruct_2D
Bimage * project_reconstruct_2D(Bproject *project, int transform_output)
Creates a 2D reconstruction from the images in a multi-image file.
Definition: mg_reconstruct.cpp:850
project_count_mg_particles
long project_count_mg_particles(Bproject *project)
Counts all the particles in the project structure.
Definition: mg_select.cpp:415
mg_particle_select.h
Select particles.
WRITE_PPX
#define WRITE_PPX
Definition: mg_processing.h:26
Boption::scale
Vector3< double > scale()
Definition: options.h:190
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:768
FULL_ASU
#define FULL_ASU
Definition: mg_processing.h:22
M_PI_4
#define M_PI_4
Definition: utilities.h:69
system_processors
size_t system_processors()
Returns the number of processors.
Definition: utilities.cpp:193
Bstring
Definition: Bstring.h:29
Bparticle
Definition: mg_processing.h:90
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::next
Boption * next
Definition: options.h:28
Vector3< double >
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
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
PART_LOG
#define PART_LOG
Definition: mg_processing.h:25
Bstring::real
double real()
Definition: Bstring.cpp:553
options.h
Header file for option handlers.
project_show_class_averages
long project_show_class_averages(Bproject *project)
Shows the class averages in a project.
Definition: mg_processing.cpp:1617
Bsymmetry
Definition: symmetry.h:38
mg_orient.h
Library routines for single particle analysis.
project
Bproject * project
Definition: bshow.cpp:33
Boption::tag
Bstring tag
Definition: options.h:29
project_determine_origins
int project_determine_origins(Bproject *project, Bimage *proj, int bin, Bsymmetry *sym, int part_select, double res_lo, double res_hi, double shift_limit, int flags)
Find the origin of every image in a project.
Definition: mg_orient.cpp:1243
verbose
int verbose
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
rwmg.h
Header file for reading and writing micrograph parameters.
project_set_mg_pixel_size
int project_set_mg_pixel_size(Bproject *project, Vector3< double > pixel_size)
Sets the pixel size for all the micrographs.
Definition: mg_processing.cpp:1997
part_reset_selection
long part_reset_selection(Bproject *project, int flag=0)
Resets selection to all particles.
Definition: mg_particle_select.cpp:133
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
part_deselect
long part_deselect(Bproject *project, int fom_index, double fommin, double fommax=1e30)
Deselects particles below a given FOM cutoff.
Definition: mg_particle_select.cpp:468
Boption::value
Bstring value
Definition: options.h:30
VERB_RESULT
#define VERB_RESULT
Definition: utilities.h:109
project_reconstruct_2D_fast
Bimage * project_reconstruct_2D_fast(Bproject *project)
Definition: mg_reconstruct.cpp:1160
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
Bproject
Definition: mg_processing.h:336
project_prepare_2D_references
Bimage * project_prepare_2D_references(Bproject *project, long first, long number, int bin=1, int ctf_action=0, double wiener=0.2)
Prepare reference images from particles.
Definition: mg_orient.cpp:65
part_select
Tcl_Obj * part_select(Bparticle *part, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bmg.cpp:563
Bstring::split_into_doubles
vector< double > split_into_doubles(const Bstring &delim)
Definition: Bstring.cpp:673
Boption::symmetry_string
Bstring symmetry_string()
Definition: options.h:233
mg_img_proc.h
Header file for image processing from micrograph structures.
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
project_determine_orientations2
int project_determine_orientations2(Bproject *project, Bimage *proj, Bstring &mask_file, int bin, Bsymmetry *sym, int part_select, vector< double > &band, double res_lo, double res_hi, double res_polar, int ann_min, int ann_max, double shift_limit, double angle_limit, double edge_radius, int flags)
Find the orientation and origin of every image in a project.
Definition: mg_orient.cpp:1046
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
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
Bproject::class_avg
Bparticle * class_avg
Definition: mg_processing.h:348
main
int main(int argc, char **argv)
Definition: borient.cpp:81
MULTI_FILE
#define MULTI_FILE
Definition: mg_processing.h:23
project_set_views_from_images
int project_set_views_from_images(Bproject *project)
Gets views from particle images.
Definition: mg_img_proc.cpp:1144
write_project
int write_project(const char *filename, Bproject *project)
Writing micrograph parameters to a file.
Definition: rwmg.cpp:321
APPLY_CTF
#define APPLY_CTF
Definition: mg_processing.h:24
mg_processing.h
Header file for micrograph processing.
Bparticle::next
Bparticle * next
Definition: mg_processing.h:91
kill_symmetry
int kill_symmetry(Bsymmetry *sym)
Deallocates a symmetry structure.
Definition: symmetry.cpp:207
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
Bproject::comment
Bstring comment
Definition: mg_processing.h:339
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
FOM_CV
@ FOM_CV
Definition: mg_processing.h:42
img_prepare_projections
Bimage * img_prepare_projections(Bstring &filename, Bstring &mask_file, int bin, Bsymmetry *sym, double theta_step, double phi_step, double side_ang)
Generate projections if a 3D file, otherwise clean projections.
Definition: mg_orient.cpp:156
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:713
use
const char * use[]
Definition: borient.cpp:26
FOM
@ FOM
Definition: mg_processing.h:40
Bproject::fom_tag
FOMType fom_tag[NFOM]
Definition: mg_processing.h:345