Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

grdplugin.C

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr
00003  *cr            (C) Copyright 1995-2016 The Board of Trustees of the
00004  *cr                        University of Illinois
00005  *cr                         All Rights Reserved
00006  *cr
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  * RCS INFORMATION:
00011  *
00012  *      $RCSfile: grdplugin.C,v $
00013  *      $Author: johns $       $Locker:  $             $State: Exp $
00014  *      $Revision: 1.17 $       $Date: 2016/11/28 05:01:54 $
00015  *
00016  ***************************************************************************/
00017 
00018 /* 
00019  * "unformatted" binary potential map, as used by Grasp and DelPhi
00020  *
00021  * Format (fortran): 
00022  * character*20 uplbl
00023  * character*10 nxtlbl,character*60 toplbl
00024  * real*4 phi(n,n,n)
00025  * character*16 botlbl
00026  * real*4 scale,oldmid(3)
00027  *
00028  * Where n is the length in grid units of each edge of the grid.
00029  *
00030  * More information can be found at:
00031  * <http://honiglab.cpmc.columbia.edu/grasp/grasp_contents.html#A.2>
00032  * <http://trantor.bioc.columbia.edu/delphi/doc/file_format.html>
00033  */
00034 
00035 #include <stdlib.h>
00036 #include <stdio.h>
00037 #include <ctype.h>
00038 #include <math.h>
00039 #include <string.h>
00040 
00041 #if defined(_AIX)
00042 #include <strings.h>
00043 #endif
00044 
00045 #include "molfile_plugin.h"
00046 #include "endianswap.h"
00047 
00048 typedef struct {
00049   FILE *fd;
00050   int nsets;
00051   int ndata;
00052   int swap;
00053   molfile_volumetric_t *vol;
00054 } grd_t;
00055 
00056 
00057 static void *open_grd_read(const char *filepath, const char *filetype,
00058     int *natoms) {
00059   FILE *fd;
00060   grd_t *grd;
00061   char uplbl[21], nxtlbl[11], toplbl[61];
00062   int swap, recordSize, gridSize, iGrid;
00063   float scale, midX, midY, midZ;
00064   
00065   fd = fopen(filepath, "rb");
00066   if (!fd) {
00067     fprintf(stderr, "grdplugin) Error opening file.\n");
00068     return NULL;
00069   }
00070 
00071   /* Check byte order. The first four bytes of the file always make up the
00072    * integer 20.
00073    */
00074   if (fread(&recordSize, 4, 1, fd) != 1) {
00075     fprintf(stderr, "grdplugin) Error reading file header: uplbl.\n");
00076     return NULL;
00077   }
00078   if (recordSize == 20) {
00079     swap = 0;
00080   }
00081   else {
00082     swap4_aligned(&recordSize, 1);
00083     if (recordSize == 20) {
00084       swap = 1;
00085     }
00086     else {
00087       fprintf(stderr, "grdplugin) Improperly formatted file header: uplbl.\n");
00088       return NULL;
00089     }
00090   }
00091   
00092   /* Check for a valid phimap 
00093    * XXX - Some programs write gibberish for this record, don't worry about
00094    * its contents
00095    * character*20 uplbl
00096    */
00097   if ( (fread(uplbl, 1, 20, fd) != 20) ||
00098        (fread(&recordSize, 4, 1, fd) != 1) ) {
00099     fprintf(stderr, "grdplugin) Error: uplbl does not match.\n");
00100     return NULL;
00101   }
00102 
00103   /* Read in the next record:
00104    * character*10 nxtlbl, character*60 toplbl 
00105    * The labels themselves are currently ignored, but they may be useful in
00106    * the future.
00107    */ 
00108   if (fread(&recordSize, 4, 1, fd) != 1) {
00109     fprintf(stderr, "grdplugin) Error reading file header: nxtlbl.\n");
00110     return NULL;
00111   }
00112   if (swap) {
00113     swap4_aligned(&recordSize, 1);
00114   }
00115   if (recordSize != 70) {
00116     fprintf(stderr, "grdplugin) Improperly formatted file header: nxtlbl.\n");
00117     return NULL;
00118   }
00119   if ( (fread(nxtlbl, 1, 10, fd) != 10) ||
00120        (fread(toplbl, 1, 60, fd) != 60) ||
00121        (fread(&recordSize, 4, 1, fd) != 1) ) {
00122     fprintf(stderr, "grdplugin) Error reading nxtlbl.\n");
00123     return NULL;
00124   }
00125   
00126   /* Find the number of data points in the file
00127    * The next integer gives the number of bytes used to store the data.
00128    */
00129   if (fread(&recordSize, 4, 1, fd) != 1) {
00130     fprintf(stderr, "grdplugin) Error reading file header: grid.\n");
00131     return NULL;
00132   }
00133   if (swap) {
00134     swap4_aligned(&recordSize, 1);
00135   }
00136   iGrid = recordSize / 4;
00137 
00138   /* Find the length in grid units of the edge of the cube, make sure it's
00139    * an integer 
00140    */
00141   gridSize = (int) (pow((double) iGrid, (double) 1.0/3.0) + 0.5);
00142   if ((gridSize*gridSize*gridSize) != iGrid) {
00143     fprintf(stderr, "grdplugin) Error: non-cube grid.\n");
00144     return NULL;
00145   }
00146 
00147   /* Read the scale and midpoint coordinates from the end of the file.
00148    */
00149   if ( (fseek(fd, -20, SEEK_END) != 0) ||
00150        (fread(&scale, sizeof(float), 1, fd) != 1) ||
00151        (fread(&midX, sizeof(float), 1, fd) != 1) ||
00152        (fread(&midY, sizeof(float), 1, fd) != 1) ||
00153        (fread(&midZ, sizeof(float), 1, fd) != 1) ) {
00154     fprintf(stderr, "grdplugin) Error reading scale and midpoint.\n");
00155     return NULL;
00156   }
00157   if (swap) {
00158     swap4_aligned(&scale, 1);
00159     swap4_aligned(&midX, 1);
00160     swap4_aligned(&midY, 1);
00161     swap4_aligned(&midZ, 1);
00162   }
00163 
00164   /* Allocate and initialize the grd structure */
00165   grd = new grd_t;
00166   grd->fd = fd;
00167   grd->vol = NULL;
00168   *natoms = MOLFILE_NUMATOMS_NONE;
00169   grd->nsets = 1; /* this file contains only one data set */
00170   grd->ndata = iGrid;
00171   grd->swap = swap;
00172 
00173   grd->vol = new molfile_volumetric_t[1];
00174   strcpy(grd->vol[0].dataname, "PHIMAP Electron Density Map");
00175 
00176   /* <midX, midY, midZ> is the middle point of the grid. */
00177   grd->vol[0].origin[0] = -0.5*(gridSize+1.0) / scale + midX;
00178   grd->vol[0].origin[1] = -0.5*(gridSize+1.0) / scale + midY;
00179   grd->vol[0].origin[2] = -0.5*(gridSize+1.0) / scale + midZ;
00180 
00181   grd->vol[0].xaxis[0] = gridSize / scale;
00182   grd->vol[0].xaxis[1] = 0;
00183   grd->vol[0].xaxis[2] = 0;
00184 
00185   grd->vol[0].yaxis[0] = 0;
00186   grd->vol[0].yaxis[1] = gridSize / scale;
00187   grd->vol[0].yaxis[2] = 0;
00188 
00189   grd->vol[0].zaxis[0] = 0;
00190   grd->vol[0].zaxis[1] = 0;
00191   grd->vol[0].zaxis[2] = gridSize / scale;
00192 
00193   grd->vol[0].xsize = gridSize;
00194   grd->vol[0].ysize = gridSize;
00195   grd->vol[0].zsize = gridSize;
00196 
00197   grd->vol[0].has_color = 0;
00198 
00199   return grd;
00200 }
00201 
00202 static int read_grd_metadata(void *v, int *nsets, 
00203   molfile_volumetric_t **metadata) {
00204   grd_t *grd = (grd_t *)v;
00205   *nsets = grd->nsets; 
00206   *metadata = grd->vol;  
00207 
00208   return MOLFILE_SUCCESS;
00209 }
00210 
00211 static int read_grd_data(void *v, int set, float *datablock,
00212                          float *colorblock) {
00213   grd_t *grd = (grd_t *)v;
00214   int ndata = grd->ndata;
00215   FILE *fd = grd->fd;
00216 
00217   /* Skip the header */
00218   fseek(fd, 110, SEEK_SET);
00219 
00220   /* Read the densities. Order for file is x fast, y medium, z slow */
00221   if (fread(datablock, sizeof(float), ndata, fd) != (unsigned int) ndata) {
00222     fprintf(stderr, "grdplugin) Error reading grid data.\n");
00223     return MOLFILE_ERROR;
00224   }
00225 
00226   if (grd->swap) {
00227     swap4_aligned(datablock, ndata);
00228   }
00229 
00230   return MOLFILE_SUCCESS;
00231 }
00232 
00233 static void close_grd_read(void *v) {
00234   grd_t *grd = (grd_t *)v;
00235 
00236   fclose(grd->fd);
00237   if (grd->vol != NULL)
00238     delete [] grd->vol; 
00239   delete grd;
00240 }
00241 
00242 /*
00243  * Initialization stuff here
00244  */
00245 static molfile_plugin_t plugin;
00246 
00247 VMDPLUGIN_API int VMDPLUGIN_init(void) { 
00248   memset(&plugin, 0, sizeof(molfile_plugin_t));
00249   plugin.abiversion = vmdplugin_ABIVERSION;
00250   plugin.type = MOLFILE_PLUGIN_TYPE;
00251   plugin.name = "grd";
00252   plugin.prettyname = "GRASP,Delphi Binary Potential Map";
00253   plugin.author = "Eamon Caddigan";
00254   plugin.majorv = 0;
00255   plugin.minorv = 6;
00256   plugin.is_reentrant = VMDPLUGIN_THREADSAFE;
00257   plugin.filename_extension = "phi,grd";
00258   plugin.open_file_read = open_grd_read;
00259   plugin.read_volumetric_metadata = read_grd_metadata;
00260   plugin.read_volumetric_data = read_grd_data;
00261   plugin.close_file_read = close_grd_read;
00262   return VMDPLUGIN_SUCCESS; 
00263 }
00264 
00265 VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
00266   (*cb)(v, (vmdplugin_t *)&plugin);
00267   return VMDPLUGIN_SUCCESS;
00268 }
00269 
00270 VMDPLUGIN_API int VMDPLUGIN_fini(void) { return VMDPLUGIN_SUCCESS; }
00271 

Generated on Mon Nov 11 03:09:48 2024 for VMD Plugins (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002