Bsoft  2.1.0
Bernard's software package
bxb.cpp
Go to the documentation of this file.
1 
9 #include "rwmodel.h"
10 #include "model_extract_build.h"
11 #include "model_select.h"
12 #include "model_views.h"
13 #include "model_links.h"
14 #include "model_util.h"
15 #include "utilities.h"
16 #include "options.h"
17 #include "timer.h"
18 
19 // Declaration of global variables
20 extern int verbose; // Level of output to the screen
21 
22 /* Usage assistance */
23 const char* use[] = {
24 " ",
25 "Usage: bxb [options] in1.star [in2.star ...]",
26 "--------------------------------------------",
27 "Extracting a part of a density map and building a new map based on a model.",
28 "Component orientational and positional refinement can be done as:",
29 " 1. random modifications over the specified iterations,",
30 " 2. a contracting grid search when the steps and accuracy are specified.",
31 " ",
32 "Actions:",
33 "-setviews local Calculate views for components. Modes: origin: current origin,",
34 " com: center-of-mass origin, map: map origin, local: neigbor plane.",
35 "-invert Invert views for selected components.",
36 "-linklength 58.4 Reference link length to generate links (angstrom, default 1).",
37 "-type component Type of element: component (default) or link.",
38 "-extract 80,50,30 Extract a part of the density with the given size.",
39 "-build 200,250,150 Build a new map of the given size.",
40 "-refine template.map Refine the positions by cross-correlation.",
41 "-average 2 Average extracted densities: averages per type (use with -extract).",
42 "-componenttype A,Ver Names to assign to component types in the template file.",
43 " ",
44 "Selections:",
45 "-all Reset selection to all models and components before other selections.",
46 "-select #Mod1@14 Select models and components.",
47 "-linkselect 2 Link selection for building a new map (first = 1, default all).",
48 "-fom 0.25 Select: FOM cutoff.",
49 " ",
50 "Parameters:",
51 "-verbose 7 Verbose output.",
52 "-datatype u Force writing of a new data type.",
53 " ",
54 "Parameters for building:",
55 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; a single value can be given).",
56 "-origin 0.8,-10,15.7 Set the origin for extracted or built map",
57 " (origins for extract and build cannot be used together).",
58 "-weigh Weigh by contributions when building a new map (default not).",
59 "-separate Separate maps for each component type (default one map).",
60 " ",
61 "Common refinement parameters:",
62 "-resolution 4.5,130 Resolution range for cross-correlation (default 0 - 1e6 angstrom).",
63 "-shift 2.3 Maximum shift allowed per iteration (angstrom, default 10% map width).",
64 "-viewangle 1.4 Maximum rotation angle of view vector allowed per iteration (default 0 degrees).",
65 "-rotationangle 3.5 Maximum rotation angle around view vector allowed per iteration (default 0 degrees).",
66 "-normal Flag to shift only along the component view vector.",
67 "-lateral Flag to shift only perpendicular to the component view vector.",
68 " ",
69 "Parameters for random refinement:",
70 "-iterations 80 Maximum number of refining iterations (default 0 - only positional refinement).",
71 " ",
72 "Parameters for grid refinement:",
73 "-viewstep 2.5 Angular steps in 2 view directions (default 0 degrees).",
74 "-rotationstep 1.3 Angular step around view (default 0 degrees).",
75 "-accuracy 0.7 Angular refinement accuracy (default 0.5 degrees).",
76 " ",
77 "Output image parameters:",
78 "-rescale -0.1,5.2 Rescale data to average and standard deviation.",
79 " ",
80 "Input:",
81 "-parameters param.star Input parameter file.",
82 "-map file.map Input map file.",
83 "-mask mask.mrc Real space mask to be applied to template and subvolumes.",
84 "-Mask mask.mrc Mask for cross-correlation during refinement.",
85 " ",
86 "Output:",
87 "-output file.star Output model parameter file.",
88 "-construct file.pif Constructed map file.",
89 "-std file.pif Standard deviation map file.",
90 " ",
91 NULL
92 };
93 
94 int main(int argc, char **argv)
95 {
96  /* Initialize variables */
97  int reset(0); // Keep selection as read from file
98  Bstring calc_views; // Mode to calculate component views
99  int inv_views(0); // Flag to invert views for selected components
100  int type(1); // Component = 1, link = 2
101  Vector3<long> extract; // Size for extracting a part
102  Vector3<long> build; // Size to build a new map
103  int extract_average(0); // Average extracted densities
104  double fom_cutoff(0); // No selection based on FOM
105  DataType nudatatype(Unknown_Type); // Conversion to new type
106  Vector3<double> sam; // Map sampling
107  Vector3<double> origin; // New image origin
108  double hires(0), lores(1e6); // Limiting resolution range (hires must be > 0 to be set)
109  double max_shift(0); // Maximum shift allowed, default infinite
110  double max_view_angle(0); // Maximum rotation angle of view vector allowed
111  double max_rot_angle(0); // Maximum rotation angle around view vector allowed
112  int shift_flag(0); // Flag to shift only along normal (1) or perpendicular to it (2)
113  double bias(1); // Selection bias for first CC
114  int max_iter(0); // Maximum refining iterations, 0 means only positional
115  double viewstep(0), rotstep(0); // Angular refinement steps in degrees
116  double accuracy(0.5); // Accuracy in degrees
117  double linklength(1); // Default link length
118  int link_select(0); // Template selection for building
119  Bstring mod_select; // Model and component selection
120  int flags(0); // Flags to weigh during building (1) and build separate maps (2)
121  double nuavg(0), nustd(0); // Rescaling to average and stdev
122  Bstring astring;
123  Bstring* ct_names = NULL; // List of component names
124  Bstring refine_template; // Template for refinement
125 // Bstring comp_select("all");
126  Bstring paramfile; // Input parameter file
127  Bstring mapfile; // Input map file
128  Bstring maskfile; // Input real space mask file
129  Bstring fsmaskfile; // Input fourier space mask file
130  Bstring modelout; // Output model file
131  Bstring mapout; // Output map file
132  Bstring stdfile; // Output standard deviation map file
133 
134  int optind;
135  Boption* option = get_option_list(use, argc, argv, optind);
136  Boption* curropt;
137  for ( curropt = option; curropt; curropt = curropt->next ) {
138  if ( curropt->tag == "setviews" ) calc_views = curropt->value.lower();
139  if ( curropt->tag == "invert" ) inv_views = 1;
140  if ( curropt->tag == "type" ) {
141  if ( curropt->value[0] == 'c' || curropt->value[0] == 'C' ) type = 1;
142  if ( curropt->value[0] == 'l' || curropt->value[0] == 'L' ) type = 2;
143  }
144  if ( curropt->tag == "extract" )
145  extract = curropt->size();
146  if ( curropt->tag == "build" )
147  build = curropt->size();
148  if ( curropt->tag == "average" )
149  if ( ( extract_average = curropt->value.integer() ) < 1 )
150  cerr << "-average: An integer must be specified" << endl;
151  if ( curropt->tag == "all" ) reset = 1;
152  if ( curropt->tag == "select" )
153  mod_select = curropt->value;
154  if ( curropt->tag == "linkselect" )
155  if ( ( link_select = curropt->value.integer() ) < 0 )
156  cerr << "-linkselect: A number must be specified" << endl;
157  if ( curropt->tag == "fom" )
158  if ( ( fom_cutoff = curropt->value.real() ) < 0.001 )
159  cerr << "-fom: A FOM cutoff must be specified!" << endl;
160  if ( curropt->tag == "datatype" )
161  nudatatype = curropt->datatype();
162  if ( curropt->tag == "sampling" )
163  sam = curropt->scale();
164  if ( curropt->tag == "origin" )
165  origin = curropt->origin();
166  if ( curropt->tag == "resolution" )
167  if ( curropt->values(hires, lores) < 1 )
168  cerr << "-resolution: A high resolution limit must be specified" << endl;
169  if ( curropt->tag == "shift" )
170  if ( ( max_shift = curropt->value.real() ) < 0.001 )
171  cerr << "-shift: A maximum shift distance must be specified" << endl;
172  if ( curropt->tag == "viewangle" ) {
173  if ( ( max_view_angle = curropt->value.real() ) < 0.001 )
174  cerr << "-viewangle: A maximum rotation angle must be specified" << endl;
175  else
176  max_view_angle *= M_PI/180.0;
177  }
178  if ( curropt->tag == "rotationangle" ) {
179  if ( ( max_rot_angle = curropt->value.real() ) < 0.001 )
180  cerr << "-rotationangle: A maximum rotation angle must be specified" << endl;
181  else
182  max_rot_angle *= M_PI/180.0;
183  }
184  if ( curropt->tag == "bias" )
185  if ( ( bias = curropt->value.real() ) < 0.001 )
186  cerr << "-bias: A bias must be specified" << endl;
187  if ( curropt->tag == "normal" ) shift_flag = 1;
188  if ( curropt->tag == "lateral" ) shift_flag = 2;
189  if ( curropt->tag == "iterations" )
190  if ( ( max_iter = curropt->value.integer() ) < 1 )
191  cerr << "-iterations: A number of iterations must be specified!" << endl;
192  if ( curropt->tag == "viewstep" ) {
193  if ( ( viewstep = curropt->value.real() ) < 0.001 )
194  cerr << "-viewstep: An angle must be specified" << endl;
195  else
196  viewstep *= M_PI/180.0;
197  }
198  if ( curropt->tag == "rotationstep" ) {
199  if ( ( rotstep = curropt->value.real() ) < 0.001 )
200  cerr << "-rotationstep: An angle must be specified" << endl;
201  else
202  rotstep *= M_PI/180.0;
203  }
204  if ( curropt->tag == "accuracy" ) {
205  if ( ( accuracy = curropt->value.real() ) < 0.001 )
206  cerr << "-accuracy: An angle must be specified" << endl;
207  else
208  accuracy *= M_PI/180.0;
209  }
210  if ( curropt->tag == "linklength" )
211  if ( ( linklength = curropt->value.real() ) < 1 )
212  cerr << "-linklength: A link length must be specified" << endl;
213  if ( curropt->tag == "weigh" ) flags |= 1;
214  if ( curropt->tag == "separate" ) flags |= 2;
215  if ( curropt->tag == "componenttype" ) {
216  astring = curropt->value;
217  if ( astring.length() ) ct_names = astring.split(",");
218  }
219  if ( curropt->tag == "rescale" )
220  if ( curropt->values(nuavg, nustd) < 2 )
221  cerr << "-rescale: Both average and standard deviation must be specified!" << endl;
222  if ( curropt->tag == "refine" )
223  refine_template = curropt->filename();
224  if ( curropt->tag == "parameters" )
225  paramfile = curropt->filename();
226  if ( curropt->tag == "map" )
227  mapfile = curropt->filename();
228  if ( curropt->tag == "mask" )
229  maskfile = curropt->filename();
230  if ( curropt->tag == "Mask" )
231  fsmaskfile = curropt->filename();
232  if ( curropt->tag == "output" )
233  modelout = curropt->filename();
234  if ( curropt->tag == "construct" )
235  mapout = curropt->filename();
236  if ( curropt->tag == "std" )
237  stdfile = curropt->filename();
238  }
239  option_kill(option);
240 
241  double ti = timer_start();
242 
243  // Read all the parameter files
244  Bstring* file_list = NULL;
245  while ( optind < argc ) string_add(&file_list, argv[optind++]);
246  if ( !file_list ) {
247  cerr << "Error: No model files specified!" << endl;
248  bexit(-1);
249  }
250 
251  Bmodel* model = read_model(file_list, paramfile);
252  string_kill(file_list);
253 
254  if ( !model ) {
255  cerr << "Error: Input file not read!" << endl;
256  bexit(-1);
257  }
258 
260 
261  if ( mod_select.length() ) model_select(model, mod_select);
262 
263  if ( fom_cutoff > 0 ) model_fom_deselect(model, fom_cutoff);
264 
265  if ( calc_views.length() ) model_calculate_views(model, calc_views);
266 
267  if ( inv_views ) model_invert_views(model);
268 
269  Bimage* p = NULL;
270  Bimage* pn = NULL;
271  Bimage* pt = NULL;
272  Bimage* pm = NULL;
273  Bimage* pfsm = NULL;
274 
275  if ( mapfile.length() ) {
276  if ( type == 2 ) {
277  p = read_img(mapfile, 1, -1);
278  if ( p == NULL ) {
279  cerr << "Error: No image file read!" << endl;
280  bexit(-1);
281  }
282  }
283  }
284 
285  if ( type == 2 && !model->link )
286  model_link_list_generate(model, linklength);
287 
288  if ( refine_template.length() ) {
289  if ( mapfile.length() ) model->fmap = mapfile;
290  pt = read_img(refine_template, 1, -1);
291  if ( pt == NULL ) {
292  cerr << "Error: No image file read!" << endl;
293  bexit(-1);
294  }
295  if ( maskfile.length() ) pm = read_img(maskfile, 1, -1);
296  if ( fsmaskfile.length() ) pfsm = read_img(fsmaskfile, 1, -1);
297  if ( type == 1 ) {
298  model_refine_components(model, ct_names, pt, pm, pfsm, max_iter,
299  viewstep, rotstep, hires, lores, accuracy,
300  max_shift, max_view_angle, max_rot_angle, shift_flag);
301  } else if ( type == 2 ) {
302  model_refine_link_positions(model, pt, pm, pfsm, hires, lores,
303  max_shift, shift_flag, bias);
304  }
305 // if ( pt->n > 1 ) model_type_from_selection(model, ct_names, pt->file_name());
306  delete pt;
307  delete pm;
308  delete pfsm;
309  } else if ( ct_names ) {
310  astring = 0;
311 // model_type_from_selection(model, ct_names, astring);
312  }
313 
314  if ( extract.volume() ) {
315  if ( mapfile.length() ) model->fmap = mapfile;
316  if ( type == 1 ) {
317  if ( extract_average > 0 ) {
318  pn = model_average_component_density(model, extract, origin, extract_average);
319  if ( mapout.length() ) model_set_comptype_filenames(model, mapout);
320  } else {
321  pn = model_extract_component_densities(model, extract, origin);
322  }
323  } else if ( type == 2 ) {
324  pn = model_average_link_density(model, extract, origin);
325  }
326  origin = 0;
327  }
328 
329  if ( build.volume() ) {
330  if ( pn ) {
331  delete p;
332  p = pn;
333  }
334  if ( type == 1 ) {
335  if ( mapfile.length() ) model_set_comptype_filenames(model, mapfile);
336  pn = model_build_from_component_density(model, build, origin, flags);
337  } else if ( type == 2 ) {
338  pn = model_build_from_link_density(model, mapfile, build, origin, link_select, flags);
339  }
340  if ( mapout.length() ) model->fmap = mapout;
341  }
342 
343  if ( modelout.length() ) {
344  write_model(modelout, model);
345  }
346 
347  if ( pn ) pn->calculate_background();
348 
349 
350  // Write an output file if a file name is given
351  if ( mapout.length() ) {
352  if ( sam.volume() > 0 ) pn->sampling(sam);
353  if ( nustd > 0 ) pn->rescale_to_avg_std(nuavg, nustd);
354  pn->change_type(nudatatype);
355  write_img(mapout, pn, 0);
356  }
357 
358  // Write an standard deviation output file if a file name is given
359  if ( stdfile.length() && pn->next )
360  write_img(stdfile, pn->next, 0);
361 
362  model_kill(model);
363  delete p;
364  delete pn;
365 
366  if ( verbose & VERB_TIME )
367  timer_report(ti);
368 
369  bexit(0);
370 }
371 
Boption
A class for a command line input option.
Definition: options.h:26
model_invert_views
long model_invert_views(Bmodel *model)
Invert views.
Definition: model_views.cpp:109
model_refine_components
int model_refine_components(Bmodel *model, Bstring *ct_names, Bimage *ptemp, Bimage *pmask, Bimage *pfsmask, int max_iter, double viewstep, double rotstep, double hires, double lores, double accuracy, double max_shift, double max_view_angle, double max_rot_angle, int shift_flag)
Refines component views and positions by cross-correlation.
Definition: model_extract_build.cpp:300
link_select
Tcl_Obj * link_select(Bmodel *model, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bmodel.cpp:1491
model_build_from_link_density
Bimage * model_build_from_link_density(Bmodel *model, Bstring &linkmap, Vector3< long > size, Vector3< double > origin, int link_select, int flags)
Builds a new map from a density of a link in a model.
Definition: model_extract_build.cpp:1139
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
rwmodel.h
Header file for reading and writing atomic model files.
Bstring
Definition: Bstring.h:29
Bimage::next
Bimage * next
Definition: Bimage.h:179
model_extract_build.h
Functions to extract subvolumes and build new maps.
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Vector3< long >
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
model_average_link_density
Bimage * model_average_link_density(Bmodel *model, Vector3< long > size, Vector3< double > origin)
Extracts a density associated with each link in a model.
Definition: model_extract_build.cpp:917
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::calculate_background
int calculate_background(long nn, int flag)
Calculates the background for one sub-image.
Definition: Bimage_background.cpp:27
model_fom_deselect
long model_fom_deselect(Bmodel *model, double fom_cutoff)
Deselects components based on the FOM.
Definition: model_select.cpp:1154
Bmodel::link
Blink * link
Definition: rwmodel.h:133
Boption::origin
Vector3< double > origin()
Definition: options.h:180
main
int main(int argc, char **argv)
Definition: bxb.cpp:94
Bmodel::fmap
Bstring fmap
Definition: rwmodel.h:125
models_process
long models_process(Bmodel *model, long(modfunc)(Bmodel *))
Process a list of models using the specified function.
Definition: model_util.cpp:36
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
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
model_extract_component_densities
Bimage * model_extract_component_densities(Bmodel *model, Vector3< long > size, Vector3< double > origin)
Extracts all densities associated with components in a model.
Definition: model_extract_build.cpp:836
model_kill
int model_kill(Bmodel *model)
Deallocates all memory in the list.
Definition: rwmodel.cpp:1127
model_refine_link_positions
int model_refine_link_positions(Bmodel *model, Bimage *ptemp, Bimage *pmask, Bimage *pfsmask, double hires, double lores, double max_shift, int shift_flag, double bias)
Refines link positions by cross-correlation.
Definition: model_extract_build.cpp:559
Bimage
General image parameter class.
Definition: Bimage.h:177
model_select.h
Header file for reading and writing atomic model files.
timer.h
Utilities for timing functions.
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
model_calculate_views
long model_calculate_views(Bmodel *model, Bstring &mode)
Calculates the views associated with each component.
Definition: model_views.cpp:263
model_reset_selection
long model_reset_selection(Bmodel *model)
Resets the selection to all components.
Definition: model_select.cpp:232
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
Bmodel
Definition: rwmodel.h:119
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
write_model
int write_model(Bstring &filename, Bmodel *model)
Writes model parameters.
Definition: rwmodel.cpp:92
Boption::size
Vector3< long > size()
Definition: options.h:163
Bstring::length
long length() const
Definition: Bstring.cpp:263
model_average_component_density
Bimage * model_average_component_density(Bmodel *model, Vector3< long > size, Vector3< double > origin, int npt)
Averages the density associated with each component type in a model.
Definition: model_extract_build.cpp:703
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:76
model_views.h
Library routines used for analysing model component views.
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
model
Bmodel * model
Definition: bshow.cpp:34
Vector3::volume
double volume()
Definition: Vector3.h:237
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
model_util.h
Library routines used for model processing.
read_model
Bmodel * read_model(Bstring &filename)
Definition: rwmodel.cpp:29
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
model_set_comptype_filenames
int model_set_comptype_filenames(Bmodel *model, Bstring &filename)
Sets the filenames of all selected component types to the given string.
Definition: rwmodel.cpp:1222
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
use
const char * use[]
Definition: bxb.cpp:23
verbose
int verbose
Bstring::lower
Bstring lower()
Definition: Bstring.cpp:316
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:713
model_build_from_component_density
Bimage * model_build_from_component_density(Bmodel *model, Vector3< long > size, Vector3< double > origin, int flags)
Builds a new map from a densities of components in a model.
Definition: model_extract_build.cpp:1008
model_select
long model_select(Bmodel *model, Bstring &comptype)
Selects models, components and component types.
Definition: model_select.cpp:36
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