Skip to content

Commit

Permalink
Merge pull request #22 from microbiomeDB/internal-diff-abund
Browse files Browse the repository at this point in the history
change name of differentialAbundance method
  • Loading branch information
d-callan authored Apr 30, 2024
2 parents bc68ee8 + e8db722 commit 71f85f7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 39 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ export(Comparator)
export(DifferentialAbundanceResult)
export(alphaDiv)
export(betaDiv)
export(differentialAbundance)
export(getAbundances)
export(getGroupLabels)
export(internalDiffAbund)
export(rankedAbundance)
export(removeIncompleteSamples)
exportClasses(AbsoluteAbundanceData)
Expand Down
12 changes: 6 additions & 6 deletions R/method-differentialAbundance.R
Original file line number Diff line number Diff line change
Expand Up @@ -246,16 +246,16 @@ setMethod("maaslin", signature("AbundanceData", "Comparator"), function(data, co
#' @importFrom purrr discard
#' @useDynLib microbiomeComputations
#' @export
#' @rdname differentialAbundance-methods
setGeneric("differentialAbundance",
function(data, comparator, method = c('DESeq2', 'Maaslin2'), pValueFloor = P_VALUE_FLOOR, verbose = c(TRUE, FALSE)) standardGeneric("differentialAbundance"),
#' @rdname internalDiffAbund-methods
setGeneric("internalDiffAbund",
function(data, comparator, method = c('DESeq2', 'Maaslin2'), pValueFloor = P_VALUE_FLOOR, verbose = c(TRUE, FALSE)) standardGeneric("internalDiffAbund"),
signature = c("data", "comparator")
)

# this is consistent regardless of rel vs abs abund. the statistical methods will differ depending on that.
#' @rdname differentialAbundance-methods
#' @aliases differentialAbundance,AbundanceData,Comparator-method
setMethod("differentialAbundance", signature("AbundanceData", "Comparator"), function(data, comparator, method = c('DESeq2', 'Maaslin2'), pValueFloor = P_VALUE_FLOOR, verbose = c(TRUE, FALSE)) {
#' @rdname internalDiffAbund-methods
#' @aliases internalDiffAbund,AbundanceData,Comparator-method
setMethod("internalDiffAbund", signature("AbundanceData", "Comparator"), function(data, comparator, method = c('DESeq2', 'Maaslin2'), pValueFloor = P_VALUE_FLOOR, verbose = c(TRUE, FALSE)) {
data <- cleanComparatorVariable(data, comparator, verbose)
recordIdColumn <- data@recordIdColumn
ancestorIdColumns <- data@ancestorIdColumns
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 27 additions & 27 deletions tests/testthat/test-differentialAbundance.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Tests for differential abundance methods

test_that('differentialAbundance returns a correctly formatted data.table', {
test_that('internalDiffAbund returns a correctly formatted data.table', {

testOTU_path <- testthat::test_path('testdata','testOTU.rda')
load(testOTU_path)
Expand Down Expand Up @@ -56,7 +56,7 @@ test_that('differentialAbundance returns a correctly formatted data.table', {
)
)

result <- differentialAbundance(testData, comparator=comparatorVariable, method='DESeq2', verbose=F)
result <- internalDiffAbund(testData, comparator=comparatorVariable, method='DESeq2', verbose=F)
expect_equal(length(result@droppedColumns), 182)
dt <- result@data
expect_equal(names(dt), c('SampleID'))
Expand Down Expand Up @@ -93,7 +93,7 @@ test_that('differentialAbundance returns a correctly formatted data.table', {
)
)
)
result <- differentialAbundance(testData, comparator=comparatorVariable, method='DESeq2', verbose=F)
result <- internalDiffAbund(testData, comparator=comparatorVariable, method='DESeq2', verbose=F)
expect_equal(length(result@droppedColumns), 407)
dt <- result@data
expect_equal(names(dt), c('SampleID'))
Expand Down Expand Up @@ -128,7 +128,7 @@ test_that('differentialAbundance returns a correctly formatted data.table', {
groupB = groupBBins
)

result <- differentialAbundance(testData, comparator=comparatorVariable, method='DESeq2', verbose=F)
result <- internalDiffAbund(testData, comparator=comparatorVariable, method='DESeq2', verbose=F)
dt <- result@data
expect_equal(names(dt), c('SampleID'))
expect_s3_class(dt, 'data.table')
Expand Down Expand Up @@ -159,7 +159,7 @@ test_that('differentialAbundance returns a correctly formatted data.table', {
groupB = groupBBins
)

result <- differentialAbundance(testData, comparator=comparatorVariable, method='DESeq2', verbose=F)
result <- internalDiffAbund(testData, comparator=comparatorVariable, method='DESeq2', verbose=F)
dt <- result@data
expect_equal(names(dt), c('SampleID'))
expect_s3_class(dt, 'data.table')
Expand All @@ -172,7 +172,7 @@ test_that('differentialAbundance returns a correctly formatted data.table', {

})

test_that("differentialAbundance can handle messy inputs", {
test_that("internalDiffAbund can handle messy inputs", {

testOTU_path <- testthat::test_path('testdata','testOTU.rda')
load(testOTU_path)
Expand Down Expand Up @@ -233,7 +233,7 @@ test_that("differentialAbundance can handle messy inputs", {
)
)

result <- differentialAbundance(testDataMessy, comparator=comparatorVariable, method='DESeq2', verbose=F)
result <- internalDiffAbund(testDataMessy, comparator=comparatorVariable, method='DESeq2', verbose=F)
dt <- result@data
expect_equal(names(dt), c('SampleID'))
expect_s3_class(dt, 'data.table')
Expand Down Expand Up @@ -267,7 +267,7 @@ test_that("differentialAbundance can handle messy inputs", {
groupB = groupBBins
)

result <- differentialAbundance(testDataMessy, comparator=comparatorVariable, method='DESeq2', verbose=F)
result <- internalDiffAbund(testDataMessy, comparator=comparatorVariable, method='DESeq2', verbose=F)
dt <- result@data
expect_equal(names(dt), c('SampleID'))
expect_s3_class(dt, 'data.table')
Expand Down Expand Up @@ -298,7 +298,7 @@ test_that("differentialAbundance can handle messy inputs", {
groupA = groupABins,
groupB = groupBBins
)
result <- differentialAbundance(testDataMessy, comparator=comparatorVariable, method='DESeq2', verbose=T)
result <- internalDiffAbund(testDataMessy, comparator=comparatorVariable, method='DESeq2', verbose=T)
dt <- result@data
expect_equal(names(dt), c('SampleID'))
expect_s3_class(dt, 'data.table')
Expand All @@ -313,7 +313,7 @@ test_that("differentialAbundance can handle messy inputs", {
})


test_that("differentialAbundance returns a ComputeResult with the correct slots" , {
test_that("internalDiffAbund returns a ComputeResult with the correct slots" , {

testOTU_path <- testthat::test_path('testdata','testOTU.rda')
load(testOTU_path)
Expand Down Expand Up @@ -365,13 +365,13 @@ test_that("differentialAbundance returns a ComputeResult with the correct slots"
)
)

result <- differentialAbundance(testData, comparator=comparatorVariable, method='DESeq2', verbose=F)
result <- internalDiffAbund(testData, comparator=comparatorVariable, method='DESeq2', verbose=F)
expect_equal(result@parameters, 'recordIdColumn = entity.SampleID, comparatorColName = entity.binA, method = DESeq2, groupA =binA_a, groupB = binA_b')
expect_equal(result@recordIdColumn, 'entity.SampleID')
expect_equal(class(result@droppedColumns), 'character')
})

test_that("differentialAbundance fails with improper inputs", {
test_that("internalDiffAbund fails with improper inputs", {

testOTU_path <- testthat::test_path('testdata','testOTU.rda')
load(testOTU_path)
Expand Down Expand Up @@ -421,11 +421,11 @@ test_that("differentialAbundance fails with improper inputs", {
groupB = groupBBins
)

expect_error(differentialAbundance(testData, comparator=comparisonVariable, method='DESeq2', verbose=F))
expect_error(internalDiffAbund(testData, comparator=comparisonVariable, method='DESeq2', verbose=F))

})

test_that("differentialAbundance catches DESeq2 errors", {
test_that("internalDiffAbund catches DESeq2 errors", {

testOTU_path <- testthat::test_path('testdata','testOTU.rda')
load(testOTU_path)
Expand Down Expand Up @@ -474,12 +474,12 @@ test_that("differentialAbundance catches DESeq2 errors", {
sampleMetadata = sampleMetadata,
recordIdColumn = 'entity.SampleID')

expect_error(differentialAbundance(testData, comparator=comparisonVariable, method='DESeq2', verbose=T))
expect_error(internalDiffAbund(testData, comparator=comparisonVariable, method='DESeq2', verbose=T))


})

test_that("differentialAbundance method Maaslin2 does stuff",{
test_that("internalDiffAbund method Maaslin2 does stuff",{

testOTU_path <- testthat::test_path('testdata','testOTU.rda')
load(testOTU_path)
Expand Down Expand Up @@ -538,15 +538,15 @@ test_that("differentialAbundance method Maaslin2 does stuff",{
)
)

result <- differentialAbundance(testData,
result <- internalDiffAbund(testData,
comparator = comparatorVariable,
method='Maaslin2',
verbose=F)
dt <- result@data
stats <- result@statistics@statistics


resultCounts <- differentialAbundance(testCountsData,
resultCounts <- internalDiffAbund(testCountsData,
comparator = comparatorVariable,
method='Maaslin2',
verbose=F)
Expand All @@ -560,7 +560,7 @@ test_that("differentialAbundance method Maaslin2 does stuff",{
expect_equal(stats, statsCounts)
})

test_that("toJSON for DifferentialAbundanceResult works",{
test_that("toJSON for internalDiffAbundResult works",{

testOTU_path <- testthat::test_path('testdata','testOTU.rda')
load(testOTU_path)
Expand Down Expand Up @@ -609,7 +609,7 @@ test_that("toJSON for DifferentialAbundanceResult works",{
)
)

result <- differentialAbundance(testData,
result <- internalDiffAbund(testData,
comparator = comparatorVariable,
method='Maaslin2',
verbose=F)
Expand Down Expand Up @@ -678,29 +678,29 @@ test_that("The smallest pvalue we can get is our p value floor", {
)

# Try with different p value floors
result <- differentialAbundance(testData, comparator=comparatorVariable, method='DESeq2', pValueFloor = 0, verbose=F)
result <- internalDiffAbund(testData, comparator=comparatorVariable, method='DESeq2', pValueFloor = 0, verbose=F)
expect_equal(min(result@statistics@statistics$pValue), 0)
expect_equal(min(result@statistics@statistics$adjustedPValue, na.rm=T), 0) # Confirmed NAs are for pvalue=1

result <- differentialAbundance(testData, comparator=comparatorVariable, method='DESeq2', pValueFloor = P_VALUE_FLOOR, verbose=F)
result <- internalDiffAbund(testData, comparator=comparatorVariable, method='DESeq2', pValueFloor = P_VALUE_FLOOR, verbose=F)
expect_equal(min(result@statistics@statistics$pValue), P_VALUE_FLOOR)
expect_equal(min(result@statistics@statistics$adjustedPValue, na.rm=T), result@statistics@adjustedPValueFloor) # Confirmed NAs are for pvalue=1



# Repeat with Maaslin2
result <- differentialAbundance(testData, comparator=comparatorVariable, method='Maaslin2', pValueFloor = 0, verbose=F)
result <- internalDiffAbund(testData, comparator=comparatorVariable, method='Maaslin2', pValueFloor = 0, verbose=F)
expect_equal(min(result@statistics@statistics$pValue), 0)
expect_equal(min(result@statistics@statistics$adjustedPValue), 0)

result <- differentialAbundance(testData, comparator=comparatorVariable, method='Maaslin2', pValueFloor = P_VALUE_FLOOR, verbose=F)
result <- internalDiffAbund(testData, comparator=comparatorVariable, method='Maaslin2', pValueFloor = P_VALUE_FLOOR, verbose=F)
expect_equal(min(result@statistics@statistics$pValue), P_VALUE_FLOOR)
expect_equal(min(result@statistics@statistics$adjustedPValue), result@statistics@adjustedPValueFloor)


})

test_that("differentialAbundance fails if comparator has one value", {
test_that("internalDiffAbund fails if comparator has one value", {

testOTU_path <- testthat::test_path('testdata','testOTU.rda')
load(testOTU_path)
Expand Down Expand Up @@ -734,6 +734,6 @@ test_that("differentialAbundance fails if comparator has one value", {
groupB = veupathUtils::BinList(S4Vectors::SimpleList(c(veupathUtils::Bin(binLabel="binB"))))
)

expect_error(differentialAbundance(testData, comparator=comparatorVariable, method='DESeq2', verbose=F))
expect_error(differentialAbundance(testData, comparator=comparatorVariable, method='Maaslin2', verbose=F))
expect_error(internalDiffAbund(testData, comparator=comparatorVariable, method='DESeq2', verbose=F))
expect_error(internalDiffAbund(testData, comparator=comparatorVariable, method='Maaslin2', verbose=F))
})

0 comments on commit 71f85f7

Please sign in to comment.