hdWGCNA icon indicating copy to clipboard operation
hdWGCNA copied to clipboard

MetaspotsByGroups : Error in `as.Graph()`

Open Prakrithi-P opened this issue 1 year ago • 6 comments

Hi Sam, i get an error while runnning MetaspotsByGroups. It used to work fine but I have recently been getting this error

Error in as.Graph(): ! Please provide rownames to the matrix before converting to a Graph Traceback:

  1. MetaspotsByGroups(K12B_wgcna, group.by = c("path_anno"), ident.group = "path_anno", . assay = "Spatial", slot = "counts")
  2. lapply(groupings, function(x) { . seurat_obj[, seurat_obj$metacell_grouping == x] . })
  3. FUN(X[[i]], ...)
  4. seurat_obj[, seurat_obj$metacell_grouping == x]
  5. [.Seurat(seurat_obj, , seurat_obj$metacell_grouping == x)
  6. subset.Seurat(x = x, features = i, cells = j, ...)
  7. suppressWarnings(expr = x[[g]] <- as.Graph(x = x[[g]][cells.g, . cells.g, drop = FALSE]))
  8. withCallingHandlers(expr, warning = function(w) if (inherits(w, . classes)) tryInvokeRestart("muffleWarning"))
  9. as.Graph(x = x[[g]][cells.g, cells.g, drop = FALSE])
  10. as.Graph.Matrix(x = x[[g]][cells.g, cells.g, drop = FALSE])
  11. abort(message = "Please provide rownames to the matrix before converting to a Graph")
  12. signal_abort(cnd, .file)

Prakrithi-P avatar Jan 28 '24 06:01 Prakrithi-P

Hi,

Can you please provide the code that you used which produced this error message? Any other relevant information would also help. It would also help me to know if you are able to reproduce this in the same dataset that was used in the tutorial?

smorabit avatar Jan 30 '24 02:01 smorabit

Closing this issue for now due to inactivity.

smorabit avatar Feb 21 '24 20:02 smorabit

Hi @smorabit

I had the same issue appear in my data. I did run the tutorial dataset and did not get this issue, and I did run my data previously and did not encounter this issue, so I'm still tracking down what's different. I'll share my code in case you have any insight, and hopefully one of us can record a solution here.

Upstream processing
library(tidyverse)  
library(here)       
# library(wgcna)
library(hdWGCNA)
# library(BPCells)
library(Seurat)
LoadSeuratRds <- SeuratObject::LoadSeuratRds
set.seed(44)
options(future.globals.maxSize= 891289600) ## 850*1024^2 = 891289600

data <- SeuratObject::LoadSeuratRds(here('saved_rds/01-obj_generation/joined_obj.Rds'))
data <- SketchData(data,
                   assay = 'RNA', 
                   ncells=10000)
data

DefaultAssay(data) <- "sketch"
data <- FindVariableFeatures(data)
data <- ScaleData(data)
data <- RunPCA(data)
data <- FindNeighbors(data, dims = 1:50)
# obj <- FindClusters(obj, resolution = 2)
data <- ProjectData(
  object = data,
  assay = "RNA",
  full.reduction = "pca.full",
  sketched.assay = "sketch",
  sketched.reduction = "pca",
  #umap.model = "umap",
  dims = 1:30#,
  #refdata = list(cluster_full = "seurat_clusters")
)
# now that we have projected the full dataset, switch back to analyzing all cells
DefaultAssay(data) <- "RNA"



data <- harmony::RunHarmony(data,
                    group.by.vars = "donor_label",
                    reduction = "pca.full",
                    reduction.save = 'harmony')

## Finding features by fraction isn't working, so using variable features, but increasing the number to have more data for wgcna
tmp <- rowSums(data@assays$RNA@layers$counts>0)
tmp1 <- tmp / 2036755 ## cell count
tmp2 <- tmp1 > 0.05
feats <- rownames(data)[tmp2]

data <- SetupForWGCNA(
  data,
  gene_select = "custom", # the gene selection approach
  # fraction = 0.1, # fraction of cells that a gene needs to be expressed in order to be included
  # group.by = 'class',
  features = feats,
  wgcna_name = "class" # the name of the hdWGCNA experiment
)
MetacellsByGroups call
data <- MetacellsByGroups(
  seurat_obj = data,
  group.by = c("class", "donor_label"), # specify the columns in [email protected] to group by
  reduction = 'harmony', # select the dimensionality reduction to perform KNN on
  dims=1:30,
  k = 25, # nearest-neighbors parameter
  max_shared = 10, # maximum number of shared cells between two metacells
  ident.group = 'class' # set the Idents of the metacell seurat object
  
)
Error
Error in `as.Graph()`:
! Please provide rownames to the matrix before converting to a Graph
---
Backtrace:
     ▆
  1. └─hdWGCNA::MetacellsByGroups(...)
  2.   └─base::lapply(...) at smorabit-hdWGCNA-8280ba5/R/metacells.R:394:2
  3.     └─hdWGCNA (local) FUN(X[[i]], ...)
  4.       ├─seurat_obj[, seurat_obj$metacell_grouping == x] at smorabit-hdWGCNA-8280ba5/R/metacells.R:394:47
  5.       └─SeuratObject:::`[.Seurat`(...) at smorabit-hdWGCNA-8280ba5/R/metacells.R:394:47
  6.         └─SeuratObject:::subset.Seurat(...)
  7.           ├─base::suppressWarnings(...)
  8.           │ └─base::withCallingHandlers(...)
  9.           ├─SeuratObject::as.Graph(x = x[[g]][cells.g, cells.g, drop = FALSE])
 10.           └─SeuratObject:::as.Graph.Matrix(x = x[[g]][cells.g, cells.g, drop = FALSE])
 11.             └─rlang::abort(message = "Please provide rownames to the matrix before converting to a Graph")
SessionInfo
R version 4.3.1 (2023-06-16)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.3 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so;  LAPACK version 3.10.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8       
 [4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: Etc/UTC
tzcode source: system (glibc)

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

other attached packages:
 [1] BPCells_0.1.0         shinyBS_0.61.1        hdWGCNA_0.3.01        igraph_1.5.1         
 [5] WGCNA_1.72-1          fastcluster_1.2.3     dynamicTreeCut_1.63-1 ggrepel_0.9.4        
 [9] Seurat_5.1.0          SeuratObject_5.0.2    sp_2.1-1              harmony_1.1.0        
[13] Rcpp_1.0.11           here_1.0.1            lubridate_1.9.3       forcats_1.0.0        
[17] stringr_1.5.0         dplyr_1.1.3           purrr_1.0.2           readr_2.1.4          
[21] tidyr_1.3.0           tibble_3.2.1          ggplot2_3.4.4         tidyverse_2.0.0      

loaded via a namespace (and not attached):
  [1] IRanges_2.36.0                    R.methodsS3_1.8.2                
  [3] progress_1.2.2                    nnet_7.3-19                      
  [5] poweRlaw_0.70.6                   goftest_1.2-3                    
  [7] DT_0.30                           Biostrings_2.70.3                
  [9] vctrs_0.6.4                       spatstat.random_3.2-1            
 [11] digest_0.6.33                     png_0.1-8                        
 [13] proxy_0.4-27                      deldir_1.0-9                     
 [15] parallelly_1.36.0                 renv_1.0.3                       
 [17] MASS_7.3-60                       Signac_1.13.0                    
 [19] reshape2_1.4.4                    httpuv_1.6.12                    
 [21] foreach_1.5.2                     BiocGenerics_0.48.1              
 [23] withr_2.5.1                       xfun_0.40                        
 [25] ellipsis_0.3.2                    survival_3.5-5                   
 [27] EnsDb.Hsapiens.v86_2.99.0         memoise_2.0.1                    
 [29] zoo_1.8-12                        gtools_3.9.4                     
 [31] pbapply_1.7-2                     R.oo_1.25.0                      
 [33] Formula_1.2-5                     prettyunits_1.2.0                
 [35] KEGGREST_1.42.0                   promises_1.2.1                   
 [37] httr_1.4.7                        restfulr_0.0.15                  
 [39] globals_0.16.2                    fitdistrplus_1.1-11              
 [41] rhdf5filters_1.14.1               rhdf5_2.46.1                     
 [43] rstudioapi_0.15.0                 miniUI_0.1.1.1                   
 [45] generics_0.1.3                    base64enc_0.1-3                  
 [47] curl_5.1.0                        S4Vectors_0.40.2                 
 [49] zlibbioc_1.48.2                   polyclip_1.10-6                  
 [51] GenomeInfoDbData_1.2.11           SparseArray_1.2.4                
 [53] xtable_1.8-4                      pracma_2.4.2                     
 [55] doParallel_1.0.17                 evaluate_0.22                    
 [57] S4Arrays_1.2.1                    BiocFileCache_2.10.2             
 [59] preprocessCore_1.64.0             hms_1.1.3                        
 [61] GenomicRanges_1.54.1              irlba_2.3.5.1                    
 [63] colorspace_2.1-0                  filelock_1.0.2                   
 [65] hdf5r_1.3.8                       ROCR_1.0-11                      
 [67] reticulate_1.34.0                 spatstat.data_3.0-3              
 [69] magrittr_2.0.3                    lmtest_0.9-40                    
 [71] later_1.3.1                       lattice_0.21-8                   
 [73] spatstat.geom_3.2-7               future.apply_1.11.0              
 [75] scattermore_1.2                   XML_3.99-0.14                    
 [77] cowplot_1.1.1                     matrixStats_1.0.0                
 [79] RcppAnnoy_0.0.21                  Hmisc_5.1-1                      
 [81] pillar_1.9.0                      nlme_3.1-162                     
 [83] iterators_1.0.14                  caTools_1.18.2                   
 [85] compiler_4.3.1                    RSpectra_0.16-1                  
 [87] stringi_1.7.12                    tensor_1.5                       
 [89] SummarizedExperiment_1.32.0       GenomicAlignments_1.38.2         
 [91] plyr_1.8.9                        crayon_1.5.2                     
 [93] abind_1.4-5                       BiocIO_1.12.0                    
 [95] googledrive_2.1.1                 bit_4.0.5                        
 [97] fastmatch_1.1-4                   codetools_0.2-19                 
 [99] SeuratData_0.2.2.9001             plotly_4.10.3                    
[101] mime_0.12                         splines_4.3.1                    
[103] fastDummies_1.7.3                 dbplyr_2.4.0                     
[105] cellranger_1.1.0                  knitr_1.44                       
[107] blob_1.2.4                        utf8_1.2.4                       
[109] seqLogo_1.68.0                    AnnotationFilter_1.26.0          
[111] fs_1.6.3                          listenv_0.9.0                    
[113] checkmate_2.3.0                   Matrix_1.6-4                     
[115] tzdb_0.4.0                        pkgconfig_2.0.3                  
[117] tools_4.3.1                       cachem_1.0.8                     
[119] RhpcBLASctl_0.23-42               RSQLite_2.3.2                    
[121] viridisLite_0.4.2                 DBI_1.1.3                        
[123] impute_1.76.0                     fastmap_1.1.1                    
[125] rmarkdown_2.25                    scales_1.2.1                     
[127] grid_4.3.1                        ica_1.0-3                        
[129] shinydashboard_0.7.2              Rsamtools_2.18.0                 
[131] patchwork_1.1.3                   dotCall64_1.1-0                  
[133] RANN_2.6.1                        rpart_4.1.19                     
[135] farver_2.1.1                      yaml_2.3.7                       
[137] MatrixGenerics_1.14.0             foreign_0.8-84                   
[139] rtracklayer_1.62.0                cli_3.6.1                        
[141] stats4_4.3.1                      tester_0.1.7                     
[143] leiden_0.4.3                      lifecycle_1.0.3                  
[145] uwot_0.1.16                       Biobase_2.62.0                   
[147] presto_1.0.0                      backports_1.4.1                  
[149] BSgenome.Hsapiens.UCSC.hg38_1.4.5 BiocParallel_1.36.0              
[151] annotate_1.80.0                   timechange_0.2.0                 
[153] gtable_0.3.4                      rjson_0.2.21                     
[155] ggridges_0.5.4                    progressr_0.14.0                 
[157] parallel_4.3.1                    jsonlite_1.8.7                   
[159] RcppHNSW_0.6.0                    TFBSTools_1.40.0                 
[161] bitops_1.0-7                      bit64_4.0.5                      
[163] Rtsne_0.16                        spatstat.utils_3.0-4             
[165] CNEr_1.38.0                       shinyjs_2.1.0                    
[167] SeuratDisk_0.0.0.9021             R.utils_2.12.2                   
[169] lazyeval_0.2.2                    shiny_1.7.5.1                    
[171] Azimuth_0.5.0                     htmltools_0.5.6.1                
[173] GO.db_3.18.0                      sctransform_0.4.1                
[175] rappdirs_0.3.3                    ensembldb_2.26.0                 
[177] glue_1.6.2                        TFMPvalue_0.0.9                  
[179] spam_2.10-0                       googlesheets4_1.1.1              
[181] XVector_0.42.0                    RCurl_1.98-1.12                  
[183] rprojroot_2.0.3                   BSgenome_1.70.2                  
[185] gridExtra_2.3                     JASPAR2020_0.99.10               
[187] R6_2.5.1                          labeling_0.4.3                   
[189] RcppRoll_0.3.0                    GenomicFeatures_1.54.4           
[191] cluster_2.1.4                     Rhdf5lib_1.24.2                  
[193] gargle_1.5.2                      GenomeInfoDb_1.38.8              
[195] DirichletMultinomial_1.44.0       DelayedArray_0.28.0              
[197] tidyselect_1.2.0                  ProtGenerics_1.34.0              
[199] htmlTable_2.4.2                   xml2_1.3.5                       
[201] AnnotationDbi_1.64.1              future_1.33.0                    
[203] munsell_0.5.0                     KernSmooth_2.23-21               
[205] data.table_1.14.8                 htmlwidgets_1.6.2                
[207] RColorBrewer_1.1-3                biomaRt_2.58.2                   
[209] rlang_1.1.1                       spatstat.sparse_3.0-3            
[211] spatstat.explore_3.2-5            fansi_1.0.5  

derrik-gratz avatar May 28 '24 17:05 derrik-gratz

From the backtrace it looks like there is a problem with the way that it is trying to subset your data. Unfortunately it is very difficult for me to resolve the issue unless I can reproduce it on my side.

smorabit avatar May 29 '24 08:05 smorabit

I was following this vignette, so I had the SetupForWGCNA call before the MetacellsByGroups call, but the vignette says

Notably, since we consider hdWGCNA to be a downstream data analysis step, we do not support subsetting the Seurat object after SetupForWGCNA has been run.

If I load the object without running SetupForWGCNA, then try to subset it on some of the values that were causing issues previously, it runs fine. Is the workflow outlined in that vignette still accurate? Should I be running SetupForWGCNA prior to creating metacells?

derrik-gratz avatar May 29 '24 20:05 derrik-gratz

I see it needs the WGCNAname set up, so I can't switch the order. I'm running it now where I define the metadata combination prior to the SetupForWGCNA call, so maybe establishing the metadata prior to the setup will help subsetting

derrik-gratz avatar May 29 '24 20:05 derrik-gratz