rm(list = ls())
#install.packages("ggplot2", repos = 'https://mirror.lzu.edu.cn/CRAN/')
library(limma)
library(ggplot2)
projectPath = "E:\\201906GBM"
data4analysisPath = paste(projectPath, "DataForAnalysis", sep = "/")
deaPath = paste(projectPath, "DEA", sep = "/")
if(!dir.exists(deaPath)) dir.create(deaPath) # 新建一个DEA文件夹,存放差异表达分析结果
datasets = c("GSE18842", "GSE19804", "GSE43458", "GSE62113")
for (dataset in datasets){
setwd(data4analysisPath)
dataset = "GSE15824"
#GenesExp = read.table(paste(dataset, "_GenesExpData.txt", sep = ""), header = T, row.names = 1, sep = "\t", quote = "")
# 对每组数据,提取样本分组信息
if(dataset == "GSE108474"){ # 28个Normal, 221个Tumor
SampleInfo = read.table(paste(dataset, "_SampleInfo.txt", sep = ""), header = T, row.names = 1, sep = "\t", quote = "")
SampleInfo = SampleInfo[-c(543:550),]
group_list = as.character( SampleInfo[, 11] )
load("GSE108474_exprSet.Rdata")
exprSet <- GSE108474_exprSet[,-c(543:550)]
n_expr = exprSet[ , grep( "normal", group_list )]
g_expr = exprSet[ , grep( "glioblastoma", group_list )]
exprSet = cbind( n_expr, g_expr )
group_list = c(rep( 'Normal', ncol( n_expr ) ),
rep( 'Tumor', ncol( g_expr ) ) )
table(group_list)
}
if(dataset == "GSE4290"){ # 23个Normal, 77个Tumor
SampleInfo = read.table(paste(dataset, "_SampleInfo.txt", sep = ""), header = T, row.names = 1, sep = "\t", quote = "")
group_list = as.character( SampleInfo[, 35] )
load("exprSet_GSE4290.Rdata")
exprSet <- exprSet_GSE4290
n_expr = exprSet[ , grep( "non-tumor", group_list )]
g_expr = exprSet[ , grep( "glioblastoma", group_list )]
exprSet = cbind( n_expr, g_expr )
group_list = c(rep( 'Normal', ncol( n_expr ) ),
rep( 'Tumor', ncol( g_expr ) ) )
table(group_list)
}
if(dataset == "GSE15824"){ # 5个Normal, 12个Tumor
SampleInfo = read.table(paste(dataset, "_SampleInfo.txt", sep = ""), header = T, row.names = 1, sep = "\t", quote = "")
group_list = as.character( SampleInfo[, 1] )
load("exprSet_GSE15824.Rdata")
exprSet <- exprSet_GSE15824
ex <- exprSet
qx <- as.numeric(quantile(ex, c(0., 0.25, 0.5, 0.75, 0.99, 1.0), na.rm=T))
LogC <- (qx[5] > 100) ||
(qx[6]-qx[1] > 50 && qx[2] > 0) ||
(qx[2] > 0 && qx[2] < 1 && qx[4] > 1 && qx[4] < 2)
if (LogC) { ex[which(ex <= 0)] <- NaN
exprSet <- log2(ex)
print("log2 transform finished")}else{print("log2 transform not needed")}
n_expr = exprSet[ , grep( "normal", group_list )]
g_expr = exprSet[ , grep( "Glioblastoma", group_list )]
exprSet = cbind( n_expr, g_expr )
group_list = c(rep( 'Normal', ncol( n_expr ) ),
rep( 'Tumor', ncol( g_expr ) ) )
table(group_list)
}
if(dataset == "GSE50161"){ # 13个Normal, 34个Tumor
SampleInfo = read.csv("GSE50161_SampleInfo.csv")
group_list = as.character( SampleInfo[, 9] )
load("exprSet_GSE50161.Rdata")
exprSet <- exprSet_GSE50161
n_expr = exprSet[ , grep( "normal", group_list )]
g_expr = exprSet[ , grep( "glioblastoma", group_list )]
exprSet = cbind( n_expr, g_expr )
group_list = c(rep( 'Normal', ncol( n_expr ) ),
rep( 'Tumor', ncol( g_expr ) ) )
table(group_list)
}
if(dataset == "GSE116520"){ # 8个Normal, 17个Tumor
SampleInfo = read.table(paste(dataset, "_SampleInfo.txt", sep = ""), header = T, row.names = 1, sep = "\t", quote = "")
group_list = as.character( SampleInfo[, 1] )
load("GSE116520_exprSet.Rdata")
exprSet <- GSE116520_exprSet
n_expr = exprSet[ , grep( "Control", group_list )]
g_expr = exprSet[ , grep( "Tumour", group_list )]
exprSet = cbind( n_expr, g_expr )
group_list = c(rep( 'Normal', ncol( n_expr ) ),
rep( 'Tumor', ncol( g_expr ) ) )
table(group_list)
}
if(dataset == "GSE90598"){ # 7个Normal, 16个Tumor
SampleInfo = read.table(paste(dataset, "_SampleInfo.txt", sep = ""), header = T, row.names = 1, sep = "\t", quote = "")
group_list = as.character( SampleInfo[, 1] )
load("GSE90598_exprSet.Rdata")
exprSet <- GSE90598_exprSet
n_expr = exprSet[ , grep( "Healthy", group_list )]
g_expr = exprSet[ , grep( "Glioblastoma", group_list )]
exprSet = cbind( n_expr, g_expr )
group_list = c(rep( 'Normal', ncol( n_expr ) ),
rep( 'Tumor', ncol( g_expr ) ) )
table(group_list)
}
DataForDEA = exprSet
SG = factor(group_list)
# limma 差异表达分析
if(dataset == "GSE19804"){ # 配对的
SI = factor(c(1:9,1:9)) # SampleInfo$title
design = model.matrix(~SI+SG)
fit = lmFit(DataForDEA, design)
fit2 = eBayes(fit)
result = topTable(fit2, coef = "SGTumor", number = nrow(DataForDEA)) # 默认number只输出10个
}else{ # 非配对的
design = model.matrix(~0+SG)
colnames(design) = levels(SG)
contrast.matrix = makeContrasts(contrasts = "Tumor-Normal", levels = design)
fit = lmFit(DataForDEA, design)
fit2 = contrasts.fit(fit, contrast.matrix)
fit2 = eBayes(fit2)
result = topTable(fit2, coef = 1, number = nrow(DataForDEA)) # 默认number只输出10个
}
sig_result = subset(result, abs(logFC) >= 1 & adj.P.Val < 0.05)
# 输出结果
setwd(deaPath)
write.csv(result[,c(1,2,4,5)], paste(dataset, "_all_result.csv", sep = ""))
write.csv(sig_result[,c(1,2,4,5)], paste(dataset, "_sig_result.csv", sep = ""))
#### vacano plot
va_info = result[, c("logFC", "adj.P.Val")]
va_info$Significant = 0
for(i in 1:nrow(va_info)){
va_info[is.na(va_info)] = 0
if(va_info[i, 1] >= 1 & va_info[i, 2] < 0.05){
va_info[i, 3] = "Up"
}else if(va_info[i, 1] <= (-1) & va_info[i, 2] < 0.05){
va_info[i, 3] = "Down"
}else{
va_info[i, 3] = "Normal"
}
}
max_y = max(abs(c(max(va_info$logFC), min(va_info$logFC))))+1
va_info$Significant = factor(va_info$Significant, levels = c("Up", "Normal", "Down"))
# set color http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/
cols = c("Up" = "#ff0000", "Normal" = "gray30", "Down" = "#32cd32")
pdf(paste(dataset, "_vacanoplot.pdf", sep = ""), width = 6, height = 5)
p = ggplot(data = va_info, aes(x = -log10(adj.P.Val), y = logFC, colour = Significant)) + ylim(-max_y, max_y) + scale_colour_manual(values = cols) + geom_point() + geom_hline(yintercept = 0, linetype = "dashed", color = "gray20", size = 1) + theme_bw() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + theme(legend.box.margin = margin(0,0,0,-10)) # + theme(legend.position="none") # 删除所有legend
print(p)
dev.off()
}