{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook shows how to use R BoolNet package from python notebooks to generate random Boolean networks, and load them either with `biolqm` or with `minibn`.\n", "\n", "Two different methods are used: the first with a pure python approach; the second using Jupyter to mix both python and R code.\n", "\n", "The documentation for BoolNet is available here: https://cran.r-project.org/web/packages/BoolNet/BoolNet.pdf" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import biolqm\n", "from colomoto import minibn" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Method 1: pure python interface\n", "\n", "We use `rpy2` to provide an interface to the `BoolNet` R package:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from rpy2.robjects.packages import importr\n", "boolnet = importr(\"BoolNet\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`boolnet` is a python object offering a bridge to the R package.\n", "Then, BoolNet functions can be directly invoked as if `boolnet` was a python module:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "net = boolnet.generateRandomNKNetwork(10, 10, topology=\"scale_free\", gamma=2, \n", " linkage=\"uniform\", functionGeneration=\"uniform\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We save the network to a temporary file:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/tmp/random.bnet'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "netfile = \"/tmp/random.bnet\"\n", "boolnet.saveNetwork(net, netfile)\n", "netfile" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can directly load it with `minibn` and show its influence graph:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "# computing graph layout...\n" ] }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "%3\n", "\n", "\n", "Gene1\n", "\n", "Gene1\n", "\n", "\n", "Gene1->Gene1\n", "\n", "\n", "-\n", "\n", "\n", "Gene2\n", "\n", "Gene2\n", "\n", "\n", "Gene1->Gene2\n", "\n", "\n", "-\n", "\n", "\n", "Gene7\n", "\n", "Gene7\n", "\n", "\n", "Gene1->Gene7\n", "\n", "\n", "+\n", "\n", "\n", "Gene6\n", "\n", "Gene6\n", "\n", "\n", "Gene2->Gene6\n", "\n", "\n", "+\n", "\n", "\n", "Gene9\n", "\n", "Gene9\n", "\n", "\n", "Gene2->Gene9\n", "\n", "\n", "+\n", "\n", "\n", "Gene3\n", "\n", "Gene3\n", "\n", "\n", "Gene3->Gene3\n", "\n", "\n", "+\n", "\n", "\n", "Gene4\n", "\n", "Gene4\n", "\n", "\n", "Gene5\n", "\n", "Gene5\n", "\n", "\n", "Gene5->Gene2\n", "\n", "\n", "-\n", "\n", "\n", "Gene10\n", "\n", "Gene10\n", "\n", "\n", "Gene7->Gene10\n", "\n", "\n", "-\n", "\n", "\n", "Gene8\n", "\n", "Gene8\n", "\n", "\n", "Gene8->Gene7\n", "\n", "\n", "-\n", "\n", "\n", "Gene9->Gene4\n", "\n", "\n", "-\n", "\n", "\n", "Gene10->Gene5\n", "\n", "\n", "+\n", "\n", "\n", "Gene10->Gene8\n", "\n", "\n", "-\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bn = minibn.BooleanNetwork.load(netfile)\n", "bn.influence_graph()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "... or with `biolqm` for further processing and translation to other analysis tools:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "JavaObject id=o19" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lqm = biolqm.load(netfile)\n", "lqm" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Method 2: Mixing python and R in Jupyter notebooks\n", "\n", "We first load the R extension offered by `rpy2`." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "%load_ext rpy2.ipython" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "R commands should be prefixed with `%R`.\n", "\n", "First, let us load the `BoolNet` package:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1], dtype=int32)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%R require(BoolNet)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Multiline R code can be written in cells starting with `%%R` line, with optional input and output variables to translate to/from python:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "%%R -o netfiles\n", "n <- generateRandomNKNetwork(n=10, k=10, topology=\"scale_free\",\n", " linkage=\"uniform\", functionGeneration=\"uniform\")\n", "netfiles <- \"/tmp/rn.bnet\"\n", "saveNetwork(n, netfiles)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then, similarly to the first method:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "bn = minibn.BooleanNetwork.load(netfiles[0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and so on." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }