Bsoft  2.1.0
Bernard's software package
mg_particle_select.cpp
Go to the documentation of this file.
1 
9 #include "mg_processing.h"
10 #include "mg_tomography.h"
11 #include "mg_select.h"
12 #include "mg_particle_select.h"
13 #include "mg_tags.h"
14 #include "symmetry.h"
15 #include "matrix_linear.h"
16 #include "Matrix.h"
17 #include "random_numbers.h"
18 #include "linked_list.h"
19 #include "utilities.h"
20 
21 // Declaration of global variables
22 extern int verbose; // Level of output to the screen
23 
24 int_float* project_fom_order(Bproject* project, long& npart, int fom_index, int defocus_fit)
25 {
26  if ( fom_index < 0 ) fom_index = 0;
27  if ( fom_index >= NFOM ) fom_index = NFOM - 1;
28 
29  long nn(0);
30 
31  Bfield* field;
32  Bmicrograph* mg;
33  Breconstruction* rec;
34  Bparticle* part;
35 
36  if ( project->select < 1 ) npart = project_count_mg_particles(project);
38 
39  if ( npart < 1 ) {
40  cerr << "Error: No particles found!" << endl;
41  return NULL;
42  }
43 
44  double slope, intercept, fom_adjust(0);
45 
46  if ( verbose & VERB_FULL )
47  cout << "Ordering " << npart << " particles based on FOM:" << endl;
48 
49  int_float* rank = new int_float[npart];
50 
51  if ( project->select < 1 ) {
52  if ( defocus_fit ) part_fom_defocus_fit(project, fom_index, intercept, slope);
53  for ( field = project->field; field; field = field->next ) {
54  for ( mg = field->mg; mg; mg = mg->next ) {
55  if ( defocus_fit && mg->ctf ) fom_adjust = slope*(mg->ctf->defocus_average() - 1e4);
56  for ( part = mg->part; part; part = part->next ) {
57  rank[nn].i = nn;
58  if ( part->sel > 0 )
59 // rank[nn].f = fom_adjust + part->fom[fom_index];
60  rank[nn].f = part->fom[fom_index] - fom_adjust;
61  else
62  rank[nn].f = 0;
63  nn++;
64  }
65  }
66  }
67  } else {
68  for ( rec = project->rec; rec; rec = rec->next ) {
69  for ( part = rec->part; part; part = part->next ) {
70  rank[nn].i = nn;
71  if ( part->sel > 0 )
72  rank[nn].f = part->fom[fom_index];
73  else
74  rank[nn].f = 0;
75  nn++;
76  }
77  }
78  }
79 
80  if ( verbose & VERB_DEBUG )
81  cout << "DEBUG project_fom_order: Ranking the FOMs:" << endl;
82 
83  // Rank according to the FOMs
84  qsort((void *) rank, npart, sizeof(int_float),
85  (int (*)(const void *, const void *)) QsortLargeToSmallIntFloat);
86 
87  return rank;
88 }
89 
90 int_float* part_fom_order(Bparticle* partlist, long& npart, int fom_index)
91 {
92  if ( fom_index < 0 ) fom_index = 0;
93  if ( fom_index >= NFOM ) fom_index = NFOM - 1;
94 
95  long nn(0);
96 
97  Bparticle* part;
98 
99  npart = particle_count(partlist);
100 
101  if ( npart < 1 ) {
102  cerr << "Error: No particles found!" << endl;
103  return NULL;
104  }
105 
106  if ( verbose & VERB_FULL )
107  cout << "Ordering " << npart << " particles based on FOM:" << endl;
108 
109  int_float* rank = new int_float[npart];
110 
111  for ( part = partlist; part; part = part->next ) if ( part->sel ) {
112  rank[nn].i = nn;
113  rank[nn].f = part->fom[fom_index];
114  nn++;
115  }
116 
117  if ( verbose & VERB_DEBUG )
118  cout << "DEBUG part_fom_order: Ranking the FOMs:" << endl;
119 
120  // Rank according to the FOMs
121  qsort((void *) rank, npart, sizeof(int_float),
122  (int (*)(const void *, const void *)) QsortLargeToSmallIntFloat);
123 
124  return rank;
125 }
126 
134 {
135  long npart(0);
136  Bfield* field;
137  Bmicrograph* mg;
138  Breconstruction* rec;
139  Bparticle* part;
140 
141  if ( project->select ) {
142  if ( verbose & VERB_PROCESS )
143  cout << "Resetting reconstruction particle selections" << endl << endl;
144  for ( rec = project->rec; rec; rec = rec->next ) {
145  if ( flag & 1 ) rec->select = 1;
146  if ( flag & 2 ) for ( part = rec->part; part; part = part->next, npart++ )
147  part->sel = 1;
148  }
149  } else {
150  if ( verbose & VERB_PROCESS )
151  cout << "Resetting micrograph particle selections" << endl << endl;
152  for ( field = project->field; field; field = field->next )
153  for ( mg = field->mg; mg; mg = mg->next ) {
154  if ( flag & 1 ) mg->select = 1;
155  if ( flag & 2 ) for ( part = mg->part; part; part = part->next, npart++ )
156  part->sel = 1;
157  }
158  }
159 
160  return npart;
161 }
162 
169 {
170  long npart(0);
171  Bfield* field;
172  Bmicrograph* mg;
173  Breconstruction* rec;
174  Bparticle* part;
175 
176  if ( project->select ) {
177  if ( verbose & VERB_PROCESS )
178  cout << "Unsetting reconstruction particle selections" << endl << endl;
179  for ( rec = project->rec; rec; rec = rec->next ) {
180 // rec->select = 1;
181  for ( part = rec->part; part; part = part->next, npart++ )
182  part->sel = 0;
183  }
184  } else {
185  if ( verbose & VERB_PROCESS )
186  cout << "Unsetting micrograph particle selections" << endl << endl;
187  for ( field = project->field; field; field = field->next )
188  for ( mg = field->mg; mg; mg = mg->next ) {
189 // mg->select = 1;
190  for ( part = mg->part; part; part = part->next, npart++ )
191  part->sel = 0;
192  }
193  }
194 
195  return npart;
196 }
197 
198 
205 {
206  long npart(0);
207  Bfield* field;
208  Bmicrograph* mg;
209  Breconstruction* rec;
210  Bparticle* part;
211 
212  if ( project->select ) {
213  if ( verbose & VERB_PROCESS )
214  cout << "Inverts reconstruction particle selections" << endl << endl;
215  for ( rec = project->rec; rec; rec = rec->next ) {
216 // rec->select = 1;
217  for ( part = rec->part; part; part = part->next, npart++ ) {
218  if ( part->sel ) part->sel = 0;
219  else part->sel = 1;
220  }
221  }
222  } else {
223  if ( verbose & VERB_PROCESS )
224  cout << "Inverts micrograph particle selections" << endl << endl;
225  for ( field = project->field; field; field = field->next )
226  for ( mg = field->mg; mg; mg = mg->next ) {
227 // mg->select = 1;
228  for ( part = mg->part; part; part = part->next, npart++ ) {
229  if ( part->sel ) part->sel = 0;
230  else part->sel = 1;
231  }
232  }
233  }
234 
235  return npart;
236 }
237 
238 
248 {
249  long i, nsel(0);
250 
251  Bfield* field;
252  Bmicrograph* mg;
253  Bparticle* part;
254 
256 
257  vector<int> numsel = select_numbers(mgselect, nmg+1);
258 
259  for ( i=1; i<=nmg; ++i ) if ( numsel[i] ) nsel++;
260 
261  if ( verbose )
262  cout << "Micrographs selected: " << nsel << " (" << nsel*100.0/nmg << " %)" << endl;
263 
264  for ( i=1, nsel=0, field = project->field; field; field = field->next ) {
265  for ( mg = field->mg; mg; mg = mg->next, ++i ) {
266  if ( numsel[i] ) {
267  mg->select = 1;
268  for ( part = mg->part; part; part = part->next )
269  if ( part->sel ) nsel++;
270  } else mg->select = 0;
271  }
272  }
273 
274  return nsel;
275 }
276 
284 {
285  long nsel(0);
286  Bfield* field;
287  Bmicrograph* mg;
288  Breconstruction* rec;
289  Bparticle* part;
290 
291  if ( project->select ) {
292  for ( rec = project->rec; rec; rec = rec->next )
293  for ( part = rec->part; part; part = part->next )
294  if ( part->sel > 0 ) {
295  part->sel = number;
296  nsel++;
297  }
298  } else {
299  for ( field = project->field; field; field = field->next )
300  for ( mg = field->mg; mg; mg = mg->next )
301  for ( part = mg->part; part; part = part->next )
302  if ( part->sel > 0 ) {
303  part->sel = number;
304  nsel++;
305  }
306  }
307 
308  return nsel;
309 }
310 
318 {
319  long nsel(0);
320  Bfield* field;
321  Bmicrograph* mg;
322  Breconstruction* rec;
323  Bparticle* part;
324 
325  if ( project->select ) {
326  for ( rec = project->rec; rec; rec = rec->next )
327  for ( part = rec->part; part; part = part->next )
328  if ( part->sel == number ) {
329  nsel++;
330  } else {
331  part->sel = 0;
332  }
333  } else {
334  for ( field = project->field; field; field = field->next )
335  for ( mg = field->mg; mg; mg = mg->next )
336  for ( part = mg->part; part; part = part->next )
337  if ( part->sel == number ) {
338  nsel++;
339  } else {
340  part->sel = 0;
341  }
342  }
343 
344  return nsel;
345 }
346 
353 {
354  long cnt(0);
355  Bfield* field;
356  Bmicrograph* mg;
357  Breconstruction* rec;
358  Bparticle* part;
359 
360  if ( project->select ) {
361  for ( rec = project->rec; rec; rec = rec->next )
362  for ( part = rec->part; part; part = part->next )
363  if ( part->sel ) part->sel = ++cnt;
364  } else {
365  for ( field = project->field; field; field = field->next )
366  for ( mg = field->mg; mg; mg = mg->next )
367  for ( part = mg->part; part; part = part->next )
368  if ( part->sel ) part->sel = ++cnt;
369  }
370 
371  return cnt;
372 }
373 
374 
382 long part_set_FOM(Bproject* project, int fom_index, double fom)
383 {
384  if ( fom_index < 0 ) fom_index = 0;
385  if ( fom_index >= NFOM ) fom_index = NFOM - 1;
386 
387  long npart(0);
388  Bfield* field;
389  Bmicrograph* mg;
390  Breconstruction* rec;
391  Bparticle* part;
392 
393  if ( project->select )
394  for ( rec = project->rec; rec; rec = rec->next )
395  for ( part = rec->part; part; part = part->next, npart++ )
396  part->fom[fom_index] = fom;
397  else
398  for ( field = project->field; field; field = field->next )
399  for ( mg = field->mg; mg; mg = mg->next )
400  for ( part = mg->part; part; part = part->next, npart++ )
401  part->fom[fom_index] = fom;
402 
403  return npart;
404 }
405 
413 {
415 
416  if ( list.length() < 1 ) return nsel;
417 
418  long smax = project_maximum_selection(project);
419 
420  Bfield* field;
421  Bmicrograph* mg;
422  Breconstruction* rec;
423  Bparticle* part;
424 
425  long n(smax+1);
426 // int* numsel = new int[n];
427 // select_numbers(list, n, numsel);
428  vector<int> numsel = select_numbers(list, n);
429 
430  nsel = 0;
431 
432  if ( project->select ) {
433  for ( rec = project->rec; rec; rec = rec->next )
434  for ( part = rec->part; part; part = part->next ) {
435  if ( part->sel > 0 ) {
436  if ( numsel[part->sel] ) part->sel = 0;
437  else nsel++;
438  }
439  }
440  } else {
441  for ( field = project->field; field; field = field->next )
442  for ( mg = field->mg; mg; mg = mg->next )
443  for ( part = mg->part; part; part = part->next ) {
444  if ( part->sel > 0 ) {
445  if ( numsel[part->sel-1] ) part->sel = 0;
446  else nsel++;
447  }
448  }
449  }
450 
451 // delete[] numsel;
452 
453  if ( verbose & VERB_PROCESS )
454  cout << "Particles selected: " << nsel << endl << endl;
455 
456  return nsel;
457 }
458 
459 
468 long part_deselect(Bproject* project, int fom_index, double fommin, double fommax)
469 {
470  if ( fom_index < 0 ) fom_index = 0;
471  if ( fom_index >= NFOM ) fom_index = NFOM - 1;
472 
473  long nsel(0);
474  Bfield* field;
475  Bmicrograph* mg;
476  Breconstruction* rec;
477  Bparticle* part;
478 
479  if ( project->select ) {
480  for ( rec = project->rec; rec; rec = rec->next )
481  for ( part = rec->part; part; part = part->next ) {
482  if ( part->fom[fom_index] < fommin || part->fom[fom_index] > fommax )
483  part->sel = 0;
484  if ( part->sel ) nsel++;
485  }
486  } else {
487  for ( field = project->field; field; field = field->next )
488  for ( mg = field->mg; mg; mg = mg->next )
489  for ( part = mg->part; part; part = part->next ) {
490  if ( part->fom[fom_index] < fommin || part->fom[fom_index] > fommax )
491  part->sel = 0;
492  if ( part->sel ) nsel++;
493  }
494  }
495 
496  if ( verbose & VERB_PROCESS ) {
497  cout << "FOM range: " << fommin << " - " << fommax << endl;
498  cout << "Particles selected: " << nsel << endl << endl;
499  }
500 
501  return nsel;
502 }
503 
504 int QsortLargeToSmallParticle(const void *x, const void *y)
505 {
506  Bparticle** part1 = (Bparticle**) x;
507  Bparticle** part2 = (Bparticle**) y;
508  if( (*part1)->fom[0] > (*part2)->fom[0] ) return -1;
509  else return 1;
510 }
511 
520 long part_deselect_redundant(Bparticle* partlist, double excl_dist, int part_select, int fom_index)
521 {
522  if ( fom_index < 0 ) fom_index = 0;
523  if ( fom_index >= NFOM ) fom_index = NFOM - 1;
524 
525  long i, j, npart(0), nsel(0);
526 
527  Bparticle** parr = particle_array(partlist, part_select, npart);
528 
529  // Rank according to the FOMs
530  qsort((void *) parr, npart, sizeof(Bparticle*),
531  (int (*)(const void *, const void *)) QsortLargeToSmallParticle);
532 
533  for ( i=0; i<npart; i++ ) {
534  for ( j=i+1; j<npart; j++ ) if ( parr[j]->sel ) {
535  if ( parr[i]->loc.distance(parr[j]->loc) < excl_dist )
536  parr[j]->sel = 0;
537  }
538  if ( parr[i]->sel ) nsel++;
539  }
540 
541  return nsel;
542 }
543 
552 long part_deselect_redundant(Bproject* project, double excl_dist, int part_select, int fom_index)
553 {
554  if ( fom_index < 0 ) fom_index = 0;
555  if ( fom_index >= NFOM ) fom_index = NFOM - 1;
556 
557  long nsel(0);
558  Bfield* field;
559  Bmicrograph* mg;
560  Breconstruction* rec;
561 
562  if ( project->select ) {
563  for ( rec = project->rec; rec; rec = rec->next )
564  nsel += part_deselect_redundant(rec->part, excl_dist, part_select, fom_index);
565  } else {
566  for ( field = project->field; field; field = field->next )
567  for ( mg = field->mg; mg; mg = mg->next )
568  nsel += part_deselect_redundant(mg->part, excl_dist, part_select, fom_index);
569  }
570 
571  if ( verbose & VERB_PROCESS ) {
572  cout << "Exclusion distance: " << excl_dist << endl;
573  cout << "Particles selected: " << nsel << endl << endl;
574  }
575 
576  return nsel;
577 }
578 
591 {
592  long nsel(0);
593  int i = 1;
594  Bfield* field;
595  Bmicrograph* mg;
596  Breconstruction* rec;
597  Bparticle* part;
598 
599  if ( nmaps < 1 ) nmaps = 1;
600 
601  if ( project->select ) {
602  for ( rec = project->rec; rec; rec = rec->next )
603  for ( part = rec->part; part; part = part->next ) {
604  if ( part_select < 0 ) {
605  if ( part->sel > 0 ) part->sel = i;
606  } else {
607  if ( part->sel == part_select ) part->sel = i;
608  else part->sel = 0;
609  }
610  if ( part->sel ) {
611  nsel++;
612  i++;
613  if ( i > nmaps ) i = 1;
614  }
615  }
616  } else {
617  for ( field = project->field; field; field = field->next )
618  for ( mg = field->mg; mg; mg = mg->next )
619  for ( part = mg->part; part; part = part->next ) {
620  if ( part_select < 0 ) {
621  if ( part->sel > 0 ) part->sel = i;
622  } else {
623  if ( part->sel == part_select ) part->sel = i;
624  else part->sel = 0;
625  }
626  if ( part->sel ) {
627  nsel++;
628  i++;
629  if ( i > nmaps ) i = 1;
630  }
631  }
632  }
633 
634  if ( verbose & VERB_PROCESS )
635  cout << "Particles selected: " << nsel << endl << endl;
636 
637  return nsel;
638 }
639 
646 {
647  long nsel(0);
648  int prev_group(0);
649  Bfield* field;
650  Bmicrograph* mg;
651  Breconstruction* rec;
652  Bparticle* part;
653 
654  if ( project->select ) {
655  for ( rec = project->rec; rec; rec = rec->next ) {
656  for ( part = rec->part; part; part = part->next ) {
657  if ( part->sel > 0 ) {
658  if ( part->group != prev_group ) {
659  nsel++;
660  prev_group = part->group;
661  }
662  part->sel = nsel;
663  }
664  }
665  prev_group = 0;
666  }
667  } else {
668  for ( field = project->field; field; field = field->next ) {
669  for ( mg = field->mg; mg; mg = mg->next ) {
670  for ( part = mg->part; part; part = part->next ) {
671  if ( part->sel > 0 ) {
672  if ( part->group != prev_group ) {
673  nsel++;
674  prev_group = part->group;
675  }
676  part->sel = nsel;
677  }
678  }
679  prev_group = 0;
680  }
681  }
682  }
683 
684  if ( verbose & VERB_PROCESS )
685  cout << "Maps selected: " << nsel << endl << endl;
686 
687  return nsel;
688 }
689 
690 int part_reselect(Bparticle* part, Bstring& tag, double reselect_min, double reselect_max)
691 {
692  int float_flag(1), idata(0);
693  double fdata;
694  Euler euler;
695 
696  fdata = reselect_min - 1;
697  euler = Euler(part->view);
698  if ( tag == PARTICLE_ID ) {
699  idata = part->id;
700  float_flag = 0;
701  }
702  if ( tag == PARTICLE_X ) fdata = part->loc[0];
703  if ( tag == PARTICLE_Y ) fdata = part->loc[1];
704  if ( tag == PARTICLE_Z ) fdata = part->loc[2];
705  if ( tag == PARTICLE_ORIGIN_X ) fdata = part->ori[0];
706  if ( tag == PARTICLE_ORIGIN_Y ) fdata = part->ori[1];
707  if ( tag == PARTICLE_ORIGIN_Z ) fdata = part->ori[2];
708  if ( tag == PARTICLE_DEFOCUS ) fdata = part->def;
709  if ( tag == PARTICLE_DEF_DEV ) fdata = part->dev;
710  if ( tag == PARTICLE_AST_ANG ) fdata = part->ast;
711  if ( tag == PARTICLE_MAGNIF ) fdata = part->mag;
712  if ( tag == PARTICLE_VIEW_X ) fdata = part->view[0];
713  if ( tag == PARTICLE_VIEW_Y ) fdata = part->view[1];
714  if ( tag == PARTICLE_VIEW_Z ) fdata = part->view[2];
715  if ( tag == PARTICLE_VIEW_ANGLE ) fdata = part->view.angle();
716  if ( tag == PARTICLE_PSI ) fdata = euler.psi()*180/M_PI;
717  if ( tag == PARTICLE_THETA ) fdata = euler.theta()*180/M_PI;
718  if ( tag == PARTICLE_PHI ) fdata = euler.phi()*180/M_PI;
719  if ( tag == PARTICLE_OMEGA ) fdata = -euler.psi()*180/M_PI;
720  if ( tag == PARTICLE_FOM ) fdata = part->fom[0];
721  if ( tag == PARTICLE_FOM_CV ) fdata = part->fom[1];
722  if ( tag == PARTICLE_HANDA_FOM ) fdata = part->fom[1];
723  if ( tag == PARTICLE_HANDB_FOM ) fdata = part->fom[2];
724  if ( tag == PARTICLE_SELECT ) {
725  idata = part->sel;
726  float_flag = 0;
727  }
728 
729  if ( !float_flag ) {
730  if ( idata < reselect_min || idata > reselect_max ) part->sel = 0;
731  } else {
732  if ( fdata < reselect_min || fdata > reselect_max ) part->sel = 0;
733  }
734 
735  return 0;
736 }
737 
749 long part_reselect(Bproject* project, Bstring& tag, double reselect_min, double reselect_max)
750 {
751  long npart(0), nmg(0), nrec(0), npsel(0), nmsel(0), nrsel(0);
752  double fdata, mpct(0), rpct(0), ppct(0);
753  Bfield* field;
754  Bmicrograph* mg;
755  Breconstruction* rec;
756  Bparticle* part;
757 
758  if ( tag[0] == '_' ) tag = tag.erase(0);
759 
760  if ( verbose ) {
761  cout << "Reselecting on tag: " << tag << endl;
762  cout << "Range: " << reselect_min << " - " << reselect_max << endl;
763  }
764 
765  if ( project->select < 1 ) {
766  for ( field = project->field; field; field = field->next ) {
767  for ( mg = field->mg; mg; mg = mg->next ) {
768  fdata = reselect_min - 1;
769  if ( mg->ctf && tag.contains(CTF) ) {
770  if ( tag == CTF_DEF_AVG ) fdata = mg->ctf->defocus_average();
771  if ( tag == CTF_DEF_DEV ) fdata = mg->ctf->defocus_deviation();
772  if ( tag == CTF_AST_ANG ) fdata = mg->ctf->astigmatism_angle();
773  if ( fdata < reselect_min || fdata > reselect_max ) mg->select = 0;
774  }
775  if ( tag.contains(PARTICLE) )
776  for ( part = mg->part; part; part = part->next )
777  part_reselect(part, tag, reselect_min, reselect_max);
778  if ( mg->select ) nmsel++;
779  else for ( part = mg->part; part; part = part->next ) part->sel = 0;
780  for ( part = mg->part; part; part = part->next, npart++ ) if ( part->sel ) npsel++;
781  nmg++;
782  }
783  }
784  } else {
785  for ( rec = project->rec; rec; rec = rec->next ) {
786  fdata = reselect_min - 1;
787  if ( rec->ctf && tag.contains(CTF) ) {
788  if ( tag == CTF_DEF_AVG ) fdata = rec->ctf->defocus_average();
789  if ( tag == CTF_DEF_DEV ) fdata = rec->ctf->defocus_deviation();
790  if ( tag == CTF_AST_ANG ) fdata = rec->ctf->astigmatism_angle();
791  if ( fdata < reselect_min || fdata > reselect_max ) rec->select = 0;
792  }
793  if ( tag.contains(PARTICLE) )
794  for ( part = rec->part; part; part = part->next )
795  part_reselect(part, tag, reselect_min, reselect_max);
796  if ( rec->select ) nrsel++;
797  else for ( part = rec->part; part; part = part->next ) part->sel = 0;
798  for ( part = rec->part; part; part = part->next, npart++ ) if ( part->sel ) npsel++;
799  nrec++;
800  }
801  }
802 
803  if ( npart ) ppct = npsel*100.0/npart;
804  if ( nmg ) mpct = nmsel*100.0/nmg;
805  if ( nrec ) rpct = nrsel*100.0/nrec;
806 
807  if ( verbose & VERB_PROCESS ) {
808  if ( project->select < 1 )
809  cout << "Micrographs reselected: " << nmsel << " (" << mpct << " %)" << endl;
810  else
811  cout << "Reconstructions reselected: " << nrsel << " (" << rpct << " %)" << endl;
812  cout << "Particles reselected: " << npsel << " (" << ppct << " %)" << endl << endl;
813  }
814 
815  return npsel;
816 }
817 
828 double part_series_comparison(Bproject* project, Bsymmetry* sym, double angle_cutoff)
829 {
830  int i, max(0), nsel(0);
831  double angle, min_angle, avg_angle(0);
832  View view_pred;
833  View* symview;
834  Vector3<double> axis(1,0,0);
835  Quaternion qt, q;
836 
837  Bfield* field;
838  Bmicrograph* mg, *mg0;
839  Bparticle* part, *part0;
840 
841  int hist[180];
842  for ( i=0; i<180; i++ ) hist[i] = 0;
843 
844  int nsymviews = 1;
845  for ( i=0; i<sym->n; i++ ) nsymviews *= sym->op[i].order;
846 
847  if ( verbose & VERB_LABEL )
848  cout << "Comparison of particles in micrograph series with " << angle_cutoff*180.0/M_PI << " degrees cutoff" << endl;
849 
850  for ( field = project->field; field; field = field->next ) {
851  mg0 = field->mg;
852  axis[0] = cos(mg0->tilt_axis);
853  axis[1] = sin(mg0->tilt_axis);
854  for ( mg=field->mg->next; mg; mg=mg->next ) {
855 // qt = quaternion_from_angle_and_axis3(mg0->tilt_angle - mg->tilt_angle, axis);
856  qt = Quaternion(axis, mg0->tilt_angle - mg->tilt_angle);
857  for ( part=mg->part, part0=mg0->part; part; part=part->next, part0=part0->next ) {
858  q = part->view.quaternion();
859  q = qt * q;
860  view_pred = View(q);
861  symview = symmetry_get_all_views(sym, part0->view);
862  min_angle = M_PI;
863  for ( i=0; i<nsymviews; i++ ) {
864  angle = view_pred.angle(symview[i]);
865  if ( min_angle > angle ) min_angle = angle;
866  }
867  if ( min_angle > angle_cutoff ) {
868  part0->sel = 0;
869  } else {
870  avg_angle += min_angle;
871  nsel++;
872  }
873  i = (int) (min_angle*180.0/M_PI);
874  hist[i]++;
875  if ( max < i ) max = i;
876  delete[] symview;
877  }
878  }
879  for ( mg=field->mg->next; mg; mg=mg->next )
880  for ( part=mg->part, part0=mg0->part; part; part=part->next, part0=part0->next )
881  part->sel = part0->sel; // Set all micrographs to have the same selection
882  }
883  if ( nsel ) avg_angle /= nsel;
884 
885  if ( verbose & VERB_PROCESS ) {
886  cout << "Angular deviation histogram:\nAngle\tCount" << endl;
887  for ( i=0; i<max; i++ ) cout << i << tab << hist[i] << endl;
888  cout << endl;
889  }
890 
891  if ( verbose & VERB_LABEL ) {
892  cout << "Average angle deviation: " << avg_angle*180/M_PI << endl;
893  cout << "Particle sets selected: " << nsel << endl << endl;
894  }
895 
896  return avg_angle;
897 }
898 
900 {
901  long n(0), nsel(0);
902  Bfield* field;
903  Bmicrograph* mg;
904  Breconstruction* rec;
905  Bparticle* part;
906 
907  if ( project->select < 1 ) {
908  for ( field = project->field; field; field = field->next ) {
909  for ( mg = field->mg; mg; mg = mg->next ) {
910  for ( part = mg->part; part; part = part->next ) {
911  part->sel = sel[n];
912  if ( part->sel > 0 ) nsel++;
913  n++;
914  }
915  }
916  }
917  } else {
918  for ( rec = project->rec; rec; rec = rec->next ) {
919  for ( part = rec->part; part; part = part->next ) {
920  part->sel = sel[n];
921  if ( part->sel > 0 ) nsel++;
922  n++;
923  }
924  }
925  }
926 
927  return nsel;
928 }
929 
945 long part_select_FOM_groups(Bproject* project, int ngroups, int fom_index, int defocus_fit)
946 {
947  long i, n(0), npart(0), nsel(0);
948 
949  if ( verbose & VERB_LABEL )
950  cout << "Selecting " << ngroups << " particle groups based on FOM ranking:" << endl;
951 
952  int_float* rank = project_fom_order(project, npart, fom_index, defocus_fit);
953 
954  if ( npart < 1 ) {
955  cerr << "Error: No particles found!" << endl;
956  return -1;
957  }
958 
961 
962  int* sel = new int[npart];
963 
964  if ( verbose & VERB_DEBUG )
965  cout << "DEBUG part_select_FOM_groups: Writing the selection array:" << endl;
966  // Write the ranking groups into the selection array
967  for ( i=0; i<nsel; i++ ) {
968  n = ngroups - (i*ngroups)/nsel;
969  sel[rank[i].i] = n;
970  }
971  for ( ; i<npart; i++ ) sel[rank[i].i] = 0;
972 
973  // Write the selection array back into the parameter hierarchy
974  nsel = part_convert_selection(project, sel);
975 
976  delete[] sel;
977  delete[] rank;
978 
979  return nsel;
980 }
981 
995 long part_select_percentage(Bproject* project, double percentage, int fom_index, int defocus_fit)
996 {
997  if ( percentage < 0 ) percentage = 0;
998  if ( percentage > 100 ) percentage = 100;
999 
1000  long npart(0), nsel(0), n;
1001 
1002  if ( verbose & VERB_LABEL )
1003  cout << "Selecting " << percentage << "% particles based on FOM[" << fom_index << "] ranking:" << endl;
1004 
1005  int_float* rank = project_fom_order(project, npart, fom_index, defocus_fit);
1006 
1007  if ( npart < 1 ) {
1008  cerr << "Error: No particles found!" << endl;
1009  return -1;
1010  }
1011 
1014 
1015  if ( verbose & VERB_DEBUG )
1016  cout << "DEBUG part_select_percentage: npart=" << npart << " nsel=" << nsel << endl;
1017 
1018  // Get the FOM cutoff
1019  long num_to_select = (long) (nsel*percentage/100.0);
1020  if ( num_to_select < 0 ) num_to_select = 0;
1021  if ( num_to_select >= npart ) num_to_select = npart - 1;
1022 
1023  int* sel = new int[npart];
1024 
1025  if ( verbose & VERB_DEBUG )
1026  cout << "DEBUG part_select_percentage: Writing the selection array:" << endl;
1027  // Write the top number into the selection array
1028  for ( n=0; n<num_to_select; n++ ) sel[rank[n].i] = 1;
1029  for ( ; n<npart; n++ ) sel[rank[n].i] = 0;
1030 
1031  // Write the selection array back into the parameter hierarchy
1032  nsel = part_convert_selection(project, sel);
1033 
1034  delete[] sel;
1035  delete[] rank;
1036 
1037  return nsel;
1038 }
1039 
1052 long part_select_best(Bproject* project, long number, int fom_index, int defocus_fit)
1053 {
1054  if ( number < 1 ) return 0;
1055 
1056  long npart(0), nsel(0), n;
1057 
1060 
1061  if ( number > nsel ) number = nsel;
1062 
1063  if ( verbose & VERB_LABEL )
1064  cout << "Selecting " << number << " particles based on FOM[" << fom_index << "] ranking:" << endl;
1065 
1066  int_float* rank = project_fom_order(project, npart, fom_index, defocus_fit);
1067 
1068  if ( npart < 1 ) {
1069  cerr << "Error: No particles found!" << endl;
1070  return -1;
1071  }
1072 
1073  if ( verbose & VERB_DEBUG )
1074  cout << "DEBUG part_select_best: npart=" << npart << " nsel=" << nsel << endl;
1075 
1076  int* sel = new int[npart];
1077 
1078  if ( verbose & VERB_DEBUG )
1079  cout << "DEBUG part_select_best: Writing the selection array:" << endl;
1080 
1081  // Write the top number into the selection array
1082  for ( n=0; n<number; n++ ) sel[rank[n].i] = 1;
1083  for ( ; n<npart; n++ ) sel[rank[n].i] = 0;
1084 
1085  // Write the selection array back into the parameter hierarchy
1086  nsel = part_convert_selection(project, sel);
1087 
1088  delete[] sel;
1089  delete[] rank;
1090 
1091  return nsel;
1092 }
1093 
1108 long part_select_FOM_avg_std(Bproject* project, double factor, int fom_index)
1109 {
1110  long n(0);
1111  double avg(0), std(0);
1112 
1113  Bfield* field;
1114  Bmicrograph* mg;
1115  Breconstruction* rec;
1116  Bparticle* part;
1117 
1118  if ( verbose & (VERB_LABEL | VERB_PROCESS) )
1119  cout << "Selecting particles based on the FOM average and standard deviation" << endl;
1120 
1121  if ( project->select < 1 ) {
1122  for ( field = project->field; field; field = field->next ) {
1123  for ( mg = field->mg; mg; mg = mg->next ) {
1124  for ( part = mg->part; part; part = part->next ) {
1125  avg += part->fom[fom_index];
1126  std += part->fom[fom_index]*part->fom[fom_index];
1127  n++;
1128  }
1129  }
1130  }
1131  } else {
1132  for ( rec = project->rec; rec; rec = rec->next ) {
1133  for ( part = rec->part; part; part = part->next ) {
1134  avg += part->fom[fom_index];
1135  std += part->fom[fom_index]*part->fom[fom_index];
1136  n++;
1137  }
1138  }
1139  }
1140 
1141  if ( n ) avg /= n;
1142  if ( n > 1 ) std = sqrt(std/n - avg*avg);
1143  else std = 0;
1144 
1145  if ( verbose & VERB_PROCESS )
1146  cout << "Average and standard deviation: " << avg << " " << std << endl << endl;
1147 
1148  long nsel = part_deselect(project, fom_index, avg + factor*std);
1149 
1150  return nsel;
1151 }
1152 
1164 {
1165  random_seed();
1166 
1167  long i, n, nsel;
1168 
1169  if ( project->select < 1 ) {
1171  } else {
1173  }
1174 
1175  if ( number >= nsel ) return nsel;
1176 
1177  long d = (long) (get_rand_max()/nsel);
1178  if ( d < 1 ) d = 1;
1179 
1180  int v = (number < nsel/2)? 1: 0;
1181  int* narr = new int[nsel];
1182  for ( i=0; i<nsel; i++ ) narr[i] = 1-v;
1183 
1184  for ( i=0; i<number; i++ ) {
1185  while ( ( n = random()/d ) >= nsel ) ;
1186  while ( n < nsel && narr[n] == v ) n++;
1187  if ( n >= nsel ) n = nsel - 1;
1188  while ( n >= 0 && narr[n] == v ) n--;
1189  if ( n < 0 ) n = 0;
1190  if ( narr[n] == v )
1191  cerr << "Warning: A random selection has been missed!" << endl;
1192  else
1193  narr[n] = v;
1194  }
1195 
1196  Bfield* field;
1197  Bmicrograph* mg;
1198  Breconstruction* rec;
1199  Bparticle* part;
1200 
1201  if ( verbose & VERB_PROCESS )
1202  cout << "Selecting " << number << " particles randomly" << endl;
1203 
1204  n = nsel = 0;
1205  if ( project->select < 1 ) {
1206  for ( field = project->field; field; field = field->next ) {
1207  for ( mg = field->mg; mg; mg = mg->next ) {
1208  for ( part = mg->part; part; part = part->next ) if ( part->sel ) {
1209  if ( narr[n] < 1 ) part->sel = 0;
1210  if ( part->sel ) nsel++;
1211  n++;
1212  }
1213  }
1214  }
1215  } else {
1216  for ( rec = project->rec; rec; rec = rec->next ) {
1217  for ( part = rec->part; part; part = part->next ) if ( part->sel ) {
1218  if ( narr[n] < 1 ) part->sel = 0;
1219  if ( part->sel ) nsel++;
1220  n++;
1221  }
1222  }
1223  }
1224 
1225  delete[] narr;
1226 
1227  if ( verbose & VERB_PROCESS )
1228  cout << "Particles selected: " << nsel << endl << endl;
1229 
1230  return nsel;
1231 }
1232 
1244 {
1245  random_seed();
1246 
1247  long nsel(0);
1248  long cut = (long) (fraction*get_rand_max());
1249 
1250  Bfield* field;
1251  Bmicrograph* mg;
1252  Breconstruction* rec;
1253  Bparticle* part;
1254 
1255  if ( verbose & VERB_PROCESS )
1256  cout << "Selecting " << fraction*100 << "% of the particles randomly" << endl;
1257 
1258  if ( project->select < 1 ) {
1259  for ( field = project->field; field; field = field->next ) {
1260  for ( mg = field->mg; mg; mg = mg->next ) {
1261  for ( part = mg->part; part; part = part->next ) {
1262  if ( random() > cut ) part->sel = 0;
1263  if ( part->sel ) nsel++;
1264  }
1265  }
1266  }
1267  } else {
1268  for ( rec = project->rec; rec; rec = rec->next ) {
1269  for ( part = rec->part; part; part = part->next ) {
1270  if ( random() > cut ) part->sel = 0;
1271  if ( part->sel ) nsel++;
1272  }
1273  }
1274  }
1275 
1276  if ( verbose & VERB_PROCESS )
1277  cout << "Particles selected: " << nsel << endl << endl;
1278 
1279  return nsel;
1280 }
1281 
1293 {
1294  random_seed();
1295 
1296  long i, max(0), nsel(0);
1297  double irm;
1298  int* sel;
1299 
1300  Bfield* field;
1301  Bmicrograph* mg;
1302  Breconstruction* rec;
1303  Bparticle* part;
1304 
1305  if ( verbose & VERB_PROCESS )
1306  cout << "Picking groups randomly to select at least " << number << " particles" << endl;
1307 
1308  if ( project->select < 1 ) {
1309  for ( field = project->field; field; field = field->next )
1310  for ( mg = field->mg; mg; mg = mg->next )
1311  for ( part = mg->part; part; part = part->next )
1312  if ( max < part->group ) max = part->group;
1313  } else {
1314  for ( rec = project->rec; rec; rec = rec->next )
1315  for ( part = rec->part; part; part = part->next )
1316  if ( max < part->group ) max = part->group;
1317  }
1318 
1319  if ( verbose & VERB_DEBUG )
1320  cout << "DEBUG part_select_random_group: max=" << max << endl;
1321 
1322  max++;
1323  irm = max*1.0L/(get_rand_max() + 1.0L);
1324  sel = new int[max];
1325  for ( i=0; i<max; i++ ) sel[i] = 0;
1326 
1327  if ( project->select < 1 ) {
1328  for ( field = project->field; field; field = field->next )
1329  for ( mg = field->mg; mg; mg = mg->next )
1330  for ( part = mg->part; part; part = part->next )
1331  sel[part->group]--;
1332  } else {
1333  for ( rec = project->rec; rec; rec = rec->next )
1334  for ( part = rec->part; part; part = part->next )
1335  sel[part->group]--;
1336  }
1337 
1338  while ( nsel < number ) {
1339  i = (long) (irm * random());
1340  if ( sel[i] < 0 ) {
1341  sel[i] = -sel[i];
1342  nsel += sel[i];
1343  }
1344  }
1345 
1346  nsel = 0;
1347 
1348  if ( project->select < 1 ) {
1349  for ( field = project->field; field; field = field->next )
1350  for ( mg = field->mg; mg; mg = mg->next )
1351  for ( part = mg->part; part; part = part->next ) {
1352  if ( sel[part->group] <= 0 ) part->sel = 0;
1353  if ( part->sel ) nsel++;
1354  }
1355  } else {
1356  for ( rec = project->rec; rec; rec = rec->next )
1357  for ( part = rec->part; part; part = part->next ) {
1358  if ( sel[part->group] <= 0 ) part->sel = 0;
1359  if ( part->sel ) nsel++;
1360  }
1361  }
1362 
1363  delete[] sel;
1364 
1365  if ( verbose & VERB_PROCESS )
1366  cout << "Particles selected: " << nsel << endl << endl;
1367 
1368  return nsel;
1369 }
1370 
1384 {
1385  random_seed();
1386 
1387  long i, rn, n = project_count_mg_part_selected(project), nsel(0);
1388 
1389  if ( verbose & VERB_PROCESS )
1390  cout << "Selecting " << number << " from " << n << " particles randomly with replacement for bootstrapping" << endl;
1391 
1392  double rnf = n*1.0L/get_rand_max();
1393 
1394  int* sel = new int[n];
1395  for ( i=0; i<n; i++ ) sel[i] = 0;
1396 
1397  for ( i=0; i<number; i++ ) {
1398  rn = n;
1399  while ( rn >= n ) rn = (long) (rnf*random());
1400  sel[rn]++;
1401  }
1402 
1403  Bfield* field;
1404  Bmicrograph* mg;
1405  Bparticle* part;
1406 
1407  for ( i=nsel=0, field = project->field; field; field = field->next ) {
1408  for ( mg = field->mg; mg; mg = mg->next ) {
1409  for ( part = mg->part; part; part = part->next ) {
1410  if ( part->sel ) {
1411  part->sel = sel[i++];
1412  if ( part->sel ) nsel++;
1413  }
1414  }
1415  }
1416  }
1417 
1418  delete[] sel;
1419 
1420  if ( verbose & VERB_PROCESS )
1421  cout << "Particles selected: " << nsel << endl << endl;
1422 
1423  return nsel;
1424 }
1425 
1443  double theta_step, double phi_step, int number)
1444 {
1445  random_seed();
1446 
1447  View* viewlist = asymmetric_unit_views(sym, theta_step, phi_step, 1);
1448 
1449  long nview = count_list((char *) viewlist);
1450  long i, j, k, n0;
1451  double d, dmin;
1452  double rnf = 1.0L/get_rand_max();
1453 
1454  int* np = new int[nview];
1455  int* nr = new int[number];
1456  int* nrn = new int[number];
1457  for ( i=0; i<nview; i++ ) np[i] = 0;
1458  for ( i=0; i<number; i++ ) nr[i] = nrn[i] = 0;
1459 
1460  Bfield* field;
1461  Bmicrograph* mg;
1462  Bparticle* part;
1463  View* view;
1464 
1465  // Assign views and count the number of particles per view
1466  for ( i=0, field = project->field; field; field = field->next ) {
1467  for ( mg = field->mg; mg; mg = mg->next ) {
1468  for ( part = mg->part; part; part = part->next ) {
1469  if ( part->sel > 0 ) {
1470  for ( j=0, dmin=10, view = viewlist; view; view = view->next, j++ ) {
1471  d = view->distance(part->view);
1472  if ( d < dmin ) {
1473  dmin = d;
1474  part->sel = -(j+1); // Negative to not be confused with positive selection later
1475  }
1476  }
1477  np[-part->sel-1]++;
1478  }
1479  }
1480  }
1481  }
1482 
1483  kill_list((char *) viewlist, sizeof(View));
1484 
1485  for ( j=n0=0; j<nview; j++ ) if ( np[j] < 1 ) n0++;
1486 
1487  if ( verbose ) {
1488  cout << "Selecting a maximum of " << number << " particles from each view:" << endl;
1489  cout << "Symmetry: " << sym->label << endl;
1490  cout << "Number of views: " << nview << endl;
1491  cout << "View coverage: " << 100*(1 - n0*1.0/nview) << " %" << endl;
1492  }
1493 
1494  // For each view, select the desired number of particles randomly
1495  for ( j=0; j<nview; j++ ) if ( np[j] ) {
1496  for ( k=0; k<number; k++ ) {
1497  nr[k] = (int) (random()*rnf*np[j]);
1498  nrn[k] = 1;
1499  for ( i=0; i<k; i++ ) if ( nr[i] == nr[k] ) nrn[i]++;
1500  }
1501  if ( verbose & VERB_DEBUG ) {
1502  cout << "DEBUG part_select_random_within_view: " << j+1 << "(" << np[j] << ") ";
1503  for ( k=0; k<number; k++ ) cout << nr[k] << "(" << nrn[k] << ") ";
1504  cout << endl;
1505  }
1506  for ( i=0, field = project->field; field; field = field->next ) {
1507  for ( mg = field->mg; mg; mg = mg->next ) {
1508  for ( part = mg->part; part; part = part->next ) {
1509  if ( part->sel == -(j+1) ) {
1510  for ( k=0; k<number && nr[k] != i; k++ ) ;
1511  if ( k >= number ) part->sel = 0;
1512  else part->sel = nrn[k]; // Selection is set to be used with bootstrapping reconstruction
1513  i++;
1514  }
1515  }
1516  }
1517  }
1518  }
1519 
1520  delete[] np;
1521  delete[] nr;
1522  delete[] nrn;
1523 
1525 
1526  if ( verbose & VERB_PROCESS )
1527  cout << "Particles selected: " << nsel << endl << endl;
1528 
1529  return nsel;
1530 }
1531 
1547  double theta_step, double phi_step, double threshfrac, double sigma, int fom_index)
1548 {
1549  View* viewlist = asymmetric_unit_views(sym, theta_step, phi_step, 1);
1550 
1551  long nview = count_list((char *) viewlist);
1552  long i, j, n0;
1553  double isig2(-0.5/(sigma*sigma)), d, dmin, f;
1554 
1555  long* np = new long[nview];
1556  double* fommax = new double[nview];
1557  for ( i=0; i<nview; i++ ) fommax[i] = np[i] = 0;
1558 
1559  Bfield* field;
1560  Bmicrograph* mg;
1561  Bparticle* part;
1562  View *view, *view2, symview;
1563 
1564  // Assign views and count the number of particles per view
1565  for ( field = project->field; field; field = field->next ) {
1566  for ( mg = field->mg; mg; mg = mg->next ) {
1567  for ( part = mg->part; part; part = part->next ) {
1568  if ( part->sel > 0 ) {
1569  for ( j=0, dmin=10, view = viewlist; view; view = view->next, j++ ) {
1570  d = view->angle(part->view);
1571  if ( d < dmin ) {
1572  dmin = d;
1573  part->sel = j+1;
1574  }
1575  }
1576  np[part->sel-1]++;
1577  if ( fommax[part->sel-1] < part->fom[fom_index] )
1578  fommax[part->sel-1] = part->fom[fom_index];
1579  }
1580  }
1581  }
1582  }
1583 
1584  for ( i=n0=0; i<nview; i++ ) if ( np[i] < 1 ) n0++;
1585 
1586  if ( verbose ) {
1587  cout << "Selecting based on smoothed maxima surface:" << endl;
1588  cout << "Symmetry: " << sym->label << endl;
1589  cout << "Number of views: " << nview << endl;
1590  cout << "View coverage: " << 100*(1 - n0*1.0/nview) << " %" << endl;
1591  cout << "Threshold fraction: " << threshfrac << endl;
1592  cout << "Smoothing sigma: " << sigma*180.0/M_PI << " degrees" << endl << endl;
1593  }
1594 
1595  if ( verbose & VERB_FULL ) {
1596  cout << "Maxima:" << endl << "#\tViewX\tViewY\tCount\tFOMmax" << endl;
1597  for ( i=0, view = viewlist; view; view = view->next, i++ )
1598  cout << i+1 << tab << view->x() << tab << view->y() << tab << np[i] << tab << fommax[i] << endl;
1599  }
1600 
1601  // Smooth maxima surface
1602  for ( i=0, view = viewlist; view; view = view->next, i++ ) {
1603  for ( j=0, view2 = viewlist; view2; view2 = view2->next, j++ )
1604  if ( i != j ) {
1605  symview = find_closest_symmetric_view(sym, *view, *view2);
1606  d = view->angle(symview);
1607  f = exp(d*d*isig2);
1608  if ( fommax[i] > fommax[j] ) {
1609  f *= fommax[i];
1610  if ( fommax[j] < f ) fommax[j] = f;
1611  } else {
1612  f *= fommax[j];
1613  if ( fommax[i] < f ) fommax[i] = f;
1614  }
1615  }
1616  }
1617 
1618  if ( verbose & VERB_FULL ) {
1619  cout << "Smoothed maxima:" << endl << "#\tViewX\tViewY\tCount\tFOMmax" << endl;
1620  for ( i=0, view = viewlist; view; view = view->next, i++ )
1621  cout << i+1 << tab << view->x() << tab << view->y() << tab << np[i] << tab << fommax[i] << endl;
1622  }
1623 
1624  kill_list((char *) viewlist, sizeof(View));
1625 
1626  // Set the thresholds
1627  for ( i=0; i<nview; i++ ) fommax[i] *= threshfrac;
1628 
1629  // Select particles based on smoothed maxima surface
1630  for ( field = project->field; field; field = field->next )
1631  for ( mg = field->mg; mg; mg = mg->next )
1632  for ( part = mg->part; part; part = part->next )
1633  if ( part->sel > 0 )
1634  if ( part->fom[fom_index] < fommax[part->sel-1] )
1635  part->sel = 0;
1636 
1638 
1639  if ( verbose & VERB_PROCESS )
1640  cout << "Particles selected: " << nsel << endl << endl;
1641 
1642  return nsel;
1643 }
1644 
1645 
1660  double theta_step, double phi_step, int number, int fom_index)
1661 {
1662  View* viewlist = asymmetric_unit_views(sym, theta_step, phi_step, 1);
1663 
1664  long nview = count_list((char *) viewlist);
1665  long j, k, n, n0, imin;
1666  double d, dmin, fommin;
1667 
1668  int* np = new int[nview];
1669  Bparticle** bestpart = new Bparticle*[number];
1670  for ( j=0; j<nview; j++ ) np[j] = 0;
1671 
1672  Bfield* field;
1673  Bmicrograph* mg;
1674  Bparticle* part;
1675  View* view;
1676 
1677  // Assign views and count the number of particles per view
1678  for ( field = project->field; field; field = field->next ) {
1679  for ( mg = field->mg; mg; mg = mg->next ) {
1680  for ( part = mg->part; part; part = part->next ) {
1681  if ( part->sel > 0 ) {
1682  for ( j=0, dmin=10, view = viewlist; view; view = view->next, j++ ) {
1683  d = view->distance(part->view);
1684  if ( d < dmin ) {
1685  dmin = d;
1686  part->sel = j+1;
1687  }
1688  }
1689  np[part->sel-1]++;
1690  }
1691  }
1692  }
1693  }
1694 
1695  kill_list((char *) viewlist, sizeof(View));
1696 
1697  for ( j=n0=0; j<nview; j++ ) if ( np[j] < 1 ) n0++;
1698 
1699  if ( verbose ) {
1700  cout << "Selecting a maximum of " << number << " best particles from each view:" << endl;
1701  cout << "Symmetry: " << sym->label << endl;
1702  cout << "Number of views: " << nview << endl;
1703  cout << "View coverage: " << 100*(1 - n0*1.0/nview) << " %" << endl;
1704  }
1705 
1706  // For each view, select the best desired number of particles
1707  for ( j=0; j<nview; j++ ) if ( np[j] ) {
1708  for ( k=n=0; k<number; k++ ) bestpart[k] = NULL;
1709  for ( field = project->field; field; field = field->next ) {
1710  for ( mg = field->mg; mg; mg = mg->next ) {
1711  for ( part = mg->part; part; part = part->next ) {
1712  if ( part->sel == j+1 ) {
1713  if ( n < number ) bestpart[n++] = part; // Fill up the array
1714  else {
1715  for ( k=0, imin=0, fommin=1e30; k<number; k++ ) { // Find the minimum
1716  if ( fommin > bestpart[k]->fom[fom_index] ) {
1717  fommin = bestpart[k]->fom[fom_index];
1718  imin = k;
1719  }
1720  }
1721  if ( part->fom[fom_index] > fommin ) { // Replace the minimum
1722  bestpart[imin]->sel = 0;
1723  bestpart[imin] = part;
1724  } else part->sel = 0;
1725  }
1726  }
1727  }
1728  }
1729  }
1730  }
1731 
1732  delete[] np;
1733  delete[] bestpart;
1734 
1736 
1737  if ( verbose & VERB_PROCESS )
1738  cout << "Particles selected: " << nsel << endl << endl;
1739 
1740  return nsel;
1741 }
1742 
1749 {
1750  if ( !project ) return 0;
1751 
1752  long nsel(0);
1753 
1754  Bfield* field;
1755  Bmicrograph* mg;
1756  Breconstruction* rec;
1757  Bparticle* part;
1758 
1759  if ( verbose & VERB_LABEL )
1760  cout << "Transferring selections to groups:" << endl;
1761 
1762  if ( project->select ) {
1763  for ( rec = project->rec; rec; rec = rec->next ) {
1764  for ( part = rec->part; part; part = part->next ) {
1765  part->group = part->sel;
1766  if ( part->sel ) ++nsel;
1767  }
1768  }
1769  } else {
1770  for ( field = project->field; field; field = field->next ) {
1771  for ( mg = field->mg; mg; mg = mg->next ) {
1772  for ( part = mg->part; part; part = part->next ) {
1773  part->group = part->sel;
1774  if ( part->sel ) ++nsel;
1775  }
1776  }
1777  }
1778  }
1779 
1780  if ( verbose & VERB_LABEL )
1781  cout << "Particles selected: " << nsel << endl << endl;
1782 
1783  return nsel;
1784 }
1785 
1793 {
1794  if ( !project ) return 0;
1795 
1796  long nsel(0);
1797 
1798  Bfield* field;
1799  Bmicrograph* mg;
1800  Breconstruction* rec;
1801  Bparticle* part;
1802 
1803  if ( verbose & VERB_LABEL )
1804  cout << "Selecting particles of group " << group << endl;
1805 
1806  if ( project->select ) {
1807  for ( rec = project->rec; rec; rec = rec->next ) {
1808  for ( part = rec->part; part; part = part->next ) if ( part->sel ) {
1809  if ( part->group == group ) part->sel = ++nsel;
1810  else part->sel = 0;
1811  }
1812  }
1813  } else {
1814  for ( field = project->field; field; field = field->next ) {
1815  for ( mg = field->mg; mg; mg = mg->next ) {
1816  for ( part = mg->part; part; part = part->next ) if ( part->sel ) {
1817  if ( part->group == group ) part->sel = ++nsel;
1818  else part->sel = 0;
1819  }
1820  }
1821  }
1822  }
1823 
1824  if ( verbose & VERB_LABEL )
1825  cout << "Particles selected: " << nsel << endl << endl;
1826 
1827  return nsel;
1828 }
1829 
1841 long part_select_sets(Bproject* project, int size, int flag)
1842 {
1843  if ( !project || size <= 0 ) return 0;
1844 
1845  long nsel(0), count(0), np, number(0), ntot(0);
1846 
1847  Bfield* field;
1848  Bmicrograph* mg;
1849  Breconstruction* rec;
1850  Bparticle* part;
1851 
1852  if ( verbose & VERB_LABEL )
1853  cout << "Generating sets of " << size << " particles each" << endl;
1854 
1855  if ( project->select ) {
1856  for ( field = project->field; field; field = field->next )
1857  for ( mg = field->mg; mg; mg = mg->next ) mg->select = 0;
1858  for ( rec = project->rec; rec; rec = rec->next ) {
1859  if ( flag && count ) {
1860  count = 0;
1861  number++;
1862  }
1863  for ( np=0, part = rec->part; part; part = part->next, ntot++ ) {
1864  if ( part->sel ) {
1865  if ( count >= size ) {
1866  number++;
1867  count = 0;
1868  }
1869  if ( number < 1 ) number = 1;
1870  part->sel = number;
1871  count++;
1872  nsel++;
1873  np++;
1874  }
1875  }
1876  if ( np < 1 ) rec->select = 0;
1877  }
1878  } else {
1879  for ( rec = project->rec; rec; rec = rec->next ) rec->select = 0;
1880  for ( field = project->field; field; field = field->next ) {
1881  for ( mg = field->mg; mg; mg = mg->next ) {
1882  if ( flag && count ) {
1883  count = 0;
1884  number++;
1885  }
1886  for ( np=0, part = mg->part; part; part = part->next, ntot++ ) {
1887  if ( part->sel ) {
1888  if ( count >= size ) {
1889  number++;
1890  count = 0;
1891  }
1892  if ( number < 1 ) number = 1;
1893  part->sel = number;
1894  count++;
1895  nsel++;
1896  np++;
1897  }
1898  }
1899  if ( np < 1 ) mg->select = 0;
1900  }
1901  }
1902  }
1903 
1904  if ( verbose ) {
1905  if ( ntot < 1 ) ntot = 1;
1906  cout << "Number of sets generated: " << number << endl;
1907  cout << "Number of particles selected: " << nsel << " (" << nsel*100.0/ntot << " %)" << endl << endl;
1908  }
1909 
1910  return nsel;
1911 }
1912 
1924 long part_select_frames(Bproject* project, int frame_start, int frame_end)
1925 {
1926  int n, sel;
1927  long nsel(0);
1928  Bfield* field;
1929  Bmicrograph* mg;
1930  Bparticle* part;
1931 
1932  if ( verbose & (VERB_LABEL | VERB_PROCESS) )
1933  cout << "Selecting particle series." << endl;
1934 
1935  for ( field = project->field; field; field = field->next ) {
1936  for ( n=1, mg = field->mg; mg; mg = mg->next, n++ ) {
1937  sel = 0;
1938  if ( n >= frame_start && n <= frame_end ) sel = 1;
1939  for ( part = mg->part; part; part = part->next ) {
1940  if ( !sel ) part->sel = 0;
1941  if ( part->sel ) nsel++;
1942  }
1943  }
1944  }
1945 
1946  return nsel;
1947 }
1948 
1949 
1951 {
1952  long nchanged(0);
1953  int i, group(-1), n[2], dir(0);
1954  double da, a[2];
1955  View view, pview;
1956  Bparticle* part;
1957  Bparticle* fil_start = NULL;
1958  Bparticle* fil_part = NULL;
1959 
1960  for ( part = partlist; part; part = part->next ) {
1961  view = part->view.backward();
1962  if ( part->group != group ) {
1963  fil_start = part;
1964  dir = 0;
1965  n[0] = 1;
1966  n[1] = 0;
1967  a[0] = a[1] = 0;
1968  group = part->group;
1969  } else {
1970  da = view.angle(pview);
1971  if ( da > M_PI_2 ) {
1972  dir = 1 - dir;
1973  da -= M_PI;
1974  if ( verbose & VERB_FULL )
1975  cout << group << tab << part->id << tab << da << tab << 1-2*dir << endl;
1976  }
1977  a[dir] += da*da;
1978  n[dir]++;
1979  if ( verbose & VERB_FULL )
1980  cout << group << tab << part->id << tab << da << tab << 1-2*dir << endl;
1981  }
1982  if ( part->next == NULL || part->next->group != group ) {
1983  for ( i=0; i<2; i++ ) if ( n[i] ) a[i] = sqrt(a[i]/n[i]);
1984  if ( verbose & VERB_PROCESS )
1985  cout << part->group << tab << n[0] << tab << n[1] << tab <<
1986  a[0]*180.0/M_PI << tab << a[1]*180.0/M_PI << endl;
1987  pview = fil_start->view.backward();
1988  if ( n[1] > n[0] ) { pview.negate(); pview[3] = -pview.angle(); }
1989  for ( fil_part = fil_start; fil_part && fil_part->group == group; fil_part = fil_part->next ) {
1990  view = fil_part->view.backward();
1991  da = view.angle(pview);
1992  if ( da > M_PI_2 ) {
1993  view.negate(); view[3] = -view.angle();
1994  fil_part->view = view.backward();
1995  nchanged++;
1996  }
1997  pview = view;
1998  }
1999  }
2000  pview = part->view.backward();
2001  }
2002 
2003  return nchanged;
2004 }
2005 
2006 long part_fil_dir_stats(Bparticle* partlist, double minpct)
2007 {
2008  if ( minpct < 1 ) minpct *= 100;
2009 
2010  long n(0), up(0), dn(0), nt(0);
2011  int group(-1), fin(0), dir;
2012  double tol(20*M_PI/180), pup, pdn;
2013  Euler euler;
2014  Bparticle* part;
2015  Bparticle* fil_part = NULL;
2016 
2017  if ( verbose & VERB_PROCESS )
2018  cout << "Fil#\t#part\tup\t%up\tdown\t%down\toutliers" << endl;
2019 
2020  for ( part = partlist; part; part = part->next ) {
2021  euler = Euler(part->view);
2022 
2023  if ( part->group != group ) {
2024  n = up = dn = fin = 0;
2025  pup = pdn = 0;
2026  group = part->group;
2027  fil_part = part;
2028  }
2029 
2030  n++;
2031  if ( euler.psi() <= -M_PI_2 + tol && euler.psi() >= -M_PI_2 - tol ) up++;
2032  if ( euler.psi() <= M_PI_2 + tol && euler.psi() >= M_PI_2 - tol ) dn++;
2033 
2034  if ( part->next ) {
2035  if ( part->next->group != group ) fin = 1;
2036  } else fin = 1;
2037 
2038  if ( fin ) {
2039  if ( n ) {
2040  pup = up*100.0/n;
2041  pdn = dn*100.0/n;
2042  nt += up + dn;
2043  }
2044 
2045  if ( verbose & VERB_PROCESS )
2046  cout << group << tab << n << tab << up << tab << pup << tab << dn << tab << pdn << tab << n-up-dn << endl;
2047 
2048  if ( pup >= minpct ) {
2049  dir = 1;
2050  } else if ( pdn >= minpct ) {
2051  dir = -1;
2052  } else {
2053  dir = 0;
2054  }
2055 
2056  for ( ; fil_part && fil_part->group == group; fil_part = fil_part->next ) if ( fil_part->sel > 0 ) {
2057  euler = Euler(fil_part->view);
2058  fil_part->sel = 0;
2059  if ( euler.psi() <= -M_PI_2 + tol && euler.psi() >= -M_PI_2 - tol && dir == 1 ) fil_part->sel = 1;
2060  if ( euler.psi() <= M_PI_2 + tol && euler.psi() >= M_PI_2 - tol && dir == -1 ) fil_part->sel = 1;
2061  }
2062  }
2063  }
2064 
2065  return nt;
2066 }
2067 
2068 
2082 {
2083  if ( !project ) return 0;
2084  if ( minpct < 1 ) minpct *= 100;
2085 
2086  long npart(0), ndir(0);
2087 
2088  Bfield* field;
2089  Bmicrograph* mg;
2090  Breconstruction* rec;
2091 
2092  if ( verbose & ( VERB_LABEL | VERB_PROCESS ) ) {
2093  cout << "Checking filament directions:" << endl;
2094  cout << "Minimum percentage directed particles: " << minpct << endl << endl;
2095  }
2096 
2097  if ( project->select ) {
2099  for ( rec = project->rec; rec; rec = rec->next ) {
2100  if ( verbose )
2101  cout << "Reconstruction: " << rec->id << endl;
2102  ndir += part_fil_dir_stats(rec->part, minpct);
2103  }
2104  } else {
2106  for ( field = project->field; field; field = field->next )
2107  for ( mg = field->mg; mg; mg = mg->next ) {
2108  if ( verbose )
2109  cout << "Micrograph: " << mg->id << endl;
2110  ndir += part_fil_dir_stats(mg->part, minpct);
2111  }
2112  }
2113 
2114  if ( verbose & VERB_LABEL )
2115  cout << endl << "Particles with direction: " << ndir << " (" << ndir*100.0/npart << "%)" << endl << endl;
2116 
2117  return ndir;
2118 }
2119 
2130 long part_view_select(Bproject* project, View view, double angle)
2131 {
2132  long nsel(0);
2133  double da;
2134  Bfield* field;
2135  Bmicrograph* mg;
2136  Breconstruction* rec;
2137  Bparticle* part;
2138 
2139  if ( verbose & (VERB_LABEL|VERB_PROCESS) )
2140  cout << "Selecting particles based on a view" << endl;
2141  if ( verbose & VERB_PROCESS ) {
2142  cout << "View: " << view << endl;
2143  cout << "Deviation angle: " << angle*180/M_PI << " degrees" << endl;
2144  }
2145 
2146  if ( project->select < 1 ) {
2147  for ( field = project->field; field; field = field->next ) {
2148  for ( mg = field->mg; mg; mg = mg->next ) {
2149  for ( part = mg->part; part; part = part->next ) {
2150  da = part->view.angle(view);
2151  if ( da > angle ) part->sel = 0;
2152  if ( part->sel ) nsel++;
2153  }
2154  }
2155  }
2156  } else {
2157  for ( rec = project->rec; rec; rec = rec->next ) {
2158  for ( part = rec->part; part; part = part->next ) {
2159  da = part->view.angle(view);
2160  if ( da > angle ) part->sel = 0;
2161  if ( part->sel ) nsel++;
2162  }
2163  }
2164  }
2165 
2166  if ( verbose & VERB_PROCESS )
2167  cout << "Particles selected: " << nsel << endl << endl;
2168 
2169  return nsel;
2170 }
2171 
2182 {
2183  long nsel(0);
2184  double dz(sin(angle));
2185  Bfield* field;
2186  Bmicrograph* mg;
2187  Breconstruction* rec;
2188  Bparticle* part;
2189 
2190  if ( verbose & (VERB_LABEL|VERB_PROCESS) )
2191  cout << "Selecting particles with side views" << endl;
2192  if ( verbose & VERB_PROCESS ) {
2193  cout << "Deviation angle: " << angle*180/M_PI << " degrees" << endl;
2194  }
2195 
2196  if ( project->select < 1 ) {
2197  for ( field = project->field; field; field = field->next ) {
2198  for ( mg = field->mg; mg; mg = mg->next ) {
2199  for ( part = mg->part; part; part = part->next ) {
2200  if ( part->view[2] > dz ) part->sel = 0;
2201  if ( part->sel ) nsel++;
2202  }
2203  }
2204  }
2205  } else {
2206  for ( rec = project->rec; rec; rec = rec->next ) {
2207  for ( part = rec->part; part; part = part->next ) {
2208  if ( part->view[2] > dz ) part->sel = 0;
2209  if ( part->sel ) nsel++;
2210  }
2211  }
2212  }
2213 
2214  if ( verbose & VERB_PROCESS )
2215  cout << "Particles selected: " << nsel << endl << endl;
2216 
2217  return nsel;
2218 }
2219 
2230 {
2231  long i, nsel(0);
2232 
2233  Bfield* field;
2234  Bmicrograph* mg;
2235  Breconstruction* rec;
2236  Bparticle* part;
2237  Euler euler;
2238 
2239  if ( verbose & (VERB_LABEL|VERB_PROCESS) )
2240  cout << "Selecting particles based on Euler angle limits" << endl;
2241  if ( verbose & VERB_PROCESS ) {
2242  cout << "Psi limits: " << euler6[0]*180/M_PI << " - " << euler6[1]*180/M_PI << " degrees" << endl;
2243  cout << "Theta limits: " << euler6[2]*180/M_PI << " - " << euler6[3]*180/M_PI << " degrees" << endl;
2244  cout << "Phi limits: " << euler6[4]*180/M_PI << " - " << euler6[5]*180/M_PI << " degrees" << endl;
2245  }
2246 
2247  if ( project->select < 1 ) {
2248  for ( field = project->field; field; field = field->next ) {
2249  for ( mg = field->mg; mg; mg = mg->next ) {
2250  for ( part = mg->part; part; part = part->next ) if ( part->sel ) {
2251  euler = Euler(part->view);
2252  for ( i=0; i<3; i++ ) {
2253  if ( euler[i] < euler6[2*i] ) euler[i] += TWOPI;
2254  if ( euler[i] > euler6[2*i+1] ) euler[i] -= TWOPI;
2255  if ( euler[i] < euler6[2*i] || euler[i] > euler6[2*i+1] ) part->sel = 0;
2256  }
2257  if ( part->sel ) nsel++;
2258  }
2259  }
2260  }
2261  } else {
2262  for ( rec = project->rec; rec; rec = rec->next ) {
2263  for ( part = rec->part; part; part = part->next ) if ( part->sel ) {
2264  euler = Euler(part->view);
2265  for ( i=0; i<3; i++ ) {
2266  if ( euler[i] < euler6[2*i] ) euler[i] += TWOPI;
2267  if ( euler[i] > euler6[2*i+1] ) euler[i] -= TWOPI;
2268  if ( euler[i] < euler6[2*i] || euler[i] > euler6[2*i+1] ) part->sel = 0;
2269  }
2270  if ( part->sel ) nsel++;
2271  }
2272  }
2273  }
2274 
2275  if ( verbose & VERB_PROCESS )
2276  cout << "Particles selected: " << nsel << endl << endl;
2277 
2278  return nsel;
2279 }
2280 
2292 {
2293  long nsel(0);
2294  double dd;
2295  Bfield* field;
2296  Bmicrograph* mg;
2297  Breconstruction* rec;
2298  Bparticle* part;
2299 
2300  if ( verbose & (VERB_LABEL|VERB_PROCESS) )
2301  cout << "Selecting particles based on the origin" << endl;
2302  if ( verbose & VERB_PROCESS ) {
2303  cout << "Origin: " << origin << endl;
2304  cout << "Maximum distance: " << distance << " pixels" << endl;
2305  }
2306 
2307  if ( project->select < 1 ) {
2308  for ( field = project->field; field; field = field->next ) {
2309  for ( mg = field->mg; mg; mg = mg->next ) {
2310  for ( part = mg->part; part; part = part->next ) {
2311  dd = part->ori.distance(origin);
2312  if ( dd > distance ) part->sel = 0;
2313  if ( part->sel ) nsel++;
2314  }
2315  }
2316  }
2317  } else {
2318  for ( rec = project->rec; rec; rec = rec->next ) {
2319  for ( part = rec->part; part; part = part->next ) {
2320  dd = part->ori.distance(origin);
2321  if ( dd > distance ) part->sel = 0;
2322  if ( part->sel ) nsel++;
2323  }
2324  }
2325  }
2326 
2327  if ( verbose & VERB_PROCESS )
2328  cout << "Particles selected: " << nsel << endl << endl;
2329 
2330  return nsel;
2331 }
2332 
2333 
2344 {
2345  int f;
2346  Bfield* field;
2347  Bmicrograph* mg, *mg0;
2348  Bparticle* part, *part0;
2349 
2350  if ( verbose & (VERB_LABEL | VERB_PROCESS) )
2351  cout << "Setting all to the first orientations in the first micrograph in the field-of-view" << endl;
2352 
2353  for ( field = project->field; field; field = field->next ) {
2354  for ( mg0=field->mg, mg=field->mg->next; mg; mg=mg->next ) {
2355  for ( part=mg->part, part0=mg0->part; part; part=part->next, part0=part0->next ) {
2356  for ( f=0; f<NFOM; f++ ) part->fom[f] = part0->fom[f];
2357  part->sel = part0->sel;
2358  part->view = part0->view;
2359  }
2360  }
2361  }
2362 
2363  return 0;
2364 }
2365 
2378 {
2379  int f;
2380  Bfield* field;
2381  Bmicrograph* mg, *mg0;
2382  Bparticle* part, *part0;
2383 
2384  if ( verbose & (VERB_LABEL | VERB_PROCESS) )
2385  cout << "Setting all to the best orientations in the field-of-view" << endl;
2386 
2387  for ( field = project->field; field; field = field->next ) {
2388  for ( mg0=field->mg, mg=field->mg->next; mg; mg=mg->next ) {
2389  for ( part=mg->part, part0=mg0->part; part; part=part->next, part0=part0->next ) {
2390  if ( part->fom[fom_index] > part0->fom[fom_index] ) {
2391  for ( f=0; f<NFOM; f++ ) part0->fom[f] = part->fom[f];
2392  part0->sel = part->sel;
2393  part0->view = part->view;
2394  }
2395  }
2396  }
2397  for ( mg0=field->mg, mg=field->mg->next; mg; mg=mg->next ) {
2398  for ( part=mg->part, part0=mg0->part; part; part=part->next, part0=part0->next ) {
2399  for ( f=0; f<NFOM; f++ ) part->fom[f] = part0->fom[f];
2400  part->sel = part0->sel;
2401  part->view = part0->view;
2402  }
2403  }
2404  }
2405 
2406  return 0;
2407 }
2408 
2421 long part_select_series(Bproject* project, int size, int flag)
2422 {
2423  if ( size < 1 ) size = 1000000000;
2424 
2425  int n;
2426  long count(0), nser(0), nsel(0);
2427  Bfield* field;
2428  Bmicrograph* mg, *mg0;
2429  Bparticle* part, *part0;
2430 
2431  if ( verbose & (VERB_LABEL | VERB_PROCESS) )
2432  cout << "Selecting particle series." << endl;
2433 
2434  for ( field = project->field; field; field = field->next ) {
2435  if ( flag ) nser = 0;
2436  for ( n=0, mg0=field->mg, mg = field->mg; mg; mg = mg->next, n++ ) {
2437  for ( part0=mg0->part, part = mg->part; part; part = part->next, part0=part0->next ) {
2438  if ( part->sel ) {
2439  if ( mg0 == mg ) part0->sel = 1;
2440  else part0->sel++;
2441  }
2442  }
2443  }
2444  for ( part0=field->mg->part; part0; part0=part0->next ) {
2445  if ( part0->sel == n ) {
2446  if ( nser%size == 0 ) count++;
2447  part0->sel = count;
2448  nser++;
2449  } else {
2450  part0->sel = 0;
2451  }
2452  }
2453  for ( mg0=field->mg, mg = field->mg; mg; mg = mg->next ) {
2454  for ( part0=mg0->part, part = mg->part; part; part = part->next, part0=part0->next ) {
2455  if ( part0->sel ) {
2456  part->sel = part0->sel;
2457  nsel++;
2458  } else {
2459  part->sel = 0;
2460  }
2461  }
2462  }
2463  }
2464 
2465  return nsel;
2466 }
2467 
2481 {
2482  int zflip(flags & 1), use_view(flags & 2);
2483  long i, n, npart(0);
2484  Matrix3 mat(1);
2485  Bfield* field;
2486  Bmicrograph* mg;
2487  Bmicrograph* mg_ref = NULL;
2488  Breconstruction* rec;
2489  Bparticle* part_list = NULL;
2490  Bparticle *part_ref, *part;
2491  Vector3<long> part_size;
2492  Vector3<double> origin, scale(1,1,1), loc, part_ori;
2493 
2494  if ( !project->rec ) project->select = 0;
2495 
2496  // Find the reference particle list
2497  if ( project->select ) {
2498  if ( verbose )
2499  cout << "Calculating micrograph particle locations from 3D locations" << endl;
2500  for ( rec = project->rec; rec && !part_list; rec = rec->next )
2501  if ( rec->part ) break;
2502  if ( rec ) {
2503  part_list = rec->part;
2504  origin = rec->origin;
2505  scale /= rec->scale;
2506  part_size = scale*rec->box_size;
2507  }
2508  } else {
2509  if ( verbose )
2510  cout << "Calculating micrograph particle locations from zero-tilt micrograph" << endl;
2511  for ( field = project->field; field && !part_list; field = field->next ) {
2512  for ( mg = field->mg; mg && !part_list; mg = mg->next ) if ( mg->part ) {
2513  mg_ref = mg;
2514  part_list = mg->part;
2515  origin = mg->origin;
2516  part_size = mg->box_size;
2517  }
2518  }
2519  }
2520 
2521  if ( !part_list ) {
2522  cerr << "Error: No particle records found!" << endl;
2523  return -1;
2524  }
2525 
2526  part_size[2] = 1;
2527  part_ori = part_size/2;
2528 
2529  // Set the selection number
2530  for ( n=0, part_ref=part_list; part_ref; part_ref=part_ref->next )
2531  part_ref->sel = ++n;
2532 
2533  double ca(1), sa(0), tt(0);
2534 
2535  // Generate new particle records
2536  if ( verbose )
2537  cout << "Number of particles: " << n << endl;
2538  for ( field = project->field; field; field = field->next ) {
2539  for ( mg = field->mg; mg; mg = mg->next ) if ( mg != mg_ref ) {
2540  if ( mg->part ) particle_kill(mg->part);
2541  mg->part = part = NULL;
2542  mg->box_size = part_size;
2543  ca = cos(mg->tilt_axis);
2544  sa = sin(mg->tilt_axis);
2545  tt = tan(mg->tilt_angle);
2546  for ( part_ref=part_list; part_ref; part_ref=part_ref->next ) {
2547  part = particle_add(&part, part_ref->id);
2548  if ( !mg->part ) mg->part = part;
2549  loc = (part_ref->loc - origin)*scale;
2550  if ( zflip ) loc[2] = -loc[2];
2551  part->loc = mg_location_from_3D_model(loc, mg->matrix, mg->origin, mg->scale);
2552  part->ori = part_ori;
2553  if ( use_view )
2554  mat = part_ref->view.matrix();
2555  part->view = View(mat * mg->matrix);
2556  for ( i=0; i<NFOM; i++ ) part->fom[i] = part_ref->fom[i];
2557  part->sel = part_ref->sel;
2558  if ( mg->ctf ) {
2559  loc = (part->loc - mg->origin)*mg->pixel_size;
2560  part->def = mg->ctf->defocus_average() +
2561  (loc[0]*sa - loc[1]*ca)*tt;
2562  part->dev = mg->ctf->defocus_deviation();
2563  part->ast = mg->ctf->astigmatism_angle();
2564  }
2565  npart++;
2566  }
2567  }
2568  }
2569 
2570  return npart;
2571 }
2572 
2584 double part_fom_defocus_fit(Bproject* project, int fom_index, double& intercept, double& slope)
2585 {
2586  slope = 0;
2587  intercept = 1;
2588 
2589  Bfield* field;
2590  Bmicrograph* mg;
2591  Bparticle* part;
2592 
2593  long i, npart, nmg = project_count_micrographs(project);
2594  double sum, CI(0);
2595  double* x = new double[nmg];
2596  double* y = new double[nmg];
2597  for ( i=0; i<nmg; i++ ) x[i] = y[i] = 0;
2598 
2599  if ( verbose & VERB_FULL )
2600  cout << "Defocus\tFOMavg" << endl;
2601  for ( i=0, field = project->field; field; field = field->next )
2602  for ( mg = field->mg; mg; mg = mg->next ) if ( mg->ctf ) {
2603  for ( npart=0, sum=0, part = mg->part; part; part = part->next )
2604  if ( part->sel ) {
2605  sum += part->fom[fom_index];
2606  npart++;
2607  }
2608  if ( npart ) {
2609  x[i] = mg->ctf->defocus_average();
2610  y[i] = sum/npart;
2611  if ( verbose & VERB_FULL )
2612  cout << x[i] << tab << y[i] << endl;
2613  i++;
2614  }
2615  }
2616  if ( verbose & VERB_FULL )
2617  cout << endl;
2618 
2619  if ( i ) CI = linear_least_squares(0, i-1, x, y, intercept, slope);
2620 
2621  delete[] x;
2622  delete[] y;
2623 
2624  if ( verbose & VERB_PROCESS ) {
2625  cout << "Linear fit: FOM = " << slope << " * defocus + " << intercept << endl;
2626  cout << "Correlation index: " << CI << endl << endl;
2627  }
2628 
2629  return CI;
2630 }
2631 
2644 double part_fom_defocus_fit_deselect(Bproject* project, int fom_index, double cutoff)
2645 {
2646  Bfield* field;
2647  Bmicrograph* mg;
2648  Bparticle* part;
2649 
2650  double slope, intercept, this_cutoff;
2651  double CI = part_fom_defocus_fit(project, fom_index, intercept, slope);
2652 
2653  for ( field = project->field; field; field = field->next )
2654  for ( mg = field->mg; mg; mg = mg->next ) {
2655 // this_cutoff = slope*mg->ctf->defocus_average() + cutoff;
2656  this_cutoff = slope*(mg->ctf->defocus_average() - 1e4) + cutoff;
2657  for ( part = mg->part; part; part = part->next )
2658  if ( part->fom[fom_index] < this_cutoff) part->sel = 0;
2659  }
2660 
2661  return CI;
2662 }
2663 
2670 {
2671  long nsel(0);
2672  Bfield* field;
2673  Bmicrograph* mg, *mg_ctf;
2674  Bparticle* part;
2675 
2676  for ( field = project->field; field; field = field->next ) {
2677  for ( mg = mg_ctf = field->mg; mg; mg = mg->next ) {
2678  if ( mg_ctf->ctf->defocus_average() > mg->ctf->defocus_average() )
2679  mg_ctf = mg;
2680  }
2681  for ( mg = field->mg; mg; mg = mg->next ) {
2682  if ( mg != mg_ctf )
2683  for ( part = mg->part; part; part = part->next )
2684  part->sel = 0;
2685  else
2686  for ( part = mg->part; part; part = part->next )
2687  if ( part->sel ) nsel++;
2688  }
2689  }
2690 
2691  return nsel;
2692 }
2693 
2700 {
2701  long nsel(0);
2702  Bfield* field;
2703  Bmicrograph* mg, *mg_ctf;
2704  Bparticle* part;
2705 
2706  for ( field = project->field; field; field = field->next ) {
2707  for ( mg = mg_ctf = field->mg; mg; mg = mg->next ) {
2708  if ( mg_ctf->ctf->defocus_average() < mg->ctf->defocus_average() )
2709  mg_ctf = mg;
2710  }
2711  for ( mg = field->mg; mg; mg = mg->next ) {
2712  if ( mg != mg_ctf )
2713  for ( part = mg->part; part; part = part->next )
2714  part->sel = 0;
2715  else
2716  for ( part = mg->part; part; part = part->next )
2717  if ( part->sel ) nsel++;
2718  }
2719  }
2720 
2721  return nsel;
2722 }
2723 
2730 {
2731  long id, nsel(0);
2732  Bfield* field;
2733  Bmicrograph* mg;
2734  Breconstruction* rec;
2735  Bparticle* part, *part_nu, *part_list;
2736 
2737  if ( project->select ) {
2738  for ( rec = project->rec; rec; rec = rec->next ) {
2739  for ( id=0, part_list = NULL, part = rec->part; part; part = part->next ) {
2740  if ( part->sel ) {
2741  part_nu = particle_copy(&part_list, part);
2742  part_nu->id = ++id;
2743  }
2744  }
2745  particle_kill(rec->part);
2746  rec->part = part_list;
2747  nsel += id;
2748  }
2749  } else {
2750  for ( field = project->field; field; field = field->next ) {
2751  for ( mg = field->mg; mg; mg = mg->next ) {
2752  for ( id=0, part_list = NULL, part = mg->part; part; part = part->next ) {
2753  if ( part->sel ) {
2754  part_nu = particle_copy(&part_list, part);
2755  part_nu->id = ++id;
2756  }
2757  }
2758  particle_kill(mg->part);
2759  mg->part = part_list;
2760  nsel += id;
2761  }
2762  }
2763  }
2764 
2765  if ( verbose & VERB_PROCESS )
2766  cout << "Particles selected: " << nsel << endl << endl;
2767 
2768  return nsel;
2769 }
2770 
2780 {
2781  long id(0);
2782  Bparticle* part, *part_nu, *nulist;
2783 
2784  for ( nulist = NULL, part = *partlist; part; part = part->next ) {
2785  if ( part->sel ) {
2786  part_nu = particle_copy(&nulist, part);
2787  part_nu->id = ++id;
2788  }
2789  }
2790 
2791  particle_kill(*partlist);
2792 
2793  *partlist = nulist;
2794 
2795  return id;
2796 }
2797 
2804 long part_fix_defocus(Bproject* project, double max_dev)
2805 {
2806  long nd(0);
2807  double dmin, dmax;
2808  Bfield* field;
2809  Bmicrograph* mg;
2810  Bparticle* part;
2811 
2812  if ( verbose & VERB_PROCESS ) {
2813  cout << "Resetting particle defocus:" << endl;
2814  cout << "Maximum difference allowed: " << max_dev << " A" << endl;
2815  }
2816 
2817  for ( field = project->field; field; field = field->next ) {
2818  for ( mg = field->mg; mg; mg = mg->next ) if ( mg->ctf ) {
2819  dmin = mg->ctf->defocus_average() - max_dev;
2820  dmax = mg->ctf->defocus_average() + max_dev;
2821  for ( part = mg->part; part; part = part->next ) {
2822  if ( part->def < dmin || part->def > dmax ) {
2823  part->def = mg->ctf->defocus_average();
2824  nd++;
2825  }
2826  }
2827  }
2828  }
2829 
2830  if ( verbose & VERB_PROCESS )
2831  cout << "Number of defocus values reset: " << nd << endl << endl;
2832 
2833  return nd;
2834 }
2835 
2842 vector<pair<Bparticle*,double>> project_part_sort(Bproject* project, Bstring tag)
2843 {
2844  Bfield* field;
2845  Bmicrograph* mg;
2846  Bparticle* part;
2847 
2848  vector<std::pair<Bparticle*,double>> part_value;
2849  pair<Bparticle*,double> pv;
2850 
2851  for ( field = project->field; field; field = field->next ) {
2852  for ( mg = field->mg; mg; mg = mg->next ) {
2853  for ( part = mg->part; part; part = part->next ) {
2854  pv.first = part;
2855  if ( tag == PARTICLE_FOM ) pv.second = part->fom[0];
2856  if ( tag == PARTICLE_FOM_CV ) pv.second = part->fom[1];
2857  part_value.push_back(pv);
2858  }
2859  }
2860  }
2861 
2862  sort(part_value.begin(), part_value.end(), [=](pair<Bparticle*,double> a, std::pair<Bparticle*,double> b) {
2863  return a.second < b.second;
2864  }
2865  );
2866 
2867  return part_value;
2868 }
particle_array
Bparticle ** particle_array(Bparticle *partlist, int part_select, long &npart)
Sets up an array of selected particle pointers.
Definition: mg_processing.cpp:1360
Bstring::erase
Bstring erase(const long i)
Definition: Bstring.cpp:493
Bmicrograph::scale
Vector3< double > scale
Definition: mg_processing.h:224
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
part_select_FOM_avg_std
long part_select_FOM_avg_std(Bproject *project, double factor, int fom_index)
Selects particles based on FOM average and standard deviation.
Definition: mg_particle_select.cpp:1108
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
Breconstruction::part
Bparticle * part
Definition: mg_processing.h:307
Breconstruction::id
Bstring id
Definition: mg_processing.h:285
part_filament_direction
long part_filament_direction(Bproject *project, double minpct)
Checks the direction of particles associated with filaments.
Definition: mg_particle_select.cpp:2081
View::next
View * next
Definition: View.h:28
matrix_linear.h
Header file for matrix functions.
View::distance
double distance(const View &view)
Definition: View.h:158
Bsymmetry::n
int n
Definition: symmetry.h:42
Bproject::rec
Breconstruction * rec
Definition: mg_processing.h:347
part_select_random_fraction
long part_select_random_fraction(Bproject *project, double fraction)
Selects particles randomly based on a fraction of the total.
Definition: mg_particle_select.cpp:1243
symmetry_get_all_views
View * symmetry_get_all_views(Bsymmetry *sym, View asu_view)
Get all symmetry-related views of one given view.
Definition: symmetry.cpp:442
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
part_fix_defocus
long part_fix_defocus(Bproject *project, double max_dev)
Resets defocus values for particles thata are too far off.
Definition: mg_particle_select.cpp:2804
PARTICLE_DEFOCUS
#define PARTICLE_DEFOCUS
Definition: mg_tags.h:212
Bproject::select
int select
Definition: mg_processing.h:342
Quaternion
Definition: Quaternion.h:25
kill_list
long kill_list(char *list, unsigned long size)
Frees all the items in a linked list.
Definition: linked_list.cpp:214
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
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
CTF_AST_ANG
#define CTF_AST_ANG
Definition: mg_tags.h:176
Bproject::field
Bfield * field
Definition: mg_processing.h:346
PARTICLE_ORIGIN_X
#define PARTICLE_ORIGIN_X
Definition: mg_tags.h:196
Euler::theta
double theta()
Definition: Euler.h:57
linear_least_squares
double linear_least_squares(int n1, int n2, double *x, double *y, double &a, double &b)
Does a linear least squares fit between two vectors.
Definition: matrix_linear.cpp:28
Breconstruction::ctf
CTFparam * ctf
Definition: mg_processing.h:306
symmetry.h
Header file for general symmetry functions.
part_unset_selection
long part_unset_selection(Bproject *project)
Unsets selection to no particles.
Definition: mg_particle_select.cpp:168
CTFparam::astigmatism_angle
double astigmatism_angle()
Definition: ctf.h:110
Bmicrograph::tilt_angle
double tilt_angle
Definition: mg_processing.h:220
Vector3< double >
random_numbers.h
Header file for functions for creating random images.
PARTICLE_VIEW_X
#define PARTICLE_VIEW_X
Definition: mg_tags.h:203
Bparticle::ast
double ast
Definition: mg_processing.h:98
Euler::psi
double psi()
Definition: Euler.h:56
View::y
double y()
Definition: View.h:96
mg_select.h
Header file for micrograph processing.
part_select_frames
long part_select_frames(Bproject *project, int frame_start, int frame_end)
Select particles from frames in a series.
Definition: mg_particle_select.cpp:1924
part_deselect_redundant
long part_deselect_redundant(Bparticle *partlist, double excl_dist, int part_select, int fom_index)
Deselects particles overlapping with better ones.
Definition: mg_particle_select.cpp:520
part_select_series
long part_select_series(Bproject *project, int size, int flag)
Select series where all are already selected.
Definition: mg_particle_select.cpp:2421
Bparticle::fom
double fom[NFOM]
Definition: mg_processing.h:103
part_select_closest_to_focus
long part_select_closest_to_focus(Bproject *project)
Deselects all particles except those from micrographs closest to focus.
Definition: mg_particle_select.cpp:2669
Breconstruction::box_size
Vector3< long > box_size
Definition: mg_processing.h:299
Bmicrograph::part
Bparticle * part
Definition: mg_processing.h:242
PARTICLE_OMEGA
#define PARTICLE_OMEGA
Definition: mg_tags.h:202
asymmetric_unit_views
View * asymmetric_unit_views(Bsymmetry *sym, double theta_step, double phi_step, int flag)
Initializes a well-distributed set of views in an asymmetric unit.
Definition: symmetry.cpp:564
View
Definition: View.h:26
PARTICLE_X
#define PARTICLE_X
Definition: mg_tags.h:190
part_set_multi_maps
long part_set_multi_maps(Bproject *project, int part_select, int nmaps)
Sets selection for generating multiple maps.
Definition: mg_particle_select.cpp:590
part_select_sets
long part_select_sets(Bproject *project, int size, int flag)
Selects sets of particles, each set with the same size.
Definition: mg_particle_select.cpp:1841
Bfield
Definition: mg_processing.h:260
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
int_float::f
float f
Definition: qsort_functions.h:12
CTF
#define CTF
Definition: mg_tags.h:161
M_PI_2
#define M_PI_2
Definition: utilities.h:66
project_maximum_selection
long project_maximum_selection(Bproject *project)
Finds the maximum selection number for a project.
Definition: mg_select.cpp:825
part_consolidate_selection
long part_consolidate_selection(Bproject *project, int number)
Consolidates selected particles under one selection number.
Definition: mg_particle_select.cpp:283
PARTICLE
#define PARTICLE
Definition: mg_tags.h:181
PARTICLE_AST_ANG
#define PARTICLE_AST_ANG
Definition: mg_tags.h:214
part_select_bootstrap
long part_select_bootstrap(Bproject *project, int number)
Selects a given number of particles randomly with replacement.
Definition: mg_particle_select.cpp:1383
int_float
Definition: qsort_functions.h:12
PARTICLE_ORIGIN_Y
#define PARTICLE_ORIGIN_Y
Definition: mg_tags.h:197
PARTICLE_Z
#define PARTICLE_Z
Definition: mg_tags.h:192
Vector3::distance
double distance(const Vector3 &v)
Definition: Vector3.h:227
PARTICLE_HANDA_FOM
#define PARTICLE_HANDA_FOM
Definition: mg_tags.h:230
part_select_micrograph
long part_select_micrograph(Bproject *project, Bstring &mgselect)
Sets selection to the micrographs indicated.
Definition: mg_particle_select.cpp:247
Breconstruction::origin
Vector3< double > origin
Definition: mg_processing.h:296
project
Bproject * project
Definition: bshow.cpp:33
part_select_random
long part_select_random(Bproject *project, long number)
Selects a specific number of particles randomly.
Definition: mg_particle_select.cpp:1163
Matrix3
Definition: Matrix3.h:18
View::backward
View backward()
Definition: View.h:118
mg_location_from_3D_model
Vector3< double > mg_location_from_3D_model(Vector3< double > loc, Matrix3 mat, Vector3< double > ori)
Calculates the 2D location of a marker from a 3D model.
Definition: mg_tomography.cpp:140
part_series_comparison
double part_series_comparison(Bproject *project, Bsymmetry *sym, double angle_cutoff)
A function to select particles based on comparison of the orientations within a series.
Definition: mg_particle_select.cpp:828
part_delete_deselected
long part_delete_deselected(Bproject *project)
Deletes deselected particles and renumber the remaining.
Definition: mg_particle_select.cpp:2729
CTF_DEF_AVG
#define CTF_DEF_AVG
Definition: mg_tags.h:172
part_series_from_seed
long part_series_from_seed(Bproject *project, int flags)
Calculates the locations of particles in a series from a seed.
Definition: mg_particle_select.cpp:2480
PARTICLE_VIEW_Y
#define PARTICLE_VIEW_Y
Definition: mg_tags.h:204
part_select_to_group
long part_select_to_group(Bproject *project)
Transfer selection numbers to the group identifiers.
Definition: mg_particle_select.cpp:1748
part_view_select
long part_view_select(Bproject *project, View view, double angle)
Selects particles based on a distance from a reference view.
Definition: mg_particle_select.cpp:2130
part_convert_selection
long part_convert_selection(Bproject *project, int *sel)
Definition: mg_particle_select.cpp:899
PARTICLE_FOM_CV
#define PARTICLE_FOM_CV
Definition: mg_tags.h:217
Bmicrograph::origin
Vector3< double > origin
Definition: mg_processing.h:223
part_select_best_within_view
long part_select_best_within_view(Bproject *project, Bsymmetry *sym, double theta_step, double phi_step, int number, int fom_index)
Selects the best number of particles within each view.
Definition: mg_particle_select.cpp:1659
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
part_set_first_view_in_series
long part_set_first_view_in_series(Bproject *project)
Sets all orientation parameters for a particle to the first in the set.
Definition: mg_particle_select.cpp:2343
PARTICLE_PSI
#define PARTICLE_PSI
Definition: mg_tags.h:199
Bsymop::order
int order
Definition: symmetry.h:26
Bmicrograph::tilt_axis
double tilt_axis
Definition: mg_processing.h:219
Bproject
Definition: mg_processing.h:336
part_reset_selection
long part_reset_selection(Bproject *project, int flag)
Resets selection to all particles.
Definition: mg_particle_select.cpp:133
part_select_maxsmooth
long part_select_maxsmooth(Bproject *project, Bsymmetry *sym, double theta_step, double phi_step, double threshfrac, double sigma, int fom_index)
Selects particles based on a smoothed maximum surface.
Definition: mg_particle_select.cpp:1546
part_deselect
long part_deselect(Bproject *project, int fom_index, double fommin, double fommax)
Deselects particles below a given FOM cutoff.
Definition: mg_particle_select.cpp:468
part_select
Tcl_Obj * part_select(Bparticle *part, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bmg.cpp:563
Bparticle::sel
long sel
Definition: mg_processing.h:104
distance
double distance(vector< T1 > &v1, vector< T2 > &v2)
Definition: Vector3.h:538
part_fil_dir_stats
long part_fil_dir_stats(Bparticle *partlist)
Definition: mg_particle_select.cpp:1950
part_side_view_select
long part_side_view_select(Bproject *project, double angle)
Selects particles based on a distance from a side view.
Definition: mg_particle_select.cpp:2181
CTFparam::defocus_deviation
double defocus_deviation()
Definition: ctf.h:108
QsortLargeToSmallParticle
int QsortLargeToSmallParticle(const void *x, const void *y)
Definition: mg_particle_select.cpp:504
PARTICLE_PHI
#define PARTICLE_PHI
Definition: mg_tags.h:201
project_fom_order
int_float * project_fom_order(Bproject *project, long &npart, int fom_index, int defocus_fit)
Definition: mg_particle_select.cpp:24
Bparticle::loc
Vector3< double > loc
Definition: mg_processing.h:100
Euler::phi
double phi()
Definition: Euler.h:58
PARTICLE_HANDB_FOM
#define PARTICLE_HANDB_FOM
Definition: mg_tags.h:231
mg_tags.h
All STAR and XML file format tags for micrographs and reconstructions.
M_PI
#define M_PI
Definition: utilities.h:63
part_origin_select
long part_origin_select(Bproject *project, Vector3< double > origin, double distance)
Selects particles based on the distance from the nominal origin.
Definition: mg_particle_select.cpp:2291
utilities.h
Header file for general utilities.
project_part_sort
vector< pair< Bparticle *, double > > project_part_sort(Bproject *project, Bstring tag)
Sorts the Particles by a selected parameter.
Definition: mg_particle_select.cpp:2842
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
part_select_furthest_from_focus
long part_select_furthest_from_focus(Bproject *project)
Deselects all particles except those from micrographs furthest from focus.
Definition: mg_particle_select.cpp:2699
PARTICLE_THETA
#define PARTICLE_THETA
Definition: mg_tags.h:200
part_fom_defocus_fit_deselect
double part_fom_defocus_fit_deselect(Bproject *project, int fom_index, double cutoff)
Deselects particles below a given FOM cutoff, adjusted for the defocus.
Definition: mg_particle_select.cpp:2644
Bmicrograph::box_size
Vector3< long > box_size
Definition: mg_processing.h:233
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
PARTICLE_ID
#define PARTICLE_ID
Definition: mg_tags.h:184
part_set_filament_maps
long part_set_filament_maps(Bproject *project)
Generates a selection number from the group number for each filament.
Definition: mg_particle_select.cpp:645
PARTICLE_MAGNIF
#define PARTICLE_MAGNIF
Definition: mg_tags.h:207
Breconstruction
Definition: mg_processing.h:283
Bstring::length
long length() const
Definition: Bstring.cpp:263
Breconstruction::select
long select
Definition: mg_processing.h:286
PARTICLE_VIEW_ANGLE
#define PARTICLE_VIEW_ANGLE
Definition: mg_tags.h:206
part_set_sequential
long part_set_sequential(Bproject *project)
Selects one selection number and sets all others to zero.
Definition: mg_particle_select.cpp:352
Bparticle::def
double def
Definition: mg_processing.h:96
Bmicrograph::matrix
Matrix3 matrix
Definition: mg_processing.h:225
mg_ctf
int mg_ctf(Bmicrograph *mg, Bimage *pmg, int action, double lores, double hires, double wiener, Vector3< long > tile_size, DataType datatype, Bstring &newname, int flags)
Definition: mg_ctf.cpp:972
random_seed
long random_seed()
Gets a random seed and sets the flag.
Definition: random_numbers.cpp:41
part_invert_selection
long part_invert_selection(Bproject *project)
Inverts the selection.
Definition: mg_particle_select.cpp:204
part_reselect
int part_reselect(Bparticle *part, Bstring &tag, double reselect_min, double reselect_max)
Definition: mg_particle_select.cpp:690
particle_add
Bparticle * particle_add(Bparticle **part, int pid)
Adds a particle parameter structure to a linked list.
Definition: mg_processing.cpp:221
Bmicrograph::ctf
CTFparam * ctf
Definition: mg_processing.h:241
verbose
int verbose
Bparticle::dev
double dev
Definition: mg_processing.h:97
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
PARTICLE_VIEW_Z
#define PARTICLE_VIEW_Z
Definition: mg_tags.h:205
Bfield::next
Bfield * next
Definition: mg_processing.h:261
Bparticle::ori
Vector3< double > ori
Definition: mg_processing.h:101
Bparticle::group
int group
Definition: mg_processing.h:94
Matrix.h
Generalized matrix class.
mg_processing.h
Header file for micrograph processing.
View::quaternion
Quaternion quaternion()
Definition: View.h:130
PARTICLE_FOM
#define PARTICLE_FOM
Definition: mg_tags.h:216
CTFparam::defocus_average
double defocus_average()
Definition: ctf.h:106
Bparticle::next
Bparticle * next
Definition: mg_processing.h:91
PARTICLE_DEF_DEV
#define PARTICLE_DEF_DEV
Definition: mg_tags.h:213
linked_list.h
Header file for generalized linked list functions.
Bmicrograph::select
long select
Definition: mg_processing.h:204
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
CTF_DEF_DEV
#define CTF_DEF_DEV
Definition: mg_tags.h:173
Breconstruction::scale
Vector3< double > scale
Definition: mg_processing.h:297
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
particle_copy
Bparticle * particle_copy(Bparticle **partlist, Bparticle *part)
Adds one particle onto a list of particle parameters.
Definition: mg_processing.cpp:614
part_select_random_group
long part_select_random_group(Bproject *project, long number)
Selects groups randomly to give at least a number of particles.
Definition: mg_particle_select.cpp:1292
Bsymmetry::op
Bsymop * op
Definition: symmetry.h:43
Bmicrograph::pixel_size
Vector3< double > pixel_size
Definition: mg_processing.h:215
irm
double irm(1.0L/get_rand_max())
QsortLargeToSmallIntFloat
int QsortLargeToSmallIntFloat(const void *x, const void *y)
Utility function for sorting int float pairs in qsort.
Definition: qsort_functions.cpp:89
part_euler_angle_select
long part_euler_angle_select(Bproject *project, double *euler6)
Selects particles based on Euler angle limits.
Definition: mg_particle_select.cpp:2229
View::negate
void negate()
Definition: View.h:117
View::matrix
Matrix3 matrix()
Definition: View.h:153
get_rand_max
long get_rand_max()
Finds the maximum random number for a system.
Definition: random_numbers.cpp:22
part_select_best
long part_select_best(Bproject *project, long number, int fom_index, int defocus_fit)
Selects the best particles based on the FOM.
Definition: mg_particle_select.cpp:1052
PARTICLE_SELECT
#define PARTICLE_SELECT
Definition: mg_tags.h:215
Bsymmetry::label
Bstring label
Definition: symmetry.h:41
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
particle_kill
int particle_kill(Bparticle *part)
Frees a list of particles.
Definition: mg_processing.cpp:528
int_float::i
int i
Definition: qsort_functions.h:12
particle_count
long particle_count(Bparticle *part)
Counts the number of particles in a linked list.
Definition: mg_select.cpp:755
Bparticle::mag
double mag
Definition: mg_processing.h:95
PARTICLE_Y
#define PARTICLE_Y
Definition: mg_tags.h:191
part_select_group
long part_select_group(Bproject *project, int group)
Selects particles with a given group identifier.
Definition: mg_particle_select.cpp:1792
part_select_random_within_view
long part_select_random_within_view(Bproject *project, Bsymmetry *sym, double theta_step, double phi_step, int number)
Selects a given number of particles within each view.
Definition: mg_particle_select.cpp:1442
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:110
View::angle
double angle()
Definition: View.h:98
Euler
Definition: Euler.h:28
Bmicrograph::next
Bmicrograph * next
Definition: mg_processing.h:202
part_fom_order
int_float * part_fom_order(Bparticle *partlist, long &npart, int fom_index)
Definition: mg_particle_select.cpp:90
Bfield::mg
Bmicrograph * mg
Definition: mg_processing.h:267
part_select_percentage
long part_select_percentage(Bproject *project, double percentage, int fom_index, int defocus_fit)
Selects a percentage of particles based on the FOM.
Definition: mg_particle_select.cpp:995
select_numbers
int select_numbers(Bstring &string, int n, int *numsel)
Converts a string with a selection specification into an integer array.
Definition: utilities.cpp:335
Bparticle::id
int id
Definition: mg_processing.h:93
part_deselect_from_list
long part_deselect_from_list(Bproject *project, Bstring list)
Deselects particles with selection numbers from a list.
Definition: mg_particle_select.cpp:412
part_set_best_view_in_series
long part_set_best_view_in_series(Bproject *project, int fom_index)
Sets all orientation parameters for a particle to the best in the set.
Definition: mg_particle_select.cpp:2377
project_count_micrographs
long project_count_micrographs(Bproject *project)
Counts all the micrographs in the project structure.
Definition: mg_select.cpp:350
PARTICLE_ORIGIN_Z
#define PARTICLE_ORIGIN_Z
Definition: mg_tags.h:198
View::x
double x()
Definition: View.h:95
mg_tomography.h
Header file for single particle image processing in Bsoft.
part_select_FOM_groups
long part_select_FOM_groups(Bproject *project, int ngroups, int fom_index, int defocus_fit)
Selects groups of particles based on the FOM.
Definition: mg_particle_select.cpp:945
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