| Bsoft 2.0.5 Bernard's software package

bmod2part.cpp
Go to the documentation of this file.
1
9 #include "rwmodel.h"
10 #include "model_select.h"
11 #include "model_util.h"
12 #include "rwmg.h"
13 #include "mg_processing.h"
14 #include "rwimg.h"
15 #include "file_util.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 // Function prototypes
24 Bproject* project_part_from_model(Bmodel* model, Vector3<double> origin,
25 Vector3<double> sampling, Vector3<long> box_size);
26 Bmodel* models_from_particles(Bproject* project);
27 Bmodel* components_from_2D_particles(Bproject* project);
28 Bmodel* components_from_3D_particles(Bproject* project);
29
30 /* Usage assistance */
31 const char* use[] = {
32 " ",
33 "Usage: bmod2part [options] input.star",
34 "-------------------------------------",
35 "Converting between model components and reconstruction particle parameters.",
36 " ",
37 "Actions:",
38 "-reconstructions Operate on reconstruction parameters rather than micrographs.",
39 "-all Reset selection to all components before other selections.",
40 "-type comp Type of conversion: part->models or part->components.",
41 " ",
42 "Parameters:",
43 "-verbose 7 Verbose output.",
44 "-origin 0,0,0 Origin placement within image (default 0,0,0).",
45 "-sampling 1 Sampling (default 1 angstrom/voxel).",
46 "-box 100,83,120 Particle box size (default 50,50,50; one sets all).",
47 " ",
48 "Input:",
49 "-map image.spi Input 3D map file.",
50 "-parameters param.star Input atomic parameter file.",
51 " ",
52 "Output:",
53 "-output part.star Output particle parameter or model file.",
54 " ",
55 NULL
56 };
57
58 int main(int argc, char **argv)
59 {
60 /* Initialize variables */
61 int use_rec(0); // Flag to process reconstructions
62 int reset(0); // Keep selection as read from file
63 Bstring type; // Type of conversion
64 Vector3<double> origin; // Coordinate origin placement
65 int set_origin(0); // Flag to set origin
66 Vector3<double> sam; // Sampling in angstrom/voxel side
67 Vector3<long> box_size(50,50,50); // Particle box size
68 Bstring mapfile; // Input map file
69 Bstring paramfile; // Input parameter file
70 Bstring outfile; // Output parameter file
71
72 int optind;
73 Boption* option = get_option_list(use, argc, argv, optind);
74 Boption* curropt;
75 for ( curropt = option; curropt; curropt = curropt->next ) {
76 if ( curropt->tag == "reconstructions" ) use_rec = 1;
77 if ( curropt->tag == "all" ) reset = 1;
78 if ( curropt->tag == "type" ) type = curropt->value.lower();
79 if ( curropt->tag == "origin" ) {
80 if ( curropt->value[0] == 'c' ) {
81 set_origin = 2;
82 } else {
83 origin = curropt->origin();
84 set_origin = 1;
85 }
86 }
87 if ( curropt->tag == "sampling" )
88 sam = curropt->sampling();
89 if ( curropt->tag == "box" )
90 box_size = curropt->size();
91 if ( curropt->tag == "map" )
92 mapfile = curropt->filename();
93 if ( curropt->tag == "parameters" )
94 paramfile = curropt->filename();
95 if ( curropt->tag == "output" )
96 outfile = curropt->filename();
97 }
98 option_kill(option);
99
100 double ti = timer_start();
101
102 // Read all the parameter files
103 Bstring* file_list = NULL;
104 while ( optind < argc ) string_add(&file_list, argv[optind++]);
105 if ( !file_list ) {
106 cerr << "Error: No model files specified!" << endl;
107 bexit(-1);
108 }
109
110 Bmodel* model = NULL;
111 Bproject* project = NULL;
112 Bimage* p = NULL;
113
114 Vector3<double> ori3, sam3;
115
116 if ( file_type(*file_list) == Model ) {
117 model = read_model(file_list, paramfile);
118 if ( !model ) {
119 cerr << "Error: Input file not read!" << endl;
120 bexit(-1);
121 }
122 if ( reset ) models_process(model, model_reset_selection);
123 if ( mapfile.length() ) model->fmap = mapfile;
124 if ( model->fmap.length() ) {
125 p = read_img(model->fmap, 0, -1);
126 if ( p ) {
127 if ( sam.volume() <= 0 ) sam = p->sampling(0);
128 if ( origin.length() <= 0 ) origin = p->image->origin();
129 else if ( set_origin == 2 ) origin = p->default_origin();
130 delete p;
131 }
132 }
133 ori3 = Vector3<double>(origin[0], origin[1], origin[2]);
134 sam3 = Vector3<double>(sam[0], sam[1], sam[2]);
135 project = project_part_from_model(model, ori3, sam3, box_size);
136 if ( project && outfile.length() ) {
137 project_update_comment(project, argc, argv);
138 write_project(outfile, project, 0, 0);
139 }
140 } else {
141 project = read_project(file_list);
142 if ( use_rec ) project->select = 1;
143 Vector3<double> ori3(origin[0], origin[1], origin[2]);
144 if ( project->select && project->rec ) {
145 if ( mapfile.length() ) project->rec->frec = mapfile;
146 if ( project->rec->frec.length() ) {
147 p = read_img(project->rec->frec, 0, -1);
148 if ( p ) {
149 if ( sam.volume() <= 0 ) sam = p->sampling(0);
150 if ( origin.length() <= 0 ) origin = p->image->origin();
151 else if ( set_origin == 2 ) origin = p->default_origin();
152 delete p;
153 }
154 }
155 ori3 = Vector3<double>(origin[0], origin[1], origin[2]);
156 if ( origin.length() > 0 ) project->rec->origin = ori3;
157 if ( sam[0] > 0 ) project_set_pixel_size(project, sam[0]);
158 if ( type.length() && type[0] == 'm' ) model = models_from_particles(project);
159 else model = components_from_3D_particles(project);
160 } else if ( project->field && project->field->mg ) {
161 ori3 = Vector3<double>(origin[0], origin[1], origin[2]);
162 if ( ori3.volume() > 0 ) project_set_micrograph_origins(project, ori3);
163 model = components_from_2D_particles(project);
164 if ( mapfile.length() ) model->fmap = mapfile;
165 }
166 if ( outfile.length() && model ) {
167 model_update_comment(model, argc, argv);
168 write_model(outfile, model);
169 }
170 }
171
172 string_kill(file_list);
173 model_kill(model);
174 project_kill(project);
175
176 if ( verbose & VERB_TIME )
177 timer_report(ti);
178
179 return 0;
180 }
181
190 Bproject* project_part_from_model(Bmodel* model, Vector3<double> origin,
191 Vector3<double> sampling, Vector3<long> box_size)
192 {
193 long i;
194
195 for ( i=0; i<3; i++ ) if ( sampling[i] <= 0 ) sampling[i] = 1;
196
197 Bstring base = model->fmap.base();
198 Bproject* project = project_init();
199 Breconstruction* rec = reconstruction_add(&project->rec, base);
200 Bparticle* part = NULL;
201
202 rec->frec = model->fmap;
203 rec->voxel_size = sampling[0];
204 rec->origin = origin;
205 rec->box_size = box_size;
206 rec->bad_radius = box_size[0]/4;
207
208 if ( verbose )
209 cout << "Generating particles from model components" << endl << endl;
210
211 Bmodel* mp;
212 Bcomponent* comp;
213
214 for ( i=0, mp = model; mp; mp = mp->next ) if ( mp->sel ) {
215 for ( comp = mp->comp; comp; comp = comp->next ) if ( comp->sel ) {
216 part = particle_add(&part, ++i);
217 if ( !rec->part ) rec->part = part;
218 part->ori = box_size/2;
219 part->loc = comp->loc/sampling + origin;
220 part->view = comp->view.backward();
221 part->sel = comp->sel;
222 part->fom[0] = comp->fom;
223 }
224 }
225
226 return project;
227 }
228
234 Bmodel* models_from_particles(Bproject* project)
235 {
236 long nmod(0);
237 Breconstruction* rec = project->rec;
238 Bparticle* part = NULL;
239 Bmodel* model = NULL;
240 Bmodel* mp = NULL;
241 Bstring id;
242
243 if ( verbose )
244 cout << "Generating models from particles" << endl;
245
246 for ( part = rec->part; part; part = part->next ) {
247 id = Bstring(part->id, "%d");
248 mp = model_add(&mp, id);
249 if ( !model ) model = mp;
250 if ( part->fpart.length() ) {
251 mp->fmap = part->fpart;
252 mp->img_num = 0;
253 } else if ( rec->fpart.length() ) {
254 mp->fmap = rec->fpart;
255 mp->img_num = part->id-1;
256 }
257 mp->fom = part->fom[0];
258 mp->sel = part->sel;
259 // comp->loc = (part->loc - rec->origin)*rec->voxel_size;
260 // comp->view = part->view.backward();
261 nmod++;
262 }
263
264 if ( verbose )
265 cout << "Models generated: " << nmod << endl << endl;
266
267 return model;
268 }
269
275 Bmodel* components_from_2D_particles(Bproject* project)
276 {
277 long i;
278 Bfield* field;
279 Bmicrograph* mg;
280 Bparticle* part = NULL;
281 Bstring id("Particles");
282 Bmodel* model = model_init(id);
283 Bcomponent* comp = NULL;
284 Bstring comptype = "PRT";
285 Bcomptype* ct = model_add_type_by_id(model, comptype);
286
287 if ( verbose )
288 cout << "Generating model components from 2D particles" << endl;
289
290 for ( i=0, field = project->field; field; field = field->next ) {
291 for ( mg = field->mg; mg; mg = mg->next ) {
292 for ( part = mg->part; part; part = part->next ) {
293 id = Bstring(++i, "%d");
294 comp = component_add(&comp, id);
295 if ( !model->comp ) model->comp = comp;
296 comp->type = ct;
297 comp->loc = (part->loc - mg->origin)*mg->pixel_size;
298 comp->view = part->view.backward();
299 comp->sel = part->sel;
300 comp->fom = part->fom[0];
301 }
302 }
303 }
304
305 if ( verbose )
306 cout << "Components generated: " << i << endl << endl;
307
308 return model;
309 }
310
316 Bmodel* components_from_3D_particles(Bproject* project)
317 {
318 long ncomp(0);
319 Breconstruction* rec = project->rec;
320 Bparticle* part = NULL;
321 Bmodel* model = model_init(rec->id);
322 Bcomponent* comp = NULL;
323 Bstring id;
324 Bstring comptype = "PRT";
325 Bcomptype* ct = model_add_type_by_id(model, comptype);
326
327 model->fmap = rec->frec;
328
329 if ( verbose )
330 cout << "Generating model components from 3D particles" << endl;
331
332 for ( part = rec->part; part; part = part->next ) {
333 id = Bstring(part->id, "%d");
334 comp = component_add(&comp, id);
335 if ( !model->comp ) model->comp = comp;
336 comp->type = ct;
337 comp->loc = (part->loc - rec->origin)*rec->voxel_size;
338 comp->view = part->view.backward();
339 comp->sel = part->sel;
340 comp->fom = part->fom[0];
341 ncomp++;
342 }
343
344 if ( verbose )
345 cout << "Components generated: " << ncomp << endl << endl;
346
347 return model;
348 }
349
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
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:705
Model
Definition: file_util.h:25
Bcomponent::type
Bcomptype * type
Definition: rwmodel.h:54
Bproject::rec
Breconstruction * rec
Definition: mg_processing.h:344
project_set_micrograph_origins
int project_set_micrograph_origins(Bproject *project, Vector3< float > origin)
Sets micrograph origins to the given origin.
Definition: mg_processing.cpp:3531
Bparticle::view
View view
Definition: mg_processing.h:101
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
Vector3::length
double length()
Definition: Vector3.h:222
Breconstruction::part
Bparticle * part
Definition: mg_processing.h:304
Breconstruction::fpart
Bstring fpart
Definition: mg_processing.h:288
project_part_from_model
Bproject * project_part_from_model(Bmodel *model, Vector3< double > origin, Vector3< double > sampling, Vector3< long > box_size)
Converts component coordinates in a model to reconstruction particle coordinates.
Definition: bmod2part.cpp:190
utilities.h
Header file for general utilities.
Boption::sampling
Vector3< double > sampling()
Definition: options.h:202
use
const char * use[]
Definition: bmod2part.cpp:31
model_util.h
Library routines used for model processing.
Boption::value
Bstring value
Definition: options.h:30
Breconstruction
Definition: mg_processing.h:280
Boption::tag
Bstring tag
Definition: options.h:29
verbose
int verbose
file_type
FileType file_type(const char *filename)
Checks the file type using the extension and contents.
Definition: file_util.cpp:178
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
particle_add
Bparticle * particle_add(Bparticle **part, int pid)
Adds a particle parameter structure to a linked list.
Definition: mg_processing.cpp:527
Bstring::length
long length() const
Definition: Bstring.cpp:263
Bmicrograph::pixel_size
double pixel_size
Definition: mg_processing.h:214
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
Bfield::next
Bfield * next
Definition: mg_processing.h:258
project_init
Bproject * project_init()
Initializes a micrograph processing project parameter structure.
Definition: mg_processing.cpp:29
Bmicrograph::part
Bparticle * part
Definition: mg_processing.h:239
timer.h
Utilities for timing functions.
Bimage
General image parameter class.
Definition: Bimage.h:163
Boption::size
Vector3< long > size()
Definition: options.h:163
reconstruction_add
Breconstruction * reconstruction_add(Breconstruction **rec, Bstring &rec_id)
Adds a reconstruction parameter structure to a linked list.
Definition: mg_processing.cpp:704
Bparticle::sel
long sel
Definition: mg_processing.h:103
Bfield
Definition: mg_processing.h:257
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
base
string base(string &filename)
Definition: string_util.cpp:28
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
Bproject::field
Bfield * field
Definition: mg_processing.h:343
VERB_TIME
#define VERB_TIME
Definition: utilities.h:110
models_from_particles
Bmodel * models_from_particles(Bproject *project)
Converts particle coordinates for a reconstruction into model component coordinates.
Definition: bmod2part.cpp:234
main
int main(int argc, char **argv)
Definition: bmod2part.cpp:58
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
Bmicrograph
Definition: mg_processing.h:200
View::backward
View backward()
Definition: View.h:115
Bmicrograph::origin
Vector3< double > origin
Definition: mg_processing.h:220
components_from_3D_particles
Bmodel * components_from_3D_particles(Bproject *project)
Converts particle coordinates for a reconstruction into model component coordinates.
Definition: bmod2part.cpp:316
file_util.h
Header file for file checking functions.
Bparticle::fpart
Bstring fpart
Definition: mg_processing.h:91
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:127
Bparticle::ori
Vector3< double > ori
Definition: mg_processing.h:100
Bmodel::next
Bmodel * next
Definition: rwmodel.h:120
Bfield::mg
Bmicrograph * mg
Definition: mg_processing.h:264
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
model
Bmodel * model
Definition: bshow.cpp:32
Bstring::base
Bstring base()
Definition: Bstring.cpp:508
components_from_2D_particles
Bmodel * components_from_2D_particles(Bproject *project)
Converts particle coordinates from micrographs into model component coordinates.
Definition: bmod2part.cpp:275
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
model_reset_selection
long model_reset_selection(Bmodel *model)
Resets the selection to all components.
Definition: model_select.cpp:232
project_kill
int project_kill(Bproject *project)
Frees a whole project.
Definition: mg_processing.cpp:1224
Bimage::default_origin
Vector3< double > default_origin()
Definition: Bimage.h:585
Bproject::select
int select
Definition: mg_processing.h:339
Bmodel::fmap
Bstring fmap
Definition: rwmodel.h:125
Breconstruction::bad_radius
double bad_radius
Definition: mg_processing.h:299
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
Bcomponent::next
Bcomponent * next
Definition: rwmodel.h:52
rwmodel.h
Header file for reading and writing atomic model files.
Vector3< double >
Bparticle::loc
Vector3< double > loc
Definition: mg_processing.h:99
Boption::origin
Vector3< double > origin()
Definition: options.h:180
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:67
Bstring::lower
Bstring lower()
Definition: Bstring.cpp:308
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
models_process
long models_process(Bmodel *model, long(modfunc)(Bmodel *))
Process a list of models using the specified function.
Definition: model_util.cpp:36
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
model_select.h
Header file for reading and writing atomic model files.
Bcomponent::loc
Vector3< float > loc
Definition: rwmodel.h:58
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:273
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:52
Bproject
Definition: mg_processing.h:333
Bimage::image
Bsub_image * image
Definition: Bimage.h:188
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
Bmicrograph::next
Bmicrograph * next
Definition: mg_processing.h:201
Bcomponent
Definition: rwmodel.h:51