Bsoft  2.1.0
Bernard's software package
btomo.cpp
Go to the documentation of this file.
1 
9 #include "mg_processing.h"
10 #include "mg_img_proc.h"
11 #include "mg_tomography.h"
12 #include "mg_align.h"
13 #include "rwmg.h"
14 #include "rwmgIMOD.h"
15 #include "matrix_linear.h"
16 #include "rwimg.h"
17 #include "qsort_functions.h"
18 #include "linked_list.h"
19 #include "random_numbers.h"
20 #include "file_util.h"
21 #include "utilities.h"
22 #include "options.h"
23 #include "timer.h"
24 
25 // Declaration of global variables
26 extern int verbose; // Level of output to the screen
27 
28 // Internal function prototype
29 int read_imod_tlt(Bstring& filename, Bproject* project);
30 
31 // Usage assistance
32 const char* use[] = {
33 " ",
34 "Usage: btomo [options] input.star/input.img [input.star/input.img]",
35 "------------------------------------------------------------------",
36 "Manipulates parameter files for tilt series and aligns them by cross-correlation.",
37 " ",
38 "Actions:",
39 "-all Select all markers.",
40 "-updatematrix Update micrograph matrices from tilt and axis angles.",
41 "-resetmodel Reset the x and y coordinates to that of the zero-degree tilt image.",
42 "-checkmarkers Determine if markers all fall within micrograph boundaries.",
43 "-deselect 5-9,33 Deselect markers by id from all micrographs and models.",
44 "-deletemarkers 5-9,33 Delete markers by id from all micrographs and models.",
45 "-rotationaxis Determine the best rotation axis from refined markers.",
46 "-clear 240,7.5 Clear extraneous areas on micrographs with this intended reconstruction thickness",
47 " and smoothing edge width (default 5).",
48 "-removemarkers 14 Mask out markers with this radius (pixels) before transformation.",
49 "-thickness 234 Calculate mean free path from a given thickness (in angstrom).",
50 "-lambda 2650 Calculate thickness from proportionality coefficient (in angstrom).",
51 "-catenate Concatenate separate micrograph image files into one file.",
52 " ",
53 "Parameters:",
54 "-verbose 7 Verbosity of output.",
55 "-datatype u Force writing of a new data type.",
56 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; a single value can be given).",
57 "-origin 110,50,44 Origin for tilt axis (default 0,0,0).",
58 "-axis 74.7 Tilt axis angle relative to x-axis (default 0 or from parameter file).",
59 "-tilt -65,1.5 Tilt series start and step size (default from parameter file).",
60 "-reference 48 Reference image for alignment (default closest to zero tilt).",
61 "-goldradius 12 Gold particle radius (default from parameter file).",
62 " ",
63 "Input:",
64 "-rawtlt series.rawtlt Text file with tilt angles.",
65 " ",
66 "Output:",
67 "-output file.star Output parameter file.",
68 "-image file.pif Output multi-image file with aligned images.",
69 "-Postscript inten.ps Postscript file with fit of tilt series intensities.",
70 " ",
71 NULL
72 };
73 
74 
75 int main(int argc, char** argv)
76 {
77  // Initializing variables
78  int select_all(0); // Flag to select all markers
79  int update_matrix(0); // Flag to update micrograph matrix
80  int reset_model(0); // Flag to reset the x and y coordinates of the model
81  int checkmarkers(0); // Flag to determine if markers all fall within micrograph boundaries
82  Bstring deselect; // String specifying markers to deselect
83  Bstring deletemarkers; // String specifying markers to delete
84  int rotation_axis(0); // Flag to determine the best rotation axis
85 // int crosscorrelate(0); // Flag to align by cross-correlation
86  double thickness(0); // Calculates the mean free path
87  double lambda(0); // Calculates the thickness
88  int catenate(0); // Falg to concatenate micrographs into one file
89  int clearareas(0); // Thickness for clearing extraneous areas
90  double edge_width(5); // Smoothing edge width
91  double marker_erase_radius(0); // Radius to mask out markers
92  double marker_radius(0); // Gold marker radius
93  DataType datatype = Unknown_Type; // Conversion to new type
94  int ref_img(-1); // Reference image for alignment, <0 means find zero tilt
95  Vector3<double> sam; // Units for the three axes (A/pixel)
96  Vector3<double> origin; // Tilt axis origin
97  double tilt_axis(0); // Tilt axis angle
98  double tilt_start(0); // Tilt angle start
99  double tilt_step(0); // Tilt_angle step size
100 // int fill_type(FILL_USER); // Type of fill value
101 // double fill(0); // Fill value for new areas
102  Bstring tltfile; // Input tilt angle file
103  Bstring maskfile; // Mask to use for cross-correlation
104  Bstring paramfile; // Output parameter file
105  Bstring imgfile; // Output multi-image file
106  Bstring psfile; // Output Postscript intensities plot file
107 
108  int optind;
109  Boption* option = get_option_list(use, argc, argv, optind);
110  Boption* curropt;
111  for ( curropt = option; curropt; curropt = curropt->next ) {
112  if ( curropt->tag == "all" ) select_all = 1;
113  if ( curropt->tag == "resetmodel" ) reset_model = 1;
114  if ( curropt->tag == "updatematrix" ) update_matrix = 1;
115  if ( curropt->tag == "checkmarkers" ) checkmarkers = 1;
116  if ( curropt->tag == "deselect" ) deselect = curropt->value;
117  if ( curropt->tag == "deletemarkers" ) deletemarkers = curropt->value;
118  if ( curropt->tag == "rotationaxis" ) rotation_axis = 1;
119 // if ( curropt->tag == "crosscorrelate" ) crosscorrelate = 1;
120  if ( curropt->tag == "thickness" )
121  if ( ( thickness = curropt->value.real() ) < 1 )
122  cerr << "-thickness: A thickness must be specified!" << endl;
123  if ( curropt->tag == "lambda" )
124  if ( ( lambda = curropt->value.real() ) < 1 )
125  cerr << "-lambda: A mean free path must be specified!" << endl;
126  if ( curropt->tag == "catenate" ) catenate = 1;
127  if ( curropt->tag == "clear" )
128  if ( curropt->values(clearareas, edge_width) < 1 )
129  cerr << "-clear: A thickness must be specified." << endl;
130  if ( curropt->tag == "removemarkers" )
131  if ( ( marker_erase_radius = curropt->value.real() ) < 1 )
132  cerr << "-removemarkers: A marker radius must be specified!" << endl;
133  if ( curropt->tag == "datatype" )
134  datatype = curropt->datatype();
135  if ( curropt->tag == "sampling" )
136  sam = curropt->scale();
137  if ( curropt->tag == "reference" )
138  if ( ( ref_img = curropt->value.integer() ) < 0 )
139  cerr << "-reference: An image number must be specified." << endl;
140  if ( curropt->tag == "origin" )
141  origin = curropt->origin();
142  if ( curropt->tag == "axis" ) {
143  tilt_axis = angle_set_negPI_to_PI(curropt->value.real()*M_PI/180.0);
144  update_matrix = 1;
145  }
146  if ( curropt->tag == "tilt" ) {
147  if ( curropt->values(tilt_start, tilt_step) < 2 )
148  cerr << "-tilt: Two angles must be specified." << endl;
149  else {
150  tilt_start = angle_set_negPI_to_PI(tilt_start*M_PI/180.0);
151  tilt_step = angle_set_negPI_to_PI(tilt_step*M_PI/180.0);
152  update_matrix = 1;
153  }
154  }
155  if ( curropt->tag == "goldradius" )
156  if ( ( marker_radius = curropt->value.real() ) < 1 )
157  cerr << "-goldradius: A gold marker radius must be specified." << endl;
158 // if ( curropt->tag == "fill" )
159 // fill = curropt->fill(fill_type);
160  if ( curropt->tag == "rawtlt" )
161  tltfile = curropt->filename();
162  if ( curropt->tag == "Mask" )
163  maskfile = curropt->filename();
164  if ( curropt->tag == "output" )
165  paramfile = curropt->filename();
166  if ( curropt->tag == "image" )
167  imgfile = curropt->filename();
168  if ( curropt->tag == "Postscript" )
169  psfile = curropt->filename();
170  }
171  option_kill(option);
172 
173  double ti = timer_start();
174 
175  Bproject* project = NULL;
176 
177  // Read all the parameter files
178  Bstring* file_list = NULL;
179  while ( optind < argc ) string_add(&file_list, argv[optind++]);
180  if ( !file_list ) {
181  cerr << "Error: No parameter or image files specified!" << endl;
182  bexit(-1);
183  }
184 
185  if ( file_type(*file_list) == Micrograph )
186  project = read_project(file_list);
187  else
188  project = project_create_from_images(file_list, "mg");
189 
190  string_kill(file_list);
191 
192  if ( !project ) {
193  cerr << "Error: Input file not read!" << endl;
194  bexit(-1);
195  }
196 
197  if ( verbose & VERB_PROCESS )
199 
200  if ( catenate )
202 
203  if ( origin[0] )
205 
206  if ( project->field && project->field->mg->origin.length() < 1 )
208 
209  if ( tilt_axis )
210  project_set_tilt_axis(project, tilt_axis);
211 
212  if ( tltfile.length() )
213  read_imod_tlt(tltfile, project);
214  else if ( tilt_step )
215  project_set_tilt_angles(project, tilt_start, tilt_step);
216 
217  if ( project->field && project->field->mg->matrix.determinant() < 0.5 )
218  update_matrix = 1;
219 
220  if ( update_matrix )
222 
223  if ( sam[0] > 0 )
225 
226  if ( clearareas )
227  project_clear_extraneous_areas(project, clearareas, edge_width);
228 
229  double tlr(0);
230  Bplot* plot = NULL;
231  if ( thickness || lambda ) {
233  tlr = project_fit_intensities(project, plot);
234  if ( plot && psfile.length() )
235  ps_plot(psfile, plot);
236  if ( thickness ) lambda = thickness/tlr;
237  if ( lambda ) thickness = lambda*tlr;
238  if ( thickness && lambda ) {
239  if ( verbose ) {
240  cout << "Thickness: " << thickness << " A" << endl;
241  cout << "Proportionality parameter: " << lambda << " A" << endl << endl;
242  }
243  }
244  }
245 
246  int i;
247  Bmicrograph* mg_ref = NULL;
248 
249  if ( marker_radius )
250  project_set_marker_radius(project, marker_radius);
251 
252  if ( ref_img > -1 && project->field )
253  for ( i=0, mg_ref=project->field->mg; mg_ref && ( i<ref_img ); mg_ref=mg_ref->next, i++ ) ;
254 
255  if ( !mg_ref )
257 
258  if ( checkmarkers )
260 
261  if ( select_all )
263 
264  if ( deselect.length() )
266 
267  if ( deletemarkers.length() )
268  project_delete_markers(project, deletemarkers);
269 
270  if ( reset_model && project->rec && project->rec->mark )
271  mg_reset_model(mg_ref, project->rec->mark);
272 
273  if ( rotation_axis )
275 
276  if ( project->rec && project->rec->mark )
278 
279  if ( marker_erase_radius )
280  project_erase_markers(project, marker_erase_radius);
281 
282  if ( imgfile.length() )
283  project_write_aligned_images(project, NULL, imgfile, datatype);
284 
285  if ( paramfile.length() )
286  write_project(paramfile, project, 0, 0);
287 
289 
290  if ( verbose & VERB_TIME )
291  timer_report(ti);
292 
293  bexit(0);
294 }
295 
Boption
A class for a command line input option.
Definition: options.h:26
qsort_functions.h
Header file for utility functions for use in qsort calls.
project_kill
int project_kill(Bproject *project)
Frees a whole project.
Definition: mg_processing.cpp:429
project_mg_tilt_to_matrix
int project_mg_tilt_to_matrix(Bproject *project)
Sets the matrix from the tilt and axis angles for each micrograph.
Definition: mg_tomography.cpp:2177
project_delete_markers
long project_delete_markers(Bproject *project, Bstring &delete_list)
Deletes selected markers in a project hierarchy.
Definition: mg_tomography.cpp:1660
Bmicrograph
Definition: mg_processing.h:201
Breconstruction::mark
Bmarker * mark
Definition: mg_processing.h:310
project_mg_marker_select
int project_mg_marker_select(Bproject *project, double fom)
Selects markers.
Definition: mg_tomography.cpp:2058
matrix_linear.h
Header file for matrix functions.
ps_plot
int ps_plot(Bstring filename, Bplot *plot)
Generates postscript graphs.
Definition: ps_plot.cpp:129
Boption::scale
Vector3< double > scale()
Definition: options.h:190
Bproject::rec
Breconstruction * rec
Definition: mg_processing.h:347
project_check_markers
int project_check_markers(Bproject *project, int flags)
Checks if the markers in a project parameter structure fall within micrographs.
Definition: mg_tomography.cpp:2108
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:768
file_util.h
Header file for file checking functions.
Bstring
Definition: Bstring.h:29
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
project_set_nominal_mg_origins
int project_set_nominal_mg_origins(Bproject *project)
Sets micrograph origins to the centers of the micrographs.
Definition: mg_img_proc.cpp:770
Boption::next
Boption * next
Definition: options.h:28
Vector3< double >
random_numbers.h
Header file for functions for creating random images.
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
verbose
int verbose
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
Bplot
Definition: Bplot.h:146
Bstring::real
double real()
Definition: Bstring.cpp:553
options.h
Header file for option handlers.
read_imod_tlt
int read_imod_tlt(Bstring &filename, Bproject *project)
Definition: rwmgIMOD.cpp:48
project_set_micrograph_origins
int project_set_micrograph_origins(Bproject *project, Vector3< double > origin)
Sets micrograph origins to the given origin.
Definition: mg_processing.cpp:2122
Boption::origin
Vector3< double > origin()
Definition: options.h:180
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
project_set_marker_radius
int project_set_marker_radius(Bproject *project, double mark_radius)
Sets the marker radius in a project parameter structure.
Definition: mg_tomography.cpp:2079
angle_set_negPI_to_PI
double angle_set_negPI_to_PI(double angle)
Returns an angle between -PI and PI.
Definition: utilities.cpp:558
rwmgIMOD.h
Converts between IMOD files and a micrograph parameter file.
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.
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
project_erase_markers
int project_erase_markers(Bproject *project, double marker_radius)
Erases or paints markers in micrographs.
Definition: mg_tomography.cpp:1294
project_marker_rotation_axis
double project_marker_rotation_axis(Bproject *project)
Determines the most likely rotation axis location for a tilt series.
Definition: mg_tomography.cpp:2341
main
int main(int argc, char **argv)
Definition: btomo.cpp:75
project_tomo_residuals
double project_tomo_residuals(Bproject *project, int show)
Calculates the residuals for an alignment solution.
Definition: mg_tomography.cpp:868
Micrograph
@ Micrograph
Definition: file_util.h:23
Bmicrograph::origin
Vector3< double > origin
Definition: mg_processing.h:223
Boption::value
Bstring value
Definition: options.h:30
project_fit_intensities
double project_fit_intensities(Bproject *project, Bplot *plot=NULL, int flag=0)
Determines the thickness to mean free path ratio from a tilt series.
Definition: mg_tomography.cpp:619
project_clear_extraneous_areas
int project_clear_extraneous_areas(Bproject *project, long thickness, double width)
Clears areas from a tilt series not used in a reconstruction.
Definition: mg_tomography.cpp:2260
project_create_from_images
Bproject * project_create_from_images(Bstring *file_list, Bstring type)
Creates a project structure using image file names.
Definition: mg_img_proc.cpp:230
timer.h
Utilities for timing functions.
Bproject
Definition: mg_processing.h:336
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
project_write_aligned_images
double project_write_aligned_images(Bproject *project, Bimage *pgr, Bstring &imgfile, DataType datatype)
Calculates aligned micrograph images and write them to a file.
Definition: mg_align.cpp:952
project_intensity_plot
Bplot * project_intensity_plot(Bproject *project)
Transfers the micrograph intensities for a field into a plot.
Definition: mg_tomography.cpp:515
mg_img_proc.h
Header file for image processing from micrograph structures.
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
project_catenate_micrographs
int project_catenate_micrographs(Bproject *project)
Concatenates all micrograph image files into one file.
Definition: mg_img_proc.cpp:472
utilities.h
Header file for general utilities.
Bstring::length
long length() const
Definition: Bstring.cpp:263
Bmicrograph::matrix
Matrix3 matrix
Definition: mg_processing.h:225
project_set_tilt_axis
int project_set_tilt_axis(Bproject *project, double tilt_axis)
Sets the tilt axis angle in a project parameter structure.
Definition: mg_tomography.cpp:1889
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
project_show_markers
long project_show_markers(Bproject *project)
Shows the markers in a project hierarchy.
Definition: mg_tomography.cpp:1535
project_set_tilt_angles
int project_set_tilt_angles(Bproject *project, double tilt_start, double tilt_step)
Sets the tilt and tilt axis angles in a project parameter structure.
Definition: mg_tomography.cpp:1936
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
Matrix3::determinant
double determinant()
Definition: Matrix3.h:260
mg_processing.h
Header file for micrograph processing.
linked_list.h
Header file for generalized linked list functions.
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
mg_reset_model
int mg_reset_model(Bmicrograph *mg, Bmarker *model)
Resets the model x and y coordinates from the zero-tilt micrograph.
Definition: mg_tomography.cpp:1349
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Vector3::length
double length()
Definition: Vector3.h:222
mg_align.h
Header file for functions to align micrographs or coordinates from micrographs and apply the resultan...
Bmicrograph::next
Bmicrograph * next
Definition: mg_processing.h:202
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
use
const char * use[]
Definition: btomo.cpp:32
mg_tomography.h
Header file for single particle image processing in Bsoft.
project_deselect_markers
long project_deselect_markers(Bproject *project, Bstring &deselect_list)
Deletes selected markers in a project hierarchy.
Definition: mg_tomography.cpp:1603
file_type
FileType file_type(const char *filename)
Checks the file type using the extension and contents.
Definition: file_util.cpp:178