Bsoft  2.1.0
Bernard's software package
bmgalign.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "mg_processing.h"
11 #include "rwmg.h"
12 #include "mg_pick.h"
13 #include "mg_align.h"
14 #include "Matrix.h"
15 #include "utilities.h"
16 #include "options.h"
17 #include "timer.h"
18 
19 // Declaration of global variables
20 extern int verbose; // Level of output to the screen
21 
22 // Usage assistance
23 const char* use[] = {
24 " ",
25 "Usage: bmgalign [options] input.star [input2.star...]",
26 "-----------------------------------------------------",
27 "Aligns micrographs within sets such as focal series specified in parameter files.",
28 " ",
29 "Actions:",
30 "-align mic Alignment type: par = particle coordinates, mic = micrograph images, fea = micrograph features.",
31 "-add Add micrograph coordinates and particle origins (default not).",
32 "-filterextremes Filter micrograph extremes before aligning.",
33 " ",
34 "Parameters:",
35 "-verbose 7 Verbosity of output.",
36 "-number 3 Number of micrographs per field-of-view (default 1).",
37 "-fieldID dbgd Set the field ID (multiple micrographs will get numbered ID's).",
38 "-reference 2 Reference micrograph in a series (default 1=first micrograph).",
39 "-datatype u Force writing of a new data type.",
40 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; a single value can be given).",
41 "-correlate 1024,1024,5 Size of subimages for correlation (default 512,512,1, use with -align mic).",
42 "-resolution 900,300 High and low resolution limits for bandpass (default 0.1,1000 angstrom).",
43 "-maxshift 55 Maximum allowed shift (default 1/4 of tile).",
44 "-threshold -3.0 Threshold used to pick features (in std dev units; default -3, use with -align fea).",
45 "-features 1,55 Features type (1=mass, 2=area, 3=darkest pixel) and maximum number (default 1,unlimited).",
46 " ",
47 "Output:",
48 "-output file.star Output parameter file.",
49 "-image file.pif Output multi-image file with aligned images.",
50 " ",
51 "Examples:",
52 "The set of input files reference a series of micrographs arranged in ",
53 "fields-of-view where each field contains a focal pair or set. The program then",
54 "calculates for each set the alignment of all other micrographs with respect",
55 "to the reference micrograph (typically the further-from-focus micrograph),",
56 "and outputs a new STAR format file with the alignment parameters and particle",
57 "coordinates corresponding to the alignment. The alignment can be done in",
58 "three different ways:",
59 "1. Alignment by cross-correlation (recommended):",
60 " bmgalign -v 7 -reference 2 -align mic -correlate 300,300,1 -out out.star input.star",
61 "2. Alignment by feature extraction:",
62 " bmgalign -v 7 -ref 2 -resol 300,1000 -align fea -thresh -2.8 -out out.star input.star",
63 "3. Alignment using picked particle coordinates:",
64 " bmgalign -v 7 -ref 2 -align part -output out.star input.star",
65 " ",
66 "All input is derived from a STAR format parameter file.",
67 "A series (such as a focal pair) is assumed to be consecutive data blocks in the STAR file.",
68 "Orientational parameters are calculated so that applying them to the first",
69 "data set or image produces the others in the focal series.",
70 "Angles are given with positive counter-clockwise.",
71 " ",
72 NULL
73 };
74 
75 int main(int argc, char* argv[])
76 {
77  // Initialize optional variables
78  DataType datatype(Unknown_Type); // Conversion to new type
79  Vector3<double> sam; // Units for the three axes (A/pixel)
80  int nseries(0); // Number of micrographs in a series from a field
81  Bstring field_id; // User-specified field ID
82  int ref_img(0); // Reference image in series (first is 1)
83  int add_coordinates(0); // For adding mg coordinates and particle origins
84  int set_align(0); // For alignment options
85  int filter_flag(0);
86 // int set_center(0); // Centering images before output
87  Vector3<long> tile_size(512,512,1); // Size of subimage to correlate
88  int max_features(1000000000); // Maximum number of features to extract
89  double resolution_low(1000); // Low resolution limit
90  double resolution_high(0.1); // High resolution limit
91  double max_shift(0);
92  double thresh(-3.0); // Threshold to find features
93  int extract_method(0); // Method for defining a feature
94  Bstring outfile; // Output parameter file name
95  Bstring imgfile; // Output multi-image file
96 
97  int optind;
98  Boption* option = get_option_list(use, argc, argv, optind);
99  Boption* curropt;
100  for ( curropt = option; curropt; curropt = curropt->next ) {
101  if ( curropt->tag == "align" ) {
102  if ( curropt->value.contains("par") ) set_align = 1;
103  else if ( curropt->value.contains("mic") ) set_align = 3;
104  else if ( curropt->value.contains("fea") ) set_align = 4;
105  else
106  cerr << "-align: An alignment method must be specified." << endl;
107  }
108  if ( curropt->tag == "filterextremes" )
109  filter_flag = 1;
110  if ( curropt->tag == "number" )
111  if ( ( nseries = curropt->value.integer() ) < 1 ) {
112  cerr << "-number: The number of micrographs in a field-of-view must be specified!" << endl;
113  bexit(-1);
114  }
115  if ( curropt->tag == "datatype" )
116  datatype = curropt->datatype();
117  if ( curropt->tag == "sampling" )
118  sam = curropt->scale();
119  if ( curropt->tag == "fieldID" ) {
120  field_id = curropt->value;
121  if ( field_id.length() < 1 )
122  cerr << "-fieldID: A field ID must be specified!" << endl;
123  }
124  if ( curropt->tag == "reference" )
125  if ( ( ref_img = curropt->value.integer() ) < 1 )
126  cerr << "-reference: The reference micrograph number in a series must be specified." << endl;
127  if ( curropt->tag == "add" )
128  add_coordinates = 1;
129  if ( curropt->tag == "correlate" ) {
130  tile_size = curropt->size();
131  if ( tile_size.volume() < 1 )
132  cerr << "-correlate: All three dimensions must be specified." << endl;
133  }
134  if ( curropt->tag == "features" )
135  if ( curropt->values(extract_method, max_features) < 1 )
136  cerr << "-features: A method for extraction must be specified." << endl;
137  if ( curropt->tag == "threshold" )
138  if ( ( thresh = curropt->value.real() ) < -3 )
139  cerr << "-threshold: A threshold must be specified." << endl;
140  if ( curropt->tag == "resolution" ) {
141  if ( curropt->values(resolution_high, resolution_low) < 1 )
142  cerr << "-resolution: Resolution limits must be specified." << endl;
143  else if ( resolution_high > resolution_low )
144  swap(resolution_high, resolution_low);
145  }
146  if ( curropt->tag == "maxshift" )
147  if ( ( max_shift = curropt->value.real() ) < 0.1 )
148  cerr << "-maxshift: A maximum shift in pixels must be specified." << endl;
149  if ( curropt->tag == "output" )
150  outfile = curropt->filename();
151  if ( curropt->tag == "image" )
152  imgfile = curropt->filename();
153  }
154  option_kill(option);
155 
156  double ti = timer_start();
157 
158  // Read all the parameter files
159  Bstring* file_list = NULL;
160  while ( optind < argc ) string_add(&file_list, argv[optind++]);
161  if ( !file_list ) {
162  cerr << "Error: No parameter files specified!" << endl;
163  bexit(-1);
164  }
165 
166  Bproject* project = read_project(file_list);
167  string_kill(file_list);
168 
169  if ( project == NULL ) {
170  cerr << "Error: No input file read!" << endl;
171  bexit(-1);
172  }
173 
174  if ( sam[0] > 0 )
176 
177  if ( nseries )
178  project_set_field_id(project, nseries, field_id);
179 
180  if ( add_coordinates )
182 
183  switch ( set_align ) {
184  case 1:
185  mg_align_coordinates(project, ref_img);
186  break;
187  case 2:
188  case 3:
189  mg_align_micrographs(project, ref_img, tile_size,
190  resolution_low, resolution_high, max_shift, filter_flag, set_align - 2);
191  break;
192  case 4:
193  mg_align_feature_extraction(project, max_features,
194  resolution_low, resolution_high, thresh, extract_method);
195  }
196 
197  if ( imgfile.length() )
198  project_write_aligned_images(project, NULL, imgfile, datatype);
199 
200  if ( project && outfile.length() ) {
201  write_project(outfile, project, 0, 0);
202  }
203 
205 
206  if ( verbose & VERB_TIME )
207  timer_report(ti);
208 
209  bexit(0);
210 }
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
main
int main(int argc, char *argv[])
Definition: bmgalign.cpp:75
use
const char * use[]
Definition: bmgalign.cpp:23
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
mg_align_micrographs
int mg_align_micrographs(Bproject *project, int refset, Vector3< long > tile_size, double res_lo, double res_hi, double max_shift, int filter_flag, int refine_flag)
Aligns a focal series of micrographs.
Definition: mg_align.cpp:319
Bstring
Definition: Bstring.h:29
verbose
int verbose
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
mg_align_feature_extraction
int mg_align_feature_extraction(Bproject *project, int max_features, double res_low, double res_high, double thresh, int extract_method)
Aligns images by feature extraction.
Definition: mg_align.cpp:545
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
mg_pick.h
Header file for single particle picking functions.
Bstring::real
double real()
Definition: Bstring.cpp:553
project_set_field_id
int project_set_field_id(Bproject *project, int nseries, Bstring &field_id)
Sets the field id's according to a regular series.
Definition: mg_processing.cpp:1456
options.h
Header file for option handlers.
project
Bproject * project
Definition: bshow.cpp:33
Boption::tag
Bstring tag
Definition: options.h:29
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
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.
Bproject
Definition: mg_processing.h:336
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
project_write_aligned_images
double project_write_aligned_images(Bproject *project, Bimage *pgr, Bstring &imgfile, DataType datatype)
Calculates aligned micrograph images and write them to a file.
Definition: mg_align.cpp:952
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
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
mg_align_coordinates
int mg_align_coordinates(Bproject *project, int refset)
Aligns the particle coordinates of a series of micrographs.
Definition: mg_align.cpp:39
utilities.h
Header file for general utilities.
Boption::size
Vector3< long > size()
Definition: options.h:163
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
Vector3::volume
double volume()
Definition: Vector3.h:237
rwimg.h
Header file for 2D and 3D image I/O.
write_project
int write_project(const char *filename, Bproject *project)
Writing micrograph parameters to a file.
Definition: rwmg.cpp:321
Matrix.h
Generalized matrix class.
mg_processing.h
Header file for micrograph processing.
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
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
mg_align.h
Header file for functions to align micrographs or coordinates from micrographs and apply the resultan...
project_add_origins_to_coords
int project_add_origins_to_coords(Bproject *project)
Adds the particle origins to the micrograph coordinates.
Definition: mg_processing.cpp:2154
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:713