Open GUOYF0412 opened 5 months ago
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. If you're still having the issues, could you make sure that your data was normalised without z-scaling so the zeros in the matrix are not transformed?
Best, Batu
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. If you're still having the issues, could you make sure that your data was normalised without z-scaling so the zeros in the matrix are not transformed?
Best, Batu
thanks for your explanation, adata.X is here
and the adata is here
here are my harmony codes
def run_harmony(adata):
# normalize
sc.pp.normalize_total(adata, target_sum=1e4)
sc.pp.log1p(adata)
adata.raw = adata
adata.layers['lognorm'] = adata.X.copy()
# HVGs
sc.pp.highly_variable_genes(adata, min_mean=0.0125, max_mean=3, min_disp=0.5)
# regress mt genes
sc.pp.regress_out(adata, 'pct_counts_mt')
# scale
sc.pp.scale(adata)
# regress cell cycle
sc.tl.score_genes_cell_cycle(adata, s_genes=s_genes, g2m_genes=g2m_genes)
sc.pp.regress_out(adata, ['S_score', 'G2M_score'])
# scale
sc.pp.scale(adata)
# pca
sc.tl.pca(adata, svd_solver='arpack')
sc.external.pp.harmony_integrate(adata, key='sample')
sc.pp.neighbors(adata, n_neighbors=10, n_pcs=40, use_rep='X_pca_harmony')
sc.tl.umap(adata)
# res
for i in [0.1, 0.2, 0.3, 0.5, 0.8, 1]:
sc.tl.leiden(adata,resolution=i, key_added='leiden {}'.format(i))
if not os.path.isdir('harmony'):
os.mkdir('harmony')
sc.pl.umap(adata,color=['leiden 0.1','leiden 0.2','leiden 0.3'],show = False)
plt.savefig('harmony/resolution_low.pdf')
sc.pl.umap(adata,color=['leiden 0.5','leiden 0.8','leiden 1'],show = False)
plt.savefig('harmony/resolution_high.pdf')
return adata
besides, I resolve code errors by modifying the source code my new question is that there are so many warning when I running the analysis method RuntimeWarning: invalid value encountered in power geom = np.power(sub_prod, 1 / len(sub_values))
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.
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 GitHubhttps://github.com/ventolab/CellphoneDB/issues/190#issuecomment-2144193288, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AZNZINDQ3MH7BNTV4M5MMHDZFPMQRAVCNFSM6AAAAABIEXUIJ6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNBUGE4TGMRYHA. You are receiving this because you were mentioned.Message ID: @.**@.>>
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 unsubscribehttps://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 @GUOYF0412,
Just to double-check, are you still having this issue since I couldn't be sure because the issue was closed and reopened. If you're still having the issues, could you make sure that your data was normalised without z-scaling so the zeros in the matrix are not transformed?
Best, Batu
Hi@
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. If you're still having the issues, could you make sure that your data was normalised without z-scaling so the zeros in the matrix are not transformed?
Best, Batu
Hi @cakirb,
I have similar issue with same iterate RuntimeWarning: invalid value encountered in power geom = np.power(sub_prod, 1 / len(sub_values)) and a KeyError.
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
sc.settings.verbosity = 1 # verbosity: errors (0), warnings (1), info (2), hints (3) sys.executable
cpdb_statistical_analysis_method.call
Version Release date v5.0.0 2023-10-31 v4.1.0 2023-03-09
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