系列回顧:
1.RNA velocity分析練習(一)文件下載以及預處理
2.RNA velocity分析練習(二)軟件安裝
3.RNA velocity分析練習(三)生成loom文件
這一篇文章就正式開始進行RNA velocity的練習。本篇文章代碼參考:
velocyto簡單使用
(一)加載數據并過濾
> input_loom <- "MyTissue.loom"
> adata <- read.loom.matrices(input_loom)
# Use the spliced data as the input data
> spliced_adata <- adata$spliced
# seperate into E12.5 and E13.5
# 因為文章里只用了E12.5天的數據來進行RNA velocity的分析,所以這里我也把它們區分一下
> E12.5_data <- spliced_adata[,1:384]
> E13.5_data <- spliced_adata[,385:768]
看一下spliced mRNA count的分布(下面的分析都用E12.5數據):
# spliced expression magnitude distribution across genes:
> hist(log10(rowSums(E12.5_data)+1),col='wheat',xlab='log10[ number of reads + 1]',main='number of reads per gene')
過濾前看一下多少基因:
> dim(E12.5_data)
[1] 26610 384 #一共是26610個基因
把在所有細胞里都不表達的基因刪掉:
> E12.5_data <- E12.5_data[as.numeric(rowSums(E12.5_data)) != 0,]
> dim(E12.5_data)
[1] 23803 384 #還剩23803個基因
保留至少在10個以上的細胞里表達的基因:
> fdata_E12.5 = E12.5_data[apply(E12.5_data,1,function(x) sum(x>1) > 10),]
> dim(fdata_E12.5)
[1] 13767 384 #這里還剩13767個基因
現在再看一下reads的分布:
> hist(log10(rowSums(fdata_E12.5)+1),col='wheat',xlab='log10[ number of reads + 1]',main='number of reads per gene')
(二)標準化
上面我們取的是spliced的數據(我們平時做表達分析的時候,實際上用的都是spliced),現在用它來進行數據的標準化,以便后面的細胞聚類。
> library(pagoda2)
#這里要先過濾一下基因名,如果不過濾的話,后面標準化步驟會報錯,告訴你基因名有重復,我也不知道怎么會有重復,很奇怪
> fdata_E12.5 <- fdata_E12.5[duplicated(rownames(fdata_E12.5))=="FALSE",]
> dim(fdata_E12.5)
[1] 13765 384 #這里只少了2個基因,說明有2個基因重復了
> fdata_E12.5_nor <- Pagoda2$new(fdata_E12.5,modelType='plain',trim=10,log.scale=T)
384 cells, 13765 genes; normalizing ... using plain model winsorizing ... log scale ... done.
#可視化
> fdata_E12.5_nor$adjustVariance(plot=T,do.par=T,gam.k=10)
calculating variance fit ... using gam 2396 overdispersed genes ... 2396persisting ... done.
(三)細胞聚類
# clustering cells,embedding analysis
> fdata_E12.5_nor$calculatePcaReduction(nPcs=100,n.odgenes=3e3,maxit=300)
running PCA using 3000 OD genes .... done
> fdata_E12.5_nor$makeKnnGraph(k=30,type='PCA',center=T,distance='cosine')
creating space of type angular done
adding data ... done
building index ... done
querying ... done
> fdata_E12.5_nor$getKnnClusters(method=multilevel.community,type='PCA',name='multilevel')
> fdata_E12.5_nor$getEmbedding(type='PCA',embeddingType='tSNE',perplexity=50,verbose=T)
#make figure(clustering)
> par(mfrow=c(1,2))
> fdata_E12.5_nor$plotEmbedding(type='PCA',embeddingType='tSNE',show.legend=F,mark.clusters=T,min.group.size=10,shuffle.colors=F,mark.cluster.cex=1,alpha=0.7,main='cell clusters')
> fdata_E12.5_nor$plotEmbedding(type='PCA',embeddingType='tSNE',colors=fdata_E12.5_nor$depth,main='depth')
(四)RNA velocity分析
現在我們需要把spliced和unspliced的數據導出來,和我們上面得到的標準化后的數據做個交集。然后把RNA velocity嵌合進t-SNE的圖里。
#prepare spliced data and unspliced data
> emat <- adata$spliced
> nmat <- adata$unspliced
# filtered cells according to fdata_E12.5_nor$counts
> emat <- emat[,rownames(fdata_E12.5_nor$counts)]
> nmat <- nmat[,rownames(fdata_E12.5_nor$counts)]
#lable the clustered data對分類數據進行標記
> cluster.label <- fdata_E12.5_nor$clusters$PCA$multilevel # take the cluster factor that was calculated by fdata_E12.5_nor
> cell.colors <- pagoda2:::fac2col(cluster.label)
# take embedding from fdata_E12.5_nor
> emb <- fdata_E12.5_nor$embeddings$PCA$tSNE
# Calculate the distance between cells
> cell.dist <- as.dist(1-armaCor(t(fdata_E12.5_nor$reductions$PCA)))
#基于最小平均表達量篩選基因(至少在一個簇中),輸出產生的有效基因數
> emat <- filter.genes.by.cluster.expression(emat,cluster.label,min.max.cluster.average = 0.2)
> nmat <- filter.genes.by.cluster.expression(nmat,cluster.label,min.max.cluster.average = 0.05)
> length(intersect(rownames(emat),rownames(nmat)))
[1] 13016
# Calculate RNA velocity
> fit.quantile <- 0.02
> rvel.cd <- gene.relative.velocity.estimates(emat,nmat,deltaT=1,kCells=25,cell.dist=cell.dist,fit.quantile=fit.quantile)
calculating cell knn ... done
calculating convolved matrices ... done
fitting gamma coefficients ... done. succesfful fit for 13016 genes
filtered out 2115 out of 13016 genes due to low nmat-emat correlation
filtered out 1543 out of 10901 genes due to low nmat-emat slope
calculating RNA velocity shift ... done
calculating extrapolated cell state ... done
可視化:
> show.velocity.on.embedding.cor(emb,rvel.cd,n=200,scale='sqrt',cell.colors=ac(cell.colors,alpha=0.5),cex=0.8,arrow.scale=3,show.grid.flow=TRUE,min.grid.cell.mass=0.5,grid.n=40,arrow.lwd=1,do.par=F,cell.border.alpha = 0.1)
delta projections ... sqrt knn ... transition probs ... done
calculating arrows ... done
grid estimates ... grid.sd= 0.3592013 min.arrow.size= 0.007184026 max.grid.arrow.length= 0.02704895 done
可視化指定的基因:
> gene <- "Serpine2"
> gene.relative.velocity.estimates(emat,nmat,deltaT=1,kCells = 100,kGenes=1,fit.quantile=fit.quantile,cell.emb=emb,cell.colors=cell.colors,cell.dist=cell.dist,show.gene=gene,old.fit=rvel.cd,do.par=T)
> gene <- "Chga"
> gene.relative.velocity.estimates(emat,nmat,deltaT=1,kCells = 100,kGenes=1,fit.quantile=fit.quantile,cell.emb=emb,cell.colors=cell.colors,cell.dist=cell.dist,show.gene=gene,old.fit=rvel.cd,do.par=T)
(五)與文獻原圖進行比較
到這里就基本完成RNA velocity的分析了。然后我想把自己的圖跟文獻里的對比一下,于是就放在了PPT里做了一張組合圖:
可以看到,原文里的細胞分化軌跡是“藍>紅(黃)>綠”的一個順序,我的是“綠色> 粉(紅)>藍>黃”,顏色不要緊,主要是要有一個線性的方向。所以這里文獻里的SCP細胞在我的圖里就是綠色群,Differentiation bridge是紅色/粉色群,chromaffin就是我的黃色的群(或者藍色?)
原文里展示的兩個基因,Serpine2主要表達在SCP群里(b左),unspliced mRNA主要出現在SCP群里(b中,右),也就是說在SCP里表達量很高,因為有很多新轉錄出來的unspliced mRNA;Chga基因主要表達在chromaffin群里,也就是分化末端里(c左),這個基因的unspliced mRNA主要在bridge群和一部分的chromaffin群里(c中,右),說明這個基因在分化過渡階段和分化終端表達量高。大體上我的結果和文章里的還是差不多的。說明分析過程沒有太大的問題。