00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <string.h>
00019 #include "molfile_plugin.h"
00020 #include "ReadPARM7.h"
00021
00022 typedef struct {
00023 parmstruct *prm;
00024 int popn;
00025 FILE *fd;
00026 int nbonds;
00027 int *from, *to;
00028 } parmdata;
00029
00030 static void *open_parm7_read(const char *filename, const char *,int *natoms) {
00031 FILE *fd;
00032 int popn = 0;
00033 if(!(fd = open_parm7_file(filename, &popn))) {
00034 fprintf(stderr, "parm7plugin) Cannot open parm file '%s'\n", filename);
00035 return NULL;
00036 }
00037 parmstruct *prm = read_parm7_header(fd);
00038 if (!prm) {
00039 close_parm7_file(fd, popn);
00040 return NULL;
00041 }
00042
00043 *natoms = prm->Natom;
00044 parmdata *p = new parmdata;
00045 memset(p, 0, sizeof(parmdata));
00046 p->prm = prm;
00047 p->popn = popn;
00048 p->fd = fd;
00049 p->from = new int[prm->Nbonh + prm->Nbona];
00050 p->to = new int[prm->Nbonh + prm->Nbona];
00051 return p;
00052 }
00053
00054 static int read_parm7_structure(void *mydata, int *optflags, molfile_atom_t *atoms) {
00055 parmdata *p = (parmdata *)mydata;
00056 const parmstruct *prm = p->prm;
00057 FILE *file = p->fd;
00058 char buf[85];
00059 char field[85];
00060 char *resnames = NULL;
00061
00062 *optflags = MOLFILE_NOOPTIONS;
00063
00064 while (fgets(buf, 85, file)) {
00065
00066 if (strncmp(buf, "%FLAG ", 6))
00067 continue;
00068 sscanf(buf+6, "%s\n", field);
00069
00070
00071
00072 while (strncmp(buf, "%FORMAT", 7)) {
00073 fgets(buf, 85, file);
00074 }
00075
00076 if (!strcmp(field, "ATOM_NAME")) {
00077 if (!parse_parm7_atoms(buf, prm->Natom, atoms, file)) break;
00078 } else if (!strcmp(field, "CHARGE")) {
00079 *optflags |= MOLFILE_CHARGE;
00080 if (!parse_parm7_charge(buf, prm->Natom, atoms, file)) break;
00081 } else if (!strcmp(field, "MASS")) {
00082 *optflags |= MOLFILE_MASS;
00083 if (!parse_parm7_mass(buf, prm->Natom, atoms, file)) break;
00084 } else if (!strcmp(field, "AMBER_ATOM_TYPE")) {
00085 if (!parse_parm7_atype(buf, prm->Natom, atoms, file)) break;
00086 } else if (!strcmp(field, "ATOMIC_NUMBER")) {
00087 *optflags |= MOLFILE_ATOMICNUMBER;
00088 if (!parse_parm7_atomicnumber(buf, prm->Natom, atoms, file)) break;
00089 } else if (!strcmp(field, "RESIDUE_LABEL")) {
00090 resnames = new char[4*prm->Nres];
00091 if (!parse_parm7_resnames(buf, prm->Nres, resnames, file)) break;
00092 } else if (!strcmp(field, "RESIDUE_POINTER")) {
00093 if (!resnames) {
00094 fprintf(stderr,
00095 "parm7plugin) Cannot parse RESIDUE_POINTER before RESIDUE_LABEL\n");
00096 continue;
00097 }
00098 if (!parse_parm7_respointers(buf, prm->Natom, atoms,
00099 prm->Nres, resnames, file))
00100 break;
00101
00102 } else if (!strcmp(field, "BONDS_WITHOUT_HYDROGEN")) {
00103 if (!parse_parm7_bonds(buf, prm->Nbona, p->from+p->nbonds,
00104 p->to+p->nbonds, file)) break;
00105 p->nbonds += prm->Nbona;
00106 } else if (!strcmp(field, "BONDS_INC_HYDROGEN")) {
00107 if (!parse_parm7_bonds(buf, prm->Nbonh, p->from+p->nbonds,
00108 p->to+p->nbonds, file)) break;
00109 p->nbonds += prm->Nbonh;
00110 }
00111 }
00112
00113
00114 for (int i=0; i<prm->Natom; i++) {
00115 atoms[i].chain[0] = '\0';
00116 atoms[i].segid[0] = '\0';
00117 }
00118
00119 delete [] resnames;
00120 return MOLFILE_SUCCESS;
00121 }
00122
00123 static int read_parm7_bonds(void *v, int *nbonds, int **fromptr, int **toptr,
00124 float **bondorderptr, int **bondtype,
00125 int *nbondtypes, char ***bondtypename){
00126 parmdata *p = (parmdata *)v;
00127 *nbonds = p->nbonds;
00128 *fromptr = p->from;
00129 *toptr = p->to;
00130 *bondorderptr = NULL;
00131 *bondtype = NULL;
00132 *nbondtypes = 0;
00133 *bondtypename = NULL;
00134 return MOLFILE_SUCCESS;
00135 }
00136
00137 static void close_parm7_read(void *mydata) {
00138 parmdata *p = (parmdata *)mydata;
00139 close_parm7_file(p->fd, p->popn);
00140 delete p->prm;
00141 delete [] p->from;
00142 delete [] p->to;
00143 delete p;
00144 }
00145
00146
00147
00148
00149
00150 static molfile_plugin_t plugin;
00151
00152 VMDPLUGIN_API int VMDPLUGIN_init(){
00153 memset(&plugin, 0, sizeof(molfile_plugin_t));
00154 plugin.abiversion = vmdplugin_ABIVERSION;
00155 plugin.type = MOLFILE_PLUGIN_TYPE;
00156 plugin.name = "parm7";
00157 plugin.prettyname = "AMBER7 Parm";
00158 plugin.author = "Brian Bennion, Justin Gullingsrud, John Stone";
00159 plugin.majorv = 0;
00160 plugin.minorv = 16;
00161 plugin.is_reentrant = VMDPLUGIN_THREADUNSAFE;
00162 plugin.filename_extension = "prmtop,parm7";
00163 plugin.open_file_read = open_parm7_read;
00164 plugin.read_structure = read_parm7_structure;
00165 plugin.read_bonds = read_parm7_bonds;
00166 plugin.close_file_read = close_parm7_read;
00167 return VMDPLUGIN_SUCCESS;
00168 }
00169
00170 VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
00171 (*cb)(v,(vmdplugin_t *)&plugin);
00172 return VMDPLUGIN_SUCCESS;
00173 }
00174
00175 VMDPLUGIN_API int VMDPLUGIN_fini(){
00176 return VMDPLUGIN_SUCCESS;
00177 }