smorabit / hdWGCNA

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

Error in dimnames(x) <- dn #169

Closed shanshancode closed 5 months ago

shanshancode commented 6 months ago

Hi, thanks for the great package!

here is my issue, when i use

ModuleNetworkPlot(seurat.obj) Writing output files to ModuleNetworks Error in dimnames(x) <- dn : length of 'dimnames' [1] not equal to array extent invalid option "error"

the same error showed up for HubGeneNetworkPlot , RunModuleUMAP...  

Steps to reproduce


# Put code in this box
> ModuleNetworkPlot(sc.obj)
Writing output files to ModuleNetworksError in dimnames(x) <- dn : 
  length of 'dimnames' [1] not equal to array extent
invalid option "error"

> sc.obj <- RunModuleUMAP(
+   sc.obj,
+   n_hubs = 10, # number of hub genes to include for the UMAP embedding
+   n_neighbors=15, # neighbors parameter for UMAP
+   min_dist=0.1 # min distance between points in UMAP space
+ )
Selecting by kME_Mesophyll_7-M1
Selecting by kME_Mesophyll_7-M2
Selecting by kME_Mesophyll_7-M3
Selecting by kME_Mesophyll_7-M4
Selecting by kME_Mesophyll_7-M5
Selecting by kME_Mesophyll_7-M6
Selecting by kME_Mesophyll_7-M7
Selecting by kME_Mesophyll_7-M8
Selecting by kME_Mesophyll_7-M9
Selecting by kME_Mesophyll_7-M10
Selecting by kME_Mesophyll_7-M11
Selecting by kME_Mesophyll_7-M12
Selecting by kME_Mesophyll_7-M13
Selecting by kME_Mesophyll_7-M14
Selecting by kME_Mesophyll_7-M15
Error in dimnames(x) <- dn : 
  length of 'dimnames' [1] not equal to array extent
invalid option "error"

**R session info**
R version 4.1.0 (2021-05-18)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.6 LTS

many thanks!
shanshan 
smorabit commented 6 months ago

Hi, can you please send the hdWGCNA code that you ran prior to these functions? Also I am wondering if you are able to reproduce this error on the tutorial dataset or if this is a problem specific to your dataset?

shanshancode commented 6 months ago

Hello, sorry for the late reply. regarding to: > Hi, can you please send the hdWGCNA code that you ran prior to these functions? here is the code: DefaultAssay(sc.obj)<-'RNA'

  sc.obj <- SetupForWGCNA(
    sc.obj,
    gene_select = "fraction", # the gene selection approach
    fraction = 0.05, # fraction of cells that a gene needs to be expressed in order to be included
    wgcna_name = i # the name of the hdWGCNA experiment
  )
  # construct metacells  in each group
  sc.obj <- MetacellsByGroups(
    seurat_obj = sc.obj,
    group.by = c("celltype0", "ttg"), # specify the columns in seurat_obj@meta.data to group by
    reduction = 'harmony', # select the dimensionality reduction to perform KNN on
    k = 25, # nearest-neighbors parameter
    max_shared = 10, # maximum number of shared cells between two metacells
    ident.group = 'celltype0' # set the Idents of the metacell seurat object
  )
  # normalize metacell expression matrix:
  sc.obj <- NormalizeMetacells(sc.obj)

  sc.obj <- SCTransform(object = sc.obj)
  sc.obj <- ScaleMetacells(sc.obj, features=VariableFeatures(sc.obj))
  sc.obj <- RunPCAMetacells(sc.obj, features=VariableFeatures(sc.obj))
  sc.obj <- RunHarmonyMetacells(sc.obj, group.by.vars='orig.ident')
  sc.obj <- RunUMAPMetacells(sc.obj, reduction='harmony', dims=1:15)

  sc.obj <- SetDatExpr(
    sc.obj,
    group_name = "Mesophyll_11", # the name of the group of interest in the group.by column
    group.by='celltype0', # the metadata column containing the cell type info. This same column should have also been used in MetacellsByGroups
    assay = 'RNA',
    slot = 'data' # using normalized data
  )
  # Test different soft powers:
  sc.obj <- TestSoftPowers(
    sc.obj,
    networkType = 'signed' # you can also use "unsigned" or "signed hybrid"
  )

  # construct co-expression network:
  sc.obj <- ConstructNetwork(
    sc.obj, 
    setDatExpr=FALSE,
    overwrite_tom = TRUE,
    tom_name = 'Mesophyll_11' # name of the topoligical overlap matrix written to disk
  )

  #TOM <- GetTOM(sc.A2_5hpi_Col0)

  # need to run ScaleData first or else harmony throws an error:
  sc.obj <- ScaleData(sc.obj, features=VariableFeatures(sc.obj))

  # harmonized module eigengenes:
  hMEs <- GetMEs(sc.obj)

  # module eigengenes:
  MEs <- GetMEs(sc.obj, harmonized=FALSE)
  # compute eigengene-based connectivity (kME):
  sc.obj <- ModuleConnectivity(
    sc.obj,
    group.by = 'celltype0', group_name = 'Mesophyll_11'
  )
  # rename the modules
  sc.obj <- ResetModuleNames(
    sc.obj,
    new_name = "Mesophyll_11-M"
  )

  # get the module assignment table:
  modules <- GetModules(sc.obj)

  # show the first 6 columns:
  hub_df <- GetHubGenes(sc.obj, n_hubs = 10)

  # compute gene scoring for the top 25 hub genes by kME for each module
  # with Seurat method
  sc.obj <- ModuleExprScore(
    sc.obj,
    n_genes = 25,
    method='Seurat'
  )

  # compute gene scoring for the top 25 hub genes by kME for each module
  # with UCell method
  sc.obj <- ModuleExprScore(
    sc.obj,
    n_genes = 25,
    method='UCell'
  )

  # get hMEs from seurat object
  MEs <- GetMEs(sc.obj, harmonized=TRUE)
  mods <- colnames(MEs); mods <- mods[mods != 'grey']

  # add hMEs to Seurat meta-data:
  sc.obj@meta.data <- cbind(sc.obj@meta.data, MEs)

Also I am wondering if you are able to reproduce this error on the tutorial dataset or if this is a problem specific to your dataset? I have not but will try the code with the tutorial dataset.

many thanks, shanshan

smorabit commented 5 months ago

It looks like in your code you did not run the ModuleEigengenes function, which is required before making the network plots.