sqjin / CellChat

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

Error in if (sum(P1) == 0) {: missing value where TRUE/FALSE needed #300

Open LucaTucciarone opened 2 years ago

LucaTucciarone commented 2 years ago

Hello!

I can run cell-chat without any issue from a scRNAseq Seurat object but I'm finding problems with a ScanPy multiomics (rna+atacseq) object: This is how I load the dataset:

setwd(wd)
ad <- import("anndata", convert = FALSE)
ad_object <- ad$read_h5ad("T1D_nPOD_rna+multiome_soupX_hg38.final.v1.h5ad")
# access normalized data matrix
data.input <- t(py_to_r(ad_object$X))
rownames(data.input) <- rownames(py_to_r(ad_object$var))
colnames(data.input) <- rownames(py_to_r(ad_object$obs))
# access meta data
meta.data <- py_to_r(ad_object$obs)
meta <- meta.data
##### Here I need to subset my metadata to just that one column cell-chat needs
meta <- meta[,ncol(meta), drop=FALSE]
levels(meta$celltype)
head(meta)

Then I create the cellchat object:

cellchat <- createCellChat(object = data.input, group.by = "celltype")
cellchat <- addMeta(cellchat, meta = meta, meta.name = "celltype")
cellchat <- setIdent(cellchat, ident.use = "celltype")

Load the L/R database:

CellChatDB <- CellChatDB.human # use CellChatDB.mouse if running on mouse data
#showDatabaseCategory(CellChatDB)
# Show the structure of the database
dplyr::glimpse(CellChatDB$interaction)
# use a subset of CellChatDB for cell-cell communication analysis
CellChatDB.use <- subsetDB(CellChatDB, search = "Secreted Signaling") # use Secreted Signaling
# set the used database in the object
cellchat@DB <- CellChatDB.use

Add the cell info into the meta slot

cellchat <- addMeta(cellchat, meta = meta)
cellchat <- setIdent(cellchat, ident.use = "celltype") # set "labels" as default cell identity
levels(cellchat@idents) # show factor levels of the cell labels
groupSize <- as.numeric(table(cellchat@idents)) # number of cells in each cell group

Pre-process the expression data

# subset the expression data of signaling genes for saving computation cost
cellchat <- subsetData(cellchat) # This step is necessary even if using the whole database
cellchat <- identifyOverExpressedGenes(cellchat)
cellchat <- identifyOverExpressedInteractions(cellchat)
# project gene expression data onto PPI network (optional)
cellchat <- projectData(cellchat, PPI.human)
cellchat <- computeCommunProb(cellchat)
# Filter out the cell-cell communication if there are only few number of cells in certain cell groups
cellchat <- filterCommunication(cellchat, min.cells = 10)

Then here It says there are a lot of NAs And that for that reason I get this error Error in if (sum(P1) == 0) {: missing value where TRUE/FALSE needed

The problem is in the "computeCommunProb(cellchat)" step

Please let me know!

I have to say that the data I am starting from Is from multiple samples and then I made a subset in python to extrapolate what I needed

LucaTucciarone commented 2 years ago

head(meta)

celltype
<fct>
MM_555_AAACCCAAGTAGTCTC macrophage
MM_555_AAACCCACAGGTATGG acinar
MM_555_AAACCCAGTGATGTAA ductal
MM_555_AAACCCAGTGGTTTGT acianr
MM_555_AAACCCATCAGGAGAC ductal
MM_555_AAACCCATCCGATAAC immune

unique(cellchat@idents)

macrophageacinarductalacianrimmunebetalymph_endoalphaactivated_stellateq_stellatemastREG+_acinardeltaendothelialschwannmuc5b_ductal
 Levels:
REG+_acinar''acianr''acinar''activated_stellate''alpha''beta''delta''ductal''endothelial''immune''lymph_endo''macrophage''mast''muc5b_ductal''q_stellate''schwann'

cellchat@data

6731 x 8823 sparse Matrix of class "dgCMatrix"

LINC01409    -0.510120094  2.18581080 -0.45797196 -1.626048e-01 -0.533872306 -0.355126828
KLHL17       -0.070135087 -0.07109839 -0.07218006 -8.376281e-02 -0.069203652 -0.076213107
PLEKHN1      -0.020366136 -0.02335113 -0.02670295  1.669724e+01 -0.017479870 -0.039200246
AL645608.7   -0.052686848 -0.04829920 -0.04337235  9.384927e-03 -0.056929369 -0.025002571
HES4         -0.180781931 -0.17278679 -0.16380911 -6.767511e-02 -0.188512623 -0.130335823
ISG15        -0.115727194 -0.11784433 -0.12022164 -1.456781e-01 -0.113680087 -0.129085451
AGRN         -0.123824224 -0.12478121 -0.12585582 -1.373628e-01  5.355777740 -0.129862487
C1orf159     -0.316225499 -0.30426022  3.24064207 -1.469536e-01 -0.327794999 -0.240729496
LINC01342    -0.062820159 -0.06174705 -0.06054206 -4.763892e-02 -0.063857771 -0.056049258
AL390719.2   -0.271775156 -0.26923713 -0.26638722 -2.358700e-01 -0.274229228 -0.255761266
TTLL10       -0.102808177 -0.10000265 -0.09685236 -6.311864e-02  8.126688004 -0.085106477
SDF4         -0.266289592 -0.26307189 -0.25945878 -2.207691e-01 -0.269400865 -0.245987251
B3GALT6      -0.072275154 -0.07167491 -0.07100092 -6.378365e-02 -0.072855540 -0.068487905
LucaTucciarone commented 2 years ago

Digging into the problem it comes out my data, that I repeat is coming from a pull of different samples and exp-points, has also been normalised in scanPy in this way: sc.pp.normalize_per_cell(adata_merged, counts_per_cell_after=1e4) and Harmony normalisation

sqjin commented 2 years ago

@LucaTucciarone You cannot use data with negative values as input of CellChat. I think the Harmony normalized data is a corrected data, which cannnot be used as an input.

dnehar commented 2 years ago

Just in case someone is facing the same problem, when trying to run cellchat on AnnData object. The following worked for me:

library(anndata)

data.input

data.input <- t(adata$raw$X) colnames(data.input) <- adata$raw$obs_names rownames(data.input) <- adata$raw$var_names dim(data.input)

meta data

meta= adata$obs

Generate cellchat object

cellchat <- createCellChat(object = as.matrix(data.input), meta = meta, group.by = "cluster_ids")

DRSEI commented 1 year ago

cellchat <- addMeta(cellchat, meta = meta) cellchat <- setIdent(cellchat, ident.use = "celltype") # set "labels" as default cell identity levels(cellchat@idents) # show factor levels of the cell labels groupSize <- as.numeric(table(cellchat@idents)) # number of cells in each cell group

Hello @LucaTucciarone ,

How do you solved the problem? I am having same issues

sandhusumiti commented 1 year ago

@sqjin I have encountered same error while running computeCommunProb(): (sum(P1_Pspatial) == 0) {: missing value where TRUE/FALSE needed

The data has non-negative normalized expression count. Have you found the source of error and possible solution for it? Thanks!

sqjin commented 1 year ago

@sandhusumiti Do you also extract the data from scanpy?

sandhusumiti commented 1 year ago

@sqjin Thank you for prompt response. I am using normalized matrix from a Seurat object.

alexandre-jf-fernandes commented 1 year ago

Hello, I'm having the same issue. My data is imported from Scanpy. Can you help me?

yyxxp commented 1 year ago

@LucaTucciarone You cannot use data with negative values as input of CellChat. I think the Harmony normalized data is a corrected data, which cannnot be used as an input.

@sqjin I encountered the exact issue with @LucaTucciarone , do you know how to slove this? Thank you so much!

yyxxp commented 1 year ago

@LucaTucciarone

cellchat <- addMeta(cellchat, meta = meta) cellchat <- setIdent(cellchat, ident.use = "celltype") # set "labels" as default cell identity levels(cellchat@idents) # show factor levels of the cell labels groupSize <- as.numeric(table(cellchat@idents)) # number of cells in each cell group

Hello @LucaTucciarone ,

How do you solved the problem? I am having same issues

@DRSEI Hi have you solved this issue?

hyjforesight commented 1 year ago

Hi @YOU-k , currently I do not have a better suggestion on the generation of batch-corrected count matrix. Generally, we simply merge the data matrix from different batches and use normalization methods such as in Seurat to obtain a data matrix as input of cellchat.

@LucaTucciarone You cannot use data with negative values as input of CellChat. I think the Harmony normalized data is a corrected data, which cannnot be used as an input.