28 #define DATLINELENGTH 120 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.",
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.",
64 "-output file.star Output parameter file.",
69 int main(
int argc,
char** argv)
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" )
88 cerr <<
"-mgselect: A number must be specified" << endl;
89 if ( curropt->
tag ==
"recselect" )
91 cerr <<
"-recselect: A number must be specified" << endl;
92 if ( curropt->
tag ==
"euler" )
94 if ( curropt->
tag ==
"magnification" )
96 if ( curropt->
tag ==
"omega" )
98 if ( curropt->
tag ==
"output" )
109 while ( optind < argc )
string_add(&file_list, argv[optind++]);
111 cerr <<
"Error: No parameter files specified!" << endl;
119 if ( project == NULL ) {
120 cerr <<
"Error: No project generated!" << endl;
124 if ( sampling > 0.1 )
149 cerr <<
"Error: No micrograph parameter filename!" << endl;
157 cout <<
"Parameter filenames:";
158 for ( thisfile = file_list; thisfile; thisfile = thisfile->
next )
159 cout <<
" " << *thisfile;
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) ;
184 cerr <<
"Error: The STAR file is not a valid form Bsoft can read!" << endl;
192 cerr <<
"Error: Extension " << ext <<
" not valid for parameter files!" << endl;
195 if ( !field ) field = project->
field;
196 else field = field->
next;
198 if ( !mg ) mg = field->
mg;
202 if ( !rec ) rec = project->
rec;
203 else rec = rec->
next;
206 cerr <<
" Parameter file " << *thisfile <<
" not read." << endl;
230 if ( filename.
empty() ) {
231 cerr <<
"Error: No micrograph parameter filename!" << endl;
250 cout <<
"Parameter filename: " << filename << endl;
261 cerr <<
"Error: Extension " << ext <<
" not valid for parameter files!" << endl;
262 cerr <<
" Parameter file " << filename <<
" not written." << endl;
281 int err(0), nx(0), ny(0), n(0);
282 char inputLine[1024], *aptr;
284 char unitsInHeaderC[10];
285 double px, volt, amp, def, dev, ast, cs;
294 if (
verbose ) cout <<
"Reading a DAT file: " << filename << endl;
299 ifstream fdat(filename.
c_str());
301 cerr <<
"File " << filename <<
" does not exist!. Exit program." << endl;
305 fdat.getline(inputLine, 1023);
306 mg->
fpart = inputLine;
311 cerr <<
"ERROR reading line 1 of PFT-format DAT file. Exit program" << endl;
316 sscanf(strchr(inputLine+mg->
fpart.
length(),
' '),
" %d %d %d", &nx, &ny, &n);
319 fdat.getline(inputLine, 1023);
320 if ( !fdat.good() ) {
321 cout <<
"ERROR reading line 2 of PFT-format DAT file. Exit program" << endl;
324 if ( sscanf(inputLine,
"%lf,%d,%lf,%lf,%lf,%lf,%lf,%lf\n", &px, &unitsInHeaderI,
325 &volt, &, &def, &dev, &ast, &cs) == 8) {
326 switch(unitsInHeaderI) {
328 strcpy(unitsInHeaderC,
"pixels");
331 strcpy(unitsInHeaderC,
"angstroms");
334 strcpy(unitsInHeaderC,
"nm");
338 }
else if ( sscanf(inputLine,
"%lf,%lf,%lf,%lf,%lf,%lf,%lf\n", &px, &volt,
339 &, &def, &dev, &ast, &cs) == 7) {
341 cout <<
"ERROR interpreting line 2 of PFT-format DAT file. Exit program" << endl;
345 if ( volt < 1000.0 ) volt *= 1000;
347 if ( def < 100 ) def *= 1e4;
348 if ( dev < 100 ) dev *= 1e4;
351 if ( dev < 0 ) dev = -dev;
364 float theta, phi, omega, ox, oy, mag, fom1, fom2, fom3;
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 ) {
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;
389 cout <<
"Number of particles read: " << numpart << endl << endl;
405 int i, f, numpart(0);
406 double def_min, def_max, omega;
415 if (
verbose ) cout <<
"Writing a DAT file: " << filename << endl;
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 ) {
422 for ( part = mg->
part; part; part = part->
next ) numpart++;
427 fdat.open(outname.
c_str());
429 cerr <<
"Unable to create file " << filename <<
"!. Exit program." << endl;
433 fdat << fixed << right << setprecision(4) << setw(9) << mg->
pixel_size <<
", " 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 <<
", " 440 << setprecision(2) << setw(7) << ctf->
Cs()/1e7 << endl;
441 for ( part = mg->
part; part; part = part->
next ) {
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];
460 cout <<
"Number of particles written: " << numpart << endl << endl;
487 if (
verbose ) cout <<
"Reading a CRD file: " << filename << endl;
492 ifstream fcrd(filename.
c_str());
494 cerr <<
"File " << filename <<
" does not exist!. Exit program." << endl;
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") ) {
509 }
else if ( strstr(line,
"OUTFILE_PREFIX") ) {
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") ) {
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);
522 }
else if ( strstr(line,
"RADIUS_PICK") ) {
523 sscanf(value,
"%lf", &d);
526 }
else if ( strstr(line,
"RADIUS_BAD") ) {
531 if ( part_prefix.
length() )
532 mg->
fpart = part_prefix +
"." + part_ext;
534 int numpart(0), numbad(0), pid;
538 if ( sscanf(line,
"%d %f %f", &pid, &x, &y) > 2 ) {
547 if ( !mg->
bad ) mg->
bad = bad;
558 cout <<
"Number of particles written: " << numpart <<
" (" << numbad <<
")" << endl << endl;
574 int i, f, numpart(0), numbad(0);
583 if (
verbose ) cout <<
"Writing a CRD file: " << filename << endl;
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 ) {
594 fcrd.open(outname.
c_str());
596 cerr <<
"Unable to create file " << filename <<
"!. Exit program." << endl;
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;
639 cout <<
"Number of particles written: " << numpart <<
" (" << numbad <<
")" << endl << endl;
649 double defU, defV, prev_defU(0), volt, Cs, amp;
655 Bstring field_id(
"1"), mg_id(
"1");
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 ) {
667 pfile = data->
post(
'@');
669 if ( !partlist ) partlist = part;
672 if ( ( item =
item_find(block,
"rlnDefocusU") ) ) {
673 for ( data=item->
data, part=partlist; data && part; data=data->
next, part=part->
next )
676 if ( ( item =
item_find(block,
"rlnDefocusV") ) ) {
677 for ( data=item->
data, part=partlist; data && part; data=data->
next, part=part->
next )
680 if ( ( item =
item_find(block,
"rlnDefocusAngle") ) ) {
681 for ( data=item->
data, part=partlist; data && part; data=data->
next, part=part->
next )
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;
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;
692 if ( ( item =
item_find(block,
"rlnAmplitudeContrast") ) ) {
693 for ( data=item->
data, part=partlist; data && part; data=data->
next, part=part->
next )
699 cout <<
"Particles: " << pid << endl;
702 for ( i=1, part = partlist; part; part = part->
next ) {
705 part->
def = (defU + defV)/2;
706 part->
dev = (defU - defV)/2;
707 if ( defU != prev_defU ) {
710 if ( !field->
mg ) field->
mg = mg;
721 if ( prev_part ) prev_part->
next = NULL;
728 cout <<
"Micrographs: " << mg_id.
integer() << endl;
731 unsigned long n1(0), n2(0);
735 for ( mg = field->
mg; mg; mg = mg->
next ) {
736 for ( part = mg->
part; part->
next; part = part->
next ) ;
739 for ( pid=1, part = mg->
part; part; part = part->
next, pid++ ) {
744 cout <<
"Extracting sub-images " << n1 <<
" - " << n2 << endl;
780 if (
verbose ) cout <<
"Reading a Relion file: " << filename << endl;
809 if (
verbose ) cout <<
"Writing a Relion file: " << filename << endl;
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;
FileType
File type enumeration.
Header file for reading and writing micrograph parameters.
int read_star(const char *filename, Bstar *star)
Reads paramaters and data into a STAR data base from a list of files.
int write_project_dat(Bstring &filename, Bproject *project, int mg_select, int rec_select)
Writing micrograph parameters to a DAT file.
int read_project_star(Bstring &filename, Bproject *project)
Reading micrograph parameters from STAR files.
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
int read_project_dat(Bstring &filename, Bproject *project)
Reading micrograph parameters from a DAT file.
Header file for general utilities.
double astigmatism_angle()
Bimage * read_img(Bstring filename, int readdata, int img_select)
General driver function to read multiple image formats.
int write_star(const char *filename, Bstar *star)
Writes a STAR data base to one or more STAR format files.
Bstar * init_star()
Creates a STAR data base.
FileType file_type(const char *filename)
Checks the file type using the extension and contents.
int project_update_first_zero(Bproject *project)
Updates the first zero from the defocus average for all the micrographs.
char * add_item(char **list, unsigned long size)
Adds an item to a linked list.
Bparticle * particle_add(Bparticle **part, int pid)
Adds a particle parameter structure to a linked list.
int error_show(const char *message, const char *file, int line)
Displays the error with file and line reference.
Bstring remove(const char c)
int kill_star(Bstar *star)
Destroys a STAR data base.
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.
Boption * get_option_list(const char *use[], int argc, char *argv[], int &optind)
Parses command line arguments based on a template.
int write_img(Bstring filename, Bimage *p)
General driver function to write multiple image formats.
Bproject * project_init()
Initializes a micrograph processing project parameter structure.
Utilities for timing functions.
General image parameter class.
bool contains(const Bstring &s)
Header file for generalized linked list functions.
Bimage * extract(long nn)
Extracts one sub-image into new image structure.
Reads and writes micrograph XML files.
int read_project_relion(Bstring &filename, Bproject *project)
Reading micrograph parameters from a Relion file.
A class for a command line input option.
Header file for reading and writing micrograph parameters from and to the STAR format.
double defocus_deviation()
int write_project_conv(Bstring &filename, Bproject *project, int mg_select, int rec_select)
Vector3< long > size() const
int read_project_xml(Bstring &filename, Bproject *project)
Reading micrograph parameters from XML files.
Header file for file checking functions.
Bfield * field_add(Bfield **field, Bstring &field_id)
Adds a field-of-view parameter structure to a linked list.
int write_project_star(const char *filename, Bproject *project, int mg_select, int rec_select)
Writing micrograph parameters to a STAR file.
int write_project_xml(Bstring &filename, Bproject *project, int mg_select, int rec_select)
Writing micrograph parameters to a XML file.
int project_kill(Bproject *project)
Frees a whole project.
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.
int relion_to_project(Bstar *star, Bproject *project)
Bproject * read_project_conv(Bstring *file_list)
int project_modify_parameters(Bproject *project, char flags)
Modifies some micrograph parameters.
int main(int argc, char **argv)
int option_kill(Boption *option)
Deallocates a linked list of option structures.
int write_project_relion(Bstring &filename, Bproject *project, int mg_select, int rec_select)
Writing micrograph parameters to a Relion file.
int project_to_relion(Bproject *project, Bstar *star, int mg_select, int rec_select)
Header file for micrograph processing.
Header file for option handlers.
int read_project_crd(Bstring &filename, Bproject *project)
Reading micrograph parameters from a CRD file.
int project_update_comment(Bproject *project, int n, char **strings)
Puts a set of strings and time in the main comment of a project.
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.
Bstar_item * item_find(Bstar_block *block, const char *tag)
Gets the STAR item associated with a tag in a STAR data block.
double timer_start()
Starts timer and prints the time.
void origin(double ox, double oy, double oz)
int string_kill(Bstring *list)
Kills a string linked list.