| Bsoft 2.0.5 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->sampling();
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->label(get_cmd_line(argc, argv));
241 p->change_type(nudatatype);
242 write_img(argv[optind], p, 0);
243 }
244
245 delete p;
246
247 #ifdef HAVE_GCD
248 fftwf_cleanup_threads();
249 #endif
250
251 if ( verbose & VERB_TIME )
252 timer_report(ti);
253
254 bexit(0);
255 }
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
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:1367
Baxis::label
Bstring & label()
Definition: Bplot.h:28
Bstring::real
double real()
Definition: Bstring.cpp:545
DataType
DataType
Base data type specifier.
Definition: utilities.h:146
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
Bimage::invert
void invert()
Inverts the data in the image.
Definition: Bimage.cpp:3108
Bplot::title
Bstring & title()
Definition: Bplot.h:189
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:1567
utilities.h
Header file for general utilities.
Boption::sampling
Vector3< double > sampling()
Definition: options.h:202
Boption::value
Bstring value
Definition: options.h:30
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Bcolumn::label
Bstring & label()
Definition: Bplot.h:63
Boption::tag
Bstring tag
Definition: options.h:29
system_processors
size_t system_processors()
Returns the number of processors.
Definition: utilities.cpp:186
main
int main(int argc, char **argv)
Definition: bampweigh.cpp:106
Bstring::length
long length() const
Definition: Bstring.cpp:263
verbose
int verbose
Bimage::fspace_weigh
int fspace_weigh(long maxrad, double *curve, int flag=0)
Definition: Bimage_fspace.cpp:797
rwFSC_XML.h
Library routines to read and write FSC curves.
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
MAXLINELEN
#define MAXLINELEN
Definition: utilities.h:44
Bimage::multiply
void multiply(long j, Complex< double > cv)
Definition: Bimage.h:312
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:1228
Bpage::column
Bcolumn & column(long i)
Definition: Bplot.h:110
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
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:76
get_cmd_line
string get_cmd_line(int argc, char **argv)
Returns the command line as a string.
Definition: utilities.cpp:36
Boption::filename
Bstring filename()
Definition: options.h:85
Bimage::fspace_weigh_dose
int fspace_weigh_dose(long nn, double dose_per_frame, vector< double > critdose)
Definition: Bimage_fspace.cpp:1398
timer.h
Utilities for timing functions.
xml_read_fsc
Bplot * xml_read_fsc(Bstring &filename, Bstring &xsdfile)
Function prototypes.
Bimage
General image parameter class.
Definition: Bimage.h:163
Bcolumn::number
long number()
Definition: Bplot.h:61
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::label
void label(string s)
Definition: Bimage.h:235
VERB_TIME
#define VERB_TIME
Definition: utilities.h:110
Bcolumn::type
long type()
Definition: Bplot.h:66
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
Bpage::axis
Baxis & axis(long id)
Definition: Bplot.h:117
use
const char * use[]
Definition: bampweigh.cpp:19
Bpage::title
Bstring & title()
Definition: Bplot.h:100
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:280
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bimage::fspace_sqrt_amp
int fspace_sqrt_amp()
Change the amlitudes to their square roots.
Definition: Bimage_fspace.cpp:427
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Baxis::flags
long flags()
Definition: Bplot.h:38
Bstring
Definition: Bstring.h:29
Bplot::page
Bpage & page(long i)
Definition: Bplot.h:211
Bimage::fspace_square_amp
int fspace_square_amp()
Change the amlitudes to their squares.
Definition: Bimage_fspace.cpp:458
Vector3< double >
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:67
read_rps
Bplot * read_rps(Bstring &filename)
Definition: bampweigh.cpp:49
Bpage::columns
void columns(long ncol)
Definition: Bplot.h:105
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:109
Bstring::integer
long integer()
Definition: Bstring.cpp:539
Unknown_Type
Definition: utilities.h:147
rwimg.h
Header file for 2D and 3D image I/O.
options.h
Header file for option handlers.
Vector3::volume
double volume()
Definition: Vector3.h:235
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:273
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:52
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:21
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:1310
Bcolumn::axis
long axis()
Definition: Bplot.h:68
Bplot
Definition: Bplot.h:146