00001
00002 #include "../headers/voronoi.h"
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 static void fill_vvertices(s_lst_vvertice *lvvert, const char fpath[], s_atm *atoms, int natoms,
00066 int min_apol_neigh, float asph_min_size, float asph_max_size) ;
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 s_lst_vvertice* load_vvertices(s_pdb *pdb, int min_apol_neigh, float asph_min_size, float asph_max_size)
00091 {
00092 int i, nb_h=0;
00093 s_atm *ca = NULL ;
00094 s_lst_vvertice *lvvert = NULL ;
00095 char tmpn1[250]="";
00096 char tmpn2[250]="";
00097 pid_t pid=getpid();
00098 sprintf(tmpn1,"/tmp/qvoro_tmp_%d.dat",pid);
00099 sprintf(tmpn2,"/tmp/fpocket_qvor_%d.dat",pid);
00100 FILE *fvoro = fopen(tmpn1, "w+");
00101 FILE *ftmp=fopen(tmpn2,"w");
00102
00103
00104
00105
00106
00107
00108 if(fvoro != NULL) {
00109 lvvert = (s_lst_vvertice *)my_malloc(sizeof(s_lst_vvertice)) ;
00110 lvvert->h_tr=NULL;
00111
00112 for(i = 0; i < pdb->natoms ; i++){
00113 ca = (pdb->latoms)+i ;
00114 if(strcmp(ca->symbol,"H")) {
00115 lvvert->h_tr=(int *)my_realloc(lvvert->h_tr,sizeof(int)*(i-nb_h+1)) ;
00116 lvvert->h_tr[i-nb_h]=i ;
00117 }
00118 else nb_h++;
00119 }
00120 lvvert->n_h_tr=i-nb_h;
00121
00122
00123 fprintf(fvoro,"3 rbox D3\n%d\n", lvvert->n_h_tr);
00124
00125 for(i = 0; i < pdb->natoms ; i++){
00126 ca = (pdb->latoms)+i ;
00127 if(strcmp(ca->symbol,"H")) {
00128
00129
00130 fprintf(fvoro,"%.3f %.3f %.3f \n", ca->x, ca->y, ca->z);
00131 }
00132 }
00133
00134 fflush(fvoro) ;
00135 rewind(fvoro);
00136
00137
00138 run_qvoronoi(fvoro,ftmp);
00139 int status=M_VORONOI_SUCCESS;
00140
00141
00142
00143 if(status == M_VORONOI_SUCCESS) {
00144 fill_vvertices(lvvert, tmpn2, pdb->latoms, pdb->natoms,
00145 min_apol_neigh, asph_min_size, asph_max_size);
00146 }
00147 else {
00148 my_free(lvvert);
00149 lvvert = NULL ;
00150 fprintf(stderr, "! Voronoi command failed with status %d...\n", status) ;
00151 }
00152
00153
00154 }
00155 else {
00156 fprintf(stderr, "! File for Voronoi vertices calculation couldn't be opened...\n") ;
00157 }
00158 fclose(fvoro) ;
00159 fclose(ftmp);
00160 remove(tmpn1);
00161 remove(tmpn2);
00162 return lvvert ;
00163 }
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187 static void fill_vvertices(s_lst_vvertice *lvvert, const char fpath[], s_atm *atoms, int natoms,
00188 int min_apol_neigh, float asph_min_size, float asph_max_size)
00189 {
00190 FILE *f = NULL ;
00191 FILE *fNb = NULL ;
00192 FILE *fvNb = NULL;
00193
00194 s_vvertice *v = NULL ;
00195
00196 float tmpRay;
00197 float xyz[3] = {0,0,0};
00198
00199 int i, j,nchar_max = 255,
00200 vInMem = 0,
00201 curLineNb = 0,
00202 trash = 0,
00203 tmpApolar=0,
00204 curVnbIdx[4],
00205 curNbIdx[4];
00206
00207 char cline[nchar_max],
00208 nbline[nchar_max],
00209 vNbline[nchar_max],
00210 *s_nvert = (char *) my_malloc(sizeof(char)*nchar_max) ;
00211
00212
00213
00214 f = fopen(fpath,"r") ;
00215 fNb = fopen(fpath,"r") ;
00216 fvNb = fopen(fpath,"r") ;
00217
00218 char *status = NULL ;
00219
00220 status = fgets(cline, nchar_max, f) ;
00221 status = fgets(cline, nchar_max, f) ;
00222 status = fgets(nbline, nchar_max, fNb) ;
00223 status = fgets(nbline, nchar_max, fNb) ;
00224 status = fgets(vNbline, nchar_max, fvNb) ;
00225 status = fgets(vNbline, nchar_max, fvNb) ;
00226
00227 sscanf(cline,"%d",&(lvvert->nvert)) ;
00228 lvvert->qhullSize = lvvert->nvert ;
00229 lvvert->tr = (int *) my_malloc(lvvert->nvert*sizeof(int));
00230 for(i = 0 ; i < lvvert->nvert ; i++) lvvert->tr[i] = -1;
00231
00232 lvvert->vertices = (s_vvertice *) my_calloc(lvvert->nvert, sizeof(s_vvertice)) ;
00233 lvvert->pvertices= (s_vvertice **) my_calloc(lvvert->nvert, sizeof(s_vvertice*)) ;
00234
00235
00236
00237 sprintf(s_nvert,"%d",lvvert->nvert);
00238 strcat(s_nvert,"\n");
00239
00240
00241 while(fgets(nbline, nchar_max, fNb) != NULL && strcmp(s_nvert, nbline) != 0) ;
00242
00243 while(fgets(vNbline,nchar_max,fvNb) != NULL && curLineNb++ < lvvert->nvert*2+1) ;
00244
00245 i = 0 ;
00246 while(fgets(cline, nchar_max, f) != NULL) {
00247
00248 if(fgets(nbline, nchar_max,fNb)!=NULL){
00249
00250 if(fgets(vNbline, nchar_max,fvNb)!=NULL){
00251
00252 if(strcmp("\n", cline) != 0 && strcmp("\n", nbline) != 0
00253 && strcmp("\n", vNbline) != 0) {
00254
00255 sscanf(cline,"%f %f %f",&xyz[0], &xyz[1], &xyz[2]);
00256 sscanf(nbline,"%d %d %d %d",&curNbIdx[0],&curNbIdx[1],
00257 &curNbIdx[2],&curNbIdx[3]);
00258 sscanf(vNbline,"%d %d %d %d %d", &trash, &curVnbIdx[0],
00259 &curVnbIdx[1],
00260 &curVnbIdx[2], &curVnbIdx[3]);
00261
00262
00263 tmpRay = testVvertice(xyz, curNbIdx, atoms, asph_min_size,
00264 asph_max_size,lvvert);
00265 if(tmpRay > 0){
00266 v = (lvvert->vertices + vInMem) ;
00267 v->x = xyz[0]; v->y = xyz[1]; v->z = xyz[2];
00268 v->ray = tmpRay;
00269 v->sort_x = -1 ;
00270 v->seen = 0 ;
00271
00272 tmpApolar=0;
00273
00274 for(j = 0 ; j < 4 ; j++) {
00275 v->neigh[j] = &(atoms[lvvert->h_tr[curNbIdx[j]]]);
00276
00277 if(atoms[lvvert->h_tr[curNbIdx[j]]].electroneg<2.8) tmpApolar++ ;
00278 if(curVnbIdx[j]>0) v->vneigh[j] = curVnbIdx[j];
00279 }
00280
00281 v->apol_neighbours = 0 ;
00282 lvvert->tr[i] = vInMem ;
00283
00284 lvvert->pvertices[vInMem] = v ;
00285
00286 vInMem++ ;
00287 v->id = natoms+i+1-vInMem ;
00288
00289 if(tmpApolar >= min_apol_neigh) v->type = M_APOLAR_AS;
00290 else v->type = M_POLAR_AS;
00291
00292 v->qhullId = i;
00293 v->resid = -1;
00294 set_barycenter(v) ;
00295 }
00296 i++ ;
00297 }
00298 }
00299 }
00300 }
00301
00302 lvvert->nvert=vInMem ;
00303 fclose(f) ;
00304 fclose(fNb) ;
00305 fclose(fvNb);
00306 }
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321 void set_barycenter(s_vvertice *v)
00322 {
00323 int i ;
00324 float xsum = 0.0,
00325 ysum = 0.0,
00326 zsum = 0.0 ;
00327
00328 for(i = 0 ; i < 4 ; i++) {
00329 xsum += v->neigh[i]->x ;
00330 ysum += v->neigh[i]->y ;
00331 zsum += v->neigh[i]->z ;
00332 }
00333
00334 v->bary[0] = xsum*0.25 ;
00335 v->bary[1] = ysum*0.25 ;
00336 v->bary[2] = zsum*0.25 ;
00337
00338 }
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359 float testVvertice(float xyz[3], int curNbIdx[4], s_atm *atoms,
00360 float min_asph_size, float max_asph_size,
00361 s_lst_vvertice *lvvert)
00362 {
00363 float x = xyz[0],
00364 y = xyz[1],
00365 z = xyz[2] ;
00366
00367 s_atm *cura = &(atoms[lvvert->h_tr[curNbIdx[0]]]) ;
00368
00369 float distVatom1 = dist(x, y, z, cura->x, cura->y, cura->z) ;
00370 float distVatom2,
00371 distVatom3,
00372 distVatom4;
00373
00374 if(min_asph_size <= distVatom1 + M_PREC_TOLERANCE && distVatom1 - M_PREC_TOLERANCE <= max_asph_size){
00375 cura = &(atoms[lvvert->h_tr[curNbIdx[1]]]) ;
00376 distVatom2 = dist(x, y, z, cura->x, cura->y, cura->z);
00377
00378 cura = &(atoms[lvvert->h_tr[curNbIdx[2]]]) ;
00379 distVatom3 = dist(x, y, z, cura->x, cura->y, cura->z);
00380
00381 cura = &(atoms[lvvert->h_tr[curNbIdx[3]]]) ;
00382 distVatom4=dist(x, y, z, cura->x, cura->y, cura->z);
00383
00384
00385
00386 if(fabs(distVatom1-distVatom2) < M_PREC_TOLERANCE &&
00387 fabs(distVatom1-distVatom3) < M_PREC_TOLERANCE &&
00388 fabs(distVatom1-distVatom4) < M_PREC_TOLERANCE){
00389
00390 return distVatom1;
00391 }
00392
00393 }
00394 return -1.0;
00395 }
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411 void print_vvertices(FILE *f, s_lst_vvertice *lvvert)
00412 {
00413 if(lvvert) {
00414 if(lvvert->vertices) {
00415 int i ;
00416 for(i = 0 ; i < lvvert->nvert ; i++) {
00417 s_vvertice *v = &(lvvert->vertices[i]) ;
00418 if( v->neigh[0] && v->neigh[1] && v->neigh[2] && v->neigh[3]) {
00419 fprintf(f, "====== Vertice %d: =====\n", i);
00420 fprintf(f, "- x = %f, y = %f, z = %f\n", v->x, v->y, v->z);
00421 fprintf(f, "- ix = %d\n",v->sort_x);
00422
00423 float d1 = dist(v->x, v->y, v->z, v->neigh[0]->x, v->neigh[0]->y, v->neigh[0]->z) ;
00424 float d2 = dist(v->x, v->y, v->z, v->neigh[1]->x, v->neigh[1]->y, v->neigh[1]->z) ;
00425 float d3 = dist(v->x, v->y, v->z, v->neigh[2]->x, v->neigh[2]->y, v->neigh[2]->z) ;
00426 float d4 = dist(v->x, v->y, v->z, v->neigh[3]->x, v->neigh[3]->y, v->neigh[3]->z) ;
00427
00428 fprintf(f, "- Neighbour: \n1 - %f (%f %f %f: %d) \n2 - %f (%f %f %f: %d)\n3 - %f (%f %f %f: %d)\n4 - %f (%f %f %f: %d)\n", d1, v->neigh[0]->x, v->neigh[0]->y, v->neigh[0]->z, v->neigh[0]->id, d2, v->neigh[1]->x, v->neigh[1]->y, v->neigh[1]->z, v->neigh[1]->id, d3, v->neigh[2]->x, v->neigh[2]->y , v->neigh[2]->z, v->neigh[2]->id, d4, v->neigh[3]->x, v->neigh[3]->y, v->neigh[3]->z, v->neigh[3]->id);
00429 }
00430 }
00431 }
00432 }
00433 }
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454 float get_verts_volume_ptr(s_vvertice **verts, int nvert, int niter,float correct)
00455 {
00456 int i = 0, j = 0,
00457 nb_in = 0;
00458
00459 float xmin = 0.0, xmax = 0.0,
00460 ymin = 0.0, ymax = 0.0,
00461 zmin = 0.0, zmax = 0.0,
00462 xtmp = 0.0, ytmp = 0.0, ztmp = 0.0,
00463 xr = 0.0, yr = 0.0, zr = 0.0,
00464 vbox = 0.0 ;
00465
00466 s_vvertice *vcur = NULL ;
00467
00468
00469 for(i = 0 ; i < nvert ; i++) {
00470 vcur = verts[i] ;
00471
00472 if(i == 0) {
00473 xmin = vcur->x - vcur->ray + correct ; xmax = vcur->x + vcur->ray + correct ;
00474 ymin = vcur->y - vcur->ray + correct ; ymax = vcur->y + vcur->ray + correct ;
00475 zmin = vcur->z - vcur->ray + correct ; zmax = vcur->z + vcur->ray + correct ;
00476 }
00477 else {
00478
00479 if(xmin > (xtmp = vcur->x - vcur->ray + correct)) xmin = xtmp ;
00480 else if(xmax < (xtmp = vcur->x + vcur->ray + correct)) xmax = xtmp ;
00481
00482 if(ymin > (ytmp = vcur->y - vcur->ray + correct)) ymin = ytmp ;
00483 else if(ymax < (ytmp = vcur->y + vcur->ray + correct)) ymax = ytmp ;
00484
00485 if(zmin > (ztmp = vcur->z - vcur->ray + correct)) zmin = ztmp ;
00486 else if(zmax < (ztmp = vcur->z + vcur->ray + correct)) zmax = ztmp ;
00487 }
00488 }
00489
00490
00491 vbox = (xmax - xmin)*(ymax - ymin)*(zmax - zmin) ;
00492
00493
00494 for(i = 0 ; i < niter ; i++) {
00495 xr = rand_uniform(xmin, xmax) ;
00496 yr = rand_uniform(ymin, ymax) ;
00497 zr = rand_uniform(zmin, zmax) ;
00498
00499 for(j = 0 ; j < nvert ; j++) {
00500 vcur = verts[j] ;
00501 xtmp = vcur->x - xr ;
00502 ytmp = vcur->y - yr ;
00503 ztmp = vcur->z - zr ;
00504
00505
00506 if(((correct+vcur->ray)*(vcur->ray + correct)) > (xtmp*xtmp + ytmp*ytmp + ztmp*ztmp)) {
00507
00508 nb_in ++ ; break ;
00509 }
00510 }
00511 }
00512
00513
00514 return ((float)nb_in)/((float)niter)*vbox;
00515 }
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530 void free_vert_lst(s_lst_vvertice *lvvert)
00531 {
00532 if(lvvert) {
00533
00534 if(lvvert->vertices) {
00535 my_free(lvvert->vertices) ;
00536 lvvert->vertices = NULL ;
00537 }
00538 if(lvvert->pvertices) {
00539 my_free(lvvert->pvertices) ;
00540 lvvert->pvertices = NULL ;
00541 }
00542 if(lvvert->tr) {
00543 my_free(lvvert->tr) ;
00544 lvvert->tr = NULL ;
00545 }
00546 if(lvvert->h_tr) {
00547 my_free(lvvert->h_tr) ;
00548 lvvert->h_tr = NULL ;
00549 }
00550 my_free(lvvert) ;
00551 }
00552 }
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567 int is_in_lst_vert(s_vvertice **lst_vert, int nb_vert, int v_id)
00568 {
00569 int i ;
00570 for(i = 0 ; i < nb_vert ; i++) {
00571 if(v_id == lst_vert[i]->id) return 1 ;
00572 }
00573
00574 return 0 ;
00575 }
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590 int is_in_lst_vert_p(s_vvertice **lst_vert, int nb_vert, s_vvertice *vert)
00591 {
00592 int i ;
00593 for(i = 0 ; i < nb_vert ; i++) {
00594 if(vert == lst_vert[i]) return 1 ;
00595 }
00596
00597 return 0 ;
00598 }
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624 void write_pdb_vert(FILE *f, s_vvertice *v)
00625 {
00626 if(v->type==M_APOLAR_AS) write_pdb_atom_line(f, "HETATM", v->id, "APOL",
00627 ' ', "STP", "C", v->resid, ' ',
00628 v->x, v->y, v->z, 0.0, 0.0,
00629 "Ve", -1);
00630
00631 else write_pdb_atom_line(f, "HETATM", v->id, " POL", ' ', "STP", "C",
00632 v->resid, ' ',v->x, v->y, v->z,0.0, 0.0,
00633 "Ve", -1);
00634 }
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651 void write_pqr_vert(FILE *f, s_vvertice *v)
00652 {
00653 if(v->type==M_APOLAR_AS) write_pqr_atom_line(f, "ATOM", v->id, "APOL", ' ',
00654 "STP", " ", v->resid, ' ',
00655 v->x, v->y, v->z, 0.0, v->ray);
00656
00657 else write_pqr_atom_line(f, "ATOM", v->id, " POL", ' ', "STP", " ",
00658 v->resid, ' ',v->x, v->y, v->z,0.0, v->ray);
00659 }
00660