library(Seurat)
library(monocle)
library(data.table)
project <- readRDS("data/PbmcRDS/pbmc.rds")
project
DimPlot(project)
FeaturePlot(object = project, features = 'CD8B', label = TRUE, cols=c('grey95', 'red3'), pt.size=0.001)
features = c("CD8A", "LYZ", "CCL5", "IL32", "PTPRCAP", "FCGR3A", "PF4")
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 response variables
cds
cds <- estimateSizeFactors(cds)
cds <- estimateDispersions(cds)
cds <- detectGenes(cds, min_expr = 0.1)
head(fData(cds))
head(pData(cds))
des <- FindAllMarkers(project)
head(des)
diff <- subset(des[grep("^RP[L|S]",des$gene, ignore.case = FALSE,invert=TRUE),],subset=avg_log2FC>0.25 & pct.1 > 0.25 & (pct.1 > pct.2) & p_val < 0.05)
dim(diff)
head(diff)
write.table(diff,file="data/PbmcRDS/monocle_DEG.txt",col.names = T,row.names = F,sep="\t",quote = F)
ordergene <- as.vector(unique(diff$gene))
cds <- setOrderingFilter(cds,ordergene)
cds
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")
#fullModelFormulaStr = "~sm.ns(Pseudotime)" "Pseudotime","CellType","Cluster"
head(pData(cds))
Deg_Pseudotime <- differentialGeneTest(cds[ordergene,],cores = 1,fullModelFormulaStr = "~ Pseudotime")
Deg_Pseudotime <- Deg_Pseudotime[order(Deg_Pseudotime$qval),]
head(Deg_Pseudotime)
write.table(Deg_Pseudotime, file = "data/PbmcRDS/Diff_pseudotime_heatmap.txt", sep = "\t",row.names = F)
# show single gene
pData(cds)$S100A9 =log2(exprs(cds)['S100A9',]+1)
plot_cell_trajectory(cds,color_by = "S100A9")
# 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 = 1)
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)
write.table(BEAM_res,file="data/PbmcRDS/branch1_genes_analysis.xls",
sep="\t",quote=FALSE,col.names=TRUE,row.names=FALSE)
save(list=c("cds","BEAM_res"),file = "data/PbmcRDS/monocle_PbmcRDS.RData")