ventolab / CellphoneDB

CellPhoneDB can be used to search for a particular ligand/receptor, or interrogate your own HUMAN single-cell transcriptomics data.
https://www.cellphonedb.org/
MIT License
304 stars 52 forks source link

KeyError #170

Open Inbarpl opened 4 months ago

Inbarpl commented 4 months ago

Hi again, Sorry but I'm not good with python Trying to generate visualization according to T3 with my input data but I keep getting this Error I'v tried altering celltype_key value but could no understand what is the problem here Thank you very much in advance Inbar:

kpy.plot_cpdb( adata = adata, cell_type1 = "0|15|11", cell_type2 = "2", means = cpdb_results['means'], pvals = cpdb_results['relevant_interactions'], celltype_key = "cell_labels", genes = ["TGFB2", "CSF1R", "COL1A2"], figsize = (11,4), title = "Interactions_V1_V2", max_size = 5, highlight_size = 0.75, degs_analysis = True, standard_scale = True, interaction_scores = cpdb_results['interaction_scores'], scale_alpha_by_interaction_scores=True, )

KeyError Traceback (most recent call last) File ~/A/projects_new/Michal_S_cellphoneDB/miniconda3/envs/cpdbv5/lib/python3.8/site-packages/pandas/core/indexes/base.py:3653, in Index.get_loc(self, key) 3652 try: -> 3653 return self._engine.get_loc(casted_key) 3654 except KeyError as err:

File ~/A/projects_new/Michal_S_cellphoneDB/miniconda3/envs/cpdbv5/lib/python3.8/site-packages/pandas/_libs/index.pyx:147, in pandas._libs.index.IndexEngine.get_loc()

File ~/A/projects_new/Michal_S_cellphoneDB/miniconda3/envs/cpdbv5/lib/python3.8/site-packages/pandas/_libs/index.pyx:176, in pandas._libs.index.IndexEngine.get_loc()

File pandas/_libs/hashtable_class_helper.pxi:7080, in pandas._libs.hashtable.PyObjectHashTable.get_item()

File pandas/_libs/hashtable_class_helper.pxi:7088, in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'cell_labels'

The above exception was the direct cause of the following exception:

KeyError Traceback (most recent call last) Cell In[30], line 1 ----> 1 kpy.plot_cpdb( 2 adata = adata, 3 cell_type1 = "0|15|11", 4 cell_type2 = "2", 5 means = cpdb_results['means'], 6 pvals = cpdb_results['relevant_interactions'], 7 celltype_key = "cell_labels", 8 genes = ["TGFB2", "CSF1R", "COL1A2"], 9 figsize = (11,4), 10 title = "Interactions_V1_V2", 11 max_size = 5, 12 highlight_size = 0.75, 13 degs_analysis = True, 14 standard_scale = True, 15 interaction_scores = cpdb_results['interaction_scores'], 16 scale_alpha_by_interaction_scores=True, 17 )

File ~/A/projects_new/Michal_S_cellphoneDB/miniconda3/envs/cpdbv5/lib/python3.8/site-packages/ktplotspy/plot/plot_cpdb.py:230, in plot_cpdb(adata, cell_type1, cell_type2, means, pvals, celltype_key, interaction_scores, cellsign, degs_analysis, splitby_key, alpha, keep_significant_only, genes, gene_family, custom_gene_family, standard_scale, cluster_rows, cmap_name, max_size, max_highlight_size, default_style, highlight_col, highlight_size, special_character_regex_pattern, exclude_interactions, title, return_table, figsize, min_interaction_score, scale_alpha_by_interaction_scores, scale_alpha_by_cellsign, filter_by_cellsign, keep_id_cp_interaction) 228 else: 229 query = [i for i in means_mat.interacting_pair if re.search("|".join(genes), i)] --> 230 metadata = ensure_categorical(meta=metadata, key=celltype_key) 231 # prepare regex query for celltypes 232 if splitby_key is not None:

File ~/A/projects_new/Michal_S_cellphoneDB/miniconda3/envs/cpdbv5/lib/python3.8/site-packages/ktplotspy/utils/support.py:111, in ensure_categorical(meta, key) 96 def ensure_categorical(meta: pd.DataFrame, key: str) -> pd.DataFrame: 97 """Enforce categorical columns. 98 99 Parameters (...) 109 Table with formatted column. 110 """ --> 111 if not is_categorical(meta[key]): 112 meta[key] = meta[key].astype("category") 113 return meta

File ~/A/projects_new/Michal_S_cellphoneDB/miniconda3/envs/cpdbv5/lib/python3.8/site-packages/pandas/core/frame.py:3761, in DataFrame.getitem(self, key) 3759 if self.columns.nlevels > 1: 3760 return self._getitem_multilevel(key) -> 3761 indexer = self.columns.get_loc(key) 3762 if is_integer(indexer): 3763 indexer = [indexer]

File ~/A/projects_new/Michal_S_cellphoneDB/miniconda3/envs/cpdbv5/lib/python3.8/site-packages/pandas/core/indexes/base.py:3655, in Index.get_loc(self, key) 3653 return self._engine.get_loc(casted_key) 3654 except KeyError as err: -> 3655 raise KeyError(key) from err 3656 except TypeError: 3657 # If we have a listlike key, _check_indexing_error will raise 3658 # InvalidIndexError. Otherwise we fall through and re-raise 3659 # the TypeError. 3660 self._check_indexing_error(key)

KeyError: 'cell_labels'

datasome commented 4 months ago

Hi Inbarpl,

ktplotspy is an external package to CellphoneDB - if what I'm about to write below doesn't help, you may want to raise an issue on https://github.com/zktuong/ktplotspy instead.

However, from what I see in line 230 of https://github.com/zktuong/ktplotspy/blob/master/ktplotspy/plot/plot_cpdb.py, it looks as if you may not have the column 'cell_labels' in your adata.obs - do you? See also https://ktplotspy.readthedocs.io/en/latest/notebooks/tutorial.html on the use of plot_cpdb function - if that helps.

Best,

Robert.