Bsoft  2.1.0
Bernard's software package
bampweigh.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: bampweigh [options] input.img output.img",
22 "-----------------------------------------------",
23 "Weighs the amplitudes of a map using a reference map's amplitudes.",
24 " ",
25 "Actions:",
26 "-invert Invert density in the image.",
27 "-rescale -0.1,5.2 Rescale data to average and standard deviation after filtering.",
28 "-dose 2.5 Weigh the amplitudes by accumulated dose using the given dose/frame (e/Å2).",
29 "-normalize 2 Normalize amplitudes (1:amplitude; 2:power).",
30 "-root Weigh by the square root of the amplitudes.",
31 "-square Weigh by the squares of the amplitudes.",
32 " ",
33 "Parameters:",
34 "-verbose 7 Verbosity of output.",
35 "-datatype u Force writing of a new data type.",
36 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; default from input file; a single value can be given).",
37 "-resolution 15 Resolution limit (angstrom)",
38 " ",
39 "Input:",
40 "-RPS file.txt Radial power spectrum.",
41 "-FSC file.xml FSC curve.",
42 "-Reference file.map Reference map.",
43 "-Mask mask.tif Mask file to exclude regions in reciprocal space.",
44 "-mask mask.mrc Real space mask to apply before weighing.",
45 " ",
46 NULL
47 };
48 
49 Bplot* read_rps(Bstring& filename)
50 {
51  if ( verbose & VERB_PROCESS )
52  cout << "# Reading radial power spectrum: " << filename << endl;
53 
54  ifstream f;
55  f.open(filename.c_str());
56  if ( f.fail() ) return NULL;
57 
58  int i, j, ncol(2), nrow(0);
59  char aline[MAXLINELEN];
60  double s, p;
61  vector<double> vs, vp;
62 
63  f.getline(aline, MAXLINELEN);
64 
65  while ( !f.eof() ) {
66  f >> s >> p;
67  if ( p ) {
68  vs.push_back(s);
69  vp.push_back(p);
70  nrow++;
71  }
72  }
73 
74  f.close();
75 
76  Bstring title = "RPS Plot";
77  Bplot* plot = new Bplot(1, nrow, ncol);
78  plot->title(title);
79  plot->page(0).title(title);
80  plot->page(0).columns(ncol);
81  for ( i=0; i<ncol; i++ ) plot->page(0).column(i).number(i);
82  plot->page(0).column(0).label("s");
83  plot->page(0).column(1).label("Power");
84 // plot->page(0).axis(1).min(0);
85 // plot->page(0).axis(1).max(1/hi_res);
86 // plot->page(0).axis(1).inc(0.1/hi_res);
87  plot->page(0).axis(1).flags(1);
88  plot->page(0).axis(1).label("Spatial Frequency (1/Å)");
89 // plot->page(0).axis(3).min(0);
90 // plot->page(0).axis(3).max(1);
91 // plot->page(0).axis(3).inc(0.1);
92  plot->page(0).column(1).type(2);
93  plot->page(0).column(0).axis(1);
94  plot->page(0).column(1).axis(3);
95 
96  for ( i=0, j=nrow; i<nrow; ++i, ++j ) {
97  (*plot)[i] = vs[i];
98  (*plot)[j] = vp[i];
99  if ( verbose & VERB_DEBUG )
100  cout << vs[i] << tab << vp[i] << endl;
101  }
102 
103  return plot;
104 }
105 
106 int main(int argc, char **argv)
107 {
108  // Initialize variables
109  int setinvert(0); // Flag to invert density
110  double weigh_dose(0); // Dose per frame to weigh by dose
111  int normalize(0); // Flag to normalize amplitudes
112  int root(0); // Flag to weigh by sqrt of amplitudes
113  int square(0); // Flag to weigh by squares of amplitudes
114  DataType nudatatype(Unknown_Type); // Conversion to new type
115  Vector3<double> sam; // Units for the three axes (A/pixel)
116  double resolution(0); // Resolution limit
117  double nuavg(0), nustd(0); // Values for rescaling
118  Bstring rpsfile; // Radial power spectrum
119  Bstring fscfile; // FSC curve
120  Bstring reffile; // Reference map
121  Bstring maskfile; // Mask to exclude regions in reciprocal space
122  Bstring realmask; // Real space mask
123 
124  int optind;
125  Boption* option = get_option_list(use, argc, argv, optind);
126  Boption* curropt;
127  for ( curropt = option; curropt; curropt = curropt->next ) {
128  if ( curropt->tag == "invert" ) setinvert = 1;
129  if ( curropt->tag == "dose" )
130  if ( ( weigh_dose = curropt->value.real() ) < 0.001 )
131  cerr << "-dose: The dose per frame must be specified!" << endl;
132  if ( curropt->tag == "normalize" )
133  if ( ( normalize = curropt->value.integer() ) < 1 )
134  cerr << "-normalize: A normalization option must be specified!" << endl;
135  if ( curropt->tag == "root" ) root = 1;
136  if ( curropt->tag == "square" ) square = 1;
137  if ( curropt->tag == "datatype" )
138  nudatatype = curropt->datatype();
139  if ( curropt->tag == "sampling" )
140  sam = curropt->scale();
141  if ( curropt->tag == "resolution" )
142  if ( ( resolution = curropt->value.real() ) < 0.1 )
143  cerr << "-resolution: A resolution must be specified!" << endl;
144  if ( curropt->tag == "rescale" ) {
145  if ( curropt->values(nuavg, nustd) < 2 )
146  cerr << "-rescale: Both average and standard deviation must be specified!" << endl;
147  else if ( nustd <= 0 )
148  cerr << "-rescale: A positive standard deviation must be specified!" << endl;
149  }
150  if ( curropt->tag == "RPS" )
151  rpsfile = curropt->filename();
152  if ( curropt->tag == "FSC" )
153  fscfile = curropt->filename();
154  if ( curropt->tag == "Reference" )
155  reffile = curropt->filename();
156  if ( curropt->tag == "Mask" )
157  maskfile = curropt->filename();
158  if ( curropt->tag == "mask" )
159  realmask = curropt->filename();
160  }
161  option_kill(option);
162 
163  double ti = timer_start();
164 
165 #ifdef HAVE_GCD
166  fftwf_init_threads();
167  fftwf_plan_with_nthreads(system_processors());
168 #endif
169  if ( verbose )
170  cout << "Number of threads: " << system_processors() << endl;
171 
172  // Read image file
173  Bimage* p = read_img(argv[optind++], 1, -1);
174  if ( p == NULL ) bexit(-1);
175 
176  if ( nudatatype == Unknown_Type )
177  nudatatype = p->data_type(); // Preserve old type
178 
179  if ( sam.volume() > 0 ) p->sampling(sam);
180 
181  if ( setinvert ) p->invert();
182 
183  Bplot* plot = NULL;
184  if ( fscfile.length() ) {
185  plot = xml_read_fsc(fscfile);
186  if ( !plot ) {
187  cerr << "Error: The FSC file " << fscfile << " was not read!" << endl;
188  bexit(-1);
189  }
190  } else if ( rpsfile.length() ) {
191  plot = read_rps(rpsfile);
192  if ( !plot ) {
193  cerr << "Error: The RPS file " << rpsfile << " was not read!" << endl;
194  bexit(-1);
195  }
196  }
197 
198  Bimage* pref = NULL;
199  Bimage* pmask = NULL;
200  Bimage* prealmask = NULL;
201  if ( reffile.length() && ( pref = read_img(reffile, 1, -1) ) ) {
202  if ( maskfile.length() )
203  pmask = read_img(maskfile, 1, -1);
204  }
205 
206  if ( realmask.length() ) {
207  prealmask = read_img(realmask, 1, -1);
208  if ( prealmask ) {
209  p->multiply(prealmask);
210  delete prealmask;
211  }
212  }
213 
214  if ( pref ) {
215  p->fspace_weigh(pref, pmask, resolution);
216  delete pref;
217  } else if ( plot ) {
218  if ( fscfile.length() )
219  p->fspace_weigh_FSC_curve(plot, resolution);
220  else if ( rpsfile.length() )
221  p->fspace_weigh_RPS_curve(plot, resolution);
222  } else if ( weigh_dose ) {
223  p->fspace_weigh_dose(weigh_dose);
224  } else if ( normalize > 0 ) {
225  p->fspace_normalize_radial(pmask, resolution, (normalize==1));
226  } else if ( root ) {
227  p->fspace_sqrt_amp();
228  } else if ( square ) {
229  p->fspace_square_amp();
230  } else {
231  p->fspace_weigh_C_curve(resolution);
232  }
233 
234  if ( plot ) delete plot;
235  if ( pmask ) delete pmask;
236 
237  if ( nustd > 0 ) p->rescale_to_avg_std(nuavg, nustd);
238 
239  if ( optind < argc ) {
240  p->change_type(nudatatype);
241  write_img(argv[optind], p, 0);
242  }
243 
244  delete p;
245 
246 #ifdef HAVE_GCD
247  fftwf_cleanup_threads();
248 #endif
249 
250  if ( verbose & VERB_TIME )
251  timer_report(ti);
252 
253  bexit(0);
254 }
Boption
A class for a command line input option.
Definition: options.h:26
MAXLINELEN
#define MAXLINELEN
Definition: utilities.h:45
Boption::scale
Vector3< double > scale()
Definition: options.h:190
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Baxis::flags
long flags()
Definition: Bplot.h:38
system_processors
size_t system_processors()
Returns the number of processors.
Definition: utilities.cpp:193
Bstring
Definition: Bstring.h:29
use
const char * use[]
Definition: bampweigh.cpp:19
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::fspace_weigh_dose
int fspace_weigh_dose(long nn, double dose_per_frame, vector< double > critdose)
Definition: Bimage_fspace.cpp:1460
Bimage::fspace_square_amp
int fspace_square_amp()
Change the amlitudes to their squares.
Definition: Bimage_fspace.cpp:455
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.
Bcolumn::type
long type()
Definition: Bplot.h:66
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Bcolumn::axis
long axis()
Definition: Bplot.h:68
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bimage::fspace_weigh_FSC_curve
int fspace_weigh_FSC_curve(Bplot *plot, double resolution=0)
Weighs an image's amplitudes with a given FSC curve.
Definition: Bimage_fspace.cpp:1365
Bpage::axis
Baxis & axis(long id)
Definition: Bplot.h:117
Bpage::title
Bstring & title()
Definition: Bplot.h:100
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
xml_read_fsc
Bplot * xml_read_fsc(Bstring &filename, Bstring &xsdfile)
Function prototypes.
Bimage::fspace_weigh_C_curve
int fspace_weigh_C_curve(double resolution=0)
Weighs an image's amplitudes with the carbon scattering curve.
Definition: Bimage_fspace.cpp:1226
Boption::value
Bstring value
Definition: options.h:30
Bimage
General image parameter class.
Definition: Bimage.h:177
verbose
int verbose
timer.h
Utilities for timing functions.
Bpage::column
Bcolumn & column(long i)
Definition: Bplot.h:110
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
Bplot::title
Bstring & title()
Definition: Bplot.h:194
Bimage::fspace_weigh
int fspace_weigh(long maxrad, double *curve, int flag=0)
Definition: Bimage_fspace.cpp:795
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
Baxis::label
Bstring & label()
Definition: Bplot.h:28
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
Bimage::fspace_normalize_radial
int fspace_normalize_radial(Bimage *pmask, double resolution=0, int flag=0)
Normalizes an image's amplitudes.
Definition: Bimage_fspace.cpp:1662
read_rps
Bplot * read_rps(Bstring &filename)
Definition: bampweigh.cpp:49
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
main
int main(int argc, char **argv)
Definition: bampweigh.cpp:106
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::fspace_sqrt_amp
int fspace_sqrt_amp()
Change the amlitudes to their square roots.
Definition: Bimage_fspace.cpp:424
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::invert
void invert()
Inverts the data in the image.
Definition: Bimage.cpp:3111
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
Bcolumn::label
Bstring & label()
Definition: Bplot.h:63
Bimage::fspace_weigh_RPS_curve
int fspace_weigh_RPS_curve(Bplot *plot, double resolution=0)
Weighs an image's amplitudes with a given RPS curve.
Definition: Bimage_fspace.cpp:1308
Bplot::page
Bpage & page(long i)
Definition: Bplot.h:216
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bcolumn::number
long number()
Definition: Bplot.h:61
rwFSC_XML.h
Library routines to read and write FSC curves.
Bpage::columns
void columns(long ncol)
Definition: Bplot.h:105
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