Skip to content

Commit

Permalink
单细胞分析十八般武艺16:AUCell
Browse files Browse the repository at this point in the history
  • Loading branch information
ixxmu committed Nov 2, 2023
1 parent b995de0 commit 5d53e91
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/2023-11/单细胞分析十八般武艺16_AUCell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: "单细胞分析十八般武艺16:AUCell"
date: 2023-11-02T15:22:55Z
draft: ["false"]
tags: [
"fetched",
"生信会客厅"
]
categories: ["Acdemic"]
---
单细胞分析十八般武艺16:AUCell by 生信会客厅
------
<div><p><em>      单细胞测序技术的发展日新月异,新的分析工具也层出不穷。每个工具都有它的优势与不足,在没有权威工具和流程的单细胞生信江湖里,多掌握几种分析方法和工具,探索数据时常常会有意想不到的惊喜。</em></p><p><br></p><h2><span><strong><span>往期专题</span></strong></span></h2><h2><a target="_blank" href="https://mp.weixin.qq.com/mp/appmsgalbum?action=getalbum&amp;album_id=1453861481023504385&amp;__biz=MzIyMzMwNDQ2MA==#wechat_redirect" tab="innerlink" data-linktype="2" wah-hotarea="click">单细胞转录组基础分析专题</a><br></h2><p><a target="_blank" href="https://mp.weixin.qq.com/mp/appmsgalbum?action=getalbum&amp;album_id=1481464359141867525&amp;__biz=MzIyMzMwNDQ2MA==#wechat_redirect" tab="innerlink" data-linktype="2" wah-hotarea="click">单细胞转录组高级分析专题</a></p><p><br></p><p>看过我以前帖子的朋友应该对基于GSVA包的基因集评分方法有所了解,也可能在做SCENIC分析时接触了AUCell,他们之间有什么区别呢?最大的不同在于GSVA算法是多年前为表达谱芯片和bulkRNA测序数据开发的,并没有考虑到10X Genomics 技术平台(或其他高通量单细胞技术平台)产生的稀疏性数据,AUCell则是针对单细胞数据开发的基因集评分方法。</p><p><br></p><p><strong><span>AUCell简介</span></strong></p><p><strong>安装AUCell</strong><br></p><p>AUCell收录在Bioconductor平台,安装起来比较简单。<br></p><section><ul><li><li><li><li><li></ul><pre data-lang="php"><code><span><span>if</span> (!requireNamespace(<span>"BiocManager"</span>, quietly = <span>TRUE</span>))</span></code><code><span>    install.packages(<span>"BiocManager"</span>)</span></code><code><span>BiocManager::install(<span>"AUCell"</span>)</span></code><code><span><span># 查看官方教程</span></span></code><code><span>browseVignettes(<span>"AUCell"</span>)</span></code></pre></section><p><br></p><p><strong>基本原理</strong><br></p><p>AUCell为每个细胞基于表达值对基因进行排序(rank),表达值相同的基因随机赋予顺序,然后用rank值代替表达值进行后续分析。它用恢复曲线(recovery curve)下的面积代表富集分数。曲线的横坐标用rank值(如下图的1-500),每个rank值对应的基因集合记作topRankSet,纵坐标对应topRankSet在评分基因集中的基因数量。例如,rank100对应的曲线横坐标是100,其topRankSet包含此细胞排名前100的基因,假设topRankSet有15个基因在评分基因集中,那么rank100对应的曲线纵坐标就是15。</p><p><img data-ratio="0.62734375" data-s="300,640" data-src="https://mmbiz.qpic.cn/mmbiz_png/VTXDic6Eia0AESsQ01dNYnlW6wI12jUEK1ornib8HLhE7USVTicBh4F2EfBOLz2gwuFeYRTKoCDjLdkTn6fr63KLTw/640?wx_fmt=png" data-type="png" data-w="1280" src="https://mmbiz.qpic.cn/mmbiz_png/VTXDic6Eia0AESsQ01dNYnlW6wI12jUEK1ornib8HLhE7USVTicBh4F2EfBOLz2gwuFeYRTKoCDjLdkTn6fr63KLTw/640?wx_fmt=png"></p><p>计算AUC值的横坐标终点对应AUCell_calcAUC函数中的aucMaxRank参数,默认值为数据集中所有基因的前5%。这可以更快地完成大数据集上运算,并减少排名底部噪音的影响(例如很多基因的表达值为0,但是也有rank值)。<mark>对于基因数量(例如10x数据中的nfeature_count)比较高的数据集,或表达值比较高的数据集,最好提高该阈值</mark>。</p><p><br></p><p><strong>分析流程</strong><br></p><p cid="n103" mdtype="paragraph">AUCell的分析流程包含以下三个步骤:</p><ol start="" cid="n104" mdtype="list"><li><p cid="n106" mdtype="paragraph">将基因表达矩阵转换为rank矩阵;</p></li><li><p cid="n108" mdtype="paragraph">计算恢复曲线下面积;</p></li><li><p cid="n110" mdtype="paragraph">为每个基因集设置活性阈值。</p></li></ol><p><br></p><p><strong>注意事项</strong><br></p><p>AUC的值并不是绝对的,它取决于细胞类型(如细胞尺寸、转录本数量)、测序技术(如检测灵敏度)和基因集本身。通常不能获得一个只在感兴趣细胞中完全“打开”,而在其他细胞中“关闭”的理想基因集。对于基因数量较小的基因集,很可能得到AUC=0的细胞。较大的基因集(100-2k基因)可能更稳定,结果更准确。作者在官方教程中强调:<span>基因集活性阈值的自动选择并非详尽无遗,因此他们强烈建议用户检查AUC直方图,并在需要时手动选择阈值</span>。</p><p><br></p><p><strong><span>数据实测</span></strong></p><p><strong>测试数据</strong></p><p>使用SeuratData包提供的pbmc3k数据集,细胞类型已经注释好了。<br></p><p><img data-ratio="0.75" data-s="300,640" data-src="https://mmbiz.qpic.cn/mmbiz_png/VTXDic6Eia0AESsQ01dNYnlW6wI12jUEK1ibcaSqUv193gVF1WATRv7iapYSPiaskh6j6H59EhvTDolp9iacj20z71xA/640?wx_fmt=png" data-type="png" data-w="1280" src="https://mmbiz.qpic.cn/mmbiz_png/VTXDic6Eia0AESsQ01dNYnlW6wI12jUEK1ibcaSqUv193gVF1WATRv7iapYSPiaskh6j6H59EhvTDolp9iacj20z71xA/640?wx_fmt=png"></p><section><ul><li><li><li><li><li><li><li><li><li><li><li><li><li><li><li><li><li><li><li><li><li><li><li><li><li><li><li><li><li></ul><pre data-lang="php"><code><span>library(AUCell)</span></code><code><span>library(Seurat)</span></code><code><span>library(SeuratData)</span></code><code><span>library(tidyverse)</span></code><code><span>library(patchwork)</span></code><code><span>rm(<span>list</span> = ls())</span></code><code><span><br></span></code><code><span><span>### 整理演示数据</span></span></code><code><span><span># 演示数据可能需要下一行代码提前安装</span></span></code><code><span><span>#InstallData("pbmc3k.SeuratData") </span></span></code><code><span>pbmc &lt;- LoadData(<span>"pbmc3k.SeuratData"</span>)</span></code><code><span>pbmc &lt;- UpdateSeuratObject(pbmc)</span></code><code><span>pbmc &lt;- NormalizeData(pbmc)</span></code><code><span>pbmc &lt;- SCTransform(pbmc)</span></code><code><span>pbmc &lt;- RunPCA(pbmc)</span></code><code><span>ElbowPlot(pbmc, ndims = <span>50</span>)</span></code><code><span>pc.num &lt;- <span>1</span>:<span>15</span></span></code><code><span>pbmc &lt;- pbmc %&gt;% RunTSNE(dims=pc.num) %&gt;% RunUMAP(dims=pc.num)</span></code><code><span>pbmc &lt;- pbmc %&gt;% FindNeighbors(dims=pc.num) %&gt;% FindClusters()</span></code><code><span>pbmc &lt;- pbmc[,!is.na(pbmc$seurat_annotations)]</span></code><code><span>p &lt;- DimPlot(pbmc, group.by = <span>"seurat_annotations"</span>, label = T)</span></code><code><span>ggsave(<span>"1601_celltype.png"</span>, p, width = <span>8</span>, height = <span>6</span>)</span></code><code><span><br></span></code><code><span><span>### 提取细胞类型signature</span></span></code><code><span>Idents(pbmc) &lt;- <span>"seurat_annotations"</span></span></code><code><span>celltype.markers &lt;- FindAllMarkers(pbmc, assay = <span>"RNA"</span>, only.pos = T)</span></code><code><span>signatures &lt;- lapply(levels(pbmc), <span><span>function</span><span>(x)</span></span>{</span></code><code><span> rownames(subset(celltype.markers, cluster==x&amp;p_val_adj&lt;<span>0.05</span>))</span></code><code><span>})</span></code></pre></section><p><img data-ratio="0.33694181326116374" data-s="300,640" data-src="https://mmbiz.qpic.cn/mmbiz_png/VTXDic6Eia0AESsQ01dNYnlW6wI12jUEK1uzuWDgzjdnY29TWpgice0raclRhaHAS64fAXznPvF5B4vViaXf1EzUiaw/640?wx_fmt=png" data-type="png" data-w="739" src="https://mmbiz.qpic.cn/mmbiz_png/VTXDic6Eia0AESsQ01dNYnlW6wI12jUEK1uzuWDgzjdnY29TWpgice0raclRhaHAS64fAXznPvF5B4vViaXf1EzUiaw/640?wx_fmt=png"></p><p><br></p><p><strong>AUCell评分</strong></p><section><ul><li><li><li><li><li><li><li><li><li><li><li><li><li><li><li></ul><pre data-lang="nginx"><code><span><span>### AUCell评分并分配活性阈值</span></span></code><code><span><span># 提取矩阵</span></span></code><code><span><span>exprMatrix</span> &lt;- pbmc<span>@assays</span><span>$RNA</span><span>@data</span></span></code><code><span><span># 表达矩阵转rank矩阵</span></span></code><code><span>cells_rankings &lt;- AUCell_buildRankings(exprMatrix)</span></code><code><span><span># min 1% 5% 10% 50% 100% </span></span></code><code><span><span># 212.00 341.00 458.85 554.00 819.00 2413.00</span></span></code><code><span><span>#以上数据意味着排名2413以后的基因没有价值</span></span></code><code><span><br></span></code><code><span><span># 参数aucMaxRank的设置最好参考上面的结果,此处先不调整默认参数</span></span></code><code><span>cells_AUC &lt;- AUCell_calcAUC(signatures, cells_rankings, nCores = <span>10</span>, </span></code><code><span> aucMaxRank=nrow(cells_rankings)*<span>0</span>.<span>05</span>)</span></code><code><span><span># 给每个基因集分配活性阈值</span></span></code><code><span>set.seed(<span>123</span>)</span></code><code><span>cells_assignment &lt;- AUCell_exploreThresholds(cells_AUC, plotHist=TRUE, assign=TRUE)</span></code></pre></section><p>自动分配的活性阈值,效果确实值得商榷,如下面两图:<br></p><p><img data-ratio="0.6007905138339921" data-s="300,640" data-src="https://mmbiz.qpic.cn/mmbiz_png/VTXDic6Eia0AESsQ01dNYnlW6wI12jUEK10ZspDcka4bUejGc1KvUSWhPKfyYReJWkHVskUN8ljJWWec672Q6LCw/640?wx_fmt=png" data-type="png" data-w="759" src="https://mmbiz.qpic.cn/mmbiz_png/VTXDic6Eia0AESsQ01dNYnlW6wI12jUEK10ZspDcka4bUejGc1KvUSWhPKfyYReJWkHVskUN8ljJWWec672Q6LCw/640?wx_fmt=png"></p><p><span>AUCell判定所有细胞对应Naive CD4 T的signature都是激活的</span><br></p><p><img data-ratio="0.6110381077529566" data-s="300,640" data-src="https://mmbiz.qpic.cn/mmbiz_png/VTXDic6Eia0AESsQ01dNYnlW6wI12jUEK1Bj7MeQCcz3Au3ddjnO0TnRumlicQkPAn60mWGfDHEIibIq6C9ia2VXQ7Q/640?wx_fmt=png" data-type="png" data-w="761" src="https://mmbiz.qpic.cn/mmbiz_png/VTXDic6Eia0AESsQ01dNYnlW6wI12jUEK1Bj7MeQCcz3Au3ddjnO0TnRumlicQkPAn60mWGfDHEIibIq6C9ia2VXQ7Q/640?wx_fmt=png"></p><p><span>CD14+ Mono出现了明显的双峰,阈值判定结果相对可靠一些</span><br></p><p><br></p><p><strong>结果展示</strong><br></p><p>先直接用AUC评分画个热图,看看是否与细胞类型匹配。<br></p><section><ul><li><li><li></ul><pre data-lang="php"><code><span>library(pheatmap)</span></code><code><span>aucMat &lt;- getAUC(cells_AUC)</span></code><code><span>pheatmap(aucMat, show_colnames = F, scale = <span>"row"</span>)</span></code></pre></section><p><img data-ratio="0.625" data-s="300,640" data-src="https://mmbiz.qpic.cn/mmbiz_png/VTXDic6Eia0AESsQ01dNYnlW6wI12jUEK18nVticlg7JQ9y87ia6dGgWiaQKbIUcK1fZA2URfWVbWcLzPvNX0nCVRyQ/640?wx_fmt=png" data-type="png" data-w="1280" src="https://mmbiz.qpic.cn/mmbiz_png/VTXDic6Eia0AESsQ01dNYnlW6wI12jUEK18nVticlg7JQ9y87ia6dGgWiaQKbIUcK1fZA2URfWVbWcLzPvNX0nCVRyQ/640?wx_fmt=png"></p><p>再来看看使用活性阈值将auc矩阵转为二进制矩阵后的效果。<br></p><section><ul><li><li><li><li><li><li><li><li><li><li><li><li><li><li><li><li><li></ul><pre data-lang="shell"><code><span><span>#</span><span>## AUC矩阵二进制转换</span></span></code><code><span><span>#</span> 自动选择的阈值有些不合适,人工调整一下</span></code><code><span>T.adj &lt;- c("Naive CD4 T" = 0.52,</span></code><code><span> "Memory CD4 T" = 0.20,</span></code><code><span> "CD14+ Mono" = 0.235,</span></code><code><span> "B" = 0.13,</span></code><code><span> "CD8 T" = 0.165,</span></code><code><span> "FCGR3A+ Mono" = 0.11,</span></code><code><span> "NK" = 0.095,</span></code><code><span> "DC" = 0.035,</span></code><code><span> "Platelet" = 0.04)</span></code><code><span><span>#</span> auc矩阵转为只有0和1的二进制矩阵,1代表激活          </span></code><code><span>aucBin &lt;- aucMat</span></code><code><span>for(i in rownames(aucMat)){</span></code><code><span> aucBin[i,] &lt;- ifelse(aucMat[i,]&gt;T.adj[i], 1, 0)</span></code><code><span>}</span></code><code><span>pheatmap(aucBin, show_colnames = F)</span></code></pre></section><p><img data-cropselx1="0" data-cropselx2="578" data-cropsely1="0" data-cropsely2="361" data-ratio="0.625" data-s="300,640" data-src="https://mmbiz.qpic.cn/mmbiz_png/VTXDic6Eia0AESsQ01dNYnlW6wI12jUEK1L95G3SAeJHlg5FuYUzxVIMlWcR1oNic9O1F6mfLuHOwwsDAicpm5R1kA/640?wx_fmt=png" data-type="png" data-w="2400" src="https://mmbiz.qpic.cn/mmbiz_png/VTXDic6Eia0AESsQ01dNYnlW6wI12jUEK1L95G3SAeJHlg5FuYUzxVIMlWcR1oNic9O1F6mfLuHOwwsDAicpm5R1kA/640?wx_fmt=png"></p><p><br></p><p><br></p><p><strong><strong><span>性能对比</span></strong></strong><br></p><p>这次测试选择已知细胞类型的topMarkers作为评分基因集,主要是为了直观地对比几种常见评分算法。效果孰优孰劣,大家自行判断<img data-ratio="1" data-src="https://mmbiz.qpic.cn/mmbiz_png/VTXDic6Eia0AGWn7TVdicicL34LVB8tibbmSKcBiahcePB8Oa97iceDWMm4Sg2cddkmUqIJY5SicvGSFIlib9YicSezuSFZw/640?wx_fmt=png" data-type="png" data-w="20" src="https://mmbiz.qpic.cn/mmbiz_png/VTXDic6Eia0AGWn7TVdicicL34LVB8tibbmSKcBiahcePB8Oa97iceDWMm4Sg2cddkmUqIJY5SicvGSFIlib9YicSezuSFZw/640?wx_fmt=png"></p><p><br></p><p><strong>AddModuleScore</strong></p><p>Seurat包中的AddModuleScore函数,第一步是计算表达矩阵中所有基因的平均表达值,然后根据平均表达值把所有基因分成24个(默认值)背景基因集。假设评分基因集为ScoreSet,分割的背景基因集为BinSets。第二步是为ScoreSet中的每个基因找到对应的BinSet,从其中随机挑选100个(默认值)基因作为背景基因。如果ScoreSet有10个基因,那么算法会为它随机挑选最多1000个背景基因(选完之后会去重),那么最后的评分是用这10个基因的均值减去那1000个基因的均值。</p><section><ul><li><li><li><li></ul><pre data-lang="kotlin"><code><span>sco &lt;- AddModuleScore(pbmc, features = signatures, name = <span>"M"</span>, assay = <span>"RNA"</span>)</span></code><code><span>mat &lt;- t(<span>sco@</span>meta.<span>data</span>[,c(paste0(<span>"M"</span>,<span>1</span>:<span>9</span>))])</span></code><code><span>rownames(mat) &lt;- levels(pbmc)</span></code><code><span>pheatmap(mat, show_colnames = F, scale = <span>"row"</span>)</span></code></pre></section><p><img data-ratio="0.625" data-s="300,640" data-src="https://mmbiz.qpic.cn/mmbiz_png/VTXDic6Eia0AGWn7TVdicicL34LVB8tibbmSKxZu2c8ERDwG1qcgV8brpDIhCDVfibBA1uHk2dJe5YOia8h7Ef5GfWT2g/640?wx_fmt=png" data-type="png" data-w="1280" src="https://mmbiz.qpic.cn/mmbiz_png/VTXDic6Eia0AGWn7TVdicicL34LVB8tibbmSKxZu2c8ERDwG1qcgV8brpDIhCDVfibBA1uHk2dJe5YOia8h7Ef5GfWT2g/640?wx_fmt=png"></p><p><br></p><p><strong>ssGSEA</strong></p><p>首先用表达值对每个样本(细胞)的基因进行排名,并用排名顺序代替表达值用于后续分析。然后用样本基因在评分基因集内和基因集外的经验累积分布函数之间的差值生成富集分数。通俗地讲就是按基因排名顺序,逐个检查基因是否在评分基因集中;如果在基因集内富集分数加分,反之扣分,并且排名越靠前加分权重越大。最后,根据基因表达值的极差对富集分数进行标准化。</p><section><ul><li><li><li><li></ul><pre data-lang="xml"><code><span>library(GSVA)</span></code><code><span>expr <span>&lt;<span>-</span> <span>as.matrix</span>(<span>pbmc</span>@<span>assays</span>$<span>RNA</span>@<span>data</span>)</span></span></code><code><span>mat1 <span>&lt;<span>-</span> <span>gsva</span>(<span>expr</span>, <span>signatures</span>, <span>method</span>=<span>"ssgsea"</span>, <span>parallel.sz</span>=<span>18L)</span></span></span></code><code><span><span>pheatmap</span>(<span>mat1</span>, <span>show_colnames</span> = <span>F,</span> <span>scale</span> = <span>"row"</span>)</span></code></pre></section><p><img data-ratio="0.625" data-s="300,640" data-src="https://mmbiz.qpic.cn/mmbiz_png/VTXDic6Eia0AGWn7TVdicicL34LVB8tibbmSKtHVVWgrzDibLhnErNqmznM9R1UAGpVmQQxfyD3YKQWkWl8Dcos3icBzA/640?wx_fmt=png" data-type="png" data-w="1280" src="https://mmbiz.qpic.cn/mmbiz_png/VTXDic6Eia0AGWn7TVdicicL34LVB8tibbmSKtHVVWgrzDibLhnErNqmznM9R1UAGpVmQQxfyD3YKQWkWl8Dcos3icBzA/640?wx_fmt=png"></p><p><br></p><p><strong>GSVA</strong></p><p>GSVA算是ssGSEA升级版,主要变化是在排序之前会用核函数对表达矩阵进行归一化,不同的数据类型归一化算法不一样,可以通过kcdf参数指定。<br></p><section><ul><li><li></ul><pre data-lang="xml"><code><span>mat2 <span>&lt;<span>-</span> <span>gsva</span>(<span>expr</span>, <span>signatures</span>, <span>method</span>=<span>"gsva"</span>, <span>kcdf</span>=<span>"Gaussian"</span>, <span>parallel.sz</span>=<span>18L)</span></span></span></code><code><span><span>pheatmap</span>(<span>mat2</span>, <span>show_colnames</span> = <span>F,</span> <span>scale</span> = <span>"row"</span>)</span></code></pre></section><p><img data-ratio="0.625" data-s="300,640" data-src="https://mmbiz.qpic.cn/mmbiz_png/VTXDic6Eia0AGWn7TVdicicL34LVB8tibbmSKtMFpFyibJK0aHjUeLQf2RlVJA93OcpAFyqUicAI2KHNK3dUiaZqqcSJiaw/640?wx_fmt=png" data-type="png" data-w="1280" src="https://mmbiz.qpic.cn/mmbiz_png/VTXDic6Eia0AGWn7TVdicicL34LVB8tibbmSKtMFpFyibJK0aHjUeLQf2RlVJA93OcpAFyqUicAI2KHNK3dUiaZqqcSJiaw/640?wx_fmt=png"></p><p><br></p><p><br></p><p><span>交流探讨</span></p><p cid="n22" mdtype="heading"><span>如果您阅读此文有所疑惑,或有不同见解,亦或其他问题,欢迎添加我的微信探讨。我工作的公司2016年开始单细胞测序服务,至今已完成近万例样本的单细胞测序,服务质量经过了10X Genomics公司的权威认证。欢迎大家联系Kinesin洽谈单细胞测序及数据分析业务!</span></p><p cid="n22" mdtype="heading"><br></p><p><img data-cropselx1="0" data-cropselx2="578" data-cropsely1="0" data-cropsely2="200" data-ratio="0.3157262905162065" data-s="300,640" data-src="https://mmbiz.qpic.cn/mmbiz_png/VTXDic6Eia0AE6434HBqdqv7YmtjwOUcB2dc6dY8hNvAExTBrmNFFOHzG88eUtXwEvVIibzQbibfxfczawALicJfS4w/640?wx_fmt=png" data-type="png" data-w="833" src="https://mmbiz.qpic.cn/mmbiz_png/VTXDic6Eia0AE6434HBqdqv7YmtjwOUcB2dc6dY8hNvAExTBrmNFFOHzG88eUtXwEvVIibzQbibfxfczawALicJfS4w/640?wx_fmt=png"></p><p><br></p></div>
<hr>
<a href="https://mp.weixin.qq.com/s/edxbRAyHXL-_dLv5PC9qQw",target="_blank" rel="noopener noreferrer">原文链接</a>

0 comments on commit 5d53e91

Please sign in to comment.