zktuong / ktplotspy

Python library for plotting Cellphonedb results. Ported from ktplots R package.
https://ktplotspy.readthedocs.io/
MIT License
41 stars 11 forks source link

Difficulty: “key_error” in dot plot for CellphoneDB output #73

Open ums2026 opened 2 weeks ago

ums2026 commented 2 weeks ago

Hello,

I am trying to create a dot plot for a Cellphone DB output, but am consistently receiving a “key_error”: “cell_type”.

My h5ad counts file looks like this one (from the CellphoneDB documentation) when I open it in a notebook:

image

Given that it looks like it is formatted properly, I am not sure why it says that I am missing something - when I run adata.obs_keys(), I do get an empty list in my data, but if that is the issue, I am not sure how to fix that. All the analyses are running correctly with this file in CellphoneDB. Is there any way to also input the metadata file to the plotting to fix this issue? Or is there some other problem causing the cell_type error?

Here is the exact error:

image

Thank you in advance!

zktuong commented 2 weeks ago

hi @ums2026, yes so basically you need to have cell_type in adata.obs and this is something that you can manually do like in the cellphonedb tutorial, do something like:

# read in the metadata file as per original tutorial
metadata = pd.read_csv(meta_file_path, sep = '\t')
# check the first 3 rows
# metadata.head(3)
# set the indices so that they match the adata.obs_names
metadata.set_index("barcode_sample", inplace=True) 
# now simply transfer
adata.obs["cell_type"] = metadata["cell_type"]

In a real-life scenario, the adata.obs would already contain this column if you were working with adata to begin with.

@luzgaral is aware of the same issue and would be pushing out an updated notebook shortly on cellphonedb's repo

ums2026 commented 1 week ago

This is odd - we have worked with the same adata variable and were able to run the analysis on it, but it did not have the column.

I tried running it like this, with a new transfer, only this time, I ended up with a new error, that says "ValueError: No significant results found." I'm not sure how to fix that - do you have any suggestions? I am able to get a heatmap, but for some reason, I am getting this issue with a dot plot.

Thank you for the help! Uma

zktuong commented 1 week ago

This is odd - we have worked with the same adata variable and were able to run the analysis on it, but it did not have the column.

yes that's because cellphonedb expects the 2 files (adata.h5ad and meta.txt) separately, but does not require that the adata.obs to contain the information in the meta.txt, whereas ktplotspy expects that that column already exists. I can certainly put in the option for ktplotspy to accept the meta.txt as well, to keep things simple.

I tried running it like this, with a new transfer, only this time, I ended up with a new error, that says "ValueError: No significant results found." I'm not sure how to fix that - do you have any suggestions? I am able to get a heatmap, but for some reason, I am getting this issue with a dot plot.

Can you show me the full command you used? did you specify genes/gene familites?

ums2026 commented 5 days ago

Here is the command:


kpy.plot_cpdb(
    adata=adata,
    cell_type1="Oligo1",
    cell_type2=".", 
    means=means,
    pvals=pvalues,
    celltype_key="cell_type",
    figsize=(13, 4),
    title="interactions",
    keep_id_cp_interaction=True,
)

But I have also tried without specifying any cell types, this way:

kpy.plot_cpdb(
    adata=adata,
    cell_type1=".",
    cell_type2=".",  # this means all cell-types
    means=means,
    pvals=pvalues,
    celltype_key="cell_type",
    figsize=(13, 4),
    title="interacting interactions!",
    keep_id_cp_interaction=True,
)

I did not specify genes/gene families. Is this a requirement?

Thank you!

zktuong commented 5 days ago

hmm would you be able to share your input files and h5ad so that i can do some tests? if it's not too big, you can send them to my email z.tuong@uq.edu.au or we can find a more secure solution?