tests/testthat/test-allele_frequencies.R

context("allele_frequencies")


test_that("calculate_allele_frequencies", {
  pop_size <- 100
  number_of_founders <- 2
  run_time <- 5
  morgan <- 1

  found <- c()
  for (r in 1:100) {
    vx <- create_population(pop_size, 2,
                            run_time, morgan, r)

    testthat::expect_true(verify_population(vx))

    for (i in 1:pop_size) {
      found <- rbind(found,
                     calc_allele_frequencies(vx[[i]],
                                             alleles =
                                               rep(0, number_of_founders * 2)
                                             )
      )
    }
  }

  v <- colMeans(found)
  testthat::expect_equal(v[[1]], 0.5, tolerance = 0.05)

  testthat::expect_equal(v[[2]], 0.5, tolerance = 0.05)

  found <- c()
  for (r in 1:100) {
    vx <- create_population(pop_size, 4,
                            run_time, morgan, r)

    testthat::expect_true(verify_population(vx))

    for (i in 1:pop_size) {
      found <- rbind(found,
                     calc_allele_frequencies(vx[[i]],
                                             alleles =
                                               rep(0, number_of_founders * 2)
                                             )
      )
    }
  }

  v <- mean(colMeans(found))
  expect_equal(v, 0.25, tolerance = 0.05)


  number_founders <- 20
  sourcepop <- create_population(
    pop_size = 10000,
    number_of_founders = number_founders,
    total_runtime = 1,
    morgan = 1,
    seed = 123)

  testthat::expect_true(verify_population(sourcepop))

  freq_output <- calculate_allele_frequencies(sourcepop,
                                              step_size = 0.01)


  require(dplyr)
  b <- freq_output %>%
    dplyr::group_by(as.factor(ancestor)) %>%
    dplyr::summarise("mean_freq" = mean(frequency))

  testthat::expect_equal(mean(b$mean_freq),
                         1 / number_founders,
                         tolerance = 0.01)
  testthat::expect_equal(sum(b$mean_freq), 1, tolerance = 0.01)

  number_founders <- 5
  sourcepop <- create_population(pop_size = 1000,
                                 number_of_founders = number_founders,
                                 total_runtime = 100,
                                 morgan = 1,
                                 seed = 123)

  testthat::expect_true(verify_population(sourcepop))

  freq_output <- calculate_allele_frequencies(sourcepop,
                                              step_size = 0.01)

  testthat::expect_equal(length(unique(freq_output$ancestor)),
                         number_founders)

  b <- freq_output %>%
    dplyr::group_by(as.factor(ancestor)) %>%
    dplyr::summarise("mean_freq" = mean(frequency))

  testthat::expect_equal(sum(b$mean_freq), 1, tolerance = 0.01)
  testthat::expect_equal(mean(b$mean_freq),
                         1 / number_founders,
                         tolerance = 0.01)

  number_founders <- 20
  sourcepop <- create_population(pop_size = 1000,
                                 number_of_founders = number_founders,
                                 total_runtime = 1,
                                 morgan = 1,
                                 seed = 123)

  testthat::expect_true(verify_population(sourcepop))
})
thijsjanzen/isoSIM documentation built on May 29, 2019, 10:39 a.m.