Bsoft  2.1.0
Bernard's software package
bmapdist.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "rwmg.h"
11 #include "mg_img_proc.h"
12 #include "Matrix.h"
13 #include "cluster.h"
14 #include "linked_list.h"
15 #include "file_util.h"
16 #include "utilities.h"
17 #include "options.h"
18 #include "timer.h"
19 
20 // Declaration of global variables
21 extern int verbose; // Level of output to the screen
22 
23 // Function prototypes
24 Matrix img_map_distance(Bstring* file_list, int type, double* cutoff,
25  double hires, double lores, Bimage* pmask);
26 int project_set_selection(Bproject* project, long n, long* idx);
27 int project_rec_select(Bproject* project, long* idx);
28 
30 {
31  Bstring* file_list = NULL;
32  Bparticle* part = project->class_avg;
33  Breconstruction* rec;
34 
35  if ( flag ) {
36  if ( part )
37 // for ( part = project->class_avg; part; part = part->next )
38  if ( part->fpart.length() > 3 ) string_add(&file_list, part->fpart);
39  } else {
40  for ( rec = project->rec; rec; rec = rec->next )
41  if ( rec->frec.length() > 3 ) string_add(&file_list, rec->frec);
42  }
43 
44  return file_list;
45 }
46 
47 
48 // Usage assistance
49 const char* use[] = {
50 " ",
51 "Usage: bmapdist [options] input.star/input.img [input.star/input.img]",
52 "---------------------------------------------------------------------",
53 "Calculates distance criterions between a set of 2D images or 3D maps.",
54 "CC: Correlation coefficient.",
55 "R: R factor (Euclidean distance).",
56 "FSC: Resolution based on Fourier shell correlation.",
57 "DPR: Resolution based on differential phase residual.",
58 "Clustering: The preference value should be set to the approximate average in the",
59 " similarity matrix. The damping factor should be ~0.5, or between 0.1 and 0.9.",
60 " ",
61 "Actions:",
62 "-type fsc Output type of criterion: cc (default), r, fsc, dpr.",
63 "-preference -10 Homogenous preference value for clustering.",
64 "-TwoD Compare 2D images rather than 3D maps.",
65 " ",
66 "Parameters:",
67 "-verbose 7 Verbosity of output.",
68 "-resolution 15.3 Resolution limit (angstrom).",
69 "-cutoff 0.5,45 Resolution cutoffs for FSC and DPR.",
70 "-lambda 0.6 Damping factor for clustering (default 0.5).",
71 " ",
72 "Input:",
73 "-mask mask.tif Real space mask (same size as input maps).",
74 " ",
75 "Output:",
76 "-output file.star Output parameter file.",
77 "-matrix mat.dat Output comparison matrix file.",
78 " ",
79 NULL
80 };
81 
82 int main(int argc, char **argv)
83 {
84  // Initialize variables
85  int type(0); // Default type is CC
86  int flag(0); // Flag to indicate 2D images
87  double hires(0.01); // Default resolution is all-inclusive
88  double lores(1e37); // Default resolution is all-inclusive
89  double cutoff[4] = {0.5,45,1,0.5}; // Cutoff values
90  double pref(-1e37); // Preference value for clustering
91  double lambda(0.5); // Damping factor for clustering
92  Bstring real_mask_file; // Real space mask file
93  Bstring outfile; // Output parameter file
94  Bstring matfile; // Output comparison matrix file
95 
96  int optind;
97  Boption* option = get_option_list(use, argc, argv, optind);
98  Boption* curropt;
99  for ( curropt = option; curropt; curropt = curropt->next ) {
100  if ( curropt->tag == "type" ) {
101  if ( curropt->value[0] == 'r' ) type = 1;
102  if ( curropt->value[0] == 'f' ) type = 2;
103  if ( curropt->value[0] == 'd' ) type = 3;
104  }
105  if ( curropt->tag == "preference" )
106  if ( ( pref = curropt->value.real() ) < -1e36 )
107  cerr << "-preference: A preference value must be specified!" << endl;
108  if ( curropt->tag == "TwoD" ) flag = 1;
109  if ( curropt->tag == "resolution" )
110  if ( curropt->values(hires, lores) < 1 )
111  cerr << "-resolution: A resolution limit must be specified!" << endl;
112  if ( curropt->tag == "cutoff" )
113  if ( curropt->values(cutoff[0], cutoff[1], cutoff[2], cutoff[3]) < 1 )
114  cerr << "-cutoff: A cuttoff must be specified!" << endl;
115  if ( curropt->tag == "lambda" )
116  if ( ( lambda = curropt->value.real() ) < 0.001 )
117  cerr << "-lambda: A damping factor must be specified!" << endl;
118  if ( curropt->tag == "mask" )
119  real_mask_file = curropt->filename();
120  if ( curropt->tag == "output" )
121  outfile = curropt->filename();
122  if ( curropt->tag == "matrix" )
123  matfile = curropt->filename();
124  }
125  option_kill(option);
126 
127  double ti = timer_start();
128 
129  // Read all the parameter files
130  Bstring* file_list = NULL;
131  while ( optind < argc ) string_add(&file_list, argv[optind++]);
132  if ( !file_list ) {
133  cerr << "Error: No image or parameter files specified!" << endl;
134  bexit(-1);
135  }
136 
137  Bproject* project = NULL;
138 
139  if ( file_type(*file_list) == Micrograph ) {
140  project = read_project(file_list);
141  string_kill(file_list);
142  file_list = project_get_filenames(project, flag);
143  }
144 
145  if ( !file_list ) {
146  cerr << "Error: No image file names found!" << endl;
147  bexit(-1);
148  }
149 
150  Bimage* pmask = NULL;
151  if ( real_mask_file.length() )
152  pmask = read_img(real_mask_file, 1, 0);
153 
154  Matrix matrix = img_map_distance(file_list, type, cutoff, hires, lores, pmask);
155 
156  long i, *idx, ncluster(0);
157  if ( matrix.rows() ) {
158  if ( pref > -1e30 ) {
159  for ( i=0; i<matrix.rows(); i++ )
160  matrix[i][i] = pref;
161  idx = affin_prop_clustering(matrix, 500, 50, lambda, ncluster);
162  if ( project ) {
163  if ( flag )
164  project_set_selection(project, ncluster, idx);
165  else
167  }
168  delete[] idx;
169  }
170 
171  if ( matfile.length() )
172  matrix.write(matfile);
173 
174  }
175 
176  // Write an output parameter file if a name is given
177  if ( project && outfile.length() ) {
178  write_project(outfile, project, 0, 0);
179  }
180 
182  delete pmask;
183 
184  if ( verbose & VERB_TIME )
185  timer_report(ti);
186 
187  bexit(0);
188 }
189 
190 Matrix img_map_distance(Bstring* file_list, int type, double* cutoff,
191  double hires, double lores, Bimage* pmask)
192 {
193  if ( type < 0 || type > 3 ) type = 0;
194 
195  if ( hires < 0.01 ) hires = 0.01;
196 
197 // long nmap = count_list((char *)file_list);
198  // Main loop over all reference maps
199  long i, j, ii, jj, n1, n2, nmap(0);
200  Bstring* fn1;
201  Bstring* fn2;
202  Bimage* p1 = NULL;
203  Bimage* p2 = NULL;
204 
205  for ( fn1 = file_list; fn1; fn1 = fn1->next ) {
206  p1 = read_img(*fn1, 0, -1);
207  nmap += p1->images();
208  delete p1;
209  }
210 
211  if ( verbose ) {
212  cout << "Calculating the similarities and differences between " << nmap << " images:" << endl;
213  cout << "Resolution limit: " << hires << endl;
214  if ( pmask )
215  cout << "Mask: " << pmask->file_name() << endl;
216  }
217 
218  Matrix matrix;
219 
220  if ( nmap < 2 ) return matrix;
221 
222  Bimage* pc = NULL;
223  double* CC = new double[nmap*nmap];
224  double* R = new double[nmap*nmap];
225  double* FSC = new double[nmap*nmap];
226  double* DPR = new double[nmap*nmap];
227  Bplot* plot;
228 
229  cout << "Map1\tMap2\tCC\tR\tFSC\tDPR" << endl;
230  for ( i=0, fn1 = file_list; fn1; fn1 = fn1->next ) {
231  p1 = read_img(*fn1, 0, -1);
232  if ( !p1 ) bexit(-1);
233  n1 = p1->images();
234  delete p1;
235  for ( ii=0; ii<n1; ++ii, ++i ) {
236  p1 = read_img(*fn1, 1, ii);
237  p1->change_type(Float);
238  p1->rescale_to_avg_std(0, 1);
239  if ( pmask ) p1->multiply(pmask);
240  for ( j=0, fn2 = file_list; j<i && fn2; fn2 = fn2->next ) {
241  p2 = read_img(*fn2, 0, -1);
242  if ( !p2 ) bexit(-1);
243  n2 = p2->images();
244  delete p2;
245  for ( jj=0; j<i && jj<n2; ++jj, ++j ) {
246  p2 = read_img(*fn2, 1, jj);
247  p2->change_type(Float);
248  p2->rescale_to_avg_std(0, 1);
249  if ( pmask ) p2->multiply(pmask);
250  CC[i*nmap+j] = CC[j*nmap+i] = p1->correlate(p2);
251  R[i*nmap+j] = R[j*nmap+i] = p1->R_factor(p2);
252  pc = p1->resolution_prepare(p2);
253  delete p2;
254  plot = pc->fsc_dpr(hires, 1);
255  delete pc;
256  FSC[i*nmap+j] = FSC[j*nmap+i] = 1/plot->cut(1, cutoff[0], -1);
257  DPR[i*nmap+j] = DPR[j*nmap+i] = 1/plot->cut(2, cutoff[1], 1);
258  delete plot;
259  cout << *fn1 << "(" << i << ")" << tab << *fn2 << "(" << j << ")" << tab <<
260  CC[i*nmap+j] << tab << R[i*nmap+j] << tab <<
261  FSC[i*nmap+j] << tab << DPR[i*nmap+j] << endl;
262  }
263  }
264  delete p1;
265  }
266  }
267 
268  // All CC values
269  cout << endl << "Correlation coefficients:" << endl;
270  for ( i=0; i<nmap; i++ ) {
271  CC[i*nmap+i] = 1;
272  cout << tab << i+1;
273  }
274  cout << endl;
275  for ( i=0; i<nmap; i++ ) {
276  cout << i+1;
277  for ( j=0; j<nmap; j++ )
278  cout << tab << CC[i*nmap+j];
279  cout << endl;
280  }
281  cout << endl;
282 
283  // The negative logarithm of the CC values
284  cout << "-log(Correlation coefficients):" << endl;
285  for ( i=0; i<nmap; i++ )
286  cout << tab << i+1;
287  cout << endl;
288  for ( i=0; i<nmap; i++ ) {
289  cout << i+1;
290  for ( j=0; j<nmap; j++ )
291  cout << tab << -log(CC[i*nmap+j]);
292  cout << endl;
293  }
294  cout << endl;
295 
296  // All R values
297  cout << "R factors:" << endl;
298  for ( i=0; i<nmap; i++ ) {
299  R[i*nmap+i] = 0;
300  cout << tab << i+1;
301  }
302  cout << endl;
303  for ( i=0; i<nmap; i++ ) {
304  cout << i+1;
305  for ( j=0; j<nmap; j++ )
306  cout << tab << R[i*nmap+j];
307  cout << endl;
308  }
309  cout << endl;
310 
311  // The FSC values
312  cout << "Resolution based on Fourier shell correlation:" << endl;
313  cout << "Cutoff: " << cutoff[0] << endl;
314  for ( i=0; i<nmap; i++ ) {
315  cout << tab << i+1;
316  FSC[i*nmap+i] = hires;
317  }
318  cout << endl;
319  for ( i=0; i<nmap; i++ ) {
320  cout << i+1;
321  for ( j=0; j<nmap; j++ )
322  cout << tab << FSC[i*nmap+j];
323  cout << endl;
324  }
325  cout << endl;
326 
327  // The DPR values
328  cout << "Resolution based on differential phase residuals:" << endl;
329  cout << "Cutoff: " << cutoff[1] << endl;
330  for ( i=0; i<nmap; i++ ) {
331  cout << tab << i+1;
332  DPR[i*nmap+i] = hires;
333  }
334  cout << endl;
335  for ( i=0; i<nmap; i++ ) {
336  cout << i+1;
337  for ( j=0; j<nmap; j++ )
338  cout << tab << DPR[i*nmap+j];
339  cout << endl;
340  }
341  cout << endl;
342 
343  matrix = Matrix(nmap, nmap);
344 
345  Bstring label;
346  for ( i=0; i<nmap; i++ ) {
347  switch ( type ) {
348  case 0:
349  for ( j=0; j<nmap; j++ ) matrix[i][j] = CC[i*nmap+j];
350  break;
351  case 1:
352  for ( j=0; j<nmap; j++ ) matrix[i][j] = R[i*nmap+j];
353  break;
354  case 2:
355  for ( j=0; j<nmap; j++ ) matrix[i][j] = FSC[i*nmap+j];
356  break;
357  case 3:
358  for ( j=0; j<nmap; j++ ) matrix[i][j] = DPR[i*nmap+j];
359  break;
360  }
361  }
362 
363  delete[] CC;
364  delete[] R;
365  delete[] FSC;
366  delete[] DPR;
367 
368  return matrix;
369 }
370 
371 int project_set_selection(Bproject* project, long n, long* idx)
372 {
373  long i, j;
374  Bimage* p = NULL;
375 
377  p = read_img(project->class_avg->fpart, 1, -1);
378  else {
379  cerr << "Error: No class averages!" << endl;
380  return -1;
381  }
382 
383  Bstring filename(project->class_avg->fpart);
384  filename = filename.pre_rev('.') + "_cluster." + filename.post_rev('.');
385 
386  if ( verbose ) {
387  cout << "Replacing class averages:" << endl;
388  cout << "Old number: " << p->images() << endl;
389  cout << "New number: " << n << endl;
390  cout << "New file name: " << filename << endl;
391  }
392 
393  Bfield* field = project->field;
394  Bmicrograph* mg = field->mg;
395  Bparticle* part = NULL;
396 
397  for ( field = project->field; field; field = field->next )
398  for ( mg = field->mg; mg; mg = mg->next )
399  for ( part = mg->part; part; part = part->next )
400  if ( part->sel )
401  part->sel = idx[part->sel-1] + 1;
402 
403  Bimage* pnu = p->copy_header(n);
404  pnu->data_alloc_and_clear();
405  Bimage* p1;
406  Bparticle* partlist = NULL;
407  Bparticle* nupart = NULL;
408 
409  for ( i=j=0, part = project->class_avg; i < p->images(); ++i, part = part->next ) {
410  if ( i == idx[i] ) {
411  nupart = particle_copy(&partlist, part);
412  nupart->fpart = filename;
413  nupart->id = ++j;
414  nupart->group = i;
415  nupart->sel = 0;
416  }
417  }
418 
419  for ( i=0, part = project->class_avg; i < p->images(); ++i, part = part->next ) {
420  for ( j=0, nupart = partlist; nupart; ++j, nupart = nupart->next )
421  if ( nupart->group == idx[i] ) break;
422  nupart->sel += part->sel;
423  p1 = p->extract(i);
424  pnu->add(j, p1);
425  delete p1;
426  }
427 
428  pnu->statistics();
429 
430  for ( j=0; j<pnu->images(); ++j )
431  pnu->rescale_to_avg_std(j, 0, 1);
432 
434  project->class_avg = partlist;
435 
436  write_img(filename, pnu, 0);
437 
438  delete pnu;
439  delete p;
440 
441  return 0;
442 }
443 
444 
446 {
447  long i;
448  Breconstruction* rec;
449 
450  for ( i=0, rec = project->rec; rec; rec = rec->next, i++ ) {
451  rec->select = idx[i] + 1;
452  }
453 
454  return 0;
455 }
456 
Bimage::R_factor
double R_factor(Bimage *p)
Calculates an R factor between two images.
Definition: Bimage_correlate.cpp:218
Boption
A class for a command line input option.
Definition: options.h:26
project_kill
int project_kill(Bproject *project)
Frees a whole project.
Definition: mg_processing.cpp:429
Bmicrograph
Definition: mg_processing.h:201
img_map_distance
Matrix img_map_distance(Bstring *file_list, int type, double *cutoff, double hires, double lores, Bimage *pmask)
Definition: bmapdist.cpp:190
Bproject::rec
Breconstruction * rec
Definition: mg_processing.h:347
cluster.h
Header file for clustering functions.
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:768
Bimage::fsc_dpr
Bplot * fsc_dpr(double hi_res, double sampling_ratio=1, int flag=0)
Calculates an FSC and DPR curves from two images.
Definition: Bimage_resolution.cpp:92
file_util.h
Header file for file checking functions.
Bstring
Definition: Bstring.h:29
Bparticle
Definition: mg_processing.h:90
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:436
Bproject::field
Bfield * field
Definition: mg_processing.h:346
Boption::next
Boption * next
Definition: options.h:28
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
Bplot
Definition: Bplot.h:146
Bstring::real
double real()
Definition: Bstring.cpp:553
Bmicrograph::part
Bparticle * part
Definition: mg_processing.h:242
options.h
Header file for option handlers.
project_set_selection
int project_set_selection(Bproject *project, long n, long *idx)
Definition: bmapdist.cpp:371
Bfield
Definition: mg_processing.h:260
use
const char * use[]
Definition: bmapdist.cpp:49
Bimage::images
long images()
Definition: Bimage.h:504
affin_prop_clustering
long * affin_prop_clustering(Matrix s, long maxit, long convit, double lambda, long &ncluster)
Generate clusters from a similarity matrix using affinity propagation.
Definition: cluster.cpp:102
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.
Bimage::multiply
void multiply(long j, Complex< double > cv)
Definition: Bimage.h:325
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
Bimage::add
void add(long j, double v)
Definition: Bimage.h:321
Bparticle::fpart
Bstring fpart
Definition: mg_processing.h:92
Matrix::rows
long rows()
Definition: Matrix.h:153
Bimage::correlate
double correlate(Bimage *p)
Calculates correlation coefficient between two images.
Definition: Bimage_correlate.cpp:36
Micrograph
@ Micrograph
Definition: file_util.h:23
Boption::value
Bstring value
Definition: options.h:30
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:421
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
Bproject
Definition: mg_processing.h:336
Bplot::cut
double cut(long icol, double threshold, int dir=1)
Definition: Bplot.h:251
Bparticle::sel
long sel
Definition: mg_processing.h:104
Bimage::resolution_prepare
Bimage * resolution_prepare(Bimage *p)
Definition: Bimage_resolution.cpp:17
mg_img_proc.h
Header file for image processing from micrograph structures.
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
main
int main(int argc, char **argv)
Definition: bmapdist.cpp:82
Bimage::data_alloc_and_clear
unsigned char * data_alloc_and_clear()
Definition: Bimage.h:272
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
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
project_get_filenames
Bstring * project_get_filenames(Bproject *project, int flag)
Definition: bmapdist.cpp:29
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
Breconstruction
Definition: mg_processing.h:283
Bstring::length
long length() const
Definition: Bstring.cpp:263
Breconstruction::select
long select
Definition: mg_processing.h:286
project_rec_select
int project_rec_select(Bproject *project, long *idx)
Definition: bmapdist.cpp:445
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:76
Bproject::class_avg
Bparticle * class_avg
Definition: mg_processing.h:348
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
rwimg.h
Header file for 2D and 3D image I/O.
Bfield::next
Bfield * next
Definition: mg_processing.h:261
Bstring::next
Bstring * next
Definition: Bstring.h:31
write_project
int write_project(const char *filename, Bproject *project)
Writing micrograph parameters to a file.
Definition: rwmg.cpp:321
Bimage::copy_header
Bimage * copy_header()
Definition: Bimage.h:658
Bparticle::group
int group
Definition: mg_processing.h:94
Matrix.h
Generalized matrix class.
Bimage::extract
Bimage * extract(long nn)
Extracts one sub-image into new image structure.
Definition: Bimage_extract.cpp:21
Bparticle::next
Bparticle * next
Definition: mg_processing.h:91
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
particle_copy
Bparticle * particle_copy(Bparticle **partlist, Bparticle *part)
Adds one particle onto a list of particle parameters.
Definition: mg_processing.cpp:614
Breconstruction::frec
Bstring frec
Definition: mg_processing.h:290
Matrix::write
void write(Bstring &filename)
Definition: Matrix.h:95
verbose
int verbose
particle_kill
int particle_kill(Bparticle *part)
Frees a list of particles.
Definition: mg_processing.cpp:528
Matrix
Definition: Matrix.h:22
Float
@ Float
Definition: utilities.h:161
Bmicrograph::next
Bmicrograph * next
Definition: mg_processing.h:202
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:713
Bfield::mg
Bmicrograph * mg
Definition: mg_processing.h:267
Bparticle::id
int id
Definition: mg_processing.h:93
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
file_type
FileType file_type(const char *filename)
Checks the file type using the extension and contents.
Definition: file_util.cpp:178
Breconstruction::next
Breconstruction * next
Definition: mg_processing.h:284
Bimage::rescale_to_avg_std
int rescale_to_avg_std(double nuavg, double nustd)
Rescales the image data to a given average and standard deviation.
Definition: Bimage_rescale.cpp:163