tests/testthat/test-dna.R

test_that("dnapath method works on list input", {
  n <- c(10, 15)
  p <- 100
  expr_pair <- list(x1 = matrix(rnorm(p * n[1], 10), nrow = n[1], ncol = p),
                    x2 = matrix(rnorm(p * n[2], 10), nrow = n[2], ncol = p))
  colnames(expr_pair[[1]]) <- 1:p
  colnames(expr_pair[[2]]) <- 1:p
  groups <- NULL
  
  # Delete some of the genes in each group
  expr_pair[[1]] <- expr_pair[[1]][, -c(1, 5, 9)]
  expr_pair[[1]] <- expr_pair[[1]][, -c(9, 10, 23, 90:p)]
  
  network_inference <- function(x, ...) suppressWarnings(run_corr(x, ...))
  n_perm = 10
  lp_set = 2
  
  pathway_list <- as.character(1:20)
  # Check that seed can be set without error and returns equivalent results.
  results1 <- dnapath(expr_pair, pathway_list, groups,
                 network_inference, n_perm, lp_set, seed = 1)
  results2 <- dnapath(expr_pair, pathway_list, groups,
                 network_inference, n_perm, lp_set, seed = 1)
  expect_equal(unclass(results1), unclass(results2))
  
  # Check that methods work on 'dnapath' object
  results <- dnapath(expr_pair, pathway_list, groups,
                 network_inference, n_perm, lp_set)
  expect_is(results, "dnapath")
  expect_is(summary(results), "tbl")
  expect_is(summarize_genes(results), "tbl")
  expect_is(summarize_pathways(results), "tbl")
  expect_invisible(plot(results))
  
  # Check that methods work on 'dnapath_list' object
  # Create pathway list.
  pathway_list <- list(as.character(5:9), 
                       as.character(c(1, 5, 8, 13, 19)), 
                       as.character(c(2, 3, 4, 6:20)))
  results <- dnapath(expr_pair, pathway_list, groups,
                 network_inference, n_perm, lp_set)
  expect_true(results[[1]]$mean_expr[1, 4] == 0)
  expect_is(results, "dnapath_list")
  expect_is(summary(results), "tbl")
  expect_is(summarize_genes(results), "tbl")
  expect_is(summarize_pathways(results), "tbl")
  expect_invisible(plot(results[[1]]))
  
  # Check that gene names are unchanged after subsetting.
  genes_before_subset <- results[[1]]$genes[results[[1]]$pathway_genes]
  results <- results[1:2]
  genes_after_subset <- results[[1]]$genes[results[[1]]$pathway_genes]
  expect_equal(genes_before_subset, genes_after_subset)
  expect_equal(results[1][[1]]$genes, c("6", "7", "8", "5", "9"))
  expect_equal(sort(results[2:3][[1]]$genes[results[2:3][[1]]$pathway_genes]), 
               sort(unique(as.character(pathway_list[[2]]))))
  
  # Check that methods work on 'dnapath_list' object
  # Now use a named pathway_list.
  pathway_list <- list("a" = 16:49, 
                       "b" = c(1, 5, 5, 5, 5, 5, 8, 13, 19), 
                       "c" = c(2, 3, 4, 6:20))
  results <- dnapath(expr_pair, pathway_list, groups,
                 network_inference, n_perm, lp_set)
  expect_is(results, "dnapath_list")
  expect_equal(names(results), names(pathway_list))
  expect_equal(unname(sapply(results, function(x) x$pathway)), 
               names(pathway_list))
  
  expect_is(summary(results), "tbl")
  expect_is(summarize_genes(results), "tbl")
  expect_is(summarize_pathways(results), "tbl")
  expect_invisible(plot(results[[1]]))
  
  # Check that gene names are unchanged after subsetting.
  expect_equal(sort(results[2:3][[1]]$genes[results[2:3][[1]]$pathway_genes]), 
               sort(unique(as.character(pathway_list[[2]]))))
  
  genes_before_subset <- results[[1]]$genes[results[[1]]$pathway_genes]
  results <- results[1:2]
  genes_after_subset <- results[[1]]$genes[results[[1]]$pathway_genes]
  expect_equal(genes_before_subset, genes_after_subset)
})


test_that("dnapath method works on data.frame input", {
  n <- c(10, 15)
  p <- 100
  expr_pair <- matrix(rnorm(p * (n[1] + n[2]), 10), ncol = p)
  colnames(expr_pair) <- 1:p
  groups <- c('A', 'A', 'B', 'B', 'A', 'B', 'B', 'B', 'B', 'A', 'A', 'A', 
              'B', 'B', 'B', 'B', 'B', 'A', 'B', 'A', 'A', 'B', 'A', 'B', 'B')
  
  # Delete some of the genes in each group
  expr_pair[groups == "A", c(1, 5, 9)] <- 0
  expr_pair[groups == "B", c(9, 10, 23, 90:p)] <- 0
  
  network_inference <- function(x, ...) suppressWarnings(run_corr(x, ...))
  n_perm = 10
  lp_set = 2
  
  pathway_list <- as.character(1:20)
  # Check that seed can be set without error and returns equivalent results.
  results1 <- dnapath(expr_pair, pathway_list, groups,
                  network_inference, n_perm, lp_set, seed = 1)
  results2 <- dnapath(expr_pair, pathway_list, groups,
                  network_inference, n_perm, lp_set, seed = 1)
  expect_equal(unclass(results1), unclass(results2))
  
  # Check that methods work on 'dnapath' object
  results <- dnapath(expr_pair, pathway_list, groups,
                 network_inference, n_perm, lp_set)
  expect_is(results, "dnapath")
  expect_is(summary(results), "tbl")
  expect_is(summarize_genes(results), "tbl")
  expect_is(summarize_pathways(results), "tbl")
  expect_invisible(plot(results))
  
  # Check that methods work on 'dnapath_list' object
  # Create pathway list.
  pathway_list <- list(as.character(5:9), 
                       as.character(c(1, 5, 8, 13, 19)), 
                       as.character(c(2, 3, 4, 6:20)))
  results <- dnapath(expr_pair, pathway_list, groups,
                 network_inference, n_perm, lp_set)
  expect_true(results[[1]]$mean_expr[1, 1] == 0)
  expect_is(results, "dnapath_list")
  expect_is(summary(results), "tbl")
  expect_is(summarize_genes(results), "tbl")
  expect_is(summarize_pathways(results), "tbl")
  expect_invisible(plot(results[[1]]))
  
  # Check that gene names are unchanged after subsetting.
  genes_before_subset <- results[[1]]$genes[results[[1]]$pathway_genes]
  results <- results[1:2]
  genes_after_subset <- results[[1]]$genes[results[[1]]$pathway_genes]
  expect_equal(genes_before_subset, genes_after_subset)
  expect_equal(results[1][[1]]$genes, c("5", "6", "7", "8", "9"))
  expect_equal(results[2:3][[1]]$genes[results[2:3][[1]]$pathway_genes], 
               unique(as.character(pathway_list[[2]])))
  
  # Check that methods work on 'dnapath_list' object
  # Now use a named pathway_list.
  pathway_list <- list("a" = 16:49, 
                       "b" = c(1, 5, 5, 5, 5, 5, 8, 13, 19), 
                       "c" = c(2, 3, 4, 6:20))
  results <- dnapath(expr_pair, pathway_list, groups,
                 network_inference, n_perm, lp_set)
  expect_is(results, "dnapath_list")
  expect_equal(names(results), names(pathway_list))
  expect_equal(unname(sapply(results, function(x) x$pathway)), 
               names(pathway_list))
  
  expect_is(summary(results), "tbl")
  expect_is(summarize_genes(results), "tbl")
  expect_is(summarize_pathways(results), "tbl")
  expect_invisible(plot(results[[1]]))
  
  # Check that gene names are unchanged after subsetting.
  expect_equal(results[2:3][[1]]$genes[results[2:3][[1]]$pathway_genes], 
               unique(as.character(pathway_list[[2]])))
  
  genes_before_subset <- results[[1]]$genes[results[[1]]$pathway_genes]
  results <- results[1:2]
  genes_after_subset <- results[[1]]$genes[results[[1]]$pathway_genes]
  expect_equal(genes_before_subset, genes_after_subset)
})


test_that("dnapath method works when lp_set is a vector", {
  n <- c(10, 15)
  p <- 100
  expr_pair <- matrix(rnorm(p * (n[1] + n[2]), 10), ncol = p)
  colnames(expr_pair) <- 1:p
  groups <- c('A', 'A', 'B', 'B', 'A', 'B', 'B', 'B', 'B', 'A', 'A', 'A', 
              'B', 'B', 'B', 'B', 'B', 'A', 'B', 'A', 'A', 'B', 'A', 'B', 'B')
  
  # Delete some of the genes in each group
  expr_pair[groups == "A", c(1, 5, 9)] <- 0
  expr_pair[groups == "B", c(9, 10, 23, 90:p)] <- 0
  
  network_inference <- function(x, ...) suppressWarnings(run_corr(x, ...))
  n_perm = 10
  lp_set = c(0.5, 1, 2, Inf)
  
  pathway_list <- as.character(1:20)
  # Check that seed can be set without error and returns equivalent results.
  results1 <- dnapath(expr_pair, pathway_list, groups,
                  network_inference, n_perm, lp_set, seed = 1)
  results2 <- dnapath(expr_pair, pathway_list, groups,
                  network_inference, n_perm, lp_set, seed = 1)
  expect_equal(unclass(results1[[1]]), unclass(results2[[1]]))
  
  # Check that methods work on 'dnapath' object
  results <- dnapath(expr_pair, pathway_list, groups,
                 network_inference, n_perm, lp_set)
  expect_is(results[[1]], "dnapath")
  expect_is(summary(results[[2]]), "tbl")
  expect_is(summarize_genes(results[[3]]), "tbl")
  expect_is(summarize_pathways(results[[4]]), "tbl")
  
  # Check that methods work on 'dnapath_list' object
  # Now use a named pathway_list.
  pathway_list <- list("a" = 16:49, 
                       "b" = c(1, 5, 5, 5, 5, 5, 8, 13, 19), 
                       "c" = c(2, 3, 4, 6:20))
  results <- dnapath(expr_pair, pathway_list, groups,
                 network_inference, n_perm, lp_set)
  expect_is(results[[2]], "dnapath_list")
  expect_equal(names(results[[1]]), names(pathway_list))
  expect_equal(unname(sapply(results[[3]], function(x) x$pathway)), 
               names(pathway_list))
  
  expect_is(summary(results[[4]]), "tbl")
  expect_is(summarize_genes(results[[3]]), "tbl")
  expect_is(summarize_pathways(results[[2]]), "tbl")
  expect_invisible(plot(results[[1]][[1]]))
})



test_that("dnapath method gives warnings and errors.", {
  # Check for various warnings and errors.
  n <- c(3, 3)
  n_perm <- 9
  p <- 20
  lp_set <- 1
  network_inference <- run_corr
  expr_pair <- rbind(matrix(rnorm(p * n[1], 10), nrow = n[1], ncol = p),
                     matrix(rnorm(p * n[2], 10), nrow = n[2], ncol = p))
  colnames(expr_pair) <- 1:p
  pathway_list <- as.character(5:15)
  groups <- rep(1:2, n)
  expect_warning(dnapath(expr_pair, pathway_list, groups,
                     network_inference, n_perm = 100, lp_set))
  expect_error(dnapath(expr_pair, pathway_list, groups = NULL,
                   network_inference, n_perm, lp_set))
  expect_error(dnapath(expr_pair, pathway_list, groups = 1,
                   network_inference, n_perm, lp_set))
  expect_error(dnapath(expr_pair, pathway_list, groups = rep(1, sum(n)),
                   network_inference, n_perm, lp_set))
})
tgrimes/dnapath2 documentation built on May 21, 2020, 5:53 p.m.