Skip to content

Instantly share code, notes, and snippets.

@pcm32
Created May 24, 2020 19:48
Show Gist options
  • Save pcm32/c760cb5d7380a23e62c4ddf933f519c4 to your computer and use it in GitHub Desktop.
Save pcm32/c760cb5d7380a23e62c4ddf933f519c4 to your computer and use it in GitHub Desktop.
Setting up a Escher, Jupyter and Cobrapy

Requirements

  • Python 3: should be available in most modern macOS or Linux installations. Please install if not available. You can check if it is available by running which python3 on the terminal.
  • pip for python 3: Check if available by running which pip3, it should normally be available with the python 3 installation. Otherwise, please visit https://pip.pypa.io/en/stable/installing/.

All commands mentioned here should be run on the same terminal, sequentially.

1.- Install virtualenv for simplicity

Execute:

pip3 install virtualenv

2.- Create virtual environment to install all tools

We are going to create a clean environment for our tools, to avoid disrupting other software installations and to avoid any conflicts with other present packages in the system installation.

mkdir -p ~/venvs
virtualenv ~/venvs/metabolic-wb

3.- Go into the metabolic-wb environment

Now we go into that virtual environment by executing:

source ~/venvs/metabolic-wb/bin/activate

whenever you want to use the tools that we will install here in a new terminal window, you need to execute the above command.

4.- Install packages

Within the same termina window where you executed the previous command which activated the environment, run:

pip install escher==1.7.3 cobra jupyterlab notebook d3flux

This will install:

  • Escher and d3flux for interactive visualization of simulated metabolic networks.
  • cobrapy.
  • JupyterLab and Notebook for easily working on the web browser and sharing work with colleagues.

More interesting packages that could be installed available here.

From now on, anytime that you need to use these packages, if you are on a new terminal window, execute step 3 again. All the other steps are only needed one time per computer.

5.- Run JupyterLabs to use cobrapy on a model

On a terminal where you had executed step 3 above at the least, run:

jupyter lab

More details here.

Then select on the file browser to the left, navigate to a folder where you have SBML or cobrapy models in YAML or JSON (or any of the other cobrapy accepted formats). Then on the launcher to the right, on the Notebook section, click on Python3. You should be able in the newly opened tab to execute code which uses cobrapy as you are used.

from cobra.io import read_sbml_model

model = read_sbml_model('my_model.xml')
...
@Lumius4
Copy link

Lumius4 commented Oct 25, 2023

Dear Pablo,
Thank you so much for this tutorial. I am new to programming and flux models etc. and I tried to use cobra in jupyter notebook for two days now! Your approach was the only who worked! Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment