libpymolfile.i 8.88 KB
Newer Older
Berk Onat's avatar
Berk Onat committed
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
/* -*- C -*-  (not really, but good for syntax highlighting) */
/* SWIG interface for libvmdmolfile of VMD molfile_plugins
   Copyright (c) 2017 Berk Onat <b.onat@warwick.ac.uk>
   Published under BSD LICENSE

   swig -python -outdir pymolfile/lib/ pymolfile/src/libpymolfile.i
*/
%define DOCSTRING
"
:Author:  Berk Onat <b.onat@warwick.ac.uk>
:Year:    2017
:Licence: BSD LICENSE


"
%enddef

%module(docstring=DOCSTRING) libpymolfile


%{
/* Python SWIG interface to libpymolfile
   Copyright (c) 2017 Berk Onat <b.onat@warwick.ac.uk>
   Published under BSD LICENSE
 */
#define SWIG_FILE_WITH_INIT
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "molfile_plugin.h"
#include "libmolfile_plugin.h"
#include "vmdplugin.h"
#include "pymolfile.h"
%}

%include "numpy.i"

%init %{
import_array();
%}


/* 
  Wrapping only high-level plugin functions to register VMD 
  plugins and to retrive the data through molfile_plugin interface.

  Only modifing call signatures. This will help one to access functions 
  without dealing with pointers from python.
*/


/* plugin status codes for error handling */
enum { pluginOK, pluginNOINIT, pluginCLOSE, pluginNOMEM, 
       pluginENDOFFILE, pluginFILENOTFOUND, pluginFORMATERROR };

/* pymolfile.c 
   initialize and finalize molfile plugins
*/
%feature("autodoc", "0") molfile_plugin_list;
extern molfile_plugin_t** molfile_plugin_list(int maxsize);

%feature("autodoc", "0") molfile_init;
extern int molfile_init(void);

%feature("autodoc", "0") molfile_finish;
extern int molfile_finish(void);

%feature("autodoc", "0") get_plugin;
extern molfile_plugin_t* get_plugin(molfile_plugin_t** plugin_list, int plugin_no);

%feature("autodoc", "0") molfile_plugin_info;
%exception molfile_plugin_info {
  $action
  if (PyErr_Occurred()) SWIG_fail;
}
%inline %{
PyObject * molfile_plugin_info(molfile_plugin_t** plugin_list, int plugin_no) {
    molfile_plugin_t *plugin;
    int *plugno = &plugin_no;
    int has_readstructure = 0;
    int has_readbonds = 0;
    int has_readangles = 0;
    int has_writestructure = 0;
    int has_writebonds = 0;
    int has_writeangles = 0;
    int has_readnexttimestep = 0;
    int has_writetimestep = 0;
    int plugin_list_size = sizeof(plugin_list) / sizeof(molfile_plugin_t**);
    if (plugno==NULL || plugin_no<0){
      PyErr_Format(PyExc_IOError, "[%d] Error: molfile plugin handle no should be given, be positive value and should not exceed the list length'%d'. You set '%d'", pluginNOINIT, plugin_list_size, plugin_no);
      return 0;
    }
    plugin = plugin_list[plugin_no];
    if(plugin==NULL || !plugin->open_file_read){
      PyErr_Format(PyExc_IOError, "[%d] Error: molfile plugin '%d' is not initialized.", pluginNOINIT, plugin_no);
      return 0;
    }
    if (plugin->read_structure) has_readstructure = 1;
    if (plugin->read_bonds) has_readbonds = 1;
    if (plugin->read_angles) has_readangles = 1;
    if (plugin->read_next_timestep) has_readnexttimestep = 1;
    if (plugin->write_structure) has_writestructure = 1;
    if (plugin->write_bonds) has_writebonds = 1;
    if (plugin->write_angles) has_writeangles = 1;
    if (plugin->write_timestep) has_writetimestep = 1;
    PyObject *tuple = PyTuple_New(17);
    PyTuple_SET_ITEM(tuple, 0, PyString_FromString(plugin->filename_extension));
    PyTuple_SET_ITEM(tuple, 1, PyString_FromString(plugin->name));
    PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong((long)has_readstructure));
    PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong((long)has_readbonds));
    PyTuple_SET_ITEM(tuple, 4, PyInt_FromLong((long)has_readangles));
    PyTuple_SET_ITEM(tuple, 5, PyInt_FromLong((long)has_readnexttimestep));
    PyTuple_SET_ITEM(tuple, 6, PyInt_FromLong((long)has_writestructure));
    PyTuple_SET_ITEM(tuple, 7, PyInt_FromLong((long)has_writebonds));
    PyTuple_SET_ITEM(tuple, 8, PyInt_FromLong((long)has_writeangles));
    PyTuple_SET_ITEM(tuple, 9, PyInt_FromLong((long)has_writetimestep));
    PyTuple_SET_ITEM(tuple, 10, PyString_FromString(plugin->prettyname));
    PyTuple_SET_ITEM(tuple, 11, PyString_FromString(plugin->type));
    PyTuple_SET_ITEM(tuple, 12, PyString_FromString(plugin->author));
    PyTuple_SET_ITEM(tuple, 13, PyInt_FromLong((long)plugin->majorv));
    PyTuple_SET_ITEM(tuple, 14, PyInt_FromLong((long)plugin->minorv));
    PyTuple_SET_ITEM(tuple, 15, PyInt_FromLong((long)plugin->abiversion));
    PyTuple_SET_ITEM(tuple, 16, PyInt_FromLong((long)plugin->is_reentrant));
    return tuple;
  }
%}

/*
%feature("autodoc", "0") molfile_open;
%exception molfile_open {
  $action
  if (PyErr_Occurred()) SWIG_fail;
}
%inline %{
PyObject * molfile_open(molfile_plugin_t* plugin, ) {
    int *plugno = &plugin_no;
    return plugin;
  }
%}
*/

/* 
   python wrappers for functions 
   and structure data in  molfile_plugin.h
*/

/* molfile_atom_t

  char name[16];      required atom name string             
  char type[16];      required atom type string             
  char resname[8];    required residue name string          
  int resid;          required integer residue ID           
  char segid[8];      required segment name string, or ""   
  char chain[2];      required chain name, or ""            
  char altloc[2];     optional PDB alternate location code  
  char insertion[2];  optional PDB insertion code           
  float occupancy;    optional occupancy value              
  float bfactor;      optional B-factor value               
  float mass;         optional mass value                   
  float charge;       optional charge value                 
  float radius;       optional radius value                 
  int atomicnumber;   optional element atomic number        
  int ctnumber;       mae ct block, 0-based, including meta 
*/


/* molfile_plugin_t */


/*
  void *(* open_file_read)(const char *filepath, const char *filetype, 
      int *natoms);
  void (* close_file_read)(void *);
  int (*read_structure)(void *, int *optflags, molfile_atom_t *atoms);
  int (*read_bonds)(void *, int *nbonds, int **from, int **to, float **bondorder, 
                    int **bondtype, int *nbondtypes, char ***bondtypename);
  int (* read_next_timestep)(void *, int natoms, molfile_timestep_t *);
  void *(* open_file_write)(const char *filepath, const char *filetype, 
      int natoms);
  int (* write_structure)(void *, int optflags, const molfile_atom_t *atoms);
  int (* write_timestep)(void *, const molfile_timestep_t *);
  void (* close_file_write)(void *);
  int (* read_molecule_metadata)(void *, molfile_metadata_t **metadata);
  int (* write_bonds)(void *, int nbonds, int *from, int *to, float *bondorder, 
                     int *bondtype, int nbondtypes, char **bondtypename);
  int (* read_angles)(void *handle, int *numangles, int **angles, int **angletypes,
                      int *numangletypes, char ***angletypenames, int *numdihedrals,
                      int **dihedrals, int **dihedraltypes, int *numdihedraltypes,
                      char ***dihedraltypenames, int *numimpropers, int **impropers,        
                      int **impropertypes, int *numimpropertypes, char ***impropertypenames,
                      int *numcterms, int **cterms, int *ctermcols, int *ctermrows);
  int (* write_angles)(void *handle, int numangles, const int *angles, const int *angletypes,
                       int numangletypes, const char **angletypenames, int numdihedrals,
                       const int *dihedrals, const int *dihedraltypes, int numdihedraltypes,
                       const char **dihedraltypenames, int numimpropers, 
                       const int *impropers, const int *impropertypes, int numimpropertypes,
                       const char **impropertypenames, int numcterms,  const int *cterms, 
                       int ctermcols, int ctermrows);
  int (* read_timestep)(void *, int natoms, molfile_timestep_t *,
                        molfile_qm_metadata_t *, molfile_qm_timestep_t *);
  int (* read_timestep_metadata)(void *, molfile_timestep_metadata_t *);
  int (* read_timestep2)(void *, molfile_ssize_t index, molfile_timestep_t *);
  molfile_ssize_t (* read_times)( void *,
                                  molfile_ssize_t start,
                                  molfile_ssize_t count,
                                  double * times );
  int (* cons_fputs)(const int, const char*);
*/


/* molfile_timestep_t

  float *coords;         coordinates of all atoms, arranged xyzxyzxyz   
  float *velocities;     space for velocities of all atoms; same layout 
  float A, B, C, alpha, beta, gamma; 
  double physical_time;  physical time point associated with this frame 

  double total_energy;
  double potential_energy;
  double kinetic_energy;
  double extended_energy;
  double force_energy;
  double total_pressure;
*/


/*  molfile_timestep_metadata_t

    unsigned int count;                   total # timesteps; -1 if unknown 
    unsigned int avg_bytes_per_timestep;  bytes per timestep                
    int has_velocities;                   if timesteps have velocities     
*/