Plot customizations#

Objective

This vignette contains various snippets of code
that show how plots and data can be customized
to ones requirements.

The h5 file used in this notebook can be found here

import missionbio.mosaic as ms

sample = ms.load_example_dataset('3 cell mix')

All the interactive plotting functions return a plotly figure. In case the layout or the color
scheme is not suitable for your data type, they can be changed before creating the final figure.

  1. The color for the plots are store either in the individual traces or the layout attributes of the plotly figure.

  2. Mosaic also contains a list of colors that can be used to customize the plots.

  3. Most importantly the mosaic configuration can be modified for certain layers of the assays, and that will affect the color scale in all plots for those layers

# Plot the first few colors
import seaborn as sns
sns.palplot(ms.COLORS[:21])
../_images/57a69e336d2c4e0adaf61a22c054b1fb19877d527b0089155a1501ef653c773b.png

Configuration options#

# The configuration of the colorscales is stored in ms.Config.Colorscale

ms.Config.Colorscale
Color palettes for plotting.
Options: ['Cnv', 'Dna', 'Protein', 'Rna']
# The default values for each assay are stored in the respective options

ms.Config.Colorscale.Dna.NGT  # This is the default colorscale for the DNA NGT layer in all plots
[(0.0, '#3B4D73'),
 (0.25, '#3B4D73'),
 (0.25, '#78A3BC'),
 (0.5, '#78A3BC'),
 (0.5, '#D7ECEE'),
 (0.75, '#D7ECEE'),
 (0.75, '#000000'),
 (1.0, '#000000')]

Updating the colors for plots#

Colors of the values#

# The default color scale for NGT is a monochromatic blue color scheme.

fig = sample.dna.heatmap('NGT')
fig.show("jpg")
../_images/f4d3dbb500c30e2daa1dbbe06fc473a0da804557b55834aa7f2d04c7b315d7ac.jpg
# Assuming these are new desired colors
# NGT=0 (WT) - grey
# NGT=1 (HET) - brown
# NGT=2 (HOM) - purple
# NGT=3 (missing) - black

wt_col = ms.COLORS[9]
het_col = ms.COLORS[5]
hom_col = ms.COLORS[4]
miss_col = ms.COLORS[-1]

sns.palplot([wt_col, het_col, hom_col, miss_col])
../_images/b96b014cad309e867054eb28bce1962c49c4b862f1288b8bd1ee3c27b5dc0872.png
# Update the coloraxis to make a plot with the new colors

ms.Config.Colorscale.Dna.NGT = [
    (0 / 4, wt_col), (1 / 4, wt_col),
    (1 / 4, het_col), (2 / 4, het_col),
    (2 / 4, hom_col), (3 / 4, hom_col),
    (3 / 4, miss_col), (4 / 4, miss_col)
]
# All subsequent plots using NGT will use the above colorscale

fig = sample.dna.heatmap("NGT")
fig.show("jpg")
../_images/9ca9e4e51786cf39fa517111d26c4904f971917037faaf0490f1ecb32bd6d195.jpg
# The same method can be used to update scatterplot which are colored by NGT

fig = sample.dna.scatterplot('umap', colorby='NGT', features=sample.dna.ids()[:4])
fig.show("jpg")
../_images/730a9c94dcc2b28873a11d7d0e43d40b27d456b501cf62671b7a4c24d8227b16.jpg

Colors of the labels#

Now the colors in the heatmap conflict with the colors in the labels. To customize those, the palette can be changed

# This is the current palette

sample.dna.get_palette()
{'Jurkat': '#1f77b4',
 'KG-1': '#ff7f0e',
 'Mixed': '#c7c7c7',
 'TOM-1': '#d62728'}
# Update this palette. It is not required to use the built in colors
# Any hexadecimal colors can be passed.

new_palette = {
    'Jurkat': ms.COLORS[3],
    'KG-1': ms.COLORS[4],
    'Mixed': '#c7c7c7',  # Use hexadecimal colors
    'TOM-1': ms.COLORS[5]
}

sample.dna.set_palette(new_palette)
# Make the heatmap with the new colors

fig = sample.dna.heatmap('NGT')
fig.show("jpg")
../_images/be6381b2709adc476d17d26de658ea507c437364227fbd6f85dbcdf0c44d714a.jpg

Scaling the plots#

Often the cnv heatmaps contain too many genes or amplicons to fit in the default layout.
This is usually not an issue when they are interactive, but when exporting as static images
it hinder the ability to interpret them.

Plotly provides an option to convert interactive figures to static images

# Scale the figure width and plot as a static image.
# Double click on the plot to zoom-in and improve the resolution

import missionbio.mosaic.utils as mutils

fig = sample.cnv.heatmap('ploidy', features='genes')
fig.layout.width = 1600
fig.show("jpg")
../_images/2105191206ad9625bad8278831e58812e303032d8d146aca6e8eed947c85ef1c.jpg

Smoothening the values#

In case the color scale get skewed to high poidy, a max value can be imposed to generate a more interpretable heatmap

The colorscale can also be changed as desired. A list of color scales can be found in the plotly documentation

# The plots can also be smoothed using a moving average with the convolve parameter

fig = sample.cnv.heatmap('ploidy', features='genes', convolve=5)
fig.show("jpg")
../_images/42fd304d7b61c3ba857cab52d4d97fd3941471fd4763b9283b07208620ccfce6.jpg

Filtering the data#

Often the number of amplicons in CNV might take over the sample level heatmap making the plot uninterpretable. Moreover there might be certain non-differentiating variants and protein in the panel. These can be dropped before making the final heatmap.

# The genes to plot on the sample heatmap

genes = ['EZH2', 'TET2']
# When `features` is None, all the ids are clustered and plotted
fig = sample.heatmap(
    ("dna", "protein", "cnv"),
    attributes=("NGT", "normalized_counts", "ploidy"),
    features=(None, None, genes)
)

# Update the width of the plot [See the section on CNV heatmaps]
fig.layout.width = 1600

# Show a static plot
fig.show("jpg")
../_images/b864b8d9acce828d5c7056d186d45cf4c2ff0155b0694dc3939b7ad40547cccc.jpg

Resetting the configuration#

Notice that the DNA colorscale is still the same as the one set in the configuration

All configuration values can be reset using the reset method.

ms.Config.Colorscale.Dna.reset("NGT")  # This will reset the NGT colorscale

# To recursively reset all colorscales of the Dna assay
# run the `reset` function of the `Colorscale` option.
ms.Config.Colorscale.reset("Dna")

ms.Config.Colorscale.reset()  # By skipping the parameter, all values will be reset i.e. Dna, Cnv, and Protein
# Creating the same plot again will 
fig = sample.heatmap(
    ("dna", "protein", "cnv"),
    attributes=("NGT", "normalized_counts", "ploidy"),
    features=(None, None, genes)
)

# Update the width of the plot [See the section on CNV heatmaps]
fig.layout.width = 1600

# Show a static plot
fig.show("jpg")
../_images/a8b7f897b6b5b65b7ae9e6d31c23e6a49c23497ce0b58188950b57db9d04487f.jpg

Saving the plots#

All plotly figures can be saved as .jpg, .png, .svg, and .webp formats

fig = sample.dna.heatmap("NGT")

fig.write_image("./saved_image.pdf")  # Save as a vector .pdf file
fig.write_image("./saved_image.svg")  # Save as a vector .svg file
fig.write_image("./saved_image.jpg")  # Save as a .jpeg
fig.write_image("./saved_image.png")  # Save as a .png
fig.write_image("./saved_image.webp")  # Save as a .webp