Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Heatmaps look for existing colors in adata.uns #46

Merged
merged 2 commits into from
Oct 6, 2022

Conversation

codyheiser
Copy link
Contributor

The issue: I found that because cnv.pl.chromosome_heatmap and cv.pl.chromosome_heatmap_summary create a copy of the adata input in CNV-space, colors mapped to categorical adata.obs fields were not transferred to this tmp_adata and thus didn't make the plots.

The fix: I simply added a quick check for "{}_colors".format(groupby) in adata.uns, and add a tmp_adata.uns["{}_colors".format(groupby)] key with the corresponding colors from the original adata if so.

Minimal reproducible example: Adapted from tutorial_3k

Here's a PDF of a notebook containing the below code for convenience:
infercnvpy_heatmap_color_fix.pdf

import scanpy as sc
import infercnvpy as cnv
import matplotlib.pyplot as plt

sc.settings.set_figure_params(figsize=(5, 5))

adata = cnv.datasets.maynard2020_3k()

cnv.tl.infercnv(
    adata,
    reference_key="cell_type",
    reference_cat=[
        "B cell",
        "Macrophage",
        "Mast cell",
        "Monocyte",
        "NK cell",
        "Plasma cell",
        "T cell CD4",
        "T cell CD8",
        "T cell regulatory",
        "mDC",
        "pDC",
    ],
    window_size=250,
)

cnv.pl.chromosome_heatmap(adata, groupby="cell_type")

# these should match colors in above heatmap
adata.uns["cell_type_colors"]

# define custom color dictionary for a couple cell types
colordict = {
    "Epithelial cell":"#e377c2",  # pink
    "Endothelial cell":"#d62728",  # red
}
# let's change colors for cell_type in adata
for x in list(colordict.keys()):
    adata.uns["cell_type_colors"][list(adata.obs.cell_type.cat.categories).index(x)] = colordict[x]

# indexes 4 and 5 should be updated now
adata.uns["cell_type_colors"]

# now, Epithelial cell should be pink and Endothelial cell should be red
cnv.pl.chromosome_heatmap(adata, groupby="cell_type")

Copy link
Member

@grst grst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,
thanks a lot for putting this PR together! The changes look good to me!

Unfortunately the CI is currently broken (will be fixed by #42) and I would like to wait until this is merged before I am going to merge this PR and make a release. This will likely take a few more weeks.

Cheers,
Gregor

@codecov-commenter
Copy link

Codecov Report

Merging #46 (de7b62d) into main (2452241) will increase coverage by 0.33%.
The diff coverage is 100.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #46      +/-   ##
==========================================
+ Coverage   65.67%   66.01%   +0.33%     
==========================================
  Files          13       13              
  Lines         405      409       +4     
==========================================
+ Hits          266      270       +4     
  Misses        139      139              
Flag Coverage Δ
unittests 66.01% <100.00%> (+0.33%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/infercnvpy/pl/_chromosome_heatmap.py 96.00% <100.00%> (+0.34%) ⬆️

@grst grst merged commit 7c90b11 into icbi-lab:main Oct 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants