| 1 |
/* |
| 2 |
* *************************************************************************** |
| 3 |
* GAMER = < Geometry-preserving Adaptive MeshER > |
| 4 |
* Copyright (C) 1994-- Michael Holst and Zeyun Yu |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 |
* *************************************************************************** |
| 20 |
*/ |
| 21 |
|
| 22 |
/* |
| 23 |
* *************************************************************************** |
| 24 |
* File: PDB2Volume.C < ... > |
| 25 |
* |
| 26 |
* Author: Zeyun Yu ([email protected]) |
| 27 |
* |
| 28 |
* Purpose: Convert a PDB into a 3D Volume |
| 29 |
* |
| 30 |
* Source: Part of this file was adapted from the PDBParser developed in |
| 31 |
* Chandrajit Bajaj's group at The University of Texas at Austin. |
| 32 |
* *************************************************************************** |
| 33 |
*/ |
| 34 |
|
| 35 |
|
| 36 |
#include "PDB2Volume.h" |
| 37 |
|
| 38 |
#define EPSILON 1.0e-3f |
| 39 |
#define MAX_STRING 256 |
| 40 |
|
| 41 |
|
| 42 |
void getMinMax(ATOM *, int, float*, float*); |
| 43 |
float evalDensity(ATOM *,int, float*, double); |
| 44 |
void blurAtoms(ATOM *,int,float*,float*,float *,int*); |
| 45 |
void write_rawiv_float(FILE*,float*,int*,float*,float*); |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
/* |
| 50 |
* *************************************************************************** |
| 51 |
* Routine: PDB2Volume < ... > |
| 52 |
* |
| 53 |
* Author: Zeyun Yu ([email protected]) |
| 54 |
* |
| 55 |
* Purpose: Convert a PDB/PQR/XYZR formats into a 3D Volume |
| 56 |
* |
| 57 |
* Notes: Each atom is treated as a Gaussian function defined by |
| 58 |
* - center and radius: (available in the PDB/PQR files) |
| 59 |
* - blobbyness: the decay rate of the Gaissian function |
| 60 |
* *************************************************************************** |
| 61 |
*/ |
| 62 |
float PDB2Volume(char *filename, float **data, int *xd, int *yd, int *zd, |
| 63 |
float p_min[3], float p_max[3],ATOM **atomlist, int *atom_num, char IsXYZR) |
| 64 |
{ |
| 65 |
int m,n,k; |
| 66 |
char line[MAX_STRING]; |
| 67 |
ATOM *atom_list; |
| 68 |
char string[8]; |
| 69 |
float min[3],max[3]; |
| 70 |
int dim[3]; |
| 71 |
float min_dimension; |
| 72 |
float *dataset; |
| 73 |
FILE *fp,*fout; |
| 74 |
PDBelementInformation eInfo; |
| 75 |
char IsPQR,IsPDB; |
| 76 |
float x,y,z,radius; |
| 77 |
char file_name[256]; |
| 78 |
|
| 79 |
|
| 80 |
if (IsXYZR) { |
| 81 |
if ((fp=fopen(filename, "r"))==NULL){ |
| 82 |
printf("read error...\n"); |
| 83 |
exit(0); |
| 84 |
}; |
| 85 |
fscanf(fp,"%d\n",&m); |
| 86 |
atom_list = (ATOM*)malloc(sizeof(ATOM)*m); |
| 87 |
for (n = 0; n < m; n++) { |
| 88 |
fscanf(fp,"%f %f %f %f\n",&x,&y,&z,&radius); |
| 89 |
|
| 90 |
atom_list[n].x = x; |
| 91 |
atom_list[n].y = y; |
| 92 |
atom_list[n].z = z; |
| 93 |
atom_list[n].radius = radius; |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
*atom_num = m; |
| 98 |
|
| 99 |
printf("number of atoms: %d\n",m); |
| 100 |
} |
| 101 |
else { |
| 102 |
IsPQR = 0; |
| 103 |
IsPDB = 0; |
| 104 |
for(m = 0; m<256; m++) { |
| 105 |
if (filename[m+3] == '\0') |
| 106 |
break; |
| 107 |
else if (filename[m] == '.' && |
| 108 |
(filename[m+1] == 'P' || filename[m+1] == 'p') && |
| 109 |
(filename[m+2] == 'Q' || filename[m+2] == 'q') && |
| 110 |
(filename[m+3] == 'R' || filename[m+3] == 'r')/* && |
| 111 |
file_name[m+4] == '\0'*/) { |
| 112 |
IsPQR = 1; |
| 113 |
break; |
| 114 |
} |
| 115 |
else if (filename[m] == '.' && |
| 116 |
(filename[m+1] == 'P' || filename[m+1] == 'p') && |
| 117 |
(filename[m+2] == 'D' || filename[m+2] == 'd') && |
| 118 |
(filename[m+3] == 'B' || filename[m+3] == 'b') /*&& |
| 119 |
file_name[m+4] == '\0'*/) { |
| 120 |
IsPDB = 1; |
| 121 |
break; |
| 122 |
} |
| 123 |
} |
| 124 |
if (IsPQR == 0 && IsPDB == 0) { |
| 125 |
printf("Input file name must be ending with PDB/PQR/XYZR/RAWIV/OFF...\n"); |
| 126 |
exit(0); |
| 127 |
} |
| 128 |
|
| 129 |
if (IsPQR) { |
| 130 |
sprintf(file_name, "%s.xyzr", filename); |
| 131 |
if ((fout=fopen(file_name, "wb"))==NULL){ |
| 132 |
printf("write error...\n"); |
| 133 |
exit(0); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
if ((fp=fopen(filename, "r"))==NULL){ |
| 138 |
printf("read error...\n"); |
| 139 |
exit(0); |
| 140 |
}; |
| 141 |
m = 0; |
| 142 |
while (fgets(line,MAX_STRING,fp) != NULL) { |
| 143 |
if (line[0]=='A' && line[1]=='T' && line[2]=='O' && line[3]=='M') |
| 144 |
m++; |
| 145 |
} |
| 146 |
printf("number of atoms: %d \n",m); |
| 147 |
fclose(fp); |
| 148 |
|
| 149 |
*atom_num = m; |
| 150 |
atom_list = (ATOM*)malloc(sizeof(ATOM)*m); |
| 151 |
if ((fp=fopen(filename, "r"))==NULL){ |
| 152 |
printf("read error...\n"); |
| 153 |
exit(0); |
| 154 |
}; |
| 155 |
m = 0; |
| 156 |
while (fgets(line,MAX_STRING,fp) != NULL) { |
| 157 |
if (line[0]=='A' && line[1]=='T' && line[2]=='O' && line[3]=='M') { |
| 158 |
/* more general format, could be used for pqr format */ |
| 159 |
k = 30; |
| 160 |
while (line[k] == ' ') k++; |
| 161 |
n = 0; |
| 162 |
while (line[k] != ' ') { |
| 163 |
string[n] = line[k]; |
| 164 |
n++; |
| 165 |
k++; |
| 166 |
if (line[k] == '-') |
| 167 |
break; |
| 168 |
} |
| 169 |
string[n] = '\0'; |
| 170 |
atom_list[m].x = atof(string); |
| 171 |
while (line[k] == ' ') k++; |
| 172 |
n = 0; |
| 173 |
while (line[k] != ' ') { |
| 174 |
string[n] = line[k]; |
| 175 |
n++; |
| 176 |
k++; |
| 177 |
if (line[k] == '-') |
| 178 |
break; |
| 179 |
} |
| 180 |
string[n] = '\0'; |
| 181 |
atom_list[m].y = atof(string); |
| 182 |
while (line[k] == ' ') k++; |
| 183 |
n = 0; |
| 184 |
while (line[k] != ' ') { |
| 185 |
string[n] = line[k]; |
| 186 |
n++; |
| 187 |
k++; |
| 188 |
if (line[k] == '-') |
| 189 |
break; |
| 190 |
} |
| 191 |
string[n] = '\0'; |
| 192 |
atom_list[m].z = atof(string); |
| 193 |
|
| 194 |
if (IsPDB) { |
| 195 |
atom_list[m].radius = 1.0f; // default radius |
| 196 |
for( n=0; n<MAX_BIOCHEM_ELEMENTS; n++ ) { |
| 197 |
eInfo = PDBelementTable[n]; |
| 198 |
if( (eInfo.atomName[0] == line[12]) && |
| 199 |
(eInfo.atomName[1] == line[13]) && |
| 200 |
(eInfo.atomName[2] == line[14]) && |
| 201 |
(eInfo.atomName[3] == line[15]) && |
| 202 |
(eInfo.residueName[0] == line[17]) && |
| 203 |
(eInfo.residueName[1] == line[18]) && |
| 204 |
(eInfo.residueName[2] == line[19]) ) { |
| 205 |
atom_list[m].radius = eInfo.radius; |
| 206 |
break; |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
else if (IsPQR) { |
| 211 |
while (line[k] == ' ') k++; |
| 212 |
n = 0; |
| 213 |
while (line[k] != ' ') { |
| 214 |
string[n] = line[k]; |
| 215 |
n++; |
| 216 |
k++; |
| 217 |
if (line[k] == '-') |
| 218 |
break; |
| 219 |
} |
| 220 |
while (line[k] == ' ') k++; |
| 221 |
n = 0; |
| 222 |
while (line[k] != ' ' && line[k] != '\n' && line[k] != '\0') { |
| 223 |
string[n] = line[k]; |
| 224 |
n++; |
| 225 |
k++; |
| 226 |
} |
| 227 |
string[n] = '\0'; |
| 228 |
atom_list[m].radius = atof(string); |
| 229 |
//atom_list[m].radius += 1.0; |
| 230 |
|
| 231 |
if (atom_list[m].radius < 1.0) |
| 232 |
atom_list[m].radius = 1.0; |
| 233 |
|
| 234 |
fprintf(fout, "%f %f %f %f\n",atom_list[m].x,atom_list[m].y,atom_list[m].z,atom_list[m].radius); |
| 235 |
} |
| 236 |
else { |
| 237 |
printf("Input file name must be ending with PDB or PQR..\n"); |
| 238 |
exit(0); |
| 239 |
} |
| 240 |
|
| 241 |
m++; |
| 242 |
} |
| 243 |
} |
| 244 |
fclose(fp); |
| 245 |
} |
| 246 |
if (IsPQR) |
| 247 |
fclose(fout); |
| 248 |
|
| 249 |
|
| 250 |
min[0] = min[1] = min[2] = 0.; |
| 251 |
max[0] = max[1] = max[2] = 0.; |
| 252 |
getMinMax(atom_list, m, min, max); |
| 253 |
p_min[0] = min[0]; |
| 254 |
p_min[1] = min[1]; |
| 255 |
p_min[2] = min[2]; |
| 256 |
p_max[0] = max[0]; |
| 257 |
p_max[1] = max[1]; |
| 258 |
p_max[2] = max[2]; |
| 259 |
|
| 260 |
min_dimension = min((max[0]-min[0]),min((max[1]-min[1]),(max[2]-min[2]))); |
| 261 |
if (min_dimension < 64.0f) { |
| 262 |
min_dimension = 64.0f/min_dimension; |
| 263 |
dim[0] = (int)((max[0]-min[0])*min_dimension)+1; |
| 264 |
dim[1] = (int)((max[1]-min[1])*min_dimension)+1; |
| 265 |
dim[2] = (int)((max[2]-min[2])*min_dimension)+1; |
| 266 |
} |
| 267 |
else { |
| 268 |
dim[0] = (int)(max[0]-min[0])+1; |
| 269 |
dim[1] = (int)(max[1]-min[1])+1; |
| 270 |
dim[2] = (int)(max[2]-min[2])+1; |
| 271 |
} |
| 272 |
dim[0] = (int)(dim[0]*DIM_SCALE); |
| 273 |
dim[1] = (int)(dim[1]*DIM_SCALE); |
| 274 |
dim[2] = (int)(dim[2]*DIM_SCALE); |
| 275 |
|
| 276 |
printf("dimension: %d X %d X %d\n",dim[0],dim[1],dim[2]); |
| 277 |
|
| 278 |
dataset = (float*)malloc(sizeof(float)*dim[0]*dim[1]*dim[2]); |
| 279 |
blurAtoms(atom_list, m, min, max, dataset, dim); |
| 280 |
printf("min[3]: %f %f %f \n",min[0],min[1],min[2]); |
| 281 |
printf("max[3]: %f %f %f \n",max[0],max[1],max[2]); |
| 282 |
printf("span[3]: %f %f %f \n",(max[0] - min[0]) / (float)(dim[0]-1), |
| 283 |
(max[1]-min[1])/(float)(dim[1]-1),(max[2]-min[2])/(float)(dim[2]-1)); |
| 284 |
|
| 285 |
float minval,maxval; |
| 286 |
minval = 999999.0; |
| 287 |
maxval = -999999.0; |
| 288 |
for (k = 0; k < dim[2]; k++) |
| 289 |
for (n = 0; n < dim[1]; n++) |
| 290 |
for (m = 0; m < dim[0]; m++) { |
| 291 |
if (dataset[((k)*dim[1]*dim[0]+(n)*dim[0]+(m))] < minval) |
| 292 |
minval = dataset[((k)*dim[1]*dim[0]+(n)*dim[0]+(m))]; |
| 293 |
if (dataset[((k)*dim[1]*dim[0]+(n)*dim[0]+(m))] > maxval) |
| 294 |
maxval = dataset[((k)*dim[1]*dim[0]+(n)*dim[0]+(m))]; |
| 295 |
} |
| 296 |
printf("min_density: %f max_density: %f \n",minval,maxval); |
| 297 |
|
| 298 |
|
| 299 |
/* write the volume to disk |
| 300 |
if ((fp=fopen("test.rawiv", "w"))==NULL){ |
| 301 |
printf("write error...\n"); |
| 302 |
exit(0); |
| 303 |
}; |
| 304 |
write_rawiv_float(fp,dataset,dim,min,max); |
| 305 |
exit(0); |
| 306 |
*/ |
| 307 |
|
| 308 |
|
| 309 |
*data = dataset; |
| 310 |
*xd = dim[0]; |
| 311 |
*yd = dim[1]; |
| 312 |
*zd = dim[2]; |
| 313 |
*atomlist = atom_list; |
| 314 |
return(maxval); |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
/* |
| 320 |
* *************************************************************************** |
| 321 |
* Routine: blurAtoms < ... > |
| 322 |
* |
| 323 |
* Author: Zeyun Yu ([email protected]) |
| 324 |
* |
| 325 |
* Purpose: Blur all atoms with Gaussian functions |
| 326 |
* Each atom spreads in a region of certain radius |
| 327 |
* *************************************************************************** |
| 328 |
*/ |
| 329 |
void blurAtoms(ATOM *atom_list, int size, float min[3], float max[3], |
| 330 |
float *dataset, int dim[3]) |
| 331 |
{ |
| 332 |
float orig[3],span[3]; |
| 333 |
int i,j,k; |
| 334 |
int m; |
| 335 |
int xdim,ydim,zdim; |
| 336 |
int amax[3],amin[3]; |
| 337 |
double c[3], maxRad; |
| 338 |
|
| 339 |
|
| 340 |
xdim = dim[0]; |
| 341 |
ydim = dim[1]; |
| 342 |
zdim = dim[2]; |
| 343 |
for (k=0; k<zdim; k++) |
| 344 |
for (j=0; j<ydim; j++) |
| 345 |
for (i=0; i<xdim; i++) |
| 346 |
dataset[IndexVect(i,j,k)] = 0; |
| 347 |
|
| 348 |
orig[0] = min[0]; |
| 349 |
orig[1] = min[1]; |
| 350 |
orig[2] = min[2]; |
| 351 |
span[0] = (max[0] - min[0]) / (float)(xdim-1); |
| 352 |
span[1] = (max[1] - min[1]) / (float)(ydim-1); |
| 353 |
span[2] = (max[2] - min[2]) / (float)(zdim-1); |
| 354 |
|
| 355 |
for (m=0; m < size; m++) { |
| 356 |
|
| 357 |
maxRad = atom_list[m].radius * sqrt(1.0 + log(EPSILON) / (2.0*BLOBBYNESS) ); |
| 358 |
|
| 359 |
// compute the dataset coordinates of the atom's center |
| 360 |
c[0] = (atom_list[m].x-orig[0])/span[0]; |
| 361 |
c[0] = ((c[0]-floor(c[0])) >= 0.5) ? ceil(c[0]) : floor(c[0]); |
| 362 |
c[1] = (atom_list[m].y-orig[1])/span[1]; |
| 363 |
c[1] = ((c[1]-floor(c[1])) >= 0.5) ? ceil(c[1]) : floor(c[1]); |
| 364 |
c[2] = (atom_list[m].z-orig[2])/span[2]; |
| 365 |
c[2] = ((c[2]-floor(c[2])) >= 0.5) ? ceil(c[2]) : floor(c[2]); |
| 366 |
|
| 367 |
// then compute the bounding box of the atom (maxRad^3) |
| 368 |
for (j=0; j < 3; j++) { |
| 369 |
int tmp; |
| 370 |
tmp = (int)(c[j] - (maxRad / span[j]) - 1); |
| 371 |
tmp = (tmp < 0) ? 0 : tmp; |
| 372 |
amin[j] = tmp; |
| 373 |
tmp = (int)(c[j] + (maxRad / span[j]) + 1); |
| 374 |
tmp = (tmp > (dim[j]-1)) ? (dim[j]-1) : tmp; |
| 375 |
amax[j] = tmp; |
| 376 |
} |
| 377 |
|
| 378 |
// begin blurring kernel |
| 379 |
for(k = amin[2]; k <= amax[2]; k++) { |
| 380 |
for(j = amin[1]; j <= amax[1]; j++) { |
| 381 |
for(i = amin[0]; i <= amax[0]; i++) { |
| 382 |
float pnt[3], density; |
| 383 |
|
| 384 |
pnt[0] = orig[0] + i*span[0]; |
| 385 |
pnt[1] = orig[1] + j*span[1]; |
| 386 |
pnt[2] = orig[2] + k*span[2]; |
| 387 |
|
| 388 |
density = evalDensity(atom_list,m, pnt, maxRad); |
| 389 |
dataset[IndexVect(i,j,k)] += density; |
| 390 |
} |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
if (((m+1) % 20) == 0 || (m+1) == size) { |
| 395 |
printf("%2.2f%% done (%08d)\r", 100.0*(m+1)/(float)size, m+1); |
| 396 |
fflush(stdout); |
| 397 |
} |
| 398 |
} |
| 399 |
printf("\n"); fflush(stdout); |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
/* |
| 406 |
* *************************************************************************** |
| 407 |
* Routine: evalDensity < ... > |
| 408 |
* |
| 409 |
* Author: Zeyun Yu ([email protected]) |
| 410 |
* |
| 411 |
* Purpose: Calculate the Gaussian function value using the blobbyness |
| 412 |
* *************************************************************************** |
| 413 |
*/ |
| 414 |
float evalDensity(ATOM *atom_list,int index, float pnt[3], double maxRadius) |
| 415 |
{ |
| 416 |
double expval; |
| 417 |
|
| 418 |
double r = (atom_list[index].x-pnt[0])*(atom_list[index].x-pnt[0]) + |
| 419 |
(atom_list[index].y-pnt[1])*(atom_list[index].y-pnt[1]) + |
| 420 |
(atom_list[index].z-pnt[2])*(atom_list[index].z-pnt[2]); |
| 421 |
double r0 = atom_list[index].radius; r0 *= r0; |
| 422 |
//expval = BLOBBYNESS*(r/r0 - 1.0); |
| 423 |
expval = BLOBBYNESS*(r-r0); |
| 424 |
|
| 425 |
// truncated gaussian |
| 426 |
if (sqrt(r) > maxRadius) |
| 427 |
return 0.0; |
| 428 |
|
| 429 |
return (float)(exp(expval)); |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
/* |
| 436 |
* *************************************************************************** |
| 437 |
* Routine: getMinMax < ... > |
| 438 |
* |
| 439 |
* Author: Zeyun Yu ([email protected]) |
| 440 |
* |
| 441 |
* Purpose: Calculate the minimum and maximum range of the blurred atoms |
| 442 |
* *************************************************************************** |
| 443 |
*/ |
| 444 |
void getMinMax(ATOM *atom_list, int size, float min[3], float max[3]) |
| 445 |
{ |
| 446 |
int i, j; |
| 447 |
float maxRad=0.0; |
| 448 |
float tempRad; |
| 449 |
|
| 450 |
|
| 451 |
min[0] = max[0] = atom_list[0].x; |
| 452 |
min[1] = max[1] = atom_list[0].y; |
| 453 |
min[2] = max[2] = atom_list[0].z; |
| 454 |
maxRad = atom_list[0].radius * sqrt(1.+log(EPSILON)/BLOBBYNESS); |
| 455 |
|
| 456 |
for(j = 1; j < size; j++) { |
| 457 |
if (atom_list[j].x < min[0]) |
| 458 |
min[0] = atom_list[j].x; |
| 459 |
if (atom_list[j].y < min[1]) |
| 460 |
min[1] = atom_list[j].y; |
| 461 |
if (atom_list[j].z < min[2]) |
| 462 |
min[2] = atom_list[j].z; |
| 463 |
if (atom_list[j].x > max[0]) |
| 464 |
max[0] = atom_list[j].x; |
| 465 |
if (atom_list[j].y > max[1]) |
| 466 |
max[1] = atom_list[j].y; |
| 467 |
if (atom_list[j].z > max[2]) |
| 468 |
max[2] = atom_list[j].z; |
| 469 |
|
| 470 |
tempRad = atom_list[j].radius * sqrt(1.0 + log(EPSILON)/BLOBBYNESS); |
| 471 |
if (maxRad < tempRad) |
| 472 |
maxRad = tempRad; |
| 473 |
} |
| 474 |
|
| 475 |
for(i = 0; i < 3; i++) { |
| 476 |
min[i] -= maxRad; |
| 477 |
max[i] += maxRad; |
| 478 |
} |
| 479 |
} |