| Bsoft 2.0.5 Bernard's software package

bpick.cpp
Go to the documentation of this file.
1
9 #include "mg_pick.h"
10 #include "mg_processing.h"
11 #include "mg_img_proc.h"
12 #include "mg_extract.h"
13 #include "mg_particles.h"
14 #include "mg_particle_select.h"
15 #include "rwmg.h"
16 #include "utilities.h"
17 #include "options.h"
18 #include "timer.h"
19
20 // Declaration of global variables
21 extern int verbose; // Level of output to the screen
22
23 // Usage assistance
24 const char* use[] = {
25 " ",
26 "Usage: bpick [options] input.star [input2.star ...]",
27 "---------------------------------------------------",
28 "Picks and extracts single particles from 2D (micrograph) or 3D (tomogram) images.",
29 " ",
30 "Actions:",
31 "-pick cc Picking type: cc, var, ring, background (default none).",
32 "-orient ref.pif Orient using a reference map.",
33 " High resolution and symmetry determine number of projections.",
34 "-center Center each particle image (specify resolution limits).",
35 "-axis 5,13.6 Pick on a symmetry axis and distance along the axis (voxels, specify -symmetry).",
36 "-extract 0.5 Extract particles into new images at the given scale.",
37 "-split Split particles into individual files on extraction.",
38 "-reconstructions Operate on reconstruction parameters rather than micrographs.",
39 "-add Adjust micrograph coordinates to particle origins.",
40 "-filterextremes Filter extremes before picking or centering.",
41 "-background Correct background of extracted particles.",
42 "-normalize Normalize extracted particles.",
43 "-delete Delete all non-selected particles.",
44 " ",
45 "Parameters:",
46 "-verbose 7 Verbosity of output.",
47 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; a single value can be given).",
48 "-origin 0.8,-10,15.7 Set the origin (default from input image).",
49 "-box 123,145,130 Particle box size (voxels, one sets all).",
50 "-resolution 23.5,200 Resolution limits for picking and centering (default 0 - 1e6 angstrom).",
51 "-fill 127 Fill value for particles extending beyond image (default background).",
52 "-mgpath dir/subdir Set the micrograph file paths.",
53 "-pspath dir/subdir Set the power spectrum file paths.",
54 "-partpath dir/subdir Set the particle file paths.",
55 "-base subvol Set the particle base file name (no extension).",
56 "-extension pif Set the particle image file format.",
57 " ",
58 "Parameters for picking particles:",
59 "-average 11 Averaging/smoothing kernel size.",
60 "-exclusion 210 Minimum distance between particles (default 0.8*x-size).",
61 "-bin 2 Binning to apply (default 1).",
62 "-fom 0.1,0.8 Selecting picked particles based on an FOM range (default 0,1e30).",
63 " ",
64 "Parameters for picking particles in variance map:",
65 "-variance 51 Calculate a local variance image using the given size kernel.",
66 "-sigma 2.5 Sigma units above average to select particles (default 5).",
67 "-maximum 241 Maximum number of background areas to pick (default 1000000).",
68 " ",
69 "Parameters for picking particles by ring pattern:",
70 "-diameters 115,130 Particle and background ring diameters.",
71 "-contrast black Foreground (particles) are white or black (default).",
72 "-angle 2.5 Angular increment (default 1 degree).",
73 " ",
74 "Parameter for orienting and selecting a symmetry axis:",
75 "-symmetry C5 Point group symmetry (used with -orient and -axis).",
76 " ",
77 "Input:",
78 "-Template file.map Input template file for picking by cross-correlation.",
79 "-Mask file.map Input frequency space mask file.",
80 "-compare file.star Input parameter file for comparison.",
81 " ",
82 "Output:",
83 "-output file.star Output parameter file.",
84 " ",
85 NULL
86 };
87
88 int main(int argc, char* argv[])
89 {
90 // Initialize optional variables
91 int pick_type(0); // 1=cc, 2=var, 3=ring, 4=bkg
92 int use_rec(0); // Flag to process reconstructions
93 int center(0); // Flag to center particles
94 int sym_axis(0); // Symmetry axis
95 int delete_deselected(0); // Flag to delete deselected particles
96 double ext_scale(0); // Flag and scale to extract particles
97 double axis_dist(0); // Distance along icosahedral axis
98 Vector3<double> sampling; // Units for the three axes (A/pixel)
99 Vector3<double> origin; // Origin
100 int set_origin(0); // Flag to set origin
101 int add_coordinates(0); // For adding mg coordinates and particle origins
102 int flags(0); // Bit 1=filter
103 Bsymmetry* sym = NULL; // Point group
104 double hires(0), lores(0); // Resolution limits for centering
105 double excl_dist(0); // Minimum distance between particles picked
106 long bin(1); // Binning to apply
107 double fommin(0), fommax(1e30); // FOM range for particle selection
108 long average_kernel(0); // Average filter kernel size
109 long var_kernel(0); // Variance filter kernel size
110 double sigma(5); // Sigma units above average to select particles
111 long maxnum(1000000); // Maximum number of background areas to pick
112 double rp_din(0), rp_dout(0); // Diameters for finding particles
113 double rp_anginc(M_PI/180); // Angular increment for finding particles
114 int contrast(0); // Foreground: white=1, black=0
115 int split(0); // Flag to split output into individual files
116 Vector3<long> box_size; // Box size for picking and extracting particles
117 int back_flag(0); // Background correction for extracting particles
118 int norm_flag(0); // Normalization for extracting particles
119 int fill_type(FILL_BACKGROUND);
120 double fill(0);
121 Bstring mgpath; // Micrograph file path
122 Bstring pspath; // Power spectrum file path
123 Bstring partpath; // Particle file path
124 Bstring partbase; // Particle base file name
125 Bstring partext; // Particle image file format
126 Bstring tempfile; // Input template file name for picking
127 Bstring orientfile; // Input template file name for orientation
128 Bstring maskfile; // Input frequency space mask file name
129 Bstring compfile; // Input comparison parameter file name
130 Bstring outfile; // Output parameter file name
131
132 int optind;
133 Boption* option = get_option_list(use, argc, argv, optind);
134 Boption* curropt;
135 for ( curropt = option; curropt; curropt = curropt->next ) {
136 if ( curropt->tag == "pick" ) {
137 if ( curropt->value[0] == 'c' ) pick_type = 1;
138 if ( curropt->value[0] == 'v' ) pick_type = 2;
139 if ( curropt->value[0] == 'r' ) pick_type = 3;
140 if ( curropt->value[0] == 'b' ) pick_type = 4;
141 }
142 if ( curropt->tag == "axis" )
143 if ( curropt->values(sym_axis, axis_dist) < 2 )
144 cerr << "-axis: Both an axis and distance must be specified!" << endl;
145 if ( curropt->tag == "orient" )
146 orientfile = curropt->filename();
147 if ( curropt->tag == "delete" )
148 delete_deselected = 1;
149 if ( curropt->tag == "extract" )
150 if ( ( ext_scale = curropt->value.real() ) < 0.01 )
151 cerr << "-extract: An extraction scale must be specified! (usually 1)" << endl;
152 if ( curropt->tag == "reconstructions" ) use_rec = 1;
153 if ( curropt->tag == "add" ) add_coordinates = 1;
154 if ( curropt->tag == "diameters" )
155 if ( curropt->values(rp_din, rp_dout) < 2 )
156 cerr << "-diameters: Two diameters must be specified!" << endl;
157 if ( curropt->tag == "contrast" ) if ( curropt->value[0] == 'w' ) contrast = 1;
158 if ( curropt->tag == "filterextremes" ) flags |= 1;
159 if ( curropt->tag == "center" ) center = 1;
160 if ( curropt->tag == "background" ) back_flag = 1;
161 if ( curropt->tag == "normalize" ) norm_flag = 1;
162 if ( curropt->tag == "split" ) split = 1;
163 if ( curropt->tag == "sampling" )
164 sampling = curropt->sampling();
165 if ( curropt->tag == "origin" ) {
166 if ( curropt->value[0] == 'c' ) {
167 set_origin = 2;
168 } else {
169 origin = curropt->origin();
170 set_origin = 1;
171 }
172 }
173 if ( curropt->tag == "box" ) box_size = curropt->size();
174 if ( curropt->tag == "symmetry" )
175 sym = curropt->symmetry();
176 if ( curropt->tag == "resolution" )
177 if ( curropt->values(hires, lores) < 1 )
178 cerr << "-resolution: A resolution limit must be specified!" << endl;
179 if ( curropt->tag == "exclusion" )
180 if ( ( excl_dist = curropt->value.real() ) <= 0 )
181 cerr << "-exclusion: An separation distance must be specified!" << endl;
182 if ( curropt->tag == "bin" )
183 if ( ( bin = curropt->value.integer() ) < 1 )
184 cerr << "-bin: An integer must be specified!" << endl;
185 if ( curropt->tag == "fom" )
186 if ( curropt->values(fommin, fommax) < 1 )
187 cerr << "-fom: A FOM cutoff must be specified!" << endl;
188 if ( curropt->tag == "average" )
189 if ( ( average_kernel = curropt->integer() ) < 1 )
190 cerr << "-average: The kernel edge size must be specified!" << endl;
191 if ( curropt->tag == "variance" )
192 if ( ( var_kernel = curropt->integer() ) < 1 )
193 cerr << "-variance: The kernel edge size must be specified!" << endl;
194 if ( curropt->tag == "sigma" )
195 if ( ( sigma = curropt->value.real() ) <= 0 )
196 cerr << "-sigma: A value must be specified!" << endl;
197 if ( curropt->tag == "maximum" )
198 if ( ( maxnum = curropt->integer() ) < 1 )
199 cerr << "-maximum: The maximum number of background areas must be specified!" << endl;
200 if ( curropt->tag == "angle" ) {
201 if ( ( rp_anginc = curropt->value.real() ) < 0.01 )
202 cerr << "-angle: An angular increment must be specified!" << endl;
203 else
204 rp_anginc *= M_PI/180.0;
205 }
206 if ( curropt->tag == "fill" )
207 fill = curropt->fill(fill_type);
208 if ( curropt->tag == "mgpath" ) {
209 mgpath = curropt->value;
210 if ( mgpath.length() < 1 )
211 cerr << "-mgpath: The micrograph file path must be specified!" << endl;
212 else
213 if ( mgpath[-1] != '/' ) mgpath += "/";
214 }
215 if ( curropt->tag == "pspath" ) {
216 pspath = curropt->value;
217 if ( pspath.length() < 1 )
218 cerr << "-pspath: The power spectrum file path must be specified!" << endl;
219 else
220 if ( pspath[-1] != '/' ) pspath += "/";
221 }
222 if ( curropt->tag == "partpath" ) {
223 partpath = curropt->value;
224 if ( partpath.length() < 1 )
225 cerr << "-partpath: The particle file path must be specified!" << endl;
226 else
227 if ( partpath[-1] != '/' ) partpath += "/";
228 }
229 if ( curropt->tag == "base" ) {
230 partbase = curropt->value;
231 if ( partbase.length() < 1 )
232 cerr << "-base: The particle base file name must be specified!" << endl;
233 }
234 if ( curropt->tag == "extension" ) {
235 partext = curropt->value;
236 if ( partext.length() < 1 )
237 cerr << "-extension: The particle file extension must be specified!" << endl;
238 }
239 if ( curropt->tag == "Template" )
240 tempfile = curropt->filename();
241 if ( curropt->tag == "Mask" )
242 maskfile = curropt->filename();
243 if ( curropt->tag == "compare" )
244 compfile = curropt->filename();
245 if ( curropt->tag == "output" )
246 outfile = curropt->filename();
247 }
248 option_kill(option);
249
250 double ti = timer_start();
251
252 // Read all the parameter files
253 Bstring* file_list = NULL;
254 while ( optind < argc ) string_add(&file_list, argv[optind++]);
255 if ( !file_list ) {
256 cerr << "Error: No parameter files specified!" << endl;
257 bexit(-1);
258 }
259
260 Bproject* project = read_project(file_list);
261 string_kill(file_list);
262
263 if ( project == NULL ) {
264 cerr << "Error: No input file read!" << endl;
265 bexit(-1);
266 }
267
268 if ( !sym ) sym = init_point_group_symmetry("C1");
269
270 if ( use_rec ) project->select = 1;
271
272 Bproject* projcomp = NULL;
273 if ( compfile.length() ) {
274 projcomp = read_project(compfile);
275 if ( use_rec ) projcomp->select = 1;
276 }
277
278 project_update_comment(project, argc, argv);
279
280 // cout << "mgpath = " << mgpath << endl;
281 if ( mgpath.length() )
282 project_set_micrograph_path(project, mgpath);
283 if ( pspath.length() )
284 project_set_powerspectrum_path(project, pspath);
285 if ( partpath.length() )
286 project_set_particle_path(project, partpath);
287
288 if ( sampling[0] > 0 )
289 project_set_pixel_size(project, sampling[0]);
290
291 if ( box_size[0] > 0 )
292 project_set_particle_box_size(project, box_size);
293
294 if ( add_coordinates )
295 project_add_origins_to_coords(project);
296
297 Bimage* ptemp = NULL;
298 if ( tempfile.length() ) {
299 ptemp = read_img(tempfile, 1, 0);
300 if ( set_origin ) {
301 if ( set_origin == 2 ) ptemp->origin(ptemp->size()/2);
302 else ptemp->origin(origin);
303 } else
304 origin = ptemp->image->origin();
305 }
306
307 if ( origin.length() < 1 ) origin = box_size/2;
308
309 Bimage* pmask = NULL;
310 if ( maskfile.length() )
311 pmask = read_img(maskfile, 1, 0);
312
313 if ( ptemp && excl_dist < 1 ) excl_dist = 0.8*ptemp->sizeX();
314
315 if ( pick_type == 1 ) {
316 if ( ptemp )
317 project_pick_particles(project, ptemp, pmask, hires, lores, fommin, fommax, excl_dist, bin);
318 else
319 cerr << "Error: Picking particles by cross-correlation requires a template!" << endl;
320 } else if ( pick_type == 2 ) {
321 project_pick_particles(project, average_kernel, var_kernel, sigma, origin[0], excl_dist, bin);
322 } else if ( pick_type == 3 ) {
323 project_pick_particles(project, rp_din, rp_dout, average_kernel, rp_anginc, flags, contrast);
324 } else if ( pick_type == 4 ) {
325 project_pick_background(project, maxnum, average_kernel, var_kernel, excl_dist);
326 }
327
328 project_set_part_links(project);
329
330 if ( sym && sym_axis > 1 )
331 project_pick_sym_axis(project, sym, sym_axis, axis_dist);
332
333 if ( center )
334 project_find_part_centers_in_mgs(project, hires, lores, flags);
335
336 if ( orientfile.length() ) {
337 project_extract_orient_particles(project, orientfile, sym, hires, lores, bin);
338 project_delesect_edge_particles(project);
339 }
340
341 if ( fommin > 0 )
342 part_deselect(project, 0, fommin, fommax);
343
344 if ( excl_dist ) {
345 part_deselect_redundant(project, excl_dist, -1, 0);
346 project_delesect_edge_particles(project);
347 }
348
349 if ( delete_deselected ) part_delete_deselected(project);
350
351 if ( projcomp )
352 project_compare_particles(project, projcomp);
353
354 if ( ext_scale )
355 project_extract_particles(project, ext_scale, back_flag, norm_flag,
356 fill_type, fill, 0, split, partbase, partpath, partext);
357
358 if ( verbose ) project_show_selected(project);
359
360 if ( project && outfile.length() )
361 write_project(outfile, project, 0, 0);
362
363 project_kill(project);
364 if ( projcomp ) project_kill(projcomp);
365 kill_symmetry(sym);
366
367 if ( verbose & VERB_TIME )
368 timer_report(ti);
369
370 return 0;
371 }
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.
project_set_particle_path
int project_set_particle_path(Bproject *project, Bstring &path)
Sets the path to particle files for all the micrographs.
Definition: mg_processing.cpp:3254
Bstring::real
double real()
Definition: Bstring.cpp:545
Boption::fill
double fill(int &fill_type)
Definition: options.h:138
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:705
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:174
project_pick_background
long project_pick_background(Bproject *project, long number, long average_kernel, long var_kernel, double excl_dist)
Picks background areas not overlapping existing particles.
Definition: mg_pick.cpp:850
project_pick_sym_axis
long project_pick_sym_axis(Bproject *project, Bsymmetry *sym, int sym_axis, double axis_dist)
Picks subregions in 3D particles on the given symmetry axis.
Definition: mg_pick.cpp:946
Vector3::length
double length()
Definition: Vector3.h:222
project_extract_orient_particles
double project_extract_orient_particles(Bproject *project, Bstring &tempfile, Bsymmetry *sym, double hires, double lores, long bin)
Picks particles using cross-correlation.
Definition: mg_pick.cpp:1303
project_delesect_edge_particles
long project_delesect_edge_particles(Bproject *project)
Deselects particles that are too close to the image edges.
Definition: mg_img_proc.cpp:757
mg_particle_select.h
Select particles.
use
const char * use[]
Definition: bpick.cpp:24
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
mg_img_proc.h
Header file for image processing from micrograph structures.
main
int main(int argc, char *argv[])
Definition: bpick.cpp:88
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_pick_particles
double project_pick_particles(Bproject *project, Bimage *ptemp, Bimage *pmask, double hires, double lores, double fommin, double fommax, double excl_dist, long bin)
Picks particles using cross-correlation.
Definition: mg_pick.cpp:535
kill_symmetry
int kill_symmetry(Bsymmetry *sym)
Deallocates a symmetry structure.
Definition: symmetry.cpp:207
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
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:76
project_show_selected
long project_show_selected(Bproject *project)
Shows the selected particles in a project.
Definition: mg_processing.cpp:2446
Boption::filename
Bstring filename()
Definition: options.h:85
timer.h
Utilities for timing functions.
Bimage
General image parameter class.
Definition: Bimage.h:163
Boption::size
Vector3< long > size()
Definition: options.h:163
project_set_particle_box_size
int project_set_particle_box_size(Bproject *project, Vector3< long > box_size)
Sets particle box size to the given value.
Definition: mg_processing.cpp:3693
M_PI
#define M_PI
Definition: utilities.h:59
Boption::integer
long integer()
Definition: options.h:100
project_compare_particles
long project_compare_particles(Bproject *project, Bproject *projcomp)
Compares the coordinates of particles between two files.
Definition: mg_particles.cpp:650
mg_particles.h
Manipulates single particle images.
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
VERB_TIME
#define VERB_TIME
Definition: utilities.h:110
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
part_delete_deselected
long part_delete_deselected(Bproject *project)
Deletes deselected particles and renumber the remaining.
Definition: mg_particle_select.cpp:2703
project_set_micrograph_path
int project_set_micrograph_path(Bproject *project, Bstring &path)
Sets the path to micrograph files for all the micrographs.
Definition: mg_processing.cpp:3167
Boption::symmetry
Bsymmetry * symmetry()
Definition: options.h:252
mg_extract.h
Header file for functions to extract particles from micrographs.
part_deselect_redundant
long part_deselect_redundant(Bparticle *partlist, double excl_dist, int part_select, int fom_index)
Deselects particles overlapping with better ones.
Definition: mg_particle_select.cpp:494
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:127
project_extract_particles
long project_extract_particles(Bproject *project, double scale, int back_flag, int norm_flag, int fill_type, double fill, int mask_width, int split, Bstring &partbase, Bstring &partpath, Bstring &partext)
Extracts particle images from micrographs defined in a project.
Definition: mg_extract.cpp:160
project_add_origins_to_coords
int project_add_origins_to_coords(Bproject *project)
Adds the particle origins to the micrograph coordinates.
Definition: mg_processing.cpp:3563
Bsymmetry
Definition: symmetry.h:38
Boption::next
Boption * next
Definition: options.h:28
Bstring
Definition: Bstring.h:29
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:377
project_kill
int project_kill(Bproject *project)
Frees a whole project.
Definition: mg_processing.cpp:1224
Bproject::select
int select
Definition: mg_processing.h:339
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
mg_pick.h
Header file for single particle picking functions.
Vector3< double >
Boption::origin
Vector3< double > origin()
Definition: options.h:180
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:67
verbose
int verbose
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:109
Bstring::integer
long integer()
Definition: Bstring.cpp:539
mg_processing.h
Header file for micrograph processing.
options.h
Header file for option handlers.
project_set_powerspectrum_path
int project_set_powerspectrum_path(Bproject *project, Bstring &path)
Sets the path to powerspectrum files for all the micrographs.
Definition: mg_processing.cpp:3225
split
vector< string > split(const string &s)
Splits a string on whitespace.
Definition: string_util.cpp:92
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
project_find_part_centers_in_mgs
long project_find_part_centers_in_mgs(Bproject *project, double hires, double lores, int filter_flag)
Finds the centers of picked particles within a micrograph.
Definition: mg_particles.cpp:319
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
project_set_part_links
long project_set_part_links(Bproject *project)
Sets the links from particles back to the micrographs and reconstructions.
Definition: mg_processing.cpp:1906
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:52
Bproject
Definition: mg_processing.h:333
Bimage::image
Bsub_image * image
Definition: Bimage.h:188
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:563
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:760