| Bsoft 2.0.5 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->sampling();
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_update_comment(project, argc, argv);
269 project->fom_tag[0] = FOM;
270 project->fom_tag[1] = FOM_CV;
271
272 if ( project_count_mg_particles(project) < 1 )
273 project_set_views_from_images(project); // This must be done before operations on particles
274
275 if ( reset ) part_reset_selection(project);
276
277 if ( sam[0] > 0 && sam[1] > 0 && sam[2] > 0 ) {
278 if ( sam[0] < 0.1 ) sam[0] = 1;
279 project_set_pixel_size(project, sam[0]);
280 }
281
282 if ( proj && outfile.length() ) {
283 if ( mode == 3 ) {
284 if ( project_determine_origins(project, proj, bin, sym,
285 part_select, res_lo, res_hi, shift_limit, flags) < 0 ) {
286 cerr << "Error: Alignment failed!" << endl;
287 bexit(-1);
288 }
289 } else {
290 if ( project_determine_orientations(project, proj, mask_filename,
291 bin, sym, part_select, band, res_lo, res_hi, res_polar,
292 ann_min, ann_max, shift_limit, angle_limit, edge_radius, flags) < 0 ) {
293 cerr << "Error: Alignment failed!" << endl;
294 bexit(-1);
295 }
296 }
297 }
298
299 delete proj;
300 mask_filename = 0;
301
302 if ( FOM_cut > 0 ) part_deselect(project, 0, FOM_cut);
303
304 Bimage* prec = NULL;
305 if ( twoD_recons_name.length() ) {
306 if ( transform_output )
307 prec = project_reconstruct_2D(project, transform_output);
308 else
309 prec = project_reconstruct_2D_fast(project);
310 if ( prec ) {
311 prec->label(get_cmd_line(argc, argv));
312 write_img(twoD_recons_name, prec, 0);
313 delete prec;
314 }
315 for ( Bparticle* part = project->class_avg; part; part = part->next )
316 part->fpart = twoD_recons_name;
317 twoD_recons_name = 0;
318 } else if ( verbose > VERB_RESULT ) {
319 project_show_class_averages(project);
320 }
321
322 time_t t = time(NULL);
323 project->comment += "# Finished at: ";
324 project->comment += asctime(localtime(&t));
325 project->comment += "\n";
326
327 // Write an output STAR format file if a name is given
328 if ( outfile.length() )
329 write_project(outfile, project, 0, 0);
330
331 project_kill(project);
332 kill_symmetry(sym);
333 outfile = 0;
334 symmetry_string = 0;
335
336 #ifdef HAVE_GCD
337 if ( !nothreads ) fftwf_cleanup_threads();
338 #endif
339
340 if ( verbose & VERB_TIME )
341 timer_report(ti);
342
343 bexit(0);
344 }
345
project_determine_orientations
int project_determine_orientations(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:329
write_project
int write_project(const char *filename, Bproject *project)
Writing micrograph parameters to a file.
Definition: rwmg.cpp:320
rwmg.h
Header file for reading and writing micrograph parameters.
Bstring::real
double real()
Definition: Bstring.cpp:545
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:705
project_set_views_from_images
int project_set_views_from_images(Bproject *project)
Gets views from particle images.
Definition: mg_img_proc.cpp:1033
PART_LOG
#define PART_LOG
Definition: mg_processing.h:25
Boption::symmetry_string
Bstring symmetry_string()
Definition: options.h:245
mg_particle_select.h
Select particles.
utilities.h
Header file for general utilities.
Boption::sampling
Vector3< double > sampling()
Definition: options.h:202
Boption::value
Bstring value
Definition: options.h:30
Boption::tag
Bstring tag
Definition: options.h:29
system_processors
size_t system_processors()
Returns the number of processors.
Definition: utilities.cpp:186
mg_img_proc.h
Header file for image processing from micrograph structures.
project_reconstruct_2D_fast
Bimage * project_reconstruct_2D_fast(Bproject *project)
Definition: mg_reconstruct.cpp:1132
WRITE_PPX
#define WRITE_PPX
Definition: mg_processing.h:26
read_project
Bproject * read_project(const char *filename, int flags=0)
Reading micrograph parameters from files.
Definition: rwmg.cpp:140
Bstring::length
long length() const
Definition: Bstring.cpp:263
project
Bproject * project
Definition: bshow.cpp:31
project_count_mg_particles
long project_count_mg_particles(Bproject *project)
Counts all the particles in the project structure.
Definition: mg_processing.cpp:811
verbose
int verbose
kill_symmetry
int kill_symmetry(Bsymmetry *sym)
Deallocates a symmetry structure.
Definition: symmetry.cpp:207
mg_orient.h
Library routines for single particle analysis.
part_select
Tcl_Obj * part_select(Bparticle *part, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bmg.cpp:432
CHECK_PPX
#define CHECK_PPX
Definition: mg_processing.h:27
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
get_cmd_line
string get_cmd_line(int argc, char **argv)
Returns the command line as a string.
Definition: utilities.cpp:36
Boption::filename
Bstring filename()
Definition: options.h:85
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:822
timer.h
Utilities for timing functions.
M_PI_4
#define M_PI_4
Definition: utilities.h:65
FOM_CV
Definition: mg_processing.h:42
Bimage
General image parameter class.
Definition: Bimage.h:163
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:584
M_PI
#define M_PI
Definition: utilities.h:59
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:442
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::label
void label(string s)
Definition: Bimage.h:235
VERB_TIME
#define VERB_TIME
Definition: utilities.h:110
Bproject::fom_tag
FOMType fom_tag[NFOM]
Definition: mg_processing.h:342
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
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:280
APPLY_CTF
#define APPLY_CTF
Definition: mg_processing.h:24
Bproject::class_avg
Bparticle * class_avg
Definition: mg_processing.h:345
part_reset_selection
long part_reset_selection(Bproject *project)
Resets selection to all particles.
Definition: mg_particle_select.cpp:131
Bstring::split_into_doubles
vector< double > split_into_doubles(const Bstring &delim)
Definition: Bstring.cpp:665
main
int main(int argc, char **argv)
Definition: borient.cpp:81
Bsymmetry
Definition: symmetry.h:38
FOM
Definition: mg_processing.h:40
VERB_RESULT
#define VERB_RESULT
Definition: utilities.h:105
Boption::next
Boption * next
Definition: options.h:28
Bparticle
Definition: mg_processing.h:89
Bstring
Definition: Bstring.h:29
FULL_ASU
#define FULL_ASU
Definition: mg_processing.h:22
project_kill
int project_kill(Bproject *project)
Frees a whole project.
Definition: mg_processing.cpp:1224
project_set_pixel_size
int project_set_pixel_size(Bproject *project, double pixel_size)
Sets the pixel size for all the micrographs.
Definition: mg_processing.cpp:3398
Vector3< double >
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:67
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:109
MULTI_FILE
#define MULTI_FILE
Definition: mg_processing.h:23
Bstring::integer
long integer()
Definition: Bstring.cpp:539
mg_processing.h
Header file for micrograph processing.
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
options.h
Header file for option handlers.
Bproject::comment
Bstring comment
Definition: mg_processing.h:336
mg_reconstruct.h
Functions for reconstruction.
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:797
project_update_comment
int project_update_comment(Bproject *project, int n, char **strings)
Puts a set of strings and time in the main comment of a project.
Definition: rwmg.cpp:468
Bparticle::next
Bparticle * next
Definition: mg_processing.h:90
project_show_class_averages
long project_show_class_averages(Bproject *project)
Shows the class averages in a project.
Definition: mg_processing.cpp:2645
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:273
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:52
Bproject
Definition: mg_processing.h:333
use
const char * use[]
Definition: borient.cpp:26
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:760
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