-
Notifications
You must be signed in to change notification settings - Fork 53
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
KeyError when scoring interactions in CellPhoneDB #190
Comments
thanks for authors and scRNA-seq genius answering my questions, |
Hi @GUOYF0412, Just to double-check, are you still having this issue since I couldn't be sure because the issue was closed and reopened. Best, |
thanks for your explanation, adata.X is here here are my harmony codes
besides, I resolve code errors by modifying the source code |
I also found an issue where score_interactions function works successfully in v4.1.0 and v5.0.0, but in v4.0.0 it also gives an error (keyerror), and when set to false, it runs successfully. |
perhaps the utlis file error, you could try to revise the original .py code. I did this and the score_interactions function works well! |
Probably the problems count for your adata count, which is the count data nor the log normalized data
From: XiongSQ ***@***.***>
Sent: Monday, June 3, 2024 11:06 AM
To: ventolab/CellphoneDB ***@***.***>
Cc: Jason GUO ***@***.***>; Mention ***@***.***>
Subject: Re: [ventolab/CellphoneDB] KeyError when scoring interactions in CellPhoneDB (Issue #190)
I also found an issue where score_interactions function works successfully in v4.1.0 and v5.0.0, but in v4.0.0 it also gives an error (keyerror), and when set to false, it runs successfully.
—
Reply to this email directly, view it on GitHub<#190 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AZNZINDQ3MH7BNTV4M5MMHDZFPMQRAVCNFSM6AAAAABIEXUIJ6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNBUGE4TGMRYHA>.
You are receiving this because you were mentioned.Message ID: ***@***.******@***.***>>
|
Sorry for the delayed response, and thank you very much for your reply. However, my adata X is indeed a normalized count matrix. And running in two other versions of db is normal. QAQ |
Hi@
Hi @cakirb, I have similar issue with same iterate RuntimeWarning: invalid value encountered in power I am using the newly updated CellphoneDB and database v5.0.0. And it is not the dash "-"issue. I was thinking is it the input value problem? They are normalized and scaled because the 3 samples need to be pooled with harmony. I have no clue what is wrong. |
Description
I encountered a KeyError when running the statistical analysis method in CellPhoneDB with interaction scoring enabled. I want to gracefully handle missing keys by returning np.nan instead of raising an error.
Steps to Reproduce
Load the anndata object with normalized counts and cell type metadata.
Generate the metadata file for CellPhoneDB.
Run the cpdb_statistical_analysis_method.call with the following parameters:
prepare data
cpdb_statistical_analysis_method.call
Version Release date
v5.0.0 2023-10-31
v4.1.0 2023-03-09
First Attempt
[CORE][22/05/24-17:42:04][INFO] Scoring interactions: Calculating scores for all interactions and cell types..
100%|██████████| 1444/1444 [05:53<00:00, 4.08it/s]
KeyError Traceback (most recent call last)
Cell In[15], line 1
----> 1 cpdb_results = cpdb_statistical_analysis_method.call(
2 cpdb_file_path = cpdb_file_path, # mandatory: CellphoneDB database zip file.
3 meta_file_path = meta_file_path, # mandatory: tsv file defining barcodes to cell label.
4 counts_file_path = counts_file_path, # mandatory: normalized count matrix - a path to the counts file, or an in-memory AnnData object
5 counts_data = 'hgnc_symbol', # defines the gene annotation in counts matrix.
6 # active_tfs_file_path = active_tf_path, # optional: defines cell types and their active TFs.
7 # microenvs_file_path = microenvs_file_path, # optional (default: None): defines cells per microenvironment.
8 score_interactions = True, # optional: whether to score interactions or not.
9 iterations = 1000, # denotes the number of shufflings performed in the analysis.
10 threshold = 0.1, # defines the min % of cells expressing a gene for this to be employed in the analysis.
11 threads = 5, # number of threads to use in the analysis.
12 debug_seed = 42, # debug randome seed. To disable >=0.
13 result_precision = 3, # Sets the rounding for the mean values in significan_means.
14 pvalue = 0.05, # P-value threshold to employ for significance.
15 subsampling = False, # To enable subsampling the data (geometri sketching).
16 subsampling_log = False, # (mandatory) enable subsampling log1p for non log-transformed data inputs.
17 subsampling_num_pc = 100, # Number of componets to subsample via geometric skectching (dafault: 100).
18 subsampling_num_cells = 1000, # Number of cells to subsample (integer) (default: 1/3 of the dataset).
19 separator = '|', # Sets the string to employ to separate cells in the results dataframes "cellA|CellB".
20 debug = False, # Saves all intermediate tables employed during the analysis in pkl format.
21 output_path = "./write/cellphonedb_out", # Path to save results.
22 output_suffix = "SMGs" # Replaces the timestamp in the output files by a user defined string in the (default: None).
23 )
File ~/miniconda3/envs/cpdb/lib/python3.8/site-packages/cellphonedb/src/core/methods/cpdb_statistical_analysis_method.py:157, in call(cpdb_file_path, meta_file_path, counts_file_path, counts_data, output_path, microenvs_file_path, active_tfs_file_path, iterations, threshold, threads, debug_seed, result_precision, pvalue, subsampling, subsampling_log, subsampling_num_pc, subsampling_num_cells, separator, debug, output_suffix, score_interactions)
154 if score_interactions:
155 # Make sure all cell types are strings
156 meta['cell_type'] = meta['cell_type'].apply(str)
--> 157 interaction_scores = scoring_utils.score_interactions_based_on_participant_expressions_product(
158 cpdb_file_path, counts4scoring, means_result.copy(), separator, meta, threshold, "cell_type", threads)
159 analysis_result['interaction_scores'] = interaction_scores
161 file_utils.save_dfs_as_tsv(output_path, output_suffix, "statistical_analysis", analysis_result)
File ~/miniconda3/envs/cpdb/lib/python3.8/site-packages/cellphonedb/utils/scoring_utils.py:344, in score_interactions_based_on_participant_expressions_product(cpdb_file_path, counts, means, separator, metadata, threshold, cell_type_col_name, threads)
340 cpdb_fms = scale_expression(cpdb_fmsh,
341 upper_range=10)
343 # Step 5: calculate the ligand-receptor score.
--> 344 interaction_scores = score_product(matrix=cpdb_fms,
345 means=means,
346 separator=separator,
347 interactions=interactions,
348 id2name=id2name,
349 threads=threads)
350 return interaction_scores
File ~/miniconda3/envs/cpdb/lib/python3.8/site-packages/cellphonedb/utils/scoring_utils.py:290, in score_product(matrix, interactions, means, separator, id2name, threads)
288 for ct_pair, lr_scores_filtered in results:
289 interacting_pair2score = dict(zip(lr_scores_filtered['interacting_pair'], lr_scores_filtered['score']))
--> 290 interaction_scores[ct_pair] = [interacting_pair2score[id] for id in interaction_scores['interacting_pair']]
292 return interaction_scores
File ~/miniconda3/envs/cpdb/lib/python3.8/site-packages/cellphonedb/utils/scoring_utils.py:290, in (.0)
288 for ct_pair, lr_scores_filtered in results:
289 interacting_pair2score = dict(zip(lr_scores_filtered['interacting_pair'], lr_scores_filtered['score']))
--> 290 interaction_scores[ct_pair] = [interacting_pair2score[id] for id in interaction_scores['interacting_pair']]
292 return interaction_scores
KeyError: 'COL11A1_integrin_a11b1_complex'
Proposed Solution:
I propose to modify the score_product function to handle missing keys gracefully by returning np.nan. Here is the modified score_product function:
KeyError: 'COL11A1_integrin_a11b1_complex'
Environment
The text was updated successfully, but these errors were encountered: