Module 3 Lab: (Bonus) Automation
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. This means that you are able to copy, share and modify the work, as long as the result is distributed under the same license.
By Ruth Isserlin
Although a lot of what we have demonstrated in Cytoscape up until now has been manual most of the features we use can be automated through multiple access points including:
- R/Rstudio using RCy3 - a bioconductor package that makes communicating with cytoscape as simple as calling a method.
- Python using py2cytoscape.
- directly through cyrest using rest calls - you can use any programming language with the rest API. See Cytoscape Automation
Automation becomes helpful when performing pipelines multiple times on similiar datasets or integrating cytoscape data into your other pipelines.
Below we demonstrate how to perform the enrichment map pipeline from R but automation is not limited to this access point. You can automate it from any flavour of programming.
Check out all the ways you can interact with Cytoscape here including directly through the cytoscape command window.
Goal of the exercise:
Run an enrichment analysis and Create an enrichment map automatically from R/Rstudio
During this exercise, you will apply what you have learnt in Module 2 labs and Module 3 labs but instead of performing them manually you will automate the process using R/Rstudio. We will use all the same data and programs we used in the previous labs but we will control them from R.
Before starting this exercise you need to set up R/Rstudio. You can do that directly on your machine or through docker.
Set Up - Option 1 - Install R/Rstudio
- Install R.
- Go to: https://cran.rstudio.com/
- If installing on Windows select “install R for the first time” to get to the required package.
RStudio is a free IDE (Integrated Development Environment) for R. RStudio is a wrapper1 for R and as far as basic R is concerned, all the underlying functions are the same, only the user interface is different (and there are a few additional functions that are very useful e.g. for managing projects).
Here is a small list of differences between R and RStudio.
pros (some pretty significant ones actually):
- Integrated version control.
- Support for “projects” that package scripts and other assets.
- Syntax-aware code colouring.
- A consistent interface across all supported platforms. (Base R GUIs are not all the same for e.g. Mac OS X and Windows.)
- Code autocompletion in the script editor. (Depending on your point of view this can be a help or an annoyance. I used to hate it. After using it for a while I find it useful.)
- “Function signaturtes” (a list of named parameters) displayed when you hover over a function name.
- The ability to set breakpoints for debugging in the script editor.
- Support for knitr, and rmarkdown; also support for R notebooks … (This supports “literate programming” and is actually a big advance in software development)
- Support for R notebooks.
cons (all minor actually):
- The tiled interface uses more desktop space than the windows of the R GUI.
- There are sometimes (rarely) situations where R functions do not behave in exactly the same way in RStudio.
- The supported R version is not always immediately the most recent release.
- Navigate to the RStudio download Website.
- Find the right version of the RStudio Desktop installer for your computer, download it and install the software.
- Open RStudio.
-
Focus on the bottom left pane of the window, this is the “console”
pane.
- Type getwd().
- This prints out the path of the current working directory. Make a (mental) note where this is. We usually always need to change this “default directory” to a project directory.
Set Up - Option 2 - Docker image with R/Rstudio
Changing versions and environments are a continuing struggle with bioinformatics pipelines and computational pipelines in general. An analysis written and performed a year ago might not run or produce the same results when it is run today. Recording package and system versions or not updating certain packages rarely work in the long run.
One the best solutions to reproducibility issues is containing your workflow or pipeline in its own coding environment where everything from the operating system, programs and packages are defined and can be built from a set of given instructions. There are many systems that offer this type of control including:
“A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.” (Merkel 2014)
Why are containers great for Bioiformatics?
- allows you to create environments to run bioinformatis pipelines.
- create a consistent environment to use for your pipelines.
- test modifications to the pipeline without disrupting your current set up.
- Coming back to an analysis years later and there is no need to install older versions of packages or programming languages. Simply create a container and re-run.
What is docker?
- Docker is a container platform, similar to a virtual machine but better.
- We can run multiple containers on our docker server. A container is an instance of an image. The image is built based on a set of instructions but consists of an operating system, installed programs and packages. (When backing up your computer you might taken an image of it and restored your machine from this image. It the same concept but the image is built based on a set of elementary commands found in your Dockerfile.) - for overview see here
- Often images are built off of previous images with specific additions you need for you pipeline. (For example, for this course we use a base image supplied by bioconductorrelease 3.11 and comes by default with basic Bioconductor packages but it builds on the base R-docker images called rocker.)
Container
- An instance of an image.
- the self-contained running system.
- There can be multiple containers derived from the same image.
Image
- An image contains the blueprint of a container.
- In docker, the image is built from a Dockerfile
Docker Volumes
- Anything written on a container will be erased when the container is erased ( or crashes) but anything written on a filesystem that is separate from the contain will persist even after a container is turned off.
- A volume is a way to assocaited data with a container that will persist even after the container. * maps a drive on the host system to a drive on the container.
- In the above docker run command (that creates our container) the statement:
- maps the directory ${PWD} to the directory /home/rstudio/projects on the container. Anything saved in /home/rstudio/projects will actually be saved in ${PWD}
- An example:
- I use the following commmand to create my docker container:
docker run -e PASSWORD=changeit --rm \
-v /Users/risserlin/code:/home/rstudio/projects \
-p 8787:8787 \
risserlin/workshop_base_image
- I create a notebook called task3.Rmd and save it in /home/rstudio/projects.
Note: Do not save it in /home/rstudio/ which is the default directory RStudio will start in
- On my host computer, if I go to /Users/risserlin/code I will find the file task3.Rmd
Install Docker
- Download and install docker desktop.
- Follow slightly different instructions for Windows or MacOS/Linux
Windows
- it might prompt you to install additional updates (for example - https://docs.Microsoft.com/en-us/windows/wsl/install-win10#step-4---download-the-linux-kernel-update-package) and require multiple restarts of your system or docker.
- launch docker desktop app.
- Open windows Power shell
- navigate to directory on your system where you plan on keeping all your code. For example: C:\USERS\risserlin\code
- Run the following command: (the only difference with the windows command is the way the current directory is written. ${PWD} instead of "$(pwd)")
docker run -e PASSWORD=changeit --rm \
-v ${PWD}:/home/rstudio/projects -p 8787:8787 \
risserlin/workshop_base_image
- Windows defender firewall might pop up with warning. Click on Allow access.
- In docker desktop you see all containers you are running and easily manage them.
MacOS / Linux
- Open Terminal
- navigate to directory on your system where you plan on keeping all your code. For example: /Users/risserlin/code
- Run the following command: (the only difference with the windows command is the way the current directory is written. ${PWD} instead of "$(pwd)")
docker run -e PASSWORD=changeit --rm \
-v "$(pwd)":/home/rstudio/projects -p 8787:8787 \
risserlin/workshop_base_image
Create your first notebook using Docker
Start coding!
- Open a web browser to localhost:8787
- enter username: rstudio
- enter password: changeit
- changing the parameter -e PASSWORD=changeit in the above docker command will change the password you need to specify
- Make sure your docker container is running. (If you rebooted your machine you will need to restart the container on reboot.)
- Make sure you got the right port.
After logging in, you will see an Rstudio window just like when you install it directly on your computer. This RStudio will be running in your docker container and will be a completely separate instance from the one you have installed on your machine (with a different set of packages and potentially versions installed).
Make sure that you have mapped a volume on your computer to a volume
in your container so that files you create are also saved on your
computer. That way, turning off or deleting your container or image will
not effect your files.
- The parameter -v ${PWD}:/home/rstudio/projects maps your current directory (i.e. the directory you are in when launching the container) to the directory /home/rstudio/projects on your container.
- You do not need to use the ${PWD} convention. You can also specify the exact path of the directory you want to map to your container.
- Make sure to save all your scripts and notebooks in the projects directory.
- Create your first notebook in your docker Rstudio.
- Save it.
- Find your newly created file on your computer.
Start using automation
- Download example R notebooks from https://github.com/risserlin/CBW_pathways_workshop_R_notebooks.
This repository contains example R Notebooks that automate the CBW pipeline.
There are two ways you can download this collection:
- If you are familiar with git then we recommend you fork the repo and use it like you would use any github repo.
- download the collection as a zip file - unzip folder and place in CBW working directory
If you are new to git and want to learn more about code versioning then we recommend you read the following tutorial And check out Github Desktop - a desktop application to communicate with github.
Running example notebooks in local RStudio
Highly recommended to use docker instead of local RStudio. If you are using local RStudio, versions of R and associated packages may be different than the ones used in the example notebooks and might require installing updated versions and additional packages.
Step 2 - create a new project
- Create a new project - File -> New R Project …
- Select Create project from - “Existing Directory”
- Click on the Browse button
- Navigate to the CBW_pathways_workshop_R_notebooks directory that is found in the directory you downloaded and unzipped from github. (for example, if it is still in your downloads directory go to ~/Downloads/Cytoscape_workflows/CBW_pathways_workshop_R_notebooks)
Step 3 - Open example RNotebook
Open the RNotebook 07-Create_EM_from_GSEA_results.Rmd
Go to File –> Open File …
Click on 07-Create_EM_from_GSEA_results.Rmd
If the file is not found in the first directory that RStudio opens up then go back and make sure that you created an Rproject from an “Existing directory” in the previous step.
Step 4 - Step through notebook to run the analysis
The RNotebook is a mixture of markdown text and code blocks.
Read through the notebook to understand what each section is doing and sequentially run the code blocks by clicking on the play button at the top right of each code block.
Run analysis directly from R for easy integration into existing pipelines.
Instead of creating an Enrichment map manually through the user interface you can create an enrichment map directly using the RCy3 bioconductor package or through direct rest calls with Cytoscape cyrest.
Follow the step by step instructions on how to run from R here - https://risserlin.github.io/CBW_pathways_workshop_R_notebooks/create-enrichment-map-from-r-with-gsea-results.html
First, make sure your environment is set up correctly by following there instructions - https://risserlin.github.io/CBW_pathways_workshop_R_notebooks/setup.html
Exercises
Once you have run through the notebook and created your enrichment map automatically try the following:
- change the fdr threshold and create a new network (without rerunning the whole notebook) with the lower FDR threshold.
- change the similarity coeffecient and create a new network (without rerunning the whole notebook) with the lower FDR threshold.
- re-run the notebook using the GSEA results you created on the first run without running GSEA.
- Modify notebook to run with a different gmt file. (Downloaded from somewhere else or a different file found on baderlab genesets download site)
- Open the notebook Supplementary_Protocol5_Multi_dataset_theme_analysis.Rmd and run through it to create a multi dataset enrichment map.
Additional resources
Check out all the different notebooks available here
References
A “wrapper” program uses another program’s functionality in its own context. RStudio is a wrapper for R since it does not duplicate R’s functions, it runs the actual R in the background.↩︎