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