# Load necessary libraries
pacman::p_load(Seurat, dplyr, ggplot2, ComplexHeatmap)
# Extract Marker Genes ----------------------------------------------------
{
# Load the Seurat object
seuObj <- readRDS("your_seurat_object.Rds")
# Preprocess the data
seuObj <- seuObj %>% NormalizeData() %>% FindVariableFeatures() %>% ScaleData()
# Load marker genes and filter based on criteria
marker_genes <- readr::read_csv("marker_genes.csv") %>% filter(avg_log2FC > 0.2, p_val_adj <= 0.01)
# Extract markers
topGenes <- marker_genes %>% filter(cluster == "celltype.A") %>% pull(gene) %>% unique()
}
# Create a heatmap data matrix --------------------------------------------
htData <- Seurat::DoHeatmap(seuObj, features = topGenes, group.by = "cluster")$data %>% tidyr::drop_na()
{
htData.mtx <- htData %>%
select(Feature, Cell, Expression) %>%
tidyr::pivot_wider(names_from = Cell, values_from = Expression, values_fill = NA) %>%
tibble::column_to_rownames("Feature")
colAnnot <- htData %>%
select(Cell, Identity) %>%
distinct(Cell, .keep_all = TRUE) %>%
mutate(Identity = as.vector(Identity), Cell = as.vector(Cell)) %>%
filter(Cell %in% colnames(htData.mtx)) %>%
arrange(Identity) %>%
tibble::column_to_rownames("Cell")
htData.mtx <- htData.mtx[, rownames(colAnnot)]
}
# Create and save the heatmap ---------------------------------------------
ht <- Heatmap(
as.matrix(htData.mtx),# Convert the data matrix to a matrix format for the heatmap
name = "Enhance Heatmap",# Name of the heatmap displayed in the legend
show_column_names = FALSE,# Do not display the column names (e.g., sample names)
row_names_side = 'left',# Display row names (e.g., gene names) on the left side
column_split = as.factor(colAnnot$Identity),# Split columns based on the 'Identity' factor from colAnnot
cluster_rows = TRUE,# Perform hierarchical clustering on the rows (genes)
show_row_dend = FALSE,# Do not display the dendrogram for rows
cluster_columns = TRUE,# Perform hierarchical clustering on the columns (samples)
cluster_column_slices = FALSE,# Do not perform clustering on column slices after splitting
show_column_dend = FALSE,# Do not display the dendrogram for columns
show_parent_dend_line = FALSE,# Do not show the lines of the parent dendrogram
top_annotation = HeatmapAnnotation(Idents = colAnnot$Identity, show_legend = FALSE),# Add a top annotation to the heatmap showing the identities, without a legend
col = colorRampPalette(c("#ec00ec", "#010001", "#fbfb00"))(n = 100),# Define the color palette for the heatmap, ranging from magenta to black to yellow
row_km = 5,# Split rows into 5 groups using k-means clustering
row_names_gp = gpar(fontsize = 8),# Set the font size for the row names
# Configure the heatmap legend with specific parameters
heatmap_legend_param = list(
at = c(-2, 0, 2), # Legend ticks at -2, 0, and 2
title = "Scaled.Expression", # Title of the legend
legend_height = unit(6, "cm"), # Height of the legend
title_position = "leftcenter-rot" # Position of the title
)
)
# Capture the heatmap 'ht' as a graphical object using 'grid.grabExpr' and store it in 'p'
p <- grid.grabExpr(draw(ht))
# Save the heatmap to a file
ggsave(filename = "heatmap_example.jpg", plot = p, width = 22, height = 12, dpi = 300)
Seurat::DoHeatmap Clustering Specific Genes
©著作权归作者所有,转载或内容合作请联系作者
禁止转载,如需转载请通过简信或评论联系作者。
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Compiled: February 08, 2021 Source: vignettes/pbmc3k_tuto...
- Seurat - Guided Clustering Tutorial Compiled: August 30, ...
- github地址:https://github.com/seandavi/awesome-single-cell这...