jinworks / CellChat

R toolkit for inference, visualization and analysis of cell-cell communication from single-cell and spatially resolved transcriptomics
GNU General Public License v3.0
277 stars 45 forks source link

CellChat2 computeCommunProb error #16

Open kcavagnero opened 11 months ago

kcavagnero commented 11 months ago

Hi Sqjin,

Thank you for the great package. v1 has been such a great resource for the single cell community. Looking forward to using v2 with the larger L-R database but I am having some issues getting started.

When running computeCommunProb() I get the below error in if (object@options$data type != "rna") {: argument is of length zero

I suspect this is because dim(cellchat@data.signaling) 0,0

Previously with same data using v1 did not have this issue and dim(cellchat@data.signaling) 744,14891

For both versions I created my cellchat objects from seurat using the code below.

library(Seurat)
library(CellChat)
data.input <- GetAssayData(healthy, assay = "RNA", slot = "data") # normalized data matrix
Idents(healthy) <- "celltype"
labels <- Idents(healthy)
meta <- data.frame(group = labels, row.names = names(labels))
cellchat <- createCellChat(object = data.input, meta = meta, group.by = "group")

Any idea how to fix?

sessionInfo() R version 4.1.3 (2022-03-10) Platform: x86_64-conda-linux-gnu (64-bit) Running under: CentOS Linux 7 (Core)

Matrix products: default BLAS/LAPACK: /home/kecavagn/miniconda3/envs/seurat_cellchat2/lib/libopenblasp-r0.3.20.so

locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] patchwork_1.1.3 CellChat_2.0.0 Biobase_2.54.0
[4] BiocGenerics_0.40.0 ggplot2_3.4.4 igraph_1.5.1
[7] dplyr_1.1.4 sp_1.4-7 SeuratObject_4.1.0 [10] Seurat_4.1.1

loaded via a namespace (and not attached): [1] backports_1.4.1 circlize_0.4.15 uuid_1.1-1
[4] systemfonts_1.0.5 NMF_0.26 plyr_1.8.9
[7] repr_1.1.4 lazyeval_0.2.2 splines_4.1.3
[10] BiocParallel_1.28.3 listenv_0.9.0 scattermore_0.8
[13] ggnetwork_0.5.12 gridBase_0.4-7 digest_0.6.33
[16] foreach_1.5.2 htmltools_0.5.7 ggalluvial_0.12.5
[19] fansi_1.0.5 magrittr_2.0.3 tensor_1.5
[22] cluster_2.1.3 doParallel_1.0.17 ROCR_1.0-11
[25] sna_2.7-1 ComplexHeatmap_2.15.4 globals_0.16.2
[28] matrixStats_1.1.0 svglite_2.1.2 spatstat.sparse_2.1-1 [31] colorspace_2.1-0 ggrepel_0.9.4 crayon_1.5.2
[34] jsonlite_1.8.7 progressr_0.10.0 spatstat.data_2.2-0
[37] survival_3.3-1 zoo_1.8-10 iterators_1.0.14
[40] glue_1.6.2 polyclip_1.10-0 registry_0.5-1
[43] gtable_0.3.4 leiden_0.4.2 GetoptLong_1.0.5
[46] car_3.1-2 shape_1.4.6 future.apply_1.11.0
[49] abind_1.4-5 scales_1.2.1 rngtools_1.5.2
[52] rstatix_0.7.2 spatstat.random_2.2-0 miniUI_0.1.1.1
[55] Rcpp_1.0.11 viridisLite_0.4.2 xtable_1.8-4
[58] clue_0.3-65 reticulate_1.34.0 spatstat.core_2.4-2
[61] stats4_4.1.3 htmlwidgets_1.6.2 httr_1.4.7
[64] FNN_1.1.3.2 RColorBrewer_1.1-3 ellipsis_0.3.2
[67] ica_1.0-2 farver_2.1.1 pkgconfig_2.0.3
[70] sass_0.4.7 uwot_0.1.11 deldir_1.0-6
[73] utf8_1.2.4 labeling_0.4.3 tidyselect_1.2.0
[76] rlang_1.1.2 reshape2_1.4.4 later_1.3.1
[79] munsell_0.5.0 tools_4.1.3 cachem_1.0.8
[82] cli_3.6.1 generics_0.1.3 statnet.common_4.9.0 [85] broom_1.0.5 ggridges_0.5.3 evaluate_0.23
[88] stringr_1.5.1 fastmap_1.1.1 goftest_1.2-3
[91] fitdistrplus_1.1-8 purrr_1.0.2 RANN_2.6.1
[94] pbapply_1.7-2 future_1.33.0 nlme_3.1-157
[97] mime_0.12 compiler_4.1.3 plotly_4.10.3
[100] png_0.1-8 ggsignif_0.6.4 spatstat.utils_2.3-1 [103] tibble_3.2.1 bslib_0.5.1 stringi_1.8.1
[106] RSpectra_0.16-1 rgeos_0.5-9 lattice_0.20-45
[109] IRdisplay_1.1 Matrix_1.6-3 vctrs_0.6.4
[112] pillar_1.9.0 lifecycle_1.0.4 BiocManager_1.30.22
[115] GlobalOptions_0.1.2 spatstat.geom_2.4-0 lmtest_0.9-40
[118] jquerylib_0.1.4 RcppAnnoy_0.0.19 BiocNeighbors_1.12.0 [121] data.table_1.14.8 cowplot_1.1.1 irlba_2.3.5.1
[124] httpuv_1.6.12 R6_2.5.1 network_1.18.1
[127] promises_1.2.1 KernSmooth_2.23-20 gridExtra_2.3
[130] IRanges_2.28.0 parallelly_1.36.0 codetools_0.2-18
[133] MASS_7.3-57 rjson_0.2.21 withr_2.5.2
[136] presto_1.0.0 sctransform_0.3.3 S4Vectors_0.32.4
[139] mgcv_1.8-40 parallel_4.1.3 grid_4.1.3
[142] rpart_4.1.16 tidyverse_2.0.0 IRkernel_1.3
[145] coda_0.19-4 tidyr_1.3.0 carData_3.0-5
[148] Cairo_1.5-15 Rtsne_0.16 ggpubr_0.6.0
[151] pbdZMQ_0.3-7 shiny_1.8.0 base64enc_0.1-3

kcavagnero commented 11 months ago

Update: this issue resolves when switching from CellChatDB.use <- CellChatDB # simply use the default CellChatDB to CellChatDB.use <- subsetDB(CellChatDB, search = "Secreted Signaling", key = "annotation") # use Secreted Signaling

suye0620 commented 11 months ago

I think u can create CellChat from SeuratObj directly,rather than a normalized data matrix.😊

zzz1314520 commented 11 months ago

hi @suye0620 Thank you for your excellent work on Cellchat. Thanks for your attention. When running computeCommunProb() I get the below error

The suggested minimum value of scaled distances is in [1,2], and the calculated value here is  Inf 
  |                                                                                                                                                               |   0%Error in if (sum(P1_Pspatial) == 0) { : 
  missing value where TRUE/FALSE needed

and my data in object@data.project and object@data.signaling as below

> str(cellchat@data.signaling)
Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
  ..@ i       : int [1:199179] 20 21 45 77 108 109 122 136 145 171 ...
  ..@ p       : int [1:7126] 0 29 64 109 126 155 190 215 248 277 ...
  ..@ Dim     : int [1:2] 615 7125
  ..@ Dimnames:List of 2
  .. ..$ : chr [1:615] "SLITRK2" "APLN" "AGTR2" "PORCN" ...
  .. ..$ : chr [1:7125] "A02784B2_10_168" "A02784B2_76_240" "A02784B2_229_271" "A02784B2_12_152" ...
  ..@ x       : num [1:199179] 1.386 0.693 1.386 1.386 1.386 ...
  ..@ factors : list()
> str(cellchat@data.project)
 num [1:615, 1:7125] 0 0 0 0 0.000478 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:615] "SLITRK2" "APLN" "AGTR2" "PORCN" ...
  ..$ : chr [1:7125] "A02784B2_10_168" "A02784B2_76_240" "A02784B2_229_271" "A02784B2_12_152" ...

Thanks for your beautiful work! Looking forward for your reply and help!

xinmiaoyan commented 9 months ago

I've got the same error when I ran computeCommunProb(),

The suggested minimum value of scaled distances is in [1,2], and the calculated value here is  Inf 
  |                                                                                                                                                               |   0%Error in if (sum(P1_Pspatial) == 0) { : 
  missing value where TRUE/FALSE needed

do you have any solution regarding to this issue? thanks

sqjin commented 9 months ago

@xinmiaoyan Can you share your object so that we can replicate your error?

nsdelablancac commented 8 months ago

Hi all!

I was following the tutorial with my ST data and I got the following error: Error in nn.ranked[idx.i, ] : subscript out of bounds

I used an integrated Seurat object of eight samples to build data.input. I annotated the spots and let some of them as NA.

data.input <- Seurat::GetAssayData(together, slot = "data", assay = "Spatial") Idents(together) <- "Manual_annotation" meta = data.frame(labels = Idents(together), samples = together@meta.data$sample_id) # manually create a dataframe consisting of the cell labels rownames(meta) <- colnames(data.input) meta$labels <- factor(meta$labels, levels = levels(Idents(together))) meta$samples <- factor(meta$samples, levels = unique(together@meta.data$sample_id)) meta$labels = droplevels(meta$labels, exclude = NA) spatial.locs <- Seurat::GetTissueCoordinates(together, scale = NULL, cols = c("imagerow", "imagecol")) spot.size = 65 # the theoretical spot size (um) in 10X Visium

spatial.factors <- rbind(spatial.factors1, spatial.factors2, spatial.factors3, spatial.factors4, spatial.factors5, spatial.factors6, spatial.factors7, spatial.factors8) cellchat_sig <- createCellChat(object = data.input, meta = meta, group.by = "labels", datatype = "spatial", coordinates = spatial.locs,spatial.factors = spatial.factors) cellchat_sig@DB <- CellChatDB.secreted cellchat_sig <- subsetData(cellchat_sig) # This step is necessary even if using the whole database future::plan("multisession", workers = 8) options(future.rng.onMisuse="ignore") cellchat_sig@DB <- CellChatDB.secreted cellchat_sig <- subsetData(cellchat_sig) # This step is necessary even if using the whole database future::plan("multisession", workers = 8) options(future.rng.onMisuse="ignore") cellchat_sig@DB <- CellChatDB.secreted cellchat_sig <- subsetData(cellchat_sig) # This step is necessary even if using the whole database future::plan("multisession", workers = 8) options(future.rng.onMisuse="ignore") cellchat_sig <- identifyOverExpressedGenes(cellchat_sig) cellchat_sig <- identifyOverExpressedInteractions(cellchat_sig) cellchat_sig <- computeCommunProb(cellchat_sig, type = "truncatedMean", trim = 0.1, distance.use = FALSE, interaction.range = 250, scale.distance = NULL, contact.dependent = TRUE, contact.range = 100 ) truncatedMean is used for calculating the average gene expression per cell group. Error in nn.ranked[idx.i, ] : subscript out of bounds

str(cellchat_sig@data.signaling) Formal class 'dgCMatrix' [package "Matrix"] with 6 slots ..@ i : int [1:241066] 13 31 41 43 71 96 109 117 129 130 ... ..@ p : int [1:6024] 0 83 129 192 248 303 376 437 449 458 ... ..@ Dim : int [1:2] 702 6023 ..@ Dimnames:List of 2 .. ..$ : chr [1:702] "TNFRSF18" "TNFRSF4" "TNFRSF14" "TNFRSF25" ... .. ..$ : chr [1:6023] "PX0296_AAACACCAATAACTGC-1" "PX0296_AAACAGGGTCTATATT-1" "PX0296_AAACAGTGTTCCTGGG-1" "PX0296_AAACCGGGTAGGTACC-1" ... ..@ x : num [1:241066] 1.414 0.938 0.938 2.172 0.938 ... ..@ factors : list()

Does anyone know why it occurs? I will appreciate your help.

Best

Here is my R session:

sessionInfo() R version 4.3.2 (2023-10-31) Platform: x86_64-conda-linux-gnu (64-bit) Running under: Ubuntu 20.04.6 LTS

Matrix products: default BLAS/LAPACK: /anaconda3/envs/liana_env/lib/libopenblasp-r0.3.26.so; LAPACK version 3.12.0

locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=es_ES.UTF-8 LC_COLLATE=en_US.UTF-8 LC_MONETARY=es_ES.UTF-8
[6] LC_MESSAGES=en_US.UTF-8 LC_PAPER=es_ES.UTF-8 LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=es_ES.UTF-8 LC_IDENTIFICATION=C

time zone: Europe/Madrid tzcode source: system (glibc)

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] Seurat_4.4.0 SeuratObject_4.1.4 sp_2.1-3 future.apply_1.11.1 future_1.33.1 patchwork_1.2.0 CellChat_2.1.2
[8] Biobase_2.62.0 BiocGenerics_0.48.1 ggplot2_3.4.4 igraph_1.6.0 dplyr_1.1.4

loaded via a namespace (and not attached): [1] RcppAnnoy_0.0.22 splines_4.3.2 later_1.3.2 tibble_3.2.1 polyclip_1.10-6 ggnetwork_0.5.12
[7] lifecycle_1.0.4 rstatix_0.7.2 doParallel_1.0.17 globals_0.16.2 lattice_0.22-5 MASS_7.3-60.0.1
[13] dendextend_1.17.1 backports_1.4.1 magrittr_2.0.3 plotly_4.10.4 sass_0.4.8 jquerylib_0.1.4
[19] httpuv_1.6.14 NMF_0.30.4.900 sctransform_0.4.1 spatstat.sparse_3.0-3 reticulate_1.35.0 cowplot_1.1.3
[25] pbapply_1.7-2 RColorBrewer_1.1-3 abind_1.4-5 Rtsne_0.17 purrr_1.0.2 presto_1.0.0
[31] pkgmaker_0.32.10 circlize_0.4.15 IRanges_2.36.0 S4Vectors_0.40.2 ggrepel_0.9.5 irlba_2.3.5.1
[37] listenv_0.9.1 spatstat.utils_3.0-4 goftest_1.2-3 RSpectra_0.16-1 spatstat.random_3.2-2 fitdistrplus_1.1-11
[43] parallelly_1.36.0 svglite_2.1.3 leiden_0.4.3.1 codetools_0.2-19 tidyselect_1.2.0 shape_1.4.6
[49] viridis_0.6.5 matrixStats_1.2.0 stats4_4.3.2 spatstat.explore_3.2-6 jsonlite_1.8.8 GetoptLong_1.0.5
[55] BiocNeighbors_1.20.0 ellipsis_0.3.2 progressr_0.14.0 ggridges_0.5.6 ggalluvial_0.12.5 survival_3.5-7
[61] iterators_1.0.14 systemfonts_1.0.5 foreach_1.5.2 tools_4.3.2 sna_2.7-2 ica_1.0-3
[67] Rcpp_1.0.12 glue_1.7.0 gridExtra_2.3 xfun_0.41 withr_3.0.0 fastmap_1.1.1
[73] fansi_1.0.6 digest_0.6.34 R6_2.5.1 mime_0.12 colorspace_2.1-0 scattermore_1.2
[79] tensor_1.5 spatstat.data_3.0-4 utf8_1.2.4 tidyr_1.3.1 generics_0.1.3 data.table_1.14.10
[85] FNN_1.1.4 httr_1.4.7 htmlwidgets_1.6.4 uwot_0.1.16 pkgconfig_2.0.3 gtable_0.3.4
[91] registry_0.5-1 ComplexHeatmap_2.18.0 lmtest_0.9-40 htmltools_0.5.7 carData_3.0-5 clue_0.3-65
[97] scales_1.3.0 tidyverse_2.0.0 png_0.1-8 knitr_1.45 rstudioapi_0.15.0 reshape2_1.4.4
[103] rjson_0.2.21 coda_0.19-4.1 statnet.common_4.9.0 nlme_3.1-164 cachem_1.0.8 zoo_1.8-12
[109] GlobalOptions_0.1.2 stringr_1.5.1 KernSmooth_2.23-22 parallel_4.3.2 miniUI_0.1.1.1 pillar_1.9.0
[115] grid_4.3.2 vctrs_0.6.5 RANN_2.6.1 promises_1.2.1 ggpubr_0.6.0 car_3.1-2
[121] xtable_1.8-4 cluster_2.1.6 cli_3.6.2 compiler_4.3.2 rlang_1.1.3 crayon_1.5.2
[127] rngtools_1.5.2 ggsignif_0.6.4 plyr_1.8.9 stringi_1.8.3 viridisLite_0.4.2 network_1.18.2
[133] deldir_2.0-2 gridBase_0.4-7 BiocParallel_1.36.0 assertthat_0.2.1 munsell_0.5.0 lazyeval_0.2.2
[139] spatstat.geom_3.2-8 Matrix_1.6-5 shiny_1.8.0 ROCR_1.0-11 broom_1.0.5 bslib_0.6.1

sqjin commented 8 months ago

@nsdelablancac "I annotated the spots and let some of them as NA." This is not allowed in CellChat analysis. If these are low quality spots, you should delete them before running CellChat. BTW, the cell labels also should not have a label "0".

nsdelablancac commented 8 months ago

Thanks a lot for your early response.

I removed those spots in the Seurat object and I ensured the labels names. However, I still having the same error.

Thank you again for your help.