| Bsoft 2.0.5 Bernard's software package

bmgtransfer.cpp
Go to the documentation of this file.
1
9 #include "mg_processing.h"
10 #include "rwmg.h"
11 #include "mg_ctf.h"
12 #include "file_util.h"
13 #include "utilities.h"
14 #include "options.h"
15
16 // Declaration of global variables
17 extern int verbose; // Level of output to the screen
18
19 long project_replace_parameters(Bproject* project, Bproject* proj_source, Bstring& which);
20
21 // Usage assistance
22 const char* use[] = {
23 " ",
24 "Usage: bmgtransfer [options] input.star/input.img [input.star/input.img]",
25 "------------------------------------------------------------------------",
26 "Transfer pieces of information from input micrograph parameter files",
27 "to a master destination parameter file.",
28 "Transfers are linked to field ID's and can occur in one of two ways:",
29 "1. From one source micrograph to all destination micrographs in the field.",
30 "2. From each source micrograph to the corresponding destination micrograph in the field.",
31 " ",
32 "Actions:",
33 "-ctf Replace CTF parameters.",
34 "-particles Replace particle parameters.",
35 "-origins Replace particle origins.",
36 " ",
37 "Parameters:",
38 "-verbose 7 Verbosity of output.",
39 " ",
40 "Input:",
41 "-master destination.star Master parameter file into which to merge information from other input files.",
42 " ",
43 "Output:",
44 "-output output.star Output parameter file (required any time \"-split\" is used to indicate the type).",
45 "-split 3 Split micrograph or reconstruction data blocks into individual files:",
46 " Argument: 1-6: number of digits inserted before extension",
47 " Argument: \"id\": micrograph ID's are used as file names (no -output required).",
48 " Argument: \"field\": field ID's are used as file names (no -output required).",
49 "-dump file.txt Dump all the particle origins and views.",
50 " ",
51 NULL
52 };
53
54 int main (int argc, char **argv)
55 {
56 // Initialize optional variables
57 Bstring which; // Specification of parameters to transfer
58 Bstring masterfile; // Input master parameter file name
59 Bstring outfile; // Output parameter file name
60 Bstring dumpfile; // File to dump particle info to
61 int split(0); // Output one big STAR file
62
63 int optind;
64 Boption* option = get_option_list(use, argc, argv, optind);
65 Boption* curropt;
66 for ( curropt = option; curropt; curropt = curropt->next ) {
67 if ( curropt->tag == "ctf" ) {
68 if ( which.length() ) which += ",";
69 which += "ctf";
70 }
71 if ( curropt->tag == "particles" ) {
72 if ( which.length() ) which += ",";
73 which += "particles";
74 }
75 if ( curropt->tag == "origins" ) {
76 if ( which.length() ) which += ",";
77 which += "origins";
78 }
79 if ( curropt->tag == "master" )
80 masterfile = curropt->filename();
81 if ( curropt->tag == "output" )
82 outfile = curropt->filename();
83 if ( curropt->tag == "split" ) {
84 if ( curropt->value.contains("id") || curropt->value.contains("ID") ) split = 9;
85 else if ( curropt->value.contains("field") || curropt->value.contains("FIELD") ) split = -9;
86 else if ( ( split = curropt->value.integer() ) < 1 )
87 cerr << "-split: An integer must be specified!" << endl;
88 else
89 if ( split > 6 ) split = 6;
90 }
91 if ( curropt->tag == "dump" )
92 dumpfile = curropt->filename();
93 }
94 option_kill(option);
95
96 Bproject* project = NULL;
97
98 if ( masterfile.length() )
99 project = read_project(masterfile);
100 else {
101 cerr << "Error: A master parameter file must be specified!" << endl;
102 bexit(-1);
103 }
104
105 // Read all the parameter files
106 Bstring* file_list = NULL;
107 while ( optind < argc ) string_add(&file_list, argv[optind++]);
108 if ( !file_list ) {
109 cerr << "Error: No parameter files specified!" << endl;
110 bexit(-1);
111 }
112
113 Bproject* project2 = read_project(file_list);
114
115 string_kill(file_list);
116
117 if ( ! project2 ) {
118 cerr << "Error: No input parameter files read!" << endl;
119 bexit(-1);
120 }
121
122 if ( which.length() )
123 project_replace_parameters(project, project2, which);
124
125 project_kill(project2);
126
127 if ( dumpfile.length() ) project_dump(project, dumpfile);
128
129 project_update_comment(project, argc, argv);
130
131 if ( project ) {
132 if ( split < 0 )
133 project_split_field_write(project);
134 else if ( outfile.length() || split == 9 ) {
135 project->split = split;
136 write_project(outfile, project, 0, 0);
137 }
138 }
139
140 project_kill(project);
141
142 bexit(0);
143 }
144
145
146 long mg_replace_parameters(Bmicrograph* mg, Bmicrograph* mg_source, Bstring& which, Vector3<double> origin)
147 {
148 Bparticle *part, *part_source;
149 Vector3<double> shift = mg->origin - origin;
150
151 if ( which.contains("ctf") ) {
152 if ( mg_source->fps.length() > 0 ) mg->fps = mg_source->fps;
153 if ( mg_source->magnification ) mg->magnification = mg_source->magnification;
154 if ( mg_source->sampling ) mg->sampling = mg_source->sampling;
155 if ( mg_source->ctf ) {
156 if ( !mg->ctf ) mg->ctf = new CTFparam;
157 mg->ctf->update(mg_source->ctf);
158 }
159 }
160
161 if ( which.contains("part") ) {
162 mg->box_size = mg_source->box_size;
163 mg->bad_radius = mg_source->bad_radius;
164 particle_kill(mg->part);
165 mg->part = particle_copy(mg_source->part);
166 for ( part = mg->part; part; part = part->next )
167 part->loc += shift;
168 }
169
170 if ( which.contains("ori") ) {
171 for ( part = mg->part, part_source = mg_source->part; part && part_source; part = part->next, part_source = part_source->next )
172 part->ori = part_source->ori;
173 }
174
175 return 1;
176 }
177
178 long field_replace_parameters(Bfield* field, Bfield* field_source, Bstring& which)
179 {
180 Bmicrograph *mg, *mg_source;
181 Vector3<double> origin = field->mg->origin; // Assumes the first micrograph is the reference
182
183 long nmg = field_count_micrographs(field);
184 long nmg_source = field_count_micrographs(field_source);
185
186 if ( nmg_source == 1 ) { // Replaces parameters from one micrograph into many
187 mg_source = field_source->mg;
188 for ( nmg=0, mg = field->mg; mg; mg = mg->next )
189 nmg += mg_replace_parameters(mg, mg_source, which, origin);
190 } else if ( nmg_source == nmg ) { // Replaces parameters from into corresponding micrographs
191 for ( nmg=0, mg = field->mg, mg_source = field_source->mg; mg && mg_source; mg = mg->next, mg_source = mg_source->next )
192 nmg += mg_replace_parameters(mg, mg_source, which, origin);
193 } else {
194 cerr << "Error in updating parameters: The numbers of micrographs are unsuitable!" << endl;
195 cerr << " Master field " << field->id << " has " << nmg << " micrographs" << endl;
196 cerr << " Source field " << field_source->id << " has " << nmg_source << " micrographs" << endl;
197 }
198
199 return nmg;
200 }
201
202 long rec_replace_parameters(Breconstruction* rec, Breconstruction* rec_source, Bstring& which)
203 {
204 Bparticle *part, *part_source;
205
206 if ( which.contains("ctf") ) {
207 if ( rec_source->fps.length() > 0 ) rec->fps = rec_source->fps;
208 if ( rec_source->ctf ) {
209 if ( !rec->ctf ) rec->ctf = new CTFparam;
210 rec->ctf->update(rec_source->ctf);
211 }
212 }
213
214 if ( which.contains("part") ) {
215 rec->box_size = rec_source->box_size;
216 rec->bad_radius = rec_source->bad_radius;
217 particle_kill(rec->part);
218 rec->part = particle_copy(rec_source->part);
219 }
220
221 if ( which.contains("ori") ) {
222 for ( part = rec->part, part_source = rec_source->part; part && part_source; part = part->next, part_source = part_source->next )
223 part->ori = part_source->ori;
224 }
225
226 return 1;
227 }
228
229 long project_replace_parameters(Bproject* project, Bproject* proj_source, Bstring& which)
230 {
231 long nmg(0), nrec(0);
232 Bfield *field, *field_source;
233 Breconstruction *rec, *rec_source;
234
235 if ( verbose )
236 cout << "Replacing parameters: " << which << endl;
237
238 if ( project->select < 1 ) {
239 for ( field_source = proj_source->field; field_source; field_source = field_source->next ) {
240 for ( field = project->field; field && field->id != field_source->id; field = field->next ) ;
241 if ( field )
242 nmg += field_replace_parameters(field, field_source, which);
243 }
244 if ( verbose )
245 cout << "Micrographs updated: " << nmg << endl << endl;
246 } else {
247 for ( rec_source = proj_source->rec; rec_source; rec_source = rec_source->next ) {
248 for ( rec = project->rec; rec && rec->id != rec_source->id; rec = rec->next ) ;
249 if ( rec )
250 nrec += rec_replace_parameters(rec, rec_source, which);
251 }
252 if ( verbose )
253 cout << "Reconstructions updated: " << nmg << endl << endl;
254 }
255
256
257 return nmg;
258 }
259
write_project
int write_project(const char *filename, Bproject *project)
Writing micrograph parameters to a file.
Definition: rwmg.cpp:320
rwmg.h
Header file for reading and writing micrograph parameters.
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:705
Bproject::rec
Breconstruction * rec
Definition: mg_processing.h:344
Breconstruction::part
Bparticle * part
Definition: mg_processing.h:304
Bmicrograph::sampling
double sampling
Definition: mg_processing.h:213
utilities.h
Header file for general utilities.
particle_copy
Bparticle * particle_copy(Bparticle **partlist, Bparticle *part)
Adds one particle onto a list of particle parameters.
Definition: mg_processing.cpp:1409
Boption::value
Bstring value
Definition: options.h:30
Breconstruction
Definition: mg_processing.h:280
Bproject::split
int split
Definition: mg_processing.h:337
Boption::tag
Bstring tag
Definition: options.h:29
field_replace_parameters
long field_replace_parameters(Bfield *field, Bfield *field_source, Bstring &which)
Definition: bmgtransfer.cpp:178
CTFparam::update
int update(CTFparam *ctf)
Definition: ctf.h:77
read_project
Bproject * read_project(const char *filename, int flags=0)
Reading micrograph parameters from files.
Definition: rwmg.cpp:140
Bstring::length
long length() const
Definition: Bstring.cpp:263
project
Bproject * project
Definition: bshow.cpp:31
particle_kill
int particle_kill(Bparticle *part)
Frees a list of particles.
Definition: mg_processing.cpp:1323
Breconstruction::ctf
CTFparam * ctf
Definition: mg_processing.h:303
project_replace_parameters
long project_replace_parameters(Bproject *project, Bproject *proj_source, Bstring &which)
Definition: bmgtransfer.cpp:229
mg_ctf.h
Functions for CTF (contrast transfer function) processing.
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
Breconstruction::box_size
Vector3< long > box_size
Definition: mg_processing.h:296
Boption::filename
Bstring filename()
Definition: options.h:85
project_split_field_write
int project_split_field_write(Bproject *project)
Splits a project into sets of particle selections and write the files.
Definition: rwmg.cpp:678
Bfield::next
Bfield * next
Definition: mg_processing.h:258
Bmicrograph::part
Bparticle * part
Definition: mg_processing.h:239
use
const char * use[]
Definition: bmgtransfer.cpp:22
project_dump
long project_dump(Bproject *project, Bstring &filename)
Dumps particle info in the project hierarchy.
Definition: mg_processing.cpp:2974
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:584
Bfield
Definition: mg_processing.h:257
Bmicrograph::ctf
CTFparam * ctf
Definition: mg_processing.h:238
Bfield::id
Bstring id
Definition: mg_processing.h:259
Boption
A class for a command line input option.
Definition: options.h:26
Bproject::field
Bfield * field
Definition: mg_processing.h:343
mg_replace_parameters
long mg_replace_parameters(Bmicrograph *mg, Bmicrograph *mg_source, Bstring &which, Vector3< double > origin)
Definition: bmgtransfer.cpp:146
Bmicrograph
Definition: mg_processing.h:200
Bmicrograph::origin
Vector3< double > origin
Definition: mg_processing.h:220
file_util.h
Header file for file checking functions.
Bparticle::ori
Vector3< double > ori
Definition: mg_processing.h:100
Bfield::mg
Bmicrograph * mg
Definition: mg_processing.h:264
Boption::next
Boption * next
Definition: options.h:28
Bmicrograph::fps
Bstring fps
Definition: mg_processing.h:210
Bparticle
Definition: mg_processing.h:89
Bstring
Definition: Bstring.h:29
project_kill
int project_kill(Bproject *project)
Frees a whole project.
Definition: mg_processing.cpp:1224
Bproject::select
int select
Definition: mg_processing.h:339
Breconstruction::bad_radius
double bad_radius
Definition: mg_processing.h:299
Bmicrograph::box_size
Vector3< long > box_size
Definition: mg_processing.h:230
Vector3< double >
Bparticle::loc
Vector3< double > loc
Definition: mg_processing.h:99
Bmicrograph::magnification
double magnification
Definition: mg_processing.h:212
rec_replace_parameters
long rec_replace_parameters(Breconstruction *rec, Breconstruction *rec_source, Bstring &which)
Definition: bmgtransfer.cpp:202
field_count_micrographs
long field_count_micrographs(Bfield *field)
Counts all the micrographs in the field-of-view structure.
Definition: mg_processing.cpp:1067
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:109
Bstring::integer
long integer()
Definition: Bstring.cpp:539
Breconstruction::fps
Bstring fps
Definition: mg_processing.h:291
mg_processing.h
Header file for micrograph processing.
options.h
Header file for option handlers.
verbose
int verbose
Bmicrograph::bad_radius
double bad_radius
Definition: mg_processing.h:233
split
vector< string > split(const string &s)
Splits a string on whitespace.
Definition: string_util.cpp:92
project_update_comment
int project_update_comment(Bproject *project, int n, char **strings)
Puts a set of strings and time in the main comment of a project.
Definition: rwmg.cpp:468
Bparticle::next
Bparticle * next
Definition: mg_processing.h:90
main
int main(int argc, char **argv)
Definition: bmgtransfer.cpp:54
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:273
Breconstruction::next
Breconstruction * next
Definition: mg_processing.h:281
Bproject
Definition: mg_processing.h:333
Breconstruction::id
Bstring id
Definition: mg_processing.h:282
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:760
CTFparam
Definition: ctf.h:26
Bmicrograph::next
Bmicrograph * next
Definition: mg_processing.h:201