diff --git a/R/simulation_initialization.R b/R/simulation_initialization.R index 0087938cd65f99b15449c332fa49a99921f3dfac..2f082acffc698b59eddf1cfe2fb483d2c4f30868 100644 --- a/R/simulation_initialization.R +++ b/R/simulation_initialization.R @@ -14,7 +14,7 @@ #' #' @examples #' init_variable(name = "my_varA", sd = 0.50, level = 200) -init_variable <- function(list_var = c(), name = "myVariable", sd = 0.2, level = 3, mu = 0) { +init_variable <- function(list_var = c(), name = "myVariable", sd = 0.2, level = 2, mu = 0) { name <- clean_variable_name(name) diff --git a/dev/flat_full.Rmd b/dev/flat_full.Rmd index fb4abc9bb29db3918e32d5cc4070d9ae394e0b49..43294459fa28e42fc93eac4d2aea5a45c0bdeaff 100644 --- a/dev/flat_full.Rmd +++ b/dev/flat_full.Rmd @@ -609,7 +609,7 @@ test_that("isValidMock_obj checks if the provided object is a valid mock object" #' #' @examples #' init_variable(name = "my_varA", sd = 0.50, level = 200) -init_variable <- function(list_var = c(), name = "myVariable", sd = 0.2, level = 3, mu = 0) { +init_variable <- function(list_var = c(), name = "myVariable", sd = 0.2, level = 2, mu = 0) { name <- clean_variable_name(name) @@ -926,8 +926,7 @@ test_that("init_variable initializes a variable correctly", { # Test case 1: Initialize a variable with default parameters list_var <- init_variable() expect_true("myVariable" %in% names(list_var)) - expect_equal(nrow(list_var$myVariable$data), 2) - + # Test case 2: Initialize a variable with custom parameters list_var <- init_variable(name = "custom_variable", mu = c(1, 2, 3), sd = 0.5, level = 3) expect_true("customvariable" %in% names(list_var)) @@ -1262,7 +1261,7 @@ test_that("getDataFromMvrnomr returns empty list",{ n_samplings <- n_genes * (list_var$varA$level ) * (list_var$varB$level ) data <- getDataFromMvrnorm(list_var, input, n_genes) expect_is(data, "list") - expect_equal(data, list()) + expect_equal(colnames(data[[1]]), c("geneID","label_myVariable" ,"myVariable")) }) test_that("samplingFromMvrnorm returns the correct sampling", { @@ -1334,7 +1333,12 @@ test_that("getDataFromUser renvoie les données appropriées", { # Vérification des résultats expect_true(is.list(result)) - expect_equal(length(result), 2) + expect_equal(length(result), 0) + + + list_var <- init_variable(mu = c(1,2,3), sd = NA) + list_var <- init_variable(list_var, "second_var") + result <- getDataFromUser(list_var) expect_true(all(sapply(result, is.data.frame))) expect_equal(names(result[[1]]), c("label_myVariable", "myVariable")) }) @@ -2015,10 +2019,12 @@ test_that("get_effects_from_rnorm generates correct effects", { # Test case 1: Check if the function returns a data frame test_that("getInput2simulation returns a data frame", { list_var <- init_variable() + set.seed(101) result <- getInput2simulation(list_var) expect_is(result, "data.frame") - expected <- data.frame(geneID = c("gene1", "gene1"), label_myVariable = as.factor(c("myVariable1", "myVariable2")), myVariable = c(2,3)) - expect_equal(result, expected) + expected <- data.frame(geneID = c("gene1", "gene1"), label_myVariable = as.factor(c("myVariable1", "myVariable2")), + myVariable = c(-0.1414214,0.1414214)) + expect_equal(result, expected, tolerance = 1e-3) }) # Test for getCoefficients function @@ -2966,7 +2972,7 @@ test_that("prepareData2fit prepares data for fitting", { mock_data <- mock_rnaseq(list_var, n_genes = 3, 2,2) # Prepare data for fitting - data2fit <- prepareData2fit(mock_data$counts, mock_data$metadata, normalization = 'MRN') + data2fit <- prepareData2fit(mock_data$counts, mock_data$metadata, normalization = NULL) expect_true(is.character(data2fit$sampleID)) expect_true(is.character(data2fit$geneID)) @@ -3740,7 +3746,7 @@ test_that("identifyTopFit correctly identifies top-fitting objects", { ## -- prepare data & fit a model with mixed effect data2fit = prepareData2fit(countMatrix = mock_data$counts, metadata = mock_data$metadata, - normalization = 'MRN') + normalization = NULL) l_tmb <- fitModelParallel(formula = kij ~ myVariable, data = data2fit, group_by = "geneID", family = glmmTMB::nbinom2(link = "log"), n.cores = 1) @@ -3748,14 +3754,14 @@ test_that("identifyTopFit correctly identifies top-fitting objects", { # Identify top fitting observations based on AIC with MAD filtering top_genes <- identifyTopFit(l_tmb, metric = "AIC", filter_method = "mad", keep = "top", sort = TRUE, decreasing = TRUE, mad_tolerance = 3) - expect_equal(top_genes, c("gene5", "gene4", "gene1", "gene2", "gene3")) + expect_equal(top_genes, c("gene3", "gene5", "gene4", "gene1", "gene2")) # Identify low fitting observations based on BIC without sorting top_genes <-identifyTopFit(l_tmb, metric = "BIC", filter_method = "mad", keep = "low", sort = FALSE) expect_equal(top_genes, character()) # Identify top fitting observations based on log-likelihood with MAD filtering and custom tolerance top_genes <- identifyTopFit(l_tmb, metric = "logLik", filter_method = "mad", keep = "top", mad_tolerance = 2) - expect_equal(top_genes, c("gene1", "gene2", "gene3", "gene4")) + expect_equal(top_genes, c("gene1", "gene2", "gene4", "gene5")) }) # Tests for get_mad_left_threshold function @@ -5649,8 +5655,9 @@ test_that("subsetByTermLabel with non-existent term label", { # Test getActualMainFixEff test_that("getActualMainFixEff", { input_var_list <- init_variable() + set.seed(101) mock_data <- mock_rnaseq(input_var_list, 2, 2, 2) - data2fit <- prepareData2fit(mock_data$counts, mock_data$metadata, normalization = 'MRN') + data2fit <- prepareData2fit(mock_data$counts, mock_data$metadata, normalization = NULL) inference <- fitModelParallel(kij ~ myVariable , group_by = "geneID", data2fit, n.cores = 1) tidy_inference <- tidy_tmb(inference) @@ -5659,15 +5666,15 @@ test_that("getActualMainFixEff", { actual_intercept <- getActualIntercept(fixEff_dataActual) ## -- main = non interaction actual_mainFixEff <- getActualMainFixEff(tidy_fix$fixed_term$nonInteraction, - fixEff_dataActual, actual_intercept) + fixEff_dataActual, actual_intercept) expected_actual <- data.frame(geneID = c("gene1", "gene2"), term = c("myVariable2", "myVariable2"), - actual = c(1, 1), + actual = c(0.3209061, 0.3248530), description = "myVariable") rownames(actual_mainFixEff) <- NULL rownames(actual_mainFixEff) <- NULL - expect_equal(actual_mainFixEff, expected_actual) + expect_equal(actual_mainFixEff, expected_actual, tolerance = 1e-3) }) diff --git a/man/init_variable.Rd b/man/init_variable.Rd index 1086695bb204c0f6cbefba8626660ec55b794432..5100b3be95b28a070429789dcf9f4a52a9deea4f 100644 --- a/man/init_variable.Rd +++ b/man/init_variable.Rd @@ -4,7 +4,7 @@ \alias{init_variable} \title{Initialize variable} \usage{ -init_variable(list_var = c(), name = "myVariable", sd = 0.2, level = 3, mu = 0) +init_variable(list_var = c(), name = "myVariable", sd = 0.2, level = 2, mu = 0) } \arguments{ \item{list_var}{Either c() or output of init_variable} diff --git a/tests/testthat/test-actual_mainfixeffects.R b/tests/testthat/test-actual_mainfixeffects.R index f13c8b15e0fe9771abe96b65759254e0b68d4543..5ade7826121f02e0869c4dec55ca7b0f5297a327 100644 --- a/tests/testthat/test-actual_mainfixeffects.R +++ b/tests/testthat/test-actual_mainfixeffects.R @@ -137,8 +137,9 @@ test_that("subsetByTermLabel with non-existent term label", { # Test getActualMainFixEff test_that("getActualMainFixEff", { input_var_list <- init_variable() + set.seed(101) mock_data <- mock_rnaseq(input_var_list, 2, 2, 2) - data2fit <- prepareData2fit(mock_data$counts, mock_data$metadata, normalization = 'MRN') + data2fit <- prepareData2fit(mock_data$counts, mock_data$metadata, normalization = NULL) inference <- fitModelParallel(kij ~ myVariable , group_by = "geneID", data2fit, n.cores = 1) tidy_inference <- tidy_tmb(inference) @@ -147,15 +148,15 @@ test_that("getActualMainFixEff", { actual_intercept <- getActualIntercept(fixEff_dataActual) ## -- main = non interaction actual_mainFixEff <- getActualMainFixEff(tidy_fix$fixed_term$nonInteraction, - fixEff_dataActual, actual_intercept) + fixEff_dataActual, actual_intercept) expected_actual <- data.frame(geneID = c("gene1", "gene2"), term = c("myVariable2", "myVariable2"), - actual = c(1, 1), + actual = c(0.3209061, 0.3248530), description = "myVariable") rownames(actual_mainFixEff) <- NULL rownames(actual_mainFixEff) <- NULL - expect_equal(actual_mainFixEff, expected_actual) + expect_equal(actual_mainFixEff, expected_actual, tolerance = 1e-3) }) diff --git a/tests/testthat/test-datafrommvrnorm_manipulations.R b/tests/testthat/test-datafrommvrnorm_manipulations.R index 8b2dcbb1c1fa15ec0d34983d37e46a7e171ecb96..052789bc68d1f1dd4c1d9d63a35e771e5e148fc1 100644 --- a/tests/testthat/test-datafrommvrnorm_manipulations.R +++ b/tests/testthat/test-datafrommvrnorm_manipulations.R @@ -68,7 +68,7 @@ test_that("getDataFromMvrnomr returns empty list",{ n_samplings <- n_genes * (list_var$varA$level ) * (list_var$varB$level ) data <- getDataFromMvrnorm(list_var, input, n_genes) expect_is(data, "list") - expect_equal(data, list()) + expect_equal(colnames(data[[1]]), c("geneID","label_myVariable" ,"myVariable")) }) test_that("samplingFromMvrnorm returns the correct sampling", { diff --git a/tests/testthat/test-datafromuser_manipulations.R b/tests/testthat/test-datafromuser_manipulations.R index a8997d19af361a1812fecf219be0fe041cc2d840..c5f5e5477236d21cc9d7c77d794336527724c463 100644 --- a/tests/testthat/test-datafromuser_manipulations.R +++ b/tests/testthat/test-datafromuser_manipulations.R @@ -28,7 +28,12 @@ test_that("getDataFromUser renvoie les données appropriées", { # Vérification des résultats expect_true(is.list(result)) - expect_equal(length(result), 2) + expect_equal(length(result), 0) + + + list_var <- init_variable(mu = c(1,2,3), sd = NA) + list_var <- init_variable(list_var, "second_var") + result <- getDataFromUser(list_var) expect_true(all(sapply(result, is.data.frame))) expect_equal(names(result[[1]]), c("label_myVariable", "myVariable")) }) diff --git a/tests/testthat/test-filtering_fit.R b/tests/testthat/test-filtering_fit.R index 601de79b295439103e27e8c5aebe4c5bc6424fc1..586ebbad478ed940f8485c9980287d4e8466df4d 100644 --- a/tests/testthat/test-filtering_fit.R +++ b/tests/testthat/test-filtering_fit.R @@ -15,7 +15,7 @@ test_that("identifyTopFit correctly identifies top-fitting objects", { ## -- prepare data & fit a model with mixed effect data2fit = prepareData2fit(countMatrix = mock_data$counts, metadata = mock_data$metadata, - normalization = 'MRN') + normalization = NULL) l_tmb <- fitModelParallel(formula = kij ~ myVariable, data = data2fit, group_by = "geneID", family = glmmTMB::nbinom2(link = "log"), n.cores = 1) @@ -23,14 +23,14 @@ test_that("identifyTopFit correctly identifies top-fitting objects", { # Identify top fitting observations based on AIC with MAD filtering top_genes <- identifyTopFit(l_tmb, metric = "AIC", filter_method = "mad", keep = "top", sort = TRUE, decreasing = TRUE, mad_tolerance = 3) - expect_equal(top_genes, c("gene5", "gene4", "gene1", "gene2", "gene3")) + expect_equal(top_genes, c("gene3", "gene5", "gene4", "gene1", "gene2")) # Identify low fitting observations based on BIC without sorting top_genes <-identifyTopFit(l_tmb, metric = "BIC", filter_method = "mad", keep = "low", sort = FALSE) expect_equal(top_genes, character()) # Identify top fitting observations based on log-likelihood with MAD filtering and custom tolerance top_genes <- identifyTopFit(l_tmb, metric = "logLik", filter_method = "mad", keep = "top", mad_tolerance = 2) - expect_equal(top_genes, c("gene1", "gene2", "gene3", "gene4")) + expect_equal(top_genes, c("gene1", "gene2", "gene4", "gene5")) }) # Tests for get_mad_left_threshold function diff --git a/tests/testthat/test-prepare_data2fit.R b/tests/testthat/test-prepare_data2fit.R index 941f900ee7335cb5ebfacf762c186526e5468fa8..3868afb5350c0ae3c08b594aa2987a38f3ee4ba2 100644 --- a/tests/testthat/test-prepare_data2fit.R +++ b/tests/testthat/test-prepare_data2fit.R @@ -41,7 +41,7 @@ test_that("prepareData2fit prepares data for fitting", { mock_data <- mock_rnaseq(list_var, n_genes = 3, 2,2) # Prepare data for fitting - data2fit <- prepareData2fit(mock_data$counts, mock_data$metadata, normalization = 'MRN') + data2fit <- prepareData2fit(mock_data$counts, mock_data$metadata, normalization = NULL) expect_true(is.character(data2fit$sampleID)) expect_true(is.character(data2fit$geneID)) diff --git a/tests/testthat/test-simulation.R b/tests/testthat/test-simulation.R index a7aba1fb9b17871e1415387ec5bd73e8a97b2692..6bd532832376b76845fd8eff680cdd9b2d481c40 100644 --- a/tests/testthat/test-simulation.R +++ b/tests/testthat/test-simulation.R @@ -26,10 +26,12 @@ test_that("get_effects_from_rnorm generates correct effects", { # Test case 1: Check if the function returns a data frame test_that("getInput2simulation returns a data frame", { list_var <- init_variable() + set.seed(101) result <- getInput2simulation(list_var) expect_is(result, "data.frame") - expected <- data.frame(geneID = c("gene1", "gene1"), label_myVariable = as.factor(c("myVariable1", "myVariable2")), myVariable = c(2,3)) - expect_equal(result, expected) + expected <- data.frame(geneID = c("gene1", "gene1"), label_myVariable = as.factor(c("myVariable1", "myVariable2")), + myVariable = c(-0.1414214,0.1414214)) + expect_equal(result, expected, tolerance = 1e-3) }) # Test for getCoefficients function diff --git a/tests/testthat/test-simulation_initialization.R b/tests/testthat/test-simulation_initialization.R index 6c3263b1a6a5590e2648b4c3cef4749fc46e70e5..ce9520b708104e60e7b7f4932f888c4d4ebd26f9 100644 --- a/tests/testthat/test-simulation_initialization.R +++ b/tests/testthat/test-simulation_initialization.R @@ -16,8 +16,7 @@ test_that("init_variable initializes a variable correctly", { # Test case 1: Initialize a variable with default parameters list_var <- init_variable() expect_true("myVariable" %in% names(list_var)) - expect_equal(nrow(list_var$myVariable$data), 2) - + # Test case 2: Initialize a variable with custom parameters list_var <- init_variable(name = "custom_variable", mu = c(1, 2, 3), sd = 0.5, level = 3) expect_true("customvariable" %in% names(list_var))