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
272 stars 41 forks source link

netVisual_bubble() skips one of the LR pair #159

Open Alexis-Varin opened 4 months ago

Alexis-Varin commented 4 months ago

Hello, I discovered one bug with netVisual_bubble()

I did the following code :

pairLR.use.up = data.frame("interaction_name" = "MIF_CD74_CXCR4")
netVisual_bubble(cellchat, pairLR.use = pairLR.use.up, sources.use = 4, targets.use = c(1:31), comparison = c(1,2))

Comparing communications on a merged object

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

The problem is that the interaction is present in my source... It shows up when doing

netVisual_bubble(cellchat, sources.use = 4, targets.use = c(1:31), comparison = c(1,2))
Alexis-Varin commented 4 months ago

Here is a concrete example with another cell type and interaction

pairLR.use.up = data.frame("interaction_name" = c("BTLA_TNFRSF14"))
netVisual_bubble(cellchat, pairLR.use = pairLR.use.up, sources.use = 11, targets.use = c(1:31), comparison = c(1,2),  angle.x = 90, remove.isolate = F, title.name = paste0("Up-regulated signaling in ",levels(cellchat@meta$labels)[11]))

Comparing communications on a merged object

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

pairLR.use.up = data.frame("interaction_name" = c("BTLA_TNFRSF14","IL16_CD4"))
netVisual_bubble(cellchat, pairLR.use = pairLR.use.up, sources.use = 11, targets.use = c(1:31), comparison = c(1,2),  angle.x = 90, remove.isolate = F, title.name = paste0("Up-regulated signaling in ",levels(cellchat@meta$labels)[11]))
cellchat bubble BTLA problem

I found out that the problem is in the subsetCommunication() part of netVisual_bubble() Since net = NULL in subsetCommunication() because netVisual_bubble() does not pass a net parameter to the function, and my object is merged, subsetCommunication() will be done for each of my merged object :

else if (object@options$mode == "merged") {
    if (is.null(net)) {
      net0 <- slot(object, "net")
      df.net <- vector("list", length(net0))
      names(df.net) <- names(net0)
      for (i in 1:length(net0)) {
        net <- net0[[i]]
        LR <- object@LR[[i]]$LRsig
        cells.level <- levels(object@idents[[i]])

        df.net[[i]] <- subsetCommunication_internal2(net, LR, cells.level, slot.name = slot.name,
                                                    sources.use = sources.use, targets.use = targets.use,
                                                    signaling = signaling,
                                                    pairLR.use = pairLR.use,
                                                    thresh = thresh,
                                                    datasets = datasets, ligand.pvalues = ligand.pvalues, ligand.logFC = ligand.logFC, ligand.pct.1 = ligand.pct.1, ligand.pct.2 = ligand.pct.2,
                                                    receptor.pvalues = receptor.pvalues, receptor.logFC = receptor.logFC, receptor.pct.1 = receptor.pct.1, receptor.pct.2 =receptor.pct.2)
      }

The problem is, my LR pair BTLA_TNFRSF14 is not present in my NR dataset; only in my R dataset. So when df.net[[1]] = subsetCommunication_internal() (corresponding to my NR dataset) is calculated, nrow(net) == 0 in subsetCommunication_internal(), since it is not present, and the function returns the "No significant signaling interactions are inferred based on the input!" error.

So when there is only one interaction to be visualized and it is only present in one of the two datasets, it will report that error.

Alexis-Varin commented 4 months ago

So the only thing I changed is in subsetCommunication_internal(), changing the stop() into a warning() :

net <- net[rowSums(is.na(net)) != ncol(net), , drop = FALSE]

  if (nrow(net) == 0) {
    warning("No significant signaling interactions are inferred based on the input!")
  }

The interaction is now correctly displayed :

pairLR.use.up = data.frame("interaction_name" = c("BTLA_TNFRSF14"))
netVisual_bubble(cellchat, pairLR.use = pairLR.use.up, sources.use = 11, targets.use = c(1:31), comparison = c(1,2),  angle.x = 90, remove.isolate = F, title.name = paste0("Up-regulated signaling in ",levels(cellchat@meta$labels)[11]))

Comparing communications on a merged object

Warning messages: 1: In subsetCommunication_internal2(net, LR, cells.level, slot.name = slot.name, : No significant signaling interactions are inferred based on the input! 2: In subsetCommunication_internal2(net, LR, cells.level, slot.name = slot.name, : No significant signaling interactions are inferred!

cellchat bubble BTLA fixed

And results are unchanged as above with c("BTLA_TNFRSF14","IL16_CD4")