Skip to content

Commit

Permalink
fix broken tests
Browse files Browse the repository at this point in the history
updated fold change to account for control_group param added to ttest
  • Loading branch information
grlloyd committed Jul 1, 2024
1 parent e1a7e05 commit a4fd613
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: structToolbox
Type: Package
Title: Data processing & analysis tools for Metabolomics and other omics
Version: 1.17.2
Version: 1.17.3
Authors@R: c(
person(
c("Gavin","Rhys"),
Expand Down
22 changes: 19 additions & 3 deletions R/fold_change_class.R
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ setMethod(f="model_apply",

counter=1

D$sample_meta[[M$factor_name]]=ordered(D$sample_meta[[M$factor_name]])
D$sample_meta[[M$factor_name]]=factor(D$sample_meta[[M$factor_name]])

# for all pairs of groups
for (A in 1:(length(L)-1)) {
Expand All @@ -209,12 +209,28 @@ setMethod(f="model_apply",
FG=filter_smeta(factor_name=M$factor_name,mode='include',levels=L[c(A,B)])
FG=model_apply(FG,D)
# change to ordered factor so that we make use of control group
FG$filtered$sample_meta[[M$factor_name]]=ordered(FG$filtered$sample_meta[[M$factor_name]],levels=L[c(A,B)])
#FG$filtered$sample_meta[[M$factor_name]]=ordered(FG$filtered$sample_meta[[M$factor_name]],levels=L[c(A,B)])

if (M$method=='geometric') {

control_group = NULL
if (length(M$control_group)>0) {
if (L[B] == M$control_group) {
control_group = M$control_group

}
}

# apply t-test
TT=ttest(alpha=0.05,mtc='none',factor_names=M$factor_name,paired=M$paired,paired_factor=M$sample_name,conf_level=M$conf_level)
TT=ttest(
alpha=0.05,
mtc='none',
factor_names=M$factor_name,
paired=M$paired,
paired_factor=M$sample_name,
conf_level=M$conf_level,
control_group=control_group)

TT=model_apply(TT,predicted(FG))
# log2(fold change) is the difference in estimate.mean from ttest
if (M$paired) {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-foldchange.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test_that('fold_change',{
D$data[1:25,3] = NA

# unpaired
FF = fold_change(factor_name='Species',method="geometric")
FF = fold_change(factor_name='Species',method="geometric",control_group='versicolor')
FF = model_apply(FF,D)
m=exp(mean(log(D$data[D$sample_meta$Species=='virginica',1]))) / exp(mean(log((D$data[D$sample_meta$Species=='versicolor',1]))))
expect_equal(FF$fold_change[1,1],m,tolerance=0.00001)
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-ttest.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test_that('ttest',{
ttest(factor_names='Species')
# apply
M = model_apply(M,D)
expect_equal(M[2]$t_statistic[1,1],-15.386,tolerance=0.0005)
expect_equal(M[2]$t_statistic[1,1],15.386,tolerance=0.0005)
})


Expand Down Expand Up @@ -68,11 +68,11 @@ test_that('paired-ttest',{

# 50 pairs - 1 without pair - 1 with an NA - 1 for ttest
expect_equal(M$dof[1],47)
expect_equal(M$t_statistic[1,1],-5.195,tolerance=0.0005)
expect_equal(M$t_statistic[1,1],5.195,tolerance=0.0005)

# 50 pairs - 1 without pair - 1 for ttest
expect_equal(M$dof[2],48)
expect_equal(M$t_statistic[2,1],-3.014,tolerance=0.0005)
expect_equal(M$t_statistic[2,1],3.014,tolerance=0.0005)

# check order dependence
expect_equal(M$t_statistic[1,1],M2$t_statistic[1,1],tolerance=0.00005)
Expand Down

0 comments on commit a4fd613

Please sign in to comment.