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