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 #include "molfile_plugin.h"
00026
00027 #include <stdio.h>
00028 #include <string.h>
00029 #include <stdlib.h>
00030
00031 #define COR_RECORD_LENGTH 141
00032
00033
00034 static void strip_whitespace(char *str, int n) {
00035 char *beg, *end;
00036
00037 beg = str;
00038 end = str + (n-2);
00039
00040
00041 while(beg <= end && *beg == ' ') {
00042 beg++;
00043 }
00044
00045
00046 while(end >= str && *end == ' ') {
00047 end--;
00048 }
00049
00050 if (beg < end) {
00051
00052 *(end+1) = '\0';
00053 memmove(str, beg, (end - beg + 2));
00054 } else {
00055 str[0] = '\0';
00056 }
00057
00058 return;
00059 }
00060
00061
00062 static char *corgets(char *s, int n, FILE *stream) {
00063 char *returnVal;
00064
00065 if (feof(stream)) {
00066 printf("corplugin) Unexpected end-of-file.\n");
00067 returnVal = NULL;
00068 } else if (ferror(stream)) {
00069 printf("corplugin) Error reading file.\n");
00070 return NULL;
00071 } else {
00072 returnVal = fgets(s, n, stream);
00073 if (returnVal == NULL) {
00074 printf("corplugin) Error reading line.\n");
00075 }
00076 }
00077
00078 return returnVal;
00079 }
00080
00081
00082
00083
00084
00085
00086 static FILE *open_cor_file(const char *fname, int *natom, int *iofoext) {
00087 char inbuf[COR_RECORD_LENGTH+2], header[11];
00088 FILE *f;
00089
00090 *natom = MOLFILE_NUMATOMS_NONE;
00091
00092 if (!fname) {
00093 printf("corplugin) Error opening file: no filename given.\n");
00094 return NULL;
00095 }
00096
00097 if ((f = fopen(fname, "r")) == NULL) {
00098 printf("corplugin) Error opening file.\n");
00099 return NULL;
00100 }
00101
00102
00103 do {
00104 if (fgets(inbuf, COR_RECORD_LENGTH+1, f) == NULL) {
00105 fclose(f);
00106 printf("corplugin) Error opening file: cannot read line.\n");
00107 return NULL;
00108 }
00109
00110 if (sscanf(inbuf, "%10c", header) != 1) {
00111 fclose(f);
00112 printf("corplugin) Error opening file: improperly formatted line.\n");
00113 return NULL;
00114 }
00115
00116 } while (header[0]=='*');
00117
00118
00119 *iofoext = 0 ;
00120 if (strstr(inbuf,"EXT") != NULL)
00121 *iofoext = 1;
00122
00123
00124 header[10] = '\0';
00125 *natom = atoi(header);
00126 if (*natom > 99999)
00127 *iofoext = 1;
00128
00129 if (*iofoext == 1)
00130 printf("corplugin) Using EXTended CHARMM coordinates file\n");
00131
00132 return f;
00133 }
00134
00135
00136
00137
00138 static int get_cor_atom(FILE *f, char *atomName, char *atomType, char
00139 *resName, char *segName, int *resId, int ioext) {
00140 char inbuf[COR_RECORD_LENGTH+2], numAtomStr[11], resNStr[11], resIdStr[11];
00141 char atomNameStr[11], segNameStr[11], resNameStr[11];
00142 int numAtom;
00143
00144 if (corgets(inbuf, COR_RECORD_LENGTH+1, f) == NULL) {
00145 return -1;
00146 }
00147
00148 if (strlen(inbuf) < 60) {
00149 printf("corplugin) Line too short: \n%s\n", inbuf);
00150 return -1;
00151 }
00152
00153 memset(numAtomStr, 0, sizeof(numAtomStr));
00154 memset(resNStr, 0, sizeof(resNStr));
00155 memset(resIdStr, 0, sizeof(resIdStr));
00156 memset(resNameStr, 0, sizeof(resNameStr));
00157 memset(segNameStr, 0, sizeof(segNameStr));
00158 memset(atomNameStr, 0, sizeof(atomNameStr));
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189 if (ioext == 1 ) {
00190 if (sscanf(inbuf, "%10c%10c%10c%10c%*20c%*20c%*20c%10c%10c",
00191 numAtomStr, resNStr, resNameStr, atomNameStr, segNameStr, resIdStr) != 6) {
00192 printf("corplugin) Improperly formatted line: \n%s\n", inbuf);
00193 return -1;
00194 }
00195 strip_whitespace(resName, sizeof(resName));
00196 strip_whitespace(atomName, sizeof(atomName));
00197 strip_whitespace(segName, sizeof(segName));
00198 memcpy(resName, resNameStr, 7);
00199 memcpy(atomName, atomNameStr, 7);
00200 memcpy(segName, segNameStr, 7);
00201 resName[7] = '\0';
00202 atomName[7] = '\0';
00203 segName[7] = '\0';
00204 } else {
00205 if (sscanf(inbuf, "%5c%5c%5c%5c%*10c%*10c%*10c%5c%5c",
00206 numAtomStr, resNStr, resName, atomName, segName, resIdStr) != 6) {
00207 printf("corplugin) Improperly formatted line: \n%s\n", inbuf);
00208 return -1;
00209 }
00210 strip_whitespace(resName, 8);
00211 strip_whitespace(atomName, 8);
00212 strip_whitespace(segName, 8);
00213 }
00214
00215 numAtom = atoi(numAtomStr);
00216 *resId = atoi(resIdStr);
00217 strcpy(atomType, atomName);
00218
00219 return numAtom;
00220 }
00221
00222
00223
00224
00225
00226
00227 typedef struct {
00228 FILE *file;
00229 int numatoms;
00230 int iofoext;
00231 } cordata;
00232
00233 static void *open_cor_read(const char *path, const char *filetype,
00234 int *natoms) {
00235 int ioext ;
00236 FILE *fd;
00237 cordata *cor;
00238
00239 if (!(fd = open_cor_file(path, natoms, &ioext))) {
00240 return NULL;
00241 }
00242 cor = (cordata *) malloc(sizeof(cordata));
00243 memset(cor, 0, sizeof(cordata));
00244 cor->numatoms = *natoms;
00245 cor->file = fd;
00246 cor->iofoext = ioext ;
00247 return cor;
00248 }
00249
00250 static int read_cor_structure(void *v, int *optflags, molfile_atom_t *atoms) {
00251 cordata *data = (cordata *)v;
00252 int i;
00253
00254
00255 *optflags = MOLFILE_NOOPTIONS;
00256
00257 for (i=0; i<data->numatoms; i++) {
00258 molfile_atom_t *atom = atoms+i;
00259 if (get_cor_atom(data->file, atom->name, atom->type,
00260 atom->resname, atom->segid,
00261 &atom->resid, data->iofoext) < 0) {
00262 printf("corplugin) couldn't read atom %d\n", i);
00263 return MOLFILE_ERROR;
00264 }
00265 atom->chain[0] = atom->segid[0];
00266 atom->chain[1] = '\0';
00267 }
00268
00269 rewind(data->file);
00270 return MOLFILE_SUCCESS;
00271 }
00272
00273 static int read_cor_timestep(void *v, int natoms, molfile_timestep_t *ts) {
00274 cordata *cor = (cordata *)v;
00275 char inbuf[COR_RECORD_LENGTH+2], header[6];
00276 char xStr[21], yStr[21], zStr[21];
00277 int i;
00278
00279 xStr[20] = '\0';
00280 yStr[20] = '\0';
00281 zStr[20] = '\0';
00282
00283
00284 do {
00285
00286 if (feof(cor->file) || ferror(cor->file) ||
00287 (fgets(inbuf, COR_RECORD_LENGTH+1, cor->file) == NULL)) {
00288 return MOLFILE_ERROR;
00289 }
00290
00291 if (sscanf(inbuf, " %5c", header) != 1) {
00292 printf("corplugin) Improperly formatted line.\n");
00293 return MOLFILE_ERROR;
00294 }
00295
00296 } while (header[0]=='*');
00297
00298
00299
00300 for (i = 0; i < natoms; i++) {
00301 if (corgets(inbuf, COR_RECORD_LENGTH+1, cor->file) == NULL) {
00302 return MOLFILE_ERROR;
00303 }
00304
00305 if (cor->iofoext == 1 ) {
00306 if (sscanf(inbuf, "%*10c%*10c%*10c%*10c%20c%20c%20c%*10c",
00307 xStr, yStr, zStr) != 3) {
00308 printf("corplugin) Error reading coordinates on line %d.\n%s\n", i, inbuf);
00309 return MOLFILE_ERROR;
00310 } else if (ts != NULL) {
00311
00312 ts->coords[3*i ] = atof(xStr);
00313 ts->coords[3*i+1] = atof(yStr);
00314 ts->coords[3*i+2] = atof(zStr);
00315 }
00316 } else {
00317 if (sscanf(inbuf, "%*5c%*5c%*5c%*5c%10c%10c%10c%*5c",
00318 xStr, yStr, zStr) != 3) {
00319 printf("corplugin) Error reading coordinates on line %d.\n%s\n", i, inbuf);
00320 return MOLFILE_ERROR;
00321 } else if (ts != NULL) {
00322
00323 ts->coords[3*i ] = atof(xStr);
00324 ts->coords[3*i+1] = atof(yStr);
00325 ts->coords[3*i+2] = atof(zStr);
00326 }
00327 }
00328 }
00329
00330 return MOLFILE_SUCCESS;
00331 }
00332
00333 static void close_cor_read(void *mydata) {
00334 cordata *data = (cordata *)mydata;
00335 if (data) {
00336 if (data->file) fclose(data->file);
00337 free(data);
00338 }
00339 }
00340
00341
00342
00343
00344
00345 static molfile_plugin_t plugin = {
00346 vmdplugin_ABIVERSION,
00347 MOLFILE_PLUGIN_TYPE,
00348 "cor",
00349 "CHARMM Coordinates",
00350 "Eamon Caddigan, John Stone",
00351 0,
00352 7,
00353 VMDPLUGIN_THREADSAFE,
00354 "cor",
00355 open_cor_read,
00356 read_cor_structure,
00357 0,
00358 read_cor_timestep,
00359 close_cor_read
00360 };
00361
00362 VMDPLUGIN_API int VMDPLUGIN_init() {
00363 return VMDPLUGIN_SUCCESS;
00364 }
00365
00366 VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
00367 (*cb)(v, (vmdplugin_t *)&plugin);
00368 return VMDPLUGIN_SUCCESS;
00369 }
00370
00371 VMDPLUGIN_API int VMDPLUGIN_fini() {
00372 return VMDPLUGIN_SUCCESS;
00373 }