Bsoft  2.1.0
Bernard's software package
bhelix.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "linked_list.h"
11 #include "options.h"
12 #include "utilities.h"
13 #include "timer.h"
14 
15 
16 // Declaration of global variables
17 extern int verbose; // Level of output to the screen
18 
19 
20 // Usage assistance
21 const char* use[] = {
22 " ",
23 "Usage: bhelix [options] input.img output.img",
24 "--------------------------------------------",
25 "Symmetrizes and projects images with helical symmetry.",
26 " ",
27 "Actions:",
28 "-normalize Normalize symmetrized output, use for maps close to symmetry (default not).",
29 "-background Calculate new background values.",
30 "-project 20 Generate projections with this angular step size (over 360 degrees).",
31 "-kernel 6,2 Reciprocal space projection: kernel size and power.",
32 "-elliptical 37,5.5 Elliptical distortion: major axis angle and shift (pixels).",
33 "-rescale -0.1,5.2 Rescale data to average and standard deviation.",
34 " ",
35 "Actions for symmetrizing:",
36 "-helix 25.3,67.1,2 Symmetrize: Helical rise and angle, and dyad axis indicated by 2.",
37 " (default 10 angstrom, 45 degrees, no dyad axis).",
38 "-rule 237.5,-2,7 Symmetrize by selection rule: Helical repeat (angstrom), turns and subunits per repeat.",
39 "-super 25.3,67.1,22,15.5 Superhelix: Helical rise and angle, and offset in xy plane.",
40 "-seam 1.5 Symmetrize with the given subunit shift along the seam.",
41 " ",
42 "Actions for determining helical parameters:",
43 "-findrs 5,8,0.1,10,20,1 Find helical parameters in real space:",
44 " rise start, end and increment, rotation angle start, end and increment.",
45 "-findcc 10,22.4,1.5 Find helical parameters by cross-correlation:",
46 " rotation angle start, end and increment.",
47 "-segment 10,22.4,1.5,18 Correlate helical segments: rotation angle start,",
48 " end, increment and segment thickness.",
49 " ",
50 "Parameters:",
51 "-verbose 1 Verbosity of output.",
52 "-datatype u Force writing of a new data type.",
53 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; a single value can be given).",
54 "-origin 110,50,44 Origin for helix (default 0,0,0 or from image).",
55 "-zlimits 8,47 Range of slices along the helical axis to use (default 0,inf).",
56 "-radius 22 Radial limit for symmetrization and finding parameters (default from image).",
57 "-resolution 15.6,200 Resolution limits for projection and cross-correlation (default 0).",
58 "-bin 2 Binning for finding parameters (default 1).",
59 " ",
60 "Output:",
61 "-Postscript file.ps Postscript output file name.",
62 " ",
63 NULL
64 };
65 
66 int main(int argc, char **argv)
67 {
68  // Initialize variables
69  DataType nudatatype(Unknown_Type); // Conversion to new type
70  int set_sampling(0);
71  Vector3<double> sam; // Units for the three axes (A/pixel)
72  double helix_rise(0); // Rise per asymmetric unit
73  double helix_angle(0); // Rotation angle per asymmetric unit
74  int dyad_axis(1); // No dyad axis
75  double helix_repeat(0); // Helical repeat
76  int turns(0), subunits(0); // Turns and subunits per repeat
77  double seam_shift(0); // Shift along seam (0=no seam)
78  Vector3<double> offset; // Superhelix offset
79  double project_angle(0); // Projection angular step size
80  int kernel_width(0); // Reciprocal space kernel width
81  int kernel_power(2); // Reciprocal space kernel power
82  double rise_start(0), rise_end(0); // Rise range for finding parameters
83  double rise_step(0); // Rise increment for finding parameters
84  double angle_start(0), angle_end(0); // Angle range for finding parameters
85  double angle_step(0); // Angle increment for finding parameters
86  int thickness(0); // Segment thickness for correlation
87  int find_bin(1); // Binning for searching
88  double hires(0), lores(0); // Resolution limits for cross-correlation
89  double nuavg(0), nustd(0); // For rescaling
90  Vector3<double> origin; // Helix origin
91  int set_origin(0); // Flag to set origin
92  int calc_background(0); // Flag to calculate background values
93  int norm_flag(0); // Normalization flag
94  int zmin(0), zmax(1000000); // Limits along the helical axis
95  double radius(0); // Radial limit for symmetrization
96  double ell_angle(0), ell_shift(0); // Elliptical distortion parameters
97  Bstring ps_file; // Postscript output file name
98 
99  int optind;
100  Boption* option = get_option_list(use, argc, argv, optind);
101  Boption* curropt;
102  for ( curropt = option; curropt; curropt = curropt->next ) {
103  if ( curropt->tag == "datatype" )
104  nudatatype = curropt->datatype();
105  if ( curropt->tag == "sampling" ) {
106  sam = curropt->scale();
107  set_sampling = 1;
108  }
109  if ( curropt->tag == "helix" ) {
110  if ( curropt->values(helix_rise, helix_angle, dyad_axis) < 2 )
111  cerr << "-helix: Both rise and angle must be specified" << endl;
112  else
113  helix_angle *= M_PI/180.0;
114  }
115  if ( curropt->tag == "rule" )
116  if ( curropt->values(helix_repeat, turns, subunits) < 3 )
117  cerr << "-rule: Repeat, turns and subunits must be specified" << endl;
118  if ( curropt->tag == "super" ) {
119  if ( curropt->values(helix_rise, helix_angle, offset[0], offset[1]) < 3 )
120  cerr << "-super: Rise, angle and offset must be specified" << endl;
121  else
122  helix_angle *= M_PI/180.0;
123  }
124  if ( curropt->tag == "seam" )
125  if ( ( seam_shift = curropt->value.real() ) < 0.1 )
126  cerr << "-seam: A unit shift must be specified" << endl;
127  if ( curropt->tag == "project" ) {
128  if ( ( project_angle = curropt->value.real() ) < 0.1 )
129  cerr << "-project: A projection angle step must be specified" << endl;
130  else
131  project_angle *= M_PI/180.0;
132  }
133  if ( curropt->tag == "kernel" )
134  if ( curropt->values(kernel_width, kernel_power) < 1 )
135  cerr << "-kernel: At least the kernel size must be specified!" << endl;
136  if ( curropt->tag == "findcc" )
137  if ( curropt->values(angle_start, angle_end, angle_step) < 3 )
138  cerr << "-findcc: All three angles must be specified" << endl;
139  if ( curropt->tag == "findrs" ) {
140  vector<double> d = curropt->value.split_into_doubles(",");
141  if ( d.size() < 6 )
142  cerr << "-findrs: All six rise and angle parameters must be specified" << endl;
143  else {
144  rise_start = d[0];
145  rise_end = d[1];
146  rise_step = d[2];
147  angle_start = d[3];
148  angle_end = d[4];
149  angle_step = d[5];
150  }
151  }
152  if ( curropt->tag == "segment" )
153  if ( curropt->values(angle_start, angle_end, angle_step, thickness) < 4 )
154  cerr << "-segment: All three angles and a thickness must be specified!" << endl;
155  if ( curropt->tag == "elliptical" ) {
156  if ( curropt->values(ell_angle, ell_shift) < 2 )
157  cerr << "-elliptical: Both major axis angle and shift must be specified!" << endl;
158  else
159  ell_angle *= M_PI/180.0;
160  }
161  if ( curropt->tag == "rescale" )
162  if ( curropt->values(nuavg, nustd) < 2 )
163  cerr << "-rescale: Both average and standard deviation must be specified!" << endl;
164  if ( curropt->tag == "background" )
165  calc_background = 1;
166  if ( curropt->tag == "origin" ) {
167  if ( curropt->value[0] == 'c' ) {
168  set_origin = 2;
169  } else {
170  origin = curropt->origin();
171  set_origin = 1;
172  }
173  }
174  if ( curropt->tag == "zlimits" )
175  if ( curropt->values(zmin, zmax) < 2 )
176  cerr << "-zlimits: Both limits on the helical axis must be specified" << endl;
177  if ( curropt->tag == "normalize" )
178  norm_flag = 1;
179  if ( curropt->tag == "radius" )
180  if ( ( radius = curropt->value.real() ) < 1 )
181  cerr << "-radius: A radius must be specified" << endl;
182  if ( curropt->tag == "resolution" )
183  if ( curropt->values(hires, lores) < 1 )
184  cerr << "-resolution: Resolution limits must be specified!" << endl;
185  if ( curropt->tag == "bin" )
186  if ( ( find_bin = curropt->value.integer() ) < 1 )
187  cerr << "-bin: A bin value must be specified!" << endl;
188  if ( curropt->tag == "Postscript" )
189  ps_file = curropt->filename();
190  }
191  option_kill(option);
192 
193  angle_start *= M_PI/180.0;
194  angle_end *= M_PI/180.0;
195  angle_step *= M_PI/180.0;
196 
197  double ti = timer_start();
198 
199  Bstring symmetry_string("H");
200  Bsymmetry* sym = init_point_group_symmetry(symmetry_string);
201  View* views = NULL;
202  Bplot* plot = NULL;
203 
204  FSI_Kernel* kernel = NULL;
205  if ( kernel_width > 1 )
206  kernel = new FSI_Kernel(kernel_width, kernel_power);
207 
208  int dataflag = 1;
209  Bimage* p = read_img(argv[optind++], dataflag, -1);
210  Bimage* proj = NULL;
211 
212  if ( !p ) {
213  cerr << "Error: No input file!" << endl;
214  bexit(-1);
215  }
216 
217  if ( set_origin ) {
218  if ( set_origin == 2 ) p->origin(p->default_origin());
219  else p->origin(origin);
220  }
221 
222  if ( set_sampling ) p->sampling(sam);
223 
224  if ( calc_background ) p->calculate_background();
225 
226  if ( subunits > 0 ) {
227  helix_rise = helix_repeat*1.0L/subunits;
228  helix_angle = TWOPI*turns*1.0L/subunits;
229  }
230 
231  if ( helix_rise && helix_angle ) {
232  if ( offset[0] ) p->convert_to_helix(helix_rise, helix_angle, offset);
233  else if ( seam_shift ) plot = p->seamed_helix_symmetrize(helix_rise, helix_angle,
234  seam_shift, dyad_axis, zmin, zmax, radius, norm_flag);
235  else p->helix_symmetrize(helix_rise, helix_angle,
236  dyad_axis, zmin, zmax, radius, norm_flag);
237  } else if ( p->sizeZ() > 1 && project_angle ) {
238  views = asymmetric_unit_views(sym, TWOPI, project_angle, 0);
239  if ( kernel )
240  proj = p->project(views, hires, kernel);
241  else
242  proj = p->project(views, 1);
243  kill_list((char *) views, sizeof(View));
244  delete p;
245  p = proj;
246  }
247 
248  if ( angle_step > 0 ) {
249  if ( thickness > 0 ) {
250  plot = p->helix_segment_correlation(thickness,
251  angle_start, angle_end, angle_step,
252  find_bin, hires, lores, radius);
253  } else if ( rise_step > 0 ) {
254  plot = p->find_helix_parameters(rise_start, rise_end, rise_step,
255  angle_start, angle_end, angle_step, find_bin, radius);
256  } else {
257  plot = p->find_helix_parameters(angle_start, angle_end, angle_step,
258  find_bin, hires, lores, radius);
259  }
260  }
261 
262  if ( ell_shift )
263  p->distort_elliptically(ell_angle, ell_shift);
264 
265  if ( ps_file.length() && plot ) ps_plot(ps_file, plot);
266 
267  delete plot;
268 
269  if ( optind < argc ) {
270  if ( nustd > 0 ) p->rescale_to_avg_std(nuavg, nustd);
271  p->change_type(nudatatype);
272  write_img(argv[optind], p, 0);
273  }
274 
275  delete p;
276  kill_symmetry(sym);
277  if ( kernel ) delete kernel;
278 
279  if ( verbose & VERB_TIME )
280  timer_report(ti);
281 
282  bexit(0);
283 }
284 
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
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
Bimage::seamed_helix_symmetrize
Bplot * seamed_helix_symmetrize(double helix_rise, double helix_angle, double seam_shift, int dyad_axis, int zmin, int zmax, double radius, int norm_flag)
Symmetrizes an image given helical symmetry parameters and seam shift parameter.
Definition: Bimage_helix.cpp:280
Bimage::helix_symmetrize
double helix_symmetrize(double helix_rise, double helix_angle, int dyad_axis, int zmin, int zmax, double radius, int norm_flag=1)
Symmetrizes an image given helical symmetry parameters.
Definition: Bimage_helix.cpp:133
kill_list
long kill_list(char *list, unsigned long size)
Frees all the items in a linked list.
Definition: linked_list.cpp:214
Bstring
Definition: Bstring.h:29
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< double >
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
Bimage::default_origin
Vector3< double > default_origin()
Definition: Bimage.h:592
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.
Bimage::calculate_background
int calculate_background(long nn, int flag)
Calculates the background for one sub-image.
Definition: Bimage_background.cpp:27
asymmetric_unit_views
View * asymmetric_unit_views(Bsymmetry *sym, double theta_step, double phi_step, int flag)
Initializes a well-distributed set of views in an asymmetric unit.
Definition: symmetry.cpp:564
View
Definition: View.h:26
Boption::origin
Vector3< double > origin()
Definition: options.h:180
Bsymmetry
Definition: symmetry.h:38
use
const char * use[]
Definition: bhelix.cpp:21
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
Bimage::project
Bimage * project(char axis, int flags=1)
Projects a 3D image to a 2D image down one of the three major axes.
Definition: Bimage_project.cpp:31
Bimage::distort_elliptically
int distort_elliptically(double angle, double shift)
Transforms a tubular cylinder to an elliptical profile.
Definition: Bimage_helix.cpp:779
verbose
int verbose
Boption::value
Bstring value
Definition: options.h:30
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
Bstring::split_into_doubles
vector< double > split_into_doubles(const Bstring &delim)
Definition: Bstring.cpp:673
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
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.
Bstring::length
long length() const
Definition: Bstring.cpp:263
init_point_group_symmetry
Bsymmetry * init_point_group_symmetry(Bstring symmetry_string)
Sets up a symmetry structure from a string identifier.
Definition: symmetry.cpp:39
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
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
rwimg.h
Header file for 2D and 3D image I/O.
Bimage::find_helix_parameters
Bplot * find_helix_parameters(double angle_start, double angle_end, double angle_step, int bin, double hires, double lores, double radius)
Finds the best helix parameters for helical map.
Definition: Bimage_helix.cpp:848
kill_symmetry
int kill_symmetry(Bsymmetry *sym)
Deallocates a symmetry structure.
Definition: symmetry.cpp:207
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
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
main
int main(int argc, char **argv)
Definition: bhelix.cpp:66
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bimage::convert_to_helix
double convert_to_helix(double helix_rise, double helix_angle, Vector3< double > offset)
Converts a map to a helix.
Definition: Bimage_helix.cpp:721
Bimage::helix_segment_correlation
Bplot * helix_segment_correlation(int thickness, double angle_start, double angle_end, double angle_step, int bin, double hires, double lores, double radius)
Calculates the correlation over rotation angles between helical segments.
Definition: Bimage_helix.cpp:1175
kernel
Definition: kernlut.h:14
FSI_Kernel
Definition: FSI_Kernel.h:14
TWOPI
#define TWOPI
Definition: utilities.h:72
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