Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
TuomasBorman committed Jan 22, 2025
1 parent 5d68c54 commit 581e8e3
Show file tree
Hide file tree
Showing 18 changed files with 108 additions and 104 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ importFrom(stats,na.omit)
importFrom(stats,p.adjust)
importFrom(stats,runif)
importFrom(stats,sd)
importFrom(stats,setNames)
importFrom(stats,terms)
importFrom(tibble,rownames_to_column)
importFrom(tibble,tibble)
Expand Down
2 changes: 1 addition & 1 deletion R/addAlpha.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#'
#' @inheritParams calculateDMN
#'
#' @param x a \code{\link{SummarizedExperiment}} object.
#' @param x a \code{\link[SummarizedExperiment]{SummarizedExperiment}} object.
#'
#' @param assay.type \code{Character scalar}. Specifies the name of assay
#' used in calculation. (Default: \code{"counts"})
Expand Down
47 changes: 24 additions & 23 deletions R/addDivergence.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#' Estimate divergence
#'
#' Estimate divergence against a given reference sample.
#'
#'
#' @inheritParams addDissimilarity
#'
#' @param x a \code{\link{SummarizedExperiment}} object.
#'
#'
#' @param x a \code{\link[SummarizedExperiment]{SummarizedExperiment}} object.
#'
#' @param assay_name Deprecated. Use \code{assay.type} instead.
#'
#' @param reference \code{Character scalar}. A column name from
Expand All @@ -20,53 +20,54 @@
#' values are used to calculate divergence instead of the assay. Can be
#' disabled with \code{NULL}. (Default: \code{NULL})
#' }
#'
#' @return \code{x} with additional \code{\link{colData}} named \code{name}
#'
#'
#' @return \code{x} with additional
#' \code{\link[SummarizedExperiment:colData]{colData}} named \code{name}
#'
#' @details
#'
#' Microbiota divergence (heterogeneity / spread) within a given sample
#' set can be quantified by the average sample dissimilarity or beta
#' diversity with respect to a given reference sample.
#'
#' The calculation makes use of the function \code{getDissimilarity()}. The
#' divergence
#' measure is sensitive to sample size. Subsampling or bootstrapping can be
#' divergence
#' measure is sensitive to sample size. Subsampling or bootstrapping can be
#' applied to equalize sample sizes between comparisons.
#'
#'
#' @seealso
#' \itemize{
#' \item \code{\link[=addAlpha]{addAlpha}}
#' \item \code{\link[=addDissimilarity]{addDissimilarity}}
#' \item \code{\link[scater:plotColData]{plotColData}}
#' }
#'
#'
#' @name addDivergence
#' @export
#'
#'
#' @examples
#' data(GlobalPatterns)
#' tse <- GlobalPatterns
#'
#'
#' # By default, reference is median of all samples. The name of column where
#' # results is "divergence" by default, but it can be specified.
#' # results is "divergence" by default, but it can be specified.
#' tse <- addDivergence(tse)
#'
#' # The method that are used to calculate distance in divergence and
#'
#' # The method that are used to calculate distance in divergence and
#' # reference can be specified. Here, euclidean distance is used. Reference is
#' # the first sample. It is recommended # to add reference to colData.
#' tse[["reference"]] <- rep(colnames(tse)[[1]], ncol(tse))
#' tse <- addDivergence(
#' tse, name = "divergence_first_sample",
#' tse, name = "divergence_first_sample",
#' reference = "reference",
#' method = "euclidean")
#'
#'
#' # Here we compare samples to global mean
#' tse <- addDivergence(tse, name = "divergence_average", reference = "mean")
#'
#'
#' # All three divergence results are stored in colData.
#' colData(tse)
#'
#'
NULL

#' @rdname addDivergence
Expand All @@ -91,7 +92,7 @@ setMethod("addDivergence", signature = c(x="SummarizedExperiment"),
#' @export
setMethod("getDivergence", signature = c(x="SummarizedExperiment"),
function(
x, assay.type = assay_name, assay_name = "counts",
x, assay.type = assay_name, assay_name = "counts",
reference = "median", method = "bray", ...){
################### Input check ###############
# Get altExp if user has specified
Expand Down Expand Up @@ -182,7 +183,7 @@ setMethod("getDivergence", signature = c(x="SummarizedExperiment"),
} else{
mat <- t(reducedDim(x, dimred))
}

# If reference type is median or mean, calculate it
if( ref_type %in% c("median", "mean") ){
reference <- apply(mat, 1, ref_type)
Expand Down Expand Up @@ -231,7 +232,7 @@ setMethod("getDivergence", signature = c(x="SummarizedExperiment"),
temp <- temp[[1]]
return(temp)
})
# Add values to data.frame that holds sample pairs
# Add values to data.frame that holds sample pairs
temp <- unlist(temp)
reference[["value"]] <- temp
return(reference)
Expand Down
39 changes: 19 additions & 20 deletions R/cluster.R
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
#' Clustering wrapper
#'
#' This function returns a \code{SummarizedExperiment} with clustering
#' This function returns a \code{SummarizedExperiment} with clustering
#' information in its colData or rowData
#'
#' @param x A
#' \code{\link[SummarizedExperiment:SummarizedExperiment-class]{SummarizedExperiment}}
#' object.
#'
#'
#' @param by \code{Character scalar}. Determines if association is calculated
#' row-wise / for features ('rows') or column-wise / for samples ('cols').
#' Must be \code{'rows'} or \code{'cols'}.
#'
#'
#' @param MARGIN Deprecated. Use \code{by} instead.
#'
#' @param clust.col \code{Character scalar}. Indicates the name of the
#'
#' @param clust.col \code{Character scalar}. Indicates the name of the
#' \code{rowData} (or \code{colData}) where the data will be stored.
#' (Default: \code{"clusters"})
#'
#'
#' @param ... Additional parameters to use altExps for example
#' @inheritParams bluster::clusterRows
#' @inheritParams runDMN
#' @inheritParams transformAssay
#'
#'
#'
#'
#' @details
#' This is a wrapper for the \code{clusterRows} function from the
#' \link[https://bioconductor.org/packages/release/bioc/html/bluster.html]{bluster}
#' package.
#' This is a wrapper for the \code{clusterRows} function from the
#' \link[bluster]{bluster} package.
#'
#' When setting \code{full = TRUE}, the clustering information will be stored in
#' the metadata of the object.
#'
#'
#' By default, clustering is done on the features.
#'
#' @return
#' \code{addCluster} returns an object of the same type as the \code{x}
#' parameter with clustering information named \code{clusters} stored in
#' \code{colData} or \code{rowData}.
#' \code{colData} or \code{rowData}.
#'
#' @name addCluster
#' @export
Expand All @@ -48,24 +47,24 @@
#'
#' # Cluster on rows using Kmeans
#' tse <- addCluster(tse, KmeansParam(centers = 3))
#'
#'
#' # Clustering done on the samples using Hclust
#' tse <- addCluster(tse,
#' by = "samples",
#' tse <- addCluster(tse,
#' by = "samples",
#' HclustParam(metric = "bray", dist.fun = vegan::vegdist))
#'
#'
#' # Getting the clusters
#' colData(tse)$clusters
#'
#'
NULL

#' @rdname addCluster
#' @export
#' @importFrom bluster clusterRows
setMethod("addCluster", signature = c(x = "SummarizedExperiment"),
function(
x, BLUSPARAM, assay.type = assay_name,
assay_name = "counts", by = MARGIN, MARGIN = "rows", full = FALSE,
x, BLUSPARAM, assay.type = assay_name,
assay_name = "counts", by = MARGIN, MARGIN = "rows", full = FALSE,
name = "clusters", clust.col = "clusters", ...) {
.require_package("bluster")
# Checking parameters
Expand Down
2 changes: 1 addition & 1 deletion R/getPrevalence.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Calculation prevalence information for features across samples
#'
#' These functions calculate the population prevalence for taxonomic ranks in a
#' \code{\link{SummarizedExperiment-class}} object.
#' \code{\link[SummarizedExperiment]{SummarizedExperiment}} object.
#'
#' @inheritParams getDissimilarity
#'
Expand Down
5 changes: 3 additions & 2 deletions R/mergeSEs.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
#' @inheritParams rarefyAssay
#' @inheritParams getDominant
#'
#' @param y a \code{\link{SummarizedExperiment}} object when \code{x} is a
#' \code{\link{SummarizedExperiment}} object. Disabled when \code{x} is a list.
#' @param y a \code{\link[SummarizedExperiment]{SummarizedExperiment}} object
#' when \code{x} is a \code{\link[SummarizedExperiment]{SummarizedExperiment}}
#' object. Disabled when \code{x} is a list.
#'
#' @param join \code{Character scalar}. A value for selecting the joining
#' method. Must be 'full', 'inner', 'left', or 'right'. 'left' and 'right' are
Expand Down
25 changes: 13 additions & 12 deletions R/runDPCoA.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
#' @inheritParams getDissimilarity
#'
#' @details
#' For \code{addDPCoA} a \linkS4class{TreeSummarizedExperiment} containing the
#' expression values as well as a \code{rowTree} to calculate \code{y} using
#' \code{\link[ape:cophenetic.phylo]{cophenetic.phylo}}.
#' For \code{addDPCoA} a
#' \code{\link[TreeSummarizedExperiment]{TreeSummarizedExperiment}} containing
#' the expression values as well as a \code{rowTree} to calculate \code{y} using
#' \code{\link[ape:cophenetic.phylo]{cophenetic.phylo}}.
#'
#' @param y a \code{dist} or a symmetric \code{matrix} compatible with
#' \code{ade4:dpcoa}
Expand All @@ -26,24 +27,24 @@
#' to use for dimensionality reduction. This can be a character vector of row
#' names, an integer vector of row indices or a logical vector.
#' (Default: \code{NULL})
#'
#'
#' @param subset_row Deprecated. Use \code{subset.row} instead.
#'
#' @param scale \code{Logical scalar}. Should the expression values be
#' standardized? (Default: \code{FALSE})
#'
#' @param name \code{Character scalar}. A name for the column of the
#'
#' @param name \code{Character scalar}. A name for the column of the
#' \code{colData} where results will be stored. (Default: \code{"DPCoA"})
#'
#'
#' @param altexp \code{Character scalar} or \code{integer scalar}. Specifies an
#' alternative experiment containing the input data. (Default: \code{NULL})
#'
#'
#' @param exprs_values Deprecated. Use \code{assay.type} instead.
#'
#'
#' @param tree.name \code{Character scalar}. Specifies the name of the
#' tree to be included in the phyloseq object that is created,
#' tree to be included in the phyloseq object that is created,
#' (Default: \code{"phylo"})
#'
#'
#' @param tree_name Deprecated. Use \code{tree.name} instead.
#'
#' @param ... Currently not used.
Expand Down Expand Up @@ -142,7 +143,7 @@ setMethod("getDPCoA", c("ANY","ANY"), .calculate_dpcoa)
#' @importFrom ape cophenetic.phylo
#' @rdname runDPCoA
setMethod("getDPCoA", signature = c("TreeSummarizedExperiment","missing"),
function(x, ..., assay.type = assay_name, assay_name = exprs_values,
function(x, ..., assay.type = assay_name, assay_name = exprs_values,
exprs_values = "counts", tree.name = tree_name, tree_name = "phylo")
{
.require_package("ade4")
Expand Down
29 changes: 15 additions & 14 deletions R/runNMDS.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
#'
#' @inheritParams getDominant
#' @inheritParams runDPCoA
#'
#'
#' @details
#' For \code{addNMDS} a \linkS4class{SingleCellExperiment}
#' For \code{addNMDS} a
#' \code{\link[TreeSummarizedExperiment]{TreeSummarizedExperiment}}
#'
#' @param keep.dist \code{Logical scalar}. Indicates whether the \code{dist}
#' object calculated by \code{FUN} should be stored as \sQuote{dist} attribute
#' of the matrix returned/stored by \code{getNMDS}/ \code{addNMDS}. (Default:
#' of the matrix returned/stored by \code{getNMDS}/ \code{addNMDS}. (Default:
#' \code{FALSE})
#'
#'
#' @param keep_dist Deprecated. Use \code{keep.dist} instead.
#'
#' @param FUN \code{Function} or \code{Character scalar}. A value with a
Expand All @@ -23,10 +24,10 @@
#' implementation, either \dQuote{isoMDS} for
#' \code{\link[MASS:isoMDS]{MASS::isoMDS}} or \dQuote{monoMDS} for
#' \code{\link[vegan:monoMDS]{vegan::monoMDS}}
#'
#'
#' @param nmdsFUN Deprecated. Use \code{nmds.fun} instead.
#'
#' @param name \code{Character scalar}. A name for the column of the
#'
#' @param name \code{Character scalar}. A name for the column of the
#' \code{colData} where results will be stored. (Default: \code{"NMDS"})
#'
#' @param ... additional arguments to pass to \code{FUN} and
Expand All @@ -37,9 +38,9 @@
#'
#' @param ndimred \code{integer vector}. Specifies the dimensions to use if
#' dimred is specified.
#'
#'
#' @param n_dimred Deprecated. Use \code{ndimred} instead.
#'
#'
#' @param exprs_values Deprecated. Use \code{assay.type} instead.
#'
#' @return For \code{getNMDS}, a matrix is returned containing the MDS
Expand Down Expand Up @@ -125,10 +126,10 @@ NULL
#' @importFrom stats cmdscale
#' @importFrom vegan vegdist monoMDS
.calculate_nmds <- function(
x, FUN = vegdist,
x, FUN = vegdist,
nmds.fun = nmdsFUN,
nmdsFUN = c("isoMDS","monoMDS"),
ncomponents = 2, ntop = 500, subset.row = subset_row,
ncomponents = 2, ntop = 500, subset.row = subset_row,
subset_row = NULL, scale = FALSE, transposed = FALSE,
keep.dist = keep_dist,
keep_dist = FALSE, ...){
Expand Down Expand Up @@ -162,7 +163,7 @@ setMethod("getNMDS", "ANY", .calculate_nmds)
#' @importFrom SummarizedExperiment assay
#' @export
setMethod("getNMDS", "SummarizedExperiment",
function(x, ..., assay.type = assay_name, assay_name = exprs_values,
function(x, ..., assay.type = assay_name, assay_name = exprs_values,
exprs_values = "counts", FUN = vegdist) {
.calculate_nmds(assay(x, assay.type), FUN = FUN, ...)
}
Expand All @@ -171,8 +172,8 @@ setMethod("getNMDS", "SummarizedExperiment",
#' @rdname runNMDS
#' @export
setMethod("getNMDS", "SingleCellExperiment",
function(x, ..., assay.type = assay_name, assay_name = exprs_values,
exprs_values = "counts", dimred = NULL, ndimred = n_dimred,
function(x, ..., assay.type = assay_name, assay_name = exprs_values,
exprs_values = "counts", dimred = NULL, ndimred = n_dimred,
n_dimred = NULL, FUN = vegdist){
mat <- .get_mat_from_sce(
x, exprs_values = assay.type, dimred = dimred, n_dimred = ndimred)
Expand Down
Loading

0 comments on commit 581e8e3

Please sign in to comment.