1 /* MACHINE GENERATED FILE, DO NOT EDIT! */
2 
3 #define VMDPLUGIN molfile_parm7plugin
4 #define STATIC_PLUGIN 1
5 
6 /***************************************************************************
7  *cr
8  *cr            (C) Copyright 1995-2016 The Board of Trustees of the
9  *cr                        University of Illinois
10  *cr                         All Rights Reserved
11  *cr
12  ***************************************************************************/
13 
14 /***************************************************************************
15  * RCS INFORMATION:
16  *
17  *      $RCSfile: parm7plugin.C,v $
18  *      $Author: johns $       $Locker:  $             $State: Exp $
19  *      $Revision: 1.34 $       $Date: 2016/11/28 05:01:54 $
20  *
21  ***************************************************************************/
22 
23 #include <string.h>
24 #include "molfile_plugin.h"
25 #include "ReadPARM7.h"
26 
27 typedef struct {
28   parmstruct *prm;
29   int popn;
30   FILE *fd;
31   int nbonds;
32   int *from, *to;
33 } parmdata;
34 
open_parm7_read(const char * filename,const char *,int * natoms)35 static void *open_parm7_read(const char *filename, const char *,int *natoms) {
36   FILE *fd;
37   int popn = 0;
38   if(!(fd = open_parm7_file(filename, &popn))) {
39     fprintf(stderr, "parm7plugin) Cannot open parm file '%s'\n", filename);
40     return NULL;
41   }
42   parmstruct *prm = read_parm7_header(fd);
43   if (!prm) {
44     close_parm7_file(fd, popn);
45     return NULL;
46   }
47 
48   *natoms = prm->Natom;
49   parmdata *p = new parmdata;
50   memset(p, 0, sizeof(parmdata));
51   p->prm = prm;
52   p->popn = popn;
53   p->fd = fd;
54   p->from = new int[prm->Nbonh + prm->Nbona];
55   p->to   = new int[prm->Nbonh + prm->Nbona];
56   return p;
57 }
58 
read_parm7_structure(void * mydata,int * optflags,molfile_atom_t * atoms)59 static int read_parm7_structure(void *mydata, int *optflags, molfile_atom_t *atoms) {
60   parmdata *p = (parmdata *)mydata;
61   const parmstruct *prm = p->prm;
62   FILE *file = p->fd;
63   char buf[85];
64   char field[85];
65   char *resnames = NULL;
66 
67   *optflags = MOLFILE_NOOPTIONS; /* no optional data to start with */
68 
69   while (fgets(buf, 85, file)) {
70     // find the next line starting with %FLAG, indicating a new section
71     if (strncmp(buf, "%FLAG ", 6))
72       continue;
73     sscanf(buf+6, "%s\n", field); // type of record
74 
75     // skip any number of lines until we get to "FORMAT". This handles
76     // the %COMMENT lines that may or may not be present
77     while (strncmp(buf, "%FORMAT", 7)) {
78         fgets(buf, 85, file);
79     }
80 
81     if (!strcmp(field, "ATOM_NAME")) {
82       if (!parse_parm7_atoms(buf, prm->Natom, atoms, file)) break;
83     } else if (!strcmp(field, "CHARGE")) {
84       *optflags |= MOLFILE_CHARGE;
85       if (!parse_parm7_charge(buf, prm->Natom, atoms, file)) break;
86     } else if (!strcmp(field, "MASS")) {
87       *optflags |= MOLFILE_MASS;
88       if (!parse_parm7_mass(buf, prm->Natom, atoms, file)) break;
89     } else if (!strcmp(field, "AMBER_ATOM_TYPE")) {
90       if (!parse_parm7_atype(buf, prm->Natom, atoms, file)) break;
91     } else if (!strcmp(field, "RESIDUE_LABEL")) {
92       resnames = new char[4*prm->Nres];
93       if (!parse_parm7_resnames(buf, prm->Nres, resnames, file)) break;
94     } else if (!strcmp(field, "RESIDUE_POINTER")) {
95       if (!resnames) {
96         fprintf(stderr,
97             "parm7plugin) Cannot parse RESIDUE_POINTER before RESIDUE_LABEL\n");
98         continue;
99       }
100       if (!parse_parm7_respointers(buf, prm->Natom, atoms,
101                                    prm->Nres, resnames, file))
102         break;
103       // XXX: we could count the bonded parameters and assign bond types.
104     } else if (!strcmp(field, "BONDS_WITHOUT_HYDROGEN")) {
105       if (!parse_parm7_bonds(buf, prm->Nbona, p->from+p->nbonds,
106             p->to+p->nbonds, file)) break;
107       p->nbonds += prm->Nbona;
108     } else if (!strcmp(field, "BONDS_INC_HYDROGEN")) {
109       if (!parse_parm7_bonds(buf, prm->Nbonh, p->from+p->nbonds,
110             p->to+p->nbonds, file)) break;
111       p->nbonds += prm->Nbonh;
112     }
113   }
114 
115   // unused items
116   for (int i=0; i<prm->Natom; i++) {
117     atoms[i].chain[0] = '\0';
118     atoms[i].segid[0] = '\0';
119   }
120 
121   delete [] resnames;
122   return MOLFILE_SUCCESS;
123 }
124 
read_parm7_bonds(void * v,int * nbonds,int ** fromptr,int ** toptr,float ** bondorderptr,int ** bondtype,int * nbondtypes,char *** bondtypename)125 static int read_parm7_bonds(void *v, int *nbonds, int **fromptr, int **toptr,
126                             float **bondorderptr, int **bondtype,
127                             int *nbondtypes, char ***bondtypename){
128   parmdata *p = (parmdata *)v;
129   *nbonds = p->nbonds;
130   *fromptr = p->from;
131   *toptr = p->to;
132   *bondorderptr = NULL; // parm files don't contain bond order information
133   *bondtype = NULL;
134   *nbondtypes = 0;
135   *bondtypename = NULL;
136   return MOLFILE_SUCCESS;
137 }
138 
close_parm7_read(void * mydata)139 static void close_parm7_read(void *mydata) {
140   parmdata *p = (parmdata *)mydata;
141   close_parm7_file(p->fd, p->popn);
142   delete p->prm;
143   delete [] p->from;
144   delete [] p->to;
145   delete p;
146 }
147 
148 /*
149  * Initialization stuff down here
150  */
151 
152 static molfile_plugin_t plugin;
153 
VMDPLUGIN_init()154 VMDPLUGIN_API int VMDPLUGIN_init(){
155   memset(&plugin, 0, sizeof(molfile_plugin_t));
156   plugin.abiversion = vmdplugin_ABIVERSION;
157   plugin.type = MOLFILE_PLUGIN_TYPE;
158   plugin.name = "parm7";
159   plugin.prettyname = "AMBER7 Parm";
160   plugin.author = "Brian Bennion, Justin Gullingsrud, John Stone";
161   plugin.majorv = 0;
162   plugin.minorv = 15;
163   plugin.is_reentrant = VMDPLUGIN_THREADUNSAFE;
164   plugin.filename_extension = "prmtop,parm7";
165   plugin.open_file_read = open_parm7_read;
166   plugin.read_structure = read_parm7_structure;
167   plugin.read_bonds = read_parm7_bonds;
168   plugin.close_file_read = close_parm7_read;
169   return VMDPLUGIN_SUCCESS;
170 }
171 
VMDPLUGIN_register(void * v,vmdplugin_register_cb cb)172 VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
173   (*cb)(v,(vmdplugin_t *)&plugin);
174   return VMDPLUGIN_SUCCESS;
175 }
176 
VMDPLUGIN_fini()177 VMDPLUGIN_API int VMDPLUGIN_fini(){
178   return VMDPLUGIN_SUCCESS;
179 }
180