Bsoft  2.1.0
Bernard's software package
bseries.cpp
Go to the documentation of this file.
1 
9 #include "mg_processing.h"
10 #include "mg_align.h"
11 #include "mg_extract.h"
12 #include "rwmg.h"
13 #include "utilities.h"
14 #include "options.h"
15 #include "timer.h"
16 
17 // Declaration of global variables
18 extern int verbose; // Level of output to the screen
19 
20 // Usage assistance
21 const char* use[] = {
22 " ",
23 "Usage: bseries [options] input.star [input.star]",
24 "------------------------------------------------",
25 "Aligns series of micrographs using consecutive pairwise cross-correlation.",
26 " ",
27 "Actions:",
28 "-bin 3,2,1 Binning by the given kernel size - one value = isotropic binning.",
29 " (i.e., 2 = binning with a 2x2 kernel, changes the units).",
30 "-align 56 Align to this reference image (image numbers start at 0).",
31 "-filament 5.3,50 Box particles along filaments with this interval and mask width (voxels).",
32 "-extract 150,111,120 Extract particles with this size (voxels, one sets all).",
33 " ",
34 "Parameters:",
35 "-verbose 7 Verbosity of output.",
36 "-datatype u Force writing of a new data type.",
37 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; a single value can be given).",
38 "-origin 110,50,44 Origin for tilt axis (default 0,0,0).",
39 " ",
40 "Parameters for alignment:",
41 "-frames Flag to align micrograph frames.",
42 "-dose 2.5 Weigh the amplitudes by accumulated dose using the given dose/frame (e/Å2).",
43 "-exposure 8.5 Exposure time to calculate drift rate from alignment (seconds)",
44 "-rate 4.5 Frame rate (frames/second)",
45 "-counts Flag to rescale images based on their counts histogram.",
46 "-resolution 900,300 High and low resolution limits for cross-correlation (default 0.1,1000 angstrom).",
47 "-shiftlimit 3.5 Limit on origin shift relative to nominal center (default 10% of box edge size).",
48 "-edge 23,12 Smooth the edge to a given width, with gaussian decay of a given width.",
49 "-subset 2-8,12 Subset of micrographs to align and average.",
50 " ",
51 "Parameters for particle extraction:",
52 "-fill 127 Fill value for particles extending beyond image (default background).",
53 "-partbase subvol Set the particle base file name (no extension).",
54 "-partpath dir/subdir Set the particle file paths.",
55 "-partextension pif Set the particle image file format extension.",
56 "-helix 16.2,39.5 Helical parameters for filament boxing: helical rise ",
57 " and angle per asymmetric unit (angstroms and degrees).",
58 "-background Correct background of extracted particles.",
59 "-normalize Normalize extracted particles.",
60 " ",
61 "Input:",
62 "-Gainreference gr.tif Gain reference to correct the input micrographs.",
63 "-Mask mask.tif Mask file to use for cross-correlation.",
64 " ",
65 "Output:",
66 "-output file.star Output parameter file, if -average, also output average parameter file.",
67 "-write sum Output either aligned frames, sum or avg.",
68 "-image file.pif Output multi-image file with aligned images.",
69 "-average file.pif Output average of aligned images.",
70 " ",
71 NULL
72 };
73 
74 
75 int main(int argc, char** argv)
76 {
77  // Initializing variables
78  DataType datatype(Unknown_Type); // Conversion to new type
79  int ref_img(-1); // Reference image for alignment, <0 means don't align
80  int frames(0); // Flag to align micrograph frames
81  int flags(0); // Flags: 1=rescale based on histogram; 2=weigh by dose; 4=write aligned frames; 8=write frame average; 16=write frame sum
82  Vector3<double> sam; // Units for the three axes (A/pixel)
83  Bstring partbase; // Particle base file name
84  Bstring partpath; // Particle file path
85  Bstring part_ext; // Particle image file format
86  Vector3<long> box_size; // Size for extracting particles
87  double boxing_interval(0); // Boxing interval for defining particles along a filament
88  int mask_width(0); // Filament mask width
89  double helix_rise(0), helix_angle(0); // Helical parameters for filaments
90  int back_flag(0); // Background correction for extracting particles
91  int norm_flag(0); // Normalization for extracting particles
92  double hi_res(0), lo_res(1e10); // Default resolution range
93  double shift_limit(-1); // Maximum shift from nominal image origin
94  Vector3<double> origin; // Tilt axis origin
95  double edge_width(0), gauss_width(0); // Edge parameters
96  int fill_type(FILL_BACKGROUND);
97  double fill(0);
98  Vector3<long> bin = {1,1,1}; // Binning before alignment
99  Bstring subset; // Subset of micrographs to average
100  JSvalue dose_frac(JSobject); // Container for dose fractionation parameters
101  Bstring paramfile; // Output parameter file
102  Bstring grfile; // Gain reference
103  Bstring maskfile; // Mask to use for cross-correlation
104  Bstring imgfile; // Output multi-image file
105  Bstring avgfile; // Output average image file
106 
107  int i, optind;
108  Boption* option = get_option_list(use, argc, argv, optind);
109  Boption* curropt;
110  for ( curropt = option; curropt; curropt = curropt->next ) {
111  if ( curropt->tag == "datatype" )
112  datatype = curropt->datatype();
113  if ( curropt->tag == "sampling" )
114  sam = curropt->scale();
115  if ( curropt->tag == "align" )
116  if ( ( ref_img = curropt->value.integer() ) < 0 )
117  cerr << "-align: A reference image number must be specified." << endl;
118  if ( curropt->tag == "filament" )
119  if ( curropt->values(boxing_interval, mask_width) < 1 )
120  cerr << "-filament: A boxing interval must be specified." << endl;
121  if ( curropt->tag == "extract" ) {
122  if ( ( i=curropt->values(box_size[0], box_size[1], box_size[2]) ) < 1 )
123  cerr << "-extract: A box size must be specified." << endl;
124  else
125  if ( i < 2 ) box_size[2] = box_size[1] = box_size[0];
126  }
127  if ( curropt->tag == "background" )
128  back_flag = 1;
129  if ( curropt->tag == "normalize" )
130  norm_flag = 1;
131  if ( curropt->tag == "frames" )
132  frames = 1;
133  if ( curropt->tag == "counts" )
134  flags |= 1;
135  if ( curropt->tag == "dose" ) {
136  dose_frac["dose_per_frame"] = curropt->value.real();
137  if ( dose_frac["dose_per_frame"].real() < 0 )
138  cerr << "-dose: The dose per frame must be specified!" << endl;
139  else
140  flags |= 2;
141  }
142  if ( curropt->tag == "exposure" ) {
143  dose_frac["exposure"] = curropt->value.real();
144  if ( dose_frac["exposure"].real() < 0 )
145  cerr << "-exposure: An exposure time in seconds must be specified." << endl;
146  }
147  if ( curropt->tag == "rate" ) {
148  dose_frac["frame_rate"] = curropt->value.real();
149  if ( dose_frac["frame_rate"].real() < 0 )
150  cerr << "-rate: The number of frames per second must be specified." << endl;
151  }
152  if ( curropt->tag == "resolution" ) {
153  if ( curropt->values(hi_res, lo_res) < 1 )
154  cerr << "-resolution: Resolution limits must be specified." << endl;
155  else if ( hi_res > lo_res )
156  swap(hi_res, lo_res);
157  }
158  if ( curropt->tag == "shiftlimit" )
159  if ( ( shift_limit = curropt->value.real() ) < 1 )
160  cerr << "-shiftlimit: A maximum shift in pixels must be specified!" << endl;
161  if ( curropt->tag == "origin" )
162  origin = curropt->origin();
163  if ( curropt->tag == "edge" )
164  if ( curropt->values(edge_width, gauss_width) < 1 )
165  cerr << "-edge: An edge width must be specified." << endl;
166  if ( curropt->tag == "fill" )
167  fill = curropt->fill(fill_type);
168  if ( curropt->tag == "bin" ) {
169  if ( ( i = curropt->values(bin[0], bin[1], bin[2]) ) < 1 )
170  cerr << "-bin: At least one bin size must be specified!" << endl;
171  else
172  if ( i < 2 ) bin[2] = bin[1] = bin[0];
173  }
174  if ( curropt->tag == "partbase" ) {
175  partbase = curropt->value;
176  if ( partbase.length() < 1 )
177  cerr << "-partbase: The particle base file name must be specified!" << endl;
178  }
179  if ( curropt->tag == "partpath" ) {
180  partpath = curropt->value;
181  if ( partpath.length() < 1 )
182  cerr << "-partpath: The particle file path must be specified!" << endl;
183  }
184  if ( curropt->tag == "partextension" ) {
185  part_ext = curropt->value;
186  if ( part_ext.length() < 1 )
187  cerr << "-partextension: The particle file extension must be specified!" << endl;
188  }
189  if ( curropt->tag == "helix" ) {
190  if ( curropt->values(helix_rise, helix_angle) < 2 )
191  cerr << "-helix: Both helical rise and angle per unit must be specified." << endl;
192  else
193  helix_angle *= M_PI/180.0;
194  }
195  if ( curropt->tag == "subset" )
196  subset = curropt->value;
197  if ( curropt->tag == "Gainreference" )
198  grfile = curropt->filename();
199  if ( curropt->tag == "Mask" )
200  maskfile = curropt->filename();
201  if ( curropt->tag == "output" )
202  paramfile = curropt->filename();
203  if ( curropt->tag == "write" ) {
204  if ( curropt->value[0] == 'f' ) flags |= 4;
205  if ( curropt->value[0] == 's' ) flags |= 8;
206  if ( curropt->value[0] == 'a' ) flags |= 8;
207  }
208  if ( curropt->tag == "image" )
209  imgfile = curropt->filename();
210  if ( curropt->tag == "average" )
211  avgfile = curropt->filename();
212  }
213  option_kill(option);
214 
215  double ti = timer_start();
216 
217  // Read all the parameter files
218  Bstring* file_list = NULL;
219  while ( optind < argc ) string_add(&file_list, argv[optind++]);
220  if ( !file_list ) {
221  cerr << "Error: No parameter or image files specified!" << endl;
222  bexit(-1);
223  }
224 
225  Bproject* project = read_project(file_list);
226  string_kill(file_list);
227 
228  if ( !project ) {
229  cerr << "Error: Input file not read!" << endl;
230  bexit(-1);
231  }
232 
233 
234  if ( sam[0] > 0 )
236 
237  Bimage* pgr = NULL;
238  if ( grfile.length() )
239  pgr = read_img(grfile, 1, 0);
240 
241  Bimage* pmask = NULL;
242  if ( maskfile.length() )
243  pmask = read_img(maskfile, 1, 0);
244 
245  if ( ref_img > -1 ) {
246  if ( frames )
247  project_align_frames(project, ref_img, pgr, pmask, origin, hi_res, lo_res,
248  shift_limit, edge_width, gauss_width, bin, dose_frac, subset, flags);
249  else
250  project_align_series(project, ref_img, pgr, pmask, origin, hi_res, lo_res,
251  shift_limit, edge_width, gauss_width, bin, subset, flags);
252  delete pgr;
253  pgr = NULL;
254  }
255 
256  if ( !frames && imgfile.length() )
257  project_write_aligned_images(project, pgr, imgfile, datatype);
258 
259 // project_ssnr(project);
260 
261  if ( partpath.length() )
263 
264  if ( box_size[0] > 0 )
266 
267  if ( boxing_interval > 0 )
268  project_filaments_to_particles(project, box_size, boxing_interval, helix_rise, helix_angle);
269 
270  if ( box_size[0] > 0 )
271  project_extract_particles(project, 1, back_flag, norm_flag,
272  fill_type, fill, mask_width, 0, partbase, partpath, part_ext);
273 
274  if ( avgfile.length() )
275  project_write_aligned_averages(project, pgr, avgfile, datatype, subset);
276 
277  if ( paramfile.length() )
278  write_project(paramfile, project, 0, 0);
279 
281  delete pgr;
282  delete pmask;
283 
284  if ( verbose & VERB_TIME )
285  timer_report(ti);
286 
287  bexit(0);
288 }
289 
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
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
Bstring
Definition: Bstring.h:29
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::datatype
DataType datatype()
Definition: options.h:93
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
Bstring::real
double real()
Definition: Bstring.cpp:553
options.h
Header file for option handlers.
Boption::origin
Vector3< double > origin()
Definition: options.h:180
main
int main(int argc, char **argv)
Definition: bseries.cpp:75
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_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_write_aligned_averages
double project_write_aligned_averages(Bproject *project, Bimage *pgr, Bstring &imgfile, DataType datatype, Bstring &subset)
Calculates aligned micrograph images and write them to a file.
Definition: mg_align.cpp:1024
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
verbose
int verbose
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
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
project_write_aligned_images
double project_write_aligned_images(Bproject *project, Bimage *pgr, Bstring &imgfile, DataType datatype)
Calculates aligned micrograph images and write them to a file.
Definition: mg_align.cpp:952
project_align_series
double project_align_series(Bproject *project, int ref_img, Bimage *pgr, Bimage *pmask, Vector3< double > origin, double hi_res, double lo_res, double shift_limit, double edge_width, double gauss_width, Vector3< long > bin, Bstring &subset, int flag)
Aligns a series of micrographs by cross-correlation.
Definition: mg_align.cpp:1446
JSvalue
Definition: json.h:53
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
project_align_frames
double project_align_frames(Bproject *project, int ref_img, Bimage *pgr, Bimage *pmask, Vector3< double > origin, double hi_res, double lo_res, double shift_limit, double edge_width, double gauss_width, Vector3< long > bin, JSvalue &dose_frac, Bstring &subset, int flag)
Aligns a series of micrographs by cross-correlation.
Definition: mg_align.cpp:1220
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_filaments_to_particles
int project_filaments_to_particles(Bproject *project, Vector3< long > box_size, double boxing_interval, double rise, double angle)
Converts filaments to sets of particles.
Definition: mg_extract.cpp:1006
Boption::fill
double fill(int &fill_type)
Definition: options.h:138
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
use
const char * use[]
Definition: bseries.cpp:21
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
JSobject
@ JSobject
Definition: json.h:26
mg_processing.h
Header file for micrograph processing.
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
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:178
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
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
mg_align.h
Header file for functions to align micrographs or coordinates from micrographs and apply the resultan...
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:713