Before integrating to Biopython, some unit testing was required, to detect possible errors and make sure future changes that break functionality won't go unnoticed.

+

+

For every Python package, it was created a "tests" directory which contains the unit tests created for each module. Here is one example of the tests created for the framework package:[http://mocapy.svn.sourceforge.net/viewvc/mocapy/branches/gSoC11/python/mocapy/framework/tests/].

+

+

While testing the code, a few issues were detected:

+

* '''Object ownership:'''

+

When passing an object created on the C++ side to a method that takes a pointer as an argument, one should be careful about the life time of that object.

+

+

For example, the set_random_gen method takes a pointer to a RandomGen object. The following code works just fine.

+

+

<python>

+

random_gen = RandomGen()

+

node.set_random_gen(random_gen=random_gen)

+

</python>

+

+

But if instead of doing that, we do the following:

+

<python>

+

node.set_random_gen(random_gen=RandomGen())

+

</python>

+

The reference count has not been incremented and therefore the object can be destroyed.

+

+

The way to solve the problem is to make sure the C++ object is held by auto_ptr:

Pointers returned via manage_new_object will also be held by auto_ptr, so this transfer-of-ownership will also work correctly.

+

For further details, see [http://www.boost.org/doc/libs/1_46_0/libs/python/doc/v2/faq.html#ownership How can I wrap a function which needs to take ownership of a raw pointer?]

+

+

*''' Translation from numpy.array to a float mdarray'''

Revision as of 19:19, 5 July 2011

Mocapy++ is a machine learning toolkit for training and using Bayesian networks. It has been used to develop probabilistic models of biomolecular structures. The goal of this project is to develop a Python interface to Mocapy++ and integrate it with Biopython. This will allow the training of a probabilistic model using data extracted from a database. The integration of Mocapy++ with Biopython will provide a strong support for the field of protein structure prediction, design and simulation.

Introduction

Discovering the structure of biomolecules is one of the biggest problems in biology. Given an amino acid or base sequence, what is the three dimensional structure?
One approach to biomolecular structure prediction is the construction of probabilistic models. A Bayesian network is a probabilistic model composed of a set of variables and their joint probability distribution, represented as a directed acyclic graph. A dynamic Bayesian network is a Bayesian network that represents sequences of variables. These sequences can be time-series or sequences of symbols, such as protein sequences.
Directional statistics is concerned mainly with observations which are unit vectors in the plane or in three-dimensional space. The sample space is typically a circle or a sphere. There must be special directional methods which take into account the structure of the sample spaces.
The union of graphical models and directional statistics allows the development of probabilistic models of biomolecular structures. Through the use of dynamic Bayesian networks with directional output it becomes possible to construct a joint probability distribution over sequence and structure. Biomolecular structures can be represented in a geometrically natural, continuous space.
Mocapy++ is an open source toolkit for inference and learning using dynamic Bayesian networks that provides support for directional statistics. Mocapy++ is excellent for constructing probabilistic models of biomolecular structures; it has been used to develop models of protein and RNA structure in atomic detail. Mocapy++ is used in several high-impact publications, and will form the core of the molecular modeling package Phaistos, which will be released soon.
The goal of this project is to develop a highly useful Python interface to Mocapy++, and to integrate that interface with the Biopython project. Through the Bio.PDB module, Biopython provides excellent functionality for data mining biomolecular structure databases. Integrating Mocapy++ and Biopython will allow training a probabilistic model using data extracted from a database.
Integrating Mocapy++ with Biopython will create a powerful toolkit for researchers to quickly implement and test new ideas, try a variety of approaches and refine their methods. It will provide strong support for the field of biomolecular structure prediction, design, and simulation.

Project Schedule

Work Plan

Build the theoretical background on the algorithms used in Mocapy++, such as parameter learning of Bayesian networks using Stochastic Expectation Maximization (SEM).

Study Mocapy++'s use cases

Read several papers and attempt to replicate part of the experiments described using Mocapy++.

Get a better understanding of biological sequence analysis done through probabilistic models of proteins and nucleic acids.

Work with Mocapy++

Understand Mocapy++'s internal architecture and algorithms by exploring its source code and running its test cases.

Research other applications of Mocapy++ in Bioinformatics.

Design Mocapy++'s Python interface

Explore the source code of Biopython to understand its design and implementation. The Mocapy++ interface to be included in Biopython must be made compatible with the methods of solving problems in Biopython.

Design a Python interface for Mocapy++, based on its data structures and algorithms. Examine Mocapy++'s use cases and existing test cases to provide guidance for the interface design.

Formulate probabilistic models using Python-Mocapy++. Apply the models to solve biological problems. Examples of problems that can be solved using dynamic Bayesian networks include deciding if a pair of sequences is evolutionarily related, finding sequences which are homologous to a known evolutionary family and predicting RNA secondary structure.

Timeline

Before April 25

Get involved with the Biopython project and community.

Learn the statistical methods used in Mocapy++ and relevant concepts in structural biology.

Community bonding period

April 25 - May 22 (4 weeks)

Familiarize myself with Mocapy++'s functionality and architecture.

Configure a development environment and explore the Mocapy++ and Bio.PDB source code more thoroughly. Document the code during exploration.

Study Mocapy++ and Bio.PDB test cases in order to understand the Mocapy++ interface requirements.

Compare options to create python bindings to C++ code (Boost Python, Cython, Swig). Perform the necessary assesments and gathering of requirements to determine the best library to create the Python bindings.

Try fixing bugs in Biopython in order to get familiar with the development cycle.

Begin of coding phase

May 23 - June 5 (2 weeks)

Design and implement test cases for the Mocapy++ Python interface. The tests must include data structures such as the Multi-Dimentional array; and statistic models such as the hidden Markov Models, a special case of dynamic Bayesian networks.

Identify functions and data structures which impose challenges for the creation of Python bindings.

Implement Python bindings for the core functions and data structures, especially the ones which are straightforward to wrap.

June 6 - June 19 (2 weeks)

Implement Python bindings for the remaining Mocapy++ functionality that composes its interface.

Run and improve the previously designed test cases. Make sure the newly designed interface meets the requirements of the use cases.

Do performance analysis (code profiling) in order to make sure the Python interface of Mocapy++ is fast enough to be usable.

Try other options to create Python bindings to C++ code, in case the implementation doesn't meet the speed requirements.

June 20 - July 3 (2 weeks)

Integrate the Mocapy++ Python interface with Biopython.

Implement integration tests for Mocapy++ Biopython.

Test the operation of each module of the modified source code.

June 4 - July 10 (1 week)

Make further changes in the code to improve robustness and functionality.

Gather, organize, and improve the documentation written during the previous weeks.

Project Code

Project Progress

Options to create Python bindings to C++ code

Swig

There is already an effort to provide bindings for Mocapy++ using Swig. However, Swig is not the best option if performance is to be required.
The Sage project aims at providing an open source alternative to Mathematica or Maple. Cython was developed in conjunction with Sage (it is an independent project, though), thus it is based on Sage's requirements. They tried Swig, but declined it for performance issues. According to the Sage programming guide "The idea was to write code in C++ for SAGE that needed to be fast, then wrap it in SWIG. This ground to a halt, because the result was not sufficiently fast. First, there is overhead when writing code in C++ in the first place. Second, SWIG generates several layers of code between Python and the code that does the actual work". This was written back in 2004, but it seems things didn't evolve much.
The only reason I would consider Swig is for future including Mocapy++ bindings on BioJava and BioRuby projects.

Boost Python

Boost Python is comprehensive and well accepted by the Python community. I would go for it for its extensive use and testing. I would decline it for being hard to debug and having a complicated building system. I don't think it would be worth including a boost dependency just for the sake of creating the Python bindings, but since Mocapy++ already depends on Boost, using it becomes a more attractive option. In my personal experience, Boost Python is very mature and there are no limitations on what one can do with it. When it comes to performance, Cython still overcomes it. Have a look at the Cython C++ wrapping benchmarks and check the timings of Cython against Boost Python. There are also previous benchmarks comparing Swig and Boost Python.

Cython

It is incredibly faster than other options to create python bindings to C++ code, according to several benchmarks available on the web. Check the Simple benchmark between Cython and Boost.Python. It is also very clean and simple, yet powerful. Python's doc on porting extension modules mentions cython: "If you are writing a new extension module, you might consider Cython."
Cython has now support for efficient interaction with numpy arrays. it is a young, but developing language and I would definitely give it a try for its leanness and speed.

Since Boost is well supported and Mocapy++ already relies on it, we decided to use Boost.Python for the bindings.

Implemented the bindings to provide a minimum subset of functionality, in order to run the implemented examples.

Compared the performance of C++ and Python versions.

Mocapy++’s interface remained unchanged, so the tests look similar to the ones in Mocapy/examples.

In the prototype the bindings were all implemented in a single module. For the actual implementation, we could mirror the src packages structure, having separated bindings for each package such as discrete, inference, etc.

It was possible to implement all the functionality required to run the examples. It was not possible to use the vector_indexing_suite when creating bindings for vectors of MDArrays. A few operators (in the MDArray) must be implemented in order to export indexable C++ containers to Python.

Two Mocapy++ examples that use discrete nodes were implemented in Python. There was no problem in exposing Mocapy’s data structures and algorithms. The performance of the Python version is very close to the original Mocapy++.

Bindings Implementation

May 23 - Jun 5: Bindings for the core functions and data structures

Data structures

Mocapy uses an internal data structure to represent arrays: MDArray. In order to make it easier for the user to interact with Mocapy's API, it was decided to provide an interface that accepts numpy arrays. Therefore, it was necessary to implement a translation between a numpy array and an MDArray.

The translation from MDArray to python was done through the use of Boost.Python to_python_converter. We've implemented a template method convert_MDArray_to_numpy_array, which converts an MDArray of any basic type to a corresponding numpy array. In order to perform the translation the original array's shape and internal data are copied into a new numpy array.

The numpy array was created using the Numpy Array API. The creation of a new PyArrayObject using existing data (PyArray_SimpleNewFromData) doesn't copy the array data, it just stores a pointer to it. Thus, one can only free the data when there is no reference to the object. This was done through the use of a Capsule. Besides encapsulating the data, the capsule also stores a destructor to be used when the array is destroyed. The PyArrayObject has a field named "base" which points to the capsule.

The translation from Python to C++, i.e. creating an MDArray from a numpy array is slightly more complex. Boost.Python will provide a
chunk of memory into which the new C++ object must constructed in-place. See the How to write boost.python converters article for more details.

A translation between std::vector of basic types (double, int...) and Python list was also implemented. For std::vector of custom types, such as Node, the translation to a Python list was not performed. If done the same ways as for basic types, a type error is raised: "TypeError: No to_python (by-value) converter found for C++ type". When using vector_indexing_suite this problem was already solved. See Wrapping std::vector<AbstractClass*>. The only inconvenience of using the vector_indexing_suite is creating new types such as vector_Node, instead of using a standard Python list.

The mocapy Python packages follow Mocapy's current source tree. For each package, a shared library with the bindings was created. This makes compilation faster and debug easier. Also, if a single library was created it wouldn't be possible to define packages.

Each of the libraries is called libmocapy_<nameofthepackage>. For example, libmocapy_gaussian provides bindings for the gaussian nodes and probability distributions. The libmocapy_data_structures is used by other libraries and, therefore, must be imported first. This is done on the Python side. Each of the libmocapy_* libraries is imported in the corresponding package. See Creating Packages.

The mdarray is created in python using a numpy.array that is translated to c++ using custom converters. The custom converters are registered in the global Boost.Python registry near the top of the module initialization function. Once flow control has passed through the registration code the automatic conversions from and to Python.

Because of this automatic conversions, it was necessary to create wrappers for functions which take pointers as arguments and change the functions which take references, to get const references. Because Mocapy++ is not const correct, changes are needed to use the const references properly. While the changes are being done, some const_cast have been used. When using const_cast one must be aware it is not always safe.

The call policies were also reviewed. When using an incorrect return value policy, you won't get a compile error, but your code will crash at runtime.