markrobinsonuzh / cytofWorkflow

MIT License
14 stars 3 forks source link

how to generate heatmap of clusters x markers, but just for a subset of data #36

Closed CNicholasMDA closed 9 months ago

CNicholasMDA commented 9 months ago

Hi, I'm using Workflow for cyTOF, and have a question about generating heatmaps using pheatmap. I ran clustering on a dataset that has two clinical timepoints in it. I want all the timepoints clustered together, but I want to generate separate "cluster vs antibody expression" heatmaps for Timepoint 1 and Timepoint 2 (and in future, with other subgroups defined in the metadata). How do I amend the below script in order to achieve this? Please let me know if you need more or different information. Thanks!

heatmap_clusters.pdf

----plot-clustering-heatmap1 (function)

plot_clustering_heatmap_wrapper <- function(expr, expr01, cell_clustering, color_clusters, cluster_merging = NULL){

Calculate the median expression, if column name starts with number, a “x” will be added to the name of each column and causes error for data matching. Panel renaming is necessary.

expr_median <- data.frame(expr, cell_clustering = cell_clustering) %>% group_by(cell_clustering) %>% summarize_all(funs(median)) expr01_median <- data.frame(expr01, cell_clustering = cell_clustering) %>% group_by(cell_clustering) %>% summarize_all(funs(median))

Sort the cell clusters with hierarchical clustering

clustering_table <- as.numeric(table(cell_clustering)) clustering_prop <- round(clustering_table / sum(clustering_table) * 100, 2)

Calculate cluster frequencies

d <- dist(expr_median[, colnames(expr)], method = "euclidean") cluster_rows <- hclust(d, method = "average")

expr_heat <- as.matrix(expr01_median[, colnames(expr01)]) rownames(expr_heat) <- expr01_median$cell_clustering

Colors for the heatmap

color_heat <- colorRampPalette(rev(brewer.pal(n = 9, name = "RdYlBu")))(100) legend_breaks = seq(from = 0, to = 1, by = 0.2) labels_row <- paste0(expr01_median$cell_clustering, " (", clustering_prop , "%)")

Annotation for the original clusters

annotation_row <- data.frame(Cluster = factor(expr01_median$cell_clustering)) rownames(annotation_row) <- rownames(expr_heat) color_clusters1 <- color_clusters[1:nlevels(annotation_row$Cluster)] names(color_clusters1) <- levels(annotation_row$Cluster) annotation_colors <- list(Cluster = color_clusters1)

Annotation for the merged clusters

if(!is.null(cluster_merging)){ cluster_merging$new_cluster <- factor(cluster_merging$new_cluster) annotation_row$Cluster_merging <- cluster_merging$new_cluster color_clusters2 <- color_clusters[1:nlevels(cluster_merging$new_cluster)] names(color_clusters2) <- levels(cluster_merging$new_cluster) annotation_colors$Cluster_merging <- color_clusters2 }

pheatmap(expr_heat, color = color_heat, cluster_cols = FALSE, cluster_rows = cluster_rows, labels_row = labels_row, display_numbers = TRUE, number_color = "black", fontsize = 8, fontsize_number = 3, legend_breaks = legend_breaks, annotation_row = annotation_row, annotation_colors = annotation_colors) }

heatmap of clusters

pdf(glue("./{out_dir}/heatmap_clusters.pdf")) plot_clustering_heatmap_wrapper(expr = expr[, lineage_markers_ord], expr01 = expr01[, lineage_markers_ord], cell_clustering = cell_clustering1, color_clusters = color_clusters) dev.off()

markrobinsonuzh commented 9 months ago

Hi @CNicholasMDA,

Just one thing of note is that if you are using plot_clustering_heatmap_wrapper(), then you have a very old version of the cytofWorkflow. So, I suggest to first look here:

https://master.bioconductor.org/packages/release/workflows/vignettes/cytofWorkflow/inst/doc/cytofWorkflow.html

And then, for the heatmaps, the function plotMultiHeatmap in the CATALYST package has really a lot of options. But ultimately, if it's difficult to create a heatmap that want with the functions, the thing to do is pull out the summaries that you want and call your favourite heatmap software on those.

Cheers, Mark

CNicholasMDA commented 9 months ago

Brilliant. Thank you. All of this makes sense. best wishes, Courtney