Bsoft  2.1.0
Bernard's software package
dhand.cpp
Go to the documentation of this file.
1 
9 #include "mg_hand.h"
10 #include "mg_tomography.h"
11 #include "mg_particle_select.h"
12 #include "rwmg.h"
13 #include "options.h"
14 #include "timer.h"
15 #include "utilities.h"
16 
17 // Global variables
18 extern int verbose; // Level of output to the screen
19 
20 // Internal function prototype
21 int hand_get_options_mgselect(Bstring& tag, Bstring& tagvalue, int* mg_select, int* mg_index, double* mg_ang);
22 
23 
24 // Usage information
25 const char* use[] = {
26 " ",
27 "Usage: dhand [options] input1.star [input2.star...]",
28 "----------------------------------------------------------------------------",
29 "Compares projections of two oppositely handed reconstructions to images",
30 "of tilted specimens. The orientation of the second view is predicted",
31 "from the first view's orientation and from the tilt-axis direction and the",
32 "rotation angle. For particles from the two views, output selection values",
33 "are set to 1 (hand A), 2 (hand B), or 0 (unassigned, FOM < min. value or ",
34 "|FOMA-FOMB| < min. acceptable difference). Particles with input selection",
35 "values of zero are ignored unless option '-all' is used.",
36 " ",
37 "Actions:",
38 "-difference 1 Compute |difference| between measured (input) and predicted",
39 " orientations, 0=do not (default), 1=pred. orient. and avg",
40 " |difference|, 2=#1 + individual values.",
41 "-org2 cross Get particle origins of second view by cross-correlation",
42 " with projections of predicted view (cross), from image",
43 " files (image), or from parameter files (param, default).",
44 "-selconsist Sets selection values in unused micrographs to be consistent",
45 " with those in views 1 and 2.",
46 " ",
47 "Parameters:",
48 "-verbose 1 Verbosity of output.",
49 "-all Include all particles (default sel>0).",
50 "-numberperfield 3 Number of micrographs per field-of-view (default 1).",
51 "-fieldID dbgd Set the field ID (multiple micrographs will get numbered ID's).",
52 "-axis 74.7 Tilt axis angle relative to x-axis (default from parameter file).",
53 //"-angle -15 Tilt angle (default from parameter file).
54 "-origin 50,5.3,50.4 Set the x,y,z origin of 3D map, in pixel units.",
55 "-mindiff 0.05 minimum acceptable |difference| between hand A & B ",
56 " correlation coefficients, default = 0.050.",
57 "-minfom 0.1 min. acceptable value for FOM of correct hand, default=0.",
58 "-radii 5,65 Radial limits (in pixels), default = all radii.",
59 "-resolution 100,17 Resolution limits in angs. (low,high), default = 400,20.",
60 "-sampling 2.54 x,y,z pixel sampling (input map and all micrographs).",
61 " ",
62 "Parameters for selecting first and second views (use mgsel or both mg1 & mg2):",
63 "-mgsel angle,0,-5.0 Criterion, value for first view, value for second view",
64 "-mg1 index,1 Micrograph in each set that is the first view",
65 "-mg2 angle,-5.0 Micrograph in each set that is the second view",
66 " Options:",
67 " angle,a (tilt angle)",
68 " index,n (index within a series, beginning with 1)",
69 " near,n (nth nearest-to-focus, beginning with 1)",
70 " far,n (nth farthest-from-focus, beginning with 1)",
71 " ",
72 "Input:",
73 "-map input.img Reference map.",
74 " ",
75 "Output:",
76 "-output output.star Outputs data in STAR format to output.star",
77 "-outprj output.img Outputs projections to output_field#_img#_imgID.img",
78 " ",
79 NULL
80 };
81 
82 
83 
84 int main(int argc, char* argv[])
85 {
86 
87  int all(0); // Flag to reselect all particles
88  long nseries(0); // Number of micrographs in a series from a field
89  Bstring field_id; // User-specified field ID
90  double tilt_axis(-1000); // Tilt axis angle
91  double tilt_angle(0); // Tilt angle start
92  double AB_min(0); // FOM for A or B must be this value or greater
93  double AmB_min(0.05); // |FOMA - FOMB| must be this value or greater
94  long count,mcount,fcount; // Placeholder for number of fields-of-view, micrographs, and particles
95  int diff_out(0); // Flag to output difference between measured and predicted orientations
96  int handedness; // Output to shell: 1=hand A, 2=hand B, 0=undetermined
97  Bstring inmap; // Input 3D map (image-format) file
98  int MapOriginFlag(0); // Flag to set origin of input 3D map
99  double mg_ang[2]; // values and flags for selection of micrographs corresponding to views 1 and 2
100  int mg_index[2];
101  int mg_select[2];
102  int mgsel(0); // Flag to make sure proper number of mg selections is made
103  int origins2(0); // Flag to determine origins for second view
104  Vector3<double> origind; // X,Y,Z origin for 3D map
105  Bstring outimg; // Prefix and suffix for output image files
106  Bstring outfile; // Output parameter file
107  Vector3<double> sam; // User-entered pixel size for map and particle images
108  Bproject* project=NULL; // Data for all micrographs
109  Bimage *penantiomer=NULL; // 3D maps, pmirror is mirror image of penantiomer
110  double rad_min(0); // minimum and maximum radius, given in pixels
111  double rad_max(-1.0);
112  double res_min(400); // minimum and maximum resolution, given in angstroms
113  double res_max(20);
114  int sel_consist(0); // Flag to set consistent selection values in all micrographs
115 
116 
117  // Get and check command-line input, program terminates if there is a problem
118  int optind;
119  Boption* option = get_option_list(use, argc, argv, optind);
120  Boption* curropt;
121  for ( curropt = option; curropt; curropt = curropt->next ) {
122  if ( curropt->tag == "all" ) // all particles will be processed
123  all = 1;
124  if ( curropt->tag == "numberperfield" )
125  if ( ( nseries = curropt->value.integer() ) < 1 ) {
126  cerr << "-numberperfield: The number of micrographs in a field-of-view must be specified!" << endl;
127  bexit(-1);
128  }
129  if ( curropt->tag == "fieldID" ) {
130  field_id = curropt->value;
131  if ( field_id.length() < 1 )
132  cerr << "-fieldID: A field ID must be specified!" << endl;
133  }
134  if ( curropt->tag == "axis" ) {
135  if ( ( tilt_axis = curropt->value.real() ) < 0.00001 )
136  cerr << "-axis: The tilt axis angle must be specified." << endl;
137  else
138  tilt_axis = angle_set_negPI_to_PI(tilt_axis*M_PI/180.0);
139  }
140  if ( curropt->tag == "angle" ) {
141  if ( ( tilt_angle = curropt->value.real() ) < 0.00001 )
142  cerr << "-angle: The tilt angle must be specified." << endl;
143  else
144  tilt_angle = angle_set_negPI_to_PI(tilt_angle*M_PI/180.0);
145  }
146  if ( curropt->tag == "difference" ) { // difference meas. & pred.
147  diff_out = curropt->value.integer();
148  if ( (diff_out < 0) || (diff_out > 2) ) {
149  cerr << "Error: -difference, a difference output option (0-2) must be specified!" << endl;
150  bexit(-1);
151  }
152  }
153  if ( curropt->tag == "map" ) // input 3D map
154  inmap = curropt->filename();
155  if ( curropt->tag == "origin" ) { // set origin of input map
156  origind = curropt->origin();
157  MapOriginFlag = 1;
158  }
159  if ( curropt->tag == "mgsel" || // selection of 1st and 2nd micrographs (views)
160  curropt->tag == "mg1" ||
161  curropt->tag == "mg2" ) {
162  if ( curropt->tag == "mgsel" ) mgsel += 2;
163  else mgsel += 1;
164  if ( mgsel > 2 ) {
165  cerr << "Error: too many micrograph selection choices were entered!" << endl;
166  bexit(-1);
167  }
168  hand_get_options_mgselect(curropt->tag, curropt->value, mg_select, mg_index, mg_ang);
169  }
170  if ( curropt->tag == "minfom" ) { // min. allowable CC value
171  if ( ( AB_min = curropt->value.real() ) < 0.0001 ) {
172  cerr << "Error: -minfom, threshold value must be specified!" << endl;
173  bexit(-1);
174  }
175  }
176  if ( curropt->tag == "mindiff" ) { // min. allowable difference between hand A & B
177  if ( ( AmB_min = curropt->value.real() ) < 0.0001 ) {
178  cerr << "Error: -mindiff, threshold value must be specified!" << endl;
179  bexit(-1);
180  }
181  }
182  if ( curropt->tag == "org2" ) { // Source of origins for images of second view
183  if ( curropt->value.contains("par") ) origins2 = 0;
184  else if ( curropt->value.contains("cro") ) origins2 = 1;
185  else if ( curropt->value.contains("im") ) origins2 = 2;
186  else {
187  cerr << "Error: -org2, " << curropt->value << " is an improper value for this option" << endl;
188  bexit(-1);
189  }
190  }
191  if ( curropt->tag == "outprj" ) { // Output projections
192  outimg = curropt->filename();
193  }
194  if ( curropt->tag == "output" ) // Output parameter file
195  outfile = curropt->filename();
196  if ( curropt->tag == "radii" ) { // Radial parameters
197  if ( curropt->values(rad_min, rad_max) < 2 ) {
198  cerr << "Error: -radii, radial limits (min,max) in pixels must be specified!" << endl;
199  bexit(-1);
200  }
201  }
202  if ( curropt->tag == "resolution" ) { // Resolution parameters
203  if ( curropt->values(res_min, res_max) < 2 ) {
204  cerr << "Error: -resolution, two resolution parameters must be specified!" << endl;
205  bexit(-1);
206  }
207  else {
208  if ( res_max > res_min) {
209  swap(res_min, res_max);
210  if (verbose) cout << "Resolution max > resolution min, so the two were swapped" << endl;
211  }
212  }
213  }
214  if ( curropt->tag == "sampling" ) // set pixel size
215  sam = curropt->scale();
216  if ( curropt->tag == "selconsist" ) // set selection values in unused micrographs to those in views 1 and 2
217  sel_consist = 1;
218  }
219  option_kill(option);
220 
221  double ti = timer_start();
222 
223  if ( mgsel != 2 ) {
224  cerr << "Error: no or not enough micrograph selection input was entered." << endl;
225  bexit(-1);
226  }
227 
228  // Read all the parameter files
229  Bstring* file_list = NULL;
230  while ( optind < argc ) string_add(&file_list, argv[optind++]);
231  if ( !file_list ) {
232  cerr << "Error: No parameter files specified!" << endl;
233  bexit(-1);
234  }
235  project = read_project(file_list);
236  string_kill(file_list);
237  if ( project == NULL ) {
238  cerr << "Error: No input file read!" << endl;
239  bexit(-1);
240  }
241 
242  // Select all particles
243  if ( all ) part_reset_selection(project, 3);
244 
245  if ( nseries )
246  project_set_field_id(project, nseries, field_id);
247 
248  if ( tilt_axis )
249  project_set_tilt_axis(project, tilt_axis);
250 
251  // Data count, there must be at least two micrographs in each field-of-view
252  fcount = project_count_fields(project);
254  if ( mcount < 2*fcount ) {
255  cerr << "Error: there must be at least two micrographs in each field-of-view:" << endl;
256  cerr << "Number of micrographs = " << mcount << " Number of fields-of-view = " << fcount << endl;
257  bexit(-1);
258  }
259  if ( verbose < VERB_PROCESS ) {
260  cout << "Fields-of-view: " << fcount << endl;
261  cout << "Micrographs: " << mcount << endl;
263  cout << "Particles in all micrographs: " << count << endl;
265  cout << "Selected particles: " << count << endl << endl;
266  }
267 
268 
269  // Read input enantiomer (hand A map),
270  if (verbose) cout << "Map file name (hand A): \"" << inmap << "\"" << endl;
271  penantiomer = read_img(inmap, 1, -1);
272 
273 
274  // Set map origin if entered
275  if ( MapOriginFlag ) // Map origin set to command-line input value
276  penantiomer->origin(origind);
277 
278 
279  // Set pixel size for input map and all micrographs, if entered
280  if ( sam.volume() > 0.0 ) {
281  penantiomer->sampling(sam);
283  }
284 
285 
286  // Set radial max. cut-off to max. radius of map, if it wasn't entered
287  if (rad_max < 0.0)
288  rad_max = penantiomer->maximum_included_radius();
289 
290 
291  // Determine handedness
292  handedness = project_get_handedness(penantiomer, project, mg_ang, mg_index, mg_select, rad_min, rad_max,
293  res_min, res_max, AmB_min, AB_min, diff_out, origins2, outimg);
294 
295 
296  // Set consistent selection values for all micrographs in a field-of-view
297  if ( sel_consist )
298  hand_select_consist(project, mg_ang, mg_index, mg_select, sel_consist);
299 
300 
301  // Output parameters
302  if ( project && outfile.length() ) {
303  write_project(outfile, project, 0, 0);
304  }
305 
306 
307  // Memory cleanup
308  delete penantiomer;
310 
311  if ( verbose & VERB_TIME )
312  timer_report(ti);
313 
314  bexit(handedness);
315 
316 } // End of main program
317 
318 /**************************************************************************
319 @author David Belnap
320 @brief Gets criteria for selecting micrographs (from a field-of-view) for a
321  handedness-determination calculation.
322 @param &tag "mgsel", "mg1", or "mg2".
323 @param &tagvalue string with one of options listed above.
324 @param *mg_select micrograph selection criteria, views 1 & 2.
325 @param *mg_index indices for micrograph selection, views 1 & 2.
326 @param *mg_ang angles for micrograph selection, views 1 & 2.
327 @return int 0.
328 
329  User has four options:
330  1. angle,a (tilt angle)
331  2. index,n (index within a series, beginning with 1)
332  3. near,n (nth nearest-to-focus, beginning with 1)
333  4. far,n (nth farthest-from-focus, beginning with 1)
334 
335 **/
336 int hand_get_options_mgselect(Bstring& tag, Bstring& tagvalue, int* mg_select, int* mg_index, double* mg_ang)
337 {
338  Bstring* strarr = tagvalue.split(",");
339 
340  if ( tag == "mgsel" ) {
341  if ( tagvalue.contains("ang") ) {
342  mg_select[0] = mg_select[1] = 4;
343  mg_ang[0] = strarr->next->real() * M_PI/180.0;
344  mg_ang[1] = strarr->next->next->real() * M_PI/180.0;
345  } else {
346  mg_index[0] = strarr->next->integer();
347  mg_index[1] = strarr->next->next->integer();
348  if ( tagvalue.contains("near") ) mg_select[0] = mg_select[1] = 0;
349  else if ( tagvalue.contains("far") ) mg_select[0] = mg_select[1] = 1;
350  else if ( tagvalue.contains("ind") ) mg_select[0] = mg_select[1] = 2;
351  }
352  } else if ( tag == "mg1" ) {
353  if ( tagvalue.contains("ang") ) {
354  mg_select[0] = 4;
355  mg_ang[0] = strarr->next->real() * M_PI/180.0;
356  } else {
357  mg_index[0] = strarr->next->integer();
358  if ( tagvalue.contains("near") ) mg_select[0] = 0;
359  else if ( tagvalue.contains("far") ) mg_select[0] = 1;
360  else if ( tagvalue.contains("ind") ) mg_select[0] = 2;
361  }
362  } else if ( tag == "mg2" ) {
363  if ( tagvalue.contains("ang") ) {
364  mg_select[1] = 4;
365  mg_ang[1] = strarr->next->real() * M_PI/180.0;
366  } else {
367  mg_index[1] = strarr->next->integer();
368  if ( tagvalue.contains("near") ) mg_select[1] = 0;
369  else if ( tagvalue.contains("far") ) mg_select[1] = 1;
370  else if ( tagvalue.contains("ind") ) mg_select[1] = 2;
371  }
372  }
373 
374  string_kill(strarr);
375 
376  return 0;
377 }
378 
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
project_count_mg_particles
long project_count_mg_particles(Bproject *project)
Counts all the particles in the project structure.
Definition: mg_select.cpp:415
mg_particle_select.h
Select particles.
Boption::scale
Vector3< double > scale()
Definition: options.h:190
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:768
Bstring
Definition: Bstring.h:29
project_count_fields
long project_count_fields(Bproject *project)
Counts all the fields in the project structure.
Definition: mg_select.cpp:335
Bstring::integer
long integer()
Definition: Bstring.cpp:547
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
main
int main(int argc, char *argv[])
Definition: dhand.cpp:84
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
project_set_field_id
int project_set_field_id(Bproject *project, int nseries, Bstring &field_id)
Sets the field id's according to a regular series.
Definition: mg_processing.cpp:1456
options.h
Header file for option handlers.
Boption::origin
Vector3< double > origin()
Definition: options.h:180
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
angle_set_negPI_to_PI
double angle_set_negPI_to_PI(double angle)
Returns an angle between -PI and PI.
Definition: utilities.cpp:558
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
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:77
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
part_reset_selection
long part_reset_selection(Bproject *project, int flag=0)
Resets selection to all particles.
Definition: mg_particle_select.cpp:133
mg_hand.h
Author: David Belnap.
Boption::value
Bstring value
Definition: options.h:30
project_count_mg_part_selected
long project_count_mg_part_selected(Bproject *project)
Counts the number of particles in a project.
Definition: mg_select.cpp:437
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
Bproject
Definition: mg_processing.h:336
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
hand_select_consist
int hand_select_consist(Bproject *project, double *mg_ang, int *mg_index, int *mg_select, int sel_consist)
Sets consistent selection values in all (including unused) micrographs.
Definition: mg_hand.cpp:897
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.
Bstring::length
long length() const
Definition: Bstring.cpp:263
verbose
int verbose
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
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
use
const char * use[]
Definition: dhand.cpp:25
project_get_handedness
int project_get_handedness(Bimage *penantiomer, Bproject *project, double *mg_ang, int *mg_index, int *mg_select, double rad_min, double rad_max, double res_min, double res_max, double AmB_min, double AB_min, int diff_out, int origins2, Bstring outimg)
Determines handedness for all selected particles (sel > 0) in a project.
Definition: mg_hand.cpp:114
Vector3::volume
double volume()
Definition: Vector3.h:237
Bstring::next
Bstring * next
Definition: Bstring.h:31
write_project
int write_project(const char *filename, Bproject *project)
Writing micrograph parameters to a file.
Definition: rwmg.cpp:321
Boption::filename
Bstring filename()
Definition: options.h:85
Bstring::split
Bstring * split()
Definition: Bstring.cpp:605
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
hand_get_options_mgselect
int hand_get_options_mgselect(Bstring &tag, Bstring &tagvalue, int *mg_select, int *mg_index, double *mg_ang)
Definition: dhand.cpp:336
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
Bimage::maximum_included_radius
double maximum_included_radius()
Returns the radius of the enclosed sphere or circle.
Definition: Bimage.cpp:2058
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:713
project_count_micrographs
long project_count_micrographs(Bproject *project)
Counts all the micrographs in the project structure.
Definition: mg_select.cpp:350
mg_tomography.h
Header file for single particle image processing in Bsoft.