小杜的生信笔记 发表于 2024-6-24 22:26:19

hdWGCNA | 单细胞(scRNA)和空间转录组共表达网络分析

[![](data/attachment/forum/plugin_zhanmishu_markdown/202406/3ef11de9e48e36ac1cf8f1ab25933ee8_1719239157_8671.jpg)](http://mp.weixin.qq.com/s?__biz=MzAwODY5NDU0MA==&mid=2455861689&idx=2&sn=bf497c5f96fbccc071751f5e9eb087a5&chksm=8cff27dabb88aecc14142b2712e8bbd0f4ed2d6660b4814cd2031212edf682bd8c61f2fb391a#rd)**「一边学习,一边总结,一边分享!」**

### 本期教程

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/6eb50abaa7c7f29bc4115eac27eee42f_1719239157_5315.jpg)

**获得本期教程文本教程,「回复关键词」:****20240520**

**❝**

**「小杜的生信笔记」,自2021年11月开始做的知识分享,主要内容是「R语言绘图教程」、「转录组上游分析」、****「转录组下游分析」**等内容。凡事在社群同学,可免费获得自2021年11月份至今全部教程,教程配备事例数据和相关代码,我们会持续更新中。

**❞**

### 往期教程部分内容

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/7176dcefb2a233e077390d8fbf02d050_1719239157_7035.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/6792e65ca3b76eeb04d2807ef69de75b_1719239157_7680.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/3b7c929b1b5cec75e69650af6972e88f_1719239157_8450.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/50a49493543456a56bf9f7d4b9c6794c_1719239157_6066.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/6e57fc16716c89db62c4a4c69434046f_1719239157_1918.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/2e3ba62f273f59b227dde4c4bf11f7c1_1719239157_8128.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/7f2c33e6ab3006e85fa4a857b87aab33_1719239157_9437.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/15b7e20f410683b23440ef659cbd7445_1719239157_7216.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/e4e60b55cf6a6c4d67d72702036ed517_1719239157_5746.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/effba8a91e77bf0927b5ef3ddc8353ac_1719239157_3386.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/cf2431aa2e1965d0f31600cf4e854641_1719239157_4483.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/9f9af3c682ad1fd90e756052408ca148_1719239157_3907.jpg)![](data/attachment/forum/plugin_zhanmishu_markdown/202406/66cfdf1723fd4bdd43686ad2d2f841c4_1719239157_6685.jpg)

# **关于hdWGCNA**

**hdWGCNA (high dimensional WGCNA,高纬度WGCNA),用于**单细胞RNA-seq**或**空间转录组学**等高维转录组学数据中执行加权基因共表达网络分析。**

```plain
详情查看:https://smorabit.github.io/hdWGCNA/articles/basic_tutorial.html
```

## hdWGCNA包

```plain
https://github.com/smorabit/hdWGCNA
```

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/2c6d26e9ad8a1c96b8784793a0dabf8c_1719239157_8582.jpg)

## 下载所需数据

```plain
# wget https://swaruplab.bio.uci.edu/public_data/Zhou_2020.rds
```

## Load the dataset and required librarie

```plain
# single-cell analysis package
library(Seurat)

# plotting and data science packages
library(tidyverse)
library(cowplot)
library(patchwork)

# co-expression network analysis packages:
library(WGCNA)
#devtools::install_github('smorabit/hdWGCNA', ref='dev')
library(hdWGCNA)

# using the cowplot theme for ggplot
theme_set(theme_cowplot())

# set random seed for reproducibility
set.seed(12345)

# optionally enable multithreading
enableWGCNAThreads(nThreads = 8)

# load the Zhou et al snRNA-seq dataset
seurat_obj <- readRDS('Zhou_2020.rds')

seurat_obj <- readRDS("C:\\sfotware\\R-4.3.2\\library\\hdWGCNA\\data\\Rdata.rds")
```

**绘制按细胞类型着色的 UMAP 图,以检查我们是否正确加载了数据,并确保我们已将细胞归入细胞群和细胞类型。**

```plain
p <- DimPlot(seurat_obj, group.by='cell_type', label=TRUE) +
   umap_theme() + ggtitle('Zhou et al Control Cortex') + NoLegend()

p
```

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/76dc39ff95949206489a3cc585754a1c_1719239157_8136.jpg)

## 设置Seurat对象

**在运行 hdWGCNA 之前,我们首先要设置 Seurat 对象。hdWGCNA 计算出的大部分信息都存储在 Seurat 对象的 @misc 插槽中。一个 Seurat 对象可以容纳多个 hdWGCNA 实验,例如代表同一单细胞数据集中的不同细胞类型。值得注意的是,由于我们将hdWGCNA 视为下游数据分析步骤,因此不支持在运行 SetupForWGCNA 之后对 Seurat 对象进行子集化。**

**在此,我们将使用**SetupForWGCNA**函数设置**Seurat**对象,并指定hdWGNCA实验的名称。该函数还将选择用于WGCNA的基因。用户可以使用 gene\_select 参数用三种不同的方法选择基因:**

* **variable**:使用存储在Seurat对象VariableFeatures中的基因。
* **fraction**:使用由**group.by**指定的在整个数据集或每组细胞中某一部分表达的基因。** **自定义:使用自定义列表中指定的基因。
* **custom**:使用自定义列表中指定的基因。

```plain
seurat_obj <- SetupForWGCNA(
seurat_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 = "tutorial"# the name of the hdWGCNA experiment
)
```

## Construct metacells

**设置好 Seurat 对象后,在 hdWGCNA 中运行 hdWGCNA pipeine 的第一步就是根据单细胞数据集构建元胞。简而言之,元细胞是由来源于同一生物样本的小群相似细胞组成的集合体。使用k-Nearest Neighbors(KNN)算法来识别需要聚合的相似细胞群,然后计算这些细胞的平均或总和表达量,从而得到元细胞基因表达矩阵。与原始表达矩阵相比,元胞表达矩阵的稀疏性大大降低,因此更适合使用。我们使用元胞代替原始单细胞的最初动机是,相关网络方法(如 WGCNA)对数据稀疏性很敏感。**

```plain
# construct metacellsin each group
seurat_obj <- MetacellsByGroups(
seurat_obj = seurat_obj,
group.by = c("cell_type", "Sample"), # 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 = 'cell_type'# set the Idents of the metacell seurat object
)

# normalize metacell expression matrix:
seurat_obj <- NormalizeMetacells(seurat_obj)
```

## Co-expression network analysis

### Set up the expression matrix

**在这里,要指定用于网络分析的表达矩阵。hdWGCNA包含**SetDatExpr**函数,用于存储用于下游网络分析的给定细胞组的转置表达矩阵。默认情况下使用元细胞表达矩阵(use\_metacells=TRUE),但如果需要,hdWGCNA也允许使用单细胞表达矩阵。该函数允许用户指定从哪个槽获取表达矩阵,例如,如果用户想应用**SCTransform**归一化而不是**NormalizeData**。**

```plain
seurat_obj <- SetDatExpr(
seurat_obj,
group_name = "INH", # the name of the group of interest in the group.by column
group.by='cell_type', # the metadata column containing the cell type info. This same column should have also been used in MetacellsByGroups
assay = 'RNA', # using RNA assay
slot = 'data'# using normalized data
)
```

### Select soft-power threshold

**选择 "soft power threshold值"。hdWGCNA构建了一个基因-基因相关性邻接矩阵,以推断基因之间的共表达关系。相关性被提升到一个幂次,以减少相关性矩阵中存在的噪声,从而保留强连接,去除弱连接。因此,确定一个合适的软幂阈值至关重要。**

**提供了一个**TestSoftPowers**函数,用于对不同的软功率阈值进行参数扫描。该函数通过检测不同功率值下的网络拓扑结构,帮助我们选择构建共表达网络的软功率阈值。共表达网络应该具有无标度拓扑结构,因此**TestSoftPowers**函数模拟了在不同软实力阈值下,共表达网络与无标度图的相似程度。**

```plain
# Test different soft powers:
seurat_obj <- TestSoftPowers(
seurat_obj,
networkType = 'signed'# you can also use "unsigned" or "signed hybrid"
)

# plot the results:
plot_list <- PlotSoftPowers(seurat_obj)

# assemble with patchwork
wrap_plots(plot_list, ncol=2)
```

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/b4b8411da3ff37b026d8a2cc7474f861_1719239157_7494.jpg)

### Construct co-expression network

```plain
# construct co-expression network:
seurat_obj <- ConstructNetwork(
seurat_obj,
tom_name = 'INH'# name of the topoligical overlap matrix written to disk
)
```

```plain
PlotDendrogram(seurat_obj, main='INH hdWGCNA Dendrogram')
```

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/f5c66cc93e9c9a709057eaa15ce96546_1719239157_9260.jpg)

### Compute module connectivity

```plain
# compute eigengene-based connectivity (kME):
seurat_obj <- ModuleConnectivity(
seurat_obj,
group.by = 'cell_type', group_name = 'INH'
)


# rename the modules
seurat_obj <- ResetModuleNames(
seurat_obj,
new_name = "INH-M"
)
```

```plain
# plot genes ranked by kME for each module
p <- PlotKMEs(seurat_obj, ncol=5)

p
```

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/adccb719f0ccb76e25186648323572c9_1719239157_1281.jpg)

### Module Feature Plots

**FeaturePlot is a commonly used Seurat visualization to show a feature of interest directly on the dimensionality reduction. hdWGCNA includes the ModuleFeaturePlot function to consruct FeaturePlots for each co-expression module colored by each module’s uniquely assigned color.**

```plain
# make a featureplot of hMEs for each module
plot_list <- ModuleFeaturePlot(
seurat_obj,
features='hMEs', # plot the hMEs
order=TRUE# order so the points with highest hMEs are on top
)

# stitch together with patchwork
wrap_plots(plot_list, ncol=6)
```

![](data/attachment/forum/plugin_zhanmishu_markdown/202406/f3b4fb8d46532448bcf6b8bcfc0b3f75_1719239157_6644.jpg)

**获得本期教程文本教程,「回复关键词」:****20240520**

**❝**

**若我们的教程对你有所帮助,请**点赞+收藏+转发**,这是对我们最大的支持。**



### 往期部分文章

**「1. 最全WGCNA教程(替换数据即可出全部结果与图形)」**

* (https://mp.weixin.qq.com/s/M0LAlE-61f2ZfpMiWN-iQg)
* (https://mp.weixin.qq.com/s/Ln9TP74nzWhtvt7obaMp1A)
* (https://mp.weixin.qq.com/s/rU76rLG4AayuiHbDhgOGBg)
* (https://mp.weixin.qq.com/s/Ot2h3LH82vfg4m7YLG8e0w)
* (https://mp.weixin.qq.com/s/ca8_eqirjJYgC8hiaF8jfg)

---

**「2. 精美图形绘制教程」**

* [精美图形绘制教程](https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MzAwODY5NDU0MA==&action=getalbum&album_id=2614156000866385923&scene=173&from_msgid=2455848496&from_itemidx=1&count=3&nolastread=1#wechat_redirect)

**「3. 转录组分析教程」**

* **「**[转录组上游分析教程[零基础]](https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MzAwODY5NDU0MA==&action=getalbum&album_id=2870608342451224581&scene=126&uin=&key=&devicetype=Windows+10+x64&version=63090719&lang=zh_CN&ascene=0)**」**
* **「**[一个转录组上游分析流程 | Hisat2-Stringtie](https://mp.weixin.qq.com/s/A4cFpkrKGqPeESVQl69jcA)**」**

**「4. 转录组下游分析」**

* [批量做差异分析及图形绘制 | 基于DESeq2差异分析](https://mp.weixin.qq.com/s/aIiR5v1olhv7bMkrZjfddQ)
* (https://mp.weixin.qq.com/s/NeHjQk9DEWtx5hGOJhWuZw)
* [单基因GSEA富集分析](https://mp.weixin.qq.com/s/g8ZWgSIIw_6fZimFLmRMng)
* [全基因集GSEA富集分析](https://mp.weixin.qq.com/s/BbkdplN6tHT_tHGEKPABhQ)

**「小杜的生信筆記」** **,主要发表或收录生物信息学教程,以及基于R分析和可视化(包括数据分析,图形绘制等);分享感兴趣的文献和学习资料!!**
页: [1]
查看完整版本: hdWGCNA | 单细胞(scRNA)和空间转录组共表达网络分析