Bsoft  2.1.0
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 
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->scale();
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 
110  // Read the model file
111  Bmodel* model = NULL;
112  Bmodel* temp = NULL;
113 
114  if ( sam.volume() <= 0 )
115  sam = project->rec->voxel_size;
116  else
118 
119  if ( tempfile.length() ) {
120  temp = read_model(tempfile, paramfile);
121  if ( !temp ) {
122  cerr << "Error: Input template file " << tempfile << " not read!" << endl;
123  bexit(-1);
124  }
125  }
126 
127  if ( consolidate.length() ) {
128  model = read_model(consolidate, paramfile);
129  if ( !model ) {
130  cerr << "Error: No model read!" << endl;
131  bexit(-1);
132  }
134  } else if ( partmodfile.length() ) {
135  model = project_model_generate(project, temp, flags);
136  } else if ( markmodfile.length() ) {
138  }
139 
140  // Write an output parameter format file if a name is given
141  if ( outfile.length() ) {
142  write_project(outfile, project, 0, 0);
143  }
144 
145  // Write an output parameter format file if a name is given
146  if ( partmodfile.length() ) {
147  write_model(partmodfile, model);
148  }
149 
150  if ( markmodfile.length() ) {
151  write_model(markmodfile, model);
152  }
153 
155  model_kill(model);
156 
157  if ( verbose & VERB_TIME )
158  timer_report(ti);
159 
160  bexit(0);
161 }
162 
182 {
183  int i;
184  Bstring id("1");
185  Bstring comptype = "PRT";
186  Breconstruction* rec;
187  Bparticle* part;
188  Bmodel* model = NULL;
189  Bmodel* mp = NULL;
190  Bmodel* partmod = NULL;
191  Bcomponent* comp = NULL;
192  Bcomptype* ct = NULL;
193  Bimage *p;
194  Bstring partfile, newpartfile, modfile;
195  Vector3<double> origin, shift;
196  Matrix3 mat;
197 
198  if ( verbose )
199  cout << "Generating models from particles:" << endl << "Reconstruction\tParticle\tModel" << endl;
200  for ( i=1, rec = project->rec; rec; rec = rec->next, i++ ) {
201  mp = model_add(&mp, rec->id);
202  if ( !model ) model = mp;
203  mp->fom = rec->fom;
204  mp->sel = rec->select;
205  mp->fmap = rec->frec;
206  mp->img_num = 0;
207 // mp->comment = project->comment;
208  for ( part = rec->part; part; part = part->next ) {
209  id = Bstring(part->id, "%d");
210  comp = component_add(&mp->comp, id);
211  comp->loc = (part->loc - rec->origin) * rec->voxel_size;
212  comp->view = part->view.backward();
213  comp->fom = part->fom[0];
214  comp->sel = part->sel;
215  comp->type = ct = model_add_type_by_id(mp, id);
216  if ( part->fpart.length() ) {
217  partfile = part->fpart;
218  modfile = partfile.pre_rev('.') + ".star";
219  newpartfile = partfile.pre_rev('.') + "_std." + partfile.post_rev('.');
220  } else if ( rec->fpart.length() ) {
221  partfile = rec->fpart;
222  modfile = partfile.pre_rev('.') + Bstring(part->id, "_%03d.star");
223  newpartfile = partfile.pre_rev('.') + Bstring(part->id, "_%03d.") + partfile.post_rev('.');
224  ct->num = part->id - 1;
225  } else {
226  modfile = rec->frec.pre_rev('.') + Bstring(part->id, "_%03d.star");
227  }
228  if ( partfile.length() ) {
229  ct->fmod = newpartfile;
230  if ( flags & 1 ) {
231  if ( ( p = read_img(partfile, 1, ct->num) ) == NULL ) {
232  error_show("project_model_generate", __FILE__, __LINE__);
233  return NULL;
234  }
235  ct->num = 0;
236  if ( p->background(long(0)) == 0 )
238  origin = p->size()/2;
239  shift = origin - part->ori;
240  p->origin(part->ori);
241  p->rotate(shift, part->view);
242  part->view = View(0,0,1,0);
243  part->ori = origin;
244  write_img(ct->fmod, p, 0);
245  delete p;
246  }
247  }
248  if ( flags & 2 ) {
249  ct->fmod = modfile;
250  ct->num = 0;
251  if ( temp ) {
252  partmod = model_copy(temp);
253  partmod->id = comp->id;
254 // origin = 0;
255  mat = part->view.matrix();
256  mat = mat.transpose();
257  model_rotate(partmod, mat, origin, shift);
258  } else partmod = model_init(comp->id);
259  partmod->fom = part->fom[0];
260  partmod->sel = part->sel;
261  partmod->fmap = partfile;
262  if ( flags & 1 ) partmod->fmap = newpartfile;
263 // partmod->comment = mp->comment;
264  write_model(modfile, partmod);
265  model_kill(partmod);
266  }
267  if ( verbose )
268  cout << rec->id << tab << part->id << tab << comp->id << endl;
269  }
270  }
271 
272  return model;
273 }
274 
290 {
291  Breconstruction* rec;
292  Bparticle* part;
293  Bmodel* mp;
294  Bstring type_id;
295  Vector3<double> origin;
296 
297  if ( verbose )
298  cout << "Consolidating particles with models:" << endl << "Reconstruction\tParticle\tModel" << endl;
299  for ( rec = project->rec, mp = model; rec; rec = rec->next ) {
300  for ( part = rec->part; part && mp; part = part->next, mp = mp->next ) {
301  if ( verbose )
302  cout << rec->id << tab << part->id << tab << mp->id << endl;
303 // type_id = mp->type_id.remove('_') + Bstring((mp->hand<0)?2:mp->hand, "%1d");
304 // part->group = type_id.integer();
305 // if ( part->group < 10 ) part->group = part->id;
306 // mp->id = rec->id + Bstring(part->id, "_%04d");
307  if ( part->fpart.length() ) mp->fmap = part->fpart;
308 // model_shift(mp, (part->loc - rec->origin)*rec->voxel_size);
309  model_rotate(mp, part->view, origin, (part->ori - rec->box_size/2)*rec->voxel_size);
310  }
311  }
312 
313  return 0;
314 }
315 
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
Breconstruction::part
Bparticle * part
Definition: mg_processing.h:307
Breconstruction::id
Bstring id
Definition: mg_processing.h:285
Breconstruction::mark
Bmarker * mark
Definition: mg_processing.h:310
Bmodel::fom
float fom
Definition: rwmodel.h:128
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
Bcomptype::fmod
Bstring fmod
Definition: rwmodel.h:29
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
Bstring
Definition: Bstring.h:29
Breconstruction::voxel_size
Vector3< double > voxel_size
Definition: mg_processing.h:295
main
int main(int argc, char **argv)
Definition: brecmod.cpp:53
Bparticle
Definition: mg_processing.h:90
verbose
int verbose
Bcomponent::id
Bstring id
Definition: rwmodel.h:53
Matrix3::transpose
Matrix3 transpose() const
Definition: Matrix3.h:268
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:436
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
Breconstruction::fom
double fom
Definition: mg_processing.h:305
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
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
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:719
Bparticle::fom
double fom[NFOM]
Definition: mg_processing.h:103
Bimage::background
double background(long nn)
Definition: Bimage.h:549
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
Breconstruction::box_size
Vector3< long > box_size
Definition: mg_processing.h:299
model_copy
Bmodel * model_copy(Bmodel *model)
Copies a model.
Definition: rwmodel.cpp:688
options.h
Header file for option handlers.
Bimage::calculate_background
int calculate_background(long nn, int flag)
Calculates the background for one sub-image.
Definition: Bimage_background.cpp:27
View
Definition: View.h:26
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:181
Bmodel::fmap
Bstring fmap
Definition: rwmodel.h:125
marker.h
Header file for image parameter functions.
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
Matrix3
Definition: Matrix3.h:18
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.
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
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
View::backward
View backward()
Definition: View.h:118
Bparticle::fpart
Bstring fpart
Definition: mg_processing.h:92
Bmodel::id
Bstring id
Definition: rwmodel.h:121
Bimage::rotate
int rotate()
Rotates an image using parameters defined in the image in place.
Definition: Bimage_transform.cpp:255
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:421
model_kill
int model_kill(Bmodel *model)
Deallocates all memory in the list.
Definition: rwmodel.cpp:1127
project_model_consolidate
int project_model_consolidate(Bproject *project, Bmodel *model)
Exchanges information between reconstruction and model structures.
Definition: brecmod.cpp:289
Bimage
General image parameter class.
Definition: Bimage.h:177
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
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
use
const char * use[]
Definition: brecmod.cpp:27
utilities.h
Header file for general utilities.
write_model
int write_model(Bstring &filename, Bmodel *model)
Writes model parameters.
Definition: rwmodel.cpp:92
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
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
Breconstruction::select
long select
Definition: mg_processing.h:286
Bcomponent::view
View view
Definition: rwmodel.h:59
Bcomptype::num
int num
Definition: rwmodel.h:30
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
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
Bcomponent::type
Bcomptype * type
Definition: rwmodel.h:54
model
Bmodel * model
Definition: bshow.cpp:34
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
Bparticle::ori
Vector3< double > ori
Definition: mg_processing.h:101
mg_processing.h
Header file for micrograph processing.
Bparticle::next
Bparticle * next
Definition: mg_processing.h:91
linked_list.h
Header file for generalized linked list functions.
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_transform.h
Library routines used for model transformation.
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
read_model
Bmodel * read_model(Bstring &filename)
Definition: rwmodel.cpp:29
View::matrix
Matrix3 matrix()
Definition: View.h:153
model_rotate
long model_rotate(Bmodel *model, Matrix3 mat)
Rotates the model.
Definition: model_transform.cpp:238
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:713
Bparticle::id
int id
Definition: mg_processing.h:93
Bcomponent::sel
int sel
Definition: rwmodel.h:66
model_from_markers
Bmodel * model_from_markers(Bmarker *markers, Vector3< double > origin, Vector3< double > sam)
Convert markers to a model.
Definition: marker.cpp:1052
Breconstruction::next
Breconstruction * next
Definition: mg_processing.h:284