bulk RNAseq | (3,4)差異表達(dá),富集分析

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
輸出差異分析結(jié)果列表、火山圖辩蛋、基于差異表達(dá)基因的富集分析結(jié)果(GO/KEGG/ReactomePA)列表,適合粗略地先看下結(jié)果呻畸。

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的基因做富集分析悼院,如需調(diào)整參數(shù)伤为,可修改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數(shù)據(jù)
  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數(shù)目過少,沒有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: 現(xiàn)設(shè)置為mouse绞愚,換物種需修改參數(shù) 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)
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市颖医,隨后出現(xiàn)的幾起案子位衩,更是在濱河造成了極大的恐慌,老刑警劉巖便脊,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蚂四,死亡現(xiàn)場離奇詭異光戈,居然都是意外死亡,警方通過查閱死者的電腦和手機遂赠,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進店門久妆,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人跷睦,你說我怎么就攤上這事筷弦。” “怎么了抑诸?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵烂琴,是天一觀的道長。 經(jīng)常有香客問我蜕乡,道長奸绷,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任层玲,我火速辦了婚禮号醉,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘辛块。我一直安慰自己畔派,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布润绵。 她就那樣靜靜地躺著线椰,像睡著了一般。 火紅的嫁衣襯著肌膚如雪尘盼。 梳的紋絲不亂的頭發(fā)上憨愉,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天,我揣著相機與錄音卿捎,去河邊找鬼莱衩。 笑死,一個胖子當(dāng)著我的面吹牛娇澎,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播睹晒,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼趟庄,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了伪很?” 一聲冷哼從身側(cè)響起戚啥,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎锉试,沒想到半個月后猫十,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年拖云,在試婚紗的時候發(fā)現(xiàn)自己被綠了贷笛。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,690評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡宙项,死狀恐怖乏苦,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情尤筐,我是刑警寧澤汇荐,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站盆繁,受9級特大地震影響掀淘,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜油昂,卻給世界環(huán)境...
    茶點故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一革娄、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧秕狰,春花似錦稠腊、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至我衬,卻和暖如春叹放,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背挠羔。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工井仰, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人破加。 一個月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓俱恶,卻偏偏與公主長得像,于是被迫代替她去往敵國和親范舀。 傳聞我的和親對象是個殘疾皇子合是,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,577評論 2 353

推薦閱讀更多精彩內(nèi)容