RNA-seq:Kallisto+Sleuth (2)

Kallisto下游推荐的分析软件是Sleuth,所以我们本次介绍如何使用Sleuth进行相关的分析。

RNA-seq在完成转录本的鉴定及定量后,我们经常做的分析之一就是差异表达。通常我们使用较多的有以下工具:

而Kallisto主要推荐的下游分析软件是Sleuth这个R包。Sleuth与其他常用的包进行比较其结果还是很不错的。

下面我们来介绍一下该包的使用。

Sleuth的安装

source("http://bioconductor.org/biocLite.R")
biocLite("rhdf5")
install.packages("devtools")
devtools::install_github("pachterlab/sleuth")

如果你安装了conda,也可以通过命令行的方式安装conda install --channel bioconda r-sleuth

Sleuth的使用

#设置kallisto生成的路径
base_dir <- "/home/617/sleuth_analysis/kallisto_qaunt_output"
#获取所有的simple_id,具体可以看一些file.path命令的使用
sample_id <- dir(file.path(base_dir))
sample_id
## [1] "WT_1" "WT_2" "WT_3" Mutation_1"
## [5] "Mutation_2" Muatation_3"
#获取结果文件的路径
kal_dirs <- sapply(sample_id, function(id) file.path(base_dir, id))
kal_dirs
##                                                  WT_1 
## "/home/617/sleuth_analysis/kallisto_qaunt_output/WT_1" 
##                                                  WT_2
## "/home/617/sleuth_analysis/kallisto_qaunt_output/WT_2" 
##                                                  WT_3 
## "/home/617/sleuth_analysis/kallisto_qaunt_output/WT_3" 
##                                                  Mutation_1 
## "/home/617/sleuth_analysis/kallisto_qaunt_output/Mutation_1" 
##                                                  Mutation_2
## "/home/617/sleuth_analysis/kallisto_qaunt_output/Mutation_2" 
##                                                  Mutation_3
## "/home/617/sleuth_analysis/kallisto_qaunt_output/Mutation_3"

#读取实验设计表
s2c <- read.table("./design_matrix.txt", header = TRUE, sep='\t',stringsAsFactors=FALSE)
s2c
##           sample condition reps
## 1 WT_1        WT    1
## 2 WT_2        WT    2
## 3 WT_3        WT    3
## 4 Mutation_1        Mutation    1
## 5 Mutation_2        Mutation    2
## 6 Mutation_3        Mutation    3
#与路径合并
s2c <- dplyr::mutate(s2c, path = kal_dirs)
print(s2c)
##           sample condition reps
## 1 WT_1        WT    1
## 2 WT_2        WT    2
## 3 WT_3        WT    3
## 4 Mutation_1        Mutation    1
## 5 Mutation_2        Mutation    2
## 6 Mutation_3        Mutation    3
##                                                                 path
## 1 /home/617/sleuth_analysis/kallisto_qaunt_output/WT_1
## 2 /home/617/sleuth_analysis/kallisto_qaunt_output/WT_2
## 3 /home/617/sleuth_analysis/kallisto_qaunt_output/WT_3
## 4 /home/617/sleuth_analysis/kallisto_qaunt_output/Mutation_1
## 5 /home/617/sleuth_analysis/kallisto_qaunt_output/IMutation_2
## 6 /home/617/sleuth_analysis/kallisto_qaunt_output/Mutation_3

#获取转录本ID信息
#方法一:基于biomart
library(biomaRt)
#change to genename
mart <- biomaRt::useMart(biomart = "ENSEMBL_MART_ENSEMBL",
                         dataset = "hsapiens_gene_ensembl",
                         host = 'ensembl.org')
t2g <- biomaRt::getBM(attributes = c("ensembl_transcript_id", "ensembl_gene_id",
                                     "external_gene_name"), mart = mart)
t2g <- dplyr::rename(t2g, target_id = ensembl_transcript_id,
                     ens_gene = ensembl_gene_id, ext_gene = external_gene_name)
#方法二:下载EnsDb.Hsapiens.v75或者其他版本相应的R包
library('EnsDb.Hsapiens.v75')
library('ensembldb')
mmsdb<- EnsDb.Hsapiens.v75
tx.mms<- transcripts(mmsdb, return.type = "DataFrame")
tx.mms<- tx.mms[, c("tx_id", "gene_id")]  
gene.mms<- genes(mmsdb, return.type = "DataFrame") 
gene.mms<- gene.mms[, c("gene_id", "symbol")]  
tx2genes.mms <- merge(tx.mms, gene.mms, by = "gene_id") 
t2g <- tx2genes.mms[, 2:3]
#方法三:自己制作相应的txt文件第一列为target_id,第二列为对应的gene(可以用ID或者是Symbol)
t2g <- read.table("./t2g.txt", header = TRUE, stringsAsFactors=FALSE)

#读取Kallisto结果文件
#如果之前在kallisto quant没有设置-b参数该步会报错
library(sleuth)
so <- sleuth_prep(s2c, ~ condition, target_mapping = t2g, extra_bootstrap_summary = TRUE)

#如果想要将一个基因的不同转录本合并,可以按照以下模式导入
so <- sleuth_prep(s2c, ~ condition, target_mapping = t2g, extra_bootstrap_summary = TRUE,gene_mode=T,aggregation_column = 'ext_gene')

#建立模型,该步通过依据不同实验条件下(WT与Mutation)的数据构建线性模型,以“smooth”每个样品的原始kallisto丰度估计值。
so <- sleuth_fit(so)
#为了鉴定不同组差异表达的转录本,sleuth进行第二次拟合即“reduced”模型,该模型假设两种条件下的丰度相等。然后Sleuth将识别与“full”模型明显更好拟合的转录本,即在假设丰度相等的情况下与reduced模型拟合度低,而加入变量不同的实验条件后与full模型的拟合度高,说明该转录本/基因表达有差异。
so <- sleuth_fit(so, ~1, 'reduced')
#Sleuth采取 likelihood ratio test(LRT) 进行鉴定
so <- sleuth_lrt(so, 'reduced', 'full')
#可以使用models()查看不同的模型
models(so)
## [  full  ]
## formula:  ~condition 
## coefficients:
##  (Intercept)
##      conditionWW
## [  reduced  ]
## formula:  ~1 
## coefficients:
##  (Intercept)

# LRT检验结果
sleuth_table <- sleuth_results(so, 'reduced:full', 'lrt', show_all = FALSE)
sleuth_significant <- dplyr::filter(sleuth_table, qval <= 0.05)

#Sleuth还提供了Wald test。该函数计算每个转录本上一个特定'beta'系数的Wald检验;这提供了β值,它们是成对比较的fold change偏差值。
so <- sleuth_wt(so,'conditionWT','full')
results_table <- sleuth_results(so, 'conditionWT', test_type = 'wt')
sleuth_significant <- dplyr::filter(results_table, qval <= 0.05)

#如何导出基因表达量TPM
sleuth_geneexp<-sleuth_to_matrix(so,'obs_norm', 'tpm')



以上就是Sleuth 的应用。如果你依然还是想要使用经典的DESeq2,你可以这样导入文件:

library(tximport)
library(DESeq2)
txi<-tximport(files=kal_dirs,type='kallisto',tx2gene=t2g)
dds1<-DESeqDataSetFromTximport(txi,s2c,~condition)

然后就可使用DESeq2进行下游的分析。

你可能感兴趣的:(RNA-seq:Kallisto+Sleuth (2))