smorabit / hdWGCNA

High dimensional weighted gene co-expression network analysis
https://smorabit.github.io/hdWGCNA/
Other
363 stars 36 forks source link

Subscript out of bounds error using ModuleConnectivity #323

Closed mrendleman closed 1 month ago

mrendleman commented 1 month ago

Describe the bug I am following the vignette to run hdWGCNA on a sketch assay of my data, and received the following error with the ModuleConnectivity function:

> seurat_obj <- ModuleConnectivity(
+   seurat_obj,
+   harmonized = FALSE,
+   group.by = 'ClusterCellType',
+   group_name = 'MyCellType'
+ )
Error in .subscript.2ary(x, i, j, drop = TRUE) : subscript out of bounds
> traceback()
5: stop("subscript out of bounds")
4: .subscript.2ary(x, i, j, drop = TRUE)
3: SeuratObject::LayerData(seurat_obj, assay = assay, layer = layer)[genes_use, 
       cells.use]
2: SeuratObject::LayerData(seurat_obj, assay = assay, layer = layer)[genes_use, 
       cells.use]
1: ModuleConnectivity(seurat_obj, harmonized = FALSE, group.by = "ClusterCellType", 
       group_name = "MyCellType")

Steps to reproduce The issue lies in how cells.use is set; it assumes that any cells in the meta.data that match the group_name are a part of the current assay:

cells.use <- seurat_obj@meta.data %>% subset(get(group.by) %in% group_name) %>% rownames

Because I'm using a sketch (subset) assay, my group.by variable has cells that match the group_name but aren't included in the assay. So when it tries to pull the expression matrix for the assay using those cells this leads to an error:

if (CheckSeurat5()) {
    exp_mat <- SeuratObject::LayerData(seurat_obj, assay = assay,
            layer = layer)[genes_use, cells.use]
}

Error in .subscript.2ary(x, i, j, drop = TRUE) : subscript out of bounds

I fixed this for my case by modifying the if block that sets cells.use to ensure that the cells selected are present in the current assay:

if (!is.null(group.by)) {
    cells.use <- seurat_obj@meta.data %>% subset(get(group.by) %in%
                                                   group_name) %>% rownames
    cells.use <- cells.use[cells.use %in% Cells(seurat_obj, 
                                                assay = assay, layer = layer)]
    MEs <- MEs[cells.use, ]
}

This isn't a perfect fix; I've only tried it with Seurat5, and the else block likely needs a similar change.

R session info This is using hdWGCNA_0.3.03.

smorabit commented 1 month ago

To clarify, hdWGCNA does not support sketched objects, since it was implemented originally for Seurat 4. I currently do not have plans to add support for sketched objects, so if you wish to proceed with your sketched object you are at risk of running into these kind of issues which you will have to figure out how to fix.