monocle构建进化

加载需要的包

library(monocle)
library(ggplot2)
library(Seurat)

加载函数

order.gene参数可选项:

  • variable
  • difftest
CreatMonocleTrajectory <- function(seurat.object,order.gene,out.path){
exp_matrix <- GetAssayData(object = seurat.object[["integrated"]],slot = "data")
exp_matrix <- 2^exp_matrix
pheno_Data <- seurat.object@meta.data
feature_Data <- data.frame(gene_short_name = row.names(exp_matrix),row.names = row.names(exp_matrix))
pd <- new("AnnotatedDataFrame",data = pheno_Data)
fd <- new("AnnotatedDataFrame",data = feature_Data)
cds <- newCellDataSet(as(as.matrix(exp_matrix), "sparseMatrix"), phenoData = pd, featureData = fd,expressionFamily = tobit())
#expressionFamily参数,稀疏矩阵用negbinomial.size(),FPKM值用tobit(),logFPKM值用uninormal()
cds <- estimateSizeFactors(cds)
print("estimateSizeFactors OK")
#cds <- estimateDispersions(cds)
#print("estimateDispersions OK")
#cds <- detectGenes(cds,min_expr=0.1) seurat已经过滤过无需再过滤
print("cds have been constructed")
if (order.gene=="variable"){
    variable_genes <- VariableFeatures(seurat.object)
    cds <-setOrderingFilter(cds,variable_genes)
    print("setOrder OK")
}
if (order.gene=="difftest"){
    clustering_DEG_genes <-differentialGeneTest(cds,fullModelFormulaStr = '~seurat_clusters', cores = 4)
    ordering_genes <-row.names(subset(clustering_DEG_genes,qval<0.01))
    cds <-setOrderingFilter(cds,ordering_genes)
    print("setOrder OK")
}
#plot_4 <- plot_ordering_genes(cds)
#ggsave('ordering_variable_genes.pdf',plot_4,path=out.path) 
print("start reduceDimension")
cds <- reduceDimension(cds,method = 'DDRTree')
print("start orderCells")
cds <- orderCells(cds)
plot_5 <- plot_cell_trajectory(cds, color_by='seurat_clusters') 
ggsave('monocle_trajectory.pdf',plot_5,path=out.path) 
plot_6 <- plot_cell_trajectory(cds, color_by='orig.ident') 
ggsave('monocle_trajectory_sample.pdf',plot_6,path=out.path) 
plot_7 <- plot_cell_trajectory(cds, color_by='seurat_clusters') + facet_wrap(~seurat_clusters, nrow=3) 
ggsave('monocle_trajectory_cluster_sep.pdf',plot_7,path=out.path) 
plot_8 <- plot_cell_trajectory(cds, color_by='orig.ident') + facet_wrap(~orig.ident, nrow=3) 
ggsave('monocle_trajectory_sample_sep.pdf',plot_8,path=out.path) 
for (i in highlight_genes){
    key_gene_plot <- plot_cell_trajectory(cds, markers=i,use_color_gradient=TRUE,,cell_size=0.5)+scale_colour_gradient(low ="white",high ="#3333FF")
    ggsave(paste0(i,"_gene_plot.pdf"),key_gene_plot,path=out.path)
}
plot_9 <- plot_cell_trajectory(cds, color_by='State')
ggsave('monocle_trajectory_by_state.pdf',plot_9,path=out.path)
saveRDS(cds,file=paste(out.path,'monocle_cds.rds',sep='/'))
return(cds)
}

加载数据

path_out_genomics_debach_FAP <- "/data/zhaoxueya/project/gut/result/10Xgenomics/debach/subset/FAP"
path_out_genomics_debach_FAP_monocle <- "/data/zhaoxueya/project/gut/result/10Xgenomics/debach/subset/FAP/monocle"
seurat_object_genomics_FAP <- readRDS(file=paste(path_out_genomics_debach_FAP,"seurat_object_subset.rds",sep="/"))
CreatMonocleTrajectory(seurat.object=seurat_object_genomics_FAP,order.gene="variable",out.path=path_out_genomics_debach_FAP_monocle)

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift中有如此多的语法和功能术语,有时候容易感到困惑。因此,此文章将充当术语的一站式词典,为Swift语言中使...
    LV大树阅读 1,745评论 0 5
  • 第一课:安装与基本操作 R的扩展包在R官网CRAN;另外,R官网还包含很多扩展资料,包括源代码,手册,FAQ,推荐...
    lizi_sjtu阅读 679评论 0 0
  • 目的: iOS APP在下一个版本会用swift开发,在多人开发中,即使有官网的规范模板.但每个人的代码风格和规...
    nick5683阅读 1,912评论 0 1
  • 目的: iOS APP在下一个版本会用swift开发,在多人开发中,即使有官网的规范模板.但每个人的代码风格和规...
    技术进阶在路上阅读 13,055评论 2 15
  • 常用配置指令说明 1. ServerRoot:服务器的基础目录,一般来说它将包含conf/和logs/子目录,其它...
    小僧有礼了阅读 4,564评论 0 5