Bsoft  2.1.0
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 "-project Project 3D particles to 2D and assign to zero-tilt micrograph.",
40 "-add Adjust micrograph coordinates to particle origins.",
41 "-filterextremes Filter extremes before picking or centering.",
42 "-background Correct background of extracted particles.",
43 "-normalize Normalize extracted particles.",
44 "-delete Delete all non-selected particles.",
45 " ",
46 "Parameters:",
47 "-verbose 7 Verbosity of output.",
48 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; a single value can be given).",
49 "-origin 0.8,-10,15.7 Set the origin (default from input image).",
50 "-box 123,145,130 Particle box size (voxels, one sets all).",
51 "-resolution 23.5,200 Resolution limits for picking and centering (default 0 - 1e6 angstrom).",
52 "-fill 127 Fill value for particles extending beyond image (default background).",
53 "-mgpath dir/subdir Set the micrograph file paths.",
54 "-pspath dir/subdir Set the power spectrum file paths.",
55 "-partpath dir/subdir Set the particle file paths.",
56 "-base subvol Set the particle base file name (no extension).",
57 "-extension pif Set the particle image file format.",
58 " ",
59 "Parameters for picking particles:",
60 "-average 11 Averaging/smoothing kernel size.",
61 "-exclusion 210 Minimum distance between particles (default 0.8*x-size).",
62 "-bin 2 Binning to apply (default 1).",
63 "-fom 0.1,0.8 Selecting picked particles based on an FOM range (default 0,1e30).",
64 " ",
65 "Parameters for picking particles in variance map:",
66 "-variance 51 Calculate a local variance image using the given size kernel.",
67 "-sigma 2.5 Sigma units above average to select particles (default 5).",
68 "-maximum 241 Maximum number of background areas to pick (default 1000000).",
69 " ",
70 "Parameters for picking particles by ring pattern:",
71 "-diameters 115,130 Particle and background ring diameters.",
72 "-contrast black Foreground (particles) are white or black (default).",
73 "-angle 2.5 Angular increment (default 1 degree).",
74 " ",
75 "Parameter for orienting and selecting a symmetry axis:",
76 "-symmetry C5 Point group symmetry (used with -orient and -axis).",
77 " ",
78 "Input:",
79 "-Template file.map Input template file for picking by cross-correlation.",
80 "-Mask file.map Input frequency space mask file.",
81 "-compare file.star Input parameter file for comparison.",
82 " ",
83 "Output:",
84 "-output file.star Output parameter file.",
85 " ",
86 NULL
87 };
88 
89 int main(int argc, char* argv[])
90 {
91  // Initialize optional variables
92  int pick_type(0); // 1=cc, 2=var, 3=ring, 4=bkg
93  int use_rec(0); // Flag to process reconstructions
94  int center(0); // Flag to center particles
95  int sym_axis(0); // Symmetry axis
96  int delete_deselected(0); // Flag to delete deselected particles
97  double ext_scale(0); // Flag and scale to extract particles
98  double axis_dist(0); // Distance along icosahedral axis
99  Vector3<double> sampling; // Units for the three axes (A/pixel)
100  Vector3<double> origin; // Origin
101  int set_origin(0); // Flag to set origin
102  int add_coordinates(0); // For adding mg coordinates and particle origins
103  int flags(0); // Bit 1=filter
104  Bsymmetry* sym = NULL; // Point group
105  double hires(0), lores(0); // Resolution limits for centering
106  double excl_dist(0); // Minimum distance between particles picked
107  long bin(1); // Binning to apply
108  double fommin(0), fommax(1e30); // FOM range for particle selection
109  long average_kernel(0); // Average filter kernel size
110  long var_kernel(0); // Variance filter kernel size
111  double sigma(5); // Sigma units above average to select particles
112  long maxnum(1000000); // Maximum number of background areas to pick
113  double rp_din(0), rp_dout(0); // Diameters for finding particles
114  double rp_anginc(M_PI/180); // Angular increment for finding particles
115  int contrast(0); // Foreground: white=1, black=0
116  int split(0); // Flag to split output into individual files
117  Vector3<long> box_size; // Box size for picking and extracting particles
118  int back_flag(0); // Background correction for extracting particles
119  int norm_flag(0); // Normalization for extracting particles
120  int fill_type(FILL_BACKGROUND);
121  double fill(0);
122  Bstring mgpath; // Micrograph file path
123  Bstring pspath; // Power spectrum file path
124  Bstring partpath; // Particle file path
125  Bstring partbase; // Particle base file name
126  Bstring partext; // Particle image file format
127  Bstring tempfile; // Input template file name for picking
128  Bstring orientfile; // Input template file name for orientation
129  Bstring maskfile; // Input frequency space mask file name
130  Bstring compfile; // Input comparison parameter file name
131  Bstring outfile; // Output parameter file name
132 
133  int optind;
134  Boption* option = get_option_list(use, argc, argv, optind);
135  Boption* curropt;
136  for ( curropt = option; curropt; curropt = curropt->next ) {
137  if ( curropt->tag == "pick" ) {
138  if ( curropt->value[0] == 'c' ) pick_type = 1;
139  if ( curropt->value[0] == 'v' ) pick_type = 2;
140  if ( curropt->value[0] == 'r' ) pick_type = 3;
141  if ( curropt->value[0] == 'b' ) pick_type = 4;
142  }
143  if ( curropt->tag == "axis" )
144  if ( curropt->values(sym_axis, axis_dist) < 2 )
145  cerr << "-axis: Both an axis and distance must be specified!" << endl;
146  if ( curropt->tag == "orient" )
147  orientfile = curropt->filename();
148  if ( curropt->tag == "delete" )
149  delete_deselected = 1;
150  if ( curropt->tag == "extract" )
151  if ( ( ext_scale = curropt->value.real() ) < 0.01 )
152  cerr << "-extract: An extraction scale must be specified! (usually 1)" << endl;
153  if ( curropt->tag == "reconstructions" ) use_rec = 1;
154  if ( curropt->tag == "project" ) use_rec = 2;
155  if ( curropt->tag == "add" ) add_coordinates = 1;
156  if ( curropt->tag == "diameters" )
157  if ( curropt->values(rp_din, rp_dout) < 2 )
158  cerr << "-diameters: Two diameters must be specified!" << endl;
159  if ( curropt->tag == "contrast" ) if ( curropt->value[0] == 'w' ) contrast = 1;
160  if ( curropt->tag == "filterextremes" ) flags |= 1;
161  if ( curropt->tag == "center" ) center = 1;
162  if ( curropt->tag == "background" ) back_flag = 1;
163  if ( curropt->tag == "normalize" ) norm_flag = 1;
164  if ( curropt->tag == "split" ) split = 1;
165  if ( curropt->tag == "sampling" )
166  sampling = curropt->scale();
167  if ( curropt->tag == "origin" ) {
168  if ( curropt->value[0] == 'c' ) {
169  set_origin = 2;
170  } else {
171  origin = curropt->origin();
172  set_origin = 1;
173  }
174  }
175  if ( curropt->tag == "box" ) box_size = curropt->size();
176  if ( curropt->tag == "symmetry" )
177  sym = curropt->symmetry();
178  if ( curropt->tag == "resolution" )
179  if ( curropt->values(hires, lores) < 1 )
180  cerr << "-resolution: A resolution limit must be specified!" << endl;
181  if ( curropt->tag == "exclusion" )
182  if ( ( excl_dist = curropt->value.real() ) <= 0 )
183  cerr << "-exclusion: An separation distance must be specified!" << endl;
184  if ( curropt->tag == "bin" )
185  if ( ( bin = curropt->value.integer() ) < 1 )
186  cerr << "-bin: An integer must be specified!" << endl;
187  if ( curropt->tag == "fom" )
188  if ( curropt->values(fommin, fommax) < 1 )
189  cerr << "-fom: A FOM cutoff must be specified!" << endl;
190  if ( curropt->tag == "average" )
191  if ( ( average_kernel = curropt->integer() ) < 1 )
192  cerr << "-average: The kernel edge size must be specified!" << endl;
193  if ( curropt->tag == "variance" )
194  if ( ( var_kernel = curropt->integer() ) < 1 )
195  cerr << "-variance: The kernel edge size must be specified!" << endl;
196  if ( curropt->tag == "sigma" )
197  if ( ( sigma = curropt->value.real() ) <= 0 )
198  cerr << "-sigma: A value must be specified!" << endl;
199  if ( curropt->tag == "maximum" )
200  if ( ( maxnum = curropt->integer() ) < 1 )
201  cerr << "-maximum: The maximum number of background areas must be specified!" << endl;
202  if ( curropt->tag == "angle" ) {
203  if ( ( rp_anginc = curropt->value.real() ) < 0.01 )
204  cerr << "-angle: An angular increment must be specified!" << endl;
205  else
206  rp_anginc *= M_PI/180.0;
207  }
208  if ( curropt->tag == "fill" )
209  fill = curropt->fill(fill_type);
210  if ( curropt->tag == "mgpath" ) {
211  mgpath = curropt->value;
212  if ( mgpath.length() < 1 )
213  cerr << "-mgpath: The micrograph file path must be specified!" << endl;
214  else
215  if ( mgpath[-1] != '/' ) mgpath += "/";
216  }
217  if ( curropt->tag == "pspath" ) {
218  pspath = curropt->value;
219  if ( pspath.length() < 1 )
220  cerr << "-pspath: The power spectrum file path must be specified!" << endl;
221  else
222  if ( pspath[-1] != '/' ) pspath += "/";
223  }
224  if ( curropt->tag == "partpath" ) {
225  partpath = curropt->value;
226  if ( partpath.length() < 1 )
227  cerr << "-partpath: The particle file path must be specified!" << endl;
228  else
229  if ( partpath[-1] != '/' ) partpath += "/";
230  }
231  if ( curropt->tag == "base" ) {
232  partbase = curropt->value;
233  if ( partbase.length() < 1 )
234  cerr << "-base: The particle base file name must be specified!" << endl;
235  }
236  if ( curropt->tag == "extension" ) {
237  partext = curropt->value;
238  if ( partext.length() < 1 )
239  cerr << "-extension: The particle file extension must be specified!" << endl;
240  }
241  if ( curropt->tag == "Template" )
242  tempfile = curropt->filename();
243  if ( curropt->tag == "Mask" )
244  maskfile = curropt->filename();
245  if ( curropt->tag == "compare" )
246  compfile = curropt->filename();
247  if ( curropt->tag == "output" )
248  outfile = curropt->filename();
249  }
250  option_kill(option);
251 
252  double ti = timer_start();
253 
254  // Read all the parameter files
255  Bstring* file_list = NULL;
256  while ( optind < argc ) string_add(&file_list, argv[optind++]);
257  if ( !file_list ) {
258  cerr << "Error: No parameter files specified!" << endl;
259  bexit(-1);
260  }
261 
262  Bproject* project = read_project(file_list);
263  string_kill(file_list);
264 
265  if ( project == NULL ) {
266  cerr << "Error: No input file read!" << endl;
267  bexit(-1);
268  }
269 
270  if ( !sym ) sym = init_point_group_symmetry("C1");
271 
272  project->select = use_rec;
273 
274  Bproject* projcomp = NULL;
275  if ( compfile.length() ) {
276  projcomp = read_project(compfile);
277  if ( use_rec ) projcomp->select = 1;
278  }
279 
280 // cout << "mgpath = " << mgpath << endl;
281  if ( mgpath.length() )
283  if ( pspath.length() )
285  if ( partpath.length() )
287 
288  if ( sampling[0] > 0 )
290 
291  if ( box_size[0] > 0 )
293 
294  if ( add_coordinates )
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 
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);
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);
347  }
348 
349  if ( delete_deselected ) part_delete_deselected(project);
350 
351  if ( 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 
359 
360  if ( project && outfile.length() )
361  write_project(outfile, project, 0, 0);
362 
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 }
Boption
A class for a command line input option.
Definition: options.h:26
project_kill
int project_kill(Bproject *project)
Frees a whole project.
Definition: mg_processing.cpp:429
mg_particle_select.h
Select particles.
mg_particles.h
Manipulates single particle images.
Boption::scale
Vector3< double > scale()
Definition: options.h:190
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:848
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:1824
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:322
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:768
Bproject::select
int select
Definition: mg_processing.h:342
Bstring
Definition: Bstring.h:29
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::next
Boption * next
Definition: options.h:28
Vector3< double >
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
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_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:944
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
mg_pick.h
Header file for single particle picking functions.
Bstring::real
double real()
Definition: Bstring.cpp:553
options.h
Header file for option handlers.
main
int main(int argc, char *argv[])
Definition: bpick.cpp:89
use
const char * use[]
Definition: bpick.cpp:24
Boption::origin
Vector3< double > origin()
Definition: options.h:180
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
Bsymmetry
Definition: symmetry.h:38
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:520
Boption::symmetry
Bsymmetry * symmetry()
Definition: options.h:240
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
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.
project_compare_particles
long project_compare_particles(Bproject *project, Bproject *projcomp)
Compares the coordinates of particles between two files.
Definition: mg_particles.cpp:653
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
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
project_show_selected
long project_show_selected(Bproject *project)
Shows the selected particles in a project.
Definition: mg_select.cpp:907
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:1853
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
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
Bproject
Definition: mg_processing.h:336
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:1301
mg_img_proc.h
Header file for image processing from micrograph structures.
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
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:1148
mg_extract.h
Header file for functions to extract particles from micrographs.
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.
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:867
Boption::size
Vector3< long > size()
Definition: options.h:163
split
vector< string > split(const string &s)
Splits a string on whitespace.
Definition: string_util.cpp:92
Boption::fill
double fill(int &fill_type)
Definition: options.h:138
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
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:2284
write_project
int write_project(const char *filename, Bproject *project)
Writing micrograph parameters to a file.
Definition: rwmg.cpp:321
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:533
mg_processing.h
Header file for micrograph processing.
kill_symmetry
int kill_symmetry(Bsymmetry *sym)
Deallocates a symmetry structure.
Definition: symmetry.cpp:207
Boption::filename
Bstring filename()
Definition: options.h:85
part_delete_deselected
long part_delete_deselected(Bproject *project)
Deletes deselected particles and renumber the remaining.
Definition: mg_particle_select.cpp:2729
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:178
verbose
int verbose
Vector3::length
double length()
Definition: Vector3.h:222
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:161
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:2154
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:713
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:1766
Boption::integer
long integer()
Definition: options.h:100