Bsoft  2.1.0
Bernard's software package
bsym.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "rwsymop.h"
11 #include "symmetry.h"
12 #include "linked_list.h"
13 #include "utilities.h"
14 #include "options.h"
15 #include "timer.h"
16 
17 // Declaration of global variables
18 extern int verbose; // Level of output to the screen
19 
20 int img_write_symmetry_views(Bimage* p, Bsymmetry* sym, View ref_view,
21  Bstring& filename, DataType datatype, double avg, double std);
22 
23 // Usage assistance
24 const char* use[] = {
25 " ",
26 "Usage: bsym [options] in.img out.img",
27 "------------------------------------",
28 "Analyzes for and applies point group symmetry to images.",
29 "Generates symmetry axes for point group symmetries.",
30 " ",
31 "Actions:",
32 "-invert Invert density in the image.",
33 "-rescale -0.1,5.2 Rescale symmetrized data to average and standard deviation.",
34 "-axis 3,1 Rotate to desired axis, with flag for alternate (requires -symmetry).",
35 "-handedness 1.4 Check handedness above threshold (density is positive).",
36 "-check C,D,I Check symmetry in correctly oriented map.",
37 "-find 1.5,2,1 Find orientation: angle step, binning, and",
38 " flag to skip major axis (use with -symmetry).",
39 "-change C5,C6 Change point group symmetry.",
40 "-Views Output all symmetry-related views, (filename: out_??.img).",
41 "-asu 3 Output a single or multi-level mask of asymmetric units (use with -symmetry).",
42 " > 0, generate the corresponding asymetric unit.",
43 " == 0, generate all asymetric units.",
44 "-edge oval Apply an edge smoothing of the indicated type.",
45 "-replicate Replicate the asymmetric unit (use with -symmetry).",
46 "-nonorm Do not normalize after symmetrization (use with -symmetry).",
47 "-show Show operational symmetry matrices (use with -symmetry).",
48 "-pdb Show PDB symmetry matrices (use with -symmetry).",
49 " ",
50 "Parameters:",
51 "-verbose 7 Verbosity of output.",
52 "-datatype f Force writing of a new image data type.",
53 "-origin 0,22.5,30 Set the origin for rotation.",
54 "-reference 0,1.5,-0.2,35 Reference symmetry axis and rotation angle (default 0,0,1,0).",
55 "-resolution 15.6,200 Resolution limits for cross-correlation (default 0).",
56 "-symmetry C5 Point group symmetry.",
57 "-fill 0.02 Fill value (default image background).",
58 "-radius 34.5,1.2 Radius in voxels of repeats for changing symmetry.",
59 " and slope along z to adjust the radius.",
60 " ",
61 "Input:",
62 "-Template temp.pif Template to find symmetry equivalent.",
63 "-mask mask.mrc Mask (must be the same size as the template).",
64 " ",
65 "Output:",
66 "-Parameters out.star Output STAR file with symmetry axes and orders.",
67 " ",
68 NULL
69 };
70 
71 int main(int argc, char **argv)
72 {
73  // Initialize variables
74  DataType nudatatype(Unknown_Type); // Conversion to new type
75  int setinvert(0); // Flag to invert density before operations
76  double nuavg(0), nustd(0); // Rescaling to average and stdev
77  long axis(0), axis_flag(0); // Axis to rotate to
78  Bstring check_sym; // String of symmetries to check for
79  double find_angle(0); // Angle step size for symmetry search
80  int find_bin(1); // Binning for searching
81  int flags(0); // Only search for minor axes
82  double radius(0); // Radius to derive shift for symmetry change
83  double z_slope(0); // Slope along z to adjust the radius
84  Vector3<double> origin; // Origin
85  int set_origin(0); // Flag to set origin
86  View ref_view; // Reference view
87  double hires(0), lores(0); // Resolution limits for cross-correlation
88  Bsymmetry* sym = NULL; // Point group
89  Bsymmetry* symnu = NULL; // New point group
90  int norm_flag(1); // Normalization flag
91  int fill_type(FILL_BACKGROUND); // Use background
92  double fill(0); // Default fill is zero
93  int write_all_views(0); // Flag to output al symmetry-related views
94  int hand_flag(0); // Flag for checking of handedness
95  double hand_threshold(0); // Threshold for checking handedness
96  int asu_mask(-1); // Flag to generate a mult-level mask
97  int setedge(-1); // Edge type; default none
98  int replicate(0); // Flag to replicate the asymmetric unit
99  int show(0); // Flag to show operational matrices
100  Bstring tempfile; // Input template for symmetry equivalents
101  Bstring maskfile; // Mask file name
102  Bstring pgfile; // Output point group file
103 
104  int optind;
105  Boption* option = get_option_list(use, argc, argv, optind);
106  Boption* curropt;
107  for ( curropt = option; curropt; curropt = curropt->next ) {
108  if ( curropt->tag == "datatype" )
109  nudatatype = curropt->datatype();
110  if ( curropt->tag == "invert" )
111  setinvert = 1;
112  if ( curropt->tag == "rescale" )
113  if ( curropt->values(nuavg, nustd) < 2 )
114  cerr << "-rescale: Both average and standard deviation must be specified!" << endl;
115  if ( curropt->tag == "axis" )
116  if ( curropt->values(axis, axis_flag) < 1 )
117  cerr << "-axis: An axis order must be specified!" << endl;
118  if ( curropt->tag == "check" ) check_sym = curropt->value;
119  if ( curropt->tag == "find" ) {
120  if ( curropt->values(find_angle, find_bin, flags) < 1 )
121  cerr << "-find: An angle step size must be specified!" << endl;
122  else
123  find_angle *= M_PI/180.0;
124  }
125  if ( curropt->tag == "change" ) {
126  sym = init_point_group_symmetry(curropt->value.pre(','));
127  symnu = init_point_group_symmetry(curropt->value.post(','));
128  }
129  if ( curropt->tag == "origin" ) {
130  if ( curropt->value[0] == 'c' ) {
131  set_origin = 2;
132  } else {
133  origin = curropt->origin();
134  set_origin = 1;
135  }
136  }
137  if ( curropt->tag == "reference" )
138  ref_view = curropt->view();
139  if ( curropt->tag == "resolution" )
140  if ( curropt->values(hires, lores) < 1 )
141  cerr << "-resolution: Resolution limits must be specified!" << endl;
142  if ( curropt->tag == "symmetry" )
143  sym = curropt->symmetry();
144  if ( curropt->tag == "fill" )
145  fill = curropt->fill(fill_type);
146  if ( curropt->tag == "Views" )
147  write_all_views = 1;
148  if ( curropt->tag == "asu" )
149  if ( ( asu_mask = curropt->value.integer() ) < 0 )
150  cerr << "-asu: An asymmetric unit index must be specified!" << endl;
151  if ( curropt->tag == "edge" ) {
152  if ( curropt->value.length() < 1 ) {
153  cerr << "-edge: An edge shape must be specified." << endl;
154  } else {
155  if ( tolower(curropt->value[0]) == 'r' ) setedge = 0;
156  if ( tolower(curropt->value[0]) == 'o' ) setedge = 1;
157  if ( tolower(curropt->value[0]) == 'c' ) setedge = 2;
158  }
159  }
160  if ( curropt->tag == "replicate" )
161  replicate = 1;
162  if ( curropt->tag == "nonorm" )
163  norm_flag = 0;
164  if ( curropt->tag == "show" ) show |= 1;
165  if ( curropt->tag == "pdb" ) show |= 2;
166  if ( curropt->tag == "handedness" ) {
167  if ( ( hand_threshold = curropt->value.real() ) )
168  cerr << "-handedness: A density threshold must be specified!" << endl;
169  else
170  hand_flag = 1;
171  }
172  if ( curropt->tag == "radius" )
173  if ( curropt->values(radius, z_slope) < 1 )
174  cerr << "-radius: A radius in voxels must be specified!" << endl;
175  if ( curropt->tag == "Template" )
176  tempfile = curropt->filename();
177  if ( curropt->tag == "mask" )
178  maskfile = curropt->filename();
179  if ( curropt->tag == "Parameters" )
180  pgfile = curropt->filename();
181  }
182  option_kill(option);
183 
184  double ti = timer_start();
185 
186  if ( show & 1 ) sym_show_operational_matrices(sym);
187  if ( show & 2) sym_show_pdb_matrices(sym);
188 
189  if ( pgfile.length() )
190  write_pointgroup(pgfile, sym, ref_view);
191 
192  // Read image file
193  int dataflag(0);
194  Bimage* p = NULL;
195  if ( hand_flag || optind < argc - 1 ) dataflag = 1;
196  if ( optind < argc ) {
197  p = read_img(argv[optind++], dataflag, -1);
198  if ( !p ) {
199  cerr << "Error: File \"" << argv[optind-1] << "\" was not read!" << endl;
200  bexit(-1);
201  }
202  if ( nudatatype == Unknown_Type ) nudatatype = p->data_type();
203  } else
204  bexit(0);
205 
206  if ( setinvert ) p->invert();
207 
208  if ( fill_type == FILL_AVERAGE ) fill = p->average();
209  if ( fill_type == FILL_BACKGROUND ) {
210  if ( !p->background(long(0)) ) p->calculate_background();
211  fill = p->background(long(0));
212  }
213 
214  p->background(fill);
215 
216  if ( set_origin ) {
217  if ( set_origin == 2 ) p->origin(p->size()/2);
218  else p->origin(origin);
219  }
220 
221  Bimage* ptemp = NULL;
222  if ( tempfile.length() ) {
223  ptemp = read_img(tempfile, 1, 0);
224  if ( !ptemp ) {
225  cerr << "Error:File \"" << tempfile << "\" was not read!" << endl;
226  bexit(-2);
227  }
228  }
229 
230  Bstring filename;
231  Bimage* pmask = NULL;
232  Vector3<double> start;
233  Matrix3 mat;
234 
235  if ( optind < argc ) {
236  filename = argv[optind];
237  if ( p ) {
238  if ( check_sym.length() ) {
239  p->check_point_group(check_sym);
240  }
241  if ( sym ) {
242  if ( write_all_views ) {
243  img_write_symmetry_views(p, sym, ref_view,
244  filename, nudatatype, nuavg, nustd);
245  }
246  if ( asu_mask >= 0 ) {
247  pmask = p->levelmask_asymmetric_units(sym, asu_mask);
248  delete p;
249  p = pmask;
250  } else if ( find_angle > 0 ) {
251  p->find_point_group(sym, find_angle, find_bin, hires, lores, flags);
252  } else if ( replicate > 0 ) {
254  } else if ( symnu ) {
255  p->change_symmetry(sym, symnu, radius, z_slope);
256  kill_symmetry(symnu);
257  } else if ( axis > 0 ) {
258  p->rotate_to_axis(sym, axis, axis_flag);
259  } else if ( ptemp ) {
260  if ( maskfile.length() )
261  pmask = read_img(maskfile, 1, -1);
262  mat = p->symmetry_equivalent(ptemp, pmask, sym);
263  p->rotate(mat);
264  } else {
265  p->symmetrize(sym, ref_view, norm_flag);
266  }
267  }
268  }
269  if ( setedge >= 0 ) p->edge(setedge, p->size(), start, 2, FILL_BACKGROUND, 0);
270  if ( nustd ) p->rescale_to_avg_std(nuavg, nustd);
271  p->change_type(nudatatype);
272  write_img(filename, p, 0);
273  }
274 
275  delete p;
276  delete ptemp;
277  kill_symmetry(sym);
278 
279  if ( verbose & VERB_TIME )
280  timer_report(ti);
281 
282  bexit(0);
283 }
284 
303  Bstring& filename, DataType datatype, double avg, double std)
304 {
305  int i;
306  Bstring outname;
307  Bimage* prot = NULL;
308  View* v;
309  View* view = symmetry_get_all_views(sym, ref_view);
310 
311  for ( i=1, v=view; v; v=v->next, i++ ) {
312  prot = p->rotate(p->size(), *v);
313  outname = filename.pre_rev('.') + Bstring(i, "_%02d.") + filename.post_rev('.');
314  if ( std ) prot->rescale_to_avg_std(avg, std);
315  prot->change_type(datatype);
316  write_img(outname, prot, 0);
317  delete prot;
318  }
319 
320  kill_list((char *) view, sizeof(View));
321 
322  return 0;
323 }
324 
325 
Boption
A class for a command line input option.
Definition: options.h:26
rwsymop.h
Header file for reading and writing symmetry operators.
View::next
View * next
Definition: View.h:28
Bimage::symmetrize
double symmetrize(Bstring &symmetry_string, int flag)
Definition: Bimage.h:1188
symmetry_get_all_views
View * symmetry_get_all_views(Bsymmetry *sym, View asu_view)
Get all symmetry-related views of one given view.
Definition: symmetry.cpp:442
main
int main(int argc, char **argv)
Definition: bsym.cpp:71
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::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:436
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
use
const char * use[]
Definition: bsym.cpp:24
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
Bimage::background
double background(long nn)
Definition: Bimage.h:549
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
Bstring::real
double real()
Definition: Bstring.cpp:553
write_pointgroup
int write_pointgroup(Bstring &filename, Bstring &symmetry_string, View ref_view)
Writing point group symmetry operators.
Definition: rwsymop.cpp:124
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
View
Definition: View.h:26
Bstring::post
Bstring post(char c)
Definition: Bstring.cpp:402
Boption::origin
Vector3< double > origin()
Definition: options.h:180
Boption::view
View view()
Definition: options.h:247
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
Matrix3
Definition: Matrix3.h:18
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::replicate_asymmetric_unit
int replicate_asymmetric_unit(Bsymmetry *sym)
Calculates a full map from one asymmetric unit.
Definition: Bimage_symmetry.cpp:971
sym_show_pdb_matrices
int sym_show_pdb_matrices(Bsymmetry *sym)
Show PDB format symmetry matrices associated with each symmetry operator.
Definition: symmetry.cpp:1161
Bstring::pre
Bstring pre(char c)
Definition: Bstring.cpp:389
Bimage::rotate
int rotate()
Rotates an image using parameters defined in the image in place.
Definition: Bimage_transform.cpp:255
Boption::value
Bstring value
Definition: options.h:30
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:421
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::average
double average(long cc, double xf, double yf, double zf, long nn, double iscale)
Averages in 3D when scaling is less than 1.
Definition: Bimage.cpp:1371
timer.h
Utilities for timing functions.
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
show
int show(0)
Bimage::levelmask_asymmetric_units
Bimage * levelmask_asymmetric_units(Bsymmetry *sym, int index)
Calculates a multi-level mask to indicate asymmetric units.
Definition: Bimage_symmetry.cpp:898
Bimage::find_point_group
double find_point_group(Bsymmetry *sym, double angle_step, int binfac, double hires, double lores, int flags)
Finds the orientation for an image with a specific point group symmetry.
Definition: Bimage_symmetry.cpp:299
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
Bimage::change_symmetry
int change_symmetry(Bsymmetry *symold, Bsymmetry *symnu, double radius, double z_slope)
Changes the symmetry order for cyclic and dihedral maps.
Definition: Bimage_symmetry.cpp:583
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
Bimage::check_point_group
double check_point_group(Bstring &check_string)
Checks for the requested symmetries.
Definition: Bimage_symmetry.cpp:112
verbose
int verbose
Bimage::symmetry_equivalent
Matrix3 symmetry_equivalent(Bimage *ptemp, Bimage *pmask, Bsymmetry *sym)
Finds the symmetry equivalent orientation of a particle with respect to a template.
Definition: Bimage_symmetry.cpp:654
Boption::fill
double fill(int &fill_type)
Definition: options.h:138
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.
sym_show_operational_matrices
int sym_show_operational_matrices(Bsymmetry *sym)
Show symmetry matrices associated with each symmetry operator.
Definition: symmetry.cpp:1140
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
Bimage::rotate_to_axis
long rotate_to_axis(Bsymmetry *sym, long axis, long axis_flag)
Rotates to a symmetry axis.
Definition: Bimage_symmetry.cpp:558
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
img_write_symmetry_views
int img_write_symmetry_views(Bimage *p, Bsymmetry *sym, View ref_view, Bstring &filename, DataType datatype, double avg, double std)
Rotates an image to all symmetry-related views and writes out the resultant images.
Definition: bsym.cpp:302
Bimage::invert
void invert()
Inverts the data in the image.
Definition: Bimage.cpp:3111
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:178
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bimage::edge
int edge(int type, Vector3< long > rect, Vector3< double > start, double width, int fill_type=0, double fill=0)
Smooths the image edge with a soft rectangular function.
Definition: Bimage_edit.cpp:321
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