Bsoft  2.1.0
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 
130  if ( project ) {
131  if ( split < 0 )
133  else if ( outfile.length() || split == 9 ) {
134  project->split = split;
135  write_project(outfile, project, 0, 0);
136  }
137  }
138 
140 
141  bexit(0);
142 }
143 
144 
146 {
147  Bparticle *part, *part_source;
148  Vector3<double> shift = mg->origin - origin;
149 
150  if ( which.contains("ctf") ) {
151  if ( mg_source->fps.length() > 0 ) mg->fps = mg_source->fps;
152  if ( mg_source->magnification ) mg->magnification = mg_source->magnification;
153  if ( mg_source->sampling ) mg->sampling = mg_source->sampling;
154  if ( mg_source->ctf ) {
155  if ( !mg->ctf ) mg->ctf = new CTFparam;
156  mg->ctf->update(mg_source->ctf);
157  }
158  }
159 
160  if ( which.contains("part") ) {
161  mg->box_size = mg_source->box_size;
162  mg->bad_radius = mg_source->bad_radius;
163  particle_kill(mg->part);
164  mg->part = particle_copy(mg_source->part);
165  for ( part = mg->part; part; part = part->next )
166  part->loc += shift;
167  }
168 
169  if ( which.contains("ori") ) {
170  for ( part = mg->part, part_source = mg_source->part; part && part_source; part = part->next, part_source = part_source->next )
171  part->ori = part_source->ori;
172  }
173 
174  return 1;
175 }
176 
177 long field_replace_parameters(Bfield* field, Bfield* field_source, Bstring& which)
178 {
179  Bmicrograph *mg, *mg_source;
180  Vector3<double> origin = field->mg->origin; // Assumes the first micrograph is the reference
181 
182  long nmg = field_count_micrographs(field);
183  long nmg_source = field_count_micrographs(field_source);
184 
185  if ( nmg_source == 1 ) { // Replaces parameters from one micrograph into many
186  mg_source = field_source->mg;
187  for ( nmg=0, mg = field->mg; mg; mg = mg->next )
188  nmg += mg_replace_parameters(mg, mg_source, which, origin);
189  } else if ( nmg_source == nmg ) { // Replaces parameters from into corresponding micrographs
190  for ( nmg=0, mg = field->mg, mg_source = field_source->mg; mg && mg_source; mg = mg->next, mg_source = mg_source->next )
191  nmg += mg_replace_parameters(mg, mg_source, which, origin);
192  } else {
193  cerr << "Error in updating parameters: The numbers of micrographs are unsuitable!" << endl;
194  cerr << " Master field " << field->id << " has " << nmg << " micrographs" << endl;
195  cerr << " Source field " << field_source->id << " has " << nmg_source << " micrographs" << endl;
196  }
197 
198  return nmg;
199 }
200 
202 {
203  Bparticle *part, *part_source;
204 
205  if ( which.contains("ctf") ) {
206  if ( rec_source->fps.length() > 0 ) rec->fps = rec_source->fps;
207  if ( rec_source->ctf ) {
208  if ( !rec->ctf ) rec->ctf = new CTFparam;
209  rec->ctf->update(rec_source->ctf);
210  }
211  }
212 
213  if ( which.contains("part") ) {
214  rec->box_size = rec_source->box_size;
215  rec->bad_radius = rec_source->bad_radius;
216  particle_kill(rec->part);
217  rec->part = particle_copy(rec_source->part);
218  }
219 
220  if ( which.contains("ori") ) {
221  for ( part = rec->part, part_source = rec_source->part; part && part_source; part = part->next, part_source = part_source->next )
222  part->ori = part_source->ori;
223  }
224 
225  return 1;
226 }
227 
229 {
230  long nmg(0), nrec(0);
231  Bfield *field, *field_source;
232  Breconstruction *rec, *rec_source;
233 
234  if ( verbose )
235  cout << "Replacing parameters: " << which << endl;
236 
237  if ( project->select < 1 ) {
238  for ( field_source = proj_source->field; field_source; field_source = field_source->next ) {
239  for ( field = project->field; field && field->id != field_source->id; field = field->next ) ;
240  if ( field )
241  nmg += field_replace_parameters(field, field_source, which);
242  }
243  if ( verbose )
244  cout << "Micrographs updated: " << nmg << endl << endl;
245  } else {
246  for ( rec_source = proj_source->rec; rec_source; rec_source = rec_source->next ) {
247  for ( rec = project->rec; rec && rec->id != rec_source->id; rec = rec->next ) ;
248  if ( rec )
249  nrec += rec_replace_parameters(rec, rec_source, which);
250  }
251  if ( verbose )
252  cout << "Reconstructions updated: " << nmg << endl << endl;
253  }
254 
255 
256  return nmg;
257 }
258 
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
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:810
Bmicrograph
Definition: mg_processing.h:201
Breconstruction::part
Bparticle * part
Definition: mg_processing.h:307
Breconstruction::id
Bstring id
Definition: mg_processing.h:285
field_count_micrographs
long field_count_micrographs(Bfield *field)
Counts all the micrographs in the field-of-view structure.
Definition: mg_select.cpp:671
main
int main(int argc, char **argv)
Definition: bmgtransfer.cpp:54
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
Bproject::select
int select
Definition: mg_processing.h:342
file_util.h
Header file for file checking functions.
Bstring
Definition: Bstring.h:29
CTFparam::update
int update(CTFparam *ctf)
Definition: ctf.h:81
Bproject::split
int split
Definition: mg_processing.h:340
Bparticle
Definition: mg_processing.h:90
Bproject::field
Bfield * field
Definition: mg_processing.h:346
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Breconstruction::ctf
CTFparam * ctf
Definition: mg_processing.h:306
Bmicrograph::bad_radius
double bad_radius
Definition: mg_processing.h:236
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
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
Bmicrograph::sampling
double sampling
Definition: mg_processing.h:214
Breconstruction::box_size
Vector3< long > box_size
Definition: mg_processing.h:299
Bmicrograph::part
Bparticle * part
Definition: mg_processing.h:242
options.h
Header file for option handlers.
Bfield::id
Bstring id
Definition: mg_processing.h:262
use
const char * use[]
Definition: bmgtransfer.cpp:22
Bfield
Definition: mg_processing.h:260
project
Bproject * project
Definition: bshow.cpp:33
Boption::tag
Bstring tag
Definition: options.h:29
rwmg.h
Header file for reading and writing micrograph parameters.
project_dump
long project_dump(Bproject *project, Bstring &filename)
Dumps particle info in the project hierarchy.
Definition: mg_processing.cpp:1646
Bmicrograph::origin
Vector3< double > origin
Definition: mg_processing.h:223
Boption::value
Bstring value
Definition: options.h:30
mg_ctf.h
Functions for CTF (contrast transfer function) processing.
Bmicrograph::magnification
double magnification
Definition: mg_processing.h:213
Bproject
Definition: mg_processing.h:336
verbose
int verbose
field_replace_parameters
long field_replace_parameters(Bfield *field, Bfield *field_source, Bstring &which)
Definition: bmgtransfer.cpp:177
Bparticle::loc
Vector3< double > loc
Definition: mg_processing.h:100
read_project
Bproject * read_project(const char *filename, int flags=0)
Reading micrograph parameters from files.
Definition: rwmg.cpp:141
utilities.h
Header file for general utilities.
Bmicrograph::box_size
Vector3< long > box_size
Definition: mg_processing.h:233
split
vector< string > split(const string &s)
Splits a string on whitespace.
Definition: string_util.cpp:92
Breconstruction
Definition: mg_processing.h:283
Bstring::length
long length() const
Definition: Bstring.cpp:263
CTFparam
Definition: ctf.h:28
Bmicrograph::ctf
CTFparam * ctf
Definition: mg_processing.h:241
Bfield::next
Bfield * next
Definition: mg_processing.h:261
write_project
int write_project(const char *filename, Bproject *project)
Writing micrograph parameters to a file.
Definition: rwmg.cpp:321
rec_replace_parameters
long rec_replace_parameters(Breconstruction *rec, Breconstruction *rec_source, Bstring &which)
Definition: bmgtransfer.cpp:201
Bparticle::ori
Vector3< double > ori
Definition: mg_processing.h:101
mg_processing.h
Header file for micrograph processing.
Breconstruction::bad_radius
double bad_radius
Definition: mg_processing.h:302
Bparticle::next
Bparticle * next
Definition: mg_processing.h:91
Boption::filename
Bstring filename()
Definition: options.h:85
Bmicrograph::fps
Bstring fps
Definition: mg_processing.h:211
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
particle_copy
Bparticle * particle_copy(Bparticle **partlist, Bparticle *part)
Adds one particle onto a list of particle parameters.
Definition: mg_processing.cpp:614
mg_replace_parameters
long mg_replace_parameters(Bmicrograph *mg, Bmicrograph *mg_source, Bstring &which, Vector3< double > origin)
Definition: bmgtransfer.cpp:145
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
particle_kill
int particle_kill(Bparticle *part)
Frees a list of particles.
Definition: mg_processing.cpp:528
Breconstruction::fps
Bstring fps
Definition: mg_processing.h:294
Bmicrograph::next
Bmicrograph * next
Definition: mg_processing.h:202
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:713
Bfield::mg
Bmicrograph * mg
Definition: mg_processing.h:267
project_replace_parameters
long project_replace_parameters(Bproject *project, Bproject *proj_source, Bstring &which)
Definition: bmgtransfer.cpp:228
Breconstruction::next
Breconstruction * next
Definition: mg_processing.h:284