Bsoft  2.1.0
Bernard's software package
bresolve.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "rwFSC_XML.h"
11 #include "utilities.h"
12 #include "options.h"
13 #include "timer.h"
14 
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17 
18 // Usage assistance
19 const char* use[] = {
20 " ",
21 "Usage: bresolve [options] input1.img input2.img",
22 "-----------------------------------------------",
23 "Determines the resolution (FSC and DPR) from two images.",
24 " ",
25 "Actions:",
26 "-rescale -0.1,5.2 Rescale data to average and standard deviation.",
27 "-variancemask 45 Mask from local variance of reference with given kernel size.",
28 " ",
29 "Parameters:",
30 "-verbose 7 Verbosity of output.",
31 "-sampling 1.5,1.5,1.5 Sampling (angstrom/pixel, a single value sets all three).",
32 "-resolution 15 Resolution limit for output (angstrom).",
33 "-aligned 25 Resolution limit for alignment (angstrom).",
34 "-fsccut 0.8,0.5,0.143 Resolution cutoffs for FSC.",
35 "-dprcut 45,70 Resolution cutoffs for DPR.",
36 "-ratio 2.5 Radial sampling to Cartesian sampling ratio (default 1).",
37 " ",
38 "Input:",
39 "-map file.map 3D reference file name.",
40 "-mask mask.tif Real space mask (same size as reference).",
41 " ",
42 "Output:",
43 "-output file.star Output STAR format file.",
44 "-Postscript file.ps Postscript output filename.",
45 "-XML file.xml XML output filename.",
46 "-tsv file.tsv Tab-separated values output filename.",
47 " ",
48 NULL
49 };
50 
51 int main(int argc, char* argv[])
52 {
53  // Initialize all settings
54  double nuavg(0), nustd(0); // Rescaling to average and stdev
55  long var_kernel(0); // Local variance kernel to generate mask
56  Vector3<double> sam; // Pixel size
57  double hi_res(0); // Resolution limit for output
58  double aligned_res(0); // Resolution limit for alignment
59  double fsccut[4] = {0.3,0,0,0}; // FSC cutoff values
60  double dprcut[4] = {0,0,0,0}; // DPR cutoff values
61  double sampling_ratio(1); // Sampling ratio in reciprocal space
62  Bstring map_file; // Reference map
63  Bstring ps_file; // Output postscript file
64  Bstring xml_file; // Output XML file
65  Bstring tsv_file; // Output tsv file
66  Bstring real_mask_file; // Real space mask file
67 
68  int optind;
69  Boption* option = get_option_list(use, argc, argv, optind);
70  Boption* curropt;
71  for ( curropt = option; curropt; curropt = curropt->next ) {
72  if ( curropt->tag == "rescale" )
73  if ( curropt->values(nuavg, nustd) < 2 )
74  cerr << "-rescale: Both average and standard deviation must be specified!" << endl;
75  if ( curropt->tag == "variancemask" )
76  if ( ( var_kernel = curropt->value.integer() ) < 3 )
77  cerr << "-variancemask: A kernel edge size must be specified!" << endl;
78  if ( curropt->tag == "sampling" )
79  sam = curropt->scale();
80  if ( curropt->tag == "resolution" )
81  if ( ( hi_res = curropt->value.real() ) < 1 )
82  cerr << "-resolution: A resolution must be specified!" << endl;
83  if ( curropt->tag == "aligned" )
84  if ( ( aligned_res = curropt->value.real() ) < 1 )
85  cerr << "-aligned: A resolution must be specified!" << endl;
86  if ( curropt->tag == "fsccut" )
87  if ( curropt->values(fsccut[0], fsccut[1], fsccut[2], fsccut[3]) < 1 )
88  cerr << "-fsccut: At least one FSC threshold must be specified!" << endl;
89  if ( curropt->tag == "dprcut" )
90  if ( curropt->values(dprcut[0], dprcut[1], dprcut[2], dprcut[3]) < 1 )
91  cerr << "-dprcut: A DPR threshold must be specified!" << endl;
92  if ( curropt->tag == "ratio" )
93  if ( ( sampling_ratio = curropt->value.real() ) < 0.1 )
94  cerr << "-ratio: A ratio must be specified!" << endl;
95  if ( curropt->tag == "map" )
96  map_file = curropt->filename();
97  if ( curropt->tag == "mask" )
98  real_mask_file = curropt->filename();
99  if ( curropt->tag == "Postscript" )
100  ps_file = curropt->filename();
101  if ( curropt->tag == "XML" )
102  xml_file = curropt->filename();
103  if ( curropt->tag == "tsv" )
104  tsv_file = curropt->filename();
105  }
106  option_kill(option);
107 
108  double ti = timer_start();
109 
110 #ifdef HAVE_GCD
111  fftwf_init_threads();
112  fftwf_plan_with_nthreads(system_processors());
113 #endif
114  if ( verbose )
115  cout << "Number of threads: " << system_processors() << endl;
116 
117 
118  Bimage* p = read_img(argv[optind++], 1, -1);
119 
120  if ( map_file.length() < 1 ) map_file = argv[optind++];
121 
122  Bimage* pr = read_img(map_file, 1, -1);
123 
124  if ( p->images() != pr->images() ) {
125  cerr << "Error: The number of images should match!" << endl;
126  bexit(-1);
127  }
128 
129  if ( nustd ) {
130  p->rescale_to_avg_std(nuavg, nustd);
131  pr->rescale_to_avg_std(nuavg, nustd);
132  }
133 
134  Bimage* pmask = NULL;
135  if ( real_mask_file.length() ) {
136  pmask = read_img(real_mask_file, 1, 0);
137  } else if ( var_kernel > 1 ) {
138  pmask = pr->variance_mask(var_kernel);
139  pmask->filter_average(11);
140  write_img("mask.pif", pmask, 0);
141  }
142 
143  if ( pmask ) {
144  p->multiply(pmask);
145  pr->multiply(pmask);
146  }
147 
148  Bimage* pc = p->resolution_prepare(pr);
149  delete p;
150  delete pr;
151 
152  Bplot* plot = NULL;
153 
154  if ( pc->images() == 1 ) {
155  plot = pc->fsc_dpr(hi_res, sampling_ratio, (dprcut[0] == 0));
156  plot->resolution_display(fsccut, dprcut);
157  } else {
158  plot = pc->fsc(hi_res, sampling_ratio);
159  }
160 
161  delete pc;
162 
163  if ( ps_file.length() ) ps_plot(ps_file, plot);
164  if ( xml_file.length() ) xml_write_fsc(xml_file, plot);
165  if ( tsv_file.length() ) plot->write_tsv(tsv_file);
166  delete plot;
167 
168  delete pmask;
169 
170 #ifdef HAVE_GCD
171  fftwf_cleanup_threads();
172 #endif
173 
174  if ( verbose & VERB_TIME )
175  timer_report(ti);
176 
177  bexit(0);
178 }
179 
Boption
A class for a command line input option.
Definition: options.h:26
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::fsc_dpr
Bplot * fsc_dpr(double hi_res, double sampling_ratio=1, int flag=0)
Calculates an FSC and DPR curves from two images.
Definition: Bimage_resolution.cpp:92
system_processors
size_t system_processors()
Returns the number of processors.
Definition: utilities.cpp:193
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
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.
Bplot::resolution_display
void resolution_display(double *fsccut=NULL, double *dprcut=NULL)
Definition: Bplot.cpp:114
Bimage::images
long images()
Definition: Bimage.h:504
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
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
Boption::value
Bstring value
Definition: options.h:30
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
xml_write_fsc
void xml_write_fsc(Bstring &filename, Bplot *plot)
Definition: rwFSC_XML.cpp:218
Bimage::resolution_prepare
Bimage * resolution_prepare(Bimage *p)
Definition: Bimage_resolution.cpp:17
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
verbose
int verbose
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
utilities.h
Header file for general utilities.
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::fsc
Bplot * fsc(double hi_res, double sampling_ratio=1)
Definition: Bimage_resolution.cpp:245
Bplot::write_tsv
void write_tsv(Bstring &filename)
Definition: Bplot.h:272
main
int main(int argc, char *argv[])
Definition: bresolve.cpp:51
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
Bimage::filter_average
int filter_average(long kernel_size)
Applies an averaging filter to an image.
Definition: Bimage_filter.cpp:207
Boption::filename
Bstring filename()
Definition: options.h:85
use
const char * use[]
Definition: bresolve.cpp:19
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
rwFSC_XML.h
Library routines to read and write FSC curves.
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