Skip to content

Commit

Permalink
new function
Browse files Browse the repository at this point in the history
  • Loading branch information
rscherrer committed Jun 9, 2021
1 parent f5e60e8 commit e76c35e
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ export(read_loci)
export(read_parameters)
export(read_population)
export(read_speciome)
export(read_traits)
export(rm_plural_colnames)
importFrom(magrittr,"%>%")
45 changes: 45 additions & 0 deletions R/read_traits.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#' Read trait-wise data
#'
#' Wrapper around \code{read_speciome} for variables with one value per
#' trait per time point.
#'
#' @param root Path to the simulation folder
#' @param variables Vector of names of variable to read (will be interpreted using \code{interpret_variable_names})
#'
#' @return A tibble containing the simulation data
#'
#' @note The function will read "time.dat" as an extra variable, no need to
#' supply it.
#'
#' @seealso \code{read_speciome}
#'
#' @examples
#'
#' root <- system.file("extdata", "sim-example", package = "speciomer")
#' read_traits(root, "trait_Fst")
#'
#' @export

read_traits <- function(root, variables) {

# Add a trait prefix to the variable names if needed
variables <- interpret_variable_names(variables, type = "trait")

# Parametrization
ncols <- rep(1, length(variables))
variables <- c("time", variables)
ncols <- c(-3, ncols)

# Read trait-wise data
data <- read_speciome(root, variables, ncols = ncols)

# Remove the prefix "trait", now redundant
colnames(data) <- stringr::str_remove(colnames(data), "trait_")

# Add a trait identifier
data <- data %>%
tibble::add_column(trait = rep(seq(3), nrow(data) / 3), .after = "time")

return(data)

}
33 changes: 33 additions & 0 deletions man/read_traits.Rd

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

9 changes: 9 additions & 0 deletions tests/testthat/test_read_traits.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root <- system.file("extdata", "sim-example", package = "speciomer")

test_that("Read trait-wise data", {

data <- read_traits(root, "Cst")
ntimes <- length(read_binary(paste0(root, "/time.dat")))
expect_equal(nrow(data), 3 * ntimes)

})

0 comments on commit e76c35e

Please sign in to comment.