Bsoft  2.1.0
Bernard's software package
mg_merge.cpp
Go to the documentation of this file.
1 
9 #include "mg_merge.h"
10 #include "mg_select.h"
11 #include "utilities.h"
12 
13 // Definition of the global variables
14 extern int verbose; // Level of output to the screen
15 
16 
33 {
34  Vector3<double> start;
35  Vector3<double> scale(1,1,1), axis(0,0,1);
36  Vector3<long> size(p->size());
37  Matrix3 mat = Matrix3(axis, angle);
38  Bimage* p1;
39 
40  p->change_type(Float);
41 
42  long n;
43 
44  for ( n=0; n<p->images(); n++ ) {
45 // p1 = p->extract(n);
46 // img_transform(p1, size, scale, origin[n], shift[n], mat, FILL_BACKGROUND, 0);
47  p1 = p->transform(n, size, scale, origin[n], shift[n], mat, FILL_BACKGROUND, 0);
48  p->replace(n, p1);
49  delete p1;
50  }
51 
52  return n;
53 }
54 
55 
76 int mg_particle_merge_series(Bproject* project, int mg_ref_select, int mg_index,
77  float mg_rot_ang, int mg_ori_select, Bstring outimg)
78 {
79 
80  long nmgs; // total number of micrographs in field
81  long i, j; // indices
82  long ifield; // index for field block
83  long long_ids(0); // flag to test for long micrograph ids
84  double inplane_angle(0); // rotation for an entire micrograph
85  long datasize; // number of data points in images
86  Vector3<float>* shifts = NULL; // array of translational shifts
87  Vector3<float>* origins = NULL; // array of origins
88  Bstring filename; // outimg + micrograph ids
89  Bstring insert("_merged."); // Insert for default file name
90 
91  Bimage* pimg = NULL; // Particle image being processed
92  Bimage* pref = NULL; // Reference particle image(s)
93  Bimage* psum = NULL; // Summed image(s)
94 
95  Bfield* field; // Field currently being processed
96  Bmicrograph* mg = NULL; // Micrograph currently being processed
97  Bmicrograph* mg_ref = NULL; // Reference micrograph, the one to which other images are aligned
98  Bmicrograph* mg2 = NULL; // Micrograph place holder
99  Bparticle* particle = NULL; // Particle currently being processed
100  Bparticle* particle_ref = NULL; // Reference particle
101 
102 
103  if ( verbose ) {
104  cout << "Merging particle images on micrograph series:" << endl;
105  if ( mg_ori_select == 0 ) cout << "Origins obtained from parameter files." << endl;
106  else if ( mg_ori_select == 1 ) cout << "Origins obtained from cross-correlation with reference micrograph." << endl;
107  else if ( mg_ori_select == 2 ) cout << "Origins obtained from particle image files." << endl;
108  else if ( mg_ori_select == -1 ) cout << "Images are merged without translational or rotational alignment." << endl;
109  }
110 
111  // Loop over all fields in project
112  for ( field = project->field, ifield=0; field; field = field->next, ifield++ ) {
113 
114  // Determine reference micrograph for current field
115  mg_ref = field_find_micrograph(field, mg_ref_select, mg_index, mg_rot_ang);
116 
117  // Read in reference image file as the image to which the other(s) will be added
118  // -copy to pref if user wishes to find origins
119  if (verbose & VERB_RESULT)
120  cout << endl << "Reference image file for field " << field->id << ": " << mg_ref->fpart << endl;
121 
122  psum = read_img(mg_ref->fpart, 1, -1);
123  if ( !psum ) {
124  error_show("Error in mg_particle_merge_series", __FILE__, __LINE__);
125  cerr << "File " << mg_ref->fpart << " was not found or read in properly." << endl;
126  return -1;
127  }
128  psum->change_type(Float); // convert to floating point
129  if ( mg_ori_select == 1 ) pref = psum->copy(); // as reference for finding origins
130 
131  datasize = psum->data_size();
132  nmgs = field_count_micrographs(field); // number of micrographs in current field, for normalizing summed images
133 
134  // Loop through micrographs in field,
135  // if not reference micrograph, align (if desired) each
136  // image to corresponding reference image, sum images
137  for ( mg = field->mg; mg; mg = mg->next ) if ( mg != mg_ref ) {
138  if (verbose & VERB_RESULT)
139  cout << "Aligning and adding images in " << mg->fpart << " to reference in " << mg_ref->fpart << endl;
140 
141  // Calculate rotation for all particles in micrograph, if alignment is desired
142  if ( mg_ori_select > -1 ) {
143  inplane_angle = mg->rot_angle - mg_ref->rot_angle;
144  if (verbose & VERB_RESULT)
145  cout << "in-plane rotation angle = " << (180/M_PI)*inplane_angle << endl;
146  }
147 
148  // Get images from "non-reference" micrograph, convert to float
149  pimg = read_img(mg->fpart, 1, -1);
150  pimg->change_type(Float);
151 
152  // Check that psum and pimg have the same number of images
153  if ( pimg->images() != psum->images() ) {
154  cerr << "WARNING: The number of images is not consistent (" << psum->images() << " and " << pimg->images() << ") and may cause program to crash." << endl;
155  }
156 
157  // Get origins and translational shift of non-std images, then align to reference images
158  if ( mg_ori_select > -1 ) {
159  origins = new Vector3<float>[pimg->images()];
160 
161  // get origins from input parameter file
162  if ( mg_ori_select == 0 ) {
163  shifts = new Vector3<float>[psum->images()];
164  for ( i=0, particle=mg->part, particle_ref=mg_ref->part; particle; i++, particle=particle->next, particle_ref=particle_ref->next) {
165  shifts[i] = particle_ref->ori - particle->ori;
166  origins[i] = particle->ori;
167  }
168 
169  }
170 
171  // find new origins via cross-correlation
172  else if ( mg_ori_select == 1 ) {
173 // img_find_shift(pimg, pref, NULL, pref->sampling(0)[0]*2, 1e10, pref->sizeX()/4.0, 0, 1);
174  pimg->find_shift(pref, NULL, pref->sampling(0)[0]*2, 1e10, pref->sizeX()/4.0, 0, 1);
175  for ( i=0, particle=mg->part; particle; i++, particle=particle->next) {
176  shifts[i] = pref->image[i].origin() - pimg->image[i].origin();
177  origins[i] = pimg->image[i].origin();
178  }
179  }
180 
181  // get origins from Bsubimage structure
182  else if ( mg_ori_select == 2 ) {
183  shifts = new Vector3<float>[psum->images()];
184  for ( i=0, particle=mg->part, particle_ref=mg_ref->part; particle; i++, particle=particle->next, particle_ref=particle_ref->next) {
185  origins[i] = pimg->image[i].origin();
186  shifts[i] = psum->image[i].origin() - origins[i];
187  }
188  }
189 
190  // align non-reference images to reference images
191  img_unique_shift_global_rotate(pimg, origins, shifts, inplane_angle);
192  delete[] shifts;
193  delete[] origins;
194 
195  }
196 
197  // Add non-reference image to corresponding reference image
198  // -sum background values
199  for ( i=0, j=0; i<datasize; i++, j++ ) psum->set(i, (*psum)[i] + (*pimg)[j]);
200  for ( i=0; i<psum->images(); i++ ) psum->background(i, psum->background(i) + pimg->background(i));
201 
202  delete pimg; // free allocated memory for non-reference image
203 
204  } // End of for loop (micrographs in field) and if statement (analyze if not a reference micrograph)
205  if ( mg_ori_select == 1 ) delete pref; // used as reference for finding origins
206 
207  // Normalize summed images
208  for ( i=0; i<datasize; i++ ) psum->set(i, (*psum)[i] / nmgs);
209  for ( i=0; i<psum->images(); i++ ) psum->background(i, psum->background(i) / nmgs);
210 
211  // Write out summed, normalized images
212  long_ids = 0;
213  if ( outimg.length() ) { // If adding micrograph ids to filename, make sure all ids are <= 6 characters
214  for ( mg = field->mg; mg; mg = mg->next ) if ( (mg->id.length() > 6) || (mg->id.length() == 0) ) long_ids++;
215  if ( long_ids )
216  cerr << "Error: Micrograph ids are too long to add together, revert to default file name." << endl;
217  } else long_ids = 1;
218 
219  if ( !long_ids ) { // Add micrograph ids or "_merged" to an input file name
220  filename = outimg.pre_rev('.') + "_" + mg_ref->id + "." + outimg.post_rev('.');
221  for ( mg = field->mg; mg; mg = mg->next ) if ( mg != mg_ref ) { // Test if micrograph is reference, if not add id to file name
222  filename = filename.pre_rev('.') + "_" + mg->id + "." + filename.post_rev('.');
223  }
224  } else filename = mg_ref->fpart.pre_rev('.') + insert + mg_ref->fpart.post_rev('.');
225 
226  if ( verbose & VERB_RESULT )
227  cout << "Writing summed images to " << filename << endl; // Write images to file
228 
229  write_img(filename, psum, 0);
230  delete psum;
231 
232  // Delete non-reference micrographs from project structure
233  for ( mg = field->mg; mg; ) {
234  mg2 = mg->next;
235  if ( mg != mg_ref ) micrograph_kill(mg);
236  mg = mg2;
237  }
238  field->mg = mg_ref;
239  mg_ref->next = NULL;
240  mg_ref->block = ifield;
241  mg_ref->fpart = filename;
242 
243  } // end of loop over all fields in project
244 
245  return 0;
246 }
247 
271 int mg_particle_unmerge(Bproject* project, Bproject* orientations, float fom_diff)
272 {
273 
274  Bfield* field = NULL; // Field-of-view currently being processed
275  Bfield* fieldo = NULL; // Corresponding field-of-view with orientations to be used
276  Bmicrograph* mg = NULL; // Micrograph currently being processed
277  Bmicrograph* mgo = NULL; // Corresponding micrograph with orientations to be used
278  Bparticle* particle = NULL; // Particle currently being processed
279  Bparticle* particleo = NULL; // Corresponding micrograph with orientation to be used
280  int change; // Flag to substitute values from orientations structure
281  int ccount(0); // Counts number of values that are changed
282  int num, numo; // For checking that the number of particles is consistent
283  int total; // Total number of particles, changed or unchanged
284  int ucount(0); // Counts number of values that are not changed
285  float rotang_diff; // Difference in rotation angle between mgo and mg
286 
287  project->euler_flag = orientations->euler_flag; // project gets euler and omega flags of the orientations, since
288  project->omega_flag = orientations->omega_flag; // these general parameters relate to the orientation
289 
290  for ( field=project->field, fieldo=orientations->field; field && fieldo; field=field->next, fieldo=fieldo->next ) {
291  if ( field->id != fieldo->id ) {
292  error_show("Error in mg_particle_unmerge", __FILE__, __LINE__);
293  cerr << "Field-of-view IDs do not match. Please check your data." << endl;
294  cerr << "ID of reference field: " << fieldo->id << endl;
295  cerr << "ID of other field: " << field->id << endl;
296  return -1;
297  }
298  if ( verbose ) cout << "\nUnmerging field: " << fieldo->id << endl;
299  for ( mgo=fieldo->mg; mgo; mgo=mgo->next ) {
300  if ( verbose & VERB_PROCESS ) cout << " reference micrograph: " << mgo->id << endl;
301  for ( mg=field->mg; mg; mg=mg->next ) {
302  if ( verbose & VERB_PROCESS ) cout << " unmerging " << mg->id << endl;
304  if ( num != numo ) {
305  error_show("Error in mg_particle_unmerge", __FILE__, __LINE__);
306  cerr << "Error: The number of particles in micrographs " << mg->id
307  << " (" << num << ") and " << mgo->id << " (" << numo
308  << ") do not match." << endl;
309  cerr << "Please check your data" << endl;
310  return -1;
311  }
312  rotang_diff = mg->rot_angle - mgo->rot_angle;
313  for ( particle=mg->part, particleo=mgo->part; particle && particleo; particle=particle->next, particleo=particleo->next ) {
314  if ( particle->id != particleo->id ) {
315  error_show("Error in mg_particle_unmerge", __FILE__, __LINE__);
316  cerr << "Error: Particle IDs do not match. Please check your data" << endl;
317  return -1;
318  }
319  change = 1;
320  if ( fom_diff > 0 ) {
321  if ( particle->fom[0] - particleo->fom[0] > fom_diff ) { // Don't change if particle FOM is better by fom_diff
322  change = 0;
323  ucount++;
324  }
325  }
326  if ( change ) {
327  particle->view = particleo->view;
328  particle->fom[0] = particleo->fom[0];
329  particle->sel = particleo->sel;
330  particle->view[3] += rotang_diff; // applies difference in micrograph rotation angle
331  ccount++;
332  }
333  }
334  }
335  }
336  }
337 
338  total = ccount + ucount;
339  if ( verbose ) {
340  cout << "\nUn-merge option:" << endl;
341  if ( fom_diff > 0 ) {
342  cout << "Total number of particles: " << total << endl;
343  cout << "Number of unchanged particles: " << ucount << endl;
344  cout << "FOM-difference threshold: " << fom_diff << endl << endl;
345  } else
346  cout << "Particle orientations changed: " << ccount << endl << endl;
347  }
348 
349  return 0;
350 }
351 
352 
verbose
int verbose
Bmicrograph::id
Bstring id
Definition: mg_processing.h:203
Bmicrograph
Definition: mg_processing.h:201
field_count_micrographs
long field_count_micrographs(Bfield *field)
Counts all the micrographs in the field-of-view structure.
Definition: mg_select.cpp:671
field_find_micrograph
Bmicrograph * field_find_micrograph(Bfield *field, int mg_select, int mg_index, double mg_ang)
Returns a pointer to a specific micrograph in a field-of-view structure based on a unique characteris...
Definition: mg_select.cpp:64
mg_particle_unmerge
int mg_particle_unmerge(Bproject *project, Bproject *orientations, float fom_diff)
Gives corresponding particles in a field-of-view the same orientation, figure-of-merit (FOM),...
Definition: mg_merge.cpp:271
Bparticle::view
View view
Definition: mg_processing.h:102
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1118
Bstring
Definition: Bstring.h:29
Bparticle
Definition: mg_processing.h:90
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:436
Bproject::field
Bfield * field
Definition: mg_processing.h:346
micrograph_kill
int micrograph_kill(Bmicrograph *mg)
Frees a whole micrograph parameter structure.
Definition: mg_processing.cpp:493
Bimage::copy
Bimage * copy()
Copies the header information and data of an image into a new image structure.
Definition: Bimage.cpp:2702
Vector3< float >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
micrograph_count_particles
long micrograph_count_particles(Bmicrograph *mg)
Counts all the particles in the micrograph structure.
Definition: mg_select.cpp:736
mg_select.h
Header file for micrograph processing.
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:719
Bparticle::fom
double fom[NFOM]
Definition: mg_processing.h:103
Bimage::background
double background(long nn)
Definition: Bimage.h:549
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
Bmicrograph::part
Bparticle * part
Definition: mg_processing.h:242
Bfield::id
Bstring id
Definition: mg_processing.h:262
Bimage::transform
int transform(Vector3< double > scale, Vector3< double > origin, Vector3< double > translate, Matrix3 mat, int fill_type=0, double fill=0)
Transforms an image by translation, rotation, scaling and skewing, in place.
Definition: Bimage_transform.cpp:37
Bmicrograph::fpart
Bstring fpart
Definition: mg_processing.h:208
Bimage::find_shift
int find_shift(Bimage *pref, Bimage *pmask, double hires, double lores, double radius, double sigma, int refine_flag)
Calculates a cross-correlation map to find the shift for the pair of images.
Definition: Bimage_correlate.cpp:929
Bfield
Definition: mg_processing.h:260
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Bproject::omega_flag
int omega_flag
Definition: mg_processing.h:344
Bimage::images
long images()
Definition: Bimage.h:504
project
Bproject * project
Definition: bshow.cpp:33
Matrix3
Definition: Matrix3.h:18
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:77
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
insert
string insert(string &filename, string ins)
Definition: string_util.cpp:48
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:421
VERB_RESULT
#define VERB_RESULT
Definition: utilities.h:109
Bimage::data_size
long data_size()
Definition: Bimage.h:270
Bimage
General image parameter class.
Definition: Bimage.h:177
Bproject
Definition: mg_processing.h:336
Bparticle::sel
long sel
Definition: mg_processing.h:104
Bimage::replace
int replace(Bimage *img)
Replaces the data with that from the given image.
Definition: Bimage.cpp:2830
mg_merge.h
Header file for functions to merge images.
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
mg_particle_merge_series
int mg_particle_merge_series(Bproject *project, int mg_ref_select, int mg_index, float mg_rot_ang, int mg_ori_select, Bstring outimg)
Aligns and merges (sums) 2D particle images from a focal, or other, series and writes to new image fi...
Definition: mg_merge.cpp:76
Bstring::length
long length() const
Definition: Bstring.cpp:263
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
Bproject::euler_flag
int euler_flag
Definition: mg_processing.h:343
Bfield::next
Bfield * next
Definition: mg_processing.h:261
Bparticle::ori
Vector3< double > ori
Definition: mg_processing.h:101
Bmicrograph::block
int block
Definition: mg_processing.h:205
Bparticle::next
Bparticle * next
Definition: mg_processing.h:91
Bmicrograph::rot_angle
double rot_angle
Definition: mg_processing.h:222
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:178
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
Float
@ Float
Definition: utilities.h:161
Bmicrograph::next
Bmicrograph * next
Definition: mg_processing.h:202
Bfield::mg
Bmicrograph * mg
Definition: mg_processing.h:267
Bparticle::id
int id
Definition: mg_processing.h:93
img_unique_shift_global_rotate
int img_unique_shift_global_rotate(Bimage *p, Vector3< float > *origin, Vector3< float > *shift, float angle)
Shifts and rotates each image as defined in individual shift vectors.
Definition: mg_merge.cpp:32