前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >多分组差异分析解决方案(2)分批次差异基因后取交集

多分组差异分析解决方案(2)分批次差异基因后取交集

作者头像
用户1359560
发布2021-06-10 16:57:59
2.6K0
发布2021-06-10 16:57:59
举报
文章被收录于专栏:生信小驿站

主要方法:如果不同分组代表着一定的趋势,例如group1,group2,group3的样本严重程度越来越重。那么就可以求group1和group2的差异基因,group2和group3的差异基因,group1和group3的差异基因,最后把三次得到的上调差异基因和下调差异基因求交集。

第一步读取数据,合并表达矩阵和分组文件

代码语言:javascript
复制
#===========================================================================


#===========================================================================


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的差异基因

代码语言:javascript
复制
#===========================================================================


#===========================================================================


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的差异基因

代码语言:javascript
复制
#===========================================================================


#===========================================================================


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的差异基因

代码语言:javascript
复制
#===========================================================================


#===========================================================================


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')

取交集

代码语言:javascript
复制
#===========================================================================


#===========================================================================


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)
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档