--- title: "MetaMarkers: Testing four placental datasets" output: html_document --- ```{r} # Load required libraries # if (!require("BiocManager", quietly = TRUE)) # install.packages("BiocManager") # BiocManager::install(version = "3.17") # BiocManager::install("SingleCellExperiment") # install.packages("Seurat") # install.packages("tidyverse") # if (!requireNamespace("devtools", quietly = TRUE)) # install.packages("devtools") # devtools::install_github("gillislab/MetaMarkers") library(SingleCellExperiment) library(Seurat) library(tidyverse) library(MetaMarkers) library(dplyr) ``` ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.align = "center", fig.width = 6, fig.height = 4.5 ) ``` ## Introduction Single-cell RNA-sequencing technologies have enabled the discovery and characterization of an incredible number of novel cell types and batches. However, cell types are usually measured in only one biological condition and subject to technical variability, making it difficult to identify robust markers, particularly for rare populations. MetaMarkers proposed a simple methodology to pool marker information across datasets while keeping datasets independent, identifying robust marker signatures and Here are some of the reasons why you may want to use MetaMarkers: - the pipeline is fast and easy to use, making it possible to aggregate a large number of datasets. - the more datasets you provide, the more technical and biological variability you sample, progressively increasing marker robustness. - MetaMarkers creates marker signatures that offer redundant information about cell types. For example, these robust signatures enable rapid cell type identification at the individual cell level, even if the main markers have not been measured in a particular cell due to dropout. - MetaMarkers offers the possibility to stratify marker selection by grouping cell types into broader classes, enabling combinatorial identification of cell types. What is required from the user’s perspective are: - a list of annotated datasets. - matching cell type names across datasets. To obtain matching annotation across datasets in an automated way, we suggest using Seurat to perform integrative clustering or MetaNeighbor to match highly replicable cell types. We illustrate how to create meta-marker lists by using 4 placenta datasets. We show how to create meta-marker lists, visualize and pick the best meta-markers. We then show how to create hierarchical lists by grouping cell types into trophoblast and non-trophoblast classes. ## Create marker lists for individual placental datasets Here, we will load the the datasets in the SingleCellExperiment format. Then, we need to make sure that gene names are identical across datasets. Here, the two datasets share the same format of gene names. Note that MetaMarkers will automatically remove duplicate gene names, so we don’t need to worry. For this trial, our objective is to create two marker sets for trophoblasts and non-trophoblasts. We will collapse the cell type information across the four datasets, and run MetaMarkers to get the marker sets that define trophoblasts vs non-trophoblasts. ```{r load_datasets, message=FALSE} vtdataset <- readRDS("/home/youkim/MetaMarkers/4-dataset-test/vtSeurat2_annotated.rds") sdataset <- readRDS("/home/youkim/MetaMarkers/4-dataset-test/sSeurat_20230515.rds") ldataset <- readRDS("/home/youkim/MetaMarkers/4-dataset-test/lSeurat_annotated.rds") hdataset <- readRDS("/home/youkim/MetaMarkers/4-dataset-test/hSeurat_annotated.rds") adataset <- readRDS("/home/youkim/Meta-analysis/Arutyanyan_et_al.2023/aSeurat_analyzed.rds") # Collapse the cell types in Liu dataset lmeta_copy <- ldataset@meta.data %>% select(Annotation) dim(ldataset@meta.data) # read the cell types data frame l_cell_types <- read.csv("/home/youkim/MetaMarkers/4-dataset-test/Liu Cell Types (P vs NP).csv") %>% dplyr::rename(Annotation = Annotations) # use inner_join() on hmeta_copy and h_cell_types df <- inner_join(lmeta_copy, l_cell_types) # Check if the order is maintained summary(df$Annotation == lmeta_copy$Annotation) # add to meta.data ldataset$merged_annotation <- df$Merged ``` ```{r} # Collapse the cell types in VT dataset vtmeta_copy <- vtdataset@meta.data %>% select(Annotation) # Do the same thing for the vtSeurat vt_cell_types <- read.csv("/home/youkim/MetaMarkers/4-dataset-test/VT Cell Types (P vs NP).csv") %>% dplyr::rename(Annotation = Annotations) # use inner_join() on vtmeta_copy and vt_cell_types df_vt <- inner_join(vtmeta_copy, vt_cell_types) # Check if the order is maintained summary(df_vt$Annotation == vtmeta_copy$Annotation) # add to meta.data vtdataset$merged_annotation <- df_vt$Merged ``` ```{r} # Collapse the cell types in Suryawanshi dataset smeta_copy <- sdataset@meta.data %>% select(CellType) %>% mutate(Annotation = CellType) sdataset@meta.data$CellType %>% table() # read the cell types data frame s_cell_types <- read.csv("/home/youkim/MetaMarkers/4-dataset-test/Suryawanshi Cell Types (P vs NP).csv") %>% dplyr::rename(Annotation = Annotations) # use inner_join() on smeta_copy and s_cell_types df_s <- inner_join(smeta_copy, s_cell_types) # Check if the order is maintained summary(df_s$Annotation == smeta_copy$Annotation) # add to meta.data sdataset$merged_annotation <- df_s$Merged ``` ```{r} # Collapse the cell types in Han dataset hmeta_copy <- hdataset@meta.data %>% select(Annotation) dim(hdataset@meta.data) # read the cell types data frame h_cell_types <- read.csv("/home/youkim/MetaMarkers/4-dataset-test/Han Cell Types (P vs NP).csv") %>% dplyr::rename(Annotation = Annotations) # use inner_join() on hmeta_copy and h_cell_types df <- inner_join(hmeta_copy, h_cell_types) # Check if the order is maintained summary(df$Annotation == hmeta_copy$Annotation) # add to meta.data hdataset$merged_annotation <- df$Merged # Collapse the cell types in Arutyanyan dataset ameta_copy <- adataset@meta.data %>% select(cell_type) %>% mutate(Annotation = cell_type) ameta_copy %<>% mutate(Annotation = as.data.frame(Idents(adataset))[,1]) # Check if order is preserved summary(ameta_copy$Annotation == Idents(adataset)) dim(adataset@meta.data) # read the cell types data frame a_cell_types <- read.csv("/home/youkim/Meta-analysis/Arutyanyan_et_al.2023/Arutyanyan Cell Types (P vs NP).csv") %>% dplyr::rename(Annotation = Annotations) # use inner_join() on lmeta_copy and l_cell_types df_a <- inner_join(ameta_copy, a_cell_types) # Check if the order is maintained summary(df_a$Annotation == ameta_copy$Annotation) # add to meta.data adataset$merged_annotation <- df_a$Merged # (I think we can use an integrated dataset at this point.) # Change the Idents of the Seurat Objects Idents(object = ldataset) <- "merged_annotation" Idents(object = vtdataset) <- "merged_annotation" Idents(object = sdataset) <- "merged_annotation" Idents(object = hdataset) <- "merged_annotation" Idents(object = adataset) <- "merged_annotation" # Convert to SCE objects lSCE <- as.SingleCellExperiment(ldataset) vtSCE <- as.SingleCellExperiment(vtdataset) sSCE <- as.SingleCellExperiment(sdataset) hSCE <- as.SingleCellExperiment(hdataset) aSCE <- as.SingleCellExperiment(adataset) ``` Next we need to match cell type names across datasets. Cell types are contained in the "label" column of the datasets (part of the colData slot, but can be accessed directly as shown here). ```{r display_cell_types} table(lSCE@colData$merged_annotation) table(vtSCE@colData$merged_annotation) table(sSCE@colData$merged_annotation) table(hSCE@colData$merged_annotation) table(aSCE@colData$merged_annotation) ``` Some cell types are only present in one dataset, but there is a good overlap for the main cell types. We update names of cell types that overlap but have different names. ```{r rename_cell_types} common_labels <- Reduce(intersect, list(unique(lSCE@colData$merged_annotation), unique(vtSCE@colData$merged_annotation), unique(sSCE@colData$merged_annotation), unique(hSCE@colData$merged_annotation), unique(aSCE@colData$merged_annotation))) common_labels ``` We only keep cell types that are common to both datasets. Note that this step is not strictly necessary: if we kept cell types present in only one dataset, the pipeline would run normally but the "meta-markers" for these cell types would be based on a single dataset. ```{r subset_datasets} lSCE <- lSCE[, lSCE@colData$merged_annotation %in% common_labels] vtSCE <- vtSCE[, vtSCE@colData$merged_annotation %in% common_labels] sSCE <- sSCE[, sSCE@colData$merged_annotation %in% common_labels] hSCE <- hSCE[, hSCE@colData$merged_annotation %in% common_labels] aSCE <- aSCE[, aSCE@colData$merged_annotation %in% common_labels] ``` The last step before computing markers is data normalization. The only requirement here is that the normalization is uniform across datasets for the meta-analytic stats to be meaningful. Any normalization procedure may be used, the MetaMarkers package offer a simple library size normalization procedure: ```{r compute_cpm} assay(lSCE, "cpm") <- convert_to_cpm(assay(lSCE)) assay(vtSCE, "cpm") <- convert_to_cpm(assay(vtSCE)) assay(sSCE, "cpm") = convert_to_cpm(assay(sSCE)) assay(hSCE, "cpm") = convert_to_cpm(assay(hSCE)) assay(aSCE, "cpm") = convert_to_cpm(assay(aSCE)) ``` We can now proceed to marker selection (based on the ROC test). In our experience, it is best to *not* use logarithmic scaling as it will provide better fold change evaluation. ```{r compute_markers} markers_lSCE <- compute_markers(assay(lSCE, "cpm"), lSCE@colData$merged_annotation) markers_vtSCE <- compute_markers(assay(vtSCE, "cpm"), vtSCE@colData$merged_annotation) markers_sSCE <- compute_markers(assay(sSCE, "cpm"), sSCE@colData$merged_annotation) markers_hSCE <- compute_markers(assay(hSCE, "cpm"), hSCE@colData$merged_annotation) markers_aSCE <- compute_markers(assay(aSCE, "cpm"), aSCE@colData$merged_annotation) ``` These functions provide marker lists for all cell types in a given dataset (ranked by AUROC). For example, we can visualize the best markers for the Trophoblast: ```{r show_syncytiotrophoblast_markers} dim(sdataset) dim(vtdataset) dim(ldataset) dim(hdataset) dim(adataset) markers_aSCE %>% filter(cell_type == "Proliferative") %>% select(gene) ``` Individual dataset markers can be saved to file for later use, which is particularly useful to accumulate datasets over time and regenerate more comprehensive meta-marker lists. ```{r export_markers} export_markers(markers_lSCE, "Liu_markers_20230615.csv") export_markers(markers_vtSCE, "VT_markers_20230615.csv") export_markers(markers_sSCE, "Suryawanshi_markers_20230615.csv") export_markers(markers_hSCE, "Han_markers_20230615.csv") export_markers(markers_aSCE, "Arutyanyan_markers_20230615.csv") ``` ## Build list of meta-analytic markers We now build meta-markers from individual dataset marker lists. First we load the markers we previously exported. Note that the export function compresses marker files by default, so the extension has changed. ```{r read_markers} markers = list( lmarkers = read_markers("Liu_markers_20230615.csv.gz"), vtmarkers = read_markers("VT_markers_20230615.csv.gz"), smarkers = read_markers("Suryawanshi_markers_20230615.csv.gz"), hmarkers = read_markers("Han_markers_20230615.csv.gz"), amarkers = read_markers("Arutyanyan_markers_20230615.csv.gz") ) ``` We create meta-markers by calling the following function. By default, the function only returns a ranked list of markers, but can also provide more detailed information about markers by setting "detailed_stats = TRUE". ```{r compute_meta_markers} meta_markers <- make_meta_markers(markers, detailed_stats = TRUE) ``` We can look at meta-analytic markers for the Cytotrophoblast cell type again. ```{r show_Trophoblast_meta_markers} meta_markers %>% filter(cell_type == "Proliferative") %>% head(100) ``` The PAGE4 gene is a member of the GAGE family. The GAGE genes are expressed in a variety of tumors and in some fetal and reproductive tissues. The "recurrence" column shows how often (in how many datasets) a gene was detected as "strongly" differentially expressed (by default FC>4, FDR<0.05, these parameters can be changed when calling "make_meta_markers"). To better appreciate the trade-off between AUROC and fold change of detection, we can look at genes that offer optimal AUROC performance vs genes that offer optimal detection performance by looking at the Pareto front of markers in the (AUROC, detection) plot. ```{r plot_cytotrophoblast_pareto_markers} plot_pareto_markers(meta_markers, "Proliferative", min_recurrence = 0) ``` In this plot, dotted lines indicate high AUROC performance (sensitive marker) and high detection performance (specific marker, binary-like behavior). To better appreciate what the above explanation means, we can look at the expression of Pareto markers in one of the datasets. ```{r pareto_markers_detail} pareto_markers = get_pareto_markers(meta_markers, "Proliferative", min_recurrence=1) plot_marker_expression(assay(lSCE, "cpm"), pareto_markers, lSCE@colData$merged_annotation) plot_marker_expression(assay(vtSCE, "cpm"), pareto_markers, vtSCE@colData$merged_annotation) plot_marker_expression(assay(sSCE, "cpm"), pareto_markers, sSCE@colData$merged_annotation) plot_marker_expression(assay(hSCE, "cpm"), pareto_markers, hSCE@colData$merged_annotation) plot_marker_expression(assay(aSCE, "cpm"), pareto_markers, aSCE@colData$merged_annotation) ``` To obtain an overview of the strength of markers we can obtain for all cell types, we can make a summary plot that contains Pareto markers for all cell types. ```{r pareto_summary, eval = F} library(ggrepel) is_pareto_front = function(x, y, tolerance = 0) { ids = seq_along(x) result = data.frame(x, y, ids) %>% dplyr::arrange(dplyr::desc(.data$x), dplyr::desc(.data$y)) %>% dplyr::mutate(max_y = cummax(.data$y), is_pareto = .data$y >= .data$max_y - tolerance) %>% dplyr::select(.data$ids, .data$is_pareto) %>% dplyr::arrange(.data$ids) %>% dplyr::pull(.data$is_pareto) return(result) } pdf("pareto_front2_20230615.pdf") new_plot <- function (meta_markers, min_recurrence = 1) { meta_markers %>% dplyr::filter(.data$recurrence >= min_recurrence) %>% dplyr::group_by(.data$cell_type) %>% dplyr::filter(is_pareto_front(.data$auroc, .data$fold_change_detection)) %>% ggplot2::ggplot(ggplot2::aes(x = log2(.data$fold_change_detection), y = .data$auroc, col = .data$cell_type, label = .data$gene)) + ggplot2::geom_line() + geom_text_repel(show.legend = FALSE) + ggplot2::geom_hline(yintercept = 0.8, linetype = "dashed") + ggplot2::geom_vline(xintercept = 3, linetype = "dashed") + ggplot2::labs(x = "log2(Fold change) of detection rate", y = "AUROC") + ggplot2::theme(legend.title = ggplot2::element_blank()) + theme_bw() } new_plot(meta_markers, min_recurrence=0) while (!is.null(dev.list())) dev.off() ``` We see that all cell types have highly sensitive markers, but the specificity (background expression) for these markers vary considerably. For example, cytotrophoblasts and syncytiotrophoblasts don’t have any marker that is both highly specific and sensitive. ```{r gamma_markers_detail} plot_pareto_markers(meta_markers, "Non-Proliferative", min_recurrence=0) pareto_markers = get_pareto_markers(meta_markers, "Syncytiotrophoblast", min_recurrence=0) plot_marker_expression(assay(lSCE, "cpm"), pareto_markers, lSCE@colData$merged_annotation) plot_marker_expression(assay(vtSCE, "cpm"), pareto_markers, vtSCE@colData$merged_annotation) plot_marker_expression(assay(sSCE, "cpm"), pareto_markers, sSCE@colData$merged_annotation) plot_marker_expression(assay(hSCE, "cpm"), pareto_markers, hSCE@colData$merged_annotation) plot_marker_expression(assay(aSCE, "cpm"), pareto_markers, aSCE@colData$merged_annotation) ``` The violin plot makes it clear how background expression is distributed across cell types. We strongly recommend saving the meta-markers to a file for later use. ```{r export_meta_markers} export_meta_markers(meta_markers, "meta_markers_(5_datasets)_20230615.csv", names(markers)) ``` ```{r} meta_markers <- read_meta_markers("/home/youkim/MetaMarkers/5-dataset-test/meta_markers_(5_datasets)_20230615.csv.gz") meta_markers <- meta_markers %>% # Top N highest values by group group_by(cell_type) %>% dplyr::slice(1:300) # filter(cell_type != "Proliferative") %>% # pull(gene) %>% # cat() meta_markers_P <- meta_markers %>% filter(cell_type == "Proliferative") dim(meta_markers_P) meta_markers_NP <- meta_markers %>% filter(cell_type == "Non-Proliferative") dim(meta_markers_NP) setwd("~/Bulk_RNA_seq/STAR_output_analysis/Second_Data") saveRDS(meta_markers_P, "Proliferative_Metamarkers(300)_20230624.rds") saveRDS(meta_markers_NP, "Non-Proliferative_Metamarkers(300)_20230624.rds") plot_pareto_summary(meta_markers, min_recurrence=0) ``` ```{r} # write.csv(meta_markers_EVT$gene, "EVT Gene List") mapping_P <- mapIds(org.Hs.eg.db, meta_markers_P$gene, 'ENSEMBL', 'SYMBOL') head(mapping_P) mapping_P %<>% as_tibble(rownames = "gene") %>% dplyr::rename(ENSEMBL = value) mapping_P %>% group_by(ENSEMBL) %>% count() %>% filter(n != 1) mapping_P %<>% dplyr::filter(!is.na(ENSEMBL)) #check for 1-many or many-1 mapping_P %>% group_by(ENSEMBL) %>% count() %>% filter(n != 1) mapping_P %>% group_by(gene) %>% count() %>% filter(n != 1) meta_markers_P %<>% inner_join(mapping_P) df1 <- meta_markers_P %>% dplyr::select(ENSEMBL, cell_type) # Let's save the metamarkers ENSEMBL IDs to a file setwd("~/Bulk_RNA_seq/STAR_output_analysis/Second_Data") write.csv(df1, "Proliferative_ENSEMBL_IDs_20230624.csv", row.names=FALSE) ``` ```{r} # Repeat the same process for NT mapping_NP <- mapIds(org.Hs.eg.db, meta_markers_NP$gene, 'ENSEMBL', 'SYMBOL') head(mapping_NP) mapping_NP %<>% as_tibble(rownames = "gene") %>% dplyr::rename(ENSEMBL = value) mapping_NP %>% group_by(ENSEMBL) %>% count() %>% filter(n != 1) mapping_NP %<>% dplyr::filter(!is.na(ENSEMBL)) #check for 1-many or many-1 mapping_NP %>% group_by(ENSEMBL) %>% count() %>% filter(n != 1) mapping_NP %>% group_by(gene) %>% count() %>% filter(n != 1) meta_markers_NP %<>% inner_join(mapping_NP) df2 <- meta_markers_NP %>% dplyr::select(ENSEMBL, cell_type) # Let's save the metamarkers ENSEMBL IDs to a file setwd("~/Bulk_RNA_seq/STAR_output_analysis/Second_Data") write.csv(df2, "NP_ENSEMBL_IDs_20230624.csv", row.names=FALSE) ```