
1简介
hdWGCNA是用于对scRNA数据进行WGCNA分析的R包,空间转录组数据也可以进行分析。hdWGCNA具有高度模块化的特点,能够在细胞和空间层次结构中构建特定情境的共表达网络。hdWGCNA可以识别高度共表达的基因模块,并通过统计检验和生物学知识资源为这些模块提供生物学背景。hdWGCNA的输入格式为Seurat对象的数据集。方便单细胞数据分析,之前我们已经详细介绍过hdWGCNA的分析,这里对教程进行更新,同时增添网络可视化。示例数据来源于Bean, J.C., Jian, J., Lu, TC. et al.Sex-specific differences in mediobasal hypothalamus in response to nutritional states. Nat Commun17, 2941 (2026).https://doi.org/10.1038/s41467-026-69239-w。此文分析也涉及了hdWGCNA分析,同时我们也学习这篇NC文章对hdWGCNA应用的思路。原文数据较大,为了方便演示,对数据进行了抽样。此处采取分层抽样,每种celltype随机抽取10%的cell,保证抽样后的数据每种celltype包含足够数量。
library(Seurat)
setwd("/home/data_analysis")
GSE282955_ARH_Sex_by_Nutr <- readRDS("~/data_analysis/GSE282955_ARH_Sex_by_Nutr.rds")
dim(GSE282955_ARH_Sex_by_Nutr)
#[1] 35318 93282#抽样
set.seed(123)
# 从 meta.data 中抽样
cells_keep <- GSE282955_ARH_Sex_by_Nutr@meta.data %>%
rownames_to_column("cell") %>%
group_by(cell_type) %>% # 替换为你注释的细胞类型列名
sample_frac(size = 0.1) %>%
ungroup() %>%
pull("cell")
# 创建子集对象
GSE282955_ARH_Sex_by_Nutr_sub <- subset(GSE282955_ARH_Sex_by_Nutr, cells = cells_keep)
DimPlot(GSE282955_ARH_Sex_by_Nutr_sub,label = T)+NoLegend()
dim(GSE282955_ARH_Sex_by_Nutr_sub)
#[1] 35318 9330
save(GSE282955_ARH_Sex_by_Nutr_sub,file='GSE282955_ARH_Sex_by_Nutr_sub.RData')此包的安装依赖比较多,直接github安装,基本上包都要进行更新安装!WGCNA有很多术语,这里简单介绍一下,方便对于结果的理解。在bulk RNA的WGCNA教程中,我们有过注释,参考转录组/蛋白组WGCNA分析及个性化作图---你的CNS文章值得拥有。
#devtools::install_github('smorabit/hdWGCNA', ref='dev')
library(hdWGCNA)
package.version('hdWGCNA')Term | Definition |
|---|---|
Co-expression network | 共表达网络定义为无向加权基因网络。该网络的节点对应于基因表达谱,基因间的边由基因表达之间的成对相关性决定。通过将相关系数绝对值提升至幂次β≥1(软阈值化),加权基因共表达网络的构建强调高相关性而弱化低相关性。 |
Module | 模块是高度互连基因的聚类。在unsigned co-expression network中,模块对应于具有高绝对相关性的基因聚类。在signed network中,模块对应于正相关的基因。 |
Connectivity | 对于每个基因,连接度定义为与网络中其他基因连接强度的总和。在共表达网络中,连接度衡量一个基因与网络中所有其他基因的相关程度。 |
Intramodular connectivity kIM | 模块内连接度衡量给定基因与特定模块内其他基因的连接程度或共表达水平。模块内连接度可被解释为成为模块成员资格的度量指标。 |
Module eigengene E | 模块特征基因定义为给定模块的第一主成分。它可以被视为模块中基因表达谱的代表。 |
Eigengene significance | 将模块特征基因与性状变量进行相关性分析。该相关系数被称为特征基因显著性。 |
Module Membership, also known as eigengene-based connectivity kME | 对于每个基因,通过将其基因表达谱与给定模块的模块特征基因进行相关性分析,定义一个”模糊”的模块成员隶属度量。例如,MMblue(i) = cor(xi, Eblue) 衡量基因i与蓝色模块特征基因的相关程度。MMblue(i) 测量第i个基因相对于蓝色模块的隶属资格。如果MMblue(i) 接近0,则第i个基因不属于蓝色模块。另一方面,如果MMblue(i) 接近1或-1,则该基因与蓝色模块基因高度连接。Module Membership的符号编码了基因与蓝色模块特征基因是正相关还是负相关关系。模块成员资格度量可以为所有输入基因定义(无论其原始模块归属如何)。事实证明,模块成员资格度量与模块内连接度kIM高度相关。高度连接的模块内枢纽基因往往对相应模块具有高模块成员资格值。 |
Hub gene | highly connected gene,共表达模块内的基因具有较高的连接度。 |
Gene significance GS | 为了将外部信息纳入共表达网络, 利用基因显著性度量。抽象地说, GSi的绝对值越大, 第i个基因的生物学意义越重要。 |
Module significance | 模块显著性被定义为给定模块中所有基因平均绝对基因显著性度量。当基因显著性被定义为基因表达谱与性状y的相关性时,这种度量往往与模块特征基因与y的相关性高度相关。 |
加载R包数据
library(hdWGCNA)
library(WGCNA)
library(Seurat)
library(tidyverse)
library(igraph)
library(cowplot)
library(patchwork)
library(dplyr)
library(ggplot2)
library(stringr)setwd('/Users/ks_ts/Documents/公众号文章/hdWGCNA复现/')
load("./GSE282955_ARH_Sex_by_Nutr_sub.RData")#将原始数据赋予一个新的对象,以保留原来数据
seurat_obj <- GSE282955_ARH_Sex_by_Nutr_sub#setup
seurat_obj <- SetupForWGCNA(
seurat_obj,#seurat对象
gene_select = "fraction", #这里使用所有基因
fraction = 0.05, #基因表达百分比,0.05表示这个基因必在5%的细胞中表达
wgcna_name = "ARH" #此次WGCNA分析项目名称
)# construct metacells in each group
seurat_obj <- MetacellsByGroups(
seurat_obj = seurat_obj,
group.by = c("cell_type3", "Batch"),
reduction = 'pca', # select the dimensionality reduction to perform KNN on
k = 25,
ident.group = 'cell_type3'
)# normalize metacell expression matrix:
seurat_obj <- NormalizeMetacells(seurat_obj)选择分析celltype:在这篇NC文章中,作者主要关注Agrp细胞中的基因表达模块,所以构建网络分析的时候选择了这种celltype,也就是在SetDatExpr函数中group_name设置为目标celltype,指定Agrp的表达矩阵用于后续的分析。
##set up the expression matrix
seurat_obj <- SetDatExpr(
seurat_obj,
group_name = "Agrp", #需要进行分析的celltype
group.by='cell_type3', #metadata中包含celltype的列
assay = 'SCT',
layer = 'data'
)#Select soft-power threshold
# Test different soft powers:
seurat_obj <- TestSoftPowers(
seurat_obj,
networkType = 'signed' #对于生物学数据,网络type更适合signed,明确区分正负调控
)# plot the results:
plot_list <- PlotSoftPowers(seurat_obj)wrap_plots(plot_list, ncol=2)
# construct co-expression network:
seurat_obj <- ConstructNetwork(
seurat_obj, soft_power=15,
setDatExpr=FALSE,
tom_name = 'Agrp'
)PlotDendrogram(seurat_obj, main='Agrp hdWGCNA Dendrogram')
模块特征基因(Module Eigengenes,MEs)是一种常用指标,用于总结整个共表达模块的基因表达谱。
seurat_obj <- ScaleData(seurat_obj, features=VariableFeatures(seurat_obj))
# compute all MEs in the full single-cell dataset
seurat_obj <- ModuleEigengenes(
seurat_obj,
group.by.vars="Batch"
)hMEs <- GetMEs(seurat_obj)# compute eigengene-based connectivity (kME):
seurat_obj<- ModuleConnectivity(
seurat_obj,
group.by = 'cell_type3',
group_name = 'Agrp'
)# rename the modules
seurat_obj <- ResetModuleNames(
seurat_obj,
new_name = "Agrp_M"#原始的module名称是阿拉伯数字1,2,3. new_name相当于在他们前面添加了前缀Agrp_M1,Agrp_M2.。。
)# get the module assignment table:
modules <- GetModules(seurat_obj) %>% subset(module != 'grey')默认情况下,hdWGCNA会为每个模块分配一个唯一的颜色,并且模块通常通过这种颜色来指代,很多时候我们需要进行排版统一,需要修改颜色,这里可以根据自己需求为hdWGCNA模块分配新的颜色,便于后续可视化修饰。
#查看目前的module
modules <- GetModules(seurat_obj)#获取module level对应的module color,也就是颜色顺序与levels(modules$module)一致
mod_colors <- dplyr::select(modules, c(module, color)) %>%
distinct %>% arrange(module) %>% .$color
mod_colors#这里一共有14个module需要重新分配颜色
my_color = c("#D52126", "#88CCEE","#FEE52C","#117733","#CC61B0","#99C945","#2F8AC4",
"#332288","#E68316", "#661101", "#F97B72", "#DDCC77", "#11A579", "#89288F")
seurat_obj <- ResetModuleColors(seurat_obj, my_color)重新修改了module color,可以重新可视化聚类树
PlotDendrogram(seurat_obj, main='Agrp hdWGCNA Dendrogram')
PlotKMEs(seurat_obj, ncol=4)
# compute gene scoring for the top 25 hub genes by kME for each module
# with UCell method
library(UCell)
seurat_obj <- ModuleExprScore(
seurat_obj,
n_genes = 25,
method='UCell'
)plot_list <- ModuleFeaturePlot(
seurat_obj,
features='scores', # plot the hub gene scores
order='shuffle',
reduction = 'tsne',# order so cells are shuffled
ucell = TRUE,# depending on Seurat vs UCell for gene scoring
point_size =0.1)wrap_plots(plot_list, ncol=4)+ plot_annotation(title = "Hub genes signature scores")
hdWGCNA提供了丰富的共表达网络可视化,ModuleNetworkPlot:为每个模块单独绘制网络图,展示按 kME 值排序的前 25 个基因。HubGeneNetworkPlot:绘制包含所有模块的网络图,每个模块显示指定数量的枢纽基因。ModuleUMAPPlot:使用UMAP降维算法同时可视化共表达网络中的所有基因。
#plot所有module的网络,并将文件导出储存在Agrp_all_ModuleNetworks文件夹
ModuleNetworkPlot(
seurat_obj,
outdir = './Agrp_all_ModuleNetworks'
)ModuleNetworkPlot(
seurat_obj,
mods = 'Agrp_M4',
n_inner = 10, # number of genes in inner ring
n_outer = 15, # number of genes in outer ring
n_conns = Inf, # show all of the connections
vertex.label.cex=1 # font size
)HubGeneNetworkPlot(
seurat_obj,
n_hubs = 10, #每个module需要展示的hub基因数量
n_other=5,#每个module需要展示的非hub基因数量
edge_prop = 0.75,#对网络中的边数进行降采样
mods = c('Agrp_M1','Agrp_M2','Agrp_M4','Agrp_M6','Agrp_M7','Agrp_M8','Agrp_M9') #为了演示选择几个module,可以选择任意module
)
##Applying UMAP to co-expression networks
seurat_obj <- RunModuleUMAP(
seurat_obj,
n_hubs = 10,
n_neighbors=15,
min_dist=0.3 # min distance between points in UMAP space
)ModuleUMAPPlot(
seurat_obj,
edge.alpha=0.5,
sample_edges=TRUE,
edge_prop=0.1, # proportion of edges to sample (20% here)
label_hubs=3 ,# how many hub genes to plot per module?
keep_grey_edges=FALSE
)

ks_hdWGCNA_UMAPnet(seurat_obj=seurat_obj,
wgcna_name = 'ARH',
edge_prop=0.05,
top_hub=3,
r_text=10)