├── .Rbuildignore ├── .gitignore ├── DESCRIPTION ├── LICENSE ├── LICENSE.md ├── NAMESPACE ├── R ├── irGSEA.barplot.R ├── irGSEA.bubble.R ├── irGSEA.density.scatterplot.R ├── irGSEA.densityheatmap.R ├── irGSEA.halfvlnplot.R ├── irGSEA.heatmap.R ├── irGSEA.hub.R ├── irGSEA.integrate.R ├── irGSEA.merge.R ├── irGSEA.ridgeplot.R ├── irGSEA.score.R ├── irGSEA.subset.R ├── irGSEA.upset.R ├── irGSEA.vlnplot.R ├── utils-pipe.R └── utils-tidy-eval.R ├── README.Rmd ├── README.md ├── _pkgdown.yml ├── docs ├── 404.html ├── LICENSE-text.html ├── LICENSE.html ├── authors.html ├── bootstrap-toc.css ├── bootstrap-toc.js ├── docsearch.css ├── docsearch.js ├── index.html ├── link.svg ├── pkgdown.css ├── pkgdown.js ├── pkgdown.yml └── reference │ ├── Rplot001.png │ ├── figures │ ├── README-unnamed-chunk-10-1.png │ ├── README-unnamed-chunk-11-1.png │ ├── README-unnamed-chunk-12-1.png │ ├── README-unnamed-chunk-13-1.png │ ├── README-unnamed-chunk-14-1.png │ ├── README-unnamed-chunk-3-1.png │ ├── README-unnamed-chunk-6-1.png │ ├── README-unnamed-chunk-7-1.png │ ├── README-unnamed-chunk-8-1.png │ └── README-unnamed-chunk-9-1.png │ ├── index.html │ ├── irGSEA.barplot.html │ ├── irGSEA.bubble.html │ ├── irGSEA.density.scatterplot.html │ ├── irGSEA.densityheatmap.html │ ├── irGSEA.halfvlnplot.html │ ├── irGSEA.heatmap.html │ ├── irGSEA.integrate.html │ ├── irGSEA.ridgeplot.html │ ├── irGSEA.score.html │ ├── irGSEA.upset.html │ └── pipe.html ├── inst └── python │ ├── gsva.py │ ├── ssgsea.py │ └── viper.py ├── irGSEA.Rproj └── man ├── figures ├── Abstrast.jpg ├── README-unnamed-chunk-10-1.png ├── README-unnamed-chunk-11-1.png ├── README-unnamed-chunk-12-1.png ├── README-unnamed-chunk-13-1.png ├── README-unnamed-chunk-14-1.png ├── README-unnamed-chunk-15-1.png ├── README-unnamed-chunk-16-1.png ├── README-unnamed-chunk-17-1.png ├── README-unnamed-chunk-18-1.png ├── README-unnamed-chunk-19-1.png ├── README-unnamed-chunk-19-2.png ├── README-unnamed-chunk-20-1.png ├── README-unnamed-chunk-20-2.png ├── README-unnamed-chunk-3-1.png ├── README-unnamed-chunk-5-1.png ├── README-unnamed-chunk-6-1.png ├── README-unnamed-chunk-7-1.png ├── README-unnamed-chunk-8-1.png ├── README-unnamed-chunk-9-1.png ├── figure3.png └── figure4.png ├── irGSEA.barplot.Rd ├── irGSEA.bubble.Rd ├── irGSEA.density.scatterplot.Rd ├── irGSEA.densityheatmap.Rd ├── irGSEA.halfvlnplot.Rd ├── irGSEA.heatmap.Rd ├── irGSEA.hub.Rd ├── irGSEA.integrate.Rd ├── irGSEA.merge.Rd ├── irGSEA.ridgeplot.Rd ├── irGSEA.score.Rd ├── irGSEA.subset.Rd ├── irGSEA.upset.Rd ├── irGSEA.vlnplot.Rd ├── pipe.Rd └── tidyeval.Rd /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | ^LICENSE\.md$ 4 | ^README\.Rmd$ 5 | ^_pkgdown\.yml$ 6 | ^docs$ 7 | ^pkgdown$ 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # docs 2 | .Rproj.user 3 | .Rhistory 4 | .Rdata 5 | .httr-oauth 6 | .DS_Store 7 | # docs 8 | docs 9 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: irGSEA 2 | Type: Package 3 | Title: The integration of single cell rank-based gene set enrichment analysis 4 | Version: 3.3.2 5 | Author: Chuiqin Fan 6 | Author@R: person("Chuiqin", "Fan", role = c('aut', 'cre'), email = '19cqfan@alumni.stu.edu.cn') 7 | Maintainer: Chuiqin Fan <19cqfan@stu.edu.cn> 8 | Description: The integration of single cell rank-based gene set enrichment analysis. 9 | Meanwhile, you are easy to visualize the results. 10 | License: MIT + file LICENSE 11 | Encoding: UTF-8 12 | LazyData: true 13 | RoxygenNote: 7.2.3 14 | Imports: 15 | AUCell, 16 | ComplexHeatmap, 17 | decoupleR, 18 | dplyr, 19 | gghalves, 20 | ggplotify, 21 | ggridges, 22 | ggsci, 23 | ggtree, 24 | GSVA, 25 | magrittr, 26 | msigdbr, 27 | Nebulosa, 28 | rlang (>= 0.4.11), 29 | RobustRankAggreg, 30 | Seurat, 31 | singscore, 32 | stringr, 33 | plyr, 34 | UCell 35 | URL: https://github.com/chuiqin/irGSEA 36 | BugReports: https://github.com/chuiqin/irGSEA/issues 37 | Suggests: 38 | aplot, 39 | BiocManager, 40 | BiocParallel, 41 | circlize, 42 | cowplot, 43 | devtools, 44 | data.table, 45 | doParallel, 46 | doRNG, 47 | fgsea, 48 | gficf, 49 | ggplot2, 50 | grDevices, 51 | ggfun, 52 | GSEABase, 53 | irlba, 54 | Matrix, 55 | methods, 56 | pagoda2, 57 | pointr, 58 | purrr, 59 | RcppML, 60 | readr, 61 | reshape2, 62 | reticulate, 63 | roxygen2, 64 | RMTstat, 65 | scde, 66 | SeuratDisk, 67 | SeuratObject, 68 | SummarizedExperiment, 69 | tidytree, 70 | tidyr, 71 | tibble, 72 | tidyselect, 73 | VAM, 74 | VISION, 75 | viper, 76 | sargent, 77 | sparseMatrixStats 78 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2021 2 | COPYRIGHT HOLDER: Chuiqin Fan 3 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) 2021 Chuiqin Fan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export("%>%") 4 | export(":=") 5 | export(.data) 6 | export(as_label) 7 | export(as_name) 8 | export(enquo) 9 | export(enquos) 10 | export(irGSEA.barplot) 11 | export(irGSEA.bubble) 12 | export(irGSEA.density.scatterplot) 13 | export(irGSEA.densityheatmap) 14 | export(irGSEA.halfvlnplot) 15 | export(irGSEA.heatmap) 16 | export(irGSEA.hub) 17 | export(irGSEA.integrate) 18 | export(irGSEA.merge) 19 | export(irGSEA.ridgeplot) 20 | export(irGSEA.score) 21 | export(irGSEA.subset) 22 | export(irGSEA.upset) 23 | export(irGSEA.vlnplot) 24 | importFrom(magrittr,"%>%") 25 | importFrom(rlang,":=") 26 | importFrom(rlang,.data) 27 | importFrom(rlang,as_label) 28 | importFrom(rlang,as_name) 29 | importFrom(rlang,enquo) 30 | importFrom(rlang,enquos) 31 | -------------------------------------------------------------------------------- /R/irGSEA.barplot.R: -------------------------------------------------------------------------------- 1 | 2 | #' Stacked bar plot 3 | #' 4 | #' Easy to show analysis results by stacked bar plot 5 | #' 6 | #' @param object A list after perform \code{\link{irGSEA.integrate}} 7 | #' @param method A vector. It should be one or more of the followling : AUCell, 8 | #' UCell, singscore, ssgsea or RRA. Default all methods when it is set to NULL. 9 | #' @param significance.color A vector. Default "c("#D0DFE6FF","#f87669")" when 10 | #' it is set to NULL. 11 | #' @param color.cluster A vector. Default "ggsci::pal_igv()(the number of colnames 12 | #' of enrichment score matrix)" when it is set to NULL. 13 | #' @param color.method A vector. Default "ggsci::pal_igv()(the number of methods 14 | #' )" when it is set to NULL. 15 | #' @param cluster.levels A vector equal to the number of clusters. 16 | #' 17 | #' 18 | #' @return stacked bar plot 19 | #' @export 20 | #' 21 | #' @examples 22 | #' \dontrun{ 23 | #' # load PBMC dataset by R package SeuratData 24 | #' library(Seurat) 25 | #' library(SeuratData) 26 | #' # download 3k PBMCs from 10X Genomics 27 | #' InstallData("pbmc3k") 28 | #' data("pbmc3k.final") 29 | #' pbmc3k.final <- SeuratObject::UpdateSeuratObject(pbmc3k.final) 30 | #' 31 | #' # Seurat object 32 | #' pbmc3k.final <- irGSEA.score(object = pbmc3k.final, assay = "RNA", 33 | #' slot = "data", msigdb = T, species = "Homo sapiens", 34 | #' category = "H", geneid = "symbol", 35 | #' method = c("AUCell", "UCell", "singscore", "ssgsea"), kcdf = 'Gaussian') 36 | #' 37 | #' # Integrated analysis 38 | #' result.dge <- irGSEA.integrate(object = pbmc3k.final, 39 | #' group.by = "seurat_annotations", metadata = NULL, col.name = NULL, 40 | #' method = c("AUCell","UCell","singscore","ssgsea")) 41 | #' 42 | #' irGSEA.barplot.plot1 <- irGSEA.barplot(object = result.dge) 43 | #' 44 | #' } 45 | #' 46 | irGSEA.barplot <- function(object = NULL, method = NULL, 47 | significance.color = NULL, 48 | color.cluster = NULL, color.method = NULL, 49 | cluster.levels = NULL){ 50 | # pretreatment 51 | if (! purrr::is_list(object)) { 52 | stop("object should be a list.") 53 | } 54 | if (! all(method %in% names(object))) { 55 | stop("`method` should be one or more of the followling : AUCell, UCell, singscore, ssgsea, JASMINE, VAM, scSE, VISION, gficf, GSVA, zscore, plage, wmean, wsum, mdt, viper, GSVApy, AddModuleScore, pagoda2, RRA.") 56 | } 57 | cluster <- NULL 58 | direction <- NULL 59 | pvalue <- NULL 60 | cell <- NULL 61 | Name <- NULL 62 | geneset <- NULL 63 | proportion <- NULL 64 | anno.cluster <- NULL 65 | anno.method <- NULL 66 | 67 | if (purrr::is_null(method)){ method <- names(object) } 68 | 69 | for (i in method) { 70 | if (i != "RRA"){ 71 | object[i] <- object[i] %>% purrr::map( ~.x %>% dplyr::rename(pvalue = p_val_adj)) 72 | } 73 | } 74 | 75 | 76 | # matrix 77 | sig.genesets.barplot <- list() 78 | for (i in seq_along(names(object[method]))) { 79 | sig.genesets.barplot[[i]] <- object[method][[names(object[method])[i]]] %>% 80 | dplyr::mutate(cell = stringr::str_c(cluster, direction, sep = "_")) %>% 81 | dplyr::select(c("Name", "pvalue", "cell")) %>% 82 | dplyr::mutate(pvalue = dplyr::if_else(pvalue < 0.05, "significant","no significant")) %>% 83 | tidyr::spread(cell, pvalue, fill = "no significant") %>% 84 | tidyr::gather(cell, pvalue, -Name) %>% 85 | dplyr::mutate(direction = stringr::str_extract(cell, pattern = "up|down"), 86 | cluster = stringr::str_remove(cell, pattern = "_up|_down"), 87 | method = names(object[method])[i]) %>% 88 | dplyr::mutate(geneset = dplyr::if_else(pvalue == "no significant", "no significant", direction)) 89 | } 90 | 91 | sig.genesets.barplot <- do.call(rbind, sig.genesets.barplot) %>% 92 | dplyr::group_by(cluster, method, geneset) %>% 93 | dplyr::summarise(proportion = dplyr::n()) %>% 94 | dplyr::mutate(cell = paste0(cluster, "_", method), 95 | method = factor(method, levels = names(object))) %>% 96 | dplyr::arrange(cluster, method) %>% 97 | dplyr::mutate(cell = factor(cell, levels = unique(cell))) %>% 98 | dplyr::filter(method %in% tidyselect::all_of(method)) 99 | 100 | # set levels of cluster 101 | if (! purrr::is_null(cluster.levels)) { 102 | sig.genesets.barplot <- sig.genesets.barplot %>% 103 | dplyr::mutate(cluster = factor(cluster, levels = cluster.levels)) %>% 104 | dplyr::arrange(cluster) %>% 105 | dplyr::mutate(cell = factor(cell, levels = unique(cell))) 106 | } 107 | 108 | # set color 109 | if (purrr::is_null(significance.color)) { 110 | significance.color <- c("#4575B4","#D0DFE6FF","#D73027") 111 | } 112 | # middle plot 113 | barplot.middle <- ggplot2::ggplot(sig.genesets.barplot , ggplot2::aes(cell, proportion, fill = geneset))+ 114 | ggplot2::geom_bar(stat = "identity", position = "fill")+ 115 | ggplot2::theme_bw()+ 116 | ggplot2::guides(fill = ggplot2::guide_legend(title = "Significance"))+ 117 | ggplot2::scale_fill_manual(values = significance.color)+ 118 | ggplot2::theme(panel.grid = ggplot2::element_blank(), 119 | axis.ticks.x = ggplot2::element_blank(), 120 | axis.text.x = ggplot2::element_blank(), 121 | axis.title.x = ggplot2::element_blank())+ 122 | ggplot2::ylab("Proportion") + 123 | ggplot2::scale_y_continuous(expand = c(0,0)) 124 | # set color 125 | significance.down.up.color <- c(significance.color[1],significance.color[3]) 126 | 127 | # above color 128 | barplot.above <- sig.genesets.barplot %>% 129 | dplyr::filter(geneset != "no significant") %>% 130 | ggplot2::ggplot(ggplot2::aes(cell, proportion, fill = geneset)) + 131 | ggplot2::geom_bar(stat = "identity", colour = "white") + 132 | ggplot2::theme_classic() + 133 | ggplot2::theme(panel.grid = ggplot2::element_blank(), 134 | axis.ticks.x = ggplot2::element_blank(), 135 | axis.text.x = ggplot2::element_blank(), 136 | axis.title.x = ggplot2::element_blank(), 137 | legend.position = "none") + 138 | ggplot2::ylab("Count") + 139 | ggplot2::scale_fill_manual(values = significance.down.up.color) + 140 | ggplot2::geom_text(ggplot2::aes(label = proportion), color = "white", 141 | position = ggplot2::position_stack(vjust = 0.5), size = 3) 142 | # set color 143 | if (purrr::is_null(color.cluster)) { 144 | color.cluster <- ggsci::pal_igv()(length(unique(sig.genesets.barplot$cluster))) 145 | } 146 | # below picture 147 | labels.cluster <- sig.genesets.barplot %>% 148 | dplyr::mutate(anno.cluster = "Cluster") %>% 149 | ggplot2::ggplot(ggplot2::aes(cell, y = anno.cluster, fill = cluster)) + 150 | ggplot2::geom_tile() + 151 | ggplot2::scale_fill_manual(values = color.cluster, name = "Cluster") + 152 | ggplot2::scale_y_discrete(position = "right") + 153 | ggplot2::theme_minimal() + 154 | ggplot2::theme(axis.text.x = ggplot2::element_blank(), 155 | axis.ticks.x = ggplot2::element_blank(), 156 | panel.grid = ggplot2::element_blank()) + 157 | ggplot2::labs(x = NULL, y = NULL) 158 | # set color 159 | if (purrr::is_null(color.method)) { 160 | color.method <- ggsci::pal_igv()(length(unique(sig.genesets.barplot$method))) 161 | } 162 | # below picture 163 | labels.method <- sig.genesets.barplot %>% 164 | dplyr::mutate(anno.method = "Method") %>% 165 | ggplot2::ggplot(ggplot2::aes(cell, y = anno.method, fill = method)) + 166 | ggplot2::geom_tile() + 167 | ggplot2::scale_fill_manual(values = color.method, name = "Method") + 168 | ggplot2::scale_y_discrete(position = "right") + 169 | ggplot2::theme_minimal() + 170 | ggplot2::theme(axis.text.x = ggplot2::element_blank(), 171 | axis.ticks.x = ggplot2::element_blank(), 172 | panel.grid = ggplot2::element_blank()) + 173 | ggplot2::labs(x = NULL, y = NULL) 174 | # combine all plots 175 | sig.genesets.bar.plot <- barplot.middle %>% 176 | aplot::insert_top(barplot.above, height = 1) %>% 177 | aplot::insert_bottom(labels.method, height = .1) %>% 178 | aplot::insert_bottom(labels.cluster, height = .1) 179 | sig.genesets.bar.plot <- ggplotify::as.ggplot(sig.genesets.bar.plot) 180 | return(sig.genesets.bar.plot) 181 | 182 | } 183 | 184 | 185 | 186 | -------------------------------------------------------------------------------- /R/irGSEA.bubble.R: -------------------------------------------------------------------------------- 1 | #' Bubble plot 2 | #' 3 | #' Easy to show analysis results by bubble plot 4 | #' 5 | #' @param object A list after perform \code{\link{irGSEA.integrate}} 6 | #' @param method A character. It should be one of the followling : AUCell, UCell, 7 | #' singscore, ssgsea or RRA. Default RRA. 8 | #' @param top The top gene sets. Default 50. 9 | #' @param show.geneset A vector including special gene sets. Default NULL. 10 | #' @param cluster.color A vector. Default "ggsci::pal_igv()(the number of colnames 11 | #' of enrichment score matrix)" when it is set to NULL. 12 | #' @param direction.color A vector. Default "c("#4575B4","#D73027")" when it 13 | #' is set to NULL. 14 | #' @param significance.color A vector. Default "c("#D0DFE6FF","#f87669")" when 15 | #' it is set to NULL. 16 | #' @param cluster_rows Whether to make cluster on rows. Defaul True. 17 | #' @param cluster.levels A vector equal to the number of clusters. 18 | #' 19 | #' @return bubble plot 20 | #' @export 21 | #' 22 | #' @examples 23 | #' \dontrun{ 24 | #' # load PBMC dataset by R package SeuratData 25 | #' library(Seurat) 26 | #' library(SeuratData) 27 | #' # download 3k PBMCs from 10X Genomics 28 | #' InstallData("pbmc3k") 29 | #' data("pbmc3k.final") 30 | #' pbmc3k.final <- SeuratObject::UpdateSeuratObject(pbmc3k.final) 31 | #' 32 | #' # Seurat object 33 | #' pbmc3k.final <- irGSEA.score(object = pbmc3k.final, assay = "RNA", 34 | #' slot = "data", msigdb = T, species = "Homo sapiens", 35 | #' category = "H", geneid = "symbol", 36 | #' method = c("AUCell", "UCell", "singscore", "ssgsea"), kcdf = 'Gaussian') 37 | #' 38 | #' # Integrated analysis 39 | #' result.dge <- irGSEA.integrate(object = pbmc3k.final, 40 | #' group.by = "seurat_annotations", metadata = NULL, col.name = NULL, 41 | #' method = c("AUCell","UCell","singscore","ssgsea")) 42 | #' 43 | #' irGSEA.bubble.plot1 <- irGSEA.bubble(object = result.dge, 44 | #' method = "RRA", top = 50) 45 | #' irGSEA.bubble.plot2 <- irGSEA.bubble(object = result.dge, 46 | #' method = "ssgsea", top = 50) 47 | #' 48 | #' } 49 | #' 50 | #' 51 | #' 52 | irGSEA.bubble <- function(object = NULL, method = "RRA", 53 | top = 50, show.geneset = NULL, 54 | cluster.color = NULL, direction.color = NULL, 55 | significance.color = NULL, 56 | cluster_rows = T, cluster.levels = NULL){ 57 | # pretreatment 58 | cluster <- NULL 59 | direction <- NULL 60 | cell <- NULL 61 | pvalue <- NULL 62 | Name <- NULL 63 | significance <- NULL 64 | anno.cluster <- NULL 65 | anno.direction <- NULL 66 | value <- NULL 67 | 68 | if (! purrr::is_list(object)) { 69 | stop("object should be a list.") 70 | } 71 | if ((! all(method %in% names(object))) | (length(method) > 1) | (purrr::is_null(method))) { 72 | stop("`method` should be one of the followling : AUCell, UCell, singscore, ssgsea, JASMINE, VAM, scSE, VISION, gficf, GSVA, zscore, plage, wmean, wsum, mdt, viper, GSVApy, AddModuleScore, pagoda2, RRA.") 73 | } 74 | pvalue <- NULL 75 | if (method %in% names(object)[! names(object) == "RRA"]) { 76 | object[method] <- object[method] %>% purrr::map( ~.x %>% dplyr::rename(pvalue = p_val_adj)) 77 | } 78 | 79 | # matrix 80 | 81 | sig.genesets.bubble <- object[[method]] %>% 82 | dplyr::mutate(cell = stringr::str_c(cluster, direction, sep = "_")) %>% 83 | dplyr::select(c("Name", "pvalue", "cell")) %>% 84 | dplyr::mutate(pvalue = dplyr::case_when( pvalue < 0.0001 ~ "< 0.0001", 85 | pvalue < 0.001 ~ "< 0.001", 86 | pvalue < 0.01 ~ "< 0.01", 87 | pvalue < 0.05 ~ "< 0.05", 88 | pvalue >= 0.05 ~ ">= 0.05", 89 | TRUE ~ NA_character_)) %>% 90 | dplyr::mutate(Name = factor(Name, levels = unique(Name))) %>% 91 | tidyr::spread(cell, pvalue, fill = ">= 0.05") %>% 92 | tibble::column_to_rownames(var = "Name") 93 | 94 | 95 | if (length(unique(object[[method]]$cluster)) != 0.5*ncol(sig.genesets.bubble)) { 96 | cell.name <- c(stringr::str_c(unique(object[[method]]$cluster), c("up"), sep = "_"), 97 | stringr::str_c(unique(object[[method]]$cluster), c("down"), sep = "_")) 98 | cell.name <- cell.name[!cell.name %in% colnames(sig.genesets.bubble)] 99 | for (i in cell.name) { 100 | sig.genesets.bubble <- sig.genesets.bubble %>% 101 | dplyr::mutate(!!rlang::sym(i):= ">= 0.05") 102 | sig.genesets.bubble <- sig.genesets.bubble[, sort(colnames(sig.genesets.bubble))] 103 | } 104 | 105 | } 106 | 107 | # top rows or custom genesets 108 | if (purrr::is_null(show.geneset)) { 109 | sig.genesets.bubble <- sig.genesets.bubble %>% dplyr::slice_head(n = top) 110 | }else{ 111 | sig.genesets.bubble <- sig.genesets.bubble[rownames(sig.genesets.bubble) %in% show.geneset, ] 112 | sig.genesets.bubble <- sig.genesets.bubble[intersect(show.geneset, rownames(sig.genesets.bubble)), ] 113 | 114 | if (purrr::is_null(sig.genesets.bubble)) { 115 | stop("All genesets of `show.geneset` are not in the `method`.") 116 | } 117 | if (! all(show.geneset %in% rownames(sig.genesets.bubble))) { 118 | a <- show.geneset[! show.geneset %in% rownames(sig.genesets.bubble)] 119 | message(paste0("Some genesets of `show.geneset` are not in the `method` : ",a)) 120 | } 121 | } 122 | 123 | # continue to edit matrix 124 | sig.genesets.bubble <- sig.genesets.bubble %>% 125 | tibble::rownames_to_column(var = "Name") %>% 126 | tidyr::gather(cell, pvalue, -Name) %>% 127 | dplyr::mutate(direction = stringr::str_extract(cell, pattern = "up|down"), 128 | anno.direction = "Direction", 129 | cluster = stringr::str_remove(cell, pattern = "_up|_down"), 130 | anno.cluster = "Cluster", 131 | pvalue = factor(pvalue, levels = rev(levels(factor(pvalue)))), 132 | significance = dplyr::if_else(pvalue == ">= 0.05", "no significant", "significant")) %>% 133 | dplyr::mutate(value = dplyr::if_else(significance == "no significant", 0, 1)) 134 | 135 | # set levels of cluster 136 | if (! purrr::is_null(cluster.levels)) { 137 | sig.genesets.bubble <- sig.genesets.bubble %>% 138 | dplyr::mutate(cluster = factor(cluster, levels = cluster.levels)) %>% 139 | dplyr::arrange(cluster) %>% 140 | dplyr::mutate(cell = factor(cell, levels = unique(cell))) 141 | } 142 | 143 | 144 | 145 | # set color 146 | if (purrr::is_null(cluster.color)) { 147 | cluster.color <- ggsci::pal_igv()(length(unique(sig.genesets.bubble$cluster))) 148 | } 149 | # above picture 150 | labels.cluster <- sig.genesets.bubble %>% 151 | ggplot2::ggplot(ggplot2::aes(cell, y = anno.cluster, fill = cluster)) + 152 | ggplot2::geom_tile() + 153 | ggplot2::scale_fill_manual(values = cluster.color, name = "Cluster") + 154 | ggplot2::scale_y_discrete(position = "right") + 155 | ggplot2::theme_minimal() + 156 | ggplot2::theme(axis.text.x = ggplot2::element_blank(), 157 | axis.ticks.x = ggplot2::element_blank(), 158 | panel.grid = ggplot2::element_blank()) + 159 | ggplot2::labs(x = NULL, y = NULL) 160 | 161 | # set color 162 | if (purrr::is_null(direction.color)) { 163 | direction.color <- c("#4575B4","#D73027") 164 | } 165 | # above picture 166 | labels.direction <- sig.genesets.bubble %>% 167 | ggplot2::ggplot(ggplot2::aes(cell, y = anno.direction, fill = direction)) + 168 | ggplot2::geom_tile() + 169 | ggplot2::scale_fill_manual(values = direction.color, name = "Direction") + 170 | ggplot2::scale_y_discrete(position = "right") + 171 | ggplot2::theme_minimal() + 172 | ggplot2::theme(axis.text.x = ggplot2::element_blank(), 173 | axis.ticks.x = ggplot2::element_blank(), 174 | panel.grid = ggplot2::element_blank()) + 175 | ggplot2::labs(x = NULL, y = NULL) 176 | 177 | # set color 178 | if (purrr::is_null(significance.color)) { 179 | significance.color <- c("#D0DFE6FF","#f87669") 180 | } 181 | # middle picture 182 | bubble.plot <- ggplot2::ggplot(sig.genesets.bubble, ggplot2::aes(x = cell, y = Name))+ 183 | ggplot2::geom_point(ggplot2::aes(size = pvalue, color = significance))+ 184 | ggplot2::scale_color_manual(values = significance.color, name = method)+ 185 | ggplot2::theme_bw()+ 186 | ggplot2::theme(panel.grid = ggplot2::element_blank(), 187 | axis.text.x = ggplot2::element_blank(), 188 | axis.ticks.x = ggplot2::element_blank(), 189 | axis.text.y = ggplot2::element_text(size = 8))+ 190 | ggplot2::labs(x=NULL,y=NULL)+ 191 | ggplot2::guides(size = ggplot2::guide_legend(title = "P Value")) + 192 | ggplot2::scale_size_manual(values = c(1,1.5,2,2.5,3)) 193 | 194 | # bulid ggtree matrix 195 | sig.genesets.bubble.matrix <- sig.genesets.bubble %>% 196 | dplyr::select(c(Name, cell, value)) %>% 197 | dplyr::mutate(Name = factor(Name, levels = unique(Name))) %>% 198 | tidyr::spread(cell, value) %>% 199 | tibble::column_to_rownames(var = "Name") 200 | 201 | # row tree 202 | phr <- ggtree::ggtree(stats::hclust(stats::dist(sig.genesets.bubble.matrix))) 203 | # combine tree 204 | if (cluster_rows == T) { 205 | sig.genesets.bubble.plot <- bubble.plot %>% 206 | aplot::insert_left(phr, width=.1) %>% 207 | aplot::insert_top(labels.direction, height = .05) %>% 208 | aplot::insert_top(labels.cluster, height = .05) 209 | }else{ 210 | sig.genesets.bubble.plot <- bubble.plot %>% 211 | aplot::insert_top(labels.direction, height = .05) %>% 212 | aplot::insert_top(labels.cluster, height = .05) 213 | } 214 | sig.genesets.bubble.plot <- ggplotify::as.ggplot(sig.genesets.bubble.plot) 215 | return(sig.genesets.bubble.plot) 216 | 217 | } 218 | 219 | -------------------------------------------------------------------------------- /R/irGSEA.density.scatterplot.R: -------------------------------------------------------------------------------- 1 | #' Density Scatter plot 2 | #' 3 | #' Easy to the data distribution by density scatter plot 4 | #' 5 | #' @param object A Seurat after perform \code{\link{irGSEA.score}} 6 | #' @param method A character. It should be one of the followling : AUCell, 7 | #' UCell, singscore, ssgsea. 8 | #' @param show.geneset A character. It should be one of the rownames of 9 | #' enrichment score matrix. 10 | #' @param reduction A character. It can not be empty and should be calculated 11 | #' in advance. 12 | #' @param ... More parameters pass to \code{\link[Nebulosa]{plot_density}} 13 | #' 14 | #' @return density scatter plot 15 | #' @export 16 | #' 17 | #' @examples 18 | #' \dontrun{ 19 | #' # load PBMC dataset by R package SeuratData 20 | #' library(Seurat) 21 | #' library(SeuratData) 22 | #' # download 3k PBMCs from 10X Genomics 23 | #' InstallData("pbmc3k") 24 | #' data("pbmc3k.final") 25 | #' pbmc3k.final <- SeuratObject::UpdateSeuratObject(pbmc3k.final) 26 | #' 27 | #' # Seurat object 28 | #' pbmc3k.final <- irGSEA.score(object = pbmc3k.final, assay = "RNA", 29 | #' slot = "data", msigdb = T, species = "Homo sapiens", 30 | #' category = "H", geneid = "symbol", 31 | #' method = c("AUCell", "UCell", "singscore", "ssgsea"), kcdf = 'Gaussian') 32 | #' 33 | #' irGSEA.density.scatterplot1 <- irGSEA.density.scatterplot(object = pbmc3k.final, 34 | #' method = "UCell", show.geneset = "HALLMARK-INFLAMMATORY-RESPONSE", 35 | #' reduction = "umap") 36 | #' irGSEA.density.scatterplot2 <- irGSEA.density.scatterplot(object = pbmc3k.final, 37 | #' method = "ssgsea", show.geneset = "HALLMARK-IL6-JAK-STAT3-SIGNALING", 38 | #' reduction = "umap") 39 | #' 40 | #' } 41 | #' 42 | #' 43 | irGSEA.density.scatterplot <- function(object = NULL, method = NULL, 44 | show.geneset = NULL, reduction = "umap", 45 | ...){ 46 | # pretreatment 47 | if ((! all(method %in% Seurat::Assays(object))) | (length(method) > 1) | (purrr::is_null(method))) { 48 | stop("`method` should be one of the followling : AUCell, UCell, singscore, ssgsea, JASMINE, VAM, scSE, VISION, gficf, GSVA, zscore, plage, wmean, wsum, mdt, viper, GSVApy, AddModuleScore, pagoda2.") 49 | } 50 | 51 | if ((! all(reduction %in% Seurat::Reductions(object))) | (length(reduction) > 1) | (purrr::is_null(reduction))) { 52 | stop("`reductions` can not be empty and should be calculated in advance.") 53 | } 54 | 55 | # geneset 56 | if (purrr::is_null(show.geneset)) { 57 | stop("`show.geneset` can not be empty.") 58 | }else{ 59 | custom.geneset <- show.geneset[show.geneset %in% rownames(object[[method]])] 60 | if (purrr::is_null(custom.geneset)) { 61 | stop("All genesets of `show.geneset` are not in the `method`.") 62 | } 63 | if (! all(show.geneset %in% rownames(object[[method]]))) { 64 | a <- show.geneset[! show.geneset %in% rownames(object[[method]])] 65 | message(paste0("Following genesets of `show.geneset` are not in such `method` : ",a)) 66 | } 67 | } 68 | 69 | # plot 70 | SeuratObject::DefaultAssay(object) <- method 71 | scores.scatterplot <- Nebulosa::plot_density(object, 72 | features = custom.geneset, 73 | slot = "scale.data", 74 | reduction = reduction, 75 | method = "wkde", 76 | joint = T, 77 | ...) + 78 | ggplot2::ggtitle(paste0(method, ": ", custom.geneset))+ 79 | ggplot2::theme(plot.title = ggplot2::element_text(size = 10, hjust = 0.5), 80 | axis.title = ggplot2::element_text(size = 10)) 81 | 82 | return(scores.scatterplot) 83 | 84 | } 85 | -------------------------------------------------------------------------------- /R/irGSEA.densityheatmap.R: -------------------------------------------------------------------------------- 1 | #' Density heatmap 2 | #' 3 | #' Easy to show the data distribution by density heatmap 4 | #' 5 | #' @param object A Seurat after perform \code{\link{irGSEA.score}} 6 | #' @param method A character. It should be one of the followling : AUCell, 7 | #' UCell, singscore, ssgsea. 8 | #' @param show.geneset A character. It should be one of the rownames of 9 | #' enrichment score matrix. 10 | #' @param group.by Default ident when it is set to NULL. You can specify other 11 | #' column of metadata. 12 | #' @param heatmap_width Width of the whole heatmap (including heatmap 13 | #' components), default 12. 14 | #' @param heatmap_height Heigh of the whole heatmap (including heatmap 15 | #' components), default 12. 16 | #' @param cluster.levels A vector equal to the number of clusters. 17 | #' @param ... More parameters pass to \code{\link[ComplexHeatmap]{densityHeatmap}} 18 | #' 19 | #' @return density heatmap 20 | #' @export 21 | #' 22 | #' @examples 23 | #' 24 | #' \dontrun{ 25 | #' # load PBMC dataset by R package SeuratData 26 | #' library(Seurat) 27 | #' library(SeuratData) 28 | #' # download 3k PBMCs from 10X Genomics 29 | #' InstallData("pbmc3k") 30 | #' data("pbmc3k.final") 31 | #' pbmc3k.final <- SeuratObject::UpdateSeuratObject(pbmc3k.final) 32 | #' 33 | #' # Seurat object 34 | #' pbmc3k.final <- irGSEA.score(object = pbmc3k.final, assay = "RNA", 35 | #' slot = "data", msigdb = T, species = "Homo sapiens", 36 | #' category = "H", geneid = "symbol", 37 | #' method = c("AUCell", "UCell", "singscore", "ssgsea"), kcdf = 'Gaussian') 38 | #' 39 | #' irGSEA.densityheatmap.plot1 <- irGSEA.densityheatmap(object = pbmc3k.final, 40 | #' method = "UCell", show.geneset = "HALLMARK-INFLAMMATORY-RESPONSE") 41 | #' irGSEA.densityheatmap.plot2 <- irGSEA.densityheatmap(object = pbmc3k.final, 42 | #' method = "ssgsea", show.geneset = "HALLMARK-IL6-JAK-STAT3-SIGNALING") 43 | #' 44 | #' } 45 | #' 46 | irGSEA.densityheatmap <- function(object = NULL, method = NULL, 47 | show.geneset = NULL, group.by = NULL, 48 | heatmap_width = 12, heatmap_height = 12, 49 | cluster.levels = NULL, ...){ 50 | # pretreatment 51 | ident <- NULL 52 | if ((! all(method %in% Seurat::Assays(object))) | (length(method) > 1) | (purrr::is_null(method))) { 53 | stop("`method` should be one of the followling : AUCell, UCell, singscore, ssgsea, JASMINE, VAM, scSE, VISION, gficf, GSVA, zscore, plage, wmean, wsum, mdt, viper, GSVApy, AddModuleScore, pagoda2.") 54 | } 55 | # group 56 | if (purrr::is_null(group.by)) { 57 | anno.ident <- SeuratObject::Idents(object) 58 | }else{ 59 | object <- SeuratObject::SetIdent(object, value = group.by) 60 | anno.ident <- SeuratObject::Idents(object) 61 | } 62 | # factors are sorted alphabetically 63 | anno.ident <- as.factor(as.character(anno.ident)) 64 | # set levels of cluster 65 | if (! purrr::is_null(cluster.levels)) { 66 | anno.ident <- factor(anno.ident, levels = cluster.levels) 67 | } 68 | SeuratObject::Idents(object) <- anno.ident 69 | 70 | # geneset 71 | if (purrr::is_null(show.geneset)) { 72 | stop("`show.geneset` can not be empty.") 73 | }else{ 74 | custom.geneset <- show.geneset[show.geneset %in% rownames(object[[method]])] 75 | if (purrr::is_null(custom.geneset)) { 76 | stop("All genesets of `show.geneset` are not in the `method`.") 77 | } 78 | if (! all(show.geneset %in% rownames(object[[method]]))) { 79 | a <- show.geneset[! show.geneset %in% rownames(object[[method]])] 80 | message(paste0("Following genesets of `show.geneset` are not in such `method` : ",a)) 81 | } 82 | } 83 | 84 | # plot 85 | scores.densityheatmap <- Seurat::VlnPlot(object = object, 86 | assay = method, 87 | slot = "data", 88 | group.by = group.by, 89 | features = custom.geneset, 90 | pt.size = 0, 91 | fill.by = "ident") 92 | scores.densityheatmap <- scores.densityheatmap$data %>% 93 | dplyr::group_split(ident, .keep = F) %>% 94 | purrr::set_names(levels(scores.densityheatmap$data$ident)) %>% 95 | purrr::map(~ .x %>% dplyr::pull(.)) 96 | 97 | 98 | scores.densityheatmap <- ComplexHeatmap::densityHeatmap(scores.densityheatmap, 99 | title = paste0(method,"'s Distribution"), 100 | ylab = custom.geneset, 101 | title_gp = grid::gpar(fontsize = 9), 102 | ylab_gp = grid::gpar(fontsize = 8), 103 | tick_label_gp = grid::gpar(fontsize = 9), 104 | column_names_gp = grid::gpar(fontsize = 9), 105 | column_names_rot = 45, 106 | show_heatmap_legend = T, 107 | heatmap_width = grid::unit(heatmap_width, "cm"), 108 | heatmap_height = grid::unit(heatmap_height, "cm"), 109 | ...) 110 | scores.densityheatmap <- ggplotify::as.ggplot( 111 | grid::grid.grabExpr(ComplexHeatmap::draw(scores.densityheatmap))) 112 | 113 | return(scores.densityheatmap) 114 | 115 | } 116 | 117 | 118 | -------------------------------------------------------------------------------- /R/irGSEA.halfvlnplot.R: -------------------------------------------------------------------------------- 1 | #' Half vlnplot 2 | #' 3 | #' Easy to show the data distribution by half vlnplot 4 | #' 5 | #' @param object A Seurat after perform \code{\link{irGSEA.score}} 6 | #' @param method A character. It should be one of the followling : AUCell, 7 | #' UCell, singscore, ssgsea. 8 | #' @param show.geneset A character. It should be one of the rownames of 9 | #' enrichment score matrix. 10 | #' @param group.by Default ident when it is set to NULL. You can specify other 11 | #' column of metadata. 12 | #' @param color.cluster A vector. Default "ggsci::pal_igv()(the number of colnames 13 | #' of enrichment score matrix)" when it is set to NULL. 14 | #' @param cluster.levels A vector equal to the number of clusters. 15 | #' 16 | #' @return half vlnplot 17 | #' @export 18 | #' 19 | #' @examples 20 | #' \dontrun{ 21 | #' # load PBMC dataset by R package SeuratData 22 | #' library(Seurat) 23 | #' library(SeuratData) 24 | #' # download 3k PBMCs from 10X Genomics 25 | #' InstallData("pbmc3k") 26 | #' data("pbmc3k.final") 27 | #' pbmc3k.final <- SeuratObject::UpdateSeuratObject(pbmc3k.final) 28 | #' 29 | #' # Seurat object 30 | #' pbmc3k.final <- irGSEA.score(object = pbmc3k.final, assay = "RNA", 31 | #' slot = "data", msigdb = T, species = "Homo sapiens", 32 | #' category = "H", geneid = "symbol", 33 | #' method = c("AUCell", "UCell", "singscore", "ssgsea"), kcdf = 'Gaussian') 34 | #' 35 | #' irGSEA.halfvlnplot.plot1 <- irGSEA.halfvlnplot(object = pbmc3k.final, 36 | #' method = "UCell", show.geneset = "HALLMARK-INFLAMMATORY-RESPONSE") 37 | #' irGSEA.halfvlnplot.plot2 <- irGSEA.halfvlnplot(object = pbmc3k.final, 38 | #' method = "ssgsea", show.geneset = "HALLMARK-IL6-JAK-STAT3-SIGNALING") 39 | #' 40 | #' 41 | #' } 42 | #' 43 | irGSEA.halfvlnplot <- function(object = NULL, method = NULL, 44 | show.geneset = NULL, group.by = NULL, 45 | color.cluster = NULL, cluster.levels = NULL){ 46 | # pretreatment 47 | ident <- NULL 48 | geneset <- NULL 49 | if ((! all(method %in% Seurat::Assays(object))) | (length(method) > 1) | (purrr::is_null(method))) { 50 | stop("`method` should be one of the followling : AUCell, UCell, singscore, ssgsea, JASMINE, VAM, scSE, VISION, gficf, GSVA, zscore, plage, wmean, wsum, mdt, viper, GSVApy, AddModuleScore, pagoda2.") 51 | } 52 | 53 | # group 54 | if (purrr::is_null(group.by)) { 55 | anno.ident <- SeuratObject::Idents(object) 56 | }else{ 57 | object <- SeuratObject::SetIdent(object, value = group.by) 58 | anno.ident <- SeuratObject::Idents(object) 59 | } 60 | # factors are sorted alphabetically 61 | anno.ident <- as.factor(as.character(anno.ident)) 62 | # set levels of cluster 63 | if (! purrr::is_null(cluster.levels)) { 64 | anno.ident <- factor(anno.ident, levels = cluster.levels) 65 | } 66 | SeuratObject::Idents(object) <- anno.ident 67 | 68 | # set colors 69 | if (purrr::is_null(color.cluster)) { 70 | color.cluster <- ggsci::pal_igv()(length(levels(object))) 71 | } 72 | 73 | # geneset 74 | if (purrr::is_null(show.geneset)) { 75 | stop("`show.geneset` can not be empty.") 76 | }else{ 77 | custom.geneset <- show.geneset[show.geneset %in% rownames(object[[method]])] 78 | if (purrr::is_null(custom.geneset)) { 79 | stop("All genesets of `show.geneset` are not in the `method`.") 80 | } 81 | if (! all(show.geneset %in% rownames(object[[method]]))) { 82 | a <- show.geneset[! show.geneset %in% rownames(object[[method]])] 83 | message(paste0("Following genesets of `show.geneset` are not in such `method` : ",a)) 84 | } 85 | } 86 | 87 | # plot 88 | scores.vlnplot <- Seurat::VlnPlot(object = object, 89 | assay = method, 90 | slot = "data", 91 | group.by = group.by, 92 | cols = color.cluster, 93 | features = custom.geneset, 94 | pt.size = 0, 95 | fill.by = "ident") + 96 | ggplot2::theme(axis.title.x = ggplot2::element_blank())+ 97 | ggplot2::guides(fill = ggplot2::guide_legend(title = "Cluster")) 98 | 99 | scores.vlnplot <- scores.vlnplot$data 100 | scores.vlnplot <- scores.vlnplot %>% 101 | dplyr::rename(c("geneset" = tidyselect::all_of(custom.geneset))) %>% 102 | ggplot2::ggplot(ggplot2::aes(x = ident, y = geneset, fill = ident), colour = "white") + 103 | gghalves::geom_half_boxplot(side = "r", outlier.color = NA,errorbar.draw = TRUE)+ 104 | gghalves::geom_half_violin() + 105 | ggplot2::theme_classic() + 106 | ggplot2::scale_fill_manual(values = color.cluster)+ 107 | ggplot2::ylab(paste0(method, " scores")) + 108 | ggplot2::xlab("") + 109 | ggplot2::ggtitle(custom.geneset) + 110 | ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5), 111 | title = ggplot2::element_text(size =12), 112 | axis.text.x = ggplot2::element_text(vjust = 0.5, hjust = 0.5, angle = 45))+ 113 | ggplot2::guides(fill = ggplot2::guide_legend(title = "Cluster")) 114 | 115 | return(scores.vlnplot) 116 | 117 | } 118 | 119 | 120 | -------------------------------------------------------------------------------- /R/irGSEA.hub.R: -------------------------------------------------------------------------------- 1 | #' Calculate the hub gene of the geneset 2 | #' 3 | #' Easy to calculate the hub gene of the geneset based on the correlation between 4 | #' the geneset's score and the expression or rank of gene included in the geneset 5 | #' 6 | #' @param object A Seurat after perform \code{\link{irGSEA.score}} 7 | #' @param assay Name of assay to calculate the correction. Default RNA. 8 | #' @param slot Default data. 9 | #' @param method A character. 10 | #' @param show.geneset A character. 11 | #' @param ncores Default 4. 12 | #' @param type expression or rank. Default rank. Calculate the correlation between 13 | #' the geneset's score and the rank of gene included in the geneset while the 14 | #' type is rank. Calculate the correlation between the geneset's score and the 15 | #' expression of gene included in the geneset while the type is expression. 16 | #' @param maxRank Maximum number of genes to rank per cell; above this rank, 17 | #' a given gene is considered as not expressed. Default 2000. 18 | #' @param top The number of top-ranked positively correlated genes in each method 19 | #' is displayed in a heatmap. 20 | #' @param correlation.color A vector. 21 | #' @param method.color A vector. Default "ggsci::pal_png()(the number of 22 | #' methods)" when it is set to NULL. 23 | #' @return list includes hub_result and hub_plot 24 | #' @export 25 | #' 26 | #' @examples 27 | #' \dontrun{ 28 | #' # load PBMC dataset by R package SeuratData 29 | #' library(Seurat) 30 | #' library(SeuratData) 31 | #' # download 3k PBMCs from 10X Genomics 32 | #' InstallData("pbmc3k") 33 | #' data("pbmc3k.final") 34 | #' pbmc3k.final <- SeuratObject::UpdateSeuratObject(pbmc3k.final) 35 | #' 36 | #' # Seurat object 37 | #' pbmc3k.final <- irGSEA.score(object = pbmc3k.final, assay = "RNA", 38 | #' slot = "data", msigdb = T, species = "Homo sapiens", 39 | #' category = "H", geneid = "symbol", 40 | #' method = c("AUCell", "UCell", "singscore", "ssgsea"), kcdf = 'Gaussian') 41 | #' 42 | #' hub.result <- irGSEA.hub(object = pbmc3k.final, assay = "RNA", slot = "data", 43 | #' method = c("AUCell","UCell","singscore", "ssgsea"), 44 | #' show.geneset = c("HALLMARK-INFLAMMATORY-RESPONSE", "HALLMARK-APOPTOSIS"), 45 | #' ncores = 4, type = "rank", maxRank = 2000, top = 5, 46 | #' correlation.color = c("#0073c2","white","#efc000"), method.color = NULL) 47 | #' 48 | #' head(hub.result$hub_result) 49 | #' hub.result$hub_plot$`HALLMARK-APOPTOSIS` 50 | #' hub.result$hub_plot$`HALLMARK-INFLAMMATORY-RESPONSE` 51 | #' 52 | #' 53 | #' } 54 | #' 55 | irGSEA.hub <- function(object = NULL, assay = "RNA", slot = "data", 56 | method = NULL, show.geneset = NULL, 57 | ncores = 4, type = "rank", maxRank = 2000, top = 5, 58 | correlation.color = c("#0073c2","white","#efc000"), 59 | method.color = NULL){ 60 | 61 | method.data <- lapply(method, function(i){ 62 | message(i) 63 | # method 64 | tryCatch({ 65 | 66 | # All the genesets are not on the assay 67 | if (all(show.geneset %in% rownames(object[[i]])) == F) { 68 | message(paste0("All the genesets: ", show.geneset, " are not on the assay: ", 69 | i, ".")) 70 | return(NULL) 71 | } 72 | 73 | show.geneset.before <- length(show.geneset) 74 | show.geneset.new <- show.geneset[show.geneset %in% rownames(object[[i]])] 75 | show.geneset.after <- length(show.geneset) 76 | 77 | # Some genesets are not on the assay 78 | if (show.geneset.before > show.geneset.after) { 79 | message(paste0("Some genesets: ", show.geneset[!show.geneset %in% show.geneset.new], 80 | " are not on the assay: ", i, ".")) 81 | } 82 | 83 | # geneset matrix 84 | geneset.data <- SeuratObject::GetAssayData(object[[i]], slot = "scale.data")[show.geneset.new, , drop=F] 85 | 86 | cor.geneset <- lapply(show.geneset.new, function(x){ 87 | message(x) 88 | 89 | # the target gene of geneset 90 | if (class(object[[i]])[1] == "Assay5") { 91 | gene <- object[[i]]@meta.data 92 | rownames(gene) <- rownames(object[[i]]) 93 | gene <- gene[x, "target.gene"] 94 | }else{ 95 | gene <- object[[i]]@meta.features[x, "target.gene"] 96 | } 97 | 98 | # if target gene of geneset is null 99 | if (gene == "") { 100 | message(paste0("Please check the column `target.gene` in meta.data or meta.features of", 101 | " the geneset `", x, "` in the assay `", i, "`. It maybe empty.")) 102 | return(NULL) 103 | } 104 | 105 | gene <- stringr::str_remove_all(gene, pattern = "\\+$|-$") 106 | gene <- stringr::str_split(gene, pattern = ", ")[[1]] 107 | 108 | # gene expression matrix or gene rank matrix 109 | if (type == "rank") { 110 | expression.data <- UCell::StoreRankings_UCell(matrix = SeuratObject::GetAssayData(object[[assay]], slot = slot), 111 | maxRank = maxRank, 112 | ncores = ncores) 113 | expression.data <- as.matrix(expression.data)[gene, , drop=F] 114 | }else{ 115 | expression.data <- SeuratObject::GetAssayData(object[[assay]], slot = slot)[gene, , drop=F] 116 | } 117 | 118 | # calcute correlation 119 | cor.list <- lapply(gene, function(k){ 120 | #message(k) 121 | test <- stats::cor.test(as.numeric(expression.data[k, ]), 122 | as.numeric(geneset.data[x, ]), 123 | type = "spearman") 124 | cor.data <- data.frame(method = i, 125 | geneset = x, 126 | gene = k, 127 | correlation = test$estimate, 128 | p.value = test$p.value) 129 | return(cor.data) 130 | }) 131 | cor.list <- do.call(rbind, cor.list) 132 | return(cor.list) 133 | 134 | }) 135 | cor.geneset <- do.call(rbind, cor.geneset) 136 | return(cor.geneset) 137 | 138 | }, error = function(e) { 139 | cat("Error: ", conditionMessage(e), "\n") 140 | }) 141 | 142 | }) 143 | method.data <- do.call(rbind, method.data) 144 | method.data$geneset <- factor(method.data$geneset, levels = show.geneset) 145 | method.data$method <- factor(method.data$method, levels = method) 146 | geneset <- NULL 147 | method.data2 <- method.data %>% 148 | dplyr::group_split(geneset, .keep = T) %>% 149 | purrr::set_names(levels(method.data$geneset)) 150 | 151 | # draw 152 | method.data2 <- lapply(method.data2, function(method.data3){ 153 | 154 | 155 | method <- NULL 156 | Module <- NULL 157 | correlation <- NULL 158 | p.value <- NULL 159 | gene <- NULL 160 | pvalue <- NULL 161 | Variable <- NULL 162 | Value <- NULL 163 | text <- NULL 164 | Method <- NULL 165 | 166 | method.data3 <- method.data3 %>% 167 | dplyr::group_by(method) %>% 168 | dplyr::arrange(dplyr::desc(correlation)) %>% 169 | dplyr::slice_head(n = top) 170 | 171 | method.data3 <- method.data3 %>% 172 | dplyr::mutate(pvalue = p.value, Value = correlation, 173 | Variable = gene, Module = method) %>% 174 | dplyr::mutate(pvalue = dplyr::case_when(pvalue < 1e-04 ~ "****", 175 | pvalue < 0.001 ~ "***", 176 | pvalue < 0.01 ~ "**", 177 | pvalue <= 0.05 ~ "*", 178 | pvalue > 0.05 ~ "", 179 | TRUE ~ NA_character_)) 180 | 181 | method.data3$Value <- round(method.data3$Value, 2) 182 | method.data3$text <- NULL 183 | # text 184 | for (i in 1:nrow(method.data3)) { 185 | if (method.data3$pvalue[i]=="") { 186 | method.data3$text[i] <- method.data3$Value[i] 187 | }else{ 188 | method.data3$text[i] <- paste0(method.data3$Value[i], "\n", "(", method.data3$pvalue[i], ")" ) 189 | } 190 | 191 | } 192 | 193 | 194 | # plot 195 | p.heatmap <- ggplot2::ggplot(method.data3, ggplot2::aes(x = Module, y = Variable, fill = Value)) + 196 | ggplot2::geom_tile(color = "black") + 197 | ggplot2::geom_text(ggplot2::aes(label = text), color = "black", show.legend = T)+ 198 | ggplot2::scale_fill_gradientn(colours = grDevices::colorRampPalette(correlation.color)(100))+ 199 | ggplot2::theme(panel.background = ggplot2::element_blank(), 200 | panel.grid.major = ggplot2::element_blank(), 201 | panel.grid.minor = ggplot2::element_blank(), 202 | axis.ticks = ggplot2::element_blank(), 203 | axis.text.x.bottom = ggplot2::element_blank())+ 204 | ggplot2::xlab("")+ 205 | ggplot2::ylab("")+ 206 | ggplot2::labs(fill = "Correlation")+ 207 | Seurat::NoLegend() 208 | 209 | # label 210 | if (is.null(method.color)) { 211 | method.color <- ggsci::pal_npg()(length(unique(method.data3$Module))) 212 | } 213 | p.label <- method.data3 %>% 214 | dplyr::mutate(Method = "Method") %>% 215 | ggplot2::ggplot(ggplot2::aes(Module, y = Method, fill = Module)) + 216 | ggplot2::ylab("Method")+ 217 | ggplot2::geom_tile() + 218 | ggplot2::scale_fill_manual(values = method.color, name = "Method") + 219 | ggplot2::scale_y_discrete(position = "left") + 220 | ggplot2::theme_minimal() + 221 | ggplot2::theme(axis.text.x = ggplot2::element_blank(), 222 | axis.ticks.x = ggplot2::element_blank(), 223 | panel.grid = ggplot2::element_blank(), 224 | axis.text.y = ggplot2::element_text(angle = 0, size =10, vjust = 0.5), 225 | axis.title.x = ggplot2::element_blank(), 226 | axis.title.y = ggplot2::element_blank())+ 227 | Seurat::NoLegend() 228 | 229 | p <- p.heatmap %>% aplot::insert_top(p.label, height = 0.1) 230 | p <- ggplotify::as.ggplot(p) + 231 | ggplot2::ggtitle(unique(method.data3$geneset))+ 232 | ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5)) 233 | 234 | # make legend 235 | col_fun <- circlize::colorRamp2(c(stats::fivenum(method.data3$Value)[1], 236 | stats::fivenum(method.data3$Value)[3], 237 | stats::fivenum(method.data3$Value)[5]), 238 | correlation.color) 239 | 240 | lgd.method <- ComplexHeatmap::Legend(labels = levels(factor(method.data3$Module)), 241 | title = "Method", 242 | legend_gp = grid::gpar(fill = method.color)) 243 | 244 | 245 | lgd.cor <- ComplexHeatmap::Legend(col_fun = col_fun, title = "Correlation") 246 | 247 | 248 | lgd.p <- ComplexHeatmap::Legend(pch = c("*", "**", "***", "****"), 249 | type = "points", 250 | labels = c("<= 0.05", "< 0.01", "< 0.001", "< 0.0001"), 251 | title = "Spearman's P Value") 252 | 253 | heatmap.legend <- ComplexHeatmap::packLegend(lgd.method, lgd.cor, lgd.p, 254 | direction = "vertical", 255 | column_gap = grid::unit(1, "cm")) 256 | heatmap.legend <- grid::grid.grabExpr(ComplexHeatmap::draw(heatmap.legend)) %>% 257 | ggplotify::as.ggplot() 258 | 259 | p <- cowplot::plot_grid(p, heatmap.legend, rel_widths = c(1,0.1)) 260 | 261 | return(p) 262 | 263 | }) 264 | 265 | result <- list() 266 | result[["hub_result"]] <- method.data 267 | result[["hub_plot"]] <- method.data2 268 | return(result ) 269 | 270 | } 271 | 272 | 273 | -------------------------------------------------------------------------------- /R/irGSEA.merge.R: -------------------------------------------------------------------------------- 1 | #' Merge the enrichment score assay 2 | #' 3 | #' Merge the enrichment score assay among various Seurat objects 4 | #' 5 | #' @param object.x Seurat object (V4 or V5). 6 | #' @param object.y The list includes various Seurat objects. The verison of 7 | #' Seurat object should be the same as the version of object.x. 8 | #' @param method A vector. Default c("AUCell", "UCell", "singscore", "ssgsea", 9 | #' "JASMINE", "viper"). 10 | #' @param overwrite Default True. The same geneset name exists in two gene scoring 11 | #' matrices, the newly added geneset will overwrite the previous geneset if 12 | #' the overwrite is true. The newly added geneset will be forcibly renamed 13 | #' as "geneset's name + serial number" if the overwrite is false. 14 | #' 15 | #' @return Seurat object including score matrix. 16 | #' @export 17 | #' 18 | #' @examples 19 | #' \dontrun{ 20 | #' # load PBMC dataset by R package SeuratData 21 | # # devtools::install_github('satijalab/seurat-data') 22 | #' library(Seurat) 23 | #' library(SeuratData) 24 | #' # download 3k PBMCs from 10X Genomics 25 | #' InstallData("pbmc3k") 26 | #' library(Seurat) 27 | #' 28 | #' library(RcppML) 29 | #' library(irGSEA) 30 | #' library(tidyverse) 31 | #' library(clusterProfiler) 32 | #' data("pbmc3k.final") 33 | #' pbmc3k.final <- SeuratObject::UpdateSeuratObject(pbmc3k.final) 34 | #' 35 | #' # download gmt file 36 | #' gmt_url1 <- "https://data.broadinstitute.org/" 37 | #' gmt_url2 <- "gsea-msigdb/msigdb/release/2023.2.Hs/", 38 | #' gmt_url3 <- "h.all.v2023.2.Hs.symbols.gmt" 39 | #' gmt_url <- paste0(gmt_url1, gmt_url2, gmt_url3) 40 | #' local_gmt <- "./h.all.v2023.2.Hs.symbols.gmt" 41 | #' download.file(gmt_url , local_gmt) 42 | #' msigdb <- clusterProfiler::read.gmt("./h.all.v2023.2.Hs.symbols.gmt") 43 | #' 44 | #' # convert to list[hallmarker] required by irGSEA package 45 | #' msigdb$term <- factor(msigdb$term) 46 | #' msigdb <- msigdb %>% 47 | #' dplyr::group_split(term, .keep = F) %>% 48 | #' purrr::map( ~.x %>% dplyr::pull(gene) %>% unique(.)) %>% 49 | #' purrr::set_names(levels(msigdb$term)) 50 | #' 51 | #' pbmc3k.final1 <- irGSEA.score(object = pbmc3k.final, assay = "RNA", slot = "data", 52 | #' custom = T, geneset = msigdb[1:25], 53 | #' method = c("AUCell", "UCell", "singscore", "ssgsea"), 54 | #' kcdf = 'Gaussian') 55 | #' pbmc3k.final2 <- irGSEA.score(object = pbmc3k.final, assay = "RNA", slot = "data", 56 | #' custom = T, geneset = msigdb[26:50], 57 | #' method = c("AUCell", "UCell", "singscore", "ssgsea"), 58 | #' kcdf = 'Gaussian') 59 | #' 60 | #' pbmc3k.final3 <- irGSEA.merge(object.x = pbmc3k.final1, 61 | #' object.y = pbmc3k.final2, 62 | #' method = c("AUCell", "UCell", "singscore", "ssgsea"), 63 | #' overwrite = T) 64 | #' 65 | #' } 66 | #' 67 | irGSEA.merge <- function(object.x = NULL, object.y = NULL, 68 | method = c("AUCell", "UCell", "singscore", 69 | "ssgsea","JASMINE", "viper"), 70 | overwrite = T 71 | ){ 72 | # create a list 73 | if (length(object.y) == 1) { 74 | object.y <- list(object.y) 75 | } 76 | 77 | # work 78 | for (k in method) { 79 | 80 | for (i in seq_along(object.y)) { 81 | 82 | # acts.x 83 | acts.x <- tryCatch({ 84 | acts.x <- SeuratObject::GetAssayData(object.x, assay = k, slot = "scale.data") 85 | }, error = function(e) { 86 | acts.x <- NULL 87 | }) 88 | 89 | # acts.y 90 | acts.y <- tryCatch({ 91 | acts.y <- SeuratObject::GetAssayData(object.y[[i]], assay = k, slot = "scale.data") 92 | }, error = function(e) { 93 | acts.y <- NULL 94 | }) 95 | 96 | # next 97 | if (is.null(acts.x) & is.null(acts.y)) { 98 | next 99 | } 100 | 101 | 102 | 103 | # merge, if overwrite, or renames the same geneset 104 | if (overwrite) { 105 | index.intersect <- !rownames(acts.x) %in% rownames(acts.y) 106 | acts <- rbind(acts.x[index.intersect,], acts.y) 107 | 108 | 109 | }else{ 110 | index.intersect <- match(rownames(acts.x), rownames(acts.y)) 111 | if (any(!is.na(index.intersect))) { 112 | index.intersect2 <- index.intersect[!is.na(index.intersect)] 113 | rownames(acts.y)[index.intersect2] <- paste0(rownames(acts.y)[index.intersect2], "-", i) 114 | acts <- rbind(acts.x, acts.y) 115 | }else{ 116 | acts <- rbind(acts.x, acts.y) 117 | } 118 | 119 | 120 | } 121 | 122 | if (is.null(acts)) { next } 123 | 124 | 125 | # if version 126 | version <- tryCatch({ 127 | # if v5 or v3 128 | if (class(object.x[[k]])[1] == "Assay5") { 129 | options(Seurat.object.assay.version = "v5") 130 | version <- "v5" 131 | }else{ 132 | options(Seurat.object.assay.version = "v3") 133 | version <- "v3" 134 | } 135 | }, error = function(e) { 136 | version <- tryCatch({ 137 | # if v5 or v3 138 | if (class(object.y[[i]][[k]])[1] == "Assay5") { 139 | options(Seurat.object.assay.version = "v5") 140 | version <- "v5" 141 | }else{ 142 | options(Seurat.object.assay.version = "v3") 143 | version <- "v3" 144 | } 145 | }, error = function(e) { 146 | options(Seurat.object.assay.version = "v3") 147 | version <- "v3" 148 | }) 149 | }) 150 | 151 | # if verison of Assay same 152 | tryCatch({ 153 | if (class(object.x[[k]])[1] != class(object.y[[i]][[k]])[1]) { 154 | message(paste0("The Seurat/Assay versions of the object.x and object.y: ", 155 | i, 156 | " are inconsistent. \n", 157 | "We convert object.y: ", 158 | i, 159 | " from ", 160 | class(object.y[[i]][[k]])[1], 161 | " to ", 162 | class(object.x[[k]])[1], 163 | " .")) 164 | 165 | # convert 166 | if (class(object.y[[i]][[k]])[1] == "Assay5") { 167 | object.y[[i]][[k]] <- methods::as(object = object.y[[i]][[k]], Class = "Assay") 168 | }else{ 169 | object.y[[i]][[k]] <- methods::as(object = object.y[[i]][[k]], Class = "Assay5") 170 | } 171 | } 172 | }, error = function(e) { 173 | # print("") 174 | }) 175 | 176 | 177 | 178 | 179 | # meta.features 180 | # object.x 181 | 182 | if (is.null(acts.x)) { 183 | object.x.meta.features <- NULL 184 | }else{ 185 | if (purrr::is_empty(object.x[[k]]@meta.features)) { 186 | if (version == "v3") { 187 | object.x.meta.features <- data.frame(geneset = rownames(object.x[[k]]), 188 | target.gene = "") 189 | }else{ 190 | object.x.meta.features <- data.frame(geneset = rownames(object.x[[k]]), 191 | target.gene = "") %>% 192 | tibble::column_to_rownames(var = "geneset") 193 | } 194 | 195 | }else{ 196 | if (version == "v3") { 197 | object.x.meta.features <- object.x[[k]]@meta.features %>% 198 | tibble::rownames_to_column(var = "geneset") 199 | }else{ 200 | object.x.meta.features <- object.x[[k]]@meta.features 201 | } 202 | } 203 | if (overwrite) { 204 | index.intersect <- !rownames(acts.x) %in% rownames(acts.y) 205 | object.x.meta.features <- object.x.meta.features[index.intersect, ] 206 | 207 | } 208 | } 209 | 210 | 211 | 212 | # object.y 213 | 214 | if (is.null(acts.y)) { 215 | object.y.meta.features <- NULL 216 | }else{ 217 | if (purrr::is_empty(object.y[[i]][[k]]@meta.features)) { 218 | 219 | if (version == "v3") { 220 | object.y.meta.features <- data.frame(geneset = rownames(object.y[[i]][[k]]), 221 | target.gene = "") 222 | }else{ 223 | object.y.meta.features <- data.frame(geneset = rownames(object.y[[i]][[k]]), 224 | target.gene = "") %>% 225 | tibble::column_to_rownames(var = "geneset") 226 | } 227 | object.y.meta.features$geneset <- rownames(acts.y) 228 | 229 | }else{ 230 | if (version == "v3") { 231 | object.y.meta.features <- object.y[[i]][[k]]@meta.features %>% 232 | tibble::rownames_to_column(var = "geneset") 233 | }else{ 234 | object.y.meta.features <- object.y[[i]][[k]]@meta.features 235 | } 236 | object.y.meta.features$geneset <- rownames(acts.y) 237 | } 238 | 239 | } 240 | 241 | 242 | 243 | 244 | if (is.null(object.x.meta.features) & is.null(object.y.meta.features) ) { 245 | next 246 | } 247 | 248 | 249 | 250 | # add matrix meta.features 251 | 252 | object.x[[k]] <- SeuratObject::CreateAssayObject(counts = acts) 253 | object.x <- SeuratObject::SetAssayData(object.x, slot = "scale.data", 254 | new.data = acts, 255 | assay = k) 256 | if (utils::packageVersion("Seurat") >= "5.0.0") { 257 | object.x[[k]]$counts <- NULL} 258 | 259 | 260 | 261 | if (version == "v3") { 262 | object.x[[k]]@meta.features <- as.data.frame(rbind(object.x.meta.features, 263 | object.y.meta.features)) %>% 264 | tibble::column_to_rownames(var = "geneset") 265 | 266 | }else{ 267 | object.x[[k]]@meta.features <- rbind(object.x.meta.features, 268 | object.y.meta.features) 269 | 270 | } 271 | 272 | rm(acts) 273 | gc() 274 | 275 | 276 | message(paste0("Finish merge ", k, " scores of object.y: ", i)) 277 | 278 | } 279 | 280 | } 281 | 282 | 283 | return(object.x) 284 | 285 | } 286 | 287 | 288 | -------------------------------------------------------------------------------- /R/irGSEA.ridgeplot.R: -------------------------------------------------------------------------------- 1 | #' Ridge plot 2 | #' Easy to show the data distribution by ridge plot 3 | #' @param object A Seurat after perform \code{\link{irGSEA.score}} 4 | #' @param method A character. It should be one of the followling : AUCell, 5 | #' UCell, singscore, ssgsea. 6 | #' @param show.geneset A character. It should be one of the rownames of 7 | #' enrichment score matrix. 8 | #' @param group.by Default ident when it is set to NULL. You can specify other 9 | #' column of metadata. 10 | #' @param color.cluster A vector. Default "ggsci::pal_igv()(the number of colnames 11 | #' of enrichment score matrix)" when it is set to NULL. 12 | #' @param cluster.levels A vector equal to the number of clusters. 13 | #' @param ... More parameters pass to \code{\link[ggridges]{geom_density_ridges}} 14 | #' 15 | #' @return ridge plot 16 | #' @export 17 | #' 18 | #' @examples 19 | #' 20 | #' \dontrun{ 21 | #' # load PBMC dataset by R package SeuratData 22 | #' library(Seurat) 23 | #' library(SeuratData) 24 | #' # download 3k PBMCs from 10X Genomics 25 | #' InstallData("pbmc3k") 26 | #' data("pbmc3k.final") 27 | #' pbmc3k.final <- SeuratObject::UpdateSeuratObject(pbmc3k.final) 28 | #' 29 | #' # Seurat object 30 | #' pbmc3k.final <- irGSEA.score(object = pbmc3k.final, assay = "RNA", 31 | #' slot = "data", msigdb = T, species = "Homo sapiens", 32 | #' category = "H", geneid = "symbol", 33 | #' method = c("AUCell", "UCell", "singscore", "ssgsea"), kcdf = 'Gaussian') 34 | #' 35 | #' irGSEA.ridgeplot.plot1 <- irGSEA.ridgeplot(object = pbmc3k.final, 36 | #' method = "UCell", show.geneset = "HALLMARK-INFLAMMATORY-RESPONSE") 37 | #' irGSEA.ridgeplot.plot2 <- irGSEA.ridgeplot(object = pbmc3k.final, 38 | #' method = "ssgsea", show.geneset = "HALLMARK-IL6-JAK-STAT3-SIGNALING") 39 | #' 40 | #' } 41 | #' 42 | irGSEA.ridgeplot <- function(object = NULL, method = NULL, 43 | show.geneset = NULL, group.by = NULL, 44 | color.cluster = NULL, cluster.levels = NULL, ...){ 45 | # pretreatment 46 | ident <- NULL 47 | geneset <- NULL 48 | if ((! all(method %in% Seurat::Assays(object))) | (length(method) > 1) | (purrr::is_null(method))) { 49 | stop("`method` should be one of the followling : AUCell, UCell, singscore, ssgsea, JASMINE, VAM, scSE, VISION, gficf, GSVA, zscore, plage, wmean, wsum, mdt, viper, GSVApy, AddModuleScore, pagoda2.") 50 | } 51 | 52 | # group 53 | if (purrr::is_null(group.by)) { 54 | anno.ident <- SeuratObject::Idents(object) 55 | }else{ 56 | object <- SeuratObject::SetIdent(object, value = group.by) 57 | anno.ident <- SeuratObject::Idents(object) 58 | } 59 | # factors are sorted alphabetically 60 | anno.ident <- as.factor(as.character(anno.ident)) 61 | # set levels of cluster 62 | if (! purrr::is_null(cluster.levels)) { 63 | anno.ident <- factor(anno.ident, levels = cluster.levels) 64 | } 65 | SeuratObject::Idents(object) <- anno.ident 66 | 67 | # set colors 68 | if (purrr::is_null(color.cluster)) { 69 | color.cluster <- ggsci::pal_igv()(length(levels(object))) 70 | } 71 | 72 | # geneset 73 | if (purrr::is_null(show.geneset)) { 74 | stop("`show.geneset` can not be empty.") 75 | }else{ 76 | custom.geneset <- show.geneset[show.geneset %in% rownames(object[[method]])] 77 | if (purrr::is_null(custom.geneset)) { 78 | stop("All genesets of `show.geneset` are not in the `method`.") 79 | } 80 | if (! all(show.geneset %in% rownames(object[[method]]))) { 81 | a <- show.geneset[! show.geneset %in% rownames(object[[method]])] 82 | message(paste0("Following genesets of `show.geneset` are not in such `method` : ",a)) 83 | } 84 | } 85 | 86 | # plot 87 | scores.ridgeplot <- Seurat::RidgePlot(object = object, 88 | assay = method, 89 | slot = "scale.data", 90 | group.by = group.by, 91 | cols = color.cluster, 92 | features = custom.geneset, 93 | fill.by = "ident") + 94 | ggplot2::theme(axis.title.y = ggplot2::element_blank(), 95 | plot.title = ggplot2::element_text(hjust = 0.5), 96 | axis.title.x = ggplot2::element_text(hjust = 0.5), 97 | panel.grid = ggplot2::element_blank())+ 98 | ggplot2::guides(fill = ggplot2::guide_legend(title = "Cluster")) 99 | 100 | scores.ridgeplot <- scores.ridgeplot$data %>% 101 | dplyr::rename(c("geneset" = tidyselect::all_of(custom.geneset))) %>% 102 | ggplot2::ggplot(ggplot2::aes(x = geneset, y = ident, fill = ident)) + 103 | ggridges::geom_density_ridges(jittered_points=TRUE, scale = .95, rel_min_height = .01, 104 | point_shape = "|", point_size = 3, size = 0.25, 105 | position = ggridges::position_points_jitter(height = 0), ...) + 106 | ggplot2::scale_y_discrete(expand = c(.01, 0), name = "Cluster") + 107 | ggplot2::scale_x_continuous(expand = c(0, 0), name = paste0(method, " scores")) + 108 | ggplot2::scale_fill_manual(values = color.cluster) + 109 | ggplot2::guides(fill = ggplot2::guide_legend( 110 | title="Cluster", 111 | override.aes = list(fill = color.cluster, color = NA, point_color = NA))) + 112 | ggplot2::ggtitle(custom.geneset) + 113 | ggridges::theme_ridges(center = TRUE)+ 114 | ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5, size = 12), 115 | axis.title = ggplot2::element_text(size = 12)) 116 | 117 | 118 | return(scores.ridgeplot) 119 | 120 | } 121 | 122 | -------------------------------------------------------------------------------- /R/irGSEA.subset.R: -------------------------------------------------------------------------------- 1 | #' Subset the enrichment score matrix 2 | #' 3 | #' Subset the enrichment score matrix 4 | #' 5 | #' @param object Seurat object (V4 or V5). 6 | #' @param features Vector. The geneset want to subset. 7 | #' @param invert If filp the geneset want to subset. 8 | #' @param method A vector. Default c("AUCell", "UCell", "singscore", "ssgsea", 9 | #' "JASMINE", "viper"). 10 | #' 11 | #' 12 | #' @return Seurat object including score matrix. 13 | #' @export 14 | #' 15 | #' @examples 16 | #' \dontrun{ 17 | #' # load PBMC dataset by R package SeuratData 18 | # # devtools::install_github('satijalab/seurat-data') 19 | #' library(Seurat) 20 | #' library(SeuratData) 21 | #' # download 3k PBMCs from 10X Genomics 22 | #' InstallData("pbmc3k") 23 | #' library(Seurat) 24 | #' 25 | #' library(RcppML) 26 | #' library(irGSEA) 27 | #' library(tidyverse) 28 | #' library(clusterProfiler) 29 | #' data("pbmc3k.final") 30 | #' pbmc3k.final <- SeuratObject::UpdateSeuratObject(pbmc3k.final) 31 | #' 32 | #' # download gmt file 33 | #' gmt_url1 <- "https://data.broadinstitute.org/" 34 | #' gmt_url2 <- "gsea-msigdb/msigdb/release/2023.2.Hs/", 35 | #' gmt_url3 <- "h.all.v2023.2.Hs.symbols.gmt" 36 | #' gmt_url <- paste0(gmt_url1, gmt_url2, gmt_url3) 37 | #' local_gmt <- "./h.all.v2023.2.Hs.symbols.gmt" 38 | #' download.file(gmt_url , local_gmt) 39 | #' msigdb <- clusterProfiler::read.gmt("./h.all.v2023.2.Hs.symbols.gmt") 40 | #' 41 | #' # convert to list[hallmarker] required by irGSEA package 42 | #' msigdb$term <- factor(msigdb$term) 43 | #' msigdb <- msigdb %>% 44 | #' dplyr::group_split(term, .keep = F) %>% 45 | #' purrr::map( ~.x %>% dplyr::pull(gene) %>% unique(.)) %>% 46 | #' purrr::set_names(levels(msigdb$term)) 47 | #' 48 | #' pbmc3k.final1 <- irGSEA.score(object = pbmc3k.final, assay = "RNA", slot = "data", 49 | #' custom = T, geneset = msigdb[1:25], 50 | #' method = c("AUCell", "UCell", "singscore", "ssgsea"), 51 | #' kcdf = 'Gaussian') 52 | #' pbmc3k.final2 <- irGSEA.score(object = pbmc3k.final, assay = "RNA", slot = "data", 53 | #' custom = T, geneset = msigdb[26:50], 54 | #' method = c("AUCell", "UCell", "singscore", "ssgsea"), 55 | #' kcdf = 'Gaussian') 56 | #' 57 | #' pbmc3k.final3 <- irGSEA.merge(object.x = pbmc3k.final1, 58 | #' object.y = pbmc3k.final2, 59 | #' method = c("AUCell", "UCell", "singscore", "ssgsea"), 60 | #' overwrite = T) 61 | #' pbmc3k.final4 <- irGSEA.subset(object = pbmc3k.final3, 62 | #' features = rownames(pbmc3k.final3[["AUCell"]])[1:25], 63 | #' method = c("AUCell", "UCell", "singscore", "ssgsea"), 64 | #' invert = F) 65 | #' } 66 | #' 67 | irGSEA.subset <- function(object = NULL, 68 | features = NULL, 69 | invert = F, 70 | method = c("AUCell", "UCell", "singscore", 71 | "ssgsea","JASMINE", "viper") 72 | ){ 73 | 74 | # work 75 | for (k in method) { 76 | # if v5 or v3 77 | if (class(object)[1] == "Assay5") { 78 | options(Seurat.object.assay.version = "v5") 79 | version <- "v5" 80 | }else{ 81 | options(Seurat.object.assay.version = "v3") 82 | version <- "v3" 83 | } 84 | if (invert) { 85 | features <- rownames(object[[k]])[!rownames(object[[k]]) %in% features] 86 | } 87 | 88 | object[[k]] <- subset(object[[k]], features = features) 89 | 90 | } 91 | 92 | 93 | return(object) 94 | 95 | } 96 | 97 | 98 | -------------------------------------------------------------------------------- /R/irGSEA.upset.R: -------------------------------------------------------------------------------- 1 | #' Upset plot 2 | #' 3 | #' Easy to show analysis results by upset plot 4 | #' 5 | #' @param object A list after perform \code{\link{irGSEA.integrate}} 6 | #' @param method A character. It should be one of the followling : AUCell, UCell, 7 | #' singscore, ssgsea or RRA. Default RRA. 8 | #' @param upset.width Width of the whole upset plot. Default 13. 9 | #' @param upset.height Height of the whole upset plot. Default 7. 10 | #' @param title.size The fointsize of rownames. Default 10. 11 | #' @param text.size The fointsize of rownames. Default 9. 12 | #' @param cluster.color A vector. Default "ggsci::pal_igv()(the number of colnames 13 | #' of enrichment score matrix)" when it is set to NULL. 14 | #' @param bar.color A character. Default "black" when it is set to NULL. 15 | #' @param cluster.levels A vector equal to the number of clusters. 16 | #' @param mode A character. It should be one of the followling : distinct, 17 | #' intersect, or union. Default distinct. It represents the mode for forming 18 | #' the combination set. see Mode section \url{https://jokergoo.github.io/ComplexHeatmap-reference/book/upset-plot.html} 19 | #' for details. 20 | #' @param set.size The minimal combination set size. Default 1. 21 | #' @param set.degree A vector. Show all combination sets when it set to NULL. 22 | #' It would show different combination set when it is set to different number. 23 | #' For example, it only show the interactions among two cluster or three cluster 24 | #' when it's set to 2 or 3. 25 | #' @param table.generate Deault FALSE. It will output a list including all 26 | #' combination sets and their gene sets when it set to TRUE. 27 | #' @param ... More parameters pass to \code{\link[ComplexHeatmap]{UpSet}} 28 | #' 29 | #' @return upset plot or list 30 | #' @export 31 | #' 32 | #' @examples 33 | #' \dontrun{ 34 | #' # load PBMC dataset by R package SeuratData 35 | #' library(Seurat) 36 | #' library(SeuratData) 37 | #' # download 3k PBMCs from 10X Genomics 38 | #' InstallData("pbmc3k") 39 | #' data("pbmc3k.final") 40 | #' pbmc3k.final <- SeuratObject::UpdateSeuratObject(pbmc3k.final) 41 | #' 42 | #' # Seurat object 43 | #' pbmc3k.final <- irGSEA.score(object = pbmc3k.final, assay = "RNA", 44 | #' slot = "data", msigdb = T, species = "Homo sapiens", 45 | #' category = "H", geneid = "symbol", 46 | #' method = c("AUCell", "UCell", "singscore", "ssgsea"), kcdf = 'Gaussian') 47 | #' 48 | #' # Integrated analysis 49 | #' result.dge <- irGSEA.integrate(object = pbmc3k.final, 50 | #' group.by = "seurat_annotations", metadata = NULL, col.name = NULL, 51 | #' method = c("AUCell","UCell","singscore","ssgsea")) 52 | #' 53 | #' irGSEA.upset.plot1 <- irGSEA.upset(object = result.dge, method = "RRA") 54 | #' irGSEA.upset.plot2 <- irGSEA.upset(object = result.dge, method = "ssgsea") 55 | #' 56 | #' } 57 | #' 58 | #' 59 | 60 | irGSEA.upset <- function(object = NULL, method = "RRA", 61 | upset.width = 13, upset.height = 7, 62 | title.size = 10, text.size = 9, 63 | cluster.color = NULL, bar.color = "black", 64 | cluster.levels = NULL, mode = "distinct", 65 | set.size = 1, set.degree = NULL, table.generate = F, 66 | ...){ 67 | # pretreatment 68 | if (! purrr::is_list(object)) { 69 | stop("object should be a list.") 70 | } 71 | if ((! all(method %in% names(object))) | (length(method) > 1) | (purrr::is_null(method))) { 72 | stop("`method` should be one of the followling : AUCell, UCell, singscore, ssgsea, JASMINE, VAM, scSE, VISION, gficf, GSVA, zscore, plage, wmean, wsum, mdt, viper, GSVApy, AddModuleScore, pagoda2, RRA.") 73 | } 74 | pvalue <- NULL 75 | if (method %in% names(object)[! names(object) == "RRA"]) { 76 | object[method] <- object[method] %>% purrr::map( ~.x %>% dplyr::rename(pvalue = p_val_adj)) 77 | } 78 | # matrix 79 | cluster <- NULL 80 | sig.genesets.upset <- object[[method]] %>% 81 | dplyr::filter(pvalue < 0.05) %>% 82 | dplyr::select(c("Name","cluster")) 83 | sig.genesets.upset.names <- levels(factor(as.character(sig.genesets.upset$cluster))) 84 | if (purrr::is_null(cluster.color)) { 85 | cluster.color <- ggsci::pal_igv()(length(sig.genesets.upset.names)) 86 | } 87 | 88 | sig.genesets.upset <- sig.genesets.upset %>% 89 | dplyr::group_split(cluster,.keep = F) %>% 90 | purrr::set_names(sig.genesets.upset.names) 91 | sig.genesets.upset <- lapply(sig.genesets.upset, function(x){x <- x$Name}) 92 | # set levels of cluster 93 | if (! purrr::is_null(cluster.levels)) { 94 | sig.genesets.upset <- sig.genesets.upset[cluster.levels] 95 | } 96 | 97 | # matrix 98 | m <- ComplexHeatmap::make_comb_mat(sig.genesets.upset, mode = mode) 99 | # degree 100 | if (purrr::is_null(set.degree)) { 101 | m <- m[ComplexHeatmap::comb_degree(m) > 0] 102 | }else{ 103 | m <- m[ComplexHeatmap::comb_degree(m) %in% c(set.degree)] 104 | } 105 | # set size 106 | m <- m[ComplexHeatmap::comb_size(m) >= set.size] 107 | 108 | # set levels of cluster 109 | ss <- ComplexHeatmap::set_size(m) 110 | if (! purrr::is_null(cluster.levels)) { 111 | cluster.order <- cluster.levels 112 | }else{ 113 | cluster.order <- order(ss) 114 | } 115 | 116 | # table 117 | if (table.generate == T) { 118 | comb.list <- lapply(ComplexHeatmap::comb_name(m), function(i){ComplexHeatmap::extract_comb(m, i)}) 119 | names(comb.list) <- ComplexHeatmap::comb_name(m, readable = TRUE) 120 | return(comb.list) 121 | } 122 | # plot 123 | cs <- ComplexHeatmap::comb_size(m) 124 | ht <- ComplexHeatmap::UpSet(m, 125 | bg_col = "#D0DFE6FF", 126 | heatmap_width = grid::unit(upset.width, "cm"), 127 | heatmap_height = grid::unit(upset.height, "cm"), 128 | column_title = method, 129 | column_title_gp = grid::gpar(fontsize = title.size), 130 | set_order = cluster.order, 131 | comb_order = order(ComplexHeatmap::comb_degree(m), -cs), 132 | top_annotation = ComplexHeatmap::HeatmapAnnotation( 133 | "Intersections" = ComplexHeatmap::anno_barplot(cs, 134 | ylim = c(0, max(cs)*1.1), 135 | border = FALSE, 136 | gp = grid::gpar(color = bar.color), 137 | height = grid::unit(2, "cm") 138 | ), 139 | annotation_name_side = "left", 140 | annotation_name_rot = 90, 141 | annotation_name_gp = grid::gpar(fontsize = text.size)), 142 | left_annotation = ComplexHeatmap::rowAnnotation( 143 | "Counts" = ComplexHeatmap::anno_barplot(-ss, baseline = 0, 144 | axis_param = list(at = c(0, -500, -1000, -1500), 145 | labels = c(0, 500, 1000, 1500), 146 | labels_rot = 0), 147 | border = FALSE, 148 | gp = grid::gpar(fill = cluster.color), 149 | width = grid::unit(1.5, "cm")), annotation_name_gp = grid::gpar(fontsize = text.size), 150 | set_name = ComplexHeatmap::anno_text(ComplexHeatmap::set_name(m), 151 | location = 0.5, 152 | just = "center", 153 | gp = grid::gpar(fontsize = text.size), 154 | width = ComplexHeatmap::max_text_width(text = ComplexHeatmap::set_name(m), 155 | gp = grid::gpar(fontsize = text.size)) + grid::unit(1, "mm"))), 156 | right_annotation = NULL, 157 | show_row_names = FALSE, ...) 158 | 159 | plot_upset <- function(){ 160 | ht <- ComplexHeatmap::draw(ht) 161 | od <- ComplexHeatmap::column_order(ht) 162 | ComplexHeatmap::decorate_annotation("Intersections", { 163 | grid::grid.text(cs[od], x = seq_along(cs), y = grid::unit(cs[od], "native") + grid::unit(2, "pt"), 164 | default.units = "native", just = c("left", "bottom"), 165 | gp = grid::gpar(fontsize = 6, col = "black"), rot = 45) 166 | }) 167 | 168 | } 169 | 170 | upset.plot <- grid::grid.grabExpr(plot_upset()) %>% 171 | ggplotify::as.ggplot() 172 | return(upset.plot) 173 | } 174 | 175 | -------------------------------------------------------------------------------- /R/irGSEA.vlnplot.R: -------------------------------------------------------------------------------- 1 | #' Half vlnplot 2 | #' 3 | #' Easy to show the data distribution by half vlnplot 4 | #' 5 | #' @param object A Seurat after perform \code{\link{irGSEA.score}} 6 | #' @param method A character. It should be one or more of the followling : AUCell, 7 | #' UCell, singscore, ssgsea, JASMINE, VAM, scSE, VISION, gficf, GSVA, zscore, 8 | #' plage, wmean, wsum, mdt, viper, GSVApy, AddModuleScore, pagoda2. 9 | #' @param show.geneset A character. It should be one of the rownames of 10 | #' enrichment score matrix. 11 | #' @param group.by Default ident when it is set to NULL. You can specify other 12 | #' column of metadata. 13 | #' @param color.cluster A vector. Default "ggsci::pal_igv()(the number of colnames 14 | #' of enrichment score matrix)" when it is set to NULL. 15 | #' @param cluster.levels A vector equal to the number of clusters. 16 | #' 17 | #' @return vlnplot 18 | #' @export 19 | #' 20 | #' @examples 21 | #' \dontrun{ 22 | #' # load PBMC dataset by R package SeuratData 23 | #' library(Seurat) 24 | #' library(SeuratData) 25 | #' # download 3k PBMCs from 10X Genomics 26 | #' InstallData("pbmc3k") 27 | #' data("pbmc3k.final") 28 | #' pbmc3k.final <- SeuratObject::UpdateSeuratObject(pbmc3k.final) 29 | #' 30 | #' # Seurat object 31 | #' pbmc3k.final <- irGSEA.score(object = pbmc3k.final, assay = "RNA", 32 | #' slot = "data", msigdb = T, species = "Homo sapiens", 33 | #' category = "H", geneid = "symbol", 34 | #' method = c("AUCell", "UCell", "singscore", "ssgsea"), kcdf = 'Gaussian') 35 | #' 36 | #' irGSEA.vlnplot.plot1 <- irGSEA.vlnplot(object = pbmc3k.final, 37 | #' method = c("AUCell", "UCell", "singscore", "ssgsea"), 38 | #' show.geneset = "HALLMARK-INFLAMMATORY-RESPONSE") 39 | #' irGSEA.vlnplot.plot2 <- irGSEA.vlnplot(object = pbmc3k.final, 40 | #' method = c("AUCell", "UCell", "singscore", "ssgsea"), 41 | #' show.geneset = "HALLMARK-IL6-JAK-STAT3-SIGNALING") 42 | #' 43 | #' 44 | #' } 45 | #' 46 | irGSEA.vlnplot <- function(object = NULL, method = NULL, 47 | show.geneset = NULL, group.by = NULL, 48 | color.cluster = NULL, cluster.levels = NULL){ 49 | # pretreatment 50 | ident <- NULL 51 | geneset <- NULL 52 | if ((! all(method %in% Seurat::Assays(object))) |(purrr::is_null(method))) { 53 | stop("`method` should be one or more of the followling : AUCell, UCell, singscore, ssgsea, JASMINE, VAM, scSE, VISION, gficf, GSVA, zscore, plage, wmean, wsum, mdt, viper, GSVApy, AddModuleScore, pagoda2.") 54 | } 55 | 56 | # group 57 | if (purrr::is_null(group.by)) { 58 | anno.ident <- SeuratObject::Idents(object) 59 | }else{ 60 | object <- SeuratObject::SetIdent(object, value = group.by) 61 | anno.ident <- SeuratObject::Idents(object) 62 | } 63 | # factors are sorted alphabetically 64 | anno.ident <- as.factor(as.character(anno.ident)) 65 | # set levels of cluster 66 | if (! purrr::is_null(cluster.levels)) { 67 | anno.ident <- factor(anno.ident, levels = cluster.levels) 68 | } 69 | SeuratObject::Idents(object) <- anno.ident 70 | 71 | # set colors 72 | if (purrr::is_null(color.cluster)) { 73 | color.cluster <- ggsci::pal_igv()(length(levels(object))) 74 | } 75 | 76 | # geneset 77 | if (purrr::is_null(show.geneset)) { 78 | stop("`show.geneset` can not be empty.") 79 | } 80 | 81 | # plot 82 | for (i in method) { 83 | object@meta.data <- object@meta.data %>% 84 | dplyr::mutate(!!rlang::sym(i):= as.numeric(SeuratObject::GetAssayData(object, assay = i, slot = "scale.data")[show.geneset,])) 85 | } 86 | scores.vlnplot <- Seurat::VlnPlot(object = object, 87 | assay = "RNA", 88 | combine = T, 89 | stack = T, 90 | flip = T, 91 | group.by = group.by, 92 | cols = color.cluster, 93 | features = method, 94 | pt.size = 0, 95 | fill.by = "ident")+ 96 | ggplot2::ggtitle(show.geneset)+ 97 | ggplot2::theme(axis.title.x = ggplot2::element_blank(), 98 | plot.title = ggplot2::element_text(hjust = 0.5), 99 | title = ggplot2::element_text(size =12), 100 | axis.text.x = ggplot2::element_text(vjust = 0.5, hjust = 0.5, angle = 45))+ 101 | ggplot2::guides(fill = ggplot2::guide_legend(title = "Cluster")) 102 | 103 | 104 | return(scores.vlnplot) 105 | 106 | } 107 | 108 | 109 | -------------------------------------------------------------------------------- /R/utils-pipe.R: -------------------------------------------------------------------------------- 1 | #' Pipe operator 2 | #' 3 | #' See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details. 4 | #' 5 | #' @name %>% 6 | #' @rdname pipe 7 | #' @keywords internal 8 | #' @export 9 | #' @importFrom magrittr %>% 10 | #' @usage lhs \%>\% rhs 11 | #' @param lhs A value or the magrittr placeholder. 12 | #' @param rhs A function call using the magrittr semantics. 13 | #' @return The result of calling `rhs(lhs)`. 14 | NULL 15 | -------------------------------------------------------------------------------- /R/utils-tidy-eval.R: -------------------------------------------------------------------------------- 1 | #' Tidy eval helpers 2 | #' 3 | #' @description 4 | #' This page lists the tidy eval tools reexported in this package from 5 | #' rlang. To learn about using tidy eval in scripts and packages at a 6 | #' high level, see the [dplyr programming 7 | #' vignette](https://dplyr.tidyverse.org/articles/programming.html) 8 | #' and the [ggplot2 in packages 9 | #' vignette](https://ggplot2.tidyverse.org/articles/ggplot2-in-packages.html). 10 | #' The [Metaprogramming 11 | #' section](https://adv-r.hadley.nz/metaprogramming.html) of [Advanced 12 | #' R](https://adv-r.hadley.nz) may also be useful for a deeper dive. 13 | #' 14 | #' * The tidy eval operators `{{`, `!!`, and `!!!` are syntactic 15 | #' constructs which are specially interpreted by tidy eval functions. 16 | #' You will mostly need `{{`, as `!!` and `!!!` are more advanced 17 | #' operators which you should not have to use in simple cases. 18 | #' 19 | #' The curly-curly operator `{{` allows you to tunnel data-variables 20 | #' passed from function arguments inside other tidy eval functions. 21 | #' `{{` is designed for individual arguments. To pass multiple 22 | #' arguments contained in dots, use `...` in the normal way. 23 | #' 24 | #' ``` 25 | #' my_function <- function(data, var, ...) { 26 | #' data %>% 27 | #' group_by(...) %>% 28 | #' summarise(mean = mean({{ var }})) 29 | #' } 30 | #' ``` 31 | #' 32 | #' * [enquo()] and [enquos()] delay the execution of one or several 33 | #' function arguments. The former returns a single expression, the 34 | #' latter returns a list of expressions. Once defused, expressions 35 | #' will no longer evaluate on their own. They must be injected back 36 | #' into an evaluation context with `!!` (for a single expression) and 37 | #' `!!!` (for a list of expressions). 38 | #' 39 | #' ``` 40 | #' my_function <- function(data, var, ...) { 41 | #' # Defuse 42 | #' var <- enquo(var) 43 | #' dots <- enquos(...) 44 | #' 45 | #' # Inject 46 | #' data %>% 47 | #' group_by(!!!dots) %>% 48 | #' summarise(mean = mean(!!var)) 49 | #' } 50 | #' ``` 51 | #' 52 | #' In this simple case, the code is equivalent to the usage of `{{` 53 | #' and `...` above. Defusing with `enquo()` or `enquos()` is only 54 | #' needed in more complex cases, for instance if you need to inspect 55 | #' or modify the expressions in some way. 56 | #' 57 | #' * The `.data` pronoun is an object that represents the current 58 | #' slice of data. If you have a variable name in a string, use the 59 | #' `.data` pronoun to subset that variable with `[[`. 60 | #' 61 | #' ``` 62 | #' my_var <- "disp" 63 | #' mtcars %>% summarise(mean = mean(.data[[my_var]])) 64 | #' ``` 65 | #' 66 | #' * Another tidy eval operator is `:=`. It makes it possible to use 67 | #' glue and curly-curly syntax on the LHS of `=`. For technical 68 | #' reasons, the R language doesn't support complex expressions on 69 | #' the left of `=`, so we use `:=` as a workaround. 70 | #' 71 | #' ``` 72 | #' my_function <- function(data, var, suffix = "foo") { 73 | #' # Use `{{` to tunnel function arguments and the usual glue 74 | #' # operator `{` to interpolate plain strings. 75 | #' data %>% 76 | #' summarise("{{ var }}_mean_{suffix}" := mean({{ var }})) 77 | #' } 78 | #' ``` 79 | #' 80 | #' * Many tidy eval functions like `dplyr::mutate()` or 81 | #' `dplyr::summarise()` give an automatic name to unnamed inputs. If 82 | #' you need to create the same sort of automatic names by yourself, 83 | #' use `as_label()`. For instance, the glue-tunnelling syntax above 84 | #' can be reproduced manually with: 85 | #' 86 | #' ``` 87 | #' my_function <- function(data, var, suffix = "foo") { 88 | #' var <- enquo(var) 89 | #' prefix <- as_label(var) 90 | #' data %>% 91 | #' summarise("{prefix}_mean_{suffix}" := mean(!!var)) 92 | #' } 93 | #' ``` 94 | #' 95 | #' Expressions defused with `enquo()` (or tunnelled with `{{`) need 96 | #' not be simple column names, they can be arbitrarily complex. 97 | #' `as_label()` handles those cases gracefully. If your code assumes 98 | #' a simple column name, use `as_name()` instead. This is safer 99 | #' because it throws an error if the input is not a name as expected. 100 | #' 101 | #' @md 102 | #' @name tidyeval 103 | #' @keywords internal 104 | #' @importFrom rlang enquo enquos .data := as_name as_label 105 | #' @aliases enquo enquos .data := as_name as_label 106 | #' @export enquo enquos .data := as_name as_label 107 | NULL 108 | -------------------------------------------------------------------------------- /_pkgdown.yml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chuiqin/irGSEA/e411feca4ecece57d287f192bf7521a28a2cc5d4/_pkgdown.yml -------------------------------------------------------------------------------- /docs/404.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 | 7 | 8 |YEAR: 2021 47 | COPYRIGHT HOLDER: Chuiqin Fan 48 |49 | 50 |
Copyright (c) 2021 Chuiqin Fan
49 |Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
50 |The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
51 |THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
52 |
47 | All functions48 | 49 | |
50 | |
---|---|
51 | 52 | | 53 |Stacked bar plot |
54 |
55 | 56 | | 57 |Bubble plot |
58 |
59 | 60 | | 61 |Density Scatter plot |
62 |
63 | 64 | | 65 |Density heatmap |
66 |
67 | 68 | | 69 |Half vlnplot |
70 |
71 | 72 | | 73 |Heatmap plot |
74 |
75 | 76 | | 77 |Calculate the hub gene of the geneset |
78 |
79 | 80 | | 81 |Integrate differential gene set calculated by all enrichment score matrixes |
82 |
83 | 84 | | 85 |Ridge plot 86 | Easy to show the data distribution by ridge plot |
87 |
88 | 89 | | 90 |Calculate enrichment scores from scRNA-seq data |
91 |
92 | 93 | | 94 |Upset plot |
95 |
96 | 97 | | 98 |Half vlnplot |
99 |
R/irGSEA.density.scatterplot.R
45 | irGSEA.density.scatterplot.Rd
Easy to the data distribution by density scatter plot
50 |irGSEA.density.scatterplot(
54 | object = NULL,
55 | method = NULL,
56 | show.geneset = NULL,
57 | reduction = "umap",
58 | ...
59 | )
A Seurat after perform irGSEA.score
A character. It should be one of the followling : AUCell, 70 | UCell, singscore, ssgsea.
A character. It should be one of the rownames of 75 | enrichment score matrix.
A character. It can not be empty and should be calculated 80 | in advance.
More parameters pass to plot_density
density scatter plot
92 |if (FALSE) {
97 | # load PBMC dataset by R package SeuratData
98 | library(Seurat)
99 | library(SeuratData)
100 | # download 3k PBMCs from 10X Genomics
101 | InstallData("pbmc3k")
102 | data("pbmc3k.final")
103 | pbmc3k.final <- SeuratObject::UpdateSeuratObject(pbmc3k.final)
104 |
105 | # Seurat object
106 | pbmc3k.final <- irGSEA.score(object = pbmc3k.final, assay = "RNA",
107 | slot = "data", msigdb = T, species = "Homo sapiens",
108 | category = "H", geneid = "symbol",
109 | method = c("AUCell", "UCell", "singscore", "ssgsea"), kcdf = 'Gaussian')
110 |
111 | irGSEA.density.scatterplot1 <- irGSEA.density.scatterplot(object = pbmc3k.final,
112 | method = "UCell", show.geneset = "HALLMARK-INFLAMMATORY-RESPONSE",
113 | reduction = "umap")
114 | irGSEA.density.scatterplot2 <- irGSEA.density.scatterplot(object = pbmc3k.final,
115 | method = "ssgsea", show.geneset = "HALLMARK-IL6-JAK-STAT3-SIGNALING",
116 | reduction = "umap")
117 |
118 | }
119 |
120 |
121 |
Easy to show the data distribution by half vlnplot
50 |irGSEA.halfvlnplot(
54 | object = NULL,
55 | method = NULL,
56 | show.geneset = NULL,
57 | group.by = NULL,
58 | color.cluster = NULL,
59 | cluster.levels = NULL
60 | )
A Seurat after perform irGSEA.score
A character. It should be one of the followling : AUCell, 71 | UCell, singscore, ssgsea.
A character. It should be one of the rownames of 76 | enrichment score matrix.
Default ident when it is set to NULL. You can specify other 81 | column of metadata.
A vector. Default "ggsci::pal_igv()(the number of colnames 86 | of enrichment score matrix)" when it is set to NULL.
A vector equal to the number of clusters.
half vlnplot
98 |if (FALSE) {
103 | # load PBMC dataset by R package SeuratData
104 | library(Seurat)
105 | library(SeuratData)
106 | # download 3k PBMCs from 10X Genomics
107 | InstallData("pbmc3k")
108 | data("pbmc3k.final")
109 | pbmc3k.final <- SeuratObject::UpdateSeuratObject(pbmc3k.final)
110 |
111 | # Seurat object
112 | pbmc3k.final <- irGSEA.score(object = pbmc3k.final, assay = "RNA",
113 | slot = "data", msigdb = T, species = "Homo sapiens",
114 | category = "H", geneid = "symbol",
115 | method = c("AUCell", "UCell", "singscore", "ssgsea"), kcdf = 'Gaussian')
116 |
117 | irGSEA.halfvlnplot.plot1 <- irGSEA.halfvlnplot(object = pbmc3k.final,
118 | method = "UCell", show.geneset = "HALLMARK-INFLAMMATORY-RESPONSE")
119 | irGSEA.halfvlnplot.plot2 <- irGSEA.halfvlnplot(object = pbmc3k.final,
120 | method = "ssgsea", show.geneset = "HALLMARK-IL6-JAK-STAT3-SIGNALING")
121 |
122 |
123 | }
124 |
125 |
See magrittr::%>%
for details.
lhs %>% rhs
A value or the magrittr placeholder.
A function call using the magrittr semantics.
The result of calling `rhs(lhs)`.
71 |