sqjin / CellChat

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

Issue in cellchat <- identifyOverExpressedGenes(cellchat) #200

Open superman2412 opened 3 years ago

superman2412 commented 3 years ago

Hi, I am facing troubles in identifyOverExpressedGenes(cellchat) and identifyOverExpressedInteractions(cellchat) functions.

I get the same error for both functions:

Error in rep(no, length.out = len) : attempt to replicate an object of type 'closure' 2. ifelse(test = future::nbrOfWorkers() == 1, yes = pbapply::pbsapply, no = future.apply::future_sapply) 1. identifyOverExpressedGenes(cellchat)

Can you please suggest what to do? I am really struggling to finish my analysis.

I am using the following code to complete previous functions:

`lung_carcinoma_seurat <- readRDS("../Output/Lung_adenocarcinoma_seurat_CellChat_NormalizedOnly.rds")

data.input = GetAssayData(lung_carcinoma_seurat, assay = "RNA", slot = "data") # normalized data matrix labels = lung_carcinoma_seurat@meta.data$Cell_subtype meta = data.frame(group = labels, row.names = names(labels)) # create a dataframe of the cell labels

cellchat <- createCellChat(object = data.input, meta = meta, group.by = 'group')

Create a CellChat object from a data matrix

The cell barcodes in 'meta' is 1 2 3 4 5 6 The cell barcodes in 'meta' is different from those in the used data matrix. We now simply assign the colnames in the data matrix to the rownames of 'mata'!Set cell identities for the new CellChat object The cell groups used for CellChat analysis are Activated DCs Alveolar Mac AT1 AT2 CD141+ DCs CD163+CD14+ DCs CD1c+ DCs CD207+CD1a+ LCs CD4+ Th CD8 low T CD8+/CD4+ Mixed Th Ciliated Club COL13A1+ matrix FBs COL14A1+ matrix FBs Cytotoxic CD8+ T EPCs Exhausted CD8+ T Exhausted Tfh FB-like cells Follicular B cells GC B cells in the DZ GC B cells in the LZ GrB-secreting B cells Lymphatic ECs Malignant cells MALT B cells MAST Mesothelial cells Microglia/Mac mo-Mac Monocytes Myofibroblasts Naive CD4+ T Naive CD8+ T NK pDCs Pericytes Plasma cells Pleural Mac Smooth muscle cells Stalk-like ECs Tip-like ECs Treg tS1 tS2 tS3 Tumor ECs Undetermined

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 use all CellChatDB for cell-cell communication analysis CellChatDB.use <- CellChatDB # simply use the default CellChatDB

set the used database in the object cellchat@DB <- CellChatDB.use

subset the expression data of signaling genes for saving computation cost

cellchat <- subsetData(cellchat) # This step is necessary even if using the whole database
future::plan("multiprocess") # do parallel
cellchat <- identifyOverExpressedGenes(cellchat)
cellchat <- identifyOverExpressedInteractions(cellchat)

project gene expression data onto PPI network (optional) cellchat <- projectData(cellchat, PPI.human)

HERE IS THE ERROR

Error in rep(no, length.out = len) : attempt to replicate an object of type 'closure'
2.
ifelse(test = future::nbrOfWorkers() == 1, yes = pbapply::pbsapply, no = future.apply::future_sapply)
1.
identifyOverExpressedGenes(cellchat)

`

Am I even uploading the Seurat object correctly? Please help, thank you so much.

sqjin commented 3 years ago

@superman2412 It looks ok. Can you show me the cellchat@data.signaling and unique(cellchat@idents)?

superman2412 commented 3 years ago

@sqjin Thank you for your prompt response. Yes, here you go. One thing to bear in mind would be that my Seurat object (inside R environment) is 7.8GB. That shouldn't make any difference right? As long as as I have enough RAM?

unique(cellchat@idents)  cellchat@data signaling_1  cellchat@data signaling2  cellchat@data signaling3
superman2412 commented 3 years ago

@sqjin Would be fantastic if you could see what the issue is here and why it isn't running.

superman2412 commented 3 years ago

@sqjin Some more information on this error.

Screenshot 2021-05-19 at 20 16 24
sqjin commented 3 years ago

@superman2412 The data matrix looks good. The error information you posted is not much informative. Can you run the source code of this function line by line to give more error information?

superman2412 commented 3 years ago

@sqjin . Managed to fix this error by changing the syntax of a code chunk found in the source code.

Source Code > R/utilities.R.

FROM:

  my.sapply <- ifelse(
    test = future::nbrOfWorkers() == 1,
    yes = pbapply::pbsapply,
    no = future.apply::future_sapply
  )

TO:

my.sapply <- if(
    test <- future::nbrOfWorkers() == 1)elsif(
    yes = pbapply::pbsapply)else(
    no = future.apply::future_sapply
  )

The code chunk belongs to the identifyOverexpressedGenes and identifyOverExpressedInteractions functions. Also, I ran the edited source code itself in my script and the function ran successfully.

Why do you think changed syntax is fixing the error? Also, do you mind uploading updated version of the CellChat package so the error is fixed globally (i.e. in case such a code chuck appears any other function)?

sqjin commented 3 years ago

@superman2412 Can you please check the edited codes you shared? There is an error when running on my computer. Error in elsif(yes = pbapply::pbsapply) : could not find function "elsif" Error in elseif(yes = pbapply::pbsapply) : could not find function "elseif"

superman2412 commented 3 years ago

@sqjin Not sure what's going on here. So for the functions identifyOverExpressedInteractions, identifyOverExpressedGenes

changing this code chunk:

 my.sapply <- ifelse(
    test = future::nbrOfWorkers() == 1,
    yes = pbapply::pbsapply,
    no = future.apply::future_sapply
  )

TO, the following code chunk

my.sapply <- if(
    test <- future::nbrOfWorkers() == 1)elsif(
    yes = pbapply::pbsapply)else(
    no = future.apply::future_sapply

Makes the code run properly, without any error.

1) Is there any thing we can do to troubleshoot this error?

2) There's another problem though, now I have the same error in the following function:

cellchat <- computeCommunProb(cellchat)

Screenshot 2021-05-24 at 01 16 44

What changes do I need to make here to the following code chunk of the computeCommunProb function to make the code run?

  my.sapply <- ifelse(
    test = future::nbrOfWorkers() == 1,
    yes = sapply,
    no = future.apply::future_sapply
  )
superman2412 commented 3 years ago

CHANGING THIS:

 my.sapply <- if(
    test = future::nbrOfWorkers() == 1,
    yes = pbapply::pbsapply,
    no = future.apply::future_sapply
  )

TO:

my.sapply <- future.apply::future_sapply

in all the files of the source code fixes the errors.

Huge thank you to @sqjin for helping me through this. If anyone wants the modified package (i.e. with the changed source code) you can find it here: https://github.com/superman2412

@sqjin you can close the issue now.

YitengDang commented 2 years ago

Thanks for this discussion. I just wanted to point out that I'm also encountering the same issue (even when running the tutorial vignette) and replacing the ifelse loop by what @superman2412 suggested solves the issue. It might have to do with an update of the future package. The command future::plan("multiprocess", workers = 4) as used in the vignette is now deprecated. Instead one should use future::plan("multisession") (or any of the other types of future currently implemented). However, this generates the error mentioned in this issue when running identifyOverExpressedGenes(). @sqjin: in 1.1.3 this does not seem to be resolved yet, but hopefully this will be addressed in a future release of CellChat.

EDIT: the same issue also arises in netClustering(), where exactly the same ifelse loop needs to be replaced. Also, future::plan("multiprocess", workers = nCores) on the line above needs to be changed into future::plan("multisession", workers = nCores).

ColeKeenum commented 1 year ago

@YitengDang and @superman2412 thanks for the info! I am still running into this issue due to a future update (I think) for the netClustering() function.

Error in rep(no, length.out = len) : 
  attempt to replicate an object of type 'closure'

For those that don't know, you can easily edit netClustering(), or any other function similarly, with trace(netClustering, edit = T) which will bring up a window to look at the source code for that function.

Edit: Just to specify, I used the same recommendations as @YitengDang and the code works now.