icbi-lab / infercnvpy

Infer copy number variation (CNV) from scRNA-seq data. Plays nicely with Scanpy.
https://infercnvpy.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
136 stars 27 forks source link

heatmap function is not recognising dendogram #71

Closed mmfalco closed 1 year ago

mmfalco commented 1 year ago

Report

Hi, I wanted to extract the dendogram from chromosome_heatmap plot but it is not possible, I would suggest that after running chromosome_heatmap function the dendogram could be stored in our adata object, or at least have the option to do so. Anyway, what I wanted to report is that if the user already has a fine tuned dendogram in adata (by previously running sc.tl.dendogram, as the prompt suggests when running cnv.pl.chromosome_heatmap), your pl.chromosome_heatmap function doesn't recognise the dendrogram_key, even though it is present in our adata object. After checking the code I see that there is a temporary object created ( tmp_adata = AnnData(X=adata.obsm[f"X_{use_rep}"], obs=adata.obs, dtype=adata.obsm[f"X_{use_rep}"].dtype) ) that looses any information not stored in obs, so it forces to calculate again pca and dendogram.

Example:

import infercnvpy as cnv
import scanpy as sc

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.tl.pca(adata)
cnv.pp.neighbors(adata)
cnv.tl.leiden(adata)

#here the user could fine tune the dendogram. which will be stored in adata.uns["dendrogram_cnv_leiden"]
sc.tl.dendrogram(
    adata,
    use_rep="X_cnv_pca",
    groupby="cnv_leiden"
)

#here dendogram and pca are calculated again because they could "not be found", when they shouldn't 
cnv.pl.chromosome_heatmap(adata, groupby="cnv_leiden", dendrogram="dendrogram_cnv_leiden")

the pl.heatmap function in scanpy specifies that the dendogram argument: "If True or a valid dendrogram key, a dendrogram based on the hierarchical clustering between the groupby categories is added."

PS. Thanks for this great package

Version information


anndata 0.8.0 hmmlearn 0.2.8 infercnvpy 0.1.dev161+ga7db710 matplotlib 3.6.2 numpy 1.21.0 pyarrow 11.0.0 scanpy 1.9.1 scipy 1.10.0 session_info 1.0.0

PIL 9.4.0 aa8f2297d25b4dc6fd3d98411eb3ba53823c4f42 NA absl NA asttokens NA astunparse 1.6.3 attr 22.2.0 backcall 0.2.0 certifi 2022.12.07 cffi 1.15.1 charset_normalizer 3.0.1 comm 0.1.2 cycler 0.10.0 cython_runtime NA dateutil 2.8.2 decorator 5.1.1 etils 1.0.0 executing 1.2.0 flatbuffers 23.1.21 fsspec 2023.1.0 gast NA google NA gtfparse NA h5py 3.8.0 idna 3.4 igraph 0.10.3 ipykernel 6.21.3 ipywidgets 8.0.4 jax 0.4.1 jaxlib 0.4.1 jedi 0.18.2 joblib 1.2.0 keras 2.11.0 kiwisolver 1.4.4 leidenalg 0.9.1 llvmlite 0.39.1 mpl_toolkits NA natsort 8.2.0 numba 0.56.4 opt_einsum v3.3.0 packaging 23.0 pandas 1.5.2 parso 0.8.3 pexpect 4.8.0 pickleshare 0.7.5 pkg_resources NA platformdirs 3.0.0 polars 0.16.7 prompt_toolkit 3.0.37 psutil 5.9.4 ptyprocess 0.7.0 pure_eval 0.2.2 pycparser 2.21 pygments 2.14.0 pynndescent 0.5.8 pyparsing 3.0.9 pyreadr 0.4.7 pytz 2022.7.1 requests 2.28.2 rich NA setuptools 65.6.3 six 1.16.0 sklearn 1.2.0 stack_data 0.6.2 tensorboard 2.11.2 tensorflow 2.11.0 termcolor NA texttable 1.6.7 threadpoolctl 3.1.0 tornado 6.2 tqdm 4.64.1 traitlets 5.9.0 typing_extensions NA umap 0.5.3 urllib3 1.26.14 wcwidth 0.2.6 wrapt 1.15.0 yaml 6.0 zmq 25.0.0 zoneinfo NA

IPython 8.10.0 jupyter_client 8.0.3 jupyter_core 5.2.0

Python 3.10.9 (main, Jan 11 2023, 15:21:40) [GCC 11.2.0] Linux-5.4.0-144-generic-x86_64-with-glibc2.31

grst commented 1 year ago

Hi @mmfalco,

thanks for reporting this. The solution would be to add uns to the temporary AnnData object. Do you want to create a PR?

Cheers, Gregor

mmfalco commented 1 year ago

@grst done ;)