edward130603 / BayesSpace

Bayesian model for clustering and enhancing the resolution of spatial gene expression experiments.
http://edward130603.github.io/BayesSpace
Other
96 stars 20 forks source link

Batch Effect Correction with harmony Error in UseMethod #89

Open dgorski147 opened 1 year ago

dgorski147 commented 1 year ago

Hi. Thank you for the great package.

I was following your joint clustering vignette and am unable to remove batch effects with harmony. It throws an error upon running the RunHarm

sce.combined = RunHarmony(sce.combined, "sample_name", verbose = F) Error in UseMethod("RunHarmony") : no applicable method for 'RunHarmony' applied to an object of class "c('SingleCellExperiment', 'RangedSummarizedExperiment', 'SummarizedExperiment', 'RectangularData', 'Vector', 'Annotated', 'vector_OR_Vector')"

Am I perhaps missing something? Below is the code. Thank you in advance for your time.

library(dplyr) library(ggplot2) library(Seurat) library(BiocManager) library(SingleCellExperiment) library(BayesSpace) library(scater) library(harmony) library(patchwork)

sce1 = getRDS("2020_maynard_prefrontal-cortex", "151673") sce2 = getRDS("2020_maynard_prefrontal-cortex", "151674") sce3 = getRDS("2020_maynard_prefrontal-cortex", "151675") sce4 = getRDS("2020_maynard_prefrontal-cortex", "151676")

((clusterPlot(sce1, "layer_guess_reordered", color = NA) | clusterPlot(sce2, "layer_guess_reordered", color = NA)) / (clusterPlot(sce3, "layer_guess_reordered", color = NA) | clusterPlot(sce4, "layer_guess_reordered", color = NA))) + plot_layout(guides = "collect") + plot_annotation(title = "Ground truth")

rowData(sce1)$is.HVG = NULL rowData(sce2)$is.HVG = NULL rowData(sce3)$is.HVG = NULL rowData(sce4)$is.HVG = NULL

Combine into 1 SCE and preprocess

sce.combined = cbind(sce1, sce2, sce3, sce4, deparse.level = 1) sce.combined = spatialPreprocess(sce.combined, n.PCs = 50) #lognormalize, PCA

sce.combined = runUMAP(sce.combined, dimred = "PCA") colnames(reducedDim(sce.combined, "UMAP")) = c("UMAP1", "UMAP2")

ggplot(data.frame(reducedDim(sce.combined, "UMAP")), aes(x = UMAP1, y = UMAP2, color = factor(sce.combined$sample_name))) + geom_point() + labs(color = "Sample") + theme_bw()

sce.combined = RunHarmony(sce.combined, "sample_name", verbose = F)

class(sce.combined$sample_name) [1] "integer"

Also tried with merging my own samples in which its a class of the batch label is a character and have gotten the same error

class(sce.merge$study_id) [1] "character"

sessionInfo() R version 4.2.0 (2022-04-22 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale: [1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8 LC_MONETARY=English_United States.utf8 LC_NUMERIC=C
[5] LC_TIME=English_United States.utf8

attached base packages: [1] stats4 stats graphics grDevices utils datasets methods base

other attached packages: [1] patchwork_1.1.2 remotes_2.4.2 forcats_0.5.2 stringr_1.4.1 purrr_0.3.4
[6] readr_2.1.2 tidyr_1.2.1 tibble_3.1.8 tidyverse_1.3.2 harmony_0.1.0
[11] Rcpp_1.0.9 scater_1.24.0 scuttle_1.6.2 BayesSpace_1.6.0 sp_1.5-0
[16] SeuratObject_4.1.1 Seurat_4.1.1 ggplot2_3.3.6 dplyr_1.0.10 BiocManager_1.30.18
[21] rlang_1.0.5 SingleCellExperiment_1.18.0 SummarizedExperiment_1.26.1 Biobase_2.56.0 GenomicRanges_1.48.0
[26] GenomeInfoDb_1.32.2 IRanges_2.30.0 MatrixGenerics_1.8.0 matrixStats_0.62.0 S4Vectors_0.34.0
[31] BiocGenerics_0.42.0

edward130603 commented 1 year ago

install.packages("devtools") devtools::install_github("immunogenomics/harmony")

can you try installing from github and retrying?

dgorski147 commented 1 year ago

Thanks for your quick response!

Tried installr::uninstall.packages("harmony")

then installed from github as you suggested and reran the code as in the vignette. Throws a different error this time

sce.combined = RunHarmony(sce.combined, "sample_name", verbose = F)

Error in harmonyObj$init_cluster_cpp(0) : element-wise multiplication: incompatible matrix dimensions: 100x4 and 100x1

Don't have much experience with harmony but looking around for a fix.

Thanks for your help

edward130603 commented 1 year ago

It seems like they require character vector input now for the grouping variable. Try adding this:

sce.combined$sample_name = as.character(sce.combined$sample_name)
dgorski147 commented 1 year ago

Worked! thank you very much! Interesting though, I tried changing to character vector before using harmony installed from CRAN and it didn't work.

Thank you for your help and time