Bsoft  2.1.0
Bernard's software package
bmissing.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "rwmg.h"
11 #include "mg_tomo_rec.h"
12 #include "file_util.h"
13 #include "options.h"
14 #include "utilities.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: bmissing [options] input.img/mg.star output.img",
24 "------------------------------------------------------",
25 "Generates binary masks corresponding to reciprocal space regions.",
26 "The selected regions have value 1 and the omitted regions 0.",
27 "The default is a blank mask created of the same size as the input image.",
28 " ",
29 "Actions for mask creation:",
30 "-create 20,60,30 Create a new mask of this size in place of input image",
31 " or using the micrograph orientations in the parameter file.",
32 "-find wedge Find the missing regions and create a new mask.",
33 " ",
34 "Actions for mask modification:",
35 "-wedge 45,-70,65 Missing wedge mask: tilt axis angle, first and last tilt angles.",
36 "-pyramid 40,-60,60,130,-55,60 Missing pyramid mask: 2 sets of tilt axis angle, first and last tilt angles.",
37 "-cone 65 Missing cone mask: one tilt angle.",
38 "-invert Invert mask just before writing the image.",
39 "-dilate 2 Dilate mask a number of times.",
40 " ",
41 "Parameters:",
42 "-verbose 1 Verbosity of output.",
43 "-datatype u Force writing of a new data type.",
44 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; a single value can be given).",
45 "-origin 110,50,44 Origin for the mask (default 0,0,0).",
46 //"-fill 28.3 Fill value (default average).",
47 "-wrap Flag to wrap around image boundaries (default not).",
48 "-resolution 20 Resolution limit (angstrom, default Nyquist).",
49 " ",
50 "Parameters for micrograph orientations:",
51 "-scale 1.2 Scale or magnification of mask compared to original images (default 1).",
52 " ",
53 "Examples:",
54 "1. Creating a new mask:",
55 " bmissing -v 7 -create 100,100,50 -wedge 45,-60,65 -origin 50,50,25 mask.mrc",
56 " ",
57 "2. Generating a mask from an image:",
58 " bmissing -v 7 -pyramid 30,-30,70,120,-60,50 -wrap input.map mask.mrc",
59 " ",
60 "3. Generating a mask from micrograph orientations:",
61 " bmissing -v 7 -create 100,100,50 -wrap mg.star mask.pif",
62 " ",
63 NULL
64 };
65 
66 int main(int argc, char **argv)
67 {
68  // Initialize variables
69  DataType nudatatype(Unknown_Type); // Conversion to new type
70  Vector3<double> sam; // Units for the three axes (A/pixel)
71  int invert(0); // Inversion flag
72 // int operation(1); // Default operation is and
73  Vector3<long> nusize; // Size of new mask
74  Bstring find; // Type of missing regions to find in input image
75  double tilt_axis(0); // Tilt axis angle
76  double tilt_neg(0); // First or negative tilt angle
77  double tilt_pos(0); // Last or positive tilt angle
78  double tilt_axis2(0); // Tilt axis angle
79  double tilt_neg2(0); // First or negative tilt angle
80  double tilt_pos2(0); // Last or positive tilt angle
81  double tilt_cone(0); // Cone tilt angle
82  int dilate(0); // Number of times to dilate
83  int set_origin(0); // Flag to set origin
84  Vector3<double> ori; // Mask origin
85 // int wrap(0); // Flag to wrap
86  double scale(1); // Scale or magnification of mask compared to original images
87  double resolution(0); // Must be set > 0 to limit resolution
88 // int fill_type = FILL_USER; // Use 1
89 // double fill = 1; // Default fill is 1
90 
91  int i, optind;
92  Boption* option = get_option_list(use, argc, argv, optind);
93  Boption* curropt;
94  for ( curropt = option; curropt; curropt = curropt->next ) {
95  if ( curropt->tag == "create" )
96  nusize = curropt->size();
97  if ( curropt->tag == "find" )
98  find = curropt->value;
99  if ( curropt->tag == "datatype" )
100  nudatatype = curropt->datatype();
101  if ( curropt->tag == "sampling" )
102  sam = curropt->scale();
103  if ( curropt->tag == "wedge" ) {
104  if ( ( i = curropt->values(tilt_axis, tilt_neg, tilt_pos) ) < 2 )
105  cerr << "-wedge: At least the tilt axis and one angle must be specified" << endl;
106  else {
107  tilt_axis *= M_PI/180.0;
108  tilt_neg *= M_PI/180.0;
109  tilt_pos *= M_PI/180.0;
110  if ( i < 3 ) tilt_pos = tilt_neg;
111  }
112  }
113  if ( curropt->tag == "pyramid" ) {
114  vector<double> d = curropt->value.split_into_doubles(",");
115  if ( d.size() < 4 )
116  cerr << "-pyramid: At least the four angles must be specified" << endl;
117  else {
118  tilt_axis = d[0] * M_PI/180.0;
119  tilt_neg = d[1] * M_PI/180.0;
120  tilt_pos = d[2] * M_PI/180.0;
121  tilt_axis2 = d[3] * M_PI/180.0;
122  if ( d.size() < 5 ) tilt_neg2 = tilt_neg;
123  else tilt_neg2 = d[4] * M_PI/180.0;
124  if ( d.size() < 6 ) tilt_pos2 = tilt_pos;
125  else tilt_pos2 = d[5] * M_PI/180.0;
126  }
127  }
128  if ( curropt->tag == "cone" ) {
129  if ( ( tilt_cone = curropt->value.real() ) < 1 )
130  cerr << "-cone: A tilt angle must be specified" << endl;
131  else
132  tilt_cone *= M_PI/180.0;
133  }
134  if ( curropt->tag == "dilate" )
135  if ( ( dilate = curropt->value.integer() ) < 1 )
136  cerr << "-dilate: A number of times to dilate must be specified!" << endl;
137  if ( curropt->tag == "origin" ) {
138  if ( curropt->value[0] == 'c' ) {
139  set_origin = 2;
140  } else {
141  ori = curropt->origin();
142  set_origin = 1;
143  }
144  }
145 // if ( curropt->tag == "fill" )
146 // fill = curropt->fill(fill_type);
147 // if ( curropt->tag == "wrap" ) wrap = 1;
148  if ( curropt->tag == "invert" ) invert = 1;
149 /* if ( curropt->tag == "operation" ) {
150  if ( curropt->value.contains("xor") ) operation = 3;
151  else if ( curropt->value.contains("or") ) operation = 2;
152  else if ( curropt->value.contains("and") ) operation = 1;
153  else operation = 0;
154  }*/
155  if ( curropt->tag == "resolution" )
156  if ( ( resolution = curropt->value.real() ) < 0.001 )
157  cerr << "-resolution: A resolution limit must be specified!" << endl;
158  if ( curropt->tag == "scale" )
159  if ( ( scale = curropt->value.real() ) < 0.0001 )
160  cerr << "-scale: A scale must be specified!" << endl;
161  }
162  option_kill(option);
163 
164  double ti = timer_start();
165 
166  Bimage* p = NULL;
167  Bproject* project = NULL;
168  Bstring filename(argv[optind++]);
169 
170  if ( file_type(filename) == Image ) {
171  p = read_img(filename, 1, 0);
172  if ( p == NULL ) bexit(-1);
173  filename = argv[optind];
174 // nusize = p->size();
175  } else if ( file_type(filename) == Micrograph ) {
176  project = read_project(filename);
177  project->select = 1;
178  if ( nusize.volume() > 0 )
180  p = project_missing_mask(project, nusize, ori, resolution, scale);
181  filename = argv[optind];
182  } else if ( nusize.volume() > 0 ) {
183  if ( nudatatype == Unknown_Type ) nudatatype = UCharacter;
184  p = new Bimage(nudatatype, TSimple, nusize, 1);
185  }
186 
187  if ( p == NULL ) bexit(-1);
188 
189  if ( !project && ( verbose & VERB_PROCESS ) )
190  cout << "Creating a mask of size: " << nusize << endl;
191 
192 /*
193  if ( nusize.volume() > 0 ) { // Create the mask in place of an input image
194  if ( access(filename.c_str(), F_OK) ) {
195  if ( verbose & VERB_PROCESS )
196  cout << "Creating a mask of size: " << nusize << endl;
197  if ( nudatatype == Unknown_Type ) nudatatype = UCharacter;
198  p = new Bimage(nudatatype, TSimple, nusize, 1);
199 // operation = 0; // Default operation is replace
200 // fill = 1;
201  } else if ( file_type(filename) == Micrograph ) {
202  if ( nusize.volume() < 1 ) {
203  cerr << "Error: A mask size must be specified!" << endl;
204  bexit(-1);
205  }
206  project = read_project(filename);
207  p = project_missing_mask(project, nusize, ori, resolution, scale);
208  project_kill(project);
209  filename = argv[optind];
210  } else {
211  cerr << "Error: " << filename << " already exists!" << endl;
212  }
213  } else {
214  p = read_img(filename, 1, -1);
215  if ( p == NULL ) bexit(-1);
216 // if ( fill_type == FILL_AVERAGE ) fill = p->avg;
217 // if ( fill_type == FILL_BACKGROUND ) fill = p->image[0].background();
218  filename = argv[optind];
219  }
220 */
221 
222  if ( set_origin ) {
223  if ( set_origin == 2 ) p->origin(p->default_origin());
224  else p->origin(ori);
225  ori = p->image->origin();
226  }
227 
228  if ( sam.volume() > 0 ) p->sampling(sam);
229 
230  if ( !project ) {
231  if ( find.length() )
232  p->mask_missing_find(ori, resolution, find);
233  else if ( tilt_pos2 )
234  p->mask_missing_pyramid(ori, tilt_axis, tilt_axis2,
235  tilt_neg, tilt_pos, tilt_neg2, tilt_pos2, resolution);
236  else if ( tilt_pos )
237  p->mask_missing_wedge(ori, tilt_axis, tilt_neg, tilt_pos, resolution);
238  else if ( tilt_cone )
239  p->mask_missing_cone(ori, tilt_cone, resolution);
240  }
241 
242  if ( invert ) p->mask_invert();
243 
244  if ( dilate > 0 )
245  p->mask_dilate(dilate);
246 
247  p->mask_stats();
248 
249  if ( filename.length() ) {
250  p->change_type(nudatatype);
251  write_img(filename, p, 0);
252  }
253 
254  delete p;
255 
256  if ( verbose & VERB_TIME )
257  timer_report(ti);
258 
259  bexit(0);
260 }
261 
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::mask_missing_pyramid
long mask_missing_pyramid(Vector3< double > origin, double tilt_axis1, double tilt_axis2, double tilt_neg1, double tilt_pos1, double tilt_neg2, double tilt_pos2, double resolution)
Generates a mask with a missing pyramid.
Definition: Bimage_mask.cpp:976
Boption::scale
Vector3< double > scale()
Definition: options.h:190
Bimage::mask_dilate
long mask_dilate(long times=1)
Dilates a binary mask.
Definition: Bimage_mask.cpp:423
Bproject::select
int select
Definition: mg_processing.h:342
UCharacter
@ UCharacter
Definition: utilities.h:153
file_util.h
Header file for file checking functions.
Bstring
Definition: Bstring.h:29
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::default_origin
Vector3< double > default_origin()
Definition: Bimage.h:592
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
Bimage::mask_missing_find
long mask_missing_find(Vector3< double > ori, double resolution, Bstring &mis_type)
Generates a missing mask from an example image.
Definition: Bimage_mask.cpp:1137
options.h
Header file for option handlers.
Boption::origin
Vector3< double > origin()
Definition: options.h:180
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
verbose
int verbose
project
Bproject * project
Definition: bshow.cpp:33
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
rwmg.h
Header file for reading and writing micrograph parameters.
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
TSimple
@ TSimple
Definition: Bimage.h:59
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
use
const char * use[]
Definition: bmissing.cpp:21
Bimage::mask_stats
long mask_stats()
Calculates statistics for a mask.
Definition: Bimage_mask.cpp:165
Micrograph
@ Micrograph
Definition: file_util.h:23
Boption::value
Bstring value
Definition: options.h:30
Bimage
General image parameter class.
Definition: Bimage.h:177
project_missing_mask
Bimage * project_missing_mask(Bproject *project, Vector3< long > size, Vector3< double > origin, double hi_res, double scale)
Creates a reciprocal space mask from the tilt series orientations.
Definition: mg_tomo_rec.cpp:1617
timer.h
Utilities for timing functions.
Bproject
Definition: mg_processing.h:336
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
Bstring::split_into_doubles
vector< double > split_into_doubles(const Bstring &delim)
Definition: Bstring.cpp:673
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
read_project
Bproject * read_project(const char *filename, int flags=0)
Reading micrograph parameters from files.
Definition: rwmg.cpp:141
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
Boption::size
Vector3< long > size()
Definition: options.h:163
Image
@ Image
Definition: file_util.h:22
Bimage::mask_missing_wedge
long mask_missing_wedge(Vector3< double > origin, double tilt_axis, double tilt_neg, double tilt_pos, double resolution)
Generates a mask with a missing wedge.
Definition: Bimage_mask.cpp:893
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::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
Bimage::mask_invert
long mask_invert()
Inverts a mask.
Definition: Bimage_mask.cpp:220
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:2284
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
mg_tomo_rec.h
Functions to do a tomographic reconstruction.
rwimg.h
Header file for 2D and 3D image I/O.
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bimage::mask_missing_cone
long mask_missing_cone(Vector3< double > origin, double mis_ang, double resolution)
Generates a mask with a missing cone.
Definition: Bimage_mask.cpp:1064
main
int main(int argc, char **argv)
Definition: bmissing.cpp:66
file_type
FileType file_type(const char *filename)
Checks the file type using the extension and contents.
Definition: file_util.cpp:178