58 double hires,
double lores,
double shift_limit,
80 if ( pfsmask ) pmaskrot = pfsmask->
extract_wrap(0, size, mat);
82 for ( n=0; n<ptemp->
images(); n++ ) {
89 if ( shift_flag == 1 ) shift[0] = shift[1] = 0;
90 else if ( shift_flag == 2 ) shift[2] = 0;
91 best_shift = mat * shift;
107 double hires,
double lores,
double shift_limit,
123 double cc, ccmax(-1);
127 pmaskrot = pmask->
rotate(size, mat);
133 for ( n=0; n<ptemp->
images(); n++ ) {
134 pone = ptemp->
extract(n, origin, size, origin, mat);
139 if ( shift_flag == 1 ) shift[0] = shift[1] = 0;
140 else if ( shift_flag == 2 ) shift[2] = 0;
148 comp->
sel = imax + 1;
155 Bimage* pfsmask,
int max_iter,
double hires,
double lores,
double max_shift,
159 cout <<
"Refining component " << comp->
id <<
" by random view variations" << endl;
161 int imax(0), iter, itm;
162 double shift_limit(max_shift/ptemp->
sampling(0)[0]);
170 for ( iter=itm=0; iter<=max_iter; iter++ ) {
173 a = max_rot_angle*(random()*
irm*2 - 1);
185 imax = comp->
sel - 1;
194 if ( s > max_shift ) {
195 comp_shift *= max_shift/s;
198 comp->
loc += comp_shift;
199 comp->
vec = comp_shift;
200 comp->
view = comp_view;
201 comp->
sel = imax + 1;
208 Bimage* pfsmask,
double viewmax,
double rotmax,
209 double viewstep,
double rotstep,
210 double hires,
double lores,
double accuracy,
double max_shift,
214 cout <<
"Refining component " << comp->
id <<
" by contracting grid search" << endl;
216 if ( viewmax < 0 ) viewmax = 0;
217 if ( rotmax < 0 ) rotmax = 0;
218 if ( viewstep < 0 ) viewstep = 0;
219 if ( rotstep < 0 ) rotstep = 0;
222 double shift_limit(max_shift/ptemp->
sampling(0)[0]);
223 double cc, ccp(-2), ccmax(-1), s;
233 if ( viewstep && accuracy > viewstep ) accuracy = viewstep;
234 if ( rotstep && accuracy > rotstep ) accuracy = rotstep;
237 for ( itm=0; notdone; itm++ ) {
239 for ( v = views; v; v = v->
next ) {
245 imax = comp->
sel - 1;
251 if ( fabs(ccp - ccmax) < tol ) {
256 if ( viewstep < tol && rotstep < tol ) notdone = 0;
257 if ( viewstep <= accuracy && rotstep <= accuracy ) notdone = 0;
262 if ( s > max_shift ) {
263 comp_shift *= max_shift/s;
266 comp->
loc += comp_shift;
267 comp->
vec = comp_shift;
268 comp->
view = comp_view;
269 comp->
sel = imax + 1;
301 Bimage* pmask,
Bimage* pfsmask,
int max_iter,
double viewstep,
double rotstep,
302 double hires,
double lores,
double accuracy,
double max_shift,
303 double max_view_angle,
double max_rot_angle,
int shift_flag)
305 if ( max_iter < 0 ) max_iter = 0;
306 if ( max_shift <= 0 ) max_shift = ptemp->
sizeX()*ptemp->
sampling(0)[0]/10.0;
326 else for ( i=1; i<=ptemp->
images(); i++ ) {
332 for ( nname=0, name = ct_names; name; name = name->
next ) nname++;
334 if ( nname != ptemp->
images() ) {
335 cerr <<
"Error: The number of component type names must equal" << endl;
336 cerr <<
" the number of template sub-images (" << ptemp->
images() <<
")" << endl << endl;
344 if ( ptemp->
images() == 1 ) {
348 for ( i=0, name = ct_names; name; name = name->
next, i++ )
373 cerr <<
"Error: No model map file read! (" <<
model->
fmap <<
")" << endl;
385 if ( hires > lores ) swap(hires, lores);
387 double radius = ptemp->
sizeX()/2;
395 cout <<
"Refining component positions and views:" << endl;
396 cout <<
"Component types: ";
397 for ( name = ct_names; name; name = name->
next ) cout << *name <<
" ";
399 cout <<
"Reference map: " << ptemp->
file_name() << endl;
400 if ( pmask ) cout <<
"Mask: " << pmask->
file_name() << endl;
401 if ( pfsmask ) cout <<
"Fourier space mask: " << pfsmask->
file_name() << endl;
402 cout <<
"Iterations: " << max_iter << endl;
403 cout <<
"Resolution limits: " << hires <<
" " << lores <<
" A" << endl;
404 cout <<
"Maximum shift: " << max_shift <<
" A" << endl;
405 cout <<
"Maximum view rotation: " << max_view_angle*180.0/
M_PI <<
" degrees" << endl;
406 cout <<
"Maximum angle rotation: " << max_rot_angle*180.0/
M_PI <<
" degrees" << endl;
407 cout <<
"Step sizes: " << viewstep*180.0/
M_PI <<
" "
408 << rotstep*180.0/
M_PI <<
" degrees" << endl;
409 cout <<
"Accuracy: " << accuracy*180.0/
M_PI <<
" degrees" << endl;
410 cout <<
"Number of views: " << nv << endl;
411 cout <<
"Density radius: " << radius << endl << endl;
415 double s, a, d, d1, d2, ccavg(0), d1avg(0), d2avg(0);
421 cout <<
"DEBUG model_refine_components: map_origin=" << map_origin <<
" scale=" << scale << endl;
425 int* num =
new int[ptemp->
images()];
426 for ( n=0; n<ptemp->
images(); n++ ) num[n] = 0;
429 if ( comp->
sel ) ncomp++;
435 loc = comp->
loc/scale + map_origin;
443 cout <<
"Comp\tType\tdx\tdy\tdz\tCC" << endl;
445 dispatch_queue_t myq = dispatch_queue_create(NULL, NULL);
446 dispatch_apply(ncomp, dispatch_get_global_queue(0, 0), ^(
size_t i){
449 max_shift, max_view_angle, max_rot_angle, shift_flag,
planf,
planb);
452 max_rot_angle, viewstep, rotstep, hires, lores, accuracy,
455 dispatch_sync(myq, ^{
456 cout << comparr[i]->
id <<
tab << comparr[i]->
sel <<
tab
457 << setprecision(2) << comparr[i]->
vec
458 <<
tab << setprecision(4) << comparr[i]->
fom << endl;
462 #pragma omp parallel for
463 for ( i=0; i<ncomp; i++ ) {
466 max_shift, max_view_angle, max_rot_angle, shift_flag,
planf,
planb);
469 max_rot_angle, viewstep, rotstep, hires, lores, accuracy,
473 cout << comparr[i]->
id <<
tab << comparr[i]->
sel <<
tab
474 << setprecision(2) << comparr[i]->
vec
475 <<
tab << setprecision(4) << comparr[i]->
fom << endl;
480 cout <<
"Comp\tType\tdx\tdy\tdz\tShift\tCC\tDens1\tDens2\tdDens" << endl;
481 for ( i=0; i<ncomp; i++ ) {
485 loc = comp->
loc/scale + map_origin;
486 shift_avg += comp->
vec;
490 d2 = p->
density(0, loc, radius);
494 for ( n=1, name = ct_names; n<comp->
sel && n<ptemp->
images(); n++, name = name->
next ) ;
498 << setprecision(2) << comp->
vec[0] <<
tab << comp->
vec[1] <<
tab << comp->
vec[2]
500 << setprecision(4) << comp->
fom <<
tab << d1 <<
tab << d2 <<
tab << d2 - d1 << endl;
513 cout <<
"Average\t\t" << shift_avg[0] <<
tab << shift_avg[1] <<
tab <<
514 shift_avg[2] <<
tab << s <<
tab << ccavg <<
tab <<
515 d1avg <<
tab << d2avg <<
tab << d2avg - d1avg << endl;
516 cout <<
"Component type distribution:\n#\tType\tCount" << endl;
517 for ( n=0, name = ct_names; n<ptemp->
images(); n++, name = name->
next )
518 cout << n+1 <<
tab << *name <<
tab << num[n] << endl;
521 for ( n=0, a=s=0, link =
model->
link; link; link = link->
next, n++ ) {
529 if ( s > 0 ) s = sqrt(s);
532 cout <<
"Average link length: " << a <<
" A (" << s <<
")" << endl << endl;
560 Bimage* pfsmask,
double hires,
double lores,
double max_shift,
int shift_flag,
double bias)
562 if ( max_shift <= 0 ) max_shift = 1e37;
568 cerr <<
"Error: No model map file read! (" <<
model->
fmap <<
")" << endl;
575 if ( pmask ) ptemp->
multiply(pmask);
577 if ( hires > lores ) swap(hires, lores);
579 double radius = ptemp->
sizeX()/2;
582 cout <<
"Refining link positions:" << endl;
583 cout <<
"Resolution limits: " << hires <<
" " << lores <<
" A" << endl;
584 cout <<
"Maximum shift: " << max_shift <<
" A" << endl;
585 cout <<
"Density radius: " << radius << endl;
586 cout <<
"Bias: " << bias << endl << endl;
589 long n, nlink, imax(0);
602 double ccmax, s, d1, d2, ccavg(0), d1avg(0), d2avg(0);
603 int* num =
new int[ptemp->
images()];
604 double* cc =
new double[ptemp->
images()];
605 for ( n=0; n<ptemp->
images(); n++ ) cc[n] = num[n] = 0;
611 cout <<
"Bond\tdx\tdy\tdz\tShift\tDens1\tDens2\tdDens";
612 for ( n=0; n<ptemp->
images(); n++ ) cout <<
"\tCC" << n+1;
615 for ( nlink=0, link =
model->
link; link; link = link->
next, nlink++ )
if ( link->
sel ) {
617 d1 = p->
density(0, link_center/scale + map_origin, radius);
619 plink = p->
extract(0, map_origin + link_center/p->
sampling(0)[0], size, origin, mat);
620 if ( pmask ) plink->
multiply(pmask);
621 if ( pfsmask ) pmaskrot = pfsmask->
extract_wrap(0, size, mat);
623 for ( n=0; n<ptemp->
images(); n++ ) {
627 if ( n==0 ) cc[n] *= bias;
628 if ( ccmax < cc[n] ) {
631 if ( shift_flag == 1 ) shift[0] = shift[1] = 0;
632 else if ( shift_flag == 2 ) shift[2] = 0;
633 link_shift = scale * (mat * shift);
641 if ( s > max_shift ) {
642 link_shift *= max_shift/s;
645 link->
comp[0]->
loc += link_shift;
646 link->
comp[1]->
loc += link_shift;
650 shift_avg += link_shift;
652 d2 = p->
density(0, link_center/scale + map_origin, radius);
656 cout << nlink+1 <<
tab << link_shift[0] <<
tab << link_shift[1] <<
tab <<
657 link_shift[2] <<
tab << s <<
tab << d1 <<
tab << d2 <<
tab << d2 - d1;
658 for ( n=0; n<ptemp->
images(); n++ ) cout <<
tab << cc[n];
677 cout <<
"Average\t" << shift_avg[0] <<
tab << shift_avg[1] <<
tab <<
678 shift_avg[2] <<
tab << s <<
tab << d1avg <<
tab << d2avg <<
tab <<
679 d2avg - d1avg <<
tab << ccavg << endl << endl;
680 cout <<
"Distribution:";
681 for ( n=0; n<ptemp->
images(); n++ ) cout <<
tab << num[n];
682 cout << endl << endl;
705 if ( origin[0] == 0 && origin[1] == 0 && origin[2] == 0 )
708 if ( npt < 1 ) npt = 1;
710 long i, j, n, navg, nc(0);
720 for ( navg=0, mp =
model; mp; mp = mp->
next )
if ( mp->
sel ) {
721 for ( ct = mp->
type; ct; ct = ct->
next )
if ( ct->
sel ) {
722 for ( temptype = comptypelist; temptype && *temptype != ct->
id; temptype = temptype->
next ) ;
725 if ( !comptypelist ) comptypelist = comptype;
733 for ( ct = mp->
type; ct; ct = ct->
next )
if ( ct->
sel ) {
734 for ( n=0, temptype = comptypelist; temptype && *temptype != ct->
id; temptype = temptype->
next ) n++;
742 cout <<
"Averaging component densities:" << endl;
743 cout <<
"Number of selected types: " << navg << endl;
744 cout <<
"Number of averages per type: " << npt << endl;
745 cout <<
"Size: " << size << endl;
746 cout <<
"Origin: " << origin << endl;
751 int* ncomp =
new int[navg];
752 for ( n=0; n<navg; n++ ) ncomp[n] = 0;
758 double radius = origin[0];
768 cerr <<
"Error: No model map file read! (" << mp->
fmap <<
")" << endl;
775 cout <<
"Model: " << mp->
id << endl;
776 cout <<
"Map: " << mp->
fmap <<
" (" << mp->
img_num <<
")" << endl;
777 cout <<
"Comp\tAvg#\tvx\tvy\tvz\tva" << endl;
779 for ( comp = mp->
comp; comp; comp = comp->
next )
if ( comp->
sel ) {
781 n = ct->
num * npt + nc%npt;
784 cout << comp->
id <<
tab << n+1 <<
tab << comp->
view << endl;
788 pone = p->
extract(0, loc, size, origin, comp->
view);
789 for ( i=n*imgsize, j=0; j<imgsize; i++, j++ ) {
790 pcomp->
add(i, (*pone)[j]);
791 fom[i] += (*pone)[j]*(*pone)[j];
799 for ( i=n=0; n<navg; n++ )
if ( ncomp[n] ) {
801 for ( j=0; j<imgsize; i++, j++ ) {
802 fom[i] = fom[i]/ncomp[n] - (*pcomp)[i]*(*pcomp)[i];
803 if ( fom[i] > 0 ) fom[i] = sqrt(fom[i]);
809 cout <<
"Component types:\n#\tType\tCount" << endl;
811 for ( i=0; i<npt; i++, n++ )
812 cout << n+1 <<
tab << ct->
id <<
tab << ncomp[n] << endl;
840 cerr <<
"Error: No model map file read! (" <<
model->
fmap <<
")" << endl;
848 if ( origin[0] == 0 && origin[1] == 0 && origin[2] == 0 )
854 cout <<
"Extracting component densities:" << endl;
855 cout <<
"Number of selected components: " << ncomp << endl;
856 cout <<
"Size: " << size << endl;
857 cout <<
"Origin: " << origin << endl;
860 double radius = origin[0];
869 cout <<
"Model: " <<
model->
id << endl;
871 cout <<
"Map origin: " << p->
image->
origin() << endl;
872 cout <<
"Comp\tx\ty\tz\tvx\tvy\tvz\tva" << endl;
876 dispatch_apply(ncomp, dispatch_get_global_queue(0, 0), ^(
size_t i){
884 cout << comp->
id <<
tab << loc <<
tab << comp->
view << endl;
887 #pragma omp parallel for
888 for (
long i=0; i<ncomp; i++ ) {
896 cout << comp->
id <<
tab << loc <<
tab << comp->
view << endl;
919 if ( origin[0] == 0 && origin[1] == 0 && origin[2] == 0 )
929 if ( link->
sel > navg ) navg = link->
sel;
932 cout <<
"Averaging link density" << endl << endl;
933 cout <<
"Number of averages: " << navg << endl;
934 cout <<
"Size: " << size << endl;
935 cout <<
"Origin: " << origin << endl;
938 int* nlink =
new int[navg+1];
939 for ( n=0; n<=navg; n++ ) nlink[n] = 0;
953 cerr <<
"Error: No model map file read! (" << mp->
fmap <<
")" << endl;
960 cout <<
"Comp1\tComp2\tAvg#" << endl;
961 for ( link = mp->
link; link; link = link->
next )
if ( link->
sel ) {
962 n = (long) link->
sel;
970 pone = p->
extract(0, loc, size, origin, mat);
971 for ( i=(n-1)*imgsize, j=0; j<imgsize; i++, j++ ) {
972 plink->
add(i, (*pone)[j]);
973 fom[i] += (*pone)[j]*(*pone)[j];
981 for ( i=n=0; n<navg; n++ )
if ( nlink[n] )
983 for ( j=0; j<imgsize; i++, j++ ) {
984 fom[i] = fom[i]/nlink[n] - (*plink)[i]*(*plink)[i];
985 if ( fom[i] > 0 ) fom[i] = sqrt(fom[i]);
1011 long i, ntype(1), nmap(1), nb;
1019 if ( ct->
sel ) ntype++;
1021 if ( ct->
sel )
break;
1022 if ( flags & 2 ) nmap = ntype;
1025 cerr <<
"Error: No component types defined for model " <<
model->
id << endl;
1032 cerr <<
"Error: No component map file read! (" << ct->
fmod <<
")" << endl;
1040 if ( origin[0] == 0 && origin[1] == 0 && origin[2] == 0 )
1048 cout <<
"Building new maps from component densities:" << endl;
1049 cout <<
"Component types selected: " << ntype << endl;
1050 cout <<
"Number of maps to build: " << pmap->
images() << endl;
1051 cout <<
"Size: " << pmap->
size() << endl;
1052 cout <<
"Origin: " << origin << endl;
1061 long x, y, z, n, ncomp, nlink;
1062 double radius, linklength, weight;
1070 cerr <<
"Error: No component map file read! (" << ct->
fmod <<
")" << endl;
1075 cout <<
"Comp\tType\tFile\tImage" << endl;
1076 for ( ncomp=nb=0, comp =
model->
comp; comp; comp = comp->
next, ncomp++ )
1077 if ( comp->
sel && comp->
type == ct ) {
1082 for ( i=0, nlink=0, linklength=0; comp->
link[i]; i++ ) {
1086 if ( nlink ) linklength /= 2*nlink;
1087 for ( z = 1; z < p->
sizeZ()-1; z++ ) {
1089 for ( y = 1; y < p->
sizeY()-1; y++ ) {
1091 for ( x = 1; x < p->
sizeX()-1; x++ ) {
1093 loc = (mat * v) + ori_comp;
1094 if ( loc[0]>=0 && loc[0]<=pmap->
sizeX()-2 && loc[1]>=0 && loc[1]<=pmap->
sizeY()-2 && loc[2]>=0 && loc[2]<=pmap->
sizeZ()-2 ) {
1095 i = p->
index(0, x, y, z, 0);
1098 radius = v.
length() - linklength;
1099 if ( radius > 0 ) weight = 1 - 0.002*radius*radius;
1103 pmap->
add(loc[0], loc[1], loc[2], n, weight*(*p)[i]);
1105 pmap->
set_max(loc[0], loc[1], loc[2], n, weight*(*p)[i]);
1106 if ( fom ) fom[pmap->
index(loc, n)] += weight;
1115 if ( flags & 2 ) n++;
1117 cout <<
"Built " << nb <<
" components of type " << ct->
id <<
" into map" << endl;
1120 if ( flags & 1 )
for ( i=0; i<pmap->
data_size(); i++ )
1121 if ( fom[i] ) pmap->
set(i, (*pmap)[i]/fom[i]);
1144 cerr <<
"Error: No link map file read! (" <<
model->
type->
fmod <<
")" << endl;
1150 if ( origin[0] == 0 && origin[1] == 0 && origin[2] == 0 )
1160 cout <<
"Building a new map from a link density" << endl << endl;
1161 cout <<
"Size: " << pmap->
size() << endl;
1162 cout <<
"Origin: " << origin << endl;
1168 long x, y, z, n, xmin, xmax, nlink;
1169 double linklength, fx, weight;
1175 cout <<
"Comp1\tComp2\tMap" << endl;
1176 for ( nlink=0, link =
model->
link; link; link = link->
next, nlink++ )
if ( link->
sel ) {
1177 n = (long) link->
sel - 1;
1187 for ( z = 1; z < p->
sizeZ()-1; z++ ) {
1189 for ( y = 1; y < p->
sizeY()-1; y++ ) {
1191 for ( x = 1; x < p->
sizeX()-1; x++ ) {
1193 loc = (mat * v) + ori_link;
1194 if ( loc[0]>=0 && loc[0]<=pmap->
sizeX()-2 && loc[1]>=0 && loc[1]<=pmap->
sizeY()-2 && loc[2]>=0 && loc[2]<=pmap->
sizeZ()-2 ) {
1195 i = p->
index(0, x, y, z, n);
1198 if ( x < xmin ) fx = xmin - x;
1199 else if ( x > xmax ) fx = x - xmax;
1200 if ( fx ) weight = 1 - 0.002*fx*fx;
1202 pmap->
add(loc[0], loc[1], loc[2], 0, weight*(*p)[i]);
1210 if ( flags & 1 )
for ( i=0; i<pmap->
data_size(); i++ )
1211 if ( fom[i] ) pmap->
set(i, (*pmap)[i]/fom[i]);