tests/testthat/test_cumfrac_rpc.R

context("Calculate reads per cell barcode cumulative fraction")

test_that("cumulative fraction is calculated correctly", {

  # create reads per cell barcode input ---------------------------------------

  # randomly generate read counts and cell barcodes
  set.seed(42)
  reads <- sort(sample(1:20000, 10),decreasing = TRUE)
  bcs <- unlist(lapply(1:10, FUN = function(x){

    paste(sample(c("A", "T", "C", "G"), size = 12, replace = TRUE),
          collapse = "")

  }))

  # combine into data.frame
  reads_per_bc <- data.frame(reads, bcs)
  colnames(reads_per_bc) <- c("V1", "V2")

  # calculate cumulative fraction ---------------------------------------------

  cumfrac <- cumsum(reads) / max(cumsum(reads))

  # test cumfrac_rpc() from 2 column data.frame -------------------------------

  # expected output
  expect_out <- cbind(reads_per_bc, cumfrac)
  colnames(expect_out) <- c("reads", "cell_barcode", "cumfrac")

  # run test
  expect_equal(cumfrac_rpc(reads_per_bc), expected = expect_out)

  # test cumfrac_rpc() from >2 column data.frame ------------------------------

  # add additional col to input data
  reads_per_bc$V3 <- 1:10

  # expected output
  expect_out <- cbind(reads_per_bc, cumfrac)
  colnames(expect_out) <- c("reads", "cell_barcode", "V3", "cumfrac")

  # run test
  expect_equal(cumfrac_rpc(reads_per_bc), expected = expect_out)

})
argschwind/dropseqr documentation built on May 23, 2019, 4:24 p.m.