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

bgfplugin.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: bgfplugin.C,v $
00013  *      $Author: johns $       $Locker:  $             $State: Exp $
00014  *      $Revision: 1.24 $       $Date: 2008/09/24 17:38:18 $
00015  *
00016  ***************************************************************************/
00017 
00018 #include "molfile_plugin.h"
00019 
00020 #include <stdlib.h>
00021 #include <stdio.h>
00022 #include <string.h>
00023 
00024 #if defined(_AIX)
00025 #include <strings.h>
00026 #endif
00027 
00028 #define LINESIZE 256
00029 
00030 typedef struct {
00031   FILE *file;
00032   molfile_atom_t *atomlist;
00033   int natoms, nbonds, optflags, coords_read;
00034   int *from, *to;
00035   float *bondorder;
00036 } bgfdata;
00037 
00038 // Open the file and create the bgf struct used to pass data to the other
00039 // functions.
00040 static void *open_bgf_read(const char *path, const char *filetype, 
00041     int *natoms) {
00042   FILE *fd;
00043   bgfdata *bgf;
00044   char line[LINESIZE]; 
00045   int nbonds, optflags;
00046   int numat=0;
00047   nbonds=0;
00048   int nbline; //Number of bonds in current line
00049 
00050   if ((fd = fopen(path, "r")) == NULL)
00051     return NULL;
00052 
00053   do {
00054     fgets(line, LINESIZE, fd);
00055     if ( ferror(fd) || feof(fd) ) {
00056       printf("bgfplugin) Improperly terminated bgf file\n");
00057       return NULL;
00058     }
00059 
00060     if ((strncmp(line, "ATOM", 4) == 0) || (strncmp(line, "HETATM", 6)==0)) 
00061       numat++;
00062 
00063     if (strncmp(line,"CONECT",6)==0) {
00064       nbline=(strlen(line)-1)/6; 
00065       nbline -= 2;
00066       nbonds += nbline;
00067     }
00068 
00069   } while ( strncmp(line, "END", 3) );
00070     
00071   optflags = MOLFILE_INSERTION | MOLFILE_CHARGE; 
00072   *natoms = numat;
00073   rewind(fd);
00074 
00075   // Allocate and initialize the bgf structure
00076   bgf = new bgfdata;
00077   bgf->file = fd;
00078   bgf->natoms = *natoms;
00079   bgf->nbonds = nbonds;
00080 
00081   bgf->optflags = optflags;
00082   bgf->coords_read = 0;
00083   bgf->from = NULL;
00084   bgf->to = NULL;
00085   bgf->bondorder = NULL;
00086 
00087   return bgf;
00088 }
00089 
00090 
00091 static void adjust_bgf_field_string(char *field) {
00092   int i, len;
00093 
00094   len = strlen(field);
00095   while (len > 0 && field[len-1] == ' ') {
00096     field[len-1] = '\0';
00097     len--;
00098   }
00099 
00100   while (len > 0 && field[0] == ' ') {
00101     for (i=0; i < len; i++)
00102       field[i] = field[i+1];
00103     len--;
00104   }
00105 }
00106 
00107 
00108 static void get_bgf_coordinates(const char *record, 
00109                                 float *x, float *y, float *z) {
00110   char numstr[50]; /* store all fields in one array to save memset calls */
00111   memset(numstr, 0, sizeof(numstr));
00112   if (x != NULL) {
00113     strncpy(numstr, record + 31, 10);
00114     *x = (float) atof(numstr);
00115   }
00116 
00117   if (y != NULL) {
00118     strncpy(numstr+10, record + 41, 10);
00119     *y = (float) atof(numstr+10);
00120   }
00121 
00122   if (z != NULL) {
00123     strncpy(numstr+20, record + 51, 10);
00124     *z = (float) atof(numstr+20);
00125   }
00126 }
00127 
00128 
00129 static void get_bgf_fields(const char *record, char *name, char *resname, 
00130                            char *chain, char* segname,
00131                            int *resid, char *type, float *charge,
00132                            float *x, float *y, float *z) {
00133   char tempresid[6];
00134   char tempcharge[9];
00135 
00136   /* get atom name */
00137   strncpy(name, record + 13, 5);
00138   name[5] = '\0';
00139   adjust_bgf_field_string(name); /* remove spaces from the name */
00140 
00141   /* get residue name */
00142   strncpy(resname, record + 19, 4);
00143   resname[4] = '\0';
00144   adjust_bgf_field_string(resname); /* remove spaces from the resname */
00145 
00146   /* set segname */
00147   segname[0]='\0';
00148 
00149   /* get chain name */
00150   chain[0] = record[23];
00151   chain[1] = '\0';
00152 
00153   /* get residue id number */
00154   strncpy(tempresid, record + 26, 5);
00155   tempresid[5] = '\0';
00156   adjust_bgf_field_string(tempresid); /* remove spaces from the resid */
00157   *resid=atoi(tempresid);
00158 
00159   /* get force field type */
00160   strncpy(type, record+61, 5);
00161   type[5]='\0';
00162   adjust_bgf_field_string(type);
00163 
00164   /* get charge*/
00165   strncpy(tempcharge, record + 72, 8);
00166   tempcharge[8] = '\0';
00167   adjust_bgf_field_string(tempcharge); /* remove spaces from the charge */
00168   *charge=atof(tempcharge);
00169 
00170   /* get x, y, and z coordinates */
00171   get_bgf_coordinates(record, x, y, z);
00172 }  
00173 
00174 
00175 // Read atom information, but not coordinates.
00176 static int read_bgf_structure(void *v, int *optflags, molfile_atom_t *atoms) {
00177   bgfdata *bgf = (bgfdata *)v;
00178   char line[LINESIZE]; 
00179   molfile_atom_t *atom;
00180   int natoms=0;
00181 
00182   *optflags = bgf->optflags;
00183 
00184   // Find and read the ATOM record
00185   rewind(bgf->file);
00186   do {
00187     fgets(line, LINESIZE, bgf->file);
00188     if ( ferror(bgf->file) || feof(bgf->file) ) {
00189       printf("bgfplugin) FORMAT ATOM record not found in file.\n");
00190       return MOLFILE_ERROR;
00191     }
00192   } while ( strncmp(line, "FORMAT ATOM", 11) );
00193 
00194   // Read the atoms
00195   do {
00196     fgets(line, LINESIZE, bgf->file);
00197     if ( ferror(bgf->file) || feof(bgf->file) ) {
00198       printf("bgfplugin) Error occurred reading atom record.\n");
00199       return MOLFILE_ERROR;
00200     }
00201 
00202     if (strncmp(line, "ATOM", 4) && strncmp(line, "HETATM", 6)) 
00203       continue;
00204 
00205     atom=atoms+natoms;
00206     natoms++;
00207 
00208     get_bgf_fields(line, atom->name, atom->resname, atom->chain, 
00209                    atom->segid, &atom->resid, atom->type, &atom->charge, 
00210                    NULL, NULL, NULL);
00211   } while (strncmp(line, "END", 3));
00212 
00213   bgf->natoms = natoms;
00214 
00215   return MOLFILE_SUCCESS;
00216 }
00217 
00218 
00219 // Read atom coordinates
00220 static int read_bgf_timestep(void *v, int natoms, molfile_timestep_t *ts) {
00221   bgfdata *bgf = (bgfdata *)v;
00222   char line[LINESIZE];
00223   int i;
00224   float x, y, z;
00225 
00226   // Since the file is rewound when coordinates are read, EOF shouldn't
00227   // happen. Instead, use a flag to indicate that the single timestep has
00228   // been read
00229   if (bgf->coords_read) {
00230     return MOLFILE_EOF;
00231   }
00232 
00233   // Find and read the ATOM record
00234   rewind(bgf->file);
00235   do {
00236     fgets(line, LINESIZE, bgf->file);
00237     if ( ferror(bgf->file) || feof(bgf->file) ) {
00238       printf("bgfplugin) No FORMAT ATOM record found in file.\n");
00239       return MOLFILE_ERROR;
00240     }
00241   } while ( strncmp(line, "FORMAT ATOM", 11) );
00242 
00243   // Read the atoms
00244   for (i = 0; i < bgf->natoms; i++) {
00245     fgets(line, LINESIZE, bgf->file);
00246     if ( ferror(bgf->file) || feof(bgf->file) ) {
00247       printf("bgfplugin) Error occurred reading atom coordinates.\n");
00248       return MOLFILE_ERROR;
00249     }
00250 
00251     // skip comments and blank lines
00252     if (strncmp(line,"ATOM",4)!=0 && strncmp(line,"HETATM",6)!=0) continue;
00253 
00254     get_bgf_coordinates(line, &x, &y, &z);
00255 
00256     if (ts) {
00257       ts->coords[3*i  ] = x;
00258       ts->coords[3*i+1] = y;
00259       ts->coords[3*i+2] = z;
00260     }
00261   }
00262 
00263   bgf->coords_read = 1;
00264   return MOLFILE_SUCCESS;
00265 }
00266 
00267 
00268 static void *open_bgf_write(const char *filename, const char *filetype, 
00269                            int natoms) {
00270   FILE *fd;
00271   bgfdata *data;
00272 
00273   if ((fd = fopen(filename, "w")) == NULL) {
00274     printf("bgfplugin) Error: unable to open bgf file %s for writing\n", filename);
00275     return NULL;
00276   }
00277   
00278   data = new bgfdata;
00279   data->natoms = natoms;
00280   data->file = fd;
00281   return data;
00282 }
00283 
00284 
00285 static int write_bgf_structure(void *mydata, int optflags, 
00286                                const molfile_atom_t *atoms) {
00287   bgfdata *data = (bgfdata *)mydata;
00288   data->atomlist = (molfile_atom_t *)malloc(data->natoms*sizeof(molfile_atom_t));
00289   memcpy(data->atomlist, atoms, data->natoms*sizeof(molfile_atom_t));
00290   return MOLFILE_SUCCESS;
00291 }
00292 
00293 static void getatomfield(char* atomfield, const char* resname) {
00294   if ((strncmp(resname,"ALA",3) == 0) || (strncmp(resname,"ASP",3) == 0) || (strncmp(resname,"ARG",3) == 0) || (strncmp(resname,"ASN",3) == 0) || (strncmp(resname,"CYS",3) == 0) || (strncmp(resname,"GLN",3) == 0) || (strncmp(resname,"GLU",3) == 0) || (strncmp(resname,"GLY",3) == 0) || (strncmp(resname,"HIS",3) == 0) || (strncmp(resname,"ILE",3) == 0) || (strncmp(resname,"LEU",3) == 0) || (strncmp(resname,"LYS",3) == 0) || (strncmp(resname,"MET",3) == 0) || (strncmp(resname,"PHE",3) == 0) || (strncmp(resname,"PRO",3) == 0) || (strncmp(resname,"SER",3) == 0) || (strncmp(resname,"THR",3) == 0) || (strncmp(resname,"TRP",3) == 0) || (strncmp(resname,"TYR",3) == 0) || (strncmp(resname,"VAL",3) == 0) || (strncmp(resname,"ADE",3) == 0) || (strncmp(resname,"THY",3) == 0) || (strncmp(resname,"GUA",3) == 0) || (strncmp(resname,"CYT",3) == 0) || (strncmp(resname,"URA",3) == 0) || (strncmp(resname,"HSD",3) == 0) || (strncmp(resname,"HSE",3) == 0) || (strncmp(resname,"HSP",3) == 0)) {
00295     strncpy(atomfield, "ATOM  \0", 7);
00296   } else {
00297     strncpy(atomfield, "HETATM\0", 7);
00298   }
00299 }
00300       
00301 static void getdreiidff(char* outputtype, const char* psftype, int& numbonds, int& lp) {
00302   //Note that while this function isn't used yet, it actually IS important, and will be enabled in the future pending dicussion with some bgf users
00303   if (strncmp(psftype,"H",1)==0) {
00304     //It's a hydrogen
00305     //FIXME: Doesn't properly identify acidic hydrogens yet
00306     strncpy(outputtype, "H_  ",4);
00307     numbonds=1;
00308     lp=0;
00309     return;
00310   } else if (strncmp(psftype,"C",1)==0) {
00311     //It's a carbon... probably
00312     if (strncmp(psftype,"C ",2)==0 || strncmp(psftype,"CA ",3)==0 || strncmp(psftype,"CPH",3)==0 || strncmp(psftype,"CPT",3)==0 || strncmp(psftype,"CC ",3)==0 || strncmp(psftype,"CD ",3)==0 || strncmp(psftype,"CN1",3)==0 || strncmp(psftype,"CN2",3)==0 || strncmp(psftype,"CN3",3)==0 || strncmp(psftype,"CN4",3)==0 || strncmp(psftype,"CN5",3)==0 || strncmp(psftype,"CNA",3)==0) {
00313       strncpy(outputtype, "C_2 ",4);
00314       numbonds=3;
00315       lp=0;
00316       return; 
00317     } else {
00318       strncpy(outputtype, "C_3 ",4);
00319       numbonds=4;
00320       lp=0;
00321       return; 
00322     }  
00323   } else if (strncmp(psftype,"N",1)==0) {
00324     //It"s probably nitrogen
00325     if (strncmp(psftype,"NR",2)==0 || strncmp(psftype,"NH1",3)==0 || strncmp(psftype,"NH2",3)==0 || strncmp(psftype,"NC2",3)==0 || strncmp(psftype,"NY",2)==0 || (strncmp(psftype,"NN",2)==0 && strncmp(psftype,"NN6",3)!=0)) {
00326       strncpy(outputtype, "N_R",4);
00327       numbonds=3;
00328       lp=0;
00329       return;
00330     } else {
00331       strncpy(outputtype, "N_3 ",4);
00332       numbonds=3;
00333       lp=1;
00334       return;
00335     }
00336   } else if (strncmp(psftype,"O",1)==0) {
00337     //Probably an oxygen
00338     if (strncmp(psftype,"OH1",3)==0 || strncmp(psftype,"OS",2)==0 || strncmp(psftype,"OT ",3)==0 || strncmp(psftype,"ON4",3)==0 || strncmp(psftype,"ON5",3)==0 || strncmp(psftype,"ON6",3)==0) {
00339       strncpy(outputtype, "O_3 ",4);
00340       numbonds=2;
00341       lp=2;
00342       return;
00343    } else {
00344       strncpy(outputtype, "O_2 ",4);
00345       numbonds=1;
00346       lp=2;
00347       return;
00348     }
00349   } else if (strncmp(psftype,"S",1)==0) {
00350     strncpy(outputtype, "S_3 ",4);
00351     numbonds=2;
00352     lp=2;
00353     return;
00354   } else if (strncmp(psftype,"P",1)==0) {
00355     strncpy(outputtype, "P_3 ",4);
00356     numbonds=6;
00357     lp=0;
00358     return;
00359   } else {
00360     strncpy(outputtype, "X_  ",4);
00361     numbonds=0;
00362     lp=0;
00363     return;
00364   }
00365 }
00366 
00367 
00368 static int read_bgf_bonds(void *v, int *nbonds, int **fromptr, int **toptr, float **bondorderptr) {
00369   bgfdata *bgf = (bgfdata *)v;
00370   char line[LINESIZE]; 
00371   char nextline[LINESIZE]; 
00372   if (bgf->nbonds == 0) {
00373     *nbonds = 0;
00374     *fromptr = NULL;
00375     *toptr = NULL;
00376     return MOLFILE_SUCCESS;
00377   }
00378 
00379   // Find and read the BOND record
00380   rewind(bgf->file);
00381   do {
00382     if ( ferror(bgf->file) || feof(bgf->file) ) {
00383       printf("bgfplugin) No bond record found in file.\n");
00384       return MOLFILE_ERROR;
00385     }
00386     fgets(line, LINESIZE, bgf->file);
00387   } while ( strncmp(line, "FORMAT CONECT", 13) != 0 );
00388 
00389   // Read the bonds
00390   int j; //From atom
00391   int k; //To atom
00392   bool conline=false; //true if line after the conect line is an order line
00393   char currbond[7]="xxxxxx"; //Stores current bond field
00394   char currcon[7]="xxxxxx"; //Stores current ORDER field
00395   char* bondptr; //pointer to current position in bond line
00396   char* conptr; //pointer to current position in order line
00397   int bonds[8]; //Stores bonds of current atom
00398   float orders[8]; //Stores bond orders of current atom
00399   int numbonds; //Stores number of bonds of current atom
00400   int numords; //Stores number of bond order records of current atom
00401   float bo; //current bond order
00402   int i=0; //Number of the current bond
00403   int numfields=0; //number of fields in the current line
00404   fgets(line, LINESIZE, bgf->file);
00405   while (1) {
00406     // bondptr=NULL;
00407     //conptr=NULL;
00408     conline=false;
00409 
00410     if (strncmp(line,"END", 3)==0) 
00411       break;
00412 
00413     fgets(nextline, LINESIZE, bgf->file);
00414     if ( ferror(bgf->file) || feof(bgf->file) ) {
00415       printf("bgfplugin) Error occurred reading bond record.\n");
00416       return MOLFILE_ERROR;
00417     }
00418 
00419     if (strncmp(nextline,"ORDER",5)==0) 
00420       conline=true;
00421 
00422     if (strncmp(line,"CONECT",6)==0) {
00423       numfields=(strlen(line)-1)/6;
00424       bondptr=&line[0];
00425       numfields--;
00426       bondptr += 6;
00427       numbonds=0;
00428       numords=0;
00429       strncpy(currbond,bondptr,6);
00430       j=atoi(currbond);
00431       numfields--;
00432       bondptr += 6;
00433 
00434       while ((numfields > 0) && (numbonds < 8)) {
00435         strncpy(currbond,bondptr,6);
00436         numfields--;
00437         bondptr += 6;
00438         bonds[numbonds]=atoi(currbond);
00439         numbonds++;
00440       }
00441 
00442       if (conline) {
00443         numfields=(strlen(line)-1)/6;
00444         conptr=&nextline[0];
00445         numfields -= 2;
00446         conptr += 12;
00447         numords=0;
00448         while ((numfields > 0) && (numords < numbonds)) {
00449           strncpy(currcon,conptr,6);
00450           numfields--;
00451           conptr+=6;
00452           bo=atof(currcon);
00453           orders[numords]=bo;
00454           numords++;
00455         }
00456       }
00457 
00458       for (int l=0;l<numbonds;l++) {
00459         k=bonds[l];
00460         if (j<k) {
00461           bgf->from[i]=j;
00462           bgf->to[i]=k;
00463 
00464           if (conline) {
00465             bgf->bondorder[i]=orders[l];
00466           } else {
00467             bgf->bondorder[i]=1.0;
00468           }
00469 
00470           i++;
00471         }
00472       }
00473         
00474       if (conline) {
00475         fgets(line, LINESIZE, bgf->file);
00476       } else {
00477         strncpy(line,nextline,LINESIZE);
00478       }
00479     } else {
00480       strncpy(line,nextline,LINESIZE);
00481     }
00482   }
00483 
00484   *nbonds = i;
00485   *fromptr = bgf->from;
00486   *toptr = bgf->to;
00487   *bondorderptr = bgf->bondorder; 
00488 
00489   return MOLFILE_SUCCESS;
00490 }
00491 
00492 
00493 static int read_bonds(void *v, int *nbonds, int **fromptr, int **toptr, float **bondorderptr) {
00494   bgfdata *bgf = (bgfdata *)v;
00495 
00496   *nbonds=bgf->nbonds;
00497   if (bgf->nbonds > 0) {
00498     bgf->from = (int *) malloc(*nbonds*sizeof(int));
00499     bgf->to = (int *) malloc(*nbonds*sizeof(int));
00500     bgf->bondorder = (float *) malloc(*nbonds*sizeof(float));
00501 
00502     if ((read_bgf_bonds(bgf, nbonds, &(bgf->from), &(bgf->to), &(bgf->bondorder))) != MOLFILE_SUCCESS) {
00503       fclose(bgf->file);
00504       bgf->file = NULL;
00505       return MOLFILE_ERROR;
00506     }
00507 
00508     *fromptr = bgf->from;
00509     *toptr = bgf->to;
00510     *bondorderptr = bgf->bondorder; 
00511   } else {
00512     printf("bgfplugin) WARNING: no bonds defined in bgf file.\n");
00513     *fromptr = NULL;
00514     *toptr = NULL;
00515     *bondorderptr = NULL;
00516   }
00517 
00518   return MOLFILE_SUCCESS;
00519 }
00520 
00521 
00522 static int write_bgf_timestep(void *mydata, const molfile_timestep_t *ts) {
00523   bgfdata *data = (bgfdata *)mydata; 
00524   const molfile_atom_t *atom;
00525   const float *pos;
00526   int i;
00527 
00528   //print header block
00529   fprintf(data->file, "BIOGRF  332\n");
00530   fprintf(data->file, "REMARK NATOM %4i\n", data->natoms);
00531   fprintf(data->file, "FORCEFIELD DREIDING\n");
00532   fprintf(data->file, "FORMAT ATOM   (a6,1x,i5,1x,a5,1x,a3,1x,a1,1x,a5,3f10.5,1x,a5,i3,i2,1x,f8.5,i2,i4,f10.5)\n");
00533 
00534   atom = data->atomlist;
00535   pos = ts->coords;
00536   int numbonds=0;
00537   int lp=0;
00538   char atomfield[7];
00539   for (i = 0; i < data->natoms; i++) {
00540     getatomfield(&atomfield[0], atom->resname);
00541     fprintf(data->file, "%-6s %5i %5s %3.3s %1s %5i%10.5f%10.5f%10.5f %-5s%3i%2i %8.5f%2i%4i\n", atomfield, i+1, atom->name, atom->resname, atom->chain, atom->resid, pos[0], pos[1], pos[2], atom->type, numbonds, lp, atom->charge, 0, 0);
00542     ++atom; 
00543     pos += 3;
00544   }
00545 
00546   //write the connectivity data
00547   fprintf(data->file,"FORMAT CONECT (a6,14i6) \nFORMAT ORDER (a6,i6,13f6.3)\n");
00548 
00549   //iterate through the bond arrays and write them all
00550   int *bonds    =(int *)  malloc((data->natoms+1) * sizeof(int) * 6);
00551   float *orders =(float *)malloc((data->natoms+1) * sizeof(float) * 6);
00552   int *numcons  =(int *)  malloc((data->natoms+1) * sizeof(int));
00553 
00554   for (i=0;i<data->natoms+1;i++) {
00555     numcons[i]=0;
00556   }
00557 
00558   int j, k;         //indices for atoms being bonded
00559   float o;          //bond order
00560   bool printorder;  //flag to print bond orders
00561   int l;            //dummy iterator in bond order loop
00562   for (i=0;i<data->nbonds;i++) {
00563     j=data->from[i];
00564     k=data->to[i];
00565     o=data->bondorder[i];
00566     numcons[j]++;
00567     numcons[k]++;
00568     if (numcons[j]>6) {
00569       printf("bgfplugin) Warning: Bond overflow. Not all bonds were written\n");
00570       numcons[j]--;
00571       numcons[k]--;
00572       continue;
00573     }
00574        
00575     if (numcons[k]>6) {
00576       printf("bgfplugin) Warning: Bond overflow. Not all bonds were written\n");
00577       numcons[k]--;
00578       numcons[j]--;
00579       continue;
00580     }
00581     bonds[6*j+numcons[j]-1]=k;
00582     bonds[6*k+numcons[k]-1]=j;
00583     orders[6*j+numcons[j]-1]=o;
00584     orders[6*k+numcons[k]-1]=o;
00585   }
00586 
00587   for (i=1;i<=data->natoms;i++) {
00588     fprintf(data->file,"CONECT%6i",i);
00589     for (j=0;j<numcons[i];j++) {
00590       fprintf(data->file,"%6i",bonds[6*i+j]);
00591     }
00592     fprintf(data->file,"\n");
00593     printorder = false;
00594     for (l=0;l<numcons[i];l++) {
00595       if (orders[6*i+l] != 1.0) {
00596         printorder = true;
00597       }
00598     }
00599     if (printorder) {
00600       fprintf(data->file,"ORDER %6i",i);
00601       for (j=0;j<numcons[i];j++) {
00602         fprintf(data->file,"%6i",int(orders[6*i+j]));
00603       }
00604       fprintf(data->file,"\n");
00605     }
00606   }
00607 
00608   if (bonds != NULL) {
00609     free(bonds);
00610     bonds = NULL;
00611   }
00612   if (orders != NULL) {
00613     free(orders);
00614     orders = NULL;
00615   }
00616   if (numcons != NULL) {
00617     free(numcons);
00618     numcons = NULL;
00619   }
00620 
00621   fprintf(data->file,"END\n");
00622   return MOLFILE_SUCCESS;
00623 }
00624 
00625 static int write_bonds(void *v, int nbonds, int *fromptr, int *toptr, float *bondorderptr) {
00626   bgfdata *data = (bgfdata *)v;
00627   data->from = (int*) malloc(nbonds * sizeof(int));
00628   data->to = (int*) malloc(nbonds * sizeof(int));
00629   data->bondorder = (float*) malloc(nbonds * sizeof(float));
00630 
00631   //set the pointers for use later
00632   for (int i=0;i<nbonds;i++) {
00633     data->from[i]=fromptr[i];
00634     data->to[i]=toptr[i];
00635     data->bondorder[i]=bondorderptr[i];
00636   }
00637 
00638   data->nbonds = nbonds;
00639   return MOLFILE_SUCCESS;
00640 }
00641 
00642 static void close_bgf_write(void *mydata) {
00643   bgfdata *data = (bgfdata *)mydata;
00644   if (data) {
00645     if (data->file != NULL) fclose(data->file);
00646     if (data->atomlist != NULL) free(data->atomlist);
00647     if (data->from != NULL) free(data->from);
00648     if (data->to != NULL) free(data->to);
00649     if (data->bondorder != NULL) free(data->bondorder);
00650     delete data;
00651   }
00652 }
00653 
00654 //
00655 // Free the memory used by the bgf structure
00656 static void close_bgf_read(void *v) {
00657   bgfdata *bgf = (bgfdata *)v;
00658   if (bgf) {
00659     if (bgf->file != NULL) fclose(bgf->file);
00660     if (bgf->from != NULL) free(bgf->from);
00661     if (bgf->to != NULL)   free(bgf->to);
00662     if (bgf->bondorder != NULL)   free(bgf->bondorder);
00663     delete bgf;
00664   }
00665 }
00666 
00667 
00668 static molfile_plugin_t plugin;
00669 
00670 VMDPLUGIN_API int VMDPLUGIN_init() {
00671   memset(&plugin, 0, sizeof(molfile_plugin_t));
00672   plugin.abiversion = vmdplugin_ABIVERSION;
00673   plugin.type = MOLFILE_PLUGIN_TYPE;
00674   plugin.name = "bgf";
00675   plugin.prettyname = "MSI Biograf Format";
00676   plugin.author = "Peter Freddolino ";
00677   plugin.majorv = 0;
00678   plugin.minorv = 12;
00679   plugin.is_reentrant = VMDPLUGIN_THREADSAFE;
00680   plugin.filename_extension = "bgf";
00681   plugin.open_file_read = open_bgf_read;
00682   plugin.read_structure = read_bgf_structure;
00683   plugin.read_bonds = read_bonds;
00684   plugin.read_next_timestep = read_bgf_timestep;
00685   plugin.close_file_read = close_bgf_read;
00686   plugin.open_file_write = open_bgf_write;
00687   plugin.write_structure = write_bgf_structure;
00688   plugin.write_timestep = write_bgf_timestep;
00689   plugin.close_file_write = close_bgf_write;
00690   plugin.write_bonds = write_bonds;
00691   return VMDPLUGIN_SUCCESS;
00692 }
00693 
00694 VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
00695   (*cb)(v, (vmdplugin_t *) &plugin);
00696   return VMDPLUGIN_SUCCESS;
00697 }
00698 
00699 VMDPLUGIN_API int VMDPLUGIN_fini() {
00700   return VMDPLUGIN_SUCCESS;
00701 }
00702 
00703 

Generated on Thu Oct 9 01:39:36 2008 for VMD Plugins (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002