| Bsoft 2.0.5 Bernard's software package

bfil.cpp
Go to the documentation of this file.
1
9 #include "mg_processing.h"
10 #include "mg_helix.h"
11 #include "mg_extract.h"
12 #include "rwmg.h"
13 #include "utilities.h"
14 #include "options.h"
15 #include "timer.h"
16
17 // Declaration of global variables
18 extern int verbose; // Level of output to the screen
19
20 // Usage assistance
21 const char* use[] = {
22 " ",
23 "Usage: bfil [options] input.star [input2.star ...]",
24 "--------------------------------------------------",
25 "Extracts filaments from a micrograph as straightened filaments or boxed particles.",
26 " ",
27 "Actions:",
28 "-reconstructions Operate on reconstruction parameters rather than micrographs.",
29 "-center 85 Center filament nodes with the given window width.",
30 "-density 25 Estimate filament density per length with the given filament width.",
31 "-extract 150,y Extract filaments with this width (voxels) along an axis (optional).",
32 "-split Split filaments into individual files.",
33 " ",
34 "Actions for boxing:",
35 "-box 125,125,1 Box particles along filaments with this size (voxels, one sets all).",
36 "-interval 25.5 Interval of boxes (default: box radius).",
37 "-maskwidth 50 Width to mask boxed images (voxels).",
38 "-background Correct background of extracted particles.",
39 "-normalize Normalize extracted particles.",
40 "-rotate y Rotate particles from filaments to align with an axis.",
41 "-powerspectrum 200 Calculates average power spectra from extracted particles with optional padding.",
42 " ",
43 "Parameters:",
44 "-verbose 7 Verbosity of output.",
45 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; a single value can be given).",
46 "-base fil_001 Set the filament/particle base file name (no extension).",
47 "-path dir/subdir Set the filament/particle file paths.",
48 "-extension pif Set the filament/particle image file format.",
49 " ",
50 "Parameters for boxing:",
51 "-helix 16.2,39.5 Helical parameters for filament boxing: helical rise ",
52 " and angle per asymmetric unit (angstroms and degrees).",
53 "-fill 127 Fill value for particles extending beyond image (default background).",
54 " ",
55 "Output:",
56 "-output file.star Output parameter file.",
57 "-Densities img.mrc One dimensional image with filament densities.",
58 "-Postscript plot.ps Histogram of filament densities.",
59 " ",
60 NULL
61 };
62
63 int main(int argc, char* argv[])
64 {
65 // Initialize optional variables
66 int use_rec(0); // Flag to process reconstructions
67 int split(0); // Flag to split output into individual files
68 Vector3<double> sam; // Units for the three axes (A/pixel)
69 Bstring base; // Particle base file name
70 Bstring path; // Particle file path
71 Bstring ext; // Particle image file format
72 int center_width(0); // Filament width for centering nodes
73 int filament_density(0); // Filament width for density estimates
74 int filament_width(0); // Filament width for extraction
75 int filament_axis(0); // Filament axis for extraction
76 char axis_char; // Filament axis character
77 double boxing_interval(0); // Boxing interval for defining particles along a filament
78 int mask_width(0); // Filament mask width
79 double helix_rise(0), helix_angle(0); // Helical parameters for filaments
80 Vector3<long> box_size; // Box size for extracting particles
81 int back_flag(0); // Background correction for extracting particles
82 int norm_flag(0); // Normalization for extracting particles
83 int powerspectrum(0); // Flag to calculate average power spectra
84 int pad(0); // Padding for power spectrum
85 int fill_type(FILL_BACKGROUND);
86 double fill(0);
87 int rotation_axis(0); // Axis to rotate particles from filaments, 0=no rotation
88 Bstring outfile; // Output parameter file name
89 Bstring densfile; // Output filament densities file
90 Bstring psfile; // Postscript output file
91
92 int optind;
93 Boption* option = get_option_list(use, argc, argv, optind);
94 Boption* curropt;
95 for ( curropt = option; curropt; curropt = curropt->next ) {
96 if ( curropt->tag == "reconstructions" ) use_rec = 1;
97 if ( curropt->tag == "split" ) split = 1;
98 if ( curropt->tag == "center" )
99 if ( ( center_width = curropt->value.integer() ) < 1 )
100 cerr << "-center: A filament width must be specified." << endl;
101 if ( curropt->tag == "density" )
102 if ( ( filament_density = curropt->value.integer() ) < 1 )
103 cerr << "-density: A filament width must be specified." << endl;
104 if ( curropt->tag == "extract" ) {
105 filament_width = curropt->value.integer();
106 if ( filament_width < 1 )
107 cerr << "-extract: A filament width must be specified." << endl;
108 else if ( curropt->value.contains(",") ) {
109 axis_char = curropt->value.post(',')[0];
110 if ( axis_char == '1' || axis_char == 'x' ) filament_axis = 1;
111 if ( axis_char == '2' || axis_char == 'y' ) filament_axis = 2;
112 if ( axis_char == '3' || axis_char == 'z' ) filament_axis = 3;
113 }
114 }
115 if ( curropt->tag == "box" ) {
116 box_size = curropt->size();
117 if ( box_size.volume() < 1 )
118 cerr << "-box: A boxing size must be specified." << endl;
119 }
120 if ( curropt->tag == "interval" )
121 if ( ( boxing_interval = curropt->value.real() ) < 1 )
122 cerr << "-interval: A boxing interval must be specified." << endl;
123 if ( curropt->tag == "maskwidth" )
124 if ( ( mask_width = curropt->value.integer() ) < 1 )
125 cerr << "-maskwidth: A mask width must be specified." << endl;
126 if ( curropt->tag == "background" ) back_flag = 1;
127 if ( curropt->tag == "normalize" ) norm_flag = 1;
128 if ( curropt->tag == "powerspectrum" ) {
129 powerspectrum = 1;
130 if ( ( pad = curropt->value.integer() ) < 1 )
131 cerr << "-powerspectrum: A padding value must be specified!" << endl;
132 }
133 if ( curropt->tag == "sampling" )
134 sam = curropt->sampling();
135 if ( curropt->tag == "base" ) {
136 base = curropt->value;
137 if ( base.length() < 1 )
138 cerr << "-base: The particle base file name must be specified!" << endl;
139 }
140 if ( curropt->tag == "path" ) {
141 path = curropt->value;
142 if ( path.length() < 1 )
143 cerr << "-path: The particle file path must be specified!" << endl;
144 else
145 if ( path[-1] != '/' ) path += "/";
146 }
147 if ( curropt->tag == "extension" ) {
148 ext = curropt->value;
149 if ( ext.length() < 1 )
150 cerr << "-extension: The particle file extension must be specified!" << endl;
151 }
152 if ( curropt->tag == "helix" ) {
153 if ( curropt->values(helix_rise, helix_angle) < 2 )
154 cerr << "-helix: Both helical rise and angle per unit must be specified." << endl;
155 else
156 helix_angle *= M_PI/180.0;
157 }
158 if ( curropt->tag == "fill" )
159 fill = curropt->fill(fill_type);
160 if ( curropt->tag == "rotate" ) {
161 if ( ( axis_char = curropt->value[0] ) < 1 )
162 cerr << "-rotate: An axis must be specified!" << endl;
163 else {
164 if ( axis_char == '1' || axis_char == 'x' ) rotation_axis = 1;
165 if ( axis_char == '2' || axis_char == 'y' ) rotation_axis = 2;
166 if ( axis_char == '3' || axis_char == 'z' ) rotation_axis = 3;
167 }
168 }
169 if ( curropt->tag == "output" )
170 outfile = curropt->filename();
171 if ( curropt->tag == "Densities" )
172 densfile = curropt->filename();
173 if ( curropt->tag == "Postscript" )
174 psfile = curropt->filename();
175 }
176 option_kill(option);
177
178 double ti = timer_start();
179
180 // Read all the parameter files
181 Bstring* file_list = NULL;
182 while ( optind < argc ) string_add(&file_list, argv[optind++]);
183 if ( !file_list ) {
184 cerr << "Error: No parameter files specified!" << endl;
185 bexit(-1);
186 }
187
188 Bproject* project = read_project(file_list);
189 string_kill(file_list);
190
191 if ( project == NULL ) {
192 cerr << "Error: No input file read!" << endl;
193 bexit(-1);
194 }
195
196 if ( use_rec ) project->select = 1;
197 else box_size[2] = 1;
198
199 project_update_comment(project, argc, argv);
200
201 Bimage* pdens = NULL;
202 Bplot* plot = NULL;
203
204 if ( sam[0] > 0 )
205 project_set_pixel_size(project, sam[0]);
206
207 if ( box_size[0] > 0 )
208 project_set_particle_box_size(project, box_size);
209
210 if ( center_width > 0 )
211 project_center_filaments(project, center_width);
212
213 if ( filament_density ) {
214 pdens = project_filament_density(project, filament_density);
215 plot = pdens->histogram_gauss_plot(100, 2);
216 // if ( verbose )
217 // cout << pdens->average() << tab << pdens->standard_deviation() << endl;
218 pdens->statistics();
219 pdens->label(get_cmd_line(argc, argv));
220 if ( densfile.length() ) write_img(densfile, pdens, 0);
221 delete pdens;
222 }
223
224 if ( filament_width > 0 )
225 project_extract_filaments(project, filament_width, filament_axis, base, path, ext, split);
226
227 if ( path.length() && box_size[0] )
228 project_set_particle_path(project, path);
229
230 if ( boxing_interval > 0 )
231 project_filaments_to_particles(project, box_size, boxing_interval, helix_rise, helix_angle);
232
233 if ( box_size[0] > 0 ) {
234 if ( rotation_axis > 0 ) {
235 project_extract_particles(project, 1, back_flag, norm_flag,
236 fill_type, fill, 0, project->select, base, path, ext);
237 project_rotate_mask_filament_particles(project, rotation_axis, back_flag, mask_width);
238 } else {
239 project_extract_particles(project, 1, back_flag, norm_flag,
240 fill_type, fill, mask_width, project->select, base, path, ext);
241 }
242 }
243
244 if ( powerspectrum )
245 project_filament_powerspectrum(project, pad, rotation_axis, path);
246
247 if ( outfile.length() )
248 write_project(outfile, project, 0, 0);
249
250 if ( psfile.length() && plot )
251 ps_plot(psfile, plot);
252
253 project_kill(project);
254 delete plot;
255
256 if ( verbose & VERB_TIME )
257 timer_report(ti);
258
259 return 0;
260 }
261
write_project
int write_project(const char *filename, Bproject *project)
Writing micrograph parameters to a file.
Definition: rwmg.cpp:320
rwmg.h
Header file for reading and writing micrograph parameters.
project_set_particle_path
int project_set_particle_path(Bproject *project, Bstring &path)
Sets the path to particle files for all the micrographs.
Definition: mg_processing.cpp:3254
Bstring::real
double real()
Definition: Bstring.cpp:545
Boption::fill
double fill(int &fill_type)
Definition: options.h:138
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:705
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:174
Bstring::post
Bstring post(char c)
Definition: Bstring.cpp:394
project_filaments_to_particles
int project_filaments_to_particles(Bproject *project, Vector3< long > box_size, double boxing_interval, double rise, double angle)
Converts filaments to sets of particles.
Definition: mg_extract.cpp:914
utilities.h
Header file for general utilities.
Boption::sampling
Vector3< double > sampling()
Definition: options.h:202
Boption::value
Bstring value
Definition: options.h:30
Boption::tag
Bstring tag
Definition: options.h:29
project_rotate_mask_filament_particles
int project_rotate_mask_filament_particles(Bproject *project, int rotation_axis, int back_flag, int mask_width)
Rotates and masks particles extracted from filaments.
Definition: mg_extract.cpp:1115
read_project
Bproject * read_project(const char *filename, int flags=0)
Reading micrograph parameters from files.
Definition: rwmg.cpp:140
Bstring::length
long length() const
Definition: Bstring.cpp:263
project
Bproject * project
Definition: bshow.cpp:31
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
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
timer.h
Utilities for timing functions.
Bimage::histogram_gauss_plot
Bplot * histogram_gauss_plot(long bins, long ngauss=1)
Fits a gaussian function to a histogram of an image.
Definition: Bimage_histogram.cpp:695
Bimage
General image parameter class.
Definition: Bimage.h:163
Boption::size
Vector3< long > size()
Definition: options.h:163
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:584
project_set_particle_box_size
int project_set_particle_box_size(Bproject *project, Vector3< long > box_size)
Sets particle box size to the given value.
Definition: mg_processing.cpp:3693
M_PI
#define M_PI
Definition: utilities.h:59
base
string base(string &filename)
Definition: string_util.cpp:28
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
Boption
A class for a command line input option.
Definition: options.h:26
use
const char * use[]
Definition: bfil.cpp:21
Bimage::label
void label(string s)
Definition: Bimage.h:235
VERB_TIME
#define VERB_TIME
Definition: utilities.h:110
mg_extract.h
Header file for functions to extract particles from micrographs.
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:280
mg_helix.h
Header file for functions to process helical data.
ps_plot
int ps_plot(Bstring filename, Bplot *plot)
Generates postscript graphs.
Definition: ps_plot.cpp:128
project_extract_particles
long project_extract_particles(Bproject *project, double scale, int back_flag, int norm_flag, int fill_type, double fill, int mask_width, int split, Bstring &partbase, Bstring &partpath, Bstring &partext)
Extracts particle images from micrographs defined in a project.
Definition: mg_extract.cpp:160
Boption::next
Boption * next
Definition: options.h:28
project_extract_filaments
long project_extract_filaments(Bproject *project, int filament_width, int axis, Bstring &base, Bstring &path, Bstring &ext, int split)
Extracts filament images from micrographs defined in a project.
Definition: mg_extract.cpp:674
Bstring
Definition: Bstring.h:29
project_kill
int project_kill(Bproject *project)
Frees a whole project.
Definition: mg_processing.cpp:1224
Bproject::select
int select
Definition: mg_processing.h:339
verbose
int verbose
project_set_pixel_size
int project_set_pixel_size(Bproject *project, double pixel_size)
Sets the pixel size for all the micrographs.
Definition: mg_processing.cpp:3398
main
int main(int argc, char *argv[])
Definition: bfil.cpp:63
project_center_filaments
double project_center_filaments(Bproject *project, int filament_width)
Centers filament nodes in all micrographs in a project.
Definition: mg_helix.cpp:221
Vector3< double >
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:67
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
mg_processing.h
Header file for micrograph processing.
project_filament_powerspectrum
int project_filament_powerspectrum(Bproject *project, int pad, int rotated, Bstring &path)
Calculates and average power spectrum from filaments.
Definition: mg_helix.cpp:278
options.h
Header file for option handlers.
project_filament_density
Bimage * project_filament_density(Bproject *project, int filament_width)
Extracts filament images from micrographs defined in a project and estimates their density per length...
Definition: mg_helix.cpp:375
split
vector< string > split(const string &s)
Splits a string on whitespace.
Definition: string_util.cpp:92
Vector3::volume
double volume()
Definition: Vector3.h:235
project_update_comment
int project_update_comment(Bproject *project, int n, char **strings)
Puts a set of strings and time in the main comment of a project.
Definition: rwmg.cpp:468
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
Bproject
Definition: mg_processing.h:333
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:760
Bplot
Definition: Bplot.h:146