| Bsoft 2.0.5 Bernard's software package

mg_multiple.cpp
Go to the documentation of this file.
1
9 #include "mg_processing.h"
10 #include "mg_img_proc.h"
11 #include "mg_particle_select.h"
12 #include "rwmg.h"
13 #include "Matrix.h"
14 #include "linked_list.h"
15 #include "utilities.h"
16
17 // Declaration of global variables
18 extern int verbose; // Level of output to the screen
19
20 long project_check_same_number_particles(Bproject* project_list)
21 {
22 long i, npart(0), npart2(0), err(0);
23 Bproject* project = NULL;
24
25 if ( project_list->select < 1 ) {
26 npart = project_count_mg_particles(project_list);
27 if ( verbose )
28 cout << "Number of particles: " << npart << endl;
29 for ( i=2, project=project_list->next; project; project=project->next, ++i ) {
30 npart2 = project_count_mg_particles(project);
31 if ( npart2 != npart ) {
32 err++;
33 cerr << "Error: Incorrect number of particles in project " << i << ": " << npart2 << endl;
34 }
35 }
36 } else {
37 npart = project_count_rec_particles(project_list);
38 if ( verbose )
39 cout << "Number of particles: " << npart << endl;
40 for ( i=2, project=project_list->next; project; project=project->next, ++i ) {
41 npart2 = project_count_rec_particles(project);
42 if ( npart2 != npart ) {
43 err++;
44 cerr << "Error: Incorrect number of particles in project " << i << ": " << npart2 << endl;
45 }
46 }
47 }
48
49 if ( err ) bexit(-1);
50
51 return npart;
52 }
53
65 Bproject* project_multi_merge(Bstring* file_list, int fom_index, int flags)
66 {
67 int use_rec(flags & 1);
68 int temp_flag(flags & 2);
69 int reset(flags & 4);
70
71 long nmg(0), nrec(0);
72 Bstring* filename;
73 Bproject* project = NULL;
74 Bfield* field = NULL;
75 Bmicrograph* mg = NULL;
76 Breconstruction* rec = NULL;
77 Bproject* project2 = NULL;
78 Bfield* field2 = NULL;
79 Bmicrograph* mg2 = NULL;
80 Breconstruction* rec2 = NULL;
81
82 if ( verbose ) {
83 cout << "Merging parameter files:" << endl;
84 cout << "FOM index: " << fom_index << endl;
85 if ( temp_flag ) cout << "First project is a template" << endl;
86 cout << endl;
87 }
88
89 for ( filename = file_list; filename; filename = filename->next ) {
90 project2 = read_project(*filename);
91 if ( use_rec ) project2->select = 1;
92 if ( reset ) part_reset_selection(project2);
93 if ( !project ) {
94 project = project2;
95 if ( temp_flag ) {
96 if ( verbose )
97 cout << "Resetting the template selections" << endl << endl;
98 part_set_selection(project, -1);
99 part_set_FOM(project, 0, 0);
100 part_set_FOM(project, 1, 0);
101 }
102 } else {
103 for ( field2 = project2->field; field2; ) { // Field to be merged
104 // Find the same field
105 for ( field = project->field; field && field->id != field2->id; field = field->next ) ;
106 if ( !field ) {
107 if ( project->field ) {
108 for ( field = project->field; field->next; field = field->next ) ;
109 field->next = field2;
110 } else {
111 project->field = field2;
112 }
113 field = field2;
114 field2 = field2->next;
115 field->next = NULL;
116 } else {
117 for ( mg2 = field2->mg; mg2; ) {
118 // Find the same micrograph
119 for ( mg = field->mg; mg && mg->id != mg2->id; mg = mg->next ) ;
120 if ( !mg ) {
121 if ( field->mg ) {
122 for ( mg = field->mg; mg->next; mg = mg->next ) ;
123 mg->next = mg2;
124 } else {
125 field->mg = mg2;
126 }
127 mg = mg2;
128 mg2 = mg2->next;
129 mg->next = NULL;
130 } else {
131 mg2->select = 1;
132 micrograph_update(mg, mg2, fom_index, 63);
133 mg = mg2;
134 mg2 = mg2->next;
135 micrograph_kill(mg);
136 nmg++;
137 }
138 }
139 field2->mg = NULL;
140 field = field2;
141 field2 = field2->next;
142 field_kill(field);
143 }
144 }
145 project2->field = NULL;
146 for ( rec2 = project2->rec; rec2; ) {
147 // Find the same reconstruction
148 for ( rec = project->rec; rec && rec2->id != rec->id; rec = rec->next ) ;
149 if ( rec ) {
150 if ( rec2->fom >= rec->fom ) {
151 rec2->select = 1;
152 reconstruction_update(rec, rec2, fom_index, 63);
153 }
154 rec = rec2;
155 rec2 = rec2->next;
156 reconstruction_kill(rec);
157 nrec++;
158 } else {
159 if ( project->rec ) {
160 for ( rec = project->rec; rec->next; rec = rec->next ) ;
161 rec->next = rec2;
162 } else {
163 project->rec = rec2;
164 }
165 rec = rec2;
166 rec2 = rec2->next;
167 rec->next = NULL;
168 }
169 }
170 project2->rec = NULL;
171 project_kill(project2);
172 }
173 }
174
175 long npart(0), nsel(0);
176 double percentage(0);
177 npart += project_count_mg_particles(project);
178 nsel += project_count_mg_part_selected(project);
179 npart += project_count_rec_particles(project);
180 nsel += project_count_rec_part_selected(project);
181
182 if ( npart ) percentage = nsel*100.0/npart;
183
184 if ( verbose ) {
185 cout << "Micrographs updated: " << nmg << endl;
186 cout << "Reconstructions updated: " << nrec << endl;
187 cout << "Number of particles selected: " << nsel << " (" << percentage << " %)" << endl << endl;
188 }
189
190 return project;
191 }
192
204 long project_multi_add(Bproject* project, Bstring* file_list, int fom_index)
205 {
206 long nmg(0), nrec(0);
207 Bstring* filename;
208 Bfield* field = NULL;
209 Bmicrograph* mg = NULL;
210 Breconstruction* rec = NULL;
211 Bproject* project2 = NULL;
212 Bfield* field2 = NULL;
213 Bmicrograph* mg2 = NULL;
214 Breconstruction* rec2 = NULL;
215
216 if ( verbose ) {
217 cout << "Merging parameter files:" << endl;
218 cout << "FOM index: " << fom_index << endl;
219 cout << endl;
220 }
221
222 for ( filename = file_list; filename; filename = filename->next ) {
223 project2 = read_project(*filename);
224 if ( !project ) {
225 project = project2;
226 } else {
227 for ( field2 = project2->field; field2; ) { // Field to be merged
228 // Find the same field
229 for ( field = project->field; field && field->id != field2->id; field = field->next ) ;
230 if ( !field ) {
231 if ( project->field ) {
232 for ( field = project->field; field->next; field = field->next ) ;
233 field->next = field2;
234 } else {
235 project->field = field2;
236 }
237 field = field2;
238 field2 = field2->next;
239 field->next = NULL;
240 } else {
241 for ( mg2 = field2->mg; mg2; ) {
242 // Find the same micrograph
243 for ( mg = field->mg; mg && mg->id != mg2->id; mg = mg->next ) ;
244 if ( !mg ) {
245 if ( field->mg ) {
246 for ( mg = field->mg; mg->next; mg = mg->next ) ;
247 mg->next = mg2;
248 } else {
249 field->mg = mg2;
250 }
251 mg = mg2;
252 mg2 = mg2->next;
253 mg->next = NULL;
254 } else {
255 mg2->select = 1;
256 micrograph_update(mg, mg2, fom_index, 63);
257 mg = mg2;
258 mg2 = mg2->next;
259 micrograph_kill(mg);
260 nmg++;
261 }
262 }
263 field2->mg = NULL;
264 field = field2;
265 field2 = field2->next;
266 field_kill(field);
267 }
268 }
269 project2->field = NULL;
270 for ( rec2 = project2->rec; rec2; ) {
271 // Find the same reconstruction
272 for ( rec = project->rec; rec && rec2->id != rec->id; rec = rec->next ) ;
273 if ( rec ) {
274 if ( rec2->fom >= rec->fom ) {
275 rec2->select = 1;
276 reconstruction_update(rec, rec2, fom_index, 63);
277 }
278 rec = rec2;
279 rec2 = rec2->next;
280 reconstruction_kill(rec);
281 nrec++;
282 } else {
283 if ( project->rec ) {
284 for ( rec = project->rec; rec->next; rec = rec->next ) ;
285 rec->next = rec2;
286 } else {
287 project->rec = rec2;
288 }
289 rec = rec2;
290 rec2 = rec2->next;
291 rec->next = NULL;
292 }
293 }
294 project2->rec = NULL;
295 project_kill(project2);
296 }
297 }
298
299 long npart(0), nsel(0);
300 double percentage(0);
301 npart += project_count_mg_particles(project);
302 nsel += project_count_mg_part_selected(project);
303 npart += project_count_rec_particles(project);
304 nsel += project_count_rec_part_selected(project);
305
306 if ( npart ) percentage = nsel*100.0/npart;
307
308 if ( verbose ) {
309 cout << "Micrographs updated: " << nmg << endl;
310 cout << "Reconstructions updated: " << nrec << endl;
311 cout << "Number of particles selected: " << nsel << " (" << percentage << " %)" << endl << endl;
312 }
313
314 return nsel;
315 }
316
335 long project_multi_select_best_FOM(Bproject* project_list, double fom_cut, int fom_index, int fom_def_flag)
336 {
337 long np = count_list((char *)project_list);
338
339 if ( np < 1 ) {
340 cout << "Selecting best FOM: Too few parameter files provided!" << endl;
341 return -1;
342 }
343
344 if ( verbose ) {
345 cout << "Selecting from " << np << " data sets:" << endl;
346 cout << "FOM cutoff: " << fom_cut << endl;
347 cout << "FOM index: " << fom_index << endl;
348 }
349
350 long i, f, nsel(0), npart(0);
351 double intercept, intercept_avg(0), slope, slope_avg(0), cutoff = fom_cut;
352
353 Bproject* project = project_list;
354 Bfield** field = NULL;
355 Bmicrograph** mg = NULL;
356 Breconstruction** rec = NULL;
357 Bparticle** part = NULL;
358
359 if ( project->select < 1 ) {
360 npart = project_check_same_number_particles(project_list);
361 field = new Bfield*[np];
362 mg = new Bmicrograph*[np];
363 part = new Bparticle*[np];
364 for ( i=0, project=project_list; i<np; i++, project=project->next ) {
365 field[i] = project->field;
366 if ( fom_def_flag ) {
367 part_fom_defocus_fit(project, fom_index, intercept, slope);
368 intercept_avg += intercept;
369 slope_avg += slope;
370 }
371 }
372 if ( fom_def_flag ) {
373 intercept_avg /= np;
374 slope_avg /= np;
375 if ( verbose )
376 cout << "FOM vs defocus slope: " << slope_avg << endl;
377 }
378 for ( ; field[0]; ) {
379 if ( verbose & VERB_FULL )
380 cout << "Field: " << field[0]->id << endl;
381 for ( i=0; i<np; i++ ) mg[i] = field[i]->mg;
382 for ( ; mg[0]; ) {
383 if ( fom_def_flag && mg[0]->ctf )
384 cutoff = slope_avg*mg[0]->ctf->defocus_average() + fom_cut;
385 for ( i=0; i<np; i++ ) part[i] = mg[i]->part;
386 for ( ; part[0]; ) {
387 if ( part[0]->sel ) {
388 for ( i=1; i<np; i++ ) {
389 if ( part[0]->fom[fom_index] < part[i]->fom[fom_index] ) {
390 part[0]->sel = i + 1;
391 part[0]->view = part[i]->view;
392 part[0]->ori = part[i]->ori;
393 part[0]->mag = part[i]->mag;
394 for ( f=0; f<NFOM; f++ )
395 part[0]->fom[f] = part[i]->fom[f];
396 }
397 }
398 }
399 if ( part[0]->fom[fom_index] < cutoff ) part[0]->sel = 0;
400 if ( part[0]->sel > 0 ) nsel++;
401 for ( i=0; i<np; i++ ) part[i] = part[i]->next;
402 }
403 for ( i=0; i<np; i++ ) mg[i] = mg[i]->next;
404 }
405 for ( i=0; i<np; i++ ) field[i] = field[i]->next;
406 }
407 delete[] field;
408 delete[] mg;
409 delete[] part;
410 } else {
411 npart = project_check_same_number_particles(project_list);
412 rec = new Breconstruction*[np];
413 part = new Bparticle*[np];
414 for ( i=0, project=project_list; i<np; i++, project=project->next ) rec[i] = project->rec;
415 for ( ; rec[0]; ) {
416 if ( verbose & VERB_FULL )
417 cout << "Reconstruction: " << rec[0]->id << endl;
418 for ( i=0; i<np; i++ ) part[i] = rec[i]->part;
419 for ( ; part[0]; ) {
420 if ( part[0]->sel ) {
421 for ( i=1; i<np; i++ ) {
422 if ( part[0]->fom[fom_index] < part[i]->fom[fom_index] ) {
423 part[0]->sel = i + 1;
424 part[0]->view = part[i]->view;
425 part[0]->ori = part[i]->ori;
426 part[0]->mag = part[i]->mag;
427 for ( f=0; f<NFOM; f++ )
428 part[0]->fom[f] = part[i]->fom[f];
429 }
430 }
431 }
432 if ( part[0]->fom[fom_index] < fom_cut ) part[0]->sel = 0;
433 if ( part[0]->sel > 0 ) nsel++;
434 for ( i=0; i<np; i++ ) part[i] = part[i]->next;
435 }
436 for ( i=0; i<np; i++ ) rec[i] = rec[i]->next;
437 }
438 delete[] rec;
439 delete[] part;
440 }
441
442 if ( verbose )
443 cout << "Number of particles selected: " << nsel << " (" << nsel*100.0/npart << " %)" << endl;
444
445
446 return nsel;
447 }
448
457 long project_multi_selection_stats(Bproject* project_list)
458 {
459 long np = count_list((char *)project_list);
460
461 if ( np < 1 ) {
462 cerr << "Error: Selection statistics: Too few parameter files provided!" << endl;
463 return -1;
464 }
465
466 long i, j, m, n, f, nsel(0), npart(0);
467 long comb = 1<<np;
468 int* sel = new int[comb];
469 for ( i=0; i<comb; i++ ) sel[i] = 0;
470
471 Bproject* project = project_list;
472 Bfield** field = NULL;
473 Bmicrograph** mg = NULL;
474 Breconstruction** rec = NULL;
475 Bparticle** part = NULL;
476
477 if ( project->select < 1 ) {
478 npart = project_check_same_number_particles(project_list);
479 field = new Bfield*[np];
480 mg = new Bmicrograph*[np];
481 part = new Bparticle*[np];
482 for ( i=0, project=project_list; i<np; i++, project=project->next ) field[i] = project->field;
483 while ( field[0] ) {
484 for ( i=0; i<np; i++ ) mg[i] = field[i]->mg;
485 while ( mg[0] ) {
486 for ( i=0; i<np; i++ ) part[i] = mg[i]->part;
487 while ( part[0] ) {
488 m = n = 0;
489 for ( i=0; i<np; i++ ) {
490 if ( part[i]->sel ) {
491 m += 1<<i;
492 if ( n == 0 ) n = i + 1;
493 else n = -1;
494 }
495 }
496 sel[m] += 1;
497 part[0]->sel = 0;
498 if ( n == 1 ) part[0]->sel = 1;
499 if ( n > 1 ) {
500 part[0]->sel = n;
501 i = n - 1;
502 part[0]->sel = i + 1;
503 part[0]->view = part[i]->view;
504 part[0]->ori = part[i]->ori;
505 part[0]->mag = part[i]->mag;
506 for ( f=0; f<NFOM; f++ )
507 part[0]->fom[f] = part[i]->fom[f];
508 }
509 if ( n > 0 ) nsel++;
510 for ( i=0; i<np; i++ ) part[i] = part[i]->next;
511 }
512 for ( i=0; i<np; i++ ) mg[i] = mg[i]->next;
513 }
514 for ( i=0; i<np; i++ ) field[i] = field[i]->next;
515 }
516 delete[] field;
517 delete[] mg;
518 delete[] part;
519 } else {
520 npart = project_check_same_number_particles(project_list);
521 rec = new Breconstruction*[np];
522 part = new Bparticle*[np];
523 for ( i=0, project=project_list; i<np; i++, project=project->next ) rec[i] = project->rec;
524 while ( rec[0] ) {
525 for ( i=0; i<np; i++ ) part[i] = rec[i]->part;
526 while ( part[0] ) {
527 m = n = 0;
528 for ( i=0; i<np; i++ ) {
529 if ( part[i]->sel ) {
530 m += 1<<i;
531 if ( n == 0 ) n = i + 1;
532 else n = -1;
533 }
534 }
535 sel[m] += 1;
536 part[0]->sel = 0;
537 if ( n == 1 ) part[0]->sel = 1;
538 if ( n > 1 ) {
539 part[0]->sel = n;
540 i = n - 1;
541 part[0]->sel = i + 1;
542 part[0]->view = part[i]->view;
543 part[0]->ori = part[i]->ori;
544 part[0]->mag = part[i]->mag;
545 for ( f=0; f<NFOM; f++ )
546 part[0]->fom[f] = part[i]->fom[f];
547 }
548 if ( n > 0 ) nsel++;
549 for ( i=0; i<np; i++ ) part[i] = part[i]->next;
550 }
551 for ( i=0; i<np; i++ ) rec[i] = rec[i]->next;
552 }
553 delete[] rec;
554 delete[] part;
555 }
556
557 // if ( verbose & VERB_PROCESS ) {
558 if ( verbose ) {
559 cout << "Selection distribution:" << endl;
560 for ( i=0; i<comb; i++ ) {
561 for ( j=0; j<np; j++ ) {
562 m = (i>>j) & 1;
563 cout << m;
564 }
565 cout << tab << sel[i] << endl;
566 }
567 cout << "Number of particles selected with no overlap: " << nsel << " (" << nsel*100.0/npart << " %)" << endl;
568 }
569
570 delete[] sel;
571
572 return nsel;
573 }
574
595 long project_multi_select_low_variance(Bproject* project_list, Bsymmetry* sym,
596 double origin_dev, double view_dev, double angle_dev, double mag_dev)
597 {
598 long np = count_list((char *)project_list);
599
600 if ( np < 2 ) {
601 cout << "Selecting lowest variance: Too few parameter files provided!" << endl;
602 return -1;
603 }
604
605 if ( verbose )
606 cout << "Selecting based on the lowest variance for symmetry " << sym->label << endl;
607
608 int i, j, h, f, nsel(0), psel, flag3D(0);
609 double var, ox_sum, oy_sum, oz_sum, ox_sqsum, oy_sqsum, oz_sqsum, origin_std, view_std, angle_std;
610 View vsum, vsqsum;
611 double size_sum, size_sqsum, size_std, defsum, defsqsum, def_std;
612 double def_avg_dev(0), origin_avg_dev(0), view_avg_dev(0), angle_avg_dev(0), size_avg_dev(0);
613
614 int hist_ang[181], hist_view[181], hist_ori[101];
615 for ( i=0; i<181; i++ ) hist_ang[i] = hist_view[i] = 0;
616 for ( i=0; i<101; i++ ) hist_ori[i] = 0;
617
618 long nmg = project_count_micrographs(project_list);
619 long npart = project_count_mg_particles(project_list);
620
621 Bproject* project;
622 Bfield** field = new Bfield*[np];
623 Bmicrograph** mg = new Bmicrograph*[np];
624 Bparticle** part = new Bparticle*[np];
625 // View* view, *v;
626 View theview;
627 // double da, da_min;
628
629 for ( i=0, project=project_list; i<np; i++, project=project->next ) field[i] = project->field;
630 while ( field[0] ) {
631 if ( verbose & VERB_FULL )
632 cout << field[0]->id << endl;
633 for ( i=0; i<np; i++ ) mg[i] = field[i]->mg;
634 while ( mg[0] ) {
635 if ( verbose & VERB_FULL )
636 cout << mg[0]->id << endl;
637 defsum = defsqsum = def_std = 0;
638 if ( mg[0]->ctf ) for ( i=0; i<np; i++ ) {
639 defsum += mg[i]->ctf->defocus_average();
640 defsqsum += mg[i]->ctf->defocus_average()*mg[i]->ctf->defocus_average();
641 }
642 var = defsqsum - defsum*defsum/np;
643 if ( var > 0 ) def_std = sqrt(var/(np-1));
644 def_avg_dev += def_std;
645 for ( i=0; i<np; i++ ) part[i] = mg[i]->part;
646 while ( part[0] ) {
647 for ( i=psel=0; i<np; i++ ) if ( part[i]->sel ) psel++;
648 if ( psel ) {
649 ox_sum = oy_sum = oz_sum = ox_sqsum = oy_sqsum = oz_sqsum = 0;
650 for ( i=0; i<4; i++ ) vsum[i] = vsqsum[i] = 0;
651 size_sum = size_sqsum = 0;
652 if ( verbose & VERB_FULL )
653 cout << part[0]->id << tab << part[0]->ori << endl;
654 for ( i=0; i<np; i++ ) {
655 theview = find_closest_symmetric_view(sym, part[0]->view, part[i]->view);
656 ox_sum += part[i]->ori[0];
657 ox_sqsum += part[i]->ori[0]*part[i]->ori[0];
658 oy_sum += part[i]->ori[1];
659 oy_sqsum += part[i]->ori[1]*part[i]->ori[1];
660 if ( flag3D ) {
661 oz_sum += part[i]->ori[2];
662 oz_sqsum += part[i]->ori[2]*part[i]->ori[2];
663 }
664 if ( theview[3] - part[0]->view[3] < -M_PI ) theview[3] += TWOPI;
665 if ( theview[3] - part[0]->view[3] > M_PI ) theview[3] -= TWOPI;
666 for ( j=0; j<4; j++ ) vsum[j] += theview[j];
667 for ( j=0; j<4; j++ ) vsqsum[j] += theview[j]*theview[j];
668 size_sum += part[i]->mag;
669 size_sqsum += part[i]->mag*part[i]->mag;
670 }
671 if ( verbose & VERB_FULL ) cout << endl;
672 origin_std = view_std = angle_std = size_std = 0;
673 var = ox_sqsum + oy_sqsum + oz_sqsum -
674 (ox_sum*ox_sum + oy_sum*oy_sum + oz_sum*oz_sum)/np;
675 if ( var > 0 ) origin_std = sqrt(var/(np-1));
676 if ( origin_std > origin_dev ) part[0]->sel = 0;
677 h = (int) (origin_std*10 + 0.5);
678 if ( h > 100 ) h = 100;
679 hist_ori[h]++;
680 var = vsqsum[0] + vsqsum[1] + vsqsum[2] -
681 (vsum[0]*vsum[0] + vsum[1]*vsum[1] + vsum[2]*vsum[2])/np;
682 if ( var > 0 ) view_std = sqrt(var/(np-1));
683 if ( view_std > view_dev ) part[0]->sel = 0;
684 h = (int) (view_std*180.0/M_PI + 0.5);
685 if ( h > 180 ) h = 180;
686 hist_view[h]++;
687 var = vsqsum.angle() - vsum.angle()*vsum.angle()/np;
688 if ( var > 0 ) angle_std = sqrt(var/(np-1));
689 if ( angle_std > angle_dev ) part[0]->sel = 0;
690 h = (int) (angle_std*180.0/M_PI + 0.5);
691 if ( h > 180 ) h = 180;
692 hist_ang[h]++;
693 var = size_sqsum - size_sum*size_sum/np;
694 if ( var > 0 ) size_std = sqrt(var/(np-1));
695 if ( size_std > mag_dev ) part[0]->sel = 0;
696 for ( i=1; i<np; i++ ) part[i]->sel = part[0]->sel;
697 if ( part[0]->sel > 0 ) {
698 nsel++;
699 origin_avg_dev += origin_std;
700 view_avg_dev += view_std;
701 angle_avg_dev += angle_std;
702 size_avg_dev += size_std;
703 }
704 for ( i=1; i<np; i++ ) if ( part[0]->fom[0] < part[i]->fom[0] )
705 for ( f=0; f<NFOM; f++ ) part[0]->fom[f] = part[i]->fom[f];
706 }
707 for ( i=0; i<np; i++ ) part[i] = part[i]->next;
708 }
709 for ( i=0; i<np; i++ ) mg[i] = mg[i]->next;
710 }
711 for ( i=0; i<np; i++ ) field[i] = field[i]->next;
712 }
713
714 def_avg_dev /= nmg;
715 if ( nsel ) {
716 origin_avg_dev /= nsel;
717 view_avg_dev /= nsel;
718 angle_avg_dev /= nsel;
719 size_avg_dev /= nsel;
720 }
721
722 if ( verbose & VERB_RESULT ) {
723 cout << "Average defocus deviation: " << def_avg_dev << " A" << endl;
724 cout << "Number of particles selected with low variance: " << nsel << " (" << nsel*100.0/npart << " %)" << endl;
725 cout << "Average origin deviation: " << origin_avg_dev << " pixels" << endl;
726 cout << "Average view deviation: " << view_avg_dev*180.0/M_PI << " degrees" << endl;
727 cout << "Average angle deviation: " << angle_avg_dev*180.0/M_PI << " degrees" << endl;
728 cout << "Average magnification deviation:" << size_avg_dev << endl << endl;
729 }
730
731 if ( verbose & VERB_PROCESS ) {
732 cout << "Origin deviation histogram:" << endl;
733 cout << "Dist\tCount" << endl;
734 for ( h=0; h<101; h++ ) cout << h*0.1 << tab << hist_ori[h] << endl;
735 cout << endl;
736
737 cout << "View and in-plane rotation angle deviation histograms:" << endl;
738 cout << "Angle\tView\tAngle" << endl;
739 for ( h=0; h<181; h++ ) cout << h << tab << hist_view[h] << tab << hist_ang[h] << endl;
740 cout << endl;
741 }
742
743 delete[] field;
744 delete[] mg;
745 delete[] part;
746
747 return nsel;
748 }
749
766 long project_multi_select_low_difference(Bproject* project1, Bproject* project2, Bsymmetry* sym,
767 double origin_err, double view_err, double angle_err, double mag_err)
768 {
769 long i(0), nmgsel(0), npart(0), nsel(0), ndsel(0);
770 double dori, dview, dang, dsize;
771 double ddef, def_avg_err(0), pdef_avg_err(0);
772 double origin_avg_err(0), view_avg_err(0), angle_avg_err(0), size_avg_err(0);
773
774 if ( verbose )
775 cout << "Selecting based on the lowest difference for symmetry " << sym->label << endl;
776
777 int h, hist_ang[361], hist_view[361], hist_ori[101];
778 for ( h=0; h<361; h++ ) hist_ang[h] = hist_view[h] = 0;
779 for ( h=0; h<101; h++ ) hist_ori[h] = 0;
780
781 Bfield *field1, *field2;
782 Bmicrograph *mg1, *mg2;
783 // Breconstruction *rec1, *rec2;
784 Bparticle *part1, *part2;
785 Bparticle** partarr1;
786 Bparticle** partarr2;
787 View view1, view2;
788
789 if ( project1->select < 1 ) {
790 // nmg = project_count_micrographs(project1);
791 partarr1 = project_mg_particle_array(project1, -1, npart);
792 partarr2 = project_mg_particle_array(project2, -1, npart);
793 for ( field1=project1->field, field2=project2->field; field1 && field2; field1=field1->next, field2=field2->next ) {
794 if ( verbose & VERB_FULL )
795 cout << field1->id << tab << field2->id << endl;
796 for ( mg1=field1->mg, mg2=field2->mg; mg1 && mg2; mg1=mg1->next, mg2=mg2->next ) {
797 if ( verbose & VERB_FULL )
798 cout << tab << mg1->id << tab << mg2->id << endl;
799 if ( mg1->ctf && mg2->ctf ) {
800 if ( verbose & VERB_DEBUG )
801 cout << "DBEUG project_multi_select_low_difference: Calculating defocus difference" << endl;
802 ddef = fabs(mg1->ctf->defocus_average() - mg2->ctf->defocus_average());
803 def_avg_err += ddef;
804 nmgsel++;
805 }
806 }
807 }
808 } else {
809 partarr1 = project_rec_particle_array(project1, -1, npart);
810 partarr2 = project_rec_particle_array(project2, -1, npart);
811 }
812
813 if ( verbose )
814 cout << "Number of particles: " << npart << endl;
815
816 for ( i=0; i<npart; i++ ) {
817 part1 = partarr1[i];
818 part2 = partarr2[i];
819 if ( verbose & VERB_FULL )
820 cout << "particle " << i << endl;
821 // cout << part1 << tab << part2 << endl;
822 if ( part1->sel && part2->sel ) {
823 if ( part1->def > 1 && part2->def > 1 ) {
824 ddef = fabs(part1->def - part2->def);
825 pdef_avg_err += ddef;
826 ndsel++;
827 }
828 dori = (part1->ori - part2->ori).length();
829 view1 = part1->view;
830 view2 = find_closest_symmetric_view(sym, view1, part2->view);
831 // cout << part1->id << tab << view1 << tab << view2 << endl;
832 // cout << view1.vector3().scalar(view2.vector3()) << endl;
833 dview = view1.angle(view2);
834 dang = fabs(angle_set_negPI_to_PI(view1.angle() - view2.angle()));
835 // if ( dang > 0.1 )
836 // cout << setprecision(3) << part1->id << tab << view1 << tab << view2 << tab << dang*180.0/M_PI << endl;
837 dsize = fabs(part1->mag - part2->mag);
838 // cout << part1->id << tab << dori << tab << dview << tab << dang << endl;
839 h = (int) (dori*10 + 0.5);
840 if ( h > 100 ) h = 100;
841 hist_ori[h]++;
842 h = (int) (dview*360.0/M_PI + 0.5);
843 if ( h > 360 ) h = 360;
844 hist_view[h]++;
845 h = (int) (dang*360.0/M_PI + 0.5);
846 if ( h > 360 ) h = 360;
847 hist_ang[h]++;
848 if ( dori > origin_err ) part1->sel = part2->sel = 0;
849 if ( dview > view_err ) part1->sel = part2->sel = 0;
850 if ( dang > angle_err ) part1->sel = part2->sel = 0;
851 if ( dsize > mag_err ) part1->sel = part2->sel = 0;
852 if ( part1->sel > 0 && part2->sel > 0 ) {
853 nsel++;
854 origin_avg_err += dori;
855 view_avg_err += dview;
856 angle_avg_err += dang;
857 }
858 }
859 }
860
861 delete[] partarr1;
862 delete[] partarr2;
863
864 if ( nmgsel ) def_avg_err /= nmgsel;
865 if ( ndsel ) pdef_avg_err /= ndsel;
866 if ( nsel ) {
867 origin_avg_err /= nsel;
868 view_avg_err /= nsel;
869 angle_avg_err /= nsel;
870 size_avg_err /= nsel;
871 }
872
873 if ( verbose & VERB_RESULT ) {
874 if ( nmgsel )
875 cout << "Average defocus error: " << def_avg_err << " A" << endl;
876 if ( ndsel )
877 cout << "Average particle defocus error: " << pdef_avg_err << " A" << endl;
878 cout << "Number of particles selected with low variance: " << nsel << " (" << nsel*100.0/npart << " %)" << endl;
879 cout << "Average origin error: " << origin_avg_err << " pixels" << endl;
880 cout << "Average view error: " << view_avg_err*180.0/M_PI << " degrees" << endl;
881 cout << "Average angle error: " << angle_avg_err*180.0/M_PI << " degrees" << endl;
882 cout << "Average magnification error: " << size_avg_err << endl << endl;
883 }
884
885 if ( verbose & VERB_PROCESS ) {
886 cout << "Origin error histogram:" << endl;
887 cout << "Dist\tCount" << endl;
888 for ( h=0; h<101; h++ ) cout << h*0.1 << tab << hist_ori[h] << endl;
889 cout << endl;
890
891 cout << "View and in-plane rotation angle error histograms:" << endl;
892 cout << "Angle\tView\tAngle" << endl;
893 for ( h=0; h<361; h++ ) cout << h*0.5 << tab << hist_view[h] << tab << hist_ang[h] << endl;
894 cout << endl;
895 }
896
897 return nsel;
898 }
899
916 long project_multi_select_low_rmsd(Bproject* project1, Bproject* project2, Bsymmetry* sym,
917 double origin_rmsd, double view_rmsd, double angle_rmsd, double mag_rmsd)
918 {
919 int nsel(0);
920 double ddef, dori, dview, dang, dsize;
921 double def_avg_rmsd(0), origin_avg_rmsd(0), view_avg_rmsd(0), angle_avg_rmsd(0), size_avg_rmsd(0);
922
923 if ( verbose )
924 cout << "Selecting based on the lowest RMSD for symmetry " << sym->label << endl;
925
926 long i(0), nmg(0), npart(0);
927
928 Bfield *field1, *field2;
929 Bmicrograph *mg1, *mg2;
930 // Breconstruction *rec1, *rec2;
931 Bparticle *part1, *part2;
932 Bparticle** partarr1;
933 Bparticle** partarr2;
934 View view1, view2;
935
936 if ( project1->select < 1 ) {
937 nmg = project_count_micrographs(project1);
938 partarr1 = project_mg_particle_array(project1, -1, npart);
939 partarr2 = project_mg_particle_array(project2, -1, npart);
940 for ( field1=project1->field, field2=project2->field; field1 && field2; field1=field1->next, field2=field2->next ) {
941 if ( verbose & VERB_FULL )
942 cout << field1->id << endl;
943 for ( mg1=field1->mg, mg2=field2->mg; mg1 && mg2; mg1=mg1->next, mg2=mg2->next ) {
944 if ( verbose & VERB_FULL )
945 cout << mg1->id << endl;
946 if ( mg1->ctf && mg2->ctf ) {
947 ddef = mg1->ctf->defocus_average() - mg2->ctf->defocus_average();
948 def_avg_rmsd += ddef*ddef;
949 }
950 }
951 }
952 } else {
953 partarr1 = project_rec_particle_array(project1, -1, npart);
954 partarr2 = project_rec_particle_array(project2, -1, npart);
955 }
956
957 for ( i=0; i<npart; i++ ) {
958 part1 = partarr1[i];
959 part2 = partarr2[i];
960 if ( part1->sel && part2->sel ) {
961 dori = (part1->ori - part2->ori).length();
962 view1 = part1->view;
963 view2 = find_closest_symmetric_view(sym, view1, part2->view);
964 dview = view1.angle(view2);
965 dang = fabs(angle_set_negPI_to_PI(view1.angle() - view2.angle()));
966 dsize = part1->mag - part2->mag;
967 if ( dori > origin_rmsd ) part1->sel = part2->sel = 0;
968 if ( dview > view_rmsd ) part1->sel = part2->sel = 0;
969 if ( dang > angle_rmsd ) part1->sel = part2->sel = 0;
970 if ( dsize > mag_rmsd ) part1->sel = part2->sel = 0;
971 if ( part1->sel > 0 ) {
972 nsel++;
973 origin_avg_rmsd += dori*dori;
974 view_avg_rmsd += dview*dview;
975 angle_avg_rmsd += dang*dang;
976 size_avg_rmsd += dsize*dsize;
977 }
978 }
979 }
980
981 delete[] partarr1;
982 delete[] partarr2;
983
984 if ( nmg ) def_avg_rmsd = sqrt(def_avg_rmsd/nmg);
985
986 if ( nsel ) {
987 origin_avg_rmsd = sqrt(origin_avg_rmsd/nsel);
988 view_avg_rmsd = sqrt(view_avg_rmsd/nsel);
989 angle_avg_rmsd = sqrt(angle_avg_rmsd/nsel);
990 size_avg_rmsd = sqrt(size_avg_rmsd/nsel);
991 }
992
993 if ( verbose & VERB_RESULT ) {
994 cout << "Average defocus RMSD: " << def_avg_rmsd << " A" << endl;
995 cout << "Number of particles selected with low RMSD: " << nsel << " (" << nsel*100.0/npart << " %)" << endl;
996 cout << "Average origin RMSD: " << origin_avg_rmsd << " pixels" << endl;
997 cout << "Average view RMSD: " << view_avg_rmsd*180.0/M_PI << " degrees" << endl;
998 cout << "Average angle RMSD: " << angle_avg_rmsd*180.0/M_PI << " degrees" << endl;
999 cout << "Average magnification RMSD: " << size_avg_rmsd << endl << endl;
1000 }
1001
1002 return nsel;
1003 }
1004
1012 long project_multi_selection_compare(Bproject* project1, Bproject* project2)
1013 {
1014 if ( verbose )
1015 cout << "Comparing the selection for two projects: " << endl;
1016
1017 long i(0), npart(0), npart2(0), nsame(0);
1018
1019 Bparticle *part1, *part2;
1020 Bparticle** partarr1;
1021 Bparticle** partarr2;
1022
1023 if ( project1->select < 1 ) {
1024 partarr1 = project_mg_particle_array(project1, -1, npart);
1025 partarr2 = project_mg_particle_array(project2, -1, npart2);
1026 } else {
1027 partarr1 = project_rec_particle_array(project1, -1, npart);
1028 partarr2 = project_rec_particle_array(project2, -1, npart2);
1029 }
1030
1031 if ( npart2 != npart ) {
1032 cerr << "Error: The number of particles must be the same in both projects:" << endl;
1033 cerr << "\tnumber[1] = " << npart << "\tnumber[2] = " << npart2 << endl;
1034 return 0;
1035 }
1036
1037 for ( i=0; i<npart; i++ ) {
1038 part1 = partarr1[i];
1039 part2 = partarr2[i];
1040 if ( part1->sel == part2->sel ) nsame++;
1041 else if ( verbose )
1042 cout << part1->rec->id << tab << part1->id << tab << part1->sel << tab << part2->sel << endl;
1043 }
1044
1045 delete[] partarr1;
1046 delete[] partarr2;
1047
1048 if ( verbose & VERB_RESULT )
1049 cout << "Selections the same: " << nsame << " (" << nsame*100.0/npart << "%)" << endl << endl;
1050
1051 return nsame;
1052 }
1053
find_closest_symmetric_view
View find_closest_symmetric_view(Bsymmetry *sym, View view_ref, View view)
Finds the closest symmetric match between two views.
Definition: symmetry.cpp:939
reconstruction_update
int reconstruction_update(Breconstruction *rec, Breconstruction *nu_rec, int fom_index, int flags)
Updates an existing reconstruction with new information.
Definition: mg_processing.cpp:1772
part_set_selection
long part_set_selection(Bproject *project, int number)
Selects one selection number and sets all others to zero.
Definition: mg_particle_select.cpp:318
Bparticle::id
int id
Definition: mg_processing.h:92
rwmg.h
Header file for reading and writing micrograph parameters.
project_multi_select_low_variance
long project_multi_select_low_variance(Bproject *project_list, Bsymmetry *sym, double origin_dev, double view_dev, double angle_dev, double mag_dev)
Selects the best particle orientations from multiple parameter files.
Definition: mg_multiple.cpp:595
Bproject::rec
Breconstruction * rec
Definition: mg_processing.h:344
Bparticle::mag
double mag
Definition: mg_processing.h:94
Bparticle::rec
Breconstruction * rec
Definition: mg_processing.h:105
Bparticle::view
View view
Definition: mg_processing.h:101
mg_particle_select.h
Select particles.
Bmicrograph::id
Bstring id
Definition: mg_processing.h:202
project_mg_particle_array
Bparticle ** project_mg_particle_array(Bproject *project, int part_select, long &npart)
Sets up an array of selected particle pointers.
Definition: mg_processing.cpp:2029
utilities.h
Header file for general utilities.
Breconstruction::fom
double fom
Definition: mg_processing.h:302
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Breconstruction
Definition: mg_processing.h:280
angle_set_negPI_to_PI
double angle_set_negPI_to_PI(double angle)
Returns an angle between -PI and PI.
Definition: utilities.cpp:594
mg_img_proc.h
Header file for image processing from micrograph structures.
project_multi_add
long project_multi_add(Bproject *project, Bstring *file_list, int fom_index)
Merges multiple projects with an existing one.
Definition: mg_multiple.cpp:204
project_count_mg_part_selected
long project_count_mg_part_selected(Bproject *project)
Counts the number of particles in a project.
Definition: mg_processing.cpp:833
project_multi_select_low_rmsd
long project_multi_select_low_rmsd(Bproject *project1, Bproject *project2, Bsymmetry *sym, double origin_rmsd, double view_rmsd, double angle_rmsd, double mag_rmsd)
Selects the best particle orientations from two parameter files.
Definition: mg_multiple.cpp:916
read_project
Bproject * read_project(const char *filename, int flags=0)
Reading micrograph parameters from files.
Definition: rwmg.cpp:140
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
Matrix.h
Generalized matrix class.
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
project_count_micrographs
long project_count_micrographs(Bproject *project)
Counts all the micrographs in the project structure.
Definition: mg_processing.cpp:746
project_multi_selection_compare
long project_multi_selection_compare(Bproject *project1, Bproject *project2)
Calculates the number of selections that are the same.
Definition: mg_multiple.cpp:1012
micrograph_update
int micrograph_update(Bmicrograph *mg, Bmicrograph *nu_mg, int fom_index, int flags)
Updates an existing micrograph with new information.
Definition: mg_processing.cpp:1670
Bfield::next
Bfield * next
Definition: mg_processing.h:258
project_count_rec_particles
long project_count_rec_particles(Bproject *project)
Counts all the particles in the project structure.
Definition: mg_processing.cpp:902
project_count_rec_part_selected
long project_count_rec_part_selected(Bproject *project)
Counts all the particles in the project structure.
Definition: mg_processing.cpp:919
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
reconstruction_kill
int reconstruction_kill(Breconstruction *rec)
Frees a whole reconstruction parameter structure.
Definition: mg_processing.cpp:1377
micrograph_kill
int micrograph_kill(Bmicrograph *mg)
Frees a whole micrograph parameter structure.
Definition: mg_processing.cpp:1288
NFOM
#define NFOM
Definition: mg_processing.h:16
Bfield::id
Bstring id
Definition: mg_processing.h:259
Bstring::next
Bstring * next
Definition: Bstring.h:31
project_multi_selection_stats
long project_multi_selection_stats(Bproject *project_list)
Counts particle assignment distributions using selection arrays.
Definition: mg_multiple.cpp:457
Bmicrograph::select
long select
Definition: mg_processing.h:203
Bproject::field
Bfield * field
Definition: mg_processing.h:343
project_multi_select_best_FOM
long project_multi_select_best_FOM(Bproject *project_list, double fom_cut, int fom_index, int fom_def_flag)
Selects the best particle orientations from multiple parameter files.
Definition: mg_multiple.cpp:335
length
double length(vector< T > v)
Definition: Vector3.h:528
Bmicrograph
Definition: mg_processing.h:200
project_rec_particle_array
Bparticle ** project_rec_particle_array(Bproject *project, int part_select, long &npart)
Sets up an array of selected particle pointers.
Definition: mg_processing.cpp:2071
field_kill
int field_kill(Bfield *field)
Frees a whole field-of-view structure.
Definition: mg_processing.cpp:1261
Bparticle::ori
Vector3< double > ori
Definition: mg_processing.h:100
part_set_FOM
long part_set_FOM(Bproject *project, int fom_index, double fom)
Sets the FOM for all particles.
Definition: mg_particle_select.cpp:357
Bproject::next
Bproject * next
Definition: mg_processing.h:334
part_reset_selection
long part_reset_selection(Bproject *project)
Resets selection to all particles.
Definition: mg_particle_select.cpp:131
project_multi_select_low_difference
long project_multi_select_low_difference(Bproject *project1, Bproject *project2, Bsymmetry *sym, double origin_err, double view_err, double angle_err, double mag_err)
Selects the best particle orientations from two parameter files.
Definition: mg_multiple.cpp:766
Bfield::mg
Bmicrograph * mg
Definition: mg_processing.h:264
Bsymmetry::label
Bstring label
Definition: symmetry.h:41
Bsymmetry
Definition: symmetry.h:38
VERB_RESULT
#define VERB_RESULT
Definition: utilities.h:105
part_fom_defocus_fit
double part_fom_defocus_fit(Bproject *project, int fom_index, double &intercept, double &slope)
Fits the particle FOMs as a function of the defocus.
Definition: mg_particle_select.cpp:2558
Bparticle
Definition: mg_processing.h:89
Bstring
Definition: Bstring.h:29
verbose
int verbose
project_kill
int project_kill(Bproject *project)
Frees a whole project.
Definition: mg_processing.cpp:1224
Bproject::select
int select
Definition: mg_processing.h:339
Breconstruction::select
long select
Definition: mg_processing.h:283
View::angle
double angle()
Definition: View.h:95
View
Definition: View.h:26
CTFparam::defocus_average
double defocus_average()
Definition: ctf.h:100
project_multi_merge
Bproject * project_multi_merge(Bstring *file_list, int fom_index, int flags)
Merges multiple projects.
Definition: mg_multiple.cpp:65
Bparticle::def
double def
Definition: mg_processing.h:95
mg_processing.h
Header file for micrograph processing.
VERB_FULL
#define VERB_FULL
Definition: utilities.h:109
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:273
Breconstruction::next
Breconstruction * next
Definition: mg_processing.h:281
TWOPI
#define TWOPI
Definition: utilities.h:68
project_check_same_number_particles
long project_check_same_number_particles(Bproject *project_list)
Definition: mg_multiple.cpp:20
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:21
Bproject
Definition: mg_processing.h:333
Breconstruction::id
Bstring id
Definition: mg_processing.h:282
Bmicrograph::next
Bmicrograph * next
Definition: mg_processing.h:201