bulk RNAseq | (3,4)差异表达,富集分析

pipeline: QC >>> alignment >>> differential expression analysis >>> enrichment analysis

primary softs or packages involved in the pipeline

  1. QC (linux)
  • fastqc: quality assessment
  • cutadapt: remove Illumina Universal Adaptor or overrepresentative sequences
  • fastx_trimmer: trim the low-quality bases (e.g the first 10bp)
  • multiqc: intergrate results of fastqc
  1. alignment (linux)
  • STAR: faster, ~90 minutes for each sample
  • bwa
  1. Differential expression analysis (R packages)
  • HTseq: calculate raw read counts for each transcript or gene
  • DESeq2
  • limma
  1. enrichment analysis
  • clusterProfiler
  1. plot
  • ggplot2
  • Cairo
  • pheatmap
  • RColorBrewer

main codes
input: raw counts
output: differential expression analysis and enrichment analysis based on DEGs
输出差异分析结果列表、火山图、基于差异表达基因的富集分析结果(GO/KEGG/ReactomePA)列表,适合粗略地先看下结果。

Volcano plot

#载入R包
require("DESeq2")
require("limma")
require("pasilla")
require("vsn")
require("ggplot2")
require("Cairo")
require("pheatmap")
require("RColorBrewer")
require(DOSE)
require(org.Mm.eg.db)
require(clusterProfiler)
require(stringr)
require(ReactomePA)
source('deg_function.r') #cal DEGs
source('enrich_function.r') #enrichment analysis

#读入raw count
d<-read.table("combined.count",row.names=1,header = T)
head(d,2)
#                    A1  A2  A3  B1  B2  B3  C1  C2  C3  D1  D2  D3  
#ENSMUSG00000000001 615 645 531 767 684 686 751 778 929 767 680 785
#ENSMUSG00000000003 0   0   0   0   0   0   0   0   0   0   0   0

tail(d)
#                    A1  A2  A3  B1  B2  B3  C1  C2  C3  D1  D2  D3  
#ENSMUSG00000116528 0   0   0   0   0   0   0   0   0   0   0   0
#__no_feature   2509484 2534451 1672031 2405219 2323971 2398776 2361692 2600649 2961624 2412725 2331754 2562243
#__ambiguous    1198166 1210490 1057190 1315088 1268587 1281891 1225386 1328553 1443758 1292656 1255907 1234649
#__too_low_aQual    0   0   0   0   0   0   0   0   0   0   0   0
#__not_aligned  0   0   0   0   0   0   0   0   0   0   0   0
#__alignment_not_unique 796927  831108  756030  936088  901476  884335  862996  827094  976800  856331  789099  848550

dat<-d[-c(53801:53806),] #删除最后几行信息

#ID convert:ENSEMBL to SYMBOL
ee=bitr(rownames(dat),fromType = "ENSEMBL",toType = "SYMBOL", OrgDb="org.Mm.eg.db")
write.csv(ee,file="ensembl_symbol.csv",quote=F)

#set groups
a=c(1:3);b=c(4:6);c=c(7:9);d=c(10:12);
caselist=list(a,b,c,d) 
namelist=c("A","B","C","D") 

#all samples included: is there any outlier?
a=c(rep("case",9),rep("ctrl",3)) #前9列为case,最后3列为ctrl
name="all"
dd=dat
deg_function(a,dd,name)

#comparison among groups: A vs. B/C/D; B vs C/D; C vs D (the first group serves as controls)
for (i in 1:3) {for(j in (i+1):4){
    a=c(rep("ctrl",3),rep("case",3))
    name=paste(namelist[i],namelist[j],sep="_")
    dd=dat[,c(caselist[[i]],caselist[[j]])]
    deg_function(a,dd,name)    
    } 
}

deg_function.r: 这里仅取p adj<0.05 & abs(log2FC)>1的基因做富集分析,如需调整参数,可修改updown

deg_function <- function(a,dd,name){
  condition=factor(a)
  coldata<-data.frame(condition)
  rownames(coldata)<-colnames(dd)
  ds<-DESeqDataSetFromMatrix(countData=dd,colData=coldata,design = ~ condition)
  #pre-filtering
  keep <- rowSums(counts(ds))>=10
  ds <- ds[keep,]
  nrow(ds) 
  ds$condition <- relevel(ds$condition,ref="ctrl") #指定对照组
  #normalization后计算样本间的距离:sampe vs. sample
  rld <- rlog(ds, blind=FALSE)
  sampleDists <- dist(t(assay(rld)))
  sampleDistMatrix <- as.matrix(sampleDists)
  rownames(sampleDistMatrix) <- colnames(rld)
  colors <- colorRampPalette( rev(brewer.pal(9, "Blues")) )(255)
  #样本间相似性图
  CairoPNG(paste(name,'distance.png',sep=""))
  pheatmap(sampleDistMatrix,
           clustering_distance_rows=sampleDists,
           clustering_distance_cols=sampleDists,
           col=colors)
  dev.off()
  
  #输出归一化后的count数据
  dds <- estimateSizeFactors(ds)
  norcounts <- counts(dds, normalized=T)
  write.csv(norcounts,file=paste(name,"norm_counts.csv",sep=""),quote=F)
  #DEG
  des <- DESeq(ds)
  res <- results(des)
  resOrdered <- res[order(res$padj),]
  resOrdered_out=cbind(resOrdered@rownames,as.data.frame(resOrdered))
  colnames(resOrdered_out)[1]=colnames(ee)[1]
  out=merge(resOrdered_out,ee,by="ENSEMBL",all.x=TRUE)  
  write.csv(out,file=paste(name,"_DEG.csv",sep=""),quote = F)  
  #出火山图
  CairoPNG(paste(name,'Volcano.png',sep=""))
  with(resOrdered, plot(resOrdered$log2FoldChange, -log10(resOrdered$padj), pch=1, main="Volcano plot",xlab="log2FC",ylab="-log10(padj)"))
  with(subset(resOrdered, padj<0.05 ), points(log2FoldChange, -log10(padj), pch=1, col="red"))
  with(subset(resOrdered, abs(log2FoldChange)>1.5), points(log2FoldChange, -log10(padj), pch=1, col="orange"))
  with(subset(resOrdered, padj<0.05 & abs(log2FoldChange)>1.5), points(log2FoldChange, -log10(padj), pch=1, col="green"))
  dev.off()
  
  #enrichment analysis
  up=subset(resOrdered,padj<0.05 & log2FoldChange>1)
  x1=rownames(up);y1=paste(name,"up",sep="_")
  if(length(x1)>5){enrich_function(x1,y1)}  #DEG数目过少,没有mapped ENTRE ID的话,会报错中断
  down=subset(resOrdered,padj<0.05 & log2FoldChange < -1)
  x2=rownames(down);y2=paste(name,"down",sep="_")
  if(length(x2)>5){enrich_function(x2,y2)}
}

enrich_function.r: 现设置为mouse,换物种需修改参数 OrgDb

enrich_function<-function(x,y){
  
  eg = bitr(x, fromType="ENSEMBL", toType="ENTREZID", OrgDb="org.Mm.eg.db")
  gene = eg$ENTREZID
  
  BP <- enrichGO(gene, "org.Mm.eg.db", keyType = "ENTREZID",ont = "BP",pvalueCutoff  = 0.05,pAdjustMethod = "BH",qvalueCutoff  = 0.1, readable=T)
  MF <- enrichGO(gene, "org.Mm.eg.db", keyType = "ENTREZID",ont = "MF",pvalueCutoff  = 0.05,pAdjustMethod = "BH",qvalueCutoff  = 0.1, readable=T)
  CC <- enrichGO(gene, "org.Mm.eg.db", keyType = "ENTREZID",ont = "CC",pvalueCutoff  = 0.05,pAdjustMethod = "BH",qvalueCutoff  = 0.1, readable=T)
  BP_simp <- simplify(BP, cutoff=0.1,by="p.adjust",select_fun=min)
  MF_simp <- simplify(MF, cutoff=0.1,by="p.adjust",select_fun=min)
  CC_simp <- simplify(CC, cutoff=0.1,by="p.adjust",select_fun=min)
  bp=cbind(rep("BP",times=nrow(as.data.frame(BP_simp@result))),as.data.frame(BP_simp@result))
  mf=cbind(rep("MF",times=nrow(as.data.frame(MF_simp@result))),as.data.frame(MF_simp@result))
  cc=cbind(rep("CC",times=nrow(as.data.frame(CC_simp@result))),as.data.frame(CC_simp@result))
  colnames(cc)=colnames(bp)
  colnames(mf)=colnames(bp)
  write.csv(rbind(bp,mf,cc),file=paste(y,"GO.csv",sep="_"),quote=F)
  
  kk <- enrichKEGG(gene = gene,
                   organism ='mmu',
                   pvalueCutoff = 0.05,
                   qvalueCutoff = 0.1,
                   minGSSize = 1,
                   #readable = TRUE ,
                   use_internal_data =FALSE)
  write.csv(as.data.frame(kk@result), file=paste(y,"kegg.csv",sep="_"),quote=FALSE)
  
  react <- enrichPathway(gene=gene,pvalueCutoff=0.05, readable=T,organism = "mouse")
  write.csv(react,file=paste(y,"ReactomePA.csv",sep="_"),quote=F)
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,793评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,567评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,342评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,825评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,814评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,680评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,033评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,687评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,175评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,668评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,775评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,419评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,020评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,978评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,206评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,092评论 2 351
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,510评论 2 343

推荐阅读更多精彩内容