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