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
320 stars 51 forks source link

numpy.core._exceptions._UFuncNoLoopError: ufunc 'add' did not contain a loop with signature matching types (dtype('int64'), dtype('<U1')) -> None #138

Closed cirsina closed 1 year ago

cirsina commented 1 year ago

I'm new to python and cellphonedb, and I'm trying to analyze my data following the tutorial. I got this error when I start the analysis:

deconvoluted, means, relevant_interactions, significant_means = cpdb_degs_analysis_method.call(cpdb_file_path = 'cpdb_database/v4.1.0/cellphonedb.zip', meta_file_path = 'meta_for_cpdb.txt', counts_file_path = 'd25d47sc.qc.h5ad', degs_file_path = 'DEGs_for_cpdb.txt', counts_data = 'hgnc_symbol', threshold = 0.1, output_path = 'output') [ ][CORE][10/08/23-18:15:15][INFO] [Cluster DEGs Analysis] Threshold:0.1 Precision:3 [ ][CORE][10/08/23-18:15:15][WARNING]


DEGs ANALYSIS IS AN EXPERIMENTAL METHOD STILL UNDER DEVELOPMENT!


Reading user files... The following user files were loaded successfully: d25d47sc.qc.h5ad meta_for_cpdb.txt DEGs_for_cpdb.txt [ ][CORE][10/08/23-18:15:22][INFO] Running Real Analysis Traceback (most recent call last): File "", line 1, in File "/home/sun/miniconda3/lib/python3.9/site-packages/cellphonedb/src/core/methods/cpdb_degs_analysis_method.py", line 133, in call real_mean_analysis = cpdb_statistical_analysis_helper.mean_analysis(interactions_filtered, File "/home/sun/miniconda3/lib/python3.9/site-packages/cellphonedb/src/core/methods/cpdb_statistical_analysis_helper.py", line 359, in mean_analysis columns=(pd.Series(cluster1_names) + separator + pd.Series(cluster2_names)).values) File "/home/sun/miniconda3/lib/python3.9/site-packages/pandas/core/ops/common.py", line 81, in new_method return method(self, other) File "/home/sun/miniconda3/lib/python3.9/site-packages/pandas/core/arraylike.py", line 186, in add return self._arith_method(other, operator.add) File "/home/sun/miniconda3/lib/python3.9/site-packages/pandas/core/series.py", line 6112, in _arith_method return base.IndexOpsMixin._arith_method(self, other, op) File "/home/sun/miniconda3/lib/python3.9/site-packages/pandas/core/base.py", line 1348, in _arith_method result = ops.arithmetic_op(lvalues, rvalues, op) File "/home/sun/miniconda3/lib/python3.9/site-packages/pandas/core/ops/array_ops.py", line 232, in arithmetic_op res_values = _na_arithmetic_op(left, right, op) # type: ignore[arg-type] File "/home/sun/miniconda3/lib/python3.9/site-packages/pandas/core/ops/array_ops.py", line 171, in _na_arithmetic_op result = func(left, right) numpy.core._exceptions._UFuncNoLoopError: ufunc 'add' did not contain a loop with signature matching types (dtype('int64'), dtype('<U1')) -> None

The data looks fine:

adata = anndata.read_h5ad(d25d47sc.qc.h5ad) Traceback (most recent call last): File "", line 1, in NameError: name 'd25d47sc' is not defined adata = anndata.read_h5ad('d25d47sc.qc.h5ad') adata.shape (14482, 32481) metadata = pd.read_txt('meta_for_cpdb.txt', sep = '\t') Traceback (most recent call last): File "", line 1, in AttributeError: module 'pandas' has no attribute 'read_txt' metadata = pd.read_table('meta_for_cpdb.txt', sep = '\t') metadata.head(3) barcode_sample cell_type 0 AAACCCAAGTGGTCAG-1 2 1 AAACCCATCACTTGTT-1 6 2 AAACGAAAGAGCACTG-1 9 metadata.shape (14482, 2) list(adata.obs.index).sort() == list(metadata['barcode_sample']).sort() True pd.read_table('DEGs_for_cpdb.txt', sep = '\t').head(3) cluster gene 0 0 COL2A1 1 0 CNMD 2 0 SNORC

I do the analysis in Ubuntu 20.04, with conda and python 3.9.

How to deal with this error?

ktroule commented 1 year ago

Hi.

From the error you show I can't think what the problem is, I have the feeling the issue could be with one of the files you provide. Can run the same method but with the data we provided in the examples notebooks? This way we can discard whether CellPhoneDB is correctly installed in your environment.

Thanks

cirsina commented 1 year ago

@ktroule Thank you for your very kind suggestion. I ran cpdb with the example data without error, indicating that there's something wrong with my data. I exported the files from the seurat object 'a', with the following script:

a.markers <- FindAllMarkers(a, only.pos = TRUE, min.pct = 0.25, logfc.threshold = 0.3) DEGs.cpdb <- a.markers %>% filter(p_val_adj<0.05) DEGs.cpdb <- DEGs.cpdb %>% select(cluster, gene) write.table(DEGs.cpdb, file="degs_for_cpdb.txt", row.names=F, sep = "\t", quote = F) meta <- as.matrix(a$seurat_clusters) write.table(meta, file="meta_for_cpdb.txt", row.names=T, col.names = NA, sep = "\t", quote = F) a.counts <- as.matrix(a@assays$RNA@counts) write.table(a.counts, file="counts_for_cpdb.txt", row.names=T, col.names = NA, sep = "\t", quote = F)

I tried both h5ad and txt format for the counts file. h5ad file was obtained from the seurat object using sceasy.

Is there anything I can do to solve this problem?

Thanks again for your help.

cirsina commented 1 year ago

@ktroule Progress reporting

I think I solved the problem :) The Error occurs because I used the cluster number as cell type, but they are numeric rather than character. I changed the cell type names and the codes went through successfully.

liuyifang commented 1 year ago

i meet the same issue, maybe cellphonedb need to handle the case that when the cell_type is number instead of string that the user haven't annotate yet

ktroule commented 1 year ago

Thanks for the suggestion.