Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

parm7plugin.C

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr
00003  *cr            (C) Copyright 1995-2006 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.22 $       $Date: 2006/02/23 19:36:45 $
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, "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   *optflags = 0;
00062 
00063   while (fgets(buf, 85, file)) {
00064     // find the next line starting with %FLAG, indicating a new section
00065     if (strncmp(buf, "%FLAG ", 6)) continue;
00066     // parse field and format indicators
00067     sscanf(buf+6, "%s\n", field); // type of record
00068     fscanf(file, "%s\n", buf);    // format
00069     if (!strcmp(field, "ATOM_NAME")) {
00070       if (!parse_parm7_atoms(buf, prm->Natom, atoms, file)) break;
00071     } else if (!strcmp(field, "CHARGE")) {
00072       *optflags |= MOLFILE_CHARGE;
00073       if (!parse_parm7_charge(buf, prm->Natom, atoms, file)) break;
00074     } else if (!strcmp(field, "MASS")) {
00075       *optflags |= MOLFILE_MASS;
00076       if (!parse_parm7_mass(buf, prm->Natom, atoms, file)) break;
00077     } else if (!strcmp(field, "AMBER_ATOM_TYPE")) {
00078       if (!parse_parm7_atype(buf, prm->Natom, atoms, file)) break;
00079     } else if (!strcmp(field, "RESIDUE_LABEL")) {
00080       resnames = new char[4*prm->Nres];
00081       if (!parse_parm7_resnames(buf, prm->Nres, resnames, file)) break;
00082     } else if (!strcmp(field, "RESIDUE_POINTER")) {
00083       if (!resnames) {
00084         fprintf(stderr, 
00085             "PARM7: cannot parse RESIDUE_POINTER before RESIDUE_LABEL\n");
00086         continue;
00087       }
00088       if (!parse_parm7_respointers(buf, prm->Natom, atoms, 
00089                                    prm->Nres, resnames, file)) 
00090         break;
00091     } else if (!strcmp(field, "BONDS_WITHOUT_HYDROGEN")) {
00092       if (!parse_parm7_bonds(buf, prm->Nbona, p->from+p->nbonds,
00093             p->to+p->nbonds, file)) break;
00094       p->nbonds += prm->Nbona;
00095     } else if (!strcmp(field, "BONDS_INC_HYDROGEN")) {
00096       if (!parse_parm7_bonds(buf, prm->Nbonh, p->from+p->nbonds,
00097             p->to+p->nbonds, file)) break;
00098       p->nbonds += prm->Nbonh;
00099     }
00100   }
00101 
00102   // unused items
00103   for (int i=0; i<prm->Natom; i++) {
00104     atoms[i].chain[0] = '\0';
00105     atoms[i].segid[0] = '\0';
00106   }
00107 
00108   delete [] resnames;
00109   return MOLFILE_SUCCESS;
00110 }
00111 
00112 static int read_parm7_bonds(void *v, int *nbonds, int **fromptr, int **toptr, float **bondorderptr){
00113   parmdata *p = (parmdata *)v;
00114   *nbonds = p->nbonds;
00115   *fromptr = p->from;
00116   *toptr = p->to;
00117   *bondorderptr = NULL; // parm files don't contain bond order information
00118   return MOLFILE_SUCCESS;
00119 }
00120 
00121 static void close_parm7_read(void *mydata) {
00122   parmdata *p = (parmdata *)mydata;
00123   close_parm7_file(p->fd, p->popn);
00124   delete p->prm;
00125   delete [] p->from;
00126   delete [] p->to;
00127   delete p;
00128 }
00129  
00130 /*
00131  * Initialization stuff down here
00132  */
00133 
00134 static molfile_plugin_t parm7plugin = {
00135   vmdplugin_ABIVERSION,          // ABI Version
00136   MOLFILE_PLUGIN_TYPE,           // type of plugin
00137   "parm7",                       // short name of plugin
00138   "AMBER7 Parm",                 // pretty name of plugin
00139   "Brian Bennion, Justin Gullingsrud, John E. Stone", // authors
00140   0,                             // major version
00141   7,                             // minor version
00142   VMDPLUGIN_THREADUNSAFE,        // is not reentrant
00143   "prmtop,parm7",                // filename extensions
00144   open_parm7_read,        
00145   read_parm7_structure,  
00146   read_parm7_bonds,
00147   0,                             // read_next_timestep
00148   close_parm7_read,     
00149   0,
00150   0,
00151   0,
00152   0,
00153 };
00154 
00155 VMDPLUGIN_API int VMDPLUGIN_init(){
00156   return VMDPLUGIN_SUCCESS;
00157 }
00158 VMDPLUGIN_API int VMDPLUGIN_fini(){
00159   return VMDPLUGIN_SUCCESS;
00160 }
00161 VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
00162   (*cb)(v,(vmdplugin_t *)&parm7plugin);
00163   return VMDPLUGIN_SUCCESS;
00164 }

Generated on Wed Mar 22 13:15:30 2006 for VMD Plugins (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002