Bsoft  2.1.0
Bernard's software package
bshell.cpp
Go to the documentation of this file.
1 
9 #include "rwmodel.h"
10 #include "model_create.h"
11 #include "model_shell.h"
12 #include "model_map.h"
13 #include "model_select.h"
14 #include "model_transform.h"
15 #include "model_views.h"
16 #include "model_links.h"
17 #include "model_util.h"
18 #include "utilities.h"
19 #include "options.h"
20 #include "timer.h"
21 
22 // Declaration of global variables
23 extern int verbose; // Level of output to the screen
24 
25 
26 /* Usage assistance */
27 const char* use[] = {
28 " ",
29 "Usage: bshell [options] in1.star [in2.star ...]",
30 "-----------------------------------------------",
31 "Generates and operates on shell models.",
32 " ",
33 "Actions for creation action:",
34 "-shell 134.2 Generate a shell with the given radius.",
35 "-dodecahedron 215,3,0.3 Generate an dodecahedral shell with the given radius and divisions,",
36 " and a fraction to make it spherical.",
37 "-icosahedron 238.5,3,0.5 Generate an icosahedral shell with the given radius and divisions,",
38 " and a fraction to make it spherical.",
39 "-circle 527,243 Generate a circle with the given radius and z offset.",
40 "-ellipse 620.5,314.9,5.2 Generate an ellipse with the given x and y semi-axes lengths and z offset.",
41 "-ellipsoid 24.3,51.2,68 Generate an ellipsoid with the given x,y,z semi-axes.",
42 "-cylinder 0,1,0,88.3,155 Generate a cylinder with the given direction, radius and length.",
43 "-add -14.7 Add a shell offset by a radial distance from selected components.",
44 "-fromcomponents 18.6,1 Generate shells from components with given radius and flag for 2D.",
45 " ",
46 "Actions:",
47 "-merge Merge models into one after all other operations.",
48 "-guide poly.star Input guide polyhedron to adjust the shell model.",
49 "-linklength 56.3 Generate links with this maximum length for the sphere model.",
50 "-views local Calculate views for components. Modes: origin: current origin,",
51 " com: center-of-mass origin, map: map origin, local: neigbor plane.",
52 "-rdf 1.2 Calculate the radial distribution function with the given sampling (angstrom).",
53 "-fit 15.6,200,1 Fits a shell model to a map using the given resolution limits,",
54 " and a flag to indicate negative contrast.",
55 "-radial Calculate a radial profile based on a shell model.",
56 "-powerspectrum out.map Calculate 2D power spectra around components in the shell.",
57 " ",
58 "Selections:",
59 "-all Reset selection to all models and components before other selections.",
60 "-select #Mod1@14 Select models and components.",
61 " ",
62 "Parameters:",
63 "-verbose 7 Verbose output.",
64 "-id model1 Set model identifier.",
65 "-newtype MEM Type for all new components.",
66 "-componentradius 0.5 Component display radius.",
67 "-linkradius 0.5 Link display radius.",
68 " ",
69 "Parameters for generating a model:",
70 "-points 152 Number of points.",
71 "-separation 2.5 Separation distance between points.",
72 " ",
73 "Parameter for adjusting to a guide model:",
74 "-fraction 0.6 Fraction of adjustment to a guide model (default 1).",
75 "-curved Flag to indicate curved hull or shell surface (default not).",
76 " ",
77 "Parameters for power spectra:",
78 "-size 45,40,20 Size of densities to extract (default 20,20,20 voxels).",
79 "-origin 0.8,-10,15.7 Set the origin for extracted densities.",
80 "-fftsize 256 Size of power spectrum (default from images).",
81 "-resolution 20,360 High and low resolution limits for cross-correlation (angstrom).",
82 "-annuli 2,95 Real space annular limits (default 0,inf pixels).",
83 " ",
84 "Input:",
85 "-parameters param.star Input parameter file.",
86 "-map file.map Input map file to associate with shell model.",
87 " ",
88 "Output:",
89 "-output file.star Output model parameter 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  int merge(0); // Flag to merge models rather than concatenate
99  double shell_radius(-1); // Radius of sphere
100  int dod_divisions(0); // Number of divisions for dodecahedral sphere
101  int ico_divisions(0); // Number of divisions for icosahedral sphere
102  double sphere_fraction(0); // Fraction for spherical shape
103  double circle_radius(0); // Circle radius
104  double circle_offset(0); // Offset of circle in z
105  Vector3<double> ellipse_semi_z; // Ellipse semi-axes and offset in z
106  Vector3<double> ellipsoid_semi; // Ellipsoid semi-axes
107  Vector3<double> cyl_dir; // Cylinder direction
108  double cyl_rad(0), cyl_len(0); // Cylinder radius and length
109  double add_distance(0); // Distance for adding a new shell
110  double comp2shell(0), twod(0); // Distance for converting components to shells
111  double linklength(0); // Link length for generating links
112  Bstring calc_views; // Mode to calculate component views
113  double rdf_interval(0); // Calculate RDF at this sampling (0=not)
114  int fit(0); // Flag to indicate fitting a shell to a map
115  int radial(0); // Flag to calculate radial profile
116  Bstring mod_select; // Model and component selection
117  double hires(0), lores(1e6); // Limiting resolution for fitting to a map
118  long ann_min(0); // Minimum annulus
119  long ann_max(1000); // Maximum annulus, reset to maximum radius in image
120  int neg_flag(0); // Flag to indicate negative contrast
121  long points(0); // Number of points on the sphere
122  Bstring model_id; // Model identifier
123  Bstring nutype; // New component type to set
124  double compradius(0); // Component display radius
125  double linkradius(0); // Link display radius
126  double separation(0); // Distance between points
127  double guide_fraction(1); // Fraction to adjust to the guide polyhedron
128  int curv_flag(0); // Adjust to guide with curved surfaces
129  Vector3<long> size(20,20,20); // Size of densities to extract
130  Vector3<double> origin; // Origin for extracted densities
131  int ft_size(0); // Power spectrum size
132  Bstring atom_select("all");
133  Bstring paramfile; // Input parameter file name
134  Bstring mapfile; // Input map file name
135  Bstring guidefile; // Guide model file
136  Bstring outfile; // Output model parameter file name
137  Bstring outps; // Output power spectrum file name
138 
139  int optind;
140  Boption* option = get_option_list(use, argc, argv, optind);
141  Boption* curropt;
142  for ( curropt = option; curropt; curropt = curropt->next ) {
143  if ( curropt->tag == "merge" ) merge = 1;
144  if ( curropt->tag == "shell" )
145  if ( ( shell_radius = curropt->value.real() ) < 0.001 )
146  cerr << "-shell: The radius must be specified!" << endl;
147  if ( curropt->tag == "dodecahedron" ) {
148  if ( curropt->values(shell_radius, dod_divisions, sphere_fraction) < 1 )
149  cerr << "-dodecahedron: The radius and divisions must be specified!" << endl;
150  else if ( dod_divisions < 1 ) dod_divisions = 1;
151  }
152  if ( curropt->tag == "icosahedron" ) {
153  if ( curropt->values(shell_radius, ico_divisions, sphere_fraction) < 1 )
154  cerr << "-icosahedron: The radius and divisions must be specified!" << endl;
155  else if ( ico_divisions < 1 ) ico_divisions = 1;
156  }
157  if ( curropt->tag == "circle" )
158  if ( curropt->values(circle_radius, circle_offset) < 1 )
159  cerr << "-circle: The circle radius must be specified!" << endl;
160  if ( curropt->tag == "ellipse" ) {
161  ellipse_semi_z = curropt->vector3();
162  if ( ellipse_semi_z.length() < 1 )
163  cerr << "-ellipse: The major and minor semiaxes must be specified!" << endl;
164  }
165  if ( curropt->tag == "ellipsoid" ) {
166  ellipsoid_semi = curropt->vector3();
167  if ( ellipsoid_semi.volume() < 1 )
168  cerr << "-ellipsoid: The 3 semiaxes must be specified!" << endl;
169  }
170  if ( curropt->tag == "cylinder" ) {
171  vector<double> d = curropt->value.split_into_doubles(",");
172  if ( d.size() < 5 )
173  cerr << "-cylinder: All 5 parameters must be specified!" << endl;
174  else {
175  cyl_dir = Vector3<double>(d[0], d[1], d[2]);
176  cyl_rad = d[3];
177  cyl_len = d[4];
178  }
179  }
180  if ( curropt->tag == "add" )
181  if ( ( add_distance = curropt->value.real() ) < 0.001 )
182  cerr << "-add: A distance must be specified!" << endl;
183  if ( curropt->tag == "fromcomponents" )
184  if ( curropt->values(comp2shell, twod) < 1 )
185  cerr << "-fromcomponents: A distance must be specified!" << endl;
186  if ( curropt->tag == "guide" )
187  guidefile = curropt->filename();
188  if ( curropt->tag == "linklength" )
189  if ( ( linklength = curropt->value.real() ) < 1 )
190  cerr << "-linklength: A link length must be specified!" << endl;
191  if ( curropt->tag == "views" ) calc_views = curropt->value.lower();
192  if ( curropt->tag == "rdf" )
193  if ( ( rdf_interval = curropt->value.real() ) < 0.1 )
194  cerr << "-rdf: A sampling interval must be specified!" << endl;
195  if ( curropt->tag == "fit" ) {
196  if ( curropt->values(hires, lores, neg_flag) < 1 )
197  cerr << "-fit: A high resolution limit must be specified!" << endl;
198  else
199  fit = 1;
200  }
201  if ( curropt->tag == "radial" ) radial = 1;
202  if ( curropt->tag == "all" ) reset = 1;
203  if ( curropt->tag == "select" )
204  mod_select = curropt->value;
205  if ( curropt->tag == "id" ) {
206  model_id = curropt->value;
207  if ( model_id.length() < 1 )
208  cerr << "-id: An identifier must be specified!" << endl;
209  }
210  if ( curropt->tag == "newtype" ) {
211  nutype = curropt->value;
212  if ( nutype.length() < 1 )
213  cerr << "-newtype: An type identifier must be specified!" << endl;
214  }
215  if ( curropt->tag == "componentradius" )
216  if ( ( compradius = curropt->value.real() ) < 1 )
217  cerr << "-componentradius: The component display radius must be specified!" << endl;
218  if ( curropt->tag == "linkradius" )
219  if ( ( linkradius = curropt->value.real() ) < 1 )
220  cerr << "-linkradius: The link display radius must be specified!" << endl;
221  if ( curropt->tag == "points" )
222  if ( ( points = curropt->value.integer() ) < 1 )
223  cerr << "-points: The number of points must be specified!" << endl;
224  if ( curropt->tag == "separation" )
225  if ( ( separation = curropt->value.real() ) < 0.001 )
226  cerr << "-separation: The separation distance must be specified!" << endl;
227  if ( curropt->tag == "fraction" )
228  if ( ( guide_fraction = curropt->value.real() ) < 0.001 )
229  cerr << "-fraction: A fraction must be specified!" << endl;
230  if ( curropt->tag == "curved" ) curv_flag = 1;
231  if ( curropt->tag == "size" )
232  size = curropt->size();
233  if ( curropt->tag == "origin" )
234  origin = curropt->origin();
235  if ( curropt->tag == "fftsize" )
236  if ( ( ft_size = curropt->value.integer() ) < 1 )
237  cerr << "-fftsize: A Fourier transform size must be specified!" << endl;
238  if ( curropt->tag == "resolution" ) {
239  if ( curropt->values(hires, lores) < 1 )
240  cerr << "-resolution: At least one resolution limit must be specified!" << endl;
241  else
242  if ( lores < hires ) swap(hires, lores);
243  }
244  if ( curropt->tag == "annuli" )
245  if ( curropt->values(ann_min, ann_max) < 1 )
246  cerr << "-annuli: At least a minimum annulus must be specified!" << endl;
247  if ( curropt->tag == "parameters" )
248  paramfile = curropt->filename();
249  if ( curropt->tag == "map" )
250  mapfile = curropt->filename();
251  if ( curropt->tag == "output" )
252  outfile = curropt->filename();
253  if ( curropt->tag == "powerspectrum" )
254  outps = curropt->filename();
255  }
256  option_kill(option);
257 
258  double ti = timer_start();
259 
260  Bstring* file_list = NULL;
261  Bmodel* model = NULL;
262  Bmodel* mp = NULL;
263  Bmodel* sph = NULL;
264  Bmodel* guide = NULL;
265 
266  // Read all the model parameter files
267  while ( optind < argc ) string_add(&file_list, argv[optind++]);
268 
269  if ( file_list ) {
270  model = read_model(file_list);
271  string_kill(file_list);
272  if ( !model ) {
273  cerr << "Error: Input file not read!" << endl;
274  bexit(-1);
275  }
276  if ( reset )
277  for ( mp = model; mp; mp = mp->next )
279  }
280 
281  if ( guidefile.length() ) guide = read_model(guidefile, paramfile);
282 
283  sph = model;
284 
285  if ( shell_radius >= 0 ) {
286  if ( dod_divisions > 0 )
287  sph = model_create_dodecahedron(shell_radius, dod_divisions, sphere_fraction);
288  else if ( ico_divisions > 0 )
289  sph = model_create_icosahedron(shell_radius, ico_divisions, sphere_fraction);
290  else
291  sph = model_create_shell(points, shell_radius, separation);
292  } else if ( circle_radius > 0) {
293  sph = model_create_circle(circle_radius, circle_offset, separation);
294  } else if ( ellipse_semi_z[0] > 0 && ellipse_semi_z[1] > 0) {
295  sph = model_create_ellipse(ellipse_semi_z, separation);
296  } else if ( ellipsoid_semi.volume() > 0) {
297  sph = model_create_ellipsoid(ellipsoid_semi, separation);
298  } else if ( cyl_len > 0 ) {
299  sph = model_create_cylinder(cyl_dir, cyl_rad, cyl_len, separation);
300  }
301 
302  if ( !sph ) {
303  cerr << "Error: Ellipsoid model not generated!" << endl;
304  bexit(-1);
305  }
306 
307  if ( !model ) model = sph;
308  else if ( model != sph ) {
309  for ( mp = model; mp->next; mp = mp->next ) ;
310  mp->next = sph;
311  }
312 
313  if ( reset ) model_reset_selection(sph);
314 
315  if ( mod_select.length() ) model_select(sph, mod_select);
316 
317  if ( nutype.length() ) model_set_type(sph, nutype);
318 
319  if ( guide ) model_align_to_guide(sph, guide);
320 
321  if ( model_id.length() ) sph->id = model_id;
322 
323  if ( add_distance ) model_add_shell(sph, add_distance, nutype);
324 
325  if ( comp2shell ) {
326  sph = model_components_to_shells(model, comp2shell, nutype, twod);
327  model_kill(model);
328  model = sph;
329  }
330 
331  if ( mapfile.length() ) sph->fmap = mapfile;
332 
333  if ( linklength > 0 ) model_link_list_generate(sph, linklength);
334 
335  if ( calc_views.length() ) model_calculate_views(sph, calc_views);
336 
337  if ( rdf_interval > 0 ) model_radial_distribution(sph, rdf_interval);
338 
339  if ( fit ) {
340  if ( sph->fmap.length() < 2 ) sph->fmap = model->fmap;
341  model_shell_fit(sph, hires, lores, neg_flag);
342  }
343 
344  if ( guide && guide_fraction > 0 )
345  model_adjust_shell_to_guide(sph, guide, guide_fraction, curv_flag);
346 
347  if ( radial ) model_shell_radial_profile(sph);
348 
349  Bimage* p = NULL;
350  if ( outps.length() ) {
351  p = model_shell_power_spectrum(sph, size, origin, ft_size, ann_min, ann_max, hires, lores);
352  write_img(outps, p, 0);
353  delete p;
354  }
355 
356  if ( compradius ) model_set_component_radius(sph, compradius);
357 
358  if ( linkradius ) model_set_link_radius(sph, linkradius);
359 
360  if ( merge ) model_merge(model);
361 
363 
364  // Write an output parameter format file if a name is given
365  if ( outfile.length() ) {
366  write_model(outfile, model);
367  }
368 
369  model_kill(model);
370 
371  if ( verbose & VERB_TIME )
372  timer_report(ti);
373 
374  bexit(0);
375 }
376 
Boption
A class for a command line input option.
Definition: options.h:26
model_create_ellipsoid
Bmodel * model_create_ellipsoid(Vector3< double > axes, double distance)
Creates an ellipsoid with given semiaxes and component separation.
Definition: model_create.cpp:1011
main
int main(int argc, char **argv)
Definition: bshell.cpp:94
model_align_to_guide
long model_align_to_guide(Bmodel *model, Bmodel *guide)
A model is oriented to coincide with a guide model.
Definition: model_transform.cpp:443
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
model_shell_power_spectrum
Bimage * model_shell_power_spectrum(Bmodel *model, Vector3< long > size, Vector3< double > origin, int ft_size, int ann_min, int ann_max, double hires, double lores)
Calculates a radial profile from a shell model.
Definition: model_map.cpp:344
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::next
Boption * next
Definition: options.h:28
Vector3< double >
model_shell_fit
int model_shell_fit(Bmodel *model, double hires, double lores, int neg)
Fits a shell model as a rigid body to a map.
Definition: model_map.cpp:192
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
model_map.h
Header file for model to map conversions.
Bmodel::next
Bmodel * next
Definition: rwmodel.h:120
model_add_shell
long model_add_shell(Bmodel *model, double add_distance, Bstring &new_type)
Adds a shell to a model using selected components.
Definition: model_shell.cpp:32
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.
model_create_circle
Bmodel * model_create_circle(double radius, double z, double distance)
Creates a circle with the given radius, height, and component separation.
Definition: model_create.cpp:897
model_merge
long model_merge(Bmodel *model)
Merges components from all models into one.
Definition: model_util.cpp:299
model_radial_distribution
long model_radial_distribution(Bmodel *model, double interval)
Calculates the radial distribution function of a model.
Definition: model_util.cpp:605
Boption::origin
Vector3< double > origin()
Definition: options.h:180
Bmodel::fmap
Bstring fmap
Definition: rwmodel.h:125
model_selection_stats
long model_selection_stats(Bmodel *model)
Calculates selection statistics.
Definition: model_select.cpp:870
Boption::tag
Bstring tag
Definition: options.h:29
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
verbose
int verbose
model_create.h
Library routines used for model creation.
Bmodel::id
Bstring id
Definition: rwmodel.h:121
Boption::value
Bstring value
Definition: options.h:30
model_kill
int model_kill(Bmodel *model)
Deallocates all memory in the list.
Definition: rwmodel.cpp:1127
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.
model_create_ellipse
Bmodel * model_create_ellipse(Vector3< double > axes, double distance)
Creates an ellipse with given semiaxes and component separation.
Definition: model_create.cpp:947
use
const char * use[]
Definition: bshell.cpp:27
model_calculate_views
long model_calculate_views(Bmodel *model, Bstring &mode)
Calculates the views associated with each component.
Definition: model_views.cpp:263
Bstring::split_into_doubles
vector< double > split_into_doubles(const Bstring &delim)
Definition: Bstring.cpp:673
model_reset_selection
long model_reset_selection(Bmodel *model)
Resets the selection to all components.
Definition: model_select.cpp:232
model_create_dodecahedron
Bmodel * model_create_dodecahedron(double radius, long divisions, double sphere_fraction)
Creates an dodecahedron point model.
Definition: model_create.cpp:675
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
Bmodel
Definition: rwmodel.h:119
model_adjust_shell_to_guide
int model_adjust_shell_to_guide(Bmodel *model, Bmodel *gmod, double fraction, int curv_flag)
Adjusts a shell model to the faces of a polyhedral guide model.
Definition: model_shell.cpp:85
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
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
model_create_cylinder
Bmodel * model_create_cylinder(Vector3< double > direction, double radius, double length, double distance)
Creates a cylinder with the given radius, length, and component separation.
Definition: model_create.cpp:1101
Boption::vector3
Vector3< double > vector3()
Definition: options.h:148
Bstring::length
long length() const
Definition: Bstring.cpp:263
model_components_to_shells
Bmodel * model_components_to_shells(Bmodel *model, double distance, Bstring &nutype, int twod)
Generates a new set of models by vonverting each component to a shell model.
Definition: model_shell.cpp:139
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.
merge
int merge(Bimage *p, int padd)
Pads an image and shifts it to the middle.
Definition: sin_gal.cpp:270
model
Bmodel * model
Definition: bshow.cpp:34
Vector3::volume
double volume()
Definition: Vector3.h:237
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
model_transform.h
Library routines used for model transformation.
model_util.h
Library routines used for model processing.
read_model
Bmodel * read_model(Bstring &filename)
Definition: rwmodel.cpp:29
model_set_type
int model_set_type(Bmodel *model, Bstring &set_type)
Reset the component types.
Definition: rwmodel.cpp:519
model_set_component_radius
long model_set_component_radius(Bmodel *model, double comprad)
Set the display radius for all components to a specific value.
Definition: rwmodel.cpp:1247
Vector3::length
double length()
Definition: Vector3.h:222
model_create_icosahedron
Bmodel * model_create_icosahedron(double radius, long divisions, double sphere_fraction)
Creates an icosahedron point model.
Definition: model_create.cpp:732
model_shell_radial_profile
int model_shell_radial_profile(Bmodel *model)
Calculates a radial profile from a shell model.
Definition: model_map.cpp:240
model_shell.h
Library routines used for shell model processing.
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_create_shell
Bmodel * model_create_shell(long number, double radius, double distance)
Creates a sphere point model.
Definition: model_create.cpp:472
model_select
long model_select(Bmodel *model, Bstring &comptype)
Selects models, components and component types.
Definition: model_select.cpp:36