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
00279
00280
00281
00282
00283
00284
00285
00286 sprintf(atom->chain, "%c", (char) (((int) 'A') + (mol_num % 26)));
00287
00288 atom++;
00289
00290 fgets(line, LINESIZE, data->file);
00291 if (feof(data->file)) {
00292 fprintf(stderr, "ERROR) unexpected end-of-file while reading structure.\n");
00293 return MOLFILE_ERROR;
00294 }
00295 if (ferror(data->file)) {
00296 fprintf(stderr, "ERROR) error reading car file while reading structure.\n");
00297 return MOLFILE_ERROR;
00298 }
00299 }
00300 fgets(line, LINESIZE, data->file);
00301 mol_num++;
00302 }
00303
00304 return MOLFILE_SUCCESS;
00305 }
00306
00307 static int read_car_timestep(void *mydata, int natoms, molfile_timestep_t *ts) {
00308 char line[LINESIZE];
00309 cardata *data = (cardata *)mydata;
00310 float *coords = NULL;
00311
00312
00313 if (data->eof)
00314 return MOLFILE_EOF;
00315
00316
00317 fseek(data->file, data->coord_location, SEEK_SET);
00318
00319
00320 if (data->pbc == PBC_ON) {
00321 fgets(line, LINESIZE, data->file);
00322
00323 if (ts) {
00324 if ( sscanf(line, "PBC %f %f %f %f %f %f %*s",
00325 &ts->A, &ts->B, &ts->C,
00326 &ts->alpha, &ts->beta, &ts->gamma) != 6 ) {
00327 fprintf(stderr, "ERROR) badly formatted PBC line:\n%s\n", line);
00328 return MOLFILE_ERROR;
00329 }
00330 }
00331 }
00332 else if (data->pbc == PBC_2D) {
00333
00334 fgets(line, LINESIZE, data->file);
00335 }
00336
00337
00338 if (data->helix)
00339 fgets(line, LINESIZE, data->file);
00340
00341 if (ts)
00342 coords = ts->coords;
00343
00344 fgets(line, LINESIZE, data->file);
00345
00346 while(strncmp(line, "end", 3)) {
00347
00348 while(strncmp(line, "end", 3)) {
00349
00350 if (ts) {
00351 if (!read_car_coordinates(coords, line)) {
00352 fprintf(stderr, "ERROR) badly formatted coordinate line:\n%s\n", line);
00353 return MOLFILE_ERROR;
00354 }
00355 coords += 3;
00356 }
00357
00358 fgets(line, LINESIZE, data->file);
00359 if (feof(data->file)) {
00360 fprintf(stderr, "ERROR) unexpected end-of-file while reading coordinates.\n");
00361 return MOLFILE_ERROR;
00362 }
00363 if (ferror(data->file)) {
00364 fprintf(stderr, "ERROR) file error while reading coordinates.\n");
00365 return MOLFILE_ERROR;
00366 }
00367 }
00368 fgets(line, LINESIZE, data->file);
00369 }
00370
00371
00372 data->eof = 1;
00373
00374 return MOLFILE_SUCCESS;
00375 }
00376
00377 static void close_car_read(void *mydata) {
00378 if (mydata) {
00379 cardata *data = (cardata *)mydata;
00380 fclose(data->file);
00381 free(data);
00382 }
00383 }
00384
00385
00386
00387 static molfile_plugin_t plugin;
00388
00389 VMDPLUGIN_API int VMDPLUGIN_init() {
00390 memset(&plugin, 0, sizeof(molfile_plugin_t));
00391 plugin.abiversion = vmdplugin_ABIVERSION;
00392 plugin.type = MOLFILE_PLUGIN_TYPE;
00393 plugin.name = "car";
00394 plugin.prettyname = "InsightII car";
00395 plugin.author = "Eamon Caddigan";
00396 plugin.majorv = 0;
00397 plugin.minorv = 5;
00398 plugin.is_reentrant = VMDPLUGIN_THREADSAFE;
00399 plugin.filename_extension = "car";
00400 plugin.open_file_read = open_car_read;
00401 plugin.read_structure = read_car_structure;
00402 plugin.read_next_timestep = read_car_timestep;
00403 plugin.close_file_read = close_car_read;
00404 return VMDPLUGIN_SUCCESS;
00405 }
00406
00407 VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
00408 (*cb)(v, (vmdplugin_t *)&plugin);
00409 return VMDPLUGIN_SUCCESS;
00410 }
00411
00412 VMDPLUGIN_API int VMDPLUGIN_fini() {
00413 return VMDPLUGIN_SUCCESS;
00414 }
00415