xbgfplugin.C 19.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/***************************************************************************
 *cr
 *cr            (C) Copyright 1995-2016 The Board of Trustees of the
 *cr                        University of Illinois
 *cr                         All Rights Reserved
 *cr
 ***************************************************************************/

/***************************************************************************
 * RCS INFORMATION:
 *
 *      $RCSfile: xbgfplugin.C,v $
 *      $Author: johns $       $Locker:  $             $State: Exp $
 *      $Revision: 1.34 $       $Date: 2016/11/28 05:01:55 $
 *
 ***************************************************************************/

#include "molfile_plugin.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#if defined(_AIX)
#include <strings.h>
#endif

#define LINESIZE 256
#define MAXBONDS 16

typedef struct {
  FILE *file;
  molfile_atom_t *atomlist;
  molfile_metadata_t *meta;
  int natoms, nbonds, optflags, coords_read;
  int *from, *to;
  float *bondorder;
} xbgfdata;


// Open the file and create the xbgf struct used to pass data to the other
// functions.
static void *open_xbgf_read(const char *path, const char *filetype, 
    int *natoms) {
  FILE *fd;
  xbgfdata *bgf;
  char line[LINESIZE]; 
  int nbonds, optflags;
  int numat=0;
  nbonds=0;
  int nbline; //Number of bonds in current line

  // Allocate and initialize the bgf structure
  bgf = (xbgfdata *) malloc(sizeof(xbgfdata));
  memset(bgf, 0, sizeof(xbgfdata));

  bgf->meta = (molfile_metadata_t *) malloc(sizeof(molfile_metadata_t));
  memset(bgf->meta, 0, sizeof(molfile_metadata_t));

  bgf->meta->remarklen = 0;
  bgf->meta->remarks = NULL;

  if ((fd = fopen(path, "r")) == NULL)
    return NULL;

  do {
    fgets(line, LINESIZE, fd);
    if ( ferror(fd) || feof(fd) ) {
      printf("xbgfplugin) Improperly terminated bgf file\n");
      return NULL;
    }

    if ((strncmp(line, "ATOM", 4) == 0) || (strncmp(line, "HETATM", 6)==0)) 
      numat++;

    if (strncmp(line,"CONECT",6)==0) {
      nbline=(strlen(line)-1)/6; 
      nbline -= 2;
      nbonds += nbline;
    }

    // Read the remarks
    if (strncmp(line, "REMARK", 4)==0 || strncmp(line, "LEWIS", 4)==0 || 
        strncmp(line, "VDW", 3)==0) {
      int len=strlen(line);
      int newlen = len + bgf->meta->remarklen;
      char *newstr=(char*) realloc(bgf->meta->remarks, newlen + 1);
      if (newstr != NULL) {
        bgf->meta->remarks = newstr;
        bgf->meta->remarks[bgf->meta->remarklen] = '\0';
        memcpy(bgf->meta->remarks + bgf->meta->remarklen, line, len);
        bgf->meta->remarks[newlen] = '\0';
        bgf->meta->remarklen = newlen;
      }
    }

    optflags = MOLFILE_INSERTION | MOLFILE_CHARGE | MOLFILE_BFACTOR | MOLFILE_OCCUPANCY | MOLFILE_ATOMICNUMBER;
  } while ( strncmp(line, "END", 3) );
    
  *natoms = numat;
  rewind(fd);

  bgf->file = fd;
  bgf->natoms = *natoms;
  bgf->nbonds = nbonds;

  bgf->optflags = optflags;
  bgf->coords_read = 0;
  bgf->from = NULL;
  bgf->to = NULL;
  bgf->bondorder = NULL;
                                                                                
  return bgf;
}


static void adjust_xbgf_field_string(char *field) {
  int i, len;

  len = strlen(field);
  while (len > 0 && field[len-1] == ' ') {
    field[len-1] = '\0';
    len--;
  }

  while (len > 0 && field[0] == ' ') {
    for (i=0; i < len; i++)
      field[i] = field[i+1];
    len--;
  }
}

static void get_xbgf_coordinates(const char *record, 
                                float *x, float *y, float *z) {
  char numstr[50]; /* store all fields in one array to save memset calls */
  memset(numstr, 0, sizeof(numstr));
  if (x != NULL) {
    strncpy(numstr, record + 32, 10);
    *x = (float) atof(numstr);
  }

  if (y != NULL) {
    strncpy(numstr+10, record + 42, 10);
    *y = (float) atof(numstr+10);
  }

  if (z != NULL) {
    strncpy(numstr+20, record + 52, 10);
    *z = (float) atof(numstr+20);
  }
}


static void get_xbgf_fields(const char *record, char *name, char *resname, 
                           char *chain, char* segname, float *occupancy, 
                           float *bfactor, int *elementindex,
                           int *resid, char *type, float *charge,
                           float *x, float *y, float *z, char* insert) {
  char tempresid[6];
  char tempcharge[8];
  char tempbeta[7];
  char tempocc[7];
  char tempelem[4];
  strcpy(insert, " ");

  /* get atom name */
  strncpy(name, record + 14, 5);
  name[5] = '\0';
  adjust_xbgf_field_string(name); /* remove spaces from the name */

  /* get residue name */
  strncpy(resname, record + 20, 4);
  resname[4] = '\0';
  adjust_xbgf_field_string(resname); /* remove spaces from the resname */

  /* set segname */
  strncpy(segname, record + 101, 4);
  segname[4]='\0';
  adjust_xbgf_field_string(segname); /* remove spaces from the segname */

  /* get chain name */
  chain[0] = record[25];
  chain[1] = '\0';

  /* get residue id number */
  strncpy(tempresid, record + 27, 5);
  tempresid[5] = '\0';
  adjust_xbgf_field_string(tempresid); /* remove spaces from the resid */
  *resid=atoi(tempresid);

  /* get force field type */
  strncpy(type, record+63, 5);
  type[5]='\0';
  adjust_xbgf_field_string(type); /* remove spaces */

  /* get charge*/
  strncpy(tempcharge, record + 74, 7);
  tempcharge[7] = '\0';
  adjust_xbgf_field_string(tempcharge); /* remove spaces from the charge */
  *charge=atof(tempcharge);

  /* Get B factor, occupancy, and element */
  strncpy(tempbeta, record + 83, 6);
  tempbeta[6] = '\0';
  adjust_xbgf_field_string(tempbeta); /* remove spaces from the beta field */
  *bfactor=atof(tempbeta);
  
  strncpy(tempocc, record + 90, 6);
  tempocc[6] = '\0';
  adjust_xbgf_field_string(tempocc); /* remove spaces from the occupancy field */
  *occupancy=atof(tempocc);
  
  strncpy(tempelem, record + 97, 3);
  tempelem[3] = '\0';
  adjust_xbgf_field_string(tempelem); /* remove spaces from the element */
  *elementindex=atoi(tempelem);

  /* get x, y, and z coordinates */
  get_xbgf_coordinates(record, x, y, z);
}  


// Read atom information, but not coordinates.
static int read_xbgf_structure(void *v, int *optflags, molfile_atom_t *atoms) {
  xbgfdata *bgf = (xbgfdata *)v;
  char line[LINESIZE]; 
  molfile_atom_t *atom;
  int natoms=0;

  //optflags = MOLFILE_INSERTION | MOLFILE_CHARGE | MOLFILE_BFACTOR | MOLFILE_OCCUPANCY | MOLFILE_ATOMICNUMBER;
  *optflags = bgf->optflags;

  // Find and read the ATOM record
  rewind(bgf->file);
  do {
    fgets(line, LINESIZE, bgf->file);
    if ( ferror(bgf->file) || feof(bgf->file) ) {
      printf("xbgfplugin) FORMAT ATOM record found in file.\n");
      return MOLFILE_ERROR;
    }
  } while ( strncmp(line, "FORMAT ATOM", 11) );

  // Read the atoms
  do {
    fgets(line, LINESIZE, bgf->file);
    if ( ferror(bgf->file) || feof(bgf->file) ) {
      printf("xbgfplugin) Error occurred reading atom record.\n");
      return MOLFILE_ERROR;
    }

    if (strncmp(line, "ATOM", 4) && strncmp(line, "HETATM", 6)) continue;
    atom=atoms+natoms;
    natoms++;
    get_xbgf_fields(line, atom->name, atom->resname, atom->chain, atom->segid, 
                   &atom->occupancy, &atom->bfactor, &atom->atomicnumber, 
                   &atom->resid, atom->type, &atom->charge, 
                   NULL, NULL, NULL, atom->insertion);
  } while (strncmp(line, "END", 3));

  bgf->natoms = natoms;

  return MOLFILE_SUCCESS;
}


// Read atom coordinates
static int read_xbgf_timestep(void *v, int natoms, molfile_timestep_t *ts) {
  xbgfdata *bgf = (xbgfdata *)v;
  char line[LINESIZE];
  int i;
  float x, y, z;

  // Since the file is rewound when coordinates are read, EOF shouldn't
  // happen. Instead, use a flag to indicate that the single timestep has
  // been read
  if (bgf->coords_read) {
    return MOLFILE_EOF;
  }

  // Find and read the ATOM record
  rewind(bgf->file);
  do {
    fgets(line, LINESIZE, bgf->file);
    if ( ferror(bgf->file) || feof(bgf->file) ) {
      printf("xbgfplugin) No FORMAT ATOM record found in file.\n");
      return MOLFILE_ERROR;
    }
  } while ( strncmp(line, "FORMAT ATOM", 11) );

  // Read the atoms
  for (i = 0; i < bgf->natoms; i++) {
    fgets(line, LINESIZE, bgf->file);
    if ( ferror(bgf->file) || feof(bgf->file) ) {
      printf("xbgfplugin) Error occurred reading atom coordinates.\n");
      return MOLFILE_ERROR;
    }

    // skip comments and blank lines
    if (strncmp(line,"ATOM",4)!=0 && strncmp(line,"HETATM",6)!=0) continue;

    get_xbgf_coordinates(line, &x, &y, &z);

    if (ts) {
      ts->coords[3*i  ] = x;
      ts->coords[3*i+1] = y;
      ts->coords[3*i+2] = z;
    }
  }

  bgf->coords_read = 1;
  return MOLFILE_SUCCESS;
}


static void *open_xbgf_write(const char *filename, const char *filetype, 
                           int natoms) {
  FILE *fd;
  xbgfdata *data;

  if ((fd = fopen(filename, "w")) == NULL) { 
    printf("xbgfplugin) Error, unable to open xbgf file %s for writing\n",
            filename);
    return NULL;
  }
  
  data = (xbgfdata *) malloc(sizeof(xbgfdata));
  memset(data, 0, sizeof(xbgfdata));
  data->natoms = natoms;
  data->file = fd;
  data->nbonds = 0;
  return data;
}


static int write_xbgf_structure(void *mydata, int optflags, 
                               const molfile_atom_t *atoms) {
  fflush(stdout);
  xbgfdata *data = (xbgfdata *)mydata;
  data->atomlist = (molfile_atom_t *)malloc(data->natoms*sizeof(molfile_atom_t));
  memcpy(data->atomlist, atoms, data->natoms*sizeof(molfile_atom_t));
  return MOLFILE_SUCCESS;
}


static int read_xbgf_bonds_aux(void *v, int *nbonds, int **fromptr, int **toptr, float **bondorderptr) {
  xbgfdata *bgf = (xbgfdata *)v;
  char line[LINESIZE]; 
  char nextline[LINESIZE]; 
  if (bgf->nbonds == 0) {
    *nbonds = 0;
    *fromptr = NULL;
    *toptr = NULL;
    *bondorderptr = NULL;
    return MOLFILE_SUCCESS;
  }

  // Find and read the BOND record
  rewind(bgf->file);
  do {
    fgets(line, LINESIZE, bgf->file);
    if ( ferror(bgf->file) || feof(bgf->file) ) {
      printf("xbgfplugin) No bond record found in file.\n");
      return MOLFILE_ERROR;
    }
  } while ( strncmp(line, "FORMAT CONECT", 13) != 0 );

  // Read the bonds
  int j; //From atom
  int k; //To atom
  bool conline=false; //true if line after the conect line is an order line
  char currbond[7]="xxxxxx"; //Stores current bond field
  char currcon[7]="xxxxxx"; //Stores current ORDER field
  char* bondptr; //pointer to current position in bond line
  char* conptr; //pointer to current position in order line
  int bonds[MAXBONDS]; //Stores bonds of current atom
  float orders[MAXBONDS]; //Stores bond orders of current atom
  int numbonds; //Stores number of bonds of current atom
  int numords; //Stores number of bond order records of current atom
  float bo; //current bond order
  int i=0; //Number of the current bond
  int numfields=0; //number of fields in the current line
  fgets(line, LINESIZE, bgf->file);
  while (1) {
    // bondptr=NULL;
    //conptr=NULL;
    conline=false;
    if (strncmp(line,"END",3)==0)
      break;

    fgets(nextline, LINESIZE, bgf->file);
    if ( ferror(bgf->file) || feof(bgf->file) ) {
      printf("xbgfplugin) Error occurred reading bond record.\n");
      return MOLFILE_ERROR;
    }

    if (strncmp(nextline,"ORDER",5)==0) 
      conline=true;

    if (strncmp(line,"CONECT",6)==0) {
      numfields=(strlen(line)-1)/6;
      bondptr=&line[0];
      numfields--;
      bondptr += 6;
      numbonds=0;
      numords=0;
      strncpy(currbond,bondptr,6);
      j=atoi(currbond);
      numfields--;
      bondptr += 6;

      while ((numfields > 0) && (numbonds <= MAXBONDS)) {
        strncpy(currbond,bondptr,6);
        numfields--;
        bondptr += 6;
        bonds[numbonds]=atoi(currbond);
        numbonds++;
      }

      if (conline) {
        numfields=(strlen(line)-1)/6;
        conptr=&nextline[0];
        numfields -= 2;
        conptr += 12;
        numords=0;
        while ((numfields > 0) && (numords < numbonds)) {
          strncpy(currcon,conptr,6);
          numfields--;
          conptr+=6;
          bo=atof(currcon);
          orders[numords]=bo;
          numords++;
        }
      }

      for (int l=0;l<numbonds;l++) {
        k=bonds[l];
        if (j<k) {
          bgf->from[i]=j;
          bgf->to[i]=k;
          if (conline) {
            bgf->bondorder[i]=orders[l];
          } else {
            bgf->bondorder[i]=1.0;
          }
          i++;
        }
      }
        
      if (conline) {
        fgets(line, LINESIZE, bgf->file);
      } else {
        strncpy(line,nextline,LINESIZE);
      }
    } else {
      strncpy(line,nextline,LINESIZE);
    }
  }

  *nbonds = i;
  *fromptr = bgf->from;
  *toptr = bgf->to;
  *bondorderptr = bgf->bondorder; // not implemented yet

  return MOLFILE_SUCCESS;
}


static int read_xbgf_bonds(void *v, int *nbonds, int **fromptr, int **toptr, 
                           float **bondorderptr, int **bondtype,
                           int *nbondtypes, char ***bondtypename) {
  xbgfdata *bgf = (xbgfdata *)v;

  /* now read bond data */
  *nbonds=bgf->nbonds;
  if (bgf->nbonds > 0) {
    bgf->from = (int *) malloc(*nbonds*sizeof(int));
    bgf->to = (int *) malloc(*nbonds*sizeof(int));
    bgf->bondorder = (float *) malloc(*nbonds*sizeof(float));

    if ((read_xbgf_bonds_aux(bgf, nbonds, &(bgf->from), &(bgf->to), &(bgf->bondorder))) != MOLFILE_SUCCESS) {
      fclose(bgf->file);
      bgf->file = NULL;
      return MOLFILE_ERROR;
    }
    *fromptr = bgf->from;
    *toptr = bgf->to;
    *bondorderptr = bgf->bondorder; // not implemented yet
    *bondtype = NULL;
    *nbondtypes = 0;
    *bondtypename = NULL;
  } else {
    printf("xbgfplugin) WARNING: no bonds defined in xbgf file.\n");
    *fromptr = NULL;
    *toptr = NULL;
    *bondorderptr = NULL;
    *bondtype = NULL;
    *nbondtypes = 0;
    *bondtypename = NULL;
  }
  return MOLFILE_SUCCESS;
}


static int write_xbgf_timestep(void *mydata, const molfile_timestep_t *ts) {
  fflush(stdout);
  xbgfdata *data = (xbgfdata *)mydata; 
  const molfile_atom_t *atom;
  const float *pos;
  int i;

  //print header block
  fprintf(data->file, "BIOGRF  332\n");
  fprintf(data->file, "REMARK NATOM %4i\n", data->natoms);
  fprintf(data->file, "FORCEFIELD DREIDING\n");
  fprintf(data->file, "FORMAT ATOM   (a6,1x,i6,1x,a5,1x,a4,1x,a1,1x,i5,3f10.5,1x,a5,i3,i2,1x,f8.5,1x,f6.3,1x,f6.3,1x,i3,1x,a4)\n");

  //print atoms block
  atom = data->atomlist;
  pos = ts->coords;
  int numbonds=0;
  int lp=0;
  for (i = 0; i < data->natoms; i++) {
    fprintf(data->file, "%-6s %6i %5s %4s %1s %5i%10.5f%10.5f%10.5f %-5s%3i%2i %8.5f %6.3f %6.3f %3i %4s\n", "ATOM", i+1, atom->name, atom->resname, atom->chain, atom->resid, pos[0], pos[1], pos[2], atom->type, numbonds, lp, atom->charge, atom->bfactor, atom->occupancy, atom->atomicnumber, atom->segid);
    ++atom; 
    pos += 3;
  }

  //write the connectivity data
  fprintf(data->file,"FORMAT CONECT (a6,14i6) \nFORMAT ORDER (a6,i6,13f6.3)\n");
    
  //iterate through the bond arrays and write them all
  int* bonds=(int *)malloc((data->natoms+1) * sizeof(int) * MAXBONDS);
  float* orders=(float *)malloc((data->natoms+1)*sizeof(float) * MAXBONDS);
  int* numcons=(int *)malloc((data->natoms+1)*sizeof(int));
  for (i=0;i<data->natoms+1;i++) {
    numcons[i]=0;
  }

  int j,k; //indices for atoms being bonded
  float o; //bond order
  for (i=0;i<data->nbonds;i++) {
    j=data->from[i];
    k=data->to[i];

    if (data->bondorder != NULL)
      o=data->bondorder[i];
    else 
      o=1.0f;

    numcons[j]++;
    numcons[k]++;
    if (numcons[j]>MAXBONDS) {
      printf("xbgfplugin) Warning: Bond overflow. Not all bonds were written\n");
      numcons[j]--;
      numcons[k]--;
      continue;
    }
       
    if (numcons[k]>MAXBONDS) {
      printf("xbgfplugin) Warning: Bond overflow. Not all bonds were written\n");
      numcons[k]--;
      numcons[j]--;
      continue;
    }
    bonds[6*j+numcons[j]-1]=k;
    bonds[6*k+numcons[k]-1]=j;
    orders[6*j+numcons[j]-1]=o;
    orders[6*k+numcons[k]-1]=o;
  }

  for (i=1;i<=data->natoms;i++) {
    fprintf(data->file,"CONECT%6i",i);
    for (j=0;j<numcons[i];j++) {
      fprintf(data->file,"%6i",bonds[6*i+j]);
    }
    fprintf(data->file,"\nORDER %6i",i);
    for (j=0;j<numcons[i];j++) {
      fprintf(data->file,"%6.3f",orders[6*i+j]);
    }
    fprintf(data->file,"\n");
  }

  if (bonds != NULL) {
    free(bonds);
    bonds = NULL;
  }
  if (orders != NULL) {
    free(orders);
    orders = NULL;
  }
  if (numcons != NULL) {
    free(numcons);
    numcons = NULL;
  }

  fprintf(data->file,"END\n");
  return MOLFILE_SUCCESS;
}

static int write_xbgf_bonds(void *v, int nbonds, int *fromptr, int *toptr, 
                            float *bondorderptr,  int *bondtype, 
                            int nbondtypes, char **bondtypename) {
  xbgfdata *data = (xbgfdata *)v;
  data->from = (int*) malloc (nbonds * sizeof(int));
  data->to = (int*) malloc (nbonds * sizeof(int));
  data->nbonds = nbonds;
  fflush(stdout);


  //set the pointers for use later
  for (int i=0;i<nbonds;i++) {
    data->from[i]=fromptr[i];
    data->to[i]=toptr[i];
  }

  if (bondorderptr != NULL) {
    data->bondorder = (float*) malloc (nbonds * sizeof(float));
    for (int i=0;i<nbonds;i++) {
      data->bondorder[i]=bondorderptr[i];
    }
  }


  return MOLFILE_SUCCESS;
}

static void close_xbgf_write(void *mydata) {
  xbgfdata *data = (xbgfdata *)mydata;
  if (data) {
    fclose(data->file);

    if (data->atomlist != NULL) free(data->atomlist);
    data->atomlist = NULL;
    if (data->from != NULL) free(data->from);
    data->from = NULL;
    if (data->to != NULL) free(data->to);
    data->to = NULL;
    if (data->bondorder != NULL) free(data->bondorder);
    data->bondorder = NULL;
    free(data);
  }
}

//
// Free the memory used by the bgf structure
static void close_xbgf_read(void *v) {
  xbgfdata *bgf = (xbgfdata *)v;
  if (bgf) {
    if (bgf->file) fclose(bgf->file);
    if (bgf->from != NULL) free(bgf->from);
    if (bgf->to != NULL)   free(bgf->to);
    if (bgf->bondorder != NULL) free(bgf->bondorder);

    if (bgf->meta->remarks != NULL)
      free(bgf->meta->remarks);
    if (bgf->meta != NULL) 
      free(bgf->meta);
    free(bgf);
  }
  bgf=NULL;
}


static int read_xbgf_molecule_metadata(void *v, molfile_metadata_t **metadata) {
  xbgfdata *bgf = (xbgfdata *)v; 
  *metadata = bgf->meta;
  return MOLFILE_SUCCESS;
}


static molfile_plugin_t plugin;

VMDPLUGIN_API int VMDPLUGIN_init() {
  memset(&plugin, 0, sizeof(molfile_plugin_t));
  plugin.abiversion = vmdplugin_ABIVERSION;
  plugin.type = MOLFILE_PLUGIN_TYPE;
  plugin.name = "xbgf";
  plugin.prettyname = "Internal Paratool Format";
  plugin.author = "Peter Freddolino ";
  plugin.majorv = 0;
  plugin.minorv = 13;
  plugin.is_reentrant = VMDPLUGIN_THREADSAFE;
  plugin.filename_extension = "xbgf";
  plugin.open_file_read = open_xbgf_read;
  plugin.read_structure = read_xbgf_structure;
  plugin.read_bonds = read_xbgf_bonds;
  plugin.read_next_timestep = read_xbgf_timestep;
  plugin.close_file_read = close_xbgf_read;
  plugin.open_file_write = open_xbgf_write;
  plugin.write_structure = write_xbgf_structure;
  plugin.write_timestep = write_xbgf_timestep;
  plugin.close_file_write = close_xbgf_write;
  plugin.read_molecule_metadata = read_xbgf_molecule_metadata;
  plugin.write_bonds = write_xbgf_bonds;
  return VMDPLUGIN_SUCCESS;
}

VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
  (*cb)(v, (vmdplugin_t *)&plugin);
  return VMDPLUGIN_SUCCESS;
}

VMDPLUGIN_API int VMDPLUGIN_fini() {
  return VMDPLUGIN_SUCCESS;
}