
Bioinformatics with Python Cookbook
By :

You have probably heard of, and maybe used, the IPython Notebook. If not, then I strongly recommend you try it as it's becoming the standard for reproducible science. Among many other features, IPython provides a framework of extensible commands called magics, which allows you to extend the language in many useful ways.
There are magic functions to deal with R. As you will see in our example, it makes R interfacing much more declarative and easy. This recipe will not introduce any new R functionalities, but hopefully, it will make clear how IPython can be an important productivity boost for scientific computing in this regard.
You will need to follow the previous getting ready steps of the rpy2 recipe. You will also need IPython. You can use the standard command line or any of the IPython consoles, but the recommended environment is the notebook.
If you are using our notebooks, open the 00_Intro/R_magic.ipynb
notebook. A notebook is more complete than the recipe presented here with more chart examples. For brevity here, we concentrate only on the fundamental constructs to interact with R using magics.
This recipe is an aggressive simplification of the previous one because it illustrates the conciseness and elegance of R magics:
import rpy2.robjects.lib.ggplot2 as ggplot2 %load_ext rpy2.ipython
%
starts an IPython-specific directive.%R print(c(1, 2))
robjects
package. Actually, rpy2 is being used to look under the hood, but it has been made transparent.sequence.index
file that was downloaded in the previous recipe:%%R seq.data <- read.delim('sequence.index', header=TRUE, stringsAsFactors=FALSE) seq.data$READ_COUNT <- as.integer(seq.data$READ_COUNT) seq.data$BASE_COUNT <- as.integer(seq.data$BASE_COUNT)
%%
). As you can see, there is no need for a function parameter name translation or (alternatively) explicitly call the robjects.r
to execute a code.seq_data = %R seq.data
%R -i seq_data %R print(colnames(seq_data))
-i
argument informs the magic system that the variable that follows on the Python space is to be copied in the R namespace. The second line just shows that the data frame is indeed available in R. We actually did not do anything with the data frame in the Python namespace, but this serves as an example on how to inject an object back into R..png
and dev.off
R functions, as the magic system will take care of this for you. When you tell R to print a chart, it will magically appear in your notebook or graphical console. For example, the histogram plotting code from the previous recipe is now simply:%%R bar <- ggplot(seq_data) + aes(factor(CENTER_NAME)) + geom_bar() + theme(axis.text.x = element_text(angle = 90, hjust = 1)) print(bar)
R magics makes interaction with R particularly easy. This is true if you think about how cumbersome multiple language integration tends to be.
The notebook has a few more examples, especially with chart printing, but the core of R-magic interaction is explained before.
Downloading the example code
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Change the font size
Change margin width
Change background colour