Bsoft  2.1.0
Bernard's software package
brecadd.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "rwmg.h"
11 #include "mg_reconstruct.h"
12 #include "mg_processing.h"
13 #include "mg_particle_select.h"
14 #include "symmetry.h"
15 #include "symmetry.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: brecadd [options] input.star [input.star]",
27 "------------------------------------------------",
28 "Averages reconstructions or subvolumes of reconstructions based on",
29 "the selection in parameter files.",
30 " ",
31 "Actions:",
32 "-all Reset selection to all particles before other selections.",
33 "-group 3022 Select particles belonging to this group.",
34 "-setnumber 3 Select given selection number and set others to zero.",
35 "-multiple 2 Generate a number of maps from alternating particle images (default 1).",
36 "-size 50,50,70 Specify a size to extract new particles from a map.",
37 "-resolution 15 Specify a resolution to do a reciprocal space summation.",
38 "-std Output standard deviation image rather than variance image.",
39 "-symmetry C5 Apply point group symmetry after reconstruction.",
40 "-rescale -0.1,5.2 Rescale output images to average and standard deviation.",
41 " ",
42 "Parameters:",
43 "-verbose 7 Verbosity of output.",
44 "-datatype u Force writing of a new data type.",
45 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; default from input file; a single value can be given).",
46 " ",
47 "Output:",
48 "-output file.star Output parameter file.",
49 "-average output.img Output image (default avg.map).",
50 "-fom fom.img Output variance/standard deviation image (default fom.map).",
51 " ",
52 NULL
53 };
54 
55 int main(int argc, char **argv)
56 {
57  // Initialize variables
58  int reset(0); // Flag to reset selection
59  int group(0); // Group to select
60  int set_number(-1); // Selection number to keep
61  int nmaps(1); // Number of maps to reconstruct
62  DataType nudatatype(Unknown_Type); // Conversion to new type
63  double nuavg(0), nustd(0); // Average and standard deviation for rescaling
64  int calcfom(0); // Flag to calculate variance=1, or std=2
65  Vector3<double> sam; // Units for the three axes (A/pixel)
66  Vector3<long> size; // Size to extract new particles
67  double resolution(0); // Resolution for frequency space reconstruction
68  Bsymmetry* sym = NULL; // Point group
69  Bstring outfile; // Output parameter file name
70  Bstring avgfile("avg.map"); // Output image file name
71  Bstring fomfile("fom.map"); // Output FOM file
72 
73  int optind;
74  Boption* option = get_option_list(use, argc, argv, optind);
75  Boption* curropt;
76  for ( curropt = option; curropt; curropt = curropt->next ) {
77  if ( curropt->tag == "all" ) reset = 1;
78  if ( curropt->tag == "group" )
79  if ( ( group = curropt->value.integer() ) < 1 )
80  cerr << "-group: A group must be specified!" << endl;
81  if ( curropt->tag == "setnumber" )
82  if ( ( set_number = curropt->value.integer() ) < 1 )
83  cerr << "-setnumber: A selection number must be specified!" << endl;
84  if ( curropt->tag == "multiple" )
85  if ( ( nmaps = curropt->value.integer() ) < 1 )
86  cerr << "-multiple: The number of maps must be specified!" << endl;
87  if ( curropt->tag == "datatype" )
88  nudatatype = curropt->datatype();
89  if ( curropt->tag == "rescale" )
90  if ( curropt->values(nuavg, nustd) < 2 )
91  cerr << "-rescale: Both average and standard deviation must be specified" << endl;
92  if ( curropt->tag == "size" )
93  size = curropt->size();
94  if ( curropt->tag == "resolution" )
95  if ( ( resolution = curropt->value.real() ) < 0.1 )
96  cerr << "-resolution: The resolution must be specified!" << endl;
97  if ( curropt->tag == "std" ) calcfom += 1;
98  if ( curropt->tag == "sampling" )
99  sam = curropt->scale();
100  if ( curropt->tag == "symmetry" )
101  sym = curropt->symmetry();
102  if ( curropt->tag == "average" )
103  avgfile = curropt->filename();
104  if ( curropt->tag == "output" )
105  outfile = curropt->filename();
106  if ( curropt->tag == "fom" ) {
107  fomfile = curropt->filename();
108  calcfom += 1;
109  }
110  }
111  option_kill(option);
112 
113  if ( avgfile.length() < 3 ) {
114  cerr << "Error: No output map file name specified!" << endl;
115  bexit(-1);
116  }
117 
118  double ti = timer_start();
119 
120  // Read all the parameter files
121  Bstring* file_list = NULL;
122  while ( optind < argc ) string_add(&file_list, argv[optind++]);
123  if ( !file_list ) {
124  cerr << "Error: No parameter files specified!" << endl;
125  bexit(-1);
126  }
127 
128  Bproject* project = read_project(file_list);
129  string_kill(file_list);
130 
131  if ( !project ) {
132  cerr << "Error: No input file read!" << endl;
133  bexit(-1);
134  }
135 
136  project->select = 1;
137 
138  if ( reset ) part_reset_selection(project, 3);
139 
140  if ( group > 0 ) part_select_group(project, group);
141 
142  if ( set_number > 0 ) part_set_selection(project, set_number);
143 
144  if ( nmaps > 1 ) part_set_multi_maps(project, set_number, nmaps);
145 
146  int n;
147  Bimage* psum = NULL;
148  Bstring id;
149  Bstring filename;
150  Breconstruction* rec;
151 
152  for ( n=1; n<=nmaps; n++ ) {
153  if ( nmaps > 1 ) set_number = n;
154  if ( resolution > 0 )
155  psum = project_reconstruct_3D(project, set_number, size, resolution);
156  else
157  psum = project_reconstruct_3D(project, set_number, calcfom, size);
158  if ( !psum ) {
159  cerr << "Error: No average calculated!" << endl;
160  bexit(-1);
161  }
162  if ( sam.volume() > 0 ) psum->sampling(sam);
163  if ( sym && sym->point > 101 ) {
164  if ( verbose )
165  cout << "Applying symmetry " << sym->label << endl;
166  psum->symmetrize(sym, 1);
167  }
168  if ( avgfile.length() ) {
169  if ( nustd > 0 ) psum->rescale_to_avg_std(nuavg, nustd);
170  psum->change_type(nudatatype);
171  id = avgfile.pre_rev('.');
172  if ( nmaps > 1 ) id += Bstring(n, "_%03d");
173  filename = id + "." + avgfile.post_rev('.');
174 // cout << "writing " << filename << endl;
175  write_img(filename, psum, 0);
176  if ( verbose )
177  cout << "Adding " << id << " to the reconstruction list" << endl;
178  rec = reconstruction_add(&project->rec, id);
179  rec->frec = filename;
180  rec->voxel_size = psum->sampling(0);
181  rec->origin = psum->image->origin();
182  if ( sym ) rec->symmetry = sym->label;
183  rec->select = 1;
184  }
185  if ( calcfom && psum->next ) {
186  filename = fomfile;
187  if ( nmaps > 1 ) filename = fomfile.pre_rev('.') + Bstring(n, "_%03d.") + fomfile.post_rev('.');
188  write_img(filename, psum->next, 0);
189  }
190  delete psum;
191  }
192 
193  if ( outfile.length() ) {
194  write_project(outfile, project, 0, 0);
195  }
196 
197  kill_symmetry(sym);
199 
200  if ( verbose & VERB_TIME )
201  timer_report(ti);
202 
203  bexit(0);
204 }
205 
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
mg_particle_select.h
Select particles.
part_set_selection
long part_set_selection(Bproject *project, int number)
Selects one selection number and sets all others to zero.
Definition: mg_particle_select.cpp:317
Bimage::symmetrize
double symmetrize(Bstring &symmetry_string, int flag)
Definition: Bimage.h:1188
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
Bsymmetry::point
int point
Definition: symmetry.h:39
Bproject::select
int select
Definition: mg_processing.h:342
Bstring
Definition: Bstring.h:29
Bimage::next
Bimage * next
Definition: Bimage.h:179
Breconstruction::voxel_size
Vector3< double > voxel_size
Definition: mg_processing.h:295
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:436
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< double >
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
part_select_group
long part_select_group(Bproject *project, int group)
Selects particles with a given group identifier.
Definition: mg_particle_select.cpp:1792
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.
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
Bsymmetry
Definition: symmetry.h:38
part_set_multi_maps
long part_set_multi_maps(Bproject *project, int part_select, int nmaps)
Sets selection for generating multiple maps.
Definition: mg_particle_select.cpp:590
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
part_reset_selection
long part_reset_selection(Bproject *project, int flag=0)
Resets selection to all particles.
Definition: mg_particle_select.cpp:133
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
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
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
verbose
int verbose
Breconstruction
Definition: mg_processing.h:283
Bstring::length
long length() const
Definition: Bstring.cpp:263
Breconstruction::select
long select
Definition: mg_processing.h:286
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.
use
const char * use[]
Definition: brecadd.cpp:24
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.
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
project_reconstruct_3D
Bimage * project_reconstruct_3D(Bproject *project, long selnum, int calcfom, Vector3< long > size)
Transforms 3D maps and calculates an average.
Definition: mg_reconstruct.cpp:1263
Breconstruction::frec
Bstring frec
Definition: mg_processing.h:290
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
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
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:713
main
int main(int argc, char **argv)
Definition: brecadd.cpp:55
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