Bsoft  2.1.0
Bernard's software package
bbackproj.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "mg_processing.h"
11 #include "mg_reconstruct.h"
12 #include "rwmg.h"
13 #include "symmetry.h"
14 #include "utilities.h"
15 #include "options.h"
16 #include "timer.h"
17 
18 // Declaration of global variables
19 extern int verbose; // Level of output to the screen
20 
21 // Usage assistance
22 const char* use[] = {
23 " ",
24 "Usage: bbackproj [options] input.star [input.star]",
25 "--------------------------------------------------",
26 "Reconstructs 3D maps from single particles by backprojection.",
27 " ",
28 "Actions:",
29 "-rescale -0.1,5.2 Rescale data to average and standard deviation.",
30 "-edge Smooth map with an oval edge.",
31 " ",
32 "Parameters for configuration:",
33 "-classes 1,3-5 Class selection: can be all, selected, or any combination of numbers (default selected).",
34 "-fullmap Output one full map per class (default if -halfmaps not used).",
35 "-halfmaps Output 2 maps from half sets.",
36 "-threads 2 Threads per class (default 1, must be even for -halfmaps).",
37 " ",
38 "Parameters:",
39 "-verbose 7 Verbosity of output.",
40 "-datatype u Force writing of a new data type.",
41 "-size 120,120,80 Output 3D volume size (default from input file).",
42 "-sampling 2,3.5,1 Sampling (angstrom/voxel, a single value sets all three).",
43 "-scale 1.2 Scale or magnification of reconstruction compared to original images (default 1).",
44 "-resolution 3.4 Resolution (angstrom).",
45 "-symmetry D6 Symmetry: Point group identifier.",
46 " ",
47 "Output:",
48 "-reconstruction file.map Output 3D reconstruction.",
49 "-output file.star Output parameter file name.",
50 " ",
51 NULL
52 };
53 
54 int main(int argc, char **argv)
55 {
56  // Initialize variables
57  DataType nudatatype(Unknown_Type); // Conversion to new type
58  Vector3<long> map_size; // New 3D volume size
59  Vector3<double> sam; // Sampling
60  double scale(1); // Scale or magnification of reconstruction compared to original images
61  double resolution(0); // Undefined resolution
62  Bsymmetry* sym = NULL; // No symmetry specified
63  double nuavg, nustd(0); // Values for rescaling
64  int smooth_oval(0); // Flag for oval smoothing
65  Bstring classes("select"); // Class selection string
66  long nmaps(0); // Number of maps per class
67  long nthreads(1); // Number of threads per map
68  long imap(0); // Select one of multiple maps to reconstruct
69  Bstring outfile; // Output parameter file
70  Bstring reconsfile; // Output reconstruction file
71 
72  int optind;
73  Boption* option = get_option_list(use, argc, argv, optind);
74  Boption* curropt;
75  for ( curropt = option; curropt; curropt = curropt->next ) {
76  if ( curropt->tag == "datatype" )
77  nudatatype = curropt->datatype();
78  if ( curropt->tag == "size" )
79  map_size = curropt->size();
80  if ( curropt->tag == "scale" )
81  if ( ( scale = curropt->value.real() ) < 0.0001 )
82  cerr << "-scale: A scale must be specified!" << endl;
83  if ( curropt->tag == "resolution" )
84  if ( ( resolution = curropt->value.real() ) < 0.001 )
85  cerr << "-resolution: Resolution must be specified" << endl;
86  if ( curropt->tag == "sampling" )
87  sam = curropt->scale();
88  if ( curropt->tag == "symmetry" )
89  sym = curropt->symmetry();
90  if ( curropt->tag == "rescale" ) {
91  if ( curropt->values(nuavg, nustd) < 2 )
92  cerr << "-rescale: Both average and standard deviation must be specified!" << endl;
93  if ( nustd <= 0 )
94  cerr << "-rescale: A positive standard deviation must be specified!" << endl;
95  }
96  if ( curropt->tag == "edge" )
97  smooth_oval = 1;
98  if ( curropt->tag == "classes" )
99  classes = curropt->value;
100  if ( curropt->tag == "fullmap" ) nmaps += 1;
101  if ( curropt->tag == "halfmaps" ) nmaps += 2;
102  if ( curropt->tag == "threads" )
103  if ( ( nthreads = curropt->value.integer() ) < 1 )
104  cerr << "-threads: A number of threads must be specified!" << endl;
105  if ( curropt->tag == "reconstruction" )
106  reconsfile = curropt->filename();
107  if ( curropt->tag == "output" )
108  outfile = curropt->filename();
109  }
110  option_kill(option);
111 
112  double ti = timer_start();
113 
114  if ( nmaps < 1 ) nmaps = 1;
115  if ( !nmaps%2 ) nmaps = 2;
116  if ( nmaps > 3 ) nmaps = 3;
117  if ( nmaps & 2 && nthreads%2 ) nthreads++;
118 
119  // Read all the parameter files
120  Bstring* file_list = NULL;
121  while ( optind < argc ) string_add(&file_list, argv[optind++]);
122  if ( !file_list ) {
123  cerr << "Error: No parameter files specified!" << endl;
124  bexit(-1);
125  }
126 
127  Bproject* project = read_project(file_list);
128  string_kill(file_list);
129 
130  if ( project == NULL ) {
131  cerr << "Error: No input file read!" << endl;
132  bexit(-1);
133  }
134 
135  if ( sam[0] > 0 ) {
136  if ( sam[0] < 0.1 ) sam = Vector3<double>(1,1,1);
138  }
139 
140  if ( verbose ) {
141  cout << "3D backprojection:" << endl;
142  }
143 
144  long nclasses(1);
145 
146  nclasses = project_configure_for_reconstruction(project, classes, nmaps, nthreads);
147 
148  long ntotal = nclasses*nthreads;
149 
150  long i;
151  Bimage** pacc = new Bimage*[ntotal];
152  Bimage** prec = new Bimage*[nclasses*nmaps];
153 // View ref_view;
154  Vector3<double> start;
155  Bstring filename = reconsfile;
156  Bstring id;
157  Breconstruction* rec = NULL;
158 
159  Vector3<long> ms;
160  if ( map_size.volume() <= 0 ) {
162  map_size = {ms[0], ms[1], ms[2]};
163  }
164 
165  if ( map_size.volume() <= 0 ) {
166  error_show("Error in bbackproj", __FILE__, __LINE__);
167  cerr << "No particles to determine the size from!" << endl << endl;
168  return -1;
169  }
170 
171  if ( verbose ) {
172  cout << "Map size: " << map_size << endl;
173  cout << "Resolution limit: " << resolution << " A" << endl;
174  cout << "Scale: " << scale << endl;
175  cout << endl;
176  }
177 
178  fft_plan planf = fft_setup_plan(map_size[0], map_size[1], 1, FFTW_FORWARD, 1);
179  fft_plan planb = fft_setup_plan(map_size[0], map_size[1], 1, FFTW_BACKWARD, 1);
180 
181  // Do the backprojection, weighing each image
182 #ifdef HAVE_GCD
183  dispatch_apply(ntotal, dispatch_get_global_queue(0, 0), ^(size_t i){
184  pacc[i] = project_back_projection(project, i+1, map_size, sam,
185  scale, resolution, planf, planb);
186  });
187 #else
188 #pragma omp parallel for
189  for ( i=0; i<ntotal; i++ )
190  pacc[i] = project_back_projection(project, i+1, map_size, sam,
191  scale, resolution, planf, planb);
192 #endif
193 
196 
197  if ( verbose )
198  cout << "Combining " << nclasses*nmaps << " reconstructions" << endl;
199 
200  // Accumulate partial reconstructions
201 #ifdef HAVE_GCD
202  dispatch_apply(nclasses*nmaps, dispatch_get_global_queue(0, 0), ^(size_t i){
203  prec[i] = img_backprojection_accumulate(pacc, i, nmaps, nthreads);
204  if ( sym ) prec[i]->symmetrize(sym, 1);
205  if ( smooth_oval ) prec[i]->edge(1, map_size, start, 5, FILL_USER, 0);
206  if ( nustd > 0 ) prec[i]->rescale_to_avg_std(nuavg, nustd);
207  prec[i]->change_type(nudatatype);
208  });
209 #else
210 #pragma omp parallel for
211  for ( i=0; i<nclasses*nmaps; i++ ) {
212  prec[i] = img_backprojection_accumulate(pacc, i, nmaps, nthreads);
213  if ( sym ) prec[i]->symmetrize(sym, 1);
214  if ( smooth_oval ) prec[i]->edge(1, map_size, start, 5, FILL_USER, 0);
215  if ( nustd > 0 ) prec[i]->rescale_to_avg_std(nuavg, nustd);
216  prec[i]->change_type(nudatatype);
217  }
218 #endif
219 
220  for ( i=0; i<ntotal; i++ ) delete pacc[i];
221  delete[] pacc;
222 
223  // Write an output reconstruction if an output filename is given
224  for ( i=0; i<nclasses*nmaps; i++ ) {
225  imap = 0;
226  if ( nmaps == 2 ) imap = i%2 + 1;
227  else if ( nmaps == 3 ) imap = i%3;
228  if ( prec[i] && reconsfile.length() ) {
229  if ( sym ) prec[i]->symmetry(sym->label.str());
230  id = reconsfile.pre_rev('.');
231  if ( imap ) id += Bstring(imap, "_%02d");
232  filename = reconsfile;
233  if (imap ) filename = reconsfile.pre_rev('.') + Bstring(imap, "_%02d.") + reconsfile.post_rev('.');
234  write_img(filename, prec[i], 0);
235  rec = reconstruction_add(&project->rec, id);
236  rec->frec = filename;
237  rec->voxel_size = project->field->mg->pixel_size/scale;
238  rec->origin = prec[i]->image->origin();
239  if ( sym ) rec->symmetry = sym->label;
240  }
241  delete prec[i];
242  }
243 
244  if ( project && outfile.length() ) {
245  write_project(outfile, project, 0, 0);
246  }
247 
248  delete[] prec;
250  if ( sym ) kill_symmetry(sym);
251 
252  if ( verbose & VERB_TIME )
253  timer_report(ti);
254 
255  bexit(0);
256 }
257 
Boption
A class for a command line input option.
Definition: options.h:26
mg_reconstruct.h
Functions for reconstruction.
project_kill
int project_kill(Bproject *project)
Frees a whole project.
Definition: mg_processing.cpp:429
Bimage::symmetry
string & symmetry()
Definition: Bimage.h:603
Bimage::symmetrize
double symmetrize(Bstring &symmetry_string, int flag)
Definition: Bimage.h:1188
Boption::scale
Vector3< double > scale()
Definition: options.h:190
use
const char * use[]
Definition: bbackproj.cpp:22
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: bbackproj.cpp:54
Bstring
Definition: Bstring.h:29
Breconstruction::voxel_size
Vector3< double > voxel_size
Definition: mg_processing.h:295
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:436
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
symmetry.h
Header file for general symmetry functions.
Boption::next
Boption * next
Definition: options.h:28
Vector3< long >
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
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:719
FILL_USER
#define FILL_USER
Definition: utilities.h:176
Bstring::real
double real()
Definition: Bstring.cpp:553
options.h
Header file for option handlers.
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
Bsymmetry
Definition: symmetry.h:38
planf
fft_plan planf
Definition: bico.cpp:24
Boption::symmetry
Bsymmetry * symmetry()
Definition: options.h:240
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
project_configure_for_reconstruction
int project_configure_for_reconstruction(Bproject *project, Bstring classes, long &nmaps, long &nthreads)
Sets up the particle selection for reconstruction.
Definition: mg_reconstruct.cpp:723
Bstring::str
string str()
Definition: Bstring.h:42
fft_destroy_plan
int fft_destroy_plan(fft_plan plan)
Deallocates a plan for fast Fourier transforms.
Definition: fft.cpp:87
Boption::value
Bstring value
Definition: options.h:30
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:421
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
project_back_projection
Bimage * project_back_projection(Bproject *project, long num_select, Vector3< long > map_size, Vector3< double > sam, double scale, double resolution, fft_plan planf, fft_plan planb)
Back projects a set of 2D images into a 3D volume.
Definition: mg_reconstruct.cpp:1568
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::symmetry
Bstring symmetry
Definition: mg_processing.h:289
Boption::size
Vector3< long > size()
Definition: options.h:163
Breconstruction
Definition: mg_processing.h:283
fft_setup_plan
fft_plan fft_setup_plan(long x, long y, long z, fft_direction dir, int opt)
Sets up a plan for fast Fourier transforms.
Definition: fft.cpp:31
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
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
Vector3::volume
double volume()
Definition: Vector3.h:237
rwimg.h
Header file for 2D and 3D image I/O.
write_project
int write_project(const char *filename, Bproject *project)
Writing micrograph parameters to a file.
Definition: rwmg.cpp:321
mg_processing.h
Header file for micrograph processing.
fft_plan
fftwf_plan fft_plan
Definition: fft.h:20
kill_symmetry
int kill_symmetry(Bsymmetry *sym)
Deallocates a symmetry structure.
Definition: symmetry.cpp:207
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
Breconstruction::frec
Bstring frec
Definition: mg_processing.h:290
verbose
int verbose
Bmicrograph::pixel_size
Vector3< double > pixel_size
Definition: mg_processing.h:215
img_backprojection_accumulate
Bimage * img_backprojection_accumulate(Bimage **pacc, int imap, int nmaps, int nthreads)
Accumulates multiple backprojections.
Definition: mg_reconstruct.cpp:1683
planb
fft_plan planb
Definition: bico.cpp:24
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bsymmetry::label
Bstring label
Definition: symmetry.h:41
reconstruction_add
Breconstruction * reconstruction_add(Breconstruction **rec, Bstring &rec_id)
Adds a reconstruction parameter structure to a linked list.
Definition: mg_processing.cpp:398
Bimage::edge
int edge(int type, Vector3< long > rect, Vector3< double > start, double width, int fill_type=0, double fill=0)
Smooths the image edge with a soft rectangular function.
Definition: Bimage_edit.cpp:321
project_set_reconstruction_size
Vector3< long > project_set_reconstruction_size(Bproject *project, double scale, int twoD_flag)
Sets the reconstruction size for reconstruction from 2D particle images.
Definition: mg_reconstruct.cpp:658
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
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