sqjin / CellChat

R toolkit for inference, visualization and analysis of cell-cell communication from single-cell data
GNU General Public License v3.0
623 stars 142 forks source link

Error with computeCommunProb (CellChat 2.1.0) #731

Closed MrModenait closed 9 months ago

MrModenait commented 9 months ago

I am following the vignette titled CellChat analysis of multiple spatial transcriptomics datasets, but I am having an issue with computeCommunProb. The issue is reminiscent of #245 (July 2021, last updated September 2022) and #300 (October 2021, last updated July 2023). Briefly, I am trying to use CellChat to analyse some data that has been collcted with the NanoString GeoMx machine. The data I have consists of 100 polygons across four different slides, each polygon having transcriptomic data for 9390 genes and associated x- and y-coordinates for that slide. I am trying to apply CellChat to this dataset despite it not being a 10X Visium dataset (each polygon contains between 70 and 330 nuclei, and the distance between polygons varies from 33.5 to 11205.4 micrometers). However, I am trying to input the data in the correct format nevertheless in the hope of obtaining some results.

When running the computeCommunProb, I get an error:

cellchat <- computeCommunProb(cellchat, 
                                type = "triMean",#type = "truncatedMean", trim = 0.1,
                                distance.use = FALSE, 
                                interaction.range = 250, 
                                contact.knn = TRUE, contact.knn.k = 6)
Error in if (sum(P1_Pspatial) == 0) { : 
  missing value where TRUE/FALSE needed

The problem also persists if I run the command without any additional arguments.

computeCommunProb(cellchat)
triMean is used for calculating the average gene expression per cell group. 
[1] ">>> Run CellChat on spatial transcriptomics data using distances as constraints of the computed communication probability <<< [2023-12-05 11:59:49.602012]"
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
In addition: Warning messages:
1: In min(d.spatial, na.rm = TRUE) :
  no non-missing arguments to min; returning Inf
2: In min(d.spatial, na.rm = TRUE) :
  no non-missing arguments to min; returning Inf

Following issue #300, I have checked that I have no negative values in my gene expression data. The row names are gene names whereas the column names are the names of my one-hundred segments, also known as regions of interest (ROIs).

         001_I    002_I   003_TI   004_TI   005_TC
A2M   8.268826 8.458067 6.546188 5.514221 6.226592
ACADM 4.924187 5.193381 4.665821 4.051230 4.153149
ACAT1 4.964649 5.151507 4.269605 3.862591 4.031447
PSEN1 4.795512 5.234075 5.905131 5.838387 5.963714
SGCA  4.121685 4.455186 3.544460 3.793801 3.589125
min(data_q_norm_log_transformed)
[1] 0.4137538

The metadata contains the column names as row names, in the same order, and with twenty-seven metadata labels including a slices column.

The same row names are found in the coordinates data frame, which is read successfully when running createCellChat:

        x_cent y_cent
001_I    13860  30805
002_I    14226  30353
003_TI   13014  31401
004_TI   13003  32921
005_TC   14178  31722
006_TC   12915  30798

Finally, I have specified ratio and tol in a ratio_tol_df data frame, with slide names as my rownames. Ratio has been found from reading the images in QuPath while tol was calculating as taking half the distance between the two closest ROIs.

The cellchat object was correctly constructed and I used "Secreted Signaling" as my subset from CellChatDB.

cellchat <- createCellChat(object = data_q_norm_log_transformed,
                            meta = metadata,
                            group.by = "labels",
                            datatype = "spatial",
                            coordinates = coordinates,
                            scale.factors = ratio_tol_df)
[1] "Create a CellChat object from a data matrix"
Create a CellChat object from spatial transcriptomics data... 
Set cell identities for the new CellChat object 
The cell groups used for CellChat analysis are  I-Exhausted CD8+ T/Myeloid cells/Plasma cells I-Exhausted CD8+ T/Pericytes/Plasma cells I-FB-like cells/Myeloid cells/Pericytes I-Follicular B cells/Exhausted CD8+ T/NK cells I-Myeloid cells TBV TC TI-Exhausted CD8+ T/Myeloid cells/Plasma cells TI-Exhausted CD8+ T/Pericytes/Plasma cells TI-FB-like cells/Myeloid cells/Pericytes TI-FB-like cells/Myeloid cells/Pericytes & Exhausted CD8+ T/Myeloid cells/Plasma cells TI-Follicular B cells/Exhausted CD8+ T/NK cells TI-Myeloid cells TI-Unknown

As you can see, the Idents taken from labels are quite long, ranging from 2 to 86 characters.

cellchat
An object of class CellChat created from a single dataset 
 9390 genes.
 100 cells. 
CellChat analysis of spatial data! The input spatial locations are 
       x_cent y_cent
001_I   13860  30805
002_I   14226  30353
003_TI  13014  31401
004_TI  13003  32921
005_TC  14178  31722
006_TC  12915  30798

I have also dug into the source for this function, presumerably for an older version of CellChat, to find out what the P1_Pspatial vector is. It is the product of P1 and P.spatial, P1 being equal to dataLR^n/(Kh^n + dataLR^n) and P.spatial being equal to 1/d.spatial (itself a result of running computeRegionDistance earlier in the code). I tried to run the code I found line by line but this was not possible due to the many errors I got, mainly because the source code dates from an older version of CellChat. The source refers to spot.size and spot.size.fullres but these seem to be replaced by ratio and tol in the version of the function that I have installed.

To address this, I downloaded CellChat 1.6.1 in a separate library and was able to get it to successfully run. I tried to run the computeCommunProbfrom this older package, but got a different error message:

cellchat <- computeCommunProb(cellchat)
Error in computeRegionDistance(coordinates = data.spatial, group = group, :
dims [product 3] do not match the length of object [4]

Oddly enough, I can get computeRegionDistance to run separately when I am offered the opportunity to specify group.

  distances <- computeRegionDistance(coordinates = coordinates,group = metadata$labels)
  distances[1:3,1:3]
I-Exhausted CD8+ T/Myeloid cells/Plasma cells I-Exhausted CD8+ T/Pericytes/Plasma cells I-FB-like cells/Myeloid cells/Pericytes
I-Exhausted CD8+ T/Myeloid cells/Plasma cells                                         0.000                                  4701.772                                5265.207
I-Exhausted CD8+ T/Pericytes/Plasma cells                                          4701.772                                     0.000                                7264.687
I-FB-like cells/Myeloid cells/Pericytes                                            5265.207                                  7264.687                                   0.000

It seems I can't find a solution when running the older package. With this said, what could be the cause of my problem? For the record:

sessionInfo()
R version 4.3.0 (2023-04-21 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.utf8 
[2] LC_CTYPE=English_United Kingdom.utf8   
[3] LC_MONETARY=English_United Kingdom.utf8
[4] LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.utf8    

time zone: Europe/Stockholm
tzcode source: internal
krasole commented 9 months ago

Having the exact same issue...

MrModenait commented 9 months ago

Having the exact same issue... Hi @krasole: are you also using CellChat to analyse GeoMx data?

leehs96 commented 9 months ago

Ok, I think i found some way to avoid this error after wasting a day's worth of time.

At least for me, this error Error in if (sum(P1_Pspatial) == 0) { : missing value where TRUE/FALSE needed was caused by the function res <- computeRegionDistance(coordinates = cellchat@images$coordinates, meta = cellchat@meta, trim = 0.1, interaction.range = 250, ratio = cellchat@images$scale.factors$tol, tol = cellchat@images$scale.factors$tol, k.min = 10, contact.knn = T, contact.knn.k = 6) which inside of function computeCommunProb() cause it return adj.contact.knn matrix contains NaN which gives error at Error in if (sum(P1_Pspatial) == 0) { : missing value where TRUE/FALSE needed by making P.spatial to NaN matrix P.spatial <- P.spatial * adj.contact.knn in this step

after i changed some code such as group <- meta$group to group <- meta$labels in the function of 'computeRegionDistance'

after this modification, it doesn't throw any error So i think it just error caused by declaring the vector 'group' from 'meta$group' which is normally 'NULL' for common users (Because the tutorial guides doing like that.)

cheers -Han-

MrModenait commented 9 months ago

Hello @leehs96, thank you for your suggestion. To my astonishing surprise, changing slices in my metadata table from a character vector to a factor was able to solve the problem, and I could run computeCommunProb loaded from CellChat 2.1.0 without any issues (labels was already a factor since before). It was really that simple. I'm curious as to how you managed to get the source for the function though. Unfortunately, it seems my attempt to analyse GeoMx data with CellChat was not very fruitful, as computeCommunProb returns hardly any significant pathways. The inferred communication probabilities are all zero:

summary(cellchat@net$prob)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
      0       0       0       0       0       0 

The p-values are all 1:

summary(cellchat@net$pval)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
      1       1       1       1       1       1 

And I get an error when trying to view all the inferred cell-to-cell communications.

subsetCommunication(cellchat)
Error in subsetCommunication_internal(net, LR, cells.level, slot.name = slot.name,  : 
  No significant signaling interactions are inferred based on the input!

I think the cause of this may be my low number of regions per interest:

                                                                                     Var1 Freq
1                                                                                      TC   37
2                                                                              TI-Unknown   13
3                                           I-Exhausted CD8+ T/Myeloid cells/Plasma cells    9
4                                          I-Follicular B cells/Exhausted CD8+ T/NK cells    8
5                                                 I-FB-like cells/Myeloid cells/Pericytes    5
6                                               I-Exhausted CD8+ T/Pericytes/Plasma cells    4
7                                                                                     TBV    4
8                                          TI-Exhausted CD8+ T/Myeloid cells/Plasma cells    4
9                                         TI-Follicular B cells/Exhausted CD8+ T/NK cells    4
10                                                                        I-Myeloid cells    3
11                                               TI-FB-like cells/Myeloid cells/Pericytes    3
12                                                                       TI-Myeloid cells    3
13 TI-FB-like cells/Myeloid cells/Pericytes & Exhausted CD8+ T/Myeloid cells/Plasma cells    2
14                                             TI-Exhausted CD8+ T/Pericytes/Plasma cells    1

When grouped by microscope slide, the number of interactions in each group is even lower.

                                                                                        slices
labels                                                                                   16PL-27370 01-01 18PL-20291 01-02 19PL-12941 01-02 19PL-27147 01-03
  I-Exhausted CD8+ T/Myeloid cells/Plasma cells                                                         0                1                8                0
  I-Exhausted CD8+ T/Pericytes/Plasma cells                                                             2                0                2                0
  I-FB-like cells/Myeloid cells/Pericytes                                                               0                3                0                2
  I-Follicular B cells/Exhausted CD8+ T/NK cells                                                        6                0                2                0
  I-Myeloid cells                                                                                       0                0                0                3
  TBV                                                                                                   0                0                0                4
  TC                                                                                                   10                9                8               10
  TI-Exhausted CD8+ T/Myeloid cells/Plasma cells                                                        0                0                4                0
  TI-Exhausted CD8+ T/Pericytes/Plasma cells                                                            0                0                1                0
  TI-FB-like cells/Myeloid cells/Pericytes                                                              0                2                0                1
  TI-FB-like cells/Myeloid cells/Pericytes & Exhausted CD8+ T/Myeloid cells/Plasma cells                0                2                0                0
  TI-Follicular B cells/Exhausted CD8+ T/NK cells                                                       3                0                1                0
  TI-Myeloid cells                                                                                      0                0                0                3
  TI-Unknown                                                                                            3                2                4                4

Unless my 0s and 1s are caused by an error, I fear this is the end of my venture into CellChat!

sqjin commented 9 months ago

@MrModenait In your case, I do not think you need to run filterCommunication as this is use for filtering out the cell groups with few number of cells.

znxfdd commented 8 months ago

我也遇到相同的报错,但和meta中的group或labels的类型无关,而是和seurat v5(多样本)的数据结构有关,在提取表达矩阵之前需要用JoinLayers函数对数据进行预处理,无需对metadata进行改动,然后运行computeCommunProb就不再报错了,希望对大家有帮助。 代码: data <- readRDS('saves2/pbmc_final_with_sample.rds') data.MMM <- subset(data,condition=='MMM') data.MMM <- JoinLayers(data.MMM[['RNA']]) data.input <- data.MMM$data.MMM …………

MrModenait commented 8 months ago

Hello @znxfdd, thanks for your reply. I presume you're working with a scRNA-seq dataset. My question is an unconventional one as I am trying to apply CellChat on a GeoMx spatial experiment.