Tuesday, May 29, 2012

libScopes interfacing in Python

After finishing off an easy task for the biographer project, it was time to move onto more challenging stuff.

At the outset interfacing libScopes with Python seemed straightforward. Just use ctypes and the library will be magically imported into Python. But soon I realised that ctypes works well only with C libraries and not C++. There are ways to get around issues like name mangling by using extern "C",  but then I ran into many problems with classes.

After spending a considerable amount of time on this method I finally decided to abandon ship and choose SWIG over ctypes. SWIG for Python converts your C/C++ code into Python extensions. It does this by generating a lot of wrapper code. Though this may potentially slow down processing, at least SWIG supports C++.

SWIG took quite a while to get used to. SWIG uses .i files to define the Python interfaces to your C++ code. I just %include'd all the C++ headers and thankfully it worked. Just a few issues with some typedefs that had to be redefined. A few modifications also had to be made to the libScopes library, mainly commenting out functions that had been defined in the header file but the function source was missing. Also included definitions in the Makefile to use SWIG to generate the Python Extension. Takes a bit of time to compile.

Phew! The Python library is ready, but what was the point of all this? Well, to run simulations of SBML files, libScopes had to be run on the server. I chose to run it as a module in Python. The code to import the SBML file and run a single iteration(dScopeStep) can be found in the models/scopes.py file in the server repository. An additional controller has been added for the simulator. Also JSON exchange(import/export) code had to be written on the server and client to communicate the states of the boolean network.

There is one small flaw in the method that I've used. The SBML is imported for every single iteration on the server. I could not avoid this as the session variable in web2py can only store objects that can be pickled(can be converted to a string). Apparently SwigPyObject cannot be pickled. The network is stored in a Net class object whose type in Python is SwigPyObject. I'm looking into alternatives like mmap.

A live server can be tested at

 
A bit of lag can be seen at every iteration. This is due to the SBML file being imported every time. Also the reaction nodes are also turning green. 

Quite a long post! Now I have to look into Lian's libSBGN.js code and check how it can be integrated. Also the simulator using libScopes output has to be verified.

No comments: