Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

parm7plugin.C

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr
00003  *cr            (C) Copyright 1995-2016 The Board of Trustees of the
00004  *cr                        University of Illinois
00005  *cr                         All Rights Reserved
00006  *cr
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  * RCS INFORMATION:
00011  *
00012  *      $RCSfile: parm7plugin.C,v $
00013  *      $Author: johns $       $Locker:  $             $State: Exp $
00014  *      $Revision: 1.35 $       $Date: 2017/08/30 17:42:33 $
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; /* no optional data to start with */
00063 
00064   while (fgets(buf, 85, file)) {
00065     // find the next line starting with %FLAG, indicating a new section
00066     if (strncmp(buf, "%FLAG ", 6)) 
00067       continue;
00068     sscanf(buf+6, "%s\n", field); // type of record
00069 
00070     // skip any number of lines until we get to "FORMAT". This handles
00071     // the %COMMENT lines that may or may not be present
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       // XXX: we could count the bonded parameters and assign bond types.
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   // unused items
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; // parm files don't contain bond order information
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  * Initialization stuff down here
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 }

Generated on Thu Dec 5 03:07:34 2024 for VMD Plugins (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002