Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

tinkerplugin.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr
00003  *cr            (C) Copyright 1995-2006 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: tinkerplugin.c,v $
00013  *      $Author: johns $       $Locker:  $             $State: Exp $
00014  *      $Revision: 1.10 $       $Date: 2006/02/23 19:36:45 $
00015  *
00016  ***************************************************************************/
00017 
00018  /*
00019   * The .arc file is the one coming straight out of tinker.
00020   * Many frames, looks like a regular tinker, except that in the last columns
00021   * after x, y and z it has :
00022   * 
00023   * atom type (not important for viz)
00024   * 
00025   * and then info in a Z-matrix form
00026   * 
00027   * atom to which you are bonded, atom to which you are 'angled' and atom to
00028   * which you are 'torsioned'.
00029   * 
00030   */
00031 
00032 #include "largefiles.h"   /* platform dependent 64-bit file I/O defines */
00033 
00034 #include <stdio.h>
00035 #include <stdlib.h>
00036 #include <string.h>
00037 #include <ctype.h>
00038 #include "molfile_plugin.h"
00039 
00040 typedef struct {
00041   FILE *file;
00042   int numatoms;
00043   char *file_name;
00044   molfile_atom_t *atomlist;
00045 } tinkerdata;
00046  
00047 static void *open_tinker_read(const char *filename, const char *filetype, 
00048                            int *natoms) {
00049   FILE *fd;
00050   tinkerdata *data;
00051   int i;
00052 
00053   fd = fopen(filename, "rb");
00054   if (!fd) return NULL;
00055   
00056   data = (tinkerdata *)malloc(sizeof(tinkerdata));
00057   data->file = fd;
00058   data->file_name = strdup(filename);
00059 
00060   /* First line is the number of atoms, followed by other stuff */
00061   i = fscanf(data->file, "%d", natoms);
00062   if (i < 1) {
00063     fprintf(stderr, "\n\nread) ERROR: tinker file '%s' should have the number of atoms in the first line.\n", filename);
00064     return NULL;
00065   }
00066   data->numatoms=*natoms;
00067 
00068   while (getc(fd) != '\n'); /* skip rest of this line */
00069   
00070   return data;
00071 }
00072 
00073 static int read_tinker_structure(void *mydata, int *optflags, 
00074                               molfile_atom_t *atoms) {
00075   int i, j, atomid;
00076   char *k;
00077   float coord;
00078   molfile_atom_t *atom;
00079   tinkerdata *data = (tinkerdata *)mydata;
00080 
00081   for (i=0; i<data->numatoms; i++) {
00082     char buffer[1024], fbuffer[1024];
00083     k = fgets(fbuffer, 1024, data->file);
00084     atom = atoms + i;
00085     j=sscanf(fbuffer, "%d %s %f %f %f", &atomid, buffer, &coord, &coord, &coord);
00086     if (k == NULL) {
00087       fprintf(stderr, "tinker structure) missing atom(s) in file '%s'\n", data->file_name);
00088       fprintf(stderr, "tinker structure) expecting '%d' atoms, found only '%d'\n", data->numatoms, i+1);
00089       return MOLFILE_ERROR;
00090     } else if (j < 5) {
00091       fprintf(stderr, "tinker structure) missing type or coordinate(s) in file '%s' for atom '%d'\n", data->file_name, i+1);
00092       return MOLFILE_ERROR;
00093     }
00094 
00095     strncpy(atom->name, buffer, sizeof(atom->name));
00096     strncpy(atom->type, atom->name, sizeof(atom->type));
00097     atom->resname[0] = '\0';
00098     atom->resid = 1;
00099     atom->chain[0] = '\0';
00100     atom->segid[0] = '\0';
00101   }
00102 
00103   rewind(data->file);
00104   return MOLFILE_SUCCESS;
00105 }
00106 
00107 static int read_tinker_timestep(void *mydata, int natoms, molfile_timestep_t *ts) {
00108   int i, j, atomid;
00109   char atom_name[1024], fbuffer[1024], *k;
00110   float x, y, z;
00111   
00112   tinkerdata *data = (tinkerdata *)mydata;
00113   
00114   /* skip over the first line */
00115   if (NULL == fgets(fbuffer, 1024, data->file))  return MOLFILE_ERROR;
00116 
00117   /* read the coordinates */
00118   for (i=0; i<natoms; i++) {
00119     k = fgets(fbuffer, 1024, data->file);
00120 
00121     /* Read in atom type, X, Y, Z, skipping any remaining data fields */
00122     j = sscanf(fbuffer, "%d %s %f %f %f", &atomid, atom_name, &x, &y, &z);
00123     if (k == NULL) {
00124       return MOLFILE_ERROR;
00125     } else if (j < 5) {
00126       fprintf(stderr, "tinker timestep) missing type or coordinate(s) in file '%s' for atom '%d'\n",data->file_name,i+1);
00127       return MOLFILE_ERROR;
00128     } else if (j >= 5) {
00129       if (ts != NULL) { 
00130         /* only save coords if we're given a timestep pointer, */
00131         /* otherwise assume that VMD wants us to skip past it. */
00132         ts->coords[3*i  ] = x;
00133         ts->coords[3*i+1] = y;
00134         ts->coords[3*i+2] = z;
00135       }
00136     } else {
00137       break;
00138     }
00139   }
00140   
00141   return MOLFILE_SUCCESS;
00142 }
00143     
00144 static void close_tinker_read(void *mydata) {
00145   tinkerdata *data = (tinkerdata *)mydata;
00146   fclose(data->file);
00147   free(data->file_name);
00148   free(data);
00149 }
00150 
00151 /* registration stuff */
00152 static molfile_plugin_t tinkerplugin = {
00153   vmdplugin_ABIVERSION,
00154   MOLFILE_PLUGIN_TYPE,                         /* type */
00155   "tinker",                                    /* name */
00156   "Tinker",                                    /* name */
00157   "John E. Stone",                             /* author */
00158   0,                                           /* major version */
00159   3,                                           /* minor version */
00160   VMDPLUGIN_THREADSAFE,                        /* is reentrant */
00161   "arc",
00162   open_tinker_read,
00163   read_tinker_structure,
00164   0,
00165   read_tinker_timestep,
00166   close_tinker_read,
00167   0,
00168   0,
00169   0,
00170   0,
00171   0,                            /* read_volumetric_metadata */
00172   0,                            /* read_volumetric_data */
00173   0                             /* read_rawgraphics */
00174 };
00175 
00176 VMDPLUGIN_API int VMDPLUGIN_init() {
00177   return VMDPLUGIN_SUCCESS;
00178 }
00179 
00180 VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
00181   (*cb)(v, (vmdplugin_t *)&tinkerplugin);
00182   return VMDPLUGIN_SUCCESS;
00183 }
00184 
00185 VMDPLUGIN_API int VMDPLUGIN_fini() {
00186   return VMDPLUGIN_SUCCESS;
00187 }
00188 
00189 
00190 #ifdef TEST_PLUGIN
00191 
00192 int main(int argc, char *argv[]) {
00193   molfile_timestep_t timestep;
00194   void *v;
00195   int natoms;
00196   int i, nsets, set;
00197 
00198   while (--argc) {
00199     ++argv;
00200     v = open_tinker_read(*argv, "tinker", &natoms);
00201     if (!v) {
00202       fprintf(stderr, "open_tinker_read failed for file %s\n", *argv);
00203       return 1;
00204     }
00205     fprintf(stderr, "open_tinker_read succeeded for file %s\n", *argv);
00206     fprintf(stderr, "number of atoms: %d\n", natoms);
00207 
00208     i = 0;
00209     timestep.coords = (float *)malloc(3*sizeof(float)*natoms);
00210     while (!read_tinker_timestep(v, natoms, &timestep)) {
00211       i++;
00212     }
00213     fprintf(stderr, "ended read_next_timestep on frame %d\n", i);
00214 
00215     close_tinker_read(v);
00216   }
00217   return 0;
00218 }
00219 
00220 #endif
00221 

Generated on Wed Mar 22 13:15:31 2006 for VMD Plugins (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002