前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >🤩 hdWGCNA | 单细胞数据怎么做WGCNA呢!?(三)(pseudobulk data)

🤩 hdWGCNA | 单细胞数据怎么做WGCNA呢!?(三)(pseudobulk data)

作者头像
生信漫卷
发布2024-05-27 12:52:18
1311
发布2024-05-27 12:52:18
举报

1写在前面

今天没什么废话,主要讲讲如何用pseudobulk datahdWGCNA。🙃

肯定有小伙伴问这种方法和之前讲的有什么区别呀,为什么要用这个!?🫠

之前我们用的方法都是基于metacells or metaspots的方法。🌟

如果说你的数据量非常大,如果还用之前的方法,运行起来会非常慢,这个时候如果使用pseudobulk的话,就会显著加速啦。⏩

当然啦,优点远不止这些,后面再慢慢介绍吧。🙊

2用到的包

代码语言:javascript
复制
rm(list = ls())
library(Seurat)
library(tidyverse)
library(cowplot)
library(patchwork)
library(WGCNA)
library(hdWGCNA)

记得设置一下哦!~😂

代码语言:javascript
复制
theme_set(theme_cowplot())

set.seed(123)

enableWGCNAThreads(nThreads = 8)

3示例数据

代码语言:javascript
复制
seurat_obj <- readRDS('Zhou_2020.rds')

4创建pseudobulk数据矩阵

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

# Construt the pseudobulk expression profiles
datExpr <- ConstructPseudobulk(
  seurat_obj,
  group.by = 'cell_type',
  replicate_col = 'Sample',
  assay = 'RNA',
  slot = 'counts', # this should always be counts!
  min_reps = 10
)

# compute log2CPM normalization
# You can substitute this with another normalization of your choosing.
cpm <- t(apply(datExpr, 1, function(x){
    y <- (x) / sum(x) * 1000000
    log2(y + 1)
}))

seurat_obj <- SetDatExpr(
  seurat_obj,
  mat = cpm
)

5只对一种细胞类型进行hdWGCNA(Optional)

如果你只想对其中一种细胞进行WGCNA,那就运行下面的代码,标注一下你要的细胞就行了。

代码语言:javascript
复制
# # we only want the data from the astrocytes 
# cur_group <- 'ASC'
# 
# # subset the matrix for just this cell type
# cur_cpm <- cpm[grepl(cur_group, rownames(cpm)),]
# 
# seurat_obj <- SetDatExpr(
#   seurat_obj,
#   mat = cur_cpm
# )

6共表达网络分析

6.1 计算soft power threshold

代码语言:javascript
复制
# select the soft power threshold
seurat_obj <- TestSoftPowers(seurat_obj)

6.2 构建网络并识别模块

代码语言:javascript
复制
# construct the co-expression network and identify gene modules
seurat_obj <- ConstructNetwork(
    seurat_obj, 
    tom_name='pseudobulk', 
    overwrite_tom=TRUE,
    mergeCutHeight=0.15
)

6.3 可视化

代码语言:javascript
复制
PlotDendrogram(seurat_obj, main='pseudobulk dendrogram')

7计算ME和kMEs并可视化

接下来,我们在单细胞水平上计算模块特征基因(ME)和基于特征基因的连通性(kMEs)。

7.1 计算ME和kMEs

代码语言:javascript
复制
# compute the MEs and kMEs
seurat_obj <- ModuleEigengenes(seurat_obj)
seurat_obj <- ModuleConnectivity(seurat_obj)

7.2 获取MEs并加入metadata

代码语言:javascript
复制
# get MEs from seurat object
MEs <- GetMEs(seurat_obj)
mods <- colnames(MEs); mods <- mods[mods != 'grey']

# add MEs to Seurat meta-data for plotting:
meta <- seurat_obj@meta.data
seurat_obj@meta.data <- cbind(meta, MEs)

7.3 dotplot可视化

代码语言:javascript
复制
# plot with Seurat's DotPlot function
p <- DotPlot(seurat_obj, features=mods, group.by = 'cell_type')

# reset the metadata
seurat_obj@meta.data <- meta

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

p

8umap降维并可视化共表达网络

代码语言:javascript
复制
# compute the co-expression network umap 
seurat_obj <- RunModuleUMAP(
  seurat_obj,
  n_hubs = 5,
  n_neighbors=10,
  min_dist=0.4,
  spread=3,
  supervised=TRUE,
  target_weight=0.3
)

# get the hub gene UMAP table from the seurat object
umap_df <- GetModuleUMAP(seurat_obj)

代码语言:javascript
复制
# plot with ggplot
p <- ggplot(umap_df, aes(x=UMAP1, y=UMAP2)) +
  geom_point(
   color=umap_df$color,
   size=umap_df$kME*2
  ) + 
  umap_theme() 

# add the module names to the plot by taking the mean coordinates
centroid_df <- umap_df %>% 
  dplyr::group_by(module) %>%
  dplyr::summarise(UMAP1 = mean(UMAP1), UMAP2 = mean(UMAP2))
 
p <- p + geom_label(
  data = centroid_df, 
  label=as.character(centroid_df$module), 
  fontface='bold', size=2) 

p

最后祝大家早日不卷!~

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-05-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 生信漫卷 微信公众号,前往查看

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

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1写在前面
  • 2用到的包
  • 3示例数据
  • 4创建pseudobulk数据矩阵
  • 5只对一种细胞类型进行hdWGCNA(Optional)
  • 6共表达网络分析
    • 6.1 计算soft power threshold
      • 6.2 构建网络并识别模块
        • 6.3 可视化
        • 7计算ME和kMEs并可视化
          • 7.1 计算ME和kMEs
            • 7.2 获取MEs并加入metadata
              • 7.3 dotplot可视化
              • 8umap降维并可视化共表达网络
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档