Bsoft  2.1.0
Bernard's software package
bhelcross.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "symmetry.h"
11 #include "linked_list.h"
12 #include "options.h"
13 #include "utilities.h"
14 #include "timer.h"
15 
16 
17 // Declaration of global variables
18 extern int verbose; // Level of output to the screen
19 
20 
21 // Usage assistance
22 const char* use[] = {
23 " ",
24 "Usage: bhelcross [options] input.img output.img",
25 "-----------------------------------------------",
26 "Calculates the 2D cross section for a helical filament.",
27 "The helix axis must coincide with the y axis.",
28 " ",
29 "Actions:",
30 "-crosssection Calculate a cross section based on helical parameters.",
31 "-extrude 254 Extrude the cross section to the indicated length.",
32 "-symmetry C5 Apply point group symmetry.",
33 "-rescale -0.1,5.2 Rescale data to average and standard deviation.",
34 " ",
35 "Parameters:",
36 "-verbose 1 Verbosity of output.",
37 "-datatype u Force writing of a new data type.",
38 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; a single value can be given).",
39 "-origin 110,50,44 Origin for helix (default 0,0,0 or from image).",
40 "-helix 25.3,67 Helical rise and angle (default 10 angstrom, 45 degrees).",
41 " ",
42 "Parameters for calculating the cross section:",
43 "-resolution 15.6 High resolution limit for reconstruction (default 0).",
44 "-scale 1.5 Reconstruction scale (default 1).",
45 "-snradius 36 Radius to distinguish signal from noise (alternative to -kernel).",
46 "-kernel 9 Local variance kernel size for generating a foreground mask (default 11).",
47 " ",
48 "Parameters for alignment:",
49 "-alignresolution 20,360 High and low resolution limits (angstrom).",
50 "-annuli 12,60 Real space annular limits (default 0,inf pixels).",
51 "-shiftlimit 3.5 Limit on origin shift relative to nominal center (default 10% of box edge size).",
52 " ",
53 "Parameter for extrusion:",
54 "-fill 1.77 Fill value for background: average (default), background, or value.",
55 " ",
56 "Input:",
57 "-reference file.map 2D reference image file name.",
58 " ",
59 NULL
60 };
61 
62 int main(int argc, char **argv)
63 {
64  // Initialize variables
65  DataType nudatatype(Unknown_Type); // Conversion to new type
66  int set_sampling(0);
67  Vector3<double> sam; // Units for the three axes (A/pixel)
68  int cross(0); // Calculate cross section
69  int extrude(0); // Extrude cross section
70  Bsymmetry* sym = NULL; // Point group
71  double helix_rise(10); // Rise per asymmetric unit
72  double helix_angle(M_PI/8); // Rotation angle per asymmetric unit
73  double hires(0); // Resolution limit
74  double scale(1); // Reconstruction scale
75  double nuavg(0), nustd(0); // For rescaling
76  Vector3<double> origin; // Helix origin
77  int set_origin(0); // Flag to set origin
78  int calc_background(0); // Flag to calculate background values
79 // int norm_flag(0); // Normalization flag
80  double snradius(0); // Signal-noise radius
81  int var_kernel(11); // Variance filter kernel size
82  double res_lo(10000); // Resolution limits for orientation finding
83  double res_hi(0); // Must be set > 0 to determine orientations
84  int ann_min(0); // Minimum annulus
85  int ann_max(1000); // Maximum annulus, reset to maximum radius in image
86  double shift_limit(-1); // Maximum shift from nominal box origin
87  double fill(0); // Fill value for resizing
88  int fill_type(FILL_AVERAGE); // Fill type for resizing
89  Bstring ref_file; // Reference file name
90 
91  int optind;
92  Boption* option = get_option_list(use, argc, argv, optind);
93  Boption* curropt;
94  for ( curropt = option; curropt; curropt = curropt->next ) {
95  if ( curropt->tag == "crosssection" ) cross = 1;
96  if ( curropt->tag == "extrude" )
97  if ( ( extrude = curropt->value.integer() ) < 1 )
98  cerr << "-extrude: An extrusion length must be specified!" << endl;
99  if ( curropt->tag == "symmetry" )
100  sym = curropt->symmetry();
101  if ( curropt->tag == "rescale" )
102  if ( curropt->values(nuavg, nustd) < 2 )
103  cerr << "-rescale: Both average and standard deviation must be specified!" << endl;
104  if ( curropt->tag == "background" )
105  calc_background = 1;
106 // if ( curropt->tag == "normalize" )
107 // norm_flag = 1;
108  if ( curropt->tag == "origin" ) {
109  if ( curropt->value[0] == 'c' ) {
110  set_origin = 2;
111  } else {
112  origin = curropt->origin();
113  set_origin = 1;
114  }
115  }
116  if ( curropt->tag == "datatype" )
117  nudatatype = curropt->datatype();
118  if ( curropt->tag == "sampling" ) {
119  sam = curropt->scale();
120  set_sampling = 1;
121  }
122  if ( curropt->tag == "helix" ) {
123  if ( curropt->values(helix_rise, helix_angle) < 2 )
124  cerr << "-helix: Both rise and angle must be specified" << endl;
125  else
126  helix_angle *= M_PI/180.0;
127  }
128  if ( curropt->tag == "resolution" )
129  if ( ( hires = curropt->value.real() ) < 0.001 )
130  cerr << "-resolution: A resolution limit must be specified!" << endl;
131  if ( curropt->tag == "scale" )
132  if ( ( scale = curropt->value.real() ) < 0.0001 )
133  cerr << "-scale: A scale must be specified!" << endl;
134  if ( curropt->tag == "snradius" )
135  if ( ( snradius = curropt->value.real() ) < 1 )
136  cerr << "-snradius: A radius must be specified!" << endl;
137  if ( curropt->tag == "kernel" )
138  if ( ( var_kernel = curropt->value.integer() ) < 1 )
139  cerr << "-kernel: The kernel edge size must be specified!" << endl;
140  if ( curropt->tag == "alignresolution" ) {
141  if ( curropt->values(res_hi, res_lo) < 1 )
142  cerr << "-resolution: At least one resolution limit must be specified!" << endl;
143  else
144  if ( res_lo < res_hi ) swap(res_hi, res_lo);
145  }
146  if ( curropt->tag == "annuli" )
147  if ( curropt->values(ann_min, ann_max) < 1 )
148  cerr << "-annuli: At least a minimum annulus must be specified!" << endl;
149  if ( curropt->tag == "shiftlimit" )
150  if ( ( shift_limit = curropt->value.real() ) < 0.001 )
151  cerr << "-shiftlimit: A maximum shift in pixels must be specified!" << endl;
152  if ( curropt->tag == "fill" )
153  fill = curropt->fill(fill_type);
154  if ( curropt->tag == "reference" )
155  ref_file = curropt->filename();
156  }
157  option_kill(option);
158 
159  double ti = timer_start();
160 
161  Bimage* p = read_img(argv[optind++], 1, -1);
162  Bimage* pcs = NULL;
163  Bimage* pmask = NULL;
164 
165  if ( !p ) {
166  cerr << "Error: No input file!" << endl;
167  bexit(-1);
168  }
169 
170  if ( set_origin ) {
171  if ( set_origin == 2 ) p->origin(p->default_origin());
172  else p->origin(origin);
173  }
174 
175  if ( set_sampling ) p->sampling(sam);
176 
177  double snr(0), nsam(fabs(p->real_size()[1]*helix_angle/(TWOPI*helix_rise)));
178 // cout << "nsam=" << nsam << endl;
179 
180  if ( calc_background ) p->calculate_background();
181 
182  if ( cross ) {
183  pcs = p->helical_cross_section(helix_rise, helix_angle, scale, hires);
184  delete p;
185  p = pcs;
186  if ( snradius ) {
187  snr = p->snvariance(snradius);
188  } else {
189  pmask = p->variance_mask(var_kernel, 0.01, 1);
190  snr = pmask->image->FOM();
191  delete pmask;
192  snr /= sqrt(nsam);
193  cout << "Adjusted signal-to-noise ratio: " << snr << endl << endl;
194  }
195  }
196 
197  Bimage* pref = NULL;
198  if ( ref_file.length() ) {
199  pref = read_img(ref_file, 1, 0);
200  p->align2D(pref, ann_min, ann_max, res_lo, res_hi, shift_limit, 0);
201  delete pref;
202  }
203 
204  if ( sym ) {
205  p->symmetrize(sym, 1);
206  kill_symmetry(sym);
207  }
208 
209  if ( extrude > 1 )
210  p->extrude_cross_section(extrude, helix_rise, helix_angle, fill_type, fill);
211 
212  if ( optind < argc ) {
213  if ( nustd > 0 ) p->rescale_to_avg_std(nuavg, nustd);
214  p->change_type(nudatatype);
215  write_img(argv[optind], p, 0);
216  }
217 
218  delete p;
219 
220  if ( verbose & VERB_TIME )
221  timer_report(ti);
222 
223  bexit(0);
224 }
225 
226 
227 
228 
229 
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::real_size
Vector3< double > real_size()
Definition: Bimage.h:500
cross
vector< T1 > cross(vector< T1 > &v1, vector< T2 > &v2)
Definition: Vector3.h:624
Bimage::symmetrize
double symmetrize(Bstring &symmetry_string, int flag)
Definition: Bimage.h:1188
Boption::scale
Vector3< double > scale()
Definition: options.h:190
Bstring
Definition: Bstring.h:29
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::datatype
DataType datatype()
Definition: options.h:93
symmetry.h
Header file for general symmetry functions.
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
Bstring::real
double real()
Definition: Bstring.cpp:553
FILL_AVERAGE
#define FILL_AVERAGE
Definition: utilities.h:177
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
Bsub_image::FOM
double FOM()
Definition: Bimage.h:165
Boption::origin
Vector3< double > origin()
Definition: options.h:180
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
Bsymmetry
Definition: symmetry.h:38
Boption::symmetry
Bsymmetry * symmetry()
Definition: options.h:240
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
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
Bimage::align2D
double align2D(Bimage *pref, double res_polar, int ann_min, int ann_max, Bimage *prs_mask, double shift_limit, double angle_limit, fft_plan planf_1D, fft_plan planb_1D, fft_plan planf_2D, fft_plan planb_2D)
Finds the best in-plane alignment for two 2D images using polar power spectra.
Definition: Bimage_align.cpp:881
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
Bimage::extrude_cross_section
int extrude_cross_section(long length, double helix_rise, double helix_angle, int fill_type, double fill)
Extrudes a 2D cross section into a 3D continuous helix.
Definition: Bimage_helix.cpp:1531
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.
Boption::fill
double fill(int &fill_type)
Definition: options.h:138
Bstring::length
long length() const
Definition: Bstring.cpp:263
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::variance_mask
Bimage * variance_mask(long kernel_size, double lowvar=1e-6, int bkg_flag=0)
Calculates a mask based on local variance.
Definition: Bimage_mask.cpp:1364
verbose
int verbose
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
Bimage::snvariance
double snvariance(double snradius)
Calculates the ratio of variance inside and outside a given radius.
Definition: Bimage_helix.cpp:1467
main
int main(int argc, char **argv)
Definition: bhelcross.cpp:62
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bimage::helical_cross_section
Bimage * helical_cross_section(double helix_rise, double helix_angle, double scale, double hires)
Calculates a helical cross section from line transforms of a filament.
Definition: Bimage_helix.cpp:1356
use
const char * use[]
Definition: bhelcross.cpp:22
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