1.利用已有的Seurat對象轉(zhuǎn)換為CellDataSet對象
加載之前seutat標準流程跑完的數(shù)據(jù),如果沒有可以走一遍流程,教程點我
#加載需要的包
library(Seurat)
# devtools::install_github('satijalab/seurat-data')
library(SeuratData)
library(monocle)
library(ggplot2)
library(patchwork)
library(dplyr)
#加載之前seutat標準流程跑完的數(shù)據(jù),如果沒有可以走一遍流程
load(file = '../section-01-cluster/basic.sce.pbmc.Rdata')
pbmc
table(Idents(pbmc))
DimPlot(pbmc, reduction = 'umap', label = TRUE, pt.size = 0.5) + NoLegend()
sce=pbmc
table( Idents(sce ))
table(sce@meta.data$seurat_clusters)
table(sce@meta.data$orig.ident)
將之前的數(shù)據(jù)調(diào)出來后,我們看一下monocle包的newCellDataSet函數(shù)。
?newCellDataSet
#Creates a new CellDateSet object.
#cellData expression data matrix for an experiment
#phenoData data frame containing attributes of individual cells
#featureData data frame containing attributes of features (e.g. genes)
可以看到這三個數(shù)據(jù)在sce中都可以調(diào)出來
head(sce@meta.data )
sample_ann <- sce@meta.data
sample_ann$celltype=Idents(sce)
head(sample_ann)
# rownames(sample_ann)=sample_ann[,1]
gene_ann <- data.frame(gene_short_name = rownames(sce@assays$RNA) , row.names = rownames(sce@assays$RNA)
)
head(gene_ann)
#newCellDataSet要求featureData和phenoData格式為AnnotatedDataFrame
pd <- new("AnnotatedDataFrame",data=sample_ann)
fd <- new("AnnotatedDataFrame",data=gene_ann)
ct=as.data.frame(sce@assays$RNA@counts)
ct[1:4,1:4]
創(chuàng)建CellDataSet對象
sc_cds <- newCellDataSet(as.matrix(ct), phenoData = pd,featureData =fd,expressionFamily = negbinomial.size(),lowerDetectionLimit=1)
sc_cds
convert the Seurat object to a SingleCellExperiment object
pbmc_sce <- as.SingleCellExperiment(pbmc)
convert singlecellexperment to Seurat
as.Seurat(對象名)
Seurat::as.CellDataSet()函數(shù)可以直接將Seurat對象轉(zhuǎn)化為monocle2的對象,進行monocle2的擬時分析
sc_cds<-Seurat::as.CellDataSet(sce)
2.monocle的標準流程
2.1 數(shù)據(jù)過濾
library(monocle)
sc_cds
colnames(fData(sc_cds))
colnames(pData(sc_cds))
sc_cds <- detectGenes(sc_cds, min_expr = 1)
colnames(fData(sc_cds))
colnames(pData(sc_cds))
#detectGenes設(shè)置一個閾值,在在此閾值之上的 CellDataSet對象中,計算在每個細胞中基因表達的數(shù)量pdata(cds)$num_genes_expressed,計算每個基因在多少細胞中表達fdata(cds)$num_cells_expressed。
sc_cds <- sc_cds[fDat(sc_cds$num_cells_expressed > 10, ]
# 數(shù)值可以自行摸索 featureData返回一個包含變量值和變量元數(shù)據(jù)信息的對象。 fvarLabels 返回測量變量名稱的字符向量。 fData 返回一個數(shù)據(jù)框,其中基因為行,變量為列。 fvarMetadata 返回一個數(shù)據(jù)框,其中變量名稱作為行,描述標簽(例如,測量單位)作為列。
sc_cds
cds <- sc_cds
2.2 標準化和歸一化
cds <- estimateSizeFactors(cds)
cds <- estimateDispersions(cds)
2.3 挑選表達量不太低的基因用于后續(xù)分析
# dispersionTable差異基因,并不是所有的基因都有作用,所以先進行挑選,合適的基因用來進行聚類。
disp_table <- dispersionTable(cds)
## 挑表達量不太低的基因,在各細胞中平均表達量大于0.1
unsup_clustering_genes <- subset(disp_table,mean_expression >= 0.1)
unsup_clustering_genes
## 準備聚類基因名單
cds <- setOrderingFilter(cds, unsup_clustering_genes$gene_id)
#按均值與離散度繪制基因,突出顯示選擇排序的基因,每個灰點都是一個基因。黑點是包含在最后一次調(diào)用 setOrderingFilter中的那些。紅色曲線顯示通過estimateDispersions() 學習的均方差模型。
plot_ordering_genes(cds)
#從歸一化后的數(shù)據(jù)中繪制基于 PCA 的每個主成分的方差百分比
plot_pc_variance_explained(cds,
2.4 降維聚類分群
# 降維,其中 num_dim 參數(shù)選擇基于上面的PCA圖
cds <- reduceDimension(cds, max_components = 2, num_dim = 6,reduction_method = 'tSNE', verbose = T)
#聚類
cds <- clusterCells(cds, num_clusters = 6)
plot_cell_clusters(cds, 1, 2 )
2.5 查看monocle和seurat聚類分群的差別
#phenoData 返回一個包含變量值和變量元數(shù)據(jù)信息的對象。 varLabels 返回測量變量的字符向量。 pData 返回一個數(shù)據(jù)框,其中樣本為行,變量為列。 varMetadata 返回一個數(shù)據(jù)框,其中變量名稱為行,描述標簽(例如,測量單位)為列。
head(rownames(pData(cds)))
colnames(pData(cds))
table(pData(cds)$Cluster)
table(pData(cds)$seurat_clusters)
table(pData(cds)$Cluster,pData(cds)$seurat_clusters)
table(pData(cds)$Cluster,pData(cds)$celltype)
可以看到 monocle 給細胞重新定義了亞群,亞群數(shù)量可以自己選擇,整體來說,monocle和seurat 各自獨立流程定義的亞群的一致性還不錯。