| Bsoft 2.0.5 Bernard's software package

rwmgRELION.cpp
Go to the documentation of this file.
1
9 #include "mg_processing.h"
10 #include "rwmgSTAR.h"
11 #include "rwstar.h"
12 #include "mg_tags.h"
13 #include "rwimg.h"
14 #include "linked_list.h"
15 #include "utilities.h"
16
17 #include <sys/stat.h>
18 #include <fcntl.h>
19
20 // Declaration of global variables
21 extern int verbose; // Level of output to the screen
22
23 int relion_to_project(Bstar* star, Bproject* project)
24 {
25 int err(0);
26 long i, pid(1);
27 Bstring pfile;
28 double px(1), mag(1), defU, defV, prev_defU(0), volt(12e4), Cs(2e7), amp(0.07);
29
30 Bstar_block* block;
31 Bstar_item* item;
32 Bstring* data;
33
34 Bstring field_id("1"), mg_id("1");
35 Bfield* field = field_add(&project->field, field_id);
36 Bmicrograph* mg = NULL;
37 Bparticle* partlist = NULL;
38 Bparticle* part = NULL;
39 Bparticle* prev_part = NULL;
40
41 Bstring* mg_name_list = NULL;
42 Bstring* mg_name = NULL;
43
44 for ( block = star->block; block; block = block->next ) {
45 for ( pid=1, data=block->item->data; data; data=data->next, pid++ ) {
46 part = particle_add(&part, pid);
47 if ( !partlist ) partlist = part;
48 }
49 if ( ( item = item_find(block, "rlnMicrographName") ) ) {
50 for ( data=item->data; data; data=data->next ) {
51 mg_name = string_add(&mg_name, *data);
52 if ( !mg_name_list ) mg_name_list = mg_name;
53 }
54 }
55 if ( ( item = item_find(block, "rlnImageName") ) ) {
56 for ( data=item->data, part=partlist; data && part; data=data->next, part=part->next ) {
57 part->id = data->pre('@').integer();
58 pfile = data->post('@');
59 }
60 }
61 if ( ( item = item_find(block, "rlnCoordinateX") ) ) {
62 for ( data=item->data, part=partlist; data && part; data=data->next, part=part->next )
63 part->loc[0] = data->real();
64 }
65 if ( ( item = item_find(block, "rlnCoordinateY") ) ) {
66 for ( data=item->data, part=partlist; data && part; data=data->next, part=part->next )
67 part->loc[1] = data->real();
68 }
69 if ( ( item = item_find(block, "rlnDetectorPixelSize") ) ) {
70 px = item->data->real();
71 }
72 if ( ( item = item_find(block, "rlnMagnification") ) ) {
73 mag = item->data->real();
74 }
75 if ( ( item = item_find(block, "rlnDefocusU") ) ) {
76 for ( data=item->data, part=partlist; data && part; data=data->next, part=part->next )
77 part->def = data->real();
78 }
79 if ( ( item = item_find(block, "rlnDefocusV") ) ) {
80 for ( data=item->data, part=partlist; data && part; data=data->next, part=part->next )
81 part->dev = data->real();
82 }
83 if ( ( item = item_find(block, "rlnDefocusAngle") ) ) {
84 for ( data=item->data, part=partlist; data && part; data=data->next, part=part->next )
85 part->ast = data->real() * M_PI/180.0;
86 }
87 if ( ( item = item_find(block, "rlnVoltage") ) ) {
88 volt = item->data->real() * 1e3;
89 }
90 if ( ( item = item_find(block, "rlnSphericalAberration") ) ) {
91 Cs = item->data->real() * 1e7;
92 }
93 if ( ( item = item_find(block, "rlnAmplitudeContrast") ) ) {
94 amp = item->data->real();
95 }
96 }
97
98 if ( verbose )
99 cout << "Particles: " << pid-1 << endl;
100
101 // Set up the micrographs
102 for ( i=1, part = partlist, mg_name=mg_name_list; part; part = part->next, ++i ) {
103 defU = part->def;
104 defV = part->dev;
105 part->def = (defU + defV)/2;
106 part->dev = fabs(defU - defV)/2;
107 if ( defU != prev_defU ) {
108 mg_id = Bstring(i, "%04d");
109 mg = micrograph_add(&mg, mg_id);
110 if ( !field->mg ) field->mg = mg;
111 if ( mg_name ) mg->fmg = *mg_name;
112 if ( pfile.length() ) mg->fpart = pfile;
113 mg->sampling = px*1e4; // Detector pxel size in micron
114 if ( mag ) {
115 mg->magnification = mag;
116 mg->pixel_size = 1e4*px/mag;
117 }
118 mg->ctf = new CTFparam;
119 mg->ctf->volt(volt);
120 mg->ctf->Cs(Cs);
121 mg->ctf->amp_shift(asin(amp));
122 mg->ctf->defocus_average(part->def);
123 mg->ctf->defocus_deviation(part->dev);
124 mg->ctf->astigmatism_angle(part->ast);
125 mg->ctf->zero(1);
126 mg->part = part;
127 if ( prev_part ) prev_part->next = NULL;
128 pid = 1;
129 }
130 part->pixel_size = mg->pixel_size;
131 prev_defU = defU;
132 prev_part = part;
133 if ( mg_name ) mg_name = mg_name->next;
134 }
135
136 if ( verbose )
137 cout << "Micrographs: " << mg_id.integer() << endl;
138
139 // Reconfigure the particle files
140 Bimage* p = NULL;
141
142 if ( pfile.length() ) p = read_img(pfile, 0, 0);
143
144 for ( mg = field->mg; mg; mg = mg->next ) {
145 for ( pid=1, part = mg->part; part; part = part->next, pid++ ) {
146 part->id = pid;
147 if ( p ) {
148 part->ori = p->size()/2;
149 part->pixel_size = p->sampling()[0];
150 }
151 if ( part->mag < 0.5 ) part->mag = 1;
152 }
153 if ( p ) {
154 mg->fpart = pfile;
155 mg->pixel_size = p->sampling()[0];
156 mg->box_size = p->size();
157 }
158 }
159
160 if ( p ) delete p;
161
162 return err;
163 }
164
165 int micrograph_to_relionblock(Bmicrograph* mg, Bstar_block* block)
166 {
167 Bparticle* part = mg->part;
168
169 item_put_list(block, "rlnCoordinateX", (char *) part,
170 (char *)&part->loc[0] - (char *)part, "%7.2lf");
171 item_put_list(block, "rlnCoordinateY", (char *) part,
172 (char *)&part->loc[1] - (char *)part, "%7.2lf");
173
174 return 0;
175 }
176
177 /*
178 loop_
179 _rlnMicrographNameNoDW #1
180 _rlnMicrographName #2
181 _rlnCtfImage #3
182 _rlnDefocusU #4
183 _rlnDefocusV #5
184 _rlnDefocusAngle #6
185 _rlnVoltage #7
186 _rlnSphericalAberration #8
187 _rlnAmplitudeContrast #9
188 _rlnMagnification #10
189 _rlnDetectorPixelSize #11
190 _rlnCtfFigureOfMerit #12
191 _rlnCtfMaxResolution #13
192 MotionCorr/job161/Micrographs/20171002_1000_A012_G000_H1000_D001_noDW.mrc MotionCorr/job161/Micrographs/20171002_1000_A012_G000_H1000_D001.mrc CtfFind/job163/Micrographs/20171002_1000_A012_G000_H1000_D001_noDW.ctf:mrc 23997.070312 23977.537109 59.369221 300.000000 2.700000 0.070000 10000.000000 0.660000 -0.004115 7.167600
193
194 loop_
195 _rlnCoordinateX #1
196 _rlnCoordinateY #2
197 _rlnClassNumber #3
198 _rlnAutopickFigureOfMerit #4
199 _rlnAnglePsi #5
200 _rlnImageName #6
201 _rlnMicrographName #7
202 _rlnVoltage #8
203 _rlnDefocusU #9
204 _rlnDefocusV #10
205 _rlnDefocusAngle #11
206 _rlnSphericalAberration #12
207 _rlnCtfBfactor #13
208 _rlnCtfScalefactor #14
209 _rlnPhaseShift #15
210 _rlnAmplitudeContrast #16
211 _rlnMagnification #17
212 _rlnDetectorPixelSize #18
213 _rlnCtfMaxResolution #19
214 _rlnCtfFigureOfMerit #20
215 6975.412944 852.888919 4 1.220089 165.000000 000001@Extract/job174/Micrographs/20171002_1000_A012_G000_H1000_D001.mrcs MotionCorr/job161/Micrographs/20171002_1000_A012_G000_H1000_D001.mrc 300.000000 23997.070312 23977.537109 59.369221 2.700000 0.000000 1.000000 0.000000 0.070000 10000.000000 1.980000 7.167600 -0.004115
216
217 */
218 int project_to_relion(Bproject* project, Bstar* star, int mg_select, int rec_select)
219 {
220 int err(0);
221 long nfield(0), nmg(0);
222 Bfield* field;
223 Bmicrograph* mg;
224
225 star->comment = project->comment;
226
227 Bstar_block* block = (Bstar_block *) add_item((char **)&star->block, sizeof(Bstar_block));
228
229 for ( field=project->field; field; field=field->next ) if ( mg_select < 1 || field->select > 0 ) {
230 for ( mg=field->mg; mg; mg=mg->next ) if ( mg_select < 1 || mg->select > 0 ) {
231 if ( verbose & VERB_FULL )
232 cout << "Writing field \"" << field->id << "\", micrograph \"" << mg->id << "\"" << endl;
233 micrograph_to_relionblock(mg, block);
234 nmg++;
235 }
236 nfield++;
237 }
238
239 return err;
240 }
241
242
249 int read_project_relion(Bstring& filename, Bproject* project)
250 {
251 int err(0);
252 Bstar* star = init_star();
253
254 if ( verbose ) cout << "Reading a Relion file: " << filename << endl;
255
256 err = read_star(filename, star);
257
258 if ( err == 0 ) {
259 if ( project->comment.length() < 1 )
260 project->comment = star->comment;
261 err = relion_to_project(star, project);
262 }
263
264 kill_star(star);
265
266 return err;
267 }
268
277 int write_project_relion(Bstring& filename, Bproject* project, int mg_select, int rec_select)
278 {
279 int err(0);
280 Bstar* star = init_star();
281 star->line_length = 160; // Set the output line length
282
283 if ( verbose ) cout << "Writing a Relion file: " << filename << endl;
284
285 err = project_to_relion(project, star, mg_select, rec_select);
286
287 if ( err == 0 )
288 err = write_star(filename, star);
289
290 kill_star(star);
291
292 return err;
293 }
294
303 int project_split_particles(Bproject* project, Bstring partfile, Bstring path, Bstring partext)
304 {
305 if ( path.length() && path[-1] != '/' ) path += "/";
306 if ( partext.length() < 1 ) partext = "pif";
307
308 if ( partfile.length() < 3 ) partfile = project->field->mg->fpart;
309 if ( partfile.length() < 3 ) partfile = project->field->mg->part->fpart;
310 if ( partfile.length() < 3 ) {
311 cerr << "Error: No particle image file specified" << endl;
312 bexit(-1);
313 }
314
315 // Directory for individual particle files
316 if ( path.length() ) {
317 mkdir(path.c_str(), O_CREAT );
318 chmod(path.c_str(), 0755);
319 }
320
321 Bfield* field = NULL;
322 Bmicrograph* mg = NULL;
323 Bparticle* part = NULL;
324
325 if ( verbose )
326 cout << "Splitting " << partfile << " into micrograph-associated particle files" << endl << endl;
327
328 Bimage* img_stack = read_img(partfile, 1, -1);
329 Bimage* img_part = NULL;
330
331 size_t n(0), m;
332 Bstring partname;
333
334 for ( field = project->field; field; field = field->next ) {
335 for ( mg = field->mg; mg; mg = mg->next ) {
336 partname = mg->fmg.post_rev('/').pre_rev('.') + "." + partext;
337 if ( path.length() ) partname = path + partname;
338 for ( m=0, part = mg->part; part; part = part->next, m++ )
339 part->ori = img_stack->size()/2;
340 if ( verbose )
341 cout << mg->id << tab << partname << tab << m << endl;
342 if ( m ) {
343 // cout << n << tab << m << endl;
344 img_part = img_stack->extract(n, n+m-1);
345 img_part->sampling(mg->pixel_size, mg->pixel_size, 1);
346 img_part->origin(img_part->size()/2);
347 write_img(partname, img_part, 0);
348 delete img_part;
349 mg->fpart = partname;
350 mg->box_size = img_stack->size();
351 n += m;
352 }
353 }
354 }
355
356 delete img_stack;
357
358
359 return 0;
360 }
361
362
363
364
Bparticle::id
int id
Definition: mg_processing.h:92
Bstar_item::data
Bstring * data
Definition: rwstar.h:43
read_star
int read_star(const char *filename, Bstar *star)
Reads paramaters and data into a STAR data base from a list of files.
Definition: rwstar.cpp:224
CTFparam::zero
double zero(int i)
Definition: ctf.h:218
CTFparam::Cs
double Cs()
Definition: ctf.h:87
Bstring::real
double real()
Definition: Bstring.cpp:545
Bparticle::pixel_size
double pixel_size
Definition: mg_processing.h:98
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:705
Bparticle::ast
double ast
Definition: mg_processing.h:97
Bstar_block::next
Bstar_block * next
Definition: rwstar.h:56
Bparticle::mag
double mag
Definition: mg_processing.h:94
Bstring::post
Bstring post(char c)
Definition: Bstring.cpp:394
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
micrograph_to_relionblock
int micrograph_to_relionblock(Bmicrograph *mg, Bstar_block *block)
Definition: rwmgRELION.cpp:165
Bmicrograph::id
Bstring id
Definition: mg_processing.h:202
Bmicrograph::sampling
double sampling
Definition: mg_processing.h:213
utilities.h
Header file for general utilities.
CTFparam::astigmatism_angle
double astigmatism_angle()
Definition: ctf.h:104
write_star
int write_star(const char *filename, Bstar *star)
Writes a STAR data base to one or more STAR format files.
Definition: rwstar.cpp:666
init_star
Bstar * init_star()
Creates a STAR data base.
Definition: rwstar.cpp:111
add_item
char * add_item(char **list, unsigned long size)
Adds an item to a linked list.
Definition: linked_list.cpp:26
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
Bstar
Definition: rwstar.h:78
kill_star
int kill_star(Bstar *star)
Destroys a STAR data base.
Definition: rwstar.cpp:132
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:76
relion_to_project
int relion_to_project(Bstar *star, Bproject *project)
Definition: rwmgRELION.cpp:23
Bfield::next
Bfield * next
Definition: mg_processing.h:258
Bmicrograph::part
Bparticle * part
Definition: mg_processing.h:239
Bstar_block::item
Bstar_item * item
Definition: rwstar.h:61
Bimage
General image parameter class.
Definition: Bimage.h:163
Bmicrograph::fpart
Bstring fpart
Definition: mg_processing.h:207
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.
project_split_particles
int project_split_particles(Bproject *project, Bstring partfile, Bstring path, Bstring partext)
Modifies some micrograph parameters.
Definition: rwmgRELION.cpp:303
Bmicrograph::ctf
CTFparam * ctf
Definition: mg_processing.h:238
Bimage::extract
Bimage * extract(long nn)
Extracts one sub-image into new image structure.
Definition: Bimage_extract.cpp:21
write_project_relion
int write_project_relion(Bstring &filename, Bproject *project, int mg_select, int rec_select)
Writing micrograph parameters to a Relion file.
Definition: rwmgRELION.cpp:277
Bstar::line_length
int line_length
Definition: rwstar.h:80
Bfield::id
Bstring id
Definition: mg_processing.h:259
Bstring::next
Bstring * next
Definition: Bstring.h:31
Bstar_block
Definition: rwstar.h:55
Bstring::pre
Bstring pre(char c)
Definition: Bstring.cpp:381
Bproject::field
Bfield * field
Definition: mg_processing.h:343
rwmgSTAR.h
Header file for reading and writing micrograph parameters from and to the STAR format.
CTFparam::defocus_deviation
double defocus_deviation()
Definition: ctf.h:102
Bmicrograph
Definition: mg_processing.h:200
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:280
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
mg_tags.h
All STAR and XML file format tags for micrographs and reconstructions.
Bparticle::fpart
Bstring fpart
Definition: mg_processing.h:91
Bparticle::ori
Vector3< double > ori
Definition: mg_processing.h:100
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:428
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
Bparticle::dev
double dev
Definition: mg_processing.h:96
Bparticle
Definition: mg_processing.h:89
Bstring
Definition: Bstring.h:29
Bstar::comment
Bstring comment
Definition: rwstar.h:81
rwstar.h
A header file for STAR format files.
CTFparam::volt
double volt()
Definition: ctf.h:83
Bmicrograph::box_size
Vector3< long > box_size
Definition: mg_processing.h:230
Bmicrograph::fmg
Bstring fmg
Definition: mg_processing.h:205
Bparticle::loc
Vector3< double > loc
Definition: mg_processing.h:99
Bmicrograph::magnification
double magnification
Definition: mg_processing.h:212
Bstar::block
Bstar_block * block
Definition: rwstar.h:82
item_put_list
int item_put_list(Bstar_block *block, const char *tag, char *list, size_t offset, const char *format)
Writes a list of string or numeric values into a data item associated with a specific data block and ...
Definition: rwstar.cpp:1436
CTFparam::defocus_average
double defocus_average()
Definition: ctf.h:100
verbose
int verbose
Bparticle::def
double def
Definition: mg_processing.h:95
Bstring::integer
long integer()
Definition: Bstring.cpp:539
mg_processing.h
Header file for micrograph processing.
rwimg.h
Header file for 2D and 3D image I/O.
Bproject::comment
Bstring comment
Definition: mg_processing.h:336
CTFparam::amp_shift
double amp_shift()
Definition: ctf.h:95
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:413
Bparticle::next
Bparticle * next
Definition: mg_processing.h:90
project_to_relion
int project_to_relion(Bproject *project, Bstar *star, int mg_select, int rec_select)
Definition: rwmgRELION.cpp:218
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
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:273
item_find
Bstar_item * item_find(Bstar_block *block, const char *tag)
Gets the STAR item associated with a tag in a STAR data block.
Definition: rwstar.cpp:1083
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:21
Bproject
Definition: mg_processing.h:333
Bstar_item
Definition: rwstar.h:36
read_project_relion
int read_project_relion(Bstring &filename, Bproject *project)
Reading micrograph parameters from a Relion file.
Definition: rwmgRELION.cpp:249
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:563
CTFparam
Definition: ctf.h:26
Bmicrograph::next
Bmicrograph * next
Definition: mg_processing.h:201