Bsoft  2.1.0
Bernard's software package
bcc.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "Matrix.h"
11 #include "options.h"
12 #include "utilities.h"
13 #include "timer.h"
14 
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17 
18 /* Usage assistance */
19 const char* use[] = {
20 " ",
21 "Usage: bcc [options] file.spi file.mrc",
22 "--------------------------------------",
23 "Calculates cross-correlation using fast Fourier transforms.",
24 " ",
25 "Actions:",
26 "-Autocorrelate Auto-correlate, output auto-correlation map.",
27 "-Crosscorrelate file.mrc Cross-correlate with reference file.mrc, output shifted image.",
28 "-Validate file.mrc Cross-correlate and cross-validate with reference file.mrc, output shifted image.",
29 "-Center Find center by cross-correlation with an inverted copy.",
30 "-halfshift Shift correlation map by half the size to put the origin in the middle.",
31 " ",
32 "Parameters:",
33 "-verbose 7 Verbosity of output.",
34 "-datatype u Force writing of a new data type.",
35 "-resolution 4.5,130 Resolution range for correlation (default 0 - 1e6 angstrom).",
36 "-origin 0,-10,30 Set origin, used with -size option (default 0,0,0).",
37 "-size 22,33,14 Size for extraction or hkl input (voxels, default from data).",
38 "-sampling 2,3.5,1 Sampling (angstrom/voxel, a single value sets all three).",
39 "-limit 25 Shift search limit in cross-correlation map (voxels, default: quarter of x-dimension).",
40 //"-wrap Turn wrapping on (default off).",
41 " ",
42 "Input:",
43 "-Mask mask.tif Reciprocal space mask to use for cross-correlation.",
44 " ",
45 "Output:",
46 "-Map ccmap.tif Cross-correlation map.",
47 " ",
48 NULL
49 };
50 
51 int main(int argc, char **argv)
52 {
53  // Initialize variables
54  DataType nudatatype(Unknown_Type); // Conversion to new type
55  int findcenter(0); // Flag to find image center
56  int setauto(0); // Flag to do auto-correlation
57  int halfshift(0); // Flag to shift origin to middle
58  double search_limit(-1); // Search limit in cross-correlation map
59  Bstring reffile; // File for cross-correlation
60  Bstring valfile; // File for cross-validation
61  Bstring ccfile; // Cross-correlation map output file
62  Bstring maskfile; // Mask to use for cross-correlation
63 
64  Vector3<double> origin; // Origin
65  int set_origin(0); // Flag for setting the origin
66  Vector3<double> sam; // Sampling
67 // int setwrap(0); // Wrapping flag
68 
69  double hires(0), lores(0); // Limiting resolution range (hires must be > 0 to be set)
70 
71  int optind;
72  Boption* option = get_option_list(use, argc, argv, optind);
73  Boption* curropt;
74  for ( curropt = option; curropt; curropt = curropt->next ) {
75  if ( curropt->tag == "datatype" )
76  nudatatype = curropt->datatype();
77  if ( curropt->tag == "Autocorrelate" )
78  setauto = 1;
79  if ( curropt->tag == "Center" )
80  findcenter = 1;
81  if ( curropt->tag == "halfshift" ) halfshift = 1;
82  if ( curropt->tag == "resolution" )
83  if ( curropt->values(hires, lores) < 1 )
84  cerr << "-resolution: A high resolution limit must be specified" << endl;
85  if ( curropt->tag == "origin" ) {
86  if ( curropt->value[0] == 'c' ) {
87  set_origin = 2;
88  } else {
89  origin = curropt->origin();
90  set_origin = 1;
91  }
92  }
93  if ( curropt->tag == "sampling" )
94  sam = curropt->scale();
95  if ( curropt->tag == "limit" )
96  if ( ( search_limit = curropt->value.real() ) < 0 )
97  cerr << "-limit: A search limit must be specified" << endl;
98 // if ( curropt->tag == "wrap" )
99 // setwrap = 1;
100  if ( curropt->tag == "Map" )
101  ccfile = curropt->filename();
102  if ( curropt->tag == "Crosscorrelate" )
103  reffile = curropt->filename();
104  if ( curropt->tag == "Validate" )
105  valfile = curropt->filename();
106  if ( curropt->tag == "Mask" )
107  maskfile = curropt->filename();
108  }
109  option_kill(option);
110 
111  double ti = timer_start();
112 
113  // Allocate memory for the structure factors and image parameters
114  Bimage* p = NULL;
115  Bimage* pref = NULL;
116  Bimage* pmask = NULL;
117  Bimage* pcc = NULL;
118 
119  // Read the file
120  if ( ( p = read_img(argv[optind++], 1, -1) ) == NULL ) {
121  cerr << "File not read!" << endl;
122  bexit(-1);
123  }
124 
125  if ( nudatatype == Unknown_Type )
126  nudatatype = p->data_type();
127  else if ( nudatatype > p->data_type() )
128  p->change_type(nudatatype);
129 
130  if ( sam.volume() > 0 ) p->sampling(sam);
131 
132  if ( set_origin ) {
133  if ( set_origin == 2 ) p->origin(p->default_origin());
134  else p->origin(origin);
135  }
136 
137  if ( maskfile.length() )
138  pmask = read_img(maskfile, 1, -1);
139 
140  if ( search_limit < 0 ) search_limit = p->sizeX()/4.0;
141 
142  // Do cross correlation or a simple Fourier transform
143  long n;
144  double v, ccavg(0), ccstd(0);
145  if ( reffile.length() ) {
146  pref = read_img(reffile, 1, -1);
147  if ( !pref ) bexit(-1);
148  pcc = p->cross_correlate(pref, hires, lores, pmask);
149 // pcc = p->cross_correlate_fspace(pref, hires, lores, search_limit);
150 // origin = pref->image->origin();
151 // cout << "origin=" << pcc->image->origin() << endl;
152  pcc->find_peak(search_limit, 0);
153 // cout << "origin=" << pcc->image->origin() << endl;
154  pcc->refine_peak();
155 // cout << "origin=" << pcc->image->origin() << endl;
156  for ( n=0; n<pcc->images(); n++ ) {
157  p->origin(n, pref->image[n].origin() + pcc->image[n].origin());
158  p->image[n].FOM(pcc->image[n].FOM());
159  }
160  if ( verbose ) {
161  cout << "Image\t dx\t dy\t dz\t CC\t P" << endl;
162  for ( n=0; n<p->images(); n++ ) {
163  v = p->image[n].FOM();
164  ccavg += v;
165  ccstd += v*v;
166  cout << n+1 << tab << fixed << setprecision(4) <<
167  pref->image[n].origin() - p->image[n].origin() << tab <<
168  p->image[n].FOM() << tab << pcc->ccmap_confidence(n) << endl;
169  }
170  cout << endl;
171  ccavg /= n;
172  ccstd /= n;
173  ccstd -= ccavg*ccavg;
174  if ( ccstd > 1 ) ccstd = sqrt(ccstd);
175  cout << "Correlation avg & stdev: " << ccavg << tab << ccstd << endl << endl;
176  }
177  delete pref;
178  if ( optind < argc ) p->center_wrap();
179  } else if ( valfile.length() ) {
180  pref = read_img(valfile, 1, -1);
181  if ( !pref ) bexit(-1);
182  pcc = p->cross_correlate_validate(pref, pmask);
183 // origin = pref->image->origin();
184  pcc->find_peak(search_limit, 0);
185  pcc->refine_peak();
186  for ( n=0; n<pcc->images(); n++ )
187  p->origin(n, pref->image[n].origin() + pcc->image[n].origin());
188  delete pref;
189  if ( verbose ) {
190  cout << "Image\t dx\t dy\t dz\t CC\t P" << endl;
191  for ( n=0; n<p->images(); n++ ) {
192  cout << n+1 << tab << fixed << setprecision(4) <<
193  pref->image[n].origin() - p->image[n].origin() << tab <<
194  p->image[n].FOM() << tab << pcc->ccmap_confidence(n) << endl;
195  }
196  cout << endl;
197  }
198  if ( optind < argc ) p->center_wrap();
199  } else if ( setauto ) {
200  p->auto_correlate(hires, lores);
201  if ( halfshift ) {
202  p->origin(0,0,0);
203  p->center_wrap();
204  }
205  pcc = p;
206  } else if ( findcenter ) {
207  p->find_center(pmask, hires, lores, search_limit, 0, 1);
208  if ( halfshift ) p->center_wrap();
209  pcc = p;
210  }
211 
212  if ( pcc && ccfile.length() ) {
213  if ( !pcc->next ) {
214  if ( verbose & VERB_DEBUG )
215  cout << "DEBUG bcc: writing pcc to " << ccfile << endl;
216  if ( halfshift ) pcc->center_wrap();
217  write_img(ccfile, pcc, 0);
218  } else {
219  if ( verbose & VERB_DEBUG )
220  cout << "DEBUG bcc: writing pcc->next to " << ccfile << endl;
221  if ( halfshift ) pcc->next->center_wrap();
222  write_img(ccfile, pcc->next, 0);
223  }
224  }
225 
226  if ( optind < argc ) {
227  p->change_type(nudatatype);
228  write_img(argv[optind], p, 0);
229  }
230 
231  if ( p != pcc ) delete pcc;
232  if ( pmask ) delete pmask;
233  delete p;
234 
235  if ( verbose & VERB_TIME )
236  timer_report(ti);
237 
238  bexit(0);
239 }
240 
Boption
A class for a command line input option.
Definition: options.h:26
use
const char * use[]
Definition: bcc.cpp:19
Boption::scale
Vector3< double > scale()
Definition: options.h:190
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Bimage::cross_correlate
Bimage * cross_correlate(Bimage *p, double hires, double lores, fft_plan planf, fft_plan planb)
Calculates a cross-correlation map by Fast Fourier transformation.
Definition: Bimage_correlate.cpp:363
Bstring
Definition: Bstring.h:29
Bimage::next
Bimage * next
Definition: Bimage.h:179
Bimage::find_center
int find_center(Bimage *pmask, double hires, double lores, double radius, double sigma, int refine_flag)
Finds the center of mass of an image by cross-correlation with its inverse.
Definition: Bimage_correlate.cpp:1235
verbose
int verbose
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Vector3< double >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
Bimage::find_peak
int find_peak(double radius=1e30, double sigma=0)
Finds the peak in an image to the nearest voxel.
Definition: Bimage_correlate.cpp:1340
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
Bimage::cross_correlate_validate
Bimage * cross_correlate_validate(Bimage *p, Bimage *pmask)
Calculates a masked cross-correlation map by Fast Fourier transformation.
Definition: Bimage_correlate.cpp:748
Bstring::real
double real()
Definition: Bstring.cpp:553
options.h
Header file for option handlers.
Bsub_image::FOM
double FOM()
Definition: Bimage.h:165
Boption::origin
Vector3< double > origin()
Definition: options.h:180
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
Bimage::ccmap_confidence
double ccmap_confidence(long nn)
Calculates a confidence level to associate with a cross-correlation peak.
Definition: Bimage_correlate.cpp:1950
Bimage::refine_peak
int refine_peak()
Refines the position of a peak to sub-voxel resolution.
Definition: Bimage_correlate.cpp:1615
Bimage::images
long images()
Definition: Bimage.h:504
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
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
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
Bimage::center_wrap
int center_wrap()
Centers an image with wrapping.
Definition: Bimage_transform.cpp:708
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
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::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
main
int main(int argc, char **argv)
Definition: bcc.cpp:51
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
rwimg.h
Header file for 2D and 3D image I/O.
Matrix.h
Generalized matrix class.
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
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bimage::auto_correlate
int auto_correlate(double hires, double lores)
Calculates an autocorrelation map by Fast Fourier transformation.
Definition: Bimage_correlate.cpp:265