tests/testthat/test_graphs.R

## test headerKable
test_that("headerKable works properly", {


  tab <- data.frame(rbind(c(20,10,10,5,5,5,5,10,10), c(20,10,10,5,5,5,5,10,10)))
  dimnames(tab) <- list(c("A", "B"), c("total", "C","D", "W", "X", "Y", "Z", "F", "M"))
  labs <- c("age", "height", "IQ", "gender")
  levs <- c(2,3,2,1)
  levsWrong <- c(2,2,3,3)

  ## test with header
  expect_is(headerKable(tab, labels=labs, levels=levs, title="test"), "knitr_kable")

  ## test without header and defaults
  expect_is(headerKable(tab, labels=labs, levels=levs, title="test"), "knitr_kable")

  ## test mismatched 'levels' and 'labels'
  expect_error(headerKable(tab, labels=labs, levels=levs[1], title="test"),
               "parameters 'labels' and 'levels' must be of same length")

  ## test incorrect number of levels
  expect_error(headerKable(tab, labels=labs, levels=levsWrong, title="test"),
               "number of levels in 'levels' does not match number of levels in 'tabley'")

})


## runDEG test code
test_that("runDEG handles various input", {

  ## test basic functionality objects: voomTest, voomIndices, voomCoefs, voomLabs, voomPheno
  load(paste0(system.file(package="RNASeqUtilities"), "/extdata/voomNormalisedData.Rda"))

  expect_is(runDEG(voomTest, voomCoefs, voomLabs, doRandomEffect=FALSE, blocker=NULL), "list")
  ##expect_is(runDEG(voomTest, voomCoefs, voomLabs, doRandomEffect=TRUE, blocker=voomPheno$SID,
  ##                 doRead=FALSE, doWrite=TRUE, rdaPath="~/tmp.rda"), "list")
  ##expect_is(runDEG(voomTest, voomCoefs, voomLabs, doRandomEffect=TRUE, blocker=voomPheno$SID,
  ##                 doRead=TRUE, doWrite=FALSE, rdaPath="~/tmp.rda"), "list")

  ## test error checking
  expect_error(runDEG(voomTest, NULL))
  expect_error(runDEG(NULL, voomCoefs))
  expect_error(runDEG(voomTest, voomCoefs, voomLabs, doRandomEffect=TRUE, blocker=NULL))
  expect_error(runDEG(voomTest, voomCoefs, voomLabs, doRandomEffect=TRUE, blocker=1:50))
  expect_error(runDEG(voomTest, c(voomCoefs, "Fred")))
  expect_error(runDEG(voomTest, voomCoefs, labels=voomCoefs[-1]))
  expect_error(runDEG(voomTest, voomCoefs, labels=voomCoefs, doWrite=TRUE))
  expect_error(runDEG(voomTest, voomCoefs, labels=voomCoefs, doRead=TRUE))
  expect_error(runDEG(voomTest, voomCoefs, labels=voomCoefs, doRead=TRUE, rdaPath="Fred"))

})


## runGSEA test code
test_that("runGSEA handles various input", {

  ## test basic functionality objects: voomTest, voomIndices, voomCoefs, voomLabs
  load(paste0(system.file(package="RNASeqUtilities"), "/extdata/voomNormalisedData.Rda"))

  expect_is(runGSEA(voomTest, voomIndices, voomCoefs, voomLabs), "list")
  expect_is(runGSEA(voomTest, voomIndices, voomCoefs, voomLabs, onlySigs=TRUE, FDRCut=0.5), "list")

  ## test error checking
  expect_error(runGSEA(voomTest, NULL, voomCoefs))
  expect_error(runGSEA(NULL, voomIndices, voomCoefs))
  expect_error(runGSEA(voomTest, voomIndices, NULL))
  expect_error(runGSEA(voomTest, voomIndices, c(voomCoefs, "Fred")))
  expect_error(runGSEA(voomTest, voomIndices, voomCoefs, labels=voomCoefs[-1]))
  expect_error(runGSEA(voomTest, voomIndices, voomCoefs, onlySigs="Fred"))
  expect_error(runGSEA(voomTest, voomIndices, voomCoefs, onlySigs=TRUE, FDRCut="Fred"))
  expect_error(runGSEA(voomTest, voomIndices, voomCoefs, onlySigs=TRUE, FDRCut=-1))
  expect_error(runGSEA(voomTest, voomIndices, voomCoefs, onlySigs=TRUE, FDRCut=2))
})


## createDEGGRaphs test code
test_that("createDEGGraphs handles various input", {

  ## test basic functionality
  set.seed(1)
  dat <- data.frame(matrix(runif(600, 0, 1), ncol=6))
  colnames(dat) <-  c("logFC", "AveExpr", "t", "P.Value", "adj.P.Val", "B")

  expect_is(createDEGGraphs(dat), "list")

  ## test for bad input
  colnames(dat)[1] <- "fred"
  expect_error(createDEGGraphs(dat))

  ## test for all significant FDR results (this incorrectly results in all black points)
  colnames(dat)[1] <- "logFC"
  dat$adj.P.Val <- rep(.05, 100)
  expect_is(createDEGGraphs(dat), "list")

})

## countSampleSizes test code
test_that("countSampleSizes handles various input", {

  dat <- data.frame(treat=rep(c("A", "B"), each=20), type=rep(c("C", "D"), 20),
                    site=rep(c("W", "X", "Y", "Z"), 10),
                    gender=rep(c("M", "F", "F", "M"), 10))

   truth <- data.frame(rbind(c(20,10,10,5,5,5,5,10,10), c(20,10,10,5,5,5,5,10,10)))
   dimnames(truth) <- list(c("A", "B"), c("total", "C","D", "W", "X", "Y", "Z", "F", "M"))

   ## test basic functionality
   expect_equal(countSampleSizes(dat, ref="treat", factors=c("type", "site", "gender")), truth)
   expect_equal(countSampleSizes(dat, ref=1, factors=2:4), truth)

   ## test bad inputs
   expect_error(countSampleSizes(dat, ref="fred", factors=c("type", "site", "gender")),
                "ERROR: fred not a column name of 'dat'")

   expect_error(countSampleSizes(dat, ref="treat", factors=c("fred", "site", "gender")),
                "ERROR: There are elements of 'factors' that are not a column name of 'dat'")
   expect_error(countSampleSizes(dat, ref=10, factors=2:4),
                "ERROR: 'ref' is not a column index of 'dat'")
   expect_error(countSampleSizes(dat, ref=1, factors=2:10),
             "ERROR: There are elements of 'factors' that are not a column indices of 'dat'")
   expect_warning(countSampleSizes(dat, ref="treat", factors=c("treat","type","site","gender")),
                "treat is an element of 'factors'. Is this what you want?")

})
carlmurie/RNASeqUtilities documentation built on Sept. 1, 2019, 11:54 p.m.