|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "id": "ea4f6f59-47f5-4890-9339-17d7be855b07", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "custom_heatmap = function(object,signaling = NULL, pattern = c(\"outgoing\",\"incoming\",\"all\"), slot.name = \"netP\",\n", |
| 11 | + " color.use = NULL, color.heatmap = 'Blues', pathway.col = c(\"Cell-Cell Contact\" = \"red\", \"ECM-Receptor\" = \"green\", \"Mixed\" = \"blue\",\n", |
| 12 | + " \"MK PTN family\" = \"yellow\",\"Secreted Signaling\" = \"orange\"),\n", |
| 13 | + " title = NULL, width = 8, height = 20, font.size = 8, font.size.title = 10,\n", |
| 14 | + " row_clusters = NULL, column_clusters = NULL, seed = 99){\n", |
| 15 | + " pattern <- match.arg(pattern)\n", |
| 16 | + " set.seed(seed)\n", |
| 17 | + " ##############################\n", |
| 18 | + " #######construct matrix#######\n", |
| 19 | + " ##############################\n", |
| 20 | + " centr <- slot(object, slot.name)$centr\n", |
| 21 | + " outgoing <- matrix(0, nrow = nlevels(object@idents), ncol = length(centr))\n", |
| 22 | + " incoming <- matrix(0, nrow = nlevels(object@idents), ncol = length(centr))\n", |
| 23 | + " dimnames(outgoing) <- list(levels(object@idents), names(centr))\n", |
| 24 | + " dimnames(incoming) <- dimnames(outgoing)\n", |
| 25 | + " for (i in 1:length(centr)) {\n", |
| 26 | + " outgoing[,i] <- centr[[i]]$outdeg\n", |
| 27 | + " incoming[,i] <- centr[[i]]$indeg\n", |
| 28 | + " }\n", |
| 29 | + " if (pattern == \"outgoing\") {\n", |
| 30 | + " mat <- t(outgoing)\n", |
| 31 | + " legend.name <- \"Outgoing\"\n", |
| 32 | + " } else if (pattern == \"incoming\") {\n", |
| 33 | + " mat <- t(incoming)\n", |
| 34 | + " legend.name <- \"Incoming\"\n", |
| 35 | + " } else if (pattern == \"all\") {\n", |
| 36 | + " mat <- t(outgoing+ incoming)\n", |
| 37 | + " legend.name <- \"Overall\"\n", |
| 38 | + " }\n", |
| 39 | + " if (!is.null(signaling)) {\n", |
| 40 | + " mat1 <- mat[rownames(mat) %in% signaling, , drop = FALSE]\n", |
| 41 | + " mat <- matrix(0, nrow = length(signaling), ncol = ncol(mat))\n", |
| 42 | + " idx <- match(rownames(mat1), signaling)\n", |
| 43 | + " mat[idx[!is.na(idx)], ] <- mat1\n", |
| 44 | + " dimnames(mat) <- list(signaling, colnames(mat1))\n", |
| 45 | + " }\n", |
| 46 | + " mat.ori <- mat\n", |
| 47 | + " mat <- sweep(mat, 1L, apply(mat, 1, max), '/', check.margin = FALSE)\n", |
| 48 | + " \n", |
| 49 | + " ##############################\n", |
| 50 | + " ###setup legend and colors####\n", |
| 51 | + " ##############################\n", |
| 52 | + " if (is.null(title)) {\n", |
| 53 | + " title <- paste0(legend.name, \" signaling patterns\")\n", |
| 54 | + " } else {\n", |
| 55 | + " title <- paste0(paste0(legend.name, \" signaling patterns\"), \" - \",title)\n", |
| 56 | + " }\n", |
| 57 | + " if (min(mat, na.rm = T) == max(mat, na.rm = T)) {\n", |
| 58 | + " legend.break <- max(mat, na.rm = T)\n", |
| 59 | + " } else {\n", |
| 60 | + " legend.break <- c(round(min(mat, na.rm = T), digits = 1), round(max(mat, na.rm = T), digits = 1))}\n", |
| 61 | + "\n", |
| 62 | + " #heatmap color\n", |
| 63 | + " color.heatmap.use = grDevices::colorRampPalette((RColorBrewer::brewer.pal(n = 9, name = color.heatmap)))(100)\n", |
| 64 | + " color.heatmap.use[1] = '#ffffff' #set 0 point to white\n", |
| 65 | + " \n", |
| 66 | + " ##############################\n", |
| 67 | + " #####heatmap annotations######\n", |
| 68 | + " ##############################\n", |
| 69 | + " ###Rows###\n", |
| 70 | + " #Left annotation\n", |
| 71 | + " library(dplyr)\n", |
| 72 | + " pathway_annotations = distinct(data.frame(pathway = cellchat@DB$interaction$pathway_name,\n", |
| 73 | + " annotation = cellchat@DB$interaction$annotation))\n", |
| 74 | + " pathway_annotations[pathway_annotations$pathway %in% \n", |
| 75 | + " pathway_annotations[duplicated(pathway_annotations$pathway),]$pathway,]$annotation = 'Mixed'\n", |
| 76 | + " pathway_annotations = distinct(pathway_annotations)\n", |
| 77 | + " pathway_annotations = pathway_annotations[match(row.names(mat),pathway_annotations$pathway),]\n", |
| 78 | + " rownames(pathway_annotations) <- NULL\n", |
| 79 | + " \n", |
| 80 | + " pathway.col = pathway.col[pathway_annotations$annotation]\n", |
| 81 | + " \n", |
| 82 | + " row_anno = HeatmapAnnotation(\"Signaling Type\" = pathway_annotations$annotation,\n", |
| 83 | + " col = list(\"Signaling Type\" = pathway.col),\n", |
| 84 | + " which = \"row\",\n", |
| 85 | + " show_legend = TRUE, show_annotation_name = FALSE,\n", |
| 86 | + " simple_anno_size = grid::unit(0.1, \"cm\"))\n", |
| 87 | + "\n", |
| 88 | + " #right annotation\n", |
| 89 | + " pSum <- rowSums(mat.ori)\n", |
| 90 | + " pSum.original <- pSum\n", |
| 91 | + " pSum <- -1/log(pSum)\n", |
| 92 | + " pSum[is.na(pSum)] <- 0\n", |
| 93 | + " idx1 <- which(is.infinite(pSum) | pSum < 0)\n", |
| 94 | + " if (length(idx1) > 0) {\n", |
| 95 | + " values.assign <- seq(max(pSum)*1.1, max(pSum)*1.5, length.out = length(idx1))\n", |
| 96 | + " position <- sort(pSum.original[idx1], index.return = TRUE)$ix\n", |
| 97 | + " pSum[idx1] <- values.assign[match(1:length(idx1), position)]\n", |
| 98 | + " }\n", |
| 99 | + " ha1 = rowAnnotation(Strength = anno_barplot(pSum, border = FALSE), show_annotation_name = FALSE)\n", |
| 100 | + "\n", |
| 101 | + " ###columns###\n", |
| 102 | + " if (is.null(color.use)) {\n", |
| 103 | + " color.use <- scPalette(length(colnames(mat)))\n", |
| 104 | + " }\n", |
| 105 | + " df<- data.frame(group = colnames(mat)); rownames(df) <- colnames(mat)\n", |
| 106 | + " names(color.use) <- colnames(mat)\n", |
| 107 | + "\n", |
| 108 | + " #bottom annotation\n", |
| 109 | + " col_annotation <- HeatmapAnnotation(df = df, col = list(group = color.use),which = \"column\",\n", |
| 110 | + " show_legend = FALSE, show_annotation_name = FALSE,\n", |
| 111 | + " simple_anno_size = grid::unit(0.2, \"cm\"))\n", |
| 112 | + " #top annotation\n", |
| 113 | + " ha2 = HeatmapAnnotation(Strength = anno_barplot(colSums(mat.ori), border = FALSE,gp = gpar(fill = color.use, col=color.use)), show_annotation_name = FALSE) \n", |
| 114 | + " \n", |
| 115 | + " \n", |
| 116 | + " ##############################\n", |
| 117 | + " #######plotting heatmap#######\n", |
| 118 | + " ##############################\n", |
| 119 | + " ht1 = Heatmap(mat, col = color.heatmap.use, na_col = \"white\", name = \"Relative strength\",\n", |
| 120 | + " right_annotation = ha1,\n", |
| 121 | + " left_annotation = row_anno,\n", |
| 122 | + " bottom_annotation = col_annotation, top_annotation = ha2,\n", |
| 123 | + " #cluster_rows=hclust(dist(mat.noNA),method = 'average'),\n", |
| 124 | + " #cluster_columns =hclust(dist(t(mat.noNA)),method = 'average'),\n", |
| 125 | + " row_km = row_clusters, row_km_repeats = 5,\n", |
| 126 | + " column_km = column_clusters, column_km_repeats = 5,\n", |
| 127 | + " border = TRUE,\n", |
| 128 | + " row_names_side = \"left\",row_names_rot = 0,row_names_gp = gpar(fontsize = font.size),column_names_gp = gpar(fontsize = font.size),\n", |
| 129 | + " width = unit(width, \"cm\"), height = unit(height, \"cm\"),\n", |
| 130 | + " column_title = title,column_title_gp = gpar(fontsize = font.size.title),column_names_rot = 90,\n", |
| 131 | + " heatmap_legend_param = list(title_gp = gpar(fontsize = 8, fontface = \"plain\"),title_position = \"leftcenter-rot\",\n", |
| 132 | + " border = NA, at = legend.break,\n", |
| 133 | + " legend_height = unit(20, \"mm\"),labels_gp = gpar(fontsize = 8),grid_width = unit(2, \"mm\"))\n", |
| 134 | + " )\n", |
| 135 | + " return(ht1)\n", |
| 136 | + " }" |
| 137 | + ] |
| 138 | + } |
| 139 | + ], |
| 140 | + "metadata": { |
| 141 | + "kernelspec": { |
| 142 | + "display_name": "cellchat", |
| 143 | + "language": "R", |
| 144 | + "name": "cellchat" |
| 145 | + }, |
| 146 | + "language_info": { |
| 147 | + "codemirror_mode": "r", |
| 148 | + "file_extension": ".r", |
| 149 | + "mimetype": "text/x-r-source", |
| 150 | + "name": "R", |
| 151 | + "pygments_lexer": "r", |
| 152 | + "version": "4.3.3" |
| 153 | + } |
| 154 | + }, |
| 155 | + "nbformat": 4, |
| 156 | + "nbformat_minor": 5 |
| 157 | +} |
0 commit comments