Bsoft  2.1.0
Bernard's software package
blocres.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "utilities.h"
11 #include "options.h"
12 #include "timer.h"
13 
14 // Declaration of global variables
15 extern int verbose; // Level of output to the screen
16 
17 
18 // Usage assistence
19 const char* use[] = {
20 " ",
21 "Usage: blocres [options] input1.img input2.img output.img",
22 "---------------------------------------------------------",
23 "The local (-box) and shell (-shell) resolution calculations are mutually exclusive.",
24 " ",
25 "Actions:",
26 "-box 20 Kernel size for determining local resolution (pixels/voxels).",
27 "-shell 14 Shell width for determining radial resolution (pixels/voxels).",
28 " ",
29 "Parameters:",
30 "-verbose 7 Verbosity of output.",
31 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; a single value can be given).",
32 "-origin 0.8,-10,15.7 Set the origin (default from input image).",
33 "-step 5 Interval between voxel samples or shells for resolution analysis (pixels, default: 1).",
34 "-radii 20,130 Radial limits for local or shell resolution (default 0,<x-dimension>).",
35 "-maxresolution 5 Maximum frequency available in the data (angstrom).",
36 "-cutoff 0.5,0.143 Resolution cutoffs for FSC (default 0.5).",
37 "-fill 200 Value to fill the background (non-masked regions; default 0).",
38 " ",
39 "Parameters for local resolution:",
40 "-pad 2 Resolution box padding factor (0 = none, default: 1 (box) and 0 (shell)).",
41 "-edge 10 Pixels at the edge of the whole map to exclude from analysis (default: half the size of the resolution box).",
42 "-taper edge Apply a windowing aperture to box volumes before resolution analysis.",
43 " none = do not taper",
44 " edge = circular mask with smooth edges",
45 " hann = hanning window (default)",
46 "-nofill Do not fill values in between (if step > 1).",
47 "-symmetry D5 Point group symmetry.",
48 " ",
49 "Parameters for shell resolution:",
50 "-smooth Smooth the shell edge.",
51 " ",
52 "Input:",
53 "-Mask mask.tif,2 Mask file to use for limiting the analysis to a defined region",
54 " and level value to use (optional, default: all but zero).",
55 " ",
56 "Output:",
57 "-usedmask used.mrc Output file name for mask after modifications.",
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  Vector3<double> origin; // New image origin
67  int set_origin(0); // Flag to set origin
68  Vector3<double> sam; // Units for the three axes (A/pixel)
69  int boxsize(0); // Linear size of box designated by user
70  int shell_width(0); // Shell thickness
71  int taper(2); // Flag to taper boxes
72  int smooth(0); // Flag to smooth the shell edge
73  Vector3<long> vedge(-1,-1,-1); // Edge size to exclude
74  int nofill(0); // Fill flag
75  int pad(-1); // Padding factor
76  double maxres(0); // Resolution limit
77  double cutoff[4] = {0.5,0,0,0}; // FSC cutoff values
78  int minrad(0); // Minimum radius for shell resolution
79  int maxrad(0); // Maximum radius for shell resolution
80  int setimg(-1); // Select all images
81  long step(1); // Sampling step
82  Bstring symmetry_string; // Point group
83  double fill(0); // Background fill value
84  Bstring maskfile; // Input mask file name
85  int mask_level(-1); // Mask level (-1 => all but zero)
86  Bstring usedmaskfile; // Output mask file name after modifications to original mask
87 
88  int optind;
89  Boption* option = get_option_list(use, argc, argv, optind);
90  Boption* curropt;
91  for ( curropt = option; curropt; curropt = curropt->next ) {
92  if ( curropt->tag == "box" )
93  if ( ( boxsize = curropt->value.integer() ) < 1 )
94  cerr << "-box: A resolution box size must be specified!" << endl;
95  if ( curropt->tag == "shell" )
96  if ( ( shell_width = curropt->value.integer() ) < 1 )
97  cerr << "-shell: A shell width must be specified!" << endl;
98  if ( curropt->tag == "sampling" )
99  sam = curropt->scale();
100  if ( curropt->tag == "origin" ) {
101  if ( curropt->value[0] == 'c' ) {
102  set_origin = 2;
103  } else {
104  origin = curropt->origin();
105  set_origin = 1;
106  }
107  }
108  if ( curropt->tag == "step" )
109  if ( ( step = curropt->value.integer() ) < 1 )
110  cerr << "-step: A sampling step size must be specified!" << endl;
111  if ( curropt->tag == "pad" )
112  if ( ( pad = curropt->value.integer() ) < 0 )
113  cerr << "-pad: A padding factor must be specified!" << endl;
114  if ( curropt->tag == "maxresolution" )
115  if ( ( maxres = curropt->value.real() ) < 0.1 )
116  cerr << "-maxresolution: A resolution limit must be specified!" << endl;
117  if ( curropt->tag == "taper" ) {
118  if ( curropt->value[0] == 'n' ) taper = 0;
119  if ( curropt->value[0] == 'h' ) taper = 2;
120  if ( curropt->value[0] == 'e' ) taper = 1;
121  }
122  if ( curropt->tag == "fill" )
123  if ( ( fill = curropt->value.real() ) < 1 )
124  cerr << "-fill: A fill value must be specified!" << endl;
125  if ( curropt->tag == "nofill" )
126  nofill = 1;
127  if ( curropt->tag == "edge" )
128  vedge = curropt->size();
129  if ( curropt->tag == "radii" )
130  if ( curropt->values(minrad, maxrad) < 1 )
131  cerr << "-radii: At least a minimum radius must be specified!" << endl;
132  if ( curropt->tag == "smooth" ) smooth = 1;
133  if ( curropt->tag == "cutoff" )
134  if ( curropt->values(cutoff[0], cutoff[1], cutoff[2], cutoff[3]) < 1 )
135  cerr << "-cutoff: At least one FSC threshold must be specified!" << endl;
136  if ( curropt->tag == "symmetry" )
137  symmetry_string = curropt->symmetry_string();
138  if ( curropt->tag == "Mask" ) {
139  maskfile = curropt->filename();
140  if ( maskfile.contains(",") ) {
141  mask_level = (maskfile.post(',')).integer();
142  maskfile = maskfile.pre(',');
143  }
144  }
145  if ( curropt->tag == "usedmask" )
146  usedmaskfile = curropt->filename();
147  }
148  option_kill(option);
149 
150  double ti = timer_start();
151 
152  Bimage* p1 = read_img(argv[optind++], 1, setimg);
153  if ( p1 == NULL ) bexit(-1);
154 
155  Bimage* p2 = read_img(argv[optind++], 1, setimg);
156  if ( p2 == NULL ) bexit(-1);
157 
158  if ( p1->size() != p2->size() ) {
159  cerr << "Error: The sizes of the two images do not match!" << endl;
160  bexit(1);
161  }
162 
163  if ( sam.volume() > 0 ) {
164  p1->sampling(sam);
165  p2->sampling(sam);
166  }
167 
168  if ( set_origin ) {
169  if ( set_origin == 2 ) origin = p1->default_origin();
170  } else {
171  origin = p1->image->origin();
172  if ( origin.length() < 1 ) origin = p1->default_origin();
173  }
174 
175  p1->origin(origin);
176  p2->origin(origin);
177 
178  if ( boxsize>0 && maskfile.length() && step>1 && !nofill) {
179  cerr << "ERROR: Can not set a step size more than one when using a mask!" << endl;
180  cerr << "Please select only one of the two options, otherwise set the nofill option" << endl;
181  bexit(1);
182  }
183 
184  Bimage* p = NULL;
185  Bimage* psymmask = NULL;
186  Bimage* pmask = NULL;
187  Bsymmetry* sym = NULL;
188 
189  if ( boxsize > 0 ) {
190  if ( maskfile.length() ) pmask = read_img(maskfile, 1, -1);
191  else {
192  pmask = p1->copy_header();
193  pmask->data_type(UCharacter);
194  pmask->data_alloc();
195  pmask->fill(1);
196  }
197 
198  if ( maxrad )
199  pmask->mask_shell(origin, minrad, maxrad);
200 
201  if ( symmetry_string.length() ) {
202  sym = init_point_group_symmetry(symmetry_string);
203  psymmask = p1->levelmask_asymmetric_units(sym, 1);
204  psymmask->mask_dilate(2);
205  pmask->multiply(psymmask);
206  delete psymmask;
207  }
208 
209  if ( usedmaskfile.length() )
210  write_img(usedmaskfile, pmask, 0);
211 
212  if ( pad < 0 ) pad = 1;
213  p = p1->fsc_local(p2, pmask, maxres, cutoff, mask_level, boxsize,
214  pad, vedge, step, taper, fill);
215  if ( !p ) {
216  cerr << "Error: No local resolution map produced!" << endl;
217  bexit(-1);
218  }
219 
220  if ( step > 1 && !nofill ) p->interpolate_gaps(step);
221 
222  if ( sym ) {
224  kill_symmetry(sym);
225  }
226  } else if ( shell_width > 0 ) {
227  pad = 0;
228  p = p1->fsc_shell(p2, maxres, cutoff, shell_width, step, minrad, maxrad,
229  pad, smooth, fill);
230  if ( !p ) {
231  cerr << "Error: No shell resolution map produced!" << endl;
232  bexit(-1);
233  }
234  }
235 
236  // Write an output file if a file name is given
237  if ( optind < argc ) {
238  p->change_type(nudatatype);
239  write_img(argv[optind], p, 0);
240  }
241 
242  if (pmask) delete pmask;
243  delete p1;
244  delete p2;
245  delete p;
246 
247  if ( verbose & VERB_TIME )
248  timer_report(ti);
249 
250  bexit(0);
251 }
252 
Boption
A class for a command line input option.
Definition: options.h:26
Boption::scale
Vector3< double > scale()
Definition: options.h:190
Bimage::mask_dilate
long mask_dilate(long times=1)
Dilates a binary mask.
Definition: Bimage_mask.cpp:423
UCharacter
@ UCharacter
Definition: utilities.h:153
Bstring
Definition: Bstring.h:29
Bstring::integer
long integer()
Definition: Bstring.cpp:547
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
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
Bstring::real
double real()
Definition: Bstring.cpp:553
options.h
Header file for option handlers.
Bstring::post
Bstring post(char c)
Definition: Bstring.cpp:402
Boption::origin
Vector3< double > origin()
Definition: options.h:180
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
Bsymmetry
Definition: symmetry.h:38
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
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
Bimage::multiply
void multiply(long j, Complex< double > cv)
Definition: Bimage.h:325
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
Bstring::pre
Bstring pre(char c)
Definition: Bstring.cpp:389
Bimage::mask_shell
long mask_shell(Vector3< double > origin, double rad_min, double rad_max)
Definition: Bimage.h:1328
Bimage::interpolate_gaps
int interpolate_gaps(long step)
Interpolate the voxels that are not calculated.
Definition: Bimage_edit.cpp:933
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
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:271
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
Boption::symmetry_string
Bstring symmetry_string()
Definition: options.h:233
Bimage::fsc_local
Bimage * fsc_local(Bimage *p, Bimage *pmask, double resolution, double *cutoff, int mask_level, int size, int pad, Vector3< long > vedge, int step=1, int taper=1, double fill=0)
Determine the local resolution at each masked voxel in a map.
Definition: Bimage_resolution.cpp:592
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
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
utilities.h
Header file for general utilities.
Boption::size
Vector3< long > size()
Definition: options.h:163
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
Vector3::volume
double volume()
Definition: Vector3.h:237
rwimg.h
Header file for 2D and 3D image I/O.
Bimage::copy_header
Bimage * copy_header()
Definition: Bimage.h:658
use
const char * use[]
Definition: blocres.cpp:19
Bimage::fill
void fill(double v)
Definition: Bimage.h:706
kill_symmetry
int kill_symmetry(Bsymmetry *sym)
Deallocates a symmetry structure.
Definition: symmetry.cpp:207
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
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
Vector3::length
double length()
Definition: Vector3.h:222
verbose
int verbose
main
int main(int argc, char **argv)
Definition: blocres.cpp:62
Bimage::fsc_shell
Bimage * fsc_shell(Bimage *p, double hi_res, double *cutoff, int thickness, int step, int minrad, int maxrad, int pad=1, int smooth=0, double fill=0)
Determine the resolution for each concentric shell in a map.
Definition: Bimage_resolution.cpp:415