tests/testthat/test_cluster_manipulation.R

# Preparation -------------------------------------------------------------
library(beadplexr)
data("lplex")
df <- lplex[[1]]
.nrow_data <- nrow(df)
.parameters <- c("FSC-A", "SSC-A")

# Calculate distance ------------------------------------------------------
test_that("Distance to center is correct", {
  expect_equal(beadplexr:::calc_dist_to_centre(.x = 5, .c = 9), 4)
  expect_equal(beadplexr:::calc_dist_to_centre(.x = 9, .c = 9), 0)

  expect_equal(beadplexr:::calc_dist_to_centre(.x = c(5, 5), .c = c(9, 9)), 5.66, tolerance = .004)
  expect_equal(beadplexr:::calc_dist_to_centre(.x = c(5,9), .c = c(5,9)), 0)

  expect_equal(beadplexr:::calc_dist_to_centre(.x = c(5, 4, 6), .c = c(9, 8, 7)), 5.74, tolerance = .01)
  expect_equal(beadplexr:::calc_dist_to_centre(.x = c(5,9, 3), .c = c(5,9, 3)), 0)
  })

test_that("Calculation fails", {
  expect_error(beadplexr:::calc_dist_to_centre(.x = "A", .c = 9))
  expect_warning(beadplexr:::calc_dist_to_centre(.x = c(5, 4, 6), .c = c(9, 8)))
})

# Trim clusters -----------------------------------------------------------
df$population <- 1L
test_that("Clusters are trimmed", {
  expect_s3_class(trim_population(df, .parameter = .parameters, .column_name = "population", .trim = 0.1), "data.frame")
  expect_equal(nrow(trim_population(df, .parameter = .parameters, .column_name = "population", .trim = 0.1)), .nrow_data)

  expect_s3_class(trim_population(df, .parameter = .parameters, .column_name = "population", .trim = 0), "data.frame")
  expect_equal(nrow(trim_population(df, .parameter = .parameters, .column_name = "population", .trim = 0)), .nrow_data)

  expect_s3_class(trim_population(df, .parameter = .parameters[1], .column_name = "population", .trim = 0), "data.frame")
  expect_equal(nrow(trim_population(df, .parameter = .parameters[2], .column_name = "population", .trim = 0)), .nrow_data)
})

test_that("Clusters fail", {
  expect_error(trim_population(df, .parameter = .parameters, .column_name = "xxx", .trim = 0.1))
  expect_error(trim_population(df, .parameter = "xxx", .column_name = "population", .trim = 0.1))
})

Try the beadplexr package in your browser

Any scripts or data that you put into this service are public.

beadplexr documentation built on July 9, 2023, 5:59 p.m.