美文网首页
RNA-seq差异表达分析实践

RNA-seq差异表达分析实践

作者: 医科研 | 来源:发表于2019-01-07 09:43 被阅读29次
####学习下limma, Glimma, edgeR三个包联用处理数据
####2017-09-30 RNA-seq analysis is easy as 1-2-3 with limma, Glimma and edgeR
####edgeR-limma workflow , Glimma for interactive graphics
#### Data source: GSE63310,Illumina HiSeq 2000 (Mus musculus)
####data informaton: raw counts data, 11 sample, only use 3basal, 3LP and 3ML
####NOte: only 9 samples were used to do analysis next

#source( “[http://bioconductor.org/biocLite.R&#8221](http://bioconductor.org/biocLite.R&#8221);)
#options(BioC_mirror=”[http://mirrors.ustc.edu.cn/bioc/&#8221](http://mirrors.ustc.edu.cn/bioc/&#8221);)##使用中科大镜像
# biocLite(“org.Mm.eg.db”,lib=”D:/R/R-3.4.1/library”)
##org.Mm.eg.db-小鼠, org.Mm.eg.db
library(limma)
library(edgeR)
setwd(“D:/R/GSE63310_RAW”)

####1-data import 注意数据需要解压缩
files <- c("GSM1545535_10_6_5_11.txt", "GSM1545536_9_6_5_11.txt",
"GSM1545538_purep53.txt","GSM1545539_JMS8-2.txt",
"GSM1545540_JMS8-3.txt","GSM1545541_JMS8-4.txt",
"GSM1545542_JMS8-5.txt","GSM1545544_JMS9-P7c.txt",
"GSM1545545_JMS9-P8c.txt")
read.delim(files[1], nrow=5)##读取其中的第一个文件查看数据结构
x <- readDGE(files, columns=c(1,3))##clolumns需要注意读取哪些列
class(x)##注意到创建了DGEList对象
dim(x)##查看数据结构

####2-organising sample information
####注意考虑实验变量的影响,包括生物学上的和技术上两方面,例如细胞类型(basal LP ML)
####样本表型(性别,年龄,疾病状态),干预措施,批次效应信息
samplenames <- substring(colnames(x), 12, nchar(colnames(x)))##从12位开始提取,nchar返回向量
samplenames
colnames(x) <- samplenames##将列名改为样本名
##以下为分组信息,注意是有顺序的
group <- as.factor(c("LP", "ML", "Basal", "Basal", "ML", "LP", "Basal", "ML", "LP"))
group##注意因子在没有指定顺序时,首字母排序
x$samples$group <- group
lane <- as.factor(rep(c("L004","L006","L008"), c(3,4,2)))##注意这种设置因子的方法
x$samples$lane <- lane
x$samples
library(Mus.musculus)##鼠基因注释包
geneid <- rownames(x)
genes <- select(Mus.musculus, keys=geneid, columns=c("SYMBOL", "TXCHROM"),
keytype="ENTREZID")
head(genes)##获取了注释信息
dim(genes)
####Note:由于ID与gene的映射好多不是一对一的关系,因此检查重复的基因ID很重要
genes <- genes[!duplicated(genes$ENTREZID),]##移除重复的geneid
dim(genes)##可以看到有100多个重复的ID
x$genes <- genes
####Note: DGEList-objec中有一个genes的数据框用于存储注释信息
x

####3-Data preprocessing
####基因差异表达分析,通常需要对raw counts预处理,消除基因长度等的影响,使其具有可比性
####常见的处理方法有,CPM,logCPM, RPKM, FPKM
cpm <- cpm(x)
lcpm =3##cpm>1区分表达与不表达
x <- x[keep,, keep.lib.sizes=FALSE]
dim(x)##数据量已减少到约一半
##绘制raw data 与 filtered data的分布情况
##################################################################
library(RColorBrewer)
nsamples <- ncol(x)
col <- brewer.pal(nsamples, "Paired")##选择颜色集
par(mfrow=c(1,2))
plot(density(lcpm[,1]), col=col[1], lwd=2, ylim=c(0,0.21), las=2,
main="", xlab="")
title(main="A. Raw data", xlab="Log-cpm")
abline(v=0, lty=3)
for (i in 2:nsamples){
den <- density(lcpm[,i])
lines(den$x, den$y, col=col[i], lwd=2)
}
legend("topright", samplenames, text.col=col, bty="n")
lcpm <- cpm(x, log=TRUE)
plot(density(lcpm[,1]), col=col[1], lwd=2, ylim=c(0,0.21), las=2,
main="", xlab="")
title(main="B. Filtered data", xlab="Log-cpm")
abline(v=0, lty=3)
for (i in 2:nsamples){
den <- density(lcpm[,i])
lines(den$x, den$y, col=col[i], lwd=2)
}
legend("topright", samplenames, text.col=col, bty="n")
###################################################################

####4-normalising gene expression distribution
####由于实验处理过程中,外部因素对实验的影响等,例如批次效应
####标准化过程要求保证各个样品的表达值分布相似
####对于DGE-List object,使用TMM方法做标准化,
####标准化因子自动储存于x$samples$norm.factors
x <- calcNormFactors(x, method = "TMM")
x$samples$norm.factors

####5-Unsupervised clustering of samples
####无监督聚类分析,multidimensional scaling (MDS) plot,用于质量控制
####理想状况下,相似样品往往聚集成簇,据此筛选不合群样品,以便正式挑选差异

############################################################################
par(mfrow=c(1,2))
col.group <- group
levels(col.group) <- brewer.pal(nlevels(col.group), "Set1")##选择颜色集
col.group <- as.character(col.group)
col.lane <- lane
levels(col.lane) <- brewer.pal(nlevels(col.lane), "Set2")
col.lane <- as.character(col.lane)
plotMDS(lcpm, labels=group, col=col.group)##分组
title(main="A. Sample groups")
##
plotMDS(lcpm, labels=lane, col=col.lane, dim=c(3,4))##batch
title(main="B. Sequencing lanes")
library(Glimma)
glMDSPlot(lcpm, labels=paste(group, lane, sep="_"), groups=x$samples[,c(2,5)],
launch=FALSE)
##############################################################################
####6-Differential expression analysis
##建立实验设计矩阵
design <- model.matrix(~0+group+lane)
design
colnames(design) <- gsub("group", "", colnames(design))##将group去掉
design
##比较矩阵
contrast.matrix <- makeContrasts(BasalvsLP = Basal-LP,
BasalvsML = Basal – ML,
LPvsML = LP – ML,
levels = colnames(design))
contrast.matrix

####Removing heteroscedascity from count data
##产生了voom EList-object
v <- voom(x, design, plot=TRUE)
v
####Fitting linear models for comparisons of interest
vfit <- lmFit(v, design)
vfit <- contrasts.fit(vfit, contrasts=contrast.matrix)
efit <- eBayes(vfit)
plotSA(efit)

####Examing the number of DEGs
##默认设置FDR=0.05
summary(decideTests(efit))
##treat method,进一步挑选
tfit <- treat(vfit, lfc=1)
dt <- decideTests(tfit)
summary(dt)##0代表没有差异的基因
de.common <- which(dt[,1]!=0 & dt[,2]!=0)##前两个比较的差异交集
length(de.common)
head(tfit$genes$SYMBOL[de.common], n=20)
vennDiagram(dt[,1:2], circle.col=c("turquoise", "salmon"))
write.fit(tfit, dt, file="results.txt")

####
basal.vs.lp <- topTreat(tfit, coef=1, n=Inf)#n=Inf指所有基因
basal.vs.ml <- topTreat(tfit, coef=2, n=Inf)#toptreat排好序了
head(basal.vs.lp)

####heatmap绘制热图,这方面还需要进一步学习啦
library(gplots)
dif1.topgenes <- basal.vs.lp$ENTREZID[1:20]##基因数量
i <- which(v$genes$ENTREZID %in% dif1.topgenes)
mycol <- colorpanel(1000,"blue","white","red")
heatmap.2(v$E[i,], scale="row",
labRow=v$genes$SYMBOL[i], labCol=group,
col=mycol, trace="none", density.info="none",
margin=c(8,6), lhei=c(2,10), dendrogram="column")

相关文章

网友评论

      本文标题:RNA-seq差异表达分析实践

      本文链接:https://www.haomeiwen.com/subject/bzrsrqtx.html