第二次RNA-seq实战总结(3)-用DESeq2进行基因表达差异分析

DESeq2是一个用于分析基因表达差异的R包,具体操作要在R语言中运行
1.R语言安装DESeq2

>source("https://bioconductor.org/biocLite.R")
>biocLite("DESeq2")

2.载入基因表达量文件,添加列名

> setwd("C:\\Users\\18019\\Desktop\\counts")
> options(stringsAsFactors=FALSE)
> control1<-read.table("SRR957677_counts.txt",sep = "\t",col.names = c("gene_id","control1"))
> head(control1)
               gene_id control1
1 ENSG00000000003.14_2     1576
2  ENSG00000000005.5_2        0
3 ENSG00000000419.12_2      756
4 ENSG00000000457.13_3      301
5 ENSG00000000460.16_5      764
6 ENSG00000000938.12_2        0
> control2<-read.table("SRR957678_counts.txt",sep = "\t",col.names = c("gene_id","control2"))
> treat1<-read.table("SRR957679_counts.txt",sep = "\t",col.names = c("gene_id","treat1"))
>treat2<-read.table("SRR957680_counts.txt",sep = "\t",col.names = c("gene_id","treat2"))

3.数据整合

> raw_count <- merge(merge(control1, control2, by="gene_id"), merge(treat1, treat2, by="gene_id"))
> head(raw_count)
                 gene_id control1 control2  treat1  treat2
1 __alignment_not_unique  7440131  2973831 7861484 8676884
2            __ambiguous   976485   412543 1014239 1179051
3           __no_feature  1860117   768637 1289737 1812056
4          __not_aligned  1198545   572588 1256232 1348068
5        __too_low_aQual        0        0       0       0
6   ENSG00000000003.14_2     1576      713    1589    1969
#删除前五行
>raw_count_filt <- raw_count[-1:-5,]
#因为我们无法在EBI数据库上直接搜索找到ENSMUSG00000024045.5这样的基因,只能是ENSMUSG00000024045的整数,没有小数点,所以需要进一步替换为整数的形式。
#将_后面的数字替换为空赋值给a
>a<- gsub("\\_\\d*", "", raw_count_filt$gene_id)
#将.后面的数字替换为空赋值给ENSEMBL
>ENSEMBL <- gsub("\\.\\d*", "", a)
# 将ENSEMBL重新添加到raw_count_filt1矩阵
>row.names(raw_count_filt) <- ENSEMBL
> raw_count_filt <- cbind(ENSEMBL,raw_count_filt)
> colnames(raw_count_filt)[1] <- c("ensembl_gene_id")
>head(raraw_count_filt )
                ensembl_gene_id              gene_id control1 control2 treat1 treat2
ENSG00000000003 ENSG00000000003 ENSG00000000003.14_2     1576      713   1589   1969
ENSG00000000005 ENSG00000000005  ENSG00000000005.5_2        0        0      0      1
ENSG00000000419 ENSG00000000419 ENSG00000000419.12_2      756      384    806    984
ENSG00000000457 ENSG00000000457 ENSG00000000457.13_3      301      151    217    324
ENSG00000000460 ENSG00000000460 ENSG00000000460.16_5      764      312    564    784
ENSG00000000938 ENSG00000000938 ENSG00000000938.12_2        0        0      0      0

4.对基因进行注释-获取gene_symbol
用bioMart对ensembl_id转换成gene_symbol

> library("biomaRt")
> library("curl")
> mart <- useDataset("hsapiens_gene_ensembl", useMart("ensembl"))
> my_ensembl_gene_id <- row.names(raw_count_filt)
>  options(timeout = 4000000)
> hg_symbols<- getBM(attributes=c('ensembl_gene_id','hgnc_symbol',"chromosome_name", "start_position","end_position", "band"), filters= 'ensembl_gene_id', values = my_ensembl_gene_id, mart = mart)
#将合并后的表达数据框raw_count_filt和注释得到的hg_symbols整合为一
readcount <- merge(raw_count_filt, hg_symbols, by="ensembl_gene_id")
> head(readcount)
  ensembl_gene_id              gene_id control1 control2 treat1 treat2 hgnc_symbol chromosome_name start_position
1 ENSG00000000003 ENSG00000000003.14_2     1576      713   1589   1969      TSPAN6               X      100627109
2 ENSG00000000005  ENSG00000000005.5_2        0        0      0      1        TNMD               X      100584802
3 ENSG00000000419 ENSG00000000419.12_2      756      384    806    984        DPM1              20       50934867
4 ENSG00000000457 ENSG00000000457.13_3      301      151    217    324       SCYL3               1      169849631
5 ENSG00000000460 ENSG00000000460.16_5      764      312    564    784    C1orf112               1      169662007
6 ENSG00000000938 ENSG00000000938.12_2        0        0      0      0         FGR               1       27612064
  end_position   band
1    100639991  q22.1
2    100599885  q22.1
3     50958555 q13.13
4    169894267  q24.2
5    169854080  q24.2
6     27635277  p35.3
#输出count表达矩阵
> write.csv(readcount, file='readcount_all.csv')
> readcount<-raw_count_filt[ ,-1:-2]
> write.csv(readcount, file='readcount.csv')
> head(readcount)
                control1 control2 treat1 treat2
ENSG00000000003     1576      713   1589   1969
ENSG00000000005        0        0      0      1
ENSG00000000419      756      384    806    984
ENSG00000000457      301      151    217    324
ENSG00000000460      764      312    564    784
ENSG00000000938        0        0      0      0

5.DEseq2筛选差异表达基因并注释(bioMart)

#载入数据(countData和colData)
> mycounts<-readcount
> head(mycounts)
                control1 control2 treat1 treat2
ENSG00000000003     1576      713   1589   1969
ENSG00000000005        0        0      0      1
ENSG00000000419      756      384    806    984
ENSG00000000457      301      151    217    324
ENSG00000000460      764      312    564    784
ENSG00000000938        0        0      0      0
> condition <- factor(c(rep("control",2),rep("treat",2)), levels = c("control","treat"))
> condition
[1] control control treat   treat  
Levels: control treat
> colData <- data.frame(row.names=colnames(mycounts), condition)
> colData
         condition
control1   control
control2   control
treat1       treat
treat2       treat

构建dds对象,开始DESeq流程

>library("DESeq2")
> dds <- DESeqDataSetFromMatrix(mycounts, colData, design= ~ condition)
> dds <- DESeq(dds)
estimating size factors
estimating dispersions
gene-wise dispersion estimates
mean-dispersion relationship
final dispersion estimates
fitting model and testing
> dds
class: DESeqDataSet 
dim: 60880 4 
metadata(1): version
assays(4): counts mu H cooks
rownames(60880): ENSG00000000003 ENSG00000000005 ... ENSG00000285993 ENSG00000285994
rowData names(22): baseMean baseVar ... deviance maxCooks
colnames(4): control1 control2 treat1 treat2
colData names(2): condition sizeFactor
#查看总体结果
> res = results(dds, contrast=c("condition", "control", "treat"))
> res = res[order(res$pvalue),]
> head(res)
log2 fold change (MLE): condition control vs treat 
Wald test p-value: condition control vs treat 
DataFrame with 6 rows and 6 columns
                        baseMean   log2FoldChange             lfcSE             stat               pvalue
                       <numeric>        <numeric>         <numeric>        <numeric>            <numeric>
ENSG00000178691 1025.66218695436 2.83012875791025 0.225513526042636 12.5497073615672 3.98981786210676e-36
ENSG00000135535 2415.77359618136 1.22406336488047 0.183431131037356 6.67314952460929  2.5037106203736e-11
ENSG00000164172 531.425786834548 1.30449018960413 0.207785830749451 6.27805170785243 3.42841906697453e-10
ENSG00000172239 483.998634607265 1.31701332235233 0.215453141699223 6.11275988813803 9.79226522597759e-10
ENSG00000237296 53.0114998109978 2.70139282483841 0.480033904207378 5.62750422660019 1.82835684560772e-08
ENSG00000196504 3592.67315807893 1.09372324353448 0.200308218929736 5.46020153031335 4.75594407815571e-08
                                padj
                           <numeric>
ENSG00000178691 3.90682965057494e-32
ENSG00000135535 1.22581671973492e-07
ENSG00000164172 1.11903598346049e-06
ENSG00000172239 2.39714652731931e-06
ENSG00000237296                   NA
ENSG00000196504 9.31404088266014e-05
> summary(res)

out of 33100 with nonzero total read count
adjusted p-value < 0.1
LFC > 0 (up)       : 78, 0.24%
LFC < 0 (down)     : 15, 0.045%
outliers [1]       : 0, 0%
low counts [2]     : 23308, 70%
(mean count < 135)
[1] see 'cooksCutoff' argument of ?results
[2] see 'independentFiltering' argument of ?results
#这里可以看到有78个基因上调,15个基因下调
#将分析结果输出
> write.csv(res,file="All_results.csv")

提取差异表达基因
这里我用的方法是倍差法
获取padj(p值经过多重校验校正后的值)小于0.05,表达倍数取以2为对数后大于1或者小于-1的差异表达基因

> diff_gene_deseq2 <-subset(res, padj < 0.05 & abs(log2FoldChange) > 1)
> dim(diff_gene_deseq2)
[1] 21  6
> head(diff_gene_deseq2)
log2 fold change (MLE): condition control vs treat 
Wald test p-value: condition control vs treat 
DataFrame with 6 rows and 6 columns
                        baseMean   log2FoldChange             lfcSE             stat               pvalue
                       <numeric>        <numeric>         <numeric>        <numeric>            <numeric>
ENSG00000178691 1025.66218695436 2.83012875791025 0.225513526042636 12.5497073615672 3.98981786210676e-36
ENSG00000135535 2415.77359618136 1.22406336488047 0.183431131037356 6.67314952460929  2.5037106203736e-11
ENSG00000164172 531.425786834548 1.30449018960413 0.207785830749451 6.27805170785243 3.42841906697453e-10
ENSG00000172239 483.998634607265 1.31701332235233 0.215453141699223 6.11275988813803 9.79226522597759e-10
ENSG00000196504 3592.67315807893 1.09372324353448 0.200308218929736 5.46020153031335 4.75594407815571e-08
ENSG00000163848 633.066990185649 1.15489622775117 0.219655131372136 5.25777030810433 1.45812478575117e-07
                                padj
                           <numeric>
ENSG00000178691 3.90682965057494e-32
ENSG00000135535 1.22581671973492e-07
ENSG00000164172 1.11903598346049e-06
ENSG00000172239 2.39714652731931e-06
ENSG00000196504 9.31404088266014e-05
ENSG00000163848 0.000230253090268928
#输出差异基因
> write.csv(diff_gene_deseq2,file= "DEG_treat_vs_control.csv")
#用bioMart对差异表达基因进行注释
> library("biomaRt")
> library("curl")
> hg_symbols<- getBM(attributes=c('ensembl_gene_id','external_gene_name',"description"),filters = 'ensembl_gene_id', values = my_ensembl_gene_id, mart = mart)
> head(hg_symbols)
  ensembl_gene_id external_gene_name
1 ENSG00000011405            PIK3C2A
2 ENSG00000100731              PCNX1
3 ENSG00000128512              DOCK4
4 ENSG00000135535              CD164
5 ENSG00000140526              ABHD2
6 ENSG00000144228              SPOPL
                                                                                                  description
1 phosphatidylinositol-4-phosphate 3-kinase catalytic subunit type 2 alpha [Source:HGNC Symbol;Acc:HGNC:8971]
2                                                               pecanex 1 [Source:HGNC Symbol;Acc:HGNC:19740]
3                                              dedicator of cytokinesis 4 [Source:HGNC Symbol;Acc:HGNC:19192]
4                                                           CD164 molecule [Source:HGNC Symbol;Acc:HGNC:1632]
5                                         abhydrolase domain containing 2 [Source:HGNC Symbol;Acc:HGNC:18717]
6                                       speckle type BTB/POZ protein like [Source:HGNC Symbol;Acc:HGNC:27934]
#合并数据:res结果hg_symbols合并成一个文件
> ensembl_gene_id<-rownames(diff_gene_deseq2)
> diff_gene_deseq2<-cbind(ensembl_gene_id,diff_gene_deseq2)
> colnames(diff_gene_deseq2)[1]<-c("ensembl_gene_id")
> diff_name<-merge(diff_gene_deseq2,hg_symbols,by="ensembl_gene_id")
> head(diff_name)
DataFrame with 6 rows and 9 columns
  ensembl_gene_id         baseMean   log2FoldChange             lfcSE             stat               pvalue
      <character>        <numeric>        <numeric>         <numeric>        <numeric>            <numeric>
1 ENSG00000011405 1600.01408863821 1.07722909393382  0.24714564887963 4.35868120202462 1.30848557424083e-05
2 ENSG00000100731 1162.93822827396  1.0006257630015 0.214393389946423 4.66724166846545 3.05270197242525e-06
3 ENSG00000128512 368.442571635954 1.19657846347522 0.262780839813213  4.5535224878867 5.27550292947225e-06
4 ENSG00000135535 2415.77359618136 1.22406336488047 0.183431131037356 6.67314952460929  2.5037106203736e-11
5 ENSG00000140526 796.447227235737 1.05296203760187  0.23492350092969 4.48214858639031 7.38952622958053e-06
6 ENSG00000144228 293.746859588111 1.10903132755747 0.283181091639851 3.91633255290906  8.9906210067011e-05
                  padj external_gene_name
             <numeric>        <character>
1  0.00533862114290257            PIK3C2A
2    0.002491004809499              PCNX1
3  0.00319558231320097              DOCK4
4 1.22581671973492e-07              CD164
5  0.00364483675888146              ABHD2
6   0.0214722343652725              SPOPL
                                                                                                  description
                                                                                                  <character>
1 phosphatidylinositol-4-phosphate 3-kinase catalytic subunit type 2 alpha [Source:HGNC Symbol;Acc:HGNC:8971]
2                                                               pecanex 1 [Source:HGNC Symbol;Acc:HGNC:19740]
3                                              dedicator of cytokinesis 4 [Source:HGNC Symbol;Acc:HGNC:19192]
4                                                           CD164 molecule [Source:HGNC Symbol;Acc:HGNC:1632]
5                                         abhydrolase domain containing 2 [Source:HGNC Symbol;Acc:HGNC:18717]
6                                       speckle type BTB/POZ protein like [Source:HGNC Symbol;Acc:HGNC:27934]
#输出含注释的差异基因文件
write.csv(diff_name,file= "diff_gene.csv")

到此为止就完成了RNA-seq的数据处理流程,下一步就是用pheatmap绘制热图了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,179评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,229评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,032评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,533评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,531评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,539评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,916评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,574评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,813评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,568评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,654评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,354评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,937评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,918评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,152评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,852评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,378评论 2 342

推荐阅读更多精彩内容