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
261 stars 40 forks source link

BiocParallel errors : Error: 'k' must be positive #94

Open Lhz002 opened 6 months ago

Lhz002 commented 6 months ago

When I try to run the codes below: cellChat <- computeCommunProb(cellChat, type = "truncatedMean", trim = 0.01, distance.use = TRUE, interaction.range = 200, scale.distance = 0.01, contact.knn = FALSE, k.min = 10) The terminal outputs is: truncatedMean is used for calculating the average gene expression per cell group. ERROR: BiocParallel errors 1 remote errors, element index: 1 0 unevaluated and other errors first remote error: Error: 'k' must be positive

The data is from cell bin(Stereopy-seq). And the seurat object is transformed from h5ad file from Stereopy-seq Here are some backgrounds for my script:

data.input <- seuratObject[["Spatial"]]$data labels <- Idents(seuratObject) meta <- seuratObject@meta.data meta <- data.frame(group = labels, row.names = names(labels)) coordinates <- data.frame(x = meta$x, y = meta$y) spatial.factors = list(spot.diameter = 50, spot = 1, ratio = 1, tol=25)

help(createCellChat)

cellChat <- createCellChat(object = data.input, meta = meta, group.by = "leiden", datatype = "spatial", coordinates = coordinates, spatial.factors = spatial.factors)

seuratObject@images <- cellChat@images seuratObject@assays cellChat <- createCellChat(object = seuratObject, group.by = "leiden", assay = "Spatial", datatype = "spatial", spatial.factors = spatial.factors)

spatial.factors <- data.frame(ratio = 1, tol = 50)

CellChatDB <- CellChatDB.mouse cellChat@DB <- CellChatDB

cellChat <- subsetData(cellChat) # subset the expression data of signaling genes cellChat <- identifyOverExpressedGenes(cellChat) cellChat <- identifyOverExpressedInteractions(cellChat, variable.both = F) options(warn = 0) cellChat <- computeCommunProb(cellChat, type = "truncatedMean", trim = 0.01, distance.use = TRUE, interaction.range = 200, scale.distance = 0.01, contact.knn = FALSE, k.min = 10)

The structure of objects:
image image

AldrichCPU-bio commented 6 months ago

It seems the 'k' value not positive when performing kNN within function 'computeRegionDistance', perhaps the error comes from line 1192 (sorry the picture is not the exact one :P), due to the 'k' value not specified in your code. Please check your 'meta.data' slot or try to re-set a certain 'k' value in the argument.

image image