Basic handling

The S4 system is one the OOP systems in R. Its largest use might in the Bioconductor collection of packages for bioinformatics and computational biology.

We use the bioconductor Biobase:

The R package contains constructors for the S4 classes defined. They are simply functions, and can be used as such through rpy2:

The object eset is an R object of type S4:

It has a class as well:

In R, objects attributes are also known as slots. The attribute names can be listed with:

The attributes can also be accessed through the rpy2 property slots. slots is a mapping between attributes names (keys) and their associated R object (values). It can be used as Python dict:

Mapping S4 classes to Python classes

Writing one's own Python class extending rpy2's RS4 is straightforward. That class can be used wrap our eset object

Custom conversion

The conversion system can also be made aware our new class by customizing the handling of S4 objects.

A simple implementation is a factory function that will conditionally wrap the object in our Python class ExpressionSet:

That function can be be register to a Converter:

When using that converter, the matching R objects are returned as instances of our Python class ExpressionSet:

Class attributes

The R attribute assayData can be accessed through the accessor method exprs() in R. We can make it a property in our Python class:

Methods

In R's S4 methods are generic functions served by a multiple dispatch system.

A natural way to expose the S4 method to Python is to use the multipledispatch package:

The R method rowMedians is also defined for matrices, which we can expose on the Python end as well:

While this is working, one can note that we call the same R function rowMedians() in the package Biobase in both Python decorated functions. What is happening is that the dispatch is performed by R.

If this is ever becoming a performance issue, the specific R function dispatched can be prefetched and explicitly called in the Python function. For example: