Bsoft  2.1.0
Bernard's software package
bmultifit.cpp
Go to the documentation of this file.
1 
9 #include "model_multifit.h"
10 #include "ps_views.h"
11 #include "linked_list.h"
12 #include "options.h"
13 #include "utilities.h"
14 #include "timer.h"
15 
16 
17 // Declaration of global variables
18 extern int verbose; // Level of output to the screen
19 
20 
21 // Usage assistance
22 const char* use[] = {
23 " ",
24 "Usage: bmultifit [options] input.img view.grd",
25 "---------------------------------------------",
26 "Searches orientation space for a set of solutions for the fit of a template within a 3D map.",
27 "Modes: Global: Search an asymmetric unit with a fixed angular step size.",
28 " Symmetry: Search all symmetry-related views using the view from the -View option.",
29 "The output image contains 4-valued views, only supported by the GRD format.",
30 "Only one image in a multi-image file will be used (see -image option).",
31 " ",
32 "Actions:",
33 "-mode sym Mode: global (default) or symmetric.",
34 "-subset 25,5 View subset selection from a start offset and with a given size.",
35 "-fom 0.35 FOM cutoff to use (default set to FOMmax/2).",
36 " ",
37 "Parameters:",
38 "-verbose 7 Verbosity of output.",
39 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; default from input file; a single value can be given).",
40 "-origin 110,50,44 Origin for rotations of the template (default taken from template image).",
41 "-angles 8.8,2.5,5.2 Step size for alpha, theta and phi, one value sets all (default 45 degrees).",
42 "-resolution 15.6,200 Resolution limits for cross-correlation (default 0).",
43 "-View 0.3,-0.5,0.8,33 View to initiate search: vector {xyz} and angle (use with -symmetry option).",
44 "-symmetry D6 Symmetry: Point group identifier (use with -View option).",
45 "-image 2 Image number in a multi-image file (default 0=first image).",
46 "-bin 3 Binning of input, template and mask by given kernel size,",
47 " output image remains same size as input.",
48 " ",
49 "Input:",
50 "-Template image.map Template to search for.",
51 "-Mask mask.tif Mask in reciprocal space to apply during cross-correlation.",
52 " ",
53 "Output:",
54 "-output file.star Model output file with best orientations.",
55 "-Postscript plot.ps Output postscript file with a plot of projection vectors.",
56 "-FOM cc.map Output correlation coefficient map.",
57 " ",
58 NULL
59 };
60 
61 int main(int argc, char **argv)
62 {
63  // Initialize variables
64  Bstring mode("global"); // Mode, default global
65  int view_start(0); // Subset start
66  int view_subset(0); // Subset size
67  double cutoff(0); // Cutoff set to 0 to calculate automatically
68  int img_num(0); // Image number in file
69  Vector3<double> sam; // Units for the three axes (A/pixel)
70  Vector3<double> origin; // Template origin
71  int set_origin(0); // Flag to set origin
72  double alpha(-1); // Rotation around view vector
73  double alpha_step = M_PI_4; // Angular step size for alpha
74  double theta_step = M_PI_4; // Angular step size for theta
75  double phi_step = M_PI_4; // Angular step size for phi
76  Bstring symmetry_string("C1"); // Default: asymmetric or C1 point group
77  double hires(0), lores(0); // Limiting resolution for cross-correlation
78  View currview(0,0,-10,0); // View to initiate search from and use as current view
79  View ref_view; // Reference view
80  Vector3<long> bin = {1,1,1}; // No binning
81  Bstring template_file;
82  Bstring mask_file;
83  Bstring model_file;
84  Bstring ps_file;
85  Bstring fom_file;
86 
87  int i, optind;
88  Boption* option = get_option_list(use, argc, argv, optind);
89  Boption* curropt;
90  for ( curropt = option; curropt; curropt = curropt->next ) {
91  if ( curropt->tag == "mode" ) {
92  mode = curropt->value.lower();
93  if ( mode.length() < 1 )
94  cerr << "-mode: A mode must be specified" << endl;
95  }
96  if ( curropt->tag == "subset" )
97  if ( curropt->values(view_start, view_subset) < 2 )
98  cerr << "-subset: A view subset start and size must be specified" << endl;
99  if ( curropt->tag == "fom" )
100  if ( ( cutoff = curropt->value.real() ) < 0.0001 )
101  cerr << "-fom: A cutoff value must be specified!" << endl;
102  if ( curropt->tag == "image" )
103  if ( ( img_num = curropt->value.integer() ) < 1 )
104  cerr << "-image: An image number must be specified" << endl;
105  if ( curropt->tag == "sampling" )
106  sam = curropt->scale();
107  if ( curropt->tag == "origin" ) {
108  if ( curropt->value[0] == 'c' ) {
109  set_origin = 2;
110  } else {
111  origin = curropt->origin();
112  set_origin = 1;
113  }
114  }
115  if ( curropt->tag == "angles" ) {
116  if ( ( i = curropt->values(alpha_step, theta_step, phi_step) ) < 1 )
117  cerr << "-angles: An angle step size must be specified" << endl;
118  else {
119  if ( i < 2 ) phi_step = theta_step = alpha_step;
120  else if ( i < 3 ) phi_step = theta_step;
121  alpha_step *= M_PI/180.0;
122  theta_step *= M_PI/180.0;
123  phi_step *= M_PI/180.0;
124  }
125  }
126  if ( curropt->tag == "resolution" )
127  if ( curropt->values(hires, lores) < 1 )
128  cerr << "-resolution: A resolution must be specified!" << endl;
129  if ( curropt->tag == "View" )
130  currview = curropt->view();
131  if ( curropt->tag == "symmetry" )
132  symmetry_string = curropt->symmetry_string();
133  if ( curropt->tag == "bin" ) {
134  if ( ( bin[0] = curropt->value.integer() ) < 1 )
135  cerr << "-bin: A bin size must be specified!" << endl;
136  else
137  bin[2] = bin[1] = bin[0];
138  }
139  if ( curropt->tag == "Template" )
140  template_file = curropt->filename();
141  if ( curropt->tag == "Mask" )
142  mask_file = curropt->filename();
143  if ( curropt->tag == "output" )
144  model_file = curropt->filename();
145  if ( curropt->tag == "Postscript" )
146  ps_file = curropt->filename();
147  if ( curropt->tag == "FOM" )
148  fom_file = curropt->filename();
149  }
150  option_kill(option);
151 
152  double ti = timer_start();
153 
154  // Do the symmetry-related stuff first
155  View* view = NULL;
156 
157  Matrix3 mat = ref_view.matrix();
158  Bsymmetry* sym = init_point_group_symmetry(symmetry_string);
159  for ( i=0; i<sym->n; i++ )
160  sym->op[i].axis = mat * sym->op[i].axis;
161 
162  if ( mode.contains("glo") ) {
163  view = asymmetric_unit_views(sym, theta_step, phi_step, 1);
164  } else if ( mode.contains("sym") ) {
165  if ( currview[2] < -1 ) currview[2] = 1;
166  view = symmetry_get_all_views(sym, currview);
167  alpha_step = TWOPI;
168  }
169 
170  if ( view_subset > 0 ) if ( view_list_subset(&view, view_start, view_subset) < 1 ) {
171  cerr << "Error: At least one view must be selected!" << endl;
172  bexit(-1);
173  }
174 
175  if ( ps_file.length() && view ) ps_views(ps_file, view);
176  kill_symmetry(sym);
177 
178  if ( verbose )
179  cout << "Number of views: " << count_list((char *) view) << endl;
180 
181  // Read image file
182  Bimage* p = NULL;
183  Bimage* ptemp = NULL;
184  Bimage* pmask = NULL;
185  Vector3<double> scale(1,1,1);
186  Vector3<long> size;
187 
188  if ( optind < argc ) {
189  p = read_img(argv[optind++], 1, img_num);
190  size = p->size();
191  if ( bin[0]*bin[1]*bin[2] > 1 ) p->bin(bin);
192  }
193 
194  if ( !p ) {
195  cerr << "Error: No input file given!" << endl;
196  bexit(-1);
197  }
198 
199  if ( template_file.length() ) {
200  ptemp = read_img(template_file, 1, 0);
201  if ( sam.volume() > 0 ) ptemp->sampling(sam);
202  if ( set_origin ) {
203  if ( set_origin == 2 ) ptemp->origin(ptemp->size()/2);
204  else ptemp->origin(origin);
205  } else origin = ptemp->image->origin();
206  if ( bin[0]*bin[1]*bin[2] > 1 ) ptemp->bin(bin);
207  }
208 
209  if ( mask_file.length() ) {
210  pmask = read_img(mask_file, 1, -1);
211  if ( bin[0]*bin[1]*bin[2] > 1 ) pmask->bin(bin);
212  }
213 
214  Bmodel* model = NULL;
215  Bimage* pfit = NULL;
216 
217  if ( ptemp ) {
218  if ( model_file.length() ) {
219  model = model_from_densities(p, ptemp, view, alpha, alpha_step, hires, lores, pmask, cutoff);
220  write_model(model_file, model);
221  model_kill(model);
222  } else {
223 // pfit = img_multifit(p, ptemp, view, alpha, alpha_step, hires, lores, pmask, cutoff);
224  pfit = p->search_volume(ptemp, view, alpha, alpha_step, hires, lores, pmask, cutoff);
225  if ( optind < argc )
226  write_img(argv[optind], pfit, 0);
227  if ( fom_file.length() )
228  write_img(fom_file, pfit->next, 0);
229 // delete pfit->next;
230  delete pfit;
231  }
232  delete ptemp;
233  }
234 
235  kill_list((char *) view, sizeof(View));
236  delete p;
237  delete pmask;
238 
239  if ( !pfit ) {
240  cerr << "Error: No output file generated!" << endl;
241  bexit(-1);
242  }
243 
244 
245  if ( verbose & VERB_TIME )
246  timer_report(ti);
247 
248  bexit(0);
249 }
250 
Boption
A class for a command line input option.
Definition: options.h:26
view_list_subset
int view_list_subset(View **view_list, int start, int size)
Replaces a view list with a subset of it.
Definition: View.cpp:474
Bsymmetry::n
int n
Definition: symmetry.h:42
Boption::scale
Vector3< double > scale()
Definition: options.h:190
symmetry_get_all_views
View * symmetry_get_all_views(Bsymmetry *sym, View asu_view)
Get all symmetry-related views of one given view.
Definition: symmetry.cpp:442
M_PI_4
#define M_PI_4
Definition: utilities.h:69
kill_list
long kill_list(char *list, unsigned long size)
Frees all the items in a linked list.
Definition: linked_list.cpp:214
Bstring
Definition: Bstring.h:29
Bimage::next
Bimage * next
Definition: Bimage.h:179
Bstring::integer
long integer()
Definition: Bstring.cpp:547
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
model_from_densities
Bmodel * model_from_densities(Bimage *p, Bimage *ptemp, View *view, double alpha, double alpha_step, double hires, double lores, Bimage *pmask, double threshold)
Searches a 2D/3D density map for a template.
Definition: model_multifit.cpp:38
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
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
main
int main(int argc, char **argv)
Definition: bmultifit.cpp:61
Bstring::real
double real()
Definition: Bstring.cpp:553
options.h
Header file for option handlers.
asymmetric_unit_views
View * asymmetric_unit_views(Bsymmetry *sym, double theta_step, double phi_step, int flag)
Initializes a well-distributed set of views in an asymmetric unit.
Definition: symmetry.cpp:564
View
Definition: View.h:26
Boption::origin
Vector3< double > origin()
Definition: options.h:180
Boption::view
View view()
Definition: options.h:247
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
Bsymmetry
Definition: symmetry.h:38
count_list
long count_list(char *list)
Counts the number of items in a linked list.
Definition: linked_list.cpp:235
Bsymop::axis
Vector3< double > axis
Definition: symmetry.h:25
Boption::tag
Bstring tag
Definition: options.h:29
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
Matrix3
Definition: Matrix3.h:18
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:77
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
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
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
use
const char * use[]
Definition: bmultifit.cpp:22
Boption::symmetry_string
Bstring symmetry_string()
Definition: options.h:233
model_multifit.h
Header file for searching for a template in a map and returning multiple hits in a model.
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
Bmodel
Definition: rwmodel.h:119
ps_views
int ps_views(Bstring &filename, Bstring &symmetry_string, View *view, int flags)
Generates postscript plot of views on the unit sphere.
Definition: ps_views.cpp:34
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
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
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::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
model
Bmodel * model
Definition: bshow.cpp:34
Vector3::volume
double volume()
Definition: Vector3.h:237
kill_symmetry
int kill_symmetry(Bsymmetry *sym)
Deallocates a symmetry structure.
Definition: symmetry.cpp:207
linked_list.h
Header file for generalized linked list functions.
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
Bsymmetry::op
Bsymop * op
Definition: symmetry.h:43
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
Bimage::search_volume
Bimage * search_volume(Bimage *ptemp, View *view, double alpha, double alpha_step, double hires, double lores, Bimage *pmask, double threshold)
Searches a 2D/3D density map for a template.
Definition: Bimage_search.cpp:208
View::matrix
Matrix3 matrix()
Definition: View.h:153
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
verbose
int verbose
ps_views.h
Header file for postscript tools dealing with views.
Bstring::lower
Bstring lower()
Definition: Bstring.cpp:316
TWOPI
#define TWOPI
Definition: utilities.h:72