From cbedbe7fcf45e7b1c02a7732b73157ec580af5fc Mon Sep 17 00:00:00 2001 From: Shreya Rao Date: Wed, 18 Dec 2024 13:36:14 +0530 Subject: [PATCH] plotImage function --- NAMESPACE | 4 +++ R/plotImage.R | 91 ++++++++++++++++++++++++++++++++++++++++++++++++ man/plotImage.Rd | 42 ++++++++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 R/plotImage.R create mode 100644 man/plotImage.Rd diff --git a/NAMESPACE b/NAMESPACE index b866fd5..42d8604 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,11 +5,15 @@ export(colTest) export(convPairs) export(getPairwise) export(getProp) +export(plotImage) export(signifPlot) export(spicy) export(spicyBoxPlot) export(topPairs) exportClasses(SpicyResults) +import(SpatialExperiment) +import(SummarizedExperiment) +import(dplyr) import(ggh4x) import(ggplot2) import(ggthemes) diff --git a/R/plotImage.R b/R/plotImage.R new file mode 100644 index 0000000..36d15ea --- /dev/null +++ b/R/plotImage.R @@ -0,0 +1,91 @@ +#' Plots an image with specified from and to cell types. +#' +#' @param cells A SummarizedExperiment object. +#' @param imageToPlot The ID of the image to be plotted. +#' @param from The "from" cell type. +#' @param to The "to" cell type. +#' @param imageID The name of the imageID column in the SummarizedExperiment object. +#' @param cellType The name of the cellType column in the SummarizedExperiment object. +#' @param spatialCoords The names of the spatialCoords column if using a SingleCellExperiment. +#' +#' @return A ggplot object. +#' +#' @examples +#' data("diabetesData") +#' plotImage(diabetesData, "A09", from = "acinar", to = "alpha") +#' +#' @export +#' @import dplyr +#' @import ggplot2 +#' @import SummarizedExperiment +#' @import SpatialExperiment +plotImage = function(cells, + imageToPlot, + from, + to, + imageID = "imageID", + cellType = "cellType", + spatialCoords = c("x", "y")) { + + if (!is(cells, "SummarizedExperiment")) { + stop(paste("Please provide a SummarizedExperiment object as input.")) + } + + if (!imageID %in% colnames(colData(cells))) { + stop(paste0(imageID, " not found in colData.")) + } + + if (!imageToPlot %in% unique(cells[[imageID]])) { + stop(paste0("imageToPlot not found in ", imageID, " column.")) + } + + if (!cellType %in% colnames(colData(cells))) { + stop(paste0(cellType, " not found in colData.")) + } + + if (!all(spatialCoords %in% colnames(colData(cells)))) { + stop(paste0(spatialCoords, " not found in colData.")) + } + + if (length(spatialCoords) != 2) { + stop(paste("Please provide x and y coordinates columns.")) + } + + if (!all(c(from, to) %in% unique(colData(cells)[[cellType]]))) { + stop("from and/or to cell types not found in data.") + } + + if (class(cells) == "SingleCellExperiment") { + cells = SpatialExperiment(assays = assays(cells), + colData = colData(cells), + rowData = rowData(cells)) + spatialCoords(cells) = as.matrix(colData(cells)[, c(spatialCoords[1], spatialCoords[2])]) + } + + # filter for specific image + subset = cells[, colData(cells)[[imageID]] == imageToPlot] + + coords = spatialCoords(subset) |> as.data.frame() + cData = data.frame(x = coords[["x"]], + y = coords[["y"]], + cellType = subset[[cellType]]) + + cData[[cellType]] = as.character(cData[[cellType]]) + cData = cData |> mutate(cellTypeNew = + ifelse(cellType %in% c(from, to), cellType, "Other")) + + + pal = setNames(c("#d6b11c", "#850f07"), c(from, to)) + + ggplot() + + stat_density_2d(data = cData, aes(x = x, y = y, fill = after_stat(density)), + geom = "raster", + contour = FALSE) + + geom_point(data = cData |> filter(cellTypeNew != "Other"), + aes(x = x, y = y, colour = cellTypeNew), size = 1) + + scale_color_manual(values = pal) + + scale_fill_distiller(palette = "Blues", direction = 1) + + theme_classic() + + labs(title = paste0(imageID, ": ", imageToPlot), + color = cellType) +} \ No newline at end of file diff --git a/man/plotImage.Rd b/man/plotImage.Rd new file mode 100644 index 0000000..b5f9f91 --- /dev/null +++ b/man/plotImage.Rd @@ -0,0 +1,42 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/plotImage.R +\name{plotImage} +\alias{plotImage} +\title{Plots an image with specified from and to cell types.} +\usage{ +plotImage( + cells, + imageToPlot, + from, + to, + imageID = "imageID", + cellType = "cellType", + spatialCoords = c("x", "y") +) +} +\arguments{ +\item{cells}{A SummarizedExperiment object.} + +\item{imageToPlot}{The ID of the image to be plotted.} + +\item{from}{The "from" cell type.} + +\item{to}{The "to" cell type.} + +\item{imageID}{The name of the imageID column in the SummarizedExperiment object.} + +\item{cellType}{The name of the cellType column in the SummarizedExperiment object.} + +\item{spatialCoords}{The names of the spatialCoords column if using a SingleCellExperiment.} +} +\value{ +A ggplot object. +} +\description{ +Plots an image with specified from and to cell types. +} +\examples{ +data("diabetesData") +plotImage(diabetesData, "A09", from = "acinar", to = "alpha") + +}