TCGA数据挖掘
步骤1:安装R包
###00_package_install.R
options("repos" = c(CRAN="http://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
if(!require("BiocManager")) install.packages("BiocManager",update = F,ask = F)
options(BioC_mirror="http://mirrors.tuna.tsinghua.edu.cn/bioconductor/")
cran_packages <- c('tidyr',
'tibble',
'dplyr',
'stringr',
'ggplot2',
'ggpubr',
'factoextra',
'FactoMineR',
'pheatmap',
"survival",
"survminer",
"patchwork",
"ggstatsplot",
"ggplotify",
"cowplot",
"glmnet",
"ROCR",
"caret",
"randomForest",
"survminer",
"Hmisc",
"e1071",
"deconstructSigs",
"timeROC")
Biocductor_packages <- c("KEGG.db",
"limma",
"clusterProfiler",
"org.Hs.eg.db",
"SummarizedExperiment",
"DESeq2",
"edgeR",
"ggpubr",
"rtracklayer",
"genefilter",
"maftools",
"ComplexHeatmap",
"GDCRNATools")
for (pkg in cran_packages){
if (! require(pkg,character.only=T) ) {
install.packages(pkg,ask = F,update = F)
require(pkg,character.only=T)
}
}
# use BiocManager to install
for (pkg in Biocductor_packages){
if (! require(pkg,character.only=T) ) {
BiocManager::install(pkg,ask = F,update = F)
require(pkg,character.only=T)
}
}
#前面的报错都先不要管。主要看这里
for (pkg in c(Biocductor_packages,cran_packages)){
require(pkg,character.only=T)
}
#哪个报错,就回去安装哪个。如果你没有安装xx包,却提示你xx包不存在,这也正常,是因为复杂的依赖关系,缺啥补啥。
if(!require(AnnoProbe))devtools::install_local("AnnoProbe-master.zip",upgrade = F,dependencies = T)
if(!require(tinyarray))devtools::install_local("tinyarray-master.zip",upgrade = F,dependencies = T)
library(AnnoProbe)
library(tinyarray)
接下来,进行分析
关于数据种类,有count类型,肯定选count,次选才是fpkm等其它。
当拿不到count数据时,可以采用其它的。如下
具体下载数据,参考之前的文章。
-
步骤2:数据整理和差异分析
1)GEO芯片的差异分析,只能用limma分析;而转录组可以用多个,limma、RNAseq和edgeR.
2)芯片的limma和转录组的limma是不同的。
为避免算法上的失误,大家可以用三个方法找到差异基因后取交集,这是最为稳妥的。
#### 三大R包差异分析
#加载R包
if(!require(stringr))install.packages('stringr')
if(!require(ggplotify))install.packages("ggplotify")
if(!require(patchwork))install.packages("patchwork")#拼图
if(!require(cowplot))install.packages("cowplot")
#三个差异分析R包
if(!require(DESeq2))BiocManager::install('DESeq2')
if(!require(edgeR))BiocManager::install('edgeR')
if(!require(limma))BiocManager::install('limma')
#
rm(list = ls())
##加载数据:包含表达数据exp和分组group
load("TCGA-CHOL_gdc.Rdata")
table(Group)
proj="TCGA-CHOL"
#################################################################################################
第一种 deseq2----
library(DESeq2)
#建立一个数据框,两列:样本名和分组信息
colData <- data.frame(row.names =colnames(exp),
condition=Group)
#打包数据
if(!file.exists(paste0(proj,"_dd.Rdata"))){
dds <- DESeqDataSetFromMatrix(#生存edseq要求的数据类型,里面包含的数据如下
countData = exp,
colData = colData,
design = ~ condition)
dds <- DESeq(dds)
save(dds,file = paste0(proj,"_dd.Rdata"))
}
load(file = paste0(proj,"_dd.Rdata"))
# 两两比较
res <- results(dds, contrast = c("condition",rev(levels(Group))))
#注意最后的顺序
#condition是列名,rev倒序排列
#> levels(Group)
#[1] "normal" "tumor"
#> rev(levels(Group))
#[1] "tumor" "normal"
resOrdered <- res[order(res$pvalue),] # 按照P值排序
#转换成数据框,有log2foldchange 和 pvalue两列我们需要的信息
DEG <- as.data.frame(resOrdered)
#知识点:sort(x)=x[order(x),] #order(x)是得出排序的下标
DEG = na.omit(DEG)#过滤
head(DEG)
#添加change列标记基因上调下调
#另外,我们也可以将阈值直接定为logFC_cutoff =1或者1.5或者2
#这里用的是95%置信区间的值
logFC_cutoff <- with(DEG,mean(abs(log2FoldChange)) + 2*sd(abs(log2FoldChange)) )
#知识点with函数的用法,with(data, ...)后面不需要data$,可以直接写上子集的名字
#logFC_cutoff <- 2
k1 = (DEG$pvalue < 0.05)&(DEG$log2FoldChange < -logFC_cutoff)
k2 = (DEG$pvalue < 0.05)&(DEG$log2FoldChange > logFC_cutoff)
DEG$change = ifelse(k1,"DOWN",ifelse(k2,"UP","NOT"))
#table下看看差异基因的情况
table(DEG$change)
head(DEG)
#下面的代码还需要用DEG这个名字,这里赋值改名字。
DESeq2_DEG <- DEG
第二种 edgeR----
library(edgeR)
dge <- DGEList(counts=exp,group=Group)
dge$samples$lib.size <- colSums(dge$counts)
dge <- calcNormFactors(dge)
design <- model.matrix(~0+Group)
rownames(design)<-colnames(dge)
colnames(design)<-levels(Group)
dge <- estimateGLMCommonDisp(dge, design)
dge <- estimateGLMTrendedDisp(dge, design)
dge <- estimateGLMTagwiseDisp(dge, design)
fit <- glmFit(dge, design)
#-1为对照组,1为实验组
fit2 <- glmLRT(fit, contrast=c(-1,1))
#得到DEG表格
DEG=topTags(fit2, n=nrow(exp))
DEG=as.data.frame(DEG)
logFC_cutoff <- with(DEG,mean(abs(logFC)) + 2*sd(abs(logFC)) )
#logFC_cutoff <- 2
k1 = (DEG$PValue < 0.05)&(DEG$logFC < -logFC_cutoff)
k2 = (DEG$PValue < 0.05)&(DEG$logFC > logFC_cutoff)
DEG$change = ifelse(k1,"DOWN",ifelse(k2,"UP","NOT"))
head(DEG)
table(DEG$change)
edgeR_DEG <- DEG
第三种 limma----
#注意:不要和芯片中的limma混合在一起
#输入数据:exp 和Group
library(limma)
design <- model.matrix(~0+Group)
colnames(design)=levels(Group)
rownames(design)=colnames(exp)
dge <- DGEList(counts=exp)
dge <- calcNormFactors(dge)
v <- voom(dge,design, normalize="quantile")
fit <- lmFit(v, design)
#constrasts: tumor-normal
constrasts = paste(rev(levels(Group)),collapse = "-")
cont.matrix <- makeContrasts(contrasts=constrasts,levels = design)
fit2=contrasts.fit(fit,cont.matrix)
fit2=eBayes(fit2)
DEG = topTable(fit2, coef=constrasts, n=Inf)
DEG = na.omit(DEG)
logFC_cutoff <- with(DEG,mean(abs(logFC)) + 2*sd(abs(logFC)) )
#logFC_cutoff <- 2
k1 = (DEG$P.Value < 0.05)&(DEG$logFC < -logFC_cutoff)
k2 = (DEG$P.Value < 0.05)&(DEG$logFC > logFC_cutoff)
DEG$change = ifelse(k1,"DOWN",ifelse(k2,"UP","NOT"))
table(DEG$change)
head(DEG)
limma_voom_DEG <- DEG
三种算法的不同差异基因数
tj = data.frame(deseq2 = as.integer(table(DESeq2_DEG$change)),
edgeR = as.integer(table(edgeR_DEG$change)),
limma_voom = as.integer(table(limma_voom_DEG$change)),
row.names = c("down","not","up")
);tj
save(DESeq2_DEG,edgeR_DEG,limma_voom_DEG,Group,tj,file = paste0(proj,"_DEG.Rdata"))
-
步骤3 可视化
rm(list = ls())
load("TCGA-CHOL_gdc.Rdata")
load("TCGA-CHOL_DEG.Rdata")
if(!require(tinyarray))devtools::install_local("tinyarray-master.zip",upgrade = F)
library(ggplot2)
library(tinyarray)
exp[1:4,1:4]
# cpm 去除文库大小的影响,去除每个样本文库大小的影响
dat = log2(cpm(exp)+1)
pca.plot = draw_pca(dat,Group);pca.plot
save(pca.plot,file = paste0(proj,"_pcaplot.Rdata"))
#差异基因,每个算法不同
cg1 = rownames(DESeq2_DEG)[DESeq2_DEG$change !="NOT"]
cg2 = rownames(edgeR_DEG)[edgeR_DEG$change !="NOT"]
cg3 = rownames(limma_voom_DEG)[limma_voom_DEG$change !="NOT"]
#画热图
h1 = draw_heatmap(dat[cg1,],Group,n_cutoff = 2)
h2 = draw_heatmap(dat[cg2,],Group,n_cutoff = 2)
h3 = draw_heatmap(dat[cg3,],Group,n_cutoff = 2)
m2d = function(x){
mean(abs(x))+2*sd(abs(x))
}
v1 = draw_volcano(DESeq2_DEG,pkg = 1,logFC_cutoff = m2d(DESeq2_DEG$log2FoldChange))
v2 = draw_volcano(edgeR_DEG,pkg = 2,logFC_cutoff = m2d(edgeR_DEG$logFC))
v3 = draw_volcano(limma_voom_DEG,pkg = 3,logFC_cutoff = m2d(limma_voom_DEG$logFC))
library(patchwork)
(h1 + h2 + h3) / (v1 + v2 + v3) +plot_layout(guides = 'collect') &theme(legend.position = "none")
ggsave(paste0(proj,"_heat_vo.png"),width = 15,height = 10)
三大R包的对比
rm(list = ls())
load("TCGA-CHOL_gdc.Rdata")
load("TCGA-CHOL_DEG.Rdata")
load("TCGA-CHOL_pcaplot.Rdata")
#挑上调基因
up=function(df){rownames(df)[df$change=="up"]}
#挑下调基因
down=function(df){rownames(df)[df$change=="down"]}
#挑选共同上下调的基因
up=intersect(intersect(up(DESeq2_DEG),up(edgeR_DEG)),up(limma_voom_DEG ))
down=intersect(intersect(down(DESeq2_DEG),down(edgeR_DEG)),down(limma_voom_DEG ))
dat=log(cpm(exp)+1)
hp=draw_heatmap(dat[c(up,down),],Group,n_cutoff=2)
#上下调基因分别画韦恩图
up_genes=list(Deseq2=up(DESeq2_DEG),#Deseq2指分类的名字,下面也一样
edgeR=up(edgeR_DEG),
limma=up(limma_voom_DEG))
down_genes=list(Deseq2=down(DESeq2_DEG),
edgeR=down(edgeR_DEG),
limma=down(limma_voom_DEG))
#画图
up.plot =draw_venn(up_genes,"UPgenes")
down.plot =draw_venn(down_genes,"DOWNgenes")
#维恩图拼图
library(patchwork)
#up.plot+down.plot
pca.plot+hp+up.plot+down.plot+plot_layout(guides="collect")
ggsave(paste0(proj,"_heat_ve_pca.png"),width=15,height=10)