library(Seurat)
library(monocle)
library(data.table)
load("data/MacrophageRDS/macrophage.rds")
set.seed(123)
project = project_monocle
project
DimPlot(project)
FeaturePlot(object = project, features = 'CD34', label = TRUE, cols=c('grey95', 'red3'), pt.size=1)
features <- c("CD34","MYB","KLF1","GATA1","PF4","GP9","MPO","LYZ","HLA-DRA","CCR2","CD14","MRC1")
DotPlot(project, features = features) + RotatedAxis()
data <- as(as.matrix(project@assays$RNA@counts),'sparseMatrix')
pd <- project@meta.data
fd <- data.frame(gene_short_name = row.names(project),row.names=row.names(project))
head(pd)
head(fd)
pd <- new("AnnotatedDataFrame",data=pd)
fd <- new("AnnotatedDataFrame",data=fd)
cds <- newCellDataSet(data, #expression data matrix for an experiment
phenoData = pd, #data frame containing attributes of individual cells
featureData = fd, #data frame containing attributes of features (e.g. genes)
lowerDetectionLimit = 0.5, #the minimum expression level that consistitutes true expression
expressionFamily = negbinomial.size()) #the VGAM family function to be used for expression
cds
cds <- estimateSizeFactors(cds)
cds <- estimateDispersions(cds)
cds <- detectGenes(cds,min_expr = 0.1)
head(fData(cds))
head(pData(cds))
project <- FindVariableFeatures(project,selection.method = "vst",nfeatures = 2000)
gene_var <- VariableFeatures(project)
head(gene_var)
write.table(gene_var,file="data/MacrophageRDS/monocle_DEG.xls",col.names = T,row.names = F,sep="\t",quote = F)
ordergene <- as.vector(unique(gene_var))
cds <- setOrderingFilter(cds,ordergene)
plot_ordering_genes(cds)
cds <- reduceDimension(cds, max_components = 2, method = 'DDRTree')
cds = orderCells(cds)
#choose the root
#cds <- orderCells(cds,root_state=?)
plot_cell_trajectory(cds,color_by="Pseudotime", size=1,show_backbone=TRUE)
plot_cell_trajectory(cds,color_by="CellType", size=1,show_backbone=TRUE)
plot_cell_trajectory(cds, color_by = "State",size=1,show_backbone=TRUE)
plot_cell_trajectory(cds, color_by = "CellType") + facet_wrap("~State", nrow = 1)
keygenes <- head(ordergene,5)
plot_genes_in_pseudotime(cds[keygenes],color_by = "State")
plot_genes_in_pseudotime(cds[keygenes],color_by = "CellType")
plot_genes_in_pseudotime(cds[keygenes],color_by = "Pseudotime")
plot_genes_jitter(cds[keygenes,],color_by = "State",grouping = "State")
plot_genes_jitter(cds[keygenes,],color_by = "CellType",grouping = "CellType")
plot_genes_jitter(cds[keygenes,],color_by = "Pseudotime",grouping = "Pseudotime")
#fullModelFormulaStr = "~sm.ns(Pseudotime)" "Pseudotime","CellType","Cluster"
Deg_Pseudotime <- differentialGeneTest(cds[ordergene,],cores = 1,fullModelFormulaStr = "~sm.ns(Pseudotime)")
Deg_Pseudotime <- Deg_Pseudotime[order(Deg_Pseudotime$qval),]
head(Deg_Pseudotime)
write.table(Deg_Pseudotime,file="data/MacrophageRDS/Diff_pseudotime_heatmap.xls",sep="\t",row.names = F)
# show single gene
pData(cds)$S100A8 =log2(exprs(cds)['S100A8',]+1)
plot_cell_trajectory(cds,color_by = "S100A8")
# heatmap
top50 = (Deg_Pseudotime$gene_short_name)[1:50]
plot_pseudotime_heatmap(cds[top50,],num_clusters = 4,cores = 1,show_rownames = T)
plot_genes_in_pseudotime(cds[top50[1:4],],nrow= 2,ncol = 2)
BEAM_res <- BEAM(cds, branch_point = 1, cores = 8)
BEAM_res <- BEAM_res[order(BEAM_res$qval),]
head(BEAM_res)
plot_genes_branched_heatmap(cds[row.names(BEAM_res[1:50,]),],branch_point = 1,
num_clusters =4,cores = 1,use_gene_short_name = T,
show_rownames = T, return_heatmap = F)
# jupyter nbconvert --to html Monocle2-Macrophage.ipynb --template classic