| Bsoft 2.0.5 Bernard's software package

brecmod.cpp
Go to the documentation of this file.
1
9 #include "mg_processing.h"
10 #include "rwmg.h"
11 #include "rwimg.h"
12 #include "rwmodel.h"
13 #include "model_transform.h"
14 #include "marker.h"
15 #include "linked_list.h"
16 #include "utilities.h"
17 #include "options.h"
18 #include "timer.h"
19
20 // Declaration of global variables
21 extern int verbose; // Level of output to the screen
22
23 Bmodel* project_model_generate(Bproject* project, Bmodel* temp, int flags);
24 int project_model_consolidate(Bproject* project, Bmodel* model);
25
26 /* Usage assistance */
27 const char* use[] = {
28 " ",
29 "Usage: brecmod [options] in.star",
30 "--------------------------------",
31 "Merges data from micrograph and model files.",
32 " ",
33 "Actions:",
34 "-consolidate mod.star Consolidate project and model parameters.",
35 "-images Convert particle images (use with -outmodel).",
36 "-submodels Generate particle models (use with -outmodel).",
37 " ",
38 "Parameters:",
39 "-verbose 7 Verbosity of output.",
40 "-sampling 1.5,1.5,1.5 Sampling of images (A/pixel; a single value can be given).",
41 " ",
42 "Input:",
43 "-template model.star Template to associate with each particle.",
44 " ",
45 "Output:",
46 "-output newmg.star New micrograph parameter file.",
47 "-outmodel partmod.star New model file from particles.",
48 "-outmark markmod.star New model file from markers.",
49 " ",
50 NULL
51 };
52
53 int main(int argc, char **argv)
54 {
55 /* Initialize variables */
56 Bstring consolidate; // Consolidate project and model parameters
57 int flags(0); // Convert particle images to model maps
58 Vector3<double> sam; // Units for the three axes (A/pixel)
59 Bstring paramfile; // Input parameter file name
60 Bstring tempfile; // Input template model file name
61 Bstring outfile; // Output micrograph parameter file name
62 Bstring partmodfile; // Output particle model file name
63 Bstring markmodfile; // Output marker model file name
64
65 int optind;
66 Boption* option = get_option_list(use, argc, argv, optind);
67 Boption* curropt;
68 for ( curropt = option; curropt; curropt = curropt->next ) {
69 if ( curropt->tag == "consolidate" )
70 consolidate = curropt->filename();
71 if ( curropt->tag == "images" ) flags |= 1;
72 if ( curropt->tag == "submodels" ) flags |= 2;
73 if ( curropt->tag == "sampling" )
74 sam = curropt->sampling();
75 if ( curropt->tag == "template" )
76 tempfile = curropt->filename();
77 if ( curropt->tag == "output" )
78 outfile = curropt->filename();
79 if ( curropt->tag == "outmodel" )
80 partmodfile = curropt->filename();
81 if ( curropt->tag == "outmark" )
82 markmodfile = curropt->filename();
83 }
84 option_kill(option);
85
86 double ti = timer_start();
87
88 // Read all the micrograph parameter files
89 Bstring* file_list = NULL;
90 while ( optind < argc ) string_add(&file_list, argv[optind++]);
91 if ( !file_list ) {
92 cerr << "Error: No parameter or image files specified!" << endl;
93 bexit(-1);
94 }
95
96 Bproject* project = read_project(file_list);
97 string_kill(file_list);
98
99 if ( !project ) {
100 cerr << "Error: Input micrograph file not read!" << endl;
101 bexit(-1);
102 }
103
104 if ( !project->rec ) {
105 cerr << "Error: No reconstruction found!" << endl;
106 bexit(-1);
107 }
108
109 project_update_comment(project, argc, argv);
110
111 // Read the model file
112 Bmodel* model = NULL;
113 Bmodel* temp = NULL;
114
115 if ( sam.volume() <= 0 )
116 sam = {project->rec->voxel_size, project->rec->voxel_size, project->rec->voxel_size};
117 else
118 project_set_pixel_size(project, sam[0]);
119
120 if ( tempfile.length() ) {
121 temp = read_model(tempfile, paramfile);
122 if ( !temp ) {
123 cerr << "Error: Input template file " << tempfile << " not read!" << endl;
124 bexit(-1);
125 }
126 }
127
128 if ( consolidate.length() ) {
129 model = read_model(consolidate, paramfile);
130 if ( !model ) {
131 cerr << "Error: No model read!" << endl;
132 bexit(-1);
133 }
134 project_model_consolidate(project, model);
135 } else if ( partmodfile.length() ) {
136 model = project_model_generate(project, temp, flags);
137 } else if ( markmodfile.length() ) {
138 model = model_from_markers(project->rec->mark, project->rec->origin, sam);
139 }
140
141 // Write an output parameter format file if a name is given
142 if ( outfile.length() ) {
143 write_project(outfile, project, 0, 0);
144 }
145
146 // Write an output parameter format file if a name is given
147 if ( partmodfile.length() ) {
148 model_update_comment(model, argc, argv);
149 write_model(partmodfile, model);
150 }
151
152 if ( markmodfile.length() ) {
153 model_update_comment(model, argc, argv);
154 write_model(markmodfile, model);
155 }
156
157 project_kill(project);
158 model_kill(model);
159
160 if ( verbose & VERB_TIME )
161 timer_report(ti);
162
163 bexit(0);
164 }
165
184 Bmodel* project_model_generate(Bproject* project, Bmodel* temp, int flags)
185 {
186 int i;
187 Bstring id("1");
188 Bstring comptype = "PRT";
189 Breconstruction* rec;
190 Bparticle* part;
191 Bmodel* model = NULL;
192 Bmodel* mp = NULL;
193 Bmodel* partmod = NULL;
194 Bcomponent* comp = NULL;
195 Bcomptype* ct = NULL;
196 Bimage *p;
197 Bstring partfile, newpartfile, modfile;
198 Vector3<double> origin, shift;
199 Matrix3 mat;
200
201 if ( verbose )
202 cout << "Generating models from particles:" << endl << "Reconstruction\tParticle\tModel" << endl;
203 for ( i=1, rec = project->rec; rec; rec = rec->next, i++ ) {
204 mp = model_add(&mp, rec->id);
205 if ( !model ) model = mp;
206 mp->fom = rec->fom;
207 mp->sel = rec->select;
208 mp->fmap = rec->frec;
209 mp->img_num = 0;
210 // mp->comment = project->comment;
211 for ( part = rec->part; part; part = part->next ) {
212 id = Bstring(part->id, "%d");
213 comp = component_add(&mp->comp, id);
214 comp->loc = (part->loc - rec->origin) * rec->voxel_size;
215 comp->view = part->view.backward();
216 comp->fom = part->fom[0];
217 comp->sel = part->sel;
218 comp->type = ct = model_add_type_by_id(mp, id);
219 if ( part->fpart.length() ) {
220 partfile = part->fpart;
221 modfile = partfile.pre_rev('.') + ".star";
222 newpartfile = partfile.pre_rev('.') + "_std." + partfile.post_rev('.');
223 } else if ( rec->fpart.length() ) {
224 partfile = rec->fpart;
225 modfile = partfile.pre_rev('.') + Bstring(part->id, "_%03d.star");
226 newpartfile = partfile.pre_rev('.') + Bstring(part->id, "_%03d.") + partfile.post_rev('.');
227 ct->num = part->id - 1;
228 } else {
229 modfile = rec->frec.pre_rev('.') + Bstring(part->id, "_%03d.star");
230 }
231 if ( partfile.length() ) {
232 ct->fmod = newpartfile;
233 if ( flags & 1 ) {
234 if ( ( p = read_img(partfile, 1, ct->num) ) == NULL ) {
235 error_show("project_model_generate", __FILE__, __LINE__);
236 return NULL;
237 }
238 ct->num = 0;
239 if ( p->background(long(0)) == 0 )
240 p->calculate_background();
241 origin = p->size()/2;
242 shift = origin - part->ori;
243 p->origin(part->ori);
244 p->rotate(shift, part->view);
245 part->view = View(0,0,1,0);
246 part->ori = origin;
247 write_img(ct->fmod, p, 0);
248 delete p;
249 }
250 }
251 if ( flags & 2 ) {
252 ct->fmod = modfile;
253 ct->num = 0;
254 if ( temp ) {
255 partmod = model_copy(temp);
256 partmod->id = comp->id;
257 // origin = 0;
258 mat = part->view.matrix();
259 mat = mat.transpose();
260 model_rotate(partmod, mat, origin, shift);
261 } else partmod = model_init(comp->id);
262 partmod->fom = part->fom[0];
263 partmod->sel = part->sel;
264 partmod->fmap = partfile;
265 if ( flags & 1 ) partmod->fmap = newpartfile;
266 // partmod->comment = mp->comment;
267 write_model(modfile, partmod);
268 model_kill(partmod);
269 }
270 if ( verbose )
271 cout << rec->id << tab << part->id << tab << comp->id << endl;
272 }
273 }
274
275 return model;
276 }
277
292 int project_model_consolidate(Bproject* project, Bmodel* model)
293 {
294 Breconstruction* rec;
295 Bparticle* part;
296 Bmodel* mp;
297 Bstring type_id;
298 Vector3<double> origin;
299
300 if ( verbose )
301 cout << "Consolidating particles with models:" << endl << "Reconstruction\tParticle\tModel" << endl;
302 for ( rec = project->rec, mp = model; rec; rec = rec->next ) {
303 for ( part = rec->part; part && mp; part = part->next, mp = mp->next ) {
304 if ( verbose )
305 cout << rec->id << tab << part->id << tab << mp->id << endl;
306 // type_id = mp->type_id.remove('_') + Bstring((mp->hand<0)?2:mp->hand, "%1d");
307 // part->group = type_id.integer();
308 // if ( part->group < 10 ) part->group = part->id;
309 // mp->id = rec->id + Bstring(part->id, "_%04d");
310 if ( part->fpart.length() ) mp->fmap = part->fpart;
311 // model_shift(mp, (part->loc - rec->origin)*rec->voxel_size);
312 model_rotate(mp, part->view, origin, (part->ori - rec->box_size/2)*rec->voxel_size);
313 }
314 }
315
316 return 0;
317 }
318
write_project
int write_project(const char *filename, Bproject *project)
Writing micrograph parameters to a file.
Definition: rwmg.cpp:320
Bparticle::id
int id
Definition: mg_processing.h:92
rwmg.h
Header file for reading and writing micrograph parameters.
Bmodel::img_num
int img_num
Definition: rwmodel.h:127
Bcomponent::id
Bstring id
Definition: rwmodel.h:53
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:705
Bcomponent::type
Bcomptype * type
Definition: rwmodel.h:54
Bproject::rec
Breconstruction * rec
Definition: mg_processing.h:344
Bmodel::id
Bstring id
Definition: rwmodel.h:121
Bparticle::view
View view
Definition: mg_processing.h:101
Breconstruction::part
Bparticle * part
Definition: mg_processing.h:304
Breconstruction::mark
Bmarker * mark
Definition: mg_processing.h:307
main
int main(int argc, char **argv)
Definition: brecmod.cpp:53
Breconstruction::fpart
Bstring fpart
Definition: mg_processing.h:288
utilities.h
Header file for general utilities.
Breconstruction::fom
double fom
Definition: mg_processing.h:302
Boption::sampling
Vector3< double > sampling()
Definition: options.h:202
Breconstruction
Definition: mg_processing.h:280
Boption::tag
Bstring tag
Definition: options.h:29
Bcomptype::fmod
Bstring fmod
Definition: rwmodel.h:29
Bparticle::fom
double fom[NFOM]
Definition: mg_processing.h:102
read_project
Bproject * read_project(const char *filename, int flags=0)
Reading micrograph parameters from files.
Definition: rwmg.cpp:140
Matrix3
Definition: Matrix3.h:17
Bstring::length
long length() const
Definition: Bstring.cpp:263
project
Bproject * project
Definition: bshow.cpp:31
component_add
Bcomponent * component_add(Bcomponent **comp, Bstring &id)
Adds a component to a linked list.
Definition: rwmodel.cpp:267
Bmodel::sel
int sel
Definition: rwmodel.h:129
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
Breconstruction::box_size
Vector3< long > box_size
Definition: mg_processing.h:296
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:76
Breconstruction::origin
Vector3< double > origin
Definition: mg_processing.h:293
Boption::filename
Bstring filename()
Definition: options.h:85
model_copy
Bmodel * model_copy(Bmodel *model)
Copies a model.
Definition: rwmodel.cpp:685
timer.h
Utilities for timing functions.
Bimage
General image parameter class.
Definition: Bimage.h:163
Bparticle::sel
long sel
Definition: mg_processing.h:103
linked_list.h
Header file for generalized linked list functions.
project_model_generate
Bmodel * project_model_generate(Bproject *project, Bmodel *temp, int flags)
Generates a model from each particle defined in a reconstruction.
Definition: brecmod.cpp:184
model_kill
int model_kill(Bmodel *model)
Deallocates all memory in the list.
Definition: rwmodel.cpp:1124
model_init
Bmodel * model_init(Bstring &id)
Initializes a model structure.
Definition: rwmodel.cpp:144
Bcomponent::view
View view
Definition: rwmodel.h:59
Bcomponent::fom
float fom
Definition: rwmodel.h:65
Boption
A class for a command line input option.
Definition: options.h:26
VERB_TIME
#define VERB_TIME
Definition: utilities.h:110
model_update_comment
int model_update_comment(Bmodel *model, int n, char **strings)
Puts a set of strings and time in the comment of a model.
Definition: rwmodel.cpp:1158
View::backward
View backward()
Definition: View.h:115
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:280
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
Bimage::calculate_background
int calculate_background(long nn, int flag)
Calculates the background for one sub-image.
Definition: Bimage_background.cpp:27
Bparticle::fpart
Bstring fpart
Definition: mg_processing.h:91
Bparticle::ori
Vector3< double > ori
Definition: mg_processing.h:100
model_rotate
long model_rotate(Bmodel *model, Matrix3 mat)
Rotates the model.
Definition: model_transform.cpp:238
Bmodel::next
Bmodel * next
Definition: rwmodel.h:120
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:428
Bcomponent::sel
int sel
Definition: rwmodel.h:66
model_add_type_by_id
Bcomptype * model_add_type_by_id(Bmodel *model, Bstring &id)
Add a type to a model if the identifier does not exist already.
Definition: rwmodel.cpp:411
use
const char * use[]
Definition: brecmod.cpp:27
model
Bmodel * model
Definition: bshow.cpp:32
read_model
Bmodel * read_model(Bstring &filename)
Definition: rwmodel.cpp:28
write_model
int write_model(Bstring &filename, Bmodel *model)
Writes model parameters.
Definition: rwmodel.cpp:91
Boption::next
Boption * next
Definition: options.h:28
Bparticle
Definition: mg_processing.h:89
Bmodel::fom
float fom
Definition: rwmodel.h:128
Bstring
Definition: Bstring.h:29
project_kill
int project_kill(Bproject *project)
Frees a whole project.
Definition: mg_processing.cpp:1224
View::matrix
Matrix3 matrix()
Definition: View.h:150
project_model_consolidate
int project_model_consolidate(Bproject *project, Bmodel *model)
Exchanges information between reconstruction and model structures.
Definition: brecmod.cpp:292
Bmodel::fmap
Bstring fmap
Definition: rwmodel.h:125
Matrix3::transpose
Matrix3 transpose() const
Definition: Matrix3.h:268
Bcomptype::num
int num
Definition: rwmodel.h:30
Breconstruction::select
long select
Definition: mg_processing.h:283
project_set_pixel_size
int project_set_pixel_size(Bproject *project, double pixel_size)
Sets the pixel size for all the micrographs.
Definition: mg_processing.cpp:3398
model_add
Bmodel * model_add(Bmodel **model, Bstring &id)
Adds a model to a linked list.
Definition: rwmodel.cpp:178
rwmodel.h
Header file for reading and writing atomic model files.
model_from_markers
Bmodel * model_from_markers(Bmarker *markers, Vector3< double > origin, Vector3< double > sam)
Convert markers to a model.
Definition: marker.cpp:1002
Vector3< double >
Bparticle::loc
Vector3< double > loc
Definition: mg_processing.h:99
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:67
View
Definition: View.h:26
Bimage::rotate
int rotate()
Rotates an image using parameters defined in the image in place.
Definition: Bimage_transform.cpp:253
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:109
Bmodel
Definition: rwmodel.h:119
mg_processing.h
Header file for micrograph processing.
Breconstruction::frec
Bstring frec
Definition: mg_processing.h:287
Breconstruction::voxel_size
double voxel_size
Definition: mg_processing.h:292
rwimg.h
Header file for 2D and 3D image I/O.
options.h
Header file for option handlers.
Bcomptype
Definition: rwmodel.h:25
Bmodel::comp
Bcomponent * comp
Definition: rwmodel.h:132
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:413
Vector3::volume
double volume()
Definition: Vector3.h:235
project_update_comment
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:468
Bparticle::next
Bparticle * next
Definition: mg_processing.h:90
Bimage::background
double background(long nn)
Definition: Bimage.h:542
Bcomponent::loc
Vector3< float > loc
Definition: rwmodel.h:58
model_transform.h
Library routines used for model transformation.
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:273
Breconstruction::next
Breconstruction * next
Definition: mg_processing.h:281
marker.h
Header file for image parameter functions.
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:52
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:755
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:21
Bproject
Definition: mg_processing.h:333
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:563
Breconstruction::id
Bstring id
Definition: mg_processing.h:282
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:760
verbose
int verbose
Bcomponent
Definition: rwmodel.h:51