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.17 $       $Date: 2006/02/23 19:36:44 $
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("Error) Unable to open bgf file %s for writing\n", filename);
00275     return NULL;
00276   }
00277   
00278   data = (bgfdata *)malloc(sizeof(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 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   // Allocate memory for the from and to arrays. This will be freed in
00380   // close_mol2_read
00381   bgf->from = new int[bgf->nbonds];
00382   bgf->to = new int[bgf->nbonds];
00383   bgf->bondorder = new float[bgf->nbonds];
00384 
00385   // Find and read the BOND record
00386   rewind(bgf->file);
00387   do {
00388     if ( ferror(bgf->file) || feof(bgf->file) ) {
00389       printf("bgfplugin) No bond record found in file.\n");
00390       return MOLFILE_ERROR;
00391     }
00392     fgets(line, LINESIZE, bgf->file);
00393   } while ( strncmp(line, "FORMAT CONECT", 13) != 0 );
00394 
00395   // Read the bonds
00396   int j; //From atom
00397   int k; //To atom
00398   bool conline=false; //true if line after the conect line is an order line
00399   char currbond[7]="xxxxxx"; //Stores current bond field
00400   char currcon[7]="xxxxxx"; //Stores current ORDER field
00401   char* bondptr; //pointer to current position in bond line
00402   char* conptr; //pointer to current position in order line
00403   int bonds[8]; //Stores bonds of current atom
00404   float orders[8]; //Stores bond orders of current atom
00405   int numbonds; //Stores number of bonds of current atom
00406   int numords; //Stores number of bond order records of current atom
00407   float bo; //current bond order
00408   int i=0; //Number of the current bond
00409   int numfields=0; //number of fields in the current line
00410   fgets(line, LINESIZE, bgf->file);
00411   while (1) {
00412     // bondptr=NULL;
00413     //conptr=NULL;
00414     conline=false;
00415 
00416     if (strncmp(line,"END", 3)==0) 
00417       break;
00418 
00419     fgets(nextline, LINESIZE, bgf->file);
00420     if ( ferror(bgf->file) || feof(bgf->file) ) {
00421       printf("bgfplugin) Error occurred reading bond record.\n");
00422       return MOLFILE_ERROR;
00423     }
00424 
00425     if (strncmp(nextline,"ORDER",5)==0) 
00426       conline=true;
00427 
00428     if (strncmp(line,"CONECT",6)==0) {
00429       numfields=(strlen(line)-1)/6;
00430       bondptr=&line[0];
00431       numfields--;
00432       bondptr += 6;
00433       numbonds=0;
00434       numords=0;
00435       strncpy(currbond,bondptr,6);
00436       j=atoi(currbond);
00437       numfields--;
00438       bondptr += 6;
00439 
00440       while ((numfields > 0) && (numbonds < 8)) {
00441         strncpy(currbond,bondptr,6);
00442         numfields--;
00443         bondptr += 6;
00444         bonds[numbonds]=atoi(currbond);
00445         numbonds++;
00446       }
00447 
00448       if (conline) {
00449         numfields=(strlen(line)-1)/6;
00450         conptr=&nextline[0];
00451         numfields -= 2;
00452         conptr += 12;
00453         numords=0;
00454         while ((numfields > 0) && (numords < numbonds)) {
00455           strncpy(currcon,conptr,6);
00456           numfields--;
00457           conptr+=6;
00458           bo=atof(currcon);
00459           orders[numords]=bo;
00460           numords++;
00461         }
00462       }
00463 
00464       for (int l=0;l<numbonds;l++) {
00465         k=bonds[l];
00466         if (j<k) {
00467           bgf->from[i]=j;
00468           bgf->to[i]=k;
00469 
00470           if (conline) {
00471             bgf->bondorder[i]=orders[l];
00472           } else {
00473             bgf->bondorder[i]=1.0;
00474           }
00475 
00476           i++;
00477         }
00478       }
00479         
00480       if (conline) {
00481         fgets(line, LINESIZE, bgf->file);
00482       } else {
00483         strncpy(line,nextline,LINESIZE);
00484       }
00485     } else {
00486       strncpy(line,nextline,LINESIZE);
00487     }
00488   }
00489 
00490   *nbonds = i;
00491   *fromptr = bgf->from;
00492   *toptr = bgf->to;
00493   *bondorderptr = bgf->bondorder; 
00494 
00495   return MOLFILE_SUCCESS;
00496 }
00497 
00498 
00499 static int read_bonds(void *v, int *nbonds, int **fromptr, int **toptr, float **bondorderptr) {
00500   bgfdata *bgf = (bgfdata *)v;
00501 
00502   *nbonds=bgf->nbonds;
00503   if (bgf->nbonds > 0) {
00504     bgf->from = (int *) malloc(*nbonds*sizeof(int));
00505     bgf->to = (int *) malloc(*nbonds*sizeof(int));
00506     bgf->bondorder = (float *) malloc(*nbonds*sizeof(float));
00507 
00508     if ((read_bgf_bonds(bgf, nbonds, &(bgf->from), &(bgf->to), &(bgf->bondorder))) != MOLFILE_SUCCESS) {
00509       fclose(bgf->file);
00510       bgf->file = NULL;
00511       return MOLFILE_ERROR;
00512     }
00513 
00514     *fromptr = bgf->from;
00515     *toptr = bgf->to;
00516     *bondorderptr = bgf->bondorder; 
00517   } else {
00518     printf("bgfplugin) WARNING: no bonds defined in bgf file.\n");
00519     *fromptr = NULL;
00520     *toptr = NULL;
00521     *bondorderptr = NULL;
00522   }
00523 
00524   return MOLFILE_SUCCESS;
00525 }
00526 
00527 
00528 static int write_bgf_timestep(void *mydata, const molfile_timestep_t *ts) {
00529   bgfdata *data = (bgfdata *)mydata; 
00530   const molfile_atom_t *atom;
00531   const float *pos;
00532   int i;
00533 
00534   //print header block
00535   fprintf(data->file, "BIOGRF  332\n");
00536   fprintf(data->file, "REMARK NATOM %4i\n", data->natoms);
00537   fprintf(data->file, "FORCEFIELD DREIDING\n");
00538   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");
00539 
00540   atom = data->atomlist;
00541   pos = ts->coords;
00542   int numbonds=0;
00543   int lp=0;
00544   char atomfield[7];
00545   for (i = 0; i < data->natoms; i++) {
00546     getatomfield(&atomfield[0], atom->resname);
00547     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);
00548     ++atom; 
00549     pos += 3;
00550   }
00551 
00552   //write the connectivity data
00553   fprintf(data->file,"FORMAT CONECT (a6,14i6) \nFORMAT ORDER (a6,i6,13f6.3)\n");
00554 
00555   //iterate through the bond arrays and write them all
00556   int *bonds    =(int *)  malloc((data->natoms+1) * sizeof(int) * 6);
00557   float *orders =(float *)malloc((data->natoms+1) * sizeof(float) * 6);
00558   int *numcons  =(int *)  malloc((data->natoms+1) * sizeof(int));
00559 
00560   for (i=0;i<data->natoms+1;i++) {
00561     numcons[i]=0;
00562   }
00563 
00564   int j, k;         //indices for atoms being bonded
00565   float o;          //bond order
00566   bool printorder;  //flag to print bond orders
00567   int l;            //dummy iterator in bond order loop
00568   for (i=0;i<data->nbonds;i++) {
00569     j=data->from[i];
00570     k=data->to[i];
00571     o=data->bondorder[i];
00572     numcons[j]++;
00573     numcons[k]++;
00574     if (numcons[j]>6) {
00575       printf("bgfplugin) Warning: Bond overflow. Not all bonds were written\n");
00576       numcons[j]--;
00577       numcons[k]--;
00578       continue;
00579     }
00580        
00581     if (numcons[k]>6) {
00582       printf("bgfplugin) Warning: Bond overflow. Not all bonds were written\n");
00583       numcons[k]--;
00584       numcons[j]--;
00585       continue;
00586     }
00587     bonds[6*j+numcons[j]-1]=k;
00588     bonds[6*k+numcons[k]-1]=j;
00589     orders[6*j+numcons[j]-1]=o;
00590     orders[6*k+numcons[k]-1]=o;
00591   }
00592 
00593   for (i=1;i<=data->natoms;i++) {
00594     fprintf(data->file,"CONECT%6i",i);
00595     for (j=0;j<numcons[i];j++) {
00596       fprintf(data->file,"%6i",bonds[6*i+j]);
00597     }
00598     fprintf(data->file,"\n");
00599     printorder = false;
00600     for (l=0;l<numcons[i];l++) {
00601       if (orders[6*i+l] != 1.0) {
00602         printorder = true;
00603       }
00604     }
00605     if (printorder) {
00606       fprintf(data->file,"ORDER %6i",i);
00607       for (j=0;j<numcons[i];j++) {
00608         fprintf(data->file,"%6i",int(orders[6*i+j]));
00609       }
00610       fprintf(data->file,"\n");
00611     }
00612   }
00613 
00614   free(bonds);
00615   free(orders);
00616   free(numcons);
00617 
00618   fprintf(data->file,"END\n");
00619   return MOLFILE_SUCCESS;
00620 }
00621 
00622 static int write_bonds(void *v, int nbonds, int *fromptr, int *toptr, float *bondorderptr) {
00623   bgfdata *data = (bgfdata *)v;
00624   data->from = new int[nbonds];
00625   data->to = new int[nbonds];
00626   data->bondorder = new float[nbonds];
00627 
00628   //set the pointers for use later
00629   for (int i=0;i<nbonds;i++) {
00630     data->from[i]=fromptr[i];
00631     data->to[i]=toptr[i];
00632     data->bondorder[i]=bondorderptr[i];
00633   }
00634 
00635   data->nbonds = nbonds;
00636   return MOLFILE_SUCCESS;
00637 }
00638 
00639 static void close_bgf_write(void *mydata) {
00640   bgfdata *data = (bgfdata *)mydata;
00641   if (data->file != NULL) fclose(data->file);
00642 
00643   if (data->atomlist != NULL) free(data->atomlist);
00644   if (data->from != NULL) free(data->from);
00645   if (data->to != NULL) free(data->to);
00646   if (data->bondorder != NULL) free(data->bondorder);
00647   free(data);
00648 }
00649 
00650 //
00651 // Free the memory used by the bgf structure
00652 static void close_bgf_read(void *v) {
00653   bgfdata *bgf = (bgfdata *)v;
00654   if (bgf) {
00655     if (bgf->file != NULL) fclose(bgf->file);
00656     if (bgf->from != NULL) free(bgf->from);
00657     if (bgf->to != NULL)   free(bgf->to);
00658     if (bgf->bondorder != NULL)   free(bgf->bondorder);
00659     delete bgf;
00660   }
00661 }
00662 
00663 
00664 static molfile_plugin_t bgfplugin = {
00665   vmdplugin_ABIVERSION,
00666   MOLFILE_PLUGIN_TYPE,                      
00667   "bgf",                                    
00668   "MSI Biograf Format",
00669   "Peter Freddolino ",    
00670   0,                                        
00671   9,                                        
00672   VMDPLUGIN_THREADSAFE,                     
00673   "bgf",
00674   open_bgf_read,
00675   read_bgf_structure,
00676   read_bonds,
00677   read_bgf_timestep,
00678   close_bgf_read,
00679   open_bgf_write,
00680   write_bgf_structure,
00681   write_bgf_timestep,
00682   close_bgf_write,
00683   0,                            
00684   0,                            
00685   0,
00686   0,
00687   write_bonds  
00688 };
00689 
00690 VMDPLUGIN_API int VMDPLUGIN_init() {
00691   return VMDPLUGIN_SUCCESS;
00692 }
00693 
00694 VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
00695   (*cb)(v, (vmdplugin_t *)&bgfplugin);
00696   return VMDPLUGIN_SUCCESS;
00697 }
00698 
00699 VMDPLUGIN_API int VMDPLUGIN_fini() {
00700   return VMDPLUGIN_SUCCESS;
00701 }
00702 
00703 

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