-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeatmap_Visualization_of_Marker_Genes.R
More file actions
84 lines (77 loc) · 2.89 KB
/
Heatmap_Visualization_of_Marker_Genes.R
File metadata and controls
84 lines (77 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
###################################################################################################
# Function: avg_exp_heatmap
# Purpose: Generate a heatmap of average gene expression (scaled) across cell states/types
# with top annotation highlighting cell-type categories of marker genes.
#
# Arguments:
# plot_obj - Seurat object
# markergene_plot - character vector of genes to plot
# cell_type_colors- named color vector for cell types
# pdfname - output filename for PDF
# top10 - dataframe of marker genes (must include columns: gene, cluster)
# heatmap_col - color scale for expression (default: blue-white-red)
# height, width - dimensions of PDF, defaults scale with # of cell states
###################################################################################################
avg_exp_heatmap <- function(
plot_obj,
markergene_plot,
cell_type_colors,
pdfname,
top10 = top10_unique_padded,
heatmap_col = colorRamp2(c(-1.5, 0, 1.5), c("#5484AF", "white", "#CE352E")),
height = length(unique(plot_obj$cellstate)),
width = length(unique(plot_obj$cellstate)) * 3
) {
# 1) Compute average expression per cluster/celltype
avg_exp_matrix <- AverageExpression(
plot_obj,
features = markergene_plot,
assays = "RNA"
)$RNA
# 2) Normalize expression: z-score per gene (rows)
avg_exp_matrix <- t(scale(t(avg_exp_matrix)))
# 3) Keep only requested genes (in specified order)
avg_exp_matrix <- avg_exp_matrix[markergene_plot, ]
# 4) Build gene annotation categories
# - Match marker genes to their assigned clusters
gene_categories_df <- top10[match(markergene_plot, top10$gene), ]
gene_categories <- factor(
gene_categories_df$cluster,
levels = names(cell_type_colors)
)
names(gene_categories) <- gene_categories_df$gene
# 5) Define top annotation (colored bars for cell-type categories)
top_annotation <- HeatmapAnnotation(
Gene_Category = gene_categories,
col = list(Gene_Category = cell_type_colors),
show_annotation_name = FALSE,
annotation_legend_param = list(
title = "Cell type",
title_gp = gpar(fontsize = 12)
)
)
# 6) Save numeric matrix for reuse
save(avg_exp_matrix, file = gsub(".pdf$", ".RData", pdfname))
# 7) Plot heatmap and export to PDF
pdf(pdfname, height = height, width = width)
p <- ComplexHeatmap::Heatmap(
t(avg_exp_matrix), # transpose so cells are on x-axis
name = "Expression",
col = heatmap_col,
cluster_rows = FALSE,
cluster_columns = FALSE,
show_row_dend = FALSE,
show_column_dend = FALSE,
show_row_names = FALSE,
show_column_names = TRUE,
column_names_rot = 90,
column_names_gp = gpar(fontsize = 12),
top_annotation = top_annotation,
heatmap_legend_param = list(
title = "Expression",
title_gp = gpar(fontsize = 12)
)
)
print(p)
dev.off()
}