Skip to content

Commit

Permalink
change param names for q matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
dramanica committed Jul 19, 2024
1 parent 62c7234 commit 79c827e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 23 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Description: We provide a tidy grammar of population genetics, facilitating
License: GPL (>= 3)
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Depends:
R (>= 3.0.2),
dplyr,
Expand Down
29 changes: 16 additions & 13 deletions R/as_q_matrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ as_q_matrix <- function(x){
#' Takes a `q_matrix` object, which is a matrix, and returns a tidied tibble.
#'
#' @param x A Q matrix object (as returned by LEA::Q()).
#' @param gen_tbl An associated gen_tibble
#' @param data An associated tibble (e.g. a [`gen_tibble`]), with the individuals in the same order as the data used to
#' generate the Q matrix
#' @param ... not currently used
#' @return A tidied tibble
#' @export
tidy.q_matrix <- function(x, gen_tbl, ...){
tidy.q_matrix <- function(x, data, ...){
rlang::check_dots_empty()
q_tbl <- x %>%
tibble::as_tibble() %>%
dplyr::mutate(id = gen_tbl$id,
dplyr::mutate(id = data$id,
# @TODO we should get this from the grouped tibble, not hardcode it!
group = gen_tbl$population)
group = data$population)

q_tbl <- q_tbl %>% tidyr::pivot_longer(cols = dplyr::starts_with(".Q"),
names_to = "q", values_to = "percentage")
Expand All @@ -63,12 +64,13 @@ tidy.q_matrix <- function(x, gen_tbl, ...){
#' produces a list of tidied tibbles ready to plot.
#'
#' @param x the name of a directory containing .Q files
#' @param gen_tbl An associated gen_tibble
#' @param data An associated tibble (e.g. a [`gen_tibble`]), with the individuals in the same order as the data used to
#' generate the Q matrix
#' @returns a list of `q_matrix` objects to plot
#'
#' @export

read_q_matrix_list <- function(x, gen_tbl){
read_q_matrix_list <- function(x, data){

files <- list.files(x, pattern = "\\.Q$", full.names = TRUE)

Expand All @@ -82,34 +84,35 @@ read_q_matrix_list <- function(x, gen_tbl){
matrix_list <- matrix_list[order(sapply(matrix_list, ncol))]

# Tidy each
matrix_list <- lapply(matrix_list, function(x) tidy(x, gen_tbl = gen_tbl))
matrix_list <- lapply(matrix_list, function(x) tidy(x, gen_tbl = data))

matrix_list
}


#' Autoplots for `q_matrix` objects
#'
#' @param object A Q matrix object (as returned by `as_q_matrix`).
#' @param gen_tbl An associated gen_tibble
#' @param object A Q matrix object (as returned by [as_q_matrix()]).
#' @param data An associated tibble (e.g. a [`gen_tibble`]), with the individuals in the same order as the data used to
#' generate the Q matrix
#' @param annotate_group Boolean determining whether to annotate the plot with the
#' group information
#' @param ... not currently used.
#' @returns a barplot of individuals, coloured by ancestry proportion
#'
#' @export

autoplot.q_matrix <- function(object, gen_tbl = NULL, annotate_group = TRUE, ...){
autoplot.q_matrix <- function(object, data = NULL, annotate_group = TRUE, ...){

rlang::check_dots_empty()
K <- ncol(object)
if (is.null(gen_tbl)) {
if (is.null(data)) {
q_tbl <- as.data.frame(object)
q_tbl$id <- 1:nrow(q_tbl)
q_tbl <- q_tbl %>% tidyr::pivot_longer(cols = dplyr::starts_with(".Q"),
names_to = "q", values_to = "percentage")
} else {
q_tbl <- tidy(object, gen_tbl)
q_tbl <- tidy(object, data)
}
plt <- ggplot2::ggplot(q_tbl,
ggplot2::aes(x = .data$id,
Expand All @@ -121,7 +124,7 @@ autoplot.q_matrix <- function(object, gen_tbl = NULL, annotate_group = TRUE, ...
theme_distruct() +
scale_fill_distruct()
if (annotate_group){
if (is.null(gen_tbl)){
if (is.null(data)){
warning("no annotation possible if 'gen_tbl' is NULL")
} else {
plt <- plt + annotate_group_info(q_tbl)
Expand Down
9 changes: 5 additions & 4 deletions man/autoplot.q_matrix.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions man/read_q_matrix_list.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions man/tidy.q_matrix.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vignettes/a03_example_clustering_and_dapc.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ head(q_mat)

We can quickly plot it with
```{r}
autoplot(q_mat,gen_tbl = anole_gt)
autoplot(q_mat, data = anole_gt)
```

We can tidy our q matrix, and attach the population and id data from our original gen_tibble, returning it
Expand Down

0 comments on commit 79c827e

Please sign in to comment.