Bsoft  2.1.0
Bernard's software package
bsim.cpp
Go to the documentation of this file.
1 
9 #include "mg_processing.h"
10 #include "mg_multislice.h"
11 #include "rwmg.h"
12 #include "rwmolecule.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: bsim [options] in.star [in2.star]",
24 "----------------------------------------",
25 "Simulates TEM images from atomic coordinates.",
26 "The program has different entry and exit points:",
27 "A new STAR file can be generated (use -generate or -symmetry option and no input STAR file)",
28 " or parameters can be read in from existing STAR files.",
29 "The provision of a coordinate file leads to calculation of potential slices.",
30 "Input of STAR file(s) without a coordinate file implies that the potentials",
31 " have already been calculated and will be used for multislice simulation.",
32 " ",
33 "Actions:",
34 "-generate 10,3,50 Generate a project, specifying number of fields, number of",
35 " micrographs per field and number particles per micrograph.",
36 "-symmetry C5 Generates images based on a grid in the asymmetric unit.",
37 "-coordinates file.pdb Input coordinate file, generate simulated images in project.",
38 "-image Generate a final image and parameter file (default not).",
39 "-poisson Add poisson noise (default not).",
40 "-gaussian 0.2 Add gaussian noise with the given signal-to-noise ratio.",
41 "-MTF 17.5 Set the MTF decay constant (required to impose MTF).",
42 " ",
43 "Parameters:",
44 "-verbose 7 Verbosity of output.",
45 " ",
46 "Parameters for generation:",
47 "-translate 2.4 Standard deviation for random translation (default 0).",
48 "-origin 120 Set the particle image origin (default 50 pixels).",
49 "-angles 8,6 Step sizes for theta and phi in the asymmetric unit, one value sets both.",
50 " Use with the -symmetry option (default 10,10).",
51 "-Pixelsize 5.5 Set the particle pixel size (default 1 angstrom/pixel).",
52 "-electrondose 35.5 Set the electron dose (default 20 electrons/angstrom).",
53 "-Cs 2.0 Set the spherical aberration, Cs (default 2 mm).",
54 "-Cc 2.0 Set the chromatic aberration, Cc (default 2.0 mm).",
55 "-alpha 0.15 Set the illumination half-angle (default 0.1 milliradians).",
56 "-energyspread 2e-5 Set the effective energy spread (default 1e-5).",
57 "-Volt 100 Set the acceleration voltage (default 120 kV).",
58 "-Amplitude 0.07 Set the amplitude contrast (fraction, default 0.07).",
59 "-Defocus 0.5,3.4 Defocus range (required to apply CTF).",
60 "-fieldbasename field Field-of-view base name (default field).",
61 "-fieldnumber 5 Field starting number (default 1).",
62 "-mgbasename mg Micrograph base name (default mg).",
63 "-mgnumber 23 Micrograph starting number (default 1).",
64 "-partbasename part Particle base name (default part).",
65 "-partnumber 158 Particle starting number (default 1).",
66 " ",
67 "Parameters for simulation:",
68 "-fieldname field Field-of-view name (for selecting a specific field).",
69 "-mgname mg Micrograph base name (for selecting a specific micrograph).",
70 "-partselect 132 Particle selection for potential generation (default all).",
71 "-type real Type of potential calculation (default reciprocal, other gauss).",
72 "-size 100,80,120 Size of enclosing box (default based on input coordinates).",
73 "-thickness 20 Slice thickness in angstrom (default 10 A).",
74 "-resolution 8.5 Resolution limits (default Nyquest frequency).",
75 "-Bfactor 24.5 Set the overall temperature factor (default 0 A^2).",
76 " ",
77 "Input:",
78 "-parameters param.star Parameter file name for electron scattering curves (default atom_prop.star).",
79 "-Water water.pdb Block of water to embed the molecule into (default none).",
80 " ",
81 "Output:",
82 "-output mg.star Parameter file (use with generate option).",
83 " ",
84 NULL
85 };
86 
87 int main(int argc, char **argv)
88 {
89  /* Initialize variables */
90  int genfield(0), genmg(0), genpart(0); // Number of randomly oriented molecules to generate
91  Bstring symmetry_string; // Default: none
92  double theta_step(M_PI*10.0/180.0); // Angular step size for theta
93  double phi_step(M_PI*10.0/180.0); // Angular step size for phi
94  double tsigma(0); // No random shift in origins
95  double img_origin(0); // Particle image origin
96  Vector3<double> pixel_size; // Take pixel size from input files
97  double dose(20); // Electron dose
98  double Cs(2e7); // Spherical aberration
99  double Cc(2e7); // Chromatic aberration (angstrom)
100  double Volts(120000); // Acceleration voltage
101  double Amp(0.07); // Amplitude contrast contribution
102  double def_min(0), def_max(0); // Defocus range (angstrom) default none
103 // DataType datatype = Float; // Default data type for projection
104  int pottype(0); // Reciprocal space potential calculation
105  Vector3<long> size; // Simulated box size
106  double thickness(10); // Slice thickness in angstrom
107  double resolution(0); // High resolution limit
108  double Bfactor(0); // Overall temperature factor
109  double alpha(0.0001); // Illumination half-angle (radians)
110  double energy_spread(1e-5); // Effective energy spread
111  int image_flag(0); // Flag to generate the final image and parameter file
112  int poisson(0); // No poisson noise added
113  double gauss(1e10); // Gaussian signal-to-noise ratio, > 1000 means no noise
114  double kmtf(0); // MTF decay constant
115  Bstring atom_select("ALL");
116  Bstring fieldbase("field"); // Field-of-view base name
117  Bstring fieldname; // Selected field-of-view name
118  Bstring mgbase("mg"); // Micrograph base name
119  Bstring mgname; // Selected micrograph name
120  Bstring partbase("part"); // Particle base name
121  int fieldnumber(1); // Starting field number
122  int mgnumber(1); // Starting micrograph number
123  int partnumber(1); // Starting particle ID
124  int partselect(0); // Selected particle ID
125  Bstring coorfile; // Input coordinate file
126  Bstring waterfile; // Input solvent coordinate file
127  Bstring paramfile; // Use default parameter file for atomic properties
128  Bstring outfile; // Output parameter file
129 
130  int i, optind;
131  Boption* option = get_option_list(use, argc, argv, optind);
132  Boption* curropt;
133  for ( curropt = option; curropt; curropt = curropt->next ) {
134  if ( curropt->tag == "generate" )
135  if ( curropt->values(genfield, genmg, genpart) < 3 )
136  cerr << "-generate: Three numbers must be specified!" << endl;
137  if ( curropt->tag == "symmetry" )
138  symmetry_string = curropt->symmetry_string();
139  if ( curropt->tag == "angles" ) {
140  if ( ( i = curropt->values(theta_step, phi_step) ) < 1 )
141  cerr << "-angles: An angle step size must be specified!" << endl;
142  else {
143  theta_step *= M_PI/180.0;
144  if ( i < 2 ) phi_step = theta_step;
145  else phi_step *= M_PI/180.0;
146  }
147  }
148  if ( curropt->tag == "translate" )
149  if ( ( tsigma = curropt->value.real() ) < 1e-30 )
150  cerr << "-translate: A translation standard deviation must be specified!" << endl;
151  if ( curropt->tag == "origin" )
152  if ( ( img_origin = curropt->value.real() ) < 1e-30 )
153  cerr << "-origin: The image origin must be specified!" << endl;
154  if ( curropt->tag == "Pixelsize" )
155  pixel_size = curropt->scale();
156 // if ( ( pixel_size = curropt->value.real() ) < 1e-30 )
157 // cerr << "-Pixelsize: The particle pixel size must be specified!" << endl;
158  if ( curropt->tag == "electrondose" )
159  if ( ( dose = curropt->value.real() ) < 1e-30 )
160  cerr << "-electrondose: The electron dose must be specified!" << endl;
161  if ( curropt->tag == "Cs" ) {
162  if ( ( Cs = curropt->value.real() ) < 1e-30 )
163  cerr << "-Cs: A Cs value must be specified!" << endl;
164  else if ( Cs < 1e3 ) Cs *= 1e7; // Assume mm
165  }
166  if ( curropt->tag == "Cc" ) {
167  if ( ( Cc = curropt->value.real() ) < 1e-30 )
168  cerr << "-Cc: A Cc value must be specified!" << endl;
169  else if ( Cc < 1e3 ) Cc *= 1e7; // Assume mm
170  }
171  if ( curropt->tag == "alpha" ) {
172  if ( ( alpha = curropt->value.real() ) < 1e-30 )
173  cerr << "-alpha: The illumination half-angle must be specified!" << endl;
174  else {
175  if ( alpha > 0.01 ) alpha /= 1000; // Assume milliradians
176  if ( alpha < 0 ) alpha = 0.0001;
177  }
178  }
179  if ( curropt->tag == "energyspread" ) {
180  if ( ( energy_spread = curropt->value.real() ) < 1e-30 )
181  cerr << "-energyspread: A energy spread must be specified!" << endl;
182  else {
183  if ( energy_spread > 0.001 ) energy_spread /= 1e5; // Assume eV
184  if ( energy_spread < 0 ) energy_spread = 0;
185  }
186  }
187  if ( curropt->tag == "Volt" ) {
188  if ( ( Volts = curropt->value.real() ) < 1e-30 )
189  cerr << "-Volt: A voltage must be specified!" << endl;
190  else if ( Volts < 1e3 ) Volts *= 1e3; // Assume kilovolts
191  }
192  if ( curropt->tag == "Amplitude" ) {
193  if ( ( Amp = curropt->value.real() ) < 1e-30 )
194  cerr << "-Amplitude: A fraction must be specified!" << endl;
195  else if ( Amp > 1 ) Amp /= 100; // Assume percentage
196  }
197  if ( curropt->tag == "Defocus" ) {
198  if ( curropt->values(def_min, def_max) < 1 )
199  cerr << "-Defocus: Both defocus minimum and maximum must be specified!" << endl;
200  if ( def_max < def_min ) def_max = def_min;
201  if ( def_min < 100 && def_max < 100 ) { // Assume um
202  def_min *= 1e4;
203  def_max *= 1e4;
204  }
205  }
206 // if ( curropt->tag == "datatype" )
207 // datatype = curropt->datatype();
208  if ( curropt->tag == "type" ) {
209  pottype = 0;
210  if ( curropt->value.contains("rea") ) pottype = 1;
211  if ( curropt->value.contains("gau") ) pottype = 2;
212  }
213  if ( curropt->tag == "size" )
214  size = curropt->size();
215  if ( curropt->tag == "thickness" )
216  if ( ( thickness = curropt->value.real() ) < 1e-30 )
217  cerr << "-thickness: A slice thickness must be specified!" << endl;
218  if ( curropt->tag == "resolution" )
219  if ( ( resolution = curropt->value.real() ) < 0.001 )
220  cerr << "-resolution: A resolution limit must be specified!" << endl;
221  if ( curropt->tag == "Bfactor" )
222  if ( ( Bfactor = curropt->value.real() ) < 0.001 )
223  cerr << "-Bfactor: A temperature factor must be specified!" << endl;
224  if ( curropt->tag == "image" )
225  image_flag = 1;
226  if ( curropt->tag == "poisson" )
227  poisson = 1;
228  if ( curropt->tag == "MTF" )
229  if ( ( kmtf = curropt->value.real() ) < 1e-30 )
230  cerr << "-MTF: The MTF decay constant must be specified!" << endl;
231  if ( curropt->tag == "gaussian" )
232  if ( ( gauss = curropt->value.real() ) < 1e-30 )
233  cerr << "-gaussian: The signal-to-noise ratio must be specified!" << endl;
234  if ( curropt->tag == "fieldbasename" ) {
235  if ( curropt->value.length() > 0 ) fieldbase = curropt->value;
236  else cerr << "-fieldbasename: A field-of-view base name must be specified!" << endl;
237  }
238  if ( curropt->tag == "fieldname" ) {
239  if ( curropt->value.length() > 0 ) fieldname = curropt->value;
240  else cerr << "-fieldname: A field-of-view name must be specified!" << endl;
241  }
242  if ( curropt->tag == "mgbasename" ) {
243  if ( curropt->value.length() > 0 ) mgbase = curropt->value;
244  else cerr << "-mgbasename: A micrograph base name must be specified!" << endl;
245  }
246  if ( curropt->tag == "mgname" ) {
247  if ( curropt->value.length() > 0 ) mgname = curropt->value;
248  else cerr << "-mgname: A micrograph name must be specified!" << endl;
249  }
250  if ( curropt->tag == "partbasename" ) {
251  if ( curropt->value.length() > 0 ) partbase = curropt->value;
252  else cerr << "-partbasename: A particle base name must be specified!" << endl;
253  }
254  if ( curropt->tag == "fieldnumber" )
255  if ( ( fieldnumber = curropt->value.integer() ) < 1 )
256  cerr << "-fieldnumber: A field-of-view starting number must be specified!" << endl;
257  if ( curropt->tag == "mgnumber" )
258  if ( ( mgnumber = curropt->value.integer() ) < 1 )
259  cerr << "-mgnumber: A micrograph starting number must be specified!" << endl;
260  if ( curropt->tag == "partnumber" )
261  if ( ( partnumber = curropt->value.integer() ) < 1 )
262  cerr << "-partnumber: A particle starting number must be specified!" << endl;
263  if ( curropt->tag == "partselect" )
264  if ( ( partselect = curropt->value.integer() ) < 1 )
265  cerr << "-partselect: A particle number must be specified!" << endl;
266  if ( curropt->tag == "coordinates" )
267  coorfile = curropt->filename();
268  if ( curropt->tag == "Water" )
269  waterfile = curropt->filename();
270  if ( curropt->tag == "Parameter" )
271  paramfile = curropt->filename();
272  if ( curropt->tag == "output" )
273  outfile = curropt->filename();
274  }
275  option_kill(option);
276 
277  double ti = timer_start();
278 
279  // Read all the parameter files
280  Bstring* file_list = NULL;
281  Bproject* project = NULL;
282  while ( optind < argc ) string_add(&file_list, argv[optind++]);
283  if ( file_list ) {
284  project = read_project(file_list);
285  string_kill(file_list);
286  }
287 
288  if ( img_origin <= 0 ) img_origin = size[0]/2;
289 
290  if ( !project ) {
291  if ( symmetry_string.length() > 0 ) {
292  project = project_generate_asu(symmetry_string, pixel_size, img_origin,
293  theta_step, phi_step, Volts, Cs, Cc, alpha, energy_spread, Amp,
294  def_min, dose, mgbase, partbase);
295  } else if ( genfield*genmg*genpart > 0 ) {
296  project = project_generate(genfield, genmg, genpart, pixel_size, img_origin,
297  Volts, Cs, Cc, alpha, energy_spread, Amp, def_min, def_max, dose,
298  tsigma, fieldbase, mgbase, partbase, fieldnumber, mgnumber, partnumber);
299  }
300  }
301 
302  if ( !project ) {
303  cerr << "Error: No STAR file generated or read!" << endl;
304  bexit(-1);
305  }
306 
307  if ( outfile.c_str() ) {
308  write_project(outfile, project, 0, 0);
309  }
310 
311  Bmolgroup* molgroup = NULL;
312  Bmolgroup* water = NULL;
313  if ( coorfile.length() ) {
314  molgroup = read_molecule(coorfile, atom_select, paramfile);
315  if ( !molgroup ) {
316  error_show(coorfile.c_str(), __FILE__, __LINE__);
317  bexit(-1);
318  }
319  molecule_update_comment(molgroup, argc, argv);
320  if ( waterfile.c_str() ) {
321  water = read_molecule(waterfile, atom_select, paramfile);
322  if ( !water ) {
323  error_show(waterfile.c_str(), __FILE__, __LINE__);
324  bexit(-1);
325  }
326  }
327  project_generate_potential(molgroup, water, project, fieldname, mgname, partselect,
328  size, thickness, resolution, Bfactor, pottype, paramfile);
329  }
330 
331  Bstring outfile2;
332  Bstring insert("_tfn.");
333  if ( image_flag ) {
334  // Generates an image with an average of one and the CTF applied
335  // Thickness and resolution must correspond to the values used for potential calculation
336  project_generate_image(project, thickness, resolution);
337 
338  if ( outfile.length() ) {
339  // Adjust for dose and apply noise and envelope functions
340  project_apply_distortions(project, poisson, gauss, kmtf);
341 // outfile2 = insert_in_filename(outfile, insert, '_');
342  outfile2 = outfile.pre_rev('.') + insert + outfile.post_rev('.');
343  write_project(outfile2, project, 0, 0);
344  }
345  }
346 
347  molgroup_kill(molgroup);
348  molgroup_kill(water);
350 
351  if ( verbose & VERB_TIME )
352  timer_report(ti);
353 
354  bexit(0);
355 }
356 
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
mg_multislice.h
Generates and manipulates projects for multislice calculations.
Boption::scale
Vector3< double > scale()
Definition: options.h:190
molgroup_kill
int molgroup_kill(Bmolgroup *molgroup)
Destroys a molecule group.
Definition: rwmolecule.cpp:374
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:768
molecule_update_comment
int molecule_update_comment(Bmolgroup *molgroup, int n, char **strings)
Puts a set of strings and time in the main comment of a molecule group.
Definition: rwmolecule.cpp:1467
Bstring
Definition: Bstring.h:29
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:436
Bstring::integer
long integer()
Definition: Bstring.cpp:547
verbose
int verbose
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
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:719
Bstring::real
double real()
Definition: Bstring.cpp:553
read_molecule
Bmolgroup * read_molecule(const char *filename, const char *select, const char *paramfile)
The generalized function for reading molecular files.
Definition: rwmolecule.cpp:818
options.h
Header file for option handlers.
project_generate
Bproject * project_generate(int nfield, int nmg, int npart, Vector3< double > pixel_size, double img_origin, double volt, double Cs, double Cc, double alpha, double energy_spread, double amp_shift, double def_min, double def_max, double dose, double tsigma, Bstring &fieldbase, Bstring &mgbase, Bstring &partbase, int fieldnumber, int mgnumber, int partnumber)
Generates a project for multislice simulations.
Definition: mg_multislice.cpp:344
project_generate_potential
int project_generate_potential(Bmolgroup *molgroup, Bmolgroup *water, Bproject *project, Bstring &fieldname, Bstring &mgname, int partselect, Vector3< long > size, double thickness, double resolution, double Bfactor, int pottype, Bstring &paramfile)
Generates potential images using a multislice calculation.
Definition: mg_multislice.cpp:504
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
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.
insert
string insert(string &filename, string ins)
Definition: string_util.cpp:48
rwmolecule.h
Header file for reading reflection files.
main
int main(int argc, char **argv)
Definition: bsim.cpp:87
Boption::value
Bstring value
Definition: options.h:30
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:421
project_apply_distortions
int project_apply_distortions(Bproject *project, int poisson, double gauss, double kmtf)
Applies imaging distortions to the final images from a multislice calculation.
Definition: mg_multislice.cpp:676
timer.h
Utilities for timing functions.
Bproject
Definition: mg_processing.h:336
Boption::symmetry_string
Bstring symmetry_string()
Definition: options.h:233
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
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
Boption::size
Vector3< long > size()
Definition: options.h:163
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
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.
use
const char * use[]
Definition: bsim.cpp:21
project_generate_image
int project_generate_image(Bproject *project, double thickness, double resolution)
Generates final images from a multislice calculation.
Definition: mg_multislice.cpp:604
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
Bmolgroup
Definition: rwmolecule.h:170
project_generate_asu
Bproject * project_generate_asu(Bstring &symmetry_string, Vector3< double > pixel_size, double img_origin, double theta_step, double phi_step, double volt, double Cs, double Cc, double alpha, double energy_spread, double amp_shift, double defocus, double dose, Bstring &mgbase, Bstring &partbase)
Generates a project for multislice calculations of an asymmetric unit.
Definition: mg_multislice.cpp:442
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:713