Bsoft  2.0.0
Bernard's software package
mgconvert.cpp
Go to the documentation of this file.
1 
17 #include "mg_processing.h"
18 #include "rwmg.h"
19 #include "rwmgSTAR.h"
20 #include "rwmgXML.h"
21 #include "mg_ctf.h"
22 #include "file_util.h"
23 #include "linked_list.h"
24 #include "utilities.h"
25 #include "options.h"
26 #include "timer.h"
27 
28 #define DATLINELENGTH 120
29 
30 // Declaration of global variables
31 extern int verbose; // Level of output to the screen
32 
33 // Function prototypes
35 int write_project_conv(Bstring& filename, Bproject* project, int mg_select, int rec_select);
36 int read_project_dat(Bstring& filename, Bproject* project);
37 int write_project_dat(Bstring& filename, Bproject* project, int mg_select, int rec_select);
38 int read_project_crd(Bstring& filename, Bproject* project);
39 int write_project_crd(Bstring& filename, Bproject* project, int mg_select, int rec_select);
41 int write_project_relion(Bstring& filename, Bproject* project, int mg_select, int rec_select);
43 
44 // Usage assistence
45 const char* use[] = {
46 " ",
47 "Usage: mgconvert [options] input.star [input2.star]",
48 "---------------------------------------------------",
49 "Converts micrograph parameter files.",
50 "DAT and CRD files only hold one micrograph per file:",
51 " multiple micrographs are written as numbered DAT or CRD files.",
52 " use -mgselect to select only one micrograph.",
53 " ",
54 "Parameters:",
55 "-verbose 7 Verbosity of output.",
56 "-sampling 1.5 Sampling (A/pixel).",
57 "-mgselect 5 Selected micrograph (default all, first micrograph = 1).",
58 "-recselect 5 Selected reconstruction (default all, first reconstruction = 1).",
59 "-euler Convert views to Euler angles.",
60 "-omega Add or subtract 90 degrees to or from the OMEGA angle.",
61 "-magnification Invert the magnification factor: 1/mag.",
62 " ",
63 "Output:",
64 "-output file.star Output parameter file.",
65 " ",
66 NULL
67 };
68 
69 int main(int argc, char** argv)
70 {
71  // Initializing variables
72  float sampling; // A/pixel
73  int mg_select(-1);
74  int rec_select(-1);
75  int euler(0); // Flag to specify Euler angles
76  char flags(0); // Flags to modify some input parameters
77  Bstring outfile; // Output parameter file
78 
79  int i, optind;
80  Boption* option = get_option_list(use, argc, argv, optind);
81  Boption* curropt;
82  for ( curropt = option; curropt; curropt = curropt->next ) {
83  if ( curropt->tag == "sampling" )
84  if ( ( sampling = curropt->value.real() ) < 0.1 )
85  cerr << "-sampling: A pixel size must be specified" << endl;
86  if ( curropt->tag == "mgselect" )
87  if ( ( mg_select = curropt->value.integer() ) < 0 )
88  cerr << "-mgselect: A number must be specified" << endl;
89  if ( curropt->tag == "recselect" )
90  if ( ( rec_select = curropt->value.integer() ) < 0 )
91  cerr << "-recselect: A number must be specified" << endl;
92  if ( curropt->tag == "euler" )
93  euler = 1;
94  if ( curropt->tag == "magnification" )
95  flags += 1;
96  if ( curropt->tag == "omega" )
97  flags += 2;
98  if ( curropt->tag == "output" )
99  outfile = curropt->filename();
100  }
101  option_kill(option);
102 
103  double ti;
104  if ( verbose & VERB_TIME )
105  ti = timer_start();
106 
107  // Read all the parameter files
108  Bstring* file_list = NULL;
109  while ( optind < argc ) string_add(&file_list, argv[optind++]);
110  if ( !file_list ) {
111  cerr << "Error: No parameter files specified!" << endl;
112  bexit(-1);
113  }
114 
115  Bproject* project = read_project_conv(file_list);
116 
117  string_kill(file_list);
118 
119  if ( project == NULL ) {
120  cerr << "Error: No project generated!" << endl;
121  bexit(-1);
122  }
123 
124  if ( sampling > 0.1 )
125  project_set_pixel_size(project, sampling);
126 
127  if ( flags )
128  project_modify_parameters(project, flags);
129 
130  if ( euler ) project->euler_flag = 1;
131 
132  // Write an output parameter file if a name is given
133  if ( outfile.length() ) {
134  project_update_comment(project, argc, argv);
135  write_project_conv(outfile, project, mg_select, rec_select);
136  }
137 
138  project_kill(project);
139 
140  if ( verbose & VERB_TIME )
141  timer_report(ti);
142 
143  bexit(0);
144 }
145 
147 {
148  if ( !file_list ) {
149  cerr << "Error: No micrograph parameter filename!" << endl;
150  return NULL;
151  }
152 
153  Bstring ext;
154  Bstring* thisfile;
155 
156  if ( verbose & VERB_LABEL ) {
157  cout << "Parameter filenames:";
158  for ( thisfile = file_list; thisfile; thisfile = thisfile->next )
159  cout << " " << *thisfile;
160  cout << endl;
161  }
162 
163  int err(0);
164  FileType type;
166  Bfield* field = NULL;
167  Bmicrograph* mg = NULL;
168  Breconstruction* rec = NULL;
169  project->filename = *file_list;
170 
171  for ( thisfile = file_list; thisfile; thisfile = thisfile->next ) {
172  for ( field = project->field; field && field->next; field = field->next ) ;
173  if ( field ) for ( mg = field->mg; mg && mg->next; mg = mg->next ) ;
174  for ( rec = project->rec; rec && rec->next; rec = rec->next) ;
175  ext = thisfile->extension();
176  err = -1;
177  if ( ext.contains("star") ) {
178  type = file_type(*thisfile);
179  if ( type == Micrograph )
180  err = read_project_star(*thisfile, project);
181  else if ( type == MgRelion )
182  err = read_project_relion(*thisfile, project);
183  else
184  cerr << "Error: The STAR file is not a valid form Bsoft can read!" << endl;
185  } else if ( ext.contains("xml") ) {
186  err = read_project_xml(*thisfile, project);
187  } else if ( ext.contains("dat") ) {
188  err = read_project_dat(*thisfile, project);
189  } else if ( ext.contains("crd") ) {
190  err = read_project_crd(*thisfile, project);
191  } else {
192  cerr << "Error: Extension " << ext << " not valid for parameter files!" << endl;
193  }
194  if ( !err ) {
195  if ( !field ) field = project->field;
196  else field = field->next;
197  if ( field ) {
198  if ( !mg ) mg = field->mg;
199  else mg = mg->next;
200 // if ( mg ) field_resolve_file_access(field, mg, *thisfile);
201  }
202  if ( !rec ) rec = project->rec;
203  else rec = rec->next;
204 // if ( rec ) reconstruction_resolve_file_access(rec, *thisfile);
205  } else {
206  cerr << " Parameter file " << *thisfile << " not read." << endl;
207  }
208  }
209 
210  if ( err < 0 ) {
211  error_show(file_list->c_str(), __FILE__, __LINE__);
212  project_kill(project);
213  return NULL;
214  }
215 
216  project_update_first_zero(project);
217 
218 // project_check(project);
219 
220 // if ( verbose & VERB_PROCESS )
221 // project_display_counts(project);
222 
223  return project;
224 }
225 
226 int write_project_conv(Bstring& filename, Bproject* project, int mg_select, int rec_select)
227 {
228  int err(0);
229 
230  if ( filename.empty() ) {
231  cerr << "Error: No micrograph parameter filename!" << endl;
232  return -1;
233  }
234 
235  project_update_first_zero(project);
236 
237 // project_check(project);
238 
239 // if ( verbose & VERB_PROCESS )
240 // project_display_counts(project);
241 
242  Bstring ext = filename.extension();
243 
244  if ( ext.contains("star") && project->split == 9 ) {
245  err = write_project_star(filename, project, mg_select, rec_select);
246  return err;
247  }
248 
249  if ( verbose & VERB_LABEL )
250  cout << "Parameter filename: " << filename << endl;
251 
252  if ( ext.contains("star") ) {
253  err = write_project_star(filename, project, mg_select, rec_select);
254  } else if ( ext.contains("xml") ) {
255  err = write_project_xml(filename, project, mg_select, rec_select);
256  } else if ( ext.contains("dat") ) {
257  err = write_project_dat(filename, project, mg_select, rec_select);
258  } else if ( ext.contains("crd") ) {
259  err = write_project_crd(filename, project, mg_select, rec_select);
260  } else {
261  cerr << "Error: Extension " << ext << " not valid for parameter files!" << endl;
262  cerr << " Parameter file " << filename << " not written." << endl;
263  err = -1;
264  }
265 
266  if ( err < 0 )
267  error_show(filename.c_str(), __FILE__, __LINE__);
268 
269  return err;
270 }
271 
272 
280 {
281  int err(0), nx(0), ny(0), n(0);
282  char inputLine[1024], *aptr;
283  int unitsInHeaderI;
284  char unitsInHeaderC[10]; // "angstrom", "nm" or "pixel"
285  double px, volt, amp, def, dev, ast, cs;
286 
287  Bstring field_id = filename.base();
288  Bstring mg_id = field_id;
289  Bfield* field = field_add(&project->field, field_id);
290  Bmicrograph* mg = micrograph_add(&field->mg, mg_id);
291  Bparticle* part = NULL;
292  CTFparam* ctf = mg->ctf = new CTFparam;
293 
294  if ( verbose ) cout << "Reading a DAT file: " << filename << endl;
295 
296  /*========================
297  Read DAT-format file
298  ========================*/
299  ifstream fdat(filename.c_str());
300  if ( fdat.fail() ) {
301  cerr << "File " << filename << " does not exist!. Exit program." << endl;
302  bexit(-1);
303  }
304 
305  fdat.getline(inputLine, 1023);
306  mg->fpart = inputLine;
307  mg->fpart = mg->fpart.no_lead_space();
308  mg->fpart = mg->fpart.pre(' ');
309  mg->fpart = mg->fpart.remove('\n');
310  if ( mg->fpart.empty() ) {
311  cerr << "ERROR reading line 1 of PFT-format DAT file. Exit program" << endl;
312  exit(-1);
313  }
314 
315  // General micrograph parameters
316  sscanf(strchr(inputLine+mg->fpart.length(), ' '), " %d %d %d", &nx, &ny, &n);
317 // cout << "\"" << inputLine << "\"" << nx << " " << ny << " " << n << endl;
318 
319  fdat.getline(inputLine, 1023);
320  if ( !fdat.good() ) {
321  cout << "ERROR reading line 2 of PFT-format DAT file. Exit program" << endl;
322  exit(-1);
323  }
324  if ( sscanf(inputLine, "%lf,%d,%lf,%lf,%lf,%lf,%lf,%lf\n", &px, &unitsInHeaderI,
325  &volt, &amp, &def, &dev, &ast, &cs) == 8) {
326  switch(unitsInHeaderI) {
327  case 0:
328  strcpy(unitsInHeaderC, "pixels");
329  break;
330  case 1:
331  strcpy(unitsInHeaderC, "angstroms");
332  break;
333  case 2:
334  strcpy(unitsInHeaderC, "nm");
335  default:
336  break;
337  }
338  } else if ( sscanf(inputLine, "%lf,%lf,%lf,%lf,%lf,%lf,%lf\n", &px, &volt,
339  &amp, &def, &dev, &ast, &cs) == 7) {
340  } else {
341  cout << "ERROR interpreting line 2 of PFT-format DAT file. Exit program" << endl;
342  exit(-1);
343  }
344 
345  if ( volt < 1000.0 ) volt *= 1000; // Volt units for STAR-format file
346  cs *= 10000000; // Angstrom units for STAR-format file
347  if ( def < 100 ) def *= 1e4; // Angstrom units for STAR-format file
348  if ( dev < 100 ) dev *= 1e4; // Angstrom units for STAR-format file
349  def = (def + dev)/2;
350  dev -= def;
351  if ( dev < 0 ) dev = -dev;
352 
353  if ( nx && ny ) mg->box_size = Vector3<int>(nx, ny, 1);
354  mg->pixel_size = px;
355  ctf->volt(volt);
356  ctf->amp_fac(amp);
357  ctf->defocus_average(def);
358  ctf->defocus_deviation(dev);
359  ctf->astigmatism_angle(ast*M_PI/180.0);
360  ctf->Cs(cs);
361 
362  char line[DATLINELENGTH];
363  int numpart(0), pid;
364  float theta, phi, omega, ox, oy, mag, fom1, fom2, fom3;
365 
366  while ( fdat.getline(line, DATLINELENGTH) && fdat.good() ) {
367  if ( sscanf(line, "%d,%f,%f,%f,%f,%f,%f,%f,%f,%f", &pid, &theta, &phi, &omega, &ox, &oy, &mag, &fom1, &fom2, &fom3) > 6 ) {
368  part = particle_add(&part, pid);
369  if ( !mg->part ) mg->part = part;
370  part->ori[0] = ox;
371  part->ori[1] = oy;
372  part->mag = mag;
373  part->fom[0] = fom1;
374  part->fom[1] = fom2;
375  part->fom[2] = fom3;
376  part->view = Euler(-omega*M_PI/180.0,
377  theta*M_PI/180.0, phi*M_PI/180.0).view();
378  numpart++;
379  }
380  }
381 
382  fdat.close(); // close input DAT file
383 
384  if ( n && numpart != n )
385  cerr << "Warning: The number of particles (" << numpart <<
386  ") does not agree with the number in the header (" << n << ")" << endl;
387 
388  if ( verbose )
389  cout << "Number of particles read: " << numpart << endl << endl;
390 
391  return err;
392 }
393 
402 int write_project_dat(Bstring& filename, Bproject* project, int mg_select, int rec_select)
403 {
404  int err(0);
405  int i, f, numpart(0);
406  double def_min, def_max, omega;
407  Euler euler;
408  Bfield* field = NULL;
409  Bmicrograph* mg = NULL;
410  Bparticle* part = NULL;
411  CTFparam* ctf;
412  ofstream fdat;
413  Bstring outname = filename;
414 
415  if ( verbose ) cout << "Writing a DAT file: " << filename << endl;
416 
417  if ( project_count_micrographs(project) < 2 ) mg_select = 1;
418 
419  for ( i=1, field = project->field; field; field = field->next ) {
420  for ( mg = field->mg; mg; mg = mg->next, i++ ) if ( mg_select < 1 || mg_select == i ) {
421  if ( mg_select < 1 ) outname = filename.pre_rev('.') + Bstring(i, "_%06d.") + filename.post_rev('.');
422  for ( part = mg->part; part; part = part->next ) numpart++;
423  ctf = mg->ctf;
424  if ( !ctf ) ctf = mg->ctf = new CTFparam;
425  def_min = ctf->defocus_average() - ctf->defocus_deviation();
426  def_max = ctf->defocus_average() + ctf->defocus_deviation();
427  fdat.open(outname.c_str());
428  if ( fdat.fail() ) { // make sure input file exists
429  cerr << "Unable to create file " << filename << "!. Exit program." << endl;
430  bexit(-1);
431  }
432  fdat << mg->fpart << " " << mg->box_size.x() << " " << mg->box_size.y() << " " << numpart << endl;
433  fdat << fixed << right << setprecision(4) << setw(9) << mg->pixel_size << ", "
434  << 1 << ", "
435  << setprecision(1) << setw(8) << ctf->volt() << ", "
436  << setprecision(4) << setw(10) << ctf->amp_fac() << ", "
437  << setprecision(4) << setw(9) << def_max/1e4 << ", "
438  << setprecision(4) << setw(9) << def_min/1e4 << ", "
439  << setprecision(3) << setw(9) << ctf->astigmatism_angle()*180.0/M_PI << ", "
440  << setprecision(2) << setw(7) << ctf->Cs()/1e7 << endl;
441  for ( part = mg->part; part; part = part->next ) {
442  euler = Euler(part->view);
443  omega = -euler.psi()*180.0/M_PI;
444  if ( omega < 0 ) omega += 360;
445  fdat << setw(7) << part->id << ", "
446  << setprecision(3) << setw(7) << euler.theta()*180.0/M_PI << ", "
447  << setprecision(3) << setw(7) << euler.phi()*180.0/M_PI << ", "
448  << setprecision(3) << setw(7) << omega << ", "
449  << setprecision(3) << setw(7) << part->ori.x() << ", "
450  << setprecision(3) << setw(7) << part->ori.y() << ", "
451  << setprecision(3) << setw(5) << part->mag;
452  for ( f=0; f<3; f++ ) fdat << ", " << setprecision(3) << setw(5) << part->fom[f];
453  fdat << endl;
454  }
455  fdat.close();
456  }
457  }
458 
459  if ( verbose )
460  cout << "Number of particles written: " << numpart << endl << endl;
461 
462  return err;
463 }
464 
472 {
473  int err(0);
474  char line[DATLINELENGTH];
475  char* value;
476  double d;
477 
478  Bstring field_id = filename.base();
479  Bstring mg_id = field_id;
480  Bfield* field = field_add(&project->field, field_id);
481  Bmicrograph* mg = micrograph_add(&field->mg, mg_id);
482  Bparticle* part = NULL;
483  Bbadarea* bad = NULL;
484  Bstring part_prefix;
485  Bstring part_ext;
486 
487  if ( verbose ) cout << "Reading a CRD file: " << filename << endl;
488 
489  /*========================
490  Read CRD-format file
491  ========================*/
492  ifstream fcrd(filename.c_str());
493  if ( fcrd.fail() ) {
494  cerr << "File " << filename << " does not exist!. Exit program." << endl;
495  bexit(-1);
496  }
497 
498  fcrd.getline(line, DATLINELENGTH);
499 
500  // General micrograph parameters
501  while ( fcrd.getline(line, DATLINELENGTH) && !strstr(line, "$END") ) {
502  value = strchr(line, '=') + 1;
503  while ( isspace(value[0]) ) value++;
504  if ( strstr(line, "PIC_FILENAME") ) {
505  mg->fmg = value;
506  mg->fmg = mg->fmg.remove('\n');
507  mg->fmg = mg->fmg.remove(',');
508  mg->fmg = mg->fmg.remove('\'');
509  } else if ( strstr(line, "OUTFILE_PREFIX") ) {
510  part_prefix = value;
511  part_prefix = part_prefix.remove('\n');
512  part_prefix = part_prefix.remove(',');
513  part_prefix = part_prefix.remove('\'');
514  } else if ( strstr(line, "OUTFILE_TYPE") ) {
515  part_ext = value;
516  part_ext = part_ext.remove('\n');
517  part_ext = part_ext.remove(',');
518  part_ext = part_ext.remove('\'');
519  } else if ( strstr(line, "ANGSTROMS") ) {
520  sscanf(value, "%lf", &d);
521  if ( d > 0 ) mg->pixel_size = d;
522  } else if ( strstr(line, "RADIUS_PICK") ) {
523  sscanf(value, "%lf", &d);
524  mg->box_size[0] = mg->box_size[1] = 2*d;
525  mg->box_size[2] = 1;
526  } else if ( strstr(line, "RADIUS_BAD") ) {
527  sscanf(value, "%f", &mg->bad_radius);
528  }
529  }
530 
531  if ( part_prefix.length() )
532  mg->fpart = part_prefix + "." + part_ext;
533 
534  int numpart(0), numbad(0), pid;
535  float x, y;
536 
537  while ( fcrd.getline(line, DATLINELENGTH) ) {
538  if ( sscanf(line, "%d %f %f", &pid, &x, &y) > 2 ) {
539  if ( pid > 0 ) {
540  part = particle_add(&part, pid);
541  if ( !mg->part ) mg->part = part;
542  part->loc[0] = x;
543  part->loc[1] = y;
544  numpart++;
545  } else {
546  bad = (Bbadarea *) add_item((char **) &bad, sizeof(Bbadarea));
547  if ( !mg->bad ) mg->bad = bad;
548  bad->loc[0] = x;
549  bad->loc[1] = y;
550  numbad++;
551  }
552  }
553  }
554 
555  fcrd.close(); // close input CRD file
556 
557  if ( verbose )
558  cout << "Number of particles written: " << numpart << " (" << numbad << ")" << endl << endl;
559 
560  return err;
561 }
562 
571 int write_project_crd(Bstring& filename, Bproject* project, int mg_select, int rec_select)
572 {
573  int err(0);
574  int i, f, numpart(0), numbad(0);
575  Bfield* field = NULL;
576  Bmicrograph* mg = NULL;
577  Bparticle* part = NULL;
578  Bbadarea* bad = NULL;
579  ofstream fcrd;
580  Bstring outname = filename;
581  Bstring prefix, type;
582 
583  if ( verbose ) cout << "Writing a CRD file: " << filename << endl;
584 
585  if ( project_count_micrographs(project) < 2 ) mg_select = 1;
586 
587  for ( i=1, field = project->field; field; field = field->next ) {
588  for ( mg = field->mg; mg; mg = mg->next, i++ ) if ( mg_select < 1 || mg_select == i ) {
589  if ( mg_select < 1 ) outname = filename.pre_rev('.') + Bstring(i, "_%06d.") + filename.post_rev('.');
590  if ( mg->fpart.length() ) {
591  prefix = mg->fpart.pre_rev('.');
592  type = mg->fpart.post_rev('.');
593  }
594  fcrd.open(outname.c_str());
595  if ( fcrd.fail() ) { // make sure input file exists
596  cerr << "Unable to create file " << filename << "!. Exit program." << endl;
597  bexit(-1);
598  }
599  fcrd << " $TRIMNEWPARAMETERS" << endl;
600  fcrd << " CRD_VERSION = 3," << endl;
601  fcrd << " PIC_FILENAME = '" << mg->fmg << "'," << endl;
602  fcrd << " PIC_FLIPFLAG = F," << endl;
603  fcrd << " FLIPALLROWSFLAG = F," << endl;
604  fcrd << " FLIPALLCOLSFLAG = F," << endl;
605  fcrd << " INVERT_INTENSTY = F," << endl;
606  fcrd << " ANGSTROMS = " << mg->pixel_size << "," << endl;
607  fcrd << " CONTRAST = 1.380000" << endl;
608  fcrd << " BRIGHTNESS = 0.120000" << endl;
609  fcrd << " OUTFILE_PREFIX = '" << prefix << "'," << endl;
610  fcrd << " OUTFILE_START = 1," << endl;
611  fcrd << " OUTFILE_WIDTH = 3," << endl;
612  fcrd << " OUTFILE_TYPE = '" << type << "'," << endl;
613  fcrd << " OD_CONVERT = F," << endl;
614  fcrd << " RADIUS = 50," << endl;
615  fcrd << " RADIUS_BAD = " << mg->bad_radius << "," << endl;
616  fcrd << " RADIUS_PICK = " << mg->box_size.x()/2.0 << "," << endl;
617  fcrd << " FADE = 9," << endl;
618  fcrd << " EXTRACT_DX = 501," << endl;
619  fcrd << " EXTRACT_DY = 501," << endl;
620  fcrd << " FINAL_DX = 401," << endl;
621  fcrd << " FINAL_DY = 401," << endl;
622  fcrd << " FINAL_MEAN = 127," << endl;
623  fcrd << " FINAL_STDDEV = 40," << endl;
624  fcrd << " SKIPMASKFLAG = F," << endl;
625  fcrd << " SKIPGRADIENTFLAG= T" << endl;
626  fcrd << " SELECTEXTENDED = T" << endl;
627  fcrd << " SELECTEXTLENMODE= FIXED" << endl;
628  fcrd << " SELECTEXTFLENGTH= 40" << endl;
629  fcrd << " $END" << endl;
630  for ( part = mg->part; part; part = part->next, numpart++ )
631  fcrd << right << setw(6) << part->id << setw(8) << part->loc.x() << setw(8) << part->loc.y() << endl;
632  for ( bad = mg->bad; bad; bad = bad->next, numbad++ )
633  fcrd << right << setw(6) << -numbad-1 << setw(8) << bad->loc.x() << setw(8) << bad->loc.y() << endl;
634  fcrd.close();
635  }
636  }
637 
638  if ( verbose )
639  cout << "Number of particles written: " << numpart << " (" << numbad << ")" << endl << endl;
640 
641  return err;
642 }
643 
645 {
646  int err(0);
647  long i, pid;
648  Bstring pfile;
649  double defU, defV, prev_defU(0), volt, Cs, amp;
650 
651  Bstar_block* block;
652  Bstar_item* item;
653  Bstring* data;
654 
655  Bstring field_id("1"), mg_id("1");
656  Bfield* field = field_add(&project->field, field_id);
657  Bmicrograph* mg = NULL;
658  Bparticle* partlist = NULL;
659  Bparticle* part = NULL;
660  Bparticle* prev_part = NULL;
661 
662 
663  for ( block = star->block; block; block = block->next ) {
664  if ( ( item = item_find(block, "rlnImageName") ) ) {
665  for ( data=item->data; data; data=data->next ) {
666  pid = data->pre('@').integer();
667  pfile = data->post('@');
668  part = particle_add(&part, pid);
669  if ( !partlist ) partlist = part;
670  }
671  }
672  if ( ( item = item_find(block, "rlnDefocusU") ) ) {
673  for ( data=item->data, part=partlist; data && part; data=data->next, part=part->next )
674  part->def = data->real();
675  }
676  if ( ( item = item_find(block, "rlnDefocusV") ) ) {
677  for ( data=item->data, part=partlist; data && part; data=data->next, part=part->next )
678  part->dev = data->real();
679  }
680  if ( ( item = item_find(block, "rlnDefocusAngle") ) ) {
681  for ( data=item->data, part=partlist; data && part; data=data->next, part=part->next )
682  part->ast = data->real() * M_PI/180.0;
683  }
684  if ( ( item = item_find(block, "rlnVoltage") ) ) {
685  for ( data=item->data, part=partlist; data && part; data=data->next, part=part->next )
686  volt = data->real() * 1e3;
687  }
688  if ( ( item = item_find(block, "rlnSphericalAberration") ) ) {
689  for ( data=item->data, part=partlist; data && part; data=data->next, part=part->next )
690  Cs = data->real() * 1e7;
691  }
692  if ( ( item = item_find(block, "rlnAmplitudeContrast") ) ) {
693  for ( data=item->data, part=partlist; data && part; data=data->next, part=part->next )
694  amp = data->real();
695  }
696  }
697 
698  if ( verbose )
699  cout << "Particles: " << pid << endl;
700 
701  // Set up the micrographs
702  for ( i=1, part = partlist; part; part = part->next ) {
703  defU = part->def;
704  defV = part->dev;
705  part->def = (defU + defV)/2;
706  part->dev = (defU - defV)/2;
707  if ( defU != prev_defU ) {
708  mg_id = Bstring(i++, "%04d");
709  mg = micrograph_add(&mg, mg_id);
710  if ( !field->mg ) field->mg = mg;
711  mg->fpart = pfile;
712  mg->ctf = new CTFparam;
713  mg->ctf->volt(volt);
714  mg->ctf->Cs(Cs);
715  mg->ctf->amp_fac(amp);
716  mg->ctf->defocus_average(part->def);
717  mg->ctf->defocus_deviation(part->dev);
718  mg->ctf->astigmatism_angle(part->ast);
719  mg->ctf->zero(1);
720  mg->part = part;
721  if ( prev_part ) prev_part->next = NULL;
722  }
723  prev_defU = defU;
724  prev_part = part;
725  }
726 
727  if ( verbose )
728  cout << "Micrographs: " << mg_id.integer() << endl;
729 
730  // Reconfigure the particle files
731  unsigned long n1(0), n2(0);
732  Bimage* p = read_img(pfile, 1, -1);
733  Bimage* pex;
734 
735  for ( mg = field->mg; mg; mg = mg->next ) {
736  for ( part = mg->part; part->next; part = part->next ) ;
737  n1 = mg->part->id - 1;
738  n2 = part->id - 1;
739  for ( pid=1, part = mg->part; part; part = part->next, pid++ ) {
740  part->id = pid;
741  part->ori = p->size()/2;
742  }
743  if ( verbose )
744  cout << "Extracting sub-images " << n1 << " - " << n2 << endl;
745  pex = p->extract(n1, n2);
746  pex->origin(p->size()/2);
747  mg->fpart = pfile.pre_rev('.') + "_" + mg->id + ".pif";
748  mg->pixel_size = p->sampling().x();
749  mg->box_size = p->size();
750  write_img(mg->fpart, pex);
751  delete pex;
752  }
753 
754  delete p;
755 
756  return err;
757 }
758 
759 int project_to_relion(Bproject* project, Bstar* star, int mg_select, int rec_select)
760 {
761  int err(0);
762 
763  star->comment = project->comment;
764 
765  return err;
766 }
767 
768 
776 {
777  int err(0);
778  Bstar* star = init_star();
779 
780  if ( verbose ) cout << "Reading a Relion file: " << filename << endl;
781 
782  err = read_star(filename, star);
783 
784  if ( err == 0 ) {
785  if ( project->comment.length() < 1 )
786  project->comment = star->comment;
787  err = relion_to_project(star, project);
788  }
789 
790  kill_star(star);
791 
792  return err;
793 }
794 
803 int write_project_relion(Bstring& filename, Bproject* project, int mg_select, int rec_select)
804 {
805  int err(0);
806  Bstar* star = init_star();
807  star->line_length = 160; // Set the output line length
808 
809  if ( verbose ) cout << "Writing a Relion file: " << filename << endl;
810 
811  err = project_to_relion(project, star, mg_select, rec_select);
812 
813  if ( err == 0 )
814  err = write_star(filename, star);
815 
816  kill_star(star);
817 
818  return err;
819 }
820 
821 
822 
830 {
831  Bfield* field = NULL;
832  Bmicrograph* mg = NULL;
833  Bparticle* part = NULL;
834 
835  for ( field = project->field; field; field = field->next ) {
836  for ( mg = field->mg; mg; mg = mg->next ) {
837  for ( part = mg->part; part; part = part->next ) {
838  if ( flags & 1 ) part->mag = 1.0/part->mag;
839  if ( flags & 2 ) part->view[3] -= M_PI_2;
840  }
841  }
842  }
843 
844  return 0;
845 }
FileType
File type enumeration.
Definition: file_util.h:20
Header file for reading and writing micrograph parameters.
Bstring * data
Definition: rwstar.h:43
int read_star(const char *filename, Bstar *star)
Reads paramaters and data into a STAR data base from a list of files.
Definition: rwstar.cpp:224
double zero(int i)
Definition: ctf.h:200
#define M_PI_2
Definition: utilities.h:56
double Cs()
Definition: ctf.h:76
int write_project_dat(Bstring &filename, Bproject *project, int mg_select, int rec_select)
Writing micrograph parameters to a DAT file.
Definition: mgconvert.cpp:402
double real()
Definition: Bstring.cpp:545
int read_project_star(Bstring &filename, Bproject *project)
Reading micrograph parameters from STAR files.
Definition: rwmgSTAR.cpp:29
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:730
Breconstruction * rec
Bstar_block * next
Definition: rwstar.h:56
Bstring post(char c)
Definition: Bstring.cpp:394
Definition: Euler.h:28
Bstring filename
Vector3< float > loc
Definition: mg_processing.h:83
int read_project_dat(Bstring &filename, Bproject *project)
Reading micrograph parameters from a DAT file.
Definition: mgconvert.cpp:279
Header file for general utilities.
double astigmatism_angle()
Definition: ctf.h:91
Bstring value
Definition: options.h:30
Bstring tag
Definition: options.h:29
Bimage * read_img(Bstring filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:82
int write_star(const char *filename, Bstar *star)
Writes a STAR data base to one or more STAR format files.
Definition: rwstar.cpp:666
Bstar * init_star()
Creates a STAR data base.
Definition: rwstar.cpp:111
FileType file_type(const char *filename)
Checks the file type using the extension and contents.
Definition: file_util.cpp:104
int project_update_first_zero(Bproject *project)
Updates the first zero from the defocus average for all the micrographs.
Definition: mg_ctf.cpp:1727
char * add_item(char **list, unsigned long size)
Adds an item to a linked list.
Definition: linked_list.cpp:26
Bparticle * particle_add(Bparticle **part, int pid)
Adds a particle parameter structure to a linked list.
long length() const
Definition: Bstring.cpp:263
Bproject * project
Definition: bshow.cpp:31
Definition: rwstar.h:78
int error_show(const char *message, const char *file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:687
Bstring remove(const char c)
Definition: Bstring.cpp:465
int kill_star(Bstar *star)
Destroys a STAR data base.
Definition: rwstar.cpp:132
long project_count_micrographs(Bproject *project)
Counts all the micrographs in the project structure.
Functions for CTF (contrast transfer function) processing.
int write_project_crd(Bstring &filename, Bproject *project, int mg_select, int rec_select)
Writing micrograph parameters to a CRD file.
Definition: mgconvert.cpp:571
Boption * get_option_list(const char *use[], int argc, char *argv[], int &optind)
Parses command line arguments based on a template.
Definition: options.cpp:40
int write_img(Bstring filename, Bimage *p)
General driver function to write multiple image formats.
Definition: rwimg.cpp:263
Bstring filename()
Definition: options.h:85
Bfield * next
Bproject * project_init()
Initializes a micrograph processing project parameter structure.
Bparticle * part
Utilities for timing functions.
General image parameter class.
Definition: Bimage.h:140
Bstring fpart
double amp_fac()
Definition: ctf.h:84
bool contains(const Bstring &s)
Definition: Bstring.cpp:584
#define M_PI
Definition: utilities.h:53
Header file for generalized linked list functions.
float pixel_size
CTFparam * ctf
Bimage * extract(long nn)
Extracts one sub-image into new image structure.
int line_length
Definition: rwstar.h:80
Reads and writes micrograph XML files.
double psi()
Definition: Euler.h:56
double phi()
Definition: Euler.h:58
Bbadarea * next
int read_project_relion(Bstring &filename, Bproject *project)
Reading micrograph parameters from a Relion file.
Definition: mgconvert.cpp:775
Bstring * next
Definition: Bstring.h:30
A class for a command line input option.
Definition: options.h:26
void sampling(double s)
Definition: Bimage.h:426
Bstring pre(char c)
Definition: Bstring.cpp:381
Bfield * field
Header file for reading and writing micrograph parameters from and to the STAR format.
#define VERB_TIME
Definition: utilities.h:104
Bstring extension()
Definition: Bstring.cpp:515
double defocus_deviation()
Definition: ctf.h:89
int write_project_conv(Bstring &filename, Bproject *project, int mg_select, int rec_select)
Definition: mgconvert.cpp:226
Vector3< long > size() const
Definition: Bimage.h:314
int read_project_xml(Bstring &filename, Bproject *project)
Reading micrograph parameters from XML files.
Definition: rwmgXML.cpp:144
Header file for file checking functions.
const char * c_str()
Definition: Bstring.h:68
Bstring post_rev(char c)
Definition: Bstring.cpp:428
Bmicrograph * mg
Bstring no_lead_space()
Definition: Bstring.cpp:341
Bfield * field_add(Bfield **field, Bstring &field_id)
Adds a field-of-view parameter structure to a linked list.
int verbose
const char * use[]
Definition: mgconvert.cpp:45
Bstring base()
Definition: Bstring.cpp:508
int write_project_star(const char *filename, Bproject *project, int mg_select, int rec_select)
Writing micrograph parameters to a STAR file.
Definition: rwmgSTAR.cpp:56
Vector3< float > loc
double theta()
Definition: Euler.h:57
Vector3< int > box_size
Boption * next
Definition: options.h:28
Type x()
Definition: Vector3.h:157
float fom[NFOM]
Definition: mg_processing.h:86
int write_project_xml(Bstring &filename, Bproject *project, int mg_select, int rec_select)
Writing micrograph parameters to a XML file.
Definition: rwmgXML.cpp:258
Bstring comment
Definition: rwstar.h:81
int project_kill(Bproject *project)
Frees a whole project.
double volt()
Definition: ctf.h:74
int project_set_pixel_size(Bproject *project, double pixel_size)
Sets the pixel size for all the micrographs.
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:67
int relion_to_project(Bstar *star, Bproject *project)
Definition: mgconvert.cpp:644
Bbadarea * bad
#define DATLINELENGTH
Definition: mgconvert.cpp:28
Bproject * read_project_conv(Bstring *file_list)
Definition: mgconvert.cpp:146
int project_modify_parameters(Bproject *project, char flags)
Modifies some micrograph parameters.
Definition: mgconvert.cpp:829
Bstar_block * block
Definition: rwstar.h:82
int main(int argc, char **argv)
Definition: mgconvert.cpp:69
double defocus_average()
Definition: ctf.h:87
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:109
int write_project_relion(Bstring &filename, Bproject *project, int mg_select, int rec_select)
Writing micrograph parameters to a Relion file.
Definition: mgconvert.cpp:803
View view()
Definition: Euler.h:62
long integer()
Definition: Bstring.cpp:539
int project_to_relion(Bproject *project, Bstar *star, int mg_select, int rec_select)
Definition: mgconvert.cpp:759
Header file for micrograph processing.
Header file for option handlers.
Type y()
Definition: Vector3.h:158
Bstring comment
int read_project_crd(Bstring &filename, Bproject *project)
Reading micrograph parameters from a CRD file.
Definition: mgconvert.cpp:471
Vector3< float > ori
Definition: mg_processing.h:84
Bstring pre_rev(char c)
Definition: Bstring.cpp:413
bool empty()
Definition: Bstring.h:69
int project_update_comment(Bproject *project, int n, char **strings)
Puts a set of strings and time in the main comment of a project.
Definition: rwmg.cpp:434
Bparticle * next
Definition: mg_processing.h:75
int euler_flag
#define VERB_LABEL
Definition: utilities.h:100
float bad_radius
Bmicrograph * micrograph_add(Bmicrograph **mg, Bstring &mg_id)
Adds a micrograph parameter structure to a linked list.
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:287
Breconstruction * next
Bstar_item * item_find(Bstar_block *block, const char *tag)
Gets the STAR item associated with a tag in a STAR data block.
Definition: rwstar.cpp:1051
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:52
void origin(double ox, double oy, double oz)
Definition: Bimage.h:469
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:785
Definition: ctf.h:25
Bmicrograph * next