加載包
library(Seurat)
library(ggplot2)
library(openxlsx)
Mapping2Ref <- function(reference.object,query.object,cell.type.id,tag,path.out){
reference.object <- RunUMAP(reference.object,reduction.key = "UMAP_",dims = 1:20,return.model = TRUE) #為創(chuàng)建reference.object創(chuàng)建model
anchors <- FindTransferAnchors(reference = reference.object,query = query.object,normalization.method = "LogNormalize",reference.reduction = "pca",dims = 1:30)
query.object <- MapQuery(
anchorset = anchors,
query = query.object,
reference = reference.object,
refdata = list(celltype = cell.type.id),
reference.reduction = "pca",
reduction.model = "umap"
)
plot_mapping <- DimPlot(query.object, reduction = "umap", group.by = "predicted.celltype", label = TRUE, label.size = 3, repel = TRUE)
ggsave(paste0(tag,"_umap_Plot_mapping.png"),plot_mapping,path=path.out)
return(query.object)
}
PlotCelltypeScore <- function(seurat.object,cell.type,tag,path.out){
cell_scores <- seurat.object@meta.data$predicted.celltype.score
cell_scores[seurat.object@meta.data$predicted.celltype!= cell.type] <- 0
seurat.object[["cell_scores"]] <- cell_scores
plot_feature <- FeaturePlot(seurat.object, features = c("cell_scores"), reduction = "umap", cols = c("lightgrey", "darkred")) & theme(plot.title = element_text(size = 10))
ggsave(paste0(tag,"_umap_Plot_",cell.type,".pdf"),plot_feature,path=path.out)
}