Skip to content

Plotting functions

Plotting functions in PIASO

This notebook will demonstrate the different plotting functions and their application to sample data.

import piaso
import piaso
import scanpy as sc
import numpy as np
import matplotlib.pyplot as plt
import warnings
sc.set_figure_params(dpi=80,dpi_save=300, color_map='viridis',facecolor='white')
from matplotlib import rcParams
rcParams['figure.figsize'] = 4, 4
warnings.simplefilter(action='ignore', category=FutureWarning)

Load the data

Let us load a 20k subsampled version of the Seattle Alzheimer’s Disease Brain Cell Atlas (SEA-AD) project dataset described in detail in Gabitto et. al. (2024). We will be using the scRNA-seq data from the dataset in this tutorial.

Download the subsampled dataset from Google Drive: https://drive.google.com/file/d/1EdRA0ECvPlEnNaOzKqj19GrEtucB7hmE/view?usp=drive_link

The original data is available on https://portal.brain-map.org/explore/seattle-alzheimers-disease.

adata = piaso.data.load_dataset("sea_ad_mtg_20k")
adata

Visualize with a discrete color map

The discrete color map can be used to visualize the categorical variables such as cell subclass.

sc.pl.embedding(adata,
basis='X_umap',
color=['Subclass'],
palette=piaso.pl.color.d_color3,
legend_fontoutline=2,
legend_fontweight=5,
cmap='Spectral_r',
size=10,
frameon=False)
output
sc.pl.embedding(adata,
basis='X_umap',
color=['Subclass'],
palette=piaso.pl.color.d_color3,
legend_fontoutline=2,
legend_fontsize=7,
legend_fontweight=5,
legend_loc='on data',
cmap='Spectral_r',
size=10,
frameon=False)
output

Visualize with a continuous color map

The continuous color map can be used to visualize the continuous variables such as gene expression.

sc.pl.umap(adata,
color=['GAD1', 'PVALB', 'SST', 'PDGFRA'],
cmap=piaso.pl.color.c_color1,
legend_fontsize=10,
legend_fontoutline=3,
ncols=2,
size=30,
frameon=False)
output
sc.pl.umap(adata,
color=['GAD1', 'PVALB', 'SST', 'PDGFRA'],
cmap=piaso.pl.color.c_color4,
legend_fontsize=10,
legend_fontoutline=3,
ncols=2,
size=30,
frameon=False)
output

Split the UMAP by condition

mapping_dict=dict(zip(adata.obs['CERAD score'], adata.obs['CERAD score']))
mapping_dict={'Absent': 'Absent',
'Sparse': 'Disease',
'Moderate': 'Disease',
'Frequent': 'Disease'}
adata.obs['Condition']=adata.obs['CERAD score'].map(mapping_dict)
sc.pl.embedding(adata,
basis='X_umap',
color=['Condition'],
palette=piaso.pl.color.d_color1,
legend_fontoutline=2,
legend_fontweight=5,
cmap='Spectral_r',
size=10,
frameon=False)
output
Visualizing the expression of a specific gene across two different experimental conditions
piaso.pl.plot_embeddings_split(adata,
color='ADGRG7',
layer=None,
splitby='Condition',
color_map=piaso.pl.color.c_color1,
size=100,
frameon=False,
legend_loc=None,)
output
piaso.pl.plot_embeddings_split(adata,
color='FEZF2',
layer=None,
splitby='Condition',
color_map=piaso.pl.color.c_color1,
size=100,
frameon=False,
legend_loc=None,)
output
Visulaizing continuous variables (Number of UMIs) across different experimental conditions
piaso.pl.plot_embeddings_split(adata,
color='Number of UMIs',
layer=None,
splitby='Condition',
color_map=piaso.pl.color.c_color4,
size=100,
frameon=False,
legend_loc=None)
output
Visualizing categorical variables (Cell subclass) across different experimental conditions
piaso.pl.plot_embeddings_split(adata,
color='Subclass',
layer=None,
splitby='Condition',
size=100,
frameon=False,)
output

We can visualize these cell subclasses alongside the subclass legend. To enhance clarity, the ncol parameter can be used to adjust the number of subplots per row.

piaso.pl.plot_embeddings_split(adata,
color='Subclass',
layer=None,
splitby='Condition',
size=100,
frameon=False,
ncol=1)
output
piaso.pl.plot_embeddings_split(adata,
color='Subclass',
layer=None,
splitby='Condition',
size=100,
frameon=False,
legend_fontsize=7,
legend_fontoutline=2,
legend_fontweight=5,
legend_loc='on data')
output

Violin plots of grouped data

Visulaize how the groups in the dataset vary across a list of different features. Here, you can plot a different subplot for each feature of interest.

piaso.pl.plot_features_violin(adata,
feature_list=[ 'CALB2', 'GAD2', 'SATB2', 'TBR1', 'Number of UMIs'],
width_single=8,
height_single=2.3,
groupby='Subclass',
show_grid=False)
output

Adding the horizontal grid line

piaso.pl.plot_features_violin(adata,
feature_list=[ 'CALB2', 'GAD2', 'SATB2', 'TBR1', 'Number of UMIs'],
width_single=8,
height_single=2.3,
groupby='Subclass')
output

The height and widhts of these plots can be adjusted according to the number of features and groups you have for better visualization.

piaso.pl.plot_features_violin(adata,
feature_list=[ 'CALB2', 'GAD2', 'SATB2', 'TBR1', 'Number of UMIs'],
width_single=20,
height_single=3,
groupby='Subclass',
show_grid=False)
output

Violin plots of entire data

We can visualize the entire dataset as a single group, adjusting the dot size to plot all the cells onto the violin plot.

piaso.pl.plot_features_violin(adata,
feature_list=[ 'Genes detected'],
width_single=4,
height_single=2.0,
size=1)
output

We can visualize the distributions of the cells across various features together.

piaso.pl.plot_features_violin(adata,
feature_list=[ 'MALAT1', 'GAD2', 'Number of UMIs'],
width_single=4,
height_single=2.3,
size=0.5,
show_grid=False)
output

Save output plots

Use the save parameter to specific the file name and path to save:

piaso.pl.plot_embeddings_split(adata,
color='ADGRG7',
layer=None,
splitby='Condition',
color_map=piaso.pl.color.c_color1,
size=100,
frameon=False,
legend_loc=None,
save='./PIASO_UMAP_split_by_condition.pdf')
output
piaso.pl.plot_features_violin(adata,
feature_list=[ 'CALB2', 'GAD2', 'SATB2', 'TBR1', 'Number of UMIs'],
width_single=8,
height_single=2.3,
groupby='Subclass',
show_grid=False,
save='./violin_plot_piaso.pdf')
output

Combine categorical variables

mapping_dict={'Absent': 'Control',
'Sparse': 'Disease',
'Moderate': 'Disease',
'Frequent': 'Disease'}
adata.obs['Condition']=adata.obs['CERAD score'].map(mapping_dict)
adata.obs['SubclassXCondition'] = piaso.pp.getCrossCategories(adata.obs, 'Subclass', 'Condition', )

By combining multiple categories, while maintaining the original orders of the categories, you could show the gene expression changes at cell type level across different conditions.

sc.pl.dotplot(
adata,
var_names=['SERPINE1'],
groupby='SubclassXCondition',
swap_axes=True,
cmap=piaso.pl.color.c_color4
)
output

We can also switch the order in which we combine the categorical variables.

adata.obs['ConditionXSubclass'] = piaso.pp.getCrossCategories(adata.obs, 'Condition', 'Subclass',)
sc.pl.dotplot(
adata,
var_names=['SERPINE1'],
groupby='ConditionXSubclass',
swap_axes=True,
cmap=piaso.pl.color.c_color4
)
output

Count the number of cells in each category

piaso.pp.table(adata.obs['Subclass'])

You can use the rank parameter to order the results, and the ascending parameter to determine the order of the ranking.

piaso.pp.table(adata.obs['Subclass'], rank = True, ascending = True)
piaso.pp.table(adata.obs['Subclass'], rank = True, ascending = False)

In order to store the results as a dataframe, set as_dataframe = True.

subclass_df = piaso.pp.table(adata.obs['Subclass'], rank = True, ascending = False, as_dataframe = True)