首先使用edgeR算法做一个差异表达分析,这里你可以使用任何一个count矩阵作为数据输入,再加上一个样本分组信息,比如我们前面用的:
去下载:https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE229904
我这里输入的数据是一个随便的具有四个样本的数据,两个case,两个control:
rm(list = ls())
options(stringsAsFactors = F)
# 加载包
library(edgeR)
library(ggplot2)
## 读取 count矩阵
head(count)
# 分组信息
group_list <- c("control","control","treat","treat")
group_list <- factor(group_list,levels = c("treat","control"))
group_list
table(group_list)

输入数据都准备好了,走edgeR算法:
# 构建线性模型。0代表x线性模型的截距为0
design <- model.matrix(~0+group_list)
rownames(design) <- colnames(count)
colnames(design) <- levels(factor(group_list))
design
# 构建edgeR的DGEList对象
DEG <- DGEList(counts=count, group=factor(group_list))
DEG <- calcNormFactors(DEG)
# 计算线性模型的参数
DEG <- estimateGLMCommonDisp(DEG,design)
DEG <- estimateGLMTrendedDisp(DEG, design)
DEG <- estimateGLMTagwiseDisp(DEG, design)
# 拟合线性模型
fit <- glmFit(DEG, design)
# 进行差异分析
lrt <- glmLRT(fit, contrast=c(1,-1)) #实验组在前
# 提取过滤差异分析结果
DEG_edgeR <- as.data.frame(topTags(lrt, n=nrow(DEG),adjust.method = "BH"))
head(DEG_edgeR)
# 筛选上下调,设定阈值
fc_cutoff <- 1.2 # 1.2 1.5 2
pvalue <- 0.05 # 0.05, 0.01, 0.001
DEG_edgeR$regulated <- "normal"#给它一个初始值
DEG_edgeR$regulated[ DEG_edgeR$logFC > log2(fc_cutoff) & DEG_edgeR$PValue < pvalue ] <- "up"
DEG_edgeR$regulated[ DEG_edgeR$logFC < -log2(fc_cutoff) & DEG_edgeR$PValue < pvalue ] <- "down"
table(DEG_edgeR$regulated)
## 添加一列gene symbol
library(org.Hs.eg.db)
library(clusterProfiler)
id2symbol <- bitr(rownames(DEG_edgeR), fromType = "ENSEMBL", toType = "SYMBOL", OrgDb = org.Hs.eg.db)
head(id2symbol)
DEG_edgeR <- cbind(GeneID=rownames(DEG_edgeR),DEG_edgeR)
DEG_edgeR_symbol <- merge(id2symbol,DEG_edgeR, by.x="ENSEMBL",by.y="GeneID",all.y=T)
head(DEG_edgeR_symbol)
差异结果如下:

这里用的上课的方法,使用简单的cpm进行的标准化值来进行同一个基因在不同样本间的比较:
##====== 检查是否上下调设置错了
# 挑选一个基因看其上下调方向
# ENSG00000037474 NSUN2
library(ggplot2)
cpm <- cpm(count) # count标准化
head(cpm)
exp <- c(t(cpm[match("ENSG00000037474",rownames(cpm)),]))
test <- data.frame(value=exp, group=group_list)
test
ggplot(data=test,aes(x=group,y=value,fill=group)) +
geom_boxplot() +
ggtitle("ENSG00000037474 NSUN2")
# 在差异结果中的方向
DEG_edgeR_symbol[DEG_edgeR_symbol$ENSEMBL=="ENSG00000037474", ]
可以看到 cpm 值显示的是:相对对照组,在treat里面上调,但是差异结果里面的log2FC<0,却是下调?

为什么呢?
其实主要在于edger使用的标准化方法为TMM算法,而我们在绘制箱线图的时候却用的 cpm 标准化,我后面试了 tmp 算法标准化,也会出现与 edger 不一致的情况,参考:count转TPM/FPKM实战(GSE229904)
edger 算法标准化 TMM 后的表达矩阵提取:
library(edgeR)
y <- DGEList(counts = count)
y <- calcNormFactors(y)
counts.tmm.edger <- round(cpm(y), 4)
head(counts.tmm.edger)
head(counts.tmm.edger["ENSG00000037474",c("control-1","control-1","treat-1","treat-2")])
# 对照组
mean(counts.tmm.edger["ENSG00000037474",c("control-1","control-1")])
# [1] 50.2231
# 实验组
mean(counts.tmm.edger["ENSG00000037474",c("treat-1","treat-2")])
# [1] 48.92365
这个时候要是想展示关键基因的表达值箱线图,可以选择算法内部的标准化方法~
今天分享到这。