R/plotting.R

Defines functions plot_dimred plot_heatmap plot_umap plot_violin

Documented in plot_dimred plot_heatmap plot_umap plot_violin

#' Violin plot
#'
#' Violin plots of RNA or protein expression in the Seurat object
#'
#' @param dataset Seurat object that you want to get expression from
#' @param genes The genes you want to plot
#' @param rna_protein Do you want to plot the RNA or protein expression?
#' @param split_condition Do you want to split the plots by disease?
#' @param point_size Do you want to add individual points to the violin plot? Use this with some caution
#' @param ncols Number of columns when plotting the graphs
#'
#' @importFrom magrittr "%>%"
#'
#' @examples \dontrun{
#' plot_violin(dataset = seurat_object,
#'             genes = "IFNG",
#'             rna_protein = "protein")
#' }
#'
#' @export

plot_violin <- function(dataset,
                        genes,
                        rna_protein = "rna",
                        split_condition = T,
                        point_size = 0,
                        ncols = NULL) {

  if (rna_protein == "rna") {

    grep_gene <- sapply(genes, function(x) paste("^", x, "$", sep = "")) %>%
      as.character() %>%
      paste(collapse = "|") %>%
      grep(x = rownames(dataset), value = T, ignore.case = T)

    if (split_condition == T) {

      Seurat::VlnPlot(dataset, features = grep_gene, pt.size = point_size, split.by = "sample", ncol = ncols)

    } else {

      Seurat::VlnPlot(dataset, features = grep_gene, pt.size = point_size)

    }

  } else {

    Seurat::DefaultAssay(dataset) <- "ADT"

    grep_gene <- paste(genes, collapse = "|")
    grep_gene <- grep(pattern = grep_gene, x = rownames(dataset@assays$ADT), value = T, ignore.case = T)

    if (split_condition == T) {

      Seurat::VlnPlot(dataset, features = grep_gene, pt.size = point_size, split.by = "sample")

    } else {

      Seurat::VlnPlot(dataset, features = grep_gene, pt.size = point_size)

    }
  }
}


#' Dimensional reduction plot with expression overlaid
#'
#' Use this to overlay expression of RNA or proteins on a UMAP/tSNE plot
#'
#' @param dataset Seurat object that you want to get expression from
#' @param genes The genes you want to plot
#' @param rna_protein Do you want to plot the RNA or protein expression?
#' @param reduction Do you want a UMAP or tSNE?
#' @param min_cutoff Don't show anything below this expression level
#' @param max_cutoff Set this as maximum on the expression level scale
#' @param split_condition Do you want to split the plots by disease?
#' @param add_cell_label Should cell names be added to UMAP as well as legend
#' @param avoid_overlap Force cell names to avoid overalpping on UMAP
#'
#'@importFrom magrittr "%>%"
#'
#' @examples \dontrun{
#' plot_umap(dataset = seurat_object,
#'           genes = "IFNG",
#'           reduction = "tnse",
#'           min_cutoff = 1)
#' }
#'
#' @export

plot_umap <- function(dataset,
                      genes,
                      rna_protein = "rna",
                      reduction = "umap",
                      min_cutoff = 0.5,
                      max_cutoff = 3,
                      split_condition = T,
                      add_cell_label = T,
                      avoid_overlap = T) {

  if (rna_protein == "rna") {

    grep_gene <- sapply(genes, function(x) paste("^", x, "$", sep = "")) %>%
      as.character() %>%
      paste(collapse = "|") %>%
      grep(x = rownames(dataset), value = T, ignore.case = T)

    if (split_condition == T) {

      Seurat::FeaturePlot(dataset,
                  features = grep_gene,
                  split.by = "sample",
                  reduction = reduction,
                  min.cutoff = min_cutoff,
                  max.cutoff = max_cutoff,
                  label = add_cell_label,
                  repel = avoid_overlap) +
        ggplot2::theme(aspect.ratio = 1)

    } else {

      Seurat::FeaturePlot(dataset,
                  features = grep_gene,
                  reduction = reduction,
                  min.cutoff = min_cutoff,
                  max.cutoff = max_cutoff,
                  label = add_cell_label,
                  repel = avoid_overlap) +
        ggplot2::theme(aspect.ratio = 1)

    }

  } else {

    Seurat::DefaultAssay(dataset) <- "ADT"

    grep_gene <- paste(genes, collapse = "|")
    grep_gene <- grep(pattern = grep_gene, x = rownames(dataset@assays$ADT), value = T, ignore.case = T)

    if (split_condition == T) {

      Seurat::FeaturePlot(dataset,
                  features = grep_gene,
                  split.by = "sample",
                  reduction = reduction,
                  min.cutoff = min_cutoff,
                  max.cutoff = max_cutoff,
                  label = add_cell_label,
                  repel = avoid_overlap) +
        ggplot2::theme(aspect.ratio = 1)

    } else {

      Seurat::FeaturePlot(dataset,
                  features = grep_gene,
                  reduction = reduction,
                  min.cutoff = min_cutoff,
                  max.cutoff = max_cutoff,
                  label = add_cell_label,
                  repel = avoid_overlap) +
        ggplot2::theme(aspect.ratio = 1)

    }
  }
}


#' Plot a heatmap of genes across cell clusters
#'
#' Use this to visualise gene expression in a heatmap across clusters. This will give you
#' expression of certain genes across clusters averaged over all conditions i.e. you can't
#' specify to split by disease
#'
#' @param dataset Seurat object that you want to get expression from
#' @param genes The genes you want to plot
#'
#' @importFrom magrittr "%>%"
#'
#' @examples \dontrun{
#' plot_heatmap(dataset = seurat_object,
#'              genes = c("IFNG", "IL10", "FOXP3", "CD69", "CD8A"))
#' }
#'
#' @export

plot_heatmap <- function(dataset,
                         genes) {

    grep_gene <- sapply(genes, function(x) paste("^", x, "$", sep = "")) %>%
      as.character() %>%
      paste(collapse = "|") %>%
      grep(x = rownames(dataset), value = T, ignore.case = T)

    dataset <- Seurat::ScaleData(dataset, features = grep_gene)

    Seurat::DoHeatmap(dataset, features = grep_gene)

}


#' UMAP or tSNE showing cell clusters
#'
#' Use this to plot data as a UMAP or tSNE
#'
#' @param dataset Seurat object that you want to get expression from
#' @param reduction Do you want to use UMAP or tSNE?
#' @param add_cell_label Should cell names be added to UMAP as well as legend
#' @param avoid_overlap Force cell names to avoid overalpping on UMAP
#'
#' @importFrom magrittr "%>%"
#'
#' @examples \dontrun{
#' plot_dimred(dataset = seurat_object,
#'             add_cell_label = T)
#' }
#'
#' @export

plot_dimred <- function(dataset,
                        reduction = "umap",
                        add_cell_label = F,
                        avoid_overlap = T) {

  Seurat::DimPlot(dataset,
          reduction = reduction,
          split.by = "sample",
          label = add_cell_label,
          repel = avoid_overlap)

}
jackbibby1/scExpression documentation built on Dec. 20, 2021, 8:05 p.m.