From a4fd61388f85789acd0382d4a4770fd417fdf1ca Mon Sep 17 00:00:00 2001 From: Gavin Rhys Lloyd Date: Mon, 1 Jul 2024 12:07:17 +0100 Subject: [PATCH] fix broken tests updated fold change to account for control_group param added to ttest --- DESCRIPTION | 2 +- R/fold_change_class.R | 22 +++++++++++++++++++--- tests/testthat/test-foldchange.R | 2 +- tests/testthat/test-ttest.R | 6 +++--- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 946c4ca..3a939cc 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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"), diff --git a/R/fold_change_class.R b/R/fold_change_class.R index ec345d9..77fb790 100644 --- a/R/fold_change_class.R +++ b/R/fold_change_class.R @@ -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)) { @@ -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) { diff --git a/tests/testthat/test-foldchange.R b/tests/testthat/test-foldchange.R index 05e14fb..667addb 100644 --- a/tests/testthat/test-foldchange.R +++ b/tests/testthat/test-foldchange.R @@ -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) diff --git a/tests/testthat/test-ttest.R b/tests/testthat/test-ttest.R index ff3ca61..6cacf8a 100644 --- a/tests/testthat/test-ttest.R +++ b/tests/testthat/test-ttest.R @@ -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) }) @@ -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)