00001
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 #include "largefiles.h"
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
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');
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
00115 if (NULL == fgets(fbuffer, 1024, data->file)) return MOLFILE_ERROR;
00116
00117
00118 for (i=0; i<natoms; i++) {
00119 k = fgets(fbuffer, 1024, data->file);
00120
00121
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
00131
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
00152 static molfile_plugin_t tinkerplugin = {
00153 vmdplugin_ABIVERSION,
00154 MOLFILE_PLUGIN_TYPE,
00155 "tinker",
00156 "Tinker",
00157 "John E. Stone",
00158 0,
00159 3,
00160 VMDPLUGIN_THREADSAFE,
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,
00172 0,
00173 0
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, ×tep)) {
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