knitr::opts_chunk$set(echo = TRUE)

Overview

This vignette will overview how to create violin plots that depict expression levels within each cluster of an input Seurat object. We will use the pbmc_small dataset that is included in Seurat (version 3) for this example.

This visualization utilizes dendrograms that communicate how each cluster of cells is related. Such dendrograms are created by default in the export_seurat_object() function.

We will begin by exporting a previously processed Seurat object into SCOTA-readable files for interactive visualization, as outlined in more detail in theExport Seurat Object Vignette.

library(Seurat)
library(tidyverse)
library(patchwork)
library(cellcuratoR)

## we use the pbmc_small dataset pre-packed with Seurat

head(pbmc_small@meta.data)
celltype <- plyr::mapvalues(x = pbmc_small@meta.data$RNA_snn_res.1, 
                            from = seq(from = 0, to = 2), 
                            to = c("celltype_A", "celltype_B", "celltype_C"))
pbmc_small@meta.data <- data.frame(pbmc_small@meta.data, celltype)

my_filepath <- file.path(find.package("cellcuratoR"))
print(paste0("exporting data to: ", my_filepath, "pbmc_cellcuratoR_export/"))

export_shiny_object(seurat_object = pbmc_small, 
                    final_cluster_column_name = "RNA_snn_res.1",
                    library_id_column_name = "orig.ident",
                    classification_column_name = "celltype",
                    create_dendrogram = TRUE,
                    custom_cluster_colors = FALSE,
                    custom_library_colors = FALSE,
                    export_data_path = file.path(my_filepath, "pbmc_cellcuratoR_export/"))
                    #export_data_path = "~/Desktop/pbmc/")

Creating violin plots are broken into two steps. The first involves generating the data (expression values for each cluster) and the colors used for the violin plots.

load(file.path(my_filepath, "pbmc_cellcuratoR_export", "seurat_obj.RData")) # Loads the seurat_obj, which renames several columns in the meta.data
                                                    # with standardized names for use in the Shiny application. 

my_violin_data <- prepare_violin_data_colors(my_object = seurat_obj,
                          genes_to_investigate = c("CD79A", "CD79B", "HLA-DRA"),
                          dendrogram_input = seurat_obj@misc$dendrogram,
                          colors = seurat_obj@misc$final_colors)
# my_violin_data contains a list containing elements (1) an expression matrix from the seurat data slot and 
# (2) ordered colors for the violin plot. 

Generated data can be converted into violin plots with the contruct_violin_plot function.

theme_shiny <- function() {
      theme(
        axis.title = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        axis.line.y = element_blank(),
        panel.background = element_rect(fill = "white"),
        panel.grid = element_blank(),
        strip.background = element_blank(),
        strip.text.x = element_text(size = rel(1.5)),
        legend.position = "none"
      )
    }

p_violins <-
  construct_violin_plot(
    my_object = seurat_obj,
    genes_to_investigate = c("CD79A", "CD79B", "HLA-DRA"),
    dendrogram_input = seurat_obj@misc$dendrogram,
    colors = seurat_obj@misc$final_colors,
    use_noise = TRUE,
    scale = "free_x"
    ) +
  theme_shiny()

dendrogram_data <- ggdendro::dendro_data(seurat_obj@misc$dendrogram, type = "rectangle")

p_dendrogram <-
  ggdendro::segment(dendrogram_data) %>%
  ggplot2::ggplot(aes(x = x, y = y)) +
  geom_segment(
    aes(
      xend = xend,
      yend = yend
      )
    ) +
  geom_text(
    data = ggdendro::label(dendrogram_data),
    aes(label = label),
    hjust = 1,
    vjust = -0.7,
    size = 6
    ) +
  scale_y_reverse() +
  scale_x_continuous(expand = c(0, 0.6)) +
  coord_flip() +
  theme_shiny()

p_dendrogram +
  p_violins +
  patchwork::plot_layout(ncol = 2, widths = c(3, 7))

Colors can be manually changed easily.

p_violins <-
  construct_violin_plot(
    my_object = seurat_obj,
    genes_to_investigate = c("CD79A", "CD79B", "HLA-DRA"),
    dendrogram_input = seurat_obj@misc$dendrogram,
    colors = c("red", "green", "yellow"),
    use_noise = TRUE,
    scale = "free_x"
    ) +
  theme_shiny()

p_dendrogram +
  p_violins +
  patchwork::plot_layout(ncol = 2, widths = c(3, 7))

By default, the VlnPlot() function within Seurat adds a small amount of noise ot the expression profiles of each cell. This changes how the violin geoms are rendered, and results in violins not being rendered for clusters that have less than 25% of cells expressing the gene of interest. Changing the add_noise parameter to FALSE results in violins being drawn for all clusters, as shown here.

p_violins <-
  construct_violin_plot(
    my_object = seurat_obj,
    genes_to_investigate = c("CD79A", "CD79B", "HLA-DRA"),
    dendrogram_input = seurat_obj@misc$dendrogram,
    colors = c("red", "green", "yellow"),
    use_noise = FALSE,
    scale = "free_x"
    ) +
  theme_shiny()


p_dendrogram +
  p_violins +
  patchwork::plot_layout(ncol = 2, widths = c(3, 7))

Notice how violins are drawn for all clusters in CD79B. As no cells in Celltype_A express CD79A, a violin is still not drawn for this cluster/gene combination.



drewvoigt10/cellcuratoR documentation built on May 22, 2023, 4:38 p.m.