Bsoft  2.1.0
Bernard's software package
dmerge.cpp
Go to the documentation of this file.
1 
9 #include "mg_merge.h"
10 #include "rwmg.h"
11 #include "utilities.h"
12 #include "options.h"
13 #include "timer.h"
14 
15 // Global variables
16 extern int verbose; // Level of output to the screen
17 
18 const char* use[] = {
19 " ",
20 "Usage: dmerge [options] input1.star [input2.star...]",
21 "----------------------------------------------------",
22 "Merges corresponding single particle images in a series.",
23 "The images can be aligned according to their origin values and",
24 "the in-plane rotational angle (of the micrograph).",
25 " ",
26 "You may use the index number of a micrograph in a series, a relative focus",
27 "level, or an in-plane rotation angle of the micrograph to select a reference",
28 "micrograph--the one to which the others are aligned. The default is to use",
29 "the closest-to-focus micrograph in each field-of-view.",
30 " ",
31 "The default is to use origins from parameter file(s), while origins may also",
32 "be determined by cross-correlation or obtained from particle image files.",
33 " ",
34 "Default image output is to one file for every field named",
35 "'[reference_particle_file_prefix]_merged.img'.",
36 " ",
37 "Actions:",
38 "-unmerge merged.star Un-merge, corresponding particles in field get orientations,",
39 " figure-of-merit (FOM), and selection from 'merged.star',",
40 " micrograph rotation angle is accounted for",
41 " ",
42 "Parameters:",
43 "-verbose 7 Verbosity of screen output, default = 0 (none)",
44 "-sampling 1.5 Global sampling (angstrom/pixel)",
45 " ",
46 "Parameters for merging:",
47 "-origins cross Get particle origins from parameter files (param, default),",
48 " by cross-correlation (cross), from image files (image),",
49 " or no alignment (none)",
50 "-reference near Reference micrograph: near, nearest-to-focus; far, farthest-from-focus;",
51 " index,1, micrograph index; or angle,0, micrograph rotation angle",
52 " Note: the index option assumes micrographs were entered",
53 " into the parameter file(s) in consistent order",
54 " ",
55 "Parameters for unmerging:",
56 "-keeporient 0.1 Keep orientation, FOM, & selection if FOM difference >= this value",
57 " ",
58 "Output:",
59 "-output file.star Output parameter file",
60 "-summed output.img Write summed images to output*.img (* = _micrograph_ids), but only",
61 " if micrograph_ids are <= 6 characters, if not default name is used ",
62 " ",
63 NULL
64 };
65 
66 
67 
68 int main(int argc, char** argv)
69 {
70  Vector3<double> sam; // For resetting the grid size
71  Bproject* orientations = NULL; // Data for orientations to be applied to all in field
72  double fom_diff(-1.0); // FOM-difference threshold to not change (for un-merging option)
73  int mg_ori_select(0); // Flag for getting origins (default use origins in parameter file)
74  int mg_ref_select(0); // Reference micrograph flag (default is closest-to-focus)
75  int mg_index(0); // Reference micrograph index
76  double mg_rot_ang(0); // Reference micrograph rotation angle
77  Bstring orientfile; // Parameter file with orientations to be applied to all in field
78  Bstring outimg; // Filename prefix and suffix for output images
79  Bstring outfile; // Output parameter file
80 
81  int optind;
82  Boption* option = get_option_list(use, argc, argv, optind);
83  Boption* curropt;
84  for ( curropt = option; curropt; curropt = curropt->next ) {
85  if ( curropt->tag == "origins" ) { // Source of origins
86  if ( curropt->value.contains("no") ) mg_ori_select = -1;
87  if ( curropt->value.contains("par") ) mg_ori_select = 0;
88  if ( curropt->value.contains("cro") ) mg_ori_select = 1;
89  if ( curropt->value.contains("im") ) mg_ori_select = 2;
90  }
91  if ( curropt->tag == "keeporient" ) { // do not change orient. in certain cases (only with unmerge option)
92  if ( ( fom_diff = curropt->value.real() ) < 0.0001 )
93  cerr << "-keeporient: A FOM-difference threshold must be specified." << endl;
94  if ( fom_diff <= 0 )
95  cerr << "-keeporient: The FOM-difference threshold (" << fom_diff << ") must be greater than zero." << endl;
96  }
97  if ( curropt->tag == "sampling" ) // set grid size of images
98  sam = curropt->scale();
99  if ( curropt->tag == "reference" ) { // reference micrograph selection
100  if ( curropt->value.contains("near") ) mg_ref_select = 0;
101  else if ( curropt->value.contains("far") ) mg_ref_select = 1;
102  else if ( curropt->value.contains("ind") ) {
103  mg_ref_select = 2;
104  mg_index = curropt->value.post(',').integer();
105  } else if ( curropt->value.contains("ang") ) {
106  mg_ref_select = 3;
107  mg_rot_ang = curropt->value.post(',').real();
108  mg_rot_ang *= M_PI/180.0;
109  }
110  }
111  if ( curropt->tag == "summed" ) // output image file
112  outimg = curropt->filename();
113  if ( curropt->tag == "unmerge" ) // input file with orientations for all members of field
114  orientfile = curropt->filename();
115  if ( curropt->tag == "output" ) // output parameter file
116  outfile = curropt->filename();
117  }
118  option_kill(option);
119 
120  double ti = timer_start();
121 
122  if ( orientfile.length() && (fom_diff > 0) ) { // option to not change orientation, etc. values only works with un-merge option
123  cerr << "ERROR: You selected the 'unchange' option. It only works if the unmerge option also is used." << endl;
124  bexit(-1);
125  }
126 
127  // Read all the parameter files
128  Bstring* file_list = NULL;
129  while ( optind < argc ) string_add(&file_list, argv[optind++]);
130  if ( !file_list ) {
131  cerr << "Error: No parameter files specified!" << endl;
132  bexit(-1);
133  }
134 
135  Bproject* project = read_project(file_list);
136  string_kill(file_list);
137 
138  if ( project == NULL ) {
139  cerr << "Error: No input file read!" << endl;
140  bexit(-1);
141  }
142 
143  if ( sam[0] > 0.1 ) // Reset sampling size for project
145 
146 
147  // Align and sum images and write to a file or "un-merge" parameter data
148  if ( orientfile.length() ) {
149  orientations = read_project(orientfile);
150  mg_particle_unmerge(project, orientations, fom_diff);
151  } else
152  mg_particle_merge_series(project, mg_ref_select, mg_index, mg_rot_ang, mg_ori_select, outimg);
153 
154 
155  // Output parameters
156  if ( project && outfile.length() ) {
157  write_project(outfile, project, 0, 0);
158  }
159 
161 
162  if ( verbose & VERB_TIME ) timer_report(ti);
163 
164  if ( verbose & VERB_TIME )
165  timer_report(ti);
166 
167  bexit(0);
168 }
169 
Boption
A class for a command line input option.
Definition: options.h:26
project_kill
int project_kill(Bproject *project)
Frees a whole project.
Definition: mg_processing.cpp:429
Boption::scale
Vector3< double > scale()
Definition: options.h:190
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:768
Bstring
Definition: Bstring.h:29
mg_particle_merge_series
int mg_particle_merge_series(Bproject *project, int mg_ref_select, int mg_index, float mg_rot_ang, int mg_ori_select, Bstring outimg)
Aligns and merges (sums) 2D particle images from a focal, or other, series and writes to new image fi...
Definition: mg_merge.cpp:76
verbose
int verbose
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::next
Boption * next
Definition: options.h:28
Vector3< double >
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
get_option_list
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
main
int main(int argc, char **argv)
Definition: dmerge.cpp:68
Bstring::real
double real()
Definition: Bstring.cpp:553
options.h
Header file for option handlers.
Bstring::post
Bstring post(char c)
Definition: Bstring.cpp:402
project
Bproject * project
Definition: bshow.cpp:33
Boption::tag
Bstring tag
Definition: options.h:29
rwmg.h
Header file for reading and writing micrograph parameters.
project_set_mg_pixel_size
int project_set_mg_pixel_size(Bproject *project, Vector3< double > pixel_size)
Sets the pixel size for all the micrographs.
Definition: mg_processing.cpp:1997
Boption::value
Bstring value
Definition: options.h:30
timer.h
Utilities for timing functions.
mg_particle_unmerge
int mg_particle_unmerge(Bproject *project, Bproject *orientations, float fom_diff)
Gives corresponding particles in a field-of-view the same orientation, figure-of-merit (FOM),...
Definition: mg_merge.cpp:271
Bproject
Definition: mg_processing.h:336
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
mg_merge.h
Header file for functions to merge images.
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
read_project
Bproject * read_project(const char *filename, int flags=0)
Reading micrograph parameters from files.
Definition: rwmg.cpp:141
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
Bstring::length
long length() const
Definition: Bstring.cpp:263
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:76
write_project
int write_project(const char *filename, Bproject *project)
Writing micrograph parameters to a file.
Definition: rwmg.cpp:321
Boption::filename
Bstring filename()
Definition: options.h:85
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
use
const char * use[]
Definition: dmerge.cpp:18
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:713