主要方法:如果不同分组代表着一定的趋势,例如group1,group2,group3的样本严重程度越来越重。那么就可以求group1和group2的差异基因,group2和group3的差异基因,group1和group3的差异基因,最后把三次得到的上调差异基因和下调差异基因求交集。
第一步读取数据,合并表达矩阵和分组文件
#===========================================================================
#===========================================================================
rm(list = ls(all.names = TRUE))
options(stringsAsFactors = F)
library(Matrix)
setwd('D:\\SCIwork\\F38KRT\\s2')
data <- read.csv('cdata.csv', header = T, row.names = 1)
data <- as.data.frame(t(data))
data[1:4,1:4]
normalized<-function(y) {
x<-y[!is.na(y)]
x<-(x - min(x)) / (max(x) - min(x))
y[!is.na(y)]<-x
return(y)
}
db <- as.data.frame(apply(data,2,normalized))
data <- db
data$sample <- rownames(data)
data$sample <- chartr(old='.', new='-', x=data$sample)
setwd('D:\\SCIwork\\F38KRT\\s3')
group <- read.csv('group2.csv', header = T)
names(group)[1] <- 'sample'
group$sample <- chartr(old='.', new='-', x=group$sample)
group <- subset(group, select=c("sample", "group"))
group$subtype <- group$group
group$group <- NULL
dt <- merge(group, data, by='sample')
dt[1:4,1:4]
dt$sample <- NULL
table(dt$subtype)
dt_total <- dt
normalized<-function(y) {
x<-y[!is.na(y)]
x<-(x - min(x)) / (max(x) - min(x))
y[!is.na(y)]<-x
return(y)}
求group1和group2的差异基因
#===========================================================================
#===========================================================================
table(dt$subtype)
dt <- dt[order(dt$subtype), ]
dt[1:4,1:4]
dt_Con <- subset(dt, dt$subtype == 'Subtype1')
dt_Con[1:4,1:4]
dt_Exp <- subset(dt, dt$subtype == 'Subtype2')
dt_Exp[1:4,1:4]
dt_Con$subtype <- paste0(dt_Con$subtype, rownames(dt_Con))
rownames(dt_Con) <- dt_Con$subtype
dt_Con$subtype <- NULL
dt_Con <- as.data.frame(t(dt_Con))
dt_Exp$subtype <- paste0(dt_Exp$subtype, rownames(dt_Exp))
rownames(dt_Exp) <- dt_Exp$subtype
dt_Exp$subtype <- NULL
dt_Exp <- as.data.frame(t(dt_Exp))
Pvalue<-c(rep(0,nrow(dt_Con)))
log2_FC<-c(rep(0,nrow(dt_Con)))
for(i in 1:nrow(dt_Con)){
y=t.test(as.numeric(dt_Con[i,]),as.numeric(dt_Exp[i,]))
Pvalue[i] <- y$p.value
log2_FC[i] <-log2(mean(as.numeric(dt_Exp[i,]))/(mean(as.numeric(dt_Con[i,]))))
}
library(dplyr)
library(tidyr)
library(tibble)
# 对p value进行FDR校正
fdr=p.adjust(Pvalue, "BH")
# 在原文件后面加入log2FC,p value和FDR,共3列;
out<- as.data.frame(cbind(log2_FC,Pvalue,fdr))
out$gene <- rownames(dt_Con)
# out <- out %>%
# dplyr::filter(log2_FC > 0.5 & Pvalue < 0.05)
setwd('D:\\SCIwork\\F38KRT\\s5')
write.csv(out, file = 'out_S1.csv')
求group2和group3的差异基因
#===========================================================================
#===========================================================================
table(dt$subtype)
dt <- dt[order(dt$subtype), ]
dt[1:4,1:4]
dt_Con <- subset(dt, dt$subtype == 'Subtype2')
dt_Con[1:4,1:4]
dt_Exp <- subset(dt, dt$subtype == 'Subtype3')
dt_Exp[1:4,1:4]
dt_Con$subtype <- paste0(dt_Con$subtype, rownames(dt_Con))
rownames(dt_Con) <- dt_Con$subtype
dt_Con$subtype <- NULL
dt_Con <- as.data.frame(t(dt_Con))
dt_Exp$subtype <- paste0(dt_Exp$subtype, rownames(dt_Exp))
rownames(dt_Exp) <- dt_Exp$subtype
dt_Exp$subtype <- NULL
dt_Exp <- as.data.frame(t(dt_Exp))
Pvalue<-c(rep(0,nrow(dt_Con)))
log2_FC<-c(rep(0,nrow(dt_Con)))
for(i in 1:nrow(dt_Con)){
y=t.test(as.numeric(dt_Con[i,]),as.numeric(dt_Exp[i,]))
Pvalue[i] <- y$p.value
log2_FC[i] <-log2(mean(as.numeric(dt_Exp[i,]))/(mean(as.numeric(dt_Con[i,]))))
}
library(dplyr)
library(tidyr)
library(tibble)
# 对p value进行FDR校正
fdr=p.adjust(Pvalue, "BH")
# 在原文件后面加入log2FC,p value和FDR,共3列;
out<- as.data.frame(cbind(log2_FC,Pvalue,fdr))
out$gene <- rownames(dt_Con)
# out <- out %>%
# dplyr::filter(log2_FC > 0.5 & Pvalue < 0.05)
setwd('D:\\SCIwork\\F38KRT\\s5')
write.csv(out, file = 'out_S2.csv')
求group1和group3的差异基因
#===========================================================================
#===========================================================================
table(dt$subtype)
dt <- dt[order(dt$subtype), ]
dt[1:4,1:4]
dt_Con <- subset(dt, dt$subtype == 'Subtype1')
dt_Con[1:4,1:4]
dt_Exp <- subset(dt, dt$subtype == 'Subtype3')
dt_Exp[1:4,1:4]
dt_Con$subtype <- paste0(dt_Con$subtype, rownames(dt_Con))
rownames(dt_Con) <- dt_Con$subtype
dt_Con$subtype <- NULL
dt_Con <- as.data.frame(t(dt_Con))
dt_Exp$subtype <- paste0(dt_Exp$subtype, rownames(dt_Exp))
rownames(dt_Exp) <- dt_Exp$subtype
dt_Exp$subtype <- NULL
dt_Exp <- as.data.frame(t(dt_Exp))
Pvalue<-c(rep(0,nrow(dt_Con)))
log2_FC<-c(rep(0,nrow(dt_Con)))
for(i in 1:nrow(dt_Con)){
y=t.test(as.numeric(dt_Con[i,]),as.numeric(dt_Exp[i,]))
Pvalue[i] <- y$p.value
log2_FC[i] <-log2(mean(as.numeric(dt_Exp[i,]))/(mean(as.numeric(dt_Con[i,]))))
}
library(dplyr)
library(tidyr)
library(tibble)
# 对p value进行FDR校正
fdr=p.adjust(Pvalue, "BH")
# 在原文件后面加入log2FC,p value和FDR,共3列;
out<- as.data.frame(cbind(log2_FC,Pvalue,fdr))
out$gene <- rownames(dt_Con)
# out <- out %>%
# dplyr::filter(log2_FC > 0.5 & Pvalue < 0.05)
setwd('D:\\SCIwork\\F38KRT\\s5')
write.csv(out, file = 'out_S3.csv')
取交集
#===========================================================================
#===========================================================================
diff1 <- read.csv('out_S1.csv', header = T, row.names = 1)
diff2 <- read.csv('out_S2.csv', header = T, row.names = 1)
diff3 <- read.csv('out_S3.csv', header = T, row.names = 1)
diff1on <- subset(diff1, diff1$log2_FC > 0.2)
diff2on <- subset(diff2, diff2$log2_FC > 0.2)
diff3on <- subset(diff3, diff3$log2_FC > 0.2)
up_gene <- intersect(diff1on$gene, diff2on$gene)
up_gene <- intersect(up_gene, diff3on$gene)
diff1down <- subset(diff1, diff1$log2_FC < -0.2)
diff2down <- subset(diff2, diff2$log2_FC < -0.2)
diff3down <- subset(diff3, diff3$log2_FC < -0.2)
down_gene <- intersect(diff1down$gene, diff2down$gene)
down_gene <- intersect(down_gene, diff3down$gene)