diff --git a/DESCRIPTION b/DESCRIPTION index 4d1f2fbd5..0d1f3f722 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -121,7 +121,8 @@ Imports: scuttle, utils, stats, - zellkonverter + zellkonverter, + lifecycle RoxygenNote: 7.3.2 Suggests: testthat, diff --git a/NAMESPACE b/NAMESPACE index 1a81bbeea..38b0b9f57 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -306,6 +306,8 @@ importFrom(dplyr,select) importFrom(dplyr,summarize) importFrom(dplyr,ungroup) importFrom(grid,gpar) +importFrom(lifecycle,deprecate_warn) +importFrom(lifecycle,deprecated) importFrom(magrittr,"%>%") importFrom(methods,slot) importFrom(reshape2,melt) diff --git a/R/ggPlotting.R b/R/ggPlotting.R index a5577a151..d298582e7 100644 --- a/R/ggPlotting.R +++ b/R/ggPlotting.R @@ -460,8 +460,9 @@ plotSCEDimReduceColData <- function(inSCE, #' @param reducedDimName saved dimension reduction name in the #' \linkS4class{SingleCellExperiment} object. Required. #' @param sample Character vector. Indicates which sample each cell belongs to. -#' @param feature Name of feature stored in assay of SingleCellExperiment +#' @param features Name of feature stored in assay of SingleCellExperiment #' object. +#' @param feature Deprecated, use `features` instead. #' @param featureLocation Indicates which column name of rowData to query gene. #' @param featureDisplay Indicates which column name of rowData to use #' to display feature for visualization. @@ -498,6 +499,7 @@ plotSCEDimReduceColData <- function(inSCE, #' @param legendTitle title of legend. Default NULL. #' @param legendTitleSize size of legend title. Default 12. #' @param legendSize size of legend. Default 10. +#' @param ncols number of columns for multiple feature plotting. Default NULL. #' @param groupBy Facet wrap the scatterplot based on value. #' Default \code{NULL}. #' @param combinePlot Must be either "all", "sample", or "none". "all" will combine all plots into a single @@ -505,16 +507,18 @@ plotSCEDimReduceColData <- function(inSCE, #' @param plotLabels labels to each plot. If set to "default", will use the name of the samples #' as the labels. If set to "none", no label will be plotted. #' @return a ggplot of the reduced dimension plot of feature data. +#' @importFrom lifecycle deprecated deprecate_warn #' @examples #' data("mouseBrainSubsetSCE") #' plotSCEDimReduceFeatures( -#' inSCE = mouseBrainSubsetSCE, feature = "Apoe", +#' inSCE = mouseBrainSubsetSCE, features = "Apoe", #' shape = NULL, reducedDimName = "TSNE_counts", #' useAssay = "counts", xlab = "tSNE1", ylab = "tSNE2" #' ) #' @export plotSCEDimReduceFeatures <- function(inSCE, - feature, + features, + feature = deprecated(), reducedDimName, sample = NULL, featureLocation = NULL, @@ -540,37 +544,40 @@ plotSCEDimReduceFeatures <- function(inSCE, legendTitle = NULL, legendSize = 10, legendTitleSize = 12, + ncols = NULL, groupBy = NULL, combinePlot = "none", plotLabels = NULL) { - combinePlot <- match.arg(combinePlot,c("all", "sample", "none")) + if (lifecycle::is_present(feature)) { + deprecate_warn("2.19.1", "singleCellTK::plotSCEDimReduceFeatures(feature = )", + "singleCellTK::plotSCEDimReduceFeatures(features = )") + features <- feature + } + combinePlot <- match.arg(combinePlot,c("all", "sample", "none")) - if(!is.null(featureDisplay)){ - featureDisplay <- match.arg(featureDisplay, - c("rownames", - colnames(SummarizedExperiment::rowData(inSCE))) - ) - }else{ - if(exists(x = "featureDisplay", inSCE@metadata)){ - featureDisplay <- inSCE@metadata$featureDisplay - } + if(!is.null(featureDisplay)){ + featureDisplay <- match.arg(featureDisplay, + c("rownames", + colnames(SummarizedExperiment::rowData(inSCE)))) + }else{ + if(exists(x = "featureDisplay", inSCE@metadata)){ + featureDisplay <- inSCE@metadata$featureDisplay } + } - mat <- getBiomarker( + if (length(features) > 1) { + plotlist <- lapply(features, function(f) { + mat <- getBiomarker( inSCE = inSCE, useAssay = useAssay, - gene = feature, + gene = f, binary = "Continuous", featureLocation = featureLocation, featureDisplay = featureDisplay - ) - counts <- mat[, 2] - - if(!is.null(featureDisplay)){ - title = utils::tail(colnames(mat),1) - } - - g <- .ggScatter( + ) + counts <- mat[, 2] + plot_title <- if(!is.null(featureDisplay)) utils::tail(colnames(mat),1) else f + .ggScatter( inSCE = inSCE, sample = sample, conditionClass = "numeric", @@ -591,17 +598,68 @@ plotSCEDimReduceFeatures <- function(inSCE, binLabel = binLabel, defaultTheme = defaultTheme, dotSize = dotSize, - title = title, + title = plot_title, titleSize = titleSize, legendTitle = legendTitle, legendTitleSize = legendTitleSize, legendSize = legendSize, groupBy = groupBy, - combinePlot = combinePlot, + combinePlot = "none", plotLabels = plotLabels + ) + }) + if (combinePlot != "none") { + plotlist <- .ggSCTKCombinePlots(plotlist, + combinePlot = combinePlot, + ncols = ncols, + labels = plotLabels) + } + return(plotlist) + } else { + mat <- getBiomarker( + inSCE = inSCE, + useAssay = useAssay, + gene = features, + binary = "Continuous", + featureLocation = featureLocation, + featureDisplay = featureDisplay + ) + counts <- mat[, 2] + if(!is.null(featureDisplay)){ + title = utils::tail(colnames(mat),1) + } + g <- .ggScatter( + inSCE = inSCE, + sample = sample, + conditionClass = "numeric", + colorBy = counts, + shape = shape, + transparency = 1, + colorLow = colorLow, + colorMid = colorMid, + colorHigh = colorHigh, + reducedDimName = reducedDimName, + xlab = xlab, + ylab = ylab, + axisSize = axisSize, + axisLabelSize = axisLabelSize, + dim1 = dim1, + dim2 = dim2, + bin = bin, + binLabel = binLabel, + defaultTheme = defaultTheme, + dotSize = dotSize, + title = title, + titleSize = titleSize, + legendTitle = legendTitle, + legendTitleSize = legendTitleSize, + legendSize = legendSize, + groupBy = groupBy, + combinePlot = combinePlot, + plotLabels = plotLabels ) - return(g) + } } #' @title Dimension reduction plot tool for all types of data diff --git a/R/runTSCAN.R b/R/runTSCAN.R index 56e27096e..0d5b6110f 100644 --- a/R/runTSCAN.R +++ b/R/runTSCAN.R @@ -814,7 +814,7 @@ plotTSCANDimReduceFeatures <- function( features <- stats::na.omit(features) for (f in features) { # Should not enter the loop if features is length zero after NA omit - g <- plotSCEDimReduceFeatures(inSCE, feature = f, + g <- plotSCEDimReduceFeatures(inSCE, features = f, useAssay = useAssay, featureLocation = by,dim1 = 1, dim2 = 2, featureDisplay = featureDisplay, diff --git a/R/seuratFunctions.R b/R/seuratFunctions.R index 285f7d8a6..aff3c1213 100644 --- a/R/seuratFunctions.R +++ b/R/seuratFunctions.R @@ -2134,11 +2134,14 @@ plotSeuratGenes <- function(inSCE, seurat.version <- .getSeuratObjectMajorVersion(seuratObject) if(plotType %in% c("dot", "heatmap")){ - if (length(features) < 2) { - stop("At least 2 features are required for this plotType.") + if (length(features) == 1) { + dummyfeature <- rownames(inSCE)[which(!rownames(inSCE) == features)][1] + seuratObject <- + Seurat::ScaleData(seuratObject, features = c(features,dummyfeature)) + }else{ + seuratObject <- + Seurat::ScaleData(seuratObject, features = features) } - seuratObject <- - Seurat::ScaleData(seuratObject, features = features) } indices <- list() diff --git a/inst/shiny/server.R b/inst/shiny/server.R index 747e5e2e4..5c81568c1 100644 --- a/inst/shiny/server.R +++ b/inst/shiny/server.R @@ -4883,7 +4883,7 @@ shinyServer(function(input, output, session) { legendSize = input$adjustlegendsize, legendTitleSize = input$adjustlegendtitlesize, conditionClass = pltVars$class, defaultTheme = as.logical(pltVars$defTheme)) }else if(input$TypeSelect_Colorby == "Expression Assays"){ - a <- plotSCEDimReduceFeatures(vals$counts, feature = input$GeneSelect_Assays_Colorby, + a <- plotSCEDimReduceFeatures(vals$counts, features = input$GeneSelect_Assays_Colorby, reducedDimName = input$QuickAccess, useAssay = input$AdvancedMethodSelect_Colorby, xlab = xname, ylab = yname, legendTitle = legendname, title = input$adjusttitle, groupBy = pltVars$groupby, bin = pltVars$bin, transparency = input$adjustalpha, diff --git a/man/plotSCEDimReduceFeatures.Rd b/man/plotSCEDimReduceFeatures.Rd index a4457a4e8..c93abbdf3 100644 --- a/man/plotSCEDimReduceFeatures.Rd +++ b/man/plotSCEDimReduceFeatures.Rd @@ -6,7 +6,8 @@ \usage{ plotSCEDimReduceFeatures( inSCE, - feature, + features, + feature = deprecated(), reducedDimName, sample = NULL, featureLocation = NULL, @@ -32,6 +33,7 @@ plotSCEDimReduceFeatures( legendTitle = NULL, legendSize = 10, legendTitleSize = 12, + ncols = NULL, groupBy = NULL, combinePlot = "none", plotLabels = NULL @@ -41,9 +43,11 @@ plotSCEDimReduceFeatures( \item{inSCE}{Input \linkS4class{SingleCellExperiment} object with saved dimension reduction components or a variable with saved results. Required.} -\item{feature}{Name of feature stored in assay of SingleCellExperiment +\item{features}{Name of feature stored in assay of SingleCellExperiment object.} +\item{feature}{Deprecated, use `features` instead.} + \item{reducedDimName}{saved dimension reduction name in the \linkS4class{SingleCellExperiment} object. Required.} @@ -108,6 +112,8 @@ Default 'blue'.} \item{legendTitleSize}{size of legend title. Default 12.} +\item{ncols}{number of columns for multiple feature plotting. Default NULL.} + \item{groupBy}{Facet wrap the scatterplot based on value. Default \code{NULL}.} @@ -127,7 +133,7 @@ Plot results of reduced dimensions data and \examples{ data("mouseBrainSubsetSCE") plotSCEDimReduceFeatures( - inSCE = mouseBrainSubsetSCE, feature = "Apoe", + inSCE = mouseBrainSubsetSCE, features = "Apoe", shape = NULL, reducedDimName = "TSNE_counts", useAssay = "counts", xlab = "tSNE1", ylab = "tSNE2" ) diff --git a/tests/testthat/test-qc.R b/tests/testthat/test-qc.R index e40016040..016eb28bb 100644 --- a/tests/testthat/test-qc.R +++ b/tests/testthat/test-qc.R @@ -17,7 +17,7 @@ test_that(desc = "Testing plotSCEScatter functions", { reducedDimName = "UMAP", labelClusters = FALSE, sample = sampleVector, combinePlot = "all") expect_is(p1, c("gg","ggplot")) - p2 <- plotSCEDimReduceFeatures(inSCE = sceres, feature = "ENSG00000251562", + p2 <- plotSCEDimReduceFeatures(inSCE = sceres, features = "ENSG00000251562", shape = NULL, reducedDimName = "UMAP", useAssay = "counts", xlab = "UMAP1", ylab = "UMAP2", sample = sampleVector, combinePlot = "all") diff --git a/vignettes/articles/2d_embedding.Rmd b/vignettes/articles/2d_embedding.Rmd index d338f2706..0be30d7e6 100644 --- a/vignettes/articles/2d_embedding.Rmd +++ b/vignettes/articles/2d_embedding.Rmd @@ -93,7 +93,7 @@ plotUMAP(sce) # Customizing scatter plot with more information from cell metadata (colData) plotSCEDimReduceColData(sce, colorBy = "cluster", reducedDimName = "UMAP") # Color scatter plot with feature expression -plotSCEDimReduceFeatures(sce, feature = "CD8A", reducedDimName = "UMAP") +plotSCEDimReduceFeatures(sce, features = "CD8A", reducedDimName = "UMAP") ``` ````{=html} diff --git a/vignettes/articles/dimensionality_reduction.Rmd b/vignettes/articles/dimensionality_reduction.Rmd index 3c3d1bbe7..cefe594ec 100644 --- a/vignettes/articles/dimensionality_reduction.Rmd +++ b/vignettes/articles/dimensionality_reduction.Rmd @@ -116,7 +116,7 @@ plotPCA(sce) # Customizing scatter plot with more information from cell metadata (colData) plotSCEDimReduceColData(sce, colorBy = "cluster", reducedDimName = "PCA") # Color scatter plot with feature expression -plotSCEDimReduceFeatures(sce, feature = "CD8A", reducedDimName = "PCA") +plotSCEDimReduceFeatures(sce, features = "CD8A", reducedDimName = "PCA") ``` Besides, SCTK also wraps elbow plot, Jackstraw plot and DimHeatmap methods from *Seurat*. For usage of these visualization methods, please refer to [Seurat Curated Workflow](seurat_curated_workflow.html)