Bsoft  2.1.0
Bernard's software package
mg_class.cpp
Go to the documentation of this file.
1 
9 #include "Bimage.h"
10 #include "rwimg.h"
11 #include "mg_class.h"
12 #include "mg_processing.h"
13 #include "mg_select.h"
14 #include "mg_ctf.h"
15 #include "Matrix3.h"
16 #include "linked_list.h"
17 #include "utilities.h"
18 
19 // Declaration of global variables
20 extern int verbose; // Level of output to the screen
21 
39 long mg_classify(Bproject* project, double resolution_hi, double resolution_lo,
40  int fom_type, double fom_cut, FSI_Kernel* kernel, int ctf_apply, int img_out)
41 {
42  // Main loop over all reference maps
43  long i, j, m, nsel(0);
44  double maxfom, CC, PD(0), R;
45  Vector3<double> translate, origin;
46  Matrix3 mat(1);
47  Bstring filename;
48  Bstring name_base;
49  Bstring insert("_diff.");
50  Bimage* p = NULL;
51  Bimage* pmap = NULL;
52  Bimage* proj = NULL;
53  Bimage* pdiff = NULL;
54  Bimage* mapproj = NULL;
55 
56  long nmap = count_list((char *)project->reference);
57  Bstring* map = project->reference;
58 
59  Bfield* field;
60  Bmicrograph* mg;
61  Bparticle* part;
62  long npart = project_count_mg_particles(project);
63 
64  int* num = new int[nmap+1];
65  float* fom = new float[npart*nmap];
66  float* fom_avg = new float[nmap+1];
67  float* fom_std = new float[nmap+1];
68 
69  for ( i=0; i<nmap+1; i++ ) fom_avg[i] = fom_std[i] = num[i] = 0;
70  for ( i=0; i<npart*nmap; i++ ) fom[i] = 0;
71 
72  if ( verbose ) {
73  cout << "Classifying particles with respect to maps:" << endl;
74  cout << "Number of maps: " << nmap << endl;
75  cout << "Resolution range: " << resolution_hi << " - " << resolution_lo << " Å" << endl;
76  cout << "FOM type: " << fom_type << endl;
77  cout << "FOM cutoff: " << fom_cut << endl;
78  if ( ctf_apply )
79  cout << "Applying CTF parameters" << endl;
80  if ( img_out%2 == 1 )
81  cout << "Output projections" << endl;
82  if ( img_out > 1 )
83  cout << "Output differences" << endl;
84  }
85 
86  pmap = read_img(*(project->reference), 0, 0);
87 
88  fft_plan planf_2D = fft_setup_plan(pmap->sizeX(), pmap->sizeY(), 1, FFTW_FORWARD, 1);
89  fft_plan planb_2D = fft_setup_plan(pmap->sizeX(), pmap->sizeY(), 1, FFTW_BACKWARD, 1);
90 
91  delete pmap;
92 
93  if ( verbose & VERB_RESULT ) {
94  cout << endl << "Map\tFile\tPart\t";
95  if ( fom_type == 1 ) cout << "R" << endl;
96  else if ( fom_type == 2 ) cout << "dPhi" << endl;
97  else cout << "CC" << endl;
98  }
99  for ( i=0, map = project->reference; map; map = map->next, i++ ) {
100  pmap = read_img(map->str(), 1, 0);
101  if ( !pmap )
102  return error_show("mg_classify", __FILE__, __LINE__);
103  pmap->change_type(Float);
104  pmap->statistics();
105  pmap->calculate_background();
106  origin = pmap->image->origin();
107  if ( origin[0] <= 0 || origin[0] >= pmap->sizeX() ) origin[0] = pmap->sizeX()/2;
108  if ( origin[1] <= 0 || origin[1] >= pmap->sizeY() ) origin[1] = pmap->sizeY()/2;
109  if ( origin[2] <= 0 || origin[2] >= pmap->sizeZ() ) origin[2] = pmap->sizeZ()/2;
110  pmap->origin(origin);
111  if ( kernel ) {
112  pmap->fft();
113  pmap->phase_shift_to_origin();
114  }
115  name_base = Bstring(i+1, "map%02d");
116  m = 0;
117  for ( field=project->field; field; field=field->next ) {
118  for ( mg=field->mg; mg; mg=mg->next ) {
119  p = read_img(mg->fpart, 0, -1);
120  if ( !p )
121  return error_show("mg_classify", __FILE__, __LINE__);
122  proj = p->copy_header(p->images());
123  delete p;
124  filename = proj->file_name();
125  filename = filename.pre_rev('.') + "_" + name_base + "." + filename.post_rev('.');
126  proj->file_name(filename.str());
127  proj->data_type(Float);
128  proj->data_alloc();
129  pdiff = proj->copy_header(proj->images());
130  filename = filename.pre_rev('.') + insert + filename.post_rev('.');
131  pdiff->file_name(filename.str());
132  pdiff->data_alloc();
133  for ( j=0, part=mg->part; part; part=part->next, j++ ) {
134  p = read_img(mg->fpart, 1, part->id-1);
135  if ( !p )
136  return error_show("Error in mg_classify", __FILE__, __LINE__);
137  p->change_type(Float);
138  p->statistics();
139  if ( !p->rescale_to_avg_std(0, 1) ) {
140  // Calculate the projected reference image
141  if ( verbose & VERB_FULL )
142  cout << "View and origin: " << part->view << tab << part->ori << endl;
143  mat = part->view.matrix();
144  if ( part->mag ) mat *= part->mag;
145  translate[0] = part->ori[0] - pmap->sizeX()/2;
146  translate[1] = part->ori[1] - pmap->sizeY()/2;
147  if ( kernel ) {
148  mapproj = pmap->central_section(mat, resolution_hi, kernel);
149  mapproj->phase_shift_to_center();
150  if ( ctf_apply )
151  img_ctf_apply_to_proj(mapproj, *(mg->ctf), part->def, 1e6, resolution_hi, planf_2D, planb_2D);
152  mapproj->fft_back(planb_2D);
153  mapproj->shift(translate);
154  mapproj->correct_background();
155  } else {
156  mapproj = pmap->rotate_project(mat, translate, pmap->sizeX()/2.0);
157  if ( ctf_apply )
158  img_ctf_apply_to_proj(mapproj, *(mg->ctf), part->def, 1e6, resolution_hi, planf_2D, planb_2D);
159  }
160  mapproj->statistics();
161  mapproj->rescale_to_avg_std(0, 1);
162  // Compare two 2D images ===> FOM
163  p->sampling(part->pixel_size);
164  p->origin(part->ori);
165  proj->image[j].origin(part->ori);
166  pdiff->image[j].origin(part->ori);
167  proj->image[j].view(part->view);
168  pdiff->image[j].view(part->view);
169  proj->replace(j, mapproj);
170  if ( fom_type == 0 ) {
171  CC = p->correlate(mapproj);
172  fom[m*nmap+i] = CC;
173  } else if ( fom_type == 2 ) {
174  PD = mapproj->average_phase_difference(p,
175  resolution_hi, resolution_lo, 1);
176  fom[m*nmap+i] = cos(PD);
177  }
178  R = mapproj->linear_fit(p, NULL, 0);
179  mapproj->invert();
180  if ( fom_type == 1 ) fom[m*nmap+i] = 1 - R;
181  pdiff->replace(j, mapproj);
182  delete p;
183  delete mapproj;
184  if ( verbose & VERB_RESULT ) {
185  cout << *map << tab << mg->fpart << tab << part->id << tab;
186  if ( fom_type == 1 ) cout << R << endl;
187  else if ( fom_type == 2 ) cout << PD*180.0/M_PI << endl;
188  else cout << CC << endl;
189  }
190  }
191  m++;
192  }
193  if ( img_out%2 == 1 ) {
194  proj->statistics();
195  cout << "Writing " << proj->file_name() << endl;
196  write_img(proj->file_name(), proj, 0);
197  mg->fpart = proj->file_name();
198  }
199  if ( img_out > 1 ) {
200  pdiff->statistics();
201  cout << "Writing " << pdiff->file_name() << endl;
202  write_img(pdiff->file_name(), pdiff, 0);
203  mg->fpart = pdiff->file_name();
204  }
205  delete proj;
206  delete pdiff;
207  }
208  }
209  delete pmap;
210  }
211 
212  fft_destroy_plan(planf_2D);
213  fft_destroy_plan(planb_2D);
214 
215  // Get the best FOM and assign the particle to that map
216  if ( verbose & VERB_RESULT ) {
217  cout << endl << "File\tPart\tMap\tFOM=";
218  if ( fom_type == 1 ) cout << "1-R" << endl;
219  else if ( fom_type == 2 ) cout << "cos(dPhi)" << endl;
220  else cout << "CC" << endl;
221  }
222  m = nsel = 0;
223  for ( field=project->field; field; field=field->next ) {
224  for ( mg=field->mg; mg; mg=mg->next ) {
225  for ( part=mg->part; part; part=part->next ) if ( part->sel ) {
226  maxfom = -1e37;
227  part->sel = 0;
228  for ( i=0; i<nmap; i++ ) {
229  if ( verbose & VERB_RESULT )
230  cout << mg->fpart << tab << part->id << tab << i+1 << tab << fom[m*nmap+i] << endl;
231  if ( maxfom < fom[m*nmap+i] ) {
232  maxfom = fom[m*nmap+i];
233  if ( maxfom >= fom_cut ) part->sel = i+1;
234  }
235  }
236  num[part->sel]++;
237  fom_avg[part->sel] += maxfom;
238  fom_std[part->sel] += maxfom*maxfom;
239  part->fom[0] = maxfom;
240  if ( part->sel ) nsel++;
241  m++;
242  }
243  }
244  }
245 
246  if ( verbose )
247  cout << "Map\tNumber\t%\tAvg\tStd" << endl;
248  for ( i=0; i<=nmap; i++ ) {
249  if ( num[i] ) {
250  fom_avg[i] /= num[i];
251  fom_std[i] = fom_std[i]/num[i] - fom_avg[i]*fom_avg[i];
252  if ( fom_std[i] > 0 )
253  fom_std[i] = sqrt(fom_std[i]);
254  else
255  fom_std[i] = 0;
256  }
257  if ( verbose )
258  cout << i << tab << num[i] << tab << num[i]*100.0/npart << tab << fom_avg[i] << tab << fom_std[i] << endl;
259  }
260 
261  delete[] num;
262  delete[] fom;
263  delete[] fom_avg;
264  delete[] fom_std;
265 
266  return nsel;
267 }
268 
project_count_mg_particles
long project_count_mg_particles(Bproject *project)
Counts all the particles in the project structure.
Definition: mg_select.cpp:415
Bmicrograph
Definition: mg_processing.h:201
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
mg_class.h
Header file for classification of raw single particle images with respect to multiple models.
Bimage::shift
int shift(Vector3< double > vec, int fill_type=0, double fill=0)
Shifts an image.
Definition: Bimage_transform.cpp:589
Bparticle::view
View view
Definition: mg_processing.h:102
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
img_ctf_apply_to_proj
int img_ctf_apply_to_proj(Bimage *proj, CTFparam em_ctf, double defocus, double res_lo, double res_hi, fft_plan planf_2D, fft_plan planb_2D)
Applies a CTF to a projection image.
Definition: mg_ctf.cpp:354
Bimage::central_section
Bimage * central_section(Matrix3 mat, double resolution, FSI_Kernel *kernel)
Calculates a central section of a 3D fourier transform.
Definition: Bimage_project.cpp:287
Vector3< double >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
Matrix3.h
3x3 Matrix manipulation functions
mg_select.h
Header file for micrograph processing.
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:719
Bparticle::fom
double fom[NFOM]
Definition: mg_processing.h:103
Bmicrograph::part
Bparticle * part
Definition: mg_processing.h:242
Bimage::calculate_background
int calculate_background(long nn, int flag)
Calculates the background for one sub-image.
Definition: Bimage_background.cpp:27
verbose
int verbose
Bimage::rotate_project
Bimage * rotate_project(Matrix3 mat, Vector3< double > translate, double radial_cutoff, int norm_flag=1)
Rotates a 3D map and projects it along the z-axis.
Definition: Bimage_project.cpp:118
Bimage::fft_back
int fft_back()
Definition: Bimage.h:831
Bsub_image::view
void view(double x, double y, double z, double a)
Definition: Bimage.h:157
Bmicrograph::fpart
Bstring fpart
Definition: mg_processing.h:208
Bfield
Definition: mg_processing.h:260
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
Bimage::correct_background
int correct_background(long nn, int flag)
Corrects the background for one sub-image.
Definition: Bimage_background.cpp:202
count_list
long count_list(char *list)
Counts the number of items in a linked list.
Definition: linked_list.cpp:235
Bimage::images
long images()
Definition: Bimage.h:504
project
Bproject * project
Definition: bshow.cpp:33
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
Bstring::str
string str()
Definition: Bstring.h:42
insert
string insert(string &filename, string ins)
Definition: string_util.cpp:48
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
fft_destroy_plan
int fft_destroy_plan(fft_plan plan)
Deallocates a plan for fast Fourier transforms.
Definition: fft.cpp:87
Bimage::correlate
double correlate(Bimage *p)
Calculates correlation coefficient between two images.
Definition: Bimage_correlate.cpp:36
Bimage::fft
int fft()
Definition: Bimage.h:830
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:421
VERB_RESULT
#define VERB_RESULT
Definition: utilities.h:109
mg_ctf.h
Functions for CTF (contrast transfer function) processing.
Bimage
General image parameter class.
Definition: Bimage.h:177
Bproject
Definition: mg_processing.h:336
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:271
Bparticle::sel
long sel
Definition: mg_processing.h:104
mg_classify
long mg_classify(Bproject *project, double resolution_hi, double resolution_lo, int fom_type, double fom_cut, FSI_Kernel *kernel, int ctf_apply, int img_out)
Classifies particle images with respect to a series of reference maps.
Definition: mg_class.cpp:39
Bparticle::pixel_size
Vector3< double > pixel_size
Definition: mg_processing.h:99
Bimage::replace
int replace(Bimage *img)
Replaces the data with that from the given image.
Definition: Bimage.cpp:2830
Bimage.h
Header file for image class.
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
fft_setup_plan
fft_plan fft_setup_plan(long x, long y, long z, fft_direction dir, int opt)
Sets up a plan for fast Fourier transforms.
Definition: fft.cpp:31
Bparticle::def
double def
Definition: mg_processing.h:96
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
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.
Bmicrograph::ctf
CTFparam * ctf
Definition: mg_processing.h:241
Bfield::next
Bfield * next
Definition: mg_processing.h:261
Bstring::next
Bstring * next
Definition: Bstring.h:31
Bimage::copy_header
Bimage * copy_header()
Definition: Bimage.h:658
Bparticle::ori
Vector3< double > ori
Definition: mg_processing.h:101
Bimage::linear_fit
double linear_fit(Bimage *p, Bimage *pmask, double max_exclude)
Linear least squares fit of two images.
Definition: Bimage_combine.cpp:477
mg_processing.h
Header file for micrograph processing.
fft_plan
fftwf_plan fft_plan
Definition: fft.h:20
Bparticle::next
Bparticle * next
Definition: mg_processing.h:91
linked_list.h
Header file for generalized linked list functions.
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
Bimage::invert
void invert()
Inverts the data in the image.
Definition: Bimage.cpp:3111
Bproject::reference
Bstring * reference
Definition: mg_processing.h:341
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
View::matrix
Matrix3 matrix()
Definition: View.h:153
Bimage::phase_shift_to_origin
int phase_shift_to_origin()
Phase shifts a set of reflections to the image origin.
Definition: Bimage_complex.cpp:327
Bparticle::mag
double mag
Definition: mg_processing.h:95
Float
@ Float
Definition: utilities.h:161
Bimage::phase_shift_to_center
int phase_shift_to_center()
Phase shifts a set of reflections to the nominal center of the image origin.
Definition: Bimage_complex.cpp:382
Bmicrograph::next
Bmicrograph * next
Definition: mg_processing.h:202
Bimage::average_phase_difference
double average_phase_difference(Bimage *p, double res_hi, double res_lo, int weighting=1)
Calculates the average of the absolute phase difference between two images within given resolution sh...
Definition: Bimage_fspace.cpp:1986
Bfield::mg
Bmicrograph * mg
Definition: mg_processing.h:267
Bparticle::id
int id
Definition: mg_processing.h:93
kernel
Definition: kernlut.h:14
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
FSI_Kernel
Definition: FSI_Kernel.h:14
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