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 training, participants 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 Integrated Development Environment (IDE)

Once Python is installed, the next step is to install a preferred Integrated Development Environment (IDE) to start coding with Python. If you are a new user, we suggest using the Jupyter Notebook.

Install Jupyter Notebook on your own computer

  • Open Anaconda Navigator and click install Jupyter Notebook.
  • Launch the Jupyter Notebook directly from the Anaconda Navigator or start the notebook server from the command line by typing the following command.
jupyter notebook 
  • You should see the notebook home page open in your web browser.

Figure 2: Jupyter Notebook

Useful links:

Step 3: Installing a Python library

If you are currently using Python using conda (or Anaconda) or if you are using the cluster, 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.

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_Ihc ER Status measured by IHC
PR_Status PR Status
HER2_status HER2 Status
HER2_status_measured_by_SNP6 HER2 status measured by SNP6
PAM50/Claudin_Subtype Pam50 + Claudin-low subtype.
3-gene_classifier/Threegene 3-Gene classifier subtype
Nottingham_prognostic_index/Npi Nottingham prognostic index
Cellularity Tumor Content
Integrative_cluster/Intclust Integrative Cluster
Mutation_count Mutation count
FOXA1 FOXA1 Expression data
MLPH MLPH Expression data
ESR1 ESR1 Expression data
ERBB2 ERBB2 Expression data
TP53 TP53 Expression data
PIK3CA PIK3CA Expression data
GATA3 GATA3 Expression data
PGR PGR Expression data
Cancer_Type_Detailed Cancer Type Detailed
Oncotree_Code Oncotree Code
Sample_Type The type of sample (i.e., normal, primary, met, recurrence).
Tmb_Nonsynonymous TMB (nonsynonymous)

Credits and Acknowledgements

This content was adapted from the following course materials: