Skip to contents

Quick Start with hdWGCNA

This guide provides a minimal working example to get you started with hdWGCNA in under 10 minutes. For comprehensive tutorials, see the single-cell tutorial or spatial transcriptomics tutorial.

Installation

# From R-universe (recommended)
install.packages("hdWGCNA", repos = "https://zaoqu-liu.r-universe.dev")

# Or from GitHub
devtools::install_github("Zaoqu-Liu/hdWGCNA")

Load Packages

library(hdWGCNA)
library(Seurat)
library(tidyverse)
library(patchwork)

# Enable multithreading for WGCNA
enableWGCNAThreads(nThreads = 4)

Workflow Overview

The hdWGCNA workflow consists of six main steps:

┌─────────────────────────────────────────────────────────────────┐
│                     hdWGCNA Workflow                            │
├─────────────────────────────────────────────────────────────────┤
│  1. SetupForWGCNA()      → Initialize hdWGCNA experiment        │
│  2. MetacellsByGroups()  → Construct metacells                  │
│  3. SetDatExpr()         → Set expression matrix                │
│  4. TestSoftPowers()     → Determine optimal soft power         │
│  5. ConstructNetwork()   → Build co-expression network          │
│  6. ModuleEigengenes()   → Compute module signatures            │
└─────────────────────────────────────────────────────────────────┘

Step-by-Step Example

Step 1: Setup

# Assuming you have a Seurat object called 'seurat_obj'
# with clustering already performed

seurat_obj <- SetupForWGCNA(
  seurat_obj,
  wgcna_name = "tutorial",
  features = VariableFeatures(seurat_obj)  # or specify custom gene list
)

Step 2: Construct Metacells

# Build metacells grouped by cell type
seurat_obj <- MetacellsByGroups(
  seurat_obj,
  group.by = "seurat_clusters",  # grouping variable
  k = 25,                         # cells per metacell
  max_shared = 10,                # max shared cells
  target_metacells = 1000         # target number of metacells
)

# Normalize metacell expression
seurat_obj <- NormalizeMetacells(seurat_obj)

Step 3: Set Expression Matrix

# Select cell population(s) for network analysis
seurat_obj <- SetDatExpr(
  seurat_obj,
  group_name = c("0", "1", "2"),  # cluster IDs to include
  group.by = "seurat_clusters",
  assay = "RNA"
)

Step 4: Test Soft Powers

# Find optimal soft power threshold
seurat_obj <- TestSoftPowers(
  seurat_obj,
  networkType = "signed"
)

# Visualize results
PlotSoftPowers(seurat_obj)

Step 5: Construct Network

# Build the co-expression network
seurat_obj <- ConstructNetwork(
  seurat_obj,
  soft_power = 6,         # use value from TestSoftPowers
  networkType = "signed",
  minModuleSize = 30,
  mergeCutHeight = 0.25
)

# Visualize the dendrogram
PlotDendrogram(seurat_obj)

Step 6: Compute Module Eigengenes

# Compute module eigengenes
seurat_obj <- ModuleEigengenes(seurat_obj)

# Compute hub gene connectivity
seurat_obj <- ModuleConnectivity(seurat_obj)

# Get hub genes
hub_genes <- GetHubGenes(seurat_obj, n_hubs = 10)
print(hub_genes)

Basic Visualizations

Module Expression in UMAP

# Get module colors
modules <- GetModules(seurat_obj)
mod_colors <- dplyr::select(modules, module, color) %>% 
  dplyr::distinct() %>%
  dplyr::arrange(module)

# Feature plot of module eigengenes
ModuleFeaturePlot(
  seurat_obj,
  features = "hMEs",
  order = TRUE
)

Module Network

# Network plot for a specific module
ModuleNetworkPlot(
  seurat_obj,
  mods = "blue"
)

Hub Gene Network

HubGeneNetworkPlot(
  seurat_obj,
  n_hubs = 5,
  mods = "all"
)

What’s Next?

After completing this quick start, explore:

Common Parameters Reference

Function Key Parameter Description Default
MetacellsByGroups k Cells per metacell 25
MetacellsByGroups max_shared Max shared cells 10
TestSoftPowers networkType Network type “signed”
ConstructNetwork soft_power Soft threshold auto
ConstructNetwork minModuleSize Min module size 50
ConstructNetwork mergeCutHeight Merge threshold 0.2

Session Information

## R version 4.4.0 (2024-04-24)
## Platform: aarch64-apple-darwin20
## Running under: macOS 15.6.1
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib 
## LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0
## 
## locale:
## [1] C
## 
## time zone: Asia/Shanghai
## tzcode source: internal
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## loaded via a namespace (and not attached):
##  [1] digest_0.6.39     desc_1.4.3        R6_2.6.1          fastmap_1.2.0    
##  [5] xfun_0.56         cachem_1.1.0      knitr_1.51        htmltools_0.5.9  
##  [9] rmarkdown_2.30    lifecycle_1.0.5   cli_3.6.5         sass_0.4.10      
## [13] pkgdown_2.1.3     textshaping_1.0.4 jquerylib_0.1.4   systemfonts_1.3.1
## [17] compiler_4.4.0    tools_4.4.0       ragg_1.5.0        bslib_0.9.0      
## [21] evaluate_1.0.5    yaml_2.3.12       otel_0.2.0        jsonlite_2.0.0   
## [25] rlang_1.1.7       fs_1.6.6          htmlwidgets_1.6.4