scCustomize:单细胞数据的自定义可视化(I)
简介
scCustomize
是一个单细胞转录组数据可视化的R包,里面集合了一些常用的数据可视化方法,可以与Seurat包进行很好的联用,支持Seurat,LIGER和SCE等常用对象的数据。
R包安装
直接使用devtools包进行安装
devtools::install_github(repo = "samuel-marsh/scCustomize")
remotes::install_github(repo = "samuel-marsh/scCustomize")
实例演示
在本教程中,我将使用来自 Marsh 等人 2020 ( bioRxiv ) 的小鼠小胶质细胞(图 1)marsh_mouse_micro
和人类死后的 snRNA-seq(图 3)marsh_human_pm
,以及SeuratData 包中的 pbmc3k 数据集进行实例演示。
加载示例数据
library(tidyverse)
library(patchwork)
library(viridis)
library(Seurat)
library(scCustomize)
library(qs)
# Load bioRxiv datasets
marsh_mouse_micro <- qread(file = "assets/marsh_2020_micro.qs")
marsh_human_pm <- qread(file = "assets/marsh_human_pm.qs")
# Load pbmc dataset
pbmc <- pbmc3k.SeuratData::pbmc3k.final
# add metadata
pbmc$sample_id <- sample(c("sample1", "sample2", "sample3", "sample4", "sample5", "sample6"), size = ncol(pbmc),
replace = TRUE)
pbmc$treatment <- sample(c("Treatment1", "Treatment", "Treatment3", "Treatment4"), size = ncol(pbmc),
replace = TRUE)
数据可视化
- 绘制高变异基因
scCustomize 包可以使用VariableFeaturePlot_scCustom()
函数绘制高度可变基因,同时提供了多个可用于自定义可视化的附加参数。
# Default scCustomize plot
VariableFeaturePlot_scCustom(seurat_object = marsh_mouse_micro, num_features = 20)
# Can remove labels if not desired
VariableFeaturePlot_scCustom(seurat_object = marsh_mouse_micro, num_features = 20, label = FALSE)
# Repel labels
VariableFeaturePlot_scCustom(seurat_object = marsh_mouse_micro, num_features = 20, repel = TRUE)
# Change the scale of y-axis from linear to log10
VariableFeaturePlot_scCustom(seurat_object = marsh_mouse_micro, num_features = 20, repel = TRUE,
y_axis_log = TRUE)
- 绘制PC主成分热图
为了便于评估 PCA 降维结果,scCustomize 包提供了PC_Plotting()
函数绘制 PC主成分热图和特征基因加载图。
PC_Plotting(seurat_object = marsh_mouse_micro, dim_number = 2)
- FeaturePlot
默认Seurat包的Seurat::FeaturePlot()
函数的绘图效果也很不错,在这里我们可以通过scCustomize包的FeaturePlot_scCustom()
函数默认设置的几种方式进行功能的增强。默认情况下,FeaturePlot_scCustom函数的参数order = TRUE是默认值,可以将高表达的细胞绘制在顶部。
# Set color palette
pal <- viridis(n = 10, option = "C", direction = -1)
# Create Plots
FeaturePlot(object = marsh_mouse_micro, features = "Jun")
FeaturePlot(object = marsh_mouse_micro, features = "Jun", order = T)
FeaturePlot(object = marsh_mouse_micro, features = "Jun", cols = pal, order = T)
# Set color palette
pal <- viridis(n = 10, option = "D")
# Create Plots
FeaturePlot_scCustom(seurat_object = marsh_mouse_micro, features = "Jun", order = F)
FeaturePlot_scCustom(seurat_object = marsh_mouse_micro, features = "Jun")
FeaturePlot_scCustom(seurat_object = marsh_mouse_micro, features = "Jun", colors_use = pal)
- Split Feature Plots
Seurat::FeaturePlot()
函数在按object@meta.data 中的变量进行拆分绘图时不再可以指定输出的列数,这使得可视化具有很多变量的对象变得困难,图形经常挤到一起。
FeaturePlot(object = marsh_mouse_micro, features = "P2ry12", split.by = "orig.ident")
FeaturePlot_scCustom 解决了这个问题,并允许设置 FeaturePlots 中的列数
FeaturePlot_scCustom(seurat_object = marsh_mouse_micro, features = "P2ry12", split.by = "sample_id",
num_columns = 4)
- Density Plots
Nebulosa包为通过密度图绘制基因表达提供了非常棒的功能。scCustomize 包提供了两个函数来扩展这些图的功能并便于绘制“联合”密度图。
自定义调色板
目前,Nebulosa 包仅支持使用 viridis 调色板中的5个画板:“viridis”、“magma”、“cividis”、“inferno”和“plasma”。Plot_Density_Custom()
函数将默认调色板更改为“magma”,还允许使用任何自定义渐变画板。
Plot_Density_Custom(seurat_object = marsh_mouse_micro, features = "Fos")
Plot_Density_Custom(seurat_object = marsh_mouse_micro, features = "Fos", custom_palette = PurpleAndYellow())
绘制联合密度图
使用Plot_Density_Joint_Only()
函数同时绘制多个基因的联合密度图。
Plot_Density_Joint_Only(seurat_object = marsh_mouse_micro, features = c("Fos", "Jun"))
- Dual Assay Plotting
当对象中同时含有多个assay的数据,我们可以使用FeaturePlot_DualAssay()
函数同时绘制不同assay中的数据。
cell_bender_example <- qread("assets/astro_nuc_seq.qs")
FeaturePlot_DualAssay(seurat_object = cell_bender_example, features = "Syt1", assay1 = "RAW", assay2 = "RNA")
- Stacked Violin Plots
与Seurat包的Seurat::VlnPlot()
函数相比,scCustomize 包提供了更美观的堆叠小提琴图绘制函数Stacked_VlnPlot()
。
gene_list_plot <- c("SLC17A7", "GAD2", "AQP4", "MYT1", "COL1A2", "CLDN5", "OPALIN", "CX3CR1", "CD3E")
human_colors_list <- c("dodgerblue", "navy", "forestgreen", "darkorange2", "darkorchid3", "orchid",
"orange", "gold", "gray")
# Create Plots
Stacked_VlnPlot(seurat_object = marsh_human_pm, features = gene_list_plot, x_lab_rotate = TRUE,
colors_use = human_colors_list)
绘制分割堆叠小提琴图
sample_colors <- c("dodgerblue", "forestgreen", "firebrick1")
# Create Plots
Stacked_VlnPlot(seurat_object = marsh_human_pm, features = gene_list_plot, x_lab_rotate = TRUE,
colors_use = sample_colors, split.by = "orig.ident")
调整垂直打印间距
根据绘制的基因数量和用户偏好,改变绘图之间的垂直间距可能会有所帮助。这可以使用plot_spacing
和spacing_unit
参数来完成。
# Default plot spacing (plot_spacing = 0.15 and spacing_unit = 'cm')
Stacked_VlnPlot(seurat_object = pbmc, features = c("CD3E", "CD14", "MS4A1", "FCER1A", "PPBP"), x_lab_rotate = TRUE)
# Double the space between plots
Stacked_VlnPlot(seurat_object = pbmc, features = c("CD3E", "CD14", "MS4A1", "FCER1A", "PPBP"), x_lab_rotate = TRUE,
plot_spacing = 0.3)
- Custom VlnPlots
除了Stacked_VlnPlot函数,scCustomize包也提供了VlnPlot_scCustom()
函数进行自定义可视化小提琴图。
VlnPlot_scCustom函数提供了自定义的内置画板
VlnPlot(object = pbmc, features = "PTPRC")
VlnPlot_scCustom(seurat_object = pbmc, features = "PTPRC")
- Custom DotPlots
scCustomize包的DotPlot_scCustom()
函数允许使用自定义渐变画板进行绘图。
micro_genes <- c("P2ry12", "Fcrls", "Trem2", "Tmem119", "Cx3cr1", "Hexb", "Tgfbr1", "Sparc", "P2ry13",
"Olfml3", "Adgrg1", "C1qa", "C1qb", "C1qc", "Csf1r", "Fcgr3", "Ly86", "Laptm5")
DotPlot(object = marsh_mouse_micro, features = micro_genes[1:6], cols = viridis_plasma_dark_high)
DotPlot_scCustom(seurat_object = marsh_mouse_micro, features = micro_genes[1:6], colors_use = viridis_plasma_dark_high)
DotPlot_scCustom()
函数还包含其他参数,以便更好地自定义绘制点图。
包括:
-
x_lab_rotate
旋转 x 轴文本, 默认值为 FALSE。 -
y_lab_rotate
旋转 y 轴文本, 默认值为 FALSE。 -
flip_axes
翻转坐标轴,默认为FALSE。 -
remove_axis_titles
删除 x 轴和 y 轴标签。默认为TRUE
DotPlot_scCustom(seurat_object = marsh_mouse_micro, features = micro_genes[1:6], x_lab_rotate = TRUE)
DotPlot_scCustom(seurat_object = marsh_mouse_micro, features = micro_genes[1:6], y_lab_rotate = TRUE)
DotPlot_scCustom(seurat_object = marsh_mouse_micro, features = micro_genes[1:6], flip_axes = T,
x_lab_rotate = TRUE)
DotPlot_scCustom(seurat_object = marsh_mouse_micro, features = micro_genes[1:6], flip_axes = T,
remove_axis_titles = FALSE)
- Clustered DotPlots
scCustomize 包还提供了一个单独的函数Clustered_DotPlot()
,可以绘制基因和样本的聚类点图。
all_markers <- FindAllMarkers(object = pbmc)
top5_markers <- Extract_Top_Markers(marker_dataframe = all_markers, num_genes = 5, named_vector = FALSE,
make_unique = TRUE)
Clustered_DotPlot(seurat_object = pbmc, features = top5_markers)
基因表达模式的聚类图
默认情况下Clustered_DotPlot
执行 k-means 聚类,k 值设置为 1。但是,用户可以更改此值以更好地可视化表达模式。
Clustered_DotPlot(seurat_object = pbmc, features = top5_markers, k = 9)
Clustered_DotPlot()
函数具有多个可选参数,用户可以根据所需的结果图修改不同可选参数。
- colors_use_exp 提供自定义色标
- exp_color_min/exp_color_max 可用于设置基因表达量刻度上的最小值和最大值。
- print_exp_quantiles 可以设置为 TRUE 以打印基因表达量分位数以帮助设置 new exp_color_min/exp_color_max。
- exp_color_middle 可用于更改用于色标中间的值。
- colors_use_idents 用于绘制的标识(列)的颜色。
- x_lab_rotate 逻辑或整数。默认值为 TRUE(45 度列标签旋转)。FALSE(0 度旋转)。自定义文本旋转值的整数。
- row_label_size 更改行文本(基因符号)的大小。
- raster 是否栅格化绘图(更快和更小的文件大小)。默认为FALSE。
Clustered_DotPlot(seurat_object = pbmc, features = top5_markers, k = 7, print_exp_quantiles = T)
#Quantiles of gene expression data are:
# 10% 50% 90% 99%
#-0.6555988 -0.3595223 1.7742718 2.6666597
在这里,我们可以根据此表达量分位数的数据范围设置最大和最小值,并更改要使用的色阶Seurat::PurpleAndYellow()
Clustered_DotPlot(seurat_object = pbmc, features = top5_markers, k = 7, exp_color_min = -1, exp_color_max = 2,
colors_use_exp = PurpleAndYellow())
- Split_FeatureScatter()
这是添加Seurat::FeatureScatter()
函数缺少的功能,在比较两个基因/特征或比较模块得分时,使用Split_FeatureScatter函数进行分割绘图可能非常有用。
# Create Plots
Split_FeatureScatter(seurat_object = marsh_mouse_micro, feature1 = "exAM_Score1", feature2 = "Microglia_Score1",
colors_use = mouse_colors, split.by = "Transcription_Method", group.by = "ident", num_columns = 2,
pt.size = 1)
- DimPlot_scCustom()
scCustomize 包的DimPlot_scCustom()
函数提供了一些新的自定义功能。
新的默认调色板DimPlot_scCustom
函数设置新的默认调色板:
- 如果少于或等于 36 个组,则将使用“多色”调色板。
- 如果超过 36 个组,“varibow”调色板将与
shuffle_pal = TRUE
. - 如果用户想使用 ggplot2 色调调色板然后设置参数
ggplot_default_colors = TRUE
。
DimPlot(object = marsh_mouse_over)
DimPlot_scCustom(seurat_object = marsh_mouse_over)
打散重叠的点
默认情况下,Seurat 的DimPlot()
函数将每个组的点都绘制在下一组之上,这会使绘图更难解释。DimPlot_scCustom
默认情况下设置shuffle = TRUE
,可以将重叠覆盖的点进行打散。
DimPlot(object = marsh_human_pm, group.by = "sample_id")
DimPlot_scCustom(seurat_object = marsh_human_pm, group.by = "sample_id")
拆分DimPlot
绘制拆分图时,Seurat::DimPlot()
函数通过根据指定的列数实现共享轴来简化坐标轴。
DimPlot(object = pbmc, split.by = "treatment")
DimPlot(object = pbmc, split.by = "sample_id", ncol = 4)
默认情况下,当split.by
与DimPlot_scCustom
布局一起使用时,每个绘图都会返回一个独立坐标轴,以便更轻松地可视化大量拆分。
DimPlot_scCustom(seurat_object = pbmc, split.by = "treatment", num_columns = 4, repel = TRUE)
还可以返回默认的 Seurat 分割图方法,同时DimPlot_scCustom
通过提供split_seurat = TRUE
参数。
DimPlot_scCustom(seurat_object = pbmc, split.by = "treatment", num_columns = 4, repel = TRUE, split_seurat = TRUE)
- Highlight Cluster(s)
scCustomize 包提供了Cluster_Highlight_Plot()
函数,可以突出显示一个或多个选定细胞群与其余细胞的分布,以突出显示它们在图上的位置。
Cluster_Highlight_Plot(seurat_object = marsh_mouse_over, cluster_name = "7", highlight_color = "navy",
background_color = "lightgray")
Cluster_Highlight_Plot(seurat_object = marsh_mouse_over, cluster_name = "8", highlight_color = "forestgreen",
background_color = "lightgray")
- Highlight Meta Data
scCustomize 包还提供了一个类似的函数Meta_Highlight_Plot()
,可以允许我们快速突出显示任何有效的metadata元数据信息。其中,元数据变量必须是“factor”, “character”, or “logical”类型,可以使用as.character
或as.factor
函数将其他类型数据进行转换。
Meta_Highlight_Plot(seurat_object = marsh_mouse_micro, meta_data_column = "Transcription_Method",
meta_data_highlight = "ENZYMATIC_NONE", highlight_color = "firebrick", background_color = "lightgray")
- DimPlot Layout Plots
使用DimPlot_All_Samples函数对指定变量进行拆分绘图。
DimPlot_All_Samples(seurat_object = pbmc, meta_data_column = "sample_id", num_col = 3, pt.size = 0.5)
可以通过提供颜色向量而不是单个值来为每个图进行着色。
DimPlot_All_Samples(seurat_object = marsh_mouse_micro, meta_data_column = "Transcription", num_col = 2,
pt.size = 0.5, color = c("firebrick3", "dodgerblue3"))
上一篇: 技能学习笔记
推荐阅读
-
35 岁实现财务*,腾讯程序员手握2300万提前退休?-1000万房产、1000万腾讯股票、加上300万的现金,一共2300万的财产。有网友算了一笔账,假设1000万的房产用于自住,剩下1300万资产按照平均税后20-50万不等进行计算,大约花上26-60年左右的时间才能赚到这笔钱。也就是说,普通人可能奋斗一辈子,才能赚到这笔钱。在很多人还在为中年危机而惶惶不可终日的时候,有的人的35岁,就已经安全着陆,试问哪个打工人不羡慕?但问题是有这样财富积累必然有像样的实力做靠山。没有人可以不劳而获。 看到这里,肯定有人说,那么对于普通人来说,卷可能真就成了唯一的出路。但是卷也有轻松的卷,“偷懒”的卷法,对于程序员而言,刨除掉一时无法改掉的开会传统占用的大部分时间,如何把有限的时间和精力放在真正重要的架构设计、需求设计上,而不是重复的造*,编码、改bug、手动测试。因此在科技改变生活的今天,学会使用AI工具成为程序员们的必备技能。 以全栈式全自动的软件开发工具飞算SoFlu软件机器人为例,作为全球首款面向微服务架构设计和最佳实践的软件机器人,SoFlu软件机器人改变了原来手工编码的作业模式,通过可视化拖拽方式以及参数配置就能实现等同于编写复杂代码的业务逻辑,在设计业务逻辑时就完成了微服务应用开发,做到“业务即图,图即代码”,实现“软件开发,十倍提效”。 而作为飞算SoFlu软件机器人的一个重要组成部分,FuncGPT(慧函数)支持所有类型函数创建。通过自然语言描述Java函数需求,实时生成高质量、高可读性的Java函数代码。生成代码可直接复制到IDEA,或一键导入Java全自动开发工具函数库。 FuncGPT(慧函数)具有五大核心能力: ● 自然语言:通过自然语言即可生成函数,降低软件开发门槛。 ● 秒级函数开发:全自动秒级开发函数,秒级完成,效率千倍提升。 ● 一站式函数开发应用:函数生成即可直接应用,效率、安全有保障。 ● 质量可靠:生成的函数代码符合业界规范、具备良好的可读性,符合最优解。 ● 灵活生成:可以根据开发人员的具体需求生成并支持调整和修改。 以“将文件流转换为File对象”这个需求为例,以下是FuncGPT(慧函数)秒级生成的代码: / 类名:FileConverter // 函数名:convertStreamToFile // 函数功能:将文件流转换为File对象 // POM依赖包:无 import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; public class FileConverter { /** * 将文件流转换为File对象 * @param inputStream 文件流 * @param filePath 文件路径 * @param fileName 文件名 * @return 转换后的File对象 * @throws IOException 如果发生I/O异常 */ public static File convertStreamToFile(InputStream inputStream, String filePath, String fileName) throws IOException { File file = new File(filePath + File.separator + fileName); // 创建File对象 try (FileOutputStream outputStream = new FileOutputStream(file)) { // 创建文件输出流 byte buffer = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { // 从文件流读取数据并写入文件 outputStream.write(buffer, 0, bytesRead); } } return file; // 返回转换后的File对象 } } // 函数示例 // 将文件流转换为File对象示例 // 入参:inputStream,文件流 // 入参:filePath,文件路径 // 入参:fileName,文件名 // 出参:file,转换后的File对象 // 调用示例: // InputStream inputStream = new FileInputStream("example.txt"); // String filePath = "C:\\Users\\User\\Documents"; // String fileName = "example.txt"; // File file = FileConverter.convertStreamToFile(inputStream, filePath, fileName); // System.out.println(file.getAbsolutePath); // 输出结果:例如,将文件流转换为File对象后,文件的绝对路径为:C:\Users\User\Documents\example.txt // 则输出结果为:C:\Users\User\Documents\example.txt 通过分析,不难发现以上代码:
-
scCustomize:单细胞数据的自定义可视化(I)
-
一图胜千言:数据可视化的不完全总结(I)
-
基于 PyMongo 的 MongoDB 实用 (I) 电影评论分析 | 数据结果的可视化显示和分析 | 评论词云 | 时间段分析
-
用于单细胞转录组的 Seurat 软件包 - 数据过滤、缩放和可视化
-
玩转Java底层:JMX详解 - jconsole与自定义MBean监控工具的实际应用与区别" 在日常JVM调优中,我们熟知的jconsole工具通过JMX包装的bean以图形化形式展示管理数据,而像jstat和jmap这类内建监控工具则由JVM直接支持。本文将以jconsole为例,深入讲解其实质——基于JMX的MBean功能,包括可视化界面上的bean属性查看和操作调用。 MBeans在jconsole中的体现是那些可观察的组件属性和方法,如上图所示,通过名为"Verbose"的属性能看到其值为false,同时还能直接操作该bean的方法,例如"closeJerryMBean"。 尽管jconsole给我们提供了直观的可视化界面,但请注意,这里的MBean并非固定不变,开发者可根据JMX提供的接口将自己的自定义bean展示到jconsole。以下步骤展示了如何创建并注册一个名为"StudyJavaMBean"的自定义MBean: 1. 首先定义接口`StudyJavaMBean`,接口需遵循MBean规范,即后缀为"MBean"且包含getter方法代表属性,如`getApplicationName`,和无返回值的setter方法代表操作,如`closeJerryMBean`。 ```java public interface StudyJavaMBean { String getApplicationName(); void closeJerryMBean(); } ``` 2. 编写接口的实现类`StudyJavaMBeanImpl`,实现接口中的方法: ```java public class StudyJavaMBeanImpl implements StudyJavaMBean { @Override public String getApplicationName() { return "每天学Java"; } @Override public void closeJerryMBean() { System.out.println("关闭Jerry应用"); } } ``` 3. 在代码中注册自定义MBean,涉及的关键步骤包括: - 获取平台MBeanServer - 定义ObjectName,指定唯一的MBean标识符 - 注册MBean到服务器 - 启动RMI连接器服务,以便jconsole能够访问 ```java public void registerMBean() throws Exception { // ... 具体实现省略 ... } ``` 实际运行注册后的MBean,您将在jconsole中发现并查看自定义bean的属性和调用相关方法。然而,这种方式相较于传统的属性/日志查看和HTTP接口,实用性相对有限,可能存在潜在的安全风险。但不可否认的是,JMX及其MBean机制对于获取操作系统信息、内存状态等关键性能指标仍然具有重要价值。例如: 1. **获取操作系统信息**:通过JMX MBean,可以直接获取到诸如CPU使用率、操作系统版本等系统级信息,这对于资源管理和优化工作具有显著帮助。
-
如何在seaborn的jointplot中自定义绘制2D KDE图?—— 实用的数据可视化技巧
-
plot1cell:优雅的可视化你的单细胞数据