Bsoft  2.1.0
Bernard's software package
rwmgIMOD.cpp
Go to the documentation of this file.
1 
9 #include "rwmgIMOD.h"
10 #include "mg_img_proc.h"
11 #include "mg_tomography.h"
12 #include "rwmg.h"
13 #include "rwimg.h"
14 #include "linked_list.h"
15 
16 // Declaration of global variables
17 extern int verbose; // Level of output to the screen
18 
19 
21 {
22  Bmicrograph* mg = NULL;
23  double a[4], dx, dy;
24 
25  ifstream fprm(filename.c_str());
26  if ( fprm.fail() ) {
27  cerr << "Error: A text file specifying 2D transformations must be given!" << endl;
28  return -1;
29  }
30 
31  if ( verbose & VERB_PROCESS )
32  cout << "Reading 2D transformations from " << filename << endl;
33 
34  for ( mg = project->field->mg; mg; mg = mg->next ) {
35  fprm >> a[0] >> a[1] >> a[2] >> a[3] >> dx >> dy;
36  mg->tilt_axis = (atan2(-a[0], a[1]) + atan2(-a[3], -a[2]))/2.0;
37  mg->origin[0] -= a[0]*dx + a[2]*dy;
38  mg->origin[1] -= a[1]*dx + a[3]*dy;
39  if ( verbose )
40  cout << mg->origin << tab << mg->tilt_axis*180.0/M_PI << endl;
41  }
42 
43  fprm.close();
44 
45  return 0;
46 }
47 
49 {
50  Bmicrograph* mg = NULL;
51  double ta;
52 
53  ifstream fprm(filename.c_str());
54  if ( fprm.fail() ) {
55  cerr << "Error: A text file specifying tilt angles must be given!" << endl;
56  return -1;
57  }
58 
59  if ( verbose & VERB_PROCESS )
60  cout << "Reading tilt angles from " << filename << endl;
61 
62  for ( mg = project->field->mg; mg; mg = mg->next ) {
63  fprm >> ta;
64  mg->tilt_angle = ta*M_PI/180.0;
65  if ( verbose )
66  cout << mg->tilt_angle*180.0/M_PI << endl;
67  }
68 
69  fprm.close();
70 
71  return 0;
72 }
73 
75 {
76  int mark_id(0), first(1);
77  double x, y, z, dum;
78  string word;
79  Bmarker* mark = NULL;
80 
81  ifstream fprm(filename.c_str());
82  if ( fprm.fail() ) {
83  cerr << "Error: A text file specifying the 3D marker model must be given!" << endl;
84  return -1;
85  }
86 
87  fprm >> mark_id >> x >> y >> z >> dum >> dum;
88  while ( fprm >> word ) {
89  if ( word == "Pix:" ) fprm >> rec->voxel_size[0];
90  if ( word == "Dim:" ) break;
91  }
92  fprm >> rec->origin[0] >> rec->origin[1];
93  rec->origin /= 2;
94 
95  if ( access(rec->frec.c_str(), R_OK) ) {
96  if ( verbose )
97  cout << "Getting reconstruction parameters from " << filename << endl;
98  } else {
99  if ( verbose )
100  cout << "Getting reconstruction parameters from " << rec->frec << endl;
101  Bimage* p = read_img(rec->frec, 0, -1);
102  rec->voxel_size = p->sampling(0);
103  rec->origin = p->size()/2;
104  delete p;
105  }
106  if ( verbose )
107  cout << setprecision(3) << "Voxel size = " << rec->voxel_size << " Origin = " << rec->origin << endl;
108 
109  while ( !fprm.eof() ) {
110  if ( first ) first = 0;
111  else fprm >> mark_id >> x >> y >> z >> dum >> dum;
112  if ( fprm.fail() ) break;
113  mark = (Bmarker *) add_item((char **) &mark, sizeof(Bmarker));
114  if ( !rec->mark ) rec->mark = mark;
115  mark->id = mark_id;
116 // mark->loc = Vector3<double>(x - rec->origin[0], y - rec->origin[1], z);
117  mark->loc = Vector3<double>(x, y, z);
118  mark->fom = 1;
119  mark->sel = 1;
120  if ( verbose )
121  cout << mark_id << tab << x << tab << y << tab << z << endl;
122  }
123 
124  fprm.close();
125 
126  return 0;
127 }
128 
130 {
131  Bmicrograph* mg = NULL;
132  Bmarker* mark = NULL;
133 
134  ifstream fprm(filename.c_str());
135  if ( fprm.fail() ) {
136  cerr << "Error: A binary IMOD file specifying the fiducial markers must be given!" << endl;
137  return -1;
138  }
139 
140  if ( verbose & VERB_PROCESS )
141  cout << "Reading markers from " << filename << endl;
142 
143  long i, j, psize;
144  char tag[8];
145  IMOD* imod = new IMOD;
146  OBJT* objt = new OBJT;
147  CONT* cont = new CONT;
148  float* point;
149 
150  tag[4] = 0;
151  fprm.read((char *)tag, 4);
152  if ( fprm.fail() ) return -2;
153  cout << tag << tab;
154  fprm.read((char *)tag, 4);
155  if ( fprm.fail() ) return -2;
156  cout << tag << endl;
157 
158  fprm.read((char *)imod, IMODSIZE);
159  if ( fprm.fail() ) return -2;
160 
161  swapbytes((unsigned char *) &imod->objsize, 4);
162 
163  if ( verbose & VERB_PROCESS )
164  cout << "number of objects = " << imod->objsize << endl;
165 
166  fprm.read((char *)tag, 4);
167  if ( fprm.fail() ) return -2;
168 
169  fprm.read((char *)objt, OBJTSIZE);
170  if ( fprm.fail() ) return -2;
171 
172  swapbytes((unsigned char *) &objt->contsize, 4);
173 
174  if ( verbose & VERB_PROCESS )
175  cout << "number of contours = " << objt->contsize << endl;
176 
177  for ( i=0; i<objt->contsize; i++ ) {
178  fprm.read((char *)tag, 4);
179  if ( fprm.fail() ) return -2;
180 
181  fprm.read((char *)cont, CONTSIZE);
182  if ( fprm.fail() ) return -2;
183 
184  swapbytes((unsigned char *) &cont->psize, 4);
185 
186  if ( verbose & VERB_PROCESS )
187  cout << "number of points = " << cont->psize << endl;
188 
189  psize = 3*cont->psize*sizeof(float);
190  point = new float[3*cont->psize];
191 
192  fprm.read((char *)point, psize);
193  if ( fprm.fail() ) return -2;
194 
195  swapbytes(psize, (unsigned char *) point, 4);
196 
197  for ( j=0, mg = project->field->mg; mg; mg = mg->next, j+=3 ) {
198  mark = (Bmarker *) add_item((char **) &mg->mark, sizeof(Bmarker));
199  mark->id = i+1;
200  mark->loc = Vector3<double>(point[j], point[j+1], 0);
201  mark->fom = 1;
202  mark->sel = 1;
203  if ( verbose )
204  cout << mg->id << tab << mark->id << tab << mark->loc << endl;
205  }
206 
207  delete[] point;
208  }
209 
210  delete imod;
211  delete objt;
212  delete cont;
213 
214  fprm.close();
215 
216  return 0;
217 }
218 
242 int read_project_imod(Bstring* file_list, Bproject* project, int flag)
243 {
244  if ( file_list->empty() ) {
245  cerr << "No filenames given to create a project!" << endl;
246  exit(-1);
247  }
248 
249  Bimage* p = NULL;
250  Bstring base, file, imgfile;
251  Bstring* thisfile = NULL;
252  Bstring ext;
253  int n;
254  Bstring mg_id, rec_id;
255  Bfield* field = NULL;
256  Bmicrograph* mg = NULL;
257  Breconstruction* rec = NULL;
258 
259 
260  // Set up the overall project based on the provided images
261  for ( thisfile = file_list; thisfile; thisfile = thisfile->next ) {
262  file = thisfile->pre_rev(':');
263  ext = file.extension();
264  if ( verbose & VERB_DEBUG )
265  cout << "DEBUG read_project_imod: reading " << file << endl;
266  if ( ext.contains("st") || ext.contains("ali") || ext.contains("rec") ) {
267  imgfile = file;
268  base = imgfile.base();
269  p = read_img(imgfile, 0, -1);
270  if ( !p ) {
271  cerr << "Error: Image file " << imgfile << " not read!" << endl;
272  error_show("read_project_imod", __FILE__, __LINE__);
273  return -1;
274  }
275  if ( ext.contains("rec") ) {
277  rec->frec = imgfile;
278  rec->voxel_size = p->sampling(0);
279  rec->origin = p->size()/2;
280  } else {
281  if ( p->sizeZ() > 1 ) p->slices_to_images();
282  field = field_add(&project->field, base);
283  for ( n=0; n<p->images(); n++ ) {
284  mg_id = base + Bstring(n+1, "_%03d");
285  mg = micrograph_add(&mg, mg_id);
286  if ( !field->mg ) field->mg = mg;
287  mg->block = n;
288  mg->img_num = n;
289  mg->fmg = imgfile;
290  mg->pixel_size = p->sampling(0);
291  mg->intensity = p->image[n].average();
292  mg->origin = p->size()/2;
293  mg->mark_radius = 10;
294  }
295  if ( verbose & VERB_FULL ) {
296  cout << "Creating field " << field->id << ", with " << n << " micrographs" << endl;
297  cout << "Pixel size: " << field->mg->pixel_size << endl;
298  cout << "Origin: " << field->mg->origin << endl;
299  }
300  }
301  delete p;
302  }
303  }
304 
305  if ( imgfile.length() < 1 ) {
306  cerr << "Error: No image file provided!" << endl;
307  error_show("read_project_imod", __FILE__, __LINE__);
308  return -2;
309  }
310 
311  int err(0);
312 
313  for ( thisfile = file_list; thisfile; thisfile = thisfile->next ) {
314  ext = thisfile->extension();
315  if ( ext.contains("xf") ) {
316  err += read_imod_xf(*thisfile, project);
317  } else if ( ext.contains("tlt") ) {
318  err += read_imod_tlt(*thisfile, project);
319  } else if ( ext.contains("xyz") ) {
320  if ( !rec ) {
322  rec->frec = base + ".rec";
323  }
324  err += read_imod_xyz(*thisfile, rec);
325  } else if ( ext.contains("fid") ) {
326  err += read_imod_fid(*thisfile, project);
327  }
328  }
329 
331 
332  return err;
333 }
334 
336 {
337  Bstring paramfile = imodfile.base() + ".xf";
338  Bmicrograph* mg = project->field->mg;
340 
341  ofstream fprm(paramfile.c_str());
342  if ( fprm.fail() ) {
343  error_show("write_imod_xf", __FILE__, __LINE__);
344  return -1;
345  }
346 
347  if ( verbose & VERB_PROCESS )
348  cout << "Writing 2D transformations to " << paramfile << endl;
349 
350  double a[4];
351 
352  for ( mg = project->field->mg; mg; mg = mg->next ) {
353  a[0] = a[3] = -sin(mg->tilt_axis);
354  a[1] = cos(mg->tilt_axis);
355  a[2] = -cos(mg->tilt_axis);
356  d = origin - mg->origin;
357  fprm << setprecision(7);
358  for ( int i=0; i<4; ++i ) fprm << " " << setw(12) << a[i];
359  fprm << setprecision(3) << " " << setw(12) << a[0]*d[0] + a[1]*d[1]
360  << " " << setw(12) << a[2]*d[0] + a[3]*d[1] << endl;
361 /* fprm << setprecision(7) << setw(12) << a[0]
362  << setw(12) << a[1]
363  << setw(12) << a[2]
364  << setw(12) << a[3]
365  << setprecision(3) << setw(12) << a[0]*d[0] + a[1]*d[1]
366  << setw(12) << a[2]*d[0] + a[3]*d[1] << endl;*/
367  }
368 
369  fprm.close();
370 
371  return 0;
372 }
373 
375 {
376  Bstring paramfile = imodfile.base() + ".tlt";
377  Bmicrograph* mg = project->field->mg;
378 
379  ofstream fprm(paramfile.c_str());
380  if ( fprm.fail() ) {
381  error_show("write_imod_tlt", __FILE__, __LINE__);
382  return -1;
383  }
384 
385  if ( verbose & VERB_PROCESS )
386  cout << "Writing tilt angles to " << paramfile << endl;
387 
388  for ( mg = project->field->mg; mg; mg = mg->next )
389  fprm << mg->tilt_angle*180.0/M_PI << endl;
390 
391  fprm.close();
392 
393  return 0;
394 }
395 
397 {
398  Bstring paramfile = imodfile.base() + "fid.xyz";
399 // Bmicrograph* mg = project->field->mg;
400  Breconstruction* rec = project->rec;
401  Bmarker* mark = NULL;
402 
403  ofstream fprm(paramfile.c_str());
404  if ( fprm.fail() ) {
405  error_show("write_project_imod", __FILE__, __LINE__);
406  return -1;
407  }
408 
409  if ( verbose & VERB_PROCESS )
410  cout << "Writing the 3D marker model to " << paramfile << endl;
411 
412  for ( mark = rec->mark; mark; mark = mark->next ) {
413  fprm << setw(4) << mark->id
414  << setprecision(2) << setw(10) << mark->loc[0] + rec->origin[0]
415  << setw(10) << mark->loc[1] + rec->origin[1]
416  << setw(10) << mark->loc[2]
417  << setw(7) << 1 << setw(5) << mark->id;
418  if ( mark == rec->mark )
419  fprm << " Pix: " << setprecision(5) << setw(11) << rec->voxel_size[0]
420  << " Dim: " << setprecision(0) << setw(6) << 2*rec->origin[0]
421  << setw(6) << 2*rec->origin[1];
422  fprm << endl;
423  }
424 
425  fprm.close();
426 
427  return 0;
428 }
429 
430 
431 
432 
453 {
454  int err(0);
455 
456  err += write_imod_xf(imodfile, project);
457 
458  err += write_imod_tlt(imodfile, project);
459 
460  if ( project->rec && project->rec->mark )
461  err += write_imod_xyz(imodfile, project);
462 
463 // err += write_imod_fid(imodfile, project);
464 
465  return err;
466 }
Bsub_image::average
double average()
Definition: Bimage.h:119
project_mg_tilt_to_matrix
int project_mg_tilt_to_matrix(Bproject *project)
Sets the matrix from the tilt and axis angles for each micrograph.
Definition: mg_tomography.cpp:2177
Bmicrograph::id
Bstring id
Definition: mg_processing.h:203
Bmicrograph
Definition: mg_processing.h:201
Breconstruction::mark
Bmarker * mark
Definition: mg_processing.h:310
Bmarker::fom
float fom
Definition: marker.h:28
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
Bproject::rec
Breconstruction * rec
Definition: mg_processing.h:347
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
CONT
Definition: rwmgIMOD.h:128
Bstring
Definition: Bstring.h:29
field_add
Bfield * field_add(Bfield **field, Bstring &field_id)
Adds a field-of-view parameter structure to a linked list.
Definition: mg_processing.cpp:121
add_item
char * add_item(char **list, unsigned long size)
Adds an item to a linked list.
Definition: linked_list.cpp:26
Breconstruction::voxel_size
Vector3< double > voxel_size
Definition: mg_processing.h:295
Bstring::extension
Bstring extension()
Definition: Bstring.cpp:523
micrograph_get_nominal_origin
Vector3< double > micrograph_get_nominal_origin(Bmicrograph *mg)
Gets the nominal origin for a micrograph.
Definition: mg_img_proc.cpp:741
Bproject::field
Bfield * field
Definition: mg_processing.h:346
base
string base(string &filename)
Definition: string_util.cpp:28
CONTSIZE
#define CONTSIZE
Definition: rwmgIMOD.h:13
Bmicrograph::tilt_angle
double tilt_angle
Definition: mg_processing.h:220
Vector3< double >
write_project_imod
int write_project_imod(Bstring &imodfile, Bproject *project)
Creates IMOD parameter files from a project structure.
Definition: rwmgIMOD.cpp:452
Bmicrograph::img_num
int img_num
Definition: mg_processing.h:212
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:719
Bstring::empty
bool empty()
Definition: Bstring.h:71
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
read_imod_tlt
int read_imod_tlt(Bstring &filename, Bproject *project)
Definition: rwmgIMOD.cpp:48
IMODSIZE
#define IMODSIZE
Definition: rwmgIMOD.h:11
read_imod_xyz
int read_imod_xyz(Bstring &filename, Breconstruction *rec)
Definition: rwmgIMOD.cpp:74
read_imod_fid
int read_imod_fid(Bstring &filename, Bproject *project)
Definition: rwmgIMOD.cpp:129
Bfield::id
Bstring id
Definition: mg_processing.h:262
Bfield
Definition: mg_processing.h:260
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
write_imod_xyz
int write_imod_xyz(Bstring &imodfile, Bproject *project)
Definition: rwmgIMOD.cpp:396
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bmarker
Definition: marker.h:21
Breconstruction::origin
Vector3< double > origin
Definition: mg_processing.h:296
Bimage::images
long images()
Definition: Bimage.h:504
rwmgIMOD.h
Converts between IMOD files and a micrograph parameter file.
project
Bproject * project
Definition: bshow.cpp:33
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.
Bmarker::next
Bmarker * next
Definition: marker.h:22
Bimage::slices_to_images
int slices_to_images()
Changes the slices in a 3D image into a set of 2D images.
Definition: Bimage.cpp:2074
Bmarker::id
int id
Definition: marker.h:23
swapbytes
void swapbytes(unsigned char *v, size_t n)
Swaps bytes.
Definition: utilities.cpp:468
Bmicrograph::origin
Vector3< double > origin
Definition: mg_processing.h:223
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:421
Bmicrograph::tilt_axis
double tilt_axis
Definition: mg_processing.h:219
Bimage
General image parameter class.
Definition: Bimage.h:177
Bproject
Definition: mg_processing.h:336
IMOD
Definition: rwmgIMOD.h:15
write_imod_tlt
int write_imod_tlt(Bstring &imodfile, Bproject *project)
Definition: rwmgIMOD.cpp:374
mg_img_proc.h
Header file for image processing from micrograph structures.
OBJTSIZE
#define OBJTSIZE
Definition: rwmgIMOD.h:12
OBJT
Definition: rwmgIMOD.h:62
Bmicrograph::mark_radius
double mark_radius
Definition: mg_processing.h:238
Bmarker::loc
Vector3< float > loc
Definition: marker.h:25
M_PI
#define M_PI
Definition: utilities.h:63
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
Breconstruction
Definition: mg_processing.h:283
Bstring::length
long length() const
Definition: Bstring.cpp:263
Bmicrograph::fmg
Bstring fmg
Definition: mg_processing.h:206
read_imod_xf
int read_imod_xf(Bstring &filename, Bproject *project)
Definition: rwmgIMOD.cpp:20
read_project_imod
int read_project_imod(Bstring *file_list, Bproject *project, int flag)
Creates a project structure using IMOD parameters.
Definition: rwmgIMOD.cpp:242
rwimg.h
Header file for 2D and 3D image I/O.
Bstring::next
Bstring * next
Definition: Bstring.h:31
Bmicrograph::mark
Bmarker * mark
Definition: mg_processing.h:245
Bmicrograph::intensity
double intensity
Definition: mg_processing.h:217
Bmarker::sel
int sel
Definition: marker.h:29
Bmicrograph::block
int block
Definition: mg_processing.h:205
micrograph_add
Bmicrograph * micrograph_add(Bmicrograph **mg, Bstring &mg_id)
Adds a micrograph parameter structure to a linked list.
Definition: mg_processing.cpp:154
linked_list.h
Header file for generalized linked list functions.
verbose
int verbose
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
Breconstruction::frec
Bstring frec
Definition: mg_processing.h:290
Bstring::base
Bstring base()
Definition: Bstring.cpp:516
Bmicrograph::pixel_size
Vector3< double > pixel_size
Definition: mg_processing.h:215
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
reconstruction_add
Breconstruction * reconstruction_add(Breconstruction **rec, Bstring &rec_id)
Adds a reconstruction parameter structure to a linked list.
Definition: mg_processing.cpp:398
write_imod_xf
int write_imod_xf(Bstring &imodfile, Bproject *project)
Definition: rwmgIMOD.cpp:335
Bmicrograph::next
Bmicrograph * next
Definition: mg_processing.h:202
Bfield::mg
Bmicrograph * mg
Definition: mg_processing.h:267
mg_tomography.h
Header file for single particle image processing in Bsoft.