Introduction to Python for Biologists

Introduction

This introduction to Python workshop will provide beginners with experience loading, exploring, and visualising biological data using the pandas and matplotlib libraries. The example data used is clinical and gene expression data from the METABRIC breast cancer dataset, allowing participants to work with realistic biomedical data structures and learn how to generate meaningful summaries and plots.

Learning Objectives

By the end of this workshop, you will be able to:

  • Load tabular data into Python using pandas
  • Calculate basic statistics such as mean and median
  • Filter and slice data based on clinical features
  • Produce simple plots such as scatter plots and boxplots using matplotlib
  • Modify the appearance of plots

Prerequisites

Before starting this course you will need to ensure that your computer is set up with the required software. If you have any difficulty installing any of this software then please contact the trainer at sandun.rajapaksa@petermac.org for help.

Step 1: Installing Python

There are multiple ways you can use Python. The easiest and most convenient way is to install Python on your own computer. However, if you prefer to avoid the installation process or need additional computational capabilities the alternative option is to use the cluster.

Install Python on your own computer

For new users, we recommend installing Anaconda. Anaconda conveniently installs Python, the Jupyter Notebook, and other commonly used packages.

Windows

If you have admin rights, follow Anaconda Navigator Installation. Otherwise, contact the IT Support.

macOS

Install the Anaconda Navigator from the Anaconda Navigator Installation.

Linux

Install the Anaconda Navigator from here.

Once installed, open the anaconda-navigator directly or type the following command in the terminal to open it.

anaconda-navigator

Figure 1: Anaconda Navigator

Useful links:

Step 2: Installing an Interactive Computing Environment (ICE)

Once Python is installed, choose an interface to write and run code. We recommend either Jupyter Notebook (classic) or JupyterLab (the newer, more flexible interface). Both run locally in your browser and work well for this workshop.

Install Jupyter Notebook (Classic)

  • Open Anaconda Navigator and install Jupyter Notebook.
  • Launch Jupyter Notebook from Anaconda Navigator, or start it from a terminal:
jupyter notebook
  • Your browser will open the Notebook home page.

Figure 2: Jupyter Notebook

Resources:

Step 3: Installing a Python library

If you are currently using Python using conda (or Anaconda), a Python library can be installed with Anaconda or Miniconda. For example, to install the pandas, numpy and matplotlib libraries of Python use the following command on the terminal.

conda install -c conda-forge pandas matplotlib numpy

If you installed Python using Pip, then a Python library can be installed via pip from Python Package Index (PyPI). To install the required libraries of Python use the following command on the terminal.

pip install pandas matplotlib numpy

If Anaconda Navigator is installed and you prefer to use the Navigator instead of typing commands on a terminal refer to Installing and managing Python packages.

Course Outline

Time Lesson Questions
25 Getting Started with Jupyter Notebook How do I use JupyterLab/Notebook to run Python?
How do I write, run, and document code in notebooks?
15 Python Fundamentals What basic data types can I work with in Python?
How can I create a new variable in Python?
How do I use a function?
Can I change the value associated with a variable after I create it?
How can I get help while learning to program?
60 Loading and Viewing Patient Data How do I load and view tabular data in Python?
40 Analysing Patient Data How do I summarize and compare patient data in Python?
60 Visualising Patient Data How do I make basic plots in Python?
How do I choose the right plot for my data?
How do I make plots beautiful and clear?
How do I share them?

Data

The Metabric study characterized the genomic mutations and gene expression profiles for 2509 primary breast tumours. In addition to the gene expression data generated using microarrays, genome-wide copy number profiles were obtained using SNP microarrays. Targeted sequencing was performed for 2509 primary breast tumours, along with 548 matched normals, using a panel of 173 of the most frequently mutated breast cancer genes as part of the Metabric study.

References:

Both the clinical data and the gene expression values were downloaded from cBioPortal.

We excluded observations for patient tumor samples lacking expression data, resulting in a data set with fewer rows.

The following table illustrates the column names and descriptions of the metabric data frame we will be using for subsequent analysis.

Description of column names in the metabric dataset
Column Name Description
Patient_ID Identifier to uniquely specify a patient.
Cohort Study group or cohort to which the patient belongs.
Age_at_diagnosis Age at Diagnosis
Survival_time/Os_Months Overall survival in months since initial diagnosis.
Survival_status/Os_Status Overall patient survival status.
Vital_status The survival state of the person.
Chemotherapy Chemotherapy.
RadioTherapy RadioTherapy
Tumor_size Tumor size in mm.
Tumor_stage Tumor stage.
Neoplasm_histologic_grade/Grade Numeric value to express the degree of abnormality of cancer cells, a measure of differentiation and aggressiveness.
Lymph_nodes_examined_positive Number of lymph nodes positive
Lymph_nodes_status Lymph nodes status
Cancer_type Cancer Type
ER_status ER Status measured by IHC
PR_Status PR Status
HER2_status HER2 Status
HER2_status_measured_by_SNP6 HER2 status measured by SNP6
PAM50 Pam50 + Claudin-low subtype.
3-gene_classifier 3-Gene classifier subtype
Nottingham_prognostic_index Nottingham prognostic index
Cellularity Tumor Content
Integrative_cluster Integrative Cluster
Mutation_count Mutation count
ESR1 ESR1 Expression data
ERBB2 ERBB2 Expression data
PGR PGR Expression data
TP53 TP53 Expression data
PIK3CA PIK3CA Expression data
GATA3 GATA3 Expression data
FOXA1 FOXA1 Expression data
MLPH MLPH Expression data

Credits and Acknowledgements

This content was adapted from the following course materials: