Bsoft  2.1.0
Bernard's software package
bmodmask.cpp
Go to the documentation of this file.
1 
9 #include "rwmodel.h"
10 #include "model_mask.h"
11 #include "model_links.h"
12 #include "model_util.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: bmodmask [options] in1.star [in2.star...]",
24 "------------------------------------------------",
25 "Generates a mask from a model if an output image is specified.",
26 " ",
27 "Actions:",
28 "-type hull Mask type: model, hull, shell, level.",
29 "-invert Invert the mask after creation.",
30 "-project 2.5,D3 Projected masks at the angular step size and symmetry.",
31 " ",
32 "Parameters:",
33 "-verbose 7 Verbose output.",
34 "-componentradius 8.4 Set display radius for all components.",
35 "-linkradius 5.1 Set display radius for all links.",
36 " ",
37 "Parameters for mask creation:",
38 "-datatype u Force writing of a new data type (default float).",
39 "-size 100,120,80 Mask size (default from model).",
40 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; a single value can be given).",
41 "-origin 0.8,-10,15.7 Set the origin for the mask.",
42 "-edge 1.5 Edge width for model mask (default 1 angstrom).",
43 "-width 34.8 Shell mask width (default 1 angstrom).",
44 "-curved Flag to indicate curved hull or shell surface (default not).",
45 "-fast Use fast but less comprehensive algorithm for shell and hull masks.",
46 "-smooth 2 Edge smoothing in pixels (default 0).",
47 " ",
48 "Input:",
49 "-parameters param.star Input parameter file.",
50 " ",
51 "Output:",
52 "-output file.star Output model file.",
53 "-mask mask.map Output mask image.",
54 " ",
55 NULL
56 };
57 
58 int main(int argc, char **argv)
59 {
60  /* Initialize variables */
61  DataType nudatatype = Float; // Mask data type
62  int mask_type(-1); // 0=model, 1=hull, 2=shell, 3=level, 4=project
63  double dang(0); // Projection angular step size
64  Bsymmetry* sym = NULL; // Point group for projections
65  Vector3<long> mask_size; // Size for creating a mask
66  Vector3<double> sam; // Map sampling
67  Vector3<double> origin; // Mask origin
68  double edge(1); // Edge width for model mask
69  double shell_width(1); // Shell mask width
70  int curv_flag(0); // Curved surface flag for hull and shell masks
71  int invert(0); // Flag for mask inversion
72  double comprad(0); // Component display radius
73  double linkrad(0); // Link display radius
74  int smooth(0); // Edge smoothing in pixels
75  int fast(0); // Flag for fast algorithm for hull and shell masks
76  Bstring paramfile; // Input parameter file
77  Bstring outfile; // Output model file
78  Bstring maskfile; // Output mask file
79 
80  int optind;
81  Boption* option = get_option_list(use, argc, argv, optind);
82  Boption* curropt;
83  for ( curropt = option; curropt; curropt = curropt->next ) {
84  if ( curropt->tag == "type" ) {
85  if ( curropt->value[0] == 'm' ) mask_type = 0;
86  if ( curropt->value[0] == 'h' ) mask_type = 1;
87  if ( curropt->value[0] == 's' ) mask_type = 2;
88  if ( curropt->value[0] == 'l' ) mask_type = 3;
89  }
90  if ( curropt->tag == "project" ) {
91  if ( ( dang = curropt->value.real() ) < 0.5 )
92  cerr << "-project: An angular step size must be specified!" << endl;
93  else {
94  dang *= M_PI/180.0;
95  if ( curropt->value.contains(",") )
96  sym = init_point_group_symmetry(curropt->value.post(','));
97  else
98  sym = init_point_group_symmetry("C1");
99  mask_type = 4;
100  }
101  }
102  if ( curropt->tag == "componentradius" )
103  if ( ( comprad = curropt->value.real() ) < 1 )
104  cerr << "-componentradius: A radius must be specified!" << endl;
105  if ( curropt->tag == "linkradius" )
106  if ( ( linkrad = curropt->value.real() ) < 1 )
107  cerr << "-linkradius: A radius must be specified!" << endl;
108  if ( curropt->tag == "invert" ) invert = 1;
109  if ( curropt->tag == "datatype" )
110  nudatatype = curropt->datatype();
111  if ( curropt->tag == "size" )
112  mask_size = curropt->size();
113  if ( curropt->tag == "sampling" )
114  sam = curropt->scale();
115  if ( curropt->tag == "origin" )
116  origin = curropt->origin();
117  if ( curropt->tag == "edge" )
118  if ( ( edge = curropt->value.real() ) < 0.001 )
119  cerr << "-edge: The edge width must be specified!" << endl;
120  if ( curropt->tag == "width" )
121  if ( ( shell_width = curropt->value.real() ) < 1 )
122  cerr << "-width: The shell width must be specified!" << endl;
123  if ( curropt->tag == "curved" ) curv_flag = 1;
124  if ( curropt->tag == "smooth" )
125  if ( ( smooth = curropt->value.integer() ) < 1 )
126  cerr << "-smooth: The number of smoothing pixels must be specified!" << endl;
127  if ( curropt->tag == "fast" ) fast = 1;
128  if ( curropt->tag == "parameters" )
129  paramfile = curropt->filename();
130  if ( curropt->tag == "output" )
131  outfile = curropt->filename();
132  if ( curropt->tag == "mask" )
133  maskfile = curropt->filename();
134  }
135  option_kill(option);
136 
137  double ti = timer_start();
138 
139  // Read all the parameter files
140  Bstring* file_list = NULL;
141  while ( optind < argc ) string_add(&file_list, argv[optind++]);
142  if ( !file_list ) {
143  cerr << "Error: No parameter or image files specified!" << endl;
144  bexit(-1);
145  }
146 
147  Bmodel* model = read_model(file_list, paramfile);
148  string_kill(file_list);
149 
150  if ( !model ) {
151  cerr << "Error: Input file not read!" << endl;
152  bexit(-1);
153  }
154 
155  if ( comprad ) models_process(model, comprad, model_set_component_radius);
156 
157  if ( linkrad ) models_process(model, linkrad, model_set_link_radius);
158 
159  Bimage* pmask = NULL;
160  if ( maskfile.length() && mask_size.volume() && mask_type >= 0 ) {
161  switch ( mask_type ) {
162  case 0:
163  pmask = model_create_mask(model, mask_size, origin, sam, edge);
164  break;
165  case 1:
166  pmask = model_create_hull_mask(model, mask_size, origin, sam, curv_flag, fast);
167  break;
168  case 2:
169  pmask = model_create_shell_mask(model, mask_size, origin, sam, shell_width, curv_flag, fast);
170  break;
171  case 3:
172  pmask = model_create_level_mask(model, mask_size, origin, sam);
173  break;
174  case 4:
175  pmask = model_create_projected_mask(model, mask_size, origin, sam, dang, sym);
176  break;
177  }
178  if ( invert ) pmask->mask_invert();
179  if ( smooth > 0 ) pmask->filter_average(2*smooth + 1);
180  pmask->change_type(nudatatype);
181  model->fmap = maskfile;
182  write_img(maskfile, pmask, 0);
183  delete pmask;
184  }
185 
186  // Write an output parameter format file if a name is given
187  if ( outfile.length() && model ) {
188  write_model(outfile, model);
189  }
190 
191  model_kill(model);
192 
193  if ( verbose & VERB_TIME )
194  timer_report(ti);
195 
196  bexit(0);
197 }
198 
model_create_mask
Bimage * model_create_mask(Bmodel *model, Vector3< long > size, Vector3< double > origin, Vector3< double > sam, double edge)
Calculates a mask of the components and links of a model structure.
Definition: model_mask.cpp:35
Boption
A class for a command line input option.
Definition: options.h:26
Boption::scale
Vector3< double > scale()
Definition: options.h:190
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:768
rwmodel.h
Header file for reading and writing atomic model files.
Bstring
Definition: Bstring.h:29
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::datatype
DataType datatype()
Definition: options.h:93
use
const char * use[]
Definition: bmodmask.cpp:21
Boption::next
Boption * next
Definition: options.h:28
Vector3< long >
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.
Bstring::post
Bstring post(char c)
Definition: Bstring.cpp:402
Boption::origin
Vector3< double > origin()
Definition: options.h:180
Bsymmetry
Definition: symmetry.h:38
Bmodel::fmap
Bstring fmap
Definition: rwmodel.h:125
models_process
long models_process(Bmodel *model, long(modfunc)(Bmodel *))
Process a list of models using the specified function.
Definition: model_util.cpp:36
Boption::tag
Bstring tag
Definition: options.h:29
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
Boption::value
Bstring value
Definition: options.h:30
model_kill
int model_kill(Bmodel *model)
Deallocates all memory in the list.
Definition: rwmodel.cpp:1127
main
int main(int argc, char **argv)
Definition: bmodmask.cpp:58
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
Bmodel
Definition: rwmodel.h:119
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
verbose
int verbose
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
write_model
int write_model(Bstring &filename, Bmodel *model)
Writes model parameters.
Definition: rwmodel.cpp:92
model_create_level_mask
Bimage * model_create_level_mask(Bmodel *model, Vector3< long > size, Vector3< double > origin, Vector3< double > sam)
Calculates a mask with one level per model.
Definition: model_mask.cpp:286
Boption::size
Vector3< long > size()
Definition: options.h:163
model_mask.h
Functions to generate masks from models.
Bstring::length
long length() const
Definition: Bstring.cpp:263
init_point_group_symmetry
Bsymmetry * init_point_group_symmetry(Bstring symmetry_string)
Sets up a symmetry structure from a string identifier.
Definition: symmetry.cpp:39
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:76
Bimage::mask_invert
long mask_invert()
Inverts a mask.
Definition: Bimage_mask.cpp:220
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
model
Bmodel * model
Definition: bshow.cpp:34
Vector3::volume
double volume()
Definition: Vector3.h:237
Bimage::filter_average
int filter_average(long kernel_size)
Applies an averaging filter to an image.
Definition: Bimage_filter.cpp:207
model_create_shell_mask
Bimage * model_create_shell_mask(Bmodel *model, Vector3< long > size, Vector3< double > origin, Vector3< double > sam, double shell_width, int curv_flag, int fast)
Calculates a shell mask covering a model structure.
Definition: model_mask.cpp:192
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
model_util.h
Library routines used for model processing.
read_model
Bmodel * read_model(Bstring &filename)
Definition: rwmodel.cpp:29
model_create_projected_mask
Bimage * model_create_projected_mask(Bmodel *model, Vector3< long > size, Vector3< double > ori, Vector3< double > sam, double dang, Bsymmetry *sym)
Calculates a set of 2D masks for the projections of a model structure.
Definition: model_mask.cpp:532
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
model_set_component_radius
long model_set_component_radius(Bmodel *model, double comprad)
Set the display radius for all components to a specific value.
Definition: rwmodel.cpp:1247
model_create_hull_mask
Bimage * model_create_hull_mask(Bmodel *model, Vector3< long > size, Vector3< double > origin, Vector3< double > sam, int curv_flag, int fast)
Calculates a mask based on the periphery of a model structure.
Definition: model_mask.cpp:104
Float
@ Float
Definition: utilities.h:161
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:713