Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ jobs:
http-user-agent: ${{ matrix.config.http-user-agent }}
use-public-rspm: true

- name: Install Maaslin2 from GitHub
run: |
if (!requireNamespace("remotes", quietly = TRUE)) install.packages("remotes")
remotes::install_github("biobakery/Maaslin2")
shell: Rscript {0}

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::rcmdcheck
Expand Down
12 changes: 7 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: microbiomeComputations
Title: Compute Results for Microbiome Analyses
Version: 5.1.6
Version: 5.1.7
Authors@R:
c(person(given = "Danielle",
family = "Callan",
Expand All @@ -20,14 +20,16 @@ Imports:
purrr,
Rcpp (>= 0.11.0),
S4Vectors,
SpiecEasi,
SpiecEasi (>= 1.0.7),
stringi,
vegan,
veupathUtils (>= 2.7.0)
mbioUtils (>= 0.1.0)
Depends:
R (>= 2.10)
Remotes:
VEuPathDB/veupathUtils
microbiomeDB/mbioUtils,
zdk123/SpiecEasi@v1.0.7
biocViews:
LinkingTo: Rcpp
URL: https://github.com/microbiomeDB/microbiomeComputations, https://microbiomedb.github.io/microbiomeComputations/
BugReports: https://github.com/microbiomeDB/microbiomeComputations/issues
Expand All @@ -36,7 +38,7 @@ License: Apache License (>= 2)
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.3
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
Expand Down
6 changes: 3 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ exportClasses(DifferentialAbundanceResult)
exportMethods(selfCorrelation)
import(DESeq2)
import(data.table)
import(mbioUtils)
import(methods)
import(veupathUtils)
importFrom(Maaslin2,Maaslin2)
importFrom(Rcpp,sourceCpp)
importFrom(S4Vectors,SimpleList)
importFrom(ape,pcoa)
importFrom(mbioUtils,CollectionWithMetadata)
importFrom(mbioUtils,SampleMetadata)
importFrom(methods,new)
importFrom(methods,slot)
importFrom(methods,validObject)
Expand All @@ -38,6 +40,4 @@ importFrom(stringi,stri_replace_all_fixed)
importFrom(stringi,stri_trans_totitle)
importFrom(vegan,diversity)
importFrom(vegan,vegdist)
importFrom(veupathUtils,CollectionWithMetadata)
importFrom(veupathUtils,SampleMetadata)
useDynLib(microbiomeComputations)
4 changes: 2 additions & 2 deletions R/class-AbundanceData.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ check_abundance_data <- function(object) {
#' @slot removeEmptyRecords A logical indicating whether empty (all NA/ zero) samples should be removed.
#' @name AbundanceData-class
#' @rdname AbundanceData-class
#' @importFrom veupathUtils SampleMetadata
#' @importFrom veupathUtils CollectionWithMetadata
#' @importFrom mbioUtils SampleMetadata
#' @importFrom mbioUtils CollectionWithMetadata
#' @export
AbundanceData <- setClass("AbundanceData", contains = "CollectionWithMetadata", validity = check_abundance_data)
34 changes: 17 additions & 17 deletions R/method-alphaDiv.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#' @return ComputeResult object
#' @importFrom vegan diversity
#' @importFrom stringi stri_trans_totitle
#' @import veupathUtils
#' @import mbioUtils
#' @import data.table
#' @importFrom methods new slot validObject
#' @importFrom stats as.dist as.formula median quantile var
Expand All @@ -29,16 +29,16 @@ setMethod("alphaDiv", signature("AbundanceData"), function(data, method = c('sha
naToZero <- data@imputeZero

# Initialize and check inputs
method <- veupathUtils::matchArg(method)
verbose <- veupathUtils::matchArg(verbose)
method <- mbioUtils::matchArg(method)
verbose <- mbioUtils::matchArg(verbose)

# Check that incoming df meets requirements
if (!'data.table' %in% class(df)) {
data.table::setDT(df)
}

computeMessage <- ''
veupathUtils::logWithTime(paste("Received df table with", nrow(df), "samples and", (ncol(df)-1), "taxa."), verbose)
mbioUtils::logWithTime(paste("Received df table with", nrow(df), "samples and", (ncol(df)-1), "taxa."), verbose)

# Compute alpha diversity
if (identical(method, 'shannon') | identical(method, 'simpson')){
Expand All @@ -58,41 +58,41 @@ setMethod("alphaDiv", signature("AbundanceData"), function(data, method = c('sha
result@ancestorIdColumns <- ancestorIdColumns

# Handle errors or return positive computeMessage
if (veupathUtils::is.error(alphaDivDT)) {
veupathUtils::logWithTime(paste('Alpha diversity computation FAILED with parameters, method =', method), verbose)
if (mbioUtils::is.error(alphaDivDT)) {
mbioUtils::logWithTime(paste('Alpha diversity computation FAILED with parameters, method =', method), verbose)
stop()
} else {
computeMessage <- paste('Computed', method, 'alpha diversity.')
veupathUtils::logWithTime(paste(method, 'alpha diversity computation complete.'), verbose)
mbioUtils::logWithTime(paste(method, 'alpha diversity computation complete.'), verbose)
}

# Assemble data table
dt <- data.table::as.data.table(df[, ..allIdColumns])
dt$alphaDiversity <- alphaDivDT

entity <- veupathUtils::strSplit(recordIdColumn, ".", 4, 1)
entity <- mbioUtils::strSplit(recordIdColumn, ".", 4, 1)
result@computationDetails <- computeMessage
result@parameters <- paste('method =', method)
result@recordIdColumn <- recordIdColumn
result@ancestorIdColumns <- ancestorIdColumns

computedVariableMetadata <- veupathUtils::VariableMetadata(
variableClass = veupathUtils::VariableClass(value = "computed"),
variableSpec = veupathUtils::VariableSpec(variableId = names(dt[, -..allIdColumns]), entityId = entity),
plotReference = veupathUtils::PlotReference(value = "yAxis"),
computedVariableMetadata <- mbioUtils::VariableMetadata(
variableClass = mbioUtils::VariableClass(value = "computed"),
variableSpec = mbioUtils::VariableSpec(variableId = names(dt[, -..allIdColumns]), entityId = entity),
plotReference = mbioUtils::PlotReference(value = "yAxis"),
displayName = computedVarLabel,
displayRangeMin = 0,
displayRangeMax = max(max(dt$alphaDiversity, na.rm = TRUE),1),
dataType = veupathUtils::DataType(value = "NUMBER"),
dataShape = veupathUtils::DataShape(value = "CONTINUOUS")
dataType = mbioUtils::DataType(value = "NUMBER"),
dataShape = mbioUtils::DataShape(value = "CONTINUOUS")
)

result@computedVariableMetadata <- veupathUtils::VariableMetadataList(S4Vectors::SimpleList(computedVariableMetadata))
names(dt) <- veupathUtils::stripEntityIdFromColumnHeader(names(dt))
result@computedVariableMetadata <- mbioUtils::VariableMetadataList(S4Vectors::SimpleList(computedVariableMetadata))
names(dt) <- mbioUtils::stripEntityIdFromColumnHeader(names(dt))
result@data <- dt

validObject(result)
veupathUtils::logWithTime(paste('Alpha diversity computation completed with parameters method=', method), verbose)
mbioUtils::logWithTime(paste('Alpha diversity computation completed with parameters method=', method), verbose)

return(result)
})
38 changes: 19 additions & 19 deletions R/method-betaDiv.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#' @importFrom vegan vegdist
#' @importFrom ape pcoa
#' @importFrom stringi stri_replace_all_fixed
#' @import veupathUtils
#' @import mbioUtils
#' @import data.table
#' @useDynLib microbiomeComputations
#' @rdname betaDiv-methods
Expand All @@ -31,16 +31,16 @@ setMethod("betaDiv", signature("AbundanceData"), function(data, method = c('bray
allIdColumns <- c(recordIdColumn, ancestorIdColumns)

# Initialize and check inputs
method <- veupathUtils::matchArg(method)
verbose <- veupathUtils::matchArg(verbose)
method <- mbioUtils::matchArg(method)
verbose <- mbioUtils::matchArg(verbose)

# Check that incoming df meets requirements - consider moving to a validateOTU function or similar
if (!'data.table' %in% class(df)) {
data.table::setDT(df)
}

computeMessage <- ''
veupathUtils::logWithTime(paste("Received df table with", nrow(df), "samples and", (ncol(df)-1), "taxa."), verbose)
mbioUtils::logWithTime(paste("Received df table with", nrow(df), "samples and", (ncol(df)-1), "taxa."), verbose)

# Compute beta diversity using given dissimilarity method
if (identical(method, 'bray') | identical(method, 'jaccard')) {
Expand All @@ -62,11 +62,11 @@ setMethod("betaDiv", signature("AbundanceData"), function(data, method = c('bray
result@ancestorIdColumns <- ancestorIdColumns

# Handle errors or return positive computeMessage
if (veupathUtils::is.error(dist)) {
veupathUtils::logWithTime(paste('Beta diversity computation FAILED with parameters method=', method, ', k=', k), verbose)
if (mbioUtils::is.error(dist)) {
mbioUtils::logWithTime(paste('Beta diversity computation FAILED with parameters method=', method, ', k=', k), verbose)
stop()
} else {
veupathUtils::logWithTime("Computed dissimilarity matrix.", verbose)
mbioUtils::logWithTime("Computed dissimilarity matrix.", verbose)
computeMessage <- paste(method, "dissimilarity matrix computation successful.")
}

Expand All @@ -80,7 +80,7 @@ setMethod("betaDiv", signature("AbundanceData"), function(data, method = c('bray

dt <- cbind(dt, df[, ..allIdColumns])
data.table::setcolorder(dt, allIdColumns)
veupathUtils::logWithTime("Finished ordination step.", verbose)
mbioUtils::logWithTime("Finished ordination step.", verbose)

# Extract percent variance
eigenvecs <- pcoa$values$Relative_eig
Expand All @@ -91,38 +91,38 @@ setMethod("betaDiv", signature("AbundanceData"), function(data, method = c('bray
# For now returning data and percentVar for how much is in the plot.
percentVar <- percentVar[1:k]

entity <- veupathUtils::strSplit(recordIdColumn,".", 4, 1)
entity <- mbioUtils::strSplit(recordIdColumn,".", 4, 1)
result@computationDetails <- paste(computeMessage, ', pcoaVariance =', percentVar)
result@parameters <- paste('method =', method)

axesNames <- names(dt[, -..allIdColumns])
displayNames <- paste0(axesNames, " ", sprintf(percentVar,fmt = '%#.1f'), "%")

makeVariableMetadataObject <- function(displayName) {
axisName <- veupathUtils::strSplit(displayName, " ")
axisName <- mbioUtils::strSplit(displayName, " ")
#bit hacky, see if you can think of something better
plotRef <- ifelse(grepl('Axis1', displayName, fixed=T), 'xAxis', 'yAxis')

veupathUtils::VariableMetadata(
variableClass = veupathUtils::VariableClass(value = "computed"),
variableSpec = veupathUtils::VariableSpec(variableId = axisName, entityId = entity),
plotReference = veupathUtils::PlotReference(value = plotRef),
mbioUtils::VariableMetadata(
variableClass = mbioUtils::VariableClass(value = "computed"),
variableSpec = mbioUtils::VariableSpec(variableId = axisName, entityId = entity),
plotReference = mbioUtils::PlotReference(value = plotRef),
displayName = displayName,
displayRangeMin = min(dt[[axisName]]),
displayRangeMax = max(dt[[axisName]]),
dataType = veupathUtils::DataType(value = "NUMBER"),
dataShape = veupathUtils::DataShape(value = "CONTINUOUS")
dataType = mbioUtils::DataType(value = "NUMBER"),
dataShape = mbioUtils::DataShape(value = "CONTINUOUS")
)
}

computedVariableMetadata <- veupathUtils::VariableMetadataList(lapply(displayNames, makeVariableMetadataObject))
computedVariableMetadata <- mbioUtils::VariableMetadataList(lapply(displayNames, makeVariableMetadataObject))

result@computedVariableMetadata <- computedVariableMetadata
names(dt) <- veupathUtils::stripEntityIdFromColumnHeader(names(dt))
names(dt) <- mbioUtils::stripEntityIdFromColumnHeader(names(dt))
result@data <- dt

validObject(result)
veupathUtils::logWithTime(paste('Beta diversity computation completed with parameters recordIdColumn=', recordIdColumn, ', method =', method, ', k =', k, ', verbose =', verbose), verbose)
mbioUtils::logWithTime(paste('Beta diversity computation completed with parameters recordIdColumn=', recordIdColumn, ', method =', method, ', k =', k, ', verbose =', verbose), verbose)

return(result)
})
26 changes: 13 additions & 13 deletions R/method-correlation.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
correlationGeneric <- getGeneric("correlation", package = "veupathUtils")
selfCorrelationGeneric <- getGeneric("selfCorrelation", package = "veupathUtils")
correlationGeneric <- getGeneric("correlation", package = "mbioUtils")
selfCorrelationGeneric <- getGeneric("selfCorrelation", package = "mbioUtils")

setClassUnion("missingOrNULL", c("missing", "NULL"))

#' Self Correlation
#'
#' This function returns correlation coefficients for variables in one AbundanceData object against itself. It generally serves as a
#' convenience wrapper around veupathUtils::correlation, with the exception that it additionally supports sparcc.
#' convenience wrapper around mbioUtils::correlation, with the exception that it additionally supports sparcc.
#'
#' @param data An AbundanceData object
#' @param method string defining the type of correlation to run. The currently supported values are 'spearman','pearson' and 'sparcc'
Expand All @@ -16,31 +16,31 @@ setClassUnion("missingOrNULL", c("missing", "NULL"))
#' @param varianceThreshold numeric threshold to filter features by variance across samples
#' @param stdDevThreshold numeric threshold to filter features by standard deviation across samples
#' @return ComputeResult object
#' @import veupathUtils
#' @import mbioUtils
#' @export
#' @rdname selfCorrelation-methods
#' @aliases selfCorrelation,AbundanceData-method
setMethod(selfCorrelationGeneric, signature("AbundanceData"),
function(data, method = c('spearman','pearson','sparcc'), format = c('ComputeResult', 'data.table'), verbose = c(TRUE, FALSE), proportionNonZeroThreshold = 0.5, varianceThreshold = 0, stdDevThreshold = 0) {

format <- veupathUtils::matchArg(format)
method <- veupathUtils::matchArg(method)
verbose <- veupathUtils::matchArg(verbose)
format <- mbioUtils::matchArg(format)
method <- mbioUtils::matchArg(method)
verbose <- mbioUtils::matchArg(verbose)

#prefilters applied
data <- veupathUtils::pruneFeatures(data, predicateFactory('proportionNonZero', proportionNonZeroThreshold), verbose)
data <- veupathUtils::pruneFeatures(data, predicateFactory('variance', varianceThreshold), verbose)
data <- veupathUtils::pruneFeatures(data, predicateFactory('sd', stdDevThreshold), verbose)
data <- mbioUtils::pruneFeatures(data, predicateFactory('proportionNonZero', proportionNonZeroThreshold), verbose)
data <- mbioUtils::pruneFeatures(data, predicateFactory('variance', varianceThreshold), verbose)
data <- mbioUtils::pruneFeatures(data, predicateFactory('sd', stdDevThreshold), verbose)

abundances <- getAbundances(data, FALSE, FALSE, verbose)
corrResult <- veupathUtils::correlation(abundances, NULL, method = method, format = 'data.table', verbose = verbose)
corrResult <- mbioUtils::correlation(abundances, NULL, method = method, format = 'data.table', verbose = verbose)

veupathUtils::logWithTime(paste("Received df table with", nrow(abundances), "samples and", (ncol(abundances)-1), "features with abundances."), verbose)
mbioUtils::logWithTime(paste("Received df table with", nrow(abundances), "samples and", (ncol(abundances)-1), "features with abundances."), verbose)

if (format == 'data.table') {
return(corrResult)
} else {
result <- veupathUtils::buildCorrelationComputeResult(corrResult, data, NULL, method, verbose)
result <- mbioUtils::buildCorrelationComputeResult(corrResult, data, NULL, method, verbose)
result@computationDetails <- 'selfCorrelation'
return(result)
}
Expand Down
Loading
Loading