| Bsoft 2.0.5 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
20 int read_imod_xf(Bstring& filename, Bproject* project)
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
48 int read_imod_tlt(Bstring& filename, Bproject* project)
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
74 int read_imod_xyz(Bstring& filename, Breconstruction* rec)
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;
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
129 int read_imod_fid(Bstring& filename, Bproject* project)
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") ) {
276 rec = reconstruction_add(&project->rec, base);
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->origin = p->size()/2;
292 mg->mark_radius = 10;
293 }
294 if ( verbose & VERB_FULL ) {
295 cout << "Creating field " << field->id << ", with " << n << " micrographs" << endl;
296 cout << "Pixel size: " << field->mg->pixel_size << endl;
297 cout << "Origin: " << field->mg->origin << endl;
298 }
299 }
300 delete p;
301 }
302 }
303
304 if ( imgfile.length() < 1 ) {
305 cerr << "Error: No image file provided!" << endl;
306 error_show("read_project_imod", __FILE__, __LINE__);
307 return -2;
308 }
309
310 int err(0);
311
312 for ( thisfile = file_list; thisfile; thisfile = thisfile->next ) {
313 ext = thisfile->extension();
314 if ( ext.contains("xf") ) {
315 err += read_imod_xf(*thisfile, project);
316 } else if ( ext.contains("tlt") ) {
317 err += read_imod_tlt(*thisfile, project);
318 } else if ( ext.contains("xyz") ) {
319 if ( !rec ) {
320 rec = reconstruction_add(&project->rec, base);
321 rec->frec = base + ".rec";
322 }
323 err += read_imod_xyz(*thisfile, rec);
324 } else if ( ext.contains("fid") ) {
325 err += read_imod_fid(*thisfile, project);
326 }
327 }
328
329 project_mg_tilt_to_matrix(project);
330
331 return err;
332 }
333
334 int write_imod_xf(Bstring& imodfile, Bproject* project)
335 {
336 Bstring paramfile = imodfile.base() + ".xf";
337 Bmicrograph* mg = project->field->mg;
338 Vector3<double> d, origin = micrograph_get_nominal_origin(mg);
339
340 ofstream fprm(paramfile.c_str());
341 if ( fprm.fail() ) {
342 error_show("write_imod_xf", __FILE__, __LINE__);
343 return -1;
344 }
345
346 if ( verbose & VERB_PROCESS )
347 cout << "Writing 2D transformations to " << paramfile << endl;
348
349 double a[4];
350
351 for ( mg = project->field->mg; mg; mg = mg->next ) {
352 a[0] = a[3] = -sin(mg->tilt_axis);
353 a[1] = cos(mg->tilt_axis);
354 a[2] = -cos(mg->tilt_axis);
355 d = origin - mg->origin;
356 fprm << setprecision(7);
357 for ( int i=0; i<4; ++i ) fprm << " " << setw(12) << a[i];
358 fprm << setprecision(3) << " " << setw(12) << a[0]*d[0] + a[1]*d[1]
359 << " " << setw(12) << a[2]*d[0] + a[3]*d[1] << endl;
360 /* fprm << setprecision(7) << setw(12) << a[0]
361 << setw(12) << a[1]
362 << setw(12) << a[2]
363 << setw(12) << a[3]
364 << setprecision(3) << setw(12) << a[0]*d[0] + a[1]*d[1]
365 << setw(12) << a[2]*d[0] + a[3]*d[1] << endl;*/
366 }
367
368 fprm.close();
369
370 return 0;
371 }
372
373 int write_imod_tlt(Bstring& imodfile, Bproject* project)
374 {
375 Bstring paramfile = imodfile.base() + ".tlt";
376 Bmicrograph* mg = project->field->mg;
377
378 ofstream fprm(paramfile.c_str());
379 if ( fprm.fail() ) {
380 error_show("write_imod_tlt", __FILE__, __LINE__);
381 return -1;
382 }
383
384 if ( verbose & VERB_PROCESS )
385 cout << "Writing tilt angles to " << paramfile << endl;
386
387 for ( mg = project->field->mg; mg; mg = mg->next )
388 fprm << mg->tilt_angle*180.0/M_PI << endl;
389
390 fprm.close();
391
392 return 0;
393 }
394
395 int write_imod_xyz(Bstring& imodfile, Bproject* project)
396 {
397 Bstring paramfile = imodfile.base() + "fid.xyz";
398 // Bmicrograph* mg = project->field->mg;
399 Breconstruction* rec = project->rec;
400 Bmarker* mark = NULL;
401
402 ofstream fprm(paramfile.c_str());
403 if ( fprm.fail() ) {
404 error_show("write_project_imod", __FILE__, __LINE__);
405 return -1;
406 }
407
408 if ( verbose & VERB_PROCESS )
409 cout << "Writing the 3D marker model to " << paramfile << endl;
410
411 for ( mark = rec->mark; mark; mark = mark->next ) {
412 fprm << setw(4) << mark->id
413 << setprecision(2) << setw(10) << mark->loc[0] + rec->origin[0]
414 << setw(10) << mark->loc[1] + rec->origin[1]
415 << setw(10) << mark->loc[2]
416 << setw(7) << 1 << setw(5) << mark->id;
417 if ( mark == rec->mark )
418 fprm << " Pix: " << setprecision(5) << setw(11) << rec->voxel_size
419 << " Dim: " << setprecision(0) << setw(6) << 2*rec->origin[0]
420 << setw(6) << 2*rec->origin[1];
421 fprm << endl;
422 }
423
424 fprm.close();
425
426 return 0;
427 }
428
429
430
431
451 int write_project_imod(Bstring& imodfile, Bproject* project)
452 {
453 int err(0);
454
455 err += write_imod_xf(imodfile, project);
456
457 err += write_imod_tlt(imodfile, project);
458
459 if ( project->rec && project->rec->mark )
460 err += write_imod_xyz(imodfile, project);
461
462 // err += write_imod_fid(imodfile, project);
463
464 return err;
465 }
rwmg.h
Header file for reading and writing micrograph parameters.
CONTSIZE
#define CONTSIZE
Definition: rwmgIMOD.h:13
Bproject::rec
Breconstruction * rec
Definition: mg_processing.h:344
rwmgIMOD.h
Converts between IMOD files and a micrograph parameter file.
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
Breconstruction::mark
Bmarker * mark
Definition: mg_processing.h:307
Bimage::slices_to_images
int slices_to_images()
Changes the slices in a 3D image into a set of 2D images.
Definition: Bimage.cpp:2052
Bmicrograph::id
Bstring id
Definition: mg_processing.h:202
Bmicrograph::img_num
int img_num
Definition: mg_processing.h:211
read_imod_xf
int read_imod_xf(Bstring &filename, Bproject *project)
Definition: rwmgIMOD.cpp:20
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Breconstruction
Definition: mg_processing.h:280
mg_img_proc.h
Header file for image processing from micrograph structures.
read_imod_xyz
int read_imod_xyz(Bstring &filename, Breconstruction *rec)
Definition: rwmgIMOD.cpp:74
Bmarker
Definition: marker.h:21
add_item
char * add_item(char **list, unsigned long size)
Adds an item to a linked list.
Definition: linked_list.cpp:26
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
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
read_imod_tlt
int read_imod_tlt(Bstring &filename, Bproject *project)
Definition: rwmgIMOD.cpp:48
Bmicrograph::tilt_angle
double tilt_angle
Definition: mg_processing.h:217
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
IMODSIZE
#define IMODSIZE
Definition: rwmgIMOD.h:11
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
Bimage
General image parameter class.
Definition: Bimage.h:163
IMOD
Definition: rwmgIMOD.h:15
Bmicrograph::tilt_axis
double tilt_axis
Definition: mg_processing.h:216
reconstruction_add
Breconstruction * reconstruction_add(Breconstruction **rec, Bstring &rec_id)
Adds a reconstruction parameter structure to a linked list.
Definition: mg_processing.cpp:704
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:584
Bfield
Definition: mg_processing.h:257
M_PI
#define M_PI
Definition: utilities.h:59
linked_list.h
Header file for generalized linked list functions.
base
string base(string &filename)
Definition: string_util.cpp:28
Bfield::id
Bstring id
Definition: mg_processing.h:259
Bstring::next
Bstring * next
Definition: Bstring.h:31
Bmicrograph::mark_radius
double mark_radius
Definition: mg_processing.h:235
OBJTSIZE
#define OBJTSIZE
Definition: rwmgIMOD.h:12
Bproject::field
Bfield * field
Definition: mg_processing.h:343
Bstring::extension
Bstring extension()
Definition: Bstring.cpp:515
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:1960
Bmicrograph
Definition: mg_processing.h:200
mg_tomography.h
Header file for single particle image processing in Bsoft.
Bmarker::fom
float fom
Definition: marker.h:28
Bmicrograph::origin
Vector3< double > origin
Definition: mg_processing.h:220
read_imod_fid
int read_imod_fid(Bstring &filename, Bproject *project)
Definition: rwmgIMOD.cpp:129
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
Bmarker::id
int id
Definition: marker.h:23
write_imod_tlt
int write_imod_tlt(Bstring &imodfile, Bproject *project)
Definition: rwmgIMOD.cpp:373
swapbytes
void swapbytes(unsigned char *v, size_t n)
Swaps bytes.
Definition: utilities.cpp:504
OBJT
Definition: rwmgIMOD.h:62
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bmicrograph::mark
Bmarker * mark
Definition: mg_processing.h:242
Bmarker::next
Bmarker * next
Definition: marker.h:22
Bfield::mg
Bmicrograph * mg
Definition: mg_processing.h:264
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:120
Bstring::base
Bstring base()
Definition: Bstring.cpp:508
Bmicrograph::block
int block
Definition: mg_processing.h:204
Bstring
Definition: Bstring.h:29
write_project_imod
int write_project_imod(Bstring &imodfile, Bproject *project)
Creates IMOD parameter files from a project structure.
Definition: rwmgIMOD.cpp:451
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:379
Vector3< double >
Bmicrograph::fmg
Bstring fmg
Definition: mg_processing.h:205
micrograph_get_nominal_origin
Vector3< double > micrograph_get_nominal_origin(Bmicrograph *mg)
Gets the nominal origin for a micrograph.
Definition: mg_img_proc.cpp:631
Bmarker::loc
Vector3< float > loc
Definition: marker.h:25
verbose
int verbose
Breconstruction::frec
Bstring frec
Definition: mg_processing.h:287
Breconstruction::voxel_size
double voxel_size
Definition: mg_processing.h:292
write_imod_xyz
int write_imod_xyz(Bstring &imodfile, Bproject *project)
Definition: rwmgIMOD.cpp:395
rwimg.h
Header file for 2D and 3D image I/O.
write_imod_xf
int write_imod_xf(Bstring &imodfile, Bproject *project)
Definition: rwmgIMOD.cpp:334
Bimage::images
long images()
Definition: Bimage.h:491
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:413
Bstring::empty
bool empty()
Definition: Bstring.h:71
VERB_FULL
#define VERB_FULL
Definition: utilities.h:109
micrograph_add
Bmicrograph * micrograph_add(Bmicrograph **mg, Bstring &mg_id)
Adds a micrograph parameter structure to a linked list.
Definition: mg_processing.cpp:461
Bmarker::sel
int sel
Definition: marker.h:29
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
CONT
Definition: rwmgIMOD.h:128
Bmicrograph::next
Bmicrograph * next
Definition: mg_processing.h:201