Bsoft  2.1.0
Bernard's software package
btomrec.cpp
Go to the documentation of this file.
1 
9 #include "mg_tomo_rec.h"
10 #include "mg_align.h"
11 #include "rwmg.h"
12 #include "utilities.h"
13 #include "options.h"
14 #include "timer.h"
15 
16 // Declaration of global variables
17 extern int verbose; // Level of output to the screen
18 
19 // Usage assistance
20 const char* use[] = {
21 " ",
22 "Usage: btomrec [options] input.star [input2.star]",
23 "-------------------------------------------------",
24 "Reconstructs a tomogram from an aligned tilt series in Fourier space.",
25 "Requires large amounts of disk space.",
26 " ",
27 "Actions:",
28 "-removemarkers 14 Mask out markers with this radius (pixels) before reconstruction.",
29 "-rescale -0.1,5.2 Rescale data to average and standard deviation.",
30 "-slab 25,40 Reconstruct a slab of these slices (default all).",
31 "-transform 2D Back transform reconstruction: none (default), full, slices.",
32 " ",
33 "Parameters:",
34 "-verbose 7 Verbosity of output.",
35 "-datatype u Force writing of a new data type.",
36 "-sampling 1.5,1.5,1.5 Sampling of input images (A/pixel; a single value can be given).",
37 "-resolution 20 Resolution limit (angstrom, default Nyquist).",
38 "-scale 1.2 Scale or magnification of reconstruction compared to original images (default 1).",
39 "-size 100,120,90 Size of reconstruction (default from images).",
40 "-interpolation weighted Interpolation type: nearest (default), weighted, trilinear.",
41 "-pad 3 Image padding factor (default 2).",
42 "-edge 15 An edge smoothing width (default 0).",
43 "-fill 127 Fill value for erasing/painting markers (default average).",
44 "-CTF flip Apply CTF correction to images before reconstruction (default not).",
45 "-wiener 0.15 Wiener factor for CTF correction (default 0.2).",
46 " ",
47 "Output:",
48 "-reconstruction file.ext Reconstruction file name.",
49 "-output file.star Output STAR file name.",
50 " ",
51 NULL
52 };
53 
54 int main(int argc, char **argv)
55 {
56  double marker_radius(0); // Radius to mask out markers
57  DataType nudatatype(Unknown_Type); // Conversion to new type
58  double nuavg(0), nustd(0); // Values for rescaling
59  Vector3<double> sam; // Units for the three axes (A/pixel)
60  Vector3<long> size; // Size of reconstruction
61  int slab_start(0), slab_end(-1);// Slab definition
62  int interp_type(0); // Interpolation type
63  int pad_factor(2); // Image padding factor
64  double edge_width(0); // An edge smoothing width
65  double scale(1); // Scale or magnification of reconstruction compared to original images
66  double resolution(0); // Must be set > 0 to limit resolution
67  int transform(0); // No back transform
68  int fill_type(FILL_AVERAGE); // Type of fill value
69  double fill(0); // Fill value for new areas
70  int ctf_action(0); // Default no CTF operation
71  double wiener(0.2); // Wiener for CTF correction
72  Bstring reconsfile; // Output reconstruction file
73  Bstring outfile; // Output STAR file
74 
75  int optind;
76  Boption* option = get_option_list(use, argc, argv, optind);
77  Boption* curropt;
78  for ( curropt = option; curropt; curropt = curropt->next ) {
79  if ( curropt->tag == "removemarkers" )
80  if ( ( marker_radius = curropt->value.real() ) < 1 )
81  cerr << "-removemarkers: A marker radius must be specified!" << endl;
82  if ( curropt->tag == "rescale" ) {
83  if ( curropt->values(nuavg, nustd) < 2 )
84  cerr << "-rescale: Both average and standard deviation must be specified!" << endl;
85  if ( nustd <= 0 )
86  cerr << "-rescale: A positive standard deviation must be specified!" << endl;
87  }
88  if ( curropt->tag == "slab" )
89  if ( curropt->values(slab_start, slab_end) < 1 )
90  cerr << "-slab: A slab size must be specified!" << endl;
91  if ( curropt->tag == "transform" ) {
92  if ( curropt->value[0] == 'n' ) transform = 0;
93  if ( curropt->value[0] == 'f' ) transform = 1;
94  if ( curropt->value[0] == 's' ) transform = 2;
95  }
96  if ( curropt->tag == "datatype" )
97  nudatatype = curropt->datatype();
98  if ( curropt->tag == "sampling" )
99  sam = curropt->scale();
100  if ( curropt->tag == "resolution" )
101  if ( ( resolution = curropt->value.real() ) < 0.1 )
102  cerr << "-resolution: A resolution limit must be specified!" << endl;
103  if ( curropt->tag == "size" )
104  size = curropt->size();
105  if ( curropt->tag == "scale" )
106  if ( ( scale = curropt->value.real() ) < 0.001 )
107  cerr << "-scale: A scale must be specified!" << endl;
108  if ( curropt->tag == "interpolation" ) {
109  interp_type = curropt->value.integer();
110  if ( curropt->value[0] == 'n' ) interp_type = 0;
111  if ( curropt->value[0] == 'w' ) interp_type = 1;
112  if ( curropt->value[0] == 't' ) interp_type = 2;
113  }
114  if ( curropt->tag == "pad" )
115  if ( ( pad_factor = curropt->value.integer() ) < 0 )
116  cerr << "-pad: An integer factor must be specified!" << endl;
117  if ( curropt->tag == "edge" )
118  if ( ( edge_width = curropt->value.real() ) < 0 )
119  cerr << "-edge: An edge smoothing width must be specified!" << endl;
120  if ( curropt->tag == "fill" )
121  fill = curropt->fill(fill_type);
122  if ( curropt->tag == "CTF" )
123  ctf_action = curropt->ctf_action();
124  if ( curropt->tag == "wiener" ) {
125  if ( ( wiener = curropt->value.real() ) < 0.000001 )
126  cerr << "-wiener: A Wiener factor must be specified!" << endl;
127  else {
128  if ( wiener < 0.01 ) wiener = 0.01;
129 // if ( wiener > 1 ) wiener = 1;
130  }
131  }
132  if ( curropt->tag == "reconstruction" )
133  reconsfile = curropt->filename();
134  if ( curropt->tag == "output" )
135  outfile = curropt->filename();
136  }
137  option_kill(option);
138 
139  double ti = timer_start();
140 
141 #ifdef HAVE_GCD
142  fftwf_init_threads();
143  fftwf_plan_with_nthreads(system_processors());
144 #endif
145  if ( verbose )
146  cout << "Number of threads: " << system_processors() << endl;
147 
148 
149  // Read all the parameter files
150  Bstring* file_list = NULL;
151  while ( optind < argc ) string_add(&file_list, argv[optind++]);
152  if ( !file_list ) {
153  cerr << "Error: No parameter files specified!" << endl;
154  bexit(-1);
155  }
156 
157  Bproject* project = read_project(file_list);
158  string_kill(file_list);
159 
160  if ( project == NULL ) {
161  cerr << "Error: No input file read!" << endl;
162  bexit(-1);
163  }
164 
165  if ( sam[0] > 0 ) {
166  if ( sam[0] < 0.1 ) sam[0] = 1;
168  }
169 
170  Bimage* prec = NULL;
171 
172  if ( size[2] < 2 ) {
173  prec = mg_tomo_reconstruct2D(project, 0, size, scale, resolution);
174  } else if ( slab_end > 0 ) {
175  prec = project_fourier_reconstruction_slab(project, resolution,
176  scale, size, slab_start, slab_end, marker_radius,
177  fill_type, fill, ctf_action, wiener);
178  } else {
179  prec = project_tomo_reconstruct(project, resolution,
180  scale, size, interp_type, pad_factor,
181  edge_width, marker_radius,
182  fill_type, fill, ctf_action, wiener);
183  prec->change_transform_size(size);
184  }
185 
186  Bstring id("1");
188  Vector3<double> ori(mg->origin);
189  Vector3<double> trans(prec->image->origin() - ori*scale);
190  if ( verbose )
191  cout << "Translating markers: " << trans << endl;
192 
193  if ( transform == 1 ) {
194  if ( verbose )
195  cout << "Full Fourier transform" << endl;
196  prec->fft_back();
197  } else if ( transform == 2 ) {
198  if ( verbose )
199  cout << "Fourier transforming slices" << endl;
201  }
202 
203  if ( prec && reconsfile.length() ) {
204  if ( nustd > 0 ) prec->rescale_to_avg_std(nuavg, nustd);
205  prec->change_type(nudatatype);
206  if ( verbose )
207  cout << "Writing " << reconsfile << endl;
208  write_img(reconsfile, prec, 0);
209  }
210 
211  if ( project && outfile.length() ) {
212  if ( !project->rec )
214  project->rec->frec = reconsfile;
217  project->rec->origin = prec->image->origin();
218  project->rec->scale = Vector3<double>(scale, scale, scale);
219  project->rec->box_size = project->field->mg->box_size * scale;
223  project->rec->sf_radius = project->field->mg->sf_radius * scale;
225 // marker_transform(project->rec->mark, tf);
227  marker_shift(project->rec->mark, trans);
228  if ( verbose )
229  cout << "Writing " << outfile << endl;
230  write_project(outfile, project, 0, 0);
231  }
232 
233  delete prec;
235 
236 #ifdef HAVE_GCD
237  fftwf_cleanup_threads();
238 #endif
239 
240  if ( verbose & VERB_TIME )
241  timer_report(ti);
242 
243  bexit(0);
244 }
245 
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
Bmicrograph
Definition: mg_processing.h:201
Breconstruction::mark
Bmarker * mark
Definition: mg_processing.h:310
project_tomo_reconstruct
Bimage * project_tomo_reconstruct(Bproject *project, double hi_res, double scale, Vector3< long > size, int interp_type, int pad_factor, double edge_width, double marker_radius, int fill_type, double fill, int action, double wiener)
Reciprocal space reconstruction from the images in a multi-image file.
Definition: mg_tomo_rec.cpp:124
Bmicrograph::filament_width
double filament_width
Definition: mg_processing.h:234
Boption::scale
Vector3< double > scale()
Definition: options.h:190
Bproject::rec
Breconstruction * rec
Definition: mg_processing.h:347
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:768
main
int main(int argc, char **argv)
Definition: btomrec.cpp:54
system_processors
size_t system_processors()
Returns the number of processors.
Definition: utilities.cpp:193
Bstring
Definition: Bstring.h:29
Breconstruction::voxel_size
Vector3< double > voxel_size
Definition: mg_processing.h:295
Bproject::field
Bfield * field
Definition: mg_processing.h:346
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::datatype
DataType datatype()
Definition: options.h:93
Breconstruction::filament_width
double filament_width
Definition: mg_processing.h:300
Bmicrograph::bad_radius
double bad_radius
Definition: mg_processing.h:236
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
Breconstruction::sf_radius
double sf_radius
Definition: mg_processing.h:303
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
marker_scale
int marker_scale(Bmarker *markers, Vector3< double > scale)
Scaling markers.
Definition: marker.cpp:258
Breconstruction::box_size
Vector3< long > box_size
Definition: mg_processing.h:299
Bstring::real
double real()
Definition: Bstring.cpp:553
FILL_AVERAGE
#define FILL_AVERAGE
Definition: utilities.h:177
options.h
Header file for option handlers.
project_fourier_reconstruction_slab
Bimage * project_fourier_reconstruction_slab(Bproject *project, double hi_res, double scale, Vector3< long > size, int slab_start, int slab_end, double marker_radius, int fill_type, double fill, int action, double wiener)
Reciprocal space reconstruction from the images in a multi-image file.
Definition: mg_tomo_rec.cpp:323
Bimage::fft_back
int fft_back()
Definition: Bimage.h:831
Breconstruction::mark_radius
double mark_radius
Definition: mg_processing.h:304
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
Breconstruction::origin
Vector3< double > origin
Definition: mg_processing.h:296
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
rwmg.h
Header file for reading and writing micrograph parameters.
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
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
Bimage::change_transform_size
Vector3< double > change_transform_size(Vector3< long > nusize)
Resizes a "standard" transform.
Definition: Bimage_fft.cpp:239
Bmicrograph::origin
Vector3< double > origin
Definition: mg_processing.h:223
Boption::ctf_action
int ctf_action()
Definition: options.h:291
Boption::value
Bstring value
Definition: options.h:30
marker_shift
int marker_shift(Bmarker *markers, Vector3< double > shift)
Shifting markers.
Definition: marker.cpp:242
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
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
verbose
int verbose
Bmicrograph::mark_radius
double mark_radius
Definition: mg_processing.h:238
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
utilities.h
Header file for general utilities.
Breconstruction::fil_node_radius
double fil_node_radius
Definition: mg_processing.h:301
Boption::size
Vector3< long > size()
Definition: options.h:163
Bmicrograph::box_size
Vector3< long > box_size
Definition: mg_processing.h:233
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
field_find_zero_tilt_mg
Bmicrograph * field_find_zero_tilt_mg(Bfield *field)
Finds the micrograph closest to a zero degree tilt in a series.
Definition: mg_select.cpp:281
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
mg_tomo_rec.h
Functions to do a tomographic reconstruction.
write_project
int write_project(const char *filename, Bproject *project)
Writing micrograph parameters to a file.
Definition: rwmg.cpp:321
Breconstruction::bad_radius
double bad_radius
Definition: mg_processing.h:302
Boption::filename
Bstring filename()
Definition: options.h:85
Breconstruction::scale
Vector3< double > scale
Definition: mg_processing.h:297
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
Breconstruction::frec
Bstring frec
Definition: mg_processing.h:290
Bmicrograph::pixel_size
Vector3< double > pixel_size
Definition: mg_processing.h:215
Bmicrograph::fil_node_radius
double fil_node_radius
Definition: mg_processing.h:235
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
reconstruction_add
Breconstruction * reconstruction_add(Breconstruction **rec, Bstring &rec_id)
Adds a reconstruction parameter structure to a linked list.
Definition: mg_processing.cpp:398
mg_align.h
Header file for functions to align micrographs or coordinates from micrographs and apply the resultan...
use
const char * use[]
Definition: btomrec.cpp:20
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:713
Bfield::mg
Bmicrograph * mg
Definition: mg_processing.h:267
Bmicrograph::sf_radius
double sf_radius
Definition: mg_processing.h:237
img_backtransform_slices
int img_backtransform_slices(Bimage *p)
Backtransforms 2D slices in a 3D volume.
Definition: mg_tomo_rec.cpp:758
mg_tomo_reconstruct2D
Bimage * mg_tomo_reconstruct2D(Bproject *project, long dimg, Vector3< long > size, double scale, double hi_res)
Reconstructs the zero-tilt image of a tilt-series.
Definition: mg_align.cpp:1609
Bimage::rescale_to_avg_std
int rescale_to_avg_std(double nuavg, double nustd)
Rescales the image data to a given average and standard deviation.
Definition: Bimage_rescale.cpp:163