smorabit / hdWGCNA

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

Error with MetaspotsbyGroups min_spots function #63

Closed JoyOtten closed 1 year ago

JoyOtten commented 1 year ago

Hi Sam,

I encountered another problem with the min_spots parameter. If you set it to default (50) it works perfectly but with anything else I'm getting the following error: Error in t.default(agg_X) : argument is not a matrix

I will check if I have the same issue with the tutorial data.

seurat_subset <- MetaspotsByGroups(

JoyOtten commented 1 year ago

Hi, I've also ran it on the tutorial code and I'm getting the same error code:

single-cell analysis package

library(Seurat)

package to install the mouse brain dataset

library(SeuratData)

plotting and data science packages

library(tidyverse) library(cowplot) library(patchwork)

co-expression network analysis packages:

library(WGCNA) library(hdWGCNA)

install this package, which allows us to compute distance between the spots

library(proxy)

enable parallel processing for network analysis (optional)

enableWGCNAThreads(nThreads = 8)

using the cowplot theme for ggplot

theme_set(theme_cowplot())

set random seed for reproducibility

set.seed(12345)

download the mouse brain ST dataset (stxBrain)

SeuratData::InstallData("stxBrain")

load the anterior and posterior samples

brain <- LoadData("stxBrain", type = "anterior1") brain$region <- 'anterior' brain2 <- LoadData("stxBrain", type = "posterior1") brain2$region <- 'posterior'

merge into one seurat object

seurat_obj <- merge(brain, brain2) seurat_obj$region <- factor(as.character(seurat_obj$region), levels=c('anterior', 'posterior'))

make a dataframe containing the image coordinates for each sample

image_df <- do.call(rbind, lapply(names(seurat_obj@images), function(x){ seurat_obj@images[[x]]@coordinates }))

merge the image_df with the Seurat metadata

new_meta <- merge(seurat_obj@meta.data, image_df, by='row.names')

fix the row ordering to match the original seurat object

rownames(new_meta) <- new_meta$Row.names ix <- match(as.character(colnames(seurat_obj)), as.character(rownames(new_meta))) new_meta <- new_meta[ix,]

add the new metadata to the seurat object

seurat_obj@meta.data <- new_meta

head(image_df)

run SCTransform

seurat_subset <- SCTransform(seurat_obj, assay = "Spatial", verbose = FALSE) seurat_obj <- RunPCA(seurat_subset, verbose = FALSE, assay = "SCT")

Louvain clustering and umap

seurat_obj <- FindNeighbors(seurat_obj, dims = 1:30) seurat_obj <- FindClusters(seurat_obj,verbose = TRUE) seurat_obj <- RunUMAP(seurat_obj, dims = 1:30)

seurat_subset <- SetupForWGCNA( seurat_obj, features = GetWGCNAGenes(seurat_subset, 'SCT'), wgcna_name = "SCT_meta" )

seurat_obj <- MetaspotsByGroups( seurat_subset, group.by = c("seurat_clusters", "region"), ident.group = "seurat_clusters", assay = 'Spatial', slot = 'counts', min_spots = 5 )

Error in t.default(agg_X) : argument is not a matrix In addition: Warning message: In MetaspotsByGroups(seurat_subset, group.by = c("seurat_clusters", : Removing the following groups that did not meet min_spots: 12#anterior, 17#anterior

smorabit commented 1 year ago

Hi,

Thank you for posting the issue, I am looking into it now.

JoyOtten commented 1 year ago

Hi,

I was wondering how it goes and if I could be of any help. Thank you

smorabit commented 1 year ago

Hi again,

At this time I have not been able to reproduce your error on my side so it's hard for me to figure out what's going on. Based on the error message it might be a data type issue under the hood. I made a small change in the newest version that might fix your issue if you give it a try, please try running the code again with the newest version.

JoyOtten commented 1 year ago

Hi Sam,

I have found that there is probably an issue with the col_bounds. In my data I have col_bounds from: 28 29 30 When I then continue to the following lines of code:

col_bounds <- col_bounds[which(1:length(col_bounds)%%4 == 0)]

That results in "NA" integer(empty)

Do you have any ideas on how to solve this? I don't follow necessary why you divide it by 4 which implies that you need at least 4 col_bounds. Could you help me out to solve this problem or am I doing something wrong with my code?

smorabit commented 1 year ago

It seems like you may be trying this on a cluster that is too spatially constrained. The metaspots algorithm works by aggregating the transcriptome information for neighboring spots. There's a space of 4 spots between each metaspot, that's why we have the %%4 in that line of code. You can see a diagram of this in Fig S5 of the hdWGCNA paper. So it seems like in your case, the selected group is very thin with only 3 spots wide.

I updated the hdWGCNA package to add a more informative warning message when for this case, and now when there are fewer than 4 col_bounds to start, the algorithm picks one of them to continue forward with for aggregation, but in my testing it just fails later on. You may wish to choose group.by parameters that would result in larger groupings due to this limitation.

szlilixing commented 1 year ago

Hi Sam, I encountered a similar issue when running MetaspotsByGroups. After deleting spots less than min_spots (25) for each clusters in each sample(orig.ident), it still showed an error. I'm not sure whether it's for the same reason.

seurat_subset <- MetaspotsByGroups( seurat_subset, group.by = c("orig.ident","seurat_clusters"), ident.group = c("seurat_clusters"), min_spots = 25, wgcna_name = "SCT_meta", assay = 'Spatial', slot = 'counts' ) Error in Matrix(from, sparse = TRUE, doDiag = FALSE) : invalid 'data'

When I performed by 'group.by = c("orig.ident"), ident.group = c("orig.ident")', it's OK.

I further performed MetaspotsByGroups in one single slide.

seurat_subset <- seurat_subset[,seurat_subset$orig.ident=="t2"] seurat_subset <- MetaspotsByGroups( seurat_subset, group.by = c("seurat_clusters"), ident.group = c("seurat_clusters"), min_spots = 25, wgcna_name = "SCT_meta", assay = 'Spatial', slot = 'counts' ) Error in Matrix(from, sparse = TRUE, doDiag = FALSE) : invalid 'data' In addition: Warning message: In MetaspotsByGroups(seurat_subset, group.by = c("seurat_clusters"), : Removing the following groups that did not meet min_spots: 12, 13, 14, 16, 7

After selecting 2 major cluster in the slide for analysis, there is no error or warning. seurat_subset <- seurat_subset[,seurat_subset$seurat_clusters%in%c("17", "3")]

It seems because some spots from one cluster scattered spatially separately. In this case, could I build metaspots via adjacent/random 4(or more) spots, or any method to ignore such cluster in any slides?