Skip to content

Commit

Permalink
Merge devel
Browse files Browse the repository at this point in the history
  • Loading branch information
thpralas committed Apr 10, 2024
2 parents 0ba5bd1 + 1d01162 commit b162c51
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 142 deletions.
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export(IdTaxaToDataFrame)
export(TAXONOMY_RANKS)
export(ZTransform)
export(addCluster)
export(addContaminantQC)
export(addNotContaminantQC)
export(addPerSampleDominantFeatures)
Expand All @@ -17,7 +18,6 @@ export(calculateJSD)
export(calculateNMDS)
export(calculateOverlap)
export(calculateUnifrac)
export(cluster)
export(countDominantFeatures)
export(countDominantTaxa)
export(estimateDivergence)
Expand Down Expand Up @@ -100,6 +100,7 @@ export(unsplitByRanks)
export(unsplitOn)
exportMethods("relabundance<-")
exportMethods(ZTransform)
exportMethods(addCluster)
exportMethods(addContaminantQC)
exportMethods(addHierarchyTree)
exportMethods(addNotContaminantQC)
Expand Down
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,4 @@ Changes in version 1.11.x
+ getPrevalence: bugfix, if assay contains NA values, it does not end up to NA anymore.
+ getExperimentCrossCorrelation fix: enable using of sampleMap in MAE.
+ Implemented the setTaxonomyRanks function to specify which ranks are recognized as taxonomy ranks.
+ Rename cluster to addCluster
166 changes: 87 additions & 79 deletions R/calculateDMM.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,20 @@ NULL
#' @rdname calculateDMN
#' @export
setGeneric("calculateDMN", signature = c("x"),
function(x, ...)
standardGeneric("calculateDMN"))
function(x, ...)
standardGeneric("calculateDMN"))

#' @importFrom DirichletMultinomial dmn
#' @importFrom stats runif
.calculate_DMN <- function(x, k = 1, BPPARAM = SerialParam(),
seed = runif(1, 0, .Machine$integer.max), ...){
seed = runif(1, 0, .Machine$integer.max), ...){
if(!is.numeric(k) ||
length(k) == 0 ||
anyNA(k) ||
any(k <= 0) ||
any(k != as.integer(k))){
length(k) == 0 ||
anyNA(k) ||
any(k <= 0) ||
any(k != as.integer(k))){
stop("'k' must be an integer vector with positive values only.",
call. = FALSE)
call. = FALSE)
}
#
old <- getAutoBPPARAM()
Expand All @@ -127,10 +127,10 @@ setGeneric("calculateDMN", signature = c("x"),
bpstart(BPPARAM)
on.exit(bpstop(BPPARAM), add = TRUE)
}

ans <- BiocParallel::bplapply(k, DirichletMultinomial::dmn, count = x,
seed = seed, ...,
BPPARAM = BPPARAM)
seed = seed, ...,
BPPARAM = BPPARAM)
ans
}

Expand All @@ -141,10 +141,11 @@ setMethod("calculateDMN", signature = c(x = "ANY"), .calculate_DMN)
#' @rdname calculateDMN
#' @export
setMethod("calculateDMN", signature = c(x = "SummarizedExperiment"),
function(x, assay.type = assay_name, assay_name = exprs_values, exprs_values = "counts",
transposed = FALSE, ...){
function(x, assay.type = assay_name, assay_name = exprs_values,
exprs_values = "counts", transposed = FALSE, ...){
.Deprecated(old="calculateDMN", new="cluster",
"Now calculateDMN is deprecated. Use cluster with DMMParam parameter instead.")
"Now calculateDMN is deprecated.
Use cluster with DMMParam parameter instead.")
mat <- assay(x, assay.type)
if(!transposed){
mat <- t(mat)
Expand All @@ -158,7 +159,8 @@ setMethod("calculateDMN", signature = c(x = "SummarizedExperiment"),
#' @export
runDMN <- function(x, name = "DMN", ...){
.Deprecated(old="runDMN", new="cluster",
"Now runDMN is deprecated. Use cluster with DMMParam parameter instead.")
"Now runDMN is deprecated.
Use cluster with DMMParam parameter instead.")
if(!is(x,"SummarizedExperiment")){
stop("'x' must be a SummarizedExperiment")
}
Expand All @@ -185,27 +187,29 @@ runDMN <- function(x, name = "DMN", ...){
.get_dmn_fit_FUN <- function(type){
type <- match.arg(type, c("laplace","AIC","BIC"))
fit_FUN <- switch(type,
laplace = DirichletMultinomial::laplace,
AIC = DirichletMultinomial::AIC,
BIC = DirichletMultinomial::BIC)
laplace = DirichletMultinomial::laplace,
AIC = DirichletMultinomial::AIC,
BIC = DirichletMultinomial::BIC)
fit_FUN
}

#' @rdname calculateDMN
#' @export
setGeneric("getDMN", signature = "x",
function(x, name = "DMN", ...)
standardGeneric("getDMN"))
function(x, name = "DMN", ...)
standardGeneric("getDMN"))

#' @rdname calculateDMN
#' @importFrom DirichletMultinomial laplace AIC BIC
#' @export
setMethod("getDMN", signature = c(x = "SummarizedExperiment"),
function(x, name = "DMN"){
.Deprecated(old="getDMN", new="cluster",
"Now getDMN is deprecated. Use cluster with DMMParam parameter and full parameter set as true instead.")
.get_dmn(x, name)
}
function(x, name = "DMN"){
.Deprecated(old="getDMN", new="cluster",
"Now getDMN is deprecated.
Use cluster with DMMParam parameter
and full parameter set as true instead.")
.get_dmn(x, name)
}
)


Expand All @@ -217,41 +221,45 @@ setMethod("getDMN", signature = c(x = "SummarizedExperiment"),
#' @rdname calculateDMN
#' @export
setGeneric("bestDMNFit", signature = "x",
function(x, name = "DMN", type = c("laplace","AIC","BIC"), ...)
standardGeneric("bestDMNFit"))
function(x, name = "DMN", type = c("laplace","AIC","BIC"), ...)
standardGeneric("bestDMNFit"))

#' @rdname calculateDMN
#' @importFrom DirichletMultinomial laplace AIC BIC
#' @export
setMethod("bestDMNFit", signature = c(x = "SummarizedExperiment"),
function(x, name = "DMN", type = c("laplace","AIC","BIC")){
.Deprecated(old="bestDMNFit", new="cluster",
"Now bestDMNFit is deprecated. Use cluster with DMMParam parameter and full parameter set as true instead.")
#
dmn <- getDMN(x, name)
fit_FUN <- .get_dmn_fit_FUN(type)
#
.get_best_dmn_fit(dmn, fit_FUN)
}
function(x, name = "DMN", type = c("laplace","AIC","BIC")){
.Deprecated(old="bestDMNFit", new="cluster",
"Now bestDMNFit is deprecated.
Use cluster with DMMParam parameter
and full parameter set as true instead.")
#
dmn <- getDMN(x, name)
fit_FUN <- .get_dmn_fit_FUN(type)
#
.get_best_dmn_fit(dmn, fit_FUN)
}
)

#' @rdname calculateDMN
#' @export
setGeneric("getBestDMNFit", signature = "x",
function(x, name = "DMN", type = c("laplace","AIC","BIC"), ...)
standardGeneric("getBestDMNFit"))
function(x, name = "DMN", type = c("laplace","AIC","BIC"), ...)
standardGeneric("getBestDMNFit"))

#' @rdname calculateDMN
#' @importFrom DirichletMultinomial laplace AIC BIC
#' @export
setMethod("getBestDMNFit", signature = c(x = "SummarizedExperiment"),
function(x, name = "DMN", type = c("laplace","AIC","BIC")){
.Deprecated(old="getBestDMNFit", new="cluster",
"Now getBestDMNFit is deprecated. Use cluster with DMMParam parameter and full parameter set as true instead.")
dmn <- getDMN(x, name)
fit_FUN <- .get_dmn_fit_FUN(type)
dmn[[.get_best_dmn_fit(dmn, fit_FUN)]]
}
function(x, name = "DMN", type = c("laplace","AIC","BIC")){
.Deprecated(old="getBestDMNFit", new="cluster",
"Now getBestDMNFit is deprecated.
Use cluster with DMMParam parameter
and full parameter set as true instead.")
dmn <- getDMN(x, name)
fit_FUN <- .get_dmn_fit_FUN(type)
dmn[[.get_best_dmn_fit(dmn, fit_FUN)]]
}
)

################################################################################
Expand All @@ -260,8 +268,8 @@ setMethod("getBestDMNFit", signature = c(x = "SummarizedExperiment"),
#' @rdname calculateDMN
#' @export
setGeneric("calculateDMNgroup", signature = c("x"),
function(x, ...)
standardGeneric("calculateDMNgroup"))
function(x, ...)
standardGeneric("calculateDMNgroup"))

#' @importFrom DirichletMultinomial dmngroup
#' @importFrom stats runif
Expand All @@ -285,20 +293,20 @@ setMethod("calculateDMNgroup", signature = c(x = "ANY"), .calculate_DMNgroup)
#' @rdname calculateDMN
#' @export
setMethod("calculateDMNgroup", signature = c(x = "SummarizedExperiment"),
function(x, variable,
assay.type = assay_name, assay_name = exprs_values, exprs_values = "counts",
transposed = FALSE, ...){
mat <- assay(x, assay.type)
if(!transposed){
mat <- t(mat)
}
variable <- colData(x)[,variable]
if(is.null(variable)){
stop("No data found in '",variable,"' column of colData(x).",
call. = FALSE)
}
calculateDMNgroup(x = mat, variable = variable, ...)
}
function(x, variable,
assay.type = assay_name, assay_name = exprs_values,
exprs_values = "counts", transposed = FALSE, ...){
mat <- assay(x, assay.type)
if(!transposed){
mat <- t(mat)
}
variable <- colData(x)[,variable]
if(is.null(variable)){
stop("No data found in '",variable,
"' column of colData(x).", call. = FALSE)
}
calculateDMNgroup(x = mat, variable = variable, ...)
}
)

################################################################################
Expand All @@ -307,13 +315,13 @@ setMethod("calculateDMNgroup", signature = c(x = "SummarizedExperiment"),
#' @rdname calculateDMN
#' @export
setGeneric("performDMNgroupCV", signature = c("x"),
function(x, ...)
standardGeneric("performDMNgroupCV"))
function(x, ...)
standardGeneric("performDMNgroupCV"))

#' @importFrom DirichletMultinomial cvdmngroup
#' @importFrom stats runif
.perform_DMNgroup_cv <- function(x, variable, k = 1,
seed = runif(1, 0, .Machine$integer.max), ...){
seed = runif(1, 0, .Machine$integer.max), ...){
# input check
if(!is.factor(variable) && is.character(variable)){
variable <- factor(variable, unique(variable))
Expand All @@ -323,7 +331,7 @@ setGeneric("performDMNgroupCV", signature = c("x"),
variable <- droplevels(variable)
if(is.null(names(k)) || !all(names(k) %in% levels(variable))){
stop("'k' must be named. Names must fit the levels of 'variable'.",
call. = FALSE)
call. = FALSE)
}
#
cvdmngroup(nrow(x), x, variable, k = k, seed = seed, ...)
Expand All @@ -336,18 +344,18 @@ setMethod("performDMNgroupCV", signature = c(x = "ANY"), .perform_DMNgroup_cv)
#' @rdname calculateDMN
#' @export
setMethod("performDMNgroupCV", signature = c(x = "SummarizedExperiment"),
function(x, variable,
assay.type = assay_name, assay_name = exprs_values, exprs_values = "counts",
transposed = FALSE, ...){
mat <- assay(x, assay.type)
if(!transposed){
mat <- t(mat)
}
variable <- colData(x)[,variable]
if(is.null(variable)){
stop("No data found in '",variable,"' column of colData(x).",
call. = FALSE)
}
performDMNgroupCV(x = mat, variable = variable, ...)
}
function(x, variable,
assay.type = assay_name, assay_name = exprs_values,
exprs_values = "counts", transposed = FALSE, ...){
mat <- assay(x, assay.type)
if(!transposed){
mat <- t(mat)
}
variable <- colData(x)[,variable]
if(is.null(variable)){
stop("No data found in '",variable,
"' column of colData(x).", call. = FALSE)
}
performDMNgroupCV(x = mat, variable = variable, ...)
}
)
19 changes: 10 additions & 9 deletions R/cluster.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
#' By default, clustering is done on the features.
#'
#' @return
#' \code{cluster} returns an object of the same type as the \code{x} parameter
#' \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}.
#'
#' @name cluster
#' @name addCluster
#' @export
#'
#' @author Basil Courbayre
Expand All @@ -42,10 +42,10 @@
#' tse <- GlobalPatterns
#'
#' # Cluster on rows using Kmeans
#' tse <- cluster(tse, KmeansParam(centers = 3))
#' tse <- addCluster(tse, KmeansParam(centers = 3))
#'
#' # Clustering done on the samples using Hclust
#' tse <- cluster(tse,
#' tse <- addCluster(tse,
#' MARGIN = "samples",
#' HclustParam(metric = "bray", dist.fun = vegan::vegdist))
#'
Expand All @@ -54,19 +54,20 @@
#'
NULL

#' @rdname cluster
#' @rdname addCluster
#' @export
setGeneric("cluster", signature = c("x"),
setGeneric("addCluster", signature = c("x"),
function(
x, BLUSPARAM, assay.type = assay_name,
assay_name = "counts", MARGIN = "features", full = FALSE,
name = "clusters", clust.col = "clusters", ...)
standardGeneric("cluster"))
standardGeneric("addCluster"))

#' @rdname cluster

#' @rdname addCluster
#' @export
#' @importFrom bluster clusterRows
setMethod("cluster", signature = c(x = "SummarizedExperiment"),
setMethod("addCluster", signature = c(x = "SummarizedExperiment"),
function(
x, BLUSPARAM, assay.type = assay_name,
assay_name = "counts", MARGIN = "features", full = FALSE,
Expand Down
Loading

0 comments on commit b162c51

Please sign in to comment.