Bsoft  2.1.0
Bernard's software package
model_multifit.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "rwmodel.h"
11 #include "model_multifit.h"
12 #include "symmetry.h"
13 #include "Matrix.h"
14 #include "linked_list.h"
15 #include "utilities.h"
16 
17 
18 // Declaration of global variables
19 extern int verbose; // Level of output to the screen
20 
39  double alpha, double alpha_step, double hires, double lores, Bimage* pmask, double threshold)
40 {
41  int mode = 0; // Mode: 0=global, 1=refine, 2=symmetry
42  long i, n = 0, nviews;
43  double alpha_min = 0;
44  double alpha_max = TWOPI - alpha_step/2;
45  double a;
46  View* v;
47 
48  if ( alpha >= 0 ) { // Case of refinement
49  mode = 1;
50  alpha_min = alpha - alpha_step;
51  alpha_max = alpha + alpha_step;
52  } else if ( alpha_step >= 1.999*M_PI ) mode = 2; // Symmetry
53 
54  if ( !p ) return NULL;
55 
56  p->change_type(Float);
57  ptemp->change_type(Float);
58  p->rescale_to_avg_std(0, 1);
59  ptemp->rescale_to_avg_std(0, 1);
61  ptemp->calculate_background();
62 
63  Vector3<double> origin(ptemp->image->origin());
64 
65  if ( p->sizeZ() == 1 ) origin[2] = 0;
66 
67  if ( origin.sum() == 0 ) {
68  origin = p->size()/2;
69  p->origin(origin);
70  }
71 
72  for ( nviews=0, v=view; v; v=v->next )
73  for ( a=alpha_min; a<=alpha_max; a+=alpha_step ) nviews++;
74 
75  View* view_arr = new View[nviews];
76  Bmodel** mod_arr = new Bmodel*[nviews];
77 
78  for ( n=0, v=view; v; v=v->next )
79  for ( a=alpha_min; a<=alpha_max; a+=alpha_step, n++ ) {
80  view_arr[n] = *v;
81  if ( mode < 2 ) view_arr[n][3] = a;
82  }
83 
84  if ( verbose ) {
85  cout << "Finding a template in a map:" << endl;
86  cout << "Number of views: " << nviews << endl;
87  cout << "Alpha step size: " << alpha_step*180.0/M_PI << endl;
88  cout << "Origin: " << origin << endl;
89  }
90 
91  Bstring id("1");
92  Bmodel* model = model_init(id);
93  Bcomponent* comp = NULL;
94 
95  model->fmap = p->file_name();
96  model->id = model->fmap.pre('.');
97 
99  ct->fmod = ptemp->file_name();
100 
101  if ( verbose & VERB_RESULT )
102  cout << "#\tx\ty\tz\tvx\tvy\tvz\tva\tCC" << endl;
103 
104 //#pragma omp parallel for private(comp)
105  for ( n=0; n<nviews; n++ ) {
106  mod_arr[n] = model_from_densities_for_view(p, ptemp, view_arr[n], hires, lores, pmask, threshold);
107  for ( comp = mod_arr[n]->comp; comp; comp = comp->next ) {
108  if ( verbose & VERB_RESULT )
109  cout << n << tab << comp->loc[0] << tab << comp->loc[1] << tab <<
110  comp->loc[2] << tab << comp->view[0] << tab << comp->view[1] << tab <<
111  comp->view[2] << tab << comp->view.angle()*180.0/M_PI << tab << comp->fom << endl;
112  }
113  }
114 
115  double fommin = 1, fommax = 0, fomavg = 0, fomstd = 0;
116  for ( n=i=0; n<nviews; n++ ) {
117  for ( comp = mod_arr[n]->comp; comp; comp = comp->next ) {
118  i++;
119  comp->id = Bstring(i, "%ld");
120  comp->type = ct;
121  comp->sel = 1;
122  if ( fommin > comp->fom ) fommin = comp->fom;
123  if ( fommax < comp->fom ) fommax = comp->fom;
124  fomavg += comp->fom;
125  fomstd += comp->fom*comp->fom;
126  }
127  if ( model->comp ) {
128  for ( comp = model->comp; comp->next; comp = comp->next ) ;
129  comp->next = mod_arr[n]->comp;
130  } else {
131  model->comp = mod_arr[n]->comp;
132  }
133  mod_arr[n]->comp = NULL;
134  model_kill(mod_arr[n]);
135  }
136 
137  delete[] view_arr;
138  delete[] mod_arr;
139 
140  if ( verbose ) {
141  if ( i ) {
142  fomavg /= i;
143  fomstd = fomstd/i - fomavg*fomavg;
144  if ( fomstd > 0 ) fomstd = sqrt(fomstd);
145  else fomstd = 0;
146  }
147  cout << "FOM minimum and maximum: " << fommin << " " << fommax << endl;
148  cout << "FOM average: " << fomavg << endl;
149  cout << "FOM standard deviation: " << fomstd << endl << endl;
150  }
151 
152  return model;
153 }
154 
172  double hires, double lores, Bimage* pmask, double threshold)
173 {
174  Matrix3 mat = view.matrix();
175 // mat = mat.transpose();
176 
177  Bimage* prot = ptemp->rotate(p->size(), mat);
178 
179  prot->view(view);
180  if ( p->sizeZ() == 1 ) prot->project('z');
181 
182  Bimage* pcc = p->cross_correlate(prot, hires, lores, pmask);
183 
184  Bmodel* model = model_from_peaks(pcc, threshold, 1);
185  Bcomponent* comp = NULL;
186 
187  for ( comp = model->comp; comp; comp = comp->next ) {
188  comp->view = view;
189  comp->sel = 1;
190  }
191 
192  delete prot;
193  delete pcc;
194 
195  return model;
196 }
197 
209 Bmodel* model_from_peaks(Bimage* p, double threshold, int wrap)
210 {
211  Bimage* pfom = p->find_peaks(3);
212 
213  if ( pfom->maximum() <= 0 ) return NULL;
214 
215  if ( threshold <= 0 ) threshold = pfom->maximum()/2;
216 
217  long i, j, x, y, z, n;
218  Vector3<long> h(p->size()/2);
219  Vector3<double> origin;
220 
221 // Bstring id(p->file_name().post_rev('/').pre_rev('.'));
222  Bstring id("Peaks");
223  Bmodel* model = model_init(id);
224  Bcomponent* comp = NULL;
225  Bstring nutype("PEAK");
226 
227  model->fmap = p->file_name();
228  model->fom = 1;
229 
230  for ( i=j=n=0; n<p->images(); n++ ) {
231  origin = p->image[n].origin();
232  for ( z=0; z<p->sizeZ(); z++ ) {
233  for ( y=0; y<p->sizeY(); y++ ) {
234  for ( x=0; x<p->sizeX(); x++, i++ ) {
235  if ( (*pfom)[i] >= threshold ) {
236  id = Bstring(++j, "%d");
237  comp = component_add(&comp, id);
238  if ( !model->comp ) model->comp = comp;
239  comp->type = model_add_type_by_id(model, nutype);
240  comp->loc = Vector3<double>(x, y, z) - origin;
241  if ( wrap ) {
242  if ( comp->loc[0] > h[0] ) comp->loc[0] -= p->sizeX();
243  if ( comp->loc[1] > h[1] ) comp->loc[1] -= p->sizeY();
244  if ( comp->loc[2] > h[2] ) comp->loc[2] -= p->sizeZ();
245  }
246  comp->loc *= p->sampling(0);
247  comp->sel = n+1;
248  comp->fom = (*pfom)[i];
249  }
250  }
251  }
252  }
253  }
254 
255  delete pfom;
256 
257  if ( verbose )
258  cout << "Components generated: " << j << endl << endl;
259 
260  return model;
261 }
262 
Bmodel::fom
float fom
Definition: rwmodel.h:128
View::next
View * next
Definition: View.h:28
Bcomponent::next
Bcomponent * next
Definition: rwmodel.h:52
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
model_from_peaks
Bmodel * model_from_peaks(Bimage *p, double threshold, int wrap)
Generates a model from peaks in a map.
Definition: model_multifit.cpp:209
Bcomponent
Definition: rwmodel.h:51
Bimage::find_peaks
Bimage * find_peaks(long kernelsize)
Finds peaks in a map with periodic boundaries.
Definition: Bimage_correlate.cpp:1739
Bcomptype::fmod
Bstring fmod
Definition: rwmodel.h:29
rwmodel.h
Header file for reading and writing atomic model files.
Bimage::cross_correlate
Bimage * cross_correlate(Bimage *p, double hires, double lores, fft_plan planf, fft_plan planb)
Calculates a cross-correlation map by Fast Fourier transformation.
Definition: Bimage_correlate.cpp:363
Bcomponent::loc
Vector3< float > loc
Definition: rwmodel.h:58
Bstring
Definition: Bstring.h:29
Bcomponent::id
Bstring id
Definition: rwmodel.h:53
component_add
Bcomponent * component_add(Bcomponent **comp, Bstring &id)
Adds a component to a linked list.
Definition: rwmodel.cpp:270
symmetry.h
Header file for general symmetry functions.
Bcomptype
Definition: rwmodel.h:25
Vector3< double >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
Bimage::calculate_background
int calculate_background(long nn, int flag)
Calculates the background for one sub-image.
Definition: Bimage_background.cpp:27
View
Definition: View.h:26
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
Bimage::maximum
double maximum()
Definition: Bimage.h:542
Bmodel::fmap
Bstring fmap
Definition: rwmodel.h:125
model_init
Bmodel * model_init(Bstring &id)
Initializes a model structure.
Definition: rwmodel.cpp:147
Bimage::images
long images()
Definition: Bimage.h:504
Bmodel::comp
Bcomponent * comp
Definition: rwmodel.h:132
Matrix3
Definition: Matrix3.h:18
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
Bimage::project
Bimage * project(char axis, int flags=1)
Projects a 3D image to a 2D image down one of the three major axes.
Definition: Bimage_project.cpp:31
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
Bstring::pre
Bstring pre(char c)
Definition: Bstring.cpp:389
Bmodel::id
Bstring id
Definition: rwmodel.h:121
Bimage::rotate
int rotate()
Rotates an image using parameters defined in the image in place.
Definition: Bimage_transform.cpp:255
VERB_RESULT
#define VERB_RESULT
Definition: utilities.h:109
model_kill
int model_kill(Bmodel *model)
Deallocates all memory in the list.
Definition: rwmodel.cpp:1127
Bimage
General image parameter class.
Definition: Bimage.h:177
model_from_densities_for_view
Bmodel * model_from_densities_for_view(Bimage *p, Bimage *ptemp, View view, double hires, double lores, Bimage *pmask, double threshold)
Searches a 2D/3D density map for a template using a specific view.
Definition: model_multifit.cpp:171
model_from_densities
Bmodel * model_from_densities(Bimage *p, Bimage *ptemp, View *view, double alpha, double alpha_step, double hires, double lores, Bimage *pmask, double threshold)
Searches a 2D/3D density map for a template.
Definition: model_multifit.cpp:38
model_multifit.h
Header file for searching for a template in a map and returning multiple hits in a model.
Bmodel
Definition: rwmodel.h:119
verbose
int verbose
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
Vector3::sum
double sum()
Definition: Vector3.h:236
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
Bcomponent::view
View view
Definition: rwmodel.h:59
Bcomponent::fom
float fom
Definition: rwmodel.h:65
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
Bcomponent::type
Bcomptype * type
Definition: rwmodel.h:54
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
model
Bmodel * model
Definition: bshow.cpp:34
rwimg.h
Header file for 2D and 3D image I/O.
Matrix.h
Generalized matrix class.
linked_list.h
Header file for generalized linked list functions.
model_add_type_by_id
Bcomptype * model_add_type_by_id(Bmodel *model, Bstring &id)
Add a type to a model if the identifier does not exist already.
Definition: rwmodel.cpp:414
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
View::matrix
Matrix3 matrix()
Definition: View.h:153
Float
@ Float
Definition: utilities.h:161
View::angle
double angle()
Definition: View.h:98
Bimage::view
void view(double vx, double vy, double vz, double va)
Definition: Bimage.h:596
TWOPI
#define TWOPI
Definition: utilities.h:72
Bcomponent::sel
int sel
Definition: rwmodel.h:66
Bimage::rescale_to_avg_std
int rescale_to_avg_std(double nuavg, double nustd)
Rescales the image data to a given average and standard deviation.
Definition: Bimage_rescale.cpp:163