Bsoft  2.1.0
Bernard's software package
bflat.cpp
Go to the documentation of this file.
1 
12 #include "rwimg.h"
13 #include "symmetry.h"
14 #include "utilities.h"
15 #include "options.h"
16 #include "timer.h"
17 
18 // Declaration of global variables
19 extern int verbose; // Level of output to the screen
20 
21 // Usage assistance
22 const char* use[] = {
23 " ",
24 "Usage: bflat [options] input.img output.img",
25 "-------------------------------------------",
26 "Does phase extension by solvent flattening.",
27 "The amplitudes are defined in three possible mutually exclusive ways:",
28 "1. Derived from the input map (default).",
29 "2. Derived from a reference map.",
30 "3. Specified in an external amplitude map.",
31 " ",
32 "Actions:",
33 "-symmetrize C5 Apply point group symmetry.",
34 " ",
35 "Parameters:",
36 "-verbose 7 Verbosity of output.",
37 "-datatype u Force writing of a new data type.",
38 "-resolution 35,15.5,1.5 Resolution limit start, end and step (default 30,20,1).",
39 "-iterations 80 Iterations (default 1).",
40 "-exit 0.1 Exit condition based on amplitude RMSD (default 0.01).",
41 "-output 5 Number of iterations between writing output maps (default 100).",
42 " ",
43 "Input:",
44 "-Reference file.map File to generate amplitudes from (default amplitudes from input map).",
45 "-Amplitudes file.map File with amplitudes (default amplitudes from input map).",
46 "-Mask mask.tif Mask file.",
47 " ",
48 NULL
49 };
50 
51 int main (int argc, char **argv)
52 {
53  // Initialize variables
54  Bsymmetry* sym = NULL; // Point group
55  DataType nudatatype(Unknown_Type); // Conversion to new type
56  int i; // loop variables
57  int imax(1); // largest iteration number
58  int iout(100); // iterations between writing output maps
59  double hi_res(20), lo_res(30); // High and low resolution limits
60  double step_res(1); // Resolution step size
61  double Rtarget(0.01); // Target for amplitude RMSD
62  Bstring maskfile; // Mask to use
63  Bstring reffile; // Reference file
64  Bstring ampfile; // Amplitude file
65 
66  int optind;
67  Boption* option = get_option_list(use, argc, argv, optind);
68  Boption* curropt;
69  for ( curropt = option; curropt; curropt = curropt->next ) {
70  if ( curropt->tag == "symmetrize" )
71  sym = curropt->symmetry();
72  if ( curropt->tag == "datatype" )
73  nudatatype = curropt->datatype();
74  if ( curropt->tag == "resolution" )
75  if ( curropt->values(lo_res, hi_res, step_res) < 1 )
76  cerr << "-resolution: A high resolution limit must be specified!" << endl;
77  if ( curropt->tag == "iterations" )
78  if ( ( imax = curropt->value.integer() ) < 1 )
79  cerr << "-iterations: A number of iterations must be specified!" << endl;
80  if ( curropt->tag == "output" )
81  if ( ( iout = curropt->value.integer() ) < 1 )
82  cerr << "-output: A number of iterations must be specified!" << endl;
83  if ( curropt->tag == "exit" )
84  if ( ( Rtarget = curropt->value.real() ) < 1 )
85  cerr << "-exit: A target RMSD must be specified!" << endl;
86  if ( curropt->tag == "Reference" )
87  reffile = curropt->filename();
88  if ( curropt->tag == "Amplitude" )
89  ampfile = curropt->filename();
90  if ( curropt->tag == "Mask" )
91  maskfile = curropt->filename();
92  }
93  option_kill(option);
94 
95  double ti = timer_start();
96 
97  int dataflag(0);
98  if ( optind < argc - 1 ) dataflag = 1;
99  Bimage* p = read_img(argv[optind++], dataflag, 0);
100  if ( p == NULL ) {
101  cerr << "Error: No input file read!" << endl;
102  bexit(-1);
103  }
104 
105  if ( optind >= argc ) {
106  cerr << "Error: No output file specified!" << endl;
107  bexit(-1);
108  }
109 
110 
111  if ( nudatatype == Unknown_Type )
112  nudatatype = p->data_type();
113 
114  p->change_type(Float);
115  p->rescale_to_avg_std(0, 1);
116 
117  Bimage* pmask = NULL;
118  if ( maskfile.length() ) {
119  pmask = read_img(maskfile, dataflag, 0);
120  if ( pmask == NULL ) {
121  cerr << "Error: Mask file " << maskfile << " not read!" << endl;
122  bexit(-1);
123  }
124  }
125 
126  Bimage* pamp = NULL;
127  Bimage* pref = NULL;
128 
129  if ( reffile.length() ) {
130  pref = read_img(reffile, dataflag, 0);
131  } else if ( ampfile.length() ) {
132  pamp = read_img(ampfile, dataflag, 0);
133  } else {
134  pref = p->copy();
135  }
136 
137  if ( pref ) pref->fft();
138 
139  if ( hi_res ) p->fspace_bandpass(hi_res, 1e10, 0);
140 
141  Bstring filename, outname(argv[optind]);
142  Bimage* pint = NULL;
143  double res = lo_res, R = 1e37;
144 
145  if ( verbose )
146  cout << "Iter\tRes\tRMSD" << endl;
147  for ( i=0; i<imax && R>Rtarget; i++ ) {
148  if ( res > hi_res ) res -= step_res;
149 
150  p->subtract_background();
151  p->multiply(pmask);
152 
153  p->fft();
154 
155  if ( pref ) {
156  R = p->merge_amplitudes_and_phases(pref, res, lo_res);
157  } else {
158  R = p->merge_amplitudes_and_phases(pamp);
159  p->fspace_bandpass(res, 1e10, 0);
160  }
161 
162  if ( verbose )
163  cout << i+1 << tab << res << tab << R << endl;
164 
165  p->fft_back();
166 
167  if ( sym ) p->symmetrize(sym, 1);
168 
169  p->statistics();
170 
171  if ( (i+1)%iout == 0 && (i+1) != imax ) {
172  filename = outname.pre_rev('.') + Bstring(i+1, "_%04d.") + outname.post_rev('.');
173  pint = p->copy();
174  pint->change_type(nudatatype);
175  write_img(filename, pint, 0);
176  delete pint;
177  }
178  }
179 
180  if ( optind < argc ) {
181  p->change_type(nudatatype);
182  write_img(argv[optind], p, 0);
183  }
184 
185  delete p;
186  delete pamp;
187  delete pmask;
188  kill_symmetry(sym);
189 
190  if ( verbose & VERB_TIME )
191  timer_report(ti);
192 
193  bexit(0);
194 }
195 
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::symmetrize
double symmetrize(Bstring &symmetry_string, int flag)
Definition: Bimage.h:1188
verbose
int verbose
Bimage::merge_amplitudes_and_phases
double merge_amplitudes_and_phases(Bimage *pamp)
Merges the amplitudes from one map with the phases of another.
Definition: Bimage_complex.cpp:1223
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
Bimage::copy
Bimage * copy()
Copies the header information and data of an image into a new image structure.
Definition: Bimage.cpp:2702
Boption::datatype
DataType datatype()
Definition: options.h:93
symmetry.h
Header file for general symmetry functions.
Boption::next
Boption * next
Definition: options.h:28
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
Bstring::real
double real()
Definition: Bstring.cpp:553
options.h
Header file for option handlers.
Bimage::fft_back
int fft_back()
Definition: Bimage.h:831
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
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
Bimage::fft
int fft()
Definition: Bimage.h:830
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
timer.h
Utilities for timing functions.
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
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::fspace_bandpass
int fspace_bandpass(double res_hi, double res_lo=0, double width=0)
Applies a bandpass filter to an image.
Definition: Bimage_fspace.cpp:496
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
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::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.
main
int main(int argc, char **argv)
Definition: bflat.cpp:51
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
use
const char * use[]
Definition: bflat.cpp:22
Bimage::subtract_background
int subtract_background()
Subtracts the background for each sub-image.
Definition: Bimage_background.cpp:337
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Float
@ Float
Definition: utilities.h:161
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
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