Bioinformatics With Python Cookbook - Sample Chapter
Bioinformatics With Python Cookbook - Sample Chapter
ee
Using the hands-on recipes in this book, you'll be able to do practical research and analysis in
computational biology with Python. We cover modern, next-generation sequencing libraries
and explore real-world examples on how to handle real data. The main focus of the book is the
practical application of bioinformatics, but we also cover modern programming techniques and
frameworks to deal with the ever increasing deluge of bioinformatics data.
phylogenetic analysis
and problems
large datasets
Tiago Antao
P U B L I S H I N G
If you are either a computational biologist or a Python programmer, you will probably relate to the
expression "explosive growth, exciting times". Python is arguably the main programming language for
big data, and the deluge of data in biology, mostly from genomics and proteomics, makes bioinformatics
one of the most exciting fields in data science.
pl
e
Bioinformatics with
Python Cookbook
Learn how to use modern Python bioinformatics libraries and
applications to do cutting-edge research in computational biology
P U B L I S H I N G
Sa
Tiago Antao
Preface
Whether you are reading this book as a computational biologist or a Python programmer, you
will probably relate to the "explosive growth, exciting times" expression. The recent growth of
Python is strongly connected with its status as the main programming language for big data.
On the other hand, the deluge of data in biology, mostly from genomics and proteomics makes
bioinformatics one of the forefront applications of data science. There is a massive need for
bioinformaticians to analyze all this data; of course, one of the main tools is Python. We will
not only talk about the programming language, but also the whole community and software
ecology behind it. When you choose Python to analyze your data, you will also get an extensive
set of libraries, ranging from statistical analysis to plotting, parallel programming, machine
learning, and bioinformatics. However, when you choose Python, you expect more than
this; the community has a tradition of providing good documentation, reliable libraries, and
frameworks. It is also friendly and supportive of all its participants.
In this book, we will present practical solutions to modern bioinformatics problems using
Python. Our approach will be hands-on, where we will address important topics, such as
next-generation sequencing, genomics, population genetics, phylogenetics, and proteomics
among others. At this stage, you probably know the language reasonably well and are aware
of the basic analysis methods in your field of research. You will dive directly into relevant
complex computational biology problems and learn how to tackle them with Python. This is
not your first Python book or your first biology lesson; this is where you will find reliable and
pragmatic solutions to realistic and complex problems.
Preface
Chapter 3, Working with Genomes, not only deals with high-quality referencessuch as the
human genomebut also discusses how to analyze other low-quality references typical in
non-model species. It introduces GFF processing, teaches you how to analyze genomic
feature information, and discusses how to use gene ontologies.
Chapter 4, Population Genetics, describes how to perform population genetics analysis of
empirical datasets. For example, on Python, we will perform Principal Components Analysis,
compute FST, or Structure/Admixture plots.
Chapter 5, Population Genetics Simulation, covers simuPOP, an extremely powerful
Python-based forward-time population genetics simulator. This chapter shows you how
to simulate different selection and demographic regimes. It also briefly discusses the
coalescent simulation.
Chapter 6, Phylogenetics, uses complete sequences of recently sequenced Ebola viruses
to perform real phylogenetic analysis, which includes tree reconstruction and sequence
comparisons. This chapter discusses recursive algorithms to process tree-like structures.
Chapter 7, Using the Protein Data Bank, focuses on processing PDB files, for example,
performing the geometric analysis of proteins. This chapter takes a look at protein visualization.
Chapter 8, Other Topics in Bioinformatics, talks about how to analyze data made
available by the Global Biodiversity Information Facility (GBIF) and how to use Cytoscape,
a powerful platform to visualize complex networks. This chapter also looks at how to work
with geo-referenced data and map-based services.
Chapter 9, Python for Big Genomics Datasets, discusses high-performance programming
techniques necessary to handle big datasets. It briefly discusses cluster usage and code
optimization platforms (such as Numba or Cython).
Python and
the Surrounding
Software Ecology
In this chapter, we will cover the following recipes:
Introduction
We will start by installing the required software. This will include the Python distribution,
some fundamental Python libraries, and external bioinformatics software. Here, we will also
be concerned with the world outside Python. In bioinformatics and Big Data, R is also a major
player; therefore, you will learn how to interact with it via rpy2 a Python/R bridge. We will also
explore the advantages that the IPython framework can give us in order to efficiently interface
with R. This chapter will set the stage for all the computational biology that we will perform in
the rest of the book.
Getting ready
Python can be run on top of different environments. For instance, you can use Python inside
the JVM (via Jython) or with .NET (with IronPython). However, here, we are concerned not only
with Python, but also with the complete software ecology around it; therefore, we will use the
standard (CPython) implementation as that the JVM and .NET versions exist mostly to interact
with the native libraries of these platforms. A potentially viable alternative will be to use the
PyPy implementation of Python (not to be confused with PyPi: the Python Package index).
An important decision is whether to choose the Python 2 or 3. Here, we will support both
versions whenever possible, but there are a few issues that you should be aware of. The first
issue is if you work with Phylogenetics, you will probably have to go with Python 2 because
most existing Python libraries do not support version 3. Secondly, in the short term, Python 2,
is generally better supported, but (save for the aforementioned Phylogenetics topic) Python
3 is well covered for computational biology. Finally, if you believe that you are in this for the
long run, Python 3 is the place to be. Whatever is your choice, here, we will support both
options unless clearly stated otherwise. If you go for Python 2, use 2.7 (or newer if it has been
released). With Python 3, use at least 3.4.
Chapter 1
If you were starting with Python and bioinformatics, any operating system will work, but here,
we are mostly concerned with the intermediate to advanced usage. So, while you can probably
use Windows and Mac OS X, most heavy-duty analysis will be done on Linux (probably on a
Linux cluster). Next-generation sequencing data analysis and complex machine learning are
mostly performed on Linux clusters.
If you are on Windows, you should consider upgrading to Linux for your bioinformatics work
because many modern bioinformatics software will not run on Windows. Mac OS X will be
fine for almost all analyses, unless you plan to use a computer cluster, which will probably
be Linux-based.
If you are on Windows or Mac OS X and do not have easy access to Linux, do not worry.
Modern virtualization software (such as VirtualBox and Docker) will come to your rescue,
which will allow you to install a virtual Linux on your operating system. If you are working
with Windows and decide that you want to go native and not use Anaconda, be careful with
your choice of libraries; you are probably safer if you install the 32-bit version for everything
(including Python itself).
Remember, if you are on Windows, many tools will be unavailable to you.
Bioinformatics and data science are moving at breakneck speed; this
is not just hype, it's a reality. If you install the default packages of your
software framework, be sure not to install old versions. For example,
if you are a Debian/Ubuntu Linux user, it's possible that the default
matplotlib package of your distribution is too old. In this case, it's
advised to either use a recent conda or pip package instead.
Usage
General
URL
http://ipython.org/
Purpose
IPython
NumPy
General
http://www.numpy.org/
Numerical Python
SciPy
General
http://scipy.org/
Scientific computing
matplotlib
General
http://matplotlib.org/
Visualization
Biopython
General
Bioinformatics
PyVCF
NGS
PySAM
NGS
simuPOP
Population Genetics
DendroPY
Phylogenetics
scikit-learn
General
PyMOL
Proteomics
http://biopython.org/
wiki/Main_Page
http://pyvcf.
readthedocs.org/en/
latest/
http://pysam.
readthedocs.org/en/
latest/
http://simupop.
sourceforge.net/
http://pythonhosted.
org/DendroPy/
http://scikit-learn.
org/stable/
http://pymol.org/
rpy2
R integration
R interface
pygraphviz
General
Reportlab
General
http://rpy.sourceforge.
net/
http://pygraphviz.
github.io/
http://reportlab.com/
seaborn
General
Visualization/Stats
Cython
Big Data
http://web.stanford.
edu/~mwaskom/software/
seaborn/
http://cython.org/
Numba
Big Data
http://numba.pydata.
org/
High performance
General
VCF processing
SAM/BAM processing
Genetics Simulation
Phylogenetics
Machine learning
Molecular
visualization
Graph library
Visualization
High performance
Chapter 1
Note that the list of available software for Python in general and bioinformatics in particular
is constantly increasing. For example, we recommend you to keep an eye on projects such as
Blaze (data analysis) or Bokeh (visualization).
How to do it
Here are the steps to perform the installation:
1. Start by downloading the Anaconda distribution from http://continuum.io/
downloads. You can either choose the Python Version 2 or 3. At this stage, this is
not fundamental because Anaconda will let you use the alternative version if you
need it. You can accept all the installation defaults, but you may want to make sure
that conda binaries are in your PATH (do not forget to open a new window so that the
PATH is updated).
If you have another Python distribution, but still decide to try Anaconda, be
careful with your PYTHONPATH and existing Python libraries. It's probably
better to unset your PYTHONPATH. As much as possible, uninstall all other
Python versions and installed Python libraries.
2. Let's go ahead with libraries. We will now create a new conda environment called
bioinformatics with Biopython 1.65, as shown in the following command:
conda create -n bioinformatics biopython biopython=1.65 python=2.7
6. Now, install the Python bioinformatics packages, apart from Biopython (you only need
to install those that you plan to use):
https://conda.binstar.org/bcbio
pysam
7.
If you need to interoperate with R, of course, you will need to install it; either
download it from the R website at http://www.r-project.org/ or use
the R provided by your operating system distribution.
On a recent Debian/Ubuntu Linux distribution, you can just run the following
command as root:
apt-get r-bioc-biobase r-cran-ggplot2
This will install Bioconductor: the main R suite for bioinformatics and
ggplot2a popular plotting library in R. Of course, this will indirectly
take care of installing R.
8. Alternatively, If you are not on Debian/Ubuntu Linux, do not have root, or prefer to
install in your home directory, after downloading and installing R manually, run the
following command in R:
source("http://bioconductor.org/biocLite.R")
biocLite()
9. Finally, you will need to install rpy2, the R-to-Python bridge. Back at the command
line, under the conda bioinformatics environment, run the following command:
pip install rpy2
There's more
There is no requirement to use Anaconda; you can easily install all this software on another
Python distribution. Make sure that you have pip installed and install all conda packages
with it, instead. You may need to install more compilers (for example, Fortran) and libraries
because installation via pip will rely on compilation more than conda. However, as you also
need pip for some packages under conda, you will need some compilers and C development
libraries with conda, anyway. If you are on Python 3, you will probably have to perform pip3
and run Python as python3 (as python/pip will call Python 2 by default on most systems).
In order to isolate your environment, you may want to consider using virtualenv (http://
docs.python-guide.org/en/latest/dev/virtualenvs/). This allows you to create
a bioninformatics environment similar to the one on conda.
6
Chapter 1
See also
The software installation and package maintenance was never Python's strongest point
(hence, the popularity of conda to address this issue). If you want to know the currently
recommended installation policies for the standard Python distribution (and avoid old
and deprecated alternatives), refer to https://packaging.python.org/.
You have probably heard of the IPython Notebook; if not, visit their page at
http://ipython.org/notebook.html.
Getting ready
If you are on Linux, the first thing you have to do is to install Docker. The safest solution is to
get the latest version from https://www.docker.com/. While your Linux distribution may
have a Docker package, it may be too old and buggy (remember the "advancing at breakneck
speed" thingy?).
If you are on Windows or Mac, do not despair; boot2docker (http://boot2docker.io/)
is here to save you. Boot2docker will install VirtualBox and Docker for you, which allows you
to run Docker containers in a virtual machine. Note that a fairly recent computer (well, not
that recent, as the technology was introduced in 2006) is necessary to run our 64-bit virtual
machine. If you have any problems, reboot your machine and make sure that on the BIOS,
VT-X or AMD-V is enabled. At the very least, you will need 6 GB of memory, preferably more.
Note that this will require a very large download from the Internet, so be sure that you have a
big network pipe. Also, be ready to wait for a long time.
How to do it
These are the steps to be followed:
1. Use the following command on the Linux shell or in boot2docker:
docker build -t bio
https://raw.githubusercontent.com/tiagoantao/bioinfpython/master/docker/2/Dockerfile
If you want the Python 3 version, replace the 2 with 3 versions on the URL.
After a fairly long wait, all should be ready.
Note that on Linux, you will either require to have root privileges or be added
to the Docker Unix group.
The -p 9875:9875 will expose the container TCP port 9875 on the host
computer port 9875.
4. If you are using boot2docker, the final configuration step will be to run the following
command in the command line of your operating system, not in boot2docker:
VBoxManage controlvm boot2docker-vm natpf1
"name,tcp,127.0.0.1,9875,,9875"
Chapter 1
See also
Docker is the most widely used containerization software and has seen
enormous growth in usage in recent times. You can read more about it
at https://www.docker.com/.
You will find a paper on arXiv, which introduces Docker with a focus on reproducible
research at http://arxiv.org/abs/1410.0846.
Getting ready
You will need to get the metadata file from the 1000 genomes sequence index. Please check
https://github.com/tiagoantao/bioinf-python/blob/master/notebooks/
Datasets.ipynb and download the sequence.index file. If you are using notebooks,
open the 00_Intro/Interfacing_R notebook.ipynb and just execute the wget
command on top.
This file has information about all FASTQ files in the project (we will use data from the Human
1000 genomes project in the chapters to come). This includes the FASTQ file, the sample
ID, and the population of origin and important statistical information per lane, such as the
number of reads and number of DNA bases read.
How to do it
Take a look at the following steps:
1. We start by importing rpy2 and reading the file, using the read_delim R function:
import rpy2.robjects as robjects
read_delim = robjects.r('read.delim')
seq_data = read_delim('sequence.index', header=True,
stringsAsFactors=False)
#In R:
# seq.data <- read.delim('sequence.index', header=TRUE,
# stringsAsFactors=FALSE)
2. Then, we call the function proper; note the following highly declarative features. First,
most atomic objectssuch as stringscan be passed without conversion. Second,
argument names are converted seamlessly (barring the dot issue). Finally, objects
are available in the Python namespace (but objects are actually not available in the R
namespace; more about this later). For reference, I have included the corresponding
R code. I hope it's clear that it's an easy conversion.
The seq_data object is a data frame. If you know basic R or the Python
pandas library, you are probably aware of this type of data structure; if not,
then this is essentially a table: a sequence of rows where each column has
the same type. Let's perform a basic inspection of this data frame as follows:
print('This dataframe has %d columns and %d rows' %
(seq_data.ncol, seq_data.nrow))
print(seq_data.colnames)
#In R:
# print(colnames(seq.data))
# print(nrow(seq.data))
# print(ncol(seq.data))
Again, note the code similarity. You can even mix styles using the
following code:
my_cols = robjects.r.ncol(seq_data)
print(my_cols)
10
Chapter 1
You can call R functions directly; in this case, we will call ncol if they do not
have dots in their name; however, be careful. This will display an output, not
26 (the number of columns), but [26] which is a vector composed of the
element 26. This is because by default, most operations in R return vectors.
If you want the number of columns, you have to perform my_cols[0].
Also, talking about pitfalls, note that R array indexing starts with 1, whereas
Python starts with 0.
3. Now, we need to perform some data cleanup. For example, some columns should be
interpreted as numbers, but they are read as strings:
as_integer = robjects.r('as.integer')
match = robjects.r.match
my_col = match('BASE_COUNT', seq_data.colnames)[0]
print(seq_data[my_col - 1][:3])
seq_data[my_col - 1] = as_integer(seq_data[my_col - 1])
print(seq_data[my_col - 1][:3])
The match function is somewhat similar to the index method in Python lists.
As expected, it returns a vector so that we can extract the 0 element. It's also
1-indexed, so we subtract one when working on Python. The as_integer
function will convert a column to integers. The first print will show strings
(values surrounded by "), whereas the second print will show numbers.
4. We will need to massage this table a bit more; details can be found on a notebook,
but here we will finalize with getting the data frame to R (remember that while it's
an R object, it's actually visible on the Python namespace only):
robjects.r.assign('seq.data', seq_data)
This will create a variable in the R namespace called seq.data with the
content of the data frame from the Python namespace. Note that after this
operation, both objects will be independent (if you change one, it will not be
reflected on the other).
While you can perform plotting on Python, R has default built-in
plotting functionalities (which we will ignore here). It also has a
library called ggplot2 that implements the Grammar of Graphics
(a declarative language to specify statistical charts).
11
6. With regards to our concrete example based on the Human 1000 genomes
project, we will first plot a histogram with the distribution of center names,
where all sequencing lanes were generated. The first thing that we need to
do is to output the chart to a PNG file. We call the R png() function as follows:
robjects.r.png('out.png')
7.
We will now use ggplot to create a chart, as shown in the following command:
from rpy2.robjects.functions import SignatureTranslatedFunction
ggplot2.theme = SignatureTranslatedFunction(ggplot2.theme,
init_prm_translate={'axis_text_x': 'axis.text.x'})
bar = ggplot2.ggplot(seq_data) + ggplot2.geom_bar() +
ggplot2.aes_string(x='CENTER_NAME') +
ggplot2.theme(axis_text_x=ggplot2.element_text(angle=90,
hjust=1))
bar.plot()
dev_off = robjects.r('dev.off')
dev_off()
8. We then draw the chart itself. Note the declarative nature of ggplot2 as we add
features to the chart. First, we specify the seq_data data frame, then we will
use a histogram bar plot called geom_bar, followed by annotating the X variable
(CENTER_NAME).
9. Finally, we rotate the text of the x axis by changing the theme.
12
Chapter 1
10. As a final example, we will now do a scatter plot of read and base counts for all the
sequenced lanes for Yoruban (YRI) and Utah residents with ancestry from Northern
and Western Europe (CEU) of the Human 1000 genomes project (the summary of the
data of this project, which we will use thoroughly, can be seen in the Working with
modern sequence formats recipe in Chapter 2, Next-generation Sequencing). We are
also interested in the difference among the different types of sequencing (exome,
high, and low coverage). We first generate a data frame with just YRI and CEU lanes
and limit the maximum base and read counts:
robjects.r('yri_ceu <- seq.data[seq.data$POPULATION %in%
c("YRI", "CEU") & seq.data$BASE_COUNT < 2E9 &
seq.data$READ_COUNT < 3E7, ]')
robjects.r('yri_ceu$POPULATION <as.factor(yri_ceu$POPULATION)')
robjects.r('yri_ceu$ANALYSIS_GROUP <as.factor(yri_ceu$ANALYSIS_GROUP)')
13
Hopefully, this example (refer to the following screenshot) makes the power of
the Grammar of Graphics approach clear. We will start by declaring the data
frame and the type of chart in use (the scatter plot implemented by geom_
point). Note how easy it is to express that the shape of each point depends
on the POPULATION variable and the color on the ANALYSIS_GROUP:
Figure 2: The ggplot2-generated scatter plot with base and read counts for all sequencing lanes read; the color and
shape of each dot reflects categorical data (population and the type of data sequenced)
14
Chapter 1
12. Finally, when you think about Python and R, you probably think about pandas: the
R-inspired Python library designed with data analysis and modeling in mind. One of
the fundamental data structures in pandas is (surprise) the data frame. It's quite
easy to convert backward and forward between R and pandas, as follows:
import pandas.rpy.common as pd_common
pd_yri_ceu = pd_common.load_data('yri_ceu')
del pd_yri_ceu['PAIRED_FASTQ']
no_paired = pd_common.convert_to_r_dataframe(pd_yri_ceu)
robjects.r.assign('no.paired', no_paired)
robjects.r("print(colnames(no.paired))")
In the interests of maintaining the momentum of the book, we will not delve into pandas
programming (there are plenty of books on this), but I recommend that you take a look
at it, not only in the context of interfacing with R, but also as a very good library for data
management of complex datasets.
There's more
It's worth repeating that the advancements on the Python software ecology are occurring at
a breakneck pace. This means that if a certain functionality is not available today, it might
be released sometime in the near future. So, if you are developing a new project, be sure to
check for the very latest developments on the Python front before using a functionality from
an R package.
There are plenty of R packages for bioinformatics in the Bioconductor project (http://
www.bioconductor.org/). This should probably be your first port of call in the R world for
bioinformatics functionalities. However, note that there are many R bioinformatics packages
that are not on Bioconductor, so be sure to search the wider R packages on CRAN (refer to the
Comprehensive R Archive Network at http://cran.r-project.org/).
There are plenty of plotting libraries for Python. matplotlib is the most common library, but
you also have a plethora of other choices. In the context of R, it's worth noting that there is a
ggplot2-like implementation for Python based on the Grammar of Graphics description language
for charts and this is calledsurprise-surpriseggplot! (http://ggplot.yhathq.com/).
15
See also
There are plenty of tutorials and books on R; check the R web page
(http://www.r-project.org/) for documentation.
If you work with NGS, you might also want to check High Throughput Sequence
Analysis with Bioconductor at http://manuals.bioinformatics.ucr.edu/
home/ht-seq.
Getting ready
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.
16
Chapter 1
How to do it
This recipe is an aggressive simplification of the previous one because it illustrates the
conciseness and elegance of R magics:
1.
See how easy it's to execute the R code without using the robjects
package. Actually, rpy2 is being used to look under the hood, but it has
been made transparent.
2. Let's read the 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)
Note that you can specify that the whole IPython cell should be interpreted
as R code (note the double %%). 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.
3. We can now transfer a variable to the Python namespace (where we could have done
Python-based operations):
seq_data = %R seq.data
The -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.
17
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.
See also
18
www.PacktPub.com
Stay Connected: