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.26 $       $Date: 2008/01/09 20:37:47 $
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)) continue;
00067     // parse field and format indicators
00068     sscanf(buf+6, "%s\n", field); // type of record
00069     fscanf(file, "%s\n", buf);    // format
00070     if (!strcmp(field, "ATOM_NAME")) {
00071       if (!parse_parm7_atoms(buf, prm->Natom, atoms, file)) break;
00072     } else if (!strcmp(field, "CHARGE")) {
00073       *optflags |= MOLFILE_CHARGE;
00074       if (!parse_parm7_charge(buf, prm->Natom, atoms, file)) break;
00075     } else if (!strcmp(field, "MASS")) {
00076       *optflags |= MOLFILE_MASS;
00077       if (!parse_parm7_mass(buf, prm->Natom, atoms, file)) break;
00078     } else if (!strcmp(field, "AMBER_ATOM_TYPE")) {
00079       if (!parse_parm7_atype(buf, prm->Natom, atoms, file)) break;
00080     } else if (!strcmp(field, "RESIDUE_LABEL")) {
00081       resnames = new char[4*prm->Nres];
00082       if (!parse_parm7_resnames(buf, prm->Nres, resnames, file)) break;
00083     } else if (!strcmp(field, "RESIDUE_POINTER")) {
00084       if (!resnames) {
00085         fprintf(stderr, 
00086             "parm7plugin) Cannot parse RESIDUE_POINTER before RESIDUE_LABEL\n");
00087         continue;
00088       }
00089       if (!parse_parm7_respointers(buf, prm->Natom, atoms, 
00090                                    prm->Nres, resnames, file)) 
00091         break;
00092     } else if (!strcmp(field, "BONDS_WITHOUT_HYDROGEN")) {
00093       if (!parse_parm7_bonds(buf, prm->Nbona, p->from+p->nbonds,
00094             p->to+p->nbonds, file)) break;
00095       p->nbonds += prm->Nbona;
00096     } else if (!strcmp(field, "BONDS_INC_HYDROGEN")) {
00097       if (!parse_parm7_bonds(buf, prm->Nbonh, p->from+p->nbonds,
00098             p->to+p->nbonds, file)) break;
00099       p->nbonds += prm->Nbonh;
00100     }
00101   }
00102 
00103   // unused items
00104   for (int i=0; i<prm->Natom; i++) {
00105     atoms[i].chain[0] = '\0';
00106     atoms[i].segid[0] = '\0';
00107   }
00108 
00109   delete [] resnames;
00110   return MOLFILE_SUCCESS;
00111 }
00112 
00113 static int read_parm7_bonds(void *v, int *nbonds, int **fromptr, int **toptr, float **bondorderptr){
00114   parmdata *p = (parmdata *)v;
00115   *nbonds = p->nbonds;
00116   *fromptr = p->from;
00117   *toptr = p->to;
00118   *bondorderptr = NULL; // parm files don't contain bond order information
00119   return MOLFILE_SUCCESS;
00120 }
00121 
00122 static void close_parm7_read(void *mydata) {
00123   parmdata *p = (parmdata *)mydata;
00124   close_parm7_file(p->fd, p->popn);
00125   delete p->prm;
00126   delete [] p->from;
00127   delete [] p->to;
00128   delete p;
00129 }
00130  
00131 /*
00132  * Initialization stuff down here
00133  */
00134 
00135 static molfile_plugin_t plugin;
00136 
00137 VMDPLUGIN_API int VMDPLUGIN_init(){
00138   memset(&plugin, 0, sizeof(molfile_plugin_t));
00139   plugin.abiversion = vmdplugin_ABIVERSION;
00140   plugin.type = MOLFILE_PLUGIN_TYPE;
00141   plugin.name = "parm7";
00142   plugin.prettyname = "AMBER7 Parm";
00143   plugin.author = "Brian Bennion, Justin Gullingsrud, John Stone";
00144   plugin.majorv = 0;
00145   plugin.minorv = 9;
00146   plugin.is_reentrant = VMDPLUGIN_THREADUNSAFE;
00147   plugin.filename_extension = "prmtop,parm7";
00148   plugin.open_file_read = open_parm7_read;
00149   plugin.read_structure = read_parm7_structure;
00150   plugin.read_bonds = read_parm7_bonds;
00151   plugin.close_file_read = close_parm7_read;
00152   return VMDPLUGIN_SUCCESS;
00153 }
00154 
00155 VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
00156   (*cb)(v,(vmdplugin_t *)&plugin);
00157   return VMDPLUGIN_SUCCESS;
00158 }
00159 
00160 VMDPLUGIN_API int VMDPLUGIN_fini(){
00161   return VMDPLUGIN_SUCCESS;
00162 }

Generated on Fri Aug 29 01:40:39 2008 for VMD Plugins (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002