### Download and Prepare Example Dataset Source: https://github.com/chuiqin/irgsea/blob/master/README.md Downloads the pbmc3k.final dataset from 10x genomics and extracts it. This dataset is intended for use with the irGSEA package and is prepared by loading necessary R libraries (dplyr, Seurat) and organizing the downloaded files. ```r # load library library(dplyr) library(Seurat) # download the pbmc3k.final dataset # the dataset is the output of the Cell Ranger pipeline from 10X # decompress the file to the specified directory download.file("https://cf.10xgenomics.com/samples/cell/pbmc3k.final3k/pbmc3k.final3k_filtered_gene_bc_matrices.tar.gz", destfile = "./pbmc3k.final3k_filtered_gene_bc_matrices.tar.gz") untar("./pbmc3k.final3k_filtered_gene_bc_matrices.tar.gz", exdir = "./") # Load the pbmc3k.final dataset # Initialize the Seurat object with the raw (non-normalized data) ``` -------------------------------- ### Install SeuratDisk from Git Source: https://github.com/chuiqin/irgsea/blob/master/README.md Installs the SeuratDisk package from a Git repository, which is a requirement for GSVApy and ssGSEApy. The installation is conditional on SeuratDisk not being currently installed. ```r if (!requireNamespace("SeuratDisk", quietly = TRUE)) { devtools::install_git("https://gitee.com/fan_chuiqin/seurat-disk.git", force =T)} ``` -------------------------------- ### Install gficf from Git Source: https://github.com/chuiqin/irgsea/blob/master/README.md Installs the gficf package from a Git repository. This package is dependent on RcppML. Installation is performed only if gficf is not already present in the R environment. ```r if (!requireNamespace("gficf", quietly = TRUE)) { devtools::install_git("https://gitee.com/fan_chuiqin/gficf.git", force =T) } ``` -------------------------------- ### Install scde from Git Source: https://github.com/chuiqin/irgsea/blob/master/README.md Installs the scde package from a Git repository. This is noted as a dependency for pagoda2. The installation is conditional on scde not being currently installed. ```r if (!requireNamespace("scde", quietly = TRUE)) { devtools::install_git("https://gitee.com/fan_chuiqin/scde.git", force =T) } ``` -------------------------------- ### Install irGSEA from Git Source: https://github.com/chuiqin/irgsea/blob/master/README.md Installs the irGSEA package directly from a Git repository. This is useful for development versions or specific forks. It checks for the package's existence before attempting installation. ```r if (!requireNamespace("irGSEA", quietly = TRUE)) { devtools::install_git("https://gitee.com/fan_chuiqin/irGSEA.git", force =T) } ``` -------------------------------- ### Install sargent from Git Source: https://github.com/chuiqin/irgsea/blob/master/README.md Installs the sargent package from a Git repository. This installation is performed only if the sargent package is not already found in the current R session. ```r if (!requireNamespace("sargent", quietly = TRUE)) { devtools::install_git("https://gitee.com/fan_chuiqin/PMCB-Sargent.git", force =T)} ``` -------------------------------- ### Install VISION from Git Source: https://github.com/chuiqin/irgsea/blob/master/README.md Installs the VISION package from a Git repository. This package is likely a dependency or complementary tool for irGSEA. The installation proceeds only if VISION is not already detected. ```r if (!requireNamespace("VISION", quietly = TRUE)) { devtools::install_git("https://gitee.com/fan_chuiqin/VISION.git", force =T) } ``` -------------------------------- ### Optional: Install sargent from Gitee Source: https://github.com/chuiqin/irgsea/blob/master/README.md Installs the sargent R package from a Gitee repository. This is an alternative installation method for the sargent package. ```r if (!requireNamespace("sargent", quietly = TRUE)) { # devtools::install_github("Sanofi-Public/PMCB-Sargent", force =T) devtools::install_git("https://gitee.com/fan_chuiqin/PMCB-Sargent.git", force =T) } ``` -------------------------------- ### Optional: Install RcppML and gficf from GitHub Source: https://github.com/chuiqin/irgsea/blob/master/README.md Installs RcppML and gficf R packages from GitHub. It includes a version check for RcppML, ensuring it's updated to at least version 0.3.7 before installing gficf. ```r if (!utils::packageVersion("RcppML") > "0.3.7") { message("The version of RcppML should greater than 0.3.7 and install RcppML package from Github") devtools::install_github("zdebruine/RcppML", force =T) } # please first `library(RcppML)` if you want to perform gficf if (!requireNamespace("gficf", quietly = TRUE)) { devtools::install_github("gambalab/gficf", force =T) } ``` -------------------------------- ### Load Pre-organized PBMC Dataset with SeuratData Source: https://github.com/chuiqin/irgsea/blob/master/README.md Demonstrates how to load pre-packaged single-cell datasets, specifically the 'pbmc3k' dataset from 10X Genomics, using the SeuratData package. This simplifies data access for benchmarking and tutorial purposes. ```r # devtools::install_github('satijalab/seurat-data') library(SeuratData) # view all available datasets View(AvailableData()) # download 3k PBMCs from 10X Genomics InstallData("pbmc3k") # the details of pbmc3k.final ?pbmc3k.final ``` -------------------------------- ### Install ranger from Git Source: https://github.com/chuiqin/irgsea/blob/master/README.md Installs the ranger package from a Git repository, noting its dependency for 'mdt'. It ensures the package is installed only if it's not currently available in the R environment. ```r if (!requireNamespace("ranger", quietly = TRUE)) { devtools::install_git("https://gitee.com/fan_chuiqin/ranger.git", force =T) } ``` -------------------------------- ### Install R Packages from CRAN Source: https://github.com/chuiqin/irgsea/blob/master/README.md Installs a list of specified R packages from CRAN if they are not already installed. This ensures all necessary utility packages are available for irGSEA. ```r cran.packages <- c("aplot", "BiocManager", "circlize", "cowplot","data.table", "devtools", "doParallel", "doRNG", "dplyr", "ggfun", "gghalves", "ggplot2", "ggplotify", "ggridges", "ggsci", "irlba", "magrittr", "Matrix", "msigdbr", "pagoda2", "plyr", "pointr", "purrr", "RcppML", "readr", "reshape2", "reticulate", "rlang", "RMTstat", "RobustRankAggreg", "roxygen2", "Seurat", "SeuratObject", "stringr", "tibble", "tidyr", "tidyselect", "tidytree", "VAM") for (i in cran.packages) { if (!requireNamespace(i, quietly = TRUE)) { install.packages(i, ask = F, update = F) } } ``` -------------------------------- ### Install RcppML from Git (Version Check) Source: https://github.com/chuiqin/irgsea/blob/master/README.md Installs the RcppML package from Git if its current version is not greater than 0.3.7. This ensures the necessary version of RcppML is installed for packages like 'gficf'. A message is displayed if an update is required. ```r if (!utils::packageVersion("RcppML") > "0.3.7") { message("The version of RcppML should greater than 0.3.7 and install RcppML package from Git") devtools::install_git("https://gitee.com/fan_chuiqin/RcppML.git", force =T) } ``` -------------------------------- ### Install Bioconductor Packages for irGSEA Source: https://github.com/chuiqin/irgsea/blob/master/README.md Installs a list of R packages from Bioconductor using the BiocManager. It verifies the presence of each package before installation, ensuring a complete set of Bioconductor-based dependencies for irGSEA. ```r bioconductor.packages <- c("AUCell", "BiocParallel", "ComplexHeatmap", "decoupleR", "fgsea", "ggtree", "GSEABase", "GSVA", "Nebulosa", "scde", "singscore", "SummarizedExperiment", "UCell", "viper") for (i in bioconductor.packages) { if (!requireNamespace(i, quietly = TRUE)) { BiocManager::install(i, ask = F, update = F) } } ``` -------------------------------- ### Configure Package Repository Mirrors Source: https://github.com/chuiqin/irgsea/blob/master/README.md Sets mirror options for Bioconductor and CRAN repositories. This can accelerate package downloads by using geographically closer mirror servers. ```r options(BioC_mirror="https://mirrors.tuna.tsinghua.edu.cn/bioconductor/") options("repos" = c(CRAN="http://mirrors.cloud.tencent.com/CRAN/")) ``` -------------------------------- ### Install R Packages from Bioconductor Source: https://github.com/chuiqin/irgsea/blob/master/README.md Installs a list of specified R packages from Bioconductor if they are not already installed. These packages are essential for gene set enrichment analysis functionalities within irGSEA. ```r bioconductor.packages <- c("AUCell", "BiocParallel", "ComplexHeatmap", "decoupleR", "fgsea", "ggtree", "GSEABase", "GSVA", "Nebulosa", "scde", "singscore", "SummarizedExperiment", "UCell", "viper","sparseMatrixStats") for (i in bioconductor.packages) { if (!requireNamespace(i, quietly = TRUE)) { BiocManager::install(i, ask = F, update = F) } } ``` -------------------------------- ### Install CRAN Packages for irGSEA Source: https://github.com/chuiqin/irgsea/blob/master/README.md Installs a list of R packages from the Comprehensive R Archive Network (CRAN). It checks if each package is already installed and installs it if not. This ensures all necessary R libraries are available for irGSEA. ```r cran.packages <- c("aplot", "BiocManager", "circlize", "cowplot", "data.table", "devtools", "doParallel", "doRNG", "dplyr", "ggfun", "gghalves", "ggplot2", "ggplotify", "ggridges", "ggsci", "irlba", "magrittr", "Matrix", "msigdbr", "pagoda2", "plyr", "pointr", "purrr", "RcppML", "readr", "reshape2", "reticulate", "rlang", "RMTstat", "RobustRankAggreg", "roxygen2", "Seurat", "SeuratObject", "stringr", "tibble", "tidyr", "tidyselect", "tidytree", "VAM") for (i in cran.packages) { if (!requireNamespace(i, quietly = TRUE)) { install.packages(i, ask = F, update = F) } } ``` -------------------------------- ### Load and Visualize PBMC Dataset from SeuratData Source: https://github.com/chuiqin/irgsea/blob/master/README.md Loads the 'pbmc3k' dataset directly using SeuratData and Seurat, updates the Seurat object, and then visualizes the UMAP plot, grouping cells by their 'seurat_annotations' for initial inspection of cell populations. ```r library(Seurat) library(SeuratData) # loading dataset data("pbmc3k.final") pbmc3k.final <- UpdateSeuratObject(pbmc3k.final) # plot DimPlot(pbmc3k.final, reduction = "umap", group.by = "seurat_annotations",label = T) + NoLegend() ``` -------------------------------- ### Install Python Packages in Conda Environment Source: https://github.com/chuiqin/irgsea/blob/master/README.md Installs a list of required Python packages (anndata, scanpy, argparse, gseapy, decoupler) into the 'irGSEA' Conda environment using pip. It checks if each package is already installed and installs it if missing, specifying a PyPI mirror for potentially faster downloads. ```r # if python package exist python.package <- reticulate::py_list_packages(envname = "irGSEA")$package require.package <- c("anndata", "scanpy", "argparse", "gseapy", "decoupler") for (i in require.package) { if (! i %in% python.package) { reticulate::conda_install(envname = "irGSEA", packages = i, pip = T, pip_options = "-i https://pypi.tuna.tsinghua.edu.cn/simple") } } ``` -------------------------------- ### Optional: Install gficf from GitHub Source: https://github.com/chuiqin/irgsea/blob/master/docs/index.html Installs the 'gficf' package from GitHub. This package is required for specific analysis methods and relies on an up-to-date RcppML installation. ```r if (!requireNamespace("gficf", quietly = TRUE)) { devtools::install_github("gambalab/gficf", force =T) } ``` -------------------------------- ### Install irGSEA from GitHub Source: https://github.com/chuiqin/irgsea/blob/master/docs/index.html Installs the irGSEA package directly from its GitHub repository. This command ensures that the latest version of the package is installed. ```r if (!requireNamespace("irGSEA", quietly = TRUE)) { devtools::install_github("chuiqin/irGSEA", force =T) } ``` -------------------------------- ### Create Conda Environment for irGSEA Source: https://github.com/chuiqin/irgsea/blob/master/README.md Creates a new Conda environment named 'irGSEA' if it does not already exist. This environment will host the necessary Python packages for irGSEA functionality. It includes a note about installing Miniconda if the `conda_create` function fails. ```r # If error (Unable to find conda binary. Is Anaconda installed) occurs, # please perform `reticulate::install_miniconda()` if (! "irGSEA" %in% reticulate::conda_list()$name) { reticulate::conda_create("irGSEA") } ``` -------------------------------- ### Optional: Install sargent from GitHub Source: https://github.com/chuiqin/irgsea/blob/master/docs/index.html Installs the 'sargent' package from GitHub, a tool for specific single-cell analysis tasks. This installation is optional and depends on the user's analytical needs. ```r if (!requireNamespace("sargent", quietly = TRUE)) { devtools::install_github("Sanofi-Public/PMCB-Sargent", force =T) } ``` -------------------------------- ### Load and Preprocess PBMC Data with Seurat Source: https://github.com/chuiqin/irgsea/blob/master/README.md Loads 10X Genomics PBMC data, creates a Seurat object, and applies initial quality control filters based on gene counts, cell features, mitochondrial gene percentage, and hemoglobin gene percentage. This is a crucial step for removing low-quality cells and debris. ```r pbmc3k.final <- Read10X(data.dir = "./filtered_gene_bc_matrices/hg19/") options(Seurat.object.assay.version = "v3") pbmc3k.final <- CreateSeuratObject(counts = pbmc3k.final, min.cells = 3, min.features = 200) pbmc3k.final pbmc3k.final[["percent.mt"]] <- PercentageFeatureSet(pbmc3k.final, pattern = "^MT-") # pbmc3k.final[["percent.ribo"]] <- PercentageFeatureSet(pbmc3k.final, pattern = "^RP[L|S]") HB.genes_total <- c("HBA1","HBA2","HBB","HBD","HBE1","HBG1","HBG2","HBM","HBQ1","HBZ") HB_m <- match(HB.genes_total,rownames(pbmc3k.final)) HB.genes <- rownames(pbmc3k.final@assays$RNA)[HB_m] HB.genes <- HB.genes[!is.na(HB.genes)] pbmc3k.final[["percent.HB"]] <- PercentageFeatureSet(pbmc3k.final, features=HB.genes) pbmc3k.final <- subset(pbmc3k.final, subset = nFeature_RNA > 200 & nFeature_RNA < 2500 & percent.mt < 5 & percent.HB < 10) ``` -------------------------------- ### Show Gene Set Expression with Density Heatmap Source: https://github.com/chuiqin/irgsea/blob/master/README.md Visualizes the expression and distribution of a specific gene set ('HALLMARK-INFLAMMATORY-RESPONSE') using a density heatmap with the UCell method among clusters. Requires an object, method, and gene set name. ```r densityheatmap <- irGSEA.densityheatmap(object = pbmc3k.final, method = "UCell", show.geneset = "HALLMARK-INFLAMMATORY-RESPONSE") densityheatmap ``` -------------------------------- ### Show Gene Set Expression with Density Scatterplot Source: https://github.com/chuiqin/irgsea/blob/master/README.md Visualizes the expression and distribution of a specific gene set ('HALLMARK-INFLAMMATORY-RESPONSE') using a density scatterplot with the UCell method on a UMAP reduction. Requires an object, method, gene set name, and reduction type. ```r scatterplot <- irGSEA.density.scatterplot(object = pbmc3k.final, method = "UCell", show.geneset = "HALLMARK-INFLAMMATORY-RESPONSE", reduction = "umap") scatterplot ``` -------------------------------- ### Download and Process KEGG Pathways with clusterProfiler in R Source: https://github.com/chuiqin/irgsea/blob/master/README.md Downloads KEGG pathways for humans using clusterProfiler's gson_KEGG function, saves them as a gson file, and then reads and converts the data into a list of gene symbols per pathway. This format is suitable for downstream analysis with packages like irGSEA. It requires the 'clusterProfiler', 'tidyverse', and 'gson' packages. ```r # load library library(clusterProfiler) library(tidyverse) ### kegg ### # download kegg pathway (human) and write as gson file kk <- clusterProfiler::gson_KEGG(species = "hsa") gson::write.gson(kk, file = "./KEGG_20231128.gson") # read gson file kk2 <- gson::read.gson("./KEGG_20231128.gson") # Convert to a data frame kegg.list <- dplyr::left_join(kk2@gsid2name, kk2@gsid2gene, by = "gsid") head(kegg.list) # gsid name gene # 1 hsa01100 Metabolic pathways 10 # 2 hsa01100 Metabolic pathways 100 # 3 hsa01100 Metabolic pathways 10005 # 4 hsa01100 Metabolic pathways 10007 # 5 hsa01100 Metabolic pathways 100137049 # 6 hsa01100 Metabolic pathways 10020 # Convert gene ID to gene symbol gene_name <- clusterProfiler::bitr(kegg.list$gene, fromType = "ENTREZID", toType = "SYMBOL", OrgDb = "org.Hs.eg.db") kegg.list <- dplyr::full_join(kegg.list, gene_name, by = c("gene"="ENTREZID")) # remove NA value if exist kegg.list <- kegg.list[complete.cases(kegg.list[, c("gene", "SYMBOL")]), ] head(kegg.list) # gsid name gene SYMBOL # 1 hsa01100 Metabolic pathways 10 NAT2 # 2 hsa01100 Metabolic pathways 100 ADA # 3 hsa01100 Metabolic pathways 10005 ACOT8 # 4 hsa01100 Metabolic pathways 10007 GNPDA1 # 5 hsa01100 Metabolic pathways 100137049 PLA2G4B # 6 hsa01100 Metabolic pathways 10020 GNE # convert to list required by irGSEA package kegg.list$name <- factor(kegg.list$name) kegg.list <- kegg.list %>% dplyr::group_split(name, .keep = F) %>% purrr::map( ~.x %>% dplyr::pull(SYMBOL) %>% unique(.)) %>% purrr::set_names(levels(kegg.list$name)) head(kegg.list) ``` -------------------------------- ### Install CRAN Packages for irGSEA Source: https://github.com/chuiqin/irgsea/blob/master/docs/index.html Installs essential R packages from CRAN that are dependencies for irGSEA. It checks if each package is already installed before attempting to install it, ensuring a clean installation process. ```r cran.packages <- c("aplot", "BiocManager", "circlize", "cowplot","data.table", "devtools", "doParallel", "doRNG", "dplyr", "ggfun", "gghalves", "ggplot2", "ggplotify", "ggridges", "ggsci", "irlba", "magrittr", "Matrix", "msigdbr", "pagoda2", "plyr", "pointr", "purrr", "RcppML", "readr", "reshape2", "reticulate", "rlang", "RMTstat", "RobustRankAggreg", "roxygen2", "Seurat", "SeuratObject", "stringr", "tibble", "tidyr", "tidyselect", "tidytree", "VAM") for (i in cran.packages) { if (!requireNamespace(i, quietly = TRUE)) { install.packages(i, ask = F, update = F) } } ``` -------------------------------- ### Convert MSigDB GO BP to irGSEA List (R) Source: https://github.com/chuiqin/irgsea/blob/master/README.md This snippet processes the MSigDB dataset to extract Gene Ontology Biological Process (GO BP) gene sets. It filters for terms containing 'GOBP_', extracts unique genes, and structures the output as a list for irGSEA. ```r # convert to list[go bp] required by irGSEA package msigdb.go.bp <- msigdb %>% dplyr::filter(str_detect(term, pattern = regex("GOBP_", ignore_case=TRUE))) msigdb.go.bp$term <- factor(msigdb.go.bp$term) msigdb.go.bp <- msigdb.go.bp %>% dplyr::group_split(term, .keep = F) %>% purrr::map( ~.x %>% dplyr::pull(gene) %>% unique(.)) %>% purrr::set_names(levels(msigdb.go.bp$term)) ``` -------------------------------- ### Install Bioconductor Packages in R Source: https://github.com/chuiqin/irgsea/blob/master/docs/index.html Installs a list of R packages from Bioconductor. It leverages the BiocManager package to handle the installation process. This is crucial for bioinformatics-specific tools. ```R # install packages from Bioconductor bioconductor.packages <- c("AUCell", "BiocParallel", "ComplexHeatmap", "decoupleR", "fgsea", "ggtree", "GSEABase", "GSVA", "Nebulosa", "scde", "singscore", "SummarizedExperiment", "UCell", "viper") for (i in bioconductor.packages) { if (!requireNamespace(i, quietly = TRUE)) { BiocManager::install(i, ask = F, update = F) } } ``` -------------------------------- ### Install Bioconductor Packages for irGSEA Source: https://github.com/chuiqin/irgsea/blob/master/docs/index.html Installs required R packages from Bioconductor, which are crucial for irGSEA's functionality. Similar to CRAN package installation, it verifies existing installations before proceeding. ```r bioconductor.packages <- c("AUCell", "BiocParallel", "ComplexHeatmap", "decoupleR", "fgsea", "ggtree", "GSEABase", "GSVA", "Nebulosa", "scde", "singscore", "SummarizedExperiment", "UCell", "viper","sparseMatrixStats") for (i in bioconductor.packages) { if (!requireNamespace(i, quietly = TRUE)) { BiocManager::install(i, ask = F, update = F) } } ``` -------------------------------- ### Install CRAN Packages in R Source: https://github.com/chuiqin/irgsea/blob/master/docs/index.html Installs a list of R packages from the Comprehensive R Archive Network (CRAN). It checks if each package is already installed and installs it if not. This ensures all required R dependencies are met for the project. ```R options("repos" = c(CRAN="http://mirrors.cloud.tencent.com/CRAN/")) # install packages from CRAN cran.packages <- c("aplot", "BiocManager", "circlize", "cowplot", "data.table", "devtools", "doParallel", "doRNG", "dplyr", "ggfun", "gghalves", "ggplot2", "ggplotify", "ggridges", "ggsci", "irlba", "magrittr", "Matrix", "msigdbr", "pagoda2", "plyr", "pointr", "purrr", "RcppML", "readr", "reshape2", "reticulate", "rlang", "RMTstat", "RobustRankAggreg", "roxygen2", "Seurat", "SeuratObject", "stringr", "tibble", "tidyr", "tidyselect", "tidytree", "VAM") for (i in cran.packages) { if (!requireNamespace(i, quietly = TRUE)) { install.packages(i, ask = F, update = F) } } ``` -------------------------------- ### Download and Process GO Biological Process with clusterProfiler in R Source: https://github.com/chuiqin/irgsea/blob/master/README.md Downloads Gene Ontology (GO) Biological Process (BP) terms for humans using clusterProfiler's gson_GO function and saves them as a gson file. It then reads this file and converts the GO terms and their associated genes into a list format, mapping gene IDs to symbols. This processed list is prepared for use with other analysis packages. Requires 'clusterProfiler', 'tidyverse', and 'gson'. ```r ### go bp ### # download go bp (human) and write as gson file go <- clusterProfiler::gson_GO(OrgDb = "org.Hs.eg.db", ont = "BP") gson::write.gson(go, file = "./go_20231128.gson") # read gson file go2 <- gson::read.gson("./go_20231128.gson") # Convert to a data frame go.list <- dplyr::left_join(go2@gsid2name, go2@gsid2gene, by = "gsid") head(go.list) # gsid name gene # 1 GO:0000001 mitochondrion inheritance # 2 GO:0000002 mitochondrial genome maintenance 142 # 3 GO:0000002 mitochondrial genome maintenance 291 # 4 GO:0000002 mitochondrial genome maintenance 1763 # 5 GO:0000002 mitochondrial genome maintenance 1890 # 6 GO:0000002 mitochondrial genome maintenance 2021 ``` -------------------------------- ### Install Git Packages in R using devtools Source: https://github.com/chuiqin/irgsea/blob/master/docs/index.html Installs R packages directly from Git repositories. This is used for packages not available on CRAN or Bioconductor, such as 'irGSEA', 'VISION', 'ranger', 'RcppML', 'gficf', 'SeuratDisk', 'sargent', and 'scde'. Ensures specific versions or custom forks can be installed. ```R # install packages from git if (!requireNamespace("irGSEA", quietly = TRUE)) { devtools::install_git("https://gitee.com/fan_chuiqin/irGSEA.git", force =T) } # VISION if (!requireNamespace("VISION", quietly = TRUE)) { devtools::install_git("https://gitee.com/fan_chuiqin/VISION.git", force =T) } # mdt need ranger if (!requireNamespace("ranger", quietly = TRUE)) { devtools::install_git("https://gitee.com/fan_chuiqin/ranger.git", force =T) } # gficf need RcppML (version > 0.3.7) package if (!utils::packageVersion("RcppML") > "0.3.7") { message("The version of RcppML should greater than 0.3.7 and install RcppML package from Git") devtools::install_git("https://gitee.com/fan_chuiqin/RcppML.git", force =T) } # please first `library(RcppML)` if you want to perform gficf if (!requireNamespace("gficf", quietly = TRUE)) { devtools::install_git("https://gitee.com/fan_chuiqin/gficf.git", force =T) } # GSVApy and ssGSEApy need SeuratDisk package if (!requireNamespace("SeuratDisk", quietly = TRUE)) { devtools::install_git("https://gitee.com/fan_chuiqin/seurat-disk.git", force =T)} # sargent if (!requireNamespace("sargent", quietly = TRUE)) { devtools::install_git("https://gitee.com/fan_chuiqin/PMCB-Sargent.git", force =T)} # pagoda2 need scde package if (!requireNamespace("scde", quietly = TRUE)) { devtools::install_git("https://gitee.com/fan_chuiqin/scde.git", force =T) } ``` -------------------------------- ### Download and Parse Msigdb SQLite Database for Gene Sets (R) Source: https://github.com/chuiqin/irgsea/blob/master/README.md This code downloads the Msigdb database (v2023.2.Hs.db.zip), unzips it, and connects to the SQLite database. It then joins several tables to extract gene set information, including collection, subcollection, gene set name, description, and gene symbols. Finally, it disconnects from the database and prepares specific subsets (Hallmarks and GO Biological Process) as lists of gene symbols per gene set. ```r #### work with newest Msigdb #### # https://data.broadinstitute.org/gsea-msigdb/msigdb/release/ # In this page, you can download human/mouse gmt file or db.zip file # The db.zip file contains metadata information for the gene set # load library library(clusterProfiler) library(tidyverse) library(DBI) library(RSQLite) ### db.zip ### # download zip file and unzip zip file zip_url <- "https://data.broadinstitute.org/gsea-msigdb/msigdb/release/2023.2.Hs/msigdb_v2023.2.Hs.db.zip" local_zip_path <- "./msigdb_v2023.2.Hs.db.zip" download.file(zip_url, local_zip_path) unzip(local_zip_path, exdir = "./") # code modified by https://rdrr.io/github/cashoes/sear/src/data-raw/1_parse_msigdb_sqlite.r con <- DBI::dbConnect(RSQLite::SQLite(), dbname = './msigdb_v2023.2.Hs.db') DBI::dbListTables(con) # define tables we want to combine geneset_db <- dplyr::tbl(con, 'gene_set') # standard_name, collection_name details_db <- dplyr::tbl(con, 'gene_set_details') # description_brief, description_full geneset_genesymbol_db <- dplyr::tbl(con, 'gene_set_gene_symbol') # meat and potatoes genesymbol_db <- dplyr::tbl(con, 'gene_symbol') # mapping from ids to gene symbols collection_db <- dplyr::tbl(con, 'collection') %>% dplyr::select(collection_name, full_name) # collection metadata # join tables msigdb <- geneset_db %>% dplyr::left_join(details_db, by = c('id' = 'gene_set_id')) %>% dplyr::left_join(collection_db, by = 'collection_name') %>% dplyr::left_join(geneset_genesymbol_db, by = c('id' = 'gene_set_id')) %>% dplyr::left_join(genesymbol_db, by = c('gene_symbol_id' = 'id')) %>% dplyr::select(collection = collection_name, subcollection = full_name, geneset = standard_name, description = description_brief, symbol) %>% dplyr::as_tibble() # clean up DBI::dbDisconnect(con) unique(msigdb$collection) # [1] "C1" "C2:CGP" "C2:CP:BIOCARTA" # [4] "C2:CP:KEGG_LEGACY" "C2:CP:PID" "C3:MIR:MIRDB" # [7] "C3:MIR:MIR_LEGACY" "C3:TFT:GTRD" "C3:TFT:TFT_LEGACY" # [10] "C4:3CA" "C4:CGN" "C4:CM" # [13] "C6" "C7:IMMUNESIGDB" "C7:VAX" # [16] "C8" "C5:GO:BP" "C5:GO:CC" # [19] "C5:GO:MF" "H" "C5:HPO" # [22] "C2:CP:KEGG_MEDICUS" "C2:CP:REACTOME" "C2:CP:WIKIPATHWAYS" # [25] "C2:CP" unique(msigdb$subcollection) # [1] "C1" "C2:CGP" "C2:CP:BIOCARTA" # [4] "C2:CP:KEGG_LEGACY" "C2:CP:PID" "C3:MIR:MIRDB" # [7] "C3:MIR:MIR_LEGACY" "C3:TFT:GTRD" "C3:TFT:TFT_LEGACY" # [10] "C4:3CA" "C4:CGN" "C4:CM" # [13] "C6" "C7:IMMUNESIGDB" "C7:VAX" # [16] "C8" "C5:GO:BP" "C5:GO:CC" # [19] "C5:GO:MF" "H" "C5:HPO" # [22] "C2:CP:KEGG_MEDICUS" "C2:CP:REACTOME" "C2:CP:WIKIPATHWAYS" # [25] "C2:CP" # convert to list[hallmarker] required by irGSEA package msigdb.h <- msigdb %>% dplyr::filter(collection=="H") %>% dplyr::select(c("geneset", "symbol")) msigdb.h$geneset <- factor(msigdb.h$geneset) msigdb.h <- msigdb.h %>% dplyr::group_split(geneset, .keep = F) %>% purrr::map( ~.x %>% dplyr::pull(symbol) %>% unique(.)) %>% purrr::set_names(levels(msigdb.h$geneset)) # convert to list[go bp] required by irGSEA package msigdb.go.bp <- msigdb %>% dplyr::filter(collection=="C5:GO:BP") %>% dplyr::select(c("geneset", "symbol")) msigdb.go.bp$geneset <- factor(msigdb.go.bp$geneset) msigdb.go.bp <- msigdb.go.bp %>% dplyr::group_split(geneset, .keep = F) %>% purrr::map( ~.x %>% dplyr::pull(symbol) %>% unique(.)) %>% purrr::set_names(levels(msigdb.go.bp$geneset)) ``` -------------------------------- ### Install Python Packages in Conda Environment Source: https://github.com/chuiqin/irgsea/blob/master/docs/index.html Installs required Python packages (anndata, scanpy, argparse, gseapy, decoupler) into the 'irGSEA' Conda environment using pip. It checks for existing packages before installation. ```r python.package <- reticulate::py_list_packages(envname = "irGSEA")$package require.package <- c("anndata", "scanpy", "argparse", "gseapy", "decoupler") for (i in seq_along(require.package)) { if (i %in% python.package) { reticulate::conda_install(envname = "irGSEA", packages = i, pip = T) } } ``` -------------------------------- ### Optional: Install SeuratDisk from GitHub Source: https://github.com/chuiqin/irgsea/blob/master/docs/index.html Installs the 'SeuratDisk' package from GitHub, which is necessary for using 'GSVApy' and 'ssGSEApy'. This facilitates data interoperability between R and Python analysis pipelines. ```r if (!requireNamespace("SeuratDisk", quietly = TRUE)) { devtools::install_github("mojaveazure/seurat-disk", force =T) } ``` -------------------------------- ### Optional: Install scde from GitHub Source: https://github.com/chuiqin/irgsea/blob/master/docs/index.html Installs the 'scde' package from GitHub, which is a dependency for 'pagoda2'. This ensures that 'pagoda2' can be utilized for its intended analyses. ```r if (!requireNamespace("scde", quietly = TRUE)) { devtools::install_github("hms-dbmi/scde", force =T) } ``` -------------------------------- ### Example: Density Scatter Plot Generation in R Source: https://github.com/chuiqin/irgsea/blob/master/docs/reference/irGSEA.density.scatterplot.html Demonstrates the usage of irGSEA.density.scatterplot by first loading a PBMC dataset, performing enrichment analysis using irGSEA.score, and then generating two density scatter plots with different gene sets and methods. This example requires the Seurat and SeuratData packages. ```R if (FALSE) { # load PBMC dataset by R package SeuratData library(Seurat) library(SeuratData) # download 3k PBMCs from 10X Genomics InstallData("pbmc3k") data("pbmc3k.final") pbmc3k.final <- SeuratObject::UpdateSeuratObject(pbmc3k.final) # Seurat object pbmc3k.final <- irGSEA.score(object = pbmc3k.final, assay = "RNA", slot = "data", msigdb = T, species = "Homo sapiens", category = "H", geneid = "symbol", method = c("AUCell", "UCell", "singscore", "ssgsea"), kcdf = 'Gaussian') irGSEA.density.scatterplot1 <- irGSEA.density.scatterplot(object = pbmc3k.final, method = "UCell", show.geneset = "HALLMARK-INFLAMMATORY-RESPONSE", reduction = "umap") irGSEA.density.scatterplot2 <- irGSEA.density.scatterplot(object = pbmc3k.final, method = "ssgsea", show.geneset = "HALLMARK-IL6-JAK-STAT3-SIGNALING", reduction = "umap") } ``` -------------------------------- ### Example: Generating Density Heatmaps with irGSEA Source: https://github.com/chuiqin/irgsea/blob/master/docs/reference/irGSEA.densityheatmap.html Demonstrates how to use the irGSEA.densityheatmap function. It first loads necessary libraries and a sample dataset (pbmc3k.final), then performs irGSEA scoring using irGSEA.score. Finally, it generates two density heatmaps using irGSEA.densityheatmap with different methods ('UCell' and 'ssgsea') and specified gene sets. ```r if (FALSE) { # load PBMC dataset by R package SeuratData library(Seurat) library(SeuratData) # download 3k PBMCs from 10X Genomics InstallData("pbmc3k") data("pbmc3k.final") pbmc3k.final <- SeuratObject::UpdateSeuratObject(pbmc3k.final) # Seurat object pbmc3k.final <- irGSEA.score(object = pbmc3k.final, assay = "RNA", slot = "data", msigdb = T, species = "Homo sapiens", category = "H", geneid = "symbol", method = c("AUCell", "UCell", "singscore", "ssgsea"), kcdf = 'Gaussian') irGSEA.densityheatmap.plot1 <- irGSEA.densityheatmap(object = pbmc3k.final, method = "UCell", show.geneset = "HALLMARK-INFLAMMATORY-RESPONSE") irGSEA.densityheatmap.plot2 <- irGSEA.densityheatmap(object = pbmc3k.final, method = "ssgsea", show.geneset = "HALLMARK-IL6-JAK-STAT3-SIGNALING") } ``` -------------------------------- ### Optional: Install ranger from GitHub Source: https://github.com/chuiqin/irgsea/blob/master/docs/index.html Installs the 'ranger' package from GitHub, which is a dependency for the 'mdt' functionality. This ensures compatibility with related tools. ```r if (!requireNamespace("ranger", quietly = TRUE)) { devtools::install_github("imbs-hl/ranger", force =T) } ``` -------------------------------- ### Filter Angiogenesis Gene Sets (R) Source: https://github.com/chuiqin/irgsea/blob/master/README.md This code block identifies and extracts gene sets from MSigDB that are related to 'angiogenesis' or 'vessel' by searching both gene set names and descriptions. It then formats these into a list for irGSEA. ```r category <- c("angiogenesis", "vessel") msigdb.vessel <- list() for (i in category) { # Ignore case matching find.index.description <- stringr::str_detect(msigdb$description, pattern = regex(all_of(i), ignore_case=TRUE)) find.index.name <- stringr::str_detect(msigdb$geneset, pattern = regex(all_of(i), ignore_case=TRUE)) msigdb.vessel[[i]] <- msigdb[find.index.description | find.index.name, ] %>% mutate(category = i) } msigdb.vessel <- do.call(rbind, msigdb.vessel) # convert gene sets associated with angiogenesis to list # required by irGSEA package msigdb.vessel <- msigdb.vessel %>% dplyr::select(c("geneset", "symbol")) msigdb.vessel$geneset <- factor(msigdb.vessel$geneset) msigdb.vessel <- msigdb.vessel %>% dplyr::group_split(geneset, .keep = F) %>% purrr::map( ~.x %>% dplyr::pull(symbol) %>% unique(.)) %>% purrr::set_names(levels(msigdb.vessel$geneset)) ``` -------------------------------- ### Convert MSigDB Hallmarker to irGSEA List (R) Source: https://github.com/chuiqin/irgsea/blob/master/README.md This code filters the MSigDB dataset for Hallmarker gene sets, extracts unique gene symbols for each, and formats them into a list suitable for the irGSEA package. It uses regular expressions for filtering. ```r # convert to list[hallmarker] required by irGSEA package msigdb.h <- msigdb %>% dplyr::filter(str_detect(term, pattern = regex("HALLMARK_", ignore_case=TRUE))) msigdb.h$term <- factor(msigdb.h$term) msigdb.h <- msigdb.h %>% dplyr::group_split(term, .keep = F) %>% purrr::map( ~.x %>% dplyr::pull(gene) %>% unique(.)) %>% purrr::set_names(levels(msigdb.h$term)) ``` -------------------------------- ### Prepare Gene Ontology (BP) with clusterProfiler Source: https://github.com/chuiqin/irgsea/blob/master/docs/index.html This R code demonstrates downloading Gene Ontology Biological Process (BP) terms for humans using clusterProfiler and saving them as a gson file. It then reads the gson file and converts the GO data into a data frame format. Dependencies include clusterProfiler, tidyverse, and org.Hs.eg.db. ```R ### go bp ### # download go bp (human) and write as gson file go <- clusterProfiler::gson_GO(OrgDb = "org.Hs.eg.db", ont = "BP") gson::write.gson(go, file = "./go_20231128.gson") # read gson file go2 <- gson::read.gson("./go_20231128.gson") # Convert to a data frame go.list <- dplyr::left_join(go2@gsid2name, go2@gsid2gene, by = "gsid") head(go.list) ```