科技资讯

plot1cell:炫酷的单细胞聚类图制作R包

发布日期:2023-06-22    点击次数:183

详情请联系作者:

❞这种NCS文章炫酷的单细胞聚类图,大家应该都见过:

图片

(reference:Mapping the single-cell transcriptomic response of murine diabetic kidney disease to therapies)还有这样的:

图片

(reference:Molecular architecture of the developing mouse brain)后来发现是Mapping the single-cell transcriptomic response of murine diabetic kidney disease to therapies这篇文章提到的R包plot1cell,链接是:https://github.com/HaojiaWu/plot1cell。plot1cell不仅可以进行炫酷的聚类图展示,也可以对单细胞数据的其他内容进行展示,例如气泡图,小提琴图,细胞比例图等等。这里我们只说几个感觉可以的,其他的感兴趣的小伙伴可自行学习。本文代码已上传QQ群文件!首先安装R包,这个包依赖包挺多的,按照提示将依赖包安装号,然后安装这个包就不会出错!
setwd('D:/KS项目/公众号文章/炫酷单细胞聚类图')devtools::install_github("TheHumphreysLab/plot1cell")library(plot1cell)# bioc.packages <- c("DoubletFinder","EnsDb.Hsapiens.v86",#                    "GEOquery","simplifyEnrichment")# BiocManager::install(bioc.packages)# # dev.packages <- c("chris-mcginnis-ucsf/DoubletFinder")# devtools::install_github(dev.packages)
一、Circlize plot to visualize cell clustering and meta data这个包可以轻松进行炫酷的单细胞聚类图展示,这个是其主要功能,输入的数据对象是seurat对象即可。但是实践中发现,作者的函数只针对UMAP,所以TSNE降维会报错。所以TSNE要做这个聚类图,需要原函数进行修改一下,这里我们先用TSNE降维的数据展示一下。当然后期其他作图的一些函数也需要。
get_metadata <- function(    seu_obj,     reductions = "tsne",     coord_scale = 0.8,     color){  metadata<-seu_obj@meta.data  metadata$Cluster<-seu_obj@active.ident  metadata$dim1<-as.numeric(seu_obj[[reductions]]@cell.embeddings[,1])  metadata$dim2<-as.numeric(seu_obj[[reductions]]@cell.embeddings[,2])  metadata$x<-transform_coordinates(metadata$dim1, zoom = coord_scale)  metadata$y<-transform_coordinates(metadata$dim2, zoom = coord_scale)  color_df<-data.frame(Cluster=levels(seu_obj), Colors=color)  cellnames<-rownames(metadata)  metadata$cells<-rownames(metadata)  metadata<-merge(metadata, color_df, by='Cluster')  rownames(metadata)<-metadata$cells  metadata<-metadata[cellnames,]  metadata}prepare_circlize_data <- function(    seu_obj,     scale =0.8){  celltypes<-levels(seu_obj)  cell_colors <- scales::hue_pal()(length(celltypes))  data_plot <- get_metadata(seu_obj, color = cell_colors, coord_scale = scale)  data_plot <- cell_order(data_plot)  data_plot$x_polar2 <- log10(data_plot$x_polar)  data_plot}###Prepare data for plotingcirc_data <- prepare_circlize_data(uterus, scale = 0.8)set.seed(1234)cluster_colors<-rand_color(length(levels(uterus)))group_colors<-rand_color(length(names(table(uterus$orig.ident))))###plot and save figurespng(filename =  'circlize_plot.png', width = 6, height = 6,units = 'in', res = 300)plot_circlize(circ_data, do.label = T, pt.size = 1,               col.use = cluster_colors ,bg.color = 'white',               kde2d.n = 500, repel = T, label.cex = 0.8)add_track(circ_data, group = "orig.ident", colors = group_colors, track_num = 2)dev.off()

图片

如果数据时UMAP降维的,那么就不用担心,直接跑代码即可:
# ============1. Circlize plot to visualize cell clustering and meta data======####Prepare data for plotingcirc_data <- prepare_circlize_data(human_data, scale = 0.8)cluster_colors<-rand_color(length(levels(human_data)))group_colors<-rand_color(length(names(table(human_data$group))))rep_colors<-rand_color(length(names(table(human_data$orig.ident))))###plot and save figurespng(filename =  'circlize_plot1.png', width = 6, height = 6,units = 'in', res = 300)plot_circlize(circ_data, do.label = T, pt.size = 1,               col.use = cluster_colors ,bg.color = 'white',               kde2d.n = 500, repel = T, label.cex = 0.8)add_track(circ_data, group = "group", colors = group_colors, track_num = 2) add_track(circ_data, group = "orig.ident",colors = rep_colors, track_num = 3) #添加legendlegend("topright",        legend = unique(human_data$group),       col = group_colors,       pch = 15,       cex=0.5,       pt.cex=2,       box.lwd = 0,       bg=NULL,       y.intersp = 1,       box.lty=0)legend("topleft",        legend = unique(human_data$orig.ident),       col = rep_colors,       pch = 15,       cex=0.5,       pt.cex=2,       box.lwd = 0,       bg=NULL,       y.intersp = 1.5,       box.lty=0)dev.off()

图片

这个图不仅是一个只能是炫酷的聚类图,在外圈的circle种,其实也有很多信息可以添加展示,例如我们的示例数据展示的是每个细胞类型下,不同分组的细胞占比。二、Dotplot to show gene expression across groups这个R包可以展示基因表达气泡图。我觉得还是挺实用,可以学习下。
png(filename =  'dotplot_multiple.png', width = 10, height = 4,units = 'in', res = 300)complex_dotplot_multiple(seu_obj = human_data,                          features = c("CD3E","S100A8","APOE","CXCL3"),                         group = "group", celltypes = c("Macrophage","T cell","mDC","Neutrophil","Mast"))dev.off()

图片

png(filename =  'dotplot_multiple2.png', width = 10, height = 4,units = 'in', res = 300)complex_dotplot_multiple(seu_obj = human_data,                          features = c("CD3E","S100A8","APOE","CXCL3"),                         group = "orig.ident", celltypes = c("Macrophage","T cell","mDC","Neutrophil","Mast"))dev.off()

图片

三、Cell proportion change across groups这个包第三个比较有用的功能就是可视化细胞比例。
png(filename =  'cell_fraction.png', width = 8, height = 4,units = 'in', res = 300)plot_cell_fraction(human_data,  celltypes = c("Macrophage","T cell","mDC","Neutrophil","Mast"), groupby = "group", show_replicate = T, rep_colname = "orig.ident")dev.off()

图片

当然了,以上三个功能是我觉得比较有用的,各取所需嘛,至于其他的感性的可自行探究。其实对于这种R包有很多,都是一些关于单细胞数据的可视化,他们的底层都是依赖于seurat的,但是修饰的效果不错。例如之前我们讲过一个:dittoSeq:一个优秀的单细胞转录组、Bulk可视化R包(都准备过年不学习了嘛?最近阅读好低)。虽然没有必要对所有包进行学习,但是这些有用的包却在我们的数据可视化过程中非常使用。如果我们不讲这个包,你可能就与这个炫酷的单细胞聚类图失之交臂了。所以看完了,对你有帮助的话,点个赞、分享下再走呗! 本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报。

上一篇:新挖的煤为什么要用水洗?洗煤的背后有我们不知道的秘密
下一篇:多大的放大镜,聚焦月光才能点燃纸张?