smorabit/hdWGCNA

Error in checkSets(MEs, checkStructure = TRUE, useSets = useSets) : No data given.

Dazcam opened this issue · 1 comments

I have a dataset spanning several brain regions, each region having 15-20 cell types. For a single brain region, I have created an independent misc slot in my Seurat object for each meta cell type:

Misc slot Screenshot 2024-07-03 at 14 53 47

Having created this, I have written a function to loop through all the cell types in my dataset and add the WGCNA results for each cell type separately to the same Seurat object and avoid creating multiple Seurat objects for a single brain region. Here is the code:

run_wgcna <- function(
    
  seurat_obj = NULL,
  cell_types = NULL,
  region = NULL,
  outdir = NULL
  
) {
  
  for (i in 1:length(cell_types)) {
    
    sink(paste0(outdir, region, '_', cell_types[i], '_hdWGCNA.log'))
    message("\n\n\nRunning hdWGCNA for: ", toupper(region), ', ', cell_types[i])
    
    if (i == 1)
      # Set up the expression matrix
      seurat_object <- SetDatExpr(
        seurat_obj,
        group_name = cell_types[i], 
        group.by = 'cellIDs', 
        assay = 'RNA', # using RNA assay
        slot = 'data', # using normalized data
        wgcna_name = paste0(cell_types[i], '_wgcna')
      )
    else
      seurat_object <- SetDatExpr(
        seurat_object,
        group_name = cell_types[i], 
        group.by = 'cellIDs', 
        assay = 'RNA', # using RNA assay
        slot = 'data', # using normalized data
        wgcna_name = paste0(cell_types[i], '_wgcna')
      )
      
    
    # Select soft power threshold
    seurat_object <- TestSoftPowers(
      seurat_object,
      networkType = 'signed', # you can also use "unsigned" or "signed hybrid"
      wgcna_name = paste0(cell_types[i], '_wgcna'))
    
    power_val <- GetPowerTable(seurat_object, paste0(cell_types[i], '_wgcna')) %>%
      select(Power, SFT.R.sq) %>%
      filter(SFT.R.sq > 0.8) %>%
      pull(Power) %>%
      dplyr::first()
    
    message("Soft Power threshold set to: ", power_val)
    
    message("Construct co-expression network ...")
    # Construct co-expression network
    seurat_object <- ConstructNetwork(
      seurat_object,
      tom_name = cell_types[i], # name of the topoligical overlap matrix written to disk
      soft_power = power_val,
      overwrite_tom = T,
      wgcna_name = paste0(cell_types[i], '_wgcna'),
      tom_outdir = outdir
    )
    message("Compute Eigengenes ...")
    # Compute Eigengenes and Connectivity
    # Compute all MEs in the full single-cell dataset
    seurat_object <- ModuleEigengenes(
      seurat_object,
    #  modules = GetModules(seurat_object, wgcna_name = paste0('FC-CycPro', '_wgcna')),
      group.by.vars = "Sample",
      wgcna_name = paste0(cell_types[i], '_wgcna')
    )
    
    message("Module Connect ...")
    # Compute module connectivity
    # compute eigengene-based connectivity (kME):
    seurat_object <- ModuleConnectivity(
      seurat_object,
      group.by = 'cellIDs', 
      group_name = cell_types[i],
      wgcna_name = paste0(cell_types[i], '_wgcna')
    )
    
    message("Rename modules ...")
    # rename the modules
    seurat_object <- ResetModuleNames(
      seurat_object,
      new_name = paste0(cell_types[i], "-M"),
      wgcna_name = paste0(cell_types[i], '_wgcna')
    )
    
  }
  
  message("Saving RDS file ...")
  saveRDS(seurat_object, file = paste0(outdir, toupper(region), 
                                       '_', cell_types[i], '_hdWGCNA.rds'))
  rm(seurat_object)
  sink()
  
}

The following issue is thrown when running ModuleEigengenes() on the first cell type:

Running hdWGCNA for: FCX_FETAL, FC-CycPro
Soft Power threshold set to: 18
Construct co-expression network ...
Compute Eigengenes ...
Error in checkSets(MEs, checkStructure = TRUE, useSets = useSets) : 
  No data given.

Note that the TOM file was created as expected during the previous ConstructNetwork() step.

My aim here is to save all the relevant cell type specific WGCNA data generated to the corresponding cell type specific slot in misc, and at the end of that function have all the data stored in a single Seurat object. I can see you have provided the functionality for this.

I'm wondering if I'm using wgcna_name parameter of the functions correctly? In this function I've been setting these to the cell type specific misc slots for each function, but perhaps this is not the correct approach?

I think this error is thrown by WGCNA, but I'm unsure how to get around it.

Any advice you could offer would be greatly appreciated.

Hi, thank you for taking the time to write this issue. I am wondering if you can distill this error into a simple example, ideally with the tutorial dataset, rather than in this custom function that you wrote?