diff --git a/DESCRIPTION b/DESCRIPTION index 1902886..96dcdf3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: scMaSigPro Type: Package Title: Application of MaSigPro Bioconductor Package for scRNA Trajectory data -Version: 0.0.2 +Version: 0.0.3 Authors@R: c( person("Ana", "Conesa", role = c("aut"), email = "ana.conesa@csic.es"), person("Maria Jose", "Nueda", role = c("aut"), email = "mj.nueda@ua.es"), @@ -17,7 +17,7 @@ Imports: assertthat, e1071, dplyr, entropy, ggplot2, igraph, magrittr, maSigPro, MASS, MatrixGenerics, methods, parallel, parallelly, plotly, RColorConesa, rlang, S4Vectors, scales, shiny, SingleCellExperiment, stats, stringr, - utils, ComplexUpset, mclust, SummarizedExperiment + utils, mclust Depends: R (>= 4.0) Encoding: UTF-8 LazyData: true @@ -27,7 +27,10 @@ Suggests: roxygen2, knitr, rmarkdown, - BiocStyle + BiocStyle, + ComplexUpset, + UpSetR, + patchwork Config/testthat/edition: 3 URL: https://github.com/BioBam/scMaSigPro/ biocViews: Clustering, Regression, TimeCourse, DifferentialExpression, diff --git a/NAMESPACE b/NAMESPACE index 1d4629c..beb0120 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -50,10 +50,6 @@ exportMethods(eSparse) exportMethods(pathAssign) exportMethods(predictors) import(ggplot2) -importFrom(ComplexUpset,intersection_matrix) -importFrom(ComplexUpset,intersection_size) -importFrom(ComplexUpset,upset) -importFrom(ComplexUpset,upset_set_size) importFrom(MASS,glm.nb) importFrom(MASS,negative.binomial) importFrom(MatrixGenerics,rowMeans) @@ -66,8 +62,6 @@ importFrom(S4Vectors,isEmpty) importFrom(SingleCellExperiment,SingleCellExperiment) importFrom(SingleCellExperiment,colData) importFrom(SingleCellExperiment,reducedDims) -importFrom(SummarizedExperiment,"assay<-") -importFrom(SummarizedExperiment,assay) importFrom(assertthat,assert_that) importFrom(dplyr,arrange) importFrom(dplyr,bind_rows) @@ -173,5 +167,6 @@ importFrom(stringr,str_split_i) importFrom(utils,View) importFrom(utils,combn) importFrom(utils,data) +importFrom(utils,packageVersion) importFrom(utils,setTxtProgressBar) importFrom(utils,txtProgressBar) diff --git a/R/plotIntersect.R b/R/plotIntersect.R index 58c28c2..b33d9e2 100644 --- a/R/plotIntersect.R +++ b/R/plotIntersect.R @@ -2,12 +2,12 @@ #' #' @description #' Generate UpSet Plot on Intersection of Significant Genes from scMaSigPro -#' object. It is a wrapper around `ComplexUpset::upset`. +#' object. It is a wrapper around `ComplexUpset::upset` and `UpSetR::upset`. #' #' @importFrom S4Vectors isEmpty -#' @importFrom ComplexUpset upset intersection_matrix intersection_size upset_set_size #' @importFrom RColorConesa colorConesa -#' +#' @importFrom utils packageVersion +#' #' @param scmpObj An object of class \code{\link{ScMaSigPro}}. #' @param min_intersection_size Minimal number of observations in an intersection #' for it to be included. @@ -16,16 +16,22 @@ #' @param keep_empty_groups Whether empty sets should be kept (including sets #' which are only empty after filtering by size) #' @param show_sets_size The overall set sizes plot, e.g. from upset_set_size() -#' (FALSE to hide) +#' @param package Which package to use for the UpsetPlot. Options are 'ComplexUpset' +#' or 'UpSetR' (Default). +#' @param verbose Print detailed output in the console. (Default is TRUE) #' -#' @return ggplot2 plot object. +#' @return ggplot2 plot object for 'ComplexUpset' or upset object for 'UpSetR'. #' #' @author Priyansh Srivastava \email{spriyansh29@@gmail.com} #' #' @export -plotIntersect <- function(scmpObj, min_intersection_size = 2, +plotIntersect <- function(scmpObj, + package = "UpSetR", + min_intersection_size = 2, keep_empty_groups = TRUE, - width_ratio = 0.1, show_sets_size = FALSE) { + width_ratio = 0.1, + show_sets_size = FALSE, + verbose = TRUE) { # Check the data assert_that( is(scmpObj, "ScMaSigPro"), @@ -37,80 +43,109 @@ plotIntersect <- function(scmpObj, min_intersection_size = 2, msg = "'sig.genes@Summary' slot is empty, please run 'sc.get.siggenes'" ) + # Check for possible options + assert_that(package %in% c("ComplexUpset", "UpSetR"), + msg = "Please provide a valid package name for UpSet plot. Options are 'ComplexUpset' or 'UpSetR'" + ) + + # Check if package is installed + if (!requireNamespace(package, quietly = TRUE)) { + stop(paste0("Package '", package, "' is not installed. Please install it first.")) + } else { + if (verbose) { + message(paste0("Using '", package, "' for UpSet plot.")) + } + } + gene_list <- scmpObj@Significant@genes - # Create a unique list of all genes - all_genes <- unique(unlist(gene_list)) - # Initialize the data frame - gene_df <- data.frame(gene = all_genes) + if (package == "UpSetR") { + # Create list object + upset_r_gene_list <- UpSetR::fromList(gene_list) - # Add columns for each pathway - for (pathway in names(gene_list)) { - gene_df[[pathway]] <- gene_df$gene %in% gene_list[[pathway]] - } + # Create Plot + p <- UpSetR::upset( + upset_r_gene_list, + main.bar.color = "#F58A53", + matrix.color = "#15918A", + line.size = 1.5, + cutoff = min_intersection_size, + empty.intersections = keep_empty_groups, + point.size = 3, + shade.color = "purple", + text.scale = 1.5, + sets.x.label = "Number of Features", + sets.bar.color = "#EE446F" + ) + return(p) + } else { + # Check version of the ggplot2 + if (packageVersion("ggplot2") >= "3.5.0") { + warning("Please downgrade the ggplot2 to '>= 3.5.0' to use 'ComplesUpset'. We will support the latest version in future. + Visit:'https://github.com/krassowski/complex-upset/issues/195' for more details.") + } else { + # # Create a unique list of all genes + all_genes <- unique(unlist(gene_list)) - # Binarize Variables and set factors - gene_df[, -1] <- lapply(gene_df[, -1], function(x) as.integer(x)) + # Initialize the data frame + gene_df <- data.frame(gene = all_genes) - # Get conesa colours - col_pal <- colorConesa(3) + # Add columns for each pathway + for (pathway in names(gene_list)) { + gene_df[[pathway]] <- gene_df$gene %in% gene_list[[pathway]] + } - if (show_sets_size) { - show_sets_size <- upset_set_size() - } + # Binarize Variables and set factors + gene_df[, -1] <- lapply(gene_df[, -1], function(x) as.integer(x)) + + # Get conesa colours + col_pal <- colorConesa(3) - # Create Upset - p <- upset( - data = gene_df, - intersect = colnames(gene_df)[-1], - width_ratio = width_ratio, - min_size = min_intersection_size, - keep_empty_groups = keep_empty_groups, - name = "Vars", - # wrap=FALSE, - set_sizes = show_sets_size, - # stripes=c('deepskyblue1'), - matrix = ( + if (show_sets_size) { + show_sets_size <- ComplexUpset::upset_set_size() + } - intersection_matrix( - geom = geom_point( - shape = "square", - size = 3.5 + # Create Upset + p <- ComplexUpset::upset( + data = gene_df, + intersect = colnames(gene_df)[-1], + width_ratio = width_ratio, + min_size = min_intersection_size, + keep_empty_groups = keep_empty_groups, + name = "Vars", + # wrap=FALSE, + set_sizes = show_sets_size, + # stripes=c('deepskyblue1'), + matrix = ( + + ComplexUpset::intersection_matrix( + geom = geom_point( + shape = "square", + size = 3.5 + ), + segment = geom_segment( + linetype = "dotted", + color = col_pal[1] + ) + ) + + scale_color_manual( + values = c("TRUE" = col_pal[1], "FALSE" = col_pal[3]), + # labels=c('TRUE'='yes', 'FALSE'='no'), + breaks = c("TRUE", "FALSE") + ) ), - segment = geom_segment( - linetype = "dotted", - color = col_pal[1] - ) - ) - + scale_color_manual( - values = c("TRUE" = col_pal[1], "FALSE" = col_pal[3]), - # labels=c('TRUE'='yes', 'FALSE'='no'), - breaks = c("TRUE", "FALSE") + base_annotations = list( + "Intersection size" = ComplexUpset::intersection_size( + counts = TRUE, + mapping = aes(fill = "bars_color") + ) + + scale_fill_manual(values = c("bars_color" = col_pal[2]), guide = "none") ) - ), - base_annotations = list( - "Intersection size" = intersection_size( - counts = TRUE, - mapping = aes(fill = "bars_color") - ) - + scale_fill_manual(values = c("bars_color" = col_pal[2]), guide = "none") - ) - ) + ggtitle("Intersection of features among paths") + - theme(legend.position = "none") - - # return plot - return(p) + ) + ggtitle("Intersection of features among paths") + + theme(legend.position = "none", legend.title = element_text(hjust = 0.5)) - # Perform UpSet plot - # upset( - # fromList(gene.list), - # main.bar.color = "#F58A53", - # matrix.color = "#15918A", - # line.size = 1.5, - # point.size = 3, - # shade.color = "purple", - # text.scale = 1.5, - # sets.x.label = "Number of Features", - # sets.bar.color = "#EE446F" - # ) + # return plot + return(p) + } + } } diff --git a/R/plotTrendCluster.R b/R/plotTrendCluster.R index eed1381..5df001b 100644 --- a/R/plotTrendCluster.R +++ b/R/plotTrendCluster.R @@ -302,7 +302,7 @@ plotTrendCluster <- function(scmpObj, theme( strip.background = element_blank(), strip.text.x = element_text(size = 10, angle = 0), - legend.position = "bottom", + legend.position = "bottom", legend.title.align = 0.5, panel.grid.major = element_line(color = "grey90", linewidth = 0.3, linetype = "dashed"), panel.grid.minor = element_blank(), axis.text.x = element_text(angle = 45, hjust = 1) # Rotate x-axis text if necessary diff --git a/R/scmpGenerics.R b/R/scmpGenerics.R index b11ab22..22c5e56 100644 --- a/R/scmpGenerics.R +++ b/R/scmpGenerics.R @@ -64,7 +64,6 @@ setReplaceMethod("cSparse", "ScMaSigPro", function(object, value) { return(object) }) ############################################################################### - #' @title Get the data for Dense Slot. #' #' @description @@ -131,158 +130,226 @@ setReplaceMethod("cDense", "ScMaSigPro", function(object, value) { return(object) }) ############################################################################### -#' @title Set value for expression counts. +#' @title Get Expression Counts from Sparse Slot #' #' @description -#' `eSparse<-` is a generic function for setting expression counts for Sparse -#' in an \code{\link{ScMaSigPro}}. +#' `eSparse` is a generic function for retrieving expression counts stored in the +#' Sparse slot of an \code{\link{ScMaSigPro}} object. #' #' @param object An object of class \code{\link{ScMaSigPro}}. -#' @param value The value to be set. -#' @param i Name of the assay. +#' @param i The name of the assay to retrieve expression counts for. +#' Default is "counts", which will return the default expression matrix. +#' @param ... Additional arguments (not used currently). #' -#' @return Modified `ScMaSigPro` object. +#' @return An expression matrix for the specified assay from the Sparse slot. #' #' @author Priyansh Srivastava \email{spriyansh29@@gmail.com} #' #' @export -setGeneric("eSparse<-", function(object, i, value) standardGeneric("eSparse<-")) +setGeneric("eSparse", function(object, i = "counts", ...) standardGeneric("eSparse")) -#' @title Get Value for expression counts. +#' @title Set Expression Counts in Sparse Slot #' #' @description -#' `eSparse<-` is a generic function for getting expression counts for Sparse -#' in an \code{\link{ScMaSigPro}}. +#' `eSparse<-` is a generic function for setting expression counts in the Sparse slot +#' of an \code{\link{ScMaSigPro}} object. #' #' @param object An object of class \code{\link{ScMaSigPro}}. -#' @param value The value to be set. (If Setting) +#' @param i The name of the assay for which to set expression counts. +#' Default is "counts", which targets the default expression counts slot. +#' @param value The new expression matrix to be set for the specified assay. #' -#' @return The expression matrix for Sparse slot. +#' @return The modified \code{\link{ScMaSigPro}} object with the updated +#' expression data in the Sparse slot. #' #' @author Priyansh Srivastava \email{spriyansh29@@gmail.com} #' #' @export -setGeneric("eSparse", function(object, value = "missing") standardGeneric("eSparse")) +setGeneric("eSparse<-", function(object, i = "counts", value) standardGeneric("eSparse<-")) -#' @title Get Value for expression counts. -#' -#' @importFrom SummarizedExperiment assay assay<- +#' @title Retrieve Expression Counts from Sparse Slot #' #' @description -#' `eSparse` is a generic function for setting/getting expression counts for -#' Sparse in an \code{\link{ScMaSigPro}}. +#' Retrieves expression counts from the Sparse slot of an +#' \code{\link{ScMaSigPro}} object. #' -#' @param object An object of class \code{\link{ScMaSigPro}}. -#' @param value The value to be set. (If Setting) +#' @param object An \code{\link{ScMaSigPro}} object from which to retrieve expression data. +#' @param i The name of the assay to retrieve expression counts for. +#' The default value "counts" is a placeholder that results in the method returning +#' the first assay's data found in the Sparse slot. +#' @param ... Additional arguments (not used currently). #' -#' @return The expression matrix for Sparse slot. +#' @return An expression matrix for the specified (or first, by default) assay +#' from the Sparse slot. #' -#' @author Priyansh Srivastava \email{spriyansh29@@gmail.com} +#' @seealso \code{\link{eSparse<-}} for setting expression counts in the Sparse slot. #' #' @export -setMethod("eSparse", "ScMaSigPro", function(object, value = "missing") { - if (identical(value, "missing")) { - return(as.matrix(assay(object@Sparse))) +setMethod("eSparse", signature(object = "ScMaSigPro"), function(object, i = "counts", ...) { + if (i == "counts") { + assayNames <- names(object@Sparse@assays@data@listData) + if (length(assayNames) > 0) { + return(as.matrix(object@Sparse@assays@data@listData[[assayNames[1]]])) + } else { + stop("No assays found in the object.") + } } else { - return(as.matrix(assay(object@Sparse, value))) + # Return the specific assay's data matrix + if (!i %in% names(object@Sparse@assays@data@listData)) { + stop(paste0("Assay '", i, "' not found in the object.")) + } + return(as.matrix(object@Sparse@assays@data@listData[[i]])) } }) -#' @title Get Value for expression counts. +#' @title Set Expression Counts in Sparse Slot #' #' @description -#' `eSparse<-` is a generic function for setting expression counts for -#' Sparse in an \code{\link{ScMaSigPro}}. +#' Sets or updates expression counts in the Sparse slot of an +#' \code{\link{ScMaSigPro}} object. #' -#' @param object An object of class \code{\link{ScMaSigPro}}. -#' @param value The value to be set. (If Setting) -#' @param i Name of the Assay. +#' @param object An \code{\link{ScMaSigPro}} object to be modified. +#' @param i The name of the assay for which to set or update expression counts. +#' Must be provided as a character string. +#' @param value The new expression data to set for the specified assay. #' -#' @return Modified `ScMaSigPro` object. -#' @author Priyansh Srivastava \email{spriyansh29@@gmail.com} +#' @return The modified \code{\link{ScMaSigPro}} object with the updated or new expression data +#' in the Sparse slot. +#' +#' @seealso \code{\link{eSparse}} for retrieving expression counts from the Sparse slot. #' #' @export -setMethod("eSparse<-", "ScMaSigPro", function(object, i, value) { - assay(object@Sparse, i) <- value - return(invisible(object)) +setMethod("eSparse<-", signature(object = "ScMaSigPro", i = "character", value = "matrix"), function(object, i, value) { + if (!inherits(value, "dgCMatrix")) { + value <- as(value, "dgCMatrix") + } + + # Check if the specified assay exists; if not, create a new entry + if (!i %in% names(object@Sparse@assays@data@listData)) { + message(paste0("Creating a new assay as '", i, "'.")) + # Update the assay data + object@Sparse@assays@data@listData[[i]] <- value + } else if (i %in% names(object@Sparse@assays@data@listData)) { + warning(paste0("Overwritting assay '", i, "'.")) + # Update the assay data + object@Sparse@assays@data@listData[[i]] <- value + } + + # Return the modified object + return(object) }) + ############################################################################## -#' @title Set value for expression counts. +#' @title Retrieve Expression Counts from Dense Slot #' #' @description -#' `eDense<-` is a generic function for setting expression counts for Dense -#' in an \code{\link{ScMaSigPro}}. +#' `eDense` is a generic function for retrieving expression counts stored in the Dense slot +#' of an \code{\link{ScMaSigPro}} object. #' #' @param object An object of class \code{\link{ScMaSigPro}}. -#' @param value The value to be set. -#' @param i Name of the assay. +#' @param i The name of the dataset to retrieve expression counts for. +#' Default is "bulk.counts", which will return the default expression matrix. +#' @param ... Additional arguments (not used currently). #' -#' @return Modified `ScMaSigPro` object. +#' @return An expression matrix for the specified dataset from the Dense slot. #' #' @author Priyansh Srivastava \email{spriyansh29@@gmail.com} #' #' @export -setGeneric("eDense<-", function(object, i, value) standardGeneric("eDense<-")) +setGeneric("eDense", function(object, i = "bulk.counts", ...) standardGeneric("eDense")) -#' @title Get Value for expression counts. +#' @title Set Expression Counts in Dense Slot #' #' @description -#' `eDense<-` is a generic function for getting expression counts for Dense -#' in an \code{\link{ScMaSigPro}}. +#' `eDense<-` is a generic function for updating or setting expression counts in the Dense slot +#' of an \code{\link{ScMaSigPro}} object. #' #' @param object An object of class \code{\link{ScMaSigPro}}. -#' @param value The value to be set. (If Setting) +#' @param i The name of the dataset for which to set expression counts. +#' Default is "bulk.counts". +#' @param value The new expression matrix to be set for the specified dataset. #' -#' @return The expression matrix for Dense slot. +#' @return The modified \code{\link{ScMaSigPro}} object with updated expression +#' data in the Dense slot. #' #' @author Priyansh Srivastava \email{spriyansh29@@gmail.com} #' #' @export -setGeneric("eDense", function(object, value = "missing") standardGeneric("eDense")) +setGeneric("eDense<-", function(object, i = "bulk.counts", value) standardGeneric("eDense<-")) -#' @title Get Value for expression counts. -#' -#' @importFrom SummarizedExperiment assay +#' @title Retrieve Expression Data from Dense Slot #' #' @description -#' `eDense` is a generic function for setting/getting expression counts for -#' Dense in an \code{\link{ScMaSigPro}}. +#' Retrieves expression data stored in the Dense slot of an +#' \code{\link{ScMaSigPro}} object. #' -#' @param object An object of class \code{\link{ScMaSigPro}}. -#' @param value The value to be set. (If Setting) +#' @param object An \code{\link{ScMaSigPro}} object from which to retrieve +#' the expression data. +#' @param i The name of the dataset to retrieve. The default value is "bulk.counts". +#' @param ... Additional arguments (not used currently). #' -#' @return The expression matrix for Dense slot. +#' @return Returns an expression matrix for the specified dataset from the Dense slot. +#' +#' @seealso \code{\link{eDense<-}} for setting expression data in the Dense slot. #' #' @author Priyansh Srivastava \email{spriyansh29@@gmail.com} #' #' @export -setMethod("eDense", "ScMaSigPro", function(object, value = "missing") { - if (identical(value, "missing")) { - return(assay(object@Dense)) +setMethod("eDense", signature(object = "ScMaSigPro"), function(object, i = "bulk.counts", ...) { + if (i == "bulk.counts") { + assayNames <- names(object@Dense@assays@data@listData) + if (length(assayNames) > 0) { + return(as.matrix(object@Dense@assays@data@listData[[assayNames[1]]])) + } else { + stop("No assays found in the object.") + } } else { - return(assay(object@Dense, value)) + # Return the specific assay's data matrix + if (!i %in% names(object@Dense@assays@data@listData)) { + stop(paste0("Assay '", i, "' not found in the object.")) + } + return(as.matrix(object@Dense@assays@data@listData[[i]])) } }) -#' @title Get Value for expression counts. +#' @title Set or Update Expression Data in Dense Slot #' #' @description -#' `eDense<-` is a generic function for setting expression counts for -#' Sparse in an \code{\link{ScMaSigPro}}. +#' `eDense<-` updates or sets new expression data within the Dense slot of an +#' \code{\link{ScMaSigPro}} object. #' -#' @param object An object of class \code{\link{ScMaSigPro}}. -#' @param value The value to be set. (If Setting) -#' @param i Name of the Assay. +#' @param object An \code{\link{ScMaSigPro}} object to be modified. +#' @param i The name of the dataset within the Dense slot to set or update. +#' @param value The expression data to set for the specified dataset. #' -#' @return Modified `ScMaSigPro` object. +#' @return The modified \code{\link{ScMaSigPro}} object with the updated or new expression data +#' in the Dense slot. +#' +#' @seealso \code{\link{eDense}} for retrieving expression data from the Dense slot. #' #' @author Priyansh Srivastava \email{spriyansh29@@gmail.com} #' #' @export -setMethod("eDense<-", "ScMaSigPro", function(object, i, value) { - assay(object@Dense, i) <- value - return(invisible(object)) +setMethod("eDense<-", signature(object = "ScMaSigPro", i = "character", value = "matrix"), function(object, i, value) { + if (!inherits(value, "dgCMatrix")) { + value <- as(value, "dgCMatrix") + } + + # Check if the specified assay exists; if not, create a new entry + if (!i %in% names(object@Dense@assays@data@listData)) { + message(paste0("Creating a new assay as '", i, "'.")) + # Update the assay data + object@Dense@assays@data@listData[[i]] <- value + } else if (i %in% names(object@Dense@assays@data@listData)) { + warning(paste0("Overwritting assay '", i, "'.")) + # Update the assay data + object@Dense@assays@data@listData[[i]] <- value + } + + # Return the modified object + return(object) }) + ############################################################################## #' @title Get or set the Branch Assignment Matrix #' diff --git a/man/eDense-ScMaSigPro-method.Rd b/man/eDense-ScMaSigPro-method.Rd index 4447cb4..18cecb7 100644 --- a/man/eDense-ScMaSigPro-method.Rd +++ b/man/eDense-ScMaSigPro-method.Rd @@ -2,21 +2,27 @@ % Please edit documentation in R/scmpGenerics.R \name{eDense,ScMaSigPro-method} \alias{eDense,ScMaSigPro-method} -\title{Get Value for expression counts.} +\title{Retrieve Expression Data from Dense Slot} \usage{ -\S4method{eDense}{ScMaSigPro}(object, value = "missing") +\S4method{eDense}{ScMaSigPro}(object, i = "bulk.counts", ...) } \arguments{ -\item{object}{An object of class \code{\link{ScMaSigPro}}.} +\item{object}{An \code{\link{ScMaSigPro}} object from which to retrieve +the expression data.} -\item{value}{The value to be set. (If Setting)} +\item{i}{The name of the dataset to retrieve. The default value is "bulk.counts".} + +\item{...}{Additional arguments (not used currently).} } \value{ -The expression matrix for Dense slot. +Returns an expression matrix for the specified dataset from the Dense slot. } \description{ -`eDense` is a generic function for setting/getting expression counts for -Dense in an \code{\link{ScMaSigPro}}. +Retrieves expression data stored in the Dense slot of an +\code{\link{ScMaSigPro}} object. +} +\seealso{ +\code{\link{eDense<-}} for setting expression data in the Dense slot. } \author{ Priyansh Srivastava \email{spriyansh29@gmail.com} diff --git a/man/eDense-set-ScMaSigPro-character-matrix-method.Rd b/man/eDense-set-ScMaSigPro-character-matrix-method.Rd new file mode 100644 index 0000000..a568756 --- /dev/null +++ b/man/eDense-set-ScMaSigPro-character-matrix-method.Rd @@ -0,0 +1,29 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/scmpGenerics.R +\name{eDense<-,ScMaSigPro,character,matrix-method} +\alias{eDense<-,ScMaSigPro,character,matrix-method} +\title{Set or Update Expression Data in Dense Slot} +\usage{ +\S4method{eDense}{ScMaSigPro,character,matrix}(object, i = "bulk.counts") <- value +} +\arguments{ +\item{object}{An \code{\link{ScMaSigPro}} object to be modified.} + +\item{i}{The name of the dataset within the Dense slot to set or update.} + +\item{value}{The expression data to set for the specified dataset.} +} +\value{ +The modified \code{\link{ScMaSigPro}} object with the updated or new expression data +in the Dense slot. +} +\description{ +`eDense<-` updates or sets new expression data within the Dense slot of an +\code{\link{ScMaSigPro}} object. +} +\seealso{ +\code{\link{eDense}} for retrieving expression data from the Dense slot. +} +\author{ +Priyansh Srivastava \email{spriyansh29@gmail.com} +} diff --git a/man/eDense-set-ScMaSigPro-method.Rd b/man/eDense-set-ScMaSigPro-method.Rd deleted file mode 100644 index 9bc158d..0000000 --- a/man/eDense-set-ScMaSigPro-method.Rd +++ /dev/null @@ -1,25 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/scmpGenerics.R -\name{eDense<-,ScMaSigPro-method} -\alias{eDense<-,ScMaSigPro-method} -\title{Get Value for expression counts.} -\usage{ -\S4method{eDense}{ScMaSigPro}(object, i) <- value -} -\arguments{ -\item{object}{An object of class \code{\link{ScMaSigPro}}.} - -\item{i}{Name of the Assay.} - -\item{value}{The value to be set. (If Setting)} -} -\value{ -Modified `ScMaSigPro` object. -} -\description{ -`eDense<-` is a generic function for setting expression counts for -Sparse in an \code{\link{ScMaSigPro}}. -} -\author{ -Priyansh Srivastava \email{spriyansh29@gmail.com} -} diff --git a/man/eDense-set.Rd b/man/eDense-set.Rd index 9ceb1cb..c1b64cc 100644 --- a/man/eDense-set.Rd +++ b/man/eDense-set.Rd @@ -2,23 +2,25 @@ % Please edit documentation in R/scmpGenerics.R \name{eDense<-} \alias{eDense<-} -\title{Set value for expression counts.} +\title{Set Expression Counts in Dense Slot} \usage{ -eDense(object, i) <- value +eDense(object, i = "bulk.counts") <- value } \arguments{ \item{object}{An object of class \code{\link{ScMaSigPro}}.} -\item{i}{Name of the assay.} +\item{i}{The name of the dataset for which to set expression counts. +Default is "bulk.counts".} -\item{value}{The value to be set.} +\item{value}{The new expression matrix to be set for the specified dataset.} } \value{ -Modified `ScMaSigPro` object. +The modified \code{\link{ScMaSigPro}} object with updated expression +data in the Dense slot. } \description{ -`eDense<-` is a generic function for setting expression counts for Dense -in an \code{\link{ScMaSigPro}}. +`eDense<-` is a generic function for updating or setting expression counts in the Dense slot +of an \code{\link{ScMaSigPro}} object. } \author{ Priyansh Srivastava \email{spriyansh29@gmail.com} diff --git a/man/eDense.Rd b/man/eDense.Rd index 4f78448..8ebb322 100644 --- a/man/eDense.Rd +++ b/man/eDense.Rd @@ -2,21 +2,24 @@ % Please edit documentation in R/scmpGenerics.R \name{eDense} \alias{eDense} -\title{Get Value for expression counts.} +\title{Retrieve Expression Counts from Dense Slot} \usage{ -eDense(object, value = "missing") +eDense(object, i = "bulk.counts", ...) } \arguments{ \item{object}{An object of class \code{\link{ScMaSigPro}}.} -\item{value}{The value to be set. (If Setting)} +\item{i}{The name of the dataset to retrieve expression counts for. +Default is "bulk.counts", which will return the default expression matrix.} + +\item{...}{Additional arguments (not used currently).} } \value{ -The expression matrix for Dense slot. +An expression matrix for the specified dataset from the Dense slot. } \description{ -`eDense<-` is a generic function for getting expression counts for Dense -in an \code{\link{ScMaSigPro}}. +`eDense` is a generic function for retrieving expression counts stored in the Dense slot +of an \code{\link{ScMaSigPro}} object. } \author{ Priyansh Srivastava \email{spriyansh29@gmail.com} diff --git a/man/eSparse-ScMaSigPro-method.Rd b/man/eSparse-ScMaSigPro-method.Rd index f04af2b..cc1e8f0 100644 --- a/man/eSparse-ScMaSigPro-method.Rd +++ b/man/eSparse-ScMaSigPro-method.Rd @@ -2,22 +2,27 @@ % Please edit documentation in R/scmpGenerics.R \name{eSparse,ScMaSigPro-method} \alias{eSparse,ScMaSigPro-method} -\title{Get Value for expression counts.} +\title{Retrieve Expression Counts from Sparse Slot} \usage{ -\S4method{eSparse}{ScMaSigPro}(object, value = "missing") +\S4method{eSparse}{ScMaSigPro}(object, i = "counts", ...) } \arguments{ -\item{object}{An object of class \code{\link{ScMaSigPro}}.} +\item{object}{An \code{\link{ScMaSigPro}} object from which to retrieve expression data.} -\item{value}{The value to be set. (If Setting)} +\item{i}{The name of the assay to retrieve expression counts for. +The default value "counts" is a placeholder that results in the method returning +the first assay's data found in the Sparse slot.} + +\item{...}{Additional arguments (not used currently).} } \value{ -The expression matrix for Sparse slot. +An expression matrix for the specified (or first, by default) assay +from the Sparse slot. } \description{ -`eSparse` is a generic function for setting/getting expression counts for -Sparse in an \code{\link{ScMaSigPro}}. +Retrieves expression counts from the Sparse slot of an +\code{\link{ScMaSigPro}} object. } -\author{ -Priyansh Srivastava \email{spriyansh29@gmail.com} +\seealso{ +\code{\link{eSparse<-}} for setting expression counts in the Sparse slot. } diff --git a/man/eSparse-set-ScMaSigPro-character-matrix-method.Rd b/man/eSparse-set-ScMaSigPro-character-matrix-method.Rd new file mode 100644 index 0000000..50934d4 --- /dev/null +++ b/man/eSparse-set-ScMaSigPro-character-matrix-method.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/scmpGenerics.R +\name{eSparse<-,ScMaSigPro,character,matrix-method} +\alias{eSparse<-,ScMaSigPro,character,matrix-method} +\title{Set Expression Counts in Sparse Slot} +\usage{ +\S4method{eSparse}{ScMaSigPro,character,matrix}(object, i = "counts") <- value +} +\arguments{ +\item{object}{An \code{\link{ScMaSigPro}} object to be modified.} + +\item{i}{The name of the assay for which to set or update expression counts. +Must be provided as a character string.} + +\item{value}{The new expression data to set for the specified assay.} +} +\value{ +The modified \code{\link{ScMaSigPro}} object with the updated or new expression data +in the Sparse slot. +} +\description{ +Sets or updates expression counts in the Sparse slot of an +\code{\link{ScMaSigPro}} object. +} +\seealso{ +\code{\link{eSparse}} for retrieving expression counts from the Sparse slot. +} diff --git a/man/eSparse-set-ScMaSigPro-method.Rd b/man/eSparse-set-ScMaSigPro-method.Rd deleted file mode 100644 index c4be6ce..0000000 --- a/man/eSparse-set-ScMaSigPro-method.Rd +++ /dev/null @@ -1,25 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/scmpGenerics.R -\name{eSparse<-,ScMaSigPro-method} -\alias{eSparse<-,ScMaSigPro-method} -\title{Get Value for expression counts.} -\usage{ -\S4method{eSparse}{ScMaSigPro}(object, i) <- value -} -\arguments{ -\item{object}{An object of class \code{\link{ScMaSigPro}}.} - -\item{i}{Name of the Assay.} - -\item{value}{The value to be set. (If Setting)} -} -\value{ -Modified `ScMaSigPro` object. -} -\description{ -`eSparse<-` is a generic function for setting expression counts for -Sparse in an \code{\link{ScMaSigPro}}. -} -\author{ -Priyansh Srivastava \email{spriyansh29@gmail.com} -} diff --git a/man/eSparse-set.Rd b/man/eSparse-set.Rd index 2ff824b..e16b457 100644 --- a/man/eSparse-set.Rd +++ b/man/eSparse-set.Rd @@ -2,23 +2,25 @@ % Please edit documentation in R/scmpGenerics.R \name{eSparse<-} \alias{eSparse<-} -\title{Set value for expression counts.} +\title{Set Expression Counts in Sparse Slot} \usage{ -eSparse(object, i) <- value +eSparse(object, i = "counts") <- value } \arguments{ \item{object}{An object of class \code{\link{ScMaSigPro}}.} -\item{i}{Name of the assay.} +\item{i}{The name of the assay for which to set expression counts. +Default is "counts", which targets the default expression counts slot.} -\item{value}{The value to be set.} +\item{value}{The new expression matrix to be set for the specified assay.} } \value{ -Modified `ScMaSigPro` object. +The modified \code{\link{ScMaSigPro}} object with the updated +expression data in the Sparse slot. } \description{ -`eSparse<-` is a generic function for setting expression counts for Sparse -in an \code{\link{ScMaSigPro}}. +`eSparse<-` is a generic function for setting expression counts in the Sparse slot +of an \code{\link{ScMaSigPro}} object. } \author{ Priyansh Srivastava \email{spriyansh29@gmail.com} diff --git a/man/eSparse.Rd b/man/eSparse.Rd index aa9a4a9..efe8e9d 100644 --- a/man/eSparse.Rd +++ b/man/eSparse.Rd @@ -2,21 +2,24 @@ % Please edit documentation in R/scmpGenerics.R \name{eSparse} \alias{eSparse} -\title{Get Value for expression counts.} +\title{Get Expression Counts from Sparse Slot} \usage{ -eSparse(object, value = "missing") +eSparse(object, i = "counts", ...) } \arguments{ \item{object}{An object of class \code{\link{ScMaSigPro}}.} -\item{value}{The value to be set. (If Setting)} +\item{i}{The name of the assay to retrieve expression counts for. +Default is "counts", which will return the default expression matrix.} + +\item{...}{Additional arguments (not used currently).} } \value{ -The expression matrix for Sparse slot. +An expression matrix for the specified assay from the Sparse slot. } \description{ -`eSparse<-` is a generic function for getting expression counts for Sparse -in an \code{\link{ScMaSigPro}}. +`eSparse` is a generic function for retrieving expression counts stored in the +Sparse slot of an \code{\link{ScMaSigPro}} object. } \author{ Priyansh Srivastava \email{spriyansh29@gmail.com} diff --git a/man/plotIntersect.Rd b/man/plotIntersect.Rd index 4b1ceec..db0e09f 100644 --- a/man/plotIntersect.Rd +++ b/man/plotIntersect.Rd @@ -6,15 +6,20 @@ \usage{ plotIntersect( scmpObj, + package = "UpSetR", min_intersection_size = 2, keep_empty_groups = TRUE, width_ratio = 0.1, - show_sets_size = FALSE + show_sets_size = FALSE, + verbose = TRUE ) } \arguments{ \item{scmpObj}{An object of class \code{\link{ScMaSigPro}}.} +\item{package}{Which package to use for the UpsetPlot. Options are 'ComplexUpset' +or 'UpSetR' (Default).} + \item{min_intersection_size}{Minimal number of observations in an intersection for it to be included.} @@ -24,15 +29,16 @@ which are only empty after filtering by size)} \item{width_ratio}{Ratio of the overall set size width to intersection matrix width.} -\item{show_sets_size}{The overall set sizes plot, e.g. from upset_set_size() -(FALSE to hide)} +\item{show_sets_size}{The overall set sizes plot, e.g. from upset_set_size()} + +\item{verbose}{Print detailed output in the console. (Default is TRUE)} } \value{ -ggplot2 plot object. +ggplot2 plot object for 'ComplexUpset' or upset object for 'UpSetR'. } \description{ Generate UpSet Plot on Intersection of Significant Genes from scMaSigPro -object. It is a wrapper around `ComplexUpset::upset`. +object. It is a wrapper around `ComplexUpset::upset` and `UpSetR::upset`. } \author{ Priyansh Srivastava \email{spriyansh29@gmail.com} diff --git a/tests/testthat/test-generic-extraction.R b/tests/testthat/test-generic-extraction.R new file mode 100644 index 0000000..5aac613 --- /dev/null +++ b/tests/testthat/test-generic-extraction.R @@ -0,0 +1,33 @@ +suppressPackageStartupMessages(library(testthat)) +suppressPackageStartupMessages(library(scMaSigPro)) + +test_that("Check Extraction Generics", { + # Step-1: Load Data + data("splat.sim", package = "scMaSigPro") + + # Step-2: Create scMaSigPro object + scmp_ob <- as_scmp( + object = splat.sim, from = "sce", + align_pseudotime = FALSE, + verbose = FALSE, + additional_params = list( + labels_exist = TRUE, + exist_ptime_col = "Step", + exist_path_col = "Group" + ) + ) + + # Step-3: Perform Pseudobulking + scmp_ob <- sc.squeeze(scmp_ob) + + # Check + expect_equal( + expected = as.matrix(scmp_ob@Sparse@assays@data@listData$counts), + object = eSparse(scmp_ob) + ) + + expect_equal( + expected = as.matrix(scmp_ob@Dense@assays@data@listData$bulk.counts), + object = eDense(scmp_ob) + ) +}) diff --git a/tests/testthat/test-manual-pseudobulking.R b/tests/testthat/test-manual-pseudobulking.R new file mode 100644 index 0000000..35f6ce4 --- /dev/null +++ b/tests/testthat/test-manual-pseudobulking.R @@ -0,0 +1,64 @@ +suppressPackageStartupMessages(library(testthat)) +suppressPackageStartupMessages(library(scMaSigPro)) + +test_that("Check Pseudo-bulking with manual bulking", { + # Step-1: Load Data + data("splat.sim", package = "scMaSigPro") + + # Step-2: Create scMaSigPro object + scmp_ob <- as_scmp( + object = splat.sim, from = "sce", + align_pseudotime = FALSE, + verbose = FALSE, + additional_params = list( + labels_exist = TRUE, + exist_ptime_col = "Step", + exist_path_col = "Group" + ) + ) + + # Step-3: Perform Pseudobulking + scmp_ob <- sc.squeeze(scmp_ob) + + # Extract data for a random gene + random_gene <- sample(nrow(eSparse(scmp_ob)), 1) + slot_gen9 <- eSparse(scmp_ob)[random_gene, ] + slot_Bgen9 <- eDense(scmp_ob)[random_gene, ] + + # Extract Pseudotime and the binned Pseudotime + Pseudotime <- cSparse(scmp_ob)$Pseudotime + names(Pseudotime) <- rownames(cSparse(scmp_ob)) + BinnedPseudotime <- cSparse(scmp_ob)$scmp_binned_pseudotime + names(BinnedPseudotime) <- rownames(cSparse(scmp_ob)) + + # Extract bin information + Bin <- cDense(scmp_ob)[[scmp_ob@Parameters@bin_ptime_col]] + names(Bin) <- rownames(cDense(scmp_ob)) + Bpath <- rep(c(1:2), each = length(Bin) / 2) + + # Check total sum + expect_identical( + expected = as.numeric(sum(slot_gen9)), + object = as.numeric(sum(slot_Bgen9)) + ) + + # Create vectors + sum1_new <- sum2_new <- NULL + + # Compute pseudo-bulk + for (i in 1:length(Bin) / 2) { + cell_vector <- names(BinnedPseudotime[BinnedPseudotime == i]) + cell_count_vector <- slot_gen9[names(slot_gen9) %in% cell_vector] + sum1_new <- c(sum1_new, sum(cell_count_vector)) + + bin_vector <- names(Bin[Bin == i]) + bin_count_vector <- slot_Bgen9[names(Bin) %in% bin_vector] + sum2_new <- c(sum2_new, sum(bin_count_vector)) + } + + # Check + expect_identical( + object = as.numeric(sum1_new), + expected = as.numeric(sum2_new) + ) +}) diff --git a/vignettes/Basic-Workflow.Rmd b/vignettes/Basic-Workflow.Rmd index 01d8850..5cf3d35 100644 --- a/vignettes/Basic-Workflow.Rmd +++ b/vignettes/Basic-Workflow.Rmd @@ -19,6 +19,7 @@ vignette: > ```{r, "setup", include=FALSE} knitr::opts_chunk$set(echo = TRUE) +knitr::opts_chunk$set(crop = NULL) library(scMaSigPro) ``` @@ -67,10 +68,10 @@ data("splat.sim", package = "scMaSigPro") ``` #### Plot the data (Optional) -```{r, "Plot the data (Optional)", eval=TRUE, echo=TRUE} +```{r, "Plot the data (Optional)", eval=TRUE, echo=TRUE, fig.width=8, fig.height=6} # Load -suppressPackageStartupMessages(library(ggplot2)) -suppressPackageStartupMessages(library(SingleCellExperiment)) +suppressPackageStartupMessages(require(ggplot2)) +suppressPackageStartupMessages(require(SingleCellExperiment)) # Extract PCA df <- as.data.frame(reducedDim(splat.sim)[, c(1:2)]) @@ -174,7 +175,7 @@ bin for each path. We can also visually inspect the binning process using a tile plot: -```{r, "Visualize bins",eval=TRUE, echo=TRUE} +```{r, "Visualize bins",eval=TRUE, echo=TRUE, fig.width=5, fig.height=3} plotBinTile(scmp_ob) ``` @@ -188,7 +189,7 @@ pseudotime scale, modeled with a quadratic polynomial. The model can be represen as follows: ```{r, "formula", echo=FALSE} -library(knitr) +require(knitr) knitr::asis_output(" \\begin{align*} Y_{i} &\\sim \\text{NegativeBinomial}(\\mu_{i}, \\theta = 10) \\\\ @@ -317,8 +318,8 @@ scmp_ob <- sc.filter( By setting the vars parameter to "groups", the function will add genes with $R^2$ >= 0.7 to the object. To explore the number of genes per group, we will make an upset plot: -```{r, "uspet",eval=TRUE, echo=TRUE} -plotIntersect(scmp_ob) +```{r, "uspet",eval=TRUE, echo=TRUE, fig.width=8, fig.height=6} +plotIntersect(scmp_ob, package = "ComplexUpset") ``` Here, we observe that 23 genes belong to both Path2vsPath1 and Path1, indicating @@ -365,7 +366,7 @@ scmp_ob <- sc.cluster.trend( ) ``` -```{r, "plot"} +```{r, "plot", fig.width=8, fig.height=6} # Plot plotTrendCluster( scmpObj = scmp_ob, @@ -384,7 +385,7 @@ to other vignettes for more in-depth analysis. ```{r, "Save and Reload", eval=TRUE, echo=FALSE, message=FALSE, warning=FALSE} # Save the object - save(splat.sim, file = "../data/splat.sim.RData") +save(splat.sim, file = "../data/splat.sim.RData") # Save processed data for next tutorial scmp.ob <- scmp_ob diff --git a/vignettes/scMaSigPro-Class.Rmd b/vignettes/scMaSigPro-Class.Rmd index 22b040d..7fc0d5f 100644 --- a/vignettes/scMaSigPro-Class.Rmd +++ b/vignettes/scMaSigPro-Class.Rmd @@ -18,7 +18,9 @@ vignette: > ```{r, "setup", include=FALSE} knitr::opts_chunk$set(echo = TRUE) +knitr::opts_chunk$set(crop = NULL) library(scMaSigPro) +library(patchwork) ``` ## Introduction diff --git a/vignettes/scMaSigPro-maSigPro.Rmd b/vignettes/scMaSigPro-maSigPro.Rmd index 70128ea..3929ca0 100644 --- a/vignettes/scMaSigPro-maSigPro.Rmd +++ b/vignettes/scMaSigPro-maSigPro.Rmd @@ -18,6 +18,7 @@ vignette: > ```{r, "setup", include=FALSE} knitr::opts_chunk$set(echo = TRUE) +knitr::opts_chunk$set(crop = NULL) library(scMaSigPro) ``` @@ -185,7 +186,10 @@ mismatches; if everything is identical, it will not throw an error. The `eDense()` function retrieves the dense expression file from the `scMaSigPro` object. ```{r, "eDense(scmp_ob)"} -expect_identical(eDense(scmp_ob), expected = data.abiotic) +expect_identical( + object = as.data.frame(eDense(scmp_ob)), + expected = data.abiotic +) ``` No errors were thrown, indicating that our setup is correct and we can proceed @@ -394,7 +398,7 @@ plotTrendCluster( scmpObj = scmp_ob, plot = "coeff", verbose = FALSE - ) +) ``` ---