首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >基于Seurat的空转单样本数据分析流程学习(四)-hdWGCNA

基于Seurat的空转单样本数据分析流程学习(四)-hdWGCNA

原创
作者头像
凑齐六个字吧
发布2025-09-21 12:51:29
发布2025-09-21 12:51:29
2460
举报
文章被收录于专栏:单细胞单细胞

hdWGCNA全称是high-dimensional Weighted Gene Co-expression Network Analysis,即高维加权基因共表达网络分析。它是对经典的WGCNA(Weighted Gene Co-expression Network Analysis)方法的扩展和优化,专门适配单细胞转录组(scRNA-seq)和空间转录组(ST)数据。

分析流程
1.导入
代码语言:javascript
复制
rm(list = ls())
library(Seurat)
library(tidyverse)
library(cowplot)
library(patchwork)
library(WGCNA)
library(hdWGCNA)
enableWGCNAThreads(nThreads = 8)

dir.create("0-hdWGCNA")   
setwd("0-hdWGCNA")   
2.数据预处理

hdWGCNA要求空间坐标存储在meta.data中,因此单样本读取的方式可能更加合适,使用了GSM8633891和GSM8633892这两个样本

代码语言:javascript
复制
getwd()
data <- Read10X_h5("./GSE281978/GSM8633891/filtered_feature_bc_matrix.h5")
dim(data)
object <- CreateSeuratObject(counts = data, 
                             assay = "Spatial", 
                             min.cells = 3, #过滤在少于3个细胞中表达的基因,以减少低表达基因的干扰
                             project = "GSM8633891")
object

# 再读取
image <- Read10X_Image(image.dir = "./GSE281978/GSM8633891/spatial/", 
                       image.name = "tissue_hires_image.png",
                       filter.matrix = TRUE)
image
  
dim(image)
image <- image[Cells(x = object)]# 筛选图像对象中包含的SPOT
DefaultAssay(object = image) <- "Spatial" # 设置图像对象的默认assay为"Spatial"

# 添加图片到object中
object[["GSM8633891"]] <- image
object@images[["GSM8633891"]]@scale.factors$lowres <- object@images[["GSM8633891"]]@scale.factors$hires

# scale.factors
object@images[["GSM8633891"]]@scale.factors

seurat_obj <- object
## 简单探索一下数据结构
as.data.frame(seurat_obj[["Spatial"]]$counts[1:4,1:4])
as.data.frame(LayerData(seurat_obj, 
                        assay = "Spatial", 
                        layer = "counts")[1:5,1:5])
head(seurat_obj@meta.data)
table(seurat_obj$orig.ident)

Layers(seurat_obj)
Assays(seurat_obj)

# 添加坐标信息
# 增加barcode列信息给Seurat obj 
seurat_obj$barcode <- colnames(seurat_obj)

# 读取样本路径
tissue_positions <- read.csv('./GSE281978/GSM8633891/tissue_positions_list.csv',header = T)
tissue_positions <- subset(tissue_positions, barcode %in% seurat_obj$barcode)

# 将image_df与Seurat的元数据合并
new_meta <- dplyr::left_join(seurat_obj@meta.data, tissue_positions, by='barcode')

# 将新的元数据添加到Seurat对象seurat_vhd
seurat_obj$row <- new_meta$array_row 
seurat_obj$imagerow <- new_meta$pxl_row_in_fullres
seurat_obj$col <- new_meta$array_col
seurat_obj$imagecol <- new_meta$pxl_col_in_fullres

library(qs)
qsave(seurat_obj, file="./GSE281978/GSM8633891/GSM8633891.qs")

数据结构和命名,这里面不同层级中有两个position的表格,更高层级的表格中补充了列名方便添加。

3.数据整合
代码语言:javascript
复制
# load the samples
dat1 <- qread("./GSE281978/GSM8633891/GSM8633891.qs")
dat1$region <- 'GSM8633891'
dat2 <- qread("./GSE281978/GSM8633892/GSM8633892.qs")
dat2$region <- 'GSM8633892'

# merge
seurat_obj <- merge(dat1, dat2)
seurat_obj$region <- factor(as.character(seurat_obj$region), 
                            levels=c('GSM8633891', 'GSM8633892'))

#joinlayers
seurat_obj <- JoinLayers(seurat_obj)

qsave(seurat_obj,file = "seurat_obj.qs")
4.聚类分析
代码语言:javascript
复制
# normalization, feature selection, scaling, and PCA
seurat_obj <- seurat_obj %>%
  NormalizeData() %>%
  FindVariableFeatures() %>%
  ScaleData() %>%
  RunPCA()

# Louvain clustering and umap
seurat_obj <- FindNeighbors(seurat_obj, dims = 1:30)
seurat_obj <- FindClusters(seurat_obj,verbose = TRUE)
seurat_obj <- RunUMAP(seurat_obj, dims = 1:30)

# 设定样本分组
seurat_obj$region <- factor(as.character(seurat_obj$orig.ident),
                             levels=c('GSM8633891', 'GSM8633892'))

# show the UMAP
p1 <- DimPlot(seurat_obj, 
              label=TRUE, 
              reduction = "umap", 
              group.by = "seurat_clusters") + NoLegend()
p1

p2 <- SpatialDimPlot(seurat_obj, 
                     label = TRUE, 
                     label.size = 3)
p2

聚类分析和映射

5.cluster注释

这里的注释是不正确的,仅供演示

代码语言:javascript
复制
# 把准备好的注释添加到Seurat对象中
annotations <- read.csv('annotations.csv',sep = ",") 
head(annotations)
#   seurat_clusters  annotation
# 1               0 fibroblasts
# 2               1 fibroblasts
# 3               2 endothelial
# 4               3   malignant
# 5               4   dendritic
# 6               5   malignant
ix <- match(seurat_obj$seurat_clusters, annotations$seurat_clusters)
seurat_obj$annotation <- annotations$annotation[ix]
a <- seurat_obj@meta.data
colnames(seurat_obj@meta.data)
# 设定注释
Idents(seurat_obj) <- seurat_obj$annotation

p3 <- SpatialDimPlot(seurat_obj, label = TRUE, label.size = 3)
p3 + NoLegend()
6.构建metaSpots

Visium空间转录组(ST)在每个spot中生成的是稀疏的基因表达谱,因此在共表达网络分析中存在与单细胞数据相同的潜在问题。为了缓解这些问题,hdWGCNA提供了一种数据聚合方法,用于生成空间metaspots,类似于之前的metacell算法。这种方法是根据空间坐标(而非转录组表达)聚合相邻的 spot。在hdWGCNA中,这一过程可以通过 MetaspotsByGroups 函数来实现。

在这里,为 hdWGCNA 设置数据并运行 MetaspotsByGroups。类似于MetacellsByGroups,group.by 参数用于对Seurat 对象seurat_vhd 进行分组,从而为每个组单独构建 metaspots。这里仅按ST切片(slides)进行分组,以便分别对不同样本执行此步骤,也可以根据分析需要指定cluster或解剖区域(anatomical regions)来进行分组。

代码语言:javascript
复制
seurat_obj <- SetupForWGCNA(
  seurat_obj,
  gene_select = "fraction",
  fraction = 0.05,
  wgcna_name = "vis"
)

seurat_obj <- MetaspotsByGroups(
  seurat_obj,
  group.by = c("region"),
  ident.group = "region",
  assay = 'Spatial'
)
seurat_obj  <- NormalizeMetacells(seurat_obj)
7.共表达网络分析
7.1 软阈值分析
代码语言:javascript
复制
# set up the expression matrix, set group.by and group_name to NULL to include all spots
seurat_obj  <- SetDatExpr(
  seurat_obj,
  group.by=NULL,
  group_name = NULL
)

# test different soft power thresholds
library(doParallel)
registerDoParallel(1)  # 单线程
seurat_obj <- TestSoftPowers(seurat_obj)
plot_list <- PlotSoftPowers(seurat_obj)

wrap_plots(plot_list, ncol=2)
7.2 Spatial hdWGCNA dendrogram
代码语言:javascript
复制
# construct co-expression network:
seurat_obj <- ConstructNetwork(
  seurat_obj,
  tom_name='test',
  overwrite_tom=TRUE
)

# plot the dendrogram
PlotDendrogram(seurat_obj, main='Spatial hdWGCNA dendrogram')
7.3 计算模块特征基因(MEs)和基于特征基因的连通性(kMEs)
代码语言:javascript
复制
seurat_obj <- ModuleEigengenes(seurat_obj)
seurat_obj <- ModuleConnectivity(seurat_obj)
7.4 用前缀“SM”(空间模块)重置模块名称
代码语言:javascript
复制
seurat_obj <- ResetModuleNames(
  seurat_obj,
  new_name = "SM"
)
7.5 可视化
代码语言:javascript
复制
# get module eigengenes and gene-module assignment tables
MEs <- GetMEs(seurat_obj)
modules <- GetModules(seurat_obj)
mods <- levels(modules$module); mods <- mods[mods != 'grey']

# add the MEs to the seurat metadata so we can plot it with Seurat functions
seurat_obj@meta.data <- cbind(seurat_obj@meta.data, MEs)

# plot with Seurat's DotPlot function
p <- DotPlot(seurat_obj, features=mods, group.by = 'annotation', dot.min=0.1)

# flip the x/y axes, rotate the axis labels, and change color scheme:
p <- p +
  coord_flip() +
  RotatedAxis() +
  scale_color_gradient2(high='red', mid='grey95', low='blue') +
  xlab('') + ylab('')

p

# 图太大了
#p1 <- SpatialFeaturePlot(
#  seurat_obj,
#  features = mods,
#  alpha = c(0.1, 1),
#  ncol = 8
#)

#p1
代码语言:javascript
复制
options(future.globals.maxSize = 50 * 1024^3)  
# perform UMAP embedding on the co-expression network
seurat_obj <- RunModuleUMAP(
  seurat_obj,
  n_hubs = 5,
  n_neighbors=15,
  min_dist=0.3,
  spread=1
)

# make the network plot
ModuleUMAPPlot(
  seurat_obj,
  edge.alpha=0.5,
  sample_edges=TRUE,
  keep_grey_edges=FALSE,
  edge_prop=0.075, 
  label_hubs=5 
)
参考资料:
  1. hdWGCNA:https://smorabit.github.io/hdWGCNA/articles/ST_basics.html

:若对内容有疑惑或者有发现明确错误的朋友,请联系后台。更多相关内容可关注公众号:生信方舟

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 分析流程
    • 1.导入
    • 2.数据预处理
    • 3.数据整合
    • 4.聚类分析
    • 5.cluster注释
    • 6.构建metaSpots
    • 7.共表达网络分析
      • 7.1 软阈值分析
      • 7.2 Spatial hdWGCNA dendrogram
      • 7.3 计算模块特征基因(MEs)和基于特征基因的连通性(kMEs)
      • 7.4 用前缀“SM”(空间模块)重置模块名称
      • 7.5 可视化
  • 参考资料:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档