| Bsoft 2.0.5 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->sampling();
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[0] = 1;
137 project_set_pixel_size(project, sam[0]);
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 ) {
161 ms = project_set_reconstruction_size(project, scale, 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
194 fft_destroy_plan(planf);
195 fft_destroy_plan(planb);
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 prec[i]->label(get_cmd_line(argc, argv));
230 if ( sym ) prec[i]->symmetry(sym->label.str());
231 id = reconsfile.pre_rev('.');
232 if ( imap ) id += Bstring(imap, "_%02d");
233 filename = reconsfile;
234 if (imap ) filename = reconsfile.pre_rev('.') + Bstring(imap, "_%02d.") + reconsfile.post_rev('.');
235 write_img(filename, prec[i], 0);
236 rec = reconstruction_add(&project->rec, id);
237 rec->frec = filename;
238 rec->voxel_size = project->field->mg->pixel_size/scale;
239 rec->origin = prec[i]->image->origin();
240 if ( sym ) rec->symmetry = sym->label;
241 }
242 delete prec[i];
243 }
244
245 if ( project && outfile.length() ) {
246 project_update_comment(project, argc, argv);
247 write_project(outfile, project, 0, 0);
248 }
249
250 delete[] prec;
251 project_kill(project);
252 if ( sym ) kill_symmetry(sym);
253
254 if ( verbose & VERB_TIME )
255 timer_report(ti);
256
257 bexit(0);
258 }
259
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
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_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:628
Bstring::real
double real()
Definition: Bstring.cpp:545
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:705
Bproject::rec
Breconstruction * rec
Definition: mg_processing.h:344
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:1527
DataType
DataType
Base data type specifier.
Definition: utilities.h:146
planb
fft_plan planb
Definition: bico.cpp:24
verbose
int verbose
symmetry.h
Header file for general symmetry functions.
utilities.h
Header file for general utilities.
Boption::sampling
Vector3< double > sampling()
Definition: options.h:202
Boption::value
Bstring value
Definition: options.h:30
Breconstruction
Definition: mg_processing.h:280
Boption::tag
Bstring tag
Definition: options.h:29
planf
fft_plan planf
Definition: bico.cpp:24
main
int main(int argc, char **argv)
Definition: bbackproj.cpp:54
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:319
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
Bmicrograph::pixel_size
double pixel_size
Definition: mg_processing.h:214
project
Bproject * project
Definition: bshow.cpp:31
kill_symmetry
int kill_symmetry(Bsymmetry *sym)
Deallocates a symmetry structure.
Definition: symmetry.cpp:207
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:693
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
Breconstruction::origin
Vector3< double > origin
Definition: mg_processing.h:293
get_cmd_line
string get_cmd_line(int argc, char **argv)
Returns the command line as a string.
Definition: utilities.cpp:36
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
reconstruction_add
Breconstruction * reconstruction_add(Breconstruction **rec, Bstring &rec_id)
Adds a reconstruction parameter structure to a linked list.
Definition: mg_processing.cpp:704
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::label
void label(string s)
Definition: Bimage.h:235
Bproject::field
Bfield * field
Definition: mg_processing.h:343
VERB_TIME
#define VERB_TIME
Definition: utilities.h:110
fft_plan
fftwf_plan fft_plan
Definition: fft.h:16
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
img_backprojection_accumulate
Bimage * img_backprojection_accumulate(Bimage **pacc, int imap, int nmaps, int nthreads)
Accumulates multiple backprojections.
Definition: mg_reconstruct.cpp:1642
Boption::symmetry
Bsymmetry * symmetry()
Definition: options.h:252
Bimage::symmetrize
double symmetrize(Bstring &symmetry_string, int flag)
Definition: Bimage.h:1178
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:280
Breconstruction::symmetry
Bstring symmetry
Definition: mg_processing.h:286
FILL_USER
#define FILL_USER
Definition: utilities.h:172
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:127
Bfield::mg
Bmicrograph * mg
Definition: mg_processing.h:264
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:428
Bsymmetry::label
Bstring label
Definition: symmetry.h:41
Bsymmetry
Definition: symmetry.h:38
use
const char * use[]
Definition: bbackproj.cpp:22
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Bstring
Definition: Bstring.h:29
project_kill
int project_kill(Bproject *project)
Frees a whole project.
Definition: mg_processing.cpp:1224
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
Vector3< long >
Bimage::symmetry
string & symmetry()
Definition: Bimage.h:596
fft_destroy_plan
int fft_destroy_plan(fft_plan plan)
Deallocates a plan for fast Fourier transforms.
Definition: fft.cpp:87
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:67
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
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.
Breconstruction::frec
Bstring frec
Definition: mg_processing.h:287
Breconstruction::voxel_size
double voxel_size
Definition: mg_processing.h:292
Unknown_Type
Definition: utilities.h:147
rwimg.h
Header file for 2D and 3D image I/O.
options.h
Header file for option handlers.
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:413
mg_reconstruct.h
Functions for reconstruction.
Vector3::volume
double volume()
Definition: Vector3.h:235
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
Bstring::str
string str()
Definition: Bstring.h:42
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
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:52
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:755
Bproject
Definition: mg_processing.h:333
Bimage::image
Bsub_image * image
Definition: Bimage.h:188
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:760