00001 #include "../headers/mdpbase.h"
00002
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
00066
00067
00068
00069
00070 void store_vertice_positions(s_mdconcat *m, c_lst_pockets *pockets) {
00071 if (pockets) {
00072 int z;
00073 int n = pockets->vertices->nvert;
00074 size_t old_n = m->n_vertpos;
00075
00076 if (m->n_vertpos == 0) alloc_first_md_concat(m, n);
00077 else realloc_md_concat(m, n);
00078 for (z = 0; z < n; z++) {
00079
00080 m->vertpos[old_n + z][0] = pockets->vertices->vertices[z].x;
00081 m->vertpos[old_n + z][1] = pockets->vertices->vertices[z].y;
00082 m->vertpos[old_n + z][2] = pockets->vertices->vertices[z].z;
00083 m->vertpos[old_n + z][3] = pockets->vertices->vertices[z].ray;
00084 }
00085 }
00086 }
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 void calculate_md_dens_grid(s_mdgrid *g, c_lst_pockets *pockets, s_mdparams *par) {
00105 int xidx = 0, yidx = 0, zidx = 0;
00106 int sxidx, syidx, szidx;
00107 int sxi = 0, syi = 0, szi = 0;
00108 float vx, vy, vz;
00109 float incre = 1.0;
00110 node_pocket *cp = NULL;
00111 s_pocket *pocket = NULL;
00112 node_vertice *cnv = NULL;
00113 s_vvertice *cv = NULL;
00114 int s = (int) (((float) M_MDP_CUBE_SIDE / 2.0) / g->resolution);
00115
00116
00117 cp = pockets->first;
00118 while (cp) {
00119 pocket = cp->pocket;
00120 cnv = pocket->v_lst->first;
00121
00122 if (par->flag_scoring) incre = pocket->pdesc->drug_score;
00123 while (cnv) {
00124 cv = cnv->vertice;
00125 vx = cv->x;
00126 vy = cv->y;
00127 vz = cv->z;
00128 xidx = (int) roundf((vx - g->origin[0]) / g->resolution);
00129 yidx = (int) roundf((vy - g->origin[1]) / g->resolution);
00130 zidx = (int) roundf((vz - g->origin[2]) / g->resolution);
00131
00132
00133 for (sxi = -s; sxi <= s; sxi++) {
00134 for (syi = -s; syi <= s; syi++) {
00135 for (szi = -s; szi <= s; szi++) {
00136
00137 if ((sxi * sxi + syi * syi + szi * szi) != 0) {
00138
00139 if (sxi < 0) sxidx = (int) ceil(((float) sxi + vx - g->origin[0]) / g->resolution);
00140 else if (sxi > 0) sxidx = (int) floor(((float) sxi + vx - g->origin[0]) / g->resolution);
00141 else if (sxi == 0) sxidx = xidx;
00142 if (syi < 0) syidx = (int) ceil(((float) syi + vy - g->origin[1]) / g->resolution);
00143 else if (syi > 0) syidx = (int) floor(((float) syi + vy - g->origin[1]) / g->resolution);
00144 else if (syi == 0) syidx = yidx;
00145 if (szi < 0) szidx = (int) ceil(((float) szi + vz - g->origin[2]) / g->resolution);
00146 else if (szi > 0) szidx = (int) floor(((float) szi + vz - g->origin[2]) / g->resolution);
00147 else if (szi == 0) szidx = zidx;
00148
00149 if (((sxidx != xidx) || (syidx != yidx) || (szidx != zidx))
00150 && (sxidx >= 0 && syidx >= 0 && szidx >= 0 && sxidx < g->nx && syidx < g->ny && szidx < g->nz)) {
00151 g->gridvalues[sxidx][syidx][szidx] += incre;
00152 }
00153 }
00154 }
00155 }
00156 }
00157 cnv = cnv->next;
00158 }
00159
00160 if (xidx < g->nx && yidx < g->ny && zidx < g->nz && xidx >= 0 && yidx >= 0 && zidx >= 0) {
00161
00162 g->gridvalues[xidx][yidx][zidx] += incre;
00163
00164 } else fprintf(stderr, "\n\nWarning (oh oh!!) : Your structure is not aligned or is moving a lot. Results might not reflect what you expect. Consider first structural alignemnt\nIf your structure is moving a lot, consider to split up analysis in two distinct parts.\n\n");
00165 cp = cp->next;
00166 }
00167 }
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184 void update_md_grid(s_mdgrid *g, s_mdgrid *refg, c_lst_pockets *pockets, s_mdparams *par) {
00185 int xidx = 0, yidx = 0, zidx = 0;
00186 int sxidx = 0, syidx = 0, szidx = 0;
00187 int sxi = 0, syi = 0, szi = 0;
00188 float vx, vy, vz;
00189 float incre = 1.0;
00190 node_pocket *cp = NULL;
00191 s_pocket *pocket = NULL;
00192 node_vertice *cnv = NULL;
00193 s_vvertice *cv = NULL;
00194 int s = (int) (((float) M_MDP_CUBE_SIDE / 2.0) / g->resolution);
00195
00196
00197 cp = pockets->first;
00198 while (cp) {
00199 pocket = cp->pocket;
00200 cnv = pocket->v_lst->first;
00201
00202 if (par->flag_scoring) incre = pocket->pdesc->drug_score;
00203 while (cnv) {
00204 cv = cnv->vertice;
00205 vx = cv->x;
00206 vy = cv->y;
00207 vz = cv->z;
00208 xidx = (int) roundf((vx - g->origin[0]) / g->resolution);
00209 yidx = (int) roundf((vy - g->origin[1]) / g->resolution);
00210 zidx = (int) roundf((vz - g->origin[2]) / g->resolution);
00211
00212
00213
00214 for (sxi = -s; sxi <= s; sxi++) {
00215 for (syi = -s; syi <= s; syi++) {
00216 for (szi = -s; szi <= s; szi++) {
00217
00218 if ((sxi * sxi + syi * syi + szi * szi) != 0) {
00219
00220 if (sxi < 0) sxidx = (int) ceil(((float) sxi + vx - g->origin[0]) / g->resolution);
00221 else if (sxi > 0) sxidx = (int) floor(((float) sxi + vx - g->origin[0]) / g->resolution);
00222 else if (sxi == 0) sxidx = xidx;
00223 if (syi < 0) syidx = (int) ceil(((float) syi + vy - g->origin[1]) / g->resolution);
00224 else if (syi > 0) syidx = (int) floor(((float) syi + vy - g->origin[1]) / g->resolution);
00225 else if (syi == 0) syidx = yidx;
00226 if (szi < 0) szidx = (int) ceil(((float) szi + vz - g->origin[2]) / g->resolution);
00227 else if (szi > 0) szidx = (int) floor(((float) szi + vz - g->origin[2]) / g->resolution);
00228 else if (szi == 0) szidx = zidx;
00229
00230 if (((sxidx != xidx) || (syidx != yidx) || (szidx != zidx))
00231 && (sxidx >= 0 && syidx >= 0 && szidx >= 0 && sxidx < g->nx && syidx < g->ny && szidx < g->nz)) {
00232 if (refg->gridvalues[sxidx][syidx][szidx] < 1.0) {
00233 g->gridvalues[sxidx][syidx][szidx] += incre;
00234 refg->gridvalues[sxidx][syidx][szidx] = 1.0;
00235
00236 }
00237 }
00238 }
00239 }
00240 }
00241 }
00242 cnv = cnv->next;
00243 }
00244
00245 if (xidx < g->nx && yidx < g->ny && zidx < g->nz && xidx >= 0 && yidx >= 0 && zidx >= 0) {
00246 if (refg->gridvalues[xidx][yidx][zidx] < 1.0) {
00247 g->gridvalues[xidx][yidx][zidx] += incre;
00248 refg->gridvalues[xidx][yidx][zidx] = 1.0;
00249 }
00250 } else fprintf(stderr, "\n\nWarning (oh oh!!) : Your structure is not aligned or is moving a lot. Results might not reflect what you expect. Consider first structural alignemnt\nIf your structure is moving a lot, consider to split up analysis in two distinct parts.\n\n");
00251 cp = cp->next;
00252 }
00253
00254 }
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269 void reset_grid(s_mdgrid *g) {
00270 int x, y, z;
00271 for (x = 0; x < g->nx; x++) {
00272 for (y = 0; y < g->ny; y++) {
00273 for (z = 0; z < g->nz; z++) {
00274 g->gridvalues[x][y][z] = 0.0;
00275 }
00276 }
00277 }
00278 }
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 void project_grid_on_atoms(s_mdgrid *g, s_pdb *pdb) {
00296 s_atm *ca = NULL;
00297 float x, y, z;
00298 int ix, iy, iz;
00299 float maxix, maxiy, maxiz;
00300 int i;
00301 float density;
00302 float ngridpoints1D = (float) M_MDP_ATOM_DENSITY_DIST / g->resolution;
00303 for (i = 0; i < pdb->natoms; i++) {
00304 density = 0.0;
00305 ca = pdb->latoms_p[i];
00306
00307 x = ca->x;
00308 y = ca->y;
00309 z = ca->z;
00310
00311 ix = (int) floor((x - M_MDP_ATOM_DENSITY_DIST - g->origin[0]) / g->resolution);
00312
00313 maxix = (int) ceil((x + M_MDP_ATOM_DENSITY_DIST - g->origin[0]) / g->resolution);
00314 maxiy = (int) ceil((y + M_MDP_ATOM_DENSITY_DIST - g->origin[1]) / g->resolution);
00315 maxiz = (int) ceil((z + M_MDP_ATOM_DENSITY_DIST - g->origin[2]) / g->resolution);
00316 while (ix <= maxix && g->nx > ix) {
00317 iy = (int) floor((y - M_MDP_ATOM_DENSITY_DIST - g->origin[1]) / g->resolution);
00318 while (iy <= maxiy && g->ny > iy) {
00319 iz = (int) floor((z - M_MDP_ATOM_DENSITY_DIST - g->origin[2]) / g->resolution);
00320 while (iz <= maxiz && g->nz > iz) {
00321 density += g->gridvalues[ix][iy][iz];
00322 iz++;
00323 }
00324 iy++;
00325 }
00326 ix++;
00327 }
00328
00329 ca->bfactor = log(1 + density / (ngridpoints1D) / (float) g->n_snapshots);
00330 }
00331 }
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348 void normalize_grid(s_mdgrid *g, int n) {
00349 int x, y, z;
00350 for (x = 0; x < g->nx; x++) {
00351 for (y = 0; y < g->ny; y++) {
00352 for (z = 0; z < g->nz; z++) {
00353 g->gridvalues[x][y][z] /= (float) n;
00354 }
00355 }
00356 }
00357 }
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374 s_mdconcat *init_md_concat(void) {
00375 s_mdconcat *m = (s_mdconcat *) my_malloc(sizeof (s_mdconcat));
00376 m->n_vertpos = 0;
00377 m->vertpos = NULL;
00378 m->n_snapshots = 0;
00379 return m;
00380 }
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397 s_min_max_pockets *float_get_min_max_from_pockets(c_lst_pockets *pockets) {
00398 if (pockets) {
00399 int z;
00400 float minx = 50000., maxx = -50000., miny = 50000., maxy = -50000., minz = 50000., maxz = -50000.;
00401 int n = pockets->vertices->nvert;
00402
00403 for (z = 0; z < n; z++) {
00404
00405 if (minx > pockets->vertices->vertices[z].x) minx = pockets->vertices->vertices[z].x;
00406 else if (maxx < pockets->vertices->vertices[z].x)maxx = pockets->vertices->vertices[z].x;
00407 if (miny > pockets->vertices->vertices[z].y) miny = pockets->vertices->vertices[z].y;
00408 else if (maxy < pockets->vertices->vertices[z].y)maxy = pockets->vertices->vertices[z].y;
00409 if (minz > pockets->vertices->vertices[z].z) minz = pockets->vertices->vertices[z].z;
00410 else if (maxz < pockets->vertices->vertices[z].z)maxz = pockets->vertices->vertices[z].z;
00411 }
00412 s_min_max_pockets *r = (s_min_max_pockets *) my_malloc(sizeof (s_min_max_pockets));
00413 r->maxx = maxx;
00414 r->maxy = maxy;
00415 r->maxz = maxz;
00416 r->minx = minx;
00417 r->miny = miny;
00418 r->minz = minz;
00419 return (r);
00420 }
00421 return (NULL);
00422 }
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438 s_mdgrid *init_md_grid(c_lst_pockets *pockets) {
00439 s_mdgrid *g = (s_mdgrid *) my_malloc(sizeof (s_mdgrid));
00440 s_min_max_pockets *mm = float_get_min_max_from_pockets(pockets);
00441 float xmax = mm->maxx;
00442 float ymax = mm->maxy;
00443 float zmax = mm->maxz;
00444 float xmin = mm->minx;
00445 float ymin = mm->miny;
00446 float zmin = mm->minz;
00447 float initvalue = 0.0;
00448 my_free(mm);
00449 int cx, cy, cz;
00450 float span = (M_MDP_CUBE_SIDE / 2.0) / M_MDP_GRID_RESOLUTION;
00451 g->resolution = M_MDP_GRID_RESOLUTION;
00452
00453 g->nx = 1 + (int) (xmax + 30. * span - xmin) / (g->resolution);
00454 g->ny = 1 + (int) (ymax + 30. * span - ymin) / (g->resolution);
00455 g->nz = 1 + (int) (zmax + 30. * span - zmin) / (g->resolution);
00456
00457 g->gridvalues = (float ***) my_malloc(sizeof (float **) * g->nx);
00458 for (cx = 0; cx < g->nx; cx++) {
00459 g->gridvalues[cx] = (float **) my_malloc(sizeof (float *) * g->ny);
00460 for (cy = 0; cy < g->ny; cy++) {
00461 g->gridvalues[cx][cy] = (float *) my_malloc(sizeof (float) * g->nz);
00462 for (cz = 0; cz < g->nz; cz++) {
00463 g->gridvalues[cx][cy][cz] = initvalue;
00464
00465 }
00466 }
00467 }
00468
00469 g->origin = (float *) my_malloc(sizeof (float) *3);
00470 g->origin[0] = xmin - 15. * span;
00471 g->origin[1] = ymin - 15. * span;
00472 g->origin[2] = zmin - 15. * span;
00473 return g;
00474 }
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490 void alloc_first_md_concat(s_mdconcat *m, size_t n) {
00491 size_t z;
00492 m->vertpos = (float **) my_malloc(sizeof (float *) * n);
00493 for (z = 0; z < n; z++) {
00494 m->vertpos[z] = (float *) my_malloc(sizeof (float) *4);
00495 m->vertpos[z][0] = 0.0;
00496 m->vertpos[z][1] = 0.0;
00497 m->vertpos[z][2] = 0.0;
00498 m->vertpos[z][3] = 0.0;
00499 }
00500 m->n_vertpos = n;
00501 }
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518 void realloc_md_concat(s_mdconcat *m, size_t n) {
00519 size_t z;
00520 m->vertpos = (float **) my_realloc(m->vertpos, sizeof (float *) *(m->n_vertpos + n));
00521 for (z = 0; z < n; z++) {
00522 m->vertpos[m->n_vertpos + z] = (float *) my_malloc(sizeof (float) *4);
00523 m->vertpos[m->n_vertpos + z][0] = 0.0;
00524 m->vertpos[m->n_vertpos + z][1] = 0.0;
00525 m->vertpos[m->n_vertpos + z][2] = 0.0;
00526 m->vertpos[m->n_vertpos + z][3] = 0.0;
00527 }
00528 m->n_vertpos += n;
00529 }
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545 void free_mdconcat(s_mdconcat *m) {
00546 size_t i;
00547 for (i = 0; i < m->n_vertpos; i++) {
00548 my_free(m->vertpos[i]);
00549 }
00550 my_free(m->vertpos);
00551 my_free(m);
00552 }
00553
00554
00555
00556