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
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 #include <stdio.h>
00081 #include <stdlib.h>
00082 #include <string.h>
00083 #include <ctype.h>
00084 #include "molfile_plugin.h"
00085
00086 #define LINESIZE 1024
00087
00088
00089 enum {PBC_ON, PBC_OFF, PBC_2D};
00090
00091 typedef struct {
00092 FILE *file;
00093 int numatoms, pbc, helix, eof;
00094 long coord_location;
00095 molfile_atom_t *atomlist;
00096 } cardata;
00097
00098
00099
00100
00101 static int read_car_structure_line(molfile_atom_t *atom, char *line) {
00102 char name[LINESIZE], type[LINESIZE];
00103 int resid;
00104 float charge;
00105
00106 if (sscanf(line, "%s %*f %*f %*f %*s %d %*s %s %f", name, &resid, type, &charge)
00107 != 4)
00108 return 0;
00109
00110
00111 if ( (strlen(name) >= 8) || (strlen(type) >= 8) )
00112 return 0;
00113 strcpy(atom->name, name);
00114 strcpy(atom->type, type);
00115
00116
00117
00118
00119 if (resid > 9999999)
00120 atom->resname[0] = '\0';
00121 else
00122 sprintf(atom->resname, "%d", resid);
00123
00124 atom->resid = resid;
00125
00126 atom->chain[0] = '\0';
00127 atom->segid[0] = '\0';
00128
00129 atom->charge = charge;
00130
00131 return 1;
00132 }
00133
00134
00135
00136
00137 static int read_car_coordinates(float *coords, char *line) {
00138 float x, y, z;
00139
00140 if (sscanf(line, "%*s %f %f %f %*s %*d %*s %*s %*f", &x, &y, &z)
00141 != 3)
00142 return 0;
00143
00144 coords[0] = x;
00145 coords[1] = y;
00146 coords[2] = z;
00147
00148 return 1;
00149 }
00150
00151 static void *open_car_read(const char *filename, const char *filetype,
00152 int *natoms) {
00153 FILE *fd;
00154 cardata *data;
00155 char line[LINESIZE];
00156
00157 fd = fopen(filename, "rb"); if (!fd) return NULL;
00158
00159 data = (cardata *)malloc(sizeof(cardata));
00160 data->eof = 0;
00161 data->file = fd;
00162
00163
00164 fgets(line, LINESIZE, fd);
00165 if (strncmp(line, "!BIOSYM archive", 15)) {
00166 fprintf(stderr, "ERROR) badly formatted/missing header.\n");
00167 return NULL;
00168 }
00169
00170
00171
00172
00173
00174 fgets(line, LINESIZE, fd);
00175 if (!strncmp(line, "HELIX", 5)) {
00176 data->helix = 1;
00177 fgets(line, LINESIZE, fd);
00178 fprintf(stdout, "WARNING) ignoring helix information.\n");
00179 }
00180 else {
00181 data->helix = 0;
00182 }
00183
00184 if (!strncmp(line, "PBC=ON", 6)) {
00185 data->pbc = PBC_ON;
00186 }
00187 else if (!strncmp(line, "PBC=OFF", 7)) {
00188 data->pbc = PBC_OFF;
00189 }
00190 else if (!strncmp(line, "PBC=2D", 6)) {
00191 data->pbc = PBC_2D;
00192 fprintf(stdout, "WARNING) ignoring 2D PBC information.\n");
00193 }
00194 else {
00195 fprintf(stderr, "ERROR) badly formatted/missing PBC info.\n");
00196 return NULL;
00197 }
00198
00199 if (data->helix && (data->pbc == PBC_ON)) {
00200 fprintf(stderr, "ERROR) car file contains helix and 3D PBC information.");
00201 return NULL;
00202 }
00203
00204
00205 fgets(line, LINESIZE, fd);
00206
00207
00208 fgets(line, LINESIZE, fd);
00209 if (strncmp(line, "!DATE", 5)) {
00210 fprintf(stderr, "ERROR) badly formatted/missing date.\n");
00211 return NULL;
00212 }
00213
00214
00215 data->coord_location = ftell(fd);
00216
00217
00218 if (data->pbc != PBC_OFF)
00219 fgets(line, LINESIZE, fd);
00220 if (data->helix)
00221 fgets(line, LINESIZE, fd);
00222
00223
00224 data->numatoms = 0;
00225 fgets(line, LINESIZE, fd);
00226 while(strncmp(line, "end", 3)) {
00227 while(strncmp(line, "end", 3)) {
00228 data->numatoms++;
00229 fgets(line, LINESIZE, fd);
00230
00231 if (feof(fd)) {
00232 fprintf(stderr, "ERROR) unexpected end-of-file.\n");
00233 return NULL;
00234 }
00235 if (ferror(fd)) {
00236 fprintf(stderr, "ERROR) error reading car file.\n");
00237 return NULL;
00238 }
00239 }
00240 fgets(line, LINESIZE, fd);
00241 }
00242 *natoms = data->numatoms;
00243
00244 return data;
00245 }
00246
00247 static int read_car_structure(void *mydata, int *optflags,
00248 molfile_atom_t *atoms) {
00249 int mol_num;
00250 char line[LINESIZE];
00251 molfile_atom_t *atom;
00252 cardata *data = (cardata *)mydata;
00253
00254 *optflags = MOLFILE_CHARGE;
00255
00256
00257
00258
00259 fseek(data->file, data->coord_location, SEEK_SET);
00260 if (data->pbc != PBC_OFF)
00261 fgets(line, LINESIZE, data->file);
00262 if (data->helix)
00263 fgets(line, LINESIZE, data->file);
00264
00265 mol_num = 0;
00266 atom = atoms;
00267 fgets(line, LINESIZE, data->file);
00268
00269 while(strncmp(line, "end", 3)) {
00270
00271 while(strncmp(line, "end", 3)) {
00272 if (!read_car_structure_line(atom, line)) {
00273 fprintf(stderr, "ERROR) badly formatted structure line:\n%s\n", line);
00274 return MOLFILE_ERROR;
00275 }
00276
00277
00278 sprintf(atom->chain, "%d", mol_num);
00279
00280 atom++;
00281
00282 fgets(line, LINESIZE, data->file);
00283 if (feof(data->file)) {
00284 fprintf(stderr, "ERROR) unexpected end-of-file while reading structure.\n");
00285 return MOLFILE_ERROR;
00286 }
00287 if (ferror(data->file)) {
00288 fprintf(stderr, "ERROR) error reading car file while reading structure.\n");
00289 return MOLFILE_ERROR;
00290 }
00291 }
00292 fgets(line, LINESIZE, data->file);
00293 mol_num++;
00294 }
00295
00296 return MOLFILE_SUCCESS;
00297 }
00298
00299 static int read_car_timestep(void *mydata, int natoms, molfile_timestep_t *ts) {
00300 char line[LINESIZE];
00301 cardata *data = (cardata *)mydata;
00302 float *coords = NULL;
00303
00304
00305 if (data->eof)
00306 return MOLFILE_EOF;
00307
00308
00309 fseek(data->file, data->coord_location, SEEK_SET);
00310
00311
00312 if (data->pbc == PBC_ON) {
00313 fgets(line, LINESIZE, data->file);
00314
00315 if (ts) {
00316 if ( sscanf(line, "PBC %f %f %f %f %f %f %*s",
00317 &ts->A, &ts->B, &ts->C,
00318 &ts->alpha, &ts->beta, &ts->gamma) != 6 ) {
00319 fprintf(stderr, "ERROR) badly formatted PBC line:\n%s\n", line);
00320 return MOLFILE_ERROR;
00321 }
00322 }
00323 }
00324 else if (data->pbc == PBC_2D) {
00325
00326 fgets(line, LINESIZE, data->file);
00327 }
00328
00329
00330 if (data->helix)
00331 fgets(line, LINESIZE, data->file);
00332
00333 if (ts)
00334 coords = ts->coords;
00335
00336 fgets(line, LINESIZE, data->file);
00337
00338 while(strncmp(line, "end", 3)) {
00339
00340 while(strncmp(line, "end", 3)) {
00341
00342 if (ts) {
00343 if (!read_car_coordinates(coords, line)) {
00344 fprintf(stderr, "ERROR) badly formatted coordinate line:\n%s\n", line);
00345 return MOLFILE_ERROR;
00346 }
00347 coords += 3;
00348 }
00349
00350 fgets(line, LINESIZE, data->file);
00351 if (feof(data->file)) {
00352 fprintf(stderr, "ERROR) unexpected end-of-file while reading coordinates.\n");
00353 return MOLFILE_ERROR;
00354 }
00355 if (ferror(data->file)) {
00356 fprintf(stderr, "ERROR) file error while reading coordinates.\n");
00357 return MOLFILE_ERROR;
00358 }
00359 }
00360 fgets(line, LINESIZE, data->file);
00361 }
00362
00363
00364 data->eof = 1;
00365
00366 return MOLFILE_SUCCESS;
00367 }
00368
00369 static void close_car_read(void *mydata) {
00370 if (mydata) {
00371 cardata *data = (cardata *)mydata;
00372 fclose(data->file);
00373 free(data);
00374 }
00375 }
00376
00377
00378
00379 static molfile_plugin_t carplugin = {
00380 vmdplugin_ABIVERSION,
00381 MOLFILE_PLUGIN_TYPE,
00382 "car",
00383 "InsightII car",
00384 "Eamon Caddigan",
00385 0,
00386 3,
00387 VMDPLUGIN_THREADSAFE,
00388 "car",
00389 open_car_read,
00390 read_car_structure,
00391 0,
00392 read_car_timestep,
00393 close_car_read,
00394 };
00395
00396 VMDPLUGIN_API int VMDPLUGIN_init() {
00397 return VMDPLUGIN_SUCCESS;
00398 }
00399
00400 VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
00401 (*cb)(v, (vmdplugin_t *)&carplugin);
00402 return VMDPLUGIN_SUCCESS;
00403 }
00404
00405 VMDPLUGIN_API int VMDPLUGIN_fini() {
00406 return VMDPLUGIN_SUCCESS;
00407 }
00408