Bsoft  2.1.0
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
25  Vector3<double> sampling, Vector3<long> box_size);
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 2.3,2.3,1 Sampling (angstrom/voxel, one value sets all).",
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->scale();
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  }
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  write_project(outfile, project, 0, 0);
138  }
139  } else {
140  project = read_project(file_list);
141  if ( use_rec ) project->select = 1;
142  Vector3<double> ori3(origin[0], origin[1], origin[2]);
143  if ( project->select && project->rec ) {
144  if ( mapfile.length() ) project->rec->frec = mapfile;
145  if ( project->rec->frec.length() ) {
146  p = read_img(project->rec->frec, 0, -1);
147  if ( p ) {
148  if ( sam.volume() <= 0 ) sam = p->sampling(0);
149  if ( origin.length() <= 0 ) origin = p->image->origin();
150  else if ( set_origin == 2 ) origin = p->default_origin();
151  delete p;
152  }
153  }
154  ori3 = Vector3<double>(origin[0], origin[1], origin[2]);
155  if ( origin.length() > 0 ) project->rec->origin = ori3;
156  if ( sam[0] > 0 ) project_set_mg_pixel_size(project, sam);
157  if ( type.length() && type[0] == 'm' ) model = models_from_particles(project);
159  } else if ( project->field && project->field->mg ) {
160  ori3 = Vector3<double>(origin[0], origin[1], origin[2]);
161  if ( ori3.volume() > 0 ) project_set_micrograph_origins(project, ori3);
163  if ( mapfile.length() ) model->fmap = mapfile;
164  }
165  if ( outfile.length() && model ) {
166  write_model(outfile, model);
167  }
168  }
169 
170  string_kill(file_list);
171  model_kill(model);
173 
174  if ( verbose & VERB_TIME )
175  timer_report(ti);
176 
177  return 0;
178 }
179 
189  Vector3<double> sampling, Vector3<long> box_size)
190 {
191  long i;
192 
193  for ( i=0; i<3; i++ ) if ( sampling[i] <= 0 ) sampling[i] = 1;
194 
195  Bstring base = model->fmap.base();
198  Bparticle* part = NULL;
199 
200  rec->frec = model->fmap;
201  rec->voxel_size = sampling;
202  rec->origin = origin;
203  rec->box_size = box_size;
204  rec->bad_radius = box_size[0]/4;
205 
206  if ( verbose )
207  cout << "Generating particles from model components" << endl << endl;
208 
209  Bmodel* mp;
210  Bcomponent* comp;
211 
212  for ( i=0, mp = model; mp; mp = mp->next ) if ( mp->sel ) {
213  for ( comp = mp->comp; comp; comp = comp->next ) if ( comp->sel ) {
214  part = particle_add(&part, ++i);
215  if ( !rec->part ) rec->part = part;
216  part->ori = box_size/2;
217  part->loc = comp->loc/sampling + origin;
218  part->view = comp->view.backward();
219  part->sel = comp->sel;
220  part->fom[0] = comp->fom;
221  }
222  }
223 
224  return project;
225 }
226 
233 {
234  long nmod(0);
235  Breconstruction* rec = project->rec;
236  Bparticle* part = NULL;
237  Bmodel* model = NULL;
238  Bmodel* mp = NULL;
239  Bstring id;
240 
241  if ( verbose )
242  cout << "Generating models from particles" << endl;
243 
244  for ( part = rec->part; part; part = part->next ) {
245  id = Bstring(part->id, "%d");
246  mp = model_add(&mp, id);
247  if ( !model ) model = mp;
248  if ( part->fpart.length() ) {
249  mp->fmap = part->fpart;
250  mp->img_num = 0;
251  } else if ( rec->fpart.length() ) {
252  mp->fmap = rec->fpart;
253  mp->img_num = part->id-1;
254  }
255  mp->fom = part->fom[0];
256  mp->sel = part->sel;
257 // comp->loc = (part->loc - rec->origin)*rec->voxel_size;
258 // comp->view = part->view.backward();
259  nmod++;
260  }
261 
262  if ( verbose )
263  cout << "Models generated: " << nmod << endl << endl;
264 
265  return model;
266 }
267 
274 {
275  long i;
276  Bfield* field;
277  Bmicrograph* mg;
278  Bparticle* part = NULL;
279  Bstring id("Particles");
280  Bmodel* model = model_init(id);
281  Bcomponent* comp = NULL;
282  Bstring comptype = "PRT";
283  Bcomptype* ct = model_add_type_by_id(model, comptype);
284 
285  if ( verbose )
286  cout << "Generating model components from 2D particles" << endl;
287 
288  for ( i=0, field = project->field; field; field = field->next ) {
289  for ( mg = field->mg; mg; mg = mg->next ) {
290  for ( part = mg->part; part; part = part->next ) {
291  id = Bstring(++i, "%d");
292  comp = component_add(&comp, id);
293  if ( !model->comp ) model->comp = comp;
294  comp->type = ct;
295  comp->loc = (part->loc - mg->origin)*mg->pixel_size;
296  comp->view = part->view.backward();
297  comp->sel = part->sel;
298  comp->fom = part->fom[0];
299  }
300  }
301  }
302 
303  if ( verbose )
304  cout << "Components generated: " << i << endl << endl;
305 
306  return model;
307 }
308 
315 {
316  long ncomp(0);
317  Breconstruction* rec = project->rec;
318  Bparticle* part = NULL;
319  Bmodel* model = model_init(rec->id);
320  Bcomponent* comp = NULL;
321  Bstring id;
322  Bstring comptype = "PRT";
323  Bcomptype* ct = model_add_type_by_id(model, comptype);
324 
325  model->fmap = rec->frec;
326 
327  if ( verbose )
328  cout << "Generating model components from 3D particles" << endl;
329 
330  for ( part = rec->part; part; part = part->next ) {
331  id = Bstring(part->id, "%d");
332  comp = component_add(&comp, id);
333  if ( !model->comp ) model->comp = comp;
334  comp->type = ct;
335  comp->loc = (part->loc - rec->origin)*rec->voxel_size;
336  comp->view = part->view.backward();
337  comp->sel = part->sel;
338  comp->fom = part->fom[0];
339  ncomp++;
340  }
341 
342  if ( verbose )
343  cout << "Components generated: " << ncomp << endl << endl;
344 
345  return model;
346 }
347 
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
Bmicrograph
Definition: mg_processing.h:201
Breconstruction::part
Bparticle * part
Definition: mg_processing.h:307
Breconstruction::id
Bstring id
Definition: mg_processing.h:285
Bmodel::fom
float fom
Definition: rwmodel.h:128
Bcomponent::next
Bcomponent * next
Definition: rwmodel.h:52
Breconstruction::fpart
Bstring fpart
Definition: mg_processing.h:291
Bcomponent
Definition: rwmodel.h:51
Boption::scale
Vector3< double > scale()
Definition: options.h:190
Bproject::rec
Breconstruction * rec
Definition: mg_processing.h:347
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:768
Bproject::select
int select
Definition: mg_processing.h:342
Bparticle::view
View view
Definition: mg_processing.h:102
rwmodel.h
Header file for reading and writing atomic model files.
Bcomponent::loc
Vector3< float > loc
Definition: rwmodel.h:58
file_util.h
Header file for file checking functions.
Bstring
Definition: Bstring.h:29
Breconstruction::voxel_size
Vector3< double > voxel_size
Definition: mg_processing.h:295
Bparticle
Definition: mg_processing.h:90
Bproject::field
Bfield * field
Definition: mg_processing.h:346
base
string base(string &filename)
Definition: string_util.cpp:28
main
int main(int argc, char **argv)
Definition: bmod2part.cpp:58
component_add
Bcomponent * component_add(Bcomponent **comp, Bstring &id)
Adds a component to a linked list.
Definition: rwmodel.cpp:270
Bcomptype
Definition: rwmodel.h:25
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
Bimage::default_origin
Vector3< double > default_origin()
Definition: Bimage.h:592
Bmodel::next
Bmodel * next
Definition: rwmodel.h:120
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
project_init
Bproject * project_init()
Initializes a micrograph processing project parameter structure.
Definition: mg_processing.cpp:30
Bparticle::fom
double fom[NFOM]
Definition: mg_processing.h:103
Breconstruction::box_size
Vector3< long > box_size
Definition: mg_processing.h:299
Bmicrograph::part
Bparticle * part
Definition: mg_processing.h:242
options.h
Header file for option handlers.
project_set_micrograph_origins
int project_set_micrograph_origins(Bproject *project, Vector3< double > origin)
Sets micrograph origins to the given origin.
Definition: mg_processing.cpp:2122
Bfield
Definition: mg_processing.h:260
Boption::origin
Vector3< double > origin()
Definition: options.h:180
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
Bmodel::fmap
Bstring fmap
Definition: rwmodel.h:125
models_process
long models_process(Bmodel *model, long(modfunc)(Bmodel *))
Process a list of models using the specified function.
Definition: model_util.cpp:36
model_init
Bmodel * model_init(Bstring &id)
Initializes a model structure.
Definition: rwmodel.cpp:147
Breconstruction::origin
Vector3< double > origin
Definition: mg_processing.h:296
project
Bproject * project
Definition: bshow.cpp:33
Boption::tag
Bstring tag
Definition: options.h:29
Bmodel::comp
Bcomponent * comp
Definition: rwmodel.h:132
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:77
rwmg.h
Header file for reading and writing micrograph parameters.
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
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
components_from_2D_particles
Bmodel * components_from_2D_particles(Bproject *project)
Converts particle coordinates from micrographs into model component coordinates.
Definition: bmod2part.cpp:273
View::backward
View backward()
Definition: View.h:118
Bparticle::fpart
Bstring fpart
Definition: mg_processing.h:92
Bmicrograph::origin
Vector3< double > origin
Definition: mg_processing.h:223
Boption::value
Bstring value
Definition: options.h:30
model_kill
int model_kill(Bmodel *model)
Deallocates all memory in the list.
Definition: rwmodel.cpp:1127
verbose
int verbose
Bimage
General image parameter class.
Definition: Bimage.h:177
model_select.h
Header file for reading and writing atomic model files.
timer.h
Utilities for timing functions.
Bproject
Definition: mg_processing.h:336
Bmodel::sel
int sel
Definition: rwmodel.h:129
Bparticle::sel
long sel
Definition: mg_processing.h:104
model_reset_selection
long model_reset_selection(Bmodel *model)
Resets the selection to all components.
Definition: model_select.cpp:232
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:188
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
Bmodel
Definition: rwmodel.h:119
Bparticle::loc
Vector3< double > loc
Definition: mg_processing.h:100
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
utilities.h
Header file for general utilities.
write_model
int write_model(Bstring &filename, Bmodel *model)
Writes model parameters.
Definition: rwmodel.cpp:92
Boption::size
Vector3< long > size()
Definition: options.h:163
Breconstruction
Definition: mg_processing.h:283
Bmodel::img_num
int img_num
Definition: rwmodel.h:127
Bstring::length
long length() const
Definition: Bstring.cpp:263
Bcomponent::view
View view
Definition: rwmodel.h:59
Bcomponent::fom
float fom
Definition: rwmodel.h:65
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:76
Bcomponent::type
Bcomptype * type
Definition: rwmodel.h:54
use
const char * use[]
Definition: bmod2part.cpp:31
components_from_3D_particles
Bmodel * components_from_3D_particles(Bproject *project)
Converts particle coordinates for a reconstruction into model component coordinates.
Definition: bmod2part.cpp:314
model
Bmodel * model
Definition: bshow.cpp:34
Vector3::volume
double volume()
Definition: Vector3.h:237
particle_add
Bparticle * particle_add(Bparticle **part, int pid)
Adds a particle parameter structure to a linked list.
Definition: mg_processing.cpp:221
rwimg.h
Header file for 2D and 3D image I/O.
Bfield::next
Bfield * next
Definition: mg_processing.h:261
write_project
int write_project(const char *filename, Bproject *project)
Writing micrograph parameters to a file.
Definition: rwmg.cpp:321
Bparticle::ori
Vector3< double > ori
Definition: mg_processing.h:101
mg_processing.h
Header file for micrograph processing.
models_from_particles
Bmodel * models_from_particles(Bproject *project)
Converts particle coordinates for a reconstruction into model component coordinates.
Definition: bmod2part.cpp:232
Breconstruction::bad_radius
double bad_radius
Definition: mg_processing.h:302
Bparticle::next
Bparticle * next
Definition: mg_processing.h:91
Boption::filename
Bstring filename()
Definition: options.h:85
model_add
Bmodel * model_add(Bmodel **model, Bstring &id)
Adds a model to a linked list.
Definition: rwmodel.cpp:181
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
Breconstruction::frec
Bstring frec
Definition: mg_processing.h:290
model_util.h
Library routines used for model processing.
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:414
Bstring::base
Bstring base()
Definition: Bstring.cpp:516
Bmicrograph::pixel_size
Vector3< double > pixel_size
Definition: mg_processing.h:215
read_model
Bmodel * read_model(Bstring &filename)
Definition: rwmodel.cpp:29
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
Vector3::length
double length()
Definition: Vector3.h:222
reconstruction_add
Breconstruction * reconstruction_add(Breconstruction **rec, Bstring &rec_id)
Adds a reconstruction parameter structure to a linked list.
Definition: mg_processing.cpp:398
Model
@ Model
Definition: file_util.h:25
Bmicrograph::next
Bmicrograph * next
Definition: mg_processing.h:202
Bstring::lower
Bstring lower()
Definition: Bstring.cpp:316
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:713
Bfield::mg
Bmicrograph * mg
Definition: mg_processing.h:267
Bparticle::id
int id
Definition: mg_processing.h:93
Bcomponent::sel
int sel
Definition: rwmodel.h:66
file_type
FileType file_type(const char *filename)
Checks the file type using the extension and contents.
Definition: file_util.cpp:178