Introduction to Python

Introduction

This workshop is designed to give beginners a solid foundation in Python programming, with a specific focus on applications in cancer biology. Participants will gain a thorough understanding of essential programming concepts through a blend of theoretical lessons, hands-on coding exercises, and practical applications.

The workshop will cover essential programming concepts and gradually introduce more advanced topics, with a focus on using the pandas library for efficient data handling and analysis and plotnine (ggplot) library for data visualization. By the end of the workshop, attendees will be equipped with the skills to enhance the reproducibility and efficiency of scientific research through powerful data analysis tools and effective visualization techniques.

Learning Objectives

Participants will gain the following skills:

  • Proficiency in using Python for data analysis.
  • Basic Python programming skills.
  • Reading, tidying, and joining datasets using pandas library.
  • Data manipulation and transformation using pandas library.
  • Creating various types of plots using plotnine library.

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 one of the trainers for help.

Step 1: Installing Python

There are multiple ways you can use Python at Peter Mac. 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 PeterMac Self-Service → Research Applications tab or from the Anaconda Navigator Installation.

Linux

Install the Anaconda Navigator from here.

  • If you are using Anaconda/Conda on your laptop/desktop on the Peter Mac network you may need to provide proxy settings by adding the following proxy servers as shown here
  • To update the .condarc file follow the quick start guide.

If you are having trouble opening the Anaconda Navigator please follow their troubleshooting page.

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:

Use Python on the cluster?

Follow the quick start guide on this page.

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. Alternatively, if you are accustomed to using R Studio, it can also serve as a platform for Python coding.

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:

Use Jupyter Notebook on Open OnDemand

Follow the guide on this page.

Useful links:

Use R Studio for Python coding

The RStudio IDE is a free and open-source IDE for Python, as well as R. You can write scripts, import modules, and interactively use Python within the RStudio IDE. Whether your intention is to seamlessly combine R and Python or solely concentrate on Python programming, there are several ways you can advance your coding:

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 and plotnine libraries of Python use the following command on the terminal.

conda install conda-forge::pandas, plotnine, 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, plotnine, 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.

Useful links:

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.

Refrences:

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 Cohort
Age_at_diagnosis Age at Diagnosis
Survival_time/Os_Months Overall survival in months since initial diagonosis.
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 lymphnodes positive
Lymph_nodes_status Lymphnodes 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 Acknowledgement

These content were adapted from the following course materials:


Back to top