Skip to content

Commit

Permalink
add tidy.q_matrix test
Browse files Browse the repository at this point in the history
  • Loading branch information
eviecarter33 committed Jan 17, 2025
1 parent 6feb14c commit 96f2196
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions tests/testthat/test_q_matrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,32 @@ test_that("get_p_matrix returns correct p-matrix",{
expect_error(get_p_matrix(q_list, k=3, run=3),"Specified run is out of range")
})

test_that("tidying a q_matrix",{
# read a single .Q
Q_path <- system.file("/extdata/anolis/anolis_ld_run1.3.Q", package = "tidypopgen")
q_mat <- read.table(Q_path)
anolis_q_k3_mat <- q_matrix(q_mat)

# create gt and metadata
vcf_path <- system.file("/extdata/anolis/punctatus_t70_s10_n46_filtered.recode.vcf.gz", package = "tidypopgen")
anole_gt <- gen_tibble(vcf_path, quiet = TRUE, backingfile = tempfile("anolis_"))
pops_path <- system.file("/extdata/anolis/plot_order_punctatus_n46.csv", package = "tidypopgen")
pops <- readr::read_csv(pops_path, show_col_types = FALSE)
anole_gt <- anole_gt %>% mutate(id = gsub('punc_',"",.data$id,))
anole_gt <- anole_gt %>% mutate(population = pops$pop[match(pops$ID,.data$id)])

# tidy without group info
tidy_q <- tidy(anolis_q_k3_mat, anole_gt)
expect_false("group" %in% colnames(tidy_q))
expect_equal(tidy_q$id, rep(anole_gt$id, each = 3))

anole_gt <- anole_gt %>% group_by(population)

# tidy using group from gen_tibble
tidy_q <- tidy(anolis_q_k3_mat, anole_gt)
expect_true("group" %in% colnames(tidy_q))
expect_equal(tidy_q$group, rep(anole_gt$population, each = 3))
expect_equal(tidy_q$id, rep(anole_gt$id, each = 3))
})


0 comments on commit 96f2196

Please sign in to comment.